📘 CodingMarble Learn

Search Algorithms in AI: Finding the Way Step by Step

Many AI problems are about finding a way from a start to a goal: a route on a map, a puzzle solution or a good move in a game. We describe the problem as a state space: states (situations) joined by actions (moves). A search algorithm explores this space in a fixed order. Blind (uninformed) search like breadth-first and depth-first search knows nothing about where the goal is. Informed search uses a heuristic, a smart guess of how far the goal is, so it opens far fewer states. A* adds the cost so far (g) to the guess (h) and, with a heuristic that never over-guesses, still finds the shortest path.

🎬 Step-by-step story

  1. This is a maze. Each square is a state. The robot can move one step up, down, left or right. The goal is to get from S to G.
  2. From each state there are a few choices. Squares one move from S light up, then squares two moves away. All states plus all moves make the state space.
  3. Breadth-first search opens every square one move away, then two moves, then three, like a ripple in water. It knows nothing about G, but it finds the shortest path.
  4. Depth-first search follows one path as deep as it can and backs up when it is stuck. It uses little memory, but its path may be long.
  5. Informed search gives every square a guess h, the distance to G. A* opens the square with the smallest g + h first. Count the squares: far fewer are opened.
  6. Your turn. Pick an algorithm, tap squares to build or remove walls and press Run. Predict which algorithm opens the fewest squares, then check.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Is the state space the same as the maze picture?

Not quite. The maze is one way to draw it. The state space is every possible situation plus the moves between them. Step 2 lights up the states one and two moves away from S.

If BFS is blind, how does it still find the shortest path?

It opens all squares 1 move away before any square 2 moves away, so the first time it reaches G, no shorter path can exist. Watch the ripple in step 3.

Why would anyone use DFS if its path is long?

DFS stores only the current path, so it needs very little memory, and it is easy to write with recursion. Step 4 shows it diving and backing up.

Does the heuristic know where the walls are?

No. The Manhattan distance ignores walls. That is why it is fast, and why it never over-guesses. In step 5 the colours show only distance to G.

Is A* always the best choice?

A* needs a good heuristic and stores many states. With no useful guess it behaves like BFS. Try a tricky wall in free play and compare.

Search in AI and the state space

Many problems can be solved by searching: trying possible steps in an organised way until we reach the goal.

To let a computer search, we describe the problem as a state space:

States and moves together form a graph or a search tree. A solution is a path from the start to a goal. An optimal solution is the cheapest one.

The search keeps a list of states waiting to be opened, called the frontier. Opening a state means looking at its neighbours and adding new ones to the frontier. Search algorithms differ only in which frontier state they open next.

Blind (uninformed) search: BFS and DFS

Blind or uninformed search only knows the rules of the problem. It has no idea which direction the goal is.

Breadth-first search (BFS)

BFS opens states level by level: all states 1 move away, then all states 2 moves away, and so on. The frontier is a queue (first in, first out).

Depth-first search (DFS)

DFS goes deep along one path and backs up (backtracks) only when stuck. The frontier is a stack (last in, first out).

Other blind methods: uniform-cost search (open the cheapest path so far, used when moves cost different amounts) and iterative deepening (DFS again and again with depth limit 1, 2, 3…).

Informed search: heuristics, greedy search and A*

A heuristic h(n) is a quick, smart guess of how far state n is from the goal. In a grid maze a good heuristic is the Manhattan distance: h = |x − xgoal| + |y − ygoal|. It ignores walls, so it is fast to compute.

If the heuristic is admissible (it never guesses more than the real distance), A* always finds the shortest path. The better (closer to the truth) the heuristic, the fewer states A* opens.

Straight-line distance on a road map and Manhattan distance on a grid are both admissible. A heuristic of 0 turns A* into uniform-cost search.

Problems needing intelligent search: paths, puzzles and games

Paths: route planning on maps, robot motion, network routing. States are places; costs are distance or time. A* is the usual choice.

Puzzles: 8-puzzle, Rubik's cube, Sudoku, the water-jug puzzle. States are puzzle positions. The number of states grows very fast (the combinatorial explosion), so blind search is too slow and heuristics are needed.

Games: in chess or tic-tac-toe two players take turns. A game tree shows my moves, then the opponent's replies. Minimax assumes the opponent plays their best: I choose the move whose worst outcome is the best for me. Alpha–beta pruning skips branches that cannot change the decision. Programs also stop at a fixed depth and use an evaluation function (a heuristic score of the position).

Search by halving: binary search and bisection

Some searches are not on a maze but on a sorted list or a number line. Here a clever rule cuts the work in half each step.

Both are examples of using information about the problem (the list is sorted, the sign changes) to avoid checking everything, the same idea as a heuristic.

Try it

In the 3D: in free play, make a long wall with one gap far from the goal. Predict: will A* still open fewer squares than BFS? Run both and write down "squares opened" and "path length".

At home: draw a 5 × 5 grid on paper, put S and G in corners and shade 5 walls. Number the squares in the order BFS would open them (use a queue written on the side). Then write the Manhattan distance to G in each square and repeat with A*.

Key formulas and definitions

Worked examples

1. Write the state space for a robot in a 3 × 3 grid that starts at the top-left and must reach the bottom-right, with no walls.

States: the 9 squares (row, column). Initial state: (1,1). Actions: move up, down, left or right if still inside the grid. Goal test: state = (3,3). Path cost: number of moves. The shortest solution has 4 moves, for example right, right, down, down.

2. In a tree, A has children B and C. B has children D and E. C has children F and G. In which order do BFS and DFS open the nodes (children left to right)?

BFS opens level by level: A, B, C, D, E, F, G. DFS goes deep first: A, B, D, E, C, F, G.

3. On a grid, the goal G is at (6, 2). Find the Manhattan heuristic for square (2, 5). If the path cost so far to (2, 5) is g = 3, find f for A*.

h = |2 − 6| + |5 − 2| = 4 + 3 = 7. f = g + h = 3 + 7 = 10.

4. A* has two frontier squares: P with g = 4, h = 5 and Q with g = 6, h = 2. Which is opened first? Which would greedy search open?

A*: f(P) = 9, f(Q) = 8, so Q is opened first. Greedy uses only h: h(Q) = 2 < h(P) = 5, so greedy also opens Q. They can disagree in other cases, e.g. g = 2, h = 5 (f = 7) against g = 8, h = 1 (f = 9): A* picks the first, greedy the second.

5. Use binary search to find 37 in the sorted list 3, 8, 15, 21, 29, 37, 44, 52, 60.

Middle (5th) = 29; 37 > 29 so keep 37, 44, 52, 60. Middle = 44 (take the 2nd of 4); 37 < 44 so keep 37. Found in 3 looks, instead of 6 looks with linear search.

6. In tic-tac-toe it is my turn. Move X leads to positions scored (by the opponent's best replies) +1 and −1; move Y leads to 0 and 0. Which move does minimax choose?

Minimax assumes the opponent picks the worst outcome for me. Worst of X = −1, worst of Y = 0. The best of these is 0, so minimax plays Y (a safe draw instead of risking a loss).

Common mistakes

Practice quiz

1. In a maze, what is a "state"?
2. Which search opens all states 1 move away before any state 2 moves away?
3. A heuristic is:
4. A* chooses the frontier state with the smallest:
5. Which search uses a stack and backtracks when stuck?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between uninformed and informed search?

Uninformed (blind) search such as BFS and DFS uses only the problem rules. Informed search such as greedy and A* also uses a heuristic, a guess of the distance to the goal, to choose which state to open next.

What is a heuristic in AI?

A heuristic is a quick rule of thumb that estimates how close a state is to the goal, for example straight-line distance on a map. It helps the search open promising states first.

Why is A* optimal?

If its heuristic never over-estimates the real remaining cost (admissible), A* cannot finish with a longer path while a shorter one is still waiting in the frontier, so the first path it completes is the shortest.

Where this is taught

PolandLiceum ogólnokształcące, klasa IIIDesigning and programming algorithms (I + II)
South Korea고등학교 2학년AI and intelligent reasoning

Learn first

Learn next

Related lessons

All Computer Science lessons