Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial order sorting?
    primarykey
    data
    text
    <p>Say, we have some items, and each defines some partial sorting rules, like this:</p> <blockquote> <p>I'm <code>A</code> and I want to be before <code>B</code></p> <p>I'm <code>C</code> and I want to be after <code>A</code> but before <code>D</code></p> </blockquote> <p>So we have items <code>A,B,C,D</code> with these rules:</p> <ul> <li><code>A&gt;B</code></li> <li><code>C&lt;A</code>, <code>C&gt;D</code></li> <li>nothing else! So, <code>B</code> and <code>D</code> have no 'preferences' in ordering and are considered equal.</li> </ul> <p>As you see, transitive relation rules are not working here. However, if <code>A&gt;B</code> it still means that <code>B&lt;A</code>. So, there can be multiple possible results of sorting:</p> <ol> <li>A B C D</li> <li>A C D B</li> <li>A C B D</li> <li>A B C D</li> </ol> <p>How can I implement a sorting algorithm that handles such a situation?</p> <hr> <p>The reason: there're multiple loadable modules, and some of them 'depend' on others in a way. Each module can declare simple rules, relative to other modules:</p> <blockquote> <p>Load me before module A</p> <p>Load me after module B</p> <p>Load me before module A but after module B</p> </blockquote> <p>now I need to implement this ordering somehow.. :)</p> <hr> <p>Answer: <a href="http://code.activestate.com/recipes/577413-topological-sort/" rel="noreferrer">code</a> by Paddy McCarthy (MIT)</p> <pre><code>## {{{ http://code.activestate.com/recipes/577413/ (r1) try: from functools import reduce except: pass data = { 'des_system_lib': set('std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee'.split()), 'dw01': set('ieee dw01 dware gtech'.split()), 'dw02': set('ieee dw02 dware'.split()), 'dw03': set('std synopsys dware dw03 dw02 dw01 ieee gtech'.split()), 'dw04': set('dw04 ieee dw01 dware gtech'.split()), 'dw05': set('dw05 ieee dware'.split()), 'dw06': set('dw06 ieee dware'.split()), 'dw07': set('ieee dware'.split()), 'dware': set('ieee dware'.split()), 'gtech': set('ieee gtech'.split()), 'ramlib': set('std ieee'.split()), 'std_cell_lib': set('ieee std_cell_lib'.split()), 'synopsys': set(), } def toposort2(data): for k, v in data.items(): v.discard(k) # Ignore self dependencies extra_items_in_deps = reduce(set.union, data.values()) - set(data.keys()) data.update({item:set() for item in extra_items_in_deps}) while True: ordered = set(item for item,dep in data.items() if not dep) if not ordered: break yield ' '.join(sorted(ordered)) data = {item: (dep - ordered) for item,dep in data.items() if item not in ordered} assert not data, "A cyclic dependency exists amongst %r" % data print ('\n'.join( toposort2(data) )) ## end of http://code.activestate.com/recipes/577413/ }}} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload