Sub topic 12
Pseudo-Polynomial Time and NP-Hardness
This chapter assumes you know Big-O notation and the bottom-up DP table from earlier chapters. That table has n + 1 rows and W + 1 columns, so it runs in O(nW) time. The bound looks polynomial. This chapter explains why it is not, and how that fits with the fact that 0/1 knapsack is NP-hard.
How input size is measured
Complexity theory measures running time against the length of the input in bits. A list of n weights needs about n numbers. The capacity W is a single number, and writing it down takes only about b = log2(W) bits.
The DP table has one column per unit of capacity. When you add one bit to W, the number of columns doubles. In terms of input length, O(nW) is O(n · 2^b), which is exponential in b.
An algorithm is pseudo-polynomial when its running time is polynomial in the numeric values of the input but not in the number of bits needed to write them.
Seeing it in practice
Multiplying every weight and the capacity by the same factor leaves the optimal set of items unchanged. The DP table still grows by that factor.
n = 3
base_capacity = 50
for scale in [1, 1_000, 1_000_000]:
W = base_capacity * scale
cells = (n + 1) * (W + 1)
print(f"scale={scale:>9} bits={W.bit_length():>2} cells={cells:,}")Output:
scale= 1 bits= 6 cells=204
scale= 1000 bits=16 cells=200,004
scale= 1000000 bits=26 cells=200,000,004The input grew by 20 bits and the work grew a million times.
Where NP-hardness comes in
The decision version of the problem asks: "Is there a subset with weight at most W and value at least V?" It is NP-complete. Subset sum, a known NP-complete problem, reduces to it by setting each item's value equal to its weight. A truly polynomial algorithm for knapsack would therefore prove P = NP.
The O(nW) DP does not contradict this, because it is only pseudo-polynomial. Knapsack is called weakly NP-hard. It becomes easy when the numbers are small, meaning bounded by a polynomial in n. Strongly NP-hard problems stay hard even when every number is small. The multidimensional knapsack covered later in this tutorial is one of them.
| Situation | Practical consequence |
|---|---|
| `W` up to about 10^7 | Weight-indexed DP is fast |
| `W` huge, values small | Index the DP by value instead |
| Both huge, `n` small | Use meet in the middle or branch and bound |
| Both huge, `n` large | Use approximation (FPTAS) |
Common errors
- Calling O(nW) "polynomial" without saying that it depends on the size of
W. - Running the DP on weights with decimals by multiplying by 10^k. Each extra digit of precision makes the table 10 times larger.
- Assuming a large
nis the main danger. A capacity of 10^12 with only 30 items already rules out the table.
Exercise
An instance has n = 20 items and a capacity written as a 40-bit number. Roughly how many cells does the weight-indexed DP table need, and is it practical?
Show answer
About 20 × 2^40 ≈ 2.2 × 10^13 cells. That is far too many for memory or time. With only 20 items, brute force over 2^20 ≈ 10^6 subsets, or meet in the middle, is much faster.
Exercise
Why doesn't the O(nW) algorithm prove that P = NP?
Show answer
Polynomial time is measured against input length in bits. W takes only log2(W) bits to write, so O(nW) is exponential in the input length. The algorithm is pseudo-polynomial, not polynomial.