You're right! Had done something very similar about two decades ago, although to detect loops and using Oracle PL/SQL. IMHO, the following (relational database inspired) method should work.

1. Arrange all the sets in a table in columns named FROM and TO where the smaller one is in the FROM column - if no such case there is a loop.

2. Assign all FROM's without an entry in TO as being at LEVEL = 1 and the corresponding TO as at LEVEL = 2

3. Starting from LEVEL = 2 and incrementing LEVEL by 1 until there are no further records to be assigned a LEVEL: Assign the next LEVEL to the corresponding TO

4. Start with the records with the largest LEVEL and work backwards from the previous LEVEL. Be careful when there is more than one "parent" at the previous LEVEL - multiple results.

5. If at end there are records with no LEVEL assigned, then there is a loop


In reply to Re^2: Sub set where all are connected by Sanjay
in thread Sub set where all are connected by Sanjay

Title:
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, 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:
    & &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.