HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, pseudopolynomial time number partitioning is a pseudopolynomial time algorithm for solving the
partition problem In number theory and computer science, the partition problem, or number partitioning, is the task of deciding whether a given multiset ''S'' of positive integers can be partitioned into two subsets ''S''1 and ''S''2 such that the sum of the numbers ...
. The problem can be solved using
dynamic programming Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. ...
when the size of the set and the size of the sum of the integers in the set are not too big to render the storage requirements infeasible. Suppose the input to the algorithm is a multiset S of cardinality N: : ''S'' = Let ''K'' be the sum of all elements in ''S''. That is: ''K'' = ''x''1 + ... + ''xN''. We will build an algorithm that determines whether there is a subset of ''S'' that sums to \lfloor K/2 \rfloor . If there is a subset, then: : if ''K'' is even, the rest of ''S'' also sums to \lfloor K/2 \rfloor : if ''K'' is odd, then the rest of ''S'' sums to \lceil K/2 \rceil . This is as good a solution as possible. e.g.1 ''S'' = , ''K'' = ''sum(S)'' = 11, ''K/2'' = 5, Find a subset from ''S'' that is closest to ''K/2'' -> = 5, 11 - 5 * 2 = 1 e.g.2 ''S'' = , ''K'' = ''sum(S)'' = 11, ''K/2'' = 5, Find a subset from ''S'' that is closest to ''K/2'' -> = 4, 11 - 4 * 2 = 3


Recurrence relation

We wish to determine if there is a subset of ''S'' that sums to \lfloor K/2 \rfloor . Let: : ''p''(''i'', ''j'') be ''True'' if a subset of sums to ''i'' and ''False'' otherwise. Then ''p''(\lfloor K/2 \rfloor , ''N'') is ''True'' if and only if there is a subset of ''S'' that sums to \lfloor K/2 \rfloor . The goal of our algorithm will be to compute ''p''(\lfloor K/2 \rfloor , ''N''). In aid of this, we have the following
recurrence relation In mathematics, a recurrence relation is an equation according to which the nth term of a sequence of numbers is equal to some combination of the previous terms. Often, only k previous terms of the sequence appear in the equation, for a parameter ...
: : ''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 : ''x''1, ..., ''xj'' if and only if either of the following is true: : There is a subset of that sums to ''i''; : there is a subset of that sums to ''i'' − ''xj'', since ''xj'' + that subset's sum = ''i''.


The pseudo-polynomial algorithm

The algorithm consists of building up a table of size \lfloor K/2 \rfloor by N containing the values of the recurrence. Remember that K is the sum of all N elements in S. Once the entire table is filled in, we return P(\lfloor K/2 \rfloor, N). Below is a depiction of the table P. There is a blue 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. function can_be_partitioned_equally(''S'') is input: A list of integers ''S''. output: True if ''S'' can be partitioned into two subsets that have equal sum. ''n'' ← , S, ''K'' ← sum(''S'') ''P'' ← empty boolean table of size ''(\lfloor K/2 \rfloor + 1)'' by ''(n + 1)'' initialize top row (''P''(0,''x'')) of ''P'' to True initialize leftmost column (''P''(''x'', 0)) of ''P'', except for ''P''(0, 0) to False for ''i'' from 1 to \lfloor K/2 \rfloor for ''j'' from 1 to n x = ''S'' 'j''-1 if (''i''-''x'') >= 0 then ''P''(''i'', ''j'') ← ''P''(''i'', ''j''-1) or ''P''(''i''-''x'', ''j''-1) else ''P''(''i'', ''j'') ← ''P''(''i'', ''j''-1) return ''P''(\lfloor K/2 \rfloor , ''n'')


Example

Below is the table ''P'' for the example set used above ''S'' = :


Analysis

This algorithm runs in time , where is the number of elements in the input set and is the sum of elements in the input set. The algorithm can be extended to the -way multi-partitioning problem, but then takes memory where is the largest number in the input, making it impractical even for unless the inputs are very small numbers. This algorithm can be generalized to a solution for the
subset sum problem The subset sum problem (SSP) is a decision problem in computer science. In its most general formulation, there is a multiset S of integers and a target-sum T, and the question is to decide whether any subset of the integers sum to precisely T''.'' T ...
.


References

{{Reflist Number partitioning Pseudo-polynomial time algorithms