pub fn connected_components<G: GraphRef>(graph: &G) -> Vec<usize>Expand description
Connected components of an undirected graph, using BFS.
For directed graphs: this computes weakly-connected components if the adapter’s neighbor lists include both in- and out-neighbors; otherwise it follows the adapter.
use graphops::{connected_components, GraphRef};
struct G(Vec<Vec<usize>>);
impl GraphRef for G {
fn node_count(&self) -> usize { self.0.len() }
fn neighbors_ref(&self, n: usize) -> &[usize] { &self.0[n] }
}
// Two components: {0,1,2} and {3,4}
let g = G(vec![vec![1], vec![0, 2], vec![1], vec![4], vec![3]]);
let labels = connected_components(&g);
assert_eq!(labels[0], labels[1]);
assert_ne!(labels[0], labels[3]);