csc 213
DESCRIPTION
CSC 213. Lecture 24: Minimum Spanning Trees. Announcements. Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205. Spanning subgraph Subgraph including every vertex Spanning tree Spanning subgraph that is also a tree Minimum spanning tree - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/1.jpg)
CSC 213
Lecture 24:Minimum Spanning Trees
![Page 2: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/2.jpg)
Announcements
Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205
![Page 3: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/3.jpg)
Minimum Spanning Tree (MST)Spanning subgraph
Subgraph including every vertex
Spanning tree Spanning subgraph
that is also a tree
Minimum spanning tree Spanning tree with
minimum total edge weight
ORD
PIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
![Page 4: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/4.jpg)
Cycle PropertyCycle Property:
MSTs do not contain cycles
So weight of each edge in MST is lower than weight of edge which completes cycle
Proof: e is edge in G & not in MST C is cycle formed when
adding e to MST For every edge f in C,
weight(f) weight(e) If weight(f) weight(e), then
exists a smaller spanning tree with e instead of f
84
2 36
7
7
9
8e
C
f
84
2 36
7
7
9
8
C
e
f
Replacing f with e yieldsa better spanning tree
![Page 5: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/5.jpg)
U V
Partition PropertyPartition Property:
Given partitions of vertices, minimum weight edge is in a MST connecting partitions
Proof: e is edge with smallest
weight connecting partitions, but not in MST
f connects partitions in MST If MST does not contain e,
consider cycle formed by including e with MST
By cycle property,weight(f) ≤ weight(e)
But since e has smallest weight,weight(f) = weight(e)
Create another MST by replacing f with e
74
2 85
7
3
9
8 e
f
74
2 85
7
3
9
8 e
f
Replacing f with e yieldsanother MST
U V
![Page 6: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/6.jpg)
Kruskal’s Algorithm
Add edges to MST greedily Priority queue contains edges outside
the cloud Element: edge Key: edge’s weight
Only want edges that will not make a cycle Unfortunately, detecting cycles can be
hard…
![Page 7: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/7.jpg)
Data Structs for Kruskal Algorithm
Algorithm maintains forest of treesEdge accepted when connecting treesUse partition (i.e., collection of disjoint sets): find(u): return set containing u union(u,v): replace sets with u & v
with their union
![Page 8: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/8.jpg)
Partition Representation
Each set is SequenceEach element refers to its set makeSet(u) takes O(1) time find(u) takes O(1) time union(u,v) moves elements of smaller set
to Sequence of larger set and updates references
By having union move smaller set into large set, elements are processed only when set size doubles n calls to union() takes O(log n) time
![Page 9: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/9.jpg)
Kruskal’s Algorithm
Algorithm KruskalMST(Graph G)Q new PriorityQueue()T new Graph()P new Partition()for each vertex v in G
P.makeSet(v)T.insertVertex(v)
for each edge e in GQ.insert(e.getWeight(), e);
while T.edges().getSize() < n-1Edge e = Q.removeMin()Assign u, v to G.endpoints(e)if P.find(u) P.find(v) then
T.insertEdge(e) P.union(P.find(u),P.find(v))
return T
![Page 10: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/10.jpg)
Kruskal Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 11: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/11.jpg)
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
Example
![Page 12: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/12.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 13: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/13.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 14: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/14.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 15: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/15.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 16: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/16.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 17: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/17.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 18: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/18.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 19: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/19.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 20: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/20.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 21: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/21.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 22: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/22.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 23: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/23.jpg)
Example
JFK
BOS
MIA
ORD
LAXDFW
SFO BWI
PVD
8672704
187
1258
849
144740
1391
184
946
1090
1121
2342
1846 621
802
1464
1235
337
![Page 24: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/24.jpg)
Prim-Jarnik’s Algorithm
Similar to Dijkstra’s algorithmPick arbitrary vertex s and grow MST as cloud of verticesLabel each vertex v with d(v) --- smallest weight of edge connecting v to vertex in cloud At each step: Add vertex u with smallest distance to cloud Update labels of vertices adjacent to u
![Page 25: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/25.jpg)
Prim-Jarnik’s Algorithm (cont.)
Priority queue stores vertices outside of cloud Key: distance Element: vertexLocator-based methods insert(k,e) – returns locator replaceKey(l,k) – changes item’s key
Store three labels with each vertex Distance from cloud Edge connecting vertex to cloud (MST edge) Locator in priority queue
![Page 26: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/26.jpg)
Prim-Jarnik’s Algorithm (cont.)
Algorithm PrimJarnikMST(Graph G)Q new PriorityQueue()s G.vertices().elemAtRank(0)for each vertex v in G
if v = sv.setDistance(0)
else v.setDistance()
v.setParent(null)l Q.insert(getDistance(v), v)
setLocator(v,l)while !Q.isEmpty()
u Q.removeMin() for each edge e in G.incidentEdges(u)
z G.opposite(u,e)r e.getWeight()if r < z.getDistance()
z.setDistance(r)z.setParent(e)
Q.replaceKey(z.getLocator(),r)
![Page 27: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/27.jpg)
Example
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
8
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5
7
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5
7
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5 4
7
![Page 28: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/28.jpg)
Example (contd.)
BD
C
A
F
E
74
28
5
7
3
9
8
03
2
5 4
7
BD
C
A
F
E
74
28
5
7
3
9
8
03
2
5 4
7
![Page 29: CSC 213](https://reader036.vdocument.in/reader036/viewer/2022070412/568149f3550346895db7227b/html5/thumbnails/29.jpg)
Prim-Jarnik’s AnalysisOperations incidentEdges called once per vertex Set/Get each vertex’s labels O(deg(v)) times
Priority queue operations Each vertex inserted once and removed
once Each insertion/removal takes O(log n)
Vertex w’s key modified O(deg(w)) times Each key change takes O(log n) time
Algorithm takes O((n m) log n) time using adjacency list structure