Keep It Simple, Stupid  
PerlMonks 
Re: Puzzle: need a more general algorithmby ferrency (Deacon) 
on Jul 08, 2002 at 20:03 UTC ( #180302=note: print w/replies, xml )  Need Help?? 
This is a very interesting problem. Broken down to its core, it seems like your problem can be restated like this: Given a set of N values, divide the set into M subsets, such that no subset is empty, and the maximum of the sums of the values in the subsets is minimized. I think a perfect solution is hard and would be slow. But you can probably get a solution that works relatively well much more quickly. One way to look at the problem: For each value (category) determine which subset (column) it belongs in. Another way is: For each subset (column), determine which values(categories) belong in it. Assuming your number of categories is at least double your number of columns, you might want to see what kind of results you get with this technique:
we'd get the following solution:
column 1: 30 column 2: 25 column 3: 15 column 4: 13 column 4: +10 column 3: +10This is equivalent to what you have, but much more straightforward to calculate. And the solution is more general. However, at this point I have nothing other than an intuition (and a few test cases) that this solution will probably be good enough most of the time. If you have many more categories than columns, this will probably stop working well pretty quickly. If you have 3 very large categories and 3 very small categories, this method won't find the solution that places all three small categories in the same column. You might also try: For each category, in decreasing height order: put that category in the column with the lowest current total height. Thanks for the thoughtprovoking node. I'm looking forward to others' solutions to this problem. Update:Dragonchild is absolutely right: I did miss that constraint. Now for some more thought on the subject... Alan
In Section
Seekers of Perl Wisdom

