pub fn topological_sorting(
graph: &Graph,
mode: DijkstraMode,
) -> IgraphResult<Vec<VertexId>>Expand description
Returns a topological ordering of graph’s vertices.
mode controls the direction of “precedes”:
DijkstraMode::Out(most common): for every edgeu → v,uappears beforevin the result. Vertices with no incoming edges come first.DijkstraMode::In: reversed — for every edgeu → v,vappears beforeu. Vertices with no outgoing edges come first.DijkstraMode::All: rejected withIgraphError::InvalidArgument— topological sorting only makes sense in a directional sense.
Self-loops are ignored when computing degrees (matches upstream): a single self-loop with no other in/out edges does not block sorting.
Errors:
IgraphError::InvalidArgumentif the graph is undirected.IgraphError::InvalidArgumentif the graph contains a non-trivial cycle (a topological ordering does not exist).
Counterpart of igraph_topological_sorting(_, _, mode) from
references/igraph/src/properties/dag.c:54.
§Examples
use rust_igraph::{Graph, topological_sorting, DijkstraMode};
// 0 → 1, 0 → 2, 1 → 3, 2 → 3: a DAG with two valid orderings.
let mut g = Graph::new(4, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 3), (2, 3)]).unwrap();
let order = topological_sorting(&g, DijkstraMode::Out).unwrap();
// 0 must come first; 3 must come last.
assert_eq!(order[0], 0);
assert_eq!(order[3], 3);