Reputation: 13
I'm trying to figure out the best method of getting/displaying the shortest distance between multiple locations. To best explain this think of it as a map and use the following below. All of the distances and paths from each location would be already put in a MySQL database.
Locations(Letter -> Locations it can reach. and distance/time in [])
A -> B [5]
A -> C [4]
B -> Z [1]
C -> Z [50]
So A can go to B and takes 5 minutes. While going A to C takes 4 minutes.
Now what I'm trying to figure out is lets say someone says they are currently at location A and want to get to Z. How can I get the system to go through the database and determine that A -> B -> Z is the shortest path compared to A -> C -> Z.
I was originally thinking of doing a loop on each system but this setup is going to contain hundreds of different locations and paths. So I just can already see it creating infinite loops the second it follows a path that leads back to the starting position.
Maybe it is impossible.
Any help or suggestions would be much appreciated!
Upvotes: 1
Views: 1618
Reputation: 12663
The Shortest Path Problem can be solved with numerous algorithms. Dijkstra is the classic:
Upvotes: 6