The Coin Changing Problem The Coin Changing Problem
The Coin Changing Problem The Coin Changing Problem
The Coin Changing Problem The Coin Changing Problem
'
The
The Coin
Coin Changing
Changing problem
problem
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
The
The Coin-Changing
Coin-Changing problem:
problem: formal
formal
description
description
Let D = {d1, d2, ..., dk } be a finite set of distinct coin
denominations. Example: d1 = 25, d2 = 10, d3 = 5, and
d4 = 1.
We can assume each di is an integer and d1 > d2 > ... > dk .
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 dk = 1 so that there is always a solution.
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
The
The Greedy
Greedy Method
Method (works
(works in
in the
the US)
US)
For the coin set { 25, 10, 5, 1}, the greedy method
always finds the optimal solution.
Exercise: prove it.
It may not work for other coin sets. For example it stops
working if we knock out the nickel.
Example: D = { 25, 10, 1} and n = 30. The Greedy
method would produce a solution:
25 + 5 1, which is not as good as 3 10.
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
A
A Dynamic
Dynamic Programming
Programming Solution:
Solution: Step
Step (i)
(i)
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
A
A Dynamic
Dynamic Programming
Programming Solution:
Solution: Step
Step (ii)
(ii)
if j < 0,
if j = 0,
C[j] = 0
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
An
An example:
example: coin
coin set
set {{ 50
50,, 25
25,, 10
10,, 1
1}}
C[0] = 0;
C[1] = min
1 + C[1 50] =
C[2] = min
1 + C[1 25] =
1 + C[1 10] =
1 + C[1 1] = 1
1 + C[2 50] =
1 + C[2 25] =
1 + C[2 10] =
1 + C[2 1] = 2
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
An
An example
example
1 + C[11 50] =
1 + C[11 25] =
C[11] = min
1 + C[11 10] = 2
1 + C[11 1]
=2
{ 1, 10 }
{ 10, 1 }
C[30] = min
1 + C[30 50] =
&
CS404/504
{ 10, 10, 10 }
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
A
A Dynamic
Dynamic Programming
Programming Solution:
Solution: Step
Step (iii)
(iii)
Step (iii): Compute values in a bottom-up fashion.
Avoid examining C[j] for j < 0 by ensuring that j di before
looking up C[j di].
COMPUTE-CHANGE(n, d, k)
C[0] := 0
for j := 1 to n do
C[j] :=
for i := 1 to k do
if j di and 1 + C[j di] < C[j] then
C[j] := 1 + C[j di]
return c
Complexity: (nk).
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
A
A Dynamic
Dynamic Programming
Programming Solution:
Solution: Step
Step (iv)
(iv)
Step (iv): Construct an optimal solution.
We use an additional array denom[1..n], where denom[j] is the
denomination of a coin used in an optimal solution to the
problem of making change for j cents.
COMPUTE-CHANGE(n, d, k)
C[0] := 0
for j := 1 to n do
C[j] :=
for i := 1 to k do
if j di and 1 + C[j di] < C[j] then
C[j] := 1 + C[j di]
denom[j] := di
return c
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
'
Step
Step (iv):
(iv): Print
Print optimal
optimal solution
solution
PRINT-COINS(denom, j)
PRINT-COINS(denom, j denom[j])
print denom[j]
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
10
'
Time
Time complexity
complexity of
of DP
DP algorithms
algorithms
Usually the complexity of a DP algorithm is:
# of sub-problems choices for each sub-problem
For example: 0/1 Knapsack Problem:
C[i, ] = max( C[i 1, ], C[i 1, - wi] + pi).
n M sub-problems, each needs to check 2 choices.
(nM )
Matrix Chain Multiplication:
C[i, j] = minik<j {C[i, k] + C[k + 1, j] + rows[Ai ] col[Ak ] col[Aj ]}
n n sub-problems, each needs to check O(n) choices
O(n3)
Coin Changing Problem: size of C = n, k possible coin types
for each C[j]. (nk).
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
11
'
Another
Another Dynamic
Dynamic Programming
Programming Solution
Solution
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
12
'
Another
Another Dynamic
Dynamic Programming
Programming Solution
Solution
1 + C[i, j di])
Computer Science
13
'
Another
Another Dynamic
Dynamic Programming
Programming Solution
Solution
if j < 0,
C[i, j] = 0
if j = 0,
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
14
'
Another
Another Dynamic
Dynamic Programming
Programming Solution
Solution
Step (iii): Compute values in a bottom-up fashion.
Avoid examining C[i, j] for j < 0 by ensuring that j di before
looking up C[j di]. Overall time complexity is (nk).
COMPUTE-CHANGE(d, k, n)
for i := 1 to k
C[i, 0] := 0
for i := 1 to k
for j := 1 to n
if j < di then
C[i, j] := C[i 1, j]
else
C[i, j] := min(C[i 1, j], 1 + C[i, j di])
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
15
'
Example:
Example: Bottom-up
Bottom-up computation
computation
Suppose we have coin set {d1, d2, d3} = {1c, 4c, 6c} and
n = 8c.
C[i,j] | 0 1 2 3 4 5 6 7 8
----------------------------d1 = 1 | 0 1 2 3 4 5 6 7 8
d2 = 4 | 0 1 2 3 1 2 3 4 2
d3 = 6 | 0 1 2 3 1 2 1 2 2
C[3, 8] = min(C[2, 8], 1 + C[3, 8 d3]) = min(2, 1 + 2)
Evidently, the optimal solution does NOT use d3.
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
16
'
Another
Another Dynamic
Dynamic Programming
Programming Solution
Solution
Step (iv): Construct an optimal solution.
Two strategies:
Online: use an additional matrix S[1..k, 0..n], where S[i, j]
indicates which of the values C[i 1, j] and C[i, j di] was
used to compute C[i, j] (use two symbols: and ).
Compute S in parallel with C.
Batch: recover the denominations of the coins used in the
optimal solution by starting backwards from C[k, n], after
computing the entire matrix C.
HW assignment: write the pseudocode for each, analyze time
& space complexity.
&
CS404/504
Computer Science
Design and Analysis of Algorithms: Lecture 19
17