Search: finding a path to a goal

14 min read

Many problems are really "find a sequence of steps that reaches a goal": driving directions, solving a puzzle, planning a robot's movements. AI calls this search.

Framing a search problem

  • State: a snapshot of the situation (where you are on the map; the arrangement of a puzzle).
  • Actions: what you can do in a state (drive to a neighboring intersection; slide a tile).
  • Transition model: which state each action leads to.
  • Goal test: how you know you've arrived.
  • Path cost: what each step costs (distance, time, money).

The algorithm keeps a frontier: states it has discovered but not yet explored. It repeatedly takes one state off the frontier, checks whether it is the goal, and adds its neighbors. It also remembers explored states so it doesn't loop forever.

Uninformed search

AlgorithmFrontier works likeBehavior
Depth-first search (DFS)A stack (last in, first out)Dives down one path as far as possible before backing up. Uses little memory; may find a long path
Breadth-first search (BFS)A queue (first in, first out)Explores everything one step away, then two steps, and so on. Finds the shortest path when all steps cost the same, but uses a lot of memory

Informed search: using a hint

A heuristic is an estimate of how far a state is from the goal, such as straight-line distance on a map.

  • Greedy best-first search always expands the state that looks closest to the goal. Fast, but can be fooled into a long detour.
  • A* search expands the state with the lowest cost so far + estimated cost remaining. If the heuristic never overestimates the true remaining cost (it is admissible), A* is guaranteed to find the cheapest path. Navigation apps use descendants of A*.

Adversarial search: games

In two-player games, your opponent is trying to beat you. Minimax assumes both sides play perfectly: you pick the move that maximizes your score, assuming your opponent will then pick the move that minimizes it, and so on down the game tree.

Game trees explode quickly (chess has more possible games than atoms in the observable universe), so programs use:

  • Alpha-beta pruning: skip branches that provably can't change the decision.
  • Depth limits with an evaluation function: stop after a few moves and estimate who is winning.

Modern game AIs like AlphaGo combine search with a neural network that learned which positions are good: classic AI and machine learning working together.

Check your understanding

Check 1 of 2Module 2 · Classic AI: search, logic, probability and optimization

Which search algorithm is guaranteed to find the cheapest path when its heuristic never overestimates?

Check 2 of 2Module 2 · Classic AI: search, logic, probability and optimization

In minimax, what does the algorithm assume about the opponent?

Answer the checks, then mark this lesson complete
Completion and scores are saved in this browser.