single_source_shortest_path_length#
- single_source_shortest_path_length(G, source, cutoff=None)[source]#
Compute the shortest path lengths from
sourceto all reachable nodes inG.- Parameters:
- GNetworkX graph
- sourcenode
Starting node for path
- cutoffinteger, optional
Depth to stop the search. Only target nodes where the shortest path to this node from the source node contains <=
cutoff + 1nodes will be included in the returned results.
- Returns:
- lengthsdict
Dict keyed by node to shortest path length from source node.
See also
shortest_path_lengthShortest path length with specifiable source, target, and weight.
single_source_dijkstra_path_lengthShortest weighted path length from source with Dijkstra algorithm.
single_source_bellman_ford_path_lengthShortest weighted path length from source with Bellman-Ford algorithm.
Examples
>>> G = nx.path_graph(5) >>> length = nx.single_source_shortest_path_length(G, 0) >>> length[4] 4 >>> for node in sorted(length): ... print(f"{node}: {length[node]}") 0: 0 1: 1 2: 2 3: 3 4: 4
Only include paths with length less than or equal to the
cutoffkeyword argument:>>> length = nx.single_source_shortest_path_length(G, 0, cutoff=2) >>> for node in sorted(length): ... print(f"{node}: {length[node]}") 0: 0 1: 1 2: 2 ----