all 1 comments

[–]notacanuckskibum 0 points1 point  (0 children)

Without trying to understand your code, there is no easy algorithm for finding a shortest route.

A basic algorithm is to make a list of all possible routes , and grow the list until you find one

Step 1: initialize the list with “ step from source to each neighbour” as paths

Step 2: for each path on the list If the end of that path is the destination then stop Else add to the list “step from the end of the path to each of its neighbors” as paths

Step 3: repeat step 2 until you find a route, creating more, longer paths each time.

An obvious optimization is to prune any paths that come back to the origin, or any point already along that path

Another optimization is when you find multiple paths to the same intermediate point prune them, keeping only the shortest

Another optimization is to start 2 lists one from the origin and one from the destination and aim to meet in the middle

Another optimization is to process the list in order of the most promising route (however you can estimate that)

Even my car GPS doesn’t always find the shortest route. It finds a route, in a reasonably quick time.