I'm trying to make a small public transport routing application.
My data is represented in a following structure:
graph = {'A': {'B':3, 'C':5},
'B': {'C':2, 'D':2},
'C': {'D':1},
'D': {'C':3},
'E': {'F':8},
'F': {'C':2}}
Where:
- graph dict key is a node
- subdict key is an edge between 2 nodes
- subdict value is an edge weight
I was using find_shortest_path algorithm described here https://www.python.org/doc/essays/graphs/ but it is rather slow because of recursion and has no support of weights.
So I moved to the algorithm described by Davide Epstein here http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/ (and even better implementation could be find there in comments with the usage of heapq)
It works great, it is really fast, but I get only the best route instead of the list of all possible routes. And that is where I stuck.
Could somebody help me with that please, or at least give a direction? I'm not very good in graph shortest paths algorithms.
Thanks in advance!
It's no doubt that there would be a huge amount of shortest paths in the graph. So it is hard to generate all shortest path in a satisfied time-complexity. But I can give you a simple method that can get as much shortest paths as you want.
Algorithm
Pseudo Code:
I've run across a similar problem in transportation network analysis. I've used the excellent python module NetworkX. It has a function to generate all simple paths between two nodes. Here is the link:
http://networkx.lanl.gov/reference/generated/networkx.algorithms.simple_paths.all_simple_paths.html
Networkx has a function to do this all_shortest_paths.
It returns a generator of all shortest paths.