safegraph 0.1.0

A type-safe, scope-aware graph library that leverages Rust's type system to prevent common graph-related bugs at compile time
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
//! # Graph Matching
//!
//! Computes matchings on undirected graphs. A **matching** is a set of edges
//! with no shared endpoints. This module provides two strategies: a fast greedy
//! heuristic that yields a *maximal* matching (no more edges can be added), and
//! an augmenting-path algorithm that finds a *maximum* matching (largest
//! possible cardinality).
//!
//! ## Components
//!
//! - [`GreedyMatching`] — lazy iterator yielding edges of a greedy maximal matching
//! - [`greedy_matching()`] — safe constructor (requires `StableEdge`)
//! - [`max_matching()`] — returns a `HashSet<EdgeIx>` of a maximum matching (requires `StableEdge`)
//!
//! ## Algorithm
//!
//! **Greedy matching** scans edges in iteration order and accepts each edge
//! whose both endpoints are still unmatched. This runs in O(E) time but may
//! miss the optimal solution.
//!
//! **Maximum matching** starts from the greedy result, then repeatedly searches
//! for augmenting paths -- paths that alternate between unmatched and matched
//! edges, starting and ending at free (unmatched) nodes. Each augmenting path
//! found increases the matching size by one (by toggling matched/unmatched
//! status along the path). The process repeats until no augmenting path exists.
//!
//! ```text
//!  Greedy phase:
//!     For each edge (u, v):
//!       +--- u and v both free? => add to matching, mark u,v matched
//!       +--- otherwise          => skip
//!
//!  Augmenting-path phase:
//!     +---> Collect free (unmatched) nodes
//!     |     |
//!     |     v
//!     |   For each free node, DFS for augmenting path:
//!     |     unmatched edge -> matched edge -> ... -> unmatched edge -> free node
//!     |     |
//!     |     +--- Path found: toggle edges along path, matching grows by 1
//!     |     +--- No path:    skip
//!     |     |
//!     +-----+  (repeat until no augmenting path found in a full round)
//! ```
//!
//! ## Example
//!
//! ```rust,no_run
//! use safegraph::BTreeGraph;
//! use safegraph::graph::{Graph, GraphOperation};
//! use safegraph::algo::matching::{greedy_matching, max_matching};
//!
//! // Path graph: 0 -- 1 -- 2
//! let mut g = BTreeGraph::<_, _>::default();
//! g.insert_node(0).unwrap();
//! g.insert_node(1).unwrap();
//! g.insert_node(2).unwrap();
//! g.insert_edge("0-1", [0, 1]).unwrap();
//! g.insert_edge("1-2", [1, 2]).unwrap();
//!
//! // Greedy picks one edge (node 1 can only appear in one matched edge)
//! let greedy: Vec<_> = greedy_matching(&g).collect();
//! assert_eq!(greedy.len(), 1);
//!
//! // Maximum matching is also 1 for a 3-node path
//! let maximum = max_matching(&g);
//! assert_eq!(maximum.len(), 1);
//! ```

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

use crate::graph::capability::{Bigraph, StableEdge};
use crate::graph::Graph;

type Adjacency<G> = HashMap<
    <G as crate::graph::GraphProperty>::NodeIx,
    Vec<(
        <G as crate::graph::GraphProperty>::EdgeIx,
        <G as crate::graph::GraphProperty>::NodeIx,
    )>,
>;

/// Iterator that lazily yields edges forming a greedy maximal matching.
///
/// A matching is a set of edges with no shared vertices. This greedy algorithm
/// iterates edges and yields each edge if neither endpoint is already matched.
///
/// The result is a maximal matching (no more edges can be added), but not necessarily
/// a maximum matching (the largest possible).
/// `E` is the edge-index iterator type (`<G as GraphOperation<'r>>::EdgeIndices`)
/// and `N` the node-index type (`G::NodeIx`); both are separate type parameters
/// so the struct carries no `Graph` bound.
pub struct GreedyMatching<'r, G: ?Sized, E, N> {
    graph: &'r G,
    edges: E,
    matched_nodes: HashSet<N>,
}

/// Returns an iterator over edges forming a greedy maximal matching.
pub fn greedy_matching<'r, G>(graph: &'r G) -> GreedyMatching<'r, G, <G as crate::graph::GraphOperation<'r>>::EdgeIndices, G::NodeIx>
where
    G: Graph + Bigraph + StableEdge + ?Sized,
{
    GreedyMatching {
        graph,
        edges: <_ as crate::graph::GraphOperation<'_>>::edge_indices(graph),
        matched_nodes: HashSet::new(),
    }
}

impl<'r, G> Iterator for GreedyMatching<'r, G, <G as crate::graph::GraphOperation<'r>>::EdgeIndices, G::NodeIx>
where
    G: Graph + Bigraph + StableEdge + ?Sized,
{
    type Item = G::EdgeIx;

    fn next(&mut self) -> Option<G::EdgeIx> {
        loop {
            let eix = self.edges.next()?;
            let eps: Vec<G::NodeIx> = unsafe { <G as crate::graph::GraphOperation<'_>>::endpoints_unchecked(self.graph, eix) }
                .into_iter()
                .collect();
            let (a, b) = (eps[0], eps[1]);

            // Skip self-loops
            if a == b {
                continue;
            }

            if !self.matched_nodes.contains(&a) && !self.matched_nodes.contains(&b) {
                self.matched_nodes.insert(a);
                self.matched_nodes.insert(b);
                return Some(eix);
            }
        }
    }
}

/// Compute a maximum matching using augmenting paths.
///
/// Uses the Hopcroft-Karp-style augmenting path approach:
/// start with a greedy matching, then repeatedly find augmenting paths
/// via BFS/DFS to improve the matching.
///
/// Returns a set of edge indices forming the maximum matching.
pub fn max_matching<G>(graph: &G) -> HashSet<G::EdgeIx>
where
    G: Graph + Bigraph + StableEdge + ?Sized,
{
    // Build adjacency: for each node, list of (edge_ix, other_node)
    let mut adj: Adjacency<G> = HashMap::new();

    for eix in <_ as crate::graph::GraphOperation<'_>>::edge_indices(graph) {
        let eps: Vec<G::NodeIx> = unsafe { <G as crate::graph::GraphOperation<'_>>::endpoints_unchecked(graph, eix) }
            .into_iter()
            .collect();
        let (a, b) = (eps[0], eps[1]);
        if a == b {
            continue; // Skip self-loops
        }
        adj.entry(a).or_default().push((eix, b));
        adj.entry(b).or_default().push((eix, a));
    }

    // Start with greedy matching
    let mut match_of: HashMap<G::NodeIx, (G::EdgeIx, G::NodeIx)> = HashMap::new();
    let mut in_matching: HashSet<G::EdgeIx> = HashSet::new();

    for eix in <_ as crate::graph::GraphOperation<'_>>::edge_indices(graph) {
        let eps: Vec<G::NodeIx> = unsafe { <G as crate::graph::GraphOperation<'_>>::endpoints_unchecked(graph, eix) }
            .into_iter()
            .collect();
        let (a, b) = (eps[0], eps[1]);
        if a == b {
            continue;
        }
        if !match_of.contains_key(&a) && !match_of.contains_key(&b) {
            match_of.insert(a, (eix, b));
            match_of.insert(b, (eix, a));
            in_matching.insert(eix);
        }
    }

    // Augment: find augmenting paths from free (unmatched) nodes
    loop {
        let free_nodes: Vec<G::NodeIx> = <_ as crate::graph::GraphOperation<'_>>::node_indices(graph)
            .filter(|n| !match_of.contains_key(n))
            .collect();

        let mut found_augmenting = false;

        for free in &free_nodes {
            // DFS for augmenting path from this free node
            // An augmenting path alternates between unmatched and matched edges,
            // starting and ending at free nodes
            if match_of.contains_key(free) {
                continue; // May have been matched during this round
            }

            if let Some(path) = find_augmenting_path(*free, &adj, &match_of, &in_matching) {
                // Augment along the path: toggle edges in/out of matching
                for (eix, in_match) in path {
                    if in_match {
                        in_matching.remove(&eix);
                    } else {
                        in_matching.insert(eix);
                    }
                }
                // Rebuild match_of from in_matching
                match_of.clear();
                for &eix in &in_matching {
                    let eps: Vec<G::NodeIx> = unsafe { <G as crate::graph::GraphOperation<'_>>::endpoints_unchecked(graph, eix) }
                        .into_iter()
                        .collect();
                    let (a, b) = (eps[0], eps[1]);
                    match_of.insert(a, (eix, b));
                    match_of.insert(b, (eix, a));
                }
                found_augmenting = true;
            }
        }

        if !found_augmenting {
            break;
        }
    }

    in_matching
}

/// Find an augmenting path starting from a free node.
/// Returns list of (edge_ix, is_currently_in_matching) along the path.
fn find_augmenting_path<N, E>(
    start: N,
    adj: &HashMap<N, Vec<(E, N)>>,
    match_of: &HashMap<N, (E, N)>,
    in_matching: &HashSet<E>,
) -> Option<Vec<(E, bool)>>
where
    N: Copy + Eq + std::hash::Hash,
    E: Copy + Eq + std::hash::Hash,
{
    // DFS with backtracking
    // State: (current_node, must_use_unmatched_next, visited, path)
    let mut visited: HashSet<N> = HashSet::new();
    visited.insert(start);

    struct Frame<N> {
        node: N,
        use_unmatched: bool, // true = next edge must be unmatched
        neighbors_idx: usize,
    }

    let mut stack: Vec<Frame<N>> = vec![Frame {
        node: start,
        use_unmatched: true, // Start with unmatched edge
        neighbors_idx: 0,
    }];

    let mut path: Vec<(E, bool)> = Vec::new();

    loop {
        let stack_len = stack.len();
        if stack_len == 0 {
            break;
        }

        let frame = &mut stack[stack_len - 1];
        let empty_vec = Vec::new();
        let neighbors = adj.get(&frame.node).unwrap_or(&empty_vec);

        if frame.neighbors_idx >= neighbors.len() {
            // Backtrack
            stack.pop();
            path.pop();
            if let Some(top) = stack.last_mut() {
                top.neighbors_idx += 1;
            }
            continue;
        }

        let (eix, neighbor) = neighbors[frame.neighbors_idx];
        let edge_in_matching = in_matching.contains(&eix);

        // We alternate: unmatched -> matched -> unmatched -> ...
        if frame.use_unmatched == edge_in_matching {
            // Wrong type of edge, skip
            frame.neighbors_idx += 1;
            continue;
        }

        if visited.contains(&neighbor) {
            frame.neighbors_idx += 1;
            continue;
        }

        let next_use_unmatched = !frame.use_unmatched;

        // Take this edge
        path.push((eix, edge_in_matching));
        visited.insert(neighbor);

        // If we used an unmatched edge and neighbor is free, we found an augmenting path
        if !edge_in_matching && !match_of.contains_key(&neighbor) {
            return Some(path);
        }

        // Continue DFS
        stack.push(Frame {
            node: neighbor,
            use_unmatched: next_use_unmatched,
            neighbors_idx: 0,
        });
    }

    None
}

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

    #[test]
    fn greedy_matching_basic() {
        // Path: 0 -> 1 -> 2
        let mut g = BTreeGraph::<_, _>::default();
        g.insert_node(0).unwrap();
        g.insert_node(1).unwrap();
        g.insert_node(2).unwrap();
        g.insert_edge("0->1", [0, 1]).unwrap();
        g.insert_edge("1->2", [1, 2]).unwrap();

        let m: HashSet<_> = greedy_matching(&g).collect();
        // At least 1 edge in the matching
        assert!(!m.is_empty());
        // At most 1 edge (since node 1 can only be in one)
        assert!(m.len() <= 1);
    }

    #[test]
    fn max_matching_path() {
        // Path: 0 -> 1 -> 2 -> 3
        // Maximum matching = 2 (e.g., {0->1, 2->3})
        let mut g = BTreeGraph::<_, _>::default();
        g.insert_node(0).unwrap();
        g.insert_node(1).unwrap();
        g.insert_node(2).unwrap();
        g.insert_node(3).unwrap();
        g.insert_edge("0->1", [0, 1]).unwrap();
        g.insert_edge("1->2", [1, 2]).unwrap();
        g.insert_edge("2->3", [2, 3]).unwrap();

        let m = max_matching(&g);
        assert_eq!(m.len(), 2);
    }

    #[test]
    fn max_matching_star() {
        // Star: 0 -> 1, 0 -> 2, 0 -> 3
        // Maximum matching = 1 (center can match only one)
        let mut g = BTreeGraph::<_, _>::default();
        g.insert_node(0).unwrap();
        g.insert_node(1).unwrap();
        g.insert_node(2).unwrap();
        g.insert_node(3).unwrap();
        g.insert_edge("0->1", [0, 1]).unwrap();
        g.insert_edge("0->2", [0, 2]).unwrap();
        g.insert_edge("0->3", [0, 3]).unwrap();

        let m = max_matching(&g);
        assert_eq!(m.len(), 1);
    }

    #[test]
    fn max_matching_complete_bipartite() {
        // K_{2,2}: {0,1} x {2,3}
        // 0->2, 0->3, 1->2, 1->3
        // Maximum matching = 2
        let mut g = BTreeGraph::<_, _>::default();
        g.insert_node(0).unwrap();
        g.insert_node(1).unwrap();
        g.insert_node(2).unwrap();
        g.insert_node(3).unwrap();
        g.insert_edge("0->2", [0, 2]).unwrap();
        g.insert_edge("0->3", [0, 3]).unwrap();
        g.insert_edge("1->2", [1, 2]).unwrap();
        g.insert_edge("1->3", [1, 3]).unwrap();

        let m = max_matching(&g);
        assert_eq!(m.len(), 2);
    }

    #[test]
    fn matching_no_shared_vertices() {
        // Verify matching property: no two edges share a vertex
        let mut g = BTreeGraph::<_, _>::default();
        g.insert_node(0).unwrap();
        g.insert_node(1).unwrap();
        g.insert_node(2).unwrap();
        g.insert_node(3).unwrap();
        g.insert_node(4).unwrap();
        g.insert_edge("0->1", [0, 1]).unwrap();
        g.insert_edge("1->2", [1, 2]).unwrap();
        g.insert_edge("2->3", [2, 3]).unwrap();
        g.insert_edge("3->4", [3, 4]).unwrap();

        let m = max_matching(&g);
        let mut used_nodes: HashSet<i32> = HashSet::new();
        for &eix in &m {
            let tail = g.edge_tail_index(eix);
            let head = g.edge_head_index(eix);
            assert!(used_nodes.insert(tail), "Node {:?} used twice", tail);
            assert!(used_nodes.insert(head), "Node {:?} used twice", head);
        }
        assert_eq!(m.len(), 2);
    }

    #[test]
    fn matching_empty_graph() {
        let g = BTreeGraph::<u32, &str>::default();
        let m: HashSet<_> = greedy_matching(&g).collect();
        assert!(m.is_empty());
    }
}