Pseudo-Polynomial Time
Why O(nW) is not polynomial, and weak versus strong NP-hardness.
Subset sum is NP-complete, yet a table decides it for a thousand numbers in a blink. There is no contradiction: the table's width is the value of the target, while the input spells that value with only about bits. Give every number 30 more bits of precision and the input grows by 30 bits per number, while the table grows about a billionfold.
What you’ll learn
- Measure input size in bits, define pseudo-polynomial time with , and show that is exponential in the length of but polynomial when numbers are written in unary.
- Explain, through the reduction from 3-SAT, why subset sum is NP-complete only because its numbers may have polynomially many digits.
- Define strong NP-hardness, prove that a strongly NP-hard problem has no pseudo-polynomial algorithm unless P = NP, and classify partition, knapsack, 3-partition and bin packing.
- Choose between a table over weights, a table over values and meet in the middle from the sizes of the numbers, and state what fine-grained complexity says about beating .
Polynomial in what?
Complexity theory measures running time against the length of the input in bits. A knapsack instance with items whose weights, values and capacity are below has length . The table of the Optimized lesson costs , and with close to that is
One more bit in every number roughly doubles the work, while the input grows by only bits. The table is fast exactly when the numbers are small, and that is a statement about their values, not about the length of the input.
| Capacity | Bits to write | Columns in the table |
|---|---|---|
| 10 | ||
| 20 | ||
| 30 | ||
| 60 |
A subset-sum instance has numbers and a target , and the bitset table has columns. Every number, and the target, gets 10 more bits of precision (each becomes about times larger). By about what factor does the table grow?
Show the answer
1024
The table has columns, and grows by a factor of . The input grows by only bits, about 1.25 kilobytes. Ten more bits cost a thousandfold more time: exponential in the length of the numbers.
Where the hardness hides: in the digits
Subset sum is NP-complete: it is one of Karp's 21 problems (1972), where it is called KNAPSACK. So any proof that it is NP-hard must produce instances whose numbers are exponentially large in their length. If a reduction from 3-SAT produced numbers bounded by a polynomial, the table would solve 3-SAT in polynomial time. The classic reduction shows where the large numbers come from.
Take a 3-CNF formula with variables and clauses . Every number has decimal digits: one per variable, one per clause. For each variable make two numbers, (" true") and (" false"), with a 1 in digit ; also has a 1 in digit when the literal occurs in , and when does.
For each clause add two slack numbers with a single 1 in digit . The target has a 1 in every variable digit and a 3 in every clause digit:
No carries. A clause digit receives at most 3 from literals and 2 from slacks, at most 5 < 10, so the digits add independently. A variable digit equal to 1 forces exactly one of : a truth assignment.
Both directions. A clause digit must reach 3 and the slacks give at most 2, so at least one chosen literal is true in every clause: the assignment satisfies the formula. Conversely, a satisfying assignment gives each clause 1 to 3 true literals, and 2, 1 or 0 slacks fill the digit up to 3. So the formula is satisfiable iff some subset sums to .
Sizes. There are numbers of digits each, a polynomial length; the reduction is a polynomial-time reduction. But the target is about , so the table would have about columns: no contradiction with .
| Number | |||||
|---|---|---|---|---|---|
| 1 | 0 | 0 | 1 | 0 | |
| 1 | 0 | 0 | 0 | 1 | |
| 0 | 1 | 0 | 1 | 1 | |
| 0 | 1 | 0 | 0 | 0 | |
| 0 | 0 | 1 | 0 | 1 | |
| 0 | 0 | 1 | 1 | 0 | |
| 0 | 0 | 0 | 1 | 0 | |
| 0 | 0 | 0 | 0 | 1 | |
| 1 | 1 | 1 | 3 | 3 |
Think first Subset sum is NP-complete, and it has an algorithm. Does that prove P = NP?
No. is polynomial in the value , which takes only about bits to write. The reduction from 3-SAT produces targets near , where the table is exponentially large, so nothing follows about P versus NP. Proving P = NP this way would need an algorithm polynomial in and .
Strong NP-hardness
Let be strongly NP-hard with polynomial , and let be its restriction to the instances with ; by definition is NP-hard. Suppose an algorithm solves in time bounded by a polynomial :
On an instance of , substitute the bound on . What remains is a polynomial in alone:
So solves the NP-hard problem in polynomial time. Every problem in NP reduces to in polynomial time, hence
The canonical strongly NP-complete number problem is 3-PARTITION: given positive integers with and total , can they be split into triples that each sum to ? Garey and Johnson (1975) proved it NP-complete even when is bounded by a polynomial in , and it passes strong hardness on to many problems with numbers:
| Problem | Pseudo-polynomial algorithm? | Status |
|---|---|---|
| Subset sum, partition | table over sums | weakly NP-complete |
| 0/1 knapsack | over weights, or over values | weakly NP-hard |
| Scheduling jobs on 2 identical machines to finish earliest | table over the first machine's load | weakly NP-hard (it is partition) |
| The same with machines, part of the input | none unless P = NP | strongly NP-hard (from 3-PARTITION) |
| Bin packing: items into bins of capacity | none unless P = NP | strongly NP-hard (3-PARTITION is the case ) |
| TSP with integer distances | none unless P = NP | strongly NP-hard: distances 1 and 2 encode Hamiltonian cycle |
In a bin-packing instance all item sizes and the capacity are integers at most , and the number of bins is part of the input. Can some algorithm polynomial in and decide whether bins suffice?
- Yes: keep a table over the loads of all bins, as in knapsack
- Not unless P = NP: bin packing stays NP-hard when its numbers are polynomially bounded
- Yes: small numbers make every number problem easy
- Yes: meet in the middle over the items
Show the answer
B. Not unless P = NP: bin packing stays NP-hard when its numbers are polynomially bounded
Bin packing contains 3-PARTITION, which is NP-hard even with numbers bounded by a polynomial. An algorithm polynomial in and would solve those instances in polynomial time, so it exists only if P = NP.
The same substitution rules out fully polynomial-time approximation schemes for strongly NP-hard optimization problems whose optimal values are polynomially bounded integers: see Approximating Knapsack.
Living with large numbers
Weak NP-hardness is good news in practice: it names the feature of an instance that makes it hard. Read the constraints, see which quantity is small, and index the table by it, or pick a method that does not need a table.
| What is small | Method | Time |
|---|---|---|
| The capacity | Table over weights (the Optimized lesson) | |
| The total value | Table over values: the least weight that reaches each total value | |
| All weights share a factor | Divide the weights by , and by rounding down | |
| The number of items, | Meet in the middle: subset sums of each half, then match them (Horowitz & Sahni, 1974) | |
| Nothing, but a near-optimal answer is enough | Round the values: an FPTAS (Approximating Knapsack) | polynomial in and |
def knapsack_by_value(weights, values, W):
V = sum(values)
INF = float('inf')
least = [0] + [INF] * V # least[q]: least weight with value exactly q
for w, v in zip(weights, values):
for q in range(V, v - 1, -1): # downward: each item at most once
least[q] = min(least[q], least[q - v] + w)
return max(q for q in range(V + 1) if least[q] <= W)
print(knapsack_by_value([10**9, 6 * 10**8, 5 * 10**8], [7, 4, 4], 11 * 10**8)) # 8Weights near would need a billion columns; total values up to 15 need 16. The two lighter items (weight , value 8) beat the heaviest one alone (value 7).
Key takeaways
- Running time is measured in input bits: is for -bit numbers, so it is pseudo-polynomial, polynomial in and , or in unary.
- Subset sum, partition and knapsack are weakly NP-hard: reductions to them, like the digit construction from 3-SAT, must create exponentially large numbers.
- A strongly NP-hard problem (3-PARTITION, bin packing, TSP) stays NP-hard with polynomially bounded numbers, so it has no pseudo-polynomial algorithm unless P = NP.
- When is large, change the table (index it by value), split the search (meet in the middle) or settle for an approximation; fine-grained conjectures say cannot be beaten by much for 0/1 knapsack.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which running time for 0/1 knapsack with items and capacity would be a genuinely polynomial-time algorithm, and so would prove P = NP?
Show the answer
C.
The input length is about bits, so a bound polynomial in and is polynomial in the input length. Any bound that grows like a power of itself is exponential in the number of bits of .
3-PARTITION is strongly NP-complete. Which statement follows?
- It has no polynomial-time algorithm, even if P = NP
- It has no pseudo-polynomial algorithm unless P = NP
- Its hard instances must contain exponentially large numbers
- It can be solved by a table over sums, like partition
Show the answer
B. It has no pseudo-polynomial algorithm unless P = NP
Strong NP-hardness means it stays NP-hard on instances whose numbers are bounded by a polynomial in the length. A pseudo-polynomial algorithm would run in polynomial time on those instances, and so would solve an NP-hard problem in polynomial time.
Which of these problems is weakly NP-hard, that is, NP-hard but solvable in pseudo-polynomial time?
- Bin packing: can items be packed into bins of capacity ?
- Partition: can the numbers be split into two groups with equal sums?
- 3-PARTITION
- The traveling salesman problem with integer distances
Show the answer
B. Partition: can the numbers be split into two groups with equal sums?
Partition is subset sum with target , solved in time by the table. The other three remain NP-hard with polynomially bounded numbers.
Suppose someone gave a polynomial-time reduction from 3-SAT to subset sum whose numbers were all bounded by a polynomial in the formula's size. What would follow?
- Nothing new: the classic reduction already does that
- P = NP, because the table would then decide 3-SAT in polynomial time
- Subset sum would become strongly NP-hard but still in P
- 3-SAT would become weakly NP-hard
Show the answer
B. P = NP, because the table would then decide 3-SAT in polynomial time
With bounded by a polynomial, the table runs in polynomial time on the reduced instances, so composing the reduction with the table decides 3-SAT in polynomial time. That is why every known reduction to subset sum produces exponentially large numbers.
Further reading
- M. R. Garey & D. S. Johnson, "'Strong' NP-completeness results: Motivation, examples, and implications," Journal of the ACM 25(3) (1978), 499–508.
- M. R. Garey & D. S. Johnson, Computers and Intractability: A Guide to the Theory of NP-Completeness (Freeman, 1979), §4.2, "Number problems and strong NP-completeness".
- R. M. Karp, "Reducibility among combinatorial problems," in Complexity of Computer Computations (Plenum, 1972), 85–103.
- M. Cygan, M. Mucha, K. Węgrzycki & M. Włodarczyk, "On problems equivalent to (min,+)-convolution," ACM Transactions on Algorithms (2019); ICALP 2017.
- C. Jin, "0-1 Knapsack in nearly quadratic time," STOC 2024.