Computational hardness
The run-time complexity of SSP depends on two parameters: * - the number of input integers. If ''n'' is a small fixed number, then an exhaustive search for the solution is practical. * - the precision of the problem, stated as the number of binary place values that it takes to state the problem. If ''L'' is a small fixed number, then there are dynamic programming algorithms that can solve it exactly. As both ''n'' and ''L'' grow large, SSP is NP-hard. The complexity of the best known algorithms is exponential in the smaller of the two parameters ''n'' and ''L''. The problem is NP-hard even when all input integers are positive (and the target-sum ''T'' is a part of the input). This can be proved by a direct reduction fromExponential time algorithms
There are several ways to solve SSP in time exponential in ''n''.Inclusion–exclusion
The most naïve algorithm would be to cycle through all subsets of ''n'' numbers and, for every one of them, check if the subset sums to the right number. The running time is of order , since there are subsets and, to check each subset, we need to sum at most ''n'' elements. The algorithm can be implemented byHorowitz and Sahni
In 1974, Horowitz and Sahni published a faster exponential-time algorithm, which runs in time , but requires much more space - . The algorithm splits arbitrarily the ''n'' elements into two sets of each. For each of these two sets, it stores a list of the sums of all possible subsets of its elements. Each of these two lists is then sorted. Using even the fastest comparison sorting algorithm, Mergesort for this step would take time . However, given a sorted list of sums for elements, the list can be expanded to two sorted lists with the introduction of a ()th element, and these two sorted lists can be merged in time . Thus, each list can be generated in sorted form in time . Given the two sorted lists, the algorithm can check if an element of the first array and an element of the second array sum up to ''T'' in time . To do that, the algorithm passes through the first array in decreasing order (starting at the largest element) and the second array in increasing order (starting at the smallest element). Whenever the sum of the current element in the first array and the current element in the second array is more than ''T'', the algorithm moves to the next element in the first array. If it is less than ''T'', the algorithm moves to the next element in the second array. If two elements that sum to ''T'' are found, it stops. (The sub-problem for two elements sum is known as two-sum.)Schroeppel and Shamir
In 1981, Schroeppel and Shamir presented an algorithm based on Horowitz and Sanhi, that requires similar runtime - , much less space - . Rather than generating and storing all subsets of ''n''/2 elements in advance, they partition the elements into 4 sets of ''n''/4 elements each, and generate subsets of ''n''/2 element pairs dynamically using a min heap, which yields the above time and space complexities since this can be done in and space given 4 lists of length k. Due to space requirements, the HS algorithm is practical for up to about 50 integers, and the SS algorithm is practical for up to 100 integers.Howgrave-Graham and Joux
Howgrave-Graham and Joux presented a probabilistic algorithm that runs faster than all previous ones - in time using space . It solves only the decision problem, cannot prove there is no solution for a given sum, and does not return the subset sum closest to ''T''. The techniques of Howgrave-Graham and Joux were subsequently extended bringing the time-complexity to .Pseudo-polynomial time dynamic programming solutions
SSP can be solved in pseudo-polynomial time using dynamic programming. Suppose we have the following sequence of elements in an instance: : We define a ''state'' as a pair (''i'', ''s'') of integers. This state represents the fact that :"there is a nonempty subset of which sums to ." Each state (''i'', ''s'') has two next states: * (''i''+1, ''s''), implying that is not included in the subset; * (''i''+1, ''s''+), implying that is included in the subset. Starting from the initial state (0, 0), it is possible to use any graph search algorithm (e.g. BFS) to search the state (''N'', ''T''). If the state is found, then by backtracking we can find a subset with a sum of exactly ''T''. The run-time of this algorithm is at most linear in the number of states. The number of states is at most ''N'' times the number of different possible sums. Let be the sum of the negative values and the sum of the positive values; the number of different possible sums is at most ''B''-''A'', so the total runtime is in . For example, if all input values are positive and bounded by some constant ''C'', then ''B'' is at most ''N C'', so the time required is . This solution does not count as polynomial time in complexity theory because is not polynomial in the ''size'' of the problem, which is the number of bits used to represent it. This algorithm is polynomial in the values of and , which are exponential in their numbers of bits. However, Subset Sum encoded in ''unary'' is in P, since then the size of the encoding is linear in B-A. Hence, Subset Sum is only ''weakly'' NP-Complete. For the case that each is positive and bounded by a fixed constant , Pisinger found a linear time algorithm having time complexity (note that this is for the version of the problem where the target sum is not necessarily zero, otherwise the problem would be trivial). In 2015, Koiliaris and Xu found a deterministic algorithm for the subset sum problem where is the sum we need to find. In 2017, Bringmann found a randomized time algorithm. In 2014, Curtis and Sanches found a simple recursion highly scalable inPolynomial time approximation algorithms
Suppose all inputs are positive. AnSimple 1/2-approximation
The following very simple algorithm has an approximation ratio of 1/2: * Order the inputs by descending value; * Put the next-largest input into the subset, as long as it fits there. When this algorithm terminates, either all inputs are in the subset (which is obviously optimal), or there is an input that does not fit. The first such input is smaller than all previous inputs that are in the subset and the sum of inputs in the subset is more than ''T''/2 otherwise the input also is less than T/2 and it would fit in the set. Such a sum greater than T/2 is obviously more than OPT/2.Fully-polynomial time approximation scheme
The following algorithm attains, for every , an approximation ratio of . Its run time is polynomial in and . Recall that ''n'' is the number of inputs and ''T'' is the upper bound to the subset sum. initialize a list ''L'' to contain one element 0. for each ''i'' from 1 to ''n'' do let ''Ui'' be a list containing all elements ''y'' in ''L'', and all sums ''xi'' + ''y'' for all ''y'' in ''L''. sort ''Ui'' in ascending order make ''L'' empty let ''y'' be the smallest element of ''Ui'' add ''y'' to ''L'' for each element ''z'' of ''Ui'' in increasing order do // Trim the list by eliminating numbers close to one another // and throw out elements greater than the target sum ''T''. if ''y'' + ''ε T''/''n'' < ''z'' ≤ ''T'' then ''y'' = ''z'' add ''z'' to ''L'' return the largest element in ''L.'' Note that without the trimming step (the inner "for each" loop), the list ''L'' would contain the sums of all subsets of inputs. The trimming step does two things: * It ensures that all sums remaining in ''L'' are below ''T'', so they are feasible solutions to the subset-sum problem. * It ensures that the list L is "sparse", that is, the difference between each two consecutive partial-sums is at least . These properties together guarantee that the list contains no more than elements; therefore the run-time is polynomial in . When the algorithm ends, if the optimal sum is in , then it is returned and we are done. Otherwise, it must have been removed in a previous trimming step. Each trimming step introduces an additive error of at most , so steps together introduce an error of at most . Therefore, the returned solution is at least which is at least . The above algorithm provides an ''exact'' solution to SSP in the case that the input numbers are small (and non-negative). If any sum of the numbers can be specified with at most bits, then solving the problem approximately with is equivalent to solving it exactly. Then, the polynomial time algorithm for approximate subset sum becomes an exact algorithm with running time polynomial in and (i.e., exponential in ). Kellerer, Mansini, Pferschy and Speranza and Kellerer, Pferschy and Pisinger present other FPTAS-s for subset sum.See also
*References
Further reading
* * A3.2: SP13, pg.223. * * {{DEFAULTSORT:Subset Sum Problem Weakly NP-complete problems Dynamic programming Articles with example pseudocode