minimum spanning tree 11 26 2003 9 54 am
play

Minimum Spanning Tree 11/26/2003 9:54 AM Minimum Spanning Trees - PDF document

Minimum Spanning Tree 11/26/2003 9:54 AM Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 187 740 144 1846 JFK 621 184 1258 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 1


  1. Minimum Spanning Tree 11/26/2003 9:54 AM Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 187 740 144 1846 JFK 621 184 1258 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 1 Outline and Reading Minimum Spanning Trees (§7.3) � Definitions � A crucial fact The Prim-Jarnik Algorithm (§7.3.2) Kruskal's Algorithm (§7.3.1) Baruvka's Algorithm (§7.3.3) Minimum Spanning Trees v1.3 2 Minimum Spanning Tree Spanning subgraph ORD Subgraph of a graph G 10 � containing all the vertices of G 1 PIT Spanning tree DEN Spanning subgraph that is 6 � 7 itself a (free) tree 9 Minimum spanning tree (MST) 3 DCA STL Spanning tree of a weighted 4 � graph with minimum total 8 5 edge weight 2 Applications Communications networks DFW ATL � Transportation networks � Minimum Spanning Trees v1.3 3 1

  2. Minimum Spanning Tree 11/26/2003 9:54 AM Prim-Jarnik’s Algorithm Like Dijkstra’s algorithm only simpler Grow the MST from arbitrary vertex s Greedily add vertices into cloud based on distance to any vertex in cloud At v, need to store d ( v ) = minimum weight edge connecting v to a cloud vertex At each step: � We add to the cloud the vertex u outside the cloud with the smallest distance label � We update the labels of the vertices adjacent to u (edge relaxation) Minimum Spanning Trees v1.3 4 Prim’s Edge Relaxation Consider an edge e = ( u,z ) such that d ( z ) = 15 10 u is the vertex most recently � e u added to the cloud z s z is not in the cloud 15 � The relaxation of edge e updates distance d ( z ) as follows: d ( z ) ← min{ d ( z ) ,e } 10 d ( z ) = 10 e u z s 15 Minimum Spanning Trees v1.3 5 Prim’s Example ∞ 7 7 D 7 D 2 2 B B 4 4 9 ∞ 9 ∞ 8 5 5 5 F F 2 2 C C 8 8 3 3 8 8 E E A A 7 7 7 7 0 0 7 7 7 D D 2 7 2 B 4 B 4 9 ∞ 5 9 4 5 5 F 2 5 F C 2 C 8 8 3 8 3 8 E E A 7 7 A 7 0 7 0 Minimum Spanning Trees v1.3 6 2

  3. Minimum Spanning Tree 11/26/2003 9:54 AM Example (contd.) 7 D 7 2 B 4 9 4 5 5 F 2 C 8 3 8 E A 3 7 7 0 D 7 2 B 4 9 4 5 5 F 2 C 8 3 8 E A 3 7 0 Minimum Spanning Trees v1.3 7 Cycle Property 8 f Cycle Property: 4 C Let T be a minimum 9 � spanning tree of a 6 2 3 weighted graph G e 7 Let e be an edge of G 8 � that is not in T and C let 7 be the cycle formed by e with T Replacing f with e yields For every edge f of C, a better spanning tree � weight ( f ) ≤ weight ( e ) 8 Proof: f 4 By contradiction � C 9 If weight ( f ) > weight ( e ) we � 6 can get a spanning tree 2 3 e of smaller weight by 7 8 replacing e with f 7 Minimum Spanning Trees v1.3 8 Correctness of Prim’s Let T k be tree produced by Prim’s after kth iteration. Let G k be the the subgraph of G induced by T k . Then T k is a MST of G k . Minimum Spanning Trees v1.3 9 3

  4. Minimum Spanning Tree 11/26/2003 9:54 AM Prim-Jarnik’s Algorithm (cont.) A priority queue stores Algorithm PrimJarnikMST ( G ) Q ← new heap-based priority queue the vertices outside the s ← a vertex of G cloud for all v ∈ G.vertices () Key: distance if v = s � setDistance ( v, 0) Element: vertex � else Locator-based methods setDistance ( v, ∞ ) setTreeEdge ( v, ∅ ) insert ( k,e ) returns a � l ← Q.insert ( getDistance ( v ) , v ) locator setLocator ( v,l ) replaceKey ( l,k ) changes while ¬ Q.isEmpty () � the key of an item u ← Q.removeMin () for all e ∈ G.incidentEdges ( u ) We store three labels z ← G.opposite ( u,e ) with each vertex: r ← weight ( e ) if r < getDistance ( z ) Distance � setDistance ( z,r ) Tree edge in MST � setTreeEdge ( z,e ) Locator in priority queue Q.replaceKey ( getLocator ( z ) ,r ) � Minimum Spanning Trees v1.3 10 Example graph Start at 1, run Prim’s 6 2 7 1 2 3 8 7 10 19 20 9 6 4 21 3 5 Minimum Spanning Trees v1.3 11 Analysis Graph operations Method incidentEdges is called once for each vertex � Label operations We set/get the distance, tree and locator labels of vertex z O (deg( z )) � times Setting/getting a label takes O (1) time � Priority queue operations Each vertex inserted and removed once taking O (log n ) time each time � for 2n times. The key of a vertex w in the priority queue is modified at most deg( w ) � times, where each key change takes O (log n ) time Prim-Jarnik’s algorithm runs in O (( n + m ) log n ) time provided the graph is represented by the adjacency list structure The running time is O ( m log n ) since the graph is connected What is running time for unsorted-sequence based priority queue? Minimum Spanning Trees v1.3 12 4

  5. Minimum Spanning Tree 11/26/2003 9:54 AM Kruskal’s MST algorithm Another greedy strategy for finding MST Gradually turn forest into tree as edges are added Add cheapest edge possible � Don’t add edge if it forms cycle Overview: kruskalMST (Graph G) Initalize F (forest) to empty. Place all edges in PQ according to cost For each edge (u,v) in PQ (in sorted order) if (u,v) does not make a cycle in F add (u,v) to F return F; Minimum Spanning Trees v1.3 13 Partition Property U V 7 f Partition Property: 4 Consider a partition of the vertices of 9 � 5 G into subsets U and V 2 8 Let e be an edge of minimum weight � 8 3 across the partition e There is a minimum spanning tree of � 7 G containing edge e Replacing f with e yields Proof: another MST Let T be an MST of G � If T does not contain e, consider the U V � 7 cycle C formed by e with T and let f f be an edge of C across the partition 4 9 By the cycle property, � 5 2 weight ( f ) ≤ weight ( e ) 8 Thus, weight ( f ) = weight ( e ) � 3 8 e We obtain another MST by replacing � f with e 7 Minimum Spanning Trees v1.3 14 Kruskal’s Algorithm Each vertex starts in its Algorithm KruskalMST ( G ) for each vertex V in G do own cloud (a partition) define a Cloud(v) of � { v } Clouds merge together as let Q be a priority queue. edges are added Insert all edges into Q using their weights as the key A priority queue stores T � ∅ edges in weight order while T has fewer than n -1 edges do edge e = T.removeMin() Key: weight � Let u , v be the endpoints of e Element: edge if Cloud(v) ≠ Cloud(u) then � Only edges between Add edge e to T Merge Cloud(v) and Cloud(u) clouds will not form return T cycles add cheapest edge � between clouds At end of algorithm: All vertices in one cloud � Edges added form MST � Minimum Spanning Trees v1.3 15 5

  6. Minimum Spanning Tree 11/26/2003 9:54 AM Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 16 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 17 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 18 6

  7. Minimum Spanning Tree 11/26/2003 9:54 AM Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 19 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 20 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 21 7

  8. Minimum Spanning Tree 11/26/2003 9:54 AM Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 22 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 23 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 24 8

  9. Minimum Spanning Tree 11/26/2003 9:54 AM Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 25 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 26 Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees v1.3 27 9

Recommend


More recommend