diff --git a/src/undirected/connected_components.rs b/src/undirected/connected_components.rs index 7526ffc2..50d27086 100644 --- a/src/undirected/connected_components.rs +++ b/src/undirected/connected_components.rs @@ -124,18 +124,24 @@ where /// Extract connected components from a graph. /// - /// - `starts` is a collection of vertices to be considered as start points. + /// - `candidates` is the collection of candidate vertices whose neighbour + /// groups should be merged into connected components. /// - `neighbours` is a function returning the neighbours of a given node. /// + /// Only the candidate vertices are queried with `neighbours`. Vertices + /// returned by `neighbours` are added to their candidate vertex group, but + /// their own neighbours are not explored unless they also appear in + /// `candidates`. + /// /// This function returns a list of sets of nodes forming disjoint connected /// sets. - pub fn connected_components(starts: &[N], mut neighbours: FN) -> C2 + pub fn connected_components(candidates: &[N], mut neighbours: FN) -> C2 where FN: FnMut(&N) -> IN, IN: IntoIterator, { ConnectedComponents::, It2, C1, C2, C3>::components( - &starts + &candidates .iter() .map(|s| { neighbours(s) @@ -216,18 +222,24 @@ where /// Extract connected components from a graph. /// -/// - `starts` is a collection of vertices to be considered as start points. +/// - `candidates` is the collection of candidate vertices whose neighbour +/// groups should be merged into connected components. /// - `neighbours` is a function returning the neighbours of a given node. /// +/// Only the candidate vertices are queried with `neighbours`. Vertices +/// returned by `neighbours` are added to their candidate vertex group, but +/// their own neighbours are not explored unless they also appear in +/// `candidates`. +/// /// This function returns a list of sets of nodes forming disjoint connected /// sets. -pub fn connected_components(starts: &[N], neighbours: FN) -> Vec> +pub fn connected_components(candidates: &[N], neighbours: FN) -> Vec> where N: Clone + Hash + Eq, FN: FnMut(&N) -> IN, IN: IntoIterator, { - ConnectedComponents::::connected_components(starts, neighbours) + ConnectedComponents::::connected_components(candidates, neighbours) } /// Locate vertices amongst disjoint sets. diff --git a/tests/connected-components.rs b/tests/connected-components.rs index d230b02c..2cd95dfe 100644 --- a/tests/connected-components.rs +++ b/tests/connected-components.rs @@ -55,7 +55,7 @@ fn empty_components() { } #[test] -fn basic_connected_components() { +fn connected_components_only_queries_candidate_vertices() { let mut counter = 0; let mut c = connected_components(&[1, 4], |&n| { counter += 1; @@ -75,6 +75,7 @@ fn basic_connected_components() { c[1].clone().into_iter().sorted().collect_vec(), vec![2, 4, 6, 8] ); + // Only the candidate vertices are queried for neighbours. assert_eq!(counter, 2); }