Skip to main content

closeness_cutoff

Function closeness_cutoff 

Source
pub fn closeness_cutoff(
    graph: &Graph,
    cutoff: u32,
    normalized: bool,
) -> IgraphResult<ClosenessCutoffResult>
Expand description

Range-limited closeness centrality.

Computes closeness centrality for all vertices, but only considering paths of length at most cutoff. Shorter cutoffs make the computation faster (early BFS termination) while capturing local structure.

The closeness of vertex v is reachable_count / sum_of_distances when normalized = true, or 1.0 / sum_of_distances when normalized = false. Vertices with no reachable neighbours within the cutoff get None.

§Parameters

  • graph — the input graph.
  • cutoff — maximum path length to consider. Paths longer than this are ignored.
  • normalized — if true, returns reach / sum_dist; if false, returns 1.0 / sum_dist.

§Examples

use rust_igraph::{Graph, closeness_cutoff};

// Path: 0—1—2—3—4
let mut g = Graph::with_vertices(5);
for i in 0..4 { g.add_edge(i, i + 1).unwrap(); }
let r = closeness_cutoff(&g, 2, true).unwrap();
// Vertex 0: reaches 1 (dist 1) and 2 (dist 2) within cutoff 2.
// closeness = 2 / (1+2) = 2/3
let expected = 2.0 / 3.0;
assert!((r.scores[0].unwrap() - expected).abs() < 1e-12);
assert_eq!(r.reachable_count[0], 2);