greedy algorithms i

Download Greedy Algorithms I

Post on 02-Feb-2017

218 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Lecture slides by Kevin WayneCopyright 2005 Pearson-Addison Wesley

    http://www.cs.princeton.edu/~wayne/kleinberg-tardos

    Last updated on 7/25/17 11:26 AM

    4. GREEDY ALGORITHMS I

    coin changing interval scheduling and partitioning scheduling to minimize lateness optimal caching

  • 4. GREEDY ALGORITHMS I

    coin changing interval scheduling and partitioning scheduling to minimize lateness optimal caching

  • Coin changing

    Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a methodto pay amount to customer using fewest coins.Ex. 34.

    Cashiers algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid.

    Ex. $2.89.

    3

  • Cashiers algorithm

    At each iteration, add coin of the largest value that does not take us past

    the amount to be paid.

    Q. Is cashiers algorithm optimal?

    4

    CASHIERS-ALGORITHM (x, c1, c2, , cn) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    SORT n coin denominations so that 0 < c1 < c2 < < cnS

    WHILE x > 0

    k largest coin denomination ck such that ck x

    IF no such k, RETURN no solution

    ELSE

    x x ck

    S S { k }

    RETURN S_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    multiset of coins selected

  • Properties of optimal solution

    Property. Number of pennies 4. Pf. Replace 5 pennies with 1 nickel.

    Property. Number of nickels 1. Property. Number of quarters 3.

    Property. Number of nickels + number of dimes 2. Pf.

    Replace 3 dimes and 0 nickels with 1 quarter and 1 nickel; Replace 2 dimes and 1 nickel with 1 quarter. Recall: at most 1 nickel.

    5

    quarters(25)

    dimes (10)

    nickels (5)

    pennies (1)

  • Analysis of cashiers algorithm

    Theorem. Cashiers algorithm is optimal for U.S. coins: 1, 5, 10, 25, 100. Pf. [by induction on x]

    Consider optimal way to change ck x < ck+1 : greedy takes coin k. We claim that any optimal solution must also take coin k.

    - if not, it needs enough coins of type c1, , ck1 to add up to x - table below indicates no optimal solution can do this

    Problem reduces to coin-changing x ck cents, which, by induction, is optimally solved by cashiers algorithm.

    k ckall optimal solutions

    must satisfy

    1 1 P 4

    2 5 N 1

    3 10 N + D 2

    4 25 Q 3

    5 100 no limit6

    max value of coins c1, c2, , ck1 in any optimal solution

    4

    4 + 5 = 9

    20 + 4 = 24

    75 + 24 = 99

  • Cashiers algorithm for other denominations

    Q. Is cashiers algorithm optimal for any set of denominations? A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.

    Cashiers algorithm: 140 = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1. Optimal: 140 = 70 + 70.

    A. No. It may not even lead to a feasible solution if c1 > 1: 7, 8, 9.

    Cashiers algorithm: 15 = 9 + ???. Optimal: 15 = 7 + 8.

    7

  • 4. GREEDY ALGORITHMS I

    coin changing interval scheduling and partitioning scheduling to minimize lateness optimal caching

  • Interval scheduling

    Job j starts at sj and finishes at fj. Two jobs compatible if they dont overlap. Goal: find maximum subset of mutually compatible jobs.

    9

    time0 1 2 3 4 5 6 7 8 9 10 11

    f

    g

    h

    e

    a

    b

    c

    d

    h

    e

    b

    jobs d and g are incompatible

  • Interval scheduling: greedy algorithms

    Greedy template. Consider jobs in some natural order. Take each job provided its compatible with the ones already taken.

    [Earliest start time] Consider jobs in ascending order of sj.

    [Earliest finish time] Consider jobs in ascending order of fj.

    [Shortest interval] Consider jobs in ascending order of fj sj.

    [Fewest conflicts] For each job j, count the number ofconflicting jobs cj. Schedule in ascending order of cj.

    10

  • Interval scheduling: greedy algorithms

    Greedy template. Consider jobs in some natural order. Take each job provided its compatible with the ones already taken.

    11

    counterexample for earliest start time

    counterexample for shortest interval

    counterexample for fewest conflicts

  • Interval scheduling: earliest-finish-time-first algorithm

    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 sj fj* . Sorting by finish time takes O(n log n) time.

    12

    EARLIEST-FINISH-TIME-FIRST (n, s1, s2, , sn , f1, f2, , fn) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    SORT jobs by finish time so that f1 f2 fnA

    FOR j = 1 TO n

    IF job j is compatible with A

    A A { j }

    RETURN A_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    set of jobs selected

  • Interval scheduling: analysis of earliest-finish-time-first algorithm

    Theorem. The earliest-finish-time-first algorithm is optimal.

    Pf. [by contradiction]

    Assume greedy is not optimal, and lets see what happens. Let i1, i2, ... ik denote set of jobs selected by greedy. Let j1, j2, ... jm denote set of jobs in an optimal solution with

    i1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r.

    13

    why not replace job jr+1 with job ir+1?

    job ir+1 exists and finishes before jr+1

    i1 i2 ir ir+1Greedy: ik. . .

    j1 j2 jr j

Recommended

View more >