0

I have the following problem: Given a set of N integers divide them into two almost equal partitions in such a way that the sum of the greater partition is minimum. This sounds almost like the classical partition problem with one exception: the even numbers can be divided by two and each half can be placed in a separate partition.

For example: N = 4 Numbers: 20 5 3 1

The result is: 15

Explanation: The first number will be divided by two and each half placed in one of the two partitions => first partition = 10, second partition = 10.The second number will be added to the first partition => first partition = 15. The last two numbers will be added to the second partition => second partition = 14

=> the sum of the greater partition(partition one) = 15.

The idea that I have is to sort decreasingly the odd numbers and using a greedy algorithm to start adding them always keeping the difference between the sum of the two partitions as optimal as possible. After finishing with the odd numbers all what is left is to take the even ones and see if adding them entirely to one partition is more optimal than dividing them by two and adding each half to one of those two partitions.

Also for the following set of data the algorithm will provide the correct answer: N = 2 Numbers: 16 15

=> will take 15, add it to the first partition, then take 16 see that is not optimal to divide it by two thus it will add it to the second partition.

=> the answer will be 16.

I am interested if you can provide me a set of data for which the algorithm will not provide the optimal answer and I would be also be grateful if you could suggest any improvements.

Thanks! :)

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
ZLMN
  • 731
  • 2
  • 10
  • 24
  • By "almost equal partitions" do you mean the absolute value of the difference between the number of elements in the partitions is less than or equal to 1? – pmg Apr 07 '11 at 20:19

3 Answers3

3

I would just split all the even numbers in one pass and then apply the classical partition algorithm. Or is there some secondary objective to minimize the number of splits?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I forgot to specify one small detail: all number are positive. – ZLMN Apr 07 '11 at 18:43
  • Good to specify that. :) I still think just splitting all the even numbers at the start and then using the classical algorithm is the easiest approach. – Ted Hopp Apr 07 '11 at 18:53
  • For the example with N = 2 and numbers: 16 15 this way of approaching the problem will provide the result 23 while the optimal one is 16. Please correct me if I got your idea wrong. Thanks! – ZLMN Apr 07 '11 at 19:02
  • It will still give the answer 16: (8, 8) vs (15). If each half of the split has to go in a separate partition, just do a pass at the end to rejoin any split pair that ended up in the same partition. – dfan Apr 07 '11 at 21:11
  • @user697334 - When I said "split all the even numbers", I didn't mean that they should be allocated to separate partitions, just that each even number should be replaced with two numbers half the size in the original set. After the even numbers have been split, then start the classical partitioning process using the modified set. – Ted Hopp Apr 07 '11 at 21:47
1

The partition problem is NP-complete, meaning that a polynomial time algorithm is unlikely to exist. Your modified variant is also NP-complete; a reduction to the original version is quite simple.

abeln
  • 3,749
  • 2
  • 22
  • 31
  • I am aware of the fact that it is a NP-complete one but I am only trying to find an algorithm that provides a solution as good as possible. Furthermore I saw that pmg`s version provides the correct answer but I am also interested if there are any mistakes in my approach. – ZLMN Apr 07 '11 at 19:18
  • Again, because the problem is NP-complete if your polynomial time algorithm is able to find a solution that is "as good as possible", then P=NP and you've won a million dollars :D What remains is using an algorithm that, while not correct in every instance, provides a good approximation. – abeln Apr 07 '11 at 19:22
  • Exactly this was what I was trying to say but you said it better :D. – ZLMN Apr 07 '11 at 19:42
0

Why don't you divide each and every number in half and put the extra 1 for odd numbers in cycling partitions? The 2nd partition will always have the smaller sum.

List: 20 17 6 5 3 0 -1 9999999

P1: 10 | 8+1 | 3 | 2 | 1+1 | 0 | -1 | 4999999+1 | ==> sum is 5000025 P2: 10 | 8 | 3 | 2+1 | 1 | 0 | -1+1 | 4999999 | ==> sum is 5000024

pmg
  • 106,608
  • 13
  • 126
  • 198
  • I thought the OP implied that only the even numbers can be divided in half. – abeln Apr 07 '11 at 19:26
  • This approach fails to provide a good approximation because the problem does not allow the division of odd numbers => partition1 = 9999999 and partition2 will be the sum of the remaining numbers. – ZLMN Apr 07 '11 at 19:47