





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!






In Section 7.1, we developed a greedy algorithm for the coin-changing prob- lem in which the goal was to make change for an amount A using the fewest number of coins, where the available denominations were
denom[ 1 ] > denom[ 2 ] > · · · > denom[n] = 1_._
We saw that whether the greedy algorithm produced the fewest number of coins depended on which denominations of coins were available. In this sec- tion, we develop a dynamic programming algorithm for the coin-changing problem that produces the fewest number of coins no matter which denom- inations are available. To break the given problem into subproblems, we vary the amount and restrict the denominations available. More precisely, we consider the prob- lem of computing the minimum number of coins for an amount j , 0 ≤ j ≤ A , where the available denominations are
denom[i] > denom[i + 1 ] > · · · > denom[n] = 1 ,
1 ≤ i ≤ n. We call this the i, j-problem. The original problem is i = 1 and j = A. We let C[i][j] denote the solution to the i, j -problem; that is, C[i][j] is the minimum number of coins to make change for the amount j , using coins i through n (see Figure 8.2.1). (We later address the problem of determining which coins achieve the minimum.)
j 0 1 2 3 4 5 6 7 8 9 10 11 12 1 0 1 2 3 4 5 1 2 3 4 1 2 2 i 2 0 1 2 3 4 5 1 2 3 4 5 6 2 3 0 1 2 3 4 5 6 7 8 9 10 11 12
Figure 8.2.1 The array C for the denominations
denom[ 1 ] = 10 , denom[ 2 ] = 6 , denom[ 3 ] = 1 ,
and amounts up to 12. The index i specifies that coins i through 3 are available, and j is the amount. When i = 3, only the coin of denomination 1 is available. Thus, in the last row, it takes j coins to make change for the amount j. When i = 2, the coins 6 and 1 are available. For example, the minimum number of coins to make change for the amount 8 is three—one coin of denomination 6 and two coins of denomination 1. When i = 1, all of the coins are available. For example, the minimum number of coins to make change for the amount 11 is two—one coin of denomination 10 and one coin of denomination 1.
To solve the i, j -problem, i < n , we must decide whether to use a coin of denomination denom[i]. If we do not use a coin of denomination denom[i] ,
in order to achieve the amount j , we must solve the (i + 1 ), j problem. Since this is a subproblem (i.e., a smaller problem in the sense that fewer coins are available), we can design our algorithm so that this subproblem is already solved. Thus, given that we do not use coin i , the minimum number of coins to make change for the amount j is C[i + 1 ][j]. On the other hand, if we use a coin of denomination denom[i] , we must complete the solution by making change for the amount j − denom[i] using coins of denominations
denom[i] > denom[i + 1 ] > · · · > denom[n] = 1_._
If we use, say, k coins for the amount j − denom[i] , our solution to the problem of making change for the amount j uses 1 + k coins (since we al- ready used one coin of denomination denom[i] ). To minimize 1 + k , we must choose k as small as possible. In other words, we must use the min- imum number of coins to solve the subproblem of making change for the amount j − denom[i] using coins i through n. (This is an example of the optimal substructure property , which we discuss thoroughly later in this sec- tion.) Therefore, we must solve the i, (j − denom[i]) -problem. Since this is also a subproblem (i.e., a smaller problem in the sense the amount is smaller than the original amount), we can also design our algorithm so that this sub- problem is already solved. Thus, given that we do use coin i , the minimum number of coins to make change for the amount j is 1 + C[i][j − denom[i]]. Now, either we use coin i or we don’t! Thus, the solution to the i, j prob- lem is
C[i][j] =
C[i + 1 ][j] if denom[i] > j min{ C[i + 1 ][j], 1 + C[i][j − denom[i]] } if denom[i] ≤ j. (8.2.1) Contrast the dynamic programming algorithm outlined in the preceding paragraphs with the greedy algorithm in Section 7.1. When the greedy algo- rithm considers using a denomination d for an amount j , if d ≤ j , it uses it—without regard to the consequences of solving the smaller problem of making change for the amount d − j. For example, if the available denomi- nations are 10 , 6 , 1 and the amount is 12, the greedy algorithm chooses one 10—without regard to the fact that the resulting problem of making change for the amount 2 requires two coins. The greedy algorithm thus chooses one 10 and two 1’s. On the other hand, the dynamic programming algorithm con- siders using denomination d and not using it and picks the better alternative. In this sense, dynamic programming can be considered an enhancement of the greedy technique. Again, if the available denominations are 10 , 6 , 1 and the amount is 12, the dynamic programming algorithm considers choosing a 10. Since this leaves the problem of making change for the amount 2, if the dynamic programming algorithm chooses a 10, three coins will be re- quired to make change for the amount 12: one 10 and two 1’s. The dynamic programming algorithm also considers not choosing a 10. In this case, the algorithm will make change for the amount 12 using only the denominations
coins to make change for a given amount and tracks which coins are used. The input is an array denom that specifies the denominations of the coins, denom[ 1 ] > denom[ 2 ] > · · · > denom[n] = 1 , and an amount A. The output consists of arrays C and used. The value, C[i][j] , is the minimum number of coins to make change for the amount j , using coins i through n. The value, used[i][j] , is true or false to signify whether coin i appears in the smallest set of coins computed by Algorithm 8.2.1 for the amount j using only coins i through n. The values of i and j satisfy 1 ≤ i ≤ n and 0 ≤ j ≤ A.
Input Parameters: denom, A Output Parameters: C, used dynamic _ coin _ change2(denom, A, C, used) { n = denom.last for j = 0 to A { C[n][j] = j used[n][j] = true } for i = n − 1 downto 1 for j = 0 to A if (denom[i] > j || C[i + 1 ][j] < 1 + C[i][j − denom[i]]) { C[i][j] = C[i + 1 ][j] used[i][j] = false } else { C[i][j] = 1 + C[i][j − denom[i]] used[i][j] = true } }
Example 8.2.3. The used array computed by Algorithm 8.2.2 for the denom-
We can now write an algorithm to output a minimum size set of coins chosen from among coins i through n for an amount j. The algorithm inputs the index i , the amount j , the array denom of Algorithm 8.2.2, and the array used computed by Algorithm 8.2.2.
Algorithm 8.2.4Computing a Minimum Size Set of Coins for a Given Amount. This algorithm outputs a minimum size set of coins to make change for an amount j using any of coins i through n with denominations specified by Algorithm 8.2.2. The algorithm inputs the index i , the amount j , the array denom of Algorithm 8.2.2, and the array used computed by Algorithm 8.2.2.
Input Parameters: i, j, denom, used Output Parameters: None
j 0 1 2 3 4 5 6 7 8 9 10 11 12 1 F F F F F F F F F F T T F i 2 F F F F F F T T T T T T T 3 T T T T T T T T T T T T T
Figure 8.2.2 The used array computed by Algorithm 8.2.2 for the denom- inations 10 , 6 , 1 and amount A = 12. used[i][j] is true (T) or false (F) to signify whether coin i appears in the smallest set of coins computed by Al- gorithm 8.2.1 for the amount j using only coins i through n. For example, used[ 1 ][ 11 ] is true because the minimum set of coins { 10 , 1 } for the amount 11 uses a coin of denomination 10. By contrast, used[ 1 ][ 12 ] is false because the minimum set of coins { 6 , 6 } for the amount 12 does not use a coin of denomination 10.
optimal _ coins _ set (i, j, denom, used) { if (j == 0 ) return if (used[i][j]) { println( “Use a coin of denomination ” + denom[i]) optimal _ coins _ set (i, j − denom[i], denom, used) } else optimal _ coins _ set (i + 1 , j, denom, used) }
Algorithm 8.2.4 terminates correctly. Each time the algorithm is called with j > 0, either i is incremented by 1 or j is decremented at least by 1. If j becomes 0 before i = n , the algorithm terminates correctly. If i = n and j > 0, used[i][j] is true so j continually decrements by 1 until it is 0, and the algorithm also terminates correctly. When Algorithm 8.2.4 is called as optimal _ coins _ set ( 1 , A, denom, used) j can be decremented at most A times and i can be incremented at most n − 1 times. Thus, the run time of Algorithm 8.2.4 with i = 1 and j = A , which outputs a minimum size set of coins chosen from among all available denominations to make change for the amount A , is O(n + A).
Constructing a Dynamic Programming Algorithm
To construct a dynamic programming algorithm, the first step is to break the given problem into subproblems using parameters to characterize the subproblems. The solution to the original problem will be built from these subproblems. The parameters control the size of the subproblems, and small problem sizes must be included. The dynamic programming algorithm will begin by solving small subproblems and end with a solution to the original problem. For example, to compute the n th Fibonacci number fn (see Section
an optimal solution, the algorithm may track the indexes that lead to optimal solutions of subproblems. For example, to compute the minimum number of coins and to construct a set of coins that gives this minimum value, the dynamic programming algorithm tracked the coins that gave the minimum values (see Algorithm 8.2.2).
The Optimal Substructure Property
The optimal substructure property is If S is an optimal solution to a problem, then the components of S are optimal solutions to subproblems. In order for a dynamic programming algorithm to solve an optimization problem correctly, the optimal substructure property must hold.
Example 8.2.5. The optimal substructure property holds for the coin-chang- ing problem. Given available denominations of coins, if S is a minimum size set of coins for an amount A and we remove one coin of denomination d from S , then S , with this coin removed, is a minimum size set of coins for
The optimal substructure property does not say that if S 1 and S 2 are op- timal solutions to subproblems, then combining S 1 and S 2 gives an optimal solution to the original problem. This is the converse of the optimal sub- structure property.
Example 8.2.6. Suppose that the available denominations are 10 , 6 , 1 for the coin-changing problem. One coin of denomination 1 and one coin of denom- ination 6 give an optimal solution for the amount 7. Five coins of denomina- tion 1 give an optimal solution for the amount 5. Combining these solutions, which results in six coins of denomination 1 and one coin of denomination 6, does not yield an optimal solution for the amount 12. Thus, for the coin-changing problem, if S 1 and S 2 are optimal solutions to subproblems, combining S 1 and S 2 does not necessarily give an optimal solution to the original problem; the converse of the optimal substructure property does not hold for the coin-changing problem. As we observed ear-
We close with an example of a problem for which the optimal substructure property does not hold.
Example 8.2.7. We show that the optimal substructure property does not hold for the longest simple path problem : Given a connected, weighted graph G and vertices v and w in G , find a longest simple path in G from v to w (a simple path is a path with no repeated vertices). Assume that all of the weights in G are positive. By inspection, a longest simple path in the graph of Figure 8.2.3 from vertex 1 to vertex 4 is 1 , 3 , 4. If the optimal substructure property holds for the longest simple path problem, then 1 , 3 is a longest simple path from 1 to
8 3
3 10 4 Figure 8.2.3 A graph that shows that the optimal substructure property does not hold for the longest simple path problem. A longest simple path from vertex 1 to vertex 4 is 1 , 3 , 4, but 1 , 3 is not a longest simple path from vertex 1 to vertex 3.
It can be shown that the longest simple path problem is NP-complete and
In Exercises 1–3, show the array C computed by Algorithm 8.2.1 for the given denominations and the amount A = 12_._
1S. 10 , 5 , 1 2. 10 , 3 , 1 3. 10 , 7 , 1
In Exercises 4–6, show the array used computed by Algorithm 8.2.2 for the given denominations and the amount A = 12_._
4S. 10 , 5 , 1 5. 10 , 4 , 1 6. 10 , 3 , 1
7S. Write an algorithm whose input is an index i ≤ n , an amount j ≤ A , the array denom , and the array C , and whose output is a minimal set of coins to make change for the amount j. Algorithm 8.2.1 describes i , n , A , denom , and C. What is the worst-case time of your algorithm?
10S. Show that the converse of the optimal substructure property does not hold for the shortest-path problem.