Algorithms Foundations
Knapsack Problem
The canonical constrained optimization problem: 0/1 knapsack (NP-hard, pseudo-polynomial DP), fractional knapsack (greedy), FPTAS, and connections to Lagrangian relaxation in ML.
Prerequisites
Why This Matters
The knapsack problem is the prototype of constrained optimization: maximize value subject to a resource constraint. This pattern is everywhere in machine learning. Selecting features subject to a computation budget, allocating GPU memory across model components, choosing which data points to label given an annotation budget. these are all knapsack-type problems.
Understanding knapsack gives you three fundamental algorithm design techniques in one problem: dynamic programming (exact solution), greedy (fast approximation for the fractional case), and FPTAS (near-optimal in polynomial time).
Mental Model
You have a backpack with a weight limit . There are items, each with a weight and a value. You want to pack the most valuable subset that fits. The catch: you cannot split items (0/1 knapsack) or you can split them (fractional knapsack). This simple change. can you take fractions?. flips the problem from NP-hard to polynomial.
Problem Definitions
0/1 Knapsack Problem
Given items with weights and values , and a capacity , find a subset that maximizes:
Each item is either included entirely or not at all.
Fractional Knapsack Problem
Same setup, but you may take any fraction of each item. Maximize:
The 0/1 Knapsack: Dynamic Programming
0/1 Knapsack DP Solution
Statement
Define as the maximum value achievable using items with capacity . The recurrence is:
with base case for all . The optimal value is . This algorithm runs in time and space (reducible to space).
Intuition
For each item, you have two choices: skip it or take it. If you skip item , the best value with capacity is the same as with items . If you take item , you gain but lose capacity. The recurrence considers both choices and picks the better one.
Proof Sketch
By induction on . Base case: with zero items, the value is 0 for any capacity. Inductive step: assume is correct. For item , any optimal solution either includes or not. If it includes , the remaining items form an optimal solution for items with capacity (by cut-and-paste argument). If not, the remaining items form an optimal solution for items with capacity . The max of these two cases gives the optimal.
Why It Matters
This is the textbook example of pseudo-polynomial time: looks polynomial, but can be exponential in the input size (which is bits). This distinction between polynomial and pseudo-polynomial is essential for understanding NP-hardness and approximation algorithms.
Failure Mode
The algorithm requires integer weights. For real-valued weights, you must discretize (which introduces approximation error) or use a different approach. The runtime is impractical when is very large (e.g., ).
The Fractional Knapsack: Greedy
Greedy Optimality for Fractional Knapsack
Statement
Sort items by value-to-weight ratio in decreasing order. Greedily take as much as possible of each item in this order until the knapsack is full. This greedy algorithm produces an optimal solution to the fractional knapsack problem in time.
Intuition
Each "unit of weight" of item is worth . To maximize total value, you should fill the knapsack with the most valuable units first. Since you can take fractions, you can perfectly fill the knapsack by taking the highest-density items and splitting the last one.
Proof Sketch
Suppose the greedy solution is not optimal. Then there exists an optimal solution that takes less of some high-ratio item and more of some low-ratio item . Swap: replace some of item with the same weight of item . Since , the total value increases, contradicting optimality of the supposed better solution.
Why It Matters
This is a clean example of the greedy method working. The key insight: the fractional relaxation removes the combinatorial structure that makes 0/1 knapsack hard. This connection between fractional relaxation and greedy optimality appears throughout combinatorial optimization.
Failure Mode
The greedy algorithm is not optimal for 0/1 knapsack. Classic counterexample: items with and . Greedy by ratio takes items 1 and 2 (value 16), but optimal takes items 2 and 3 (value 22).
The FPTAS: Near-Optimal in Polynomial Time
FPTAS for 0/1 Knapsack
Statement
There exists a fully polynomial-time approximation scheme (FPTAS) for 0/1 knapsack that, for any , produces a solution with value at least in time .
Intuition
The DP runs in time. Instead of DP over weights, do DP over values: is the minimum weight to achieve value using items . The issue is that values can be large. The FPTAS fixes this by rounding all values down to multiples of a small number , reducing the number of distinct value levels to .
Proof Sketch
Rounding each value down by at most changes the optimal value by at most . So the rounded solution has value . The DP over rounded values has value levels and items, giving time. Since this is polynomial in both and , it is a FPTAS.
Why It Matters
The FPTAS shows that while 0/1 knapsack is NP-hard exactly, it is easy to approximate. You can get within any desired fraction of optimal in polynomial time. This is the gold standard for NP-hard problems. Not all NP-hard problems admit an FPTAS (unless P = NP).
Failure Mode
The runtime can still be large. For and , you need operations. In practice, branch-and-bound methods are often faster for real-world instances.
Connection to Lagrangian Relaxation
The constrained optimization structure of knapsack connects directly to Lagrangian methods used throughout ML. The Lagrangian relaxation of the 0/1 knapsack is:
For a given multiplier , this decomposes into independent decisions: include item if , i.e., if the "value minus cost" is positive. The optimal is found by solving the dual problem .
This Lagrangian perspective appears whenever you have a constrained optimization in ML: the multiplier plays the same role as the regularization parameter in regularized ERM, the KL penalty coefficient in RLHF, or the constraint threshold in constrained optimization.
Common Confusions
Pseudo-polynomial is not polynomial
The DP looks polynomial, but is an integer whose representation requires bits. The runtime is exponential in the input size . If someone gives you a knapsack instance with , the DP is utterly infeasible despite being "O(nW)."
Greedy works for fractional but not 0/1
The greedy algorithm (sort by value-to-weight ratio) is optimal for fractional knapsack but can be arbitrarily bad for 0/1 knapsack without modification. The integrality constraint changes the problem from continuous optimization (easy) to combinatorial optimization (hard).
NP-hard does not mean practically impossible
The FPTAS gives near-optimal solutions in polynomial time. Branch-and-bound solves most practical instances quickly. NP-hardness is a worst-case complexity statement, not a practical impossibility statement. Real-world knapsack instances with millions of items are routinely solved.
Summary
- 0/1 knapsack: NP-hard, solved by DP in pseudo-polynomial time
- Fractional knapsack: greedy (sort by ) is optimal,
- FPTAS: -approximate solution in time
- Pseudo-polynomial is not polynomial: depends on the magnitude of
- Lagrangian relaxation connects knapsack to regularized optimization in ML
- The fractional/0/1 gap illustrates how integrality makes problems hard
Exercises
Problem
Solve the 0/1 knapsack instance: items with capacity . Fill in the DP table and trace back the optimal solution.
Problem
Prove that the greedy algorithm for 0/1 knapsack (take items in decreasing order of until one does not fit, then skip it) can achieve value as low as in the worst case. Give a concrete example.
Problem
Explain why the knapsack FPTAS does not violate the fact that 0/1 knapsack is NP-hard. What is the distinction between exact and approximate solutions?
References
Canonical:
- Korte & Vygen, Combinatorial Optimization, Chapter 17
- Garey & Johnson, Computers and Intractability (1979)
Current:
- Kellerer, Pferschy, Pisinger, Knapsack Problems (2004)
- Williamson & Shmoys, The Design of Approximation Algorithms (2011), Chapter 3
Next Topics
The natural next steps from knapsack:
- Lagrangian relaxation: the general framework for constrained optimization
- Integer programming: the broader class of problems knapsack belongs to
- Approximation algorithms: systematic techniques for NP-hard problems
Last reviewed: April 2026
Prerequisites
Foundations this topic depends on.
- Dynamic ProgrammingLayer 0A
- Greedy AlgorithmsLayer 0A