The number of levels, or 0 for a graph without nodes (Java: levelCount()). It equals the
number of nodes on a longest path.
Time: O(1).
The level of every node, by node ID; not to be modified (Java: levels(), an unmodifiable
list).
Time: O(1).
The nodes in topological order, level by level: for every edge u -> v, u comes before v;
not to be modified (Java: order(), an unmodifiable list).
Time: O(1).
StaticofReturns the topological order and the levels of graph.
On a BiAdjacentGraph, it reads the in-degrees instead of counting them in a pass over the edges.
Time: O(nodeCount + edgeCount). Memory: 8 x nodeCount bytes for the result; while it runs, 8 x nodeCount bytes more.
the DAG to sort
the order and the levels
A topological order of a DAG (directed acyclic graph) with the level of each node. A root node has level 0, and every other node has one more than the highest level of its in-nodes.
The level of a node is the number of edges on a longest path to it from a root node, so no path connects two nodes of the same level. TopOrderAndLevels.order lists the nodes level by level, TopOrderAndLevels.levelOf and TopOrderAndLevels.levels return their levels, and TopOrderAndLevels.levelCount the number of levels.
Uses Kahn's algorithm: TopOrderAndLevels.of starts with the root nodes in ascending order, places a node once all its in-nodes are placed and takes the out-nodes of each node in ascending order, so a graph always gets the same order. For the order alone, TopOrder.of needs less memory.
Instances are immutable.
Memory: 8 x nodeCount bytes.