Try   HackMD

Ch16: Greedy Algorithms

tags: algorithm

16-1 Coin changing

Consider the problem of making change for

n cents using the fewest number of coins. Assume that each coin’s value is an integer.

a. Describe a greedy algorithm to make change consisting of quarters, dimes, nickels, and pennies. Prove that your algorithm yields an optimal solution.

Algorithm

  1. Pick the coin with the largest denomination of
    dkn
    cents and add it to the change.
  2. Update
    n
    to the remaining owed amount.
  3. Repeat 1. and 2. until the full amount is payed.

Proof

In order to prove our solution optimal, we have to show that our solution satisfies the greedy-choice property and exhibits optimal substructure.

  • Let the owed change be
    n
    cents.
  • Let
    D={1,5,10,25}
    represent all existing denominations sorted in monotonically-increasing order.
1. Greedy-choice property

The first key ingredient is the greedy-choice property: we can assemble a globally optimal solution by making locally optimal (greedy) choices. textbook, p.424

Consider the optimal way to change

dkn<dk+1 (
d5=
): The greedy choice takes coin
ck
.

  • Claim: Any optimal solution
    Opt
    must also take coin
    ck
    .

Say

Opt does not include
ck
, then
Opt
must only consist of coins
c1...ck1
. This means that coins
c1...ck1
must add up to
n
.

k
dk
Constraints Max. value of
c1...ck1
in any
Opt
1 1
P4
2 5
N1
1×4=4
3 10
N+D2
4+5×1=9
4 25
unlimited
4+10×2=24

As we can see from the chart,

i=1k1count(ci)di<dkn, which shows that no optimal solution exists under this assumption.

Therefore, coin

ck is indeed the greedy choice to make. This reduces the problem to coin-changing
ndk
cents, which can again be optimally solved using the greedy-choice property.

2. Optimal substructure

A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. textbook, p.425

Let

M[i] be the minimum number of coins needed to coin-change
i
cents.
Suppose
Opt
is an optimal solution to
M[i]
, then:

  • Opt{c1}
    is an optimal solution to
    M[id1]
  • Opt{c2}
    is an optimal solution to
    M[id2]
  • Opt{c3}
    is an optimal solution to
    M[id3]
  • Opt{c4}
    is an optimal solution to
    M[id4]

M[i]=min1j4(M[idj])+1

Therefore, by substituting

M[i=n], the
{1,5,10,25}
cashier's algorithm exhibits optimal substructure.

b. Suppose that the available coins are in the denominations that are powers of
c
, i.e., the denominations are
c0,c1,...,ck
for some integers
c>1
and
k1
. Show that the greedy algorithm always yields an optimal solution.

1. Greedy-choice property

  • Conjecture: Making the greedy choice at every iteration leads to a globally optimal solution.

Let

j be such that
cjn<cj+1(j>kj=k)
and the greedy algorithm returns
A={cj}+A
where
A
is a solution. Let
Opt
be an optimal solution for
n
.

  1. Basis step
    If

    A=, then
    n=cj
    and
    A={cj}=Opt
    .

  2. Inductive step
    Assume

    A=Opt for
    ncj
    , then
    A={cj}+A={cj}+Opt=Opt
    .
    According to our conjecture,
    Opt
    must also contain a
    cj
    .
    Suppose
    Opt
    does not contain
    cj
    , then
    Opt=i=0j1count(ci)ci=ncj
    .
    If any
    count(ci)c
    ,
    count(ci)cicci=ci+1
    . This would violate the optimality of
    Opt
    since
    count(ci)
    number of coins could be traded for a single
    ci+1
    coin.
    Opt=i=0j1count(ci)ci(c1)i=0j1ci=(c1)cj1c1=cj1

    A contradiction is reached. Thus
    Opt
    must contain a
    cj
    .

We showed that making the greedy choice

cj at every iteration leads to the optimal solution
Opt
. This proves the greedy-choice property.

2. Optimal substructure

To prove that the greedy algorithm exhibits optimal substructure, we let

cjn<cj+1(j>kj=k) and let
Opt
be an optimal solution.

We assume that

Opt=Opt{cj} is an optimal solution for
ncj
. If it were not, then
|Opt|<|Opt|
for an optimal solution
Opt
. This means
|Opt{cj}|<|Opt|
, which is a contradiction since
Opt
is an optimal solution.

Thus,

Opt=Opt{cj} is an optimal solution for
ncj
. This proves the optimal substructure property.

c. Give a set of coin denominations for which the greedy algorithm does not yield an optimal solution. Your set should include a penny so that there is a solution for every value of
n
.

  • Let
    n=10
    and
    D={1,5,6}
    .
    Greedy yields
    |{6,1,1,1,1}|=5
    but the optimal solution is
    |{5,5}|=2
    .
  • Let
    n=30
    and
    D={1,10,25}
    .
    Greedy yields
    |{25,1,1,1,1,1}|=6
    but the optimal solution is
    |{10,10,10}|=3
    .

d. Give an
O(nk)
-time algorithm that makes change for any set of
k
different coin denominations, assuming that one of the coins is a penny.

// D = list of k different coin denominations function MINIMUM-CHANGE(n,D) { let count = 0 // number of coins added to change sort D in ascending order // O(n) if we sort in linear time while n > 0 // O(n) for i = D.length downto 1 // O(k) if D[i] <= n do increment count subtract D[i] from n return count }
  • T(n)=
    sort:
    O(n)
    +
    while loop:
    O(n)
    ×
    for loop:
    O(k)=O(nk)