610 likes | 670 Views
M i n im um S pa nn i ng T r ee s. Mi n i m u m S pan n i n g T r e e. A t o w n h as a s et o f h o u s es a n d a s et o f r o a d s A r o ad c o n n e cts 2 a n d on ly 2 h o u s es A r o ad c o n n e cti n g h o u s es u a n d v h as a r e p air c o s t w ( u , v)
E N D
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
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
MinimumSpanningTree • A spanningtreewhoseweightisminimumoverallspanningtrees iscalleda • Minimum SpanningTree,orMST • Example: • Inthis example,thereis morethanoneMST • Replaceedge(b,c)by (a,h) • Geta differentspanningtreewiththesameweight
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
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)
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.
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.
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.
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.
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.
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
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
Kruskal’s Algorithm NetworkingLaboratory 11-37/62 Spring2006Algorithm
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)); }
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])
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])
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])
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);
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])
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])
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])
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])
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])
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])
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])