Give a Set S
, partition the set into k
disjoint subsets such that the difference of their sums is minimal.
say, S = {1,2,3,4,5}
and k = 2
, so { {3,4}, {1,2,5} }
since their sums {7,8}
have minimal difference. For S = {1,2,3}, k = 2
it will be {{1,2},{3}}
since difference in sum is 0
.
The problem is similar to The Partition Problem from The Algorithm Design Manual. Except Steven Skiena discusses a method to solve it without rearrangement.
I was going to try Simulated Annealing. So i wondering, if there was a better method?
Thanks in advance.
If the sets are large, I would definitely go for stochastic search. Don't know exactly what spinning_plate means when writing that "the neighborhood is not clearly defined". Of course it is --- you either move one item from one set to another, or swap items from two different sets, and this is a simple neighborhood. I would use both operations in stochastic search (which in practice could be tabu search or simulated annealing.)
The pseudo-polytime algorithm for a knapsack can be used for
k=2
. The best we can do is sum(S)/2. Run the knapsack algorithmthen look at sum(S)/2, followed by sum(S)/2 +/- 1, etc.
For 'k>=3' I believe this is NP-complete, like the 3-partition problem.
The simplest way to do it for k>=3 is just to brute force it, here's one way, not sure if it's the fastest or cleanest.
Simulated annealing might be good, but since the 'neighbors' of a particular solution aren't really clear, a genetic algorithm might be better suited to this. You'd start out by randomly picking a group of subsets and 'mutate' by moving numbers between subsets.