Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: 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:

  • Sort the categories by height, decreasing.
  • Once for each column: put the highest unassigned category into this column.
  • For each column in reverse order, until there are no categories left: put the next highest unassigned category into this column.
With your sample data:

my @height = qw/ 10 15 25 30 10 13 /;
we'd get the following solution:

column 1: 30
column 2: 25
column 3: 15
column 4: 13
column 4: +10
column 3: +10
This 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 thought-provoking 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...


In reply to Re: Puzzle: need a more general algorithm by ferrency
in thread Puzzle: need a more general algorithm by Ovid

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (9)
    As of 2015-11-27 14:12 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (729 votes), past polls