Coin Change
Goal: Make change with the smallest number of coins
Definition (Canonical coin system)
A coin system is canonical if the value of a coin can be composed of previous values
Cashier’s Algorithm
Algorithm: Start with the largest denominations and dispense as many of those that fit (Greedy Algorithms)
Note: Only optimal for canonical coin systems
Claim
If solution is optimal, then number of nickels + number of dimes 2
Proof: Assume to the contrary that the solution is optimal but sum > 2 Number of nickels must be 1 because 2 nickels would be better expressed as a dime
Case 1: The solution has no nickels. Then it has at least 3 dimes. But then replacing 3 dimes with 1 quarter and 1 nickels gives a more optimal solution, a contradiction.
Case 2: The solution has 1 nickel. So it also has at least 2 dimes. But then replace 1 nickel and 2 dimes with 1 quarter, same contradiction.
Theorem
The cashier’s algorithm is optimal for the U.S. coin system ( )
Proof: Induction on the amount to be paid, .
Base case:
- The only optimal solution is to take 1 penny.
- That is the greedy option, so the cashiers algorithm is optimal.
Inductive Hypothesis:
- Assume that the cashier’s algorithm is optimal when
Inductive Step:
- Let be the largest coin such that
- The cashier’s algorithm will take
- We will show that any optimal solution takes
- Assume some optimal solution for does not take
- This needs enough coins of type to add up to
- No optimal solution can do this because , , ,
- Next, after taking , we will make change for
- Since , the IH says that the cashier’s algorithm is optimal
- Observe that + optimal solution for is optimal solution for
Dynamic Solution
- Dynamic Programming
- Also works with non canonical coin systems
- Similar to the knapsack problem except you’re trying to minimize the number of coins instead of maximize value