Skip to main content

is_tree

Function is_tree 

Source
pub fn is_tree(
    graph: &Graph,
    mode: DijkstraMode,
) -> IgraphResult<Option<VertexId>>
Expand description

Returns Some(root) iff graph is a tree under mode, otherwise None. The null graph (vcount == 0) is not a tree by convention.

For undirected graphs the canonical root is 0. For directed graphs:

Counterpart of igraph_is_tree(_, _, _, mode) from references/igraph/src/properties/trees.c:251.

§Examples

use rust_igraph::{Graph, is_tree, DijkstraMode};

// Undirected path 0-1-2-3: tree, root = 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();
assert_eq!(is_tree(&g, DijkstraMode::All).unwrap(), Some(0));

// Out-arborescence rooted at 0: 0→1, 0→2, 1→3.
let mut g = Graph::new(4, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 3)]).unwrap();
assert_eq!(is_tree(&g, DijkstraMode::Out).unwrap(), Some(0));

// 1→2←3→4 is an undirected forest, but not an out-tree.
let mut g = Graph::new(4, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (2, 1), (2, 3)]).unwrap();
assert_eq!(is_tree(&g, DijkstraMode::Out).unwrap(), None);
// Treated as undirected, it has 3 edges on 4 vertices and is connected ⇒ tree.
assert_eq!(is_tree(&g, DijkstraMode::All).unwrap(), Some(0));