# A Search Algorithm to Solve a Maze

By: marwa • Essay • 1,184 Words • May 18, 2010 • 1,452 Views

**Page 1 of 5**

A Search Algorithm to Solve a Maze

Abstract

Our project will be to come up with a smart agent – MiFo – that is capable of solving a maze using the shortest path possible. So, clearly, the concept of path finding is the main idea behind our project.

In this paper, we will discuss the search algorithm that we will be using to come up with the fastest solution to our maze. To make the maze more pragmatic we will add some constraints, such as walls and traps.

Since path finding is one of the most important aspects in Artificial intelligence, we will also shed some light on other algorithms that have been used to solve mazes and the advantages of our chosen algorithm in comparison to them.

Delving In

So, our agent is MiFo. The start state is a start square on the maze that we choose at the beginning, and we place MiFo on it. The goal state is a square that represents a goal that MiFo has to reach.

We have divided our search area into a square grid. So, our agent will be dealing with discrete steps, not with continuous movements. Besides, dividing the search area into a square grid will reduce the cost of the non-optimal solutions that could be generated without division, not to mention the time taken to find the route.

We will have to scenarios.

In Scenario A, the maze is unknown to MiFo. So, he will probably try out every square, then when he reaches a wall, he will backtrack to the last branch that he was at, and take the other route this time, and keep trying this method of moving forward then backtracking so many times until he reaches the goal square provided that a path exists.

In Scenario B, MiFo knows the maze ahead of time, so he can implement a search algorithm and calculate the shortest path to the goal square (again, provided that a path to the goal exists), save the shortest path that he came up with, and use it in reaching his goal.

So, MiFo will have to use some algorithm. So many algorithms were used, such as Best First Search, Depth First Search (which was the algorithm of choice for Mike Gold who was the first to solve a maze using a search algorithm).

The Algorithms' Race

Using the A* algorithm will provide the most economical path for the agent, once the start and goal squares are selected. We could have chosen Best First Search which consists of expanding the closer node to the goal node. But the search algorithm that we should choose should keep track of the cost from the current node to the goal, and that's why Best First Search does not work.

But luckily, A* algorithm does the job perfectly. So, an optimal solution is guaranteed. And that's one of the major reasons why we chose A* search. Another reason for choosing A* would be the use of the heuristic. And since the heuristic is admissible, then the algorithm becomes optimal.

The path finding is going to be performed on a relatively small search space, so we will not run out of memory. But in case we ran out of memory, we can either shrink the size of the search space, or change some aspects in our algorithm, specifically the heuristic value.

The Cost

In A* search the total cost is referred as the cost of reaching the goal from current node aka g(n) added to the estimated heuristic i.e. h(n).

Therefore:

f(n) = g(n) + h(n)

Note that an ideal heuristic will give the potentially minimal cost for reaching the goal.

However, the heuristic estimate is going to be the Manhattan Distance.

The red line shown above is the Manhattan distance between the two points. It is computed by adding x component to the y component. This heuristic is not expensive computation-wise and that's an advantage of using it.

A* Algorithm – Our Winner!

Basically, we have two lists, an open list and a closed list. The open list has all the squares that we might consider. A closed list will eventually contain all the squares that will lead us to a solution.

We choose one square in the matrix to be our start square. This start square SS is placed in the open list, and all adjacent squares to it that are not walls of course, are also added to the open list. Once all the adjacent squares are added to the list, we drop SS from the open list and add it to the closed list. In this case SS is the parent square for those adjacent squares to it.

The second step would be to consider each of the adjacent

**A Search Algorithm to Solve a Maze**