CSC 213 – Large Scale Programming LECTURE 35: SPANNING TREES Minimum Spanning Tree (MST) Spanning subgraph ORD Subgraph w/ all vertices 10 1 PIT DEN 9 7 3 STL 4 8 DFW 6 DCA 5 2 ATL Minimum Spanning Tree (MST) Spanning subgraph ORD Subgraph w/ all vertices 10 1 PIT DEN 7 9 4 DCA STL 5 DFW 2 ATL Minimum Spanning Tree (MST) Spanning subgraph ORD Subgraph w/ all vertices Spanning tree 1 DEN Combines spanning subgraph + tree 4 10 PIT 6 STL 7 DCA 2 DFW ATL Minimum Spanning Tree (MST) Spanning subgraph ORD Subgraph w/ all vertices Spanning tree 1 DEN Combines 6 7 3 spanning subgraph + tree MST PIT 4 STL DCA 2 Spanning tree which minimizes sum of edge weights DFW ATL No Cycles in MST Edge in MST cheaper than one making cycle Assume there exists an edge e not in MST Cycle, C, occurs after adding e to min. spanning tree Assume that f in C heavier than e Shrink MST using e and dropping f from C 8 f 8 f 4 2 9 6 3 8 7 4 2 e 7 9 6 3 8 7 e 7 Partition Property Given partitioning of vertices in a graph Required that each vertex in exactly 1 partition Use smallest edge to connect each of the To complete following MST, can use either f or e 7 2 9 5 8 8 7 4 3 Kruskal’s Algorithm Similar to Prim-Jarnik, including finding MST Also like Prim-Jarnik, adds edges greedily But Kruskal processes Edges using PriorityQueue Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if 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 Edges using PriorityQueue Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if 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 Edges using PriorityQueue Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if 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 Set holds tree from graph Methods supporting Sets defined by Partition find(u): find and returns Set containing u union(p,r): replace p & r with their union makeSet(u): creates Set for u & adds to Partition Kruskal’s Algorithm Algorithm KruskalMST(Graph G) Q new PriorityQueue() 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 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 ORD 740 1846 621 SFO JFK PVD 144 184 802 1464 187 BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 187 ORD 1846 621 SFO PVD 144 184 802 1464 JFK BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 187 ORD 1846 621 SFO PVD 144 184 802 1464 JFK BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 849 187 ORD 1846 621 SFO PVD 144 184 802 1464 JFK BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 187 ORD 1846 621 SFO PVD 144 184 802 1464 JFK BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 867 187 ORD 1846 621 SFO PVD 144 184 802 1464 JFK BOS 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 BOS 187 ORD 1846 621 SFO 144 184 802 1464 JFK PVD 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 BOS 187 ORD 1846 621 SFO 144 184 802 1464 JFK PVD 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 BOS 187 ORD 1846 621 SFO 144 184 802 1464 JFK PVD 1391 1258 BWI 1090 337 LAX 1235 946 DFW 1121 MIA 2342 Kruskal Example 2704 BOS 187 ORD 1846 621 184 802 SFO 1464 JFK 1391 BWI 337 LAX 1235 946 DFW 1121 MIA 2342 PVD 144 1258 Kruskal Example 2704 BOS 187 ORD 1846 621 184 802 SFO 1464 JFK 1391 BWI 337 LAX 1235 DFW 946 MIA 2342 PVD 144 1258 Kruskal Example 2704 BOS 187 ORD 1846 621 184 802 SFO 1464 JFK 1391 BWI 337 LAX 1235 DFW 946 MIA 2342 PVD 144 1258 Kruskal Example 2704 BOS 187 ORD 1846 621 184 802 SFO 1464 JFK 1391 BWI 337 LAX 1235 DFW 946 MIA 2342 PVD 144 1258 Kruskal Example 2704 BOS 187 ORD 1846 621 184 802 SFO 1464 JFK 1391 BWI 337 LAX 1235 DFW 946 MIA 2342 PVD 144 Kruskal Example 2704 BOS 187 ORD 1846 621 802 SFO 1464 JFK 184 BWI 337 LAX 1235 DFW 946 MIA 2342 PVD 144 Kruskal Example BOS 187 ORD 621 802 SFO JFK 184 BWI 337 LAX 1235 DFW 946 MIA PVD 144 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