networkx.algorithms.shortest_paths.unweighted.single_source_shortest_path_length

single_source_shortest_path_length(G, source, cutoff=None)[source]

Compute the shortest path lengths from source to all reachable nodes.

Parameters
GNetworkX graph
sourcenode

Starting node for path

cutoffinteger, optional

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

Returns
lengthsdict

Dict keyed by node to shortest path length to source.

See also

shortest_path_length

Examples

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