NetworkX

Previous topic

networkx.algorithms.shortest_paths.unweighted.predecessor

Next topic

networkx.algorithms.shortest_paths.weighted.dijkstra_path_length

networkx.algorithms.shortest_paths.weighted.dijkstra_path

networkx.algorithms.shortest_paths.weighted.dijkstra_path(G, source, target, weight='weight')

Returns the shortest path from source to target in a weighted graph G.

Parameters :

G : NetworkX graph

source : node

Starting node

target : node

Ending node

weight: string, optional :

Edge data key corresponding to the edge weight

Returns :

path : list

List of nodes in a shortest path.

Raises :

NetworkXNoPath :

If no path exists between source and target.

Notes

Uses a bidirectional version of Dijkstra’s algorithm. Edge weight attributes must be numerical.

Examples

>>> G=nx.path_graph(5)
>>> print(nx.dijkstra_path(G,0,4))
[0, 1, 2, 3, 4]