Algorithms / Knapsack problem

Knapsack problem

Which items to pack when not everything fits: the take-or-skip rule, the table it fills, how to read the chosen items back out, and the easier version where items can be cut.

Intermediate7 steps~7 min
Bag — holds 5 kg, one slot per kilo
Item — as wide as it is heavy; value written inside
Cell — best value for one smaller problem
// THE SHORT VERSION

In 30 seconds

You have a bag with a weight limit and items that each have a weight and a value. In the 0/1 knapsack problem each item is taken whole or left behind, and the goal is the most value that fits.

Grabbing the best value per kilo first can go wrong. What works is a table: for every bag size and every item, record the better of skipping the item or taking it.

The last cell is the answer, and walking back through the table shows which items were chosen. If items can be cut, the simple greedy rule is enough.

Knapsack problem, step by step

STEPS01 / 07

The table needs whole-number weights and has items × capacity cells, so it suits small capacities. In general 0/1 knapsack is NP-hard: no method is known that is fast for every input.

STEP 01 / 07 A bag that is too small
THE MATH
bag: 5 kg A 1 kg $6 B 2 kg $10 C 3 kg $12 take whole, or leave
  1. Step 1: A bag that is too small. A bag carries 5 kg. Three items are on offer, each with a weight and a value, and together they weigh 6 kg. Each item goes in whole or stays behind: that is the “0/1” in the 0/1 knapsack problem. Which choice gives the most value?
  2. Step 2: Take it or skip it. Look at one item, say C, and there are only two options. Skip it: the answer is the best you can do with A and B in the full 5 kg. Take it: you bank $12 and have 2 kg left for A and B. The answer is the larger of the two, and both are smaller versions of the same question.
  3. Step 3: A table of small answers. Those smaller questions repeat, so answer each once and store it. Make a table: one column per bag size from 0 to 5 kg, one row per item added. A cell holds the best value for that row’s items in that size of bag. With no items every cell is 0; with only A, any bag of 1 kg or more is worth 6.
  4. Step 4: Fill a cell from the row above. Now allow B. For a 3 kg bag: skipping B copies the cell above, 6. Taking B earns 10 and leaves 1 kg, and the row above already says 1 kg is worth 6, so 16. Keep the larger. Every cell needs just two look-ups in the row above, which is why the table is quick to fill.
  5. Step 5: The last cell is the answer. Fill the last row the same way. Its final cell covers all three items and the full 5 kg bag: skipping C gives 16, taking C gives 12 plus the 10 that a 2 kg bag was worth, so 22. That is the answer. Each extra item would double the number of combinations to try, but adds only one row to the table.
  6. Step 6: Walk back to find the items. The table gives the value, not yet the items. Start at the answer and compare with the cell above. If it differs, that row’s item was taken: note it and move left by its weight. If it is the same, the item was skipped: move straight up. The walk gives C and B, 5 kg in all, worth 22.
  7. Step 7: If items can be cut. If items can be divided, like flour or gold dust, no table is needed: take the most valuable per kilo first and cut the last item to fill the bag. A, B and 2 kg of C give 24. That greedy rule is optimal for the fractional problem, but on whole items it packs A and B for only 16 and misses the 22.
VALUES SO FAR
bag5 kglimit
A1 kg, $6item
B2 kg, $10item
C3 kg, $12item
rule····rule
A+B, 3kg····cell
best····result
packed····result
if cut····greedy

Spotted a mistake on this page?