rust_dsa

Function find_cycle

Source
pub fn find_cycle<N, E>(graph: &DiGraph<N, E>) -> Option<Vec<&N>>
where N: Hash + Eq,
Expand description

Returns a cycle in the graph, if one exists.

ยงExample

use rust_dsa::{DiGraph, find_cycle};

let no_cycle = DiGraph::from([('a', 'b', ()), ('b', 'c', ())]);
assert!(find_cycle(&no_cycle).is_none());

let with_cycle = DiGraph::from([
    (1, 2, ()),
    (2, 3, ()),
    (3, 4, ()),
    (3, 1, ()),
]);
let cycle = find_cycle(&with_cycle);
assert!(
    cycle == Some(vec![&1, &2, &3])
        || cycle == Some(vec![&2, &3, &1])
        || cycle == Some(vec![&3, &1, &2])
);