Skip to main content

is_acyclic

Function is_acyclic 

Source
pub fn is_acyclic(graph: &Graph) -> bool
Expand description

Returns true iff graph contains no cycle.

For directed graphs this is equivalent to crate::is_dag. For undirected graphs, a cycle is any non-trivial closed walk — in particular self-loops and parallel undirected edges count as cycles.

Algorithm:

  • Directed: delegate to is_dag (matches upstream).
  • Undirected: walk every edge; for each (u, v) use union-find to check whether u and v are already connected. If yes, we just closed a cycle ⇒ return false. Otherwise union and continue.

Counterpart of igraph_is_acyclic from references/igraph/src/properties/trees.c:753.

§Examples

use rust_igraph::{Graph, is_acyclic};

// Undirected tree 0-1-2-3: acyclic.
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_acyclic(&g));

// Triangle 0-1-2-0: cyclic.
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();
assert!(!is_acyclic(&g));