Knapsack 0/1 Calculator (Combinatorial Optimization)
Knapsack 0/1, fractional and bounded calculator: dynamic programming, greedy, value/weight efficiency comparison, FPTAS overview and solution reconstruction.
Report a calculation error
Help us improve. Describe what is wrong with the calculation.
Result
| Voce | Valore |
|---|
Show full details
Share result
Disclaimer: this calculation is for informational purposes only. For important decisions, consult a qualified professional.
The knapsack problem
The knapsack problem is one of Karp's original 21 NP-complete problems (1972) and a cornerstone of combinatorial optimization. Given a collection of items each with weight and value, and a knapsack of capacity W, determine the subset maximizing total value without exceeding capacity. Main variants are 0/1 (each item taken whole or not), fractional (fractions allowed), and bounded (limited copies).
Mathematical formulation
Maximize Sigma v_i * x_i subject to Sigma w_i * x_i = V exist?" is NP-complete; optimization is NP-hard. Pseudo-polynomial O(nW) algorithms exist via dynamic programming.
Dynamic programming
The classic algorithm builds dp[i][w] = max value using first i items with capacity w. Recurrence: dp[i][w] = max(dp[i-1][w], dp[i-1][w-w_i] + v_i) if w_i <= w, else dp[i-1][w]. Base case dp[0][w] = 0. Optimal solution is dp[n][W]; chosen items recovered by backtracking comparing dp[i][w] and dp[i-1][w]. Complexity O(nW) time, O(nW) space (reducible to O(W) with reverse iteration).
The four modes
- Solve 0/1: classic dynamic programming with optimal solution reconstruction.
- Fractional: greedy algorithm sorting by v/w descending and filling until capacity exhausted.
- Bounded: each item has max quantity q_i. Implemented via binary decomposition reducing to 0/1 with O(log q_i) copies.
- Efficiency comparison: computes value/weight ratio per item, useful to intuit greedy solution.
Greedy fails on 0/1
Sorting by v/w descending solves fractional but can be arbitrarily suboptimal for 0/1. Example: W=10, items [(6,60), (5,50), (5,50)]. Greedy picks first (ratio 10) leaving W=4 unused: value 60. Optimum picks last two: value 100. Modified greedy (max of greedy vs best single item) guarantees 1/2 of optimum.
Approximation: FPTAS
A fully polynomial approximation scheme (FPTAS) exists for 0/1 knapsack: for every epsilon>0, O(n^3/epsilon) algorithm returning value >= (1-epsilon) * OPT. Obtained by scaling values by v_max*epsilon/n and applying DP keyed on values instead of weights.
Classic example
W=10, items: (w=2, v=3), (w=3, v=4), (w=4, v=5), (w=5, v=6). DP yields dp[4][10] = 13 selecting items 1, 2, 4 (weights 2+3+5=10, values 3+4+6=13). Use as benchmark to verify code.
Real applications
- Budget allocation: project selection with maximum ROI under budget constraint.
- Cutting stock: optimization in textile, paper, metal cutting.
- Portfolio optimization: stock selection with capital limit.
- Container loading: sea and air logistics.
- Cloud computing resource allocation: VM placement on hosts with RAM/CPU limits.
- Fixed-bitrate lossy data compression.
History of the knapsack problem
The knapsack problem takes its name from the intuitive situation of someone who must fill a bag of limited capacity by choosing, among many objects, those of maximum total value. Formalised in operations research in the mid-twentieth century, it became one of the most studied problems in theoretical computer science. In 1972 Richard Karp included it in his famous list of 21 NP-complete problems, sealing its intrinsic difficulty. The problem also played a historical role in cryptography: in 1978 Ralph Merkle and Martin Hellman proposed one of the first public-key cryptosystems based on the knapsack (the Merkle-Hellman knapsack). The idea was to use the problem's difficulty as a security guarantee, but the system was spectacularly broken by Adi Shamir in 1982, who exploited its special structure. Since then the knapsack has remained a fundamental testbed for exact, heuristic and approximation algorithms.
Why the algorithm is pseudo-polynomial
Dynamic programming solves 0/1 knapsack in time O(n·W), where n is the number of items and W the capacity. At first sight it seems efficient, but there is a subtle detail: W is a numerical value, not the size of the input. To represent the capacity W only about log₂(W) bits are needed, so the time O(n·W) is exponential in the length of the input data. This is what is meant by a pseudo-polynomial algorithm: polynomial in the value of the numbers, but exponential in the number of digits. This is why knapsack is weakly NP-hard: if capacities and weights stay small, the DP is very fast, but with huge numbers (hundreds of digits) it becomes impractical. A truly polynomial algorithm in the input size is not known, and its existence would amount to proving P = NP.
Updated July 2026
Comments (0)
Login to leave a comment.
No comments yet. Be the first!