• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
CSG713 Advanced Algorithms
Dynamic Programming Example
Fall 2004
September 27, 2004
Dynamic Programming Solution to the
Coin Changing Problem
(1) Characterize the Structure of an Optimal Solution.The Coin Changing problem exhibits opti-

mal substructure in the following manner. Consider any optimal solution to making change forn cents using coins of denominationsd1 ,d2 ,...,dk. Now consider breaking that solution into two di\ufb00erent pieces along any coin boundary. Suppose that the \u201cleft-half\u201d of the solution amounts tob cents and the \u201cright-half\u201d of the solution amounts ton\u2212 b cents, as shown below.

b
\ue001
\ue004\ue003
\ue002
d1
d3
d1
d2
n\u2212b
\ue001
\ue004\ue003
\ue002
d4
d5
d2
Claim 1The left-half of the solution must be an optimal way to make change forb cents using coins of
denominationsd1 ,d2 ,...,dk, and the right-half of the solution must be an optimal way to make change for
n\u2212 bcents using coins of denominations d1 ,d2 ,...,dk.
Proof:By contradiction, suppose that there was a better solution to making change forb cents than the

\u201cleft-half\u201d of the optimal solution shown. Then the left-half of the optimal solution could be replaced with this better solution, yielding a valid solution to making change forn cents with fewer coins than the solution being considered. But this contradicts the supposed optimality of the given solution,\u2192\u2190. An identical argument applies to the \u201cright-half\u201d of the solution.

2
Thus, the optimal solution to the coin changing problem is composed of optimal solutions to smaller
subproblems.
(2) Recursively De\ufb01ne the Value of the Optimal Solution.First, we de\ufb01ne in English the quantity

we shall later de\ufb01ne recursively. LetC[p] be the minimum number of coins of denominationsd1 ,d2 ,...,dk needed to make change forp cents. In the optimal solution to making change forp cents, there must exist some \ufb01rst coindi, wheredi\u2264 p. Furthermore, the remaining coins in the optimal solution must themselves be the optimal solution to making change forp\u2212 di cents, since coin changing exhibits optimal substructure as proven above. Thus, ifdi is the \ufb01rst coin in the optimal solution to making change forp cents, then

C[p] = 1+ C[p\u2212 di]; i.e., one dicoin plus C[p\u2212 di] coins to optimally make change for p\u2212 dicents. We don\u2019t

know which coindi is the \ufb01rst coin in the optimal solution to making change forp cents; however, we may check allk such possibilities (subject to the constraint thatdi\u2264 p), and the value of the optimal solution must correspond to the minimum value of 1 +C[p\u2212 di], by de\ufb01nition. Furthermore, when making change for 0 cents, the value of the optimal solution is clearly 0 coins. We thus have the following recurrence.

Claim 2C[p] =
\ue000
0
ifp= 0
mini:di\u2264p{1 +C [p\u2212 di]}ifp > 0
Proof:The correctness of this recursive de\ufb01nition is embodied in the paragraph which precedes it.
2
1
(3) Compute the Value of the Optimal Solution Bottom-up.Consider the following piece of pseu-
docode, whered is the array of denomination values,k is the number of denominations, andn is the amount
for which change is to be made.
Change(d, k, n)
1C [0]\u2190 0
2forp\u2190 1ton
3
min\u2190\u221e
4
fori\u21901 tok
5
ifd[i]\u2264 p then
6
if1 +C [p\u2212 d[i]]<min then
7
min\u21901 +C [p \u2212d[i]]
8
coin\u2190i
9
C[p]\u2190min
10
S[p]\u2190coin
11returnC andS
Claim 3When the above procedure terminates, for all0\u2264p \u2264n,C[p] will contain the correct minimum
number of coins needed to make change forp cents, andS[p] will contain (the index of ) the \ufb01rst coin in an
optimal solution to making change forp cents.
Proof:The correctness of the above procedure is based on the fact that it correctly implements the recursive
de\ufb01nition given above. The base case is properly handled in Line 1, and the recursive case is properly handled
in Lines 2 to 10, as shown below. Note that since the loop de\ufb01ned in Line 2 goes from 1 ton and since
di\u22651 for all i, no array element C[\u00b7] is accessed in either Line 6 or 7 before it has been computed. Lines 3
to 7 correctly compute mini:di\u2264p{1 +C[p\u2212 di]}, andC[p] is set to this value in Line 9. Similarly, Lines 3
to 8 correctly compute arg mini:di\u2264p{1 +C[p\u2212 di]}, andS[p] is set to this value in Line 10.
2
(4) Construct the Optimal Solution from the Computed Information.Consider the following
piece of pseudocode, whereS is the array computed above,d is the array of denomination values, andn is
the amount for which change is to be made.
Make-Change(S, d, n)
1whilen > 0
2
PrintS[n]
3
n\u2190 n\u2212 d[S[n]]
Claim 4The above procedure correctly outputs an optimal set of coins for making change forn cents.
Proof:By Claim 3,S[n] will contain (the index of) the \ufb01rst coin in an optimal solution to making change

forn cents, and this coin in printed in Line 2 during the \ufb01rst pass through thewhile loop. Since our problem exhibits optimal substructure by Claim 1, it must be the case that the solution to the remainingn\u2212 dS[n] cents be optimal as well. By settingn\u2190 n\u2212 d[S[n]] in Line 3, the \ufb01rst coin in such a solution will be printed in the next pass through thewhile loop, and so on. (Properly, one would prove that the sequence of coins output by this procedure is correct by induction, but the above su\ufb03ces as far as I\u2019m concerned.)

2
2
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...