ruvector_mincut/cluster/
mod.rs

1//! Multi-level Cluster Hierarchy for Dynamic Minimum Cut
2//!
3//! Implements hierarchical clustering from the December 2024 paper.
4//! Enables efficient cut maintenance through recursive decomposition.
5
6pub mod hierarchy;
7
8use crate::graph::{DynamicGraph, VertexId, EdgeId};
9use std::collections::{HashMap, HashSet};
10use std::sync::Arc;
11
12/// A cluster at a specific level in the hierarchy
13#[derive(Debug, Clone)]
14pub struct Cluster {
15    /// Unique cluster ID
16    pub id: u64,
17    /// Level in hierarchy (0 = leaf level)
18    pub level: usize,
19    /// Vertices contained in this cluster
20    pub vertices: HashSet<VertexId>,
21    /// Boundary edges (edges leaving the cluster)
22    pub boundary_edges: Vec<EdgeId>,
23    /// Boundary size (cut value if this cluster were separated)
24    pub boundary_size: u64,
25    /// Parent cluster ID (None for root)
26    pub parent: Option<u64>,
27    /// Child cluster IDs (empty for leaf clusters)
28    pub children: Vec<u64>,
29}
30
31/// Multi-level cluster hierarchy
32pub struct ClusterHierarchy {
33    /// All clusters indexed by ID
34    pub clusters: HashMap<u64, Cluster>,
35    /// Root cluster ID
36    root_id: Option<u64>,
37    /// Number of levels
38    num_levels: usize,
39    /// Vertex to leaf cluster mapping
40    vertex_cluster: HashMap<VertexId, u64>,
41    /// Next cluster ID
42    next_id: u64,
43    /// Reference to graph
44    graph: Arc<DynamicGraph>,
45    /// Target cluster size at each level
46    target_sizes: Vec<usize>,
47}
48
49impl ClusterHierarchy {
50    /// Create a new hierarchy for the given graph
51    pub fn new(graph: Arc<DynamicGraph>) -> Self {
52        let mut hierarchy = Self {
53            clusters: HashMap::new(),
54            root_id: None,
55            num_levels: 0,
56            vertex_cluster: HashMap::new(),
57            next_id: 0,
58            graph,
59            target_sizes: Vec::new(),
60        };
61        hierarchy.rebuild();
62        hierarchy
63    }
64
65    /// Rebuild the entire hierarchy from scratch
66    pub fn rebuild(&mut self) {
67        self.clusters.clear();
68        self.vertex_cluster.clear();
69        self.next_id = 0;
70
71        let vertices = self.graph.vertices();
72        if vertices.is_empty() {
73            self.root_id = None;
74            self.num_levels = 0;
75            return;
76        }
77
78        // Compute number of levels: O(log n)
79        let n = vertices.len();
80        self.num_levels = (n as f64).log2().ceil() as usize + 1;
81
82        // Compute target sizes for each level
83        self.target_sizes = (0..self.num_levels)
84            .map(|l| 2usize.pow(l as u32).min(n))
85            .collect();
86
87        // Build leaf clusters (level 0)
88        let leaf_ids = self.build_leaf_clusters(&vertices);
89
90        // Build upper levels recursively
91        let mut current_level_ids = leaf_ids;
92        for level in 1..self.num_levels {
93            current_level_ids = self.build_level(level, &current_level_ids);
94            if current_level_ids.len() == 1 {
95                self.root_id = Some(current_level_ids[0]);
96                break;
97            }
98        }
99
100        if self.root_id.is_none() && !current_level_ids.is_empty() {
101            self.root_id = Some(current_level_ids[0]);
102        }
103    }
104
105    /// Build leaf clusters (each vertex is its own cluster initially)
106    fn build_leaf_clusters(&mut self, vertices: &[VertexId]) -> Vec<u64> {
107        vertices.iter().map(|&v| {
108            let cluster_id = self.next_id;
109            self.next_id += 1;
110
111            // Compute boundary
112            let (boundary_edges, boundary_size) = self.compute_vertex_boundary(v);
113
114            let cluster = Cluster {
115                id: cluster_id,
116                level: 0,
117                vertices: [v].into_iter().collect(),
118                boundary_edges,
119                boundary_size,
120                parent: None,
121                children: Vec::new(),
122            };
123
124            self.clusters.insert(cluster_id, cluster);
125            self.vertex_cluster.insert(v, cluster_id);
126            cluster_id
127        }).collect()
128    }
129
130    /// Build a level by merging clusters from the previous level
131    fn build_level(&mut self, level: usize, child_ids: &[u64]) -> Vec<u64> {
132        // Group children into parent clusters
133        // Target: reduce number of clusters by factor of 2
134        let _target_count = (child_ids.len() + 1) / 2;
135        let mut parent_ids = Vec::new();
136
137        for chunk in child_ids.chunks(2) {
138            let parent_id = self.next_id;
139            self.next_id += 1;
140
141            // Merge child vertices
142            let mut vertices = HashSet::new();
143            for &child_id in chunk {
144                if let Some(child) = self.clusters.get_mut(&child_id) {
145                    vertices.extend(child.vertices.iter().copied());
146                    child.parent = Some(parent_id);
147                }
148            }
149
150            // Compute boundary for merged cluster
151            let (boundary_edges, boundary_size) = self.compute_cluster_boundary(&vertices);
152
153            let parent = Cluster {
154                id: parent_id,
155                level,
156                vertices,
157                boundary_edges,
158                boundary_size,
159                parent: None,
160                children: chunk.to_vec(),
161            };
162
163            self.clusters.insert(parent_id, parent);
164            parent_ids.push(parent_id);
165        }
166
167        parent_ids
168    }
169
170    /// Compute boundary edges and size for a single vertex
171    fn compute_vertex_boundary(&self, v: VertexId) -> (Vec<EdgeId>, u64) {
172        let mut boundary_edges = Vec::new();
173        let mut boundary_size = 0u64;
174
175        for edge in self.graph.edges() {
176            if edge.source == v || edge.target == v {
177                boundary_edges.push(edge.id);
178                boundary_size += 1;
179            }
180        }
181
182        (boundary_edges, boundary_size)
183    }
184
185    /// Compute boundary edges and size for a cluster
186    fn compute_cluster_boundary(&self, vertices: &HashSet<VertexId>) -> (Vec<EdgeId>, u64) {
187        let mut boundary_edges = Vec::new();
188        let mut boundary_size = 0u64;
189
190        for edge in self.graph.edges() {
191            let src_in = vertices.contains(&edge.source);
192            let tgt_in = vertices.contains(&edge.target);
193
194            // Edge crosses boundary if exactly one endpoint is inside
195            if src_in != tgt_in {
196                boundary_edges.push(edge.id);
197                boundary_size += 1;
198            }
199        }
200
201        (boundary_edges, boundary_size)
202    }
203
204    /// Handle edge insertion
205    pub fn insert_edge(&mut self, _edge_id: EdgeId, u: VertexId, v: VertexId) {
206        // Update boundaries for all clusters containing u or v
207        self.update_boundaries_for_vertices(&[u, v]);
208    }
209
210    /// Handle edge deletion
211    pub fn delete_edge(&mut self, _edge_id: EdgeId, u: VertexId, v: VertexId) {
212        // Update boundaries for all clusters containing u or v
213        self.update_boundaries_for_vertices(&[u, v]);
214    }
215
216    /// Update boundary information for clusters containing given vertices
217    fn update_boundaries_for_vertices(&mut self, vertices: &[VertexId]) {
218        // Find all clusters that need updating (traverse up the hierarchy)
219        let mut clusters_to_update = HashSet::new();
220
221        for &v in vertices {
222            if let Some(&cluster_id) = self.vertex_cluster.get(&v) {
223                let mut current = Some(cluster_id);
224                while let Some(id) = current {
225                    clusters_to_update.insert(id);
226                    current = self.clusters.get(&id).and_then(|c| c.parent);
227                }
228            }
229        }
230
231        // Update each cluster's boundary
232        for cluster_id in clusters_to_update {
233            if let Some(cluster) = self.clusters.get(&cluster_id) {
234                let vertices = cluster.vertices.clone();
235                let (boundary_edges, boundary_size) = self.compute_cluster_boundary(&vertices);
236
237                if let Some(cluster) = self.clusters.get_mut(&cluster_id) {
238                    cluster.boundary_edges = boundary_edges;
239                    cluster.boundary_size = boundary_size;
240                }
241            }
242        }
243    }
244
245    /// Find the smallest cluster containing both vertices
246    pub fn lowest_common_cluster(&self, u: VertexId, v: VertexId) -> Option<u64> {
247        let u_cluster = self.vertex_cluster.get(&u)?;
248        let v_cluster = self.vertex_cluster.get(&v)?;
249
250        // Build path from u to root
251        let mut u_path = HashSet::new();
252        let mut current = Some(*u_cluster);
253        while let Some(id) = current {
254            u_path.insert(id);
255            current = self.clusters.get(&id).and_then(|c| c.parent);
256        }
257
258        // Find first intersection with v's path
259        current = Some(*v_cluster);
260        while let Some(id) = current {
261            if u_path.contains(&id) {
262                return Some(id);
263            }
264            current = self.clusters.get(&id).and_then(|c| c.parent);
265        }
266
267        None
268    }
269
270    /// Get minimum boundary size across all clusters
271    pub fn min_boundary(&self) -> u64 {
272        self.clusters.values()
273            .filter(|c| !c.vertices.is_empty() && c.vertices.len() < self.graph.num_vertices())
274            .map(|c| c.boundary_size)
275            .min()
276            .unwrap_or(u64::MAX)
277    }
278
279    /// Get cluster by ID
280    pub fn get_cluster(&self, id: u64) -> Option<&Cluster> {
281        self.clusters.get(&id)
282    }
283
284    /// Get number of levels
285    pub fn num_levels(&self) -> usize {
286        self.num_levels
287    }
288
289    /// Get root cluster
290    pub fn root(&self) -> Option<&Cluster> {
291        self.root_id.and_then(|id| self.clusters.get(&id))
292    }
293}
294
295#[cfg(test)]
296mod tests {
297    use super::*;
298
299    #[test]
300    fn test_empty_graph() {
301        let graph = Arc::new(DynamicGraph::new());
302        let hierarchy = ClusterHierarchy::new(graph);
303        assert_eq!(hierarchy.num_levels(), 0);
304        assert!(hierarchy.root().is_none());
305    }
306
307    #[test]
308    fn test_single_vertex() {
309        let graph = Arc::new(DynamicGraph::new());
310        graph.add_vertex(1);
311        let hierarchy = ClusterHierarchy::new(graph);
312        assert!(hierarchy.num_levels() >= 1);
313    }
314
315    #[test]
316    fn test_path_graph() {
317        let graph = Arc::new(DynamicGraph::new());
318        for i in 0..9 {
319            graph.insert_edge(i, i+1, 1.0).unwrap();
320        }
321        let hierarchy = ClusterHierarchy::new(graph);
322        assert!(hierarchy.num_levels() > 1);
323        assert_eq!(hierarchy.min_boundary(), 1); // Path has min cut 1
324    }
325
326    #[test]
327    fn test_cycle_graph() {
328        let graph = Arc::new(DynamicGraph::new());
329        for i in 0..5 {
330            graph.insert_edge(i, (i+1) % 5, 1.0).unwrap();
331        }
332        let hierarchy = ClusterHierarchy::new(graph);
333        assert_eq!(hierarchy.min_boundary(), 2); // Cycle has min cut 2
334    }
335
336    #[test]
337    fn test_lowest_common_cluster() {
338        let graph = Arc::new(DynamicGraph::new());
339        graph.insert_edge(0, 1, 1.0).unwrap();
340        graph.insert_edge(1, 2, 1.0).unwrap();
341
342        let hierarchy = ClusterHierarchy::new(graph);
343        let lcc = hierarchy.lowest_common_cluster(0, 2);
344        assert!(lcc.is_some());
345    }
346
347    #[test]
348    fn test_dynamic_update() {
349        let graph = Arc::new(DynamicGraph::new());
350        graph.insert_edge(0, 1, 1.0).unwrap();
351        graph.insert_edge(1, 2, 1.0).unwrap();
352
353        let mut hierarchy = ClusterHierarchy::new(Arc::clone(&graph));
354        let before = hierarchy.min_boundary();
355
356        // Add edge to form cycle
357        let edge_id = graph.insert_edge(0, 2, 1.0).unwrap();
358        hierarchy.insert_edge(edge_id, 0, 2);
359
360        let after = hierarchy.min_boundary();
361        assert!(after >= before); // Adding edge can only increase min cut
362    }
363}