Skip to main content

is_connected

Function is_connected 

Source
pub fn is_connected(
    graph: &Graph,
    mode: ConnectednessMode,
) -> IgraphResult<bool>
Expand description

Tests whether the graph is connected.

For undirected graphs, mode is ignored and the function checks whether all vertices are reachable from each other.

For directed graphs:

  • Weak: checks if the underlying undirected graph is connected.
  • Strong: checks if every vertex is reachable from every other vertex following edge directions.

An empty graph (0 vertices) is considered connected. A graph with a single vertex is always connected.

ยงExamples

use rust_igraph::{Graph, is_connected, ConnectednessMode};

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_connected(&g, ConnectednessMode::Weak).unwrap());

// Disconnected graph
let g = Graph::with_vertices(4);
assert!(!is_connected(&g, ConnectednessMode::Weak).unwrap());

// Directed: weakly but not strongly connected
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
assert!(is_connected(&g, ConnectednessMode::Weak).unwrap());
assert!(!is_connected(&g, ConnectednessMode::Strong).unwrap());