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 3 items, 1 56, 5 55, 6 48, each weight then value, in a bag that holds 4?

the answer, and why

the answer is D

3 rows of 4 cells, a row for each item and a cell for each room from 1 to 4. 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 numbersthe test page's 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 C

4 34, 3 31; 8 fills 16 cells, a row for each item and a cell for each room, the most of the four.

if you chose another

  • That bag fills 14 cells, fewer than 16.
  • That bag fills 12 cells, fewer than 16.
  • That bag fills 14 cells, fewer than 16.

new numbersthe test page's numbers