1 / 53

Minimum Spanning Trees

Minimum Spanning Trees. Minimum- Spanning Trees. 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree. Concrete example. Imagine: You wish to connect all the computers in an office building using the least amount of cable

salome
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 Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree

  3. Concrete example • Imagine: You wish to connect all the computers in an • office building using the least amount of cable • Each vertex in a graph G represents a computer • Each edge represents the amount of cable needed to • connect all computers

  4. Spanning Tree • A spanning tree of Gis a subgraph which • is tree (acyclic) • and connect all the vertices in V. • Spanning tree has only |V| - 1 edges.

  5. Minimum Spanning Tree • Input: • Undirected connected graphG = (V, E) and weight function w : E→R, • Output: • A Minimum spanning treeT : tree that connects all the vertices and minimizes • Greedy Algorithms • Generic MST algorithm • Kruskal’s algorithm • Prim’s algorithm

  6. Example: MST

  7. Example: MST

  8. Kruskal's Algorithm • Edge based algorithm • Greedy strategy: • From the remaining edges, select a least-cost edge that does not result in a cycle when added to the set of already selected edges • Repeat |V|-1 times

  9. Kruskal's Algorithm • INPUT: • edge-weighted graph G = (V, E), with |V| = n • OUTPUT: • a spanning tree A of G • touches all vertices, • has n-1 edges • of minimum cost ( = total edge weight) • Algorithm: • Start with A empty, • Add the edges one at a time, in increasing weight order • An edge is accepted it if connects vertices of distinct trees (if the edge does not form a cycle in A) • until A contains n-1 edges

  10. Kruskal's Algorithm MST-Kruskal(G,w) 1A¬Æ 2 for each vertex vÎV[G] do 3 Make-Set(v) //creates set containing v (for initialization) 4 sort the edges of E 5 foreach (u,v)ÎE do 6 ifFind-Set(u) ¹Find-Set(v) then // different component 7 A¬AÈ {(u,v)} 8 Union(Set(u),Set(v)) // merge 9 returnA

  11. Data Structures For Kruskal’s Algorithm 7 3 6 4 • Does the addition of an edge (u, v) toTresult in a cycle? • Each component of T is a tree. • When u and v are in the • same component, the addition of the edge (u, v) creates a cycle. • different components, the addition of the edge(u, v)does not create a cycle. 1 3 2 2 4 5 7 6 8

  12. Data Structures For Kruskal’s Algorithm 1 3 5 7 7 3 6 4 2 2 4 6 8 • Each component of T is defined by the vertices in the component. • Represent each component as a set of vertices. • {1, 2, 3, 4}, {5, 6}, {7, 8} • Two vertices are in the same component iff they are in the same set of vertices.

  13. Data Structures For Kruskal’s Algorithm 7 3 6 4 • When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component • In our set representation of components, the set that has vertex u and the set that has vertex v are united. • {1, 2, 3, 4} + {5, 6}{1, 2, 3, 4, 5, 6} 1 3 5 7 2 6 8 2 4

  14. Kruskal’s Algorithm

  15. Kruskal’s Algorithm

  16. Kruskal’s Algorithm

  17. Kruskal’s Algorithm

  18. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  19. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  20. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  21. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  22. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  23. 5 A B 4 6 2 2 D cycle!! C 3 1 2 3 E F 4

  24. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  25. 5 A B 4 6 2 2 D C 3 1 2 3 E F 4

  26. minimum- spanning tree A B 2 2 D C 1 2 3 E F

  27. Kruskal's Algorithm MST-Kruskal(G,w) 1A¬Æ 2 for each vertex vÎV[G] do //takes O(V) 3 Make-Set(v) 4 sort the edges of E //takes O(E lg E) //takes O(E) 5 foreach (u,v)ÎE, in nondecreasing of weight do 6 ifFind-Set(u) ¹Find-Set(v) then 7 A¬AÈ {(u,v)} 8 Union(Set(u),Set(v)) 9 returnA

  28. Running Time of Kruskal’s Algorithm • Kruskal’s Algorithm consists of two stages. • Initializing the set A in line 1 takes O(1) time. • Sorting the edges by weight in line 4. • takes O(E lg E) • Performing • |V| MakeSet() operations for loop in lines 2-3. • |E| FindSet(), for loop in lines 5-8. • |V| - 1 Union(), for loop in lines 5-8. • which takes O(V + E) • The total running time is • O(E lg E) • We have lg │E│ = O(lg V) because # of E = V-1 • So total running time becomes O(E lg V).

  29. Prim’s Algorithm • The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V. • At each step, • Adds only edges that are safe for A. • When algorithm terminates, edges in A form MST. • Vertex based algorithm. • Grows one tree T, one vertex at a time

  30. Prim’s Algorithm MST-Prim(G,w,r) //G: graph with weight w and a root vertex r 1 for each u Î V[G]{ 2 key[u] ¬¥ • p[u] ¬ NULL // parent of u } 4 key[r] ¬ 0 5 Q = BuildMinHeap(V,key); // Q – vertices out of T 6 while Q ¹Ædo 7 u ¬ ExtractMin(Q) // making u part of T 8 for each v Î Adj[u] do 9 if v Î Q and w(u,v)  key[v] then 10 p[v] ¬ u 11 key[v] ¬ w(u,v) updating keys • For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. • key [v]= ∞ if there is no edge and p [v] names parent of v in tree. • When algorithm terminates the min-priority queue Q is empty.

  31. Prim’s Algorithm • Lines 1-5 set the key of each vertex to ∞ (except root r, whose key is set to 0 first vertex processed). Also, set parent of each vertex to NULL, and initialize min-priority queue Q to contain all vertices. • Line 7 identifies a vertex u є Q • Removing u from set Q adds it to set Q-V of vertices in tree, thus adding (u, p[ u]) to A. • The for loop of lines 8-11 update key and p fields of every vertex v adjacent to u but not in tree.

  32. Run on example graph

  33. Run on example graph

  34. Run on example graph

  35. Run on example graph

  36. Run on example graph

  37. Run on example graph

  38. Run on example graph Extract_min from Q

  39. Run on example graph

  40. Run on example graph

  41. Run on example graph

  42. Run on example graph

  43. Run on example graph

  44. Run on example graph

  45. Run on example graph

  46. Run on example graph

  47. Run on example graph

  48. Run on example graph

  49. Run on example graph

  50. Run on example graph

More Related