Skip to content

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 tripstops leftplanshow many
131+1+1+1, 1+1+2, 1+2+1, 1+34
222+1+1, 2+22
313+11
The plans for 4 stops, by first trip. After a first trip of 1, what follows is a plan for 3 stops, so ways(3) is 4. In all, 4 + 2 + 1 = 7 plans.

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.

15 calls, 5 different problems432100100210010
  • first time
  • asked before
The method on 4 stops. Each circle is one call, joined to the call that asked for it. It runs from the left, so the left-hand 2 comes first, and the other 2 repeats it.
stopsplanscalls, plaindifferent problems
47155
1027460011
20121,415266,07921
3053,798,080117,897,84031
Counted by running the method. In these rows, each plan costs a little over 2 calls, while the different problems grow by just 1 a stop.

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.

10 calls: 5 worked out, 5 answered at once4321001021
  • first time
  • asked before, answered at once
The same method with a memo, on 4 stops. A repeat asks for nothing more, so the 5 calls under the repeats go, and 15 calls become 10.

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.

cell 0 holds 1101234cell 1: cell 0 holds 11011234cell 2: 1 + 1 = 210112234cell 3: 2 + 1 + 1 = 4101122434cell 4: 4 + 2 + 1 = 71011224374
The table for 4 stops, filling from the left, one row for each cell written. Arrows show what each cell reads. Under each cell, its number of stops.

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.

Merge sort on 8 numbers. Each stretch is one call, 1 + 2 + 4 + 8 = 15 of them, and no two sort the same part. The numbers at the right are not calls: each is how many numbers that level puts in place as it merges, 24 in all.

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 tries each coin107100112233445566172839110211312413214coin 10: cell 4 holds 4, so 5coin 7: cell 7 holds 1, so 2, the fewestcoin 1: cell 13 holds 4, so 5
In each cell, the fewest coins for the amount under it. Cell 14 reads the cells its 3 coins leave, 4, 7 and 13, each coin beside its arrow.

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.

reading back from cell 147700112233445566172839110211312413214at 14, coin 7 leaves 7, which holds 1at 7, coin 7 leaves 0, which holds 0paid: 7 and 7, 2 coins
Reading back from cell 14, each coin on its arrow. Each step lands on a cell holding one fewer, until cell 0.

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 nn, 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.

question 1How fast must the plain method's calls grow? Let nn be the number of stops. Start at no stops and add 3 stops, mm times over. Let FF be a floor, a number the calls never fall below.
  1. the move: count the calls

    the line:
    calls for n stops:1+ the calls for n−1,n−2 and n−3\begin{gathered} \text{calls for } n \text{ stops:} \\ 1 + \text{ the calls for } n - 1, \\ n - 2 \text{ and } n - 3 \end{gathered}
    stopscallshow
    01no smaller problem
    121 + 1
    241 + 2 + 1
    381 + 4 + 2 + 1
    4151 + 8 + 4 + 2
    5281 + 15 + 8 + 4
    6521 + 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.

  2. the move: more stops, more calls

    the line:
    the calls for n−1 and n−2are each at least those for n−3\begin{gathered} \text{the calls for } n - 1 \text{ and } n - 2 \\ \text{are each at least those for } n - 3 \end{gathered}

    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.

  3. the move: leave out the 1

    the line:
    the calls for n are more thanthose for n−1,n−2 and n−3added up\begin{gathered} \text{the calls for } n \text{ are more than} \\ \text{those for } n - 1, n - 2 \text{ and } n - 3 \\ \text{added up} \end{gathered}

    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.

  4. the move: swap each for the smallest

    the line:
    the calls for n are at least3 times those for n−3\begin{gathered} \text{the calls for } n \text{ are at least} \\ 3 \text{ times those for } n - 3 \end{gathered}

    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.

  5. the move: repeat from no stops

    the line:
    F=3mF = 3^m
    stopslots of 3at leastcounted
    0011
    3138
    62952
    9327326
    301059,049117,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 mm lots of 3 stops take at least mm 3s multiplied together. Name that FF.

  6. the move: what it says

    the line:
    3 more stops at leasttriple the calls\begin{gathered} \text{3 more stops at least} \\ \text{triple the calls} \end{gathered}

    why it is allowed: Line 4 in words, and line 5 is the floor it gives. On 30 stops, mm is 10, so at least 59,049 calls, and counted, 117,897,840.

try it with a small mm: at m=2m = 2, line 5 gives F=9F = 9, which you can work out by hand

question 2With a memo, how many calls, MM, does the method make on nn stops, from 2 stops up?
  1. the move: count who asks

    the line:
    M=1+1+2+3(n−2)M = 1 + 1 + 2 + 3(n - 2)
    stopssolved once, it asks forcalls
    first callnobody asks it1
    1ways(0)1
    2ways(1), ways(0)2
    3ways(2), ways(1), ways(0)3
    4ways(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 n−2n - 2 problems from 3 stops up for 3. 0 stops ask for no call.

  2. the move: add the first three

    the line:
    M=4+3(n−2)M = 4 + 3(n - 2)

    why it is allowed: 1 + 1 + 2 is 4.

  3. the move: multiply out

    the line:
    M=4+3n−6M = 4 + 3n - 6

    why it is allowed: 3 lots of n−2n - 2 is 3 lots of nn, less 6.

  4. the move: add them up

    the line:
    M=3n−2M = 3n - 2
    stopscalls, plaincalls, memo
    41510
    1060028
    20266,07958
    30117,897,84088
    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.

  5. the move: the rule to keep

    the line:
    the work is at mostthe different problems× the most work of each\begin{gathered} \text{the work is at most} \\ \text{the different problems} \\ \times \text{ the most work of each} \end{gathered}

    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.

try it with a small nn: at n=4n = 4, line 1 gives M=10M = 10, which you can work out by hand

question 3Why is a cell 1 more than the fewest its coins leave? Every set of coins here holds a 1, so every amount can be paid. Let kk be the fewest coins for an amount, and RR the number of coins left after taking out one.
  1. the move: no more than any try

    the line:
    k is at most 1+ the fewestfor what each coin leaves\begin{gathered} k \text{ is at most } 1 + \text{ the fewest} \\ \text{for what each coin leaves} \end{gathered}

    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.

  2. the move: take out one coin

    the line:
    R=k−1R = k - 1

    why it is allowed: Take a best payment and take out any one coin. The RR 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.

  3. the move: put it back

    the line:
    R+1=kR + 1 = k

    why it is allowed: Line 2, plus 1 on each side.

  4. the move: that try is exactly k

    the line:
    that coin’s trygives exactly k\begin{gathered} \text{that coin's try} \\ \text{gives exactly } k \end{gathered}

    why it is allowed: The RR coins pay what that coin leaves, so its try gives R+1R + 1 or fewer, which by line 3 is kk or fewer. By line 1, no try gives fewer than kk. On 14, coin 7 leaves cell 7, which holds 1, so 1 + 1 = 2, which is kk.

  5. the move: every cell is right

    the line:
    every cell holdsits fewest\begin{gathered} \text{every cell holds} \\ \text{its fewest} \end{gathered}

    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.

try it with a small kk: at k=2k = 2, line 2 gives R=1R = 1, which you can work out by hand

question 4Why does reading back always find a coin, and end with the fewest coins? Let kk be the number in the cell you start from, and HH the number in the cell you reach after kk coins.
  1. the move: a coin is always there

    the line:
    a cell holding k, from 1 up,leaves one holding k−1\begin{gathered} \text{a cell holding } k \text{, from 1 up,} \\ \text{leaves one holding } k - 1 \end{gathered}

    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.

  2. the move: one fewer each time

    the line:
    H=k−kH = k - k

    why it is allowed: Each coin moves you to a cell holding one fewer. After kk coins, you have taken kk away. On cell 14, 2 coins, 7 then 7.

  3. the move: take k from k

    the line:
    H=0H = 0

    why it is allowed: kk less kk is 0.

  4. the move: at amount 0, all is paid

    the line:
    the k coins paythe amount, in the fewest\begin{gathered} \text{the } k \text{ coins pay} \\ \text{the amount, in the fewest} \end{gathered}

    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 kk of them, the fewest by question 3.

try it with a small kk: at k=2k = 2, line 2 gives H=0H = 0, 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.

  1. Cell 0 holds 0. Cells 1 to 5 fit only coin 1, so they hold 1, 2, 3, 4 and 5.
  2. Cell 6: coin 6 leaves cell 0, which holds 0, so 1. Coin 1 leaves cell 5, which holds 5, so 6. Keep 1.
  3. Cells 7 to 11 go the same way, as the table below shows.
  4. 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.
  5. 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.
cellcoin 9coin 6coin 1keep
6no fit161
7no fit222
8no fit333
91441
102522
113633
124242
What each coin tries, the number in the cell it leaves plus 1, and the fewest kept.

the trap

check yourself

  1. 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.

  2. 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.

  3. 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.