csc 213
Post on 15-Jan-2016
38 Views
Preview:
DESCRIPTION
TRANSCRIPT
CSC 213
Lecture 24:Minimum Spanning Trees
Announcements
Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205
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
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
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
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…
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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
top related