1 / 33

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Tree. Problem: given a connected, undirected, weighted graph. 6. 4. 5. 9. 14. 2. 10. 15. 3. 8. Minimum Spanning Tree. Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight. 6.

jkeen
Download Presentation

Minimum Spanning Trees

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 Spanning Trees

  2. Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph 6 4 5 9 14 2 10 15 3 8

  3. Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 6 4 5 9 14 2 10 15 3 8

  4. Minimum Spanning Tree Number of edges = |V| -1 Acyclic Minimum total weight

  5. Minimum Spanning Tree • Which edges form the minimum spanning tree (MST) of this graph? A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F

  6. Minimum Spanning Tree • Answer: A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F

  7. Minimum Spanning Tree • Applications • Design of networks • Design of electrical circuits • Airline routes

  8. Minimum Spanning Tree • Thm: (Light Edges are in MST) • Let T be MST of G, and let A  T be subtree of T • Let (u,v) be min-weight edge connecting A to V-A • Then (u,v)  T

  9. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

  10. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph

  11. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5    14 2 10 15    3 8  Run on example graph

  12. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5    14 2 10 15 r 0   3 8  Pick a start vertex r

  13. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5    14 2 10 15 u 0   3 8  Grey vertices have been removed from Q

  14. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5    14 2 10 15 u 0   3 8 3 Red arrows indicate parent pointers

  15. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 14   14 2 10 15 u 0   3 8 3

  16. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 14   14 2 10 15 0   3 8 3 u

  17. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 14   14 2 10 15 0 8  3 8 3 u

  18. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10   14 2 10 15 0 8  3 8 3 u

  19. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10   14 2 10 15 0 8  3 8 3 u

  20. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10 2  14 2 10 15 0 8  3 8 3 u

  21. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10 2  14 2 10 15 0 8 15 3 8 3 u

  22. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10 2  14 2 10 15 0 8 15 3 8 3

  23. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3

  24. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3

  25. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3

  26. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3

  27. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3

  28. Prim’s Algorithm u MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3

  29. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 u 14 2 10 15 0 8 15 3 8 3

  30. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

  31. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

  32. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

  33. Prim’s Algorithm MST-Prim(G, w, r) Q = VG; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? Using heap: O(E lg V)

More Related