Class TopOrder
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
The topological order of a DAG (directed acyclic graph), in which every node comes before its out-nodes.
public static class TopOrder
- Inheritance
-
TopOrder
- Inherited Members
Remarks
Uses a depth-first search, without recursion: Of(Graph) 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 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 Of(Graph) rejects it; IsDag(Graph) tests for a cycle, and FindCycle(Graph) returns one. On a graph with cycles, SccsOf(Graph) puts each cycle into one SCC (strongly connected component) and numbers the SCCs in reverse topological order.
Methods
Of(Graph)
Returns the nodes of graph in topological order: for every edge
u -> v, u comes before v.
public static ImmutableArray<int> Of(Graph graph)
Parameters
graphGraphThe DAG to sort.
Returns
- ImmutableArray<int>
The node IDs in topological order.
Remarks
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.
Exceptions
- ArgumentException
graphhas a cycle, or a depth-first path is longer than 2^30 - 29 nodes.