1

If there are multiple paths from a source to destination, how do I get these ALL of these paths using NetworkX? Note that this is a simplified example, I want to actually be using the nx.all_pairs_shortest_path() function and get all shortest paths between any two nodes.

Code:

import networkx as nx
G = nx.Graph([(0, 1), (0, 2), (1, 3), (2, 3)])
nx.draw(G)
print(nx.shortest_path(G,0,3))

Output I Get:

[0, 1, 3]

Output I Want:

[[0, 1, 3], [0, 2, 3]]

1 Answer 1

0

all_shortest_paths does what you're after, but it is a generator. If you want a list, then put list around it

shortest_path_generator = nx.all_shortest_paths(G,0,3)
list(shortest_path_generator)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.