Skip to main content

closeness

Function closeness 

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

Per-vertex closeness centrality (Vec<Option<f64>>).

result[v] is Some(reachable_count / sum_of_distances) if vertex v has at least one reachable neighbour; None for isolated vertices (matches upstream’s IGRAPH_NAN).

Edge directions follow distances: out-edges for directed graphs (IGRAPH_OUT), full undirected reachability otherwise.

§Examples

use rust_igraph::{Graph, closeness};

// Star with centre 0 and 3 leaves: centre has the highest closeness.
let mut g = Graph::with_vertices(4);
for v in 1..4 { g.add_edge(0, v).unwrap(); }
let c = closeness(&g).unwrap();
// Centre: 3 reachable, sum_dist = 3 → 3/3 = 1.0.
assert_eq!(c[0], Some(1.0));
// Leaves: 3 reachable (centre + 2 other leaves), sum = 1 + 2 + 2 = 5 → 3/5 = 0.6.
for leaf in 1..4 {
    assert_eq!(c[leaf], Some(0.6));
}