Given a (multi) set of positive integers and an integer target value , the SubsetSum problem is to decide if there is a (multi) subset of that sums up to . The SubsetSum is a classical problem with relatively long history. It is one of Karp’s original NP-complete problems , closely related to other fundamental NP-complete problems such as Knapsack , Constrained Shortest Path , and various other graph problems with cardinality constraints [9, 12, 16]. Furthermore, it is one of the initial weakly NP-complete problems; problems that admit pseudopolynomial time algorithms – a classification identified by Garey and Johnson in . The first such algorithm was given in 1957 111 Note that Bellman wrote this paper before the definition of pseudopolynomial time algorithms was provided by Garey and Johnson in 1977. by Bellman, who showed how to solve the problem in time using dynamic programming .
The importance of the SubsetSum problem in computer science is further highlighted by its role in teaching. Both the problem and its algorithm have been included in undergraduate algorithms courses’ curriculums and textbooks for several decades ([6, Chapter 34.5.5], used as archetypal examples for introducing the notions of weak NP-completeness and pseudopolynomial time algorithms to college students [15, Chapter 8.8]. In addition, the conceptually simple problem statement makes this problem a great candidate in the study of NP-completeness [8, Chapter 8.1]), and, finally, Bellman’s algorithm is also often introduced in the context of teaching dynamic programming [10, Chapter 5.6].
Extensive work has been done on finding better and faster pseudopolynomial time algorithms for the SubsetSum (for a collection of previous results see [17, Table 1.1]). The first improvement on the running time was a time algorithm by , almost two decades go. Recently, the state-of-the-art was improved significantly to time by the authors . Shortly after, in a follow up work, the running time was further improved to time by Bringmann  – the algorithm is randomized and somewhat involved. Abboud et al.  showed that it is unlikely that any SubsetSum algorithm runs in time , for any constant and target number , as such an algorithm would imply that the Strong Exponential Time Hypothesis (SETH) of Impagliazzo and Paturi  is false.
In this paper, we present a new simple algorithm for the SubsetSum
problem. The algorithm follows the divide-and-conquer paradigm and uses the Fast Fourier Transform (), matching the best deterministic running timeof  with a cleaner and more straightforward analysis. The algorithm partitions the input by congruence into classes, computes the subset sums of each class recursively, and combines the results. We believe this new simple algorithm, although not improving upon the state-of-the-art, reduces the conceptual complexity of the problem and improves our understanding of it. We believe the new algorithm can be used in teaching as an example of a pseudopolynomial time algorithm for the SubsetSum problem, as well as a striking example of applying to a seemingly unrelated problem.
Comparison to previous work
Our previous algorithm  used a more complicated divide-and-conquer strategy that resulted in forming sets of two different types, that had to be handled separately. Bringmann’s algorithm  uses randomization and a two-stage color-coding process. Both algorithms are significantly more complicated than the one presented here.
Let denote the set of integers in the interval . Given a set , let and denote the set of all subset sums of up to by
and the set of all subset sums of up to with cardinality information by
Let , be two sets, the set of pairwise sums of and up to is denoted by
If , are sets of points in the plane, then
Observe, that if and are two disjoint sets, then .
Next, we define two generalizations of the SubsetSum problem. Both can be solved by the new algorithm.
Note that the case where the input is a multiset can be reduced to the case of a set with little loss in generality and running time (see [17, Section 2.2]), hence for simplicity of exposition we assume the input is a set throughout the paper.
3 The algorithm
Here, we show how to solve AllSubsetSums in time. Clearly, computing all subset sums up to also decides SubsetSum with target value .
3.1 Building blocks
The following well-known lemma describes how to compute pairwise sums between sets in almost linear time, in the size of their ranges, using .
[Computing pairwise sums ] The following are true:
Given two sets , , one can compute in time.
Given sets , one can compute in time.
Given two sets of points , , one can compute in time.
(A) Let be the characteristic polynomial of . Construct, in a similar fashion, the polynomial (for the set ) and let . Observe that for , the coefficient of in is nonzero if and only if . Using , one can compute the polynomial in time, and extract from it.
(B) Let , and let , for . Compute each , from and , in time using part (A). The total running time is .
(C) As in (A), let and be the characteristic polynomials of and , respectively, and let . For the coefficient of is nonzero if and only if . One can compute the polynomial by a straightforward reduction to regular (see multidimensional [4, Chapter 12.8]), in time, and extract from it. ∎
The next lemma shows how to answer AllSubsetSums quickly, originally shown by the authors in , the proof is included for completeness.
[AllSubsetSums ] Let be a given set of elements. One can compute, in time, the set , which includes all subset sums of up to with cardinality information.
Partition into two sets and of roughly the same size. Compute and recursively, and observe that , . Finally, note that . Applying Lemma 3.1.C yields .
The running time follows the recursive formula , which is , proving the claim. ∎
Next, we show how to compute the subset sums of elements in a congruence class quickly.
Let , with . Given a set of size , one can compute in time.
An element can be written as . Let . As such, for any subset of size , we have that
In particular, a pair corresponds to a set of size , such that . The set in turn corresponds to the set . By the above, the sum of the elements of is . As such, compute , using the algorithm of Lemma 3.1, and return as the desired result. ∎
[AllSubsetSums] Let be a given set of elements. One can compute, in time, the set , which contains all subset sums of up to .
AllSubsetSums is a generalization of SubsetSum, so the algorithm of subsection 3.3 applies to it.
-  Amir Abboud, Karl Bringmann, Danny Hermelin, and Dvir Shabtay. SETH-based lower bounds for subset sum and bicriteria path. CoRR, abs/1704.04546, 2017. URL: http://arxiv.org/abs/1704.04546, arXiv:1704.04546.
-  Kyriakos Axiotis and Christos Tzamos. Capacitated dynamic programming: Faster knapsack and graph algorithms. CoRR, abs/1802.06440, 2018. URL: http://arxiv.org/abs/1802.06440, arXiv:1802.06440.
-  Richard Bellman. Notes on the theory of dynamic programming IV - maximization over discrete sets. Naval Research Logistics Quarterly, 3(1-2):67–70, 1956. URL: http://dx.doi.org/10.1002/nav.3800030107, doi:10.1002/nav.3800030107.
-  Richard E. Blahut. Fast Algorithms for Digital Signal Processing. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1st edition, 1985.
-  Karl Bringmann. A near-linear pseudopolynomial time algorithm for subset sum. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms, pages 1073–1084. Society for Industrial and Applied Mathematics, 2017.
-  Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Third Edition. The MIT Press, 3rd edition, 2009.
-  George B. Dantzig. Discrete-variable extremum problems. Operations Research, 5(2):266–277, 1957. URL: http://www.jstor.org/stable/167356.
-  Sanjoy Dasgupta, Christos H. Papadimitriou, and Umesh Vazirani. Algorithms. McGraw-Hill, Inc., New York, NY, USA, first edition, 2008.
-  David Eppstein. Minimum range balanced cuts via dynamic subset sums. Journal of Algorithms, 23(2):375 – 385, 1997. URL: http://www.sciencedirect.com/science/article/pii/S019667749690841X, doi:http://dx.doi.org/10.1006/jagm.1996.0841.
-  Jeff Erickson. Algorithms, etc., January 2015. Course materials, 1250 pages. URL: https://jeffe.cs.illinois.edu/teaching/algorithms/.
-  Michael R Garey and David S Johnson. “Strong” NP-Completeness Results: Motivation, Examples, and Implications. Journal of the ACM (JACM), 25(3):499–508, 1978.
-  Venkatesan Guruswami, Yury Makarychev, Prasad Raghavendra, David Steurer, and Yuan Zhou. Finding almost-perfect graph bisections. In Innovations in Computer Science - ICS 2010, Tsinghua University, Beijing, China, January 7-9, 2011. Proceedings, pages 321–337, 2011. URL: http://conference.itcs.tsinghua.edu.cn/ICS2011/content/papers/11.html.
-  Russell Impagliazzo and Ramamohan Paturi. On the complexity of k-sat. Journal of Computer and System Sciences, 62(2):367 – 375, 2001. URL: http://www.sciencedirect.com/science/article/pii/S0022000000917276, doi:https://doi.org/10.1006/jcss.2000.1727.
-  Richard M. Karp. Reducibility among combinatorial problems. In Raymond E. Miller, James W. Thatcher, and Jean D. Bohlinger, editors, Complexity of Computer Computations, The IBM Research Symposia Series, pages 85–103. Springer US, 1972. URL: http://dx.doi.org/10.1007/978-1-4684-2001-2_9, doi:10.1007/978-1-4684-2001-2_9.
-  Jon Kleinberg and Eva Tardos. Algorithm Design. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 2005.
-  Bettina Klinz and Gerhard J. Woeginger. A note on the bottleneck graph partition problem. Networks, 33(3):189–191, 1999. URL: http://dx.doi.org/10.1002/(SICI)1097-0037(199905)33:3<189::AID-NET5>3.0.CO;2-2, doi:10.1002/(SICI)1097-0037(199905)33:3<189::AID-NET5>3.0.CO;2-2.
-  Konstantinos Koiliaris and Chao Xu. A faster pseudopolynomial time algorithm for subset sum. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms, pages 1062–1072. SIAM, 2017.
-  David Pisinger. Linear time algorithms for knapsack problems with bounded weights. Journal of Algorithms, 33(1):1 – 14, 1999.