1 / 38

Minimum-Cost Spanning Tree

Minimum-Cost Spanning Tree. CS 110: Data Structures and Algorithms First Semester, 2010-2011. Minimum-Cost Spanning Tree. Given a weighted graph G, determine a spanning tree with minimum total edge cost

branxton
Download Presentation

Minimum-Cost Spanning Tree

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. Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011

  2. Minimum-Cost Spanning Tree • Given a weighted graph G, determine a spanning tree with minimum total edge cost • Recall: spanning subgraph means all vertices are included, tree means the graph is connected and has no cycles • Useful in applications that ensure connectivity of nodes of optimal cost

  3. Kruskal’s Algorithm • Solves the minimum-cost spanning tree problem • Strategy: repeatedly select the lowest-cost edge as long as it does not form a cycle with previously selected edges • Stop when n-1 edges have been selected (n is the number of vertices)

  4. Kruskal’s Algorithm • Use a priority queue of edges to facilitate selection of lowest-edge cost (just disregard edges that form a cycle) • Time complexityO( m log m )  O( m log n )

  5. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  6. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  7. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  8. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  9. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  10. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  11. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  12. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  13. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  14. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  15. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  16. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  17. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  18. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  19. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  20. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  21. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  22. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  23. Kruskal’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  24. Pseudo-Code: Kruskal function Kruskal( Graph g ) n <-- number of vertices in g for each vertex v in g define an elementary cluster C(v) <-- {v} E <-- all edges in G Es <-- sort(E) T <-- null // will contain edges of MCST i <-- 0 while T has edges fewer than n-1 (u, v) <-- Es[i] Let C(v) be the cluster containing v Let C(u) be the cluster containing u if C(v) != C(u) then Add edge (v, u) to T Merge C(v) and C(u) into one cluster i = i + 1 return tree T

  25. Prim’s Algorithm • Start at a specific vertex • Choose the edge of minimum cost which is incident on the vertex being considered • Add the new vertex on which the previously chosen edge is incident • Repeat until the MCST is found • Unlike Kruskal’s, make sure that a tree is always build as the algorithm progresses

  26. Prim’s Algorithm • Start at a specific vertex • Choose the edge of minimum cost which is incident on the vertex being considered • Add the new vertex on which the previously chosen edge is incident • Repeat until the MCST is found • Unlike Kruskal’s, make sure that a tree is always build as the algorithm progresses

  27. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  28. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  29. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  30. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  31. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  32. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  33. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  34. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  35. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  36. Prim’s Algorithm 8 7 b c d 4 2 9 4 14 a i e 11 7 6 8 10 h g f 1 2

  37. Prim’s Algorithm 8 7 b c d 4 2 9 4 a i e h g f 1 2

  38. Pseudo-Code: Prim function Prim( Graph g ) select any vertex v of g D[v] <-- 0 for each vertex u != v D[u] <-- infinity Initialize T <-- null Initalize a priority queue Q with an item ( (u, null), D[u] ) for each vertex u, where (u, null) is the element and D[u] is the key while Q is not empty (u, e) <-- Q.removeMin() Add vertex u and edge e to T for each vertex z adjacent to u such that z is in Q if w( (u,z) ) < D[z] D[z] <-- w( (u,z) ) Change to (z, (u,z)) the element of vertex z in Q Change to D[z] the key of vertex z in Q return the tree T

More Related