ruvector-mincut 2.0.6

World's first subpolynomial dynamic min-cut: self-healing networks, AI optimization, real-time graph analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Stub implementation of ProperCutInstance
//!
//! Brute-force reference implementation for testing.
//! Recomputes minimum cut on every query - O(2^n) worst case.
//! Only suitable for small graphs (n < 20).

use super::witness::WitnessHandle;
use super::{InstanceResult, ProperCutInstance};
use crate::graph::{DynamicGraph, EdgeId, VertexId};
use roaring::RoaringBitmap;
use std::collections::{HashMap, HashSet, VecDeque};

/// Stub instance that does brute-force min cut computation
///
/// This implementation:
/// - Stores a local copy of all edges
/// - Enumerates all proper subsets on each query
/// - Checks connectivity via BFS
/// - Computes exact boundary values
///
/// # Performance
///
/// - Query: O(2^n ยท m) where n = vertices, m = edges
/// - Only practical for n < 20
///
/// # Purpose
///
/// Used as a reference implementation to test the wrapper logic
/// before the real LocalKCut algorithm is ready.
pub struct StubInstance {
    /// Lambda bounds
    lambda_min: u64,
    lambda_max: u64,
    /// Local copy of edges for computation
    edges: Vec<(VertexId, VertexId, EdgeId)>,
    /// Vertex set
    vertices: HashSet<VertexId>,
    /// Adjacency list: vertex -> [(neighbor, edge_id), ...]
    adjacency: HashMap<VertexId, Vec<(VertexId, EdgeId)>>,
}

impl StubInstance {
    /// Create a new stub instance with initial graph state
    ///
    /// This is used for direct testing. The wrapper should use `init()` instead.
    pub fn new(graph: &DynamicGraph, lambda_min: u64, lambda_max: u64) -> Self {
        let mut instance = Self {
            lambda_min,
            lambda_max,
            edges: Vec::new(),
            vertices: HashSet::new(),
            adjacency: HashMap::new(),
        };

        // Copy initial graph state
        for edge in graph.edges() {
            instance.vertices.insert(edge.source);
            instance.vertices.insert(edge.target);
            instance.edges.push((edge.source, edge.target, edge.id));
        }

        instance.rebuild_adjacency();
        instance
    }

    /// Create an empty stub instance for use with the wrapper
    ///
    /// The wrapper will apply all edges via apply_inserts/apply_deletes.
    pub fn new_empty(lambda_min: u64, lambda_max: u64) -> Self {
        Self {
            lambda_min,
            lambda_max,
            edges: Vec::new(),
            vertices: HashSet::new(),
            adjacency: HashMap::new(),
        }
    }

    /// Compute minimum cut via brute-force enumeration
    ///
    /// For each non-empty proper subset S:
    /// 1. Check if S is connected
    /// 2. If connected, compute boundary size
    /// 3. Track minimum
    ///
    /// Returns None if graph is empty or disconnected.
    fn compute_min_cut(&self) -> Option<(u64, WitnessHandle)> {
        if self.vertices.is_empty() {
            return None;
        }

        let n = self.vertices.len();
        if n == 1 {
            // Single vertex: no proper cuts
            return None;
        }

        // Stub instance only works for small graphs to avoid overflow
        // For large graphs, we return a large value to signal AboveRange
        if n >= 20 {
            // Return a large value that will trigger AboveRange
            return None;
        }

        // Check if graph is connected
        if !self.is_connected() {
            // Disconnected graph has min cut 0
            let membership =
                RoaringBitmap::from_iter(self.vertices.iter().take(1).map(|&v| v as u32));
            let seed = *self.vertices.iter().next().unwrap();
            let witness = WitnessHandle::new(seed, membership, 0);
            return Some((0, witness));
        }

        let vertex_vec: Vec<VertexId> = self.vertices.iter().copied().collect();
        let mut min_cut = u64::MAX;
        let mut best_set = HashSet::new();

        // Enumerate all non-empty proper subsets (2^n - 2 subsets)
        // We use bitmasks from 1 to 2^n - 2
        let max_mask = 1u64 << n;

        for mask in 1..max_mask - 1 {
            // Build subset from bitmask
            let mut subset = HashSet::new();
            for (i, &vertex) in vertex_vec.iter().enumerate() {
                if mask & (1 << i) != 0 {
                    subset.insert(vertex);
                }
            }

            // Check if subset is connected
            if !self.is_connected_set(&subset) {
                continue;
            }

            // Compute boundary
            let (boundary_value, _boundary_edges) = self.compute_boundary(&subset);

            if boundary_value < min_cut {
                min_cut = boundary_value;
                best_set = subset.clone();
            }
        }

        if min_cut == u64::MAX {
            // No proper connected cuts found (shouldn't happen for connected graphs)
            return None;
        }

        // Build witness using new API
        // Convert HashSet to RoaringBitmap (u32)
        let membership: RoaringBitmap = best_set.iter().map(|&v| v as u32).collect();

        // Use first vertex in set as seed
        let seed = *best_set.iter().next().unwrap();

        let witness = WitnessHandle::new(seed, membership, min_cut);

        Some((min_cut, witness))
    }

    /// Check if a subset of vertices is connected
    ///
    /// Uses BFS within the subset to check connectivity.
    fn is_connected_set(&self, vertices: &HashSet<VertexId>) -> bool {
        if vertices.is_empty() {
            return true;
        }

        // Start BFS from arbitrary vertex in the set
        let start = *vertices.iter().next().unwrap();
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();

        queue.push_back(start);
        visited.insert(start);

        while let Some(current) = queue.pop_front() {
            if let Some(neighbors) = self.adjacency.get(&current) {
                for &(neighbor, _edge_id) in neighbors {
                    // Only follow edges within the subset
                    if vertices.contains(&neighbor) && visited.insert(neighbor) {
                        queue.push_back(neighbor);
                    }
                }
            }
        }

        // Connected if we visited all vertices in the subset
        visited.len() == vertices.len()
    }

    /// Compute boundary of a vertex set
    ///
    /// Returns (boundary_value, boundary_edges).
    /// Boundary = edges with exactly one endpoint in the set.
    fn compute_boundary(&self, set: &HashSet<VertexId>) -> (u64, Vec<EdgeId>) {
        let mut boundary_value = 0u64;
        let mut boundary_edges = Vec::new();

        for &(u, v, edge_id) in &self.edges {
            let u_in_set = set.contains(&u);
            let v_in_set = set.contains(&v);

            // Edge is in boundary if exactly one endpoint is in set
            if u_in_set != v_in_set {
                boundary_value += 1;
                boundary_edges.push(edge_id);
            }
        }

        (boundary_value, boundary_edges)
    }

    /// Check if entire graph is connected
    fn is_connected(&self) -> bool {
        self.is_connected_set(&self.vertices)
    }

    /// Rebuild adjacency list from edges
    fn rebuild_adjacency(&mut self) {
        self.adjacency.clear();

        for &(u, v, edge_id) in &self.edges {
            self.adjacency
                .entry(u)
                .or_insert_with(Vec::new)
                .push((v, edge_id));

            self.adjacency
                .entry(v)
                .or_insert_with(Vec::new)
                .push((u, edge_id));
        }
    }

    fn insert(&mut self, edge_id: EdgeId, u: VertexId, v: VertexId) {
        // Add edge to local copy
        self.vertices.insert(u);
        self.vertices.insert(v);
        self.edges.push((u, v, edge_id));
        self.rebuild_adjacency();
    }

    fn delete(&mut self, edge_id: EdgeId, _u: VertexId, _v: VertexId) {
        // Remove edge from local copy
        self.edges.retain(|(_, _, eid)| *eid != edge_id);
        self.rebuild_adjacency();
    }
}

impl ProperCutInstance for StubInstance {
    fn init(_graph: &DynamicGraph, lambda_min: u64, lambda_max: u64) -> Self {
        // For wrapper use: start empty, wrapper will call apply_inserts
        Self::new_empty(lambda_min, lambda_max)
    }

    fn apply_inserts(&mut self, edges: &[(EdgeId, VertexId, VertexId)]) {
        for &(edge_id, u, v) in edges {
            self.insert(edge_id, u, v);
        }
    }

    fn apply_deletes(&mut self, edges: &[(EdgeId, VertexId, VertexId)]) {
        for &(edge_id, u, v) in edges {
            self.delete(edge_id, u, v);
        }
    }

    fn query(&mut self) -> InstanceResult {
        match self.compute_min_cut() {
            Some((value, witness)) if value <= self.lambda_max => {
                InstanceResult::ValueInRange { value, witness }
            }
            _ => InstanceResult::AboveRange,
        }
    }

    fn bounds(&self) -> (u64, u64) {
        (self.lambda_min, self.lambda_max)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::DynamicGraph;

    #[test]
    fn test_empty_graph() {
        let graph = DynamicGraph::new();
        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        assert!(matches!(result, InstanceResult::AboveRange));
    }

    #[test]
    fn test_single_vertex() {
        let graph = DynamicGraph::new();
        graph.add_vertex(1);

        let mut instance = StubInstance::new(&graph, 0, 10);
        let result = instance.query();
        assert!(matches!(result, InstanceResult::AboveRange));
    }

    #[test]
    fn test_path_graph() {
        // Path: 1 - 2 - 3
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => {
                // Min cut of path is 1
                assert_eq!(value, 1);
            }
            _ => panic!("Expected ValueInRange result"),
        }
    }

    #[test]
    fn test_cycle_graph() {
        // Cycle: 1 - 2 - 3 - 1
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();
        graph.insert_edge(3, 1, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => {
                // Min cut of cycle is 2
                assert_eq!(value, 2);
            }
            _ => panic!("Expected ValueInRange result"),
        }
    }

    #[test]
    fn test_complete_graph_k4() {
        // Complete graph K4
        let graph = DynamicGraph::new();
        for i in 1..=4 {
            for j in i + 1..=4 {
                graph.insert_edge(i, j, 1.0).unwrap();
            }
        }

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => {
                // Min cut of K4 is 3 (minimum degree)
                assert_eq!(value, 3);
            }
            _ => panic!("Expected ValueInRange result"),
        }
    }

    #[test]
    fn test_disconnected_graph() {
        // Two separate edges: 1-2 and 3-4
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(3, 4, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => {
                // Disconnected graph has min cut 0
                assert_eq!(value, 0);
            }
            _ => panic!("Expected ValueInRange with value 0"),
        }
    }

    #[test]
    fn test_bridge_graph() {
        // Two triangles connected by a bridge
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();
        graph.insert_edge(3, 1, 1.0).unwrap();
        graph.insert_edge(3, 4, 1.0).unwrap(); // Bridge
        graph.insert_edge(4, 5, 1.0).unwrap();
        graph.insert_edge(5, 6, 1.0).unwrap();
        graph.insert_edge(6, 4, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => {
                // Min cut is the bridge (value = 1)
                assert_eq!(value, 1);
            }
            _ => panic!("Expected ValueInRange result"),
        }
    }

    #[test]
    fn test_is_connected_set() {
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();
        graph.insert_edge(3, 4, 1.0).unwrap();

        let instance = StubInstance::new(&graph, 0, 10);

        // Test connected subset
        let mut subset = HashSet::new();
        subset.insert(1);
        subset.insert(2);
        subset.insert(3);
        assert!(instance.is_connected_set(&subset));

        // Test disconnected subset (1 and 4 not directly connected)
        let mut subset = HashSet::new();
        subset.insert(1);
        subset.insert(4);
        assert!(!instance.is_connected_set(&subset));

        // Test single vertex (always connected)
        let mut subset = HashSet::new();
        subset.insert(1);
        assert!(instance.is_connected_set(&subset));
    }

    #[test]
    fn test_compute_boundary() {
        let graph = DynamicGraph::new();
        let e1 = graph.insert_edge(1, 2, 1.0).unwrap();
        let e2 = graph.insert_edge(2, 3, 1.0).unwrap();
        let _e3 = graph.insert_edge(3, 4, 1.0).unwrap();

        let instance = StubInstance::new(&graph, 0, 10);

        // Boundary of {1, 2}
        let mut set = HashSet::new();
        set.insert(1);
        set.insert(2);
        let (value, edges) = instance.compute_boundary(&set);
        assert_eq!(value, 1); // Only edge 2-3 crosses
        assert_eq!(edges.len(), 1);
        assert!(edges.contains(&e2));

        // Boundary of {2}
        let mut set = HashSet::new();
        set.insert(2);
        let (value, edges) = instance.compute_boundary(&set);
        assert_eq!(value, 2); // Edges 1-2 and 2-3 cross
        assert_eq!(edges.len(), 2);
        assert!(edges.contains(&e1));
        assert!(edges.contains(&e2));
    }

    #[test]
    fn test_dynamic_updates() {
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        // Initial min cut (path: 1-2-3) is 1
        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => assert_eq!(value, 1),
            _ => panic!("Expected ValueInRange"),
        }

        // Insert edge to form cycle
        let e3_id = 100; // Mock edge ID
        instance.apply_inserts(&[(e3_id, 3, 1)]);

        // Now min cut (cycle: 1-2-3-1) is 2
        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => assert_eq!(value, 2),
            _ => panic!("Expected ValueInRange"),
        }

        // Delete one edge to get back to path
        instance.apply_deletes(&[(e3_id, 3, 1)]);

        // Min cut should be 1 again
        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => assert_eq!(value, 1),
            _ => panic!("Expected ValueInRange"),
        }
    }

    #[test]
    fn test_range_bounds() {
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();

        // Instance with range [2, 5]
        let mut instance = StubInstance::new(&graph, 2, 5);

        // Min cut is 1, which is below range [2,5], but stub only checks <= lambda_max
        // so it returns ValueInRange
        let result = instance.query();
        // Stub doesn't check lambda_min, so behavior depends on implementation

        // Instance with range [0, 1]
        let mut instance = StubInstance::new(&graph, 0, 1);

        // Min cut is 1, which is in range
        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, .. } => assert_eq!(value, 1),
            _ => panic!("Expected ValueInRange"),
        }

        // Instance with range [0, 0]
        let mut instance = StubInstance::new(&graph, 0, 0);

        // Min cut is 1, which is above range
        let result = instance.query();
        assert!(matches!(result, InstanceResult::AboveRange));
    }

    #[test]
    fn test_witness_information() {
        let graph = DynamicGraph::new();
        graph.insert_edge(1, 2, 1.0).unwrap();
        graph.insert_edge(2, 3, 1.0).unwrap();

        let mut instance = StubInstance::new(&graph, 0, 10);

        let result = instance.query();
        match result {
            InstanceResult::ValueInRange { value, witness } => {
                assert_eq!(value, 1);
                assert_eq!(witness.boundary_size(), 1);
                assert!(witness.cardinality() > 0);
                assert!(witness.cardinality() < 3); // Proper cut
            }
            _ => panic!("Expected ValueInRange with witness"),
        }
    }
}