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.


unweighted_average_shortest_path_length(graph, parallel_threshold=300, disconnected=False)[source]#

Return the average shortest path length 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 \(s\) to \(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.

  • graph – The graph to compute the average shortest path length for, can be either a PyGraph or PyDiGraph.

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

  • as_undirected (bool) – If set to True the input directed graph will be treated as if each edge was bidirectional/undirected while finding the shortest paths. Default: False.

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


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

Return type: