Skip to main content

simple_cycles

Function simple_cycles 

Source
pub fn simple_cycles(
    graph: &Graph,
    mode: SimpleCycleMode,
    min_length: u32,
    max_length: Option<u32>,
    max_results: Option<usize>,
) -> IgraphResult<Vec<SimpleCycle>>
Expand description

Finds all simple cycles in the graph using Johnson’s algorithm.

Returns cycles with length in [min_length, max_length]. If max_results is Some(n), at most n cycles are returned.

For undirected graphs, mode is ignored. Each undirected cycle is reported once (the canonical orientation has the first edge id smaller than the closing edge id).

§Errors

Returns IgraphError::InvalidArgument if min_length < 1.

§Examples

use rust_igraph::{Graph, simple_cycles, SimpleCycleMode};

// Directed triangle: 0→1→2→0
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();

let cycles = simple_cycles(&g, SimpleCycleMode::Out, 1, None, None).unwrap();
assert_eq!(cycles.len(), 1);
assert_eq!(cycles[0].vertices, vec![0, 1, 2]);
assert_eq!(cycles[0].edges, vec![0, 1, 2]);