title: Topological sort with support for cyclic dependencies
body: <p>Consider the following dependencies (where <code>A --&gt; B</code> means B depends on A, so effectively, A is the 'parent')</p>
  
  <pre><code>A --&gt; B
  A --&gt; C
  C --&gt; D
  C --&gt; E
  </code></pre>
  
  <p>More graphically:</p>
  
  <pre><code>    A
      |
  ----------
  |        |
  B        C
           |
      -----------
      |         |
      D         E
  </code></pre>
  
  <p>A topological sort algorithm would return something like:</p>
  
  <pre><code>ABCDE
  </code></pre>
  
  <p>I have found code for this (<a href="http://www.universalthread.com/ViewPageNewDownload.aspx?ID=37199" rel="noreferrer">exhibit A</a> and <a href="http://tawani.blogspot.be/2009/02/topological-sorting-and-cyclic.html" rel="noreferrer">exhibit B</a>), but neither support cyclice dependencies. I am in the situtation that this could happen:</p>
  
  <pre><code>A --&gt; B
  B --&gt; C
  B --&gt; D
  C --&gt; B
  C --&gt; E
  </code></pre>
  
  <p>Graphically:</p>
  
  <pre><code>A
  |
  B &lt;--&gt; C
  |      |
  D      E
  </code></pre>
  
  <p>This could return <code>ABCDE</code> or <code>ACBDE</code>. So because B and C are on the same 'level', the order between them is not important (likewise for D and E).</p>
  
  <p>How could I accomplish such a thing. I realize this isn't exactly a topological sorting, but I'm not expert mathematician, so I don't really know where to start looking, let alone how to implement this.</p>
  
  <p>Personally, I'm working in C#, but if you know how to do it in any other language, I'd be happy to study your code and translate it to C#.</p>
  
  <p><strong>update</strong></p>
  
  <p>I can also have the following situation:</p>
  
  <pre><code>A &lt;--------
  |         |
  --&gt; B --&gt; C
      |     |
      D     E
  </code></pre>
  
  <p>So, <strong>important</strong>, this doesn't have to be a tree. I can have any arbitrary graph. In fact, not all nodes have to be connected to one another.</p>