Skip to main content

is_lobster

Function is_lobster 

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

Check whether a graph is a lobster tree.

A lobster is a tree where every vertex is within distance 2 of a central path. Equivalently, removing all leaves twice yields a path (or empty/single vertex).

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

§Examples

use rust_igraph::{Graph, is_lobster};

// Star is a lobster (it's even a caterpillar)
let mut g = Graph::with_vertices(5);
for i in 1..5u32 {
    g.add_edge(0, i).unwrap();
}
assert!(is_lobster(&g).unwrap());

// Caterpillar with secondary branches → lobster
let mut g = Graph::with_vertices(8);
g.add_edge(0, 1).unwrap(); // spine
g.add_edge(1, 2).unwrap(); // spine
g.add_edge(0, 3).unwrap(); // leaf off spine
g.add_edge(1, 4).unwrap(); // branch
g.add_edge(4, 5).unwrap(); // leaf off branch (distance 2 from spine)
g.add_edge(2, 6).unwrap(); // branch
g.add_edge(6, 7).unwrap(); // leaf off branch
assert!(is_lobster(&g).unwrap());