Skip to main content

adjacency_spectral_gap_ratio

Function adjacency_spectral_gap_ratio 

Source
pub fn adjacency_spectral_gap_ratio(graph: &Graph) -> IgraphResult<f64>
Expand description

Compute the spectral gap ratio.

(λ₁ − λ₂) / λ₁ where λ₁ and λ₂ are the two largest eigenvalues of the adjacency matrix (computed via power iteration). A large gap indicates an expander-like structure; a small gap suggests the graph is close to disconnected. Returns 0.0 for graphs with fewer than 2 vertices or if λ₁ ≈ 0.

§Examples

use rust_igraph::{Graph, adjacency_spectral_gap_ratio};

// K_4: λ₁=3, λ₂=-1 → gap = (3-(-1))/3 = 4/3 ≈ 1.333
// But we only consider the two LARGEST eigenvalues:
// K_n has eigenvalues n-1 (mult 1) and -1 (mult n-1)
// Two largest: 3 and -1 → (3 - (-1))/3 = 4/3
// Actually sorted by magnitude: the two largest eigenvalues are 3, -1
// Sorted descending: λ₁=3, λ₂=-1
// ratio = (3 - (-1))/3 = 4/3
let g = Graph::from_edges(
    &[(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)], false, Some(4)
).unwrap();
let r = adjacency_spectral_gap_ratio(&g).unwrap();
assert!(r > 1.0);