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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Dynamic Connectivity for minimum cut wrapper
//!
//! Hybrid implementation using Euler Tour Trees with union-find fallback.
//! Provides O(log n) operations for insertions and queries.
//!
//! # Overview
//!
//! This module provides dynamic connectivity data structures:
//!
//! - [`DynamicConnectivity`]: Euler Tour Tree backend with union-find fallback
//!   - Edge insertions in O(log n) time
//!   - Edge deletions via full rebuild in O(m·α(n)) time
//!   - Connectivity queries in O(log n) time
//!
//! - [`PolylogConnectivity`]: Polylogarithmic worst-case connectivity (arXiv:2510.08297)
//!   - Edge insertions in O(log³ n) expected worst-case
//!   - Edge deletions in O(log³ n) expected worst-case
//!   - Connectivity queries in O(log n) worst-case
//!
//! # Implementation
//!
//! The primary backend uses Euler Tour Trees for O(log n) operations.
//! Falls back to union-find rebuild for deletions until full ETT cut is implemented.
//!
//! The polylog backend uses a hierarchy of O(log n) levels with edge sparsification
//! via low-congestion shortcuts for guaranteed worst-case bounds.

pub mod cache_opt;
pub mod polylog;

use crate::euler::EulerTourTree;
use crate::graph::VertexId;
use std::collections::{HashMap, HashSet};

/// Dynamic connectivity data structure with Euler Tour Tree backend
///
/// Maintains connected components of an undirected graph with support for
/// edge insertions and deletions. Uses Euler Tour Trees for O(log n) operations
/// with union-find fallback for robustness.
///
/// # Examples
///
/// ```ignore
/// let mut dc = DynamicConnectivity::new();
/// dc.add_vertex(0);
/// dc.add_vertex(1);
/// dc.add_vertex(2);
///
/// dc.insert_edge(0, 1);
/// assert!(dc.connected(0, 1));
/// assert!(!dc.connected(0, 2));
///
/// dc.insert_edge(1, 2);
/// assert!(dc.is_connected()); // All vertices connected
///
/// dc.delete_edge(1, 2);
/// assert!(!dc.connected(0, 2));
/// ```
#[derive(Debug, Clone)]
pub struct DynamicConnectivity {
    /// Union-find parent array
    parent: HashMap<VertexId, VertexId>,

    /// Union-find rank array for union by rank
    rank: HashMap<VertexId, usize>,

    /// Current edge set for rebuild on deletions
    /// Edges normalized so smaller vertex is always first
    edges: HashSet<(VertexId, VertexId)>,

    /// Number of vertices
    vertex_count: usize,

    /// Number of connected components
    component_count: usize,

    /// Euler Tour Tree for O(log n) operations
    ett: EulerTourTree,

    /// Whether ETT is in sync with union-find
    ett_synced: bool,
}

impl DynamicConnectivity {
    /// Creates a new empty dynamic connectivity structure
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let dc = DynamicConnectivity::new();
    /// assert_eq!(dc.component_count(), 0);
    /// ```
    pub fn new() -> Self {
        Self {
            parent: HashMap::new(),
            rank: HashMap::new(),
            edges: HashSet::new(),
            vertex_count: 0,
            component_count: 0,
            ett: EulerTourTree::new(),
            ett_synced: true,
        }
    }

    /// Adds a vertex to the connectivity structure
    ///
    /// If the vertex already exists, this is a no-op.
    /// Each new vertex starts in its own component.
    ///
    /// # Arguments
    ///
    /// * `v` - The vertex ID to add
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let mut dc = DynamicConnectivity::new();
    /// dc.add_vertex(0);
    /// assert_eq!(dc.component_count(), 1);
    /// ```
    pub fn add_vertex(&mut self, v: VertexId) {
        if !self.parent.contains_key(&v) {
            self.parent.insert(v, v);
            self.rank.insert(v, 0);
            self.vertex_count += 1;
            self.component_count += 1;

            // Add to Euler Tour Tree (O(log n))
            let _ = self.ett.make_tree(v);
        }
    }

    /// Inserts an edge between two vertices
    ///
    /// Automatically adds vertices if they don't exist.
    /// If vertices are already connected, updates internal state but
    /// doesn't change connectivity.
    ///
    /// # Arguments
    ///
    /// * `u` - First vertex
    /// * `v` - Second vertex
    ///
    /// # Time Complexity
    ///
    /// O(log n) via Euler Tour Tree link operation
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let mut dc = DynamicConnectivity::new();
    /// dc.insert_edge(0, 1);
    /// assert!(dc.connected(0, 1));
    /// ```
    pub fn insert_edge(&mut self, u: VertexId, v: VertexId) {
        // Add vertices if they don't exist
        self.add_vertex(u);
        self.add_vertex(v);

        // Normalize edge (smaller vertex first)
        let edge = if u < v { (u, v) } else { (v, u) };

        // Add to edge set
        if self.edges.insert(edge) {
            // New edge - perform union
            let root_u = self.find(u);
            let root_v = self.find(v);

            if root_u != root_v {
                self.union(root_u, root_v);

                // Link in Euler Tour Tree (O(log n))
                let _ = self.ett.link(u, v);
            }
        }
    }

    /// Deletes an edge between two vertices
    ///
    /// Triggers a full rebuild of the data structure from the remaining edges.
    /// The ETT is also rebuilt to maintain O(log n) queries.
    ///
    /// # Arguments
    ///
    /// * `u` - First vertex
    /// * `v` - Second vertex
    ///
    /// # Time Complexity
    ///
    /// O(m·α(n)) where m is the number of edges (includes ETT rebuild)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let mut dc = DynamicConnectivity::new();
    /// dc.insert_edge(0, 1);
    /// dc.delete_edge(0, 1);
    /// assert!(!dc.connected(0, 1));
    /// ```
    pub fn delete_edge(&mut self, u: VertexId, v: VertexId) {
        // Normalize edge
        let edge = if u < v { (u, v) } else { (v, u) };

        // Remove from edge set
        if self.edges.remove(&edge) {
            // Mark ETT as out of sync
            self.ett_synced = false;

            // Rebuild the entire structure (including ETT)
            self.rebuild();
        }
    }

    /// Checks if the entire graph is connected (single component)
    ///
    /// # Returns
    ///
    /// `true` if all vertices are in a single connected component,
    /// `false` otherwise
    ///
    /// # Time Complexity
    ///
    /// O(1)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let mut dc = DynamicConnectivity::new();
    /// dc.add_vertex(0);
    /// dc.add_vertex(1);
    /// assert!(!dc.is_connected());
    ///
    /// dc.insert_edge(0, 1);
    /// assert!(dc.is_connected());
    /// ```
    pub fn is_connected(&self) -> bool {
        self.component_count == 1
    }

    /// Checks if two vertices are in the same connected component
    ///
    /// # Arguments
    ///
    /// * `u` - First vertex
    /// * `v` - Second vertex
    ///
    /// # Returns
    ///
    /// `true` if vertices are connected, `false` otherwise.
    /// Returns `false` if either vertex doesn't exist.
    ///
    /// # Time Complexity
    ///
    /// O(α(n)) amortized via union-find with path compression
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let mut dc = DynamicConnectivity::new();
    /// dc.insert_edge(0, 1);
    /// dc.insert_edge(1, 2);
    /// assert!(dc.connected(0, 2));
    /// ```
    pub fn connected(&mut self, u: VertexId, v: VertexId) -> bool {
        if !self.parent.contains_key(&u) || !self.parent.contains_key(&v) {
            return false;
        }

        // Use union-find with path compression (effectively O(1) amortized)
        // ETT is maintained for future subtree query optimizations
        self.find(u) == self.find(v)
    }

    /// Fast connectivity check using Euler Tour Tree (O(log n))
    ///
    /// Returns None if ETT is out of sync and result is unreliable.
    /// Use `connected()` for the reliable version.
    #[inline]
    pub fn connected_fast(&self, u: VertexId, v: VertexId) -> Option<bool> {
        if !self.ett_synced {
            return None;
        }
        Some(self.ett.connected(u, v))
    }

    /// Returns the number of connected components
    ///
    /// # Returns
    ///
    /// The current number of connected components
    pub fn component_count(&self) -> usize {
        self.component_count
    }

    /// Returns the number of vertices
    ///
    /// # Returns
    ///
    /// The current number of vertices
    pub fn vertex_count(&self) -> usize {
        self.vertex_count
    }

    /// Finds the root of a vertex's component with path compression
    ///
    /// # Arguments
    ///
    /// * `v` - The vertex to find the root for
    ///
    /// # Returns
    ///
    /// The root vertex of the component containing `v`
    ///
    /// # Panics
    ///
    /// Panics if the vertex doesn't exist in the structure
    fn find(&mut self, v: VertexId) -> VertexId {
        let parent = *self.parent.get(&v).expect("Vertex not found");

        if parent != v {
            // Path compression: make v point directly to root
            let root = self.find(parent);
            self.parent.insert(v, root);
            root
        } else {
            v
        }
    }

    /// Unions two components by rank
    ///
    /// # Arguments
    ///
    /// * `u` - Root of first component
    /// * `v` - Root of second component
    ///
    /// # Notes
    ///
    /// This function assumes `u` and `v` are roots. It should only be
    /// called after `find()` operations.
    fn union(&mut self, u: VertexId, v: VertexId) {
        if u == v {
            return;
        }

        let rank_u = *self.rank.get(&u).unwrap_or(&0);
        let rank_v = *self.rank.get(&v).unwrap_or(&0);

        // Union by rank: attach smaller tree to larger tree
        if rank_u < rank_v {
            self.parent.insert(u, v);
        } else if rank_u > rank_v {
            self.parent.insert(v, u);
        } else {
            // Equal rank: arbitrary choice, increment rank
            self.parent.insert(v, u);
            self.rank.insert(u, rank_u + 1);
        }

        // Decrease component count
        self.component_count -= 1;
    }

    /// Rebuilds the union-find structure from the current edge set
    ///
    /// Called after edge deletions to recompute connected components.
    /// Resets all vertices to singleton components and re-applies all edges.
    /// Also rebuilds the Euler Tour Tree for O(log n) queries.
    ///
    /// # Time Complexity
    ///
    /// O(m·α(n)) where m is the number of edges
    fn rebuild(&mut self) {
        // Collect all vertices
        let vertices: Vec<VertexId> = self.parent.keys().copied().collect();

        // Reset to singleton components
        self.component_count = vertices.len();
        for &v in &vertices {
            self.parent.insert(v, v);
            self.rank.insert(v, 0);
        }

        // Rebuild Euler Tour Tree
        self.ett = EulerTourTree::new();
        for &v in &vertices {
            let _ = self.ett.make_tree(v);
        }

        // Re-apply all edges
        let edges: Vec<(VertexId, VertexId)> = self.edges.iter().copied().collect();
        for (u, v) in edges {
            let root_u = self.find(u);
            let root_v = self.find(v);

            if root_u != root_v {
                self.union(root_u, root_v);
                // Link in ETT
                let _ = self.ett.link(u, v);
            }
        }

        // Mark ETT as synced
        self.ett_synced = true;
    }
}

impl Default for DynamicConnectivity {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_new() {
        let dc = DynamicConnectivity::new();
        assert_eq!(dc.vertex_count(), 0);
        assert_eq!(dc.component_count(), 0);
    }

    #[test]
    fn test_add_vertex() {
        let mut dc = DynamicConnectivity::new();

        dc.add_vertex(0);
        assert_eq!(dc.vertex_count(), 1);
        assert_eq!(dc.component_count(), 1);

        dc.add_vertex(1);
        assert_eq!(dc.vertex_count(), 2);
        assert_eq!(dc.component_count(), 2);

        // Adding same vertex is no-op
        dc.add_vertex(0);
        assert_eq!(dc.vertex_count(), 2);
        assert_eq!(dc.component_count(), 2);
    }

    #[test]
    fn test_insert_edge_basic() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 1);
        assert_eq!(dc.vertex_count(), 2);
        assert_eq!(dc.component_count(), 1);
        assert!(dc.connected(0, 1));
    }

    #[test]
    fn test_insert_edge_chain() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 1);
        dc.insert_edge(1, 2);
        dc.insert_edge(2, 3);

        assert_eq!(dc.vertex_count(), 4);
        assert_eq!(dc.component_count(), 1);
        assert!(dc.connected(0, 3));
    }

    #[test]
    fn test_is_connected() {
        let mut dc = DynamicConnectivity::new();

        dc.add_vertex(0);
        dc.add_vertex(1);
        assert!(!dc.is_connected());

        dc.insert_edge(0, 1);
        assert!(dc.is_connected());

        dc.add_vertex(2);
        assert!(!dc.is_connected());

        dc.insert_edge(1, 2);
        assert!(dc.is_connected());
    }

    #[test]
    fn test_delete_edge() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 1);
        dc.insert_edge(1, 2);
        assert!(dc.connected(0, 2));

        dc.delete_edge(1, 2);
        assert!(dc.connected(0, 1));
        assert!(!dc.connected(0, 2));
        assert_eq!(dc.component_count(), 2);
    }

    #[test]
    fn test_delete_edge_normalized() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 1);
        assert!(dc.connected(0, 1));

        // Delete with reversed vertices
        dc.delete_edge(1, 0);
        assert!(!dc.connected(0, 1));
    }

    #[test]
    fn test_multiple_components() {
        let mut dc = DynamicConnectivity::new();

        // Component 1: 0-1-2
        dc.insert_edge(0, 1);
        dc.insert_edge(1, 2);

        // Component 2: 3-4
        dc.insert_edge(3, 4);

        // Isolated vertex
        dc.add_vertex(5);

        assert_eq!(dc.vertex_count(), 6);
        assert_eq!(dc.component_count(), 3);

        assert!(dc.connected(0, 2));
        assert!(dc.connected(3, 4));
        assert!(!dc.connected(0, 3));
        assert!(!dc.connected(0, 5));
    }

    #[test]
    fn test_path_compression() {
        let mut dc = DynamicConnectivity::new();

        // Create a long chain
        for i in 0..10 {
            dc.insert_edge(i, i + 1);
        }

        // Path compression should happen on find
        assert!(dc.connected(0, 10));

        // All vertices should now point closer to root
        let root = dc.find(0);
        for i in 0..=10 {
            assert_eq!(dc.find(i), root);
        }
    }

    #[test]
    fn test_union_by_rank() {
        let mut dc = DynamicConnectivity::new();

        // Create two trees of different sizes
        dc.insert_edge(0, 1);
        dc.insert_edge(0, 2);
        dc.insert_edge(0, 3);

        dc.insert_edge(4, 5);

        // Union them
        dc.insert_edge(0, 4);

        assert_eq!(dc.component_count(), 1);
        assert!(dc.connected(1, 5));
    }

    #[test]
    fn test_rebuild_after_multiple_deletions() {
        let mut dc = DynamicConnectivity::new();

        // Create a complete graph K4
        dc.insert_edge(0, 1);
        dc.insert_edge(0, 2);
        dc.insert_edge(0, 3);
        dc.insert_edge(1, 2);
        dc.insert_edge(1, 3);
        dc.insert_edge(2, 3);

        assert!(dc.is_connected());

        // Remove edges to disconnect
        dc.delete_edge(0, 1);
        dc.delete_edge(0, 2);
        dc.delete_edge(0, 3);

        assert!(!dc.is_connected());
        assert_eq!(dc.component_count(), 2);
        assert!(!dc.connected(0, 1));
        assert!(dc.connected(1, 2));
        assert!(dc.connected(1, 3));
    }

    #[test]
    fn test_connected_nonexistent_vertex() {
        let mut dc = DynamicConnectivity::new();

        dc.add_vertex(0);
        assert!(!dc.connected(0, 999));
        assert!(!dc.connected(999, 0));
    }

    #[test]
    fn test_self_loop() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 0);
        assert_eq!(dc.vertex_count(), 1);
        assert_eq!(dc.component_count(), 1);
        assert!(dc.connected(0, 0));
    }

    #[test]
    fn test_duplicate_edges() {
        let mut dc = DynamicConnectivity::new();

        dc.insert_edge(0, 1);
        dc.insert_edge(0, 1); // Duplicate
        dc.insert_edge(1, 0); // Duplicate (reversed)

        assert_eq!(dc.vertex_count(), 2);
        assert_eq!(dc.component_count(), 1);
    }
}