ScienceComputer Science

Knapsack
calculator.

Pick the items with the most total value that fit under one weight or budget limit.

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.

leave\displaystyle \text{leave}=best(i−1,c)\displaystyle {}=\;\text{best}(i-1, c)
take\displaystyle \text{take}=best(i−1,c−wi)+vi\displaystyle {}=\;\text{best}(i-1, c - w_i) + v_i
best(i,c)\displaystyle \text{best}(i, c)=max⁡(leave,take)\displaystyle {}= \max(\text{leave}, \text{take})

Here best(i,c)\text{best}(i, c) is the most value you can get from the first ii items with room cc. Item ii weighs wiw_i and is worth viv_i. For each cell you either leave item ii out, or take it and add the best you can do with what room is left. Take only counts when wiw_i fits in cc. Bowdoin College notes count the work as nn times WW 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.

Open this example in the tool

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.

Open this example in the tool

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.

Open this example in the tool

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.

Questions people ask

Greedy commits to the item that looks best now and never looks back. With whole items that choice can use space in a way that blocks a better pair. In the textbook case it takes A and B for 160 and misses B and C for 220.

Sources