Skip to content

Knapsack from the top down

planning ahead

Level 2 of 3

Also called 0/1 knapsack, memoised and Top-down dynamic programming.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Items, each written weight then value, then what the bag holds after the semicolon: 2 30, 1 20, 3 40; 6. How many cells does the method work out?

the answer, and why

the answer is B

C at 6 asks B at 6 and 3. Each asks A at two rooms, 6 and 5, then 3 and 2. 1 + 2 + 4 cells.

if you chose another

  • That is the table, 3 rows of 6. This method asks only for the cells it needs.
  • That counts the calls, the first and two for each cell.
  • That is the fewest, one cell a row, which comes only when the second and third items each weigh as much as the bag or more.

question 2

On the watch page's first example, C weighs 3. Working out C at room 5, which cells does the method ask for?

the answer, and why

the answer is B

Leaving C asks for B at the same room, 5. C fits, so taking it asks for B at 5 less 3, which is 2.

this tests how it works, on the learn page

if you chose another

  • That is what the table fills. This method asks for two cells at most.
  • That leaves C out only, but C fits, so taking it is asked too.
  • C asks the row just above it, row B, never row A.

question 3

Which bag makes the method work out the most cells? Each item is weight then value, then what the bag holds.

the answer, and why

the answer is A

Items of 1, 2 and 1 in a room of 6 ask for 1 + 2 + 4 rooms, 7 cells.

if you chose another

  • No item fits in 5, so one cell a row, 3.
  • One item, one cell.
  • Two items, 1 + 2 cells, 3.

question 4

With 6 items, at most how many cells can the method work out, however big the bag?

the answer, and why

the answer is D

The rows ask for at most 1, 2, 4, 8, 16 and 32 rooms. The doubling sum adds them to 64 less 1, which is 63.

this tests where the formula comes from, on the learn page

if you chose another

  • That is the next term, 2 to the power 6. The sum is one less.
  • That is the fewest, one cell a row.
  • That allows 2 a row, but each row can hold twice the row below.

pen and paper

question 5about 8 minutes

Run the method by hand on three items in a bag that holds 4. A weighs 2 and is worth 20, B weighs 1 and is worth 15, and C weighs 3 and is worth 35.

  1. List the cells it asks for, from the last cell down, and work each out.
  2. What is the most value, and how many cells did it work out?
  3. Which of the table's 12 cells are never asked for, and why?
a hint

A cell asks the row above for its own room and, when its item fits, for the room the item leaves. No items, or no room, holds 0.

a worked answer

C at 4 asks B at 4 and B at 1. B at 4 asks A at 4 and A at 3, and each holds 20, as A fits. So B at 4: leave B, 20; take B, 15 + A at 3's 20, which is 35. B at 1 asks A at 1, which is 0 as A is too heavy, and A at 0, so take B: 15. C at 4: leave C, B at 4's 35; take C, 35 + B at 1's 15, which is 50. 6 cells, against the table's 12. Never asked: A at 2, B at 2 and 3, and C at 1, 2 and 3. Row C is asked only for the whole bag. B is asked for 4, and 4 less C's 3. A is asked for 4 and 3 from B at 4, and 1 from B at 1.

item1234
A0-2020
B15--35
C---50
The cells asked for, A, B and C in a bag that holds 4. A dash is never asked. The last cell holds 50.

this tests how it works, on the learn page

what a full answer includes
  • C at 4; B at 4 and 1; A at 4, 3 and 1, as drawn.
  • 50, and 6 cells.
  • A at 2, B at 2 and 3, and C at 1 to 3: no ask leads to them.

question 6about 6 minutes

A radio host fills a show from 20 songs, each a whole number of seconds long and each worth some number of votes. The show's length in seconds is the room.

  1. However long the show, at most how many cells can the method work out?
  2. For a show of 1,000 seconds the table fills 20 times 1,000 cells. Why can this method never work out more?
  3. How long must the show be before 1,048,575 is fewer than the table's 20 times its length?
a hint

Question 3 of the learn page's section 4 counts the rows.

a worked answer

At most 1 + 2 + 4 and on to 2 to the power 19, which is 2 to the power 20 less 1: 1,048,575. Every cell asked for is a cell of the table, so at 1,000 seconds at most 20,000. 20 times the length passes 1,048,575 from 52,429 seconds, as 20 times 52,428 is 1,048,560.

Question 3: with nn items, at most how many cells, MM, can the method work out, however big the bag?
  1. the move: row by row

    the line:
    rows ask for at most1,2,4,…,2n−1\begin{gathered} \text{rows ask for at most} \\ 1, 2, 4, \dots, 2^{n - 1} \end{gathered}

    why it is allowed: By question 1's lines 1 and 2, the last row is asked for 1 room, and each row above for at most twice the row below. With 3 items, 1, 2 and 4.

  2. the move: big items, big bag

    the line:
    weights 2n−1,…,2,1reach every one\begin{gathered} \text{weights } 2^{n - 1}, \dots, 2, 1 \\ \text{reach every one} \end{gathered}
    items taken below Atheir weightroom left for A
    none04
    C13
    B22
    B and C31
    Items weighing 4, 2 and 1 in a room of 4. Each choice of B and C takes a different weight, so row A is asked for 4 different rooms. With 1 room in row C and 2 in row B, 1 + 2 + 4, which is 7 cells.

    why it is allowed: From the last item, weights 1, 2, 4 and on, in a bag of 2n−12^{n - 1} or more. The items below row A can weigh 0, 1, 2 and on, up to one less than 2n−12^{n - 1} in all, each total in just one way. So no room is asked twice, and each is at least 1. B, C and the rest fit every room they are asked for, so each of their cells asks twice; A's asks reach only the row of no items. With 3 items weighing 4, 2 and 1, a room of 4 is enough.

  3. the move: add the rows

    the line:
    M=1+2+4+⋯+2n−1M = 1 + 2 + 4 + \dots + 2^{n - 1}

    why it is allowed: By lines 1 and 2, the most is every row at its most. With 3 items, 1 + 2 + 4.

  4. the move: use the doubling sum

    the line:
    M=2n−1M = 2^n - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

      the line:
      S=1+2+4+⋯+2nS = 1 + 2 + 4 + \dots + 2^n

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term. Its lines 6 and 7 take this sum, ending at 2n−12^{n - 1}, to 2n−12^n - 1. For 3 items, 1 + 2 + 4 is 7, one less than 8.

  5. the move: against the table

    the line:
    never more than thetable’s n×W\begin{gathered} \text{never more than the} \\ \text{table's } n \times W \end{gathered}

    why it is allowed: Each cell asked for is a cell of the table, so the method never works out more than n×Wn \times W: in a small bag that is the tighter limit. With 20 items, never more than 1,048,575. Reaching it needs row A's 524,288 different rooms, each from 1 to WW, so a room of 524,288 or more.

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

this tests where the formula comes from, on the learn page

what a full answer includes
  • 1,048,575.
  • Each cell asked for is one of the table's 20,000.
  • From 52,429 seconds on.