Lecture 9. Dynamic Programming: optimal coin change - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 9. Dynamic Programming: optimal coin change

Description:

Lecture 9. Dynamic Programming: optimal coin change We have seen this problem before: you are given an amount in cents, and you want to make ... – PowerPoint PPT presentation

Number of Views:734
Avg rating:3.0/5.0
Slides: 6
Provided by: csUwater
Category:

less

Transcript and Presenter's Notes

Title: Lecture 9. Dynamic Programming: optimal coin change


1
Lecture 9. Dynamic Programming optimal coin
change
  • We have seen this problem before you are given
    an amount in cents, and you want to make change
    using a system of denominations, using the
    smallest number of coins possible. Sometimes the
    greedy algorithm gives the optimal solution. But
    sometimes (as we have seen) it does not -- an
    example was the system (12, 5, 1), where the
    greedy algorithm gives 15 12 1 1 1 but a
    better answer is 15 5 5 5. And sometimes
    Greedy cannot even find the proper changes
    (25,10,4), change for 41 cents.
  • So how can we find the optimal solution (fewest
    coins) when the greedy algorithm may not work?
    One way is using dynamic programming.

2
Optimal coin-changing --
  • The idea to make change for n cents, the optimal
    method must use some denomination di. That is,
    the optimal is made by choosing the optimal
    solution for n di for some di, and adding one
    coin of di to it. We don't know which di to use,
    but some must work. So we try them all, assuming
    we know how to make optimal changes for lt n
    cents.
  • To formalize letting optj be the optimal
    number of coins to make change for j cents, we
    have
  • optj 1 min( optj-di) over all
    dij

3
Optimal coin-change
  • coins(n, d1..k)
  • / returns optimal number of coins to make
    change for n using denominations d1..k, with
    d1 1 /
  • for j 1 to n do
  • optj infinity
  • for i k downto 1 do
  • if di j then
  • optj 1
  • largestj j
  • else if di lt j then
  • a 1optj-di
  • if a lt optj then
  • optj a
  • largestj di
  • return(optn)

Running time O(nk) Input size ?
4
Example.
j optj largestj 15 3 5
16 4 5 17 5 5 18
1 18 19 2 18 20 3 18
21 4 18 22 5 18 23
2 18 24 3 18 25 1 25
26 2 25 27 3 25
28 3 18 29 4 18
  • j optj largestj
  • 1 1 1
  • 2 2 1
  • 3 3 1
  • 4 4 1
  • 5 1 5
  • 6 2 5
  • 7 3 5
  • 8 4 5
  • 9 5 5
  • 10 2 5
  • 11 3 5
  • 12 4 5
  • 13 5 5
  • 14 6 5
  • Suppose we use the system of denominations
    (1,5,18,25). To represent 29, the greedy
    algorithm gives 251111,
  • Our algorithm largest coin 18. 29-1811, which
    has a representation of size 3, with largest coin
    5. 11-56, which has a representation of size 2,
    with largest coin 5. 6-51. So 29 18 5 5
    1.

5
Paradigm 7. Exploiting the problems structure
  • Example Computing GCD
  • Euclids Alg (300BC)
  • We say d is a divisor of u if there exists an
    integer k such that u kd. We write d u. We
    say d is a common divisor of u and v if d u and
    d v. We say d is the greatest common divisor if
    d is a common divisor of u and v and no other
    common divisor is larger.
  • Example gcd(12,18)6.
  • Observe
  • d u d v iff d v and d (u mod v)
  • Euclid(u,v)
  • while (v ? 0) do
  • (u, v) (v, u mod v)
  • return(u)
  • Thm (Finck 1841). This algorithm runs in O(log v)
    steps, for ugtvgt0.
  • Proof Let r u mod v.
  • Case 1. vgtu/2, then r u-v lt u/2
  • Case 2. vu/2, then r0
  • Case 3. vlt u/2, then rltvltu/2
  • So after 2 steps, the numbers decrease by
    factor of 2. Hence after O(log v) steps, to 0.
Write a Comment
User Comments (0)
About PowerShow.com