Compute shortest paths in the graph.
Parameters: | G : NetworkX graph source : node, optional
target : node, optional
weighted : bool, optional
|
---|---|
Returns: | path: list or dictionary :
|
Notes
There may be more than one shortest path between a source and target. This returns only one of them.
If weighted=True and the graph has no ‘weight’ edge attribute the value 1 will be used.
For digraphs this returns a shortest directed path. To find paths 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(G,source=0,target=4)
[0, 1, 2, 3, 4]
>>> p=nx.shortest_path(G,source=0) # target not specified
>>> p[4]
[0, 1, 2, 3, 4]
>>> p=nx.shortest_path(G) # source,target not specified
>>> p[0][4]
[0, 1, 2, 3, 4]