1、Graph Traversals,Algorithm : Design & Analysis 12,In the last class,Dynamic Equivalence Relation Implementing Dynamic Set by Union-Find Straight Union-Find Making Shorter Tree by Weighted Union Compressing Path by Compressing-Find Amortized Analysis of wUnion-cFind,Graph Traversals,Depth-First and B
2、readth-First Search Finding Connected Components General Depth-First Search Skeleton Depth-First Search Trace,Graph Traversal: an Example,G,F,E,D,C,B,A,Starting node,G,F,E,D,C,B,A,Starting node,Depth-First Search,Breadth-First Search,Not reachable,Not reachable,Edges only “checked”,Outline of Depth-
3、First Search,dfs(G,v)Mark v as “discovered”.For each vertex w that edge vw is in G:If w is undiscovered:dfs(G,w)Otherwise:“Check” vw without visiting w.Mark v as “finished”.,That is: exploring vw, visiting w, exploring from there as much as possible, and backtrack from w to v.,A vertex must be exact
4、 one of three different status:undiscovereddiscovered but not finishedfinished,Outline of Breadth First Search,Bfs(G,s)Mark s as “discovered”;enqueue(pending,s);while (pending is nonempty)dequeue(pending, v);For each vertex w that edge vw is in G:If w is “undiscovered” Mark w as “discovered” and enq
5、ueue(pending, w)Mark v as “finished”;,Graph as Group of Linked-List,adjVertices,1 2 3 4 5 6 7,Undirected graph as a symmetric directed graph,Input: a symmetric digraph G, with n nodes and 2m edges(interpreted as an undirected graph), implemented as a array adjVerteces1,n of adjacency lists. Output:
6、an array cc1n of component number for each node vi void connectedComponents(Intlist adjVertices, int n, int cc)/This is a wraper procedureint color=new intn+1;int v;for (v=1; vn; v+)if (colorv=white)ccDFS(adjVertices, color, v, v, cc);return,Finding Connected Components,Depth-first search,void ccDFS
7、(IntList adjVertices, int color, int v, int ccNum, int cc)/v as the code of current connected componentint w;IntList remAdj;colorv=gray;ccv=ccNum;remAdj=adjVerticesv;while (remAdjnil)w=first(remAdj);if (color=white)ccDFS(adjVertices, color, w, ccNum, cc);remAdj=rest(remAdj);colorv=black;return,ccDFS
8、: the procedure,The elements of remAdj are neighbors of v,Processing the next neighbor, if existing, another depth-first search to be incurred,v finished,Analysis of CC Algorithm,connectedComponents, the wrapper Linear in n (color array initialization+for loop on adjVertices ) ccDFS, the depth-first
9、 searcher In one execution of ccDFS on v, the number of instructions(rest(remAdj) executed is proportional to the size of adjVerticesv. Note: (size of adjVerticesv) is 2m, and the adjacency lists are traveresed only once. So, the complexity is in (m+n) Extra space requirements: color array activatio
10、n frame stack for recursion,Depth-First Search Trees,G,F,E,D,C,B,A,Root of tree 1,Root of tree 2,T.E: tree edge B.E: back edge D.E: descendantedge C.E: cross edge,T.E,T.E,T.E,T.E,T.E,C.E,C.E,D.E,B.E,B.E,C.E,C.E,B.E,DFS forest=(DFS tree1), (DFS tree2),A finished vertex is never revisited, such as C,V
11、isits On a Vertex,Classification for the visits on a vertex First visit(exploring): status: whitegray (Possibly) multi-visits by backtracking to: status keeps gray Last visit(no more branch-finished): status: grayblack Different operations can be done on the vertex or (selected) incident edges durin
12、g the different visits on a specific vertex,Depth-First Search: Generalized,Input: Array adjVertices for graph G Output: Return value depends on application. int dfsSweep(IntList adjVertices,int n, )int ans;For each vertex v of G, in some orderif (colorv=white)int vAns=dfs(adjVertices, color, v, );/
13、 Continue loopreturn ans;,Depth-First Search: Generalized,int dfs(IntList adjVertices, int color, int v, )int w;IntList remAdj;int ans;colorv=gray;remAdj=adjVerticesv;while (remAdjnil)w=first(remAdj);if (colorw=white)int wAns=dfs(adjVertices, color, w, );elseremAdj=rest(remAdj);colorv=black;return a
14、ns;,If partial search is used for a application, tests for termination may be inserted here.,Specialized for connected components: parameter added preorder processing inserted ccv=ccNum,Breadth-First Search: the Skeleton,Input: Array adjVertices for graph G Output: Return value depends on applicatio
15、n. void bfsSweep(IntList adjVertices,int n, )int ans;For each vertex v of G, in some orderif (colorv=white)void bfs(adjVertices, color, v, );/ Continue loopreturn;,Breadth-First Search: the Skeleton,void bfs(IntList adjVertices, int color, int v, )int w; IntList remAdj; Queue pending;colorv=gray; en
16、queue(pending, v);while (pending is nonempty)w=dequeue(pending); remAdj=adjVerticesw;while (remAdjnil)x=first(remAdj);if (colorx=white)colorx=gray; enqueue(pending, x);remAdj=rest(remAdj);colorw=black;return ;,DFS vs. BFS Search,Processing Opportunities for a node Depth-first: 2 At discovering At fi
17、nishing Breadth-first: only 1, when de-queued At the second processing opportunity for the DFS, the algorithm can make use of information about the descendants of the corrent node.,Time Relation on Changing Color,Keeping the order in which vertices are encountered for the first or last time A global
18、 interger time: 0 as the initial value, incremented with each color changing for any vertex, and the final value is 2n Array discoverTime: the i th element records the time vertex vi turns into gray Array finishTime: the i th element records the time vertex vi turns into black The active interval fo
19、r vertex v, denoted as active(v), is the duration while v is gray, that is: discoverTimev, , finishTimev,Depth-First Search Trace,General DFS skeleton modified to compute discovery and finishing times and “construct” the depth-first search forest. int dfsTraceSweep(IntList adjVertices,int n, int dis
20、coverTime, int finishTime, int parent)int ans; int time=0For each vertex v of G, in some orderif (colorv=white)parentv=-1int vAns=dfsTrace(adnVertices, color, v, discoverTime, finishTime, parent, time );/ Continue loopreturn ans;,Depth-First Search Trace,int dfsTrace(intList adjVertices, int color,
21、int v, int discoverTime, int finishTime, int parent int time)int w; IntList remAdj; int ans;colorv=gray; time+; discoverTimev=time;remAdj=adjVerticesv;while (remAdjnil)w=first(remAdj);if (colorw=white)parentw=v;int wAns=dfsTrace(adjVertices, color, w, discoverTime, finishTime, parent, time);else rem
22、Adj=rest(remAdj);time+; finishTimev=time; colorv=black;return ans;,Edge Classification and the Active Intervals,G,F,E,D,C,B,A,T.E,T.E,T.E,T.E,T.E,C.E,C.E,D.E,B.E,B.E,C.E,C.E,C.E,1/10,8/9,3/4,2/7,5/6,11/14,12/13,Time,1 2 3 4 5 6 7 8 9 10 11 12 13 14,A,F,B,D,C,G,E,The relations are summarized in the n
23、ext frame,Properties about Active Intervals(1),If w is a descendant of v in the DFS forest, then active(w)active(v), and the inclusion is proper if wv. Proof: Define a partial order : wv iff. w is a proper descentants of v in its DFS tree. The proof is by induction on ) If v is minimal. The only des
24、cendant of v is itself. Trivial. Assume that for all xv, if w is a descendant of x, then active(w)active(x). Let w is any proper descendant of v in the DFS tree, there must be some x such that vx is a tree edge on the tree path to w, so w is a descendant of x. According to dfsTrace, we have active(x
25、)active(v), by inductive hypothesis, active(w)active(v),Properties about Active Intervals(2),If v and w have no ancestor/descendant relationship in the DFS forest, then their active intervals are disjoint. Proof: If v and w are in different DFS tree, it is triavially true, since the trees are proces
26、sed one by one. Otherwise, there must be a vertex c, satisfying that there are tree paths c to v, and c to w, without edges in common. Let the leading edges of the two tree path are cy, cz, respectively. According to dfsTrace, active(y) and active(z) are disjoint. We have active(v)active(y), active(
27、w)active(z). So, active(v) and active(w) are disjoint.,Properties about Active Intervals(3),If active(w)active(v), then w is a descendant of v. And if active(w)active(v), then w is a proper descendant of v. That is: w is discovered while v is active. Proof: If w is not a descendant of v, there are t
28、wo cases: v is a proper descendant of w, then active(v)active(w), so, it is impossible that active(w)active(v), contradiction. There is no ancestor/descendant relationship between v and w, then active(w) and active(v) are disjoint, contradiction.,Properties about Active Intervals(4),If edge vwEG, th
29、en vw is a cross edge iff. active(w) entirely precedes active(v). vw is a descendant edge iff. there is some third vertex x, such that active(w)active(x)active(v), vw is a tree edge iff. active(w)active(v), and there is no third vertex x, such that active(w)active(x) active(v), vw is a back edge iff
30、. active(v)active(w),DFS Tree Path,White Path Theorem w is a descendant of v in a DFS tree iff. at the time v is discovered(just to be changing color into gray), there is a path in G from v to w consisting entirely of white vertices.,v,x1,xi,w,A white path from v to w,P2,P1,Proof of White Path Theor
31、em,Proof All the vertices in the path are descendants of v. by induction on the length k of a white path from v to w. When k=0, v=w. For k0, let P=(v, x1,x2,xk=w). There must be some vertex on P which is discovered during the active interval of v, e.g. x1, Let xi is earliest discovered among them. Divide P into P1 from v to xi, and P2 from xi to w. P2 is a white path with length less than k, so, by inductive hypothesis, w is a descendant of xi. Note: active(xi)active(v), so xi is a descendant of v. By transitivity, w is a descendant of v.,Home Assignments,7.12 7.14 7.15 7.16,