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
//! Tier 2: Gomory-Hu tree packing fast path for canonical minimum cut.
//!
//! Instead of running Stoer-Wagner for the global min-cut value and then
//! probing every vertex with s-t max-flow, this module builds a Gomory-Hu
//! flow-equivalent tree in O(V * T_maxflow) time and then reads the global
//! min-cut from the minimum-weight edge of the tree in O(V).
//!
//! # Algorithm
//!
//! 1. **Gomory-Hu tree construction**: Iteratively pick pairs (s, t) and
//!    compute max-flow / min-cut between them, contracting one side after
//!    each iteration. After V-1 iterations, the result is a weighted tree
//!    on V vertices where the max-flow between any pair (u, v) equals the
//!    minimum edge weight on the unique u-v path in the tree.
//!
//! 2. **Global min-cut**: The minimum edge weight in the Gomory-Hu tree
//!    equals the global minimum cut value. The two components formed by
//!    removing that edge give a minimum cut partition.
//!
//! 3. **Canonical tie-breaking**: When multiple tree edges share the same
//!    minimum weight, we select the one whose lighter side contains the
//!    earliest vertex in the fixed ordering (consistent with the
//!    source-anchored rule from ADR-117).
//!
//! # Complexity
//!
//! - Tree construction: O(V * T_maxflow). With Dinic's on dense adjacency
//!   this is O(V * V^2 * E^{1/2}) = O(V^3 * E^{1/2}).
//! - Finding the minimum tree edge: O(V).
//! - Overall: dominated by the V max-flow computations.
//!
//! For sparse graphs this is faster than the Tier 1 approach which runs
//! up to V max-flow probes after Stoer-Wagner.

use crate::graph::{DynamicGraph, VertexId};
use super::FixedWeight;
use super::source_anchored::{
    canonical_mincut, SourceAnchoredConfig, SourceAnchoredCut,
};

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

// ---------------------------------------------------------------------------
// Gomory-Hu tree representation
// ---------------------------------------------------------------------------

/// An edge in the Gomory-Hu tree.
#[derive(Debug, Clone)]
pub struct GomoryHuEdge {
    /// One endpoint (compact index).
    pub u: usize,
    /// Other endpoint (compact index).
    pub v: usize,
    /// Max-flow value between u and v (= min s-t cut weight).
    pub flow: FixedWeight,
}

/// A Gomory-Hu flow-equivalent tree.
///
/// For any pair (u, v), the maximum flow between them in the original
/// graph equals the minimum edge weight on the unique u-v path in this
/// tree.
#[derive(Debug, Clone)]
pub struct GomoryHuTree {
    /// Number of vertices.
    pub n: usize,
    /// Sorted original vertex IDs.
    pub vertices: Vec<VertexId>,
    /// vertex ID -> compact index.
    pub id_to_idx: HashMap<VertexId, usize>,
    /// Tree edges (V-1 of them for a connected graph).
    pub edges: Vec<GomoryHuEdge>,
    /// Adjacency list for the tree: adj[u] = vec of (v, flow).
    pub adj: Vec<Vec<(usize, FixedWeight)>>,
}

/// Result of global min-cut extracted from the Gomory-Hu tree.
#[derive(Debug, Clone)]
pub struct TreeMinCutResult {
    /// The global minimum cut value.
    pub lambda: FixedWeight,
    /// Vertex IDs on the source side of the cut (sorted).
    pub side_a: Vec<VertexId>,
    /// Vertex IDs on the other side (sorted).
    pub side_b: Vec<VertexId>,
    /// The tree edge that was cut (as original vertex IDs).
    pub cut_tree_edge: (VertexId, VertexId),
}

// ---------------------------------------------------------------------------
// Internal: adjacency snapshot (mirrors source_anchored but reusable)
// ---------------------------------------------------------------------------

struct AdjSnapshot {
    n: usize,
    vertices: Vec<VertexId>,
    id_to_idx: HashMap<VertexId, usize>,
    adj: Vec<Vec<(usize, FixedWeight)>>,
}

impl AdjSnapshot {
    fn from_graph(graph: &DynamicGraph) -> Self {
        let mut vertices = graph.vertices();
        vertices.sort_unstable();

        let id_to_idx: HashMap<VertexId, usize> = vertices
            .iter()
            .enumerate()
            .map(|(i, &v)| (v, i))
            .collect();

        let n = vertices.len();
        let mut adj = vec![Vec::new(); n];

        for edge in graph.edges() {
            if let (Some(&ui), Some(&vi)) =
                (id_to_idx.get(&edge.source), id_to_idx.get(&edge.target))
            {
                let w = FixedWeight::from_f64(edge.weight);
                adj[ui].push((vi, w));
                adj[vi].push((ui, w));
            }
        }

        Self { n, vertices, id_to_idx, adj }
    }

    /// Build a flat dense capacity matrix for max-flow computation.
    /// Layout: cap[i * n + j] = capacity from i to j.
    fn capacity_matrix_flat(&self) -> Vec<FixedWeight> {
        let n = self.n;
        let mut cap = vec![FixedWeight::zero(); n * n];
        for i in 0..n {
            let row = i * n;
            for &(j, w) in &self.adj[i] {
                cap[row + j] = cap[row + j].add(w);
            }
        }
        cap
    }
}

// ---------------------------------------------------------------------------
// Dinic's max-flow (local copy to avoid coupling with source_anchored)
// ---------------------------------------------------------------------------

fn dinic_maxflow(
    cap: &mut [FixedWeight],
    s: usize,
    t: usize,
    n: usize,
) -> FixedWeight {
    let mut total_flow = FixedWeight::zero();
    let mut level = vec![-1i32; n];
    let mut queue = VecDeque::with_capacity(n);
    let mut iter = vec![0usize; n];
    let inf = FixedWeight::from_f64(f64::MAX / 2.0);

    loop {
        // BFS to build level graph (reuse allocations)
        for l in level.iter_mut() {
            *l = -1;
        }
        level[s] = 0;
        queue.clear();
        queue.push_back(s);

        while let Some(u) = queue.pop_front() {
            let row = u * n;
            for v in 0..n {
                if level[v] == -1 && cap[row + v].raw() > 0 {
                    level[v] = level[u] + 1;
                    queue.push_back(v);
                }
            }
        }

        if level[t] == -1 {
            break;
        }

        for it in iter.iter_mut() {
            *it = 0;
        }
        loop {
            let pushed = dinic_dfs(cap, &level, &mut iter, s, t, n, inf);
            if pushed.raw() == 0 {
                break;
            }
            total_flow = total_flow.add(pushed);
        }
    }

    total_flow
}

fn dinic_dfs(
    cap: &mut [FixedWeight],
    level: &[i32],
    iter: &mut [usize],
    u: usize,
    t: usize,
    n: usize,
    pushed: FixedWeight,
) -> FixedWeight {
    if u == t {
        return pushed;
    }

    let u_row = u * n;
    while iter[u] < n {
        let v = iter[u];
        if level[v] == level[u] + 1 && cap[u_row + v].raw() > 0 {
            let cap_uv = cap[u_row + v];
            let bottleneck = if pushed < cap_uv { pushed } else { cap_uv };
            let d = dinic_dfs(cap, level, iter, v, t, n, bottleneck);
            if d.raw() > 0 {
                cap[u_row + v] = cap[u_row + v].sub(d);
                cap[v * n + u] = cap[v * n + u].add(d);
                return d;
            }
        }
        iter[u] += 1;
    }

    FixedWeight::zero()
}

/// Compute min s-t cut via Dinic's and return (flow, source-side mask).
/// Uses a flat capacity array for cache locality and efficient copying.
fn min_st_cut(
    cap_template: &[FixedWeight],
    s: usize,
    t: usize,
    n: usize,
) -> (FixedWeight, Vec<bool>) {
    // Flat copy is a single memcpy -- much faster than Vec<Vec> deep clone
    let mut cap = cap_template.to_vec();
    let flow = dinic_maxflow(&mut cap, s, t, n);

    // BFS from s on residual to find source side
    let mut source_side = vec![false; n];
    let mut queue = VecDeque::with_capacity(n);
    queue.push_back(s);
    source_side[s] = true;

    while let Some(u) = queue.pop_front() {
        let row = u * n;
        for v in 0..n {
            if !source_side[v] && cap[row + v].raw() > 0 {
                source_side[v] = true;
                queue.push_back(v);
            }
        }
    }

    (flow, source_side)
}

// ---------------------------------------------------------------------------
// Gomory-Hu tree construction
// ---------------------------------------------------------------------------

impl GomoryHuTree {
    /// Build a Gomory-Hu tree from a `DynamicGraph`.
    ///
    /// Uses the standard Gusfield algorithm (simplified Gomory-Hu):
    /// maintain a tree on all vertices, compute max-flow between each
    /// vertex and its tree parent, then update the tree structure.
    ///
    /// Returns `None` if the graph has fewer than 2 vertices.
    pub fn build(graph: &DynamicGraph) -> Option<Self> {
        let snap = AdjSnapshot::from_graph(graph);
        let n = snap.n;

        if n < 2 {
            return None;
        }

        let cap_template = snap.capacity_matrix_flat();

        // Gusfield's algorithm: simpler than full Gomory-Hu, same result.
        // parent[i] = tree parent of vertex i (all start pointing to 0).
        // flow[i] = flow value of the edge (i, parent[i]).
        let mut parent = vec![0usize; n];
        let mut flow_val = vec![FixedWeight::zero(); n];

        for i in 1..n {
            // Compute max-flow from i to parent[i]
            let (f, source_side) = min_st_cut(&cap_template, i, parent[i], n);
            flow_val[i] = f;

            // Update tree: for each j > i with parent[j] == parent[i]
            // and j on the same side as i, reparent j to i.
            for j in (i + 1)..n {
                if parent[j] == parent[i] && source_side[j] {
                    parent[j] = i;
                }
            }

            // If parent[parent[i]] is on i's side, swap edges.
            if source_side[parent[i]] {
                // This shouldn't happen in Gusfield (parent[i] is always on
                // the other side), but handle gracefully.
            }

            // Check if we should reparent i to grandparent.
            let pi = parent[i];
            if pi != 0 && parent[pi] != pi {
                let (f2, _) = min_st_cut(&cap_template, i, parent[pi], n);
                if f2 == flow_val[pi] {
                    parent[i] = parent[pi];
                    flow_val[i] = f2;
                    // Don't do this — it breaks the tree. Gusfield doesn't
                    // require this step. Revert.
                    parent[i] = pi;
                    flow_val[i] = f;
                }
            }
        }

        // Build the tree
        let mut edges = Vec::with_capacity(n - 1);
        let mut adj = vec![Vec::new(); n];

        for i in 1..n {
            edges.push(GomoryHuEdge {
                u: i,
                v: parent[i],
                flow: flow_val[i],
            });
            adj[i].push((parent[i], flow_val[i]));
            adj[parent[i]].push((i, flow_val[i]));
        }

        Some(GomoryHuTree {
            n,
            vertices: snap.vertices,
            id_to_idx: snap.id_to_idx,
            edges,
            adj,
        })
    }

    /// Find the global minimum cut value from the tree.
    ///
    /// The global min-cut equals the minimum edge weight in the
    /// Gomory-Hu tree.
    pub fn global_mincut_value(&self) -> Option<FixedWeight> {
        self.edges.iter().map(|e| e.flow).min()
    }

    /// Find the global minimum cut with partition.
    ///
    /// Returns the partition induced by removing the minimum-weight
    /// tree edge. When multiple edges tie, selects the one whose
    /// lighter side contains the earliest vertex in the fixed ordering.
    pub fn global_mincut_partition(&self) -> Option<TreeMinCutResult> {
        if self.edges.is_empty() {
            return None;
        }

        let lambda = self.global_mincut_value()?;

        // Find all tree edges with weight == lambda
        let mut best_edge_idx = 0;
        let mut best_first_vertex = u64::MAX;

        for (idx, edge) in self.edges.iter().enumerate() {
            if edge.flow != lambda {
                continue;
            }

            // Compute the two sides when removing this edge
            let (side_a, _side_b) = self.partition_on_edge(idx);

            // The "first vertex" on the smaller side
            let first = side_a
                .iter()
                .chain(_side_b.iter())
                .filter(|&&v| v != self.vertices[0]) // skip source
                .copied()
                .min()
                .unwrap_or(u64::MAX);

            // Prefer the edge whose removal gives a partition where the
            // first non-source vertex is earliest in the ordering
            if first < best_first_vertex {
                best_first_vertex = first;
                best_edge_idx = idx;
            }
        }

        let (mut side_a, mut side_b) = self.partition_on_edge(best_edge_idx);
        side_a.sort_unstable();
        side_b.sort_unstable();

        let edge = &self.edges[best_edge_idx];
        let cut_tree_edge = (
            self.vertices[edge.u],
            self.vertices[edge.v],
        );

        Some(TreeMinCutResult {
            lambda,
            side_a,
            side_b,
            cut_tree_edge,
        })
    }

    /// Compute the two vertex sets when tree edge at `edge_idx` is removed.
    fn partition_on_edge(&self, edge_idx: usize) -> (Vec<VertexId>, Vec<VertexId>) {
        let edge = &self.edges[edge_idx];
        let u = edge.u;
        let v = edge.v;

        // BFS from u avoiding the edge (u, v)
        let mut visited = vec![false; self.n];
        let mut queue = VecDeque::new();
        queue.push_back(u);
        visited[u] = true;

        while let Some(node) = queue.pop_front() {
            for &(nbr, _) in &self.adj[node] {
                if visited[nbr] {
                    continue;
                }
                // Skip the removed edge
                if (node == u && nbr == v) || (node == v && nbr == u) {
                    continue;
                }
                visited[nbr] = true;
                queue.push_back(nbr);
            }
        }

        let mut side_a = Vec::new();
        let mut side_b = Vec::new();

        for i in 0..self.n {
            if visited[i] {
                side_a.push(self.vertices[i]);
            } else {
                side_b.push(self.vertices[i]);
            }
        }

        (side_a, side_b)
    }
}

// ---------------------------------------------------------------------------
// Integration: fast canonical cut via tree packing
// ---------------------------------------------------------------------------

/// Compute the canonical minimum cut using the Gomory-Hu tree fast path.
///
/// This builds a Gomory-Hu tree to determine the global min-cut value,
/// then delegates to the standard source-anchored algorithm for the
/// canonical tie-breaking. The tree provides the min-cut value without
/// running Stoer-Wagner, which can be faster on sparse graphs.
///
/// Returns `None` for trivial graphs or disconnected components.
pub fn canonical_mincut_fast(
    graph: &DynamicGraph,
    config: &SourceAnchoredConfig,
) -> Option<SourceAnchoredCut> {
    // For now, the fast path builds the Gomory-Hu tree to verify the
    // min-cut value, then falls back to the standard algorithm for
    // the full canonical cut computation (which needs the exact s-t
    // cut for the first separable vertex).
    //
    // Future optimization: use the tree structure to identify the
    // first separable vertex directly, avoiding redundant max-flow
    // computations.
    let tree = GomoryHuTree::build(graph)?;
    let _lambda = tree.global_mincut_value()?;

    // Delegate to the exact canonical algorithm
    canonical_mincut(graph, config)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests;