1 / 44

More Chapter 7: Greedy Algorithms

More Chapter 7: Greedy Algorithms. Kruskal’s Minimum Spanning Tree Algorithm. 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.

Télécharger la présentation

More Chapter 7: Greedy Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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-graphthat(i) connects all the nodes and (ii) the sum of the edge weights is minimal. • This sub-graph will always be a tree. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  3. MST Example • Given the followinggraph, find the MST • First, we want allthe nodes connected • Second, we want topick the lowest weight edges. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  4. MST Example • Greedy step 1: • Start with Aand select theminimum edge • This would connect D. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 3

  5. MST Example • Greedy step 2: • From Dselect theminimum edge • This would connect H. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 6

  6. MST Example • Greedy step 3: • From Hselect theminimum edge • This would connect I. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 11

  7. MST Example • Greedy step 4: • From Iselect theminimum edge • This would connect J. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 16

  8. MST Example • Greedy step 5: • From Jselect theminimum edge • This would connect G. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 20

  9. MST Example • Greedy step 6: • From Gselect theminimum edge • This would connect F. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 24

  10. MST Example • Greedy step 6: • From Gselect theminimum edge • This would connect F. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 24

  11. MST Example • What is the running timeof the algorithm? 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 27

  12. MST Example • N steps • Each step mustfind the minimumedge from a node • Worst case:N-1 + N-1 + N-1 + … + N-1 = O(N2) 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  13. MST Example • You might think the worst case is:N-1 + N-2 + N-3 + … + 3 + 2 + 1 = O(N2)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(N2) pick node v from the node_list. while node_listis 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 Eselect theminimum edge • This would connect B. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 33

  16. MST Example • Greedy step 9: • From Bselect theminimum edge • This would connect C. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 38

  17. MST Example • This greedyalgorithm failed • Why? 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I Total Weight: 38

  18. MST Example • It makes a localdecision. • From E, itchooses to go toB • We have to consider other options. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I 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. 4 5 A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  21. 4 5 A B C Kruskal’s 3 6 4 5 6 7 3 4 G D E F kruskal(e, n) { sort(e); 7 6 4 5 3 5 5 J H I

  22. 4 5 5 A B C B B C C 3 6 4 5 6 6 6 7 3 4 7 G D E F G D E E E F 7 6 4 5 7 6 3 5 5 A J H I H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E Kruskal’s kruskal(e, n) { sort(e);

  23. 4 5 5 A B C B B C C 3 6 4 5 6 6 6 7 3 4 7 G D E F G D E E E F 7 6 4 5 7 6 3 5 5 A J H I H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C D E F G H I J kruskal(e, n) { sort(e); for (i = A to J) makeset(i)

  24. 4 5 5 A B C B B C C 3 6 4 5 6 6 6 7 3 4 7 G D E F G D E E E F 7 6 4 5 7 6 3 5 5 A J H I H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C D E F G H I J i 1 Count 0 kruskal(e, n) { ... count = 0; i = 1

  25. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C D E F G H I J n 10 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++; }

  26. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C DH E F G I J n 10 Count 1 i 2 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++; }

  27. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C DH EF G I J n 10 Count 2 i 3 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++; }

  28. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADH B C EF G I J n 10 Count 3 i 4 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++; }

  29. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADH B C EFG I J n 10 Count 4 i 5 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++; }

  30. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHB C EFG I J n 10 Count 5 i 6 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++; }

  31. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHB CEFG I J n 10 Count 6 i 7 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++; }

  32. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHB CEFGJ I n 10 Count 7 i 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++; }

  33. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHBCEFGJ I n 10 Count 8 i 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++; }

  34. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHBCEFGJ I n 10 Count 8 i 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++; }

  35. 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E ADHBCEFGJI n 10 Count 9 i 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++; }

  36. 4 5 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  37. 4 5 5 B B C C 6 6 7 G D E E E F 7 6 A H I 3 D 3 C E F D 3 4 4 4 G F A B H F G F A 5 H I 4 5 5 5 J J I J E A B C 3 6 4 5 6 7 3 4 G D E F 7 6 4 5 3 5 5 J H I

  38. 4 5 A B C A 3 6 4 5 6 7 3 4 G D E F D E B 7 6 4 5 3 5 5 J H I H F G C J I

  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. 4 5 A B C G Minimal Spanning Tree of G Theorem 7.2.5 pp. 280 3 6 4 5 6 A 7 3 4 G D E F 7 6 4 5 D E B 3 • 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 5 5 J H I H F G C J I

  41. 4 5 4 A A B 5 7 D D E E 3 H G’ Subset of Minimal Spanning Tree of G A B C G Theorem 7.2.5 pp. 280 3 6 4 5 6 7 3 4 G D E F A C 7 6 4 5 3 • 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. 5 5 J H I D E S

  42. 4 5 4 A A B 5 7 D D E E 3 H G’ Subset of Minimal Spanning Tree of G A B C G Theorem 7.2.5 pp. 280 3 6 4 5 6 7 3 4 G D E F A C 7 6 4 5 3 • If we add a minimum weight edge from S to G’, the resulting graph is also contained in a minimal spanning tree of G 5 5 J H I D E S

  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.

More Related