NetworkX

Previous topic

networkx.shortest_path_length

Next topic

networkx.single_source_shortest_path

networkx.average_shortest_path_length

average_shortest_path_length(G, weighted=False)

Return the average shortest path length.

The average shortest path length is the sum of path lengths d(u,v) between all pairs of nodes (assuming the length is zero if v is not reachable from v) normalized by n*(n-1) where n is the number of nodes in G.

Parameters:

G : NetworkX graph

weighted : bool, optional, default=False

If True use edge weights on path.

Notes

If weighted=True and the graph has no ‘weight’ edge attribute the value 1 will be used.

Examples

>>> G=nx.path_graph(5)
>>> print nx.average_shortest_path_length(G)
2.0