Compute shortest path lengths in the graph.
Parameters : | G : NetworkX graph source : node, optional
target : node, optional
weight : None or string, optional (default = None)
|
---|---|
Returns : | length: int or dictionary :
|
Raises : | NetworkXNoPath :
|
See also
all_pairs_shortest_path_length, all_pairs_dijkstra_path_length, single_source_shortest_path_length, single_source_dijkstra_path_length
Notes
The length of the path is always 1 less than the number of nodes involved in the path since the length measures the number of edges followed.
For digraphs this returns the shortest directed path length. To find path lengths in the reverse direction use G.reverse(copy=False) first to flip the edge orientation.
Examples
>>> G=nx.path_graph(5)
>>> print(nx.shortest_path_length(G,source=0,target=4))
4
>>> p=nx.shortest_path_length(G,source=0) # target not specified
>>> p[4]
4
>>> p=nx.shortest_path_length(G,target=4) # source not specified
>>> p[0]
4
>>> p=nx.shortest_path_length(G) # source,target not specified
>>> p[0][4]
4