networkx.algorithms.shortest_paths.unweighted.single_target_shortest_path_length

single_target_shortest_path_length(G, target, cutoff=None)[source]

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

Parameters
GNetworkX graph
targetnode

Target node for path

cutoffinteger, optional

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

Returns
lengthsiterator

(source, shortest path length) iterator

See also

single_source_shortest_path_length, shortest_path_length

Examples

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