[CLRS] Problems 16-1: Coin changing

link http://ripcrixalis.blog.com/2011/02/08/clrs-chapter-16/


Problems 16-1: Coin changing

Consider the problem of making change for n cents using the fewest number of coins. Assume that each coin’s value is an integer.
a. Describe a greedy algorithm to make change consisting of quarters, dimes, nickels, and pennies. Prove that your algorithm yields an optimal solution.
b. Suppose that the available coins are in the denominations that are powers of c, i.e., the denominations are c^0, c^1, …, c^k for some integers c > 1 and k ≥ 1. Show that the greedy algorithm always yields an optimal solution.
c. Give a set of coin denominations for which the greedy algorithm does not yield an optimal solution. Your set should include a penny so that there is a solution for every value of n.
d. Give an O(nk)-time algorithm that makes change for any set of k different coin denominations, assuming that one of the coins is a penny.
a. Determine the largest coin whose value is less than or equal to n. Let this coin have value c. Give one such coin, and then recursively solve the subproblem of making change for n − c cents.
To prove that this algorithm yields an optimal solution, we first need to show that the greedy-choice property holds, that is, that some optimal solution to making change for n cents includes one coin of value c, where c is the largest coin value such that c ≤ n. Consider some optimal solution. If this optimal solution includes a coin of value c, then we are done. Otherwise, this optimal solution does not include a coin of value c. We have four cases to consider:
• If 1 ≤ n < 5, then c = 1. A solution may consist only of pennies, and so it must contain the greedy choice.
• If 5 ≤ n < 10, then c = 5. By supposition, this optimal solution does not contain a nickel, and so it consists of only pennies. Replace Tve pennies by one nickel to give a solution with four fewer coins.
• If 10 ≤ n < 25, then c = 10. By supposition, this optimal solution does not contain a dime, and so it contains only nickels and pennies. Some subset of the nickels and pennies in this solution adds up to 10 cents, and so we can replace these nickels and pennies by a dime to give a solution with (between 1 and 9) fewer coins.
• If 25 ≤ n, then c = 25. By supposition, this optimal solution does not contain a quarter, and so it contains only dimes, nickels, and pennies. If it contains three dimes, we can replace these three dimes by a quarter and a nickel, giving a solution with one fewer coin. If it contains at most two dimes, then some subset of the dimes, nickels, and pennies adds up to 25 cents, and so we can replace these coins by one quarter to give a solution with fewer coins.
Thus, we have shown that there is always an optimal solution that includes the greedy choice, and that we can combine the greedy choice with an optimal solution to the remaining subproblem to produce an optimal solution to our original problem. Therefore, the greedy algorithm produces an optimal solution.
b. Determine the largest j that c^j ≤ n, give one coin of denomination c^j, and then recursively solve the subproblem of making change for n − c^j cents.
To prove that the greedy algorithm produces an optimal solution, first we claim that, for i = 0, 1, . . . , k-1, the number of coins of denomination c^i used in an optimal solution for n cents is less than c. If not, we can improve the solution by using one more coin of denomination c^i+1 and c fewer coins of denomination c^i.
Let j = max{0 ≤ i ≤ k : c^i ≤ n}, so that the greedy solution uses at least one coin of denomination c^j; a nongreedy solution must use no coins of denomination c^j or higher. Thus for the non-greedy solution, we have,
∑(i=0~j-1) ai*c^i = n ≥ c^j.
However we have ai ≤ c-1, so ∑(i=0~j-1) ai*c^i ≤ ∑(i=0~j-1) (c-1)*c^i = (c-1) ∑(i=0~j-1) c^i = (c-1) (c^j – 1) / (c-1) < c^j.
This contradiction shows the non-greedy solution is not optimal. And greedy algorithm has the running time O(k).
c. The set is {10, 9 ,1}, n = 27. Greedy leads to 2 + 7 = 9 coins. Actually the optimal is 3 coins.
d. Using dynamic programming. Let c[j] denote the minimum number of coins we need to make change for j cents.
c[j] = {
0 if j ≤ 0,
1 + min 1≤i≤k {c[j−di]} if j > 1.
}
It runs in O(nk) time.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值