Search code examples
algorithmpatha-starshort

Multiple solutions in Shorthest Path Algorithm (A*)


So the other day I found a website, where I could refresh my knowledge about algorithms and I was bumped into a problem.

What will happen, if I run an A* algorithm on a map, where two - exactly the same - possible solutions are available?

  • How the algorithm decides, which solution is the better? Which one will be choosen and why?
  • If it's random, then is there a way to bring some consistency into it through code (and how)?

Solution

  • I believe it depends on how you implement the algorithm. It's not random. If you run the algorithm on the same map multiple times, then the same solution is given every time. It all depends on how you implement the algorithm.

    In the example that you've linked, you can implement the algorithm in two ways. You can make it check the 'up'-direction first, or you can make it check the 'right'-direction first.

    I hope this answer helped