Skip to main content

mean_distance_weighted

Function mean_distance_weighted 

Source
pub fn mean_distance_weighted(
    graph: &Graph,
    weights: &[f64],
    directed: bool,
    unconn: bool,
) -> IgraphResult<Option<f64>>
Expand description

Compute the weighted mean distance (average shortest path length).

Runs Dijkstra from every vertex and averages all finite pairwise distances.

  • weights: non-negative edge weights (length must equal edge count).
  • directed: if true, only follow edge directions; if false, treat edges as undirected (ignored for undirected graphs).
  • unconn: if true, average only over connected pairs; if false, return f64::INFINITY if any pair is disconnected.

Returns None if the graph has fewer than 2 vertices, or if unconn is true and no pairs are connected.

ยงExamples

use rust_igraph::{Graph, mean_distance_weighted};

let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
let weights = vec![1.0, 2.0];
let md = mean_distance_weighted(&g, &weights, false, false).unwrap();
// pairs: (0,1)=1, (1,0)=1, (0,2)=3, (2,0)=3, (1,2)=2, (2,1)=2
// mean = (1+1+3+3+2+2)/6 = 12/6 = 2.0
assert!((md.unwrap() - 2.0).abs() < 1e-10);