Download - Chapter 7: Greedy Algorithms
Chapter 7: Greedy Algorithms
Kruskal’s, Prim’s, and Dijkstra’s Algorithms
Kruskal’s Algorithm
• Solves the Minimum Spanning Tree Problem
• Input:– List of edges in a graph– n – the number of vertices
• Output:– Prints the list of edges in the Minimum
Spanning Tree
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
Kruskal’skruskal(e, n) {
sort(e);
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
Kruskal’skruskal(e, n) {
sort(e);
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
kruskal(e, n) {
sort(e);
for (i = A to J)
makeset(i)
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C D E F G H I J
kruskal(e, n) {
...
count = 0;
i = 1
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C D E F G H I J
Count
0
i
1
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C D E F G H I J
count
0
i
1
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C DH E F G I J
Count
1
i
2
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C DH EF G I J
Count
2
i
3
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADH B C EF G I J
Count
3
i
4
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADH B C EFG I J
Count
4
i
5
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHB C EFG I J
Count
5
i
6
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHB CEFG I J
Count
6
i
7
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHB CEFGJ I
Count
7
i
8
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHBCEFGJ I
Count
8
i
9
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHBCEFGJ I
Count
8
i
10
n
10
kruskal(e, n) {
while (count < n-1) {
if (findset(e[i].v) != findset(e[i].w)) {
print(e[i].v + “ ”+ e[i].w);
count++;
union(e[i].v, e[i].w);
}
i++;
}
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
ADHBCEFGJI
Count
9
i
11
n
10
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A
D
3
D
H
3
E F3 A B4
C
F
4
F G4
G
J
4
A
E
5
H I5
I J5
F
J
5
B C5
B
E
6
C
G
6
F
I
6
D E7
E
H
7
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
A
B
C
D E
F
G
H
I
J
Theorem 7.2.5 pp. 280
• Let G be a connected, weighted graph, and let G’ be a sub-graph of a minimal spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G’, the resulting graph is also contained in a minimal spanning tree of G
Theorem 7.2.5 pp. 280• Let G be a connected, weighted graph, and let
G’ be a sub-graph of a minimal spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G’, the resulting graph is also contained in a minimal spanning tree of G
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
G A
B
C
D E
F
G
H
I
J
Minimal Spanning Tree of G
Theorem 7.2.5 pp. 280• G’ be a sub-graph of a minimal
spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C.
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
G G’ Subset of Minimal
Spanning Tree of G
S
C A
D E
A B4
A
E
5 D E7D
H
3
Theorem 7.2.5 pp. 280• If we add a minimum
weight edge in S to G’, the resulting graph is also contained in a minimal spanning tree of G
A B C
D E F G
H I J
4 5
7 3 4
5 5
3 6
7
4
6 43
5 6
5
G G’ Subset of Minimal
Spanning Tree of G
S
C A
D E
A B4
A
E
5 D E7D
H
3
Theorem 7.2.6: Kruskal’s Algorithm finds minimum spanning tree
Proof by induction
• G’ is a sub-graph constructed by Kruskal’s Algorithm
• G’ is initially empty but each step of the Algorithm increases the size of G’
• Inductive Assumption: G’ is contained in the MST.
Theorem 7.2.6: Kruskal’s Algorithm finds minimum spanning tree
Proof by induction• Let (v,w) be the next edge selected by Kruskal’s
Algorithm• Kruskal’s algorithm finds the minimum weight
edge (v,w) such that v and w are not already in G’• C can be any subset of the MST, so you can
always construct a C such that v is in C and w is not.
• Therefore, by Theorem 7.2.5, when (v,w) is added to G’, the resulting graph is also contained in the MST.