1 / 46

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity. Winter 2001 Lecture 10 Instructor: Paul Beame. DFS(v) for a directed graph. 1. 2. 10. 3. 11. 12. 4. 8. 13. 5. 9. 6. 7. DFS(v). 1. tree edges. forward edges. 2. 10. 3. 11. 12. back edges. 4. 8. 13. 5. 9.

ahubbs
Télécharger la présentation

CSE 417: Algorithms and Computational Complexity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame

  2. DFS(v) for a directed graph 1 2 10 3 11 12 4 8 13 5 9 6 7

  3. DFS(v) 1 tree edges forward edges 2 10 3 11 12 back edges 4 8 13 5 9 NO  cross edges 6  cross edges 7

  4. Strongly-connected components • In directed graph if there is a path from a to b there might not be one from b to a • a and b arestrongly connectediff there is a path in both directions (i.e. a directed cycle containing both a and b • Breaks graph into components

  5. Strongly-connected components 1 2 10 3 11 12 4 8 13 5 9 6 7

  6. Uses for SCC’s • Optimizing compilers need to find loops, which are SCC’s in the program flow graph. • If (u,v) means process u is waiting for process v, SCC’s show deadlocks.

  7. Directed Acyclic Graphs • If we collapse each SCC to a single vertex we get a directed graph with no cycles • a directed acyclic graph or DAG • Many problems on directed graphs can be solved as follows: • Compute SCC’s and resulting DAG • Do one computation on each SCC • Do another computation on the overall DAG

  8. Simple SCC Algorithm • u,v in same SCC iff there are paths u  v & v  u • DFS from every u, v: O(nm) = O(n3)

  9. Better method • Can compute all the SCC’s while doing a single DFS! O(n+m) time • We won’t do the full algorithm but will give some ideas

  10. Definition The root of an SCC is the first vertex in it visited by DFS. Equivalently, the root is the vertex in the SCC with the smallest number in DFS ordering.

  11. Subgoal • All members of an SCC are descendants of its root. • Can we identify some root? • How about the root of the first SCC completely explored by DFS? • Key idea: no exit from first SCC(first SCC is leftmost “leaf” in collapsed DAG)

  12. v v x x Definition x is anexitfrom v (from v’s subtree) if • x is not a descendant of v, but • x is the head of a (cross- or back-) edge from a descendant of v (including v itself) • Any non-root vertex v has an exit

  13. Finding SCC’s • A root nodes v sometimes have exits • only via a cross-edge to a node x that is not in a component with a root above v, e.g. vertex 10 in the example.

  14. Strongly-connected components 1 2 10 3 11 12 4 8 13 5 9 6 7

  15. Minimum Spanning Trees (Forests) • Given an undirected graph G=(V,E) with each edge e having a weightw(e) • Find a subgraphT of G of minimum total weight s.t. every pair of vertices connected in G are also connected in T • if G is connected then T is a tree otherwise it is a forest

  16. 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 8 5 7 9 13 Weighted Undirected Graph

  17. First Greedy Algorithm • Prim’s Algorithm: • start at a vertex v • add the cheapest edge adjacent to v • repeatedly add the cheapest edge that joins the vertices explored so far to the rest of the graph. • We’ll show it works later

  18. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  19. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  20. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  21. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  22. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  23. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  24. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  25. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  26. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  27. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  28. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  29. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  30. Prim’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 MST weight =41 5 8 7 9 13

  31. Second Greedy Algorithm • Kruskal’s Algorithm • Start with the vertices and no edges • Repeatedly add the cheapest edge that joins two different components. i.e. that doesn’t create a cycle • Again we save the proof of correctness for later

  32. 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 8 5 7 9 13 Kruskal’s Algorithm

  33. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  34. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  35. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  36. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  37. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  38. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  39. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  40. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  41. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  42. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  43. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  44. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  45. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 5 8 7 9 13

  46. Kruskal’s Algorithm 1 4 2 5 2 7 3 1 4 3 3 4 6 6 7 5 9 8 5 -1 9 8 11 4 12 10 produces same tree as Prim’s algorithm 5 8 7 9 13

More Related