1 / 27

Algorithm Design and Analysis

L ECTURE 8 Greedy Graph Alg’s II Implementing Dijkstra MST. Algorithm Design and Analysis. CSE 565. Adam Smith. Rough algorithm ( Dijkstra ). Maintain a set of explored nodes S whose shortest path distance d(u) from s to u is known. Initialize S = { s }, d(s) = 0.

lihua
Télécharger la présentation

Algorithm Design and Analysis

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. LECTURE 8 • Greedy Graph Alg’s II • Implementing Dijkstra • MST Algorithm Design and Analysis CSE 565 Adam Smith A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  2. Rough algorithm (Dijkstra) • Maintain a set of explored nodes S whose shortest path distance d(u) from s to u is known. • Initialize S = {s}, d(s) = 0. • Repeatedly choose unexplored node v which minimizes • add v to S, and set d(v) = (v). shortest path to some u in explored part, followed by a single edge (u, v) (e) v d(u) u S s A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  3. Review Question • Is Dijsktra’s algorithm correct with negative edge weights? A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  4. Proof of Correctness (Greedy Stays Ahead) Invariant. For each node u S, d(u) is the length of the shortest path from s to u. Proof:(by induction on |S|) • Base case: |S| = 1 is trivial. • Inductive hypothesis: Assume for |S| = k 1. • Let v be next node added to S, and let (u,v) be the chosen edge. • The shortest s-u path plus (u,v) is an s-v path of length (v). • Consider any s-v path P. We'll see that it's no shorter than (v). • Let (x,y) be the first edge in P that leaves S,and let P' be the subpath to x. • P + (x,y) has length ·d(x)+ (x,y)·(y)·(v) P y x P' s S u v A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  5. Implementation • For unexplored nodes, maintain • Next node to explore = node with minimum (v). • When exploring v, for each edge e = (v,w), update • Efficient implementation: Maintain a priority queue of unexplored nodes, prioritized by (v). Priority Queue A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  6. Priority queues • Maintain a set of items with priorities (= “keys”) • Example: jobs to be performed • Operations: • Insert • Increase key • Decrease key • Extract-min: find and remove item with least key • Common data structure: heap • Time: O(logn) per operation A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  7. Pseudocode for Dijkstra(G, ) • d[s]  0 • for each vÎV – {s} • dod[v]  ¥; p[v]  ¥ • S   • Q  V ⊳Q is a priority queue maintainingV – S,keyed on [v] • whileQ ¹ • dou EXTRACT-MIN(Q) • SSÈ {u}; d[u] p[u] • for each vÎAdjacency-list[u] • doif[v] > [u] + (u, v) • then [v] d[u] + (u, v) explore edges leaving v Implicit DECREASE-KEY A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  8. n times degree(u) times Handshaking Lemma  ·mimplicit DECREASE-KEY’s. Analysis of Dijkstra • whileQ ¹ • dou EXTRACT-MIN(Q) • SSÈ {u} • for each vÎAdj[u] • doif[v] >[u] + w(u, v) • then[v] [u] + w(u, v) PQ Operation Dijkstra Array Binary heap d-way Heap Fib heap † ExtractMin n n log n HW3 log n DecreaseKey m 1 log n HW3 1 Total n2 m log n m log m/n n m + n log n † Individual ops are amortized bounds A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  9. Output: A spanning treeT — a tree that connects all vertices — of minimum weight: . Minimum spanning tree (MST) • Input: A connected undirected graph G = (V, E) with weight function w : E R. • For now, assume all edge weights are distinct. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  10. Example of MST 6 12 9 5 7 14 15 8 10 3 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  11. Example of MST 6 12 9 5 7 14 15 8 10 3 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  12. Greedy Algorithms for MST • Kruskal's: Start with T = . Consider edges in ascending order of weights. Insert edge e in T unless doing so would create a cycle. • Reverse-Delete: Start with T = E. Consider edges in descending order of weights. Delete edge e from T unless doing so would disconnect T. • Prim's: Start with some root node s. Grow a tree T from s outward. At each step, add to T the cheapest edge e with exactly one endpoint in T. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  13. Cycles and Cuts • Cycle: Set of edges the form (a,b),(b,c),(c,d),…,(y,z),(z,a). • Cut: a subset of nodes S. The corresponding cutset D is the subset of edges with exactly one endpoint in S. 2 3 1 6 4 Cycle C = (1,2),(2,3),(3,4),(4,5),(5,6),(6,1) 5 8 7 2 3 1 Cut S = { 4, 5, 8 } Cutset D = (5,6), (5,7), (3,4), (3,5), (7,8) 6 4 S 5 8 7 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  14. Cycle-Cut Intersection • Claim. A cycle and a cutset intersect in an even number of edges. • Proof: A cycle has to leave and enter the cut the same number of times. C S V - S A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  15. C f S e e is in the MST f is not in the MST Cut and Cycle Properties • Cut property. Let S be a subset of nodes. Let e be the min weight edge with exactly one endpoint in S. Then the MST contains e. • Cycle property. Let C be a cycle, and let f be the max weight edge in C. Then the MST does not contain f. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  16. Proof of Cut Property Cut property: Let S be a subset of nodes. Let e be the min weight edge with exactly one endpoint in S. Then the MST T* contains e. • Proof:(exchange argument) • Suppose e does not belong to T*. • Adding e to T* creates a cycle C in T*. • Edge e is both in the cycle C and in the cutset D corresponding to S  there exists another edge, say f, that is in both C and D. • T' = T*  {e} - {f} is also a spanning tree. • Since ce < cf, cost(T') < cost(T*). Contradiction. ▪ f S e T* A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  17. Proof of Cycle Property Cycle property: Let C be a cycle in G. Let f be the max weight edge in C. Then the MST T* does not contain f. • Proof:(exchange argument) • Suppose f belongs to T*. • Deleting f from T* creates a cut S in T*. • Edge f is both in the cycle C and in the cutset D corresponding to S  there exists another edge, say e, that is in both C and D. • T' = T*  {e} - {f} is also a spanning tree. • Since ce < cf, cost(T') < cost(T*). Contradiction. ▪ f S e T* A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  18. Greedy Algorithms for MST • Kruskal's: Start with T = . Consider edges in ascending order of weights. Insert edge e in T unless doing so would create a cycle. • Reverse-Delete: Start with T = E. Consider edges in descending order of weights. Delete edge e from T unless doing so would disconnect T. • Prim's: Start with some root node s. Grow a tree T from s outward. At each step, add to T the cheapest edge e with exactly one endpoint in T. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  19. Prim's Algorithm: Correctness • Prim's algorithm. [Jarník 1930, Prim 1959] • Apply cut property to S. • When edge weights are distinct, every edge that isadded must be in the MST • Thus, Prim’s alg. outputs the MST S A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  20. Correctness of Kruskal • [Kruskal, 1956]: Consider edges in ascending order of weight. • Case 1: If adding e to T creates a cycle, discard e according to cycle property. • Case 2: Otherwise, insert e = (u, v) into T according to cut property where S = set of nodes in u's connected component. e Case 1 S v e u Case 2 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  21. Review Questions Let G be a connected undirected graph with distinct edge weights. Answer true or false: • Let e be the cheapest edge in G. Some MST of G contains e? • Let e be the most expensive edge in G. No MST of G contains e? A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  22. Non-distinct edges? A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  23. Implementation of Prim(G,w) IDEA: Maintain V – Sas a priority queue Q(as in Dijkstra). Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in S. • Q V • key[v]  ¥ for all vÎV • key[s]  0 for some arbitrary sÎV • whileQ¹  • dou  EXTRACT-MIN(Q) • for each vÎAdjacency-list[u] • do ifvÎQ and w(u, v) < key[v] • thenkey[v]  w(u, v) ⊳DECREASE-KEY • p[v]  u At the end, {(v, p[v])} forms the MST. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  24. Q(n) total n times degree(u) times Analysis of Prim • Q V • key[v]  ¥ for all vÎV • key[s]  0 for some arbitrary sÎV • whileQ¹  • dou  EXTRACT-MIN(Q) • for each vÎAdj[u] • do ifvÎQ and w(u, v) < key[v] • thenkey[v]  w(u, v) • p[v]  u Handshaking Lemma  Q(m)implicit DECREASE-KEY’s. Time: as in Dijkstra A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  25. n times degree(u) times Analysis of Prim • whileQ¹  • dou  EXTRACT-MIN(Q) • for each vÎAdj[u] • do ifvÎQ and w(u, v) < key[v] • thenkey[v]  w(u, v) • p[v]  u Handshaking Lemma  Q(m)implicit DECREASE-KEY’s. PQ Operation Prim Array Binary heap d-way Heap Fib heap † ExtractMin n n log n HW3 log n DecreaseKey m 1 log n HW3 1 Total n2 m log n m log m/n n m + n log n † Individual ops are amortized bounds A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  26. Greedy Algorithms for MST • Kruskal's:Start with T = . Consider edges in ascending order of weights. Insert edge e in T unless doing so would create a cycle. • Reverse-Delete: Start with T = E. Consider edges in descending order of weights. Delete edge e from T unless doing so would disconnect T. • Prim's: Start with some root node s. Grow a tree T from s outward. At each step, add to T the cheapest edge e with exactly one endpoint in T. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  27. Union-Find Data Structures • With modifications, amortized time for tree structure is O(nAck(n)), where Ack(n), the Ackerman function grows much more slowly than log n. • See KT Chapter 4.6 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

More Related