Minimum Spanning Tree 7/8/03 12:53 Outline and Reading Minimum Spanning Trees ( § 12.7.3) Minimum Spanning Tree n Definitions n A crucial fact Prim-Jarnik’s Algorithm ( § 12.7.3.2) Kruskal’s Algorithm ( § 12.7.3.1) 7/8/03 12:53 Minimum Spanning Tree 1 7/8/03 12:53 Minimum Spanning Tree 2 Minimum Spanning Tree Cycle Property Cycle Property: 8 f Spanning subgraph Let T be a minimum 4 n ORD spanning tree of a C Subgraph of a graph G 10 9 n weighted graph G 6 containing all the vertices of 2 1 PIT 3 e G Let e be an edge of G n 7 8 Spanning tree that is not in T and let C DEN 6 7 be the cycle formed by Spanning subgraph that is 7 9 n adding e to T itself a (free) tree 3 DCA Replacing f with e yields For every edge f of C, STL Minimum spanning tree (MST) n a better spanning tree 4 weight ( f ) £ weight ( e ) Spanning tree of a weighted Proof: 8 n 8 5 2 f graph with minimum total 4 By contradiction edge weight C 9 If weight ( f ) > > weight ( e ) we DFW 6 ATL n Applications 2 can get a spanning tree 3 e Communications networks 7 of smaller weight by 8 n Transportation networks replacing e with f n 7 7/8/03 12:53 Minimum Spanning Tree 3 7/8/03 12:53 Minimum Spanning Tree 4 1
Minimum Spanning Tree 7/8/03 12:53 Partition Property Prim-Jarnik’s Algorithm Partition Property: Prim-Jarnik’s algorithm for computing an MST is Consider a partition of the vertices U V similar to Dijkstra’s algorithm 7 f of G into subsets U and V. Let e be 4 We assume that the graph is connected an edge of minimum weight across 9 5 the partition. There is a minimum 2 We pick an arbitrary vertex s and we grow the 8 spanning tree of G containing edge e MST as a cloud of vertices, starting from s 3 8 e Proof: We store with each vertex v a label d ( v ) 7 n Let T be an MST of G representing the smallest weight of an edge Replacing f with e yields n If T does not contain e, consider the another MST connecting v to any vertex in the cloud (as opposed to cycle C formed by e with T and let f be an edge of C across the partition U V the total sum of edge weights on a path from the start vertex to u). 7 f At each step n By the cycle property, 4 weight ( f ) £ £ weight ( e ) 9 n We add to the cloud the vertex u outside the cloud with 5 n Thus, weight ( f ) = 2 = weight ( e ) 8 the smallest distance label n We obtain another MST by replacing f 8 3 n We update the labels of the vertices adjacent to u e with e 7 7/8/03 12:53 Minimum Spanning Tree 5 7/8/03 12:53 Minimum Spanning Tree 6 Algorithm PrimJarnik(G): Input: A weighted graph G. Output: A minimum spanning tree T for G. pick any vertex v of G {grow the tree starting with vertex v} T ¨ {v} Use a priority queue Q whose keys are D labels, and whose D[u] ¨ 0 elements are vertex-edge pairs . E[u] ¨ ∅ for each vertex u ≠ v do Any vertex v can be the starting vertex. D[u] ¨ ∞ let Q be a priority queue that contains vertices, using the D labels as keys We still initialize all the D[u] values to INFINITE, but we also while Q ≠ ∅ do {pull u into the cloud C} initialize E[u] (the edge associated with u) to null. u ¨ Q.removeMinElement() add vertex u and edge E[u] to T Return the minimum-spanning tree T. for each vertex z adjacent to u do if z is in Q We can reuse code from Dijkstra’s, and we only have to change a few {perform the relaxation operation on edge (u, z) } things. Let’s look at the pseudocode.... if weight(u, z) < D[z] then D[z] ¨ weight(u, z) E[z] ¨ (u, z) change the key of z in Q to D[z] return tree T 7/8/03 12:53 Minimum Spanning Tree 7 7/8/03 12:53 Minimum Spanning Tree 8 2
Minimum Spanning Tree 7/8/03 12:53 Example Example (contd.) 7 • 7 D 7 D 2 2 7 B B D 4 4 7 2 9 • 9 • 8 5 B 4 5 5 F F 2 2 9 4 C C 5 8 8 5 F 2 3 3 C 8 8 8 E E 3 A A 7 7 8 7 7 0 0 E A 3 7 7 0 7 D 7 2 7 D 7 B 4 2 7 D 2 B 9 4 4 5 B 4 5 F 2 9 5 • C 9 4 5 F 5 8 2 5 F C 2 3 C 8 8 8 E 3 8 3 A 3 8 E 7 0 E A 7 A 7 7 0 7 0 7/8/03 12:53 Minimum Spanning Tree 9 7/8/03 12:53 Minimum Spanning Tree 10 Prim-Jarnik… Analysis Why It Works Graph operations Method incidentEdges is called once for each vertex n Label operations This is an application of the Cycle Property! We set/get the labels of vertex z O (deg( z )) times n Setting/getting a label takes O (1) time n Let the minimum edge at some iteration be Priority queue operations (u,v). If there is an MST not containing Each vertex is inserted once into and removed once from the n (u,v), then (u,v) completes a cycle. Since priority queue, where each insertion or removal takes O (log n ) time The key of a vertex w in the priority queue is modified at most (u,v) was considered before some other n deg( w ) times, where each key change takes O (log n ) time edge connecting v to the cluster, it must Prim-Jarnik’s algorithm runs in O (( n + m ) log n ) time provided have weight equal to or lower than that the graph is represented by the adjacency list structure other edge. A new MST can be formed by Recall that S v deg( v ) = 2 m n swapping. The running time is O ( m log n ) since the graph is connected 7/8/03 12:53 Minimum Spanning Tree 11 7/8/03 12:53 Minimum Spanning Tree 12 3
Minimum Spanning Tree 7/8/03 12:53 Dijkstra vs. Prim-Jarnik Kruskal’s Algorithm Algorithm DijkstraShortestPaths ( G, s ) Algorithm PrimJarnikMST ( G ) Q ¨ new heap-based priority queue Q ¨ new heap-based priority queue Each vertex is initially stored as its own s ¨ a vertex of G cluster. for all v Œ G.vertices () for all v Œ G.vertices () if v = s if v = s At each iteration, the minimum weight edge setDistance ( v, 0) setDistance ( v, 0) else else is added to the spanning tree if it joins 2 setDistance ( v, • ) setDistance ( v, • ) setParent ( v, ∅ ) setParent ( v, ∅ ) distinct clusters. l ¨ Q.insert ( getDistance ( v ) , v ) l ¨ Q.insert ( getDistance ( v ) , v ) setLocator ( v,l ) setLocator ( v,l ) The algorithm ends when all the vertices while ÿ Q.isEmpty () while ÿ Q.isEmpty () u ¨ Q.removeMin () u ¨ Q.removeMin () are in the same cluster. for all e Œ G.incidentEdges ( u ) for all e Œ G.incidentEdges ( u ) z ¨ G.opposite ( u,e ) z ¨ G.opposite ( u,e ) r ¨ getDistance ( u ) + weight ( e ) r ¨ weight ( e ) if r < getDistance ( z ) if r < getDistance ( z ) setDistance ( z,r ) setDistance ( z,r ) setParent ( z,e ) setParent ( z,e ) Q.replaceKey ( getLocator ( z ) ,r ) Q.replaceKey ( getLocator ( z ) ,r ) 7/8/03 12:53 Minimum Spanning Tree 13 7/8/03 12:53 Minimum Spanning Tree 14 Kruskal’s Algorithm… Example Why It Works 7 • D D 7 7 2 2 B B This is an application of the Partition 4 4 9 9 8 • 5 • 5 5 Property! F F 2 2 C C 8 8 3 3 8 8 E E A A 7 7 If the minimum edge at some iteration is 7 7 0 0 (u,v), then if we consider a partition of G 7 7 with u in one cluster and v in the other, D 7 D 2 7 2 B 4 then the partition property says that there B 4 9 • 5 9 4 5 5 F must be an MST containing (u,v). 2 5 F C 2 C 8 8 3 8 3 8 E E A 7 7 A 7 0 7 0 7/8/03 12:53 Minimum Spanning Tree 15 7/8/03 12:53 Minimum Spanning Tree 16 4
Minimum Spanning Tree 7/8/03 12:53 Kruskal’s Algorithm A priority queue stores Algorithm KruskalMST ( G ) for each vertex V in G do the edges outside the define a Cloud(v) of fl { v } cloud let Q be a priority queue. Key: weight Insert all edges into Q using their n weights as the key Element: edge n T fl ∅ At the end of the while T has fewer than n -1 edges do algorithm edge e = T.removeMin() Let u , v be the endpoints of e We are left with one if Cloud(v) ≠ Cloud(u) then n cloud that encompasses Add edge e to T the MST Merge Cloud(v) and Cloud(u) return T A tree T which is our n MST 7/8/03 12:53 Minimum Spanning Tree 17 5
Recommend
More recommend