Skip to content

0/1 knapsack

planning ahead

Level 2 of 3

Also called Zero-one knapsack problem and Knapsack, by dynamic programming.

how it works

The idea on the watch page's first example, three items and a bag that holds 5. The books this site follows pack a removal van the same way (volume three, chapter 39). Then the whole method on that example, step by step.

A bag holds a weight of 5. Item A weighs 4 and is worth 50, B weighs 2 and is worth 30, and C weighs 3 and is worth 40. Each item goes in whole or not at all. Taking the most valuable first takes A, and then nothing else fits: 50. But B and C together weigh 5 and are worth 70. Taking the most valuable first misses this, and taking the most value per weight first fails on other bags, as the books warn (volume three, chapter 39). So fill a table. Each row adds one item to those above it: none, then A, then B, then C. Each column is a room, from 0 to 5. A cell holds the most value its row's items can carry in its column's room. With no items or no room, that is 0. Fill the other cells row by row. Look at the row's own item. If it weighs more than the room, it cannot go in, so copy the cell above. If not, take the more of two numbers. Leaving it keeps the cell above. Taking it is its value, plus the row above at the room it leaves. For C in room 5: leaving C keeps 50. C weighs 3, so taking it leaves room 5 less 3, which is 2, and the row above holds 30 there. Taking it is 40 plus 30: 70. The cell holds 70. The last cell is the answer: 70. To see which items, read back from it. A cell worth more than the one above took its item: step up a row, to the room the item leaves. A cell worth the same did not: step straight up. This page counts cells, one for each cell the rule fills, not the first row and column: 15 here, as the watch page counts. The code below, in the language Python, fills the table and returns the last cell; it leaves reading back out.

item12345
-00000
A0005050
B030305050
C030405070
Each row adds its item to those above, and each column is a room from 1 to 5. Room 0 holds 0 in every row and is not drawn. Room 1 holds 0 too, as no item weighs 1. A dash is the row with no items. The last cell holds 70, the answer.
cellholdsaboveitem
C at room 57050in, leaves room 2
B at room 2300in, leaves room 0
A at room 000out
Reading back from the last cell. More than the cell above means the item went in. So take B and C, weight 5, worth 70.
The most valuable first is not best: 8 moments of its 17 steps, the steps between them left for the watch page
  1. step 1 of 17

    012345ABC000000000

    Pack a bag that holds 5 with the most value from 3 items. With no items or no room it holds nothing, so the first row and column are 0.

    what it remembers to know where it is
    item
    not yet
    room
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 2 of 17

    012345ABC0000000000

    Item A weighs 4, more than the room of 1, so it cannot go in. Keep 0 from the cell above.

    what it remembers to know where it is
    item
    A
    room
    1

    watch from here

  3. step 5 of 17

    012345ABC00000000005000

    With room 4, leaving A out keeps 0. Taking it is worth 50, plus 0 for the room it leaves: 50. Taking it is better, so write 50.

    what it remembers to know where it is
    item
    A
    room
    4

    watch from here

  4. step 8 of 17

    012345ABC0000000000505000300

    With room 2, leaving B out keeps 0. Taking it is worth 30, plus 0 for the room it leaves: 30. Taking it is better, so write 30.

    what it remembers to know where it is
    item
    B
    room
    2

    watch from here

  5. step 10 of 17

    012345ABC00000000005050003030500

    With room 4, leaving B out keeps 50. Taking it is worth 30, plus 0 for the room it leaves: 30. That is no better, so leave it and write 50.

    what it remembers to know where it is
    item
    B
    room
    4

    watch from here

  6. step 14 of 17

    012345ABC000000000050500030305050003040

    With room 3, leaving C out keeps 30. Taking it is worth 40, plus 0 for the room it leaves: 40. Taking it is better, so write 40.

    what it remembers to know where it is
    item
    C
    room
    3

    watch from here

  7. step 16 of 17

    012345ABC0000000000505000303050500030405070

    With room 5, leaving C out keeps 50. Taking it is worth 40, plus 30 for the room it leaves: 70. Taking it is better, so write 70.

    what it remembers to know where it is
    item
    C
    room
    5

    watch from here

  8. step 17 of 17

    012345ABC0000000000505000303050500030405070

    Done: take B and C, weighing 5 in all and worth 70, the most the bag holds. The marked cells are where each item went in.

    what it remembers to know where it is
    item
    not yet
    room
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. the first row and column: with no items or no room, the bag holds nothingPython lines 8 and 9
  2. for each item, and each room from 1 up to what the bag holdsPython lines 10, 11 and 12
  3. too heavy for the room, so keep the best without it, the cell abovePython lines 13 and 14
  4. otherwise keep the more of leaving it and taking it, with the best for the room it leavesPython lines 15 and 16
  5. the last cell is the most the bag holds; reading back, a cell worth more than the one above took its itemPython line 17
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

def knapsack(items, capacity):
    """The most value a bag that holds capacity can carry, each item whole or left out.

    items is a list of (weight, value) pairs, each a whole number from 1 up,
    and capacity is a whole number from 0. Returns the most value.
    """
    n = len(items)
    # best[i][c]: the most value the first i items carry in a room of c
    best = [[0] * (capacity + 1) for _ in range(n + 1)]  # no items or no room: 0
    for i in range(1, n + 1):  # each item, in turn
        weight, value = items[i - 1]
        for c in range(1, capacity + 1):  # each room from 1 up
            if weight > c:  # too heavy for this room
                best[i][c] = best[i - 1][c]  # keep the cell above
            else:  # the more of leaving it and taking it
                best[i][c] = max(best[i - 1][c], value + best[i - 1][c - weight])
    return best[n][capacity]  # the last cell

how it scales

The plot counts the cells the method fills for the watch page's 3 items, in a bag that holds WW, measured from 10 to 100,000.

Each mark across or up is ten times the one before. The line climbs one mark up for each mark across: 300 cells at 100 and 3,000 at 1,000: ten times the room, ten times the cells. Here the cost is the cells, and a slope of 1 means they grow as WW, as section 4's question 1 derives. Twice the room makes twice the cells.

cells filled, ten times more at each mark up

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

WW, how much weight the bag holds, ten times more at each mark to the right

  • 3 items in a bag that holds WW, whatever their weights and valuessolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times W, 10 times the work
how long filling the cells takes, at a billion a second
WWcells filledtime
1,0003,000about 3 microseconds
100,000300,000about 300 microseconds
10,000,00030,000,000about 30 milliseconds

The cells filled are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count the knapsack's cells yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each room W from 10 to 100,000 it
prints the cells the method fills for the watch page's 3 items in a bag
that holds W. The counts are the same on any computer, whatever the
weights and values; only the time differs.
"""


def knapsack(items, capacity):
    """The most value a bag that holds capacity can carry, each item whole or left out.

    items is a list of (weight, value) pairs, each a whole number from 1 up,
    and capacity is a whole number from 0. Returns the most value.
    """
    n = len(items)
    # best[i][c]: the most value the first i items carry in a room of c
    best = [[0] * (capacity + 1) for _ in range(n + 1)]  # no items or no room: 0
    for i in range(1, n + 1):  # each item, in turn
        weight, value = items[i - 1]
        for c in range(1, capacity + 1):  # each room from 1 up
            if weight > c:  # too heavy for this room
                best[i][c] = best[i - 1][c]  # keep the cell above
            else:  # the more of leaving it and taking it
                best[i][c] = max(best[i - 1][c], value + best[i - 1][c - weight])
    return best[n][capacity]  # the last cell


def count(items, capacity):
    """How many cells the method fills: each item makes a row, with one
    cell for each room from 1 to capacity, whatever the weights and values."""
    cells = 0
    for _ in items:  # a row for each item
        cells += capacity  # a cell in it for each room from 1 up
    return cells


# The watch page's first example's items, each (weight, value).
ITEMS = [(4, 50), (2, 30), (3, 40)]


if __name__ == "__main__":
    print(f"{'W':>7} {'cells':>12}")
    for W in (10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000, 50_000, 100_000):
        print(f"{W:>7} {count(ITEMS, W):>12}")

best, worst and average

Best, worst and average usually name different inputs of one size. Here the size is WW, what the bag holds, and this page keeps 3 items. Then every bag fills the same cells, whatever the weights and values. So the table has one column, under the name worst, and no average is worked out. In the grid below, from 1 up means for any WW of 1 or more.

A case is one kind of input: here, which items. A bound says how fast its cost grows as the room grows.

The counts are in the grid below. A count is often made of pieces that grow at different speeds, and the fastest-growing piece swamps the rest. With its first row and column, the table holds 4W+44W + 4 numbers, as question 2 works out: at a room of 1,000, 4,000 plus 4, and the 4 hardly matters. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
worst3 items in a bag that holds WW, whatever their weights and values
cells filled3W3W, from 1 up
how the cells filled grow in each case
tight, Θ\ThetaΘ(W)\Theta(W)
ceiling, OOO(W)O(W)
floor, Ω\OmegaΩ(W)\Omega(W)

extra memory: A table of one number for each row and each room, or one row for the most value alone.

Extra memory is the room it needs on top of the input itself, while it runs.

itemsroomcellsmost value
4 50, 2 30, 3 4041250
1 10, 1 20, 1 3041260
5 20, 6 30, 7 404120
Three bags of 3 items, each written weight then value, in a room of 4, counted by the run. The most value changes with the items, and the cells do not, 3 times 4.
Wcellsnumbers held
138
2612
51524
82436
100300404
1,0003,0004,004
The watch page's 3 items in a bag that holds WW. The table also holds its first row and column, as question 2 counts.

where the formula comes from

Five questions, each worked line by line. Question 1 is the count the plot draws.

Question 1: how many cells, CC, does the method fill for 3 items in a bag that holds WW, from 1 up?
  1. the move: a row for each item

    the line:
    n items, room W:n×W cells\begin{gathered} n \text{ items, room } W\text{:} \\ n \times W \text{ cells} \end{gathered}
    order0123
    -givengivengivengiven
    Agiven123
    Bgiven456
    Cgiven789
    Three items in a room of 3, each cell numbered in the order it is filled, row by row. Given cells are written first, not counted. 3 rows of 3, 9 cells.

    why it is allowed: Each item has a row, and each row fills one cell for each room from 1 to WW. The watch page's 3 items in a room of 5 fill 3 times 5, 15 cells.

  2. the move: keep 3 items

    the line:
    C=3WC = 3W

    why it is allowed: This page keeps 3 items, so nn is 3, and 3 times WW is written 3W3W. When WW is 3, 9.

  3. the move: a ceiling

    the line:
    C≤3WC \le 3W

    why it is allowed: By line 2, as a number is at most itself.

  4. the move: read the bound

    the line:
    C∈O(W)C \in O(W)

    why it is allowed: By line 3, with the fixed multiple 3. ∈\in reads is in, and O(W)O(W) means at most a fixed multiple of WW, from some WW on.

  5. the move: a floor

    the line:
    C≥3WC \ge 3W

    why it is allowed: By line 2, as a number is at least itself.

  6. the move: read the bound

    the line:
    C∈Ω(W)C \in \Omega(W)

    why it is allowed: By line 5, with the fixed multiple 3. Ω(W)\Omega(W) means at least a fixed multiple of WW, above 0, from some WW on.

  7. the move: both together

    the line:
    C∈Θ(W)C \in \Theta(W)

    why it is allowed: Lines 4 and 6 bound CC by the same growth. Θ(W)\Theta(W) means both.

try it with a small WW: at W=3W = 3, line 2 gives C=9C = 9, which you can work out by hand

Question 2: how many numbers, TT, does the table hold for 3 items in a bag that holds WW, from 1 up?
  1. the move: count the rows and columns

    the line:
    T=4×(W+1)T = 4 \times (W + 1)
    item0123
    -0000
    A00025
    B0151525
    C0152035
    Section 5's items, A weighing 3 and worth 25, B 1 and 15, and C 2 and 20, in a room of 3. 4 rows of 4 numbers, 16 in all. The first row and column count, though no step fills them.

    why it is allowed: A row for no items, then one for each of the 3 items, so 4 rows. A column for each room from 0 to WW, so W+1W + 1. When WW is 3, 4 times 4, which is 16.

  2. the move: multiply out

    the line:
    T=4W+4T = 4W + 4

    why it is allowed: 4 lots of WW, and 4 lots of 1. When WW is 3, 12 + 4, which is 16.

  3. the move: a ceiling

    the line:
    T≤8WT \le 8W

    why it is allowed: From 1 up, 4 is at most 4W4W. Add 4W4W to both, and 4W+44W + 4 is at most 8 lots of WW. When WW is 3, 16 is at most 24.

  4. the move: read the bound

    the line:
    T∈O(W)T \in O(W)

    why it is allowed: By line 3, with the fixed multiple 8.

  5. the move: a floor

    the line:
    T≥4WT \ge 4W

    why it is allowed: By line 2: take 4 away from TT, and 4W4W is left, which is less. When WW is 3, 16 is at least 12.

  6. the move: read the bound

    the line:
    T∈Ω(W)T \in \Omega(W)

    why it is allowed: By line 5, with the fixed multiple 4.

  7. the move: both together

    the line:
    T∈Θ(W)T \in \Theta(W)

    why it is allowed: Lines 4 and 6 bound TT by the same growth.

try it with a small WW: at W=3W = 3, line 1 gives T=16T = 16, which you can work out by hand

Question 3: if you want only the most value, how many numbers, RR, does one row keep, from 1 up?
  1. the move: what a cell reads

    the line:
    a cell reads onlythe row above\begin{gathered} \text{a cell reads only} \\ \text{the row above} \end{gathered}

    why it is allowed: Leaving the item reads the cell above. Taking it reads the row above, at the room it leaves. So once a row is full, the rows above it are never read again.

  2. the move: write over one row, from the right

    the line:
    write over one row,from the biggest room down\begin{gathered} \text{write over one row,} \\ \text{from the biggest room down} \end{gathered}
    row12345
    after A0005050
    B, from the right030305050
    B, from the left030306060
    B's numbers written over A's, on the watch page's first example, rooms 1 to 5. From the right, room 4 reads room 2 while it still holds A's 0, and keeps 50. From the left, room 2 already holds B's 30, so room 4 takes B twice, 60, which is wrong.

    why it is allowed: Write each item's numbers over the item before's, starting at room WW. A cell reads its own room and the room its item leaves, to its left. From the right, neither is written yet, so both still hold the row above. Filled from the left, the smaller room is already new, and an item could go in twice, as the books show (volume three, chapter 39).

  3. the move: one row

    the line:
    R=W+1R = W + 1

    why it is allowed: So the row holds one number for each room from 0 to WW. When WW is 3, 4 numbers, against the table's 16.

  4. the move: but not the items

    the line:
    reading back needsevery row\begin{gathered} \text{reading back needs} \\ \text{every row} \end{gathered}

    why it is allowed: Reading back compares a cell with the one above it, in every row from the last up. One row has written over them, so it gives the most value, not which items, as the books note (volume three, chapter 39).

try it with a small WW: at W=3W = 3, line 3 gives R=4R = 4, which you can work out by hand

Question 4: why is every number in the table right, and how many is that? Let KK be how many, for 3 items in a bag that holds WW.
  1. the move: the promise

    the line:
    each cell holds themost value its row’sitems carry in its room\begin{gathered} \text{each cell holds the} \\ \text{most value its row's} \\ \text{items carry in its room} \end{gathered}

    why it is allowed: A cell's row allows the items from A down to its own, and its column gives a room. This is what to prove. Lines 2 to 8 prove it, cell by cell, in the order the cells are filled.

  2. the move: it starts right

    the line:
    the first row andcolumn are right\begin{gathered} \text{the first row and} \\ \text{column are right} \end{gathered}

    why it is allowed: With no items, or no room, nothing goes in, so the most is 0, as written.

  3. the move: suppose so far

    the line:
    suppose every cell filledbefore this one is right\begin{gathered} \text{suppose every cell filled} \\ \text{before this one is right} \end{gathered}
    rowreadsright by
    no itemsnothingline 2
    Athe row with no itemsline 2 at room 0, lines 4 to 7 after
    Brow Aline 2 at room 0, lines 4 to 7 after
    Crow Bline 2 at room 0, lines 4 to 7 after
    Each row reads only the row above it, so being right passes down, row by row, from the row with no items.

    why it is allowed: Rows fill from the top, so the cells a cell reads, in the row above, are written or filled before it. Suppose each of those holds the most. This is fair. Line 2 starts it, and lines 4 to 7 show the cell is then right too. Line 8 carries that to every cell, in the order they are filled.

  4. the move: two kinds of load

    the line:
    a load leavesthe row’s itemor takes it\begin{gathered} \text{a load leaves} \\ \text{the row's item} \\ \text{or takes it} \end{gathered}

    why it is allowed: A load is a set of the row's items whose weights add up to at most the room. Either it leaves the row's own item out, or it takes it.

  5. the move: leaving it

    the line:
    leaving it: worth at mostthe cell above\begin{gathered} \text{leaving it: worth at most} \\ \text{the cell above} \end{gathered}

    why it is allowed: Without its item, the load uses only the items above, in the same room. By line 3, the cell above holds the most such a load is worth.

  6. the move: taking it

    the line:
    taking it: worth at mostits value+the rowabove at the room left\begin{gathered} \text{taking it: worth at most} \\ \text{its value} + \text{the row} \\ \text{above at the room left} \end{gathered}

    why it is allowed: With its item, the rest uses only the items above, and weighs at most the room less the item's weight. By line 3, that cell in the row above holds the most the rest is worth. An item heavier than the room cannot be taken at all, so then every load leaves it.

  7. the move: so the cell is right

    the line:
    the rule writes the moreof the two, the most\begin{gathered} \text{the rule writes the more} \\ \text{of the two, the most} \end{gathered}
    C in room 5readsworth
    leave Cthe cell above, room 550
    take C40 + the row above, room 270
    The two choices for the last cell of the watch page's first example. The more is 70, and no load of A, B and C in room 5 is worth more.

    why it is allowed: By lines 5 and 6, no load is worth more than the more of the two. Both can be packed: the cell above's load, or the row above's load at the room left plus this item, which fits. So the more of the two is the most, and it is what the rule writes. For C in room 5: leaving C keeps 50, and taking it is 40 + 30, which is 70, so 70. For an item too heavy, the rule copies the cell above, the most by lines 5 and 6, as every load leaves it.

  8. the move: every cell

    the line:
    every cell is right\text{every cell is right}

    why it is allowed: By line 2, the first row and column are. By lines 3 to 7, each later cell is right once those before it are. So, filled in order, every cell is, the last included.

  9. the move: count them

    the line:
    K=4×(W+1)K = 4 \times (W + 1)

    why it is allowed: Every number the table holds, each right by line 8: the same count as question 2's TT. When WW is 3, 16.

try it with a small WW: at W=3W = 3, line 9 gives K=16K = 16, which you can work out by hand

Question 5: how fast do the cells grow with the length of WW in digits? Take 3 items and WW from 1 up, let dd be how many digits WW has, and let LL be the cells the smallest WW of dd digits fills.
  1. the move: the smallest of d digits

    the line:
    the smallest Wof d digits: 1 andd−1 zeros\begin{gathered} \text{the smallest } W \\ \text{of } d \text{ digits: 1 and} \\ d - 1 \text{ zeros} \end{gathered}
    dsmallest Wcells3 times d
    1133
    210306
    31003009
    41,0003,00012
    71,000,0003,000,00021
    The watch page's 3 items in the smallest bag of dd digits. One digit more, and the cells are 10 times as many. Beside them, 3 times dd, a cost that grows with the length in digits, adds only 3.

    why it is allowed: A number of dd digits starts with a digit of 1 or more, and d−1d - 1 digits follow. The smallest is 1 and then d−1d - 1 zeros. For 3 digits, 100.

  2. the move: write it as a power

    the line:
    the smallest W ofd digits is 10d−1\begin{gathered} \text{the smallest } W \text{ of} \\ d \text{ digits is } 10^{d - 1} \end{gathered}

    why it is allowed: 1 and then d−1d - 1 zeros is d−1d - 1 tens multiplied together, written 10d−110^{d - 1}. For 1 digit there are no tens to multiply, which leaves 1. For 3 digits, 100.

  3. the move: its cells

    the line:
    L=3×10d−1L = 3 \times 10^{d - 1}

    why it is allowed: By question 1 of section 4, the cells are 3 times the room. So by line 2, the smallest WW of dd digits fills 3×10d−13 \times 10^{d - 1}: that is LL. For 3 digits, 300.

  4. the move: every room of d digits

    the line:
    every W of d digitsfills at least L cells\begin{gathered} \text{every } W \text{ of } d \text{ digits} \\ \text{fills at least } L \text{ cells} \end{gathered}

    why it is allowed: By line 1, every such WW is at least the smallest. By question 1 of section 4, a bigger WW fills more cells, 3W3W, so LL is the fewest.

  5. the move: each digit more

    the line:
    each digit more makes L10 times bigger\begin{gathered} \text{each digit more makes } L \\ 10 \text{ times bigger} \end{gathered}

    why it is allowed: One digit more multiplies 10d−110^{d - 1} by one more 10, so LL grows 10 times with each digit. Against the room, the cells grow in a straight line, 3W3W. Against its length in digits, the fewest grow tenfold a digit, and 7 digits, a bag of 1,000,000, already make 3,000,000 cells. A cost like this is called pseudo-polynomial.

try it with a small dd: at d=3d = 3, line 3 gives L=300L = 300, which you can work out by hand

count it yourself

Three items, each written weight then value, in a bag that holds 3, the number after the semicolon. Step by step, each cell filled adds 1 to the count on its own step.

3 25, 1 15, 2 20; 3, every step written down
stepwhat happenscells filled
1Pack a bag that holds 3 with the most value from 3 items. With no items or no room it holds nothing, so the first row and column are 0.0
2Item A weighs 3, more than the room of 1, so it cannot go in. Keep 0 from the cell above.1
3Item A weighs 3, more than the room of 2, so it cannot go in. Keep 0 from the cell above.2
4With room 3, leaving A out keeps 0. Taking it is worth 25, plus 0 for the room it leaves: 25. Taking it is better, so write 25.3
5With room 1, leaving B out keeps 0. Taking it is worth 15, plus 0 for the room it leaves: 15. Taking it is better, so write 15.4
6With room 2, leaving B out keeps 0. Taking it is worth 15, plus 0 for the room it leaves: 15. Taking it is better, so write 15.5
7With room 3, leaving B out keeps 25. Taking it is worth 15, plus 0 for the room it leaves: 15. That is no better, so leave it and write 25.6
8Item C weighs 2, more than the room of 1, so it cannot go in. Keep 15 from the cell above.7
9With room 2, leaving C out keeps 15. Taking it is worth 20, plus 0 for the room it leaves: 20. Taking it is better, so write 20.8
10With room 3, leaving C out keeps 25. Taking it is worth 20, plus 15 for the room it leaves: 35. Taking it is better, so write 35.9
11Done: take B and C, weighing 3 in all and worth 35, the most the bag holds. The marked cells are where each item went in.9

In all, 9 cells filled. The formula from section 4, n×Wn \times W, gives C=9C = 9 at W=3W = 3: the same.

where you meet it

The problem is old. George Dantzig's 1957 paper on such problems already treats it as known, as the books note (volume three, chapter 39).

what is used instead

When the room is a huge number, or is measured very finely, the table is too big. Solvers then search the sets of items, cutting off any that cannot beat the best found so far. Where an item can be split, taking the most value per weight first is exactly right, as the books note (volume three, chapter 39).

how it compares

The plain method asks the same question as calls, with no table: leave the last item, or take it where it fits. The 10 items below weigh 1 to 10, 55 in all. In a room of 55 or more, every item fits, so each call with an item left asks 2 more, one level for each of the 10 items. That makes 1 + 2 + 4 + ... + 1,024 calls, the last 2 multiplied together 10 times, which is 2,047, in both rooms below. A memo keeps each answer once worked out, as the page on solving each smaller problem once shows, and reaches only the rooms some set of items leaves. The table fills every room, so its cells grow with WW, and one row is enough for the answer alone. Coin change fills a row of amounts too, but there a coin can be used again, as filling from the left allows.

wayroom 100room 100,000
plain method2,047 calls2,047 calls
with a memo481 calls481 calls
the table1,000 cells1,000,000 cells
one row955 cells999,955 cells
10 items weighing 1 to 10, in that order, each row counted by running the method. One row skips the rooms an item is too heavy for, 45 in all, as item 10 skips rooms 1 to 9. The calls stay the same as the room grows, and the cells grow with it. A call and a cell are different steps, so compare a row's two rooms, not one row with another.

race them on one row of your own