Skip to main content

diversity

Function diversity 

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

Structural diversity index for all vertices in an undirected graph.

The diversity of vertex i is the normalised Shannon entropy of the weights of its incident edges:

D(i) = H(i) / ln(k_i)

H(i) = −∑_j  p_{i,j} · ln(p_{i,j})

p_{i,j} = w_{i,j} / s_i   where  s_i = ∑_l w_{i,l}

k_i is the degree of vertex i.

  • Isolated vertices (degree 0): f64::NAN
  • Degree-1 vertices with positive weight: 0.0
  • Degree-1 vertices with zero weight: f64::NAN

§Constraints

  • Graph must be undirected.
  • Graph must have no multi-edges (simplify first if needed).
  • All weights must be non-negative and not NaN.

§Arguments

  • graph — undirected simple graph.
  • weights — non-negative edge weight vector of length graph.ecount().

§Errors

  • IgraphError::InvalidArgument if the graph is directed, has multi-edges, weights.len() != graph.ecount(), or any weight is negative or NaN.

§Examples

use rust_igraph::{Graph, diversity};

// Triangle 0-1-2 with equal weights → maximum diversity = 1.0
let mut g = Graph::with_vertices(3);
for (u, v) in [(0, 1), (0, 2), (1, 2)] {
    g.add_edge(u, v).unwrap();
}
let d = diversity(&g, &[1.0, 1.0, 1.0]).unwrap();
for val in &d {
    assert!((*val - 1.0).abs() < 1e-12);
}