data structures in java
play

Data Structures in Java Lecture 19: Applications of DFS 11/30/2015 - PowerPoint PPT Presentation

Data Structures in Java Lecture 19: Applications of DFS 11/30/2015 Daniel Bauer 1 Contents Applications of DFS Euler Circuits Biconnectivity in Undirected Graphs. Finding Strongly Connected Components for Directed Graphs. 2


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. Contents • Applications of DFS • Euler Circuits • Biconnectivity in Undirected Graphs. • Finding Strongly Connected Components for Directed Graphs. 91

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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