<p>There are some papers on incrementally maintaining the topological order of nodes in a graph, with variations on the algorithm you describe.  </p>

<p>If the graph has <code>n</code> nodes and <code>m</code> edges, you spend time <code>O(m + n)</code> every time you insert an edge. 
The papers ask how much time will it take to insert <code>k</code> edges? Trivially, <code>O(k * (n + m))</code>. But in fact you can show much better upper bounds - something like <code>O(k * sqrt(m + n))</code> for large enough <code>k</code>.</p>

<p>Some links below, there are more:</p>

<p><a href="http://igitur-archive.library.uu.nl/math/2007-0725-201647/2005-011.pdf" rel="nofollow noreferrer">http://igitur-archive.library.uu.nl/math/2007-0725-201647/2005-011.pdf</a></p>

<p><a href="http://arxiv.org/abs/0802.1059" rel="nofollow noreferrer">http://arxiv.org/abs/0802.1059</a></p>

<p><a href="http://www.siam.org/proceedings/soda/2009/SODA09_120_benderm.pdf" rel="nofollow noreferrer">http://www.siam.org/proceedings/soda/2009/SODA09_120_benderm.pdf</a></p>
How this DAG topological restructuring can be called?
<p>I had interest in Direct Acyclic Graphs (DAG) for a long time and after reading about Topological sort at Wikipedia, I did not find any special mentioning of an approach involving <strong>layers numbering</strong> (although layers are extensively mentioned for drawing). With this approach the graph is not technically topologically sorted, but knowing that every node contains the correct number for layer (level), we always can tell whether a particular node "bigger" than another topologically. On the other side as long as we don't have an ordered list, we can not enumerate the nodes topologically (although this can be done with a final conventional sort that compares the levels of the nodes). </p>

<p>This approach allows implementing arbitrary connecting while maintaining the correctness of levels information. The steps can be:</p>

<ul>
<li>For any newly added node (without any connection) the level applied is 1. </li>
<li>If a connection between two nodes is requested (from m to n) and the n.level > m.level then they are just simply being connected, no level fixing for other nodes is required. </li>
<li>If for requested connection (from m to n) n.level&lt;=m.level then we change n.level to (m.level + 1) and recursively check any dependencies of n for similar level increase (or no increase if the levels on a recursive step are compatible). </li>
<li>If we keep the list of recursively entered nodes then we can detect an attempt to cycle and implement some kind of undo operation (returning the levels of all affected nodes to previous values)</li>
</ul>

<p>For any set of known nodes and connections between them, we just add all nodes applying level=1 to them and just try to apply all known connections between (ignoring and undoing cicles). </p>

<p>The final level information not only allows comparing nodes topologically, but contains other useful information. For example: </p>

<ul>
<li>every node with level = 1 has no incoming connections (every path starts from one of them). So any DAG enumeration can be started from them.  </li>
<li>The longest path (the number of edges) can not be longer than the (largest level + 1) </li>
</ul>

<p>I suppose that for some artificial data (n nodes, every Node(n) connected to Node(n + 1)) this algorithm can be very slow. But for real-world data I tried it with (Wikipedia categories - 800,000 nodes - 2,000,000 connections) the time is decent (5-10 minutes) and the number of levels and cycle attempts is low (369 levels, 1000 cycle attempts)</p>

<p>So is this method new or is well-known, but just not widely presented in Wikipedia and other resources? Since it's not a sort (technically), should it be called a data restructuring?</p>