Subset Sum, Faster
Bitset DP, and near-linear pseudo-polynomial algorithms.
Bellman's table answers "does some subset of numbers sum to ?" in time, and for sixty years that looked like the end of the story. In 2017 two papers broke it, one reaching , and soon after a lower bound based on the Strong Exponential Time Hypothesis showed that the dependence on cannot be improved much further.
What you’ll learn
- Run the subset-sum table as shifts and ORs of a bitset, prove its invariant, and count its cost: word operations on a word RAM.
- Compute all subset sums up to with sumsets and the FFT, and analyze the divide and conquer behind the bound.
- Explain the ideas of the algorithms (color coding; power series) and state the SETH-based lower bound that rules out .
- Solve subset sum with huge numbers in time by meet in the middle and in space by Schroeppel and Shamir, and say what is open below .
Bellman's table as a bitset
Let be the set of sums of subsets of the first numbers that are at most , stored as a bit vector of length (bit set iff ). Bellman's recurrence (1957) is one line of bit operations:
Invariant: a subset of the first numbers either avoids (its sum is in ) or contains it (its sum is more than a sum in ). On a word RAM with -bit words, shifting and ORing a -bit vector costs word operations, so the whole table costs : the same cells, 64 at a time (word-parallelism).
#include <bitset>
#include <cstdio>
#include <vector>
const int MAXT = 100000; // largest target, fixed at compile time
bool subset_sum(const std::vector<int>& a, int t) {
std::bitset<MAXT + 1> reach; // reach[s] == 1 <=> some subset sums to s
reach[0] = 1;
for (int x : a) reach |= reach << x; // bits shifted past MAXT are dropped
return reach[t];
}
int main() {
printf("%d %d\n", subset_sum({2, 3, 7}, 12), subset_sum({2, 3, 7}, 11)); // 1 0
}The contest idiom: std::bitset packs the table into 64-bit words, so and cost about word operations.
With numbers, target and 64-bit words, about how many million word operations does the bitset table perform?
Show the answer
15.6
, about 15.6 million: a fraction of a second, against cell updates for the plain table. It is still pseudo-polynomial: dividing by 64 is a constant factor.
Sumsets and the FFT
Write for the set of all subset sums of a multiset , and for the sumset. If and are disjoint, every subset of is a subset of plus a subset of , so
A sumset is a convolution in disguise: for , multiply the polynomials and ; the exponents with nonzero coefficients are exactly . With the FFT (see The Fast Fourier Transform) that takes time, however large and are.
ALL-SUMS(A, u): # every subset sum of the multiset A that is at most u
if |A| = 1: return {0, a} ∩ [0, u] # A = {a}
split A into two halves A1, A2
S1 ← ALL-SUMS(A1, min(u, sum(A1)))
S2 ← ALL-SUMS(A2, min(u, sum(A2)))
return SUMSET(S1, S2) ∩ [0, u] # FFT of two 0/1 vectors of length ≤ min(u, sum(A)) + 1Call ALL-SUMS(A, t). Every number is positive, so sums above the cap can be dropped at every level.
Let . At recursion depth the subproblems partition , and each one's cap is at most the sum of its own items, so the caps at one depth add up to at most :
An FFT on vectors of length costs , so one depth costs , and there are depths:
With the cap at every call instead, depth would cost , and the total would grow to , worse than the bitset. Koiliaris and Xu (SODA 2017; journal version 2019) combined sumset recursions of this kind with further ideas to obtain a deterministic algorithm for all subset sums up to :
Near-linear time, and a matching lower bound
Two randomized algorithms reach , near-linear in the length of one row of Bellman's table.
- Bringmann (SODA 2017) groups the numbers into layers by size. A subset with sum at most contains fewer than numbers larger than , so only few large numbers can be used together. Inside a layer he uses color coding: a random partition of the numbers into groups in which, with good probability, the numbers of a fixed solution land in different groups. Each group then contributes at most one number, and the work reduces to sumsets of short lists with small caps. The error is one-sided: a "yes" is always correct.
- Jin and Wu (SOSA 2019) count instead of deciding, with a generating function and power-series arithmetic modulo a prime.
The coefficient of in this product counts the subsets with sum :
Take logarithms. The product becomes a sum, and each factor has a sparse series:
Only exponents up to matter. Group equal values: copies of contribute times the series of , which has terms. Over the distinct values that is terms in all, by the harmonic series:
Exponentiate the truncated series with Newton's method in arithmetic operations, working modulo a random prime so that dividing by is allowed. If , some subset sums to . If , it is at most , so it has at most prime factors, and a random prime from a large enough range rarely divides it.
The proof is a reduction from -SAT that encodes a formula into the digits of subset-sum instances. A corollary is a "direct-OR" theorem: deciding whether at least one of instances with target is a yes-instance takes time under SETH, so batching instances does not help. The same paper shows that a classic pseudo-polynomial algorithm for bicriteria shortest paths (Joksch, 1966) is essentially optimal.
Which of these algorithms for subset sum would refute SETH, according to this theorem?
- An algorithm
- An algorithm
- An algorithm
- An bitset algorithm
Show the answer
B. An algorithm
is , so has the forbidden form with . The other three are already known, or consistent with the bound.
When the numbers are huge
If the numbers have about bits, every pseudo-polynomial bound is exponential, and the question becomes how far below one can go.
Meet in the middle (Horowitz & Sahni, 1974): split the numbers into halves and list the subset sums of each half in sorted order (adding one number to a sorted list is a merge of and , so this costs in all). Then walk the two lists with two pointers, one ascending and one descending, looking for a pair that sums to . Time and space ; see Meet in the Middle.
Schroeppel and Shamir (1981) keep the time and cut the space. Split the numbers into quarters with sorted lists of sums each, generate the sums of in increasing order with a min-heap that holds one candidate per element of , and those of in decreasing order with a max-heap. The two-pointer walk then runs over these virtual lists: time and space.
| Setting | Best known | Source |
|---|---|---|
| Worst case, time | Horowitz & Sahni (1974) | |
| Worst case, space with time | , then (randomized) | Schroeppel & Shamir (1981); Nederlof & Węgrzycki (STOC 2021) |
| Random instances with (density about 1) | about , heuristic | Becker, Coron & Joux (2011), after Howgrave-Graham & Joux (2010) |
| Small | , randomized | Bringmann (2017); Jin & Wu (2019) |
Key takeaways
- The subset-sum table is a bitset: for each number, word operations; folding duplicates leaves numbers when the total is small.
- Subset sums of disjoint parts combine by sumsets, which the FFT computes in ; divide and conquer with caps gives , and Koiliaris and Xu reach deterministically.
- Randomized algorithms exist (Bringmann 2017; Jin & Wu 2019), and under SETH no algorithm runs in : near-linear in is the end of the road.
- With huge numbers, meet in the middle runs in and Schroeppel–Shamir cuts the space to ; beating in the worst case is open.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
The bitset table runs in word operations. Why is it still not a polynomial-time algorithm for subset sum?
- Because is a constant, and is exponential in the number of bits of
- Because shifting a bitset costs per bit
- Because the table needs rows of bits
- It is polynomial on the word RAM, where words have bits
Show the answer
A. Because is a constant, and is exponential in the number of bits of
Word parallelism divides the work by , typically 64 or about , which is far from enough: itself is exponential in its length, so is too. The bitset is a constant-factor (or logarithmic) speedup of a pseudo-polynomial algorithm.
What does the SETH-based lower bound of Abboud, Bringmann, Hermelin and Shabtay say about subset sum?
- No algorithm runs in time
- No algorithm runs in time
- The dependence on cannot be lowered to , even with any factor, so is optimal up to subpolynomial factors
- Subset sum needs time on every instance
Show the answer
C. The dependence on cannot be lowered to , even with any factor, so is optimal up to subpolynomial factors
Under SETH there is no algorithm for any . Bringmann's randomized algorithm is consistent with this, and matches it up to subpolynomial factors.
You must solve subset sum for numbers of 80 bits each, exactly. Which approach is feasible?
- The bitset table over sums up to
- Bringmann's algorithm
- Meet in the middle: about sums per half, then match them
- Brute force over all subsets
Show the answer
C. Meet in the middle: about sums per half, then match them
sums per half, generated in sorted order and matched with two pointers: a few hundred million simple steps. Every pseudo-polynomial method pays for .
Further reading
- K. Bringmann, "A near-linear pseudopolynomial time algorithm for subset sum," SODA 2017.
- K. Koiliaris & C. Xu, "Faster pseudopolynomial time algorithms for subset sum," ACM Transactions on Algorithms 15(3) (2019); SODA 2017.
- C. Jin & H. Wu, "A simple near-linear pseudopolynomial time randomized algorithm for subset sum," SOSA 2019.
- A. Abboud, K. Bringmann, D. Hermelin & D. Shabtay, "SETH-based lower bounds for subset sum and bicriteria path," ACM Transactions on Algorithms 18(1) (2022); SODA 2019.
- R. Schroeppel & A. Shamir, "A , algorithm for certain NP-complete problems," SIAM Journal on Computing 10(3) (1981), 456–464.