rustworkx.graph_unweighted_average_shortest_path_length#

graph_unweighted_average_shortest_path_length(graph, /, parallel_threshold=300, disconnected=False)#

Return the average shortest path length for a PyGraph with unweighted edges.

The average shortest path length is calculated as

\[a =\sum_{s,t \in V, s \ne t} \frac{d(s, t)}{n(n-1)}\]

where \(V\) is the set of nodes in graph, \(d(s, t)\) is the shortest path length from node \(s\) to node \(t\), and \(n\) is the number of nodes in graph. If disconnected is set to True, the average will be taken only between connected nodes.

This function is also multithreaded and will run in parallel if the number of nodes in the graph is above the value of parallel_threshold (it defaults to 300). 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. By default it will use all available CPUs if the environment variable is not specified.

Parameters:
  • graph (PyGraph) – The graph to compute the average shortest path length for

  • parallel_threshold (int) – The number of nodes to calculate the the distance matrix in parallel at. It defaults to 300, but this can be tuned to any number of nodes.

  • disconnected (bool) – If set to True only connected vertex pairs will be included in the calculation. If False, infinity is returned for disconnected graphs. Default: False.

Returns:

The average shortest path length. If no vertex pairs can be included in the calculation this will return NaN.

Return type:

float