networkx.all_shortest_paths¶
-
networkx.all_shortest_paths(G, source, target, weight=None)[source]¶ Compute all shortest paths in the graph.
Parameters: G : NetworkX graph
source : node
Starting node for path.
target : node
Ending node for path.
weight : None or string, optional (default = None)
If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1.
Returns: paths : generator of lists
A generator of all paths between source and target.
Notes
There may be many shortest paths between the source and target.
Examples
>>> G = nx.Graph() >>> nx.add_path(G, [0, 1, 2]) >>> nx.add_path(G, [0, 10, 2]) >>> print([p for p in nx.all_shortest_paths(G, source=0, target=2)]) [[0, 1, 2], [0, 10, 2]]