rust-igraph 0.7.0

Pure-Rust, high-performance graph & network analysis library — 1297 APIs, zero unsafe, igraph-compatible
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
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::many_single_char_names,
    clippy::too_many_lines,
    clippy::similar_names,
    clippy::module_name_repetitions
)]

//! Bipartite matching algorithms.
//!
//! Provides:
//! - [`is_matching`] — validate a matching vector against a graph
//! - [`is_maximal_matching`] — check whether a valid matching is maximal
//! - [`maximum_bipartite_matching`] — maximum cardinality bipartite matching
//!   (push-relabel, unweighted)
//! - [`maximum_bipartite_matching_weighted`] — maximum weight bipartite
//!   matching (Hungarian / Kuhn-Munkres)
//!
//! Reference: `igraph/src/misc/matching.c` (1013 lines).

use std::collections::VecDeque;

use crate::core::error::{IgraphError, IgraphResult};
use crate::core::graph::Graph;

/// Result of [`maximum_bipartite_matching`] or
/// [`maximum_bipartite_matching_weighted`].
///
/// # Fields
///
/// * `matching_size` — number of matched vertex pairs.
/// * `matching_weight` — total weight of matched edges (equals `matching_size`
///   for unweighted).
/// * `matching` — per-vertex match: `matching[v]` is the partner of `v`, or
///   `None` if `v` is unmatched.
///
/// ```
/// use rust_igraph::{create, maximum_bipartite_matching, MatchingResult};
///
/// // K_{2,2}: 0-2, 0-3, 1-2, 1-3
/// let g = create(&[(0, 2), (0, 3), (1, 2), (1, 3)], 4, false).unwrap();
/// let types = vec![false, false, true, true];
/// let r = maximum_bipartite_matching(&g, &types).unwrap();
/// assert_eq!(r.matching_size, 2);
/// ```
#[derive(Debug, Clone)]
pub struct MatchingResult {
    /// Number of matched vertex pairs.
    pub matching_size: usize,
    /// Total weight of matched edges (1.0 per edge if unweighted).
    pub matching_weight: f64,
    /// Per-vertex matching partner: `Some(j)` if matched to `j`, `None` if unmatched.
    pub matching: Vec<Option<u32>>,
}

/// Check whether `matching` is a valid matching for `graph`.
///
/// A matching vector has length `vcount`; entry `i` is `Some(j)` when vertex
/// `i` is matched to `j`, or `None` if unmatched. The function verifies:
/// 1. Length equals `vcount`.
/// 2. Matched pairs are mutual (`matching[i] == Some(j)` ⟹ `matching[j] == Some(i)`).
/// 3. Every matched pair is connected by an edge (ignoring direction).
/// 4. If `types` is provided, matched vertices have different types.
///
/// ```
/// use rust_igraph::{create, is_matching};
///
/// let g = create(&[(0, 1), (1, 2)], 3, false).unwrap();
/// let m = vec![Some(1), Some(0), None];
/// assert!(is_matching(&g, None, &m).unwrap());
/// ```
pub fn is_matching(
    graph: &Graph,
    types: Option<&[bool]>,
    matching: &[Option<u32>],
) -> IgraphResult<bool> {
    let n = graph.vcount() as usize;
    if matching.len() != n {
        return Ok(false);
    }

    let adj = build_undirected_adj(graph);

    for (i, &mi) in matching.iter().enumerate() {
        let Some(j) = mi else { continue };
        let j_usize = j as usize;
        if j_usize >= n {
            return Ok(false);
        }
        if matching[j_usize] != Some(i as u32) {
            return Ok(false);
        }
        if !adj[i].contains(&j) {
            return Ok(false);
        }
    }

    if let Some(t) = types {
        if t.len() < n {
            return Err(IgraphError::InvalidArgument(
                "types vector too short".into(),
            ));
        }
        for (i, &mi) in matching.iter().enumerate() {
            let Some(j) = mi else { continue };
            if t[i] == t[j as usize] {
                return Ok(false);
            }
        }
    }

    Ok(true)
}

/// Check whether `matching` is a *maximal* matching for `graph`.
///
/// A matching is maximal if no unmatched vertex has an unmatched neighbor
/// (respecting bipartite types if given).
///
/// ```
/// use rust_igraph::{create, is_maximal_matching};
///
/// let g = create(&[(0, 1), (1, 2)], 3, false).unwrap();
/// // Only 0-1 matched; vertex 2 is unmatched but has no unmatched neighbor → maximal
/// let m = vec![Some(1), Some(0), None];
/// assert!(is_maximal_matching(&g, None, &m).unwrap());
/// ```
pub fn is_maximal_matching(
    graph: &Graph,
    types: Option<&[bool]>,
    matching: &[Option<u32>],
) -> IgraphResult<bool> {
    if !is_matching(graph, types, matching)? {
        return Ok(false);
    }

    let n = graph.vcount() as usize;
    let adj = build_undirected_adj(graph);

    for i in 0..n {
        if matching[i].is_some() {
            continue;
        }
        for &nb in &adj[i] {
            if matching[nb as usize].is_none() {
                if let Some(t) = types {
                    if t[i] == t[nb as usize] {
                        continue;
                    }
                }
                return Ok(false);
            }
        }
    }

    Ok(true)
}

/// Compute a maximum cardinality matching in an unweighted bipartite graph.
///
/// Uses a push-relabel algorithm with greedy initialization and global
/// relabeling every `n/2` steps. Returns a [`MatchingResult`] where
/// `matching_weight == matching_size` (since all edges have unit weight).
///
/// `types` must be a bipartite partition: `types[v]` is `false` for one side
/// and `true` for the other. The function validates that every edge connects
/// vertices of different types.
///
/// ```
/// use rust_igraph::{create, maximum_bipartite_matching};
///
/// let g = create(&[(0, 2), (0, 3), (1, 2), (1, 3)], 4, false).unwrap();
/// let types = vec![false, false, true, true];
/// let r = maximum_bipartite_matching(&g, &types).unwrap();
/// assert_eq!(r.matching_size, 2);
/// ```
pub fn maximum_bipartite_matching(graph: &Graph, types: &[bool]) -> IgraphResult<MatchingResult> {
    let n = graph.vcount() as usize;
    if types.len() < n {
        return Err(IgraphError::InvalidArgument(
            "types vector too short".into(),
        ));
    }

    let adj = build_undirected_adj(graph);
    let (matching, num_matched) = push_relabel_unweighted(graph, &adj, types, n)?;

    Ok(MatchingResult {
        matching_size: num_matched,
        matching_weight: num_matched as f64,
        matching,
    })
}

/// Compute a maximum weight matching in a weighted bipartite graph.
///
/// Uses the Hungarian algorithm (Kuhn-Munkres) with push-relabel
/// initialization on tight edges. `weights[e]` gives the weight of edge `e`
/// (by edge id). `eps` controls floating-point tolerance for "tight" edge
/// detection; pass `0.0` for integer weights.
///
/// ```
/// use rust_igraph::{create, maximum_bipartite_matching_weighted};
///
/// let g = create(&[(0, 2), (0, 3), (1, 2), (1, 3)], 4, false).unwrap();
/// let types = vec![false, false, true, true];
/// let weights = vec![1.0, 10.0, 10.0, 1.0];
/// let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).unwrap();
/// assert_eq!(r.matching_size, 2);
/// assert!((r.matching_weight - 20.0).abs() < 1e-9);
/// ```
pub fn maximum_bipartite_matching_weighted(
    graph: &Graph,
    types: &[bool],
    weights: &[f64],
    eps: f64,
) -> IgraphResult<MatchingResult> {
    let n = graph.vcount() as usize;
    let ne = graph.ecount();
    if types.len() < n {
        return Err(IgraphError::InvalidArgument(
            "types vector too short".into(),
        ));
    }
    if weights.len() < ne {
        return Err(IgraphError::InvalidArgument(
            "weights vector too short".into(),
        ));
    }
    let eps = if eps < 0.0 { 0.0 } else { eps };

    hungarian(graph, types, weights, eps, n, ne)
}

// ── helpers ──────────────────────────────────────────────────────────

fn build_undirected_adj(graph: &Graph) -> Vec<Vec<u32>> {
    let n = graph.vcount() as usize;
    let mut adj: Vec<Vec<u32>> = vec![Vec::new(); n];
    for eid in 0..graph.ecount() {
        if let Ok((u, v)) = graph.edge(eid as u32) {
            adj[u as usize].push(v);
            if u != v {
                adj[v as usize].push(u);
            }
        }
    }
    adj
}

// ── push-relabel unweighted ─────────────────────────────────────────

fn push_relabel_unweighted(
    _graph: &Graph,
    adj: &[Vec<u32>],
    types: &[bool],
    n: usize,
) -> IgraphResult<(Vec<Option<u32>>, usize)> {
    let mut matching: Vec<i64> = vec![-1; n];
    let mut labels: Vec<i64> = vec![0; n];

    // Determine smaller set and greedy init
    let count_true = types[..n].iter().filter(|&&t| t).count();
    let smaller_set = count_true <= n / 2;

    let mut num_matched: usize = 0;
    for i in 0..n {
        if matching[i] != -1 {
            continue;
        }
        for &nb in &adj[i] {
            let nb_usize = nb as usize;
            if types[nb_usize] == types[i] {
                return Err(IgraphError::InvalidArgument(
                    "Graph is not bipartite with supplied types vector".into(),
                ));
            }
            if matching[nb_usize] == -1 {
                matching[nb_usize] = i64::from(i as u32);
                matching[i] = i64::from(nb);
                num_matched += 1;
                break;
            }
        }
    }

    // Global relabeling
    global_relabel(adj, &mut labels, &matching, types, smaller_set, n);

    // Fill push queue with unmatched vertices from smaller set
    let mut q: VecDeque<usize> = VecDeque::new();
    for i in 0..n {
        if matching[i] == -1 && types[i] == smaller_set {
            q.push_back(i);
        }
    }

    let relabeling_freq = (n / 2).max(1);
    let mut label_changed: usize = 0;

    while let Some(v) = q.pop_front() {
        if label_changed >= relabeling_freq {
            global_relabel(adj, &mut labels, &matching, types, smaller_set, n);
            label_changed = 0;
        }

        let mut best_u: i64 = -1;
        let mut best_label: i64 = 2 * n as i64;

        for &nb in &adj[v] {
            let nb_usize = nb as usize;
            if labels[nb_usize] < best_label {
                best_u = i64::from(nb);
                best_label = labels[nb_usize];
                label_changed += 1;
            }
        }

        if best_label < n as i64 {
            let u = best_u as usize;
            labels[v] = labels[u] + 1;
            if matching[u] != -1 {
                let w = matching[u] as usize;
                if w != v {
                    matching[u] = -1;
                    matching[w] = -1;
                    q.push_back(w);
                    num_matched -= 1;
                }
            }
            matching[u] = v as i64;
            matching[v] = u as i64;
            num_matched += 1;
            labels[u] += 2;
            label_changed += 1;
        }
    }

    let result: Vec<Option<u32>> = matching
        .iter()
        .map(|&m| if m < 0 { None } else { Some(m as u32) })
        .collect();

    Ok((result, num_matched))
}

fn global_relabel(
    adj: &[Vec<u32>],
    labels: &mut [i64],
    matching: &[i64],
    types: &[bool],
    smaller_set: bool,
    n: usize,
) {
    labels.fill(n as i64);

    let mut q: VecDeque<usize> = VecDeque::new();
    for i in 0..n {
        if types[i] != smaller_set && matching[i] == -1 {
            q.push_back(i);
            labels[i] = 0;
        }
    }

    while let Some(v) = q.pop_front() {
        for &nb in &adj[v] {
            let w = nb as usize;
            if labels[w] == n as i64 {
                labels[w] = labels[v] + 1;
                let matched_to = matching[w];
                if matched_to != -1 {
                    let mt = matched_to as usize;
                    if labels[mt] == n as i64 {
                        q.push_back(mt);
                        labels[mt] = labels[w] + 1;
                    }
                }
            }
        }
    }
}

// ── Hungarian (weighted) ────────────────────────────────────────────

fn hungarian(
    graph: &Graph,
    types: &[bool],
    weights: &[f64],
    eps: f64,
    n: usize,
    ne: usize,
) -> IgraphResult<MatchingResult> {
    // Build incidence list: for each vertex, list of (edge_id, other_vertex)
    let mut incidence: Vec<Vec<(u32, u32)>> = vec![Vec::new(); n];
    let mut edges: Vec<(u32, u32)> = Vec::with_capacity(ne);
    for eid in 0..ne {
        let (u, v) = graph.edge(eid as u32)?;
        edges.push((u, v));
        incidence[u as usize].push((eid as u32, v));
        if u != v {
            incidence[v as usize].push((eid as u32, u));
        }
    }

    // Find smaller and larger sets
    let count_false = types[..n].iter().filter(|&&t| !t).count();
    let smaller_set_type = count_false > n / 2;
    let smaller_set_size = if smaller_set_type {
        n - count_false
    } else {
        count_false
    };

    let mut smaller_set: Vec<usize> = Vec::with_capacity(smaller_set_size);
    let mut larger_set: Vec<usize> = Vec::with_capacity(n - smaller_set_size);
    for (i, &tp) in types[..n].iter().enumerate() {
        if tp == smaller_set_type {
            smaller_set.push(i);
        } else {
            larger_set.push(i);
        }
    }

    // Initial labeling: for each vertex in smaller set, label = max incident weight
    let mut labels: Vec<f64> = vec![0.0; n];
    for (i, &tp) in types[..n].iter().enumerate() {
        if tp != smaller_set_type {
            continue;
        }
        let mut max_w: f64 = 0.0;
        for &(eid, other) in &incidence[i] {
            if types[other as usize] == types[i] {
                return Err(IgraphError::InvalidArgument(
                    "Graph is not bipartite with supplied types vector".into(),
                ));
            }
            if weights[eid as usize] > max_w {
                max_w = weights[eid as usize];
            }
        }
        labels[i] = max_w;
    }

    // Compute initial slack and tight edges
    let mut slack: Vec<f64> = vec![0.0; ne];
    let mut tight_edges: Vec<(u32, u32)> = Vec::new();
    for eid in 0..ne {
        let (u, v) = edges[eid];
        slack[eid] = labels[u as usize] + labels[v as usize] - weights[eid];
        if slack[eid] <= eps {
            tight_edges.push((u, v));
        }
    }

    // Build initial matching on tight edges using push-relabel
    let tight_graph = crate::algorithms::constructors::create::create(
        &tight_edges.iter().map(|&(a, b)| (a, b)).collect::<Vec<_>>(),
        n as u32,
        false,
    )?;
    let tight_adj = build_undirected_adj(&tight_graph);
    let (init_match_opt, mut msize) = push_relabel_unweighted(&tight_graph, &tight_adj, types, n)?;
    let mut matching: Vec<i64> = init_match_opt
        .iter()
        .map(|o| match o {
            Some(v) => i64::from(*v),
            None => -1,
        })
        .collect();

    // Tight phantom edges adjacency (sorted for binary search)
    let mut tight_phantom: Vec<Vec<usize>> = vec![Vec::new(); n];

    // Main Hungarian loop
    while msize < smaller_set_size {
        let mut parent: Vec<i64> = vec![-1; n];
        let mut reachable_smaller: Vec<usize> = Vec::new();
        let mut reachable_larger: Vec<usize> = Vec::new();

        // Fill queue with unmatched vertices from smaller set
        let mut q: VecDeque<usize> = VecDeque::new();
        for &s in &smaller_set {
            if matching[s] == -1 {
                q.push_back(s);
                parent[s] = s as i64;
                reachable_smaller.push(s);
            }
        }

        // BFS along tight edges
        let mut alternating_path_endpoint: i64 = -1;
        'bfs: while let Some(v) = q.pop_front() {
            // Real tight edges
            for &(eid, other) in &incidence[v] {
                let u = other as usize;
                if slack[eid as usize] > eps {
                    continue;
                }
                if parent[u] >= 0 {
                    continue;
                }
                parent[u] = v as i64;
                reachable_larger.push(u);
                let w = matching[u];
                if w == -1 {
                    alternating_path_endpoint = u as i64;
                    break 'bfs;
                }
                let w_usize = w as usize;
                q.push_back(w_usize);
                parent[w_usize] = u as i64;
                reachable_smaller.push(w_usize);
            }

            // Tight phantom edges
            for &u in &tight_phantom[v] {
                if parent[u] >= 0 {
                    continue;
                }
                if (labels[v] + labels[u]).abs() > eps {
                    continue;
                }
                parent[u] = v as i64;
                reachable_larger.push(u);
                let w = matching[u];
                if w == -1 {
                    alternating_path_endpoint = u as i64;
                    break 'bfs;
                }
                let w_usize = w as usize;
                q.push_back(w_usize);
                parent[w_usize] = u as i64;
                reachable_smaller.push(w_usize);
            }
        }

        if alternating_path_endpoint != -1 {
            // Augment along alternating path
            let mut v = alternating_path_endpoint as usize;
            let mut u = parent[v] as usize;
            while u != v {
                let w = matching[v];
                if w != -1 {
                    matching[w as usize] = -1;
                }
                matching[v] = u as i64;
                let w2 = matching[u];
                if w2 != -1 {
                    matching[w2 as usize] = -1;
                }
                matching[u] = v as i64;

                v = parent[u] as usize;
                u = parent[v] as usize;
            }
            msize += 1;
            continue;
        }

        // No augmenting path found — update labels
        // Find minimum slack between reachable smaller-set and unreachable larger-set

        // Upper bound from phantom edges
        let mut min_label_larger = f64::INFINITY;
        for &l in &larger_set {
            if labels[l] < min_label_larger {
                min_label_larger = labels[l];
            }
        }
        let mut min_label_reachable_smaller = f64::INFINITY;
        for &s in &reachable_smaller {
            if parent[s] >= 0 && labels[s] < min_label_reachable_smaller {
                min_label_reachable_smaller = labels[s];
            }
        }
        let mut min_slack = min_label_larger + min_label_reachable_smaller;

        // Check real edges
        for &u in &reachable_smaller {
            for &(eid, other) in &incidence[u] {
                let v_node = other as usize;
                if parent[v_node] >= 0 {
                    continue;
                }
                if slack[eid as usize] < min_slack {
                    min_slack = slack[eid as usize];
                }
            }
        }

        if min_slack > 0.0 {
            // Update labels and slack
            for &u in &reachable_smaller {
                labels[u] -= min_slack;
                for &(eid, _) in &incidence[u] {
                    slack[eid as usize] -= min_slack;
                }
            }
            for &u in &reachable_larger {
                labels[u] += min_slack;
                for &(eid, _) in &incidence[u] {
                    slack[eid as usize] += min_slack;
                }
            }
        }

        // Update tight phantom edges
        for &u in &smaller_set {
            for &v in &larger_set {
                if (labels[u] + labels[v]).abs() <= eps {
                    let phantoms = &mut tight_phantom[u];
                    match phantoms.binary_search(&v) {
                        Ok(_) => {} // already present
                        Err(pos) => phantoms.insert(pos, v),
                    }
                }
            }
        }
    }

    // Remove phantom matches
    for &u in &smaller_set {
        let v = matching[u];
        if v != -1 {
            let v_usize = v as usize;
            if tight_phantom[u].binary_search(&v_usize).is_ok() {
                // Check if this is a real edge or phantom
                let is_real = incidence[u]
                    .iter()
                    .any(|&(_, other)| other as usize == v_usize);
                if !is_real {
                    matching[u] = -1;
                    matching[v_usize] = -1;
                    msize -= 1;
                }
            }
        }
    }

    // Compute matching weight
    let mut total_weight: f64 = 0.0;
    for eid in 0..ne {
        if slack[eid] <= eps {
            let (u, v) = edges[eid];
            if matching[u as usize] == i64::from(v) {
                total_weight += weights[eid];
            }
        }
    }

    let result_matching: Vec<Option<u32>> = matching
        .iter()
        .map(|&m| if m < 0 { None } else { Some(m as u32) })
        .collect();

    Ok(MatchingResult {
        matching_size: msize,
        matching_weight: total_weight,
        matching: result_matching,
    })
}

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

    fn make_k22() -> (Graph, Vec<bool>) {
        let g = create(&[(0, 2), (0, 3), (1, 2), (1, 3)], 4, false).expect("K22");
        let types = vec![false, false, true, true];
        (g, types)
    }

    // ── is_matching ─────────────────────────────────────────────

    #[test]
    fn is_matching_valid() {
        let (g, types) = make_k22();
        let m = vec![Some(2), Some(3), Some(0), Some(1)];
        assert!(is_matching(&g, Some(&types), &m).expect("ok"));
    }

    #[test]
    fn is_matching_wrong_length() {
        let (g, _) = make_k22();
        let m = vec![Some(1), Some(0)];
        assert!(!is_matching(&g, None, &m).expect("ok"));
    }

    #[test]
    fn is_matching_non_mutual() {
        let (g, _) = make_k22();
        let m = vec![Some(2), None, None, None];
        assert!(!is_matching(&g, None, &m).expect("ok"));
    }

    #[test]
    fn is_matching_no_edge() {
        let g = create(&[(0, 1)], 3, false).expect("ok");
        // vertices 0 and 2 are not connected
        let m = vec![Some(2), None, Some(0)];
        assert!(!is_matching(&g, None, &m).expect("ok"));
    }

    #[test]
    fn is_matching_all_unmatched_with_types() {
        let g = create(&[(0, 1), (1, 2), (2, 3)], 4, false).expect("ok");
        let types = vec![false, true, false, true];
        let m = vec![None, None, None, None];
        assert!(is_matching(&g, Some(&types), &m).expect("ok"));
    }

    #[test]
    fn is_matching_types_same_partition() {
        let g = create(&[(0, 1), (1, 2)], 3, false).expect("ok");
        let types = vec![false, false, true]; // 0 and 1 same type but matched
        let m = vec![Some(1), Some(0), None];
        assert!(!is_matching(&g, Some(&types), &m).expect("ok"));
    }

    // ── is_maximal_matching ─────────────────────────────────────

    #[test]
    fn is_maximal_matching_true() {
        let (g, types) = make_k22();
        let m = vec![Some(2), Some(3), Some(0), Some(1)];
        assert!(is_maximal_matching(&g, Some(&types), &m).expect("ok"));
    }

    #[test]
    fn is_maximal_matching_false() {
        let (g, types) = make_k22();
        // Only 0-2 matched, but 1 and 3 are both unmatched and connected → not maximal
        let m = vec![Some(2), None, Some(0), None];
        assert!(!is_maximal_matching(&g, Some(&types), &m).expect("ok"));
    }

    #[test]
    fn is_maximal_all_unmatched_no_edges() {
        let g = Graph::new(3, false).expect("ok");
        let m = vec![None, None, None];
        assert!(is_maximal_matching(&g, None, &m).expect("ok"));
    }

    // ── maximum_bipartite_matching ──────────────────────────────

    #[test]
    fn max_matching_k22() {
        let (g, types) = make_k22();
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 2);
        assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
    }

    #[test]
    fn max_matching_empty() {
        let g = Graph::new(0, false).expect("ok");
        let types: Vec<bool> = vec![];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 0);
    }

    #[test]
    fn max_matching_singleton() {
        let g = Graph::new(1, false).expect("ok");
        let types = vec![false];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 0);
    }

    #[test]
    fn max_matching_path_4() {
        // 0-1-2-3, bipartite with types [F,T,F,T]
        let g = create(&[(0, 1), (1, 2), (2, 3)], 4, false).expect("ok");
        let types = vec![false, true, false, true];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 2);
        assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
    }

    #[test]
    fn max_matching_star() {
        // Star: 0 connected to 1,2,3,4
        let g = create(&[(0, 1), (0, 2), (0, 3), (0, 4)], 5, false).expect("ok");
        let types = vec![false, true, true, true, true];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 1);
        assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
    }

    #[test]
    fn max_matching_complete_bipartite_k33() {
        let g = create(
            &[
                (0, 3),
                (0, 4),
                (0, 5),
                (1, 3),
                (1, 4),
                (1, 5),
                (2, 3),
                (2, 4),
                (2, 5),
            ],
            6,
            false,
        )
        .expect("ok");
        let types = vec![false, false, false, true, true, true];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 3);
        assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
    }

    #[test]
    fn max_matching_not_bipartite_error() {
        // Triangle: not bipartite
        let g = create(&[(0, 1), (1, 2), (2, 0)], 3, false).expect("ok");
        let types = vec![false, true, false]; // 0 and 2 connected but same type
        let r = maximum_bipartite_matching(&g, &types);
        assert!(r.is_err());
    }

    #[test]
    fn max_matching_disconnected() {
        // Two disconnected edges
        let g = create(&[(0, 1), (2, 3)], 4, false).expect("ok");
        let types = vec![false, true, false, true];
        let r = maximum_bipartite_matching(&g, &types).expect("ok");
        assert_eq!(r.matching_size, 2);
    }

    #[test]
    fn max_matching_types_too_short() {
        let g = create(&[(0, 1)], 2, false).expect("ok");
        let types = vec![false];
        let r = maximum_bipartite_matching(&g, &types);
        assert!(r.is_err());
    }

    // ── maximum_bipartite_matching_weighted ──────────────────────

    #[test]
    fn weighted_matching_simple() {
        // K2,2 with weights: prefer 0-3 and 1-2
        let g = create(&[(0, 2), (0, 3), (1, 2), (1, 3)], 4, false).expect("ok");
        let types = vec![false, false, true, true];
        let weights = vec![1.0, 10.0, 10.0, 1.0];
        let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
        assert_eq!(r.matching_size, 2);
        assert!((r.matching_weight - 20.0).abs() < 1e-9);
    }

    #[test]
    fn weighted_matching_mit_notes() {
        // Test graph from MIT lecture notes on matching
        // 10 vertices: 0-4 in set A, 5-9 in set B
        let g = create(
            &[
                (0, 6),
                (0, 7),
                (0, 8),
                (0, 9),
                (1, 5),
                (1, 6),
                (1, 7),
                (1, 8),
                (1, 9),
                (2, 5),
                (2, 6),
                (2, 7),
                (2, 8),
                (2, 9),
                (3, 5),
                (3, 7),
                (3, 9),
                (4, 7),
            ],
            10,
            false,
        )
        .expect("ok");
        let types: Vec<bool> = (0..10).map(|i| i >= 5).collect();
        let weights = vec![
            2.0, 7.0, 2.0, 3.0, // edges from 0
            1.0, 3.0, 9.0, 3.0, 3.0, // edges from 1
            1.0, 3.0, 3.0, 1.0, 2.0, // edges from 2
            4.0, 1.0, 2.0, // edges from 3
            3.0, // edge from 4
        ];
        let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
        assert_eq!(r.matching_size, 4);
        assert!((r.matching_weight - 19.0).abs() < 1e-9);
        assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
    }

    #[test]
    fn weighted_matching_generated_case1() {
        let g = create(&[(0, 8), (2, 7), (3, 7), (3, 8), (4, 5), (4, 9)], 10, false).expect("ok");
        let types: Vec<bool> = (0..10).map(|i| i >= 5).collect();
        let weights = vec![8.0, 5.0, 9.0, 18.0, 20.0, 13.0];
        let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
        assert!((r.matching_weight - 43.0).abs() < 1e-9);
    }

    #[test]
    fn weighted_matching_generated_case2() {
        let g = create(&[(0, 5), (0, 6), (1, 7), (2, 5), (3, 5), (3, 9)], 10, false).expect("ok");
        let types: Vec<bool> = (0..10).map(|i| i >= 5).collect();
        let weights = vec![20.0, 4.0, 20.0, 3.0, 13.0, 1.0];
        let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
        assert!((r.matching_weight - 41.0).abs() < 1e-9);
    }

    #[test]
    fn weighted_matching_empty() {
        let g = Graph::new(0, false).expect("ok");
        let r = maximum_bipartite_matching_weighted(&g, &[], &[], 0.0).expect("ok");
        assert_eq!(r.matching_size, 0);
    }

    #[test]
    fn weighted_matching_no_edges() {
        let g = Graph::new(4, false).expect("ok");
        let types = vec![false, false, true, true];
        let r = maximum_bipartite_matching_weighted(&g, &types, &[], 0.0).expect("ok");
        assert_eq!(r.matching_size, 0);
    }

    // ── proptest ────────────────────────────────────────────────

    #[cfg(all(test, feature = "proptest-harness"))]
    mod proptests {
        use super::*;
        use proptest::prelude::*;

        fn arb_bipartite_graph(
            max_a: u32,
            max_b: u32,
        ) -> impl Strategy<Value = (Graph, Vec<bool>)> {
            (1..=max_a, 1..=max_b).prop_flat_map(move |(a, b)| {
                let pool = (a as usize) * (b as usize);
                let mask_len = pool.min(20);
                proptest::collection::vec(proptest::bool::ANY, mask_len).prop_map(move |mask| {
                    let n = a + b;
                    let mut edges = Vec::new();
                    for (idx, &present) in mask.iter().enumerate() {
                        if present {
                            let u = (idx as u32) / b;
                            let v = a + (idx as u32) % b;
                            edges.push((u, v));
                        }
                    }
                    let g = create(&edges, n, false).expect("bipartite graph");
                    let types: Vec<bool> = (0..n).map(|i| i >= a).collect();
                    (g, types)
                })
            })
        }

        proptest! {
            #[test]
            fn matching_is_valid((g, types) in arb_bipartite_graph(6, 6)) {
                let r = maximum_bipartite_matching(&g, &types).expect("ok");
                prop_assert!(is_matching(&g, Some(&types), &r.matching).expect("ok"));
                prop_assert!(is_maximal_matching(&g, Some(&types), &r.matching).expect("ok"));
            }

            #[test]
            fn matching_size_leq_min_partition(
                (g, types) in arb_bipartite_graph(6, 6)
            ) {
                let r = maximum_bipartite_matching(&g, &types).expect("ok");
                let a_size = types.iter().filter(|&&t| !t).count();
                let b_size = types.iter().filter(|&&t| t).count();
                prop_assert!(r.matching_size <= a_size.min(b_size));
            }

            #[test]
            fn matching_size_leq_ecount(
                (g, types) in arb_bipartite_graph(6, 6)
            ) {
                let r = maximum_bipartite_matching(&g, &types).expect("ok");
                prop_assert!(r.matching_size <= g.ecount());
            }

            #[test]
            fn weighted_matching_is_valid((g, types) in arb_bipartite_graph(5, 5)) {
                let ne = g.ecount();
                let weights: Vec<f64> = (0..ne).map(|i| (i as f64) + 1.0).collect();
                if ne > 0 {
                    let r = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
                    prop_assert!(is_matching(&g, Some(&types), &r.matching).expect("ok"));
                }
            }

            #[test]
            fn weighted_geq_unweighted_unit(
                (g, types) in arb_bipartite_graph(5, 5)
            ) {
                let unw = maximum_bipartite_matching(&g, &types).expect("ok");
                let ne = g.ecount();
                let weights: Vec<f64> = vec![1.0; ne];
                if ne > 0 {
                    let w = maximum_bipartite_matching_weighted(&g, &types, &weights, 0.0).expect("ok");
                    // With unit weights, weighted should find same size or better
                    prop_assert!(w.matching_size >= unw.matching_size.saturating_sub(1),
                        "weighted: {}, unweighted: {}", w.matching_size, unw.matching_size);
                }
            }
        }
    }
}