# Lecture 16

```Minimum-Cost
Spanning Tree
CS 110: Data Structures and Algorithms
First Semester, 2010-2011
Minimum-Cost Spanning Tree
► Given
a weighted graph G, determine a
spanning tree with minimum total edge
cost
► Recall: spanning subgraph means all
vertices are included, tree means the
graph is connected and has no cycles
► Useful in applications that ensure
connectivity of nodes of optimal cost
Kruskal’s Algorithm
► Solves
the minimum-cost spanning tree
problem
► Strategy: repeatedly select the lowestcost edge as long as it does not form a
cycle with previously selected edges
► Stop when n-1 edges have been selected
(n is the number of vertices)
Kruskal’s Algorithm
► Use
a priority queue of edges to facilitate
selection of lowest-edge cost (just
disregard edges that form a cycle)
► Time complexity
O( m log m )  O( m log n )
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Kruskal’s Algorithm
2704
BOS
867
1846
ORD
740
BWI
DFW
1090
946
1235
1121
2342
1258
184
1464
LAX
144
JFK
1391
337
PVD
187
621
802
SFO
849
MIA
Pseudo-Code: Kruskal
function Kruskal( Graph g )
n &lt;-- number of vertices in g
for each vertex v in g
define an elementary cluster C(v) &lt;-- {v}
E &lt;-- all edges in G
Es &lt;-- sort(E)
T &lt;-- null // will contain edges of MCST
i &lt;-- 0
while T has edges fewer than n-1
(u, v) &lt;-- Es[i]
Let C(v) be the cluster containing v
Let C(u) be the cluster containing u
if C(v) != C(u) then
Add edge (v, u) to T
Merge C(v) and C(u) into one cluster
i = i + 1
return tree T
Prim’s Algorithm
► Start
at a specific vertex
► Choose the edge of minimum cost which
is incident on the vertex being considered
► Add the new vertex on which the
previously chosen edge is incident
► Repeat until the MCST is found
► Unlike Kruskal’s, make sure that a tree is
always build as the algorithm progresses
Prim’s Algorithm
► Start
at a specific vertex
► Choose the edge of minimum cost which
is incident on the vertex being considered
► Add the new vertex on which the
previously chosen edge is incident
► Repeat until the MCST is found
► Unlike Kruskal’s, make sure that a tree is
always build as the algorithm progresses
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
8
b
4
d
2
11
a
c
7
h
4
i
7
8
9
14
6
1
g
e
10
2
f
Prim’s Algorithm
b
8
4
c
7
d
2
a
9
4
i
h
1
g
2
e
f
Pseudo-Code: Prim
function Prim( Graph g )
select any vertex v of g
D[v] &lt;-- 0
for each vertex u != v
D[u] &lt;-- infinity
Initialize T &lt;-- null
Initalize a priority queue Q with an item ( (u, null), D[u] )
for each vertex u, where (u, null) is the element and D[u] is
the key
while Q is not empty
(u, e) &lt;-- Q.removeMin()
Add vertex u and edge e to T
for each vertex z adjacent to u such that z is in Q
if w( (u,z) ) &lt; D[z]
D[z] &lt;-- w( (u,z) )
Change to (z, (u,z)) the element of vertex z in Q
Change to D[z] the key of vertex z in Q
return the tree T
```