- all_shortest_paths(G, source, target, weight=None, method='dijkstra')¶
Compute all shortest simple paths in the graph.
- GNetworkX graph
Starting node for path.
Ending node for path.
- weightNone 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.
- methodstring, optional (default = ‘dijkstra’)
The algorithm to use to compute the path lengths. Supported options: ‘dijkstra’, ‘bellman-ford’. Other inputs produce a ValueError. If
weightis None, unweighted graph methods are used, and this suggestion is ignored.
- pathsgenerator of lists
A generator of all paths between source and target.
methodis not among the supported options.
targetcannot be reached from
There may be many shortest paths between the source and target. If G contains zero-weight cycles, this function will not produce all shortest paths because doing so would produce infinitely many paths of unbounded length – instead, we only produce the shortest simple paths.
>>> 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]]