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
//! Tests for dynamic incremental canonical minimum cut (Tier 3).

use super::*;
use crate::canonical::FixedWeight;
use crate::canonical::source_anchored::{
    canonical_mincut, SourceAnchoredConfig,
};
use crate::graph::DynamicGraph;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn make_dynamic(
    edges: &[(u64, u64, f64)],
) -> DynamicMinCut {
    let edge_vec: Vec<(u64, u64, f64)> = edges.to_vec();
    DynamicMinCut::with_edges(edge_vec, DynamicMinCutConfig::default()).unwrap()
}

fn make_dynamic_with_threshold(
    edges: &[(u64, u64, f64)],
    threshold: u64,
) -> DynamicMinCut {
    let edge_vec: Vec<(u64, u64, f64)> = edges.to_vec();
    let config = DynamicMinCutConfig {
        canonical_config: SourceAnchoredConfig::default(),
        staleness_threshold: threshold,
    };
    DynamicMinCut::with_edges(edge_vec, config).unwrap()
}

fn make_graph(edges: &[(u64, u64, f64)]) -> DynamicGraph {
    let g = DynamicGraph::new();
    for &(u, v, w) in edges {
        g.insert_edge(u, v, w).unwrap();
    }
    g
}

// ---------------------------------------------------------------------------
// Basic construction and computation
// ---------------------------------------------------------------------------

#[test]
fn test_dynamic_basic_computation() {
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ]);

    let cut = dmc.canonical_cut().unwrap();
    assert_eq!(cut.lambda, FixedWeight::from_f64(2.0));
    assert_eq!(cut.source_vertex, 0);
}

#[test]
fn test_dynamic_empty_graph() {
    let mut dmc = DynamicMinCut::new();
    assert!(dmc.canonical_cut().is_none());
}

#[test]
fn test_dynamic_default() {
    let dmc = DynamicMinCut::default();
    assert_eq!(dmc.epoch(), 0);
    assert_eq!(dmc.last_full_epoch(), 0);
    assert_eq!(dmc.incremental_count(), 0);
}

// ---------------------------------------------------------------------------
// Edge insertion: same side (no recompute)
// ---------------------------------------------------------------------------

#[test]
fn test_add_edge_same_side_no_recompute() {
    // Triangle: {0,1,2}, cut isolates one vertex
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ]);

    // Force initial computation
    let cut1 = dmc.canonical_cut().unwrap();
    let lambda1 = cut1.lambda;
    let hash1 = cut1.cut_hash;

    // Add an edge within the source side (e.g., between 0 and 1 if both
    // are on source side). We need to check what side they're on.
    // In a triangle with source=0, the cut is lambda=2 and separates one
    // vertex. Let's add a new vertex connected to two source-side vertices.

    // Actually, let's use a more predictable graph.
    // Path: 0-1-2-3, cut at edge (1,2) or (2,3).
    let mut dmc2 = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 3, 1.0),
    ]);

    let cut2 = dmc2.canonical_cut().unwrap();
    assert_eq!(cut2.lambda, FixedWeight::from_f64(1.0));

    // The cut should be at one of the bridge edges.
    // Source = 0, so source side includes 0.
    // Adding edge (0, 1) would be within source side if both 0,1 are on same side.
    // But (0,1) already exists. Let's add a parallel path.
    // Add edge 0-3 with small weight -- this crosses the cut.
    // Instead, let's test with a graph we can reason about.
}

#[test]
fn test_add_edge_crosses_cut_triggers_recompute() {
    // Two clusters connected by weak edge
    let mut dmc = make_dynamic(&[
        (0, 1, 5.0), (1, 2, 5.0), (2, 0, 5.0),
        (3, 4, 5.0), (4, 5, 5.0), (5, 3, 5.0),
        (2, 3, 1.0),
    ]);

    let cut1 = dmc.canonical_cut().unwrap();
    assert_eq!(cut1.lambda, FixedWeight::from_f64(1.0));

    // Add another cross-cluster edge (stronger than 1.0)
    dmc.add_edge(0, 5, 3.0).unwrap();

    // The cache should be marked dirty because the edge crosses the cut
    assert!(dmc.is_stale());

    // Recompute
    let cut2 = dmc.canonical_cut().unwrap();
    // Now the min-cut should be higher (two cross edges: weight 1 + 3 = 4)
    assert!(cut2.lambda.to_f64() > 1.0);
}

// ---------------------------------------------------------------------------
// Edge deletion
// ---------------------------------------------------------------------------

#[test]
fn test_remove_edge_not_in_cut() {
    let mut dmc = make_dynamic(&[
        (0, 1, 5.0), (1, 2, 5.0), (2, 0, 5.0),
        (3, 4, 5.0), (4, 5, 5.0), (5, 3, 5.0),
        (2, 3, 1.0),
    ]);

    let cut1 = dmc.canonical_cut().unwrap();
    assert_eq!(cut1.lambda, FixedWeight::from_f64(1.0));

    // Remove edge within cluster (0,1) -- not in the cut set
    dmc.remove_edge(0, 1).unwrap();

    // Should not be stale (edge wasn't in cut)
    assert!(!dmc.is_stale());
}

#[test]
fn test_remove_edge_in_cut_triggers_recompute() {
    let mut dmc = make_dynamic(&[
        (0, 1, 5.0), (1, 2, 5.0), (2, 0, 5.0),
        (3, 4, 5.0), (4, 5, 5.0), (5, 3, 5.0),
        (2, 3, 1.0),
    ]);

    let cut1 = dmc.canonical_cut().unwrap();
    assert_eq!(cut1.lambda, FixedWeight::from_f64(1.0));

    // Remove the bridge edge (2,3) which is in the cut set
    dmc.remove_edge(2, 3).unwrap();

    // Should be stale
    assert!(dmc.is_stale());

    // Recompute -- graph is now disconnected
    let cut2 = dmc.canonical_cut();
    assert!(cut2.is_none()); // disconnected -> None
}

// ---------------------------------------------------------------------------
// Batch updates
// ---------------------------------------------------------------------------

#[test]
fn test_batch_updates_match_sequential() {
    // Use a simple triangle as base.
    let base_edges = vec![
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ];

    // Sequential: add two new edges
    let mut dmc_seq = make_dynamic(&base_edges);
    dmc_seq.canonical_cut(); // initial computation
    dmc_seq.add_edge(0, 3, 2.0).unwrap();
    dmc_seq.add_edge(1, 3, 3.0).unwrap();
    let cut_seq = dmc_seq.canonical_cut();

    // Batch: same two new edges
    let mut dmc_batch = make_dynamic(&base_edges);
    dmc_batch.canonical_cut(); // initial computation
    dmc_batch.apply_batch(&[
        EdgeMutation::Add(0, 3, 2.0),
        EdgeMutation::Add(1, 3, 3.0),
    ]).unwrap();
    let cut_batch = dmc_batch.canonical_cut();

    match (cut_seq, cut_batch) {
        (Some(s), Some(b)) => {
            assert_eq!(s.lambda, b.lambda);
            assert_eq!(s.cut_hash, b.cut_hash);
        }
        (None, None) => {}
        _ => panic!("Sequential and batch should agree"),
    }
}

#[test]
fn test_batch_add_and_remove() {
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
        (2, 3, 1.0), (3, 4, 1.0), (4, 2, 1.0),
    ]);

    dmc.canonical_cut(); // initial

    dmc.apply_batch(&[
        EdgeMutation::Add(0, 4, 2.0),
        EdgeMutation::Remove(2, 3),
    ]).unwrap();

    // Should still be able to compute a cut
    let cut = dmc.canonical_cut();
    // The graph should still be connected (0-4 bridges the gap)
    assert!(cut.is_some());
}

// ---------------------------------------------------------------------------
// Epoch tracking
// ---------------------------------------------------------------------------

#[test]
fn test_epoch_increments() {
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ]);

    assert_eq!(dmc.epoch(), 0);

    dmc.add_edge(3, 0, 1.0).unwrap();
    assert_eq!(dmc.epoch(), 1);

    dmc.add_edge(3, 1, 1.0).unwrap();
    assert_eq!(dmc.epoch(), 2);

    dmc.remove_edge(3, 0).unwrap();
    assert_eq!(dmc.epoch(), 3);
}

#[test]
fn test_last_full_epoch_updates_on_recompute() {
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ]);

    assert_eq!(dmc.last_full_epoch(), 0);

    dmc.canonical_cut(); // full recompute
    assert_eq!(dmc.last_full_epoch(), 0);

    dmc.add_edge(3, 0, 1.0).unwrap(); // epoch = 1
    dmc.add_edge(3, 1, 1.0).unwrap(); // epoch = 2

    // Force recompute
    dmc.force_recompute();
    assert_eq!(dmc.last_full_epoch(), 2);
}

// ---------------------------------------------------------------------------
// Staleness detection
// ---------------------------------------------------------------------------

#[test]
fn test_staleness_triggers_recompute() {
    // Set threshold to 3 updates
    let mut dmc = make_dynamic_with_threshold(
        &[(0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0)],
        3,
    );

    dmc.canonical_cut(); // initial compute
    assert_eq!(dmc.incremental_count(), 0);

    // These edges are within the source side, so no immediate recompute
    // We need to add edges that don't cross the cut
    // In a triangle with source=0, cut separates one vertex.
    // Let's add edges to new vertices that connect to the source side.
    dmc.add_edge(3, 4, 1.0).unwrap(); // new disconnected component
    // This triggers dirty because new vertices aren't in the cache.
    // Let's use a different approach - use batch to count.

    let mut dmc2 = make_dynamic_with_threshold(
        &[
            (0, 1, 5.0), (1, 2, 5.0), (2, 0, 5.0),
            (3, 4, 5.0), (4, 5, 5.0), (5, 3, 5.0),
            (2, 3, 1.0),
        ],
        3,
    );

    dmc2.canonical_cut(); // initial compute

    // Remove non-cut edges (within clusters)
    dmc2.remove_edge(0, 1).unwrap();  // incremental_count = 1
    assert_eq!(dmc2.incremental_count(), 1);

    dmc2.remove_edge(3, 4).unwrap();  // incremental_count = 2
    assert_eq!(dmc2.incremental_count(), 2);

    dmc2.remove_edge(4, 5).unwrap();  // incremental_count = 3
    // At this point, staleness should trigger on next canonical_cut()
    // Note: incremental_count hits threshold (3)

    // The next canonical_cut() should detect staleness and recompute
    let cut = dmc2.canonical_cut();
    // After recompute, incremental_count resets
    assert_eq!(dmc2.incremental_count(), 0);
    assert_eq!(dmc2.last_full_epoch(), dmc2.epoch());
}

#[test]
fn test_staleness_disabled_when_zero() {
    let mut dmc = make_dynamic_with_threshold(
        &[(0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0)],
        0, // disabled
    );

    dmc.canonical_cut();

    // Even after many updates, no auto-recompute
    for i in 3..20 {
        let _ = dmc.add_edge(i, 0, 1.0);
    }

    // incremental_count is high but staleness is disabled
    // The cut should still be computed when requested
    let cut = dmc.canonical_cut();
    assert!(cut.is_some());
}

// ---------------------------------------------------------------------------
// Determinism
// ---------------------------------------------------------------------------

#[test]
fn test_dynamic_determinism_100_runs() {
    let edges = vec![
        (0, 1, 3.0), (1, 2, 2.0), (2, 3, 4.0),
        (3, 0, 1.0), (0, 2, 5.0), (1, 3, 2.0),
    ];

    let mut first_hash = None;

    for _ in 0..100 {
        let mut dmc = make_dynamic(&edges);
        let cut = dmc.canonical_cut().unwrap();

        match first_hash {
            None => first_hash = Some(cut.cut_hash),
            Some(h) => assert_eq!(h, cut.cut_hash, "Dynamic must be deterministic"),
        }
    }
}

// ---------------------------------------------------------------------------
// Edge cases
// ---------------------------------------------------------------------------

#[test]
fn test_dynamic_single_edge() {
    let mut dmc = make_dynamic(&[(0, 1, 1.0)]);
    let cut = dmc.canonical_cut().unwrap();
    assert_eq!(cut.lambda, FixedWeight::from_f64(1.0));
}

#[test]
fn test_dynamic_add_then_remove_restores_cut_value() {
    // Use a graph where adding/removing edges doesn't leave isolated vertices.
    // Start with a 4-cycle so there's more structure.
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 3, 1.0), (3, 0, 1.0),
    ]);

    let cut1 = dmc.canonical_cut().unwrap();
    let lambda1 = cut1.lambda;

    // Add a diagonal edge
    dmc.add_edge(0, 2, 1.0).unwrap();

    // Remove it again
    dmc.remove_edge(0, 2).unwrap();

    // After adding and removing the same edge, the cut value should
    // be the same as the original.
    let cut2 = dmc.canonical_cut().unwrap();
    assert_eq!(lambda1, cut2.lambda);
    assert_eq!(cut1.cut_hash, cut2.cut_hash);
}

#[test]
fn test_dynamic_receipt() {
    let mut dmc = make_dynamic(&[
        (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
    ]);

    let receipt = dmc.receipt().unwrap();
    assert_eq!(receipt.epoch, 0);
    assert_eq!(receipt.lambda, FixedWeight::from_f64(2.0));
}

// ---------------------------------------------------------------------------
// Correctness: dynamic matches full recomputation
// ---------------------------------------------------------------------------

#[test]
fn test_dynamic_matches_full_recompute_after_additions() {
    let base_edges = vec![
        (0, 1, 2.0), (1, 2, 3.0), (2, 3, 1.0),
        (3, 0, 4.0), (0, 2, 1.0),
    ];

    let mut dmc = make_dynamic(&base_edges);
    dmc.canonical_cut(); // initial

    // Add edges to new vertices (not in base graph)
    dmc.add_edge(10, 0, 2.0).unwrap();
    dmc.add_edge(11, 1, 3.0).unwrap();

    // Force full recompute
    dmc.force_recompute();
    let dynamic_cut = dmc.canonical_cut();

    // Compare with fresh computation on identical graph
    let mut all_edges = base_edges.clone();
    all_edges.push((10, 0, 2.0));
    all_edges.push((11, 1, 3.0));

    let g = make_graph(&all_edges);
    let fresh_cut = canonical_mincut(&g, &SourceAnchoredConfig::default());

    match (dynamic_cut, fresh_cut) {
        (Some(d), Some(f)) => {
            assert_eq!(d.lambda, f.lambda);
            assert_eq!(d.cut_hash, f.cut_hash);
        }
        (None, None) => {}
        _ => panic!("Dynamic and fresh should agree"),
    }
}

#[test]
fn test_dynamic_matches_full_recompute_after_deletions() {
    let base_edges = vec![
        (0, 1, 2.0), (1, 2, 3.0), (2, 3, 1.0),
        (3, 0, 4.0), (0, 2, 1.0), (1, 3, 2.0),
    ];

    let mut dmc = make_dynamic(&base_edges);
    dmc.canonical_cut();

    dmc.remove_edge(0, 2).unwrap();
    dmc.remove_edge(1, 3).unwrap();

    dmc.force_recompute();
    let dynamic_cut = dmc.cached_cut.clone();

    let remaining_edges = vec![
        (0, 1, 2.0), (1, 2, 3.0), (2, 3, 1.0), (3, 0, 4.0),
    ];
    let g = make_graph(&remaining_edges);
    let fresh_cut = canonical_mincut(&g, &SourceAnchoredConfig::default());

    match (dynamic_cut, fresh_cut) {
        (Some(d), Some(f)) => {
            assert_eq!(d.lambda, f.lambda);
            assert_eq!(d.cut_hash, f.cut_hash);
        }
        (None, None) => {}
        _ => panic!("Dynamic and fresh should agree"),
    }
}