Skip to main content

topological_sorting

Function topological_sorting 

Source
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”:

Self-loops are ignored when computing degrees (matches upstream): a single self-loop with no other in/out edges does not block sorting.

Errors:

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);