Skip to main content

assortativity_degree

Function assortativity_degree 

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

Degree assortativity coefficient of graph (undirected, unweighted). Returns None for graphs with no edges or for regular graphs (all vertices same degree — the variance denominator vanishes, matching upstream’s IGRAPH_NAN).

Counterpart of igraph_assortativity_degree(_, _, /*directed=*/false). Directed graphs return crate::IgraphError::Unsupported.

§Examples

use rust_igraph::{Graph, assortativity_degree};

// 4-cycle: all vertices have degree 2 → regular graph → None.
let mut g = Graph::with_vertices(4);
for i in 0..4u32 { g.add_edge(i, (i + 1) % 4).unwrap(); }
assert_eq!(assortativity_degree(&g).unwrap(), None);

// Path 0-1-2: deg=[1,2,1]. Two edges, both connect a deg-1 vertex
// to the deg-2 centre. The metric is well-defined here:
// num1 = (1*2 + 2*1) / 2 = 2
// num2 = ((1+2 + 2+1) / 4)^2 = (6/4)^2 = 2.25
// den1 = (1+4 + 4+1) / 4 = 2.5
// r = (2 - 2.25) / (2.5 - 2.25) = -1.0  (perfectly disassortative)
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
assert_eq!(assortativity_degree(&g).unwrap(), Some(-1.0));