pub fn closeness_centrality<G>(graph: G, wf_improved: bool) -> Vec<Option<f64>>where
    G: NodeIndexable + IntoNodeIdentifiers + GraphBase + IntoEdges + Visitable + NodeCount + IntoEdgesDirected,
    G::NodeId: Hash + Eq,
Expand description

Compute the closeness centrality of each node in the graph.

The closeness centrality of a node u is the reciprocal of the average shortest path distance to u over all n-1 reachable nodes.

In the case of a graphs with more than one connected component there is an alternative improved formula that calculates the closeness centrality as “a ratio of the fraction of actors in the group who are reachable, to the average distance” 1. You can enable this by setting wf_improved to true.

1 Wasserman, S., & Faust, K. (1994). Social Network Analysis: Methods and Applications (Structural Analysis in the Social Sciences). Cambridge: Cambridge University Press. doi:10.1017/CBO9780511815478

Arguments:

  • graph - The graph object to run the algorithm on
  • wf_improved - If true, scale by the fraction of nodes reachable.

Example

use rustworkx_core::petgraph;
use rustworkx_core::centrality::closeness_centrality;

// Calculate the closeness centrality of Graph
let g = petgraph::graph::UnGraph::<i32, ()>::from_edges(&[
    (0, 4), (1, 2), (2, 3), (3, 4), (1, 4)
]);
let output = closeness_centrality(&g, true);
assert_eq!(
    vec![Some(1./2.), Some(2./3.), Some(4./7.), Some(2./3.), Some(4./5.)],
    output
);

// Calculate the closeness centrality of DiGraph
let dg = petgraph::graph::DiGraph::<i32, ()>::from_edges(&[
    (0, 4), (1, 2), (2, 3), (3, 4), (1, 4)
]);
let output = closeness_centrality(&dg, true);
assert_eq!(
    vec![Some(0.), Some(0.), Some(1./4.), Some(1./3.), Some(4./5.)],
    output
);

  1.