pub fn connected_components<T, S>(
graph: &S,
directed: bool,
connection: &str,
returnlabels: bool,
) -> SparseResult<(usize, Option<Array1<usize>>)>
Expand description
Find connected components in a graph
§Arguments
graph
- The graph as a sparse matrixdirected
- Whether to treat the graph as directedconnection
- Type of connectivity for directed graphs (“weak” or “strong”)returnlabels
- Whether to return component labels for each vertex
§Returns
A tuple containing:
- Number of connected components
- Optional array of component labels for each vertex
§Examples
use scirs2_sparse::csgraph::connected_components;
use scirs2_sparse::csr_array::CsrArray;
// Create a graph with two components
let rows = vec![0, 1, 2, 3];
let cols = vec![1, 0, 3, 2];
let data = vec![1.0, 1.0, 1.0, 1.0];
let graph = CsrArray::from_triplets(&rows, &cols, &data, (4, 4), false).unwrap();
let (n_components, labels) = connected_components(&graph, false, "weak", true).unwrap();
assert_eq!(n_components, 2);