Skip to main content

connected_components

Function connected_components 

Source
pub fn connected_components<N>(
    nodes: &[N],
    edges: &[(N, N, f32)],
) -> Vec<(N, usize)>
where N: Clone + Ord,
Expand description

Compute connected components over an abstract graph.

Inputs:

  • nodes: the declared node universe.
  • edges: weighted edges (src, dst, weight). Edges are treated as UNDIRECTED for connectivity. Edge endpoints not present in nodes are still included in the universe (nodes ∪ all edge endpoints).

Output: exactly one (node, island_id) pair per distinct node in the universe.

Determinism (guaranteed and tested):

  • The id universe is built from a BTreeSet, so it is sorted and deduped.
  • Union-find unions toward the smaller index, so each component’s representative is its smallest node.
  • island_ids are assigned in ascending order of each component’s smallest node, yielding 0, 1, 2, ....
  • Output is ordered by node ascending.

Identical input always produces identical output.