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
  • G (NetworkX graph)

  • target (node) – Target node for path

  • cutoff (integer, optional) – Depth to stop the search. Only paths of length <= cutoff are returned.

Returns

lengths – (source, shortest path length) iterator

Return type

iterator

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('{}: {}'.format(node, length[node]))
0: 4
1: 3
2: 2
3: 1
4: 0

See also

single_source_shortest_path_length(), shortest_path_length()