Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. • Add a back edge for every skipped edge. A B D E C 60
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. A A B D E C 61
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. A A B D E B C 62
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. A A B D E B C C 63
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. A A B E B D C C D 64
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. • Add a back edge for every skipped edge. A A B E B D C C D 65
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. • Add a back edge for every skipped edge. A A B E B D C C D 66
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. • Add a back edge for every skipped edge. A A B B D E C C E D 67
Depth First Spanning Tree • The steps taken by DFS can be illustrated as a (directed) spanning tree. • Add a tree edge for every graph edge taken by DFS. • Add a back edge for every skipped edge. A A B B D E C C E D 68
Identifying Articulation Points (1) • If the root of the DFS spanning tree has two outgoing tree edges, the root is an articulation point. C A G B B A C D F C is an D articulation G E point. E F 69
Identifying Articulation Points (1) • If the root of the DFS spanning tree has two outgoing tree edges, the root is an articulation point. A • Depends on which vertex we start DFS from. B A B C C D G D F A is not an E G E articulation point. F 70
Identifying Articulation Points (2) • Any non-root vertex v is an articulation point iff • v has a child w such that there is no back-edge from the subtree below w A to any ancestor of v . B A B C D C G D F C is an articulation E E G point because F of G. 71
Identifying Articulation Points (2) • Any non-root vertex v is an articulation point iff • v has a child w such that there is no back-edge from the subtree below w A to any ancestor of v. B A B C C D G D F D is an articulation E G E point because F of E. 72
Preorder Numbers • Assign numbers to each vertex in the order in which they are visited by DFS. • For every tree edge (u,v): Num(u) < Num(v) A 1 • For every back edge (u,v): Num(u) > Num(v) B 2 v Num(v) C A 1 3 B 2 C 3 G D 7 D 4 4 E 5 E F 6 5 G 7 F 6 73
Low numbers • For each vertex, find the lowest numbered vertex that is reachable by following a path that contains A at most one back edge. 1 B 2 v Num(v) Low(v) C A 1 1 3 B 2 C 3 G D 7 4 D 4 E 5 E F 6 5 G 7 F 6 74
Low numbers • For each vertex, find the lowest numbered vertex that is reachable by following a path that contains A at most one back edge. 1 B 2 v Num(v) Low(v) C A 1 1 3 1 B 2 C 3 1 G D 7 4 D 4 1 E 5 E F 6 5 G 7 F 6 75
Low numbers • For each vertex, find the lowest numbered vertex that is reachable by following a path that contains A at most one back edge. 1 B 2 v Num(v) Low(v) C A 1 1 3 1 B 2 C 3 1 G D 7 4 D 4 1 E 5 4 E 4 F 6 5 7 G 7 F 6 76
Identifying Articulation Points • Any non-root vertex v is an articulation point iff • v has a child w such that there is no A 1 back-edge from the subtree below w to any ancestor of v. B 2 v Num(v) Low(v) C A 1 1 3 1 B 2 C 3 1 G D 7 4 D 4 1 E 5 4 E 4 F 6 5 7 G 7 F 6 77
Identifying Articulation Points • Any non-root vertex v is an articulation point iff • v has a child w such that Low(w) ≥ Num(v) A 1 B 2 v Num(v) Low(v) C A 1 1 3 1 B 2 C 3 1 G 7 D 4 D 4 1 E 5 4 E 4 F 6 5 7 G 7 F 6 78
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 Low(v) = Num(u); C } 3 for all tree edges (v,u) { G compute_low(u); D 7 4 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E } 5 } F 6 79
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 Low(v) = Num(u); C } 3 for all tree edges (v,u) { G compute_low(u); D 7 4 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E } 5 } F 6 80
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 for all tree edges (v,u) { G compute_low(u); D 7 4 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E } 5 } F 6 81
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { G compute_low(u); D 7 4 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E } 5 } F 6 82
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E } 5 } F 6 83
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 5 } 5 } F 6 84
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 5 } 5 } F 6 4 85
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 4 } 5 } F 6 4 86
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 3 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 4 } 5 } F 6 4 87
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 2 Low(v) = Num(u); C } 1 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 4 } 5 } F 6 4 88
Computing Low Numbers Recursively compute_low(v) { A Low(v) = Num(v) 1 1 for all back edges (v,u) { B if ( Num(u) < Low(v) ) 2 1 Low(v) = Num(u); C } 1 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 4 } 5 } F 6 4 89
Computing Low Numbers Time to compute preorder numbers: O(|V| +|E|) compute_low(v) { A Low(v) = Num(v) 1 1 Time to compute low numbers: O(|V|+|E|) for all back edges (v,u) { Time to check for articulation points: O(|V|+|E|) B if ( Num(u) < Low(v) ) 2 1 Low(v) = Num(u); Total: O(|V|+|E|) C } 1 3 for all tree edges (v,u) { 7 G compute_low(u); D 7 4 1 if ( Low(u) < Low(v) ) Low(v) = Low(u) ; E 4 } 5 } F 6 4 90
Contents • Applications of DFS • Euler Circuits • Biconnectivity in Undirected Graphs. • Finding Strongly Connected Components for Directed Graphs. 91
Connectivity in Directed Graphs • A directed graph is weakly connected if there is an undirected path from every vertex to every other vertex. weakly connected graph 92
Strongly Connected Graphs • A directed graph is strongly connected if there is a path from every vertex to every other vertex. v Weakly connected, but not strongly connected (no other vertex can be reached from v). 93
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 94
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 95
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 96
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 97
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 98
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 99
Testing if a Graph is Strongly Connected • Run DFS to see if all vertices are reachable from some start node s. s • Reverse direction of edges and run DFS again. 100
Recommend
More recommend