Skip to content

Knapsack Problem Calculator

Choose the maximum-value subset of indivisible items under a whole-number capacity with an exact, inspectable 0/1 dynamic program.

Privacy: your files never leave your device. All processing happens locally in your browser.

How to use

  1. 1.Enter a whole-number capacity from 1 through 10,000.
  2. 2.Add each indivisible item as label, weight, value, using a positive whole-number weight.
  3. 3.Solve the exact 0/1 model, inspect every selected item and assumption, then copy the selection if useful.

About Knapsack Problem Calculator

Knapsack Problem Calculator finds the highest-value subset of indivisible items that fits within one capacity. Enter a whole-number capacity and rows in label, weight, value format. The result identifies every selected item, total value, total weight, and unused capacity. Calculation stays in the browser, and the optimal selection can be copied as plain text.

This page solves the classic single-container 0/1 knapsack problem. “0/1” means each listed item is either excluded or selected once; it cannot be split, repeated, or partially packed. Google OR-Tools describes the same objective as choosing a subset whose total value is maximized without exceeding capacity. This differs from fractional knapsack, where portions may be taken, and from multiple-knapsack or bin-packing problems, which use several containers or another objective.

The implementation uses exact dynamic programming for integer capacity. For each item and each capacity from zero through the entered limit, it compares two states: the best value obtained without the item, and the best value obtained by including it plus the best compatible earlier state. The larger value is retained. After the table is complete, the solver walks backward through the decisions to reconstruct the chosen labels. Equal-value ties retain the earlier solution rather than switching arbitrarily, so repeated input produces a deterministic result.

Exact here refers to the stated bounded model, not to every real packing problem. The dynamic program proves the maximum entered value for one capacity, positive whole-number weights, and items used at most once. It does not know physical dimensions, balance, fragility, item interactions, mandatory groups, incompatible pairs, deadlines, risk, or uncertainty. A mathematically optimal scalar selection can still be physically impossible or operationally inappropriate.

Capacity and weights must be whole numbers because the table has one state per capacity unit. If your measurements have fixed decimals, scale them consistently before entry—for example, convert 2.5 kilograms to 25 tenths—and remember that a finer scale creates a larger table. Capacity is limited to 10,000 and the list to 100 items to keep browser memory and response time bounded. Values may be zero or positive decimals and need not use the same unit as weight.

The tool rejects blank fields, duplicate labels, nonpositive weights, weights larger than capacity, non-finite values, and malformed comma-separated rows. Labels cannot contain commas because commas delimit the three fields. Items heavier than capacity are rejected rather than silently ignored, making it clear that the input cannot be represented as a selectable candidate under the current capacity.

Eight hand-audited golden cases cover exact fills, equal-value ties, decimal values, zero-value items, and examples where choosing the highest value-to-weight ratio greedily would miss the optimum. Tests also assert that selected weight never exceeds capacity. The classic 10-capacity case chooses weight 4/value 40 plus weight 3/value 50 for value 90, rather than following a simplistic input order.

Use the result for classroom problems, feature prioritization with an explicit effort budget, bounded portfolio examples, or small allocation drafts. For financial portfolios, safety loading, medical resource allocation, or other high-stakes decisions, the scalar model is insufficient: validate the objective, dependencies, constraints, and real-world consequences with an appropriate domain model and accountable reviewer.

Methodology & sources

Validate one whole-number capacity and up to 100 unique labelled items, build the standard 0/1 dynamic-programming table over items and capacity, retain exclusion on exact value ties for deterministic reconstruction, and backtrack the maximum-value selection. Assert each item appears at most once and total weight never exceeds capacity.

Frequently asked questions

Is the returned value globally optimal?
Yes for the entered bounded 0/1 model: one capacity, whole-number weights, nonnegative values, and each item available once.
Can an item be split or selected more than once?
No. Each row is an indivisible item that may be selected zero or one time.
Why must weights be whole numbers?
The exact dynamic-programming table uses one state per capacity unit; fixed decimals can be scaled consistently before entry.
Does this pack physical boxes?
No. It models one scalar weight or cost and ignores dimensions, balance, fragility, and other physical constraints.

Calculators guides

View all