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: 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.

new 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 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.

new numbers