Skip to main content

is_dag

Function is_dag 

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

Returns true iff graph is a directed acyclic graph.

Undirected graphs return false unconditionally (matches upstream — DAGs are directed by definition). Empty graphs and graphs with isolated vertices but no edges return true.

Algorithm: Kahn’s topological-sort peel. Start with vertices whose in-degree is 0 in a queue; pop one, “remove” it by decrementing the in-degree of its out-neighbours. If any neighbour reaches 0, queue it. A self-loop is short-circuited to false because the loop’s tail can never have its in-degree reduced past itself.

Counterpart of igraph_is_dag from references/igraph/src/properties/dag.c:151.

§Examples

use rust_igraph::{Graph, is_dag};

// Linear chain 0 → 1 → 2 — a DAG.
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
assert!(is_dag(&g));

// Cycle 0 → 1 → 2 → 0 — not a DAG.
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
assert!(!is_dag(&g));