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.digraph_closeness_centrality#

digraph_closeness_centrality(graph, wf_improved=True, parallel_threshold=50)#

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

The closeness centrality of a node u is defined as the reciprocal of the average shortest path distance to u over all n1 reachable nodes in the graph. In its general form this can be expressed as:

C(u)=n1v=1n1d(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

CWF(u)=n1N1n1v=1n1d(v,u),

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

This function is multithreaded and will run in parallel if the number of nodes in the graph is above the value of parallel_threshold (it defaults to 50). If the function will be running in parallel the env var RAYON_NUM_THREADS can be used to adjust how many threads will be used.

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

  • parallel_threshold (int) – The number of nodes to calculate the the betweenness centrality in parallel at if the number of nodes in the graph is less than this value it will run in a single thread. The default value is 50

Returns:

A dictionary mapping each node index to its closeness centrality.

Return type:

CentralityMapping