NetworkX

Previous topic

networkx.single_source_shortest_path

Next topic

networkx.all_pairs_shortest_path

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.

Parameters:

G : NetworkX graph

source : node label

Starting node for path

cutoff : integer, optional

Depth to stop the search - only paths of length <= cutoff are returned.

Examples

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