Note

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.

rustworkx.graph_closeness_centrality#

graph_closeness_centrality(graph, wf_improved=True)#

Compute the closeness centrality of each node in a PyGraph 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)},\]

where:

  • \(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.

Parameters:
  • graph (PyGraph) – 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.

Returns:

A dictionary mapping each node index to its closeness centrality.

Return type:

CentralityMapping