Skip to content

0/1 knapsack

planning ahead

Level 2 of 3

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

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from 0/1 knapsack's own run on the numbers shown.

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.

new numbers

question 2

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.

new numbers