Skip to main content

minimum_cycle_basis

Function minimum_cycle_basis 

Source
pub fn minimum_cycle_basis(
    graph: &Graph,
    bfs_cutoff: Option<u32>,
    complete: bool,
) -> IgraphResult<Vec<Vec<u32>>>
Expand description

Compute the minimum weight cycle basis of a graph.

Returns a list of cycles, where each cycle is a Vec<EdgeId> listing the edge IDs forming that cycle. The cycle basis has the smallest possible total weight (sum of cycle lengths for unweighted graphs).

If bfs_cutoff is Some(k), only cycles of length at most 2*k + 1 are guaranteed to be minimum-weight. Longer cycles may still be included to complete the basis if complete is true.

If complete is true, a complete cycle basis spanning the entire cycle space is returned. If false and bfs_cutoff is set, only short cycles are returned (the result may not span the full cycle space).

Edge directions are ignored (the graph is treated as undirected).

§Errors

Returns an error if internal computation fails.

§Examples

use rust_igraph::{Graph, minimum_cycle_basis};

// Triangle: one cycle of length 3.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
let cycles = minimum_cycle_basis(&g, None, true).unwrap();
assert_eq!(cycles.len(), 1);
assert_eq!(cycles[0].len(), 3);

// K4: 3 independent cycles, each of length 3.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(2, 3).unwrap();
let cycles = minimum_cycle_basis(&g, None, true).unwrap();
assert_eq!(cycles.len(), 3);
// All minimum cycles in K4 are triangles (length 3)
for c in &cycles {
    assert_eq!(c.len(), 3);
}