0

I am interested in learning what the best algortihm for finding a way between two points in a grid, while there are walls present, which makes it a maze. Once the maze has been scanned once, and we know where every wall is, what is the best algorithm, for getting as quickly as possible from point A (x1,y1) to point B(x2, y2). I am thinking it is Dijkstras algorithm. It should also work if the maze has cyclical corridors.

I tried different algortihms such as BFS and DFS

2
  • 4
    Welcome to SO! What exactly have you tried so far? We're much more here to help with specific questions of the form "I tried X, but it did not do what I expect and instead resulted in an error!" accompanied by a Minimal, Complete, and Verifiable example Commented Jun 24, 2024 at 21:20
  • If the maze is very maze-like (long twisty paths), then BFS. Otherwise A* with a manhattan distance heuristic. Dijkstra's only makes sense if it's maze-like and moves are weighted with different costs. Commented Jun 25, 2024 at 13:35

1 Answer 1

0

You may want to consider shortest-path algorithms that utilize heuristics, such as A*. A heuristic is essentially additional knowledge that can be applied to guide a search.

A* is similar to Dijkstra's, except it chooses the next node to explore by considering 1) the overall cost from the start to the proposed node, and 2) the predicted "value" of the node based on a specified heuristic. If a proposed node has a high value (it appears more likely to reach the destination), it will be chosen first over other options that have the same cost. But as the path is continually explored, the overall cost will increase. Once the cost outweighs the predicted values of the other options, the algorithm will stop exploring this route and will instead try a different one that (now) appears more optimal.

You can see how the chosen heuristic here is crucial, since a better heuristic will be more effective in guiding the algorithm.

You mention that you are given a start point (x1, y1) and end point (x2, y2), so it is possible that a useful heuristic can be the straight-line distance from the current location to the target destination. The idea is that your pathfinder will always want to close the distance between the start point and end point, so the route that appears to bring it closer to the destination will be prioritized over taking side paths.

But, this heuristic is a poor choice if you know in advance that the most direct route will never be the correct path.

As you can see, the "best" shortest-path algorithm is highly dependent on the problem itself. The more that is known about the context, the more likely it is that there exists some optimization that can be used to enhance the algorithm or determine a more ideal heuristic. Unfortunately, without more details, it is difficult to identify these optimizations.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.