ConstReturns the nodes of graph in topological order: for every edge u -> v, u comes before
v.
Time: O(nodeCount + edgeCount). Memory: 4 x nodeCount bytes for the result; while it runs, 2 bits per node and up to 8 x nodeCount bytes for the stack, which grows with the deepest path.
the DAG to sort
the node IDs in topological order; a new array that must not be modified (Java: unmodifiable)
The topological order of a DAG (directed acyclic graph), in which every node comes before its out-nodes.
Uses a depth-first search, without recursion: TopOrder.of returns its reverse postorder. The search starts at the nodes in ascending order and takes the out-nodes of each node in ascending order, as DfsTraverser.run does, so a graph always gets the same order. TopOrderAndLevels returns another topological order, together with a level per node.
A graph with a cycle has no topological order, and TopOrder.of rejects it; Graphs.isDag tests for a cycle, and Graphs.findCycle returns one. On a graph with cycles, Components.sccsOf puts each cycle into one SCC (strongly connected component) and numbers the SCCs in reverse topological order.