Best algorithm for maze solving?
Asked Answered
L

1

6

I recently made a project to solve a given maze using different pathfinding algorithms. I did this by importing a black and white maze image, and making each junction a node. I tried solving this using DFS, BFS, Dijkstra and A*, but noticed that surprisingly DFS gave me the shortest running time. My question then is, does it ever make sense to use a more advanced algorithm such as Dijkstra or A* on a perfect maze(one that only has one solution)? Or do those algorithms only make sense in a maze with multiple solutions?

I researched this online, and found that a lot of people like using A* for this sort of problem, but I don’t understand how that’s better, atleast for a perfect maze.

Lilian answered 14/4, 2020 at 23:30 Comment(0)
V
17

This is an interesting question. Let's explore it to see why you might be seeing what you're seeing.

Of the four algorithms you've mentioned - BFS, DFS, Dijkstra's and A* - three of them (BFS, Dijkstra's, and A*) are designed to find shortest paths in structures where there are multiple different paths available and you're interested in finding the shortest. In that sense, running BFS, Dijkstra's, and A* will all, in some sense, incur a cost overhead because you're paying for something you aren't using. Dijkstra's algorithm, in particular, should perform no better than BFS in this case. Taking any step will cost you the same amount, and the cost of maintaining a priority queue or some other structure to find the lowest-cost node in the frontier will likely cost more than a standard queue. In that sense, we can probably rule out Dijkstra's as a candidate for the fastest algorithm here.

That leaves us BFS, A*, and DFS. Let's first compare BFS and DFS. The advantage of DFS is that it's theoretically fast (linear time) and the memory access patterns involved in running DFS (maintaining the top of a stack and probing places near the most-recently-visited spot) plays well with caches. The advantage of BFS is that it will stop searching as soon as it finds the shortest path, with the drawback being that memory accesses are more scattered and play less well with caches.

Let's make a quick geometric argument here. BFS expands outward from the starting location by exploring paths of progressively longer and longer lengths. In that sense, you can imagine that the regions searched by BFS will form something that vaguely approximates a circle centered on the start location. The radius of this circle will be equal to the length of the shortest path found. In that sense, if there were no obstacles, you'd expect BFS to visit some constant fraction of the total spaces in the maze before finding the exit, and with obstacles present it's likely to explore most, if not all, of the spaces. DFS stops as soon as it finds the exit, and it's likely to explore lots of dead ends along the way, so there's similarly a good chance that it'll explore a large fraction of the maze cells. Given the choice between the two, my bet is that DFS would be slightly faster, since generally speaking the constant factor for DFS is lower than BFS.

Then there's DFS versus A*. That's a harder one to analyze a priori. DFS is generally speaking a much faster algorithm than A* because of the associated overhead of maintaining distances in A*, but A* tends to search in directions that are much more likely to get you to the destination. It would probably depend on the shape of the maze. If the maze was constructed in a way that has a lot of long, twisty passageways, then A* would probably do better because it would avoid going the wrong direction until it absolutely had to, where DFS might spend lots of effort descending the wrong way. But you'd have to look at the balance between those factors to be sure.

There's one other issue and that's how the maze itself was generated. There are many different maze generation algorithms - you can use Kruskal's algorithm, DFS, Prim's algorithm, or Wilson's algorithm, for example, to generate mazes. Mazes made with DFS tend to have fewer, longer corridors, while Kruskal's algorithm and Prim's algorithm tend to make many shorter corridors. It may be the case that DFS tends to do better in some of those cases than others, while A* may do better or worse as well. So perhaps the difference between A* and DFS has to do with the maze shape in addition to their own implementation details.

So overall, I'm not surprised to hear that your DFS was the fastest maze-solving algorithm mostly due to DFS's simplicity and good cache locality compared with the other algorithms. The fact that it's beating A* is likely due to the overhead associated with A* not being worth the savings in spaces explored. But to get more data, perhaps you should look at the following:

  • What fraction of the maze, on average, does each algorithm explore before finding the shortest path?

  • How long are the shortest paths in the mazes?

  • How were the mazes generated, and do the answers to the above questions depend on the algorithm used?

Vern answered 15/4, 2020 at 0:7 Comment(3)
Wow. Thanks for your answer! With regards to A*, what sort of heuristic do you think is best for this situation? Just the Manhattan or Euclidean distance? Also, is there any way to generate mazes that have more than one possible path(i.e. one that would make using Dijkstra or A* significantly advantageous)?Lilian
Nice summary. Also, if the entrance and exit are on opposite sides of the maze, I find that BFS seems to explore 75% of it or so on average: mtimmerm.github.io/webStuff/maze.html DFS would probably explore about half of it on average, so there would be an advantage there, too.Hindrance
@SamvitAgarwal If you take it to an extreme, if there are no walls, A* will make a beeline for the exit of the maze and explore almost nothing else, while DFS might take its sweet time exploring random directions. So in that sense, the more paths there are, the better you’d expect A* to perform. I’m curious how, exactly, the number of paths impacts this and that sounds like a good experiment to run. For heuristics: Manhattan distance is a good one unless you have more knowledge of the maze structure.Vern

© 2022 - 2024 — McMap. All rights reserved.