oxicuda-graphalg 0.3.0

OxiCUDA: Classical graph algorithms (BFS/DFS, shortest paths, MST, max-flow, matching, SCC, centrality, community, TSP, coloring, isomorphism)
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
//! Lipton-Tarjan style planar separator (1979).
//!
//! Given a (planar) graph on `n` vertices, computes a vertex separator `S`
//! together with a partition of the remaining vertices into `A` and `B` such
//! that
//!
//! * there is **no edge** between `A` and `B` (removing `S` disconnects them),
//! * each side is balanced: `|A| <= 2n/3` and `|B| <= 2n/3`.
//!
//! # Construction (scope)
//!
//! This is a *compact* realisation of the Lipton-Tarjan program, built from the
//! two ingredients the theorem relies on, chosen per input:
//!
//! * **BFS levelling** (the heart of Lipton-Tarjan). Run a breadth-first search
//!   from a root. Because every edge of a BFS-layered graph joins vertices in
//!   the same or adjacent levels, deleting *one entire level* `l` separates the
//!   strictly-lower levels from the strictly-higher levels. The *median* level
//!   always balances both sides below `n/2 <= 2n/3`; among all levels that keep
//!   both sides within `2n/3` we pick the **smallest** one as the separator. On
//!   a `sqrt(n) x sqrt(n)` grid the BFS levels are anti-diagonals of size
//!   `O(sqrt(n))`, giving the textbook `O(sqrt(n))` separator.
//!
//! * **Tree centroid** specialisation. For a tree (the degenerate planar case)
//!   the BFS-level separator can be as large as `n/2`, yet a single *centroid*
//!   vertex already splits every branch below `n/2`. When the input component is
//!   a tree we therefore return the centroid (a size-`1` separator) and balance
//!   its branches into `A`/`B` with an exact subset-sum partition.
//!
//! Disconnected inputs are handled component-wise. The balance and
//! "no `A`-`B` edge" guarantees hold for every input; the `O(sqrt(n))` *size*
//! guarantee is for the planar classes exercised in the tests (grids, trees,
//! paths), matching the latitude of the assignment.

use std::collections::VecDeque;

use crate::error::{GraphalgError, GraphalgResult};
use crate::repr::adjacency_list::AdjacencyList;

/// The three parts of a separator decomposition. Together they partition
/// `0..n` with no edge crossing between [`SeparatorResult::part_a`] and
/// [`SeparatorResult::part_b`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeparatorResult {
    /// First balanced part.
    pub part_a: Vec<usize>,
    /// The separator (whose removal disconnects `A` from `B`).
    pub separator: Vec<usize>,
    /// Second balanced part.
    pub part_b: Vec<usize>,
}

/// Planar separator solver over an undirected graph.
#[derive(Debug, Clone)]
pub struct PlanarSeparator {
    n: usize,
    adj: Vec<Vec<usize>>,
}

impl PlanarSeparator {
    /// Create an edgeless solver on `n` vertices.
    pub fn new(n: usize) -> Self {
        Self {
            n,
            adj: vec![Vec::new(); n],
        }
    }

    /// Add an undirected edge `(u, v)`. Self-loops are ignored; parallel edges
    /// are de-duplicated.
    pub fn add_edge(&mut self, u: usize, v: usize) -> GraphalgResult<()> {
        if u >= self.n || v >= self.n {
            return Err(GraphalgError::IndexOutOfBounds {
                index: u.max(v),
                len: self.n,
            });
        }
        if u == v {
            return Ok(());
        }
        if !self.adj[u].contains(&v) {
            self.adj[u].push(v);
        }
        if !self.adj[v].contains(&u) {
            self.adj[v].push(u);
        }
        Ok(())
    }

    /// Build a planar separator solver from an [`AdjacencyList`], symmetrising
    /// the edges (the graph is treated as undirected).
    pub fn from_adjacency_list(g: &AdjacencyList) -> GraphalgResult<Self> {
        let mut sep = Self::new(g.n);
        for u in 0..g.n {
            for &v in &g.edges[u] {
                sep.add_edge(u, v)?;
            }
        }
        Ok(sep)
    }

    /// Number of vertices.
    pub fn num_nodes(&self) -> usize {
        self.n
    }

    /// Compute a balanced vertex separator.
    pub fn separate(&self) -> GraphalgResult<SeparatorResult> {
        if self.n == 0 {
            return Ok(SeparatorResult {
                part_a: Vec::new(),
                separator: Vec::new(),
                part_b: Vec::new(),
            });
        }
        if self.n == 1 {
            return Ok(SeparatorResult {
                part_a: vec![0],
                separator: Vec::new(),
                part_b: Vec::new(),
            });
        }
        let comps = self.connected_components();
        let limit = (2 * self.n) / 3;
        if comps.len() == 1 {
            let (a, s, b) = self.separate_component(&comps[0])?;
            return Ok(finalize(a, s, b));
        }
        // Multiple components.
        let max_comp = comps.iter().map(|c| c.len()).max().unwrap_or(0);
        if max_comp <= limit {
            // No separator needed: bin-pack components into A and B.
            let sizes: Vec<usize> = comps.iter().map(|c| c.len()).collect();
            let (a_idx, b_idx) = best_partition(&sizes);
            let mut part_a = Vec::new();
            let mut part_b = Vec::new();
            for &i in &a_idx {
                part_a.extend_from_slice(&comps[i]);
            }
            for &i in &b_idx {
                part_b.extend_from_slice(&comps[i]);
            }
            return Ok(finalize(part_a, Vec::new(), part_b));
        }
        // One giant component dominates: separate inside it and distribute the
        // remaining (small) components onto the lighter side.
        let giant_idx = comps
            .iter()
            .enumerate()
            .max_by_key(|(_, c)| c.len())
            .map(|(i, _)| i)
            .unwrap_or(0);
        let (mut ga, gs, mut gb) = self.separate_component(&comps[giant_idx])?;
        for (i, c) in comps.iter().enumerate() {
            if i == giant_idx {
                continue;
            }
            if ga.len() <= gb.len() {
                ga.extend_from_slice(c);
            } else {
                gb.extend_from_slice(c);
            }
        }
        Ok(finalize(ga, gs, gb))
    }

    /// Connected components as vertex lists.
    fn connected_components(&self) -> Vec<Vec<usize>> {
        let mut comp_id = vec![usize::MAX; self.n];
        let mut comps: Vec<Vec<usize>> = Vec::new();
        for start in 0..self.n {
            if comp_id[start] != usize::MAX {
                continue;
            }
            let id = comps.len();
            let mut members = Vec::new();
            let mut queue = VecDeque::new();
            queue.push_back(start);
            comp_id[start] = id;
            while let Some(u) = queue.pop_front() {
                members.push(u);
                for &w in &self.adj[u] {
                    if comp_id[w] == usize::MAX {
                        comp_id[w] = id;
                        queue.push_back(w);
                    }
                }
            }
            comps.push(members);
        }
        comps
    }

    /// Number of undirected edges with both endpoints inside `verts`.
    fn component_edge_count(&self, in_set: &[bool], verts: &[usize]) -> usize {
        let mut deg_sum = 0usize;
        for &u in verts {
            for &w in &self.adj[u] {
                if in_set[w] {
                    deg_sum += 1;
                }
            }
        }
        deg_sum / 2
    }

    /// Separate a single connected component (`verts`) into `(A, S, B)`.
    fn separate_component(
        &self,
        verts: &[usize],
    ) -> GraphalgResult<(Vec<usize>, Vec<usize>, Vec<usize>)> {
        let k = verts.len();
        if k == 0 {
            return Ok((Vec::new(), Vec::new(), Vec::new()));
        }
        if k == 1 {
            return Ok((vec![verts[0]], Vec::new(), Vec::new()));
        }
        let mut in_set = vec![false; self.n];
        for &v in verts {
            in_set[v] = true;
        }
        let edge_count = self.component_edge_count(&in_set, verts);
        if edge_count == k - 1 {
            self.tree_centroid_separator(verts, &in_set)
        } else {
            self.bfs_level_separator(verts, &in_set)
        }
    }

    /// Tree case: remove a centroid, split its branches into `A`/`B`.
    fn tree_centroid_separator(
        &self,
        verts: &[usize],
        in_set: &[bool],
    ) -> GraphalgResult<(Vec<usize>, Vec<usize>, Vec<usize>)> {
        let k = verts.len();
        let root = verts[0];
        // Rooted BFS order + parents.
        let mut parent = vec![usize::MAX; self.n];
        let mut visited = vec![false; self.n];
        let mut order = Vec::with_capacity(k);
        let mut queue = VecDeque::new();
        queue.push_back(root);
        visited[root] = true;
        while let Some(u) = queue.pop_front() {
            order.push(u);
            for &w in &self.adj[u] {
                if in_set[w] && !visited[w] {
                    visited[w] = true;
                    parent[w] = u;
                    queue.push_back(w);
                }
            }
        }
        // Subtree sizes (reverse BFS order).
        let mut size = vec![0usize; self.n];
        for &v in verts {
            size[v] = 1;
        }
        for &u in order.iter().rev() {
            if parent[u] != usize::MAX {
                size[parent[u]] += size[u];
            }
        }
        // Centroid: minimise the largest component after removal.
        let mut best_c = root;
        let mut best_max = usize::MAX;
        for &u in verts {
            let mut mx = k - size[u]; // parent side
            for &w in &self.adj[u] {
                if in_set[w] && parent[w] == u {
                    mx = mx.max(size[w]);
                }
            }
            if mx < best_max {
                best_max = mx;
                best_c = u;
            }
        }
        // Branch components after removing best_c.
        let mut removed = vec![false; self.n];
        removed[best_c] = true;
        let mut branch_visited = vec![false; self.n];
        branch_visited[best_c] = true;
        let mut branches: Vec<Vec<usize>> = Vec::new();
        for &w in &self.adj[best_c] {
            if in_set[w] && !branch_visited[w] {
                let mut comp = Vec::new();
                let mut q = VecDeque::new();
                q.push_back(w);
                branch_visited[w] = true;
                while let Some(u) = q.pop_front() {
                    comp.push(u);
                    for &x in &self.adj[u] {
                        if in_set[x] && !branch_visited[x] && x != best_c {
                            branch_visited[x] = true;
                            q.push_back(x);
                        }
                    }
                }
                branches.push(comp);
            }
        }
        let sizes: Vec<usize> = branches.iter().map(|c| c.len()).collect();
        let (a_idx, b_idx) = best_partition(&sizes);
        let mut part_a = Vec::new();
        let mut part_b = Vec::new();
        for &i in &a_idx {
            part_a.extend_from_slice(&branches[i]);
        }
        for &i in &b_idx {
            part_b.extend_from_slice(&branches[i]);
        }
        Ok((part_a, vec![best_c], part_b))
    }

    /// General case: separate by removing the smallest balancing BFS level.
    fn bfs_level_separator(
        &self,
        verts: &[usize],
        in_set: &[bool],
    ) -> GraphalgResult<(Vec<usize>, Vec<usize>, Vec<usize>)> {
        let k = verts.len();
        let root = verts[0];
        let mut level = vec![usize::MAX; self.n];
        let mut queue = VecDeque::new();
        level[root] = 0;
        queue.push_back(root);
        let mut max_level = 0usize;
        while let Some(u) = queue.pop_front() {
            let lu = level[u];
            if lu > max_level {
                max_level = lu;
            }
            for &w in &self.adj[u] {
                if in_set[w] && level[w] == usize::MAX {
                    level[w] = lu + 1;
                    queue.push_back(w);
                }
            }
        }
        // Level sizes and cumulative counts.
        let mut level_size = vec![0usize; max_level + 1];
        for &v in verts {
            level_size[level[v]] += 1;
        }
        let mut prefix = vec![0usize; max_level + 2];
        for l in 0..=max_level {
            prefix[l + 1] = prefix[l] + level_size[l];
        }
        let limit = (2 * k) / 3;
        // Among levels keeping both sides within 2k/3, pick the smallest level.
        let mut chosen = usize::MAX;
        let mut chosen_size = usize::MAX;
        for l in 0..=max_level {
            let below = prefix[l];
            let above = k - prefix[l + 1];
            if below <= limit && above <= limit && level_size[l] < chosen_size {
                chosen_size = level_size[l];
                chosen = l;
            }
        }
        // The median level always qualifies, so `chosen` is set; guard anyway.
        if chosen == usize::MAX {
            chosen = 0;
        }
        let mut part_a = Vec::new();
        let mut separator = Vec::new();
        let mut part_b = Vec::new();
        for &v in verts {
            let lv = level[v];
            if lv < chosen {
                part_a.push(v);
            } else if lv == chosen {
                separator.push(v);
            } else {
                part_b.push(v);
            }
        }
        Ok((part_a, separator, part_b))
    }
}

/// Sort each part and assemble a [`SeparatorResult`].
fn finalize(mut a: Vec<usize>, mut s: Vec<usize>, mut b: Vec<usize>) -> SeparatorResult {
    a.sort_unstable();
    s.sort_unstable();
    b.sort_unstable();
    SeparatorResult {
        part_a: a,
        separator: s,
        part_b: b,
    }
}

/// Split item `sizes` into two index groups whose total sizes are as balanced
/// as possible, via exact subset-sum dynamic programming.
fn best_partition(sizes: &[usize]) -> (Vec<usize>, Vec<usize>) {
    let m = sizes.len();
    if m == 0 {
        return (Vec::new(), Vec::new());
    }
    let total: usize = sizes.iter().sum();
    if total == 0 {
        return ((0..m).collect(), Vec::new());
    }
    let mut reach = vec![vec![false; total + 1]; m + 1];
    reach[0][0] = true;
    for i in 1..=m {
        let s = sizes[i - 1];
        for x in 0..=total {
            if reach[i - 1][x] {
                reach[i][x] = true;
                if x + s <= total {
                    reach[i][x + s] = true;
                }
            }
        }
    }
    let target = total / 2;
    let mut best_x = 0usize;
    let mut best_diff = usize::MAX;
    for x in 0..=total {
        if reach[m][x] {
            let diff = x.abs_diff(target);
            if diff < best_diff {
                best_diff = diff;
                best_x = x;
            }
        }
    }
    let mut a = Vec::new();
    let mut b = Vec::new();
    let mut x = best_x;
    for i in (1..=m).rev() {
        let s = sizes[i - 1];
        if x >= s && reach[i - 1][x - s] {
            a.push(i - 1);
            x -= s;
        } else {
            b.push(i - 1);
        }
    }
    (a, b)
}

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

    /// Verify the structural separator invariants against an explicit edge list.
    fn check_separator(
        n: usize,
        edges: &[(usize, usize)],
        res: &SeparatorResult,
        balance_limit: usize,
    ) {
        // Partition of 0..n with no overlaps.
        let mut seen = vec![0u8; n];
        for &v in res.part_a.iter().chain(&res.separator).chain(&res.part_b) {
            seen[v] += 1;
        }
        for (v, &c) in seen.iter().enumerate() {
            assert_eq!(c, 1, "vertex {v} appears {c} times (must be exactly once)");
        }
        // No edge between A and B.
        let a: BTreeSet<usize> = res.part_a.iter().copied().collect();
        let b: BTreeSet<usize> = res.part_b.iter().copied().collect();
        for &(u, v) in edges {
            let ab = a.contains(&u) && b.contains(&v);
            let ba = b.contains(&u) && a.contains(&v);
            assert!(!ab && !ba, "edge ({u},{v}) crosses A-B separator");
        }
        // Balance.
        assert!(
            res.part_a.len() <= balance_limit,
            "|A|={} exceeds {balance_limit}",
            res.part_a.len()
        );
        assert!(
            res.part_b.len() <= balance_limit,
            "|B|={} exceeds {balance_limit}",
            res.part_b.len()
        );
    }

    fn build_grid(a: usize) -> (PlanarSeparator, Vec<(usize, usize)>) {
        let n = a * a;
        let mut sep = PlanarSeparator::new(n);
        let mut edges = Vec::new();
        for i in 0..a {
            for j in 0..a {
                let id = i * a + j;
                if j + 1 < a {
                    sep.add_edge(id, id + 1).expect("edge");
                    edges.push((id, id + 1));
                }
                if i + 1 < a {
                    sep.add_edge(id, id + a).expect("edge");
                    edges.push((id, id + a));
                }
            }
        }
        (sep, edges)
    }

    #[test]
    fn grid_separator_is_small_and_balanced() {
        let a = 7;
        let n = a * a;
        let (sep, edges) = build_grid(a);
        let res = sep.separate().expect("separate");
        let limit = (2 * n) / 3;
        check_separator(n, &edges, &res, limit);
        // O(sqrt(n)): a single anti-diagonal/row is ~ sqrt(n).
        let bound = (3.0 * (n as f64).sqrt()).ceil() as usize;
        assert!(
            res.separator.len() <= bound,
            "separator size {} exceeds O(sqrt(n)) bound {bound}",
            res.separator.len()
        );
        // Removing S must leave both sides non-empty for a grid (real split).
        assert!(!res.part_a.is_empty() && !res.part_b.is_empty());
    }

    #[test]
    fn grid_partition_property_holds() {
        let a = 5;
        let n = a * a;
        let (sep, edges) = build_grid(a);
        let res = sep.separate().expect("separate");
        check_separator(n, &edges, &res, (2 * n) / 3);
    }

    #[test]
    fn path_has_size_one_separator() {
        let n = 11;
        let mut sep = PlanarSeparator::new(n);
        let mut edges = Vec::new();
        for i in 0..n - 1 {
            sep.add_edge(i, i + 1).expect("edge");
            edges.push((i, i + 1));
        }
        let res = sep.separate().expect("separate");
        check_separator(n, &edges, &res, (2 * n) / 3);
        assert_eq!(
            res.separator.len(),
            1,
            "path centroid separator is one vertex"
        );
    }

    #[test]
    fn balanced_binary_tree_uses_centroid() {
        // Complete binary tree with 15 nodes: BFS-level would remove ~8 leaves,
        // but the centroid is a single vertex.
        let n = 15;
        let mut sep = PlanarSeparator::new(n);
        let mut edges = Vec::new();
        for i in 1..n {
            let p = (i - 1) / 2;
            sep.add_edge(p, i).expect("edge");
            edges.push((p, i));
        }
        let res = sep.separate().expect("separate");
        check_separator(n, &edges, &res, (2 * n) / 3);
        assert_eq!(res.separator.len(), 1, "tree separator is the centroid");
    }

    #[test]
    fn star_separator_is_center() {
        let n = 9;
        let mut sep = PlanarSeparator::new(n);
        let mut edges = Vec::new();
        for i in 1..n {
            sep.add_edge(0, i).expect("edge");
            edges.push((0, i));
        }
        let res = sep.separate().expect("separate");
        check_separator(n, &edges, &res, (2 * n) / 3);
        // Removing the hub disconnects everything; centroid = hub.
        assert_eq!(res.separator, vec![0]);
    }

    #[test]
    fn disconnected_components_partition_with_empty_separator() {
        // Two triangles (disjoint), each 3 vertices.
        let n = 6;
        let mut sep = PlanarSeparator::new(n);
        let edges = [(0, 1), (1, 2), (0, 2), (3, 4), (4, 5), (3, 5)];
        for &(u, v) in &edges {
            sep.add_edge(u, v).expect("edge");
        }
        let res = sep.separate().expect("separate");
        check_separator(n, &edges, &res, (2 * n) / 3);
        assert!(
            res.separator.is_empty(),
            "two equal triangles split with no separator"
        );
        assert_eq!(res.part_a.len(), 3);
        assert_eq!(res.part_b.len(), 3);
    }

    #[test]
    fn trivial_graphs_handled() {
        let empty = PlanarSeparator::new(0);
        let r0 = empty.separate().expect("separate");
        assert!(r0.part_a.is_empty() && r0.separator.is_empty() && r0.part_b.is_empty());

        let single = PlanarSeparator::new(1);
        let r1 = single.separate().expect("separate");
        assert_eq!(r1.part_a, vec![0]);
        assert!(r1.separator.is_empty() && r1.part_b.is_empty());
    }

    #[test]
    fn rejects_out_of_range_edge() {
        let mut sep = PlanarSeparator::new(3);
        assert!(sep.add_edge(0, 5).is_err());
    }

    #[test]
    fn from_adjacency_list_round_trip() {
        let mut g = AdjacencyList::new(4);
        g.add_undirected_edge(0, 1).expect("edge");
        g.add_undirected_edge(1, 2).expect("edge");
        g.add_undirected_edge(2, 3).expect("edge");
        let sep = PlanarSeparator::from_adjacency_list(&g).expect("build");
        let res = sep.separate().expect("separate");
        let edges = [(0, 1), (1, 2), (2, 3)];
        check_separator(4, &edges, &res, (2 * 4) / 3);
    }
}