Partition problem minimum difference between sum of 2 sets

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
temper_3243
Experienced poster
Posts: 105
Joined: Wed May 25, 2005 7:23 am

Partition problem minimum difference between sum of 2 sets

Post by temper_3243 »

hi,
i have n numbers , n <= 10^5. Each number 0 <= k <= 2^30. Now i want to split them into 2 sets such that difference of sum of sets is minimum.

Can someone suggest a good algorithm with approximation which works for these cases very well ? Which cases doesn't it work well ?

Regards
Terry
N|N0
New poster
Posts: 36
Joined: Tue Jan 25, 2005 10:33 pm
Location: Ljubljana, Slovenija
Contact:

Post by N|N0 »

temper_3243
Experienced poster
Posts: 105
Joined: Wed May 25, 2005 7:23 am

Post by temper_3243 »

I don't understand how to reduce this to a knapsack problem. Can you give the algo or an example
Learnable
New poster
Posts: 1
Joined: Tue Nov 15, 2005 1:47 pm

Re: Partition problem minimum difference between sum of 2 se

Post by Learnable »

A number either belongs to set 1 or to set 2, and you can recurse on that. But with the amount of numbers you have, you'll probably have to find an approximate solution, or find a good way to evaluate partial solutions.

If the numbers are uniformly distributed, sort the numbers, and then alternatively add numbers to each set from both ends of the list.

For example, you have 1,2,3,4,5,6.

You start with 1 and 6. Add both to set 1. Then you get 2 and 5 - add them to set 2. When you get the last two numbers, add one to set 1 and another to set 2. If n is odd, add the last number to the set with the lower total.

So,

Set 1: 1 6 3
Set 2: 2 5 4

Once you do that, try to randomly exchange numbers between the sets to improve upon the solution.
kp
Learning poster
Posts: 71
Joined: Tue Apr 26, 2005 1:29 am
Location: Russia

Post by kp »

temper_3243 wrote:I don't understand how to reduce this to a knapsack problem. Can you give the algo or an example
1. Calculate Capacity = (sum of all numbers)/2
2. Now all you need is to find maximum sum of Xi that <= Capacity. That is exactly knapsack problem, and you probably need some approximate algorithm of solving knapsack problem
Post Reply

Return to “Algorithms”