rust-igraph 0.0.1-alpha.4

Pure-Rust, high-performance graph & network analysis library — 400+ algorithms, 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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//! Eigenvector centrality (ALGO-PR-012 / PR-012b).
//!
//! Counterpart of `igraph_eigenvector_centrality()` from
//! `references/igraph/src/centrality/eigenvector.c`. The eigenvector
//! centrality of `v` is its component in the dominant eigenvector of
//! the (possibly weighted, possibly directed) adjacency matrix —
//! largest real-part eigenvalue, real and non-negative by
//! Perron-Frobenius for strongly-connected non-negative weights.
//!
//! Implemented via shifted power iteration on `(M + σI)`:
//! `x_new[v] = σ·x_old[v] + Σ_{u ~ v} m_{u,v}·x_old[u]`, then normalize
//! so `max|x| == 1`. The shift breaks the `±λ` bipartite symmetry of
//! symmetric matrices and turns the "largest-real-part" eigenvalue of
//! `M` into the unique "largest-magnitude" eigenvalue of `M + σI`, so
//! plain power iteration converges to it for non-negative `M`. Iterate
//! until L1 change < `1e-12` or 5000 iterations.
//!
//! ## Variants
//!
//! - [`eigenvector_centrality`] — undirected, unweighted; backward
//!   compatible (returns `Vec<f64>` for the vector only).
//! - [`eigenvector_centrality_weighted`] — undirected, weighted.
//! - [`eigenvector_centrality_directed`] — directed, unweighted.
//! - [`eigenvector_centrality_directed_weighted`] — directed, weighted.
//! - [`eigenvector_centrality_full`] — single-entry master function
//!   matching upstream's signature `(g, mode, weights?) -> {vec, λ}`.
//!
//! ## Directed-mode convention
//!
//! For mode = [`EigenvectorMode::Out`] (the standard convention; matches
//! upstream's `IGRAPH_OUT`), the centrality of `v` is proportional to
//! the sum of centralities of vertices pointing into `v`, i.e. we walk
//! `v`'s incoming edges. For [`EigenvectorMode::In`] we walk outgoing
//! edges. [`EigenvectorMode::All`] treats the graph as undirected and
//! falls through to the symmetric path.
//!
//! ## DAG short-circuit
//!
//! When the graph is a DAG and weights are non-negative, the adjacency
//! matrix is nilpotent and every eigenvalue is zero. ARPACK behaviour
//! is then non-deterministic; matching upstream, we return `eigenvalue
//! = 0` and a vector of `1`s on sinks (for [`EigenvectorMode::Out`]) /
//! sources (for [`EigenvectorMode::In`]) and `0`s elsewhere. See
//! <https://github.com/igraph/igraph/issues/2679> for upstream
//! rationale.

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

// Tighter than the L1-convergence default we use elsewhere because
// rustdoc/conformance compare bit-precise vs python-igraph's ARPACK.
const DEFAULT_EPS: f64 = 1e-14;
const DEFAULT_MAX_ITER: usize = 5000;

// Tolerance for the shifted-power-iter (slightly looser since we run on
// `(M + σI)` and need to recover λ ≈ ρ - σ which loses ~log10(σ/ρ) bits).
const SHIFTED_EPS: f64 = 1e-12;
const SHIFTED_MAX_ITER: usize = 5000;

/// How to consider edge directions for [`eigenvector_centrality_full`]
/// and friends. Mirrors upstream's `IGRAPH_OUT` / `IGRAPH_IN` /
/// `IGRAPH_ALL`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EigenvectorMode {
    /// Standard convention: centrality of `v` is proportional to the
    /// sum of centralities of vertices pointing to `v` (we walk
    /// incoming edges of `v`). Equivalent to ARPACK's "left
    /// eigenvector of the adjacency matrix".
    Out,
    /// Reverse convention: centrality of `v` is proportional to the
    /// sum of centralities of vertices `v` points to (we walk outgoing
    /// edges of `v`). Equivalent to ARPACK's "right eigenvector".
    In,
    /// Treat the graph as undirected and use the symmetric eigenvector
    /// path. Mandatory for undirected input.
    All,
}

/// Output of [`eigenvector_centrality_full`]: the normalised
/// centrality vector and the dominant eigenvalue of the (possibly
/// shifted-back) adjacency matrix.
#[derive(Debug, Clone, PartialEq)]
pub struct EigenvectorScores {
    /// Centrality per vertex, length `vcount()`. Max-absolute element
    /// is exactly `1.0` (matching python-igraph), unless all entries
    /// are zero (empty / DAG / all-zero-weight sentinels).
    pub vector: Vec<f64>,
    /// Largest-real-part eigenvalue of the adjacency / weighted
    /// adjacency matrix. `0.0` for the empty-edge, all-zero-weight,
    /// and DAG sentinel cases.
    pub eigenvalue: f64,
}

/// Backward-compatible undirected, unweighted entry point.
///
/// Returns just the centrality vector (max-1 normalised). Directed
/// graphs return [`IgraphError::Unsupported`] — use
/// [`eigenvector_centrality_directed`] or [`eigenvector_centrality_full`]
/// for the directed paths.
///
/// Counterpart of `igraph_eigenvector_centrality(g, &v, NULL, IGRAPH_ALL,
/// NULL, NULL)`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, eigenvector_centrality};
///
/// // Triangle: every vertex has identical centrality 1.0.
/// let mut g = Graph::with_vertices(3);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(2, 0).unwrap();
/// let ec = eigenvector_centrality(&g).unwrap();
/// assert!((ec[0] - 1.0).abs() < 1e-9);
/// assert!((ec[1] - 1.0).abs() < 1e-9);
/// assert!((ec[2] - 1.0).abs() < 1e-9);
/// ```
pub fn eigenvector_centrality(graph: &Graph) -> IgraphResult<Vec<f64>> {
    if graph.is_directed() {
        return Err(IgraphError::Unsupported(
            "directed eigenvector_centrality — use eigenvector_centrality_directed or _full",
        ));
    }
    undirected_unweighted(graph).map(|s| s.vector)
}

/// Undirected weighted eigenvector centrality.
///
/// `weights.len()` must equal `graph.ecount()`. Returns the
/// max-1-normalised vector and the dominant eigenvalue of the weighted
/// symmetric adjacency `W[i,j] = Σ_{e: i~j} w_e`. Non-negative weights
/// are assumed (negative weights are accepted but a Perron-Frobenius
/// guarantee no longer applies and entries may be negative; matches
/// upstream).
///
/// All-zero weights and edge-less graphs both return a vector of `1`s
/// and `eigenvalue = 0`, matching upstream's sentinel.
///
/// Counterpart of `igraph_eigenvector_centrality(g, &v, &λ, IGRAPH_ALL,
/// weights, NULL)`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, eigenvector_centrality_weighted};
///
/// // Weighted star K_{1,4} with unit weights → leaf:centre = 1:2.
/// let mut g = Graph::with_vertices(5);
/// for v in 1..5 {
///     g.add_edge(0, v).unwrap();
/// }
/// let s = eigenvector_centrality_weighted(&g, &vec![1.0; g.ecount()]).unwrap();
/// assert!((s.vector[0] - 1.0).abs() < 1e-9);
/// assert!((s.vector[1] - 0.5).abs() < 1e-9);
/// assert!((s.eigenvalue - 2.0).abs() < 1e-6);
/// ```
pub fn eigenvector_centrality_weighted(
    graph: &Graph,
    weights: &[f64],
) -> IgraphResult<EigenvectorScores> {
    if graph.is_directed() {
        return Err(IgraphError::Unsupported(
            "directed eigenvector_centrality_weighted — use eigenvector_centrality_directed_weighted",
        ));
    }
    validate_weights(graph, weights)?;
    undirected_weighted(graph, Some(weights))
}

/// Directed unweighted eigenvector centrality.
///
/// `mode` controls which side of the adjacency matrix's left/right
/// eigenvector is returned. Undirected input is rejected — use
/// [`eigenvector_centrality`] instead.
///
/// Counterpart of `igraph_eigenvector_centrality(g, &v, &λ, mode, NULL,
/// NULL)` on directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, EigenvectorMode, eigenvector_centrality_directed};
///
/// // Directed 4-cycle 0→1→2→3→0 plus chord 1→3.
/// let mut g = Graph::new(4, true).unwrap();
/// g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 3), (3, 0), (1, 3)]).unwrap();
/// let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
/// // Eigenvalue ≈ 1.22074 (real root of x³ = 1 + x²).
/// assert!((s.eigenvalue - 1.22074).abs() < 1e-3);
/// ```
pub fn eigenvector_centrality_directed(
    graph: &Graph,
    mode: EigenvectorMode,
) -> IgraphResult<EigenvectorScores> {
    eigenvector_centrality_full(graph, mode, None)
}

/// Directed weighted eigenvector centrality.
///
/// Like [`eigenvector_centrality_directed`] but with per-edge weights.
/// Weights of parallel edges are summed (`W[i,j] = Σ_{e: i→j} w_e`).
///
/// Counterpart of `igraph_eigenvector_centrality(g, &v, &λ, mode,
/// weights, NULL)` on directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, eigenvector_centrality_directed_weighted, EigenvectorMode};
///
/// let mut g = Graph::new(3, true).unwrap();
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(2, 0).unwrap();
/// let r = eigenvector_centrality_directed_weighted(&g, EigenvectorMode::All, &[1.0, 1.0, 1.0]).unwrap();
/// assert_eq!(r.vector.len(), 3);
/// ```
pub fn eigenvector_centrality_directed_weighted(
    graph: &Graph,
    mode: EigenvectorMode,
    weights: &[f64],
) -> IgraphResult<EigenvectorScores> {
    eigenvector_centrality_full(graph, mode, Some(weights))
}

/// Master entry point matching upstream's signature.
///
/// Dispatches to the undirected / directed and weighted / unweighted
/// branches based on `graph.is_directed()`, `mode`, and `weights`.
/// Validates `weights.len() == graph.ecount()` when supplied.
///
/// Counterpart of `igraph_eigenvector_centrality(g, &v, &λ, mode,
/// weights, NULL)` — the C-level "do everything" signature.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, eigenvector_centrality_full, EigenvectorMode};
///
/// let mut g = Graph::with_vertices(3);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(2, 0).unwrap();
/// let r = eigenvector_centrality_full(&g, EigenvectorMode::All, None).unwrap();
/// assert_eq!(r.vector.len(), 3);
/// ```
pub fn eigenvector_centrality_full(
    graph: &Graph,
    mode: EigenvectorMode,
    weights: Option<&[f64]>,
) -> IgraphResult<EigenvectorScores> {
    if let Some(w) = weights {
        validate_weights(graph, w)?;
    }
    // Undirected input forces ALL mode (matches upstream).
    let effective_mode = if graph.is_directed() {
        mode
    } else {
        EigenvectorMode::All
    };
    match effective_mode {
        EigenvectorMode::All => match weights {
            None => undirected_unweighted(graph),
            Some(w) => undirected_weighted(graph, Some(w)),
        },
        EigenvectorMode::Out | EigenvectorMode::In => directed_path(graph, effective_mode, weights),
    }
}

// ---------- internal: shared validation ----------

fn validate_weights(graph: &Graph, weights: &[f64]) -> IgraphResult<()> {
    let m = graph.ecount();
    if weights.len() != m {
        return Err(IgraphError::InvalidArgument(format!(
            "weights vector length ({}) not equal to number of edges ({})",
            weights.len(),
            m
        )));
    }
    Ok(())
}

// ---------- internal: undirected unweighted ----------

fn undirected_unweighted(graph: &Graph) -> IgraphResult<EigenvectorScores> {
    let n = graph.vcount();
    let n_us = n as usize;
    if n == 0 {
        return Ok(EigenvectorScores {
            vector: Vec::new(),
            eigenvalue: 0.0,
        });
    }
    if graph.ecount() == 0 {
        return Ok(EigenvectorScores {
            vector: vec![1.0; n_us],
            eigenvalue: 0.0,
        });
    }
    if n == 1 {
        return Ok(EigenvectorScores {
            vector: vec![1.0],
            eigenvalue: 0.0,
        });
    }

    let mut adj: Vec<Vec<u32>> = Vec::with_capacity(n_us);
    for v in 0..n {
        adj.push(graph.neighbors(v)?);
    }

    let mut x = vec![1.0_f64; n_us];
    let mut x_new = vec![0.0_f64; n_us];

    for _ in 0..DEFAULT_MAX_ITER {
        // x_new = (A + I) · x — shift breaks ±λ bipartite symmetry.
        for v in 0..n_us {
            let mut sum = x[v];
            for &u in &adj[v] {
                sum += x[u as usize];
            }
            x_new[v] = sum;
        }
        let max = x_new.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()));
        if max > 0.0 {
            for slot in &mut x_new {
                *slot /= max;
            }
        }
        let mut diff = 0.0_f64;
        for v in 0..n_us {
            diff += (x_new[v] - x[v]).abs();
        }
        std::mem::swap(&mut x, &mut x_new);
        if diff < DEFAULT_EPS {
            break;
        }
    }

    // Eigenvalue from Rayleigh quotient: λ = (xᵀ·A·x) / (xᵀ·x).
    let mut ax = vec![0.0_f64; n_us];
    for v in 0..n_us {
        let mut sum = 0.0_f64;
        for &u in &adj[v] {
            sum += x[u as usize];
        }
        ax[v] = sum;
    }
    let num: f64 = x.iter().zip(ax.iter()).map(|(a, b)| a * b).sum();
    let den: f64 = x.iter().map(|a| a * a).sum();
    let eigenvalue = if den > 0.0 { num / den } else { 0.0 };

    // Sign cleanup: drop tiny negatives that come from numerical drift
    // (only valid because A here is non-negative).
    for slot in &mut x {
        if *slot < 0.0 {
            *slot = 0.0;
        }
    }

    Ok(EigenvectorScores {
        vector: x,
        eigenvalue,
    })
}

// ---------- internal: undirected weighted ----------

#[allow(
    clippy::too_many_lines,
    clippy::many_single_char_names,
    clippy::cast_possible_truncation
)]
fn undirected_weighted(graph: &Graph, weights: Option<&[f64]>) -> IgraphResult<EigenvectorScores> {
    let n = graph.vcount();
    let n_us = n as usize;
    let m = graph.ecount();
    if n == 0 {
        return Ok(EigenvectorScores {
            vector: Vec::new(),
            eigenvalue: 0.0,
        });
    }
    if m == 0 {
        return Ok(EigenvectorScores {
            vector: vec![1.0; n_us],
            eigenvalue: 0.0,
        });
    }
    if let Some(w) = weights {
        if w.iter().all(|&x| x == 0.0) {
            return Ok(EigenvectorScores {
                vector: vec![1.0; n_us],
                eigenvalue: 0.0,
            });
        }
    }
    if n == 1 {
        return Ok(EigenvectorScores {
            vector: vec![1.0],
            eigenvalue: 0.0,
        });
    }

    let negative_weights = weights.is_some_and(|w| w.iter().any(|&x| x < 0.0));

    // Pre-cache incident edges per vertex (undirected → both endpoints
    // appear in their respective incidence lists).
    let mut inc: Vec<Vec<u32>> = Vec::with_capacity(n_us);
    for v in 0..n {
        inc.push(graph.incident(v)?);
    }

    // Seed: weighted strength fallback to degree if zero strength (matches C).
    let mut x = vec![0.0_f64; n_us];
    for v in 0..n {
        let v_us = v as usize;
        let mut strength = 0.0_f64;
        for &e in &inc[v_us] {
            let w = weights.map_or(1.0, |ws| ws[e as usize]);
            strength += w;
        }
        if strength != 0.0 {
            x[v_us] = strength.abs();
        } else if !negative_weights {
            x[v_us] = 0.0;
        } else {
            // Negative weights can produce zero strength even on
            // non-zero-degree vertices; seed those with 1.
            x[v_us] = if inc[v_us].is_empty() { 0.0 } else { 1.0 };
        }
    }
    // Make sure at least one entry is non-zero so power iter has signal.
    if x.iter().all(|&y| y == 0.0) {
        x.fill(1.0);
    }
    // Normalise seed to max=1 to keep numerics tame.
    let max0 = x.iter().fold(0.0_f64, |acc, &y| acc.max(y.abs()));
    if max0 > 0.0 {
        for slot in &mut x {
            *slot /= max0;
        }
    }

    // Shifted power iter on (W + σI), σ = 1 suffices since W is bounded
    // and we just need to break ±λ symmetry. For negative weights, this
    // doesn't guarantee convergence to largest-real, but matches the
    // upstream "best effort" contract.
    let shift = 1.0_f64;
    let mut x_new = vec![0.0_f64; n_us];

    for _ in 0..SHIFTED_MAX_ITER {
        // x_new = σ·x + W·x  (W·x walks incident edges with weight w_e)
        for v in 0..n_us {
            let mut sum = shift * x[v];
            for &e in &inc[v] {
                let other = graph.edge_other(e, v as u32)?;
                let w = weights.map_or(1.0, |ws| ws[e as usize]);
                sum += w * x[other as usize];
            }
            x_new[v] = sum;
        }
        // For negative-weight case, track signed component with greatest
        // magnitude so we converge to largest real eigenvalue, not just
        // largest magnitude. For positive weights this is equivalent to
        // max abs.
        let pivot = if negative_weights {
            let mut best = 0.0_f64;
            for &v in &x_new {
                if v.abs() > best.abs() {
                    best = v;
                }
            }
            best
        } else {
            x_new.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()))
        };
        if pivot != 0.0 {
            for slot in &mut x_new {
                *slot /= pivot;
            }
        }
        let mut diff = 0.0_f64;
        for v in 0..n_us {
            diff += (x_new[v] - x[v]).abs();
        }
        std::mem::swap(&mut x, &mut x_new);
        if diff < SHIFTED_EPS {
            break;
        }
    }

    // Rayleigh quotient on the original W (no shift): λ = xᵀ·W·x / xᵀ·x
    let mut wx = vec![0.0_f64; n_us];
    for v in 0..n_us {
        let mut sum = 0.0_f64;
        for &e in &inc[v] {
            let other = graph.edge_other(e, v as u32)?;
            let w = weights.map_or(1.0, |ws| ws[e as usize]);
            sum += w * x[other as usize];
        }
        wx[v] = sum;
    }
    let num: f64 = x.iter().zip(wx.iter()).map(|(a, b)| a * b).sum();
    let den: f64 = x.iter().map(|a| a * a).sum();
    let eigenvalue = if den > 0.0 { num / den } else { 0.0 };

    // Renormalise to max-abs = 1 (Rayleigh-quotient pass may have
    // produced a vector whose dominant component is signed).
    let max = x.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()));
    if max > 0.0 {
        for slot in &mut x {
            *slot /= max;
        }
    }
    if !negative_weights {
        for slot in &mut x {
            if *slot < 0.0 {
                *slot = 0.0;
            }
        }
    }

    Ok(EigenvectorScores {
        vector: x,
        eigenvalue,
    })
}

// ---------- internal: directed (with optional weights) ----------

#[allow(
    clippy::too_many_lines,
    clippy::many_single_char_names,
    clippy::cast_possible_truncation,
    clippy::needless_range_loop
)]
fn directed_path(
    graph: &Graph,
    mode: EigenvectorMode,
    weights: Option<&[f64]>,
) -> IgraphResult<EigenvectorScores> {
    let n = graph.vcount();
    let n_us = n as usize;
    let m = graph.ecount();
    if n == 0 {
        return Ok(EigenvectorScores {
            vector: Vec::new(),
            eigenvalue: 0.0,
        });
    }
    if m == 0 {
        return Ok(EigenvectorScores {
            vector: vec![1.0; n_us],
            eigenvalue: 0.0,
        });
    }
    if let Some(w) = weights {
        if w.iter().all(|&x| x == 0.0) {
            return Ok(EigenvectorScores {
                vector: vec![1.0; n_us],
                eigenvalue: 0.0,
            });
        }
    }

    let negative_weights = weights.is_some_and(|w| w.iter().any(|&x| x < 0.0));

    // DAG short-circuit (only valid for non-negative weights — see
    // upstream comment block on lines 350-369). Returns 1s on sinks
    // (Out) / sources (In) and 0s elsewhere.
    if !negative_weights && crate::algorithms::properties::is_dag::is_dag(graph) {
        return dag_sentinel(graph, mode, weights);
    }

    // For Out: centrality of v ∝ Σ_{u: u→v} c_u, so we walk IN-edges of v.
    // For In:  centrality of v ∝ Σ_{u: v→u} c_u, so we walk OUT-edges of v.
    // Pre-build per-vertex (edge_id, neighbour) list.
    let mut walk: Vec<Vec<(u32, u32)>> = vec![Vec::new(); n_us];
    let m_u32 = u32::try_from(m)
        .map_err(|_| IgraphError::InvalidArgument("edge count exceeds u32::MAX".into()))?;
    match mode {
        EigenvectorMode::Out => {
            for e in 0..m_u32 {
                let (u, v) = graph.edge(e)?;
                walk[v as usize].push((e, u));
            }
        }
        EigenvectorMode::In => {
            for e in 0..m_u32 {
                let (u, v) = graph.edge(e)?;
                walk[u as usize].push((e, v));
            }
        }
        EigenvectorMode::All => unreachable!("All is dispatched to undirected_*"),
    }

    // Seed: in-strength (Out) / out-strength (In). Fallback to in-degree
    // when negative weights produce zero strength on a non-empty list.
    let mut x = vec![0.0_f64; n_us];
    for v in 0..n_us {
        let mut strength = 0.0_f64;
        for &(e, _) in &walk[v] {
            let w = weights.map_or(1.0, |ws| ws[e as usize]);
            strength += w;
        }
        if strength != 0.0 {
            x[v] = strength.abs();
        } else if !negative_weights {
            x[v] = 0.0;
        } else {
            x[v] = if walk[v].is_empty() { 0.0 } else { 1.0 };
        }
    }
    if x.iter().all(|&y| y == 0.0) {
        x.fill(1.0);
    }
    let max0 = x.iter().fold(0.0_f64, |acc, &y| acc.max(y.abs()));
    if max0 > 0.0 {
        for slot in &mut x {
            *slot /= max0;
        }
    }

    // Shift: σ = max absolute row sum of M ensures (M + σI) has its
    // largest-magnitude eigenvalue equal to largest-real-part of M + σ,
    // because for non-negative M the largest-real eigenvalue is real
    // and non-negative (Perron-Frobenius). We pad by 1 to also break
    // exact tie configurations.
    let mut row_norm: f64 = 0.0;
    for v in 0..n_us {
        let mut s = 0.0_f64;
        for &(e, _) in &walk[v] {
            let w = weights.map_or(1.0, |ws| ws[e as usize]);
            s += w.abs();
        }
        if s > row_norm {
            row_norm = s;
        }
    }
    let shift = row_norm.max(1.0) + 1.0;

    let mut x_new = vec![0.0_f64; n_us];
    for _ in 0..SHIFTED_MAX_ITER {
        for v in 0..n_us {
            let mut sum = shift * x[v];
            for &(e, nei) in &walk[v] {
                let w = weights.map_or(1.0, |ws| ws[e as usize]);
                sum += w * x[nei as usize];
            }
            x_new[v] = sum;
        }
        let pivot = if negative_weights {
            let mut best = 0.0_f64;
            for &v in &x_new {
                if v.abs() > best.abs() {
                    best = v;
                }
            }
            best
        } else {
            x_new.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()))
        };
        if pivot != 0.0 {
            for slot in &mut x_new {
                *slot /= pivot;
            }
        }
        let mut diff = 0.0_f64;
        for v in 0..n_us {
            diff += (x_new[v] - x[v]).abs();
        }
        std::mem::swap(&mut x, &mut x_new);
        if diff < SHIFTED_EPS {
            break;
        }
    }

    // Rayleigh quotient on unshifted M.
    let mut mx = vec![0.0_f64; n_us];
    for v in 0..n_us {
        let mut sum = 0.0_f64;
        for &(e, nei) in &walk[v] {
            let w = weights.map_or(1.0, |ws| ws[e as usize]);
            sum += w * x[nei as usize];
        }
        mx[v] = sum;
    }
    let num: f64 = x.iter().zip(mx.iter()).map(|(a, b)| a * b).sum();
    let den: f64 = x.iter().map(|a| a * a).sum();
    let mut eigenvalue = if den > 0.0 { num / den } else { 0.0 };

    // Pathological case: dominant eigenvalue is zero (i.e. M is
    // nilpotent yet we slipped past the DAG fast-path due to numerical
    // noise). Fall back to upstream's "all zeros" output.
    if !negative_weights && eigenvalue <= SHIFTED_EPS {
        eigenvalue = 0.0;
        x.fill(0.0);
    } else {
        let max = x.iter().fold(0.0_f64, |acc, &v| acc.max(v.abs()));
        if max > 0.0 {
            for slot in &mut x {
                *slot /= max;
            }
        }
        if !negative_weights {
            for slot in &mut x {
                if *slot < 0.0 {
                    *slot = 0.0;
                }
            }
        }
    }

    Ok(EigenvectorScores {
        vector: x,
        eigenvalue,
    })
}

// DAG sentinel: 1s on sinks (Out mode) / sources (In mode), 0s elsewhere.
#[allow(clippy::many_single_char_names)]
fn dag_sentinel(
    graph: &Graph,
    mode: EigenvectorMode,
    weights: Option<&[f64]>,
) -> IgraphResult<EigenvectorScores> {
    let n = graph.vcount();
    let n_us = n as usize;
    let m = graph.ecount();
    // Compute the strength in the "outgoing" direction (Out → vertex
    // with no outgoing edges; In → vertex with no incoming edges).
    // Upstream's logic: sinks for Out mode = vertices where out-strength
    // is zero.
    let mut strength = vec![0.0_f64; n_us];
    let m_u32 = u32::try_from(m)
        .map_err(|_| IgraphError::InvalidArgument("edge count exceeds u32::MAX".into()))?;
    for e in 0..m_u32 {
        let (u, v) = graph.edge(e)?;
        let w = weights.map_or(1.0, |ws| ws[e as usize]);
        match mode {
            EigenvectorMode::Out => strength[u as usize] += w,
            EigenvectorMode::In => strength[v as usize] += w,
            EigenvectorMode::All => unreachable!(),
        }
    }
    let vector: Vec<f64> = strength
        .iter()
        .map(|&s| if s == 0.0 { 1.0 } else { 0.0 })
        .collect();
    Ok(EigenvectorScores {
        vector,
        eigenvalue: 0.0,
    })
}

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

    fn close(actual: &[f64], expected: &[f64], tol: f64) {
        assert_eq!(actual.len(), expected.len(), "length mismatch");
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!((a - e).abs() < tol, "vertex {i}: actual={a} expected={e}");
        }
    }

    #[test]
    fn empty_graph_yields_empty() {
        let g = Graph::with_vertices(0);
        assert!(eigenvector_centrality(&g).unwrap().is_empty());
    }

    #[test]
    fn singleton_yields_one() {
        let g = Graph::with_vertices(1);
        assert_eq!(eigenvector_centrality(&g).unwrap(), vec![1.0]);
    }

    #[test]
    fn isolated_vertices_yield_uniform_one() {
        let g = Graph::with_vertices(3);
        let ec = eigenvector_centrality(&g).unwrap();
        close(&ec, &[1.0, 1.0, 1.0], 1e-9);
    }

    #[test]
    fn triangle_uniform_one() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        g.add_edge(2, 0).unwrap();
        let ec = eigenvector_centrality(&g).unwrap();
        close(&ec, &[1.0, 1.0, 1.0], 1e-9);
    }

    #[test]
    fn star_4_centre_one_leaves_inverse_sqrt_three() {
        let mut g = Graph::with_vertices(4);
        for v in 1..4 {
            g.add_edge(0, v).unwrap();
        }
        let ec = eigenvector_centrality(&g).unwrap();
        let inv_sqrt3 = 1.0 / 3f64.sqrt();
        close(&ec, &[1.0, inv_sqrt3, inv_sqrt3, inv_sqrt3], 1e-9);
    }

    #[test]
    fn k4_uniform_one() {
        let mut g = Graph::with_vertices(4);
        for u in 0..4u32 {
            for v in (u + 1)..4 {
                g.add_edge(u, v).unwrap();
            }
        }
        let ec = eigenvector_centrality(&g).unwrap();
        close(&ec, &[1.0, 1.0, 1.0, 1.0], 1e-9);
    }

    #[test]
    fn directed_graph_returns_unsupported() {
        let mut g = Graph::new(3, true).unwrap();
        g.add_edge(0, 1).unwrap();
        assert!(eigenvector_centrality(&g).is_err());
    }

    // ---- weighted undirected ----

    #[test]
    fn weighted_star_unit_matches_unweighted() {
        let mut g = Graph::with_vertices(5);
        for v in 1..5 {
            g.add_edge(0, v).unwrap();
        }
        let s = eigenvector_centrality_weighted(&g, &vec![1.0; g.ecount()]).unwrap();
        close(&s.vector, &[1.0, 0.5, 0.5, 0.5, 0.5], 1e-9);
        assert!((s.eigenvalue - 2.0).abs() < 1e-6);
    }

    #[test]
    fn weighted_k5_unit_eigenvalue_four() {
        let mut g = Graph::with_vertices(5);
        for u in 0..5u32 {
            for v in (u + 1)..5 {
                g.add_edge(u, v).unwrap();
            }
        }
        let s = eigenvector_centrality_weighted(&g, &vec![1.0; g.ecount()]).unwrap();
        close(&s.vector, &[1.0, 1.0, 1.0, 1.0, 1.0], 1e-9);
        assert!((s.eigenvalue - 4.0).abs() < 1e-6);
    }

    #[test]
    fn weighted_empty_returns_ones() {
        let g = Graph::with_vertices(4);
        let s = eigenvector_centrality_weighted(&g, &[]).unwrap();
        close(&s.vector, &[1.0; 4], 1e-15);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    #[test]
    fn weighted_all_zero_returns_ones() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        let s = eigenvector_centrality_weighted(&g, &[0.0, 0.0]).unwrap();
        close(&s.vector, &[1.0; 3], 1e-15);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    #[test]
    fn weighted_length_mismatch_errors() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        assert!(eigenvector_centrality_weighted(&g, &[1.0]).is_err());
    }

    // ---- directed ----

    #[test]
    fn directed_full_eigenvalue_n_minus_one() {
        // K_5 directed (no loops) has dominant eigenvalue n-1=4.
        let mut g = Graph::new(5, true).unwrap();
        for u in 0..5u32 {
            for v in 0..5u32 {
                if u != v {
                    g.add_edge(u, v).unwrap();
                }
            }
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        close(&s.vector, &[1.0, 1.0, 1.0, 1.0, 1.0], 1e-9);
        assert!((s.eigenvalue - 4.0).abs() < 1e-6);
    }

    #[test]
    fn directed_dag_out_star_returns_leaves_one() {
        // out-star: 0 → 1, 2, 3, 4. Sinks (Out mode) are 1..4.
        let mut g = Graph::new(5, true).unwrap();
        for v in 1..5u32 {
            g.add_edge(0, v).unwrap();
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        close(&s.vector, &[0.0, 1.0, 1.0, 1.0, 1.0], 1e-12);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    #[test]
    fn directed_dag_in_star_in_mode_marks_leaves() {
        // In-star: 1, 2, 3, 4 → 0. With In mode (centrality ∝ vertices
        // we point TO), upstream's DAG sentinel marks sources of the
        // adjacency matrix — vertices with no incoming edges, i.e. the
        // leaves 1..4. Vertex 0 has 4 incoming edges, no outgoing, so
        // it gets 0. Symmetric with the out-star + Out-mode case.
        let mut g = Graph::new(5, true).unwrap();
        for v in 1..5u32 {
            g.add_edge(v, 0).unwrap();
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::In).unwrap();
        close(&s.vector, &[0.0, 1.0, 1.0, 1.0, 1.0], 1e-12);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    #[test]
    fn directed_dag_in_star_out_mode_marks_centre() {
        // In-star: 1, 2, 3, 4 → 0. With default Out mode, upstream
        // marks SINKS (no outgoing edges) = vertex 0 only. Matches the
        // golden case at references/igraph/tests/unit/igraph_eigenvector_centrality.out.
        let mut g = Graph::new(5, true).unwrap();
        for v in 1..5u32 {
            g.add_edge(v, 0).unwrap();
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        close(&s.vector, &[1.0, 0.0, 0.0, 0.0, 0.0], 1e-12);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    #[test]
    fn directed_small_cycle_chord_eigenvalue_matches_arpack() {
        // 0→1, 1→2, 2→3, 3→0, 1→3 — from upstream's golden test.
        // Expected eigenvalue ≈ 1.22074 (real root of x³ = 1 + x²).
        let mut g = Graph::new(4, true).unwrap();
        g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 3), (3, 0), (1, 3)])
            .unwrap();
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        assert!(
            (s.eigenvalue - 1.220_744).abs() < 1e-3,
            "expected ≈ 1.22074, got {}",
            s.eigenvalue
        );
        // Vertex 3 should be most central (max-1 by construction).
        let max_idx = s
            .vector
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
            .unwrap()
            .0;
        assert_eq!(max_idx, 3);
    }

    #[test]
    fn directed_empty_directed_returns_ones() {
        let g = Graph::new(5, true).unwrap();
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        close(&s.vector, &[1.0; 5], 1e-12);
        assert!(s.eigenvalue.abs() < f64::EPSILON);
    }

    // ---- master function dispatch ----

    #[test]
    fn full_undirected_with_all_mode_works() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        g.add_edge(2, 0).unwrap();
        let s = eigenvector_centrality_full(&g, EigenvectorMode::All, None).unwrap();
        close(&s.vector, &[1.0, 1.0, 1.0], 1e-9);
    }

    #[test]
    fn full_undirected_forces_all_mode() {
        // Passing Out on undirected graph still hits the symmetric path.
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        g.add_edge(2, 0).unwrap();
        let a = eigenvector_centrality_full(&g, EigenvectorMode::Out, None).unwrap();
        let b = eigenvector_centrality_full(&g, EigenvectorMode::All, None).unwrap();
        close(&a.vector, &b.vector, 1e-12);
        assert!((a.eigenvalue - b.eigenvalue).abs() < 1e-12);
    }

    // ---- stress / hard spectrum cases ----

    #[test]
    fn directed_cycle_50_converges_to_unit_eigenvalue() {
        // 0→1→2→...→49→0. Adjacency eigenvalues are 50th roots of
        // unity; largest-real = 1 (only λ=1 itself is real). With our
        // shift σ = max_row_norm+1 = 2, shifted eigenvalues are
        // ω_k + 2, of which |ω_0 + 2| = 3 is the strict maximum.
        // Power iter should converge in O(1) iterations.
        let n: u32 = 50;
        let mut g = Graph::new(n, true).unwrap();
        for i in 0..n {
            g.add_edge(i, (i + 1) % n).unwrap();
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        assert!(
            (s.eigenvalue - 1.0).abs() < 1e-9,
            "expected λ ≈ 1.0, got {}",
            s.eigenvalue
        );
        // Circulant graph: vector should be constant.
        let mx = s.vector.iter().copied().fold(0.0_f64, f64::max);
        let mn = s.vector.iter().copied().fold(f64::INFINITY, f64::min);
        assert!(mx - mn < 1e-9, "vec spread {} too large", mx - mn);
    }

    #[test]
    fn directed_k10_eigenvalue_n_minus_one() {
        // Complete digraph K_10 (no loops): eigenvalue = n - 1 = 9,
        // uniform vector.
        let n: u32 = 10;
        let mut g = Graph::new(n, true).unwrap();
        for u in 0..n {
            for v in 0..n {
                if u != v {
                    g.add_edge(u, v).unwrap();
                }
            }
        }
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        assert!(
            (s.eigenvalue - 9.0).abs() < 1e-9,
            "expected λ = 9, got {}",
            s.eigenvalue
        );
        close(&s.vector, &[1.0; 10], 1e-9);
    }

    #[test]
    fn directed_cycle_chord_eigenvector_components_match_arpack() {
        // Upstream golden: cycle 4 + chord 1→3, mode = Out.
        // Vector (max-1) = [0.819173, 0.671044, 0.549700, 1.000000]
        // λ ≈ 1.220744.
        let mut g = Graph::new(4, true).unwrap();
        g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 3), (3, 0), (1, 3)])
            .unwrap();
        let s = eigenvector_centrality_directed(&g, EigenvectorMode::Out).unwrap();
        assert!((s.eigenvalue - 1.220_744).abs() < 1e-4);
        close(
            &s.vector,
            &[0.819_173, 0.671_044, 0.549_700, 1.000_000],
            1e-3,
        );
    }
}