









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Suppose we need to make change for 67
. We want to do
nickels, dimes and quarters are available.this using the fewest number of coins possible. Pennies,
Optimal solution for 67
has six coins: two quarters, one
dime, a nickel, and two pennies.
We can use a
greedy algorithm
to solve this problem:
remaining sum, until the desired sum is obtained.repeatedly choose the largest coin less than or equal to the
This is how millions of people make change every day (*).
CS404/
Computer Science
1
Design and Analysis of Algorithms: Lecture 19
Let
d
1 , d
2
, ..., d
k
}
be a finite set of distinct coin
denominations. Example:
d
1
d
2
d
3
, and
d
4
We can assume each
d
i
is an integer and
d
1
> d
2
> ... > d
k .
Each denomination is available in unlimited quantity.
The Coin-Changing problem:
Make change for
n
cents, using a minimum total number
of coins.
Assume that
d k
= 1 so that there is always a solution.
CS404/
Computer Science
2
Design and Analysis of Algorithms: Lecture 19
Step (i):
Characterize the structure of a coin-change solution
Define
j ] to be the minimum number of coins we need to
make change for
j
cents.
making change forIf we knew that an optimal solution for the problem of
j
cents used a coin of denomination
d
i ,
we would have:
j ] = 1 +
C [ j − d i
CS404/
Computer Science
4
Design and Analysis of Algorithms: Lecture 19
Step (ii):
Recursively define the value of an optimal solution
j ] =
if
j <
if
j
min
1 ≤ i ≤ k { C [ j − d i ] }
if
j
CS404/
Computer Science
5
Design and Analysis of Algorithms: Lecture 19
[11] = min
[30] = min
% $
CS404/
Computer Science
7
Design and Analysis of Algorithms: Lecture 19
Step (iii):
Compute values in a bottom-up fashion.
Avoid examining
j ] for
j <
0 by ensuring that
j
d
i
before
looking up
C [ j − d i
C
C
n, d, k
for j := 1 to n do
j ] :=
for i := 1 to k do
if
j
d
i
and 1 +
C [ j − d i ]
j ] then
j ] := 1 +
C [ j − d i ]
return
c
Complexity: Θ(
nk
CS404/
Computer Science
8
Design and Analysis of Algorithms: Lecture 19
denom, j
if
j >
denom, j
denom
j ])
denom
j ]
Initial call is PRINT-COINS(
denom, n
Example: CS404/
Computer Science
10
Design and Analysis of Algorithms: Lecture 19
Usually the complexity of a DP algorithm is:
of sub-problems
choices for each sub-problem
For example:
0/1 Knapsack Problem:
i,̟
] = max( C[
i
−
i
−
w
i ] +
p
i ).
sub-problems, each needs to check
choices.
nM
C Matrix Chain Multiplication:
i, j
] = min
i ≤
k
Step (i):
Characterize the structure of a coin-change solution
Making change for
j
cents with coins
d 1
, d
2
, ..., d
i
can be
d i
(even if it’s possible):
i, j
i
−
, j
d
i
and reduce the amount:
C
i, j
i, j
d i ].
We will pick the solution with least number of coins:
i, j
] = min(
i
−
, j
i, j
d
i ])
CS404/
Computer Science
13
Design and Analysis of Algorithms: Lecture 19
Step (ii):
Recursively define the value of an optimal solution
i, j
if
j <
if
j
j
if
i
= 0
min
{ C [ i − 1
, j
i, j
d
i ] }
if
j
CS404/
Computer Science
14
Design and Analysis of Algorithms: Lecture 19
Suppose we have coin set
d
1 , d
2
, d
3 }
c,
c,
c }
and
n
c .
d3 = 6 | 0 1 2 3 1 2 1 2 2d2 = 4 | 0 1 2 3 1 2 3 4 2d1 = 1 | 0 1 2 3 4 5 6 7 8----------------------------- C[i,j] | 0 1 2 3 4 5 6 7 8
8] = min(
d
3
]) = min(
Evidently, the optimal solution does NOT use
d
3
.
CS404/
Computer Science
16
Design and Analysis of Algorithms: Lecture 19
Step (iv):
Construct an optimal solution.
Two strategies:
Online: use an additional matrix
..k,
..n
], where
i, j
indicates which of the values
i
−
, j
] and
i, j
d
i ] was
used to compute
i, j
] (use two symbols:
and
Compute
in parallel with
optimal solution by starting backwards fromBatch: recover the denominations of the coins used in the
k, n
], after
computing the entire matrix
space complexity. HW exercise: write the pseudocode for each, analyze time & CS404/
Computer Science
17
Design and Analysis of Algorithms: Lecture 19