average_shortest_path_length(G, weight=None, method=None)
The average shortest path length is
$$a =\sum_{s,t \in V} \frac{d(s, t)}{n(n-1)}$$where :None:None:`V`
is the set of nodes in G
, :None:None:`d(s, t)`
is the shortest path from :None:None:`s`
to t
, and :None:None:`n`
is the number of nodes in G
.
If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. If this is a function, the weight of an edge is the value returned by the function. The function must accept exactly three positional arguments: the two endpoints of an edge and the dictionary of edge attributes for that edge. The function must return a number.
The algorithm to use to compute the path lengths. Supported options are 'unweighted', 'dijkstra', 'bellman-ford', 'floyd-warshall' and 'floyd-warshall-numpy'. Other method values produce a ValueError. The default method is 'unweighted' if :None:None:`weight`
is None, otherwise the default method is 'dijkstra'.
If G
is the null graph (that is, the graph on zero nodes).
If G
is not connected (or not weakly connected, in the case of a directed graph).
If :None:None:`method`
is not among the supported options.
Returns the average shortest path length.
>>> G = nx.path_graph(5)
... nx.average_shortest_path_length(G) 2.0
For disconnected graphs, you can compute the average shortest path length for each component
>>> G = nx.Graph([(1, 2), (3, 4)])See :
... for C in (G.subgraph(c).copy() for c in nx.connected_components(G)):
... print(nx.average_shortest_path_length(C)) 1.0 1.0
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.shortest_paths.generic.average_shortest_path_length
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them