Search code examples
pythongraphnetworkx

Python Networkx route though all nodes


I'm trying to write a small route planning application in python just to learn about graphs. In the end, the user should be able to pass in his "home" location and enter some locations he wants to stop by. The application will then calculate the optimal path that starts and ends in his home while visiting every location. So far I've got the API requests all sorted out and a Network with all possible routes between all nodes and corresponding weights is automatically created. Now I'm stuck with a 'G' and don't know how to proceed. I've looked into the Networkx documentation about the shortest paths and cannot find a function that seems to do what I want. The best results I got when searching where Wikipedia articles about Dijkstra and the all_pairs_shortest_path() function, which, too, do not yield the answers I'm searching for.

Maybe there is someone out there who stumbled upon the same problem as I have and knows a solution that I cannot find.


Solution

  • If you have a graph G and want to find the route from A (home) to B to C to D (final destination) in order, you'd call dijkstra_path on it for (A, B), (B, C) and (C, D), and concatenate the paths generated.