IS 320 Introduction to lgorithms all 2014 Lecture 15 epth irst Search 1
Traversing raphs Systematic search of every edge and vertex of graph (directed or undirected) fficiency: each edge is visited no more than twice orrectness: no vertex is missed IS320, 03, Lec10, Liao 2
epth-irst Search (S) S() 1. for each vertex u V[] 2. do color[u] WHIT 3. time 0 4. for each vertex u V[] 5. do if color[u] = WHIT 6. then S-Visit(u) S-Visit(u) 1. color[u] RY 2. d[u] time // record discovery time 3. time time +1 // global time increase by one 4. for each v dj[u] // explore all adjacent nodes of u 5. do if color[v] = WHIT 6. then S-Visit(v) 7. color[u] LK // finish with u, and mark it black 8. f[u] time // record finishing time 9. time time + 1 IS320, 03, Lec10, Liao 3
S Running time or each vertex u, S-visit is called uring S-visit(u), loop on line 4-6 is executed dj[u] times. u V dj[u] = Θ() Therefore, the total running time of S is Θ(V+) IS320, 03, Lec10, Liao 4
S S: Parenthesis Theorem Represent discovery of u with left parenthesis (u Represent finishing u by right parenthesis u) The history of discoveries and finishings makes a well-formed expression, i.e., the parentheses are properly nested. Or more formally, for any two vertices u and v, exactly one of the following three conditions holds: Interval [d[u], f[u]] and [d[v], f[v]] are entirely disjoint Interval [d[u], f[u]] is contained entirely within [d[v], f[v]], and u is a descendant of v in the depth-first search tree Interval [d[v], f[v]] is contained entirely within [d[u], f[u]], and v is a descendant of u in the depth-first search tree. IS320, 03, Lec10, Liao 5
Parenthesis theorem: example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ( ( ( ) ( ) ) ( ) ) ( ( ) ) IS320, 03, Lec10, Liao 6
White-path theorem: In any S of a graph, a vertex w is a descendant of a vertex v in a depth-first search tree if and only if, at the time vertex v is discovered (just before coloring it gray), there is a path in from v to w consisting entirely of white vertices. IS320, 03, Lec10, Liao 7
S edge classification epth-first search tree: The edges that lead to undiscovered vertices during a depth-first search of a digraph form a rooted tree. epth-first search forest: If not all vertices are reachable from the starting vertex, a complete traversal of partitions the vertices into several trees, which together are called depth-first search forest. Tree edge: (RY to WHIT) form spanning forest with no cycles ack edge: (RY to RY) w is ancestor of v, then vw is a back edge orward edge: (RY to LK) ross edge: (RY to LK) IS320, 03, Lec10, Liao 8
White-Path Theorem: In any S of a graph, a vertex w is a descendant of a vertex v in a depth-first search tree if and only if, at the time vertex v is discovered (just before coloring it gray), there is a path in from v to w consisting entirely of white vertices. Proof: - (Only if) If w is a descendant of v, by the parenthesis theorem, the path of tree edges from v to w is a white path. - (If) y induction on k, the length of a white path from v to w. v x 1 xi w IS320, 03, Lec10, Liao 9
Traversing raphs back back cross IS320, 03, Lec10, Liao 10
Topological sort irected cyclic raph () Topological order of a dag = (V, ) is a linear ordering of all vertices If contains an edge (u,v), then u appears before v in the ordering TopologicalSort() 1. all S() to compute finishing times f[u] for each vertex u. 2. s soon as each vertex is finished, insert it onto the front of a linked list 3. Return the linked list of vertices. IS320, 03, Lec10, Liao 11
Topological sort Theorem: directed graph is acyclic iif a S of yields no back edges. Proof: - If S() yields a back edge (u,v), then there is a cycle in. (why?) - Suppose has a cycle c. dge (u,v) is part of c, and v is the first vertex on c discovered by S(). ll other vertices on c form a white path from v to u. y White path theorem, u is a descendant of v and edge (u,v) becomes a back edge. (ecause S-visit(v) won t return to v until all reachable vertices are reached. When it reaches u, (u,v) is a back edge.) u v IS320, 03, Lec10, Liao 12
Topological sort Running time: S part: O(V+) Insert each of the V vertices onto the front of the linked list: O(V) Total time: O(V+) orrectness: If is a dag, then for any edge (u,v) f[u] > f[v] when (u,v) is explored, u is RY. 1. v is also RY. Then (u, v) is a back edge. This means is not a dag. 2. v is WHIT. Then v is a descendant of u. Therefore v is finished before u, namely f[v] < f[u]. 3. v is LK. Then v is already finished, i.e., f[v] < f[u]. IS320, 03, Lec10, Liao 13
Topological sort xample 11/16 undershorts socks 17/18 watch 9/10 12/15 pants shoes 13/14 shirt 1/8 6/7 belt tie 2/5 jacket 3/4 socks undershorts pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 IS320, 03, Lec10, Liao 14
Strongly connected component efinition: scc of a digraph = (V,) is a maximal set of vertices U ( V) such that every pair of vertices are reachable from each other in (why not just in U?). Q: is it possible that u and v are in a scc and there are edges (u,x) and (x,v), but x is not in the same scc? S() 1. all S() to compute finishing times f[u] for each vertex u; push u onto finishstack when it is finished. 2. ompute T 3. all S( T ), but in the main loop of S, consider the vertices in order of decreasing f[u] (as computed in step 1) 4. Output vertices of each tree (in the depth-first forest of step 3) as a separate S IS320, 03, Lec10, Liao 15
Strongly connected component xercise: ondensation graph (or called component graph) is a dag. IS320, 03, Lec10, Liao 16
Strongly connected component 1. S() tree1 tree2 tree2 tree1 finishstack scc1 scc2 scc3 2. ompute T 3. S( T ) following finish stack 4. ach tree yielded by S( T ) is a S scc2 scc3 scc1 IS320, 03, Lec10, Liao 17
Strongly connected component Running time of S(): S(): O(V+) ompute T : O(V+) (xercise) all S( T ): O(V+) Total time: O(V+) Space usage: O(V+) using adjacent list representation. orrectness: 1. each tree can contain one or more Ss, never contains partial S. (S push a topological ordering of condensation graph of onto the finishstack) 2. In S( T ), each tree can contain only one S, because there is no edge to go to the next S. (all edges in condensation graph of are reversed) IS320, 03, Lec10, Liao 18