Download presentation
Presentation is loading. Please wait.
1
Chapter 7: Greedy Algorithms
Kruskal’s, Prim’s, and Dijkstra’s Algorithms
2
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
3
4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J
4
Kruskal’s kruskal(e, n) { sort(e); 4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7
H I J
5
Kruskal’s kruskal(e, n) { sort(e); A B C D E F G H I J 4 5 7 3 6 A E 5
6
kruskal(e, n) { sort(e); for (i = A to J) makeset(i) A B C D E F G H I
4 5 7 3 6 A B C D E F G H I J kruskal(e, n) { sort(e); for (i = A to J) makeset(i) A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
7
kruskal(e, n) { ... count = 0; i = 1 A B C D E F G H I J 4 5 7 3 6 A B
A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
8
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 B C D E F G H I J n 10 count i 1 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
9
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 B C DH E F G I J n 10 Count 1 i 2 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
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 B C DH EF G I J n 10 Count 2 i 3 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
11
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++; ADH B C EF G I J n 10 Count 3 i 4 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
12
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++; ADH B C EFG I J n 10 Count 4 i 5 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
13
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++; ADHB C EFG I J n 10 Count 5 i 6 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
14
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++; ADHB CEFG I J n 10 Count 6 i 7 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
15
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++; ADHB CEFGJ I n 10 Count 7 i 8 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
16
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++; ADHBCEFGJ I n 10 Count 8 i 9 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
17
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++; ADHBCEFGJ I n 10 Count 8 i 10 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
18
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++; ADHBCEFGJI n 10 Count 9 i 11 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
19
4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
20
4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7
21
4 5 A B C A 3 6 4 5 6 7 3 4 D E F G D E B 7 6 4 5 3 5 5 H I J H F G C J I
22
Theorem 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
23
G Theorem 7.2.5 pp. 280 4 5 A B C Minimal Spanning Tree of G 3 6 4 5 6
D E F G 7 6 4 5 3 D E B 5 5 H I J H F Theorem 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 G C J I
24
G C G’ Subset of Minimal Spanning Tree of G S Theorem 7.2.5 pp. 280 4
3 6 4 5 6 7 3 4 D E F G A 7 6 4 C 5 3 5 5 H I J D E Theorem 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 4 A E 5 D H 3 D E 7 S
25
G C G’ Subset of Minimal Spanning Tree of G S Theorem 7.2.5 pp. 280
4 5 G’ Subset of Minimal Spanning Tree of G A B C G 3 6 4 5 6 7 3 4 D E F G A 7 6 4 C 5 3 5 5 H I J D E Theorem 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 4 A E 5 D H 3 D E 7 S
26
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.
27
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.