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.

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 knapsack from the top down's own run on the numbers shown.

choose one

question 1

Items, each written weight then value, then what the bag holds after the semicolon: 3 99, 1 87, 2 5; 7. How many cells does the method work out?

the answer, and why

the answer is C

It works out only the cells its calls ask for: 7 of the table's 21.

if you chose another

  • That is the table, 3 rows of 7. 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.

new numbersthe test page's numbers

question 2

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 C

5 20, 7 92, 4 83, 4 41; 7 works out 7 cells, the most of the four.

if you chose another

  • That bag works out 3 cells, fewer than 7.
  • That bag works out 4 cells, fewer than 7.
  • That bag works out 5 cells, fewer than 7.

new numbersthe test page's numbers