Compute shortest path lengths in the graph.
This function can compute the single source shortest path lengths by specifying only the source or all pairs shortest path lengths by specifying neither the source or target.
Parameters : | G : NetworkX graph source : node, optional
target : node, optional
weight : None or string, optional (default = None)
|
---|---|
Returns : | length : number, or container of numbers
|
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
For digraphs this returns the shortest directed path. 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) # source,target not specified
>>> p[0][4]
4