[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.
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目前最全的算法导论第三版的答案 清晰度极好 另每章都有章节重点总结 在第二版基础上新增习题的解答详尽清楚 很适合 用这本经典教材的同学! Contents Revision History R-1 Preface P-1 Chapter 2: Getting Started Lecture Notes 2-1 Solutions 2-17 Chapter 3: Growth of Functions Lecture Notes 3-1 Solutions 3-7 Chapter 4: Divide-and-Conquer Lecture Notes 4-1 Solutions 4-17 Chapter 5: Probabilistic Analysis and Randomized Algorithms Lecture Notes 5-1 Solutions 5-9 Chapter 6: Heapsort Lecture Notes 6-1 Solutions 6-10 Chapter 7: Quicksort Lecture Notes 7-1 Solutions 7-9 Chapter 8: Sorting in Linear Time Lecture Notes 8-1 Solutions 8-10 Chapter 9: Medians and Order Statistics Lecture Notes 9-1 Solutions 9-10 Chapter 11: Hash Tables Lecture Notes 11-1 Solutions 11-16 Chapter 12: Binary Search Trees Lecture Notes 12-1 Solutions 12-15 Chapter 13: Red-Black Trees Lecture Notes 13-1 Solutions 13-13 Chapter 14: Augmenting Data Structures Lecture Notes 14-1 Solutions 14-9 iv Contents Chapter 15: Dynamic Programming Lecture Notes 15-1 Solutions 15-21 Chapter 16: Greedy Algorithms Lecture Notes 16-1 Solutions 16-9 Chapter 17: Amortized Analysis Lecture Notes 17-1 Solutions 17-14 Chapter 21: Data Structures for Disjoint Sets Lecture Notes 21-1 Solutions 21-6 Chapter 22: Elementary Graph Algorithms Lecture Notes 22-1 Solutions 22-13 Chapter 23: Minimum Spanning Trees Lecture Notes 23-1 Solutions 23-8 Chapter 24: Single-Source Shortest Paths Lecture Notes 24-1 Solutions 24-13 Chapter 25: All-Pairs Shortest Paths Lecture Notes 25-1 Solutions 25-9 Chapter 26: Maximum Flow Lecture Notes 26-1 Solutions 26-12 Chapter 27: Multithreaded Algorithms Solutions 27-1 Index I-1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值