350 likes | 510 Views
CSC 213 – Large Scale Programming. Lecture 35: Spanning Trees. Minimum Spanning Tree (MST). Spanning subgraph Subgraph w/ all vertices. ORD. 10. 1. PIT. DEN. 6. 7. 9. 3. DCA. STL. 4. 5. 8. 2. DFW. ATL. Minimum Spanning Tree (MST). Spanning subgraph
E N D
CSC 213 – Large Scale Programming Lecture 35:Spanning Trees
Minimum Spanning Tree (MST) • Spanning subgraph • Subgraph w/ all vertices ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL
Minimum Spanning Tree (MST) • Spanning subgraph • Subgraph w/ all vertices ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL
Minimum Spanning Tree (MST) • Spanning subgraph • Subgraph w/ all vertices • Spanning tree • Combinesspanning subgraph+ tree ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL
Minimum Spanning Tree (MST) • Spanning subgraph • Subgraph w/ all vertices • Spanning tree • Combinesspanning subgraph+ tree • MST • Spanning tree which minimizes sum of edge weights ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL
f f 8 8 4 4 9 9 6 6 2 2 3 3 e e 7 7 8 8 7 7 No Cycles in MST • Edge in MST cheaper than one making cycle • Assume there exists an edge enot in MST • Cycle,C, occurs after adding e to min. spanning tree • Assume that finCheavier than e • Shrink MST using eand dropping f from C
Partition Property • Given partitioning of vertices in a graph • Required that each vertex in exactly1 partition • Use smallest edge to connect each of the • To complete following MST, can use either fore 7 4 9 5 2 8 3 8 7
Kruskal’s Algorithm • Similar to Prim-Jarnik, including finding MST • Also like Prim-Jarnik, adds edges greedily • But Kruskal processes Edgesusing PriorityQueue • Check ifEdgemakes cyclebefore adding to MST • No cycles in an MST, so addEdgeif no cycle occurs • Detecting cycles hard, however
Kruskal’s Algorithm • Similar to Prim-Jarnik, including finding MST • Also like Prim-Jarnik, adds edges greedily • But Kruskal processes Edgesusing PriorityQueue • Check ifEdgemakes cyclebefore adding to MST • No cycles in an MST, so addEdgeif no cycle occurs • Detecting cycles hard, however
Kruskal’s Algorithm • Similar to Prim-Jarnik, including finding MST • Also like Prim-Jarnik, adds edges greedily • But Kruskal processes Edgesusing PriorityQueue • Check ifEdgemakes cyclebefore adding to MST • No cycles in an MST, so addEdgeif no cycle occurs • Detecting cycles hard, however
Structure for Kruskal’s Algorithm • Kruskal’s needs to maintain collection of trees • Accept Edge connecting 2 trees & reject it otherwise • Data structure named Partition relied upon • Store disjoint Set instances within a Partition • For Kruskal’s, each Setholds tree from graph • Methods supporting Sets defined byPartition • find(u):find and returns Set containing u • union(p,r):replace p & rwith their union • makeSet(u): creates Set for u & adds to Partition
Kruskal’s Algorithm AlgorithmKruskalMST(Graph G) Q newPriorityQueue() T new Graph() P new Partition() for (Vertex v : G.vertices()) P.makeSet(v) T.insertVertex(v) for (Edge e : G.edges()) Q.insert(e.getWeight(), e); while (T.numEdges() < T.numVertices()-1) Edge e = Q.removeMin().value() Assign u, v to G.endpoints(e) if P.find(u) P.find(v) T.insertEdge(e) P.union(P.find(u),P.find(v)) return T
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 1846 144 JFK 621 184 802 1258 SFO 1464 BWI 1391 1090 337 946 DFW 1235 LAX 1121 MIA 2342
For Next Lecture • Weekly assignment available on Angel • Due at special time: before next Monday’s quiz • Programming assignment #3 designs due today • Graph Quiz will be on Monday • Started before test, so could include implementations • Bring notes, templates, & anything else you want • Weekly assignment question review highly encouraged