Answer:c(x)=1+min{c(x−1),c(x−4),c(x−6)} over the coins that fit, c(0)=0; table 0, 1, 2, 3, 1, 2, 1, 2, 2, 3, 2, 3, 2, 3, 3; c(14)=3 using 6 + 4 + 4; greedy gives 6 + 6 + 1 + 1, four coins
(a) In an optimal way of making x, the last coin has value d∈{1,4,6}, and removing it must leave an optimal way of making x−d (otherwise swapping in a better one would improve the total). This optimal substructure gives
c(x)=1+d∈{1,4,6},d≤xminc(x−d),c(0)=0.
(b) Fill the table in increasing x, using only entries already computed:
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|
| c(x) | 0 | 1 | 2 | 3 | 1 | 2 | 1 | 2 | 2 | 3 | 2 | 3 | 2 | 3 | 3 |
For example c(4)=1+min(c(3),c(0))=1, c(8)=1+min(c(7),c(4),c(2))=1+1=2, c(10)=1+min(c(9),c(6),c(4))=2, and c(14)=1+min(c(13),c(10),c(8))=1+min(3,2,2)=3.
(c) c(14)=3. Trace back through the choices: c(14) came from c(8) with a 6 (or equally from c(10) with a 4), c(8) from c(4) with a 4, and c(4) from c(0) with a 4. The coins are 6 + 4 + 4 = 14. No two coins can reach 14, since the largest two sum to 12, so 3 is indeed optimal.
(d) Greedy takes 6, then another 6 (leaving 2), then 1 and 1: four coins, 6 + 6 + 1 + 1. The dynamic-programming solution uses three, so greedy is not optimal for this coin system. It fails because taking the second 6 leaves a remainder (2) that only the 1-coins can cover, whereas giving up one 6 for two 4s covers the remainder exactly.
c(x)=1+min{c(x−1),c(x−4),c(x−6)} with c(0)=0; c(14)=3 using coins 6, 4, 4; greedy needs 4 coins.