rust_dsa

Function is_topological_sort

Source
pub fn is_topological_sort<N, E>(graph: &DiGraph<N, E>, sort: &[&N]) -> bool
where N: Hash + Eq,
Expand description

Returns true if the ordering is a topological sort for the graph.

Returns false if the ordering is not a topological sort or if the graph contains cycles.

ยงExample

use rust_dsa::{DiGraph, is_topological_sort};

//    +---+      +---+      +---+
//    |'a'| ---> |'b'| ---> |'c'|
//    +---+      +---+      +---+
let graph = DiGraph::from([('a', 'b', ()), ('b', 'c', ())]);

assert!(is_topological_sort(
    &graph,
    &[&'a', &'b', &'c']
));

assert!(!is_topological_sort(
    &graph,
    &[&'b', &'a', &'c']
));

assert!(!is_topological_sort(
    &graph,
    &[&'a', &'b']
));