简体   繁体   中英

Python - Dijkstra's algorithm on a large scale

I have a large network, about 400 nodes, and what I'm trying to do is calculate every possible route you can make on the network. That means, the nodes traversed and the total weighting of the route from Node 1 to Node 2, Node 1 to Node 3 up to Node 1 to Node 400.

Then, from Node 2 to Node 3, Node 2 to Node 4 up to Node 2 to Node 400.

(Just FYI, I have very limited knowledge of python, day to day I work with HTML/CSS)

I've been using this code that I discovered: http://bytes.com/topic/python/insights/877227-dijkstras-algorithm-finding-shortest-route

Which is great, however, due to the size of my network, calculating every possible way to go from any node to any node takes a very, very long time.

From what I understand of the algorithm I linked to, it visits each node in turn from the start node, and gives it a value which is the weighting required to get to that node from the start (I imagine it also records the route required to get to each specific node?). If it finds a shorter route to get to that node it overwrites what it had before. Once it gets to the destination it stops and returns the result.

I figure, if the script can be edited slightly, can it instead of stopping at a specific destination, just visit all the nodes as it usually would and print out a report of the shortest route and weighting to each one? That way, the algorithm would only have to be run a total of 400 times, once for each possible starting position.

Thanks for any advice you can give and I hope that was clear!

You can use the Python graph module NetworkX which has methods for all pairs shortest paths ( weighted and unweighted ). And they are optimized in the sense that they use the best known algorithms for such tasks.

The tweak which you are suggesting will simply change its method of sorting the possible route and nothing more. What you need is to drastically alter the traversal by using assumptions and also djikstra is not that efficient on large scale actually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM