0

I'm trying to solve a variant of the partition problem. I have two important twists. I need to solve for k partitions, not just 2, as in the classic partition problem.

The following code does that:

https://gist.github.com/ishikawa/21680

I also need to allow the freedom to jumble up the order of the items, so that I can get the optimal solution. So, where the classic problem requires the order of the elements be left in tact, and the array is just split at an semi-optimal points, I need to allow the array to be re-ordered in such a way as the difference between the partitions is smallest.

How can I tackle this? Both twists are necessary for this real world application. I'd be extremely happy if I could find a Java library that already handles this.

Adam
  • 913
  • 1
  • 9
  • 26
  • But isn't the problem defined on sets anyway? So order doesn't matter anyway. – laune Sep 19 '15 at 14:01
  • @laune I don't understand your comment. As far as I understand, the classic partition problem is to divide into k sets, while maintaining object order. I don't need to maintain object order, which makes the solution more complex. So, where 1,2,3,4,5,6,7 would be [1,2,3,4,5] and [6,7] in the classic problem, I need a solution like [2,3,4,5] and [1,6,7], and my number of partitions may not be 2. – Adam Sep 19 '15 at 14:13
  • I don't know any definition of this problem that does not state it in terms of *sets* and *subsets*. And *sets* are *never* ordered. - If you have to maintain order, splitting into two partitions is a very simple programming exercise. – laune Sep 19 '15 at 14:26
  • @laune I agree. If the we maintain order, the problem is very simple. If not, the problem is much more difficult. Unfortunately, the examples I'm finding keep the objects in the same order, which constrains the solution, in most cases, to a suboptimal distribution. Example: https://www8.cs.umu.se/kurser/TDBA77/VT06/algorithms/BOOK/BOOK2/NODE45.HTM – Adam Sep 19 '15 at 17:06
  • This might be a better starting point: https://en.wikipedia.org/wiki/Partition_problem Perhaps the Greedy algorithm could be extended to `k>2`. - You may not be able to obtain the "optimal solution", but for most real-world apps a fairly good solution will do. – laune Sep 19 '15 at 17:19
  • I have such a small number of objects for each set that greedy doesn't work very well. – Adam Sep 19 '15 at 19:13

0 Answers0