|
|
| Line 1: |
Line 1: |
| In [[computer science]], the '''partition problem''' is the task of deciding whether a given [[multiset]] ''S'' of positive integers can be [[partition of a set|partitioned]] into two subsets ''S''<sub>1</sub> and ''S''<sub>2</sub> such that the sum of the numbers in ''S''<sub>1</sub> equals the sum of the numbers in ''S''<sub>2</sub>. Although the partition problem is [[NP-complete]], there is a [[pseudo-polynomial time]] [[dynamic programming]] solution, and there are heuristics that solve the problem in many instances, either optimally or approximately. For this reason, it has been called "The Easiest Hard Problem".<ref name=hayes>{{Harvnb|Hayes|2002}}</ref>
| | Hi there. Allow me begin by introducing the author, [http://mybrandcp.com/xe/board_XmDx25/107997 telephone psychic] her title is Sophia. Ohio is where his home is and his family members enjoys it. Office supervising is exactly where her main earnings arrives from but she's currently utilized for an additional one. [http://www.onbizin.co.kr/xe/?document_srl=320614 clairvoyance] The favorite pastime for him and his kids is to perform lacross and he would by no means give it up.<br><br>my web page - free online tarot card readings, [http://www.weddingwall.com.au/groups/easy-advice-for-successful-personal-development-today/ http://www.weddingwall.com.au/groups/easy-advice-for-successful-personal-development-today/], |
| | |
| There is an [[optimization problem|optimization version]] of the partition problem, which is to partition the multiset ''S'' into two subsets ''S''<sub>1</sub>, ''S''<sub>2</sub> such that the difference between the sum of elements in ''S''<sub>1</sub> and the sum of elements in ''S''<sub>2</sub> is minimized.
| |
| | |
| ==Examples==
| |
| Given ''S'' = {3,1,1,2,2,1}, a valid solution to the partition problem is the two sets ''S''<sub>1</sub> = {1,1,1,2} and ''S''<sub>2</sub> = {2,3}. Both sets sum to 5, and they [[Partition of a set|partition]] ''S''. Note that this solution is not unique. ''S''<sub>1</sub> = {3,1,1} and ''S''<sub>2</sub> = {2,2,1} is another solution.
| |
| | |
| Not every [[multiset]] of positive integers has a partition into two halves with equal sum. An example of such a set is ''S'' = {2,5,1,11,3,5,10,24,555,123,985,1337,9,13,24,89,19,27}.
| |
| | |
| ==Pseudo-polynomial time algorithm==
| |
| The problem can be solved using [[dynamic programming]] 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 list of the form:
| |
| :''S = x''<sub>1</sub>, ..., ''x''<sub>''n''</sub>
| |
| | |
| Let ''N'' be the sum of all elements in ''S''. That is: ''N'' = ''x''<sub>1</sub> + ... + ''x''<sub>''n''</sub>. We will build an algorithm that determines if there is a subset of ''S'' that sums to <math>\lfloor N/2 \rfloor </math>. If there is a subset, then:
| |
| : if N is even, the rest of ''S'' also sums to <math>\lfloor N/2 \rfloor </math>
| |
| : if N is odd, then the rest of ''S'' sums to <math>\lceil N/2 \rceil </math>. This is as good a solution as possible.
| |
| | |
| ===Recurrence relation===
| |
| We wish to determine if there is a subset of ''S'' that sums to <math>\lfloor N/2 \rfloor </math>. Let:
| |
| :''p''(''i'', ''j'') be ''True'' if a subset of { ''x''<sub>1</sub>, ..., ''x''<sub>''j''</sub> } sums to ''i'' and ''False'' otherwise.
| |
| | |
| Then ''p''(<math>\lfloor N/2 \rfloor </math>, ''n'') is ''True'' if and only if there is a subset of ''S'' that sums to <math>\lfloor N/2 \rfloor </math>. The goal of our algorithm will be to compute ''p''(<math>\lfloor N/2 \rfloor </math>, ''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'' − ''x''<sub>''j''</sub>, ''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''<sub>1</sub>, ..., ''x''<sub>''j''</sub>
| |
| | |
| if and only if either of the following is true:
| |
| : There is a subset of { ''x''<sub>1</sub>, ..., ''x''<sub>''j''</sub> } that '''does not''' use ''x''<sub>''j''</sub> and that sums to ''i''
| |
| : There is a subset of { ''x''<sub>1</sub>, ..., ''x''<sub>''j''</sub> } that '''does not''' use ''x''<sub>''j''</sub> and that sums to ''i'' − ''x''<sub>''j''</sub> (since ''x''<sub>''j''</sub> + that subset's sum = ''i'')
| |
| | |
| ===The pseudo-polynomial algorithm===
| |
| The algorithm is to build up a table of size <math>\lfloor N/2 \rfloor </math> by ''n'' containing the values of the recurrence. Once the entire table is filled in, return ''P''(<math>\lfloor N/2 \rfloor </math>, ''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.
| |
| | |
| [[File:Partition Problem DP table showing dependencies.png|thumb|none|300px|Dependencies of table entry (''i'', ''j'')]]
| |
| | |
| 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 ← |S|
| |
| 3 ''N'' ← ''sum(S)''
| |
| 4 ''P'' ← empty boolean table of size ''(<math>\lfloor N/2 \rfloor </math> + 1)'' by ''(n + 1)''
| |
| 5 '''initialize''' top row (''P(0,x)'') of ''P'' to True
| |
| 6 '''initialize''' leftmost column (''P(x, 0)'') of ''P'', except for ''P(0, 0)'' to False | |
| 7 '''for''' ''i'' '''from''' 1 '''to''' <math>\lfloor N/2 \rfloor </math>
| |
| 8 '''for''' ''j'' '''from''' 1 '''to''' n
| |
| 9 ''P(i, j)'' ← ''P(i, j-1)'' '''or''' ''P(i-S[j-1], j-1)''
| |
| 10 '''return''' ''P(<math>\lfloor N/2 \rfloor </math>, n)''
| |
| | |
| C# code:
| |
| //pseudo-polynomial algorithm
| |
| public static bool balancePatition(int[] S)
| |
| {
| |
| var n = S.Length;
| |
| var N = S.Sum();
| |
| bool[,] P = new bool[N / 2 + 1, n + 1];
| |
| '''for''' (int ''i'' = 0; ''i'' < n + 1; ''i''++)
| |
| P[0, ''i''] = true;
| |
| '''for''' (int ''i'' = 1; ''i'' < N / 2 + 1; ''i''++)
| |
| P[''i'', 0] = false;
| |
| '''for''' (int ''i'' = 1; ''i'' <= N / 2; ''i''++)
| |
| '''for''' (int ''j'' = 1; ''j'' <= n; ''j''++)
| |
| P[''i'', ''j''] = S[''j'' - 1] <= ''i'' ? P[''i'', ''j'' - 1] || P[''i'' - S[''j'' - 1], ''j'' - 1] : P[''i'', ''j'' - 1];
| |
| '''return''' P[N / 2, n];
| |
| }
| |
| | |
| ===Example===
| |
| Below is the table ''P'' for the example set used above ''S'' = {3, 1, 1, 2, 2, 1}:
| |
| [[File:Partition Prob DP table example.jpg|thumb|none|300px|Result of example execution of algorithm on the table P]]
| |
| | |
| ===Runtime===
| |
| This algorithm runs in time <math>O(Nn)</math>, where <math>n</math> is the number of elements in the input set and <math>N</math> is the sum of elements in the input set.
| |
| | |
| ==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]].
| |
| | |
| ==Approximation algorithm approaches==
| |
| | |
| ===The 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 <math>O(n \log(n))</math>. 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 ''S''<sub>1</sub> = {5, 4} and ''S''<sub>2</sub> = {5, 3, 3} which are not a solution to the partition problem. The solution is ''S''<sub>1</sub> = {5, 5} and ''S''<sub>2</sub> = {4, 3, 3}.
| |
| | |
| This greedy approach is known to give a 4/3-[[approximation algorithm|approximation]] to the optimal solution of the optimization version (if the greedy algorithm gives two sets <math>S_1, S_2</math>, then <math>\max(\operatorname{sum}(S_1), \operatorname{sum}(S_2)) \le 4/3\mathrm{OPT}</math>). 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''' ''sum(A) <= sum(B)''
| |
| 7 add element ''i'' to set ''A''
| |
| 8 '''else'''
| |
| 9 add element ''i'' to set ''B''
| |
| 10 '''return''' ''{A, B}''
| |
| | |
| This algorithm can be extended to take the <math>K</math> 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 <math>K=2</math>.) This version runs in time <math>O(2^K n^2)</math> and is known to give a <math>(K+2)/(K+1)</math> approximation; thus we have a [[polynomial-time approximation scheme]] (PTAS) for the number partition problem, though this is not a [[Fully polynomial-time approximation scheme|fully polynomial time approximation scheme]] (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.<ref name=knapsack/><ref name="MartelloToth">{{cite book |chapter=4 Subset-sum problem|pages=105–136| title = Knapsack problems: Algorithms and computer interpretations | last1=Martello|first1=Silvano|last2=Toth|first2=Paolo| publisher =Wiley-Interscience | year = 1990 | isbn = 0-471-92420-2|mr=1086874|ref=harv}}</ref>
| |
| | |
| ===Differencing algorithm===
| |
| Another heuristic, due to [[Narendra Karmarkar]] and [[Richard Karp]],<ref>{{Harvnb|Karmarkar|Karp|1982}}</ref> 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.<ref name="hayes" />
| |
| | |
| Java code:
| |
| int karmarkarKarpPartition( int[] baseArr ){
| |
| // create max heap
| |
| PriorityQueue<Integer> heap = new PriorityQueue<Integer>(baseArr.length, REVERSE_INT_CMP);
| |
| for( int value : baseArr ){
| |
| heap.add( value );
| |
| }
| |
| while( heap.size() > 1 ){
| |
| int val1 = heap.poll();
| |
| int val2 = heap.poll();
| |
| heap.add( val1 - val2 );
| |
| }
| |
| return heap.poll();
| |
| }
| |
| ===Other approaches===
| |
| There are also [[anytime algorithm]]s, 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).<ref>{{Harvnb|Korf|1998}}, {{Harvnb|Mertens|1999}}</ref>
| |
| | |
| ==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 <math>m/n < 1</math> tends to have many solutions and <math>m/n > 1</math> 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 based on empirical evidence by Gent and Walsh,<ref>{{Harvnb|Gent|Walsh|1996}}</ref> then using methods from statistical physics by Mertens,<ref>{{Harvnb|Mertens|1998}}, {{Harvnb|Mertens|2001}}</ref> and later proved by [[Christian Borgs|Borgs]], [[Jennifer Tour Chayes|Chayes]], and [[Boris Pittel|Pittel]].<ref>{{Harvnb|Borgs|Chayes|Pittel|2001}}</ref>
| |
| | |
| ==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]]'''.<ref name="Garey & Johnson">{{cite book| pages=96–105|title=Computers and Intractability; A Guide to the Theory of NP-Completeness|last1=Garey|first1=Michael|last2=Johnson|first2=David|year=1979|isbn=0-7167-1045-5}}</ref> For generalizations of the partition problem, see the [[Bin packing problem]].
| |
| | |
| == Alternative forms of the problem ==
| |
| | |
| An interesting related problem, somewhat similar to the [[Birthday paradox]] is that of determining the size of the input set so that we have a probability of one half that there is a solution, under the assumption that each element in the set is randomly selected with uniform distribution between 1 and some given value.
| |
| | |
| The problem is interesting in that the solution can be counter-intuitive (like the [[Birthday paradox]]). For example, with elements randomly selected in between 1 and one million, many people's intuition is that the answer is in the thousands, tens, or even hundreds of thousands, whereas the correct answer is approximately 23 (see [[Birthday problem#Partition problem]] for details).
| |
| | |
| == See also ==
| |
| * [[Subset sum problem]]
| |
| * [[Bin packing problem]]
| |
| | |
| == Notes ==
| |
| {{reflist|refs=
| |
| <ref name=knapsack>{{citation | year=2004 | title = Knapsack problems | author1=Hans Kellerer | author2=Ulrich Pferschy | author3=David Pisinger | publisher=Springer | isbn=9783540402862 | page=97 | url=http://books.google.com/?id=u5DB7gck08YC&pg=PA97}}</ref>
| |
| }}
| |
| | |
| ==References==
| |
| * {{citation | url=http://www.americanscientist.org/issues/pub/2002/3/the-easiest-hard-problem |title=The Easiest Hard Problem | magazine=[[American Scientist]] | last=Hayes|first=Brian|authorlink=Brian Hayes (scientist) |date=May–June 2002 }}
| |
| *{{Citation
| |
| | last = Karmarkar
| |
| | first = Narenda
| |
| | last2= Karp
| |
| | first2 = Richard M
| |
| | title = The Differencing Method of Set Partitioning
| |
| | journal = Technical Report UCB/CSD 82/113
| |
| | publisher = Computer Science Division (EECS)
| |
| | location = University of California at Berkeley
| |
| | year = 1982
| |
| }}
| |
| *{{Citation
| |
| | last = Gent
| |
| | first = Ian
| |
| | last2 = Walsh
| |
| | first2 = Toby
| |
| | title = Phase Transitions and Annealed Theories: Number Partitioning as a Case Study
| |
| | url = http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.4475
| |
| |date=August 1996
| |
| | conference = ECAI-96
| |
| | editor = Wolfgang Wahlster
| |
| | booktitle = Proceedings of 12th European Conference on Artificial Intelligence
| |
| | publisher = John Wiley and Sons
| |
| | pages = 170–174
| |
| }}
| |
| *{{Citation
| |
| | last = Gent
| |
| | first = Ian
| |
| | last2 = Walsh
| |
| | first2 = Toby
| |
| | title = Analysis of Heuristics for Number Partitioning
| |
| | url = http://dx.doi.org/10.1111/0824-7935.0006
| |
| | year = 1998
| |
| | journal = Computational Intelligence
| |
| | volume = 14
| |
| | issue = 3
| |
| | pages = 430–451
| |
| | doi = 10.1111/0824-7935.00069
| |
| }}
| |
| *{{Citation
| |
| | doi = 10.1103/PhysRevLett.81.4281
| |
| | volume = 81
| |
| | issue = 20
| |
| | pages = 4281
| |
| | last = Mertens
| |
| | first = Stephan
| |
| | title = Phase Transition in the Number Partitioning Problem
| |
| | journal = Physical Review Letters
| |
| | accessdate = 2009-10-03
| |
| | date = November 1998
| |
| | url = http://link.aps.org/abstract/PRL/v81/p4281
| |
| | bibcode=1998PhRvL..81.4281M
| |
| |arxiv = cond-mat/9807077 }}
| |
| | |
| * {{Citation
| |
| | doi = 10.1016/S0304-3975(01)00153-0
| |
| | last = Mertens
| |
| | first = Stephan
| |
| | title = A physicist's approach to number partitioning
| |
| | journal = Theoretical Computer Science
| |
| | volume = 265
| |
| | issue = 1-2
| |
| | pages = 79–108| year = 2001
| |
| }}
| |
| *{{citation | year=2006 | chapter=The Easiest Hard Problem: Number Partitioning | last=Mertens|first=Stephan | title = Computational complexity and statistical physics | editor1=Allon Percus | editor2=Gabriel Istrate | editor3=[[Cris Moore|Cristopher Moore]] | publisher=Oxford University Press US | isbn=9780195177374 | page=125 | url=http://books.google.com/?id=4YD6AxV95zEC&pg=PA125 |arxiv=cond-mat/0310317|bibcode = 2003cond.mat.10317M }}
| |
| *{{Citation
| |
| | doi = 10.1002/rsa.10004
| |
| | volume = 19
| |
| | issue = 3-4
| |
| | pages = 247–288
| |
| | last1 = Borgs
| |
| | first1 = Christian
| |
| | last2 = Chayes
| |
| | first2 = Jennifer
| |
| | last3 = Pittel
| |
| | first3 = Boris
| |
| | title = Phase transition and finite-size scaling for the integer partitioning problem
| |
| | journal = Random Structures and Algorithms
| |
| | accessdate = 2009-10-04
| |
| | year = 2001
| |
| | url = http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.89.9577
| |
| }}
| |
| * {{Citation
| |
| | doi = 10.1016/S0004-3702(98)00086-1
| |
| | issn = 0004-3702
| |
| | volume = 106
| |
| | issue = 2
| |
| | pages = 181–203
| |
| | last = Korf
| |
| | first = Richard E.
| |
| | title = A complete anytime algorithm for number partitioning
| |
| | journal = Artificial Intelligence
| |
| | accessdate = 2009-10-04
| |
| | year = 1998
| |
| | url = http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.90.993
| |
| }}
| |
| *{{Citation
| |
| | last = Mertens
| |
| | first = Stephan
| |
| | title = A complete anytime algorithm for balanced number partitioning| year = 1999
| |
| | arxiv = cs/9903011
| |
| |bibcode = 1999cs........3011M }}
| |
| | |
| [[Category:NP-complete problems]]
| |