solving each smaller problem once
why it works
what it is
Some methods solve the same smaller problem again and again. Write each answer down the first time, and for a van with 30 stops, 117,897,840 calls become 88. This page shows how, and how to read the choices back.
A van has 4 stops to make, in order. Each trip out makes 1, 2 or 3 stops, then comes back. How many plans are there? Name the answer ways(4). Every plan starts with exactly one of those trips, which leaves 3, 2 or 1 stops, a smaller problem of the same kind. So ways(4) is ways(3) + ways(2) + ways(1). Each of those splits the same way, down to ways(0), no stops left. That finishes one plan, so it counts 1.
| first trip | stops left | plans | how many |
|---|---|---|---|
| 1 | 3 | 1+1+1+1, 1+1+2, 1+2+1, 1+3 | 4 |
| 2 | 2 | 2+1+1, 2+2 | 2 |
| 3 | 1 | 3+1 | 1 |
The method is right, and wasteful. Each time it runs on a number of stops is one call. It makes 15 calls, but only 5 different problems: 4, 3, 2, 1 and 0 stops. The other 10 calls solve again what is already solved.
- first time
- asked before
| stops | plans | calls, plain | different problems |
|---|---|---|---|
| 4 | 7 | 15 | 5 |
| 10 | 274 | 600 | 11 |
| 20 | 121,415 | 266,079 | 21 |
| 30 | 53,798,080 | 117,897,840 | 31 |
So solve each one once. Keep a list of answers, called a memo, or a memory. Before solving a smaller problem, look in the memo. If its answer is there, use it and ask for nothing more. If not, solve it and write the answer in.
- first time
- asked before, answered at once
A memo starts at the biggest problem and works down. You can also turn it round. Start at the smallest and fill a table, one cell for each number of stops. Cell 0 holds 1. Each later cell adds the cells just before it, at most 3, already filled: cell 4 adds cells 3, 2 and 1. No cell is worked out twice.
A memo only helps when smaller problems repeat. Merge sort can call itself too: cut the row in half, sort each half, merge them. On 8 numbers, that makes 15 calls, as the van does on 4 stops. But each call sorts a different part of the row, so no answer is ever wanted twice. And the answer must depend on the smaller problem alone. If Tuesday bans trips of 3, ways(3) is 3, not Monday's 4, and Monday's memo is wrong.
Now a problem with choices, from the page on greedy choices: coins of 10, 7 and 1, and 14 to pay. The biggest coin first pays 5 coins. Make a table: a cell for each amount, holding the fewest coins that pay it. Cell 0 holds 0, and a coin of 1 means every amount can be paid. To fill a cell, try each coin that fits: take the number in the cell it leaves, and add 1 for that coin. Keep the fewest. For cell 8, coin 7 leaves cell 1, which holds 1, so 2.
Cell 14 holds 2. Read back: 2 is 1 + 1, so some coin leaves a cell holding 1. Coin 10 leaves cell 4, which holds 4: no. Coin 7 leaves cell 7, which holds 1: yes. From cell 7, coin 7 leaves cell 0, which holds 0. Paid: 7 and 7. Books call this whole idea dynamic programming.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: count the calls
the line:stops calls how 0 1 no smaller problem 1 2 1 + 1 2 4 1 + 2 + 1 3 8 1 + 4 + 2 + 1 4 15 1 + 8 + 4 + 2 5 28 1 + 15 + 8 + 4 6 52 1 + 28 + 15 + 8 Each row is 1, for the first call, plus the rows it asks for, at most the 3 just above. why it is allowed: From 3 stops up, the first call counts 1, and it asks for 3 smaller problems. Each makes all its own calls, as nothing is written down.
the move: more stops, more calls
the line:why it is allowed: Any call with a stop to make can start with a trip of 1 stop, so it makes at least all the calls of one stop fewer, plus itself. 5 stops make 28 calls, and 4 stops make 15. So more stops always mean more calls.
guess it, thenthe move: leave out the 1
the line:why it is allowed: Line 1, without the first call's 1. On 6 stops, 52 is more than 28 + 15 + 8, which is 51.
guess it, thenthe move: swap each for the smallest
the line:why it is allowed: By line 2, the calls for 1 or 2 stops fewer are each at least those for 3 stops fewer. On 6 stops, 52 is at least 3 × 8.
guess it, thenthe move: repeat from no stops
the line:stops lots of 3 at least counted 0 0 1 1 3 1 3 8 6 2 9 52 9 3 27 326 30 10 59,049 117,897,840 The floor against the calls as counted. why it is allowed: 0 stops take 1 call. By line 4, each 3 more stops multiply the calls by at least 3. So lots of 3 stops take at least 3s multiplied together. Name that .
guess it, thenthe move: what it says
the line:why it is allowed: Line 4 in words, and line 5 is the floor it gives. On 30 stops, is 10, so at least 59,049 calls, and counted, 117,897,840.
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: count who asks
the line:stops solved once, it asks for calls first call nobody asks it 1 1 ways(0) 1 2 ways(1), ways(0) 2 3 ways(2), ways(1), ways(0) 3 4 ways(3), ways(2), ways(1) 3 On 4 stops, 1 + 1 + 2 + 3 + 3 = 10 calls, the 10 circles of the memo's tree. why it is allowed: The first call counts 1. Every other call is asked for by a call solving its problem for the first time, and that happens once for each problem. 1 stop asks for 1 smaller problem, 2 stops for 2, and each of the problems from 3 stops up for 3. 0 stops ask for no call.
the move: add the first three
the line:why it is allowed: 1 + 1 + 2 is 4.
guess it, thenthe move: multiply out
the line:why it is allowed: 3 lots of is 3 lots of , less 6.
guess it, thenthe move: add them up
the line:stops calls, plain calls, memo 4 15 10 10 600 28 20 266,079 58 30 117,897,840 88 The memo's calls, by line 4, beside the plain method's counts. why it is allowed: Adding 4 and taking 6 takes 2 in all. On 4 stops, 10. On 30 stops, 88.
guess it, thenthe move: the rule to keep
the line:why it is allowed: A memo or a table works out each different problem once. So count those problems, then the most work of each. On 4 stops, 5 problems × at most 3 answers is at most 15 reads. The table makes 0 + 1 + 2 + 3 + 3 = 9 reads, and no calls.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: no more than any try
the line:why it is allowed: Take a coin that fits. Pay what it leaves in the fewest coins, then add that coin. That pays the amount, so the fewest is no more. On 14, coin 10 gives 5, coin 7 gives 2, coin 1 gives 5.
the move: take out one coin
the line:why it is allowed: Take a best payment and take out any one coin. The coins left pay what that coin leaves. On 14, a best payment is 7 and 7. Take out a 7, and one 7 pays 7.
guess it, thenthe move: put it back
the line:why it is allowed: Line 2, plus 1 on each side.
guess it, thenthe move: that try is exactly k
the line:why it is allowed: The coins pay what that coin leaves, so its try gives or fewer, which by line 3 is or fewer. By line 1, no try gives fewer than . On 14, coin 7 leaves cell 7, which holds 1, so 1 + 1 = 2, which is .
guess it, thenthe move: every cell is right
the line:why it is allowed: By lines 1 and 4, the fewest for an amount from 1 up is 1 + the smallest of the fewests its coins leave, all amounts to its left. On 14, 1 + the smallest of 4, 1 and 4. Cell 0 holds 0, which is right. Once the cells to its left are right, filling makes this one right. So all are, by the strong induction on the page on proof by induction.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: a coin is always there
the line:why it is allowed: A cell was written as 1 + the smallest cell its coins leave, by question 3. So the coin that gave that smallest leaves a cell holding one fewer. On cell 14, that is coin 7.
the move: one fewer each time
the line:why it is allowed: Each coin moves you to a cell holding one fewer. After coins, you have taken away. On cell 14, 2 coins, 7 then 7.
guess it, thenthe move: take k from k
the line:why it is allowed: less is 0.
guess it, thenthe move: at amount 0, all is paid
the line:why it is allowed: Any amount from 1 up needs a coin, so only cell 0 holds 0. You are at amount 0, so the coins paid add up to the amount you started from. There are of them, the fewest by question 3.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
Fill the table and read the coins back by hand, with coins of 9, 6 and 1 and 12 to pay. The biggest coin first pays 9, 1, 1, 1, which is 4 coins.
- Cell 0 holds 0. Cells 1 to 5 fit only coin 1, so they hold 1, 2, 3, 4 and 5.
- Cell 6: coin 6 leaves cell 0, which holds 0, so 1. Coin 1 leaves cell 5, which holds 5, so 6. Keep 1.
- Cells 7 to 11 go the same way, as the table below shows.
- Cell 12: coin 9 leaves cell 3, which holds 3, so 4. Coin 6 leaves cell 6, which holds 1, so 2. Coin 1 leaves cell 11, which holds 3, so 4. Keep 2.
- Read back. Cell 12 holds 2, so some coin leaves a cell holding 1. Coin 9 leaves cell 3, which holds 3: no. Coin 6 leaves cell 6, which holds 1: yes. From cell 6, coin 6 leaves cell 0, which holds 0. Paid: 6 and 6.
| cell | coin 9 | coin 6 | coin 1 | keep |
|---|---|---|---|---|
| 6 | no fit | 1 | 6 | 1 |
| 7 | no fit | 2 | 2 | 2 |
| 8 | no fit | 3 | 3 | 3 |
| 9 | 1 | 4 | 4 | 1 |
| 10 | 2 | 5 | 2 | 2 |
| 11 | 3 | 6 | 3 | 3 |
| 12 | 4 | 2 | 4 | 2 |
the trap
check yourself
Trips make 1 or 2 stops only. Fill the table up to 6 stops.
the answer
1, 1, 2, 3, 5, 8, 13, each cell adding the 2 before it, and cell 1 reading only cell 0. 13 plans for 6 stops.
On Monday, trips make 1, 2 or 3 stops, and a memo says ways(4) is 7. On Tuesday, trips make only 1 or 2. What is ways(4) on Tuesday, and what must the memo also keep?
the answer
5, as cells 0 to 4 hold 1, 1, 2, 3, 5. The answer depends on the day too, so the memo must also keep the day, or Monday's 7 comes back wrong.
Coins of 5, 4 and 1, and 8 to pay. How few coins, and which?
the answer
Cells 0 to 8 hold 0, 1, 2, 3, 1, 1, 2, 3, 2. Cell 8 holds 2. Coin 5 leaves cell 3, holding 3, no. Coin 4 leaves cell 4, holding 1, yes. Coin 4 again leaves cell 0. Paid 4 and 4, against 5, 1, 1, 1 biggest first.