Knapsack Problem and Pseudo-Polynomial Time

College Depth 73 in the knowledge graph I know this Set as goal
Unlocks 5 downstream topics
np-hard optimization dynamic-programming

Core Idea

The 0/1 knapsack problem is NP-hard in the strong sense, but admits a pseudo-polynomial time algorithm using dynamic programming. This algorithm runs in time O(nW) where W is the knapsack capacity. Pseudo-polynomial algorithms are tractable when input values are small but become exponential if values are encoded in binary, illustrating the distinction between weak and strong NP-hardness.

How It's Best Learned

Implement the DP solution O(nW) and observe that it depends on the value W, not the bit-length of W. Try the same problem with exponentially large weights to see where the algorithm breaks down.

Common Misconceptions

Explainer

The 0/1 knapsack problem asks: given n items each with a weight and a value, and a capacity W, which items should you pack to maximize value without exceeding the capacity? You already know this is NP-hard. But it behaves differently from problems like graph 3-coloring or SAT in a subtle and instructive way: it has a dynamic programming solution that appears — but isn't quite — polynomial.

The DP algorithm builds a table where entry (i, w) stores the maximum value achievable using the first i items with weight budget w. Filling this table takes O(nW) time. For small W — say, items with weights in the hundreds — this is perfectly practical. The confusion arises because complexity is measured in the bit-length of the input, not the numerical value of the numbers. W can be encoded in log₂W bits, meaning that if W = 2^{100}, the input is only about 100 bits long but the DP table has 2^{100} entries. The algorithm's running time is exponential in the input size. This is what pseudo-polynomial time means: polynomial in the numerical value of the input, but potentially exponential in the bit-length.

This distinction separates weakly NP-hard problems from strongly NP-hard ones. Knapsack is weakly NP-hard: hard in general, but tractable when input values are bounded by a polynomial in n. By contrast, 3-SAT is strongly NP-hard — no pseudo-polynomial shortcut exists because the problem has no natural numeric parameter to exploit. The significance for approximation is that weakly NP-hard problems often admit an FPTAS. For knapsack, you can round the item values to nearby multiples, making W effectively small enough for the DP to run in polynomial time in both n and 1/ε — giving a (1+ε)-approximation for any ε > 0 at polynomial cost.

The knapsack problem is also a useful lens on the relationship between DP and NP-hardness more generally. Dynamic programming solves many optimization problems efficiently by exploiting optimal substructure — the property that the optimal solution to the whole problem can be built from optimal solutions to subproblems. For knapsack, this structure exists, but the state space (the table size) depends on the input values rather than just their count. When the values are bounded, the DP is fast. When they are unbounded, the DP is exponential, and we are back to the NP-hard baseline. The lesson: DP does not bypass NP-hardness; it works within it when the problem's numeric structure allows.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionBig-O Notation and Asymptotic AnalysisBreadth-First Search (BFS)Shortest Paths in Unweighted GraphsDijkstra's Shortest Path AlgorithmAlgorithm Analysis and Big-O NotationTuring MachinesThe Church-Turing ThesisThe Halting ProblemComputability ReductionsPolynomial-Time ReductionsLower Bounds Techniques in Computational ComplexityNP-CompletenessKnapsack Problem and Pseudo-Polynomial Time

Longest path: 74 steps · 415 total prerequisite topics

Prerequisites (2)

Leads To (2)