%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

0/1 Knapsack Solver

Results update as you type.

Your numbers

Understanding the 0/1 knapsack solver — concept and principle

The 0/1 knapsack problem asks: given items with weights and values and a bag with a weight limit, which items should you take to get the most value? Each item is either taken whole or left behind. The same structure appears in budgeting, project selection and cargo loading. This solver finds the exact optimum by dynamic programming.

0/1 knapsack: optimal versus greedyItems 2 and 3 fill capacity 7 exactly for value 9; greedy by value per weight takes items 4 and 1 for only 8.Capacity 7: which items fit best?item 1w 1 · v 1item 2w 3 · v 4item 3w 4 · v 5item 4w 5 · v 7Best (DP table)items 2 + 3#2#3weight 7 / 7value 9Greedy by v/witem 4 (1.4) + item 1#4#1weight 6 / 7value 8
The DP table finds value 9; picking the best value-per-weight first stops at 8.

How it is calculated

Let D[i][c] be the best value using only the first i items with capacity c.

To list the chosen items, the solver walks back: whenever D[i][c] differs from D[i−1][c], item i was taken and c drops by wᵢ. Weights are rounded to integers, both lists must have the same length, and capacity is limited to 100,000.

The principle

For the last item there are only two choices: leave it, or take it and solve the rest with less capacity. The best answer for the whole problem is the better of those two, and each depends only on smaller subproblems (fewer items, smaller capacity). The table has (n + 1) × (capacity + 1) cells, so the time is O(n · W). This is called pseudo-polynomial, because W is a number, not an input size; knapsack is NP-hard in general, so no known method is fast for every input with huge capacities.

Worked example

  1. Inputs: weights 1, 3, 4, 5; values 1, 4, 5, 7; capacity 7.
  2. The table's last row for capacities 0–7 is 0, 1, 1, 4, 5, 7, 8, 9, so the maximum value is 9.
  3. Trace-back picks item 3 (weight 4, value 5) and item 2 (weight 3, value 4): total weight 7, value 9, so items 2, 3.
  4. Compare the greedy approach by value per weight: item 4 (7 ÷ 5 = 1.4) first, then item 1 fits in the remaining 2 units, for only 8. Greedy is not optimal.

How to read the result

The result is the exact best value for the given numbers. If several combinations tie, the solver reports one of them. Try nearby capacities to see how much extra value one more unit of capacity would buy; here capacity 8 would allow items 2 and 4 for a value of 11.

Common mistakes

Keep in mind: The table grows with capacity, so very large capacities are refused. Problems with extra constraints may need integer programming tools.

Frequently asked questions

Why can't I just sort by value per weight?

That greedy rule can leave unused capacity, as the example shows; it is only optimal when items can be split.

Is the knapsack problem NP-complete?

Its decision version is, as shown by Karp in 1972, but the dynamic program is fast when capacities are moderate.

How much memory does it use?

About (n + 1) × (capacity + 1) numbers; the value alone can be found with a single row of capacity + 1.

Sources: R. M. Karp, "Reducibility among combinatorial problems", in Complexity of Computer Computations, Plenum Press, 1972 · T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, MIT Press (dynamic programming, greedy algorithms)

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →