fbpx
Wikipedia

Subset sum problem

The subset sum problem (SSP) is a decision problem in computer science. In its most general formulation, there is a multiset of integers and a target-sum , and the question is to decide whether any subset of the integers sum to precisely .[1] The problem is known to be NP-hard. Moreover, some restricted variants of it are NP-complete too, for example:[1]

  • The variant in which all inputs are positive.
  • The variant in which inputs may be positive or negative, and . For example, given the set , the answer is yes because the subset sums to zero.
  • The variant in which all inputs are positive, and the target sum is exactly half the sum of all inputs, i.e., . This special case of SSP is known as the partition problem.

SSP can also be regarded as an optimization problem: find a subset whose sum is at most T, and subject to that, as close as possible to T. It is NP-hard, but there are several algorithms that can solve it reasonably quickly in practice.

SSP is a special case of the knapsack problem and of the multiple subset sum problem.

Computational hardness edit

The run-time complexity of SSP depends on two parameters:

  • n - the number of input integers. If n is a small fixed number, then an exhaustive search for the solution is practical.
  • L - 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 from 3SAT.[2] It can also be proved by reduction from 3-dimensional matching (3DM):[3]

  • We are given an instance of 3DM, where the vertex sets are W, X, Y. Each set has n vertices. There are m edges, where each edge contains exactly one vertex from each of W, X, Y. Denote L := ceiling(log2(m+1)), so that L is larger than the number of bits required to represent the number of edges.
  • We construct an instance of SSP with m positive integers. The integers are described by their binary representation. Each input integer can be represented by 3nL bits, divided into 3n zones of L bits. Each zone corresponds to a vertex.
  • For each edge (w,x,y) in the 3DM instance, there is an integer in the SSP instance, in which exactly three bits are "1": the least-significant bits in the zones of the vertices w, x, and y. For example, if n=10 and L=3, and W=(0,...,9), X=(10,...,19), Y=(20,...,29), then the edge (0, 10, 20) is represented by the number (20+230+260).
  • The target sum T in the SSP instance is set to an integer with "1" in the least-significant bit of every zone, that is, (20+21+...+23n-1).
  • If the 3DM instance has a perfect matching, then summing the corresponding integers in the SSP instance yields exactly T.
  • Conversely, if the SSP instance has a subset with sum exactly T, then, since the zones are sufficiently large so that there are no "carries" from one zone to the next, the sum must correspond to a perfect matching in the 3DM instance.

The following variants are also known to be NP-hard:

  • The input integers can be both positive and negative, and the target-sum T = 0. This can be proved by reduction from the variant with positive integers. Denote that variant by SubsetSumPositive and the current variant by SubsetSumZero. Given an instance (S, T) of SubsetSumPositive, construct an instance of SubsetSumZero by adding a single element with value −T. Given a solution to the SubsetSumPositive instance, adding the −T yields a solution to the SubsetSumZero instance. Conversely, given a solution to the SubsetSumZero instance, it must contain the −T (since all integers in S are positive), so to get a sum of zero, it must also contain a subset of S with a sum of +T, which is a solution of the SubsetSumPositive instance.
  • The input integers are positive, and T = sum(S)/2. This can also be proved by reduction from the general variant; see partition problem.

The analogous counting problem #SSP, which asks to enumerate the number of subsets summing to the target, is #P-complete.[4]

Exponential time algorithms edit

There are several ways to solve SSP in time exponential in n.[5]

Inclusion–exclusion edit

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 by depth-first search of a binary tree: each level in the tree corresponds to an input number; the left branch corresponds to excluding the number from the set, and the right branch corresponds to including the number (hence the name Inclusion-Exclusion). The memory required is  . The run-time can be improved by several heuristics:[5]

  • Process the input numbers in descending order.
  • If the integers included in a given node exceed the sum of the best subset found so far, the node is pruned.
  • If the integers included in a given node, plus all remaining integers, are less than the sum of the best subset found so far, the node is pruned.

Horowitz and Sahni edit

In 1974, Horowitz and Sahni[6] 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.[7])

Schroeppel and Shamir edit

In 1981, Schroeppel and Shamir presented an algorithm[8] 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.[5]

Howgrave-Graham and Joux edit

In 2010, Howgrave-Graham and Joux[9] 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 [10] bringing the time-complexity to  .

Pseudo-polynomial time dynamic programming solutions edit

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 s."

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 A be the sum of the negative values and B 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 A and B, 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 C, in 1999, 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, as otherwise the problem would be trivial).[11] In 2015, Koiliaris and Xu found a deterministic   algorithm for the subset sum problem where T is the sum we need to find.[12] In 2017, Bringmann found a randomized   time algorithm.[13]

In 2014, Curtis and Sanches found a simple recursion highly scalable in SIMD machines having   time and   space, where p is the number of processing elements,   and   is the lowest integer.[14] This is the best theoretical parallel complexity known so far.

A comparison of practical results and the solution of hard instances of the SSP is discussed by Curtis and Sanches.[15]

Polynomial time approximation algorithms edit

Suppose all inputs are positive. An approximation algorithm to SSP aims to find a subset of S with a sum of at most T and at least r times the optimal sum, where r is a number in (0,1) called the approximation ratio.

Simple 1/2-approximation edit

The following very simple algorithm has an approximation ratio of 1/2:[16]

  • 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 edit

The following algorithm attains, for every  , an approximation ratio of  . Its run time is polynomial in n 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 < zT 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 L contains no more than   elements; therefore the run-time is polynomial in  .

When the algorithm ends, if the optimal sum is in L, 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 n 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 P 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 n and   (i.e., exponential in P).

Kellerer, Mansini, Pferschy and Speranza[17] and Kellerer, Pferschy and Pisinger[18] present other FPTAS-s for subset sum.

See also edit

  • Knapsack problem – Problem in combinatorial optimization - a generalization of SSP in which each input item has both a value and a weight. The goal is to maximize the value subject to a bound on the total weight.
  • Multiple subset sum problem - a generalization of SSP in which one should choose several subsets.
  • 3SUM – Problem in computational complexity theory
  • Merkle–Hellman knapsack cryptosystem – one of the earliest public key cryptosystems invented by Ralph Merkle and Martin Hellman in 1978. The ideas behind it are simpler than those involving RSA, and it has been broken

References edit

  1. ^ a b Kleinberg, Jon; Tardos, Éva (2006). Algorithm Design (2nd ed.). p. 491. ISBN 0-321-37291-3.
  2. ^ Goodrich, Michael. "More NP complete and NP hard problems" (PDF). Archived (PDF) from the original on 2022-10-09.
  3. ^ Garey, Michael R.; Johnson, David S. (1979). Computers and Intractability: A Guide to the Theory of NP-Completeness. Series of Books in the Mathematical Sciences (1st ed.). New York: W. H. Freeman and Company. ISBN 9780716710455. MR 0519066. OCLC 247570676., Section 3.1 and problem SP1 in Appendix A.3.1.
  4. ^ Filmus, Yuval (30 January 2016). Answer to: "Is there a known, fast algorithm for counting all subsets that sum to below a certain number?". Theoretical Computer Science Stack Exchange. Note that Filmus' citation in support of the claim (Faliszewski, Piotr; Hemaspaandra, Lane (2009). "The complexity of power-index comparison". Theoretical Computer Science. Elsevier. 410: 101-107. DOI 10.1016/j.tcs.2008.09.034) does not in fact prove the claim, instead directing readers to another citation (Papadimitriou, Christos (1994). Computational Complexity. Addison-Wesley: Reading, MA. Chapter 9. ISBN 0-201-53082-1 — via the Internet Archive), which does not explicitly prove the claim either. Papadimitriou's proof that SSP is NP-complete via reduction of 3SAT does, however, generalize to a reduction from #3SAT to #SSP.
  5. ^ a b c Richard E. Korf, Ethan L. Schreiber, and Michael D. Moffitt (2014). "Optimal Sequential Multi-Way Number Partitioning" (PDF). Archived (PDF) from the original on 2022-10-09.{{cite web}}: CS1 maint: multiple names: authors list (link)
  6. ^ Horowitz, Ellis; Sahni, Sartaj (1974). "Computing partitions with applications to the knapsack problem" (PDF). Journal of the Association for Computing Machinery. 21 (2): 277–292. doi:10.1145/321812.321823. hdl:1813/5989. MR 0354006. S2CID 16866858. Archived (PDF) from the original on 2022-10-09.
  7. ^ "The Two-Sum Problem" (PDF). Archived (PDF) from the original on 2022-10-09.
  8. ^ Schroeppel, Richard; Shamir, Adi (1981-08-01). "A T = O(2n/2), S = O(2n/4) algorithm for certain NP-complete problems". SIAM Journal on Computing. 10 (3): 456–464. doi:10.1137/0210033. ISSN 0097-5397.
  9. ^ Howgrave-Graham, Nick; Joux, Antoine (2010). "New Generic Algorithms for Hard Knapsacks". In Gilbert, Henri (ed.). Advances in Cryptology – EUROCRYPT 2010. Lecture Notes in Computer Science. Vol. 6110. Berlin, Heidelberg: Springer. pp. 235–256. doi:10.1007/978-3-642-13190-5_12. ISBN 978-3-642-13190-5.
  10. ^ Becker, Anja; Joux, Antoine (2010). "Improved Generic Algorithms for Hard Knapsacks". In Gilbert, Henri (ed.). Advances in Cryptology – EUROCRYPT 2011. Lecture Notes in Computer Science. Vol. 6632. Berlin, Heidelberg: Springer. pp. 364–385. doi:10.1007/978-3-642-20465-4_21. ISBN 978-3-642-20465-4.
  11. ^ Pisinger, David (1999). "Linear time algorithms for knapsack problems with bounded weights". Journal of Algorithms. 33 (1): 1–14. doi:10.1006/jagm.1999.1034. MR 1712690.
  12. ^ Koiliaris, Konstantinos; Xu, Chao (2015-07-08). "A Faster Pseudopolynomial Time Algorithm for Subset Sum". arXiv:1507.02318 [cs.DS].
  13. ^ Bringmann, Karl (2017). "A near-linear pseudopolynomial time algorithm for subset sum". In Klein, Philip N. (ed.). Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA 2017). SIAM. pp. 1073–1084. arXiv:1610.04712. doi:10.1137/1.9781611974782.69.
  14. ^ Curtis, V. V.; Sanches, C. A. A. (January 2016). "An efficient solution to the subset-sum problem on GPU: An efficient solution to the subset-sum problem on GPU". Concurrency and Computation: Practice and Experience. 28 (1): 95–113. doi:10.1002/cpe.3636. S2CID 20927927.
  15. ^ Curtis, V. V.; Sanches, C. A. A. (July 2017). "A low-space algorithm for the subset-sum problem on GPU". Computers & Operations Research. 83: 120–124. doi:10.1016/j.cor.2017.02.006.
  16. ^ Caprara, Alberto; Kellerer, Hans; Pferschy, Ulrich (2000-02-01). "The Multiple Subset Sum Problem". SIAM Journal on Optimization. 11 (2): 308–319. doi:10.1137/S1052623498348481. ISSN 1052-6234.
  17. ^ Kellerer, Hans; Mansini, Renata; Pferschy, Ulrich; Speranza, Maria Grazia (2003-03-01). "An efficient fully polynomial approximation scheme for the Subset-Sum Problem". Journal of Computer and System Sciences. 66 (2): 349–370. doi:10.1016/S0022-0000(03)00006-0. ISSN 0022-0000.
  18. ^ Hans Kellerer; Ulrich Pferschy; David Pisinger (2004). Knapsack problems. Springer. p. 97. ISBN 9783540402862.

Further reading edit

subset, problem, subset, problem, decision, problem, computer, science, most, general, formulation, there, multiset, displaystyle, integers, target, displaystyle, question, decide, whether, subset, integers, precisely, displaystyle, problem, known, hard, moreo. The subset sum problem SSP is a decision problem in computer science In its most general formulation there is a multiset S displaystyle S of integers and a target sum T displaystyle T and the question is to decide whether any subset of the integers sum to precisely T displaystyle T 1 The problem is known to be NP hard Moreover some restricted variants of it are NP complete too for example 1 The variant in which all inputs are positive The variant in which inputs may be positive or negative and T 0 displaystyle T 0 For example given the set 7 3 2 9000 5 8 displaystyle 7 3 2 9000 5 8 the answer is yes because the subset 3 2 5 displaystyle 3 2 5 sums to zero The variant in which all inputs are positive and the target sum is exactly half the sum of all inputs i e T 1 2 a 1 a n displaystyle T frac 1 2 a 1 dots a n This special case of SSP is known as the partition problem SSP can also be regarded as an optimization problem find a subset whose sum is at most T and subject to that as close as possible to T It is NP hard but there are several algorithms that can solve it reasonably quickly in practice SSP is a special case of the knapsack problem and of the multiple subset sum problem Contents 1 Computational hardness 2 Exponential time algorithms 2 1 Inclusion exclusion 2 2 Horowitz and Sahni 2 3 Schroeppel and Shamir 2 4 Howgrave Graham and Joux 3 Pseudo polynomial time dynamic programming solutions 4 Polynomial time approximation algorithms 4 1 Simple 1 2 approximation 4 2 Fully polynomial time approximation scheme 5 See also 6 References 7 Further readingComputational hardness editThe run time complexity of SSP depends on two parameters n the number of input integers If n is a small fixed number then an exhaustive search for the solution is practical L 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 from 3SAT 2 It can also be proved by reduction from 3 dimensional matching 3DM 3 We are given an instance of 3DM where the vertex sets are W X Y Each set has n vertices There are m edges where each edge contains exactly one vertex from each of W X Y Denote L ceiling log2 m 1 so that L is larger than the number of bits required to represent the number of edges We construct an instance of SSP with m positive integers The integers are described by their binary representation Each input integer can be represented by 3nL bits divided into 3n zones of L bits Each zone corresponds to a vertex For each edge w x y in the 3DM instance there is an integer in the SSP instance in which exactly three bits are 1 the least significant bits in the zones of the vertices w x and y For example if n 10 and L 3 and W 0 9 X 10 19 Y 20 29 then the edge 0 10 20 is represented by the number 20 230 260 The target sum T in the SSP instance is set to an integer with 1 in the least significant bit of every zone that is 20 21 23n 1 If the 3DM instance has a perfect matching then summing the corresponding integers in the SSP instance yields exactly T Conversely if the SSP instance has a subset with sum exactly T then since the zones are sufficiently large so that there are no carries from one zone to the next the sum must correspond to a perfect matching in the 3DM instance The following variants are also known to be NP hard The input integers can be both positive and negative and the target sum T 0 This can be proved by reduction from the variant with positive integers Denote that variant by SubsetSumPositive and the current variant by SubsetSumZero Given an instance S T of SubsetSumPositive construct an instance of SubsetSumZero by adding a single element with value T Given a solution to the SubsetSumPositive instance adding the T yields a solution to the SubsetSumZero instance Conversely given a solution to the SubsetSumZero instance it must contain the T since all integers in S are positive so to get a sum of zero it must also contain a subset of S with a sum of T which is a solution of the SubsetSumPositive instance The input integers are positive and T sum S 2 This can also be proved by reduction from the general variant see partition problem The analogous counting problem SSP which asks to enumerate the number of subsets summing to the target is P complete 4 Exponential time algorithms editThere are several ways to solve SSP in time exponential in n 5 Inclusion exclusion edit The most naive 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 O 2 n n displaystyle O 2 n cdot n nbsp since there are 2 n displaystyle 2 n nbsp subsets and to check each subset we need to sum at most n elements The algorithm can be implemented by depth first search of a binary tree each level in the tree corresponds to an input number the left branch corresponds to excluding the number from the set and the right branch corresponds to including the number hence the name Inclusion Exclusion The memory required is O n displaystyle O n nbsp The run time can be improved by several heuristics 5 Process the input numbers in descending order If the integers included in a given node exceed the sum of the best subset found so far the node is pruned If the integers included in a given node plus all remaining integers are less than the sum of the best subset found so far the node is pruned Horowitz and Sahni edit In 1974 Horowitz and Sahni 6 published a faster exponential time algorithm which runs in time O 2 n 2 n 2 displaystyle O 2 n 2 cdot n 2 nbsp but requires much more space O 2 n 2 displaystyle O 2 n 2 nbsp The algorithm splits arbitrarily the n elements into two sets of n 2 displaystyle n 2 nbsp each For each of these two sets it stores a list of the sums of all 2 n 2 displaystyle 2 n 2 nbsp 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 O 2 n 2 n displaystyle O 2 n 2 n nbsp However given a sorted list of sums for k displaystyle k nbsp elements the list can be expanded to two sorted lists with the introduction of a k 1 displaystyle k 1 nbsp th element and these two sorted lists can be merged in time O 2 k displaystyle O 2 k nbsp Thus each list can be generated in sorted form in time O 2 n 2 displaystyle O 2 n 2 nbsp 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 O 2 n 2 displaystyle O 2 n 2 nbsp 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 7 Schroeppel and Shamir edit In 1981 Schroeppel and Shamir presented an algorithm 8 based on Horowitz and Sanhi that requires similar runtime O 2 n 2 n 4 displaystyle O 2 n 2 cdot n 4 nbsp much less space O 2 n 4 displaystyle O 2 n 4 nbsp 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 O k 2 log k displaystyle O k 2 log k nbsp and space O k displaystyle O k nbsp 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 5 Howgrave Graham and Joux edit In 2010 Howgrave Graham and Joux 9 presented a probabilistic algorithm that runs faster than all previous ones in time O 2 0 337 n displaystyle O 2 0 337n nbsp using space O 2 0 256 n displaystyle O 2 0 256n nbsp 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 10 bringing the time complexity to O 2 0 291 n displaystyle O 2 0 291n nbsp Pseudo polynomial time dynamic programming solutions editSSP can be solved in pseudo polynomial time using dynamic programming Suppose we have the following sequence of elements in an instance x 1 x N displaystyle x 1 ldots x N nbsp We define a state as a pair i s of integers This state represents the fact that there is a nonempty subset of x 1 x i displaystyle x 1 ldots x i nbsp which sums to s Each state i s has two next states i 1 s implying that x i 1 displaystyle x i 1 nbsp is not included in the subset i 1 s x i 1 displaystyle x i 1 nbsp implying that x i 1 displaystyle x i 1 nbsp 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 A be the sum of the negative values and B the sum of the positive values the number of different possible sums is at most B A so the total runtime is in O N B A displaystyle O N B A nbsp 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 O N 2 C displaystyle O N 2 C nbsp This solution does not count as polynomial time in complexity theory because B A displaystyle B A nbsp 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 A and B 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 x i displaystyle x i nbsp is positive and bounded by a fixed constant C in 1999 Pisinger found a linear time algorithm having time complexity O N C displaystyle O NC nbsp note that this is for the version of the problem where the target sum is not necessarily zero as otherwise the problem would be trivial 11 In 2015 Koiliaris and Xu found a deterministic O T N displaystyle tilde O T sqrt N nbsp algorithm for the subset sum problem where T is the sum we need to find 12 In 2017 Bringmann found a randomized O T N displaystyle tilde O T N nbsp time algorithm 13 In 2014 Curtis and Sanches found a simple recursion highly scalable in SIMD machines having O N m x min p displaystyle O N m x min p nbsp time and O N m x min displaystyle O N m x min nbsp space where p is the number of processing elements m min s x i s displaystyle m min s sum x i s nbsp and x min displaystyle x min nbsp is the lowest integer 14 This is the best theoretical parallel complexity known so far A comparison of practical results and the solution of hard instances of the SSP is discussed by Curtis and Sanches 15 Polynomial time approximation algorithms editThis section needs additional citations for verification Please help improve this article by adding citations to reliable sources in this section Unsourced material may be challenged and removed February 2021 Learn how and when to remove this message Suppose all inputs are positive An approximation algorithm to SSP aims to find a subset of S with a sum of at most T and at least r times the optimal sum where r is a number in 0 1 called the approximation ratio Simple 1 2 approximation edit The following very simple algorithm has an approximation ratio of 1 2 16 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 edit The following algorithm attains for every ϵ gt 0 displaystyle epsilon gt 0 nbsp an approximation ratio of 1 ϵ displaystyle 1 epsilon nbsp Its run time is polynomial in n and 1 ϵ displaystyle 1 epsilon nbsp 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 e T n lt 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 2 n displaystyle 2 n nbsp 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 ϵ T n displaystyle epsilon T n nbsp These properties together guarantee that the list L contains no more than n ϵ displaystyle n epsilon nbsp elements therefore the run time is polynomial in n ϵ displaystyle n epsilon nbsp When the algorithm ends if the optimal sum is in L 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 ϵ T n displaystyle epsilon T n nbsp so n steps together introduce an error of at most ϵ T displaystyle epsilon T nbsp Therefore the returned solution is at least OPT ϵ T displaystyle text OPT epsilon T nbsp which is at least 1 ϵ OPT displaystyle 1 epsilon text OPT nbsp 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 P bits then solving the problem approximately with ϵ 2 P displaystyle epsilon 2 P nbsp is equivalent to solving it exactly Then the polynomial time algorithm for approximate subset sum becomes an exact algorithm with running time polynomial in n and 2 P displaystyle 2 P nbsp i e exponential in P Kellerer Mansini Pferschy and Speranza 17 and Kellerer Pferschy and Pisinger 18 present other FPTAS s for subset sum See also editKnapsack problem Problem in combinatorial optimization a generalization of SSP in which each input item has both a value and a weight The goal is to maximize the value subject to a bound on the total weight Multiple subset sum problem a generalization of SSP in which one should choose several subsets 3SUM Problem in computational complexity theory Merkle Hellman knapsack cryptosystem one of the earliest public key cryptosystems invented by Ralph Merkle and Martin Hellman in 1978 The ideas behind it are simpler than those involving RSA and it has been brokenPages displaying wikidata descriptions as a fallbackReferences edit a b Kleinberg Jon Tardos Eva 2006 Algorithm Design 2nd ed p 491 ISBN 0 321 37291 3 Goodrich Michael More NP complete and NP hard problems PDF Archived PDF from the original on 2022 10 09 Garey Michael R Johnson David S 1979 Computers and Intractability A Guide to the Theory of NP Completeness Series of Books in the Mathematical Sciences 1st ed New York W H Freeman and Company ISBN 9780716710455 MR 0519066 OCLC 247570676 Section 3 1 and problem SP1 in Appendix A 3 1 Filmus Yuval 30 January 2016 Answer to Is there a known fast algorithm for counting all subsets that sum to below a certain number Theoretical Computer Science Stack Exchange Note that Filmus citation in support of the claim Faliszewski Piotr Hemaspaandra Lane 2009 The complexity of power index comparison Theoretical Computer Science Elsevier 410 101 107 DOI 10 1016 j tcs 2008 09 034 does not in fact prove the claim instead directing readers to another citation Papadimitriou Christos 1994 Computational Complexity Addison Wesley Reading MA Chapter 9 ISBN 0 201 53082 1 via the Internet Archive which does not explicitly prove the claim either Papadimitriou s proof that SSP is NP complete via reduction of 3SAT does however generalize to a reduction from 3SAT to SSP a b c Richard E Korf Ethan L Schreiber and Michael D Moffitt 2014 Optimal Sequential Multi Way Number Partitioning PDF Archived PDF from the original on 2022 10 09 a href Template Cite web html title Template Cite web cite web a CS1 maint multiple names authors list link Horowitz Ellis Sahni Sartaj 1974 Computing partitions with applications to the knapsack problem PDF Journal of the Association for Computing Machinery 21 2 277 292 doi 10 1145 321812 321823 hdl 1813 5989 MR 0354006 S2CID 16866858 Archived PDF from the original on 2022 10 09 The Two Sum Problem PDF Archived PDF from the original on 2022 10 09 Schroeppel Richard Shamir Adi 1981 08 01 A T O 2n 2 S O 2n 4 algorithm for certain NP complete problems SIAM Journal on Computing 10 3 456 464 doi 10 1137 0210033 ISSN 0097 5397 Howgrave Graham Nick Joux Antoine 2010 New Generic Algorithms for Hard Knapsacks In Gilbert Henri ed Advances in Cryptology EUROCRYPT 2010 Lecture Notes in Computer Science Vol 6110 Berlin Heidelberg Springer pp 235 256 doi 10 1007 978 3 642 13190 5 12 ISBN 978 3 642 13190 5 Becker Anja Joux Antoine 2010 Improved Generic Algorithms for Hard Knapsacks In Gilbert Henri ed Advances in Cryptology EUROCRYPT 2011 Lecture Notes in Computer Science Vol 6632 Berlin Heidelberg Springer pp 364 385 doi 10 1007 978 3 642 20465 4 21 ISBN 978 3 642 20465 4 Pisinger David 1999 Linear time algorithms for knapsack problems with bounded weights Journal of Algorithms 33 1 1 14 doi 10 1006 jagm 1999 1034 MR 1712690 Koiliaris Konstantinos Xu Chao 2015 07 08 A Faster Pseudopolynomial Time Algorithm for Subset Sum arXiv 1507 02318 cs DS Bringmann Karl 2017 A near linear pseudopolynomial time algorithm for subset sum In Klein Philip N ed Proceedings of the Twenty Eighth Annual ACM SIAM Symposium on Discrete Algorithms SODA 2017 SIAM pp 1073 1084 arXiv 1610 04712 doi 10 1137 1 9781611974782 69 Curtis V V Sanches C A A January 2016 An efficient solution to the subset sum problem on GPU An efficient solution to the subset sum problem on GPU Concurrency and Computation Practice and Experience 28 1 95 113 doi 10 1002 cpe 3636 S2CID 20927927 Curtis V V Sanches C A A July 2017 A low space algorithm for the subset sum problem on GPU Computers amp Operations Research 83 120 124 doi 10 1016 j cor 2017 02 006 Caprara Alberto Kellerer Hans Pferschy Ulrich 2000 02 01 The Multiple Subset Sum Problem SIAM Journal on Optimization 11 2 308 319 doi 10 1137 S1052623498348481 ISSN 1052 6234 Kellerer Hans Mansini Renata Pferschy Ulrich Speranza Maria Grazia 2003 03 01 An efficient fully polynomial approximation scheme for the Subset Sum Problem Journal of Computer and System Sciences 66 2 349 370 doi 10 1016 S0022 0000 03 00006 0 ISSN 0022 0000 Hans Kellerer Ulrich Pferschy David Pisinger 2004 Knapsack problems Springer p 97 ISBN 9783540402862 Further reading editCormen Thomas H Leiserson Charles E Rivest Ronald L Stein Clifford 2001 1990 35 5 The subset sum problem Introduction to Algorithms 2nd ed MIT Press and McGraw Hill ISBN 0 262 03293 7 Michael R Garey and David S Johnson 1979 Computers and Intractability A Guide to the Theory of NP Completeness W H Freeman ISBN 0 7167 1045 5 A3 2 SP13 pg 223 Lagarias J C Odlyzko A M 1985 01 01 Solving low density subset sum problems Journal of the ACM 32 1 229 246 doi 10 1145 2455 2461 ISSN 0004 5411 S2CID 885632 Martello Silvano Toth Paolo 1990 4 Subset sum problem Knapsack problems Algorithms and computer interpretations Wiley Interscience pp 105 136 ISBN 0 471 92420 2 MR 1086874 Retrieved from https en wikipedia org w index php title Subset sum problem amp oldid 1217852471, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.