Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.
-
Upload
ferdinand-lee -
Category
Documents
-
view
215 -
download
1
Transcript of Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.
![Page 1: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/1.jpg)
Minimum-Cost Spanning TreeCS 110: Data Structures and
Algorithms
First Semester, 2010-2011
![Page 2: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/2.jpg)
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
![Page 3: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/3.jpg)
Kruskal’s Algorithm
►Solves the minimum-cost spanning tree problem
►Strategy: repeatedly select the lowest-cost 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)
![Page 4: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/4.jpg)
Kruskal’s Algorithm
►Use a priority queue of edges to facilitate selection of lowest-edge cost (just disregard edges that form a cycle)
►Time complexityO( m log m ) O( m log n )
![Page 5: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/5.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 6: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/6.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 7: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/7.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 8: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/8.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 9: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/9.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 10: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/10.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 11: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/11.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 12: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/12.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 13: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/13.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 14: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/14.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 15: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/15.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 16: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/16.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 17: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/17.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 18: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/18.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 19: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/19.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 20: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/20.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 21: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/21.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 22: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/22.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 23: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/23.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Kruskal’s Algorithm
![Page 24: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/24.jpg)
Pseudo-Code: Kruskalfunction Kruskal( Graph g )
n <-- number of vertices in gfor each vertex v in g
define an elementary cluster C(v) <-- {v}E <-- all edges in GEs <-- sort(E)T <-- null // will contain edges of MCSTi <-- 0while T has edges fewer than n-1
(u, v) <-- Es[i]Let C(v) be the cluster containing vLet C(u) be the cluster containing uif C(v) != C(u) then
Add edge (v, u) to TMerge C(v) and C(u) into one cluster
i = i + 1return tree T
![Page 25: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/25.jpg)
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
![Page 26: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/26.jpg)
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
![Page 27: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/27.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 28: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/28.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 29: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/29.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 30: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/30.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 31: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/31.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 32: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/32.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 33: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/33.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 34: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/34.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 35: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/35.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 36: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/36.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
9
14
10
4
21
8
11
7
2
6
![Page 37: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/37.jpg)
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
4
8 7
94
21
2
![Page 38: Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester, 2010-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062720/56649f125503460f94c266b7/html5/thumbnails/38.jpg)
Pseudo-Code: Primfunction Prim( Graph g )
select any vertex v of gD[v] <-- 0for each vertex u != v
D[u] <-- infinityInitialize T <-- nullInitalize 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 keywhile Q is not empty
(u, e) <-- Q.removeMin()Add vertex u and edge e to Tfor each vertex z adjacent to u such that z is in Q
if w( (u,z) ) < D[z]D[z] <-- w( (u,z) )Change to (z, (u,z)) the element of vertex z
in QChange to D[z] the key of vertex z in Q
return the tree T