Skip to main content

is_cycle

Function is_cycle 

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

Check whether a graph is a cycle graph.

A cycle graph C_n has n ≥ 3 vertices, each with degree exactly 2, forming a single Hamiltonian cycle. The graph must be connected and simple.

Returns false for directed graphs, graphs with fewer than 3 vertices, disconnected graphs, and graphs with any vertex not having degree 2.

§Examples

use rust_igraph::{Graph, is_cycle};

// C_4: 0-1-2-3-0
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 0).unwrap();
assert!(is_cycle(&g).unwrap());

// P_4 is NOT a cycle (endpoints have degree 1)
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
assert!(!is_cycle(&g).unwrap());