Compute shortest paths and lengths in a weighted graph G.
Uses Dijkstra’s algorithm for shortest paths.
Parameters : | G : NetworkX graph source : node label
target : node label, optional
cutoff : integer or float, optional
|
---|---|
Returns : | distance,path : dictionaries
|
Notes
Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed.
Based on the Python cookbook recipe (119466) at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/119466
This algorithm is not guaranteed to work if edge weights are negative or are floating point numbers (overflows and roundoff errors can cause problems).
Examples
>>> G=nx.path_graph(5)
>>> length,path=nx.single_source_dijkstra(G,0)
>>> print(length[4])
4
>>> print(length)
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
>>> path[4]
[0, 1, 2, 3, 4]