NetworkX

Previous topic

networkx.shortest_path

Next topic

networkx.single_source_shortest_path

networkx.shortest_path_length

shortest_path_length(G, source, target)

Return the shortest path length between the source and target.

Raise an exception if no path exists.

Parameters:

G : NetworkX graph

source : node label

Starting node for path

target : node label

Ending node for path

Notes

G is treated as an unweighted graph. For weighted graphs see dijkstra_path_length.

Examples

>>> G=nx.path_graph(5)
>>> print nx.shortest_path_length(G,0,4)
4