Definition · The fewest-coins problemGiven a set of coin denominations and an amount a, find the smallest number of coins, each denomination usable any number of times, that add to exactly a. If no multiset of the coins adds to a the answer is that the amount cannot be made, which is a result and not a failure.
Two things are being separated here, and conflating them is the reason this lesson comes first. A rule says what to do next: take the largest coin that fits. A recurrence says what the answer is in terms of smaller answers, and commits to nothing. Greedy Algorithms and Matroids is about the conditions under which a rule can be proved correct; this course is about what to do when it cannot.
Example · Six units from 1, 3 and 4Largest-first takes 4, leaving 2, then 1, leaving 1, then 1: 4 + 1 + 1, three coins. The lab reports the answer as 2 and the coins it uses as 3 + 3, re-added on the panel to 6. Both are legal ways to pay. Only one of them is fewest, and the rule does not find it on this input.
Notice how little it took. The coin system is three denominations and the amount is six; nothing here is adversarially large. A reader who has just spent a course proving greedy rules correct by exchange should read this as the boundary being drawn rather than as a trick, because the exchange argument that works for the fractional problem really does fail here, and the lesson after next is where that failure is made explicit on a second problem.
Definition · Optimal substructureA problem has optimal substructure when an optimal solution to it contains, as a part, an optimal solution to a smaller instance of the same problem. The phrase names a proof obligation: you show that a better solution to the part could be cut in, which is what makes the recurrence a statement about optima rather than about some solutions.
Theorem · The coin recurrence is correctLet f(a) be the fewest coins that add to a, with f(0) = 0 and f(a) undefined when no multiset adds to a. Then for every a > 0 that can be made, f(a) = 1 + min of f(a − c) over all coins c ≤ a for which a − c can be made.
Both directions. Any multiset of coins adding to a has a first coin c, and removing it leaves a multiset adding to a − c with one fewer coin; so f(a) ≥ 1 + f(a − c) for that c, and therefore f(a) ≥ 1 + min of f(a − c).
For the other direction, take the c attaining the minimum and a cheapest multiset for a − c; adding one c to it gives a multiset adding to a of size 1 + f(a − c), so f(a) ≤ 1 + min of f(a − c). The two inequalities give equality, and the substitution in the second half is the optimal-substructure argument doing its work.
□
What the rule needed, and the recurrence does not
The exchange argument behind a greedy rule needs more than optimal substructure: it needs the greedy choice property, that some optimal solution agrees with the rule on the first step. With coins 1, 3 and 4 at an amount of six there is no optimal solution containing a 4, so that property is simply false, and the exchange has nothing to exchange into. Optimal substructure survives untouched — which is precisely why the recurrence still works when the rule does not.
This is the split the Subject's ordering is built on. Greedy needs both properties and pays O(n log n); dynamic programming needs only the first and pays for it in table cells. Neither is stronger than the other; they assume different things, and the assumption is the thing to check.
Three routes, and three costs that are not the point until they agree
The lab runs the same definition three ways: as a recursion with no memory, as the same recursion writing each answer down the first time, and as a table filled bottom-up. On this instance the three answers are all 2, and the costs are 24 calls, 14 calls, and 28 cells. The panel prints the ratio as an exact fraction — 12/7, about 1.7 times fewer calls — and says explicitly that the comparison is worth nothing unless the answers match, which is why they are checked before they are counted.
Read those three numbers carefully, because they do not say what a reader expects. The table fills 28 cells; the memo-free recursion makes 24 calls. On this input the exponential method does less work than the polynomial one. The proved bounds are unchanged — the table is Θ(nk) cells for n denominations and amount k, and the plain recursion is exponential in k — and the next lesson moves the amount to eighteen, where the same two routes cost 22 089 calls and 76 cells. A count at one size cannot order two growth rates, and this page is the smallest instance on the course where it visibly gets the order wrong.