Upload
branxton
View
79
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Minimum-Cost Spanning TreeCS 110: Data Structures and
AlgorithmsFirst 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
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)
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 )
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
914
10
4
21
8
117
2
6
Prim’s Algorithm
a
b
g
c
f
e
d
h
i
48 7
94
21
2
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