Skip to main content

avg_nearest_neighbor_degree_weighted

Function avg_nearest_neighbor_degree_weighted 

Source
pub fn avg_nearest_neighbor_degree_weighted(
    graph: &Graph,
    weights: &[f64],
) -> IgraphResult<Vec<Option<f64>>>
Expand description

Weighted average nearest-neighbour degree (Barrat formula).

result[v] = Some( (1/s_v) Σ_{u ∼ v} w(v,u) · deg(u) ) where s_v = Σ_{u ∼ v} w(v,u) is v’s strength (sum of incident edge weights, with self-loops counted twice for undirected graphs to match upstream’s IGRAPH_LOOPS convention).

Returns None for vertices with strength 0 (no neighbours, or all incident weights are 0). Weights must have length graph.ecount() and contain only finite, non-negative values; otherwise this returns IgraphError. Reference: Barrat et al., PNAS 101 3747 (2004), equation (6).

§Examples

use rust_igraph::{Graph, avg_nearest_neighbor_degree_weighted};

// Triangle 0-1-2 with weights (1,2,4) on edges (0,1),(1,2),(2,0).
// Vertex 0 incident to e0=1.0 (→1) and e2=4.0 (→2). deg[1]=deg[2]=2.
// s_0 = 1+4 = 5; sum = 1*2 + 4*2 = 10; knn[0] = 10/5 = 2.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 r = avg_nearest_neighbor_degree_weighted(&g, &[1.0, 2.0, 4.0]).unwrap();
assert_eq!(r, vec![Some(2.0); 3]);