1 / 32

Minimum Spanning Tree (MST)

Minimum Spanning Tree (MST). Motivation. For an electrical circuit certain pins have to be grounded. Finding the arrangement of wires (connecting those pins) that uses the least amount of wire.

damia
Download Presentation

Minimum Spanning Tree (MST)

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 Tree (MST)

  2. Motivation For an electrical circuit certain pins have to be grounded. Finding the arrangement of wires (connecting those pins) that uses the least amount of wire. Let a G(V,E) be a graph such that (u, v)  E and a weight w(u,v) corresponding to wire needed to join u and v. We are looking for an acyclic subset T  E that connects all vertices and whose total weight w(T) is minimized.

  3. Motivation Since T is acyclic and connects all the vertices, it must form a tree. Since it spans the graph, it is called a spanning tree. MST or actually means Minimum Weight Spanning Tree.

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

  5. 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. Minimum Spanning Tree • Which edges form the minimum spanning tree (MST) of the following graph? A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F

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

  8. Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; 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);

  9. Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 14 2 10 15 3 8 Run on example graph

  10. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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);    14 2 10 15    3 8  Run on example graph

  11. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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);    14 2 10 15 r 0   3 8  Pick a start vertex r

  12. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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);    14 2 10 15 u 0   3 8  Black vertices have been removed from Q

  13. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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);    14 2 10 15 u 0   3 8 3 Black arrows indicate parent pointers

  14. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 14   14 2 10 15 u 0   3 8 3

  15. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 14   14 2 10 15 0   3 8 3 u

  16. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 14   14 2 10 15 0 8  3 8 3 u

  17. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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   14 2 10 15 0 8  3 8 3 u

  18. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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   14 2 10 15 0 8  3 8 3 u

  19. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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 2  14 2 10 15 0 8  3 8 3 u

  20. Prim’s Algorithm  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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 2  14 2 10 15 0 8 15 3 8 3 u

  21. Prim’s Algorithm u  6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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 2  14 2 10 15 0 8 15 3 8 3

  22. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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 2  14 2 10 15 0 8 15 3 8 3

  23. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2  14 2 10 15 0 8 15 3 8 3

  24. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2 9 14 2 10 15 0 8 15 3 8 3

  25. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2 9 14 2 10 15 0 8 15 3 8 3

  26. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2 9 14 2 10 15 0 8 15 3 8 3

  27. Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2 9 14 2 10 15 0 8 15 3 8 3

  28. Prim’s Algorithm 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; 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); 5 2 9 u 14 2 10 15 0 8 15 3 8 3

  29. Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; 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?

  30. Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; 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));

  31. Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; 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?

  32. Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; 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?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

More Related