This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.


closeness_centrality(graph, wf_improved=True)[source]#

Compute the closeness centrality of each node in a graph object.

The closeness centrality of a node \(u\) is defined as the reciprocal of the average shortest path distance to \(u\) over all \(n-1\) reachable nodes in the graph. In it’s general form this can be expressed as:

\[C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},\]


  • \(d(v, u)\) - the shortest-path distance between \(v\) and \(u\)

  • \(n\) - the number of nodes that can reach \(u\).

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” [WF]. This can be expressed as

\[C_{WF}(u) = \frac{n-1}{N-1} \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},\]

where \(N\) is the number of nodes in the graph. This alternative formula can be used with the wf_improved argument.

  • graph – The input graph. Can either be a PyGraph or PyDiGraph.

  • wf_improved (bool) – This is optional; the default is True. If True, scale by the fraction of nodes reachable.


A dictionary mapping each node index to its closeness centrality.

Return type:



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