Consider the problem of making change for 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.
Algorithm
Pick the coin with the largest denomination of cents and add it to the change.
Update to the remaining owed amount.
Repeat 1. and 2. until the full amount is payed.
Proof
In order to prove our solution optimal, we have to show that our solution satisfies the greedy-choice property and exhibits optimal substructure.
Let the owed change be cents.
Let represent all existing denominations sorted in monotonically-increasing order.
1. Greedy-choice property
The first key ingredient is the greedy-choice property: we can assemble a globally optimal solution by making locally optimal (greedy) choices. –textbook, p.424
Consider the optimal way to change (): The greedy choice takes coin .
Claim: Any optimal solution must also take coin .
Say does not include , then must only consist of coins . This means that coins must add up to .
k
Constraints
Max. value of in any
1
1
2
5
3
10
4
25
As we can see from the chart, , which shows that no optimal solution exists under this assumption.
Therefore, coin is indeed the greedy choice to make. This reduces the problem to coin-changing cents, which can again be optimally solved using the greedy-choice property.
2. Optimal substructure
A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. –textbook, p.425
Let be the minimum number of coins needed to coin-change cents. Suppose is an optimal solution to , then:
is an optimal solution to
is an optimal solution to
is an optimal solution to
is an optimal solution to
⟹
Therefore, by substituting , the cashier's algorithm exhibits optimal substructure.
b. Suppose that the available coins are in the denominations that are powers of , i.e., the denominations are for some integers and . Show that the greedy algorithm always yields an optimal solution.
1. Greedy-choice property
Conjecture: Making the greedy choice at every iteration leads to a globally optimal solution.
Let be such that and the greedy algorithm returns where is a solution. Let be an optimal solution for .
Basis step If , then and .
Inductive step Assume for , then . According to our conjecture, must also contain a . Suppose does not contain , then . If any , . This would violate the optimality of since number of coins could be traded for a single coin. A contradiction is reached. Thus must contain a .
We showed that making the greedy choice at every iteration leads to the optimal solution . This proves the greedy-choice property.
2. Optimal substructure
To prove that the greedy algorithm exhibits optimal substructure, we let and let be an optimal solution.
We assume that is an optimal solution for . If it were not, then for an optimal solution . This means , which is a contradiction since is an optimal solution.
Thus, is an optimal solution for . This proves the optimal substructure property.
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 .
Let and . Greedy yields but the optimal solution is .
Let and . Greedy yields but the optimal solution is .
d. Give an -time algorithm that makes change for any set of different coin denominations, assuming that one of the coins is a penny.
// D = list of k different coin denominationsfunctionMINIMUM-CHANGE(n,D){let count =0// number of coins added to change
sort Din ascending order // O(n) if we sort in linear timewhile n >0// O(n)for i =D.length downto 1// O(k)ifD[i]<= n do
increment count
subtract D[i] from n
return count
}