Knapsack
calculator.
Pick the items with the most total value that fit under one weight or budget limit.
| # | Item | Weight | Value |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |
- Items packed
- 3
- Space left
- 0
- Value per weight
- 11.5
Pack Tent, Camera, Food. Taking the best value per unit of weight first would get only 110, because it fills the space badly. One more unit of capacity would raise the best value to 120.
| # | Item | Weight | Value |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |
Best value by capacity
Drag the point to change the capacityBest value at each capacity from 0 to 15. At 10 it is 115, and at 11 it is 120. The line only steps up where another item, or a better mix, starts to fit.
Best choice or greedy?
Greedy picks Tent, Sleeping bag, Stove, Camera and leaves 0 of space it cannot use.
Working
- 1Fill a table: the best value with the first i items at each capacity c, either leaving item i out or putting it inbest(i, c) = max( best(i − 1, c), best(i − 1, c − wᵢ) + vᵢ )
- 2Read the choices back, last item first: take an item when the best value is higher with itFood, capacity 10: with 115, without 110 → takeBooks, capacity 5: with 35, without 65 → leaveCamera, capacity 5: with 65, without 45 → takeStove, capacity 4: with 15, without 40 → leaveSleeping bag, capacity 4: with 30, without 40 → leaveTent, capacity 4: with 40, without 0 → take
- 3Check: the chosen items fit and their values add up to the best valueweight 4 + 1 + 5 = 10 ≤ 10; value 40 + 25 + 50 = 115
Items and what was packed
| Item | Weight | Value | Per weight | Packed |
|---|---|---|---|---|
| Tentweight 4 | 4 | 4010 each | 10 | Yes |
| Sleeping bagweight 3 | 3 | 3010 each | 10 | No |
| Stoveweight 2 | 2 | 157.5 each | 7.5 | No |
| Cameraweight 1 | 1 | 2525 each | 25 | Yes |
| Booksweight 3 | 3 | 103.3333 each | 3.3333 | No |
| Foodweight 5 | 5 | 5010 each | 10 | Yes |
How it's worked out
Each item has a weight and a value, and the bag has a capacity. In the 0/1 problem you take an item whole or leave it. Testing every subset takes 2 to the power n tries, so the tool builds a table instead, a method called dynamic programming.
Here is the most value you can get from the first items with room . Item weighs and is worth . For each cell you either leave item out, or take it and add the best you can do with what room is left. Take only counts when fits in . Bowdoin College notes count the work as times subproblems, one per item and capacity. That is why the weights must be whole numbers.
To find which items made the best value, read the table back from the last item. If the best value at the room left is higher with the item than without it, the item is in, and its weight comes off the room.
Fractional knapsack allows part of an item, like grain or time. Then the greedy rule is the best answer: rank by value per unit of weight and fill from the top, taking a share of the last item. Unbounded knapsack allows any number of copies, and the table needs only one row.
Worked examples
Jordan packs for a 10 kg weekend hike
Jordan scores six items by how much each matters, and the pack can carry 10 kg. The best choice is Tent, Camera and Food, worth 115 and weighing 10 kg. Reading back, the first item taken is Food: with it the value is 115, without it 110.
Taking the best value per kilo first gets Tent, Sleeping bag, Stove and Camera for 110. That is less, even though the camera, the best per kilo, is in both. With 11 kg the best value would be 120, and with 12 kg it would be 130.
The textbook case where greedy goes wrong
Items A (weight 10, value 60), B (20, 100) and C (30, 120) go in a bag of 50. A has the best value per unit, 6, so greedy takes A and then B for 160, and C no longer fits. The table finds B and C for 220.
If the items could be split, greedy would be right: A, B and 67% of C fill the bag exactly and give 240. These are the same figures the Bowdoin notes use.
Coins with unlimited copies
A small coin weighs 2 and is worth 3; a large one weighs 3 and is worth 5. With room for 7 and as many coins as you like, the best mix is 2 small and 1 large for 11, using all 7 units.
With only one of each allowed, the best is 8. Extra copies can never make the answer worse, only equal or better.
Reading your result
The best value is the most you can carry. Weight used can be below the capacity when no remaining item fits; that space is real, not a rounding issue.
The capacity chart steps up only where a new item, or a better mix, starts to fit. A long flat stretch means extra room buys nothing until the next step. That is worth knowing before you pay for a bigger pack or a larger budget.
Greedy next to the best answer shows how far a quick rule can fall short. The UC Davis notes use items worth 6, 10 and 12 with weights 1, 2 and 3 and a limit of 5. Greedy gets 16 and leaves room unused, while the best is 22.
Ties happen. When two mixes have the same value, the tool shows the one it reads back first. Change a value by a small amount to see the other.
Common mistakes
- Trusting value per weight for whole items. It is only safe when items can be split. For whole items it can leave a gap that a better mix would fill.
- Decimal weights in the 0/1 table. Weights of 1.5 and 0.8 kg are refused (the table needs whole units). Enter them as 1,500 and 800 g with a limit of 2,000 g, and the best value is 30.
- Picking the wrong version. Gear, projects and files are 0/1. Fuel, time and grain are fractional. Coins and stock you can reorder are unbounded.
- Expecting the table to scale forever. The work grows with the capacity in units. A limit of 1,000,000 g on 100 items is 100 million cells, so use kilograms or a coarser unit when you can.