Skip to main content

is_triangle_free

Function is_triangle_free 

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

Test whether a graph is triangle-free.

A graph is triangle-free if it contains no 3-clique (cycle of length 3). Edge directions, multi-edges, and self-loops are ignored for the purpose of this check (the underlying simple undirected graph is tested).

Uses sorted-neighbor-intersection with early exit for efficiency.

ยงExamples

use rust_igraph::{Graph, is_triangle_free};

// Path graph: no triangles
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_triangle_free(&g).unwrap());

// Add edge 0-2 to create a triangle
g.add_edge(0, 2).unwrap();
assert!(!is_triangle_free(&g).unwrap());