Skip to main content

is_caterpillar

Function is_caterpillar 

Source
pub fn is_caterpillar(graph: &Graph) -> IgraphResult<bool>
Expand description

Check whether a graph is a caterpillar tree.

Returns false for directed graphs and non-tree graphs. Returns true for the empty graph, single vertex, single edge, paths, and stars (all are caterpillars).

§Examples

use rust_igraph::{Graph, is_caterpillar};

// Star `K_{1,4}` is a caterpillar (spine is just the center)
let mut g = Graph::with_vertices(5);
for i in 1..5u32 {
    g.add_edge(0, i).unwrap();
}
assert!(is_caterpillar(&g).unwrap());

// Spider (3 legs of length 2 from a center) is NOT a caterpillar
// Center 0 → 1→4, 2→5, 3→6
let mut g = Graph::with_vertices(7);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 4).unwrap();
g.add_edge(2, 5).unwrap();
g.add_edge(3, 6).unwrap();
assert!(!is_caterpillar(&g).unwrap());