NetworkX

Previous topic

networkx.single_source_shortest_path

Next topic

networkx.all_pairs_shortest_path

Quick search

networkx.single_source_shortest_path_length

single_source_shortest_path_length(G, source, cutoff=None)

Return the shortest path length from source to all reachable nodes.

Returns a dictionary of shortest path lengths keyed by target.

>>> G=nx.path_graph(5)
>>> length=nx.single_source_shortest_path_length(G,1)
>>> length[4]
3
>>> print length
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}

cutoff is optional integer depth to stop the search - only paths of length <= cutoff are returned.