greedy algorithms greed is good greedy algorithms
play

Greedy algorithms: greed is good? Greedy algorithms Greed, for lack - PowerPoint PPT Presentation

Greedy algorithms: greed is good? Greedy algorithms Greed, for lack of a better word, Coin Changing, Interval Scheduling, Interval Partitioning is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures, the Tyler Moore


  1. Greedy algorithms: greed is good? Greedy algorithms Greed, for lack of a better word, Coin Changing, Interval Scheduling, Interval Partitioning is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures, the Tyler Moore essence of the evolutionary spirit. Greed, in all of its forms; greed CS 2123, The University of Tulsa for life, for money, for love, knowledge, has marked the upward surge of mankind and greed, you mark my words, will Some slides created by or adapted from Dr. Kevin Wayne. For more information see not only save Teldar Paper, but http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused from Python Algorithms by Magnus Lie that other malfunctioning Hetland. corporation called the U.S.A. 2 / 24 Greedy algorithms Coin changing Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins. A greedy algorithm builds a solution incrementally, making the best local decision to construct a global solution Ex. 34¢. The clever thing about greedy algorithms is that they find ways to consider only a portion of the solution space at each step We’ve already seen one greedy algorithm Cashier's algorithm. At each iteration, add coin of the largest value that Gale-Shapley algorithm to solve stable-matching problem: men propose does not take us past the amount to be paid. to their best choice, women accept/decline without considering other prospective offers Ex. $2.89. 3 3 / 24 4 / 24

  2. Cashier's algorithm Properties of optimal solution Property. Number of pennies ≤ 4. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Pf. Replace 5 pennies with 1 nickel. C ASHIERS -A LGORITHM ( x, c 1 , c 2 , …, c n ) Property. Number of nickels ≤ 1. _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ Property. Number of quarters ≤ 3. SORT n coin denominations so that c 1 < c 2 < … < c n S ← φ set of coins selected Property. Number of nickels + number of dimes ≤ 2. WHILE x > 0 Pf. k ← largest coin denomination c k such that c k ≤ x ・ Replace 3 dimes and 0 nickels with 1 quarter and 1 nickel; IF no such k, RETURN "no solution" ・ Replace 2 dimes and 1 nickel with 1 quarter. ELSE ・ Recall: at most 1 nickel. x ← x – c k S ← S ∪ { k } RETURN S _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ Q. Is cashier's algorithm optimal? 4 5 5 / 24 6 / 24 Analysis of cashier's algorithm Cashier's algorithm for other denominations Theorem. Cashier's algorithm is optimal for U.S. coins: 1, 5, 10, 25, 100. Q. Is cashier's algorithm for any set of denominations? Pf. [by induction on x] ・ Consider optimal way to change c k ≤ x < c k +1 : greedy takes coin k . A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. ・ We claim that any optimal solution must also take coin k . ・ Cashier's algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1. - if not, it needs enough coins of type c 1 , …, c k –1 to add up to x ・ Optimal: 140¢ = 70 + 70. - table below indicates no optimal solution can do this ・ Problem reduces to coin-changing x – c k cents, which, by induction, is optimally solved by cashier's algorithm. ▪ all optimal solutions max value of coins k c k must satisfy c 1 , c 2 , …, c k–1 in any OPT P ≤ 4 – 1 1 N ≤ 1 2 5 4 A. No. It may not even lead to a feasible solution if c 1 > 1 : 7, 8, 9. N + D ≤ 2 3 10 4 + 5 = 9 ・ Cashier's algorithm: 15¢ = 9 + ???. Q ≤ 3 4 25 20 + 4 = 24 ・ Optimal: 15¢ = 7 + 8. 5 100 no limit 75 + 24 = 99 6 7 7 / 24 8 / 24

  3. Interval scheduling Interval scheduling: greedy algorithms ・ Job j starts at s j and finishes at f j . Greedy template. Consider jobs in some natural order. ・ Two jobs compatible if they don't overlap. Take each job provided it's compatible with the ones already taken. ・ Goal: find maximum subset of mutually compatible jobs. ・ [Earliest start time] Consider jobs in ascending order of s j . ・ [Earliest finish time] Consider jobs in ascending order of f j . a ・ [Shortest interval] Consider jobs in ascending order of f j – s j . b b c ・ [Fewest conflicts] For each job j , count the number of d conflicting jobs c j . Schedule in ascending order of c j . jobs d and g are incompatible e e f g h h time 0 1 2 3 4 5 6 7 8 9 10 11 9 10 9 / 24 10 / 24 Interval scheduling: greedy algorithms Interval scheduling: earliest-finish-time-first algorithm Greedy template. Consider jobs in some natural order. E ARLIEST -F INISH -T IME -F IRST ( n, s 1 , s 2 , …, s n , f 1 , f 2 , …, f n ) Take each job provided it's compatible with the ones already taken. _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ S ORT jobs by finish time so that f 1 ≤ f 2 ≤ … ≤ f n A ← φ counterexample for earliest start time set of jobs selected F OR j = 1 TO n I F job j is compatible with A A ← A ∪ { j } R ETURN A counterexample for shortest interval _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ counterexample for fewest conflicts Proposition. Can implement earliest-finish-time first in O ( n log n ) time. ・ Keep track of job j * that was added last to A . ・ Job j is compatible with A iff s j ≥ f j * . ・ Sorting by finish time takes O ( n log n ) time. 11 12 11 / 24 12 / 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend