lecture 35: spanning trees
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
LECTURE 35:SPANNING TREES
CSC 213 – Large Scale Programming
Minimum Spanning Tree (MST)
Spanning subgraph Subgraph w/ all
vertices
ORDPIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
Minimum Spanning Tree (MST)
Spanning subgraph Subgraph w/ all
vertices
ORDPIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
Minimum Spanning Tree (MST)
Spanning subgraph Subgraph w/ all
vertices Spanning tree
Combinesspanning subgraph + tree
ORDPIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
Minimum Spanning Tree (MST)
Spanning subgraph Subgraph w/ all
vertices Spanning tree
Combinesspanning subgraph + tree
MST Spanning tree which
minimizes sum of edge weights
ORDPIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
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
84
2 36
7
7
9
8e
f 84
2 36
7
7
9
8e
f
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 e7
4
28
5
7
3
9
8
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 AlgorithmAlgorithm 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
8672704
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
187
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
Kruskal Example
8672704
187
1258
849
144740
1391
9461090
1121
2342
1846621
8021464
1235
337
BOS
PVD
JFK
ORD
BWI184
MIA
DFWLAX
SFO
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