CS 312: Algorithm Analysis - PowerPoint PPT Presentation

dacian
cs 312 algorithm analysis n.
Skip this Video
Loading SlideShow in 5 Seconds..
CS 312: Algorithm Analysis PowerPoint Presentation
Download Presentation
CS 312: Algorithm Analysis

play fullscreen
1 / 32
Download Presentation
CS 312: Algorithm Analysis
94 Views
Download Presentation

CS 312: Algorithm Analysis

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #19: Greedy Algorithms and Minimal Spanning Trees Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick and figures from Dasgupta et al.

  2. Announcements • HW #12 • Due now • Mid-Term Exam • Testing Center: ends Thursday • 3 hours max – beware closing time! • 1 page of notes written/typed by you • Project #4: Intelligent Scissors • Questions?

  3. Objectives • Define a greedy algorithm • Solve the coins problem with a greedy algorithm • Define the Minimal Spanning Tree (MST) problem • Understand Kruskal’s Algorithm • Prove correctness of Kruskal’s Algorithm

  4. Given: unbounded supply of coins of various denominations. Given: a number Find: minimal number of coins that add up to . Learning Activity Need a volunteer to solve the problem Explain why you did what you did at every step Everyone: write an algorithm to solve this problem (on scratch paper) Volunteer to write algorithm on the board. Coins Problem

  5. Greedy Algorithms: Main Idea • Optimize some quantity of interest • “optimize”=minimize/maximize • Build up a solution piece by piece • Always choose the next piece that offers the most obvious and immediate benefit • Without violating given constraints

  6. function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  Generalizing Greedy Algs.

  7. function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  Greedy takes a set C and returns: Optionally: a set An optimal value for the quantity of interest OR A result  indicating failure Generalizing Greedy Algs.

  8. function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  Start off with S empty, since we haven’t added anything to the solution Generalizing Greedy Algs.

  9. function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  As long as … There are still candidates to choose from And S is not a solution… (solution() function returns true iff S is a solution.) Generalizing Greedy Algs.

  10. function greedy (C) Input: set of candidates C Output: solution S (a set) ,optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  Select the next-best candidate from C Remove this candidate from C Add this candidate to S, if it’s feasible. select(): choose the next best candidate feasible(): if x is added, is it possible to get a solution? Generalizing Greedy Algs.

  11. function greedy (C) Input: set of candidates C Output: solution S (a set) ,optimal quantity S  while (C !=  & !solution(S)) x  select(C) C  C \ {x} iffeasible(S{x}) then S  S  {x} ifsolution(S) thenreturn S elsereturn  After going through the loop, either S is a solution or 2. there aren’t any solutions Generalizing Greedy Algs.

  12. Returning to Coins Algorithm • What are the candidates? • What is the solution set? • Solution test? • Select fn.? • Feasibility test? • Objective?

  13. Problem: Minimal Spanning Trees • Given: A connected, undirected graph G=V,E • V = a finite set of vertices • E  VV = a finite set of edges • w:ER0 = a cost function. 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7

  14. Problem: Minimal Spanning Trees • Given: A connected, undirected graph G=V,E • V = a finite set of vertices • E  VV = a finite set of edges • w:ER0 = a cost function. • Find: a set of edges that • includes every vertex in V • forms a tree (i.e., no cycles) • has minimum possible cost 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7

  15. Problem: Minimal Spanning Trees • Two solutions: • Kruskal’s Algorithm • Prim’s Algorithm • (You’ve seen these before in CS 236) • (More depth this time) • Application: • Networking computers • Plumbing • Electrical network • Approximation to Hamiltonianpath! • … 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7

  16. Kruskal’s Algorithm • Idea: Repeatedly add the next lightest edge that doesn’t produce a cycle. • Greedy?

  17. 1 2 1 2 3 5 6 6 4 4 3 8 4 5 6 7 4 3 7 Kruskal’s Algorithm Sort edges by cost 1: {1,2} 2: {2,3} 3: {4,5} 3: {6,7} 4: {1,4} 4: {2,5} 4: {4,7} 5: {3,5}

  18. Kruskal’s Algorithm Make each vertex a component 1 2 1: {1,2} {1}{2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  19. Kruskal’s Algorithm Add first edge to T 1 2 1: {1,2} {1}{2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  20. Kruskal’s Algorithm Merge vertices in added edges 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  21. Kruskal’s Algorithm Process each edge in order 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  22. Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  23. Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  24. Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  25. Kruskal’s Algorithm Must join separate components 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} rejected 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7

  26. Kruskal’s Algorithm Stop when all vertices connected 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} rejected 4: {2,5} 7 4 {1,2,3,4,5,6,7} done 3 4: {4,7} 5: {3,5} 7

  27. Kruskal’s Algorithm Candidate Set Solution Set Selection Function Feasibility Test Solution Test break if size(X) == |V|-1 • Identify: • Important operations • Elements of a Greedy algorithm Objective: Minimize Spanning Tree Cost

  28. Kruskal’s Algorithm break if size(X) == |V|-1 • Identify: • Important operations • Elements of a Greedy algorithm Objective: Minimize Spanning Tree Cost

  29. 3 Questions • Is it correct? • Now • How long does it take? • Next time • Can we do better? • Next time

  30. Correctness • Depends on the idea of a “cut” • Cut Property (a Lemma): Suppose edges X are part of a minimum spanning tree of G=(V,E). Pick any subset of nodes S for which X does not cross between S and V-S, and let e be the lightest edge across this partition. Then X U {e} is part of some MST

  31. Correctness: Kruskal’s Algorithm Theorem: Kruskal’s Algorithm finds a minimum spanning tree Basis: X = is part of a minimum spanning tree This is because: a) part of every set b) An MST must exist since G is connected

  32. Correctness: Kruskal’s Algorithm Theorem: Kruskal’s Algorithm finds a minimum spanning tree Induction Step: Assume is part of an MST S 1 2 •  E • Choose S to be a connected component on one end of the edge, , chosen by Kruskall’s • S vs. V-S is a cut • No edge in leaves S • is lightest edge that leaves • Cut Property holds • U {e} is part of an MST u 5 6 6 4 4 3 8 v 7 4 3