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

newman_weighted_closeness_centrality(graph, weight_fn=None, wf_improved=True, default_weight=1.0, parallel_threshold=50)[source]#

Compute the weighted closeness centrality of each node in the graph.

The weighted closeness centrality is an extension of the standard closeness centrality measure where edge weights represent connection strength rather than distance. To properly compute shortest paths, weights are inverted so that stronger connections correspond to shorter effective distances. The algorithm follows the method described by Newman (2001) in analyzing weighted graphs.[Newman]

The edges originally represent connection strength between nodes. The idea is that if two nodes have a strong connection, the computed distance between them should be small (shorter), and vice versa. Note that this assume that the graph is modelling a measure of connection strength (e.g. trust, collaboration, or similarity). If the graph is not modelling a measure of connection strength, the function weight_fn should invert the weights before calling this function, if not it is considered as a logical error.

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] You can enable this by setting wf_improved to true.

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 (PyGraph) – The input graph. Can either be a PyGraph or PyDiGraph.

  • weight_fn – An optional input callable that will be passed the edge’s payload object and is expected to return a float weight for that edge. If this is not specified default_weight will be used as the weight for every edge in graph

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

  • default_weight (float) – If weight_fn is not set the default weight value to use for the weight of all edges

  • parallel_threshold (int) – The number of nodes to calculate the the closeness 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