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
weighted : bool, optional
|
---|---|
Returns: | length : number, or container of numbers
|
Raises: | NetworkXError :
|
Notes
If weighted=True and the graph has no ‘weight’ edge attribute the value 1 will be used.
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