chapter 7: greedy algorithms
DESCRIPTION
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. 4. 5. A. B. C. 3. 6. 4. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/1.jpg)
Chapter 7: Greedy Algorithms
Kruskal’s, Prim’s, and Dijkstra’s Algorithms
![Page 2: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/3.jpg)
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
![Page 4: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/5.jpg)
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
![Page 6: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/6.jpg)
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
![Page 7: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/7.jpg)
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
![Page 8: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/8.jpg)
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
![Page 9: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/11.jpg)
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
![Page 12: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/12.jpg)
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
![Page 13: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/13.jpg)
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
![Page 14: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/16.jpg)
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
![Page 17: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/17.jpg)
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
![Page 18: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/18.jpg)
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
![Page 19: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/21.jpg)
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
![Page 22: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/22.jpg)
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
![Page 23: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/23.jpg)
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
![Page 24: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/24.jpg)
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
![Page 25: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/25.jpg)
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
![Page 26: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/26.jpg)
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.
![Page 27: Chapter 7: Greedy Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022081420/56814f4f550346895dbcf407/html5/thumbnails/27.jpg)
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.