Skip to content

0/1 knapsack

planning ahead

Level 2 of 3

Also called Zero-one knapsack problem and Knapsack, by 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

How many cells does the method fill for 4 items in a bag that holds 6?

the answer, and why

the answer is C

4 rows of 6 cells, a row for each item and a cell for each room from 1 to 6. The first row and column are written at the start.

if you chose another

  • That counts the first row and column too, which are written at the start, not filled.
  • That adds the items and the room, but each row fills a cell for each room.
  • That counts room 0 too, which is written at the start.

question 2

P weighs 6 and is worth 60, Q weighs 2 and is worth 30, and R weighs 5 and is worth 45. The bag holds 7. What is the most value it can carry?

the answer, and why

the answer is C

Q and R weigh 7 and are worth 75. P is the most valuable, but after it only 1 is left, and nothing weighs 1.

this tests how it works, on the learn page

if you chose another

  • That is P alone, the most valuable; the 1 it leaves holds no item.
  • P and Q weigh 8, more than the bag holds.
  • All three weigh 13.

question 3

Which bag makes the method fill the most cells? Each item is written weight then value, and the number after the semicolon is what the bag holds.

the answer, and why

the answer is B

2 items in a room of 8, 2 times 8, which is 16.

if you chose another

  • 4 times 3, which is 12. More items, but a smaller room.
  • 1 times 8, which is 8.
  • 3 times 5, which is 15.

question 4

On the watch page's first example, C weighs 3 and is worth 40. Filling C's cell in room 5, which cells does the method read?

the answer, and why

the answer is C

Leaving C reads the cell above, at room 5. Taking C reads the row above at the room it leaves, 5 less 3, which is 2.

this tests how it works, on the learn page

if you chose another

  • To the left is C's own row at a smaller room, which the rule never reads.
  • The rule reads the row above, never its own row.
  • It reads only two cells, the same room and the room C leaves.

pen and paper

question 5about 8 minutes

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

  1. Fill the table.
  2. What is the most value, and how many cells did you fill?
  3. Read back which items go in.
a hint

The first row and column hold 0. An item too heavy for the room copies the cell above. Otherwise take the more of the cell above, and the item's value plus the row above at the room it leaves.

a worked answer

The table is drawn below. The last cell says 50, and you fill 12 cells, 3 rows of 4. Reading back: C at room 4 holds 50, the same as above, so C stays out. B at room 4 holds 50, more than the 15 above it, so B goes in and leaves room 1. A at room 1 holds 15, more than the 0 above it, so A goes in. Take A and B.

item01234
-00000
A015151515
B015153550
C015254050
A, B and C in a bag that holds 4. The last cell holds 50.

this tests how it works, on the learn page

what a full answer includes
  • The table as drawn.
  • 50, and 12 cells.
  • A and B, as C's cell equals the one above and B's and A's beat theirs.

question 6about 6 minutes

A team picks from 20 features to build, each costing whole dollars and worth some value to the team, within a budget of 400 dollars.

  1. How many cells does the method fill, with the budget as the room?
  2. The budget grows to 40,000 dollars. How many cells now?
  3. Count the digits in 400 and in 40,000. What did the 2 extra digits do to the cells?
a hint

Each feature is an item, its cost its weight, and the budget the room. Question 1 of the learn page's section 4 counts the cells, and question 5 the digits.

a worked answer

20 times 400, which is 8,000 cells. At 40,000 dollars, 20 times 40,000, which is 800,000 cells, 100 times as many. 400 has 3 digits and 40,000 has 5. 40,000 is 400 with 2 more zeros, 100 times as big, so the cells are 100 times as many.

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

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

what a full answer includes
  • 8,000 cells.
  • 800,000 cells.
  • 3 and 5 digits; 2 more zeros, 100 times the cells.