Sub topic 17
Pseudo-Polynomial Time and Subset Sum
This chapter assumes you know the Subset Sum problem from the tour of classic NP-complete problems. You should also know Big-O notation and the rule that "polynomial time" means polynomial in the input size.
Subset Sum is NP-complete. Despite that, a short dynamic program solves it quickly for many inputs. The two facts fit together once you look closely at what "input size" means.
A Fast-Looking Algorithm
The program below records which sums from 0 to target can be reached with some subset of the numbers seen so far.
def subset_sum_dp(numbers, target):
"""numbers: non-negative integers; target: non-negative integer."""
reachable = [False] * (target + 1)
reachable[0] = True
for x in numbers:
for s in range(target, x - 1, -1):
if reachable[s - x]:
reachable[s] = True
return reachable[target]
print(subset_sum_dp([3, 34, 4, 12, 5, 2], 9)) # True (4 + 5)
print(subset_sum_dp([3, 34, 4, 12, 5, 2], 30)) # FalseThe inner loop runs backward so that each number is used at most once. The total work is O(n · T), where n is the count of numbers and T is the target value. That looks polynomial, but it isn't.
Value Versus Length
Input size means the number of bits needed to write the input down. A target T takes about b = log₂ T bits. That makes O(n · T) equal to O(n · 2^b), which is exponential in the length of the input.
| Target bits b | Target value T (about) | DP table size |
|---|---|---|
| 10 | 1,000 | 1 thousand cells |
| 30 | 1 billion | 1 billion cells |
| 64 | 1.8 × 10^19 | infeasible |
Adding one bit to the target doubles the work. An algorithm that is polynomial in the numeric values of the input, but not in their bit length, is called pseudo-polynomial.
Weak and Strong NP-Completeness
These ideas split NP-complete problems into two groups:
- Weakly NP-complete problems become easy when their numbers are small, meaning bounded by a polynomial in n. Subset Sum, Partition, and 0/1 Knapsack are in this group.
- Strongly NP-complete problems stay NP-complete even when every number is that small, or when the input has no numbers at all. 3-SAT, Vertex Cover, and 3-Partition are in this group. Unless P = NP, a strongly NP-complete problem has no pseudo-polynomial algorithm.
In practice, the pseudo-polynomial DP is often the right tool when the numbers are bounded. Budgets in whole dollars and weights in grams are typical cases.
Exercise
A Subset Sum instance has 100 numbers and a target written with 40 bits. Roughly how many table updates does subsetsumdp perform in the worst case, and is that practical?
Show answer
About n · T = 100 · 2^40, which is roughly 1.1 × 10^14 updates. That is too many for a quick run, and every extra bit in the target doubles it.
Exercise
Could Vertex Cover have a pseudo-polynomial algorithm, assuming P ≠ NP?
Show answer
No. Vertex Cover's only number is k, and k is at most the number of vertices, so its values are already polynomially bounded. A pseudo-polynomial algorithm would therefore run in true polynomial time, which would imply P = NP.