hypergraph_rs/algorithms/connected.rs
1//! Connected components of a hypergraph (`xgi/algorithms/connected.py`).
2//!
3//! Components are extracted by a deterministic node-neighbour BFS (III.7:
4//! ordered by `node_ids()`), while `number_connected_components` delegates the
5//! count to `rustworkx_core::connectivity` on the 1-skeleton — a genuine
6//! rustworkx-core plugin reuse. A test asserts the two agree.
7
8use std::collections::{HashSet, VecDeque};
9
10use rustworkx_core::connectivity::number_connected_components as rx_ncc;
11
12use crate::Hypergraph;
13
14/// BFS the component containing `source`, returning its nodes in `node_ids()`
15/// order. Assumes `source` exists.
16fn bfs_component<N, E, M>(h: &Hypergraph<N, E, M>, source: &str) -> HashSet<String> {
17 let mut seen: HashSet<String> = HashSet::new();
18 let mut queue: VecDeque<String> = VecDeque::new();
19 seen.insert(source.to_string());
20 queue.push_back(source.to_string());
21 // Bounded: each node enters the queue at most once (guarded by `seen`),
22 // so the loop runs at most `num_nodes` times.
23 while let Some(cur) = queue.pop_front() {
24 for nb in h.node_neighbors(&cur).unwrap_or_default() {
25 if seen.insert(nb.clone()) {
26 queue.push_back(nb);
27 }
28 }
29 }
30 seen
31}
32
33/// All connected components, each a node list in `node_ids()` order, the
34/// components themselves ordered by their first-seen node.
35/// XGI parity: `xgi.connected_components(H)` (`connected.py:48-82`).
36pub fn connected_components<N, E, M>(h: &Hypergraph<N, E, M>) -> Vec<Vec<String>> {
37 let node_ids = h.node_ids();
38 let mut seen: HashSet<String> = HashSet::new();
39 let mut components: Vec<Vec<String>> = Vec::new();
40 for v in &node_ids {
41 if !seen.contains(v) {
42 let comp = bfs_component(h, v);
43 seen.extend(comp.iter().cloned());
44 // Emit in node_ids() order for determinism.
45 let ordered: Vec<String> = node_ids
46 .iter()
47 .filter(|n| comp.contains(*n))
48 .cloned()
49 .collect();
50 components.push(ordered);
51 }
52 }
53 components
54}
55
56/// Whether the hypergraph is connected (one component; empty ⇒ trivially so is
57/// undefined — matches XGI, which indexes `list(H.nodes)[0]`, so guard empty).
58/// XGI parity: `xgi.is_connected(H)` (`connected.py:16-45`).
59pub fn is_connected<N, E, M>(h: &Hypergraph<N, E, M>) -> bool {
60 if h.num_nodes() == 0 {
61 return false;
62 }
63 number_connected_components(h) == 1
64}
65
66/// Number of connected components, delegated to rustworkx-core on the skeleton.
67/// XGI parity: `xgi.number_connected_components(H)` (`connected.py:85-121`).
68pub fn number_connected_components<N, E, M>(h: &Hypergraph<N, E, M>) -> usize {
69 let g = h.skeleton();
70 rx_ncc(&g)
71}
72
73/// The connected component containing node `n`, in `node_ids()` order, or
74/// `None` if the node is absent. XGI parity: `node_connected_component`
75/// (`connected.py:154-188`) — XGI raises; we return `None`.
76pub fn node_connected_component<N, E, M>(h: &Hypergraph<N, E, M>, n: &str) -> Option<Vec<String>> {
77 if !h.has_node(n) {
78 return None;
79 }
80 let comp = bfs_component(h, n);
81 Some(
82 h.node_ids()
83 .into_iter()
84 .filter(|x| comp.contains(x))
85 .collect(),
86 )
87}
88
89/// The largest connected component (ties broken by first-seen order).
90/// XGI parity: `largest_connected_component` (`connected.py:124-151`).
91pub fn largest_connected_component<N, E, M>(h: &Hypergraph<N, E, M>) -> Vec<String> {
92 connected_components(h)
93 .into_iter()
94 .max_by_key(|c| c.len())
95 .unwrap_or_default()
96}