Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.

Similar presentations


Presentation on theme: "More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm."— Presentation transcript:

1 More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.

2 Minimum Spanning Tree (MST) Problem Given a weighted graph, i.e. a graph with edge weights… try to find a sub-graph that (i) connects all the nodes and (ii) the sum of the edge weights is minimal. This sub-graph will always be a tree. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

3 MST Example Given the following graph, find the MST First, we want all the nodes connected Second, we want to pick the lowest weight edges. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

4 MST Example Greedy step 1: Start with A and select the minimum edge This would connect D. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 3

5 MST Example Greedy step 2: From D select the minimum edge This would connect H. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 6

6 MST Example Greedy step 3: From H select the minimum edge This would connect I. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 11

7 MST Example Greedy step 4: From I select the minimum edge This would connect J. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 16

8 MST Example Greedy step 5: From J select the minimum edge This would connect G. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 20

9 MST Example Greedy step 6: From G select the minimum edge This would connect F. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 24

10 MST Example Greedy step 6: From G select the minimum edge This would connect F. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 24

11 MST Example What is the running time of the algorithm? ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 27

12 MST Example N steps Each step must find the minimum edge from a node Worst case: N-1 + N-1 + N-1 + … + N-1 = O(N 2 ) ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

13 MST Example You might think the worst case is: N-1 + N-2 + N-3 + … + 3 + 2 + 1 = O(N 2 ) because at every step you connect a node and don’t have to consider it’s edges. However, think about how the algorithm would actually be implemented, and how you would keep track of this info?

14 MST Example You might think the worst case is: N-1 + N-2 + N-3 + … + 3 + 2 + 1 = O(N 2 ) pick node v from the node_list. while node_list is not empty { mark v as visited. min_hop = infinity; foreach of v’s edges (v,w) if (w is not visited) if (edge (v,w) < min_hop) { min_hop = edge(v,w) min_edge = w; } remove v from node from node_list v = w; } The first while loop will always take N iterations The foreach loop could take N-1 iteration in a complete graph

15 MST Example Greedy step 8: From E select the minimum edge This would connect B. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 33

16 MST Example Greedy step 9: From B select the minimum edge This would connect C. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 38

17 MST Example This greedy algorithm failed Why? ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 38

18 MST Example It makes a local decision. From E, it chooses to go to B We have to consider other options. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 Total Weight: 38

19 Kruskal’s Algorithm Solves the Minimum Spanning Tree Problem using a better Greedy Approach Input: –List of edges in a graph –n – the number of vertices Output: –Prints the list of edges in the Minimum Spanning Tree

20 ABC DEF G HI J 45 734 55 36 7 4 64 3 56 5

21 Kruskal’s kruskal(e, n) { sort(e); ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

22 Kruskal’s kruskal(e, n) { sort(e); ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7

23 kruskal(e, n) { sort(e); for (i = A to J) makeset(i) ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ

24 kruskal(e, n) {... count = 0; i = 1 ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ Count 0 i1i1

25 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ count 0 i1i1 n 10

26 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDHEFGIJ Count 1 i2i2 n 10

27 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDHEFGIJ Count 2 i3i3 n 10

28 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 3 i4i4 n 10

29 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 4 i5i5 n 10

30 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 5 i6i6 n 10

31 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 6 i7i7 n 10

32 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 7 i8i8 n 10

33 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 8 i9i9 n 10

34 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 8 i 10 n 10

35 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 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 9 i 11 n 10

36 A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

37 A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5

38 ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 A B C DE F G H I J

39 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

40 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 ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 G A B C DE F G H I J Minimal Spanning Tree of G

41 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. ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 G G’ Subset of Minimal Spanning Tree of G S C A DE AB 4 A E 5 DE 7 D H 3

42 Theorem 7.2.5 pp. 280 If we add a minimum weight edge from S to G’, the resulting graph is also contained in a minimal spanning tree of G ABC DEF G H I J 45 734 55 36 7 4 64 3 56 5 G G’ Subset of Minimal Spanning Tree of G S C A DE AB 4 A E 5 DE 7 D H 3

43 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.

44 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.


Download ppt "More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm."

Similar presentations


Ads by Google