I have a set of objects. Each has a 'depends_on' list (of other objects) and a 'depended_upon_by' list. The lists are symetric - A is on B's 'depends_on' list IFF B is on A's 'depended_upon_by' list. There are no cycles; no object is on its own lists.
I want to sort the list into ranks. The first rank should have all the objects with empty 'depends_on' lists. No object can be in the same rank with another object in its 'depends_on' list. Each object is in as low a rank as possible, without violating the forgoing constraints.
I can think of several algorithms that would do this. Mainly,
* Everybody tracks their current rank
* Start everybody off in the first rank,
* Everybody adds 1 to their current rank and tells all their 'depended_upon_by's that they have to be at least that high.
* If an object is told to be at least N, if they aren't already N, they increase their rank to N and tells their 'depended_upon_by's to increase to at least N+1.
And I can think of both recursive and iterative ways to do this. But I have this nagging feeling that there's something really compact and elegant using sort. I just can't think what it would be.
Anybody?<p
thanks
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.