Skip to main content

connected_components

Function connected_components 

Source
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 analyze
  • visited - Tracker for visited nodes
  • components - Buffer to store the connected components as (slice, size) tuples
  • node_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);