scirs2-graph 0.4.2

Graph processing module for SciRS2 (scirs2-graph)
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
//! Additional network reliability algorithms: factoring-based exact computation,
//! k-connectivity, and reliability bounds.
//!
//! These supplement the Monte-Carlo and BDD methods in `network_reliability.rs`.

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

// ─────────────────────────────────────────────────────────────────────────────
// All-terminal reliability via factoring
// ─────────────────────────────────────────────────────────────────────────────

/// Computes all-terminal reliability by the factoring algorithm:
/// R(G) = p · R(G/e) + (1 − p) · R(G − e)
///
/// where G/e contracts edge e and G − e deletes edge e.
/// Base cases: R(K_n) = 1, R(disconnected) = 0.
///
/// Memoisation is keyed on the canonical edge-sorted representation of the
/// current multi-graph.
///
/// # Arguments
/// * `edges`   – undirected edge list (may contain parallel edges)
/// * `n_nodes` – number of vertices
/// * `p`       – per-edge survival probability
pub fn all_terminal_reliability_factoring(
    edges: &[(usize, usize)],
    n_nodes: usize,
    p: f64,
) -> f64 {
    if n_nodes <= 1 {
        return 1.0;
    }
    // Normalise edge representation (remove self-loops, canonicalise direction)
    let clean: Vec<(usize, usize)> = edges
        .iter()
        .filter(|&&(u, v)| u != v && u < n_nodes && v < n_nodes)
        .map(|&(u, v)| if u < v { (u, v) } else { (v, u) })
        .collect();

    if !is_connected(&clean, n_nodes) {
        return 0.0;
    }

    let mut memo: HashMap<Vec<(usize, usize)>, f64> = HashMap::new();
    factoring_recurse(&clean, n_nodes, p, &mut memo)
}

fn factoring_recurse(
    edges: &[(usize, usize)],
    n_nodes: usize,
    p: f64,
    memo: &mut HashMap<Vec<(usize, usize)>, f64>,
) -> f64 {
    // Sort for canonical key
    let mut sorted = edges.to_vec();
    sorted.sort_unstable();

    if let Some(&cached) = memo.get(&sorted) {
        return cached;
    }

    // Base cases
    if n_nodes <= 1 {
        let result = 1.0;
        memo.insert(sorted, result);
        return result;
    }
    if !is_connected(edges, n_nodes) {
        memo.insert(sorted, 0.0);
        return 0.0;
    }
    if edges.is_empty() {
        // Connected with no edges only possible for n=1
        let result = if n_nodes == 1 { 1.0 } else { 0.0 };
        memo.insert(sorted, result);
        return result;
    }

    // Pick first edge to factor on
    let (eu, ev) = sorted[0];
    let rest: Vec<(usize, usize)> = sorted[1..].to_vec();

    // G − e: delete edge (eu, ev)
    let rel_delete = factoring_recurse(&rest, n_nodes, p, memo);

    // G / e: contract edge (eu, ev) — merge ev into eu
    let contracted = contract_edge(&rest, n_nodes, eu, ev);
    let new_n = n_nodes - 1; // one vertex merged away
    let rel_contract = factoring_recurse(&contracted, new_n, p, memo);

    let result = p * rel_contract + (1.0 - p) * rel_delete;
    memo.insert(sorted, result);
    result
}

/// Contract edge (eu, ev): replace all occurrences of ev with eu and
/// remove resulting self-loops.
fn contract_edge(
    edges: &[(usize, usize)],
    _n_nodes: usize,
    keep: usize,
    remove: usize,
) -> Vec<(usize, usize)> {
    // Remap: vertices > remove shift down by 1
    let remap = |v: usize| -> usize {
        let v2 = if v == remove { keep } else { v };
        // Shift down vertices that are above the removed vertex
        if v2 > remove { v2 - 1 } else { v2 }
    };
    let keep_mapped = remap(keep);

    edges
        .iter()
        .filter_map(|&(u, v)| {
            let u2 = remap(u);
            let v2 = remap(v);
            if u2 == v2 {
                None // self-loop
            } else {
                let e = if u2 < v2 { (u2, v2) } else { (v2, u2) };
                Some(e)
            }
        })
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Two-terminal reliability via path enumeration
// ─────────────────────────────────────────────────────────────────────────────

/// Computes two-terminal reliability P(s connected to t) using inclusion-
/// exclusion over all simple paths between s and t (exact for small graphs).
///
/// For larger graphs (many paths) an upper bound is returned based on the
/// most reliable single path.
pub fn two_terminal_reliability(
    edges: &[(usize, usize)],
    n_nodes: usize,
    source: usize,
    target: usize,
    p: f64,
) -> f64 {
    if source == target {
        return 1.0;
    }
    if source >= n_nodes || target >= n_nodes {
        return 0.0;
    }
    let adj = build_adj(edges, n_nodes);
    // Enumerate simple paths (up to a limit)
    let paths = enumerate_simple_paths(&adj, source, target, n_nodes, 500);
    if paths.is_empty() {
        return 0.0;
    }
    // Inclusion-exclusion over path events
    // For large numbers of paths, use an approximation
    if paths.len() <= 20 {
        inclusion_exclusion_reliability(&paths, edges, n_nodes, p)
    } else {
        // Upper bound: 1 − Π(1 − P(path_i reliable))
        let approx: f64 = paths
            .iter()
            .map(|path| {
                path.windows(2)
                    .map(|w| {
                        if edge_exists(edges, w[0], w[1]) { p } else { 0.0 }
                    })
                    .product::<f64>()
            })
            .fold(1.0f64, |acc, pp| acc * (1.0 - pp));
        1.0 - approx
    }
}

/// Exact inclusion-exclusion over path events.
fn inclusion_exclusion_reliability(
    paths: &[Vec<usize>],
    edges: &[(usize, usize)],
    n_nodes: usize,
    p: f64,
) -> f64 {
    let m = paths.len();
    let mut result = 0.0f64;
    for mask in 1u64..(1u64 << m) {
        // Collect union of edges in the selected paths
        let mut edge_set: std::collections::HashSet<(usize, usize)> =
            std::collections::HashSet::new();
        let mut count = 0u32;
        for i in 0..m {
            if mask & (1 << i) != 0 {
                count += 1;
                let path = &paths[i];
                for w in path.windows(2) {
                    let e = if w[0] < w[1] { (w[0], w[1]) } else { (w[1], w[0]) };
                    edge_set.insert(e);
                }
            }
        }
        // Probability all edges in the union survive
        let prob: f64 = edge_set
            .iter()
            .map(|&(u, v)| if edge_exists(edges, u, v) { p } else { 0.0 })
            .product();
        // Inclusion-exclusion sign
        if count % 2 == 1 {
            result += prob;
        } else {
            result -= prob;
        }
    }
    // Clamp to [0,1]
    result.max(0.0).min(1.0)
}

fn edge_exists(edges: &[(usize, usize)], u: usize, v: usize) -> bool {
    edges.iter().any(|&(a, b)| (a == u && b == v) || (a == v && b == u))
}

/// Enumerate simple paths from `src` to `dst` (up to `limit` paths).
fn enumerate_simple_paths(
    adj: &[Vec<usize>],
    src: usize,
    dst: usize,
    n: usize,
    limit: usize,
) -> Vec<Vec<usize>> {
    let mut paths = Vec::new();
    let mut visited = vec![false; n];
    let mut current_path = vec![src];
    visited[src] = true;
    dfs_paths(adj, src, dst, &mut visited, &mut current_path, &mut paths, limit);
    paths
}

fn dfs_paths(
    adj: &[Vec<usize>],
    v: usize,
    dst: usize,
    visited: &mut Vec<bool>,
    path: &mut Vec<usize>,
    paths: &mut Vec<Vec<usize>>,
    limit: usize,
) {
    if paths.len() >= limit {
        return;
    }
    if v == dst {
        paths.push(path.clone());
        return;
    }
    for &w in &adj[v] {
        if !visited[w] {
            visited[w] = true;
            path.push(w);
            dfs_paths(adj, w, dst, visited, path, paths, limit);
            path.pop();
            visited[w] = false;
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Reliability polynomial (exact, spanning subgraphs)
// ─────────────────────────────────────────────────────────────────────────────

/// Computes the coefficients of the reliability polynomial R(G, p) =
/// Σ_i  c_i · p^i  for small graphs via spanning subgraph enumeration.
///
/// Returns coefficient vector `[c_0, c_1, ..., c_m]` (length = m+1 where
/// m = number of edges).
///
/// # Complexity
/// O(2^m · (N + M)) — practical only for m ≤ 25.
pub fn reliability_polynomial(edges: &[(usize, usize)], n_nodes: usize) -> Vec<f64> {
    let clean: Vec<(usize, usize)> = edges
        .iter()
        .filter(|&&(u, v)| u != v && u < n_nodes && v < n_nodes)
        .map(|&(u, v)| if u < v { (u, v) } else { (v, u) })
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    let m = clean.len();
    if m > 25 {
        // Too large for exact enumeration — return empty
        return vec![];
    }
    let mut coeffs = vec![0i64; m + 1];
    for mask in 0u32..(1u32 << m) {
        let k = mask.count_ones() as usize;
        let sub: Vec<(usize, usize)> = (0..m)
            .filter(|&i| mask & (1 << i) != 0)
            .map(|i| clean[i])
            .collect();
        if is_connected_subset(&sub, n_nodes) {
            coeffs[k] += 1;
        }
    }
    // Polynomial: R(p) = sum_k coeffs[k] * p^k * (1-p)^(m-k)
    // Expand into standard polynomial coefficients
    let mut poly = vec![0.0f64; m + 1];
    for k in 0..=m {
        if coeffs[k] == 0 {
            continue;
        }
        // p^k * (1-p)^(m-k) = sum_j binom(m-k, j) (-1)^j p^{k+j}
        for j in 0..=(m - k) {
            let sign = if j % 2 == 0 { 1.0 } else { -1.0 };
            let binom = binom_coeff(m - k, j) as f64;
            poly[k + j] += coeffs[k] as f64 * sign * binom;
        }
    }
    poly
}

fn binom_coeff(n: usize, k: usize) -> u64 {
    if k > n {
        return 0;
    }
    if k == 0 || k == n {
        return 1;
    }
    let k = k.min(n - k);
    let mut result = 1u64;
    for i in 0..k {
        result = result * (n - i) as u64 / (i + 1) as u64;
    }
    result
}

fn is_connected_subset(edges: &[(usize, usize)], n: usize) -> bool {
    if n <= 1 {
        return true;
    }
    is_connected(edges, n)
}

// ─────────────────────────────────────────────────────────────────────────────
// Reliability bound via minimum cut
// ─────────────────────────────────────────────────────────────────────────────

/// Returns an upper bound on all-terminal reliability:
/// R(G) ≤ p^{λ} where λ is the edge-connectivity.
pub fn min_cut_reliability_bound(edges: &[(usize, usize)], n_nodes: usize, p: f64) -> f64 {
    let lambda = k_edge_connectivity(edges, n_nodes);
    p.powi(lambda as i32)
}

// ─────────────────────────────────────────────────────────────────────────────
// Edge and vertex connectivity
// ─────────────────────────────────────────────────────────────────────────────

/// Returns the edge connectivity κ'(G): minimum number of edges whose removal
/// disconnects the graph.
///
/// Computed by repeated max-flow (Ford-Fulkerson) from a fixed source to every
/// other vertex; the minimum flow value is the edge connectivity.
pub fn k_edge_connectivity(edges: &[(usize, usize)], n_nodes: usize) -> usize {
    if n_nodes <= 1 {
        return 0;
    }
    if !is_connected(edges, n_nodes) {
        return 0;
    }
    // Edge connectivity = min over all t ≠ 0 of max_flow(0, t)
    let mut min_flow = usize::MAX;
    for t in 1..n_nodes {
        let f = max_flow_bfs(edges, n_nodes, 0, t);
        if f < min_flow {
            min_flow = f;
        }
    }
    if min_flow == usize::MAX { 0 } else { min_flow }
}

/// Returns the vertex connectivity κ(G): minimum number of vertices whose
/// removal disconnects the graph (or reduces it to a single vertex).
///
/// Uses node-splitting: each vertex v is split into v_in and v_out with a
/// unit-capacity edge; then max-flow gives the minimum vertex cut.
pub fn k_vertex_connectivity(edges: &[(usize, usize)], n_nodes: usize) -> usize {
    if n_nodes <= 1 {
        return 0;
    }
    if !is_connected(edges, n_nodes) {
        return 0;
    }
    // For complete graphs the vertex connectivity is n-1
    let edge_set: std::collections::HashSet<(usize, usize)> = edges
        .iter()
        .map(|&(u, v)| if u < v { (u, v) } else { (v, u) })
        .collect();
    let max_edges = n_nodes * (n_nodes - 1) / 2;
    if edge_set.len() == max_edges {
        return n_nodes - 1;
    }

    // Node-splitting: vertex i → (i, i+n) with capacity 1
    // For source s=0: s_in=0, s_out=n
    // For target t: t_in=t, t_out=t+n
    // Edge (u,v) → (u_out, v_in) and (v_out, u_in) with capacity ∞
    let n2 = 2 * n_nodes;
    let mut min_cut = usize::MAX;
    for t in 1..n_nodes {
        let f = max_flow_node_split(edges, n_nodes, n2, 0, t);
        if f < min_cut {
            min_cut = f;
        }
    }
    if min_cut == usize::MAX { 0 } else { min_cut }
}

// ─────────────────────────────────────────────────────────────────────────────
// Max-flow (BFS / Edmonds-Karp)
// ─────────────────────────────────────────────────────────────────────────────

/// Edmonds-Karp max-flow for unit-capacity undirected edges.
fn max_flow_bfs(
    edges: &[(usize, usize)],
    n: usize,
    source: usize,
    sink: usize,
) -> usize {
    // Build residual capacity matrix
    let mut cap = vec![vec![0i64; n]; n];
    for &(u, v) in edges {
        if u < n && v < n && u != v {
            cap[u][v] += 1;
            cap[v][u] += 1;
        }
    }
    let mut flow = 0usize;
    loop {
        // BFS to find augmenting path
        let mut prev = vec![usize::MAX; n];
        let mut visited = vec![false; n];
        visited[source] = true;
        let mut queue = VecDeque::new();
        queue.push_back(source);
        while let Some(v) = queue.pop_front() {
            for w in 0..n {
                if !visited[w] && cap[v][w] > 0 {
                    visited[w] = true;
                    prev[w] = v;
                    if w == sink {
                        break;
                    }
                    queue.push_back(w);
                }
            }
        }
        if !visited[sink] {
            break;
        }
        // Find bottleneck
        let mut bot = i64::MAX;
        let mut cur = sink;
        while cur != source {
            let pr = prev[cur];
            bot = bot.min(cap[pr][cur]);
            cur = pr;
        }
        // Update residual
        cur = sink;
        while cur != source {
            let pr = prev[cur];
            cap[pr][cur] -= bot;
            cap[cur][pr] += bot;
            cur = pr;
        }
        flow += bot as usize;
    }
    flow
}

/// Max-flow on the node-split network for vertex connectivity.
fn max_flow_node_split(
    edges: &[(usize, usize)],
    n: usize,
    n2: usize,
    source: usize,
    sink: usize,
) -> usize {
    // Node-split network: 2n nodes
    // v_in = v, v_out = v + n
    // Internal edge (v_in, v_out): capacity 1 for all v ≠ source, ≠ sink
    // Graph edge (u,v): (u_out → v_in) and (v_out → u_in) with capacity ∞=n
    let mut cap = vec![vec![0i64; n2]; n2];
    for v in 0..n {
        if v != source && v != sink {
            cap[v][v + n] = 1;
        } else {
            // Source and sink get infinite internal capacity
            cap[v][v + n] = n as i64;
        }
    }
    let inf = n as i64;
    for &(u, v) in edges {
        if u < n && v < n && u != v {
            cap[u + n][v] += inf;
            cap[v + n][u] += inf;
        }
    }
    // Source is source_out (can emit freely), sink is sink_in
    let s = source + n;
    let t = sink;
    max_flow_generic(&mut cap, n2, s, t)
}

fn max_flow_generic(cap: &mut Vec<Vec<i64>>, n: usize, source: usize, sink: usize) -> usize {
    let mut flow = 0usize;
    loop {
        let mut prev = vec![usize::MAX; n];
        let mut visited = vec![false; n];
        visited[source] = true;
        let mut queue = VecDeque::new();
        queue.push_back(source);
        'bfs: while let Some(v) = queue.pop_front() {
            for w in 0..n {
                if !visited[w] && cap[v][w] > 0 {
                    visited[w] = true;
                    prev[w] = v;
                    if w == sink {
                        break 'bfs;
                    }
                    queue.push_back(w);
                }
            }
        }
        if !visited[sink] {
            break;
        }
        let mut bot = i64::MAX;
        let mut cur = sink;
        while cur != source {
            let pr = prev[cur];
            bot = bot.min(cap[pr][cur]);
            cur = pr;
        }
        cur = sink;
        while cur != source {
            let pr = prev[cur];
            cap[pr][cur] -= bot;
            cap[cur][pr] += bot;
            cur = pr;
        }
        flow += bot as usize;
    }
    flow
}

// ─────────────────────────────────────────────────────────────────────────────
// Graph connectivity helpers
// ─────────────────────────────────────────────────────────────────────────────

fn build_adj(edges: &[(usize, usize)], n: usize) -> Vec<Vec<usize>> {
    let mut adj = vec![vec![]; n];
    for &(u, v) in edges {
        if u < n && v < n && u != v {
            adj[u].push(v);
            adj[v].push(u);
        }
    }
    adj
}

fn is_connected(edges: &[(usize, usize)], n: usize) -> bool {
    if n == 0 {
        return true;
    }
    let adj = build_adj(edges, n);
    let mut visited = vec![false; n];
    let mut queue = VecDeque::new();
    queue.push_back(0usize);
    visited[0] = true;
    let mut count = 1usize;
    while let Some(v) = queue.pop_front() {
        for &w in &adj[v] {
            if !visited[w] {
                visited[w] = true;
                count += 1;
                queue.push_back(w);
            }
        }
    }
    count == n
}

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

    fn triangle() -> Vec<(usize, usize)> {
        vec![(0, 1), (1, 2), (0, 2)]
    }

    fn path3() -> Vec<(usize, usize)> {
        vec![(0, 1), (1, 2)]
    }

    #[test]
    fn test_all_terminal_reliability_complete_graph() {
        // Complete graph K3 — all edges present with p=1 → reliability = 1
        let r = all_terminal_reliability_factoring(&triangle(), 3, 1.0);
        assert!((r - 1.0).abs() < 1e-9, "r = {r}");
    }

    #[test]
    fn test_all_terminal_reliability_zero_probability() {
        let r = all_terminal_reliability_factoring(&triangle(), 3, 0.0);
        assert!(r < 0.01, "r = {r}");
    }

    #[test]
    fn test_two_terminal_reliability_path3() {
        // Path 0-1-2: only one path, each edge survives with p
        let r = two_terminal_reliability(&path3(), 3, 0, 2, 0.9);
        assert!((r - 0.81).abs() < 0.01, "r = {r}");
    }

    #[test]
    fn test_two_terminal_reliability_same_node() {
        assert_eq!(two_terminal_reliability(&triangle(), 3, 0, 0, 0.5), 1.0);
    }

    #[test]
    fn test_reliability_polynomial_path2() {
        // Single edge: connected iff edge survives → R(p) = p
        let poly = reliability_polynomial(&[(0, 1)], 2);
        assert!(!poly.is_empty());
        // Evaluate at p=0.5 → should be 0.5
        let val: f64 = poly.iter().enumerate().map(|(i, &c)| c * 0.5f64.powi(i as i32)).sum();
        assert!((val - 0.5).abs() < 1e-9, "R(0.5) = {val}");
    }

    #[test]
    fn test_k_edge_connectivity_triangle() {
        assert_eq!(k_edge_connectivity(&triangle(), 3), 2);
    }

    #[test]
    fn test_k_edge_connectivity_path() {
        assert_eq!(k_edge_connectivity(&path3(), 3), 1);
    }

    #[test]
    fn test_k_vertex_connectivity_triangle() {
        assert_eq!(k_vertex_connectivity(&triangle(), 3), 2);
    }

    #[test]
    fn test_k_vertex_connectivity_path() {
        assert_eq!(k_vertex_connectivity(&path3(), 3), 1);
    }

    #[test]
    fn test_min_cut_reliability_bound() {
        // Lambda=2 for triangle, bound = p^2
        let bound = min_cut_reliability_bound(&triangle(), 3, 0.9);
        assert!((bound - 0.81).abs() < 1e-9);
    }

    #[test]
    fn test_disconnected_graph_reliability_zero() {
        let r = all_terminal_reliability_factoring(&[(0, 1)], 3, 0.9);
        assert!(r < 1e-9, "disconnected graph has reliability 0");
    }
}