Dynamic Programming Solution for Coin Changing Problem in CSG713 Advanced Algorithms, Study notes of Advanced Algorithms

A dynamic programming solution to the coin changing problem, where the goal is to make change for a given amount using the least number of coins of given denominations. The structure of an optimal solution, recursively defines the value of the optimal solution, and provides pseudocode for computing and constructing the optimal solution. The document also discusses the running time and space requirements of the algorithm.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

lilylily
lilylily 🇬🇧

4

(8)

218 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 for ncents using
coins of denominations d1, d2, . . . , dk. Now consider breaking that solution into two different pieces along
any coin boundary. Suppose that the “left-half of the solution amounts to bcents and the “right-half” of
the solution amounts to nbcents, as shown below.
b
z }| {
d1d3d1d2
nb
z }| {
d4d5d2
Claim 1 The left-half of the solution must be an optimal way to make change for bcents using coins of
denominations d1, d2, . . . , dk, and the right-half of the solution must be an optimal way to make change for
nbcents using coins of denominations d1, d2, . . . , dk.
Proof: By contradiction, suppose that there was a better solution to making change for bcents than the
“left-half” 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 for ncents with fewer coins than the solution
being considered. But this contradicts the supposed optimality of the given solution, →←. An identical
argument applies to the “right-half” of the solution. 2
Thus, the optimal solution to the coin changing problem is composed of optimal solutions to smaller
subproblems.
(2) Recursively Define the Value of the Optimal Solution. First, we define in English the quantity
we shall later define recursively. Let C[p] b e the minimum number of coins of denominations d1, d2, . . . , dk
needed to make change for pcents. In the optimal solution to making change for pcents, there must exist
some first coin di, where dip. Furthermore, the remaining coins in the optimal solution must themselves
be the optimal solution to making change for pdicents, since coin changing exhibits optimal substructure
as proven above. Thus, if diis the first coin in the optimal solution to making change for pcents, then
C[p] = 1 + C[pdi]; i.e., one dicoin plus C[pdi] coins to optimally make change for pdicents. We don’t
know which coin diis the first coin in the optimal solution to making change for pcents; however, we may
check all ksuch possibilities (subject to the constraint that dip), and the value of the optimal solution
must correspond to the minimum value of 1 + C[pdi], by definition. Furthermore, when making change
for 0 cents, the value of the optimal solution is clearly 0 coins. We thus have the following recurrence.
Claim 2 C[p] = 0if p= 0
mini:dip{1 + C[pdi]}if p > 0
Proof: The correctness of this recursive definition is embodied in the paragraph which precedes it. 2
1
pf3

Partial preview of the text

Download Dynamic Programming Solution for Coin Changing Problem in CSG713 Advanced Algorithms and more Study notes Advanced Algorithms in PDF only on Docsity!

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 for n cents using coins of denominations d 1 , d 2 ,... , dk. Now consider breaking that solution into two different pieces along any coin boundary. Suppose that the “left-half” of the solution amounts to b cents and the “right-half” of the solution amounts to n − b cents, as shown below.

b ︷ ︸︸ ︷ d 1 d 3 d 1 d 2

n−b ︷ ︸︸ ︷ d 4 d 5 d 2

Claim 1 The left-half of the solution must be an optimal way to make change for b cents using coins of denominations d 1 , d 2 ,... , dk, and the right-half of the solution must be an optimal way to make change for n − b cents using coins of denominations d 1 , d 2 ,... , dk.

Proof: By contradiction, suppose that there was a better solution to making change for b cents than the “left-half” 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 for n cents with fewer coins than the solution being considered. But this contradicts the supposed optimality of the given solution, →←. An identical argument applies to the “right-half” of the solution. 2

Thus, the optimal solution to the coin changing problem is composed of optimal solutions to smaller subproblems.

(2) Recursively Define the Value of the Optimal Solution. First, we define in English the quantity we shall later define recursively. Let C[p] be the minimum number of coins of denominations d 1 , d 2 ,... , dk needed to make change for p cents. In the optimal solution to making change for p cents, there must exist some first coin di, where di ≤ p. Furthermore, the remaining coins in the optimal solution must themselves be the optimal solution to making change for p − di cents, since coin changing exhibits optimal substructure as proven above. Thus, if di is the first coin in the optimal solution to making change for p cents, then C[p] = 1 + C[p − di]; i.e., one di coin plus C[p − di] coins to optimally make change for p − di cents. We don’t know which coin di is the first coin in the optimal solution to making change for p cents; however, we may check all k such possibilities (subject to the constraint that di ≤ p), and the value of the optimal solution must correspond to the minimum value of 1 + C[p − di], by definition. Furthermore, when making change for 0 cents, the value of the optimal solution is clearly 0 coins. We thus have the following recurrence.

Claim 2 C[p] =

0 if p = 0 mini:di≤p{1 + C[p − di]} if p > 0

Proof: The correctness of this recursive definition is embodied in the paragraph which precedes it. 2

(3) Compute the Value of the Optimal Solution Bottom-up. Consider the following piece of pseu- docode, where d is the array of denomination values, k is the number of denominations, and n is the amount for which change is to be made.

Change(d, k, n) 1 C[0] ← 0 2 for p ← 1 to n 3 min ← ∞ 4 for i ← 1 to k 5 if d[i] ≤ p then 6 if 1 + C[p − d[i]] < min then 7 min ← 1 + C[p − d[i]] 8 coin ← i 9 C[p] ← min 10 S[p] ← coin 11 return C and S

Claim 3 When the above procedure terminates, for all 0 ≤ p ≤ n, C[p] will contain the correct minimum number of coins needed to make change for p cents, and S[p] will contain (the index of ) the first coin in an optimal solution to making change for p cents.

Proof: The correctness of the above procedure is based on the fact that it correctly implements the recursive definition 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 defined in Line 2 goes from 1 to n and since di ≥ 1 for all i, no array element C[·] is accessed in either Line 6 or 7 before it has been computed. Lines 3 to 7 correctly compute mini:di≤p{1 + C[p − di]}, and C[p] is set to this value in Line 9. Similarly, Lines 3 to 8 correctly compute arg mini:di≤p{1 + C[p − di]}, and S[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, where S is the array computed above, d is the array of denomination values, and n is the amount for which change is to be made.

Make-Change(S, d, n) 1 while n > 0 2 Print S[n] 3 n ← n − d[S[n]]

Claim 4 The above procedure correctly outputs an optimal set of coins for making change for n cents.

Proof: By Claim 3, S[n] will contain (the index of) the first coin in an optimal solution to making change for n cents, and this coin in printed in Line 2 during the first pass through the while loop. Since our problem exhibits optimal substructure by Claim 1, it must be the case that the solution to the remaining n − dS[n] cents be optimal as well. By setting n ← n − d[S[n]] in Line 3, the first coin in such a solution will be printed in the next pass through the while loop, and so on. (Properly, one would prove that the sequence of coins output by this procedure is correct by induction, but the above suffices as far as I’m concerned.) 2