pub fn connected_components<'a, G, NI, VT>(
graph: &G,
visited: &mut VT,
components: &mut [(&'a [NI], usize)],
node_buffer: &'a mut [NI],
) -> Result<usize, AlgorithmError<NI>>where
G: Graph<NI>,
NI: NodeIndex,
VT: VisitedTracker<NI> + ?Sized,
AlgorithmError<NI>: From<G::Error>,Expand description
Find all connected components in an undirected graph
This function identifies all connected components in the graph by performing DFS from each unvisited node. Each connected component is collected into the provided buffer.
§Arguments
graph- The graph to analyzevisited- Tracker for visited nodescomponents- Buffer to store the connected components as (slice, size) tuplesnode_buffer- Temporary buffer for collecting nodes in each component
§Returns
The number of connected components found, or an error if buffers are too small
§Example
let edges = [(0, 1)];
let nodes = [0, 1, 2]; // Explicitly include the isolated node
let graph = EdgeNodeList::new(edges, nodes).unwrap();
let mut visited = [false; 3];
let mut components = [(&[0usize][..], 0usize); 3];
let mut node_buffer = [0usize; 3];
let component_count = connected_components(
&graph,
visited.as_mut_slice(),
&mut components,
&mut node_buffer
).unwrap();
assert_eq!(component_count, 2);