Subset Sum Problem - Pseudo-polynomial Time Dynamic Programming Solution

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 determine if there is 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 boolean-valued function Q(i,s) to be the value (true or false) of

"there is a nonempty subset of x1, ..., xi which sums to s".

Thus, the solution to the problem is the value of Q(n,0).

Clearly, Q(i,s) = false if s < N or s > P so these values do not need to be stored or computed. Create an array to hold the values Q(i,s) for 1 ≤ in and NsP.

The array can now be filled in using a simple recursion. Initially, for NsP, set

Q(1,s) := (x1 == s).

Then, for i = 2, …, n, set

Q(i,s) := Q(i − 1,s) or (xi == s) or Q(i − 1,sxi) for NsP.

For each assignment, the values of Q on the right side are already known, either because they were stored in the table for the previous value of i or because Q(i − 1,sxi) = false if sxi < N or sxi > P. Therefore, the total number of arithmetic operations is O(n(PN)). For example, if all the values are O(nk) for some k, then the time required is O(nk+2).

This algorithm is easily modified to return the subset with sum 0 if there is one.

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

For the case that each xi is positive and bounded by a fixed constant r, Pisinger found a linear time algorithm having time complexity O(nr).

Read more about this topic:  Subset Sum Problem

Famous quotes containing the words time, dynamic, programming and/or solution:

    A thing is mighty big when time and distance cannot shrink it.
    Zora Neale Hurston (1891–1960)

    Knowledge about life is one thing; effective occupation of a place in life, with its dynamic currents passing through your being, is another.
    William James (1842–1910)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    Coming out, all the way out, is offered more and more as the political solution to our oppression. The argument goes that, if people could see just how many of us there are, some in very important places, the negative stereotype would vanish overnight. ...It is far more realistic to suppose that, if the tenth of the population that is gay became visible tomorrow, the panic of the majority of people would inspire repressive legislation of a sort that would shock even the pessimists among us.
    Jane Rule (b. 1931)