connected_components

Function connected_components 

Source
pub fn connected_components<T, S>(
    graph: &S,
    directed: bool,
    connection: &str,
    returnlabels: bool,
) -> SparseResult<(usize, Option<Array1<usize>>)>
where T: Float + Debug + Copy + 'static, S: SparseArray<T>,
Expand description

Find connected components in a graph

§Arguments

  • graph - The graph as a sparse matrix
  • directed - Whether to treat the graph as directed
  • connection - 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);