User:Ryankaplan/sandbox

In computer science, the partition problem is the task of deciding whether a given multiset of integers can be partitioned into two subsets S1 and S2 such that the sum of the numbers in S1 equals the sum of the numbers in S2. Although the partition problem is NP-complete, there is a pseudo-polynomial time dynamic programming solution, and there are are heuristics that solve the problem in many instances, either optimally or approximately. For this reason, it has been called "The Easiest Hard Problem".

There is an optimization version of the partition problem, which is to partition the multiset "S" into two subsets S1, S2 such that the difference between the sum of elements in S1 and the sum of elements in S2 is minimized.

Examples
Given S={3, 1, 1, 2, 2, 1}, a valid solution to the partition problem is the two sets S1={1,1,1,2} and S2={2,3}. Both sets sum to 5, and they partition S. Note that this solution is not unique. S1={3, 1, 1} and S2={2,2, 1} is another solution.

Not every multiset of integers has a partition into two halves with equal sum. An example of such a set is S={2, 5}.

Pseudo-polynomial time algorithm
The problem can be solved using dynamic programming. Suppose the input to the algorithm is a list of the form:
 * S = x1, ..., xn

Let N be the sum of all elements in S. That is: N = x1+ ...+ xn. We will build an algorithm that determines if there is a subset of S that sums to $$\lfloor N/2 \rfloor $$. If there is a subset, then:
 * if N is even, the rest of S also sums to $$\lfloor N/2 \rfloor $$
 * if N is odd, then the rest of S sums to $$\lceil N/2 \rceil $$. This is as good a solution as is possible.

Recurrence relation
We wish to determine if there is a subset of S that sums to $$\lfloor N/2 \rfloor $$. Let:
 * p(i, j) be True if a subset of { x1, ..., xj } sums to i and False otherwise.

Then p($$\lfloor N/2 \rfloor $$, n) is True if and only if there is a subset of S that sums to $$\lfloor N/2 \rfloor $$. The goal of our algorithm will be to compute p($$\lfloor N/2 \rfloor $$, n). In aid of this, we have the following recurrence relation:
 * p(i, j) is True if either p(i, j - 1) is True or if p(i - xj, j - 1)'' is True
 * p(i, j) is False otherwise

The reasoning for this is as follows: there is some subset of S that sums to i using numbers
 * x1, ..., xj

if and only if either of the following is true:
 * There is a subset of { x1, ..., xj } that doesn't use xj and that sums to i
 * There is a subset of { x1, ..., xj } that does use xj and that sums to i - xj

The algorithm
The algorithm is to build up a table of size $$\lfloor N/2 \rfloor $$ by n containing the values of the recurrence. Once the entire table is filled in, return P($$\lfloor N/2 \rfloor $$, n). Below is a picture of the table P. There is a purple arrow from one block to another if the value of the target-block might depend on the value of the source-block. This dependence is a property of the recurrence relation.



INPUT: A list of integers S OUTPUT: True if S can be partitioned into two subsets that have equal sum 1 function find_partition( S ): 2    N ← sum(S) 3    P ← empty table of size ($$\lfloor N/2 \rfloor $$) by n 4    initialize top row of P to True 5    initialize leftmost-column of P, except for P[0, 0] to False 6    for i from 2 to $$\lfloor N/2 \rfloor $$ 7         for j from 2 to n 8          P(i, j) ← P(i-1, j) or P(i-1, j-S[i]) 9    return P($$\lfloor N/2 \rfloor $$, n)

Example
Below is the table P for the example set used above S = {3, 1, 1, 2, 2, 1}:

Runtime
This algorithm runs in time $$O(Nn)$$.

Special case of the subset-sum problem
The partition problem can be viewed as a special case of the subset sum problem and the pseudo-polynomial time dynamic programming solution given above generalizes to a solution for the subset sum problem.

Greedy Algorithm
One approach to the problem, imitating the way children choose teams for a game, is the greedy algorithm, which iterates through the numbers in descending order, assigning each of them to whichever subset has the smaller sum. This works well when the numbers in the set are of about the same size as its cardinality or less. This approach has a running time of $$O(n log(n))$$. An example of a set upon which this heuristic "breaks" is:
 * S = {5, 5, 4, 3, 3}

For the above input, the greedy approach would build sets S1 = {5, 4, 3} and S2 = {5, 3} which are not a solution to the partition problem. The solution is S1 = {5, 5} and S2 = {4, 3, 3}.

This greedy approach is known to give a 4/3-approximation to the optimal solution of the optimization version (if the greedy algorithm gives two sets $$S_1, S_2$$, then $$\max(\operatorname{sum}(S_1), \operatorname{sum}(S_2)) \le 4/3\mathrm{OPT}$$). Below is pseudocode for the greedy algorithm.

INPUT: A list of integers S OUTPUT: An attempt at a partition of S into two sets of equal sum 1 function find_partition( S ): 2    A ← {} 3    B ← {} 4    sort S in descending order 5    for i in S: 6        if |A| < |B| 7             A.push(i) 8        else 9             B.push(i) 10    return {A, B}

This algorithm can be extended to take the $$K$$ largest elements, and for each partition of them, extends the partition by adding the remaining elements successively to whichever set is smaller. (The simple version above corresponds to $$K=2$$.) This version runs in time $$O(2^K n^2)$$ and is known to give a $$(K+2)/(K+1)$$ approximation; thus we have a polynomial-time approximation scheme (PTAS) for the number partition problem, though this is not an FPTAS (the running time is exponential in the desired approximation guarantee). However, there are variations of this idea that are fully polynomial-time approximation schemes for the subset-sum problem, and hence for the partition problem as well.

Differencing Algorithm
Another heuristic, due to Narendra Karmarkar and Richard Karp, is the differencing algorithm, which at each step removes two numbers from the set and replaces them by their difference. This represents the decision to put the two numbers in different sets, without immediately deciding which one is in which set. The differencing heuristic performs better than the greedy one, but is still bad for instances where the numbers are exponential in the size of the set.

Other approaches
There are also anytime algorithms, based on the differencing heuristic, that first find the solution returned by the differencing heuristic, then find progressively better solutions as time allows (possibly requiring exponential time to reach optimality, for the worst instances).

Hard instances
Sets with only one, or no partitions tend to be hardest (or most expensive) to solve compared to their input sizes. When the values are small compared to the size of the set, perfect partitions are more likely. The problem is known to undergo a "phase transition"; being likely for some sets and unlikely for others. If m is the number of bits needed to express any number in the set and n is the size of the set then $$m/n < 1$$ tends to have many solutions and $$m/n > 1$$ tends to have few or no solutions. As n and m get larger, the probability of a perfect partition goes to 1 or 0 respectively. This was originally argued using methods from physics by Stephan Mertens, and later proved by Borgs, Chayes, and Pittel.

The k-partition problem
There is a problem called the 3-partition problem which is to partition the set S into |S|/3 triples each with the same sum. The 3-partition problem is quite different than the Partition Problem and has no pseudo-polynomial time algorithm unless P = NP. The generalizations of the partition problem, see the Bin packing problem.