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
//! Graph-level centralization indices (ALGO-PR-033 + ALGO-PR-043).
//!
//! Counterpart of `igraph_centralization*` from
//! `references/igraph/src/centrality/centralization.c` (723 lines).
//!
//! Centralization measures how much a graph's structure revolves
//! around a single vertex. Given per-vertex centrality scores, the
//! graph-level centralization is:
//!
//! ```text
//! C = Σ_v (max_u c_u - c_v)
//! ```
//!
//! Optionally divided by the theoretical maximum for the most
//! centralized structure (usually a star graph) of the same size.
//!
//! ## Convenience wrappers (ALGO-PR-043)
//!
//! [`centralization_degree_wrapper`], [`centralization_betweenness_wrapper`],
//! [`centralization_closeness_wrapper`], and
//! [`centralization_eigenvector_wrapper`] compose the per-vertex score
//! functions with the tmax + centralization formula into single calls.

use super::betweenness::betweenness;
use super::closeness::closeness;
use super::degree::DegreeMode;
use super::eigenvector::{EigenvectorMode, eigenvector_centrality_full};
use super::strength::{StrengthMode, strength_with_mode};
use crate::core::{Graph, IgraphResult};

/// How loops are counted when computing degree centralization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoopMode {
    /// Ignore loop edges entirely.
    NoLoops,
    /// Count each loop edge once.
    LoopsOnce,
    /// Count each loop edge twice (undirected) or once (directed).
    LoopsTwice,
}

/// Whether centralization considers in-degree, out-degree, or total.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CentralizationMode {
    /// In-degree centralization (directed graphs).
    In,
    /// Out-degree centralization (directed graphs).
    Out,
    /// Total degree centralization.
    All,
}

/// Compute the graph-level centralization score from per-vertex scores.
///
/// Returns `C = n * max(scores) - sum(scores)` when `normalized` is
/// `false`, or `C / theoretical_max` when `normalized` is `true`.
///
/// Returns `NaN` for empty score vectors.
///
/// # Examples
///
/// ```
/// use rust_igraph::centralization;
///
/// // Star graph degree scores: center=4, leaves=1,1,1,1
/// let scores = [4.0, 1.0, 1.0, 1.0, 1.0];
/// let c = centralization(&scores, 12.0, true);
/// assert!((c - 1.0).abs() < 1e-9);
/// ```
#[allow(clippy::cast_precision_loss)]
pub fn centralization(scores: &[f64], theoretical_max: f64, normalized: bool) -> f64 {
    if scores.is_empty() {
        return f64::NAN;
    }

    let max_score = scores.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let n = scores.len() as f64;
    let sum: f64 = scores.iter().sum();
    let cent = n * max_score - sum;

    if normalized {
        cent / theoretical_max
    } else {
        cent
    }
}

/// Theoretical maximum degree centralization for a star graph.
///
/// Returns `NaN` for `n == 0`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{CentralizationMode, LoopMode, centralization_degree_tmax};
///
/// // Undirected, no loops, 5 vertices: (5-1)*(5-2) = 12
/// let tmax = centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::NoLoops);
/// assert!((tmax - 12.0).abs() < 1e-9);
/// ```
#[allow(clippy::cast_precision_loss)]
pub fn centralization_degree_tmax(
    n: u32,
    directed: bool,
    mode: CentralizationMode,
    loops: LoopMode,
) -> f64 {
    if n == 0 {
        return f64::NAN;
    }

    let nf = f64::from(n);

    if directed {
        match mode {
            CentralizationMode::In | CentralizationMode::Out => {
                if matches!(loops, LoopMode::NoLoops) {
                    (nf - 1.0) * (nf - 1.0)
                } else {
                    (nf - 1.0) * nf
                }
            }
            CentralizationMode::All => {
                if matches!(loops, LoopMode::NoLoops) {
                    2.0 * (nf - 1.0) * (nf - 2.0)
                } else {
                    2.0 * (nf - 1.0) * (nf - 1.0)
                }
            }
        }
    } else {
        match loops {
            LoopMode::NoLoops => (nf - 1.0) * (nf - 2.0),
            LoopMode::LoopsOnce => (nf - 1.0) * (nf - 1.0),
            LoopMode::LoopsTwice => (nf - 1.0) * nf,
        }
    }
}

/// Theoretical maximum betweenness centralization for a star graph.
///
/// Returns `NaN` for `n == 0`.
///
/// # Examples
///
/// ```
/// use rust_igraph::centralization_betweenness_tmax;
///
/// // Undirected, 5 vertices: (5-1)*(5-1)*(5-2)/2 = 24
/// let tmax = centralization_betweenness_tmax(5, false);
/// assert!((tmax - 24.0).abs() < 1e-9);
/// ```
pub fn centralization_betweenness_tmax(n: u32, directed: bool) -> f64 {
    if n == 0 {
        return f64::NAN;
    }

    let nf = f64::from(n);

    if directed {
        (nf - 1.0) * (nf - 1.0) * (nf - 2.0)
    } else {
        (nf - 1.0) * (nf - 1.0) * (nf - 2.0) / 2.0
    }
}

/// Theoretical maximum closeness centralization for a star graph.
///
/// For directed graphs, `mode` distinguishes IN/OUT from ALL.
/// For undirected, `mode` should be `All`.
///
/// Returns `NaN` for `n == 0`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{CentralizationMode, centralization_closeness_tmax};
///
/// // Undirected, 5 vertices: (5-1)*(5-2)/(2*5-3) = 12/7
/// let tmax = centralization_closeness_tmax(5, CentralizationMode::All);
/// assert!((tmax - 12.0 / 7.0).abs() < 1e-9);
/// ```
pub fn centralization_closeness_tmax(n: u32, mode: CentralizationMode) -> f64 {
    if n == 0 {
        return f64::NAN;
    }

    let nf = f64::from(n);

    match mode {
        CentralizationMode::In | CentralizationMode::Out => (nf - 1.0) * (1.0 - 1.0 / nf),
        CentralizationMode::All => (nf - 1.0) * (nf - 2.0) / (2.0 * nf - 3.0),
    }
}

/// Theoretical maximum eigenvector centralization for a star graph.
///
/// For directed graphs, `mode` distinguishes IN/OUT from ALL.
/// For undirected, `mode` should be `All`.
///
/// Returns `NaN` for `n == 0`, `0.0` for `n == 1`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{CentralizationMode, centralization_eigenvector_tmax};
///
/// // Undirected, 5 vertices: 5-2 = 3
/// let tmax = centralization_eigenvector_tmax(5, CentralizationMode::All);
/// assert!((tmax - 3.0).abs() < 1e-9);
/// ```
pub fn centralization_eigenvector_tmax(n: u32, mode: CentralizationMode) -> f64 {
    if n == 0 {
        return f64::NAN;
    }
    if n == 1 {
        return 0.0;
    }

    let nf = f64::from(n);

    match mode {
        CentralizationMode::In | CentralizationMode::Out => nf - 1.0,
        CentralizationMode::All => nf - 2.0,
    }
}

/// Result of a centralization convenience wrapper.
#[derive(Debug, Clone, PartialEq)]
pub struct CentralizationResult {
    /// Per-vertex centrality scores.
    pub scores: Vec<f64>,
    /// Graph-level centralization value.
    pub centralization: f64,
    /// Theoretical maximum for the most centralized graph of this size.
    pub theoretical_max: f64,
}

fn degree_to_strength_mode(mode: DegreeMode) -> StrengthMode {
    match mode {
        DegreeMode::Out => StrengthMode::Out,
        DegreeMode::In => StrengthMode::In,
        DegreeMode::All => StrengthMode::All,
    }
}

fn degree_to_cent_mode(mode: DegreeMode) -> CentralizationMode {
    match mode {
        DegreeMode::Out => CentralizationMode::Out,
        DegreeMode::In => CentralizationMode::In,
        DegreeMode::All => CentralizationMode::All,
    }
}

/// Degree centralization: compute per-vertex degree scores and the
/// graph-level centralization in one call.
///
/// `mode` selects in-/out-/total degree for directed graphs (ignored
/// for undirected). `loops` controls whether self-loops are counted.
///
/// Counterpart of `igraph_centralization_degree()`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, DegreeMode, centralization_degree_wrapper};
///
/// // Star K_{1,4}: normalized degree centralization = 1.0.
/// let mut g = Graph::with_vertices(5);
/// for v in 1..5u32 { g.add_edge(0, v).unwrap(); }
/// let r = centralization_degree_wrapper(&g, DegreeMode::All, false, true).unwrap();
/// assert!((r.centralization - 1.0).abs() < 1e-9);
/// assert!((r.scores[0] - 4.0).abs() < 1e-9);
/// ```
pub fn centralization_degree_wrapper(
    graph: &Graph,
    mode: DegreeMode,
    loops: bool,
    normalized: bool,
) -> IgraphResult<CentralizationResult> {
    let n = graph.vcount();
    let ecount = graph.ecount();

    let scores = if ecount == 0 {
        vec![0.0_f64; n as usize]
    } else {
        let unit_w = vec![1.0_f64; ecount];
        strength_with_mode(graph, &unit_w, degree_to_strength_mode(mode), loops)?
    };

    let loop_mode = if loops {
        LoopMode::LoopsTwice
    } else {
        LoopMode::NoLoops
    };
    let tmax =
        centralization_degree_tmax(n, graph.is_directed(), degree_to_cent_mode(mode), loop_mode);
    let cent = centralization(&scores, tmax, normalized);

    Ok(CentralizationResult {
        scores,
        centralization: cent,
        theoretical_max: tmax,
    })
}

/// Betweenness centralization: compute per-vertex betweenness scores
/// and the graph-level centralization in one call.
///
/// Directedness is derived from `graph.is_directed()`.
///
/// Counterpart of `igraph_centralization_betweenness()`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, centralization_betweenness_wrapper};
///
/// // Star K_{1,4}: normalized betweenness centralization = 1.0.
/// let mut g = Graph::with_vertices(5);
/// for v in 1..5u32 { g.add_edge(0, v).unwrap(); }
/// let r = centralization_betweenness_wrapper(&g, true).unwrap();
/// assert!((r.centralization - 1.0).abs() < 1e-9);
/// ```
pub fn centralization_betweenness_wrapper(
    graph: &Graph,
    normalized: bool,
) -> IgraphResult<CentralizationResult> {
    let scores = betweenness(graph)?;
    let directed = graph.is_directed();
    let tmax = centralization_betweenness_tmax(graph.vcount(), directed);
    let cent = centralization(&scores, tmax, normalized);

    Ok(CentralizationResult {
        scores,
        centralization: cent,
        theoretical_max: tmax,
    })
}

/// Closeness centralization: compute per-vertex closeness scores and
/// the graph-level centralization in one call.
///
/// Vertices with no reachable neighbors produce `NaN` scores; if any
/// vertex is unreachable the centralization itself will be `NaN`
/// (matching igraph C semantics for disconnected graphs).
///
/// Counterpart of `igraph_centralization_closeness()`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, centralization_closeness_wrapper};
///
/// // Star K_{1,4}: normalized closeness centralization = 1.0.
/// let mut g = Graph::with_vertices(5);
/// for v in 1..5u32 { g.add_edge(0, v).unwrap(); }
/// let r = centralization_closeness_wrapper(&g, true).unwrap();
/// assert!((r.centralization - 1.0).abs() < 1e-9);
/// ```
pub fn centralization_closeness_wrapper(
    graph: &Graph,
    normalized: bool,
) -> IgraphResult<CentralizationResult> {
    let raw = closeness(graph)?;
    let scores: Vec<f64> = raw.into_iter().map(|v| v.unwrap_or(f64::NAN)).collect();

    let cent_mode = if graph.is_directed() {
        CentralizationMode::Out
    } else {
        CentralizationMode::All
    };
    let tmax = centralization_closeness_tmax(graph.vcount(), cent_mode);
    let cent = centralization(&scores, tmax, normalized);

    Ok(CentralizationResult {
        scores,
        centralization: cent,
        theoretical_max: tmax,
    })
}

/// Eigenvector centralization: compute per-vertex eigenvector centrality
/// scores and the graph-level centralization in one call.
///
/// For directed graphs, uses `EigenvectorMode::Out` (the standard
/// convention matching upstream). The eigenvector is max-1 normalised.
///
/// Counterpart of `igraph_centralization_eigenvector_centrality()`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, centralization_eigenvector_wrapper};
///
/// // Star K_{1,4}: center has eigenvector centrality 1.0, leaves ≈ 0.5.
/// let mut g = Graph::with_vertices(5);
/// for v in 1..5u32 { g.add_edge(0, v).unwrap(); }
/// let r = centralization_eigenvector_wrapper(&g, true).unwrap();
/// assert!(r.centralization > 0.0);
/// assert!((r.scores[0] - 1.0).abs() < 1e-9);
/// ```
pub fn centralization_eigenvector_wrapper(
    graph: &Graph,
    normalized: bool,
) -> IgraphResult<CentralizationResult> {
    let eig_mode = if graph.is_directed() {
        EigenvectorMode::Out
    } else {
        EigenvectorMode::All
    };
    let eig = eigenvector_centrality_full(graph, eig_mode, None)?;
    let scores = eig.vector;

    let cent_mode = if graph.is_directed() {
        CentralizationMode::Out
    } else {
        CentralizationMode::All
    };
    let tmax = centralization_eigenvector_tmax(graph.vcount(), cent_mode);
    let cent = centralization(&scores, tmax, normalized);

    Ok(CentralizationResult {
        scores,
        centralization: cent,
        theoretical_max: tmax,
    })
}

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

    fn approx_eq(a: f64, b: f64) -> bool {
        (a - b).abs() < 1e-9
    }

    #[test]
    fn centralization_empty() {
        let c = centralization(&[], 1.0, false);
        assert!(c.is_nan());
    }

    #[test]
    fn centralization_single() {
        let c = centralization(&[5.0], 1.0, false);
        assert!(approx_eq(c, 0.0));
    }

    #[test]
    fn centralization_star_degree() {
        let scores = [4.0, 1.0, 1.0, 1.0, 1.0];
        let c = centralization(&scores, 12.0, false);
        assert!(approx_eq(c, 12.0));

        let c_norm = centralization(&scores, 12.0, true);
        assert!(approx_eq(c_norm, 1.0));
    }

    #[test]
    fn centralization_uniform() {
        let scores = [3.0, 3.0, 3.0, 3.0];
        let c = centralization(&scores, 6.0, false);
        assert!(approx_eq(c, 0.0));
    }

    #[test]
    fn centralization_normalized() {
        let scores = [4.0, 2.0, 1.0];
        let unnorm = centralization(&scores, 1.0, false);
        let norm = centralization(&scores, 6.0, true);
        assert!(approx_eq(unnorm, 5.0));
        assert!(approx_eq(norm, 5.0 / 6.0));
    }

    #[test]
    fn degree_tmax_zero() {
        assert!(
            centralization_degree_tmax(0, false, CentralizationMode::All, LoopMode::NoLoops)
                .is_nan()
        );
    }

    #[test]
    fn degree_tmax_undirected_no_loops() {
        assert!(approx_eq(
            centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::NoLoops),
            12.0
        ));
        assert!(approx_eq(
            centralization_degree_tmax(10, false, CentralizationMode::All, LoopMode::NoLoops),
            72.0
        ));
    }

    #[test]
    fn degree_tmax_undirected_loops_once() {
        assert!(approx_eq(
            centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::LoopsOnce),
            16.0
        ));
    }

    #[test]
    fn degree_tmax_undirected_loops_twice() {
        assert!(approx_eq(
            centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::LoopsTwice),
            20.0
        ));
    }

    #[test]
    fn degree_tmax_directed_in_no_loops() {
        assert!(approx_eq(
            centralization_degree_tmax(5, true, CentralizationMode::In, LoopMode::NoLoops),
            16.0
        ));
    }

    #[test]
    fn degree_tmax_directed_all_no_loops() {
        assert!(approx_eq(
            centralization_degree_tmax(5, true, CentralizationMode::All, LoopMode::NoLoops),
            24.0
        ));
    }

    #[test]
    fn degree_tmax_directed_in_with_loops() {
        assert!(approx_eq(
            centralization_degree_tmax(5, true, CentralizationMode::In, LoopMode::LoopsTwice),
            20.0
        ));
    }

    #[test]
    fn degree_tmax_directed_all_with_loops() {
        assert!(approx_eq(
            centralization_degree_tmax(5, true, CentralizationMode::All, LoopMode::LoopsTwice),
            32.0
        ));
    }

    #[test]
    fn betweenness_tmax_zero() {
        assert!(centralization_betweenness_tmax(0, false).is_nan());
    }

    #[test]
    fn betweenness_tmax_undirected() {
        assert!(approx_eq(centralization_betweenness_tmax(5, false), 24.0));
        assert!(approx_eq(centralization_betweenness_tmax(3, false), 2.0));
    }

    #[test]
    fn betweenness_tmax_directed() {
        assert!(approx_eq(centralization_betweenness_tmax(5, true), 48.0));
        assert!(approx_eq(centralization_betweenness_tmax(3, true), 4.0));
    }

    #[test]
    fn closeness_tmax_zero() {
        assert!(centralization_closeness_tmax(0, CentralizationMode::All).is_nan());
    }

    #[test]
    fn closeness_tmax_undirected() {
        let tmax = centralization_closeness_tmax(5, CentralizationMode::All);
        assert!(approx_eq(tmax, 12.0 / 7.0));
    }

    #[test]
    fn closeness_tmax_directed() {
        let tmax = centralization_closeness_tmax(5, CentralizationMode::In);
        assert!(approx_eq(tmax, 4.0 * (1.0 - 1.0 / 5.0)));
    }

    #[test]
    fn eigenvector_tmax_zero() {
        assert!(centralization_eigenvector_tmax(0, CentralizationMode::All).is_nan());
    }

    #[test]
    fn eigenvector_tmax_one() {
        assert!(approx_eq(
            centralization_eigenvector_tmax(1, CentralizationMode::All),
            0.0
        ));
    }

    #[test]
    fn eigenvector_tmax_undirected() {
        assert!(approx_eq(
            centralization_eigenvector_tmax(5, CentralizationMode::All),
            3.0
        ));
    }

    #[test]
    fn eigenvector_tmax_directed() {
        assert!(approx_eq(
            centralization_eigenvector_tmax(5, CentralizationMode::In),
            4.0
        ));
    }

    #[test]
    fn star5_degree_centralization() {
        let scores = [4.0, 1.0, 1.0, 1.0, 1.0];
        let tmax = centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::NoLoops);
        let c = centralization(&scores, tmax, true);
        assert!(approx_eq(c, 1.0), "star degree centralization = {c}");
    }

    #[test]
    fn ring_degree_centralization() {
        let scores = [2.0, 2.0, 2.0, 2.0, 2.0];
        let tmax = centralization_degree_tmax(5, false, CentralizationMode::All, LoopMode::NoLoops);
        let c = centralization(&scores, tmax, true);
        assert!(approx_eq(c, 0.0), "ring degree centralization = {c}");
    }

    #[test]
    fn star_betweenness_centralization() {
        let n = 5u32;
        let scores = [6.0, 0.0, 0.0, 0.0, 0.0];
        let tmax = centralization_betweenness_tmax(n, false);
        let c = centralization(&scores, tmax, true);
        assert!(approx_eq(c, 1.0), "star betweenness centralization = {c}");
    }

    // ── wrapper tests ────────────────────────────────────────────────

    fn make_star(n: u32) -> Graph {
        let mut g = Graph::with_vertices(n);
        for v in 1..n {
            g.add_edge(0, v).unwrap();
        }
        g
    }

    fn make_ring(n: u32) -> Graph {
        let mut g = Graph::with_vertices(n);
        for v in 0..n {
            g.add_edge(v, (v + 1) % n).unwrap();
        }
        g
    }

    fn make_path(n: u32) -> Graph {
        let mut g = Graph::with_vertices(n);
        for v in 0..n - 1 {
            g.add_edge(v, v + 1).unwrap();
        }
        g
    }

    // -- degree wrapper --

    #[test]
    fn wrapper_degree_star5() {
        let g = make_star(5);
        let r = centralization_degree_wrapper(&g, DegreeMode::All, false, true).unwrap();
        assert!(approx_eq(r.centralization, 1.0), "got {}", r.centralization);
        assert!(approx_eq(r.scores[0], 4.0));
        for i in 1..5 {
            assert!(approx_eq(r.scores[i], 1.0));
        }
    }

    #[test]
    fn wrapper_degree_ring5() {
        let g = make_ring(5);
        let r = centralization_degree_wrapper(&g, DegreeMode::All, false, true).unwrap();
        assert!(approx_eq(r.centralization, 0.0), "got {}", r.centralization);
    }

    #[test]
    fn wrapper_degree_empty() {
        let g = Graph::with_vertices(0);
        let r = centralization_degree_wrapper(&g, DegreeMode::All, false, true).unwrap();
        assert!(r.scores.is_empty());
        assert!(r.centralization.is_nan());
    }

    #[test]
    fn wrapper_degree_single_vertex() {
        let g = Graph::with_vertices(1);
        let r = centralization_degree_wrapper(&g, DegreeMode::All, false, false).unwrap();
        assert_eq!(r.scores.len(), 1);
        assert!(approx_eq(r.scores[0], 0.0));
        assert!(approx_eq(r.centralization, 0.0));
    }

    #[test]
    fn wrapper_degree_unnormalized() {
        let g = make_star(5);
        let r = centralization_degree_wrapper(&g, DegreeMode::All, false, false).unwrap();
        assert!(approx_eq(r.centralization, 12.0));
        assert!(approx_eq(r.theoretical_max, 12.0));
    }

    #[test]
    fn wrapper_degree_with_self_loops() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 0).unwrap(); // self-loop
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        let r_loops = centralization_degree_wrapper(&g, DegreeMode::All, true, false).unwrap();
        let r_no_loops = centralization_degree_wrapper(&g, DegreeMode::All, false, false).unwrap();
        // With loops: vertex 0 degree = 2+1 = 3 (self-loop twice + edge)
        assert!(r_loops.scores[0] > r_no_loops.scores[0]);
    }

    // -- betweenness wrapper --

    #[test]
    fn wrapper_betweenness_star5() {
        let g = make_star(5);
        let r = centralization_betweenness_wrapper(&g, true).unwrap();
        assert!(approx_eq(r.centralization, 1.0), "got {}", r.centralization);
    }

    #[test]
    fn wrapper_betweenness_path5() {
        let g = make_path(5);
        let r = centralization_betweenness_wrapper(&g, false).unwrap();
        assert!(r.centralization > 0.0);
        // Center vertex (2) should have highest betweenness
        assert!(r.scores[2] > r.scores[0]);
        assert!(r.scores[2] > r.scores[4]);
    }

    #[test]
    fn wrapper_betweenness_ring() {
        let g = make_ring(5);
        let r = centralization_betweenness_wrapper(&g, true).unwrap();
        assert!(approx_eq(r.centralization, 0.0), "got {}", r.centralization);
    }

    #[test]
    fn wrapper_betweenness_empty() {
        let g = Graph::with_vertices(0);
        let r = centralization_betweenness_wrapper(&g, true).unwrap();
        assert!(r.scores.is_empty());
    }

    // -- closeness wrapper --

    #[test]
    fn wrapper_closeness_star5() {
        let g = make_star(5);
        let r = centralization_closeness_wrapper(&g, true).unwrap();
        assert!(approx_eq(r.centralization, 1.0), "got {}", r.centralization);
        // Center has closeness 1.0
        assert!(approx_eq(r.scores[0], 1.0));
    }

    #[test]
    fn wrapper_closeness_ring() {
        let g = make_ring(5);
        let r = centralization_closeness_wrapper(&g, true).unwrap();
        assert!(approx_eq(r.centralization, 0.0), "got {}", r.centralization);
    }

    #[test]
    fn wrapper_closeness_disconnected_is_nan() {
        // Two isolated vertices → closeness is None → centralization is NaN
        let g = Graph::with_vertices(2);
        let r = centralization_closeness_wrapper(&g, true).unwrap();
        assert!(r.scores[0].is_nan());
        assert!(r.scores[1].is_nan());
        assert!(r.centralization.is_nan());
    }

    // -- eigenvector wrapper --

    #[test]
    fn wrapper_eigenvector_star5() {
        let g = make_star(5);
        let r = centralization_eigenvector_wrapper(&g, true).unwrap();
        assert!(r.centralization > 0.0);
        // Center should have highest score (1.0)
        assert!(approx_eq(r.scores[0], 1.0));
        // Leaves should have equal score
        for i in 1..5 {
            assert!(approx_eq(r.scores[i], r.scores[1]));
        }
    }

    #[test]
    fn wrapper_eigenvector_ring() {
        let g = make_ring(5);
        let r = centralization_eigenvector_wrapper(&g, true).unwrap();
        // Regular graph: all vertices have same eigenvector centrality
        assert!(approx_eq(r.centralization, 0.0), "got {}", r.centralization);
    }

    #[test]
    fn wrapper_eigenvector_empty() {
        let g = Graph::with_vertices(0);
        let r = centralization_eigenvector_wrapper(&g, true).unwrap();
        assert!(r.scores.is_empty());
    }
}

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

    proptest! {
        #[test]
        fn centralization_non_negative(
            scores in proptest::collection::vec(0.0f64..100.0, 1..20)
        ) {
            let c = centralization(&scores, 1.0, false);
            prop_assert!(c >= 0.0, "centralization should be >= 0, got {c}");
        }

        #[test]
        fn centralization_uniform_is_zero(val in 0.0f64..100.0, n in 1usize..20) {
            let scores = vec![val; n];
            let c = centralization(&scores, 1.0, false);
            prop_assert!(
                c.abs() < 1e-9,
                "uniform scores should give centralization 0, got {c}"
            );
        }

        #[test]
        fn tmax_non_negative(n in 3u32..100) {
            let deg = centralization_degree_tmax(n, false, CentralizationMode::All, LoopMode::NoLoops);
            let bet = centralization_betweenness_tmax(n, false);
            let clo = centralization_closeness_tmax(n, CentralizationMode::All);
            let eig = centralization_eigenvector_tmax(n, CentralizationMode::All);
            prop_assert!(deg > 0.0, "degree tmax should be > 0 for n={n}");
            prop_assert!(bet > 0.0, "betweenness tmax should be > 0 for n={n}");
            prop_assert!(clo > 0.0, "closeness tmax should be > 0 for n={n}");
            prop_assert!(eig > 0.0, "eigenvector tmax should be > 0 for n={n}");
        }
    }
}