slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
M i n im um S pa nn i ng T r ee s PowerPoint Presentation
Download Presentation
M i n im um S pa nn i ng T r ee s

Loading in 2 Seconds...

play fullscreen
1 / 61
cadman-mckinney

M i n im um S pa nn i ng T r ee s - PowerPoint PPT Presentation

53 Views
Download Presentation
M i n im um S pa nn i ng T r ee s
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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. MinimumSpanning Trees

  2. MinimumSpanningTree • Atownhasaset ofhousesandaset of roads • A roadconnects 2andonly2houses • A roadconnectinghousesuandvhas arepaircostw(u,v) • Goal:Repairenough (andnomore) roads suchthat • »everyonestays connected:canreacheveryhousefromall otherhouses,and • »totalrepaircostis minimum

  3. MinimumSpanningTree • Modelas agraph: • »UndirectedgraphG=(V,E) • »Weightw(u,v)oneachedge(u,v)∈E • »FindT⊆Esuchthat • Tconnects allvertices • (Tisa spanningtree) • w(T)= ∑w(u,v)isminimized (u,v)∈T

  4. MinimumSpanningTree • A spanningtreewhoseweightisminimumoverallspanningtrees iscalleda • Minimum SpanningTree,orMST • Example: • Inthis example,thereis morethanoneMST • Replaceedge(b,c)by (a,h) • Geta differentspanningtreewiththesameweight

  5. MinimumSpanningTree • Whichedges formthe MinimumSpanning Tree (MST)of thebelow graph? A 5 6 4 9 H B C 14 2 10 15 G E D 3 8 F

  6. MinimumSpanningTree • MSTssatisfytheoptimalsubstructureproperty: anoptimaltreeis composed ofoptimalsubtrees • »LetT beanMSTofGwithan edge(u,v)inthemiddle • »Removing(u,v)partitions Tintotwotrees T1andT2 • »Claim:T1isanMSTofG1=(V1,E1),andT2is anMSTofG2=(V2,E2) (DoV1andV2sharevertices?Why?) • »Proof:w(T)= w(u,v)+w(T1)+w(T2) • (Therecan’tbea bettertreethanT1orT2, orTwouldbe suboptimal)

  7. Some definitions • A cut (S,V – S)of an undirected graph G =(V,E)is a partition of V • We say that an edge (u,v) ϵE crosses the (S, V – S) if one of its endpoints is in S and the other is in V - S.

  8. Some definitions • We say that a cut respects a set A of edges if no edge in A crosses the cut. • An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. • Note that there can be more than one light edge crossing a cut in the case of ties. • More generally, we say that an edge is a light edge satisfying a given property if its weight is the minimum of any edge satisfying the property • Theorem 23.1 Let G = (V,E)be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, let (S,V – S) be any cut of G that respects A, and let (u, v) be a light edge crossing (S, V – S). Then, edge (u, v) is safe for A.

  9. Proof of theorem Except for the dashed edge (u, v), all edges shown are in T. A is some subset of the edges of T, but A cannot contain any edges that cross the cut (S, V − S), since this cut respects A. Shaded edges are the path p.

  10. Proof of theorem (1) Since the cut respects A, edge (x, y) is not in A. To form T‘from T : • Remove (x, y). Breaks T into two components. • Add (u, v). Reconnects. So T‘ = T − {(x, y)} ∪ {(u, v)}. T’ is a spanning tree. w(T’ ) = w(T ) − w(x, y) + w(u, v) ≤ w(T) , since w(u, v) ≤ w(x, y). Since T is a spanning tree, w(T’) ≤ w(T ), and T is an MST, then T’ must be an MST. Need to show that (u, v) is safe for A: • A ⊆ T and (x, y) ∉A ⇒ A ⊆ T’. • A ∪ {(u, v)} ⊆ T’ . • Since T’is an MST, (u, v) is safe for A.

  11. Generic-MST So, in GENERIC-MST: • A is a forest containing connected components. Initially, each component is a single vertex. • Any safe edge merges two of these components into one. Each component is a tree. • Since an MST has exactly |V| − 1 edges, the for loop iterates |V| − 1 times. Equivalently, after adding |V|−1 safe edges, we.re down to just one component. Corollary If C = (VC, EC) is a connected component in the forest GA = (V, A) and (u, v) is a light edge connecting C to some other component in GA (i.e., (u, v) is a light edge crossing the cut (VC, V − VC)), then (u, v) is safe for A. Proof Set S = VC in the theorem.

  12. GrowingAnMST • Some properties ofanMST: • Ithas |V| -1edges • Ithas nocycles • Itmightnot beunique • Buildingupthesolution • »Wewill buildasetAofedges • »Initially,Ahasnoedges • »As weaddedges toA,maintainaloopinvariant: • •Loopinvariant:Aisasubset ofsomeMST • »Addonlyedgesthatmaintaintheinvariant • IfAis a subsetofsomeMST,anedge(u,v)is safeforA • ifandonlyifAυ{(u,v)}is alsoasubset ofsomeMST • Sowewilladdonlysafeedges

  13. GrowingAnMST

  14. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  15. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  16. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1? MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  17. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  18. Kruskal’s Algorithm Run thealgorithm: 2? 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  19. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  20. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5? 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  21. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  22. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8? 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  23. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  24. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9? 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  25. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  26. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13? 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  27. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  28. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14? 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  29. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  30. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17? 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  31. Kruskal’s Algorithm Run thealgorithm: 2 19? Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  32. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21? 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  33. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25? T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  34. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  35. Kruskal’s Algorithm Run thealgorithm: 2 19 Kruskal() { 9 14 17 8 25 T=∅; for eachv∈V 5 21 13 1 MakeSet(v); sortEbyincreasing edge weightw for each(u,v)∈E(in sorted if FindSet(u)≠ FindSet(v) order) T=TU{{u,v}}; Union(FindSet(u), FindSet(v)); }

  36. Kruskal’s Algorithm

  37. Kruskal’s Algorithm

  38. Kruskal’s Algorithm NetworkingLaboratory 11-37/62 Spring2006Algorithm

  39. Kruskal’s Algorithm What will affectthe runningtime? 1 Sort O(V) MakeSet() calls O(E) FindSet()calls O(V) Union() calls (Exactly howmany Union()s?) Kruskal() { T=∅; for eachv∈V MakeSet(v); sortE by increasing edge weightw for each(u,v)∈E (in sortedorder) if FindSet(u)≠ FindSet(v) T=TU {{u,v}}; Union(FindSet(u), FindSet(v)); }

  40. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u ∈Q key[u]= ∞; key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v ∈Adj[u] if (v ∈Q and w(u,v) П[v] =u; key[v]= w(u,v); 6 4 9 2 15 5 14 10 3 8 Runonexample graph < key[v])

  41. Prim’s Algorithm ∞ 5 6 4 • MST-Prim(G,w, r) Q = V[G]; • for each u ∈Q • key[u]= ∞; • key[r]= 0; • П[r] =NULL; • while (Q notempty) • u = ExtractMin(Q); for each v ∈Adj[u] • if (v ∈Q and w(u,v) < • П[v]= u; • key[v]= w(u,v); 9 ∞ ∞ ∞ 14 2 10 15 ∞ ∞ ∞ 3 8 ∞ Runonexample graph key[v])

  42. Prim’s Algorithm ∞ 5 6 4 • MST-Prim(G,w, r) Q = V[G]; • for each u ∈Q • key[u]= ∞; • key[r]= 0; • П[r] =NULL; • while (Q notempty) • u = ExtractMin(Q); for each v∈Adj[u] • if (v∈Q andw(u,v)< • П[v] =u; • key[v]= w(u,v); 9 ∞ ∞ ∞ 14 2 10 15 0 r ∞ ∞ 3 8 ∞ Pickastartvertexr key[v])

  43. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 ∞ ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); 15 0 u ∞ ∞ 3 8 ∞ Blackverticeshave beenremovedfromQ for each v∈Adj[u] if (v∈Q andw(u,v)< key[v]) П[v] =u; key[v]= w(u,v);

  44. Prim’s Algorithm ∞ 5 6 4 • MST-Prim(G,w, r) Q = V[G]; • for each u∈Q • key[u]= ∞; • key[r]= 0; • П[r] =NULL; • while (Q notempty) • u = ExtractMin(Q); for each v∈Adj[u] • if (v∈Q andw(u,v)< • П[v] =u; • key[v]= w(u,v); 9 ∞ ∞ ∞ 14 2 10 15 0 u ∞ ∞ 3 8 3 Blackarrowsindicateparent pointers key[v])

  45. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 14 ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); 15 0 u ∞ ∞ 3 8 3 for each v∈Adj[u] if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])

  46. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 14 ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v∈Adj[u] 15 0 ∞ ∞ 3 8 3 u if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])

  47. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 14 ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v∈Adj[u] 15 0 8 ∞ 3 8 3 u if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])

  48. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 10 ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v∈Adj[u] 15 0 8 ∞ 3 8 3 u if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])

  49. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 10 ∞ ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v∈Adj[u] 15 0 8 ∞ 3 8 3 u if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])

  50. Prim’s Algorithm MST-Prim(G,w, r) Q = V[G]; for each u∈Q key[u]= ∞; ∞ 5 6 4 9 10 2 ∞ 14 2 10 key[r]= 0; П[r] =NULL; while (Q notempty) u = ExtractMin(Q); for each v∈Adj[u] 15 0 8 ∞ 3 8 3 u if (v∈Q andw(u,v)< П[v] =u; key[v]= w(u,v); key[v])