1

Possible Duplicate:
equal k subsets algorithm

Say I've a set of numbers, I want to divide the numbers into k subsets such that the numbers are evenly distributed. By evenly distributed, I mean the sum of values in the subsets are closest to other sums of other subsets. Can we implement a DP solution to this problem??

Please suggest!

Community
  • 1
  • 1
4sh1sh
  • 751
  • 1
  • 5
  • 9
  • This problem is known to be NP-hard. Are the numbers integers? Or arbitrary real numbers? – templatetypedef Sep 05 '11 at 22:14
  • I'm tempted to think that it doesn't matter whether they're integer or not. Since the only operation is addition, any problem involving real numbers can be scaled/perturbed to an equivalent problem with only integers. – Mysticial Sep 05 '11 at 22:18
  • 2
    @Mystical- It actually does matter. Not all problems that deal with reals can be adapted to work for integers; for example, linear programming in in P, while integer programming in NP-hard and conjectured not to have any polynomial-time solution. In this particular case, there may be a DP solution based on integers that does not work for the real numbers. The knapsack problem, for example, can be solved in pseudopolynomial time using DP, but the knapsack problem for reals cannot. – templatetypedef Sep 05 '11 at 22:20
  • @templatetypedef: Point taken. I was under the impression that since only addition is involved (addition cannot generate non-integers from integers) it was possible to scale the entire problem such that the differences between all numbers is significantly larger than one - at which everything can be rounded to an integer. My error is that this only holds assuming all the numbers have finite precision to begin with (in a computer). – Mysticial Sep 05 '11 at 22:36
  • @templatetypedef Can you suggest any approximation algorithm if the numbers are integers?? – 4sh1sh Sep 05 '11 at 23:03
  • @charlesworth It's a completely different problem, it doesnt state the subsets to have equal sums but the sums be closest to each other. – 4sh1sh Sep 05 '11 at 23:05
  • @4sh1sh: the [wikipedia page](http://en.wikipedia.org/wiki/Partition_problem#Methods) suggests some approximation for this problem, which is the partition problem. Other possibilities are [SAHC](http://en.wikipedia.org/wiki/Hill_climbing#Variants) and [genetic algorithms](http://en.wikipedia.org/wiki/Genetic_algorithm). If you specify why do you need this, we might be able to suggest which heuristic is better for your purpose – amit Sep 06 '11 at 06:14

1 Answers1

-1

All I can offer is my best attempt, here goes:

It seems to me that if m is the size of your set, then m/k = n; which is the number of elements in each set.

Now I am assuming you are working with integers, lets say we a set, s:

s ={1,2,3,4,5,6,7,8}

Now this is a simple idea that if you set is sorted then the sum of positions
-Sum(0 and last-0) = Sum(1,Last-1) = Sum(2,last-2) = Sum(3,last-3)... and so forth.

the variables would be:

  • m = 8
  • k = 2 (just for example)
  • n = 4

so we want 4 sets: s1 = 1,8 = Sum is 9 s2 = 2,7 = Sum is 9 s3 = 3,6 = Sum is 9 s4 = 4,5 = Sum is 9

Now there will of course be some trickiness if the set size is odd and/or if k is odd, but these can be dealt with using special cases- implementing the situation that works best for your specific purpose.

Hope this gives you a push in the right, or pretty much any direction.

Aziz
  • 1,584
  • 4
  • 23
  • 43
  • You cant say for sure "Sum(0 and last-0) = Sum(1,Last-1) = Sum(2,last-2) = Sum(3,last-3)... and so forth" is always true in a sorted set of integers. Just take a simple case of this set {1,1,1,1,1,99} . And your algorithm doesn't work best with many simple cases, lets say we have S={1,2,2,4,8,10} and k=3. The answer according to your approach is {{1,10},{2,8},{2,4}}. The solution to this case will be {{1,2,2,4},{8},{10}}. It doesn't have to be the necessarily the same number of integers in each subset, they can differ. – 4sh1sh Sep 05 '11 at 22:58