# Subset sum problem

﻿
Subset sum problem

In computer science, the subset sum problem is an important problem in complexity theory and cryptography. The problem is this: given a set of integers, does the sum of some non-empty subset equal exactly zero? For example, given the set { −7, −3, −2, 5, 8}, the answer is YES because the subset { −3, −2, 5} sums to zero. The problem is NP-Complete.

An equivalent problem is this: given a set of integers and an integer "s", does any non-empty subset sum to "s"? Subset sum can also be thought of as a special case of the knapsack problem. One interesting special case of subset sum is the partition problem, in which "s" is half of the sum of all elements in the set.

General discussion

The subset sum problem is a good introduction to the NP-complete class of problems. There are two reasons for this

* It is a decision and not an optimization problem
* It has a very simple formal definition and problem statement.

A solution that has a ± 1% precision is good enough for many physical problems. Being asked to solve a subset sum problem for 100-digit numbers with a precision of ±10−100 might seem silly and irrelevant. There are reasons why this is not the case.

First, the number of place values in the problem is essentially equivalent to the number of simultaneous constraints that need to be solved. A numerical precision of 1% means solving the problem to just the first 7 base two place values (any numerical error after that is less than 1/128 of the first digit). However, if there are 100 base 2 place values in the problem, solving just 7 of them amounts to solving only 7% of the constraints. Moreover, given that the volume of the solution space in this case would be 2100, and you have only covered a volume of 27, then there is still a solution space of 293 left uncovered. In this way a solution with a 1% numerical precision has covered essentially none of the real problem. The only way that a solution to the Subset Sum Problem can be used as a solution to other NP problems is to solve all of the problem (and all of the constraints) exactly.

Second, in at least one context, it is actually important to solve real subset sum problems exactly. In cryptography, Subset Sum problem comes up when a codebreaker attempts, given a message and ciphertext, to deduce the secret key. A key that is not equal to but within ± 1% of the real key is essentially useless for the codebreaker.

Lastly, from a purely theoretical point of view it the exact problem and its solution are of interest.

Although the subset sum problem is a decision problem, the cases when an approximate solution is sufficient have also been studied, in the field of approximations algorithms. One algorithm for the approximate version of the subset sum problem is given below.

The complexity of subset sum

The complexity (difficulty of solution) of subset sum can be viewed as depending on two parameters, "N", the number of decision variables, and "P", the precision of the problem (stated as the number of binary place values that it takes to state the problem). (Note: here the letters "N" and "P" mean something different than what they mean in the "NP" class of problems.)

The complexity of the best known algorithms is exponential in the smaller of the two parameters "N" and "P". Thus, the problem is most difficult if "N" and "P" are of the same order. It only becomes easy if either "N" or "P" becomes very small.

If "N" (the number of variables) is small, then an exhaustive search for the solution is practical. If "P" (the number of place values) is a small fixed number, then there are dynamic programming algorithms that can solve it exactly.

What is happening is that the problem becomes seemingly non-exponential when it is practical to count the entire solution space. There are two ways to count the solution space in the subset sum problem. One is to count the number of ways the variables can be combined. There are 2N possible ways to combine the variables. However, with N = 10, there are only 1024 possible combinations to check. These can be counted easily with a branching search. The other way is to count all possible numerical values that the combinations can take. There are 2P possible numerical sums. However, with P = 5 there are only 32 possible numerical values that the combinations can take. These can be counted easily with a dynamic programming algorithm. When N = P and both are large, then there is no aspect of the solution space that can be counted easily.

Efficient algorithms for both small "N" and small "P" cases are given below.

Exponential time algorithm

There are several ways to solve subset sum in time exponential in N. 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 "O(2NN)", since there are "2N" subsets and, to check each subset, we need to sum at most "N" elements.

A better exponential time algorithm is known, which runs in time "O(2N/2N)". The algorithm splits the "N" elements into two sets of "N/2" each. For each of these two sets, it calculates sums of all "2N/2" possible subsets of its elements and stores them in an array of length "2N/2". It then sorts each of these two arrays, which can be done in time "O(2N/2N)". When arrays are sorted, the algorithm can check if an element of the first array and an element of the second array sum up to "s" in time "O(2N/2)". 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 "s", the algorithm moves to the next element in the first array. If it is less than "s", the algorithm moves to the next element in the second array. If two elements with sum "s" are found, it stops.

No better algorithm has been found since Horowitz and Sahni first published this algorithm in 1974, suggesting that this improved algorithm may have the best running-time possible of all algorithms which solve the subset sum problem. If true, this would imply that $P e NP$, which is one of the most famous long standing unsolved problems (see P = NP problem).

Pseudo-polynomial time dynamic programming solution

The problem can be solved as follows using dynamic programming. Suppose the sequence is

:"x1", ..., "xn"

and we wish to find a nonempty subset which sums to zero.

Let "N" be the sum of the negative values and "P" the sum of the positive values. Define the function "Q(i,s) to be 0 if there is no subset of "x1", ..., "xi" which sums to "s"; 1 if there is a nonempty such subset; or 2 if only empty subset sums to "s" (i.e. when "s" is zero).

(Thus, the question we really want to know is whether "Q"(n,0) equals 1.)

Clearly:"Q(i,s) = 0 for "s"<"N" or "s">"P".

Create an array to hold the values "Q(i,s)" for 1&le;"i"&le;"n" and "N"&le;"s"&le;"P". The array can now be filled in using a simple recursion.

Initialize all "Q(1,s)" to 0. Let "Q(1,0)" be 2. Let "Q(1, x1)" be 1. For "i">1, if "Q(i-1,s-xi)" is nonzero, let "Q(i,s)" be 1 otherwise let it be value of "Q(i-1,s)".

(Note that "Q(i,s)" can be made a boolean valued function if we are interested in subset which sums to something other than zero.)

The total number of arithmetic operations is

:"O"("n"("P" − "N")).

For example, if all the values are

:"O"("nk")

for some "k", then the time required is

:"O"("nk+1").

This solution does not count as polynomial time in complexity theory because "P-N" 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 value of N and P, which are exponential in their numbers of bits.

Polynomial time approximate algorithm

An approximate version of the subset sum would be: given a set of "N" numbers "x1", "x2", ..., "xN" and a number "s", output
* yes, if there is a subset that sums up to "s";
* no, if there is no subset summing up to a number between "(1-c)s" and "s" for some small "c>0";
* any answer, if there is a subset summing up to a number between "(1-c)s" and "s" but no subset summing up to "s".If all numbers are non-negative, the approximate subset sum is solvable in time polynomial in "N" and "1/c".

The solution for subset sum also provides the solution for the original subset sum problem in the case where the numbers are small (again, for nonnegative numbers). If any sum of the numbers can be specified with at most "P" bits, then solving the problem approximately with "c=2-P" 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 "2P" (i.e., exponential in "P").

The algorithm for the approximate subset sum problem is as follows: initialize a list "S" to contain one element 0. for each "i" from 1 to "N" do let "T" be a list consisting of "xi+y", for all "y" in "S" let "U" be the union of "T" and "S" sort "U" make "S" empty let "y" be the smallest element of "U" add "y" to "S" for each element "z" of "U" in increasing order do //trim the list by eliminating numbers close one to another if "y<(1-c/N)z", set "y=z" and add "z" to "S" if "S" contains a number between "(1-c)s" and "s", output "yes", otherwise "no"The algorithm is polynomial time because the lists "S", "T" and "U" always remain of size polynomial in "N" and "1/c" and, as long as they are of polynomial size, all operations on them can be done in polynomial time. The size of lists is kept polynomial by the trimming step, in which we only include a number "z" into "S" if the previous "y" is at most

:(1 − "c"/"N")"z".

This step ensures that each element in "S" is smaller than the next one by at least a factor of (1 − "c"/"N") and any list with that property is of at most polynomial size.

The algorithm is correct because each step introduces a multiplicative error of at most (1 −"c"/"N") and "N" steps together introduce the error of at most

:(1 − "c"/"N")"N" < 1 − "c".

References

*
* A3.2: SP13, pg.223.

* [http://www.geocities.com/SiliconValley/Garage/3323/aat/a_diop.html#diophant An algorithm (exponential time) for solving the subset sum problem]

Wikimedia Foundation. 2010.

### Look at other dictionaries:

• Subset-Sum — Die Untermengensumme (engl. Subset Sum) ist ein berühmtes Problem der Informatik und des Operations Research. Es ist ein spezielles Rucksackproblem. Problembeschreibung Gegeben sei eine Menge von ganzen Zahlen I = {w1,w2,...,wn}. Gesucht ist eine …   Deutsch Wikipedia

• Subset Sum — Die Untermengensumme (engl. Subset Sum) ist ein berühmtes Problem der Informatik und des Operations Research. Es ist ein spezielles Rucksackproblem. Problembeschreibung Gegeben sei eine Menge von ganzen Zahlen I = {w1,w2,...,wn}. Gesucht ist eine …   Deutsch Wikipedia

• Zero-sum problem — In number theory, zero sum problems are a certain class of combinatorial questions. In general, a finite abelian group G is considered. The zero sum problem for the integer n is the following: Find the smallest integer k such that any sequence of …   Wikipedia

• Problem of Apollonius — In Euclidean plane geometry, Apollonius problem is to construct circles that are tangent to three given circles in a plane (Figure 1); two circles are tangent if they touch at a single point. Apollonius of Perga (ca. 262 BC ndash; ca. 190 BC)… …   Wikipedia

• Knapsack problem — BKP redirects here. For other uses, see BKP (disambiguation). Example of a one dimensional (constraint) knapsack problem: which boxes should be chosen to maximize the amount of money while still keeping the overall weight under or equal to… …   Wikipedia

• P versus NP problem — Unsolved problems in computer science Is P = NP ? …   Wikipedia

• P = NP problem — The relationship between the complexity classes P and NP is an unsolved question in theoretical computer science. It is considered to be the most important problem in the field – the Clay Mathematics Institute has offered a \$1 million US prize… …   Wikipedia

• Partition problem — In computer science, the partition problem is an NP complete problem. The problem is to decide whether a given multiset of integers can be partitioned into two halves that have the same sum. More precisely, given a multiset S of integers, is… …   Wikipedia

• 3-partition problem — The 3 partition problem is an NP complete problem in computer science. The problem is to decide whether a given multiset of integers can be partitioned into triples that all have the same sum. More precisely, given a multiset S of n = 3 m… …   Wikipedia

• Bin packing problem — In computational complexity theory, the bin packing problem is a combinatorial NP hard problem. In it, objects of different volumes must be packed into a finite number of bins of capacity V in a way that minimizes the number of bins used.There… …   Wikipedia