A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Knapsack Problem and Pseudo-Polynomial Time

College Depth 92 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
544prerequisites beneath it
See this on the map →
NP-CompletenessTime Complexity and the Class P→→Approximation AlgorithmsTraveling Salesman Problem (TSP)
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 = 2100, the input is only about 100 bits long but the DP table has 2100 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

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Introduction to Propositional Logic → Introduction to Predicate Logic (First-Order Logic) → First-Order Logic Syntax → ZFC Axioms Overview → Axiom Schema of Separation → Axiom Schema of Replacement → Von Neumann Ordinals → Hereditarily Finite Sets → Recursive Definitions on Finite Sets → Well-Founded Relations and Transfinite Recursion → The Axiom of Choice and Equivalent Formulations → Axiom of Choice → Well-Ordering Theorem → Infinite Cardinal Numbers → Cantor's Theorem → Uncountability and the Diagonal Argument → The Cantor Set: An Uncountable Nowhere Dense Example → Uncountable Sets and Cantor Diagonalization → The Halting Problem → Computability Reductions → Polynomial-Time Reductions → Lower Bounds Techniques in Computational Complexity → NP-Completeness → Knapsack Problem and Pseudo-Polynomial Time

Longest path: 93 steps · 544 total prerequisite topics

Prerequisites (2)

Leads To (2)