Compute shortest paths in the graph.
Parameters : | G : NetworkX graph source : node, optional
target : node, optional
weight : None or string, optional (default = None)
|
---|---|
Returns : | path: list or dictionary :
|
See also
all_pairs_shortest_path, all_pairs_dijkstra_path, single_source_shortest_path, single_source_dijkstra_path
Notes
There may be more than one shortest path between a source and target. This returns only one of them.
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]