Skip to main content

Module top_order

Module top_order 

Source
Expand description

The topological order of a DAG (directed acyclic graph), in which every node comes before its out-nodes - Java’s class TopOrder.

Uses a depth-first search, without recursion: 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.

use graphity_core::{top_order, GraphBuilder};

let mut b = GraphBuilder::new();
b.add_nodes(4);
b.add_edge_pairs(&[0, 2, 0, 1, 1, 3, 2, 3]);
assert_eq!(top_order::of(&b.build()).unwrap(), [0, 2, 1, 3]);

A graph with a cycle has no topological order, and of rejects it; graphs::is_dag tests for a cycle, and graphs::find_cycle returns one. On a graph with cycles, components::sccs_of puts each cycle into one SCC (strongly connected component) and numbers the SCCs in reverse topological order.

Functions§

of
Returns the nodes of graph in topological order: for every edge u -> v, u comes before v.