Greedy: Canonical Coin Change
Canonical Coin Change with denominations 1, 5, 10, 25: always choose the largest possible coin. For this system, Greedy is optimal.
Arbitrary denominations (not greedy): Coin Change [1].
Where used
Everyday example and counterexample machine: Greedy fails with non-canonical coin systems. It trains distrust of local choices without proof.
Depth
In the Greedy coin change, the largest suitable coin is chosen each time. For some coin systems, this always provides a minimal number of coins; such systems are called canonical. The common euro denominations are designed so that the rule works for typical integer amounts.
For arbitrary denominations, the rule is not reliable. A single counterexample is sufficient to disprove the general validity. Dynamic programming, on the other hand, can compute the smallest number of coins for all amounts up to the target and is also useful for systematically checking a coin system in a finite range.
Difficulty levels
- Execute the Greedy selection for a given coin system.
- Disprove a coin system with a counterexample.
- Compare Greedy results and DP optimum over many target amounts.
Pitfalls
Just because the rule works for multiple tested amounts does not constitute a general proof. Additionally, reachability and a coin with a value of one must be considered separately from the optimality question.
Sources
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Intermediate
- Completed: 0 users