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
of cardinality
:
: ''S'' =
Let ''K'' be the sum of all elements in ''S''. That is: ''K'' = ''x''
1 + ... + ''x
N''. We will build an algorithm that determines whether there is a subset of ''S'' that sums to
. If there is a subset, then:
: if ''K'' is even, the rest of ''S'' also sums to
: if ''K'' is odd, then the rest of ''S'' sums to
. 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
. Let:
: ''p''(''i'', ''j'') be ''True'' if a subset of sums to ''i'' and ''False'' otherwise.
Then ''p''(
, ''N'') is ''True'' if and only if there is a subset of ''S'' that sums to
. The goal of our algorithm will be to compute ''p''(
, ''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'' − ''x
j'', ''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, ..., ''x
j''
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'' − ''x
j'', since ''x
j'' + that subset's sum = ''i''.
The pseudo-polynomial algorithm
The algorithm consists of building up a table of size
by
containing the values of the recurrence. Remember that
is the sum of all
elements in
. Once the entire table is filled in, we return
. Below is a depiction of the table
. 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 ''(
+ 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
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''(
, ''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