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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
//! Subpolynomial Dynamic Minimum Cut Algorithm
//!
//! Implementation of the December 2024 breakthrough achieving n^{o(1)} update time:
//! "Deterministic and Exact Fully-dynamic Minimum Cut of Superpolylogarithmic Size
//! in Subpolynomial Time" (arXiv:2512.13105)
//!
//! # Key Components
//!
//! 1. **Multi-Level Hierarchy**: O(log^{1/4} n) levels of expander decomposition
//! 2. **Deterministic LocalKCut**: Tree packing with color-coded enumeration
//! 3. **Fragmenting Algorithm**: Boundary-sparse cut detection
//! 4. **Witness Trees**: Certificate-based cut verification
//!
//! # Complexity Guarantees
//!
//! - **Update Time**: O(n^{o(1)}) = 2^{O(log^{1-c} n)} amortized
//! - **Query Time**: O(1)
//! - **Space**: O(m log n)
//! - **Cut Size**: Up to 2^{Θ(log^{3/4-c} n)}
//!
//! # Example
//!
//! ```rust,no_run
//! use ruvector_mincut::subpolynomial::{SubpolynomialMinCut, SubpolyConfig};
//!
//! let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());
//!
//! // Build initial graph
//! mincut.insert_edge(1, 2, 1.0);
//! mincut.insert_edge(2, 3, 1.0);
//! mincut.insert_edge(3, 1, 1.0);
//!
//! // Query minimum cut
//! let cut_value = mincut.min_cut_value();
//! println!("Min cut: {}", cut_value);
//!
//! // Updates are subpolynomial!
//! mincut.insert_edge(3, 4, 1.0);
//! println!("New min cut: {}", mincut.min_cut_value());
//! ```

use std::collections::{HashMap, HashSet, VecDeque};

use crate::cluster::hierarchy::{
    Expander, HierarchyCluster, HierarchyConfig, Precluster, ThreeLevelHierarchy,
};
use crate::error::{MinCutError, Result};
use crate::time_compat::PortableInstant;
use crate::expander::{ExpanderComponent, ExpanderDecomposition};
use crate::fragmentation::{Fragmentation, FragmentationConfig, TrimResult};
use crate::graph::{DynamicGraph, EdgeId, VertexId, Weight};
use crate::localkcut::deterministic::{DeterministicLocalKCut, LocalCut as DetLocalCut};
use crate::witness::{LazyWitnessTree, WitnessTree};

/// Configuration for the subpolynomial algorithm
#[derive(Debug, Clone)]
pub struct SubpolyConfig {
    /// Expansion parameter φ = 2^{-Θ(log^{3/4} n)}
    /// For n < 10^6, we use a practical approximation
    pub phi: f64,
    /// Maximum cut size to support exactly
    /// λ_max = 2^{Θ(log^{3/4-c} n)}
    pub lambda_max: u64,
    /// Approximation parameter ε for (1+ε)-approximate internal operations
    pub epsilon: f64,
    /// Target number of hierarchy levels: O(log^{1/4} n)
    pub target_levels: usize,
    /// Enable recourse tracking for complexity verification
    pub track_recourse: bool,
    /// Enable mirror cut certification
    pub certify_cuts: bool,
    /// Enable parallel processing
    pub parallel: bool,
}

impl Default for SubpolyConfig {
    fn default() -> Self {
        Self {
            phi: 0.01,
            lambda_max: 1000,
            epsilon: 0.1,
            target_levels: 4, // O(log^{1/4} n) for n ~= 10^6
            track_recourse: true,
            certify_cuts: true,
            parallel: true,
        }
    }
}

impl SubpolyConfig {
    /// Create config optimized for graph of size n
    pub fn for_size(n: usize) -> Self {
        let log_n = (n.max(2) as f64).ln();

        // φ = 2^{-Θ(log^{3/4} n)}
        let phi = 2.0_f64.powf(-log_n.powf(0.75) / 4.0);

        // λ_max = 2^{Θ(log^{3/4-c} n)} with c = 0.1
        let lambda_max = 2.0_f64.powf(log_n.powf(0.65)).min(1e9) as u64;

        // Target levels = O(log^{1/4} n)
        let target_levels = (log_n.powf(0.25).ceil() as usize).max(2).min(10);

        Self {
            phi,
            lambda_max,
            epsilon: 0.1,
            target_levels,
            track_recourse: true,
            certify_cuts: true,
            parallel: true,
        }
    }
}

/// Statistics for recourse tracking
#[derive(Debug, Clone, Default)]
pub struct RecourseStats {
    /// Total recourse across all updates
    pub total_recourse: u64,
    /// Number of updates processed
    pub num_updates: u64,
    /// Maximum recourse in a single update
    pub max_single_recourse: u64,
    /// Recourse per level
    pub recourse_per_level: Vec<u64>,
    /// Average update time in microseconds
    pub avg_update_time_us: f64,
    /// Theoretical subpolynomial bound (computed)
    pub theoretical_bound: f64,
}

impl RecourseStats {
    /// Check if recourse is within subpolynomial bounds
    pub fn is_subpolynomial(&self, n: usize) -> bool {
        if n < 2 || self.num_updates == 0 {
            return true;
        }

        let log_n = (n as f64).ln();
        // Subpolynomial: 2^{O(log^{1-c} n)} with c = 0.1
        let bound = 2.0_f64.powf(log_n.powf(0.9));

        (self.total_recourse as f64 / self.num_updates as f64) <= bound
    }

    /// Get amortized recourse per update
    pub fn amortized_recourse(&self) -> f64 {
        if self.num_updates == 0 {
            return 0.0;
        }
        self.total_recourse as f64 / self.num_updates as f64
    }
}

/// A level in the multi-level hierarchy
#[derive(Debug)]
pub struct HierarchyLevel {
    /// Level index (0 = base, higher = coarser)
    pub level: usize,
    /// Expander decomposition at this level
    pub expanders: HashMap<u64, LevelExpander>,
    /// Vertex to expander mapping
    pub vertex_expander: HashMap<VertexId, u64>,
    /// Next expander ID
    next_id: u64,
    /// Recourse at this level
    pub recourse: u64,
    /// Configuration
    phi: f64,
}

/// An expander within a hierarchy level
#[derive(Debug, Clone)]
pub struct LevelExpander {
    /// Unique ID
    pub id: u64,
    /// Vertices in this expander
    pub vertices: HashSet<VertexId>,
    /// Boundary edges
    pub boundary_size: usize,
    /// Volume (sum of degrees)
    pub volume: usize,
    /// Certified minimum cut within expander
    pub internal_min_cut: f64,
    /// Is this a valid φ-expander?
    pub is_valid_expander: bool,
    /// Parent expander ID at next level (if any)
    pub parent_id: Option<u64>,
    /// Child expander IDs at previous level
    pub children_ids: Vec<u64>,
}

/// The main subpolynomial dynamic minimum cut structure
#[derive(Debug)]
pub struct SubpolynomialMinCut {
    /// Configuration
    config: SubpolyConfig,
    /// Graph adjacency
    adjacency: HashMap<VertexId, HashMap<VertexId, Weight>>,
    /// All edges
    edges: HashSet<(VertexId, VertexId)>,
    /// Multi-level hierarchy
    levels: Vec<HierarchyLevel>,
    /// Deterministic LocalKCut for cut discovery
    local_kcut: Option<DeterministicLocalKCut>,
    /// Current minimum cut value
    current_min_cut: f64,
    /// Recourse statistics
    recourse_stats: RecourseStats,
    /// Number of vertices
    num_vertices: usize,
    /// Number of edges
    num_edges: usize,
    /// Next vertex/edge ID tracking
    next_id: u64,
    /// Is hierarchy built?
    hierarchy_built: bool,
}

impl SubpolynomialMinCut {
    /// Create new subpolynomial min-cut structure
    pub fn new(config: SubpolyConfig) -> Self {
        let num_levels = config.target_levels;
        let levels = (0..num_levels)
            .map(|i| HierarchyLevel {
                level: i,
                expanders: HashMap::new(),
                vertex_expander: HashMap::new(),
                next_id: 1,
                recourse: 0,
                phi: config.phi * (1.0 + i as f64 * 0.1), // Slightly increasing φ per level
            })
            .collect();

        Self {
            config,
            adjacency: HashMap::new(),
            edges: HashSet::new(),
            levels,
            local_kcut: None,
            current_min_cut: f64::INFINITY,
            recourse_stats: RecourseStats::default(),
            num_vertices: 0,
            num_edges: 0,
            next_id: 1,
            hierarchy_built: false,
        }
    }

    /// Create with config optimized for expected graph size
    pub fn for_size(expected_n: usize) -> Self {
        Self::new(SubpolyConfig::for_size(expected_n))
    }

    /// Insert an edge
    pub fn insert_edge(&mut self, u: VertexId, v: VertexId, weight: Weight) -> Result<f64> {
        let start = PortableInstant::now();

        let key = Self::edge_key(u, v);
        if self.edges.contains(&key) {
            return Err(MinCutError::EdgeExists(u, v));
        }

        // Add to graph
        self.edges.insert(key);
        let is_new_u = !self.adjacency.contains_key(&u);
        let is_new_v = !self.adjacency.contains_key(&v);

        self.adjacency.entry(u).or_default().insert(v, weight);
        self.adjacency.entry(v).or_default().insert(u, weight);

        if is_new_u {
            self.num_vertices += 1;
        }
        if is_new_v && u != v {
            self.num_vertices += 1;
        }
        self.num_edges += 1;

        // Update hierarchy incrementally if built
        if self.hierarchy_built {
            let recourse = self.handle_edge_insert(u, v, weight);
            self.update_recourse_stats(recourse, start.elapsed().as_micros() as f64);
        }

        // Update LocalKCut
        if let Some(ref mut lkc) = self.local_kcut {
            lkc.insert_edge(u, v, weight);
        }

        Ok(self.current_min_cut)
    }

    /// Delete an edge
    pub fn delete_edge(&mut self, u: VertexId, v: VertexId) -> Result<f64> {
        let start = PortableInstant::now();

        let key = Self::edge_key(u, v);
        if !self.edges.remove(&key) {
            return Err(MinCutError::EdgeNotFound(u, v));
        }

        // Remove from graph
        if let Some(neighbors) = self.adjacency.get_mut(&u) {
            neighbors.remove(&v);
        }
        if let Some(neighbors) = self.adjacency.get_mut(&v) {
            neighbors.remove(&u);
        }
        self.num_edges -= 1;

        // Update hierarchy incrementally if built
        if self.hierarchy_built {
            let recourse = self.handle_edge_delete(u, v);
            self.update_recourse_stats(recourse, start.elapsed().as_micros() as f64);
        }

        // Update LocalKCut
        if let Some(ref mut lkc) = self.local_kcut {
            lkc.delete_edge(u, v);
        }

        Ok(self.current_min_cut)
    }

    /// Build the multi-level hierarchy
    ///
    /// This creates O(log^{1/4} n) levels of expander decomposition,
    /// enabling subpolynomial update time.
    pub fn build(&mut self) {
        if self.adjacency.is_empty() {
            return;
        }

        // Adjust number of levels based on actual graph size
        let n = self.num_vertices;
        let log_n = (n.max(2) as f64).ln();
        let optimal_levels = (log_n.powf(0.25).ceil() as usize).max(2).min(10);

        // Resize levels if needed
        while self.levels.len() < optimal_levels {
            let i = self.levels.len();
            self.levels.push(HierarchyLevel {
                level: i,
                expanders: HashMap::new(),
                vertex_expander: HashMap::new(),
                next_id: 1,
                recourse: 0,
                phi: self.config.phi * (1.0 + i as f64 * 0.1),
            });
        }

        // Build base level (level 0) expanders
        self.build_base_level();

        // Build subsequent levels by coarsening
        for level in 1..self.levels.len() {
            self.build_level(level);
        }

        // Initialize LocalKCut
        self.local_kcut = Some(DeterministicLocalKCut::new(
            self.config.lambda_max,
            self.num_vertices * 10,
            2,
        ));

        // Collect edge data first
        let edge_data: Vec<(VertexId, VertexId, Weight)> = self
            .edges
            .iter()
            .map(|&(u, v)| (u, v, self.get_weight(u, v).unwrap_or(1.0)))
            .collect();

        // Add edges to LocalKCut
        if let Some(ref mut lkc) = self.local_kcut {
            for (u, v, weight) in edge_data {
                lkc.insert_edge(u, v, weight);
            }
        }

        // Compute initial minimum cut
        self.recompute_min_cut();

        self.hierarchy_built = true;

        // Update theoretical bound
        self.recourse_stats.theoretical_bound = 2.0_f64.powf(log_n.powf(0.9));
    }

    /// Build the base level (level 0) expanders
    fn build_base_level(&mut self) {
        let vertices: HashSet<_> = self.adjacency.keys().copied().collect();
        if vertices.is_empty() {
            return;
        }

        // Get phi from level before mutating
        let phi = self.levels[0].phi;

        // First pass: grow all expanders (uses immutable self)
        let mut remaining = vertices.clone();
        let mut expander_sets: Vec<HashSet<VertexId>> = Vec::new();

        while !remaining.is_empty() {
            let start = *remaining.iter().next().unwrap();
            let expander_vertices = self.grow_expander(&remaining, start, phi);

            if expander_vertices.is_empty() {
                remaining.remove(&start);
                continue;
            }

            for &v in &expander_vertices {
                remaining.remove(&v);
            }

            expander_sets.push(expander_vertices);
        }

        // Second pass: compute properties (uses immutable self)
        let mut expanders_to_create: Vec<LevelExpander> = Vec::new();
        let mut vertex_mappings: Vec<(VertexId, u64)> = Vec::new();
        let mut next_id = self.levels[0].next_id;

        for expander_vertices in &expander_sets {
            let id = next_id;
            next_id += 1;

            let volume = expander_vertices.iter().map(|&v| self.degree(v)).sum();

            let boundary_size = self.count_boundary(expander_vertices);

            let expander = LevelExpander {
                id,
                vertices: expander_vertices.clone(),
                boundary_size,
                volume,
                internal_min_cut: f64::INFINITY,
                is_valid_expander: true,
                parent_id: None,
                children_ids: Vec::new(),
            };

            for &v in expander_vertices {
                vertex_mappings.push((v, id));
            }

            expanders_to_create.push(expander);
        }

        // Third pass: apply all changes (uses mutable self)
        let level = &mut self.levels[0];
        level.expanders.clear();
        level.vertex_expander.clear();
        level.next_id = next_id;

        for expander in expanders_to_create {
            level.expanders.insert(expander.id, expander);
        }

        for (v, id) in vertex_mappings {
            level.vertex_expander.insert(v, id);
        }
    }

    /// Build a level by coarsening the previous level
    fn build_level(&mut self, level_idx: usize) {
        if level_idx == 0 || level_idx >= self.levels.len() {
            return;
        }

        // Collect expander IDs from previous level
        let prev_expander_ids: Vec<u64> = self.levels[level_idx - 1]
            .expanders
            .keys()
            .copied()
            .collect();

        // Group adjacent expanders
        let mut groups: Vec<Vec<u64>> = Vec::new();
        let mut assigned: HashSet<u64> = HashSet::new();

        for &exp_id in &prev_expander_ids {
            if assigned.contains(&exp_id) {
                continue;
            }

            let mut group = vec![exp_id];
            assigned.insert(exp_id);

            // Find adjacent expanders
            for &other_id in &prev_expander_ids {
                if assigned.contains(&other_id) {
                    continue;
                }

                if self.expanders_adjacent_at_level(level_idx - 1, exp_id, other_id) {
                    group.push(other_id);
                    assigned.insert(other_id);

                    // Limit group size
                    if group.len() >= 4 {
                        break;
                    }
                }
            }

            groups.push(group);
        }

        // First, collect all vertices from child expanders
        let mut group_vertices: Vec<(Vec<u64>, HashSet<VertexId>)> = Vec::new();
        for group in &groups {
            let mut vertices = HashSet::new();
            for &child_id in group {
                if let Some(child) = self.levels[level_idx - 1].expanders.get(&child_id) {
                    vertices.extend(&child.vertices);
                }
            }
            group_vertices.push((group.clone(), vertices));
        }

        // Now compute properties using immutable self
        let mut expanders_to_create: Vec<(u64, LevelExpander, HashMap<VertexId, u64>)> = Vec::new();
        let mut next_id = self.levels[level_idx].next_id;

        for (group, vertices) in &group_vertices {
            let id = next_id;
            next_id += 1;

            let volume = vertices.iter().map(|&v| self.degree(v)).sum();

            let boundary_size = self.count_boundary(vertices);

            let expander = LevelExpander {
                id,
                vertices: vertices.clone(),
                boundary_size,
                volume,
                internal_min_cut: f64::INFINITY,
                is_valid_expander: true,
                parent_id: None,
                children_ids: group.clone(),
            };

            let mut vertex_map = HashMap::new();
            for &v in vertices {
                vertex_map.insert(v, id);
            }

            expanders_to_create.push((id, expander, vertex_map));
        }

        // Now apply all changes
        let level = &mut self.levels[level_idx];
        level.expanders.clear();
        level.vertex_expander.clear();
        level.next_id = next_id;

        for (id, expander, vertex_map) in expanders_to_create {
            level.expanders.insert(id, expander);
            level.vertex_expander.extend(vertex_map);
        }

        // Update parent pointers in children (separate borrow)
        for (group, _) in &group_vertices {
            // Find the parent ID for this group
            let parent_id = self.levels[level_idx]
                .expanders
                .values()
                .find(|e| &e.children_ids == group)
                .map(|e| e.id);

            if let Some(pid) = parent_id {
                for &child_id in group {
                    if let Some(child) = self.levels[level_idx - 1].expanders.get_mut(&child_id) {
                        child.parent_id = Some(pid);
                    }
                }
            }
        }
    }

    /// Grow an expander from a starting vertex
    fn grow_expander(
        &self,
        available: &HashSet<VertexId>,
        start: VertexId,
        phi: f64,
    ) -> HashSet<VertexId> {
        let mut expander = HashSet::new();
        let mut queue = VecDeque::new();

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

        let max_size = (self.num_vertices / 4).max(10);

        while let Some(v) = queue.pop_front() {
            if expander.len() >= max_size {
                break;
            }

            for (neighbor, _) in self.neighbors(v) {
                if !available.contains(&neighbor) || expander.contains(&neighbor) {
                    continue;
                }

                // Check expansion property
                let mut test_set = expander.clone();
                test_set.insert(neighbor);

                let volume: usize = test_set.iter().map(|&u| self.degree(u)).sum();
                let boundary = self.count_boundary(&test_set);

                let expansion = if volume > 0 {
                    boundary as f64 / volume as f64
                } else {
                    0.0
                };

                // Add if it maintains reasonable expansion
                if expansion >= phi * 0.5 || expander.len() < 3 {
                    expander.insert(neighbor);
                    queue.push_back(neighbor);
                }
            }
        }

        expander
    }

    /// Handle edge insertion with subpolynomial update
    fn handle_edge_insert(&mut self, u: VertexId, v: VertexId, weight: Weight) -> u64 {
        let mut total_recourse = 0u64;

        // Find affected expanders at each level
        for level_idx in 0..self.levels.len() {
            let recourse = self.update_level_for_insert(level_idx, u, v, weight);
            total_recourse += recourse;

            if level_idx < self.levels.len() {
                self.levels[level_idx].recourse += recourse;
            }
        }

        // Update minimum cut
        self.update_min_cut_incremental(u, v, true);

        total_recourse
    }

    /// Handle edge deletion with subpolynomial update
    fn handle_edge_delete(&mut self, u: VertexId, v: VertexId) -> u64 {
        let mut total_recourse = 0u64;

        // Find affected expanders at each level
        for level_idx in 0..self.levels.len() {
            let recourse = self.update_level_for_delete(level_idx, u, v);
            total_recourse += recourse;

            if level_idx < self.levels.len() {
                self.levels[level_idx].recourse += recourse;
            }
        }

        // Update minimum cut
        self.update_min_cut_incremental(u, v, false);

        total_recourse
    }

    /// Update a level for edge insertion
    fn update_level_for_insert(
        &mut self,
        level_idx: usize,
        u: VertexId,
        v: VertexId,
        _weight: Weight,
    ) -> u64 {
        if level_idx >= self.levels.len() {
            return 0;
        }

        let mut recourse = 0u64;

        // Get expanders containing u and v
        let exp_u = self.levels[level_idx].vertex_expander.get(&u).copied();
        let exp_v = self.levels[level_idx].vertex_expander.get(&v).copied();

        match (exp_u, exp_v) {
            (Some(eu), Some(ev)) if eu == ev => {
                // Same expander - just update internal properties
                recourse += 1;
                self.update_expander_properties(level_idx, eu);
            }
            (Some(eu), Some(ev)) => {
                // Different expanders - update both
                recourse += 2;
                self.update_expander_properties(level_idx, eu);
                self.update_expander_properties(level_idx, ev);
            }
            (Some(eu), None) => {
                // Add v to expander containing u
                recourse += self.try_add_to_expander(level_idx, v, eu);
            }
            (None, Some(ev)) => {
                // Add u to expander containing v
                recourse += self.try_add_to_expander(level_idx, u, ev);
            }
            (None, None) => {
                // Create new expander for both vertices
                recourse += self.create_new_expander(level_idx, &[u, v]);
            }
        }

        recourse
    }

    /// Update a level for edge deletion
    fn update_level_for_delete(&mut self, level_idx: usize, u: VertexId, v: VertexId) -> u64 {
        if level_idx >= self.levels.len() {
            return 0;
        }

        let mut recourse = 0u64;

        // Get expanders containing u and v
        let exp_u = self.levels[level_idx].vertex_expander.get(&u).copied();
        let exp_v = self.levels[level_idx].vertex_expander.get(&v).copied();

        if let (Some(eu), Some(ev)) = (exp_u, exp_v) {
            if eu == ev {
                // Same expander - check if it needs to split
                recourse += self.check_and_split_expander(level_idx, eu);
            } else {
                // Different expanders - update boundary
                recourse += 2;
                self.update_expander_properties(level_idx, eu);
                self.update_expander_properties(level_idx, ev);
            }
        }

        recourse
    }

    /// Update properties of an expander
    fn update_expander_properties(&mut self, level_idx: usize, exp_id: u64) {
        if level_idx >= self.levels.len() {
            return;
        }

        // Get vertices and phi first
        let (vertices, phi) = match self.levels[level_idx].expanders.get(&exp_id) {
            Some(e) => (e.vertices.clone(), self.levels[level_idx].phi),
            None => return,
        };

        let volume: usize = vertices.iter().map(|&v| self.degree(v)).sum();
        let boundary_size = self.count_boundary(&vertices);

        // Check if still valid expander
        let expansion = if volume > 0 {
            boundary_size as f64 / volume as f64
        } else {
            0.0
        };
        let is_valid = expansion >= phi * 0.3;

        if let Some(expander) = self.levels[level_idx].expanders.get_mut(&exp_id) {
            expander.volume = volume;
            expander.boundary_size = boundary_size;
            expander.is_valid_expander = is_valid;
        }
    }

    /// Try to add a vertex to an expander
    fn try_add_to_expander(&mut self, level_idx: usize, v: VertexId, exp_id: u64) -> u64 {
        if level_idx >= self.levels.len() {
            return 0;
        }

        // Check if adding would violate expansion
        let (can_add, volume, boundary) = {
            let level = &self.levels[level_idx];
            if let Some(expander) = level.expanders.get(&exp_id) {
                let mut test_vertices = expander.vertices.clone();
                test_vertices.insert(v);

                let volume: usize = test_vertices.iter().map(|&u| self.degree(u)).sum();
                let boundary = self.count_boundary(&test_vertices);

                let expansion = if volume > 0 {
                    boundary as f64 / volume as f64
                } else {
                    0.0
                };

                (expansion >= level.phi * 0.3, volume, boundary)
            } else {
                (false, 0, 0)
            }
        };

        if can_add {
            let level = &mut self.levels[level_idx];
            if let Some(expander) = level.expanders.get_mut(&exp_id) {
                expander.vertices.insert(v);
                expander.volume = volume;
                expander.boundary_size = boundary;
            }
            level.vertex_expander.insert(v, exp_id);
            1
        } else {
            self.create_new_expander(level_idx, &[v])
        }
    }

    /// Create a new expander for vertices
    fn create_new_expander(&mut self, level_idx: usize, vertices: &[VertexId]) -> u64 {
        if level_idx >= self.levels.len() {
            return 0;
        }

        let vertex_set: HashSet<_> = vertices.iter().copied().collect();
        let volume: usize = vertex_set.iter().map(|&v| self.degree(v)).sum();
        let boundary_size = self.count_boundary(&vertex_set);

        let level = &mut self.levels[level_idx];
        let id = level.next_id;
        level.next_id += 1;

        let expander = LevelExpander {
            id,
            vertices: vertex_set.clone(),
            boundary_size,
            volume,
            internal_min_cut: f64::INFINITY,
            is_valid_expander: true,
            parent_id: None,
            children_ids: Vec::new(),
        };

        for &v in &vertex_set {
            level.vertex_expander.insert(v, id);
        }

        level.expanders.insert(id, expander);

        vertices.len() as u64
    }

    /// Check if an expander needs to split after edge deletion
    fn check_and_split_expander(&mut self, level_idx: usize, exp_id: u64) -> u64 {
        if level_idx >= self.levels.len() {
            return 0;
        }

        // Check expansion property
        let needs_split = {
            let level = &self.levels[level_idx];
            if let Some(expander) = level.expanders.get(&exp_id) {
                let expansion = if expander.volume > 0 {
                    expander.boundary_size as f64 / expander.volume as f64
                } else {
                    0.0
                };
                expansion < level.phi * 0.2
            } else {
                false
            }
        };

        if needs_split {
            // For now, just mark as invalid and update properties
            // A full split would require more complex logic
            self.update_expander_properties(level_idx, exp_id);
            2
        } else {
            self.update_expander_properties(level_idx, exp_id);
            1
        }
    }

    /// Update minimum cut incrementally
    fn update_min_cut_incremental(&mut self, u: VertexId, v: VertexId, is_insert: bool) {
        // Use LocalKCut for local cut discovery
        if let Some(ref lkc) = self.local_kcut {
            let cuts_u = lkc.query(u);
            let cuts_v = lkc.query(v);

            let mut min_local = f64::INFINITY;

            for cut in cuts_u.iter().chain(cuts_v.iter()) {
                if cut.cut_value < min_local {
                    min_local = cut.cut_value;
                }
            }

            if is_insert {
                // Edge insertion can only increase cuts
                // But might enable new paths that reduce other cuts
                self.current_min_cut = self.current_min_cut.min(min_local);
            } else {
                // Edge deletion might decrease the min cut
                if min_local < self.current_min_cut * 1.5 {
                    // Need to verify more carefully
                    self.recompute_min_cut();
                }
            }
        } else {
            // Fallback to full recomputation
            self.recompute_min_cut();
        }
    }

    /// Recompute the minimum cut from scratch
    fn recompute_min_cut(&mut self) {
        if self.edges.is_empty() {
            self.current_min_cut = f64::INFINITY;
            return;
        }

        let mut min_cut = f64::INFINITY;

        // Check all level boundaries
        for level in &self.levels {
            for expander in level.expanders.values() {
                // Boundary cut value
                let boundary_cut = expander.boundary_size as f64;
                min_cut = min_cut.min(boundary_cut);

                // Internal cut (from cached value)
                min_cut = min_cut.min(expander.internal_min_cut);
            }
        }

        // Also query LocalKCut for local cuts
        if let Some(ref lkc) = self.local_kcut {
            for v in self.adjacency.keys().take(10) {
                let cuts = lkc.query(*v);
                for cut in cuts {
                    min_cut = min_cut.min(cut.cut_value);
                }
            }
        }

        self.current_min_cut = min_cut;
    }

    /// Update recourse statistics
    fn update_recourse_stats(&mut self, recourse: u64, time_us: f64) {
        self.recourse_stats.total_recourse += recourse;
        self.recourse_stats.num_updates += 1;
        self.recourse_stats.max_single_recourse =
            self.recourse_stats.max_single_recourse.max(recourse);

        // Update average time
        let n = self.recourse_stats.num_updates as f64;
        self.recourse_stats.avg_update_time_us =
            (self.recourse_stats.avg_update_time_us * (n - 1.0) + time_us) / n;

        // Update per-level recourse
        self.recourse_stats.recourse_per_level = self.levels.iter().map(|l| l.recourse).collect();
    }

    // === Helper methods ===

    fn edge_key(u: VertexId, v: VertexId) -> (VertexId, VertexId) {
        if u < v {
            (u, v)
        } else {
            (v, u)
        }
    }

    fn get_weight(&self, u: VertexId, v: VertexId) -> Option<Weight> {
        self.adjacency.get(&u).and_then(|n| n.get(&v).copied())
    }

    fn degree(&self, v: VertexId) -> usize {
        self.adjacency.get(&v).map_or(0, |n| n.len())
    }

    fn neighbors(&self, v: VertexId) -> Vec<(VertexId, Weight)> {
        self.adjacency
            .get(&v)
            .map(|n| n.iter().map(|(&v, &w)| (v, w)).collect())
            .unwrap_or_default()
    }

    fn count_boundary(&self, vertices: &HashSet<VertexId>) -> usize {
        let mut boundary = 0;
        for &v in vertices {
            for (neighbor, _) in self.neighbors(v) {
                if !vertices.contains(&neighbor) {
                    boundary += 1;
                }
            }
        }
        boundary
    }

    fn expanders_adjacent_at_level(&self, level_idx: usize, exp1: u64, exp2: u64) -> bool {
        if level_idx >= self.levels.len() {
            return false;
        }

        let level = &self.levels[level_idx];

        let e1 = match level.expanders.get(&exp1) {
            Some(e) => e,
            None => return false,
        };
        let e2 = match level.expanders.get(&exp2) {
            Some(e) => e,
            None => return false,
        };

        // Check if any vertex in e1 has a neighbor in e2
        for &v in &e1.vertices {
            for (neighbor, _) in self.neighbors(v) {
                if e2.vertices.contains(&neighbor) {
                    return true;
                }
            }
        }
        false
    }

    // === Public API ===

    /// Get the current minimum cut value
    pub fn min_cut_value(&self) -> f64 {
        self.current_min_cut
    }

    /// Get detailed minimum cut result
    pub fn min_cut(&self) -> MinCutQueryResult {
        MinCutQueryResult {
            value: self.current_min_cut,
            cut_edges: None, // Would need more work to track
            partition: None,
            is_exact: true,
            complexity_verified: self.recourse_stats.is_subpolynomial(self.num_vertices),
        }
    }

    /// Get configuration
    pub fn config(&self) -> &SubpolyConfig {
        &self.config
    }

    /// Get number of vertices
    pub fn num_vertices(&self) -> usize {
        self.num_vertices
    }

    /// Get number of edges
    pub fn num_edges(&self) -> usize {
        self.num_edges
    }

    /// Get number of hierarchy levels
    pub fn num_levels(&self) -> usize {
        self.levels.len()
    }

    /// Get recourse statistics
    pub fn recourse_stats(&self) -> &RecourseStats {
        &self.recourse_stats
    }

    /// Get hierarchy statistics
    pub fn hierarchy_stats(&self) -> HierarchyStatistics {
        HierarchyStatistics {
            num_levels: self.levels.len(),
            expanders_per_level: self.levels.iter().map(|l| l.expanders.len()).collect(),
            total_expanders: self.levels.iter().map(|l| l.expanders.len()).sum(),
            avg_expander_size: if self.levels[0].expanders.is_empty() {
                0.0
            } else {
                self.levels[0]
                    .expanders
                    .values()
                    .map(|e| e.vertices.len())
                    .sum::<usize>() as f64
                    / self.levels[0].expanders.len() as f64
            },
        }
    }

    /// Check if updates are subpolynomial
    pub fn is_subpolynomial(&self) -> bool {
        self.recourse_stats.is_subpolynomial(self.num_vertices)
    }

    /// Certify cuts using LocalKCut verification
    pub fn certify_cuts(&mut self) {
        // First collect all expander info (exp_id, vertices)
        let mut expander_data: Vec<(usize, u64, HashSet<VertexId>)> = Vec::new();
        for level_idx in 0..self.levels.len() {
            for (&exp_id, expander) in &self.levels[level_idx].expanders {
                expander_data.push((level_idx, exp_id, expander.vertices.clone()));
            }
        }

        // Now process each expander with immutable self
        let mut updates: Vec<(usize, u64, f64)> = Vec::new();

        if let Some(ref lkc) = self.local_kcut {
            for (level_idx, exp_id, vertices) in &expander_data {
                // Sample boundary vertices
                let boundary_verts: Vec<_> = vertices
                    .iter()
                    .filter(|&&v| self.neighbors(v).iter().any(|(n, _)| !vertices.contains(n)))
                    .take(5)
                    .copied()
                    .collect();

                let mut min_internal_cut = f64::INFINITY;

                for v in boundary_verts {
                    let cuts = lkc.query(v);
                    for cut in cuts {
                        // Check if cut is internal to expander
                        let is_internal = cut.vertices.iter().all(|u| vertices.contains(u));

                        if is_internal {
                            min_internal_cut = min_internal_cut.min(cut.cut_value);
                        }
                    }
                }

                updates.push((*level_idx, *exp_id, min_internal_cut));
            }
        }

        // Now apply all updates
        for (level_idx, exp_id, min_cut) in updates {
            if let Some(expander) = self.levels[level_idx].expanders.get_mut(&exp_id) {
                expander.internal_min_cut = min_cut;
            }
        }
    }
}

/// Result of a minimum cut query
#[derive(Debug, Clone)]
pub struct MinCutQueryResult {
    /// The minimum cut value
    pub value: f64,
    /// Edges in the cut (if computed)
    pub cut_edges: Option<Vec<(VertexId, VertexId)>>,
    /// Partition (S, T) (if computed)
    pub partition: Option<(Vec<VertexId>, Vec<VertexId>)>,
    /// Whether this is an exact result
    pub is_exact: bool,
    /// Whether subpolynomial complexity is verified
    pub complexity_verified: bool,
}

/// Statistics about the hierarchy
#[derive(Debug, Clone)]
pub struct HierarchyStatistics {
    /// Number of levels
    pub num_levels: usize,
    /// Expanders at each level
    pub expanders_per_level: Vec<usize>,
    /// Total expanders across all levels
    pub total_expanders: usize,
    /// Average expander size at base level
    pub avg_expander_size: f64,
}

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

    #[test]
    fn test_subpoly_config_default() {
        let config = SubpolyConfig::default();
        assert!(config.phi > 0.0);
        assert!(config.lambda_max > 0);
        assert!(config.target_levels > 0);
    }

    #[test]
    fn test_subpoly_config_for_size() {
        let config = SubpolyConfig::for_size(1_000_000);
        assert!(config.phi < 0.1);
        assert!(config.lambda_max > 100);
        assert!(config.target_levels >= 2);
    }

    #[test]
    fn test_create_empty() {
        let mincut = SubpolynomialMinCut::new(SubpolyConfig::default());
        assert_eq!(mincut.num_vertices(), 0);
        assert_eq!(mincut.num_edges(), 0);
        assert_eq!(mincut.min_cut_value(), f64::INFINITY);
    }

    #[test]
    fn test_insert_edges() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        assert_eq!(mincut.num_vertices(), 3);
        assert_eq!(mincut.num_edges(), 3);
    }

    #[test]
    fn test_build_hierarchy() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Build a path graph
        for i in 0..10 {
            mincut.insert_edge(i, i + 1, 1.0).unwrap();
        }

        mincut.build();

        assert!(mincut.num_levels() >= 2);
        let stats = mincut.hierarchy_stats();
        assert!(stats.total_expanders > 0);
    }

    #[test]
    fn test_min_cut_triangle() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        mincut.build();

        assert!(mincut.min_cut_value() <= 2.0);
    }

    #[test]
    fn test_min_cut_bridge() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Two triangles connected by a bridge
        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        mincut.insert_edge(3, 4, 1.0).unwrap(); // Bridge

        mincut.insert_edge(4, 5, 1.0).unwrap();
        mincut.insert_edge(5, 6, 1.0).unwrap();
        mincut.insert_edge(6, 4, 1.0).unwrap();

        mincut.build();

        // Min cut should be 1 (the bridge)
        assert!(mincut.min_cut_value() <= 2.0);
    }

    #[test]
    fn test_incremental_updates() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Build initial graph
        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        mincut.build();

        let initial_cut = mincut.min_cut_value();

        // Add more edges
        mincut.insert_edge(3, 4, 1.0).unwrap();
        mincut.insert_edge(4, 5, 1.0).unwrap();

        // Cut might have changed
        assert!(mincut.min_cut_value() <= initial_cut * 2.0);

        // Check recourse tracking
        let stats = mincut.recourse_stats();
        assert!(stats.num_updates > 0);
    }

    #[test]
    fn test_delete_edge() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        mincut.build();

        mincut.delete_edge(1, 2).unwrap();

        assert_eq!(mincut.num_edges(), 2);
    }

    #[test]
    fn test_recourse_stats() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Build graph
        for i in 0..20 {
            mincut.insert_edge(i, i + 1, 1.0).unwrap();
        }

        mincut.build();

        // Do updates
        mincut.insert_edge(0, 10, 1.0).unwrap();
        mincut.insert_edge(5, 15, 1.0).unwrap();

        let stats = mincut.recourse_stats();
        assert!(stats.num_updates >= 2);
        assert!(stats.amortized_recourse() >= 0.0);
    }

    #[test]
    fn test_is_subpolynomial() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Build small graph
        for i in 0..10 {
            mincut.insert_edge(i, i + 1, 1.0).unwrap();
        }

        mincut.build();

        // Do some updates
        mincut.insert_edge(0, 5, 1.0).unwrap();

        // Should be subpolynomial for small graph
        assert!(mincut.is_subpolynomial());
    }

    #[test]
    fn test_certify_cuts() {
        let mut mincut = SubpolynomialMinCut::new(SubpolyConfig::default());

        // Build graph
        mincut.insert_edge(1, 2, 1.0).unwrap();
        mincut.insert_edge(2, 3, 1.0).unwrap();
        mincut.insert_edge(3, 1, 1.0).unwrap();

        mincut.build();
        mincut.certify_cuts();

        // Should complete without panic
    }

    #[test]
    fn test_large_graph() {
        let mut mincut = SubpolynomialMinCut::for_size(1000);

        // Build a larger graph
        for i in 0..100 {
            mincut.insert_edge(i, i + 1, 1.0).unwrap();
        }
        // Add some cross edges
        for i in 0..10 {
            mincut.insert_edge(i * 10, i * 10 + 50, 1.0).unwrap();
        }

        mincut.build();

        let stats = mincut.hierarchy_stats();
        assert!(stats.num_levels >= 2);

        // Test updates
        mincut.insert_edge(25, 75, 1.0).unwrap();

        assert!(mincut.recourse_stats().num_updates > 0);
    }
}