Search: Shortest Paths and A*
Planning is finding a cheapest path in a graph of states. Dijkstra expands outward in every direction; A* adds a heuristic guess of the remaining cost, , so it heads toward the goal and expands far fewer nodes. As long as the guess never overestimates, the path it finds is still the shortest.
What the symbols mean
- Priority
The estimated cost of the best complete route through n; A* expands the smallest f first.
- Cost so far
The cheapest known cost from the start to n.
- Heuristic
A guess of the remaining cost from n to the goal.
What the symbols mean
- True remaining cost
The actual cheapest cost from n to the goal.
What the symbols mean
- Edge cost
The cost of the step from u to v.
Explanation
A* (say "A-star") is simply Dijkstra's shortest-path algorithm with a sense of direction. Dijkstra explores outward from the start in every direction at once, like a ripple in a pond. A* also explores, but it favors nodes that look closer to the goal, using a guess of how far each node still has to go. In , the pieces are (what it actually cost to get to ), (the guess of what's left, called the heuristic) and (the estimated total cost of the best trip through ). A* always expands the node with the smallest . That one change is the whole algorithm, and the rest of this lesson is about why it still finds the shortest path.
Planning as graph search. A lot of AI planning is a shortest-path problem in disguise. The nodes are states (a robot's position, an arrangement of a sliding puzzle, a board position) and the edges are actions, each with a cost (distance, time, fuel, moves). The graph is usually far too big to write down: the 8-puzzle alone has reachable states, and a robot's map can have millions of cells. So neighbors are generated on the fly, and the real cost of a search is how many nodes it expands (takes off the frontier and generates the neighbors of).
Dijkstra, recalled. From Paths, Connectivity and Shortest Paths: settle the unsettled node with the smallest , update its neighbors, repeat. It's correct, but it has no idea where the goal is. Ask it for a route east and it searches west just as hard, settling every node closer to the start than the goal is.
Heuristics. A good is cheap to compute and close to the true remaining cost without going over. On a road map, straight-line distance works: no route can beat a straight line (the triangle inequality). On a grid where you move one cell up, down, left or right at cost 1, the Manhattan distance works, because each move fixes at most one unit of one coordinate. In the 8-puzzle, the number of misplaced tiles works, since each one needs at least one move.
Why A* still finds the shortest path. Call the cheapest cost . Suppose is admissible: it never overestimates, . Before the goal is expanded, some node on an optimal path is always waiting on the frontier with its correct cost-so-far (the start is, and expanding such a node puts its successor on the path onto the frontier, correct too). Its priority is
Now suppose the goal sat on the frontier with a worse cost . Its priority is , so comes off the frontier first, and so does every later node on the optimal path, until the goal itself is reached with cost . So the first time A* expands the goal, it has the cheapest route. Admissibility was used exactly once, in the . And the nodes A* ever needs to expand are those with : a better heuristic pushes more nodes above that line, so they never get touched.
Consistency, and the extremes. A heuristic is consistent if along every edge: a triangle inequality. Then never decreases along a path, and A* never has to revisit a node, just like Dijkstra. In fact A* with a consistent is exactly Dijkstra on the same graph with each edge's cost changed to , which consistency keeps nonnegative. Two extremes show the range. With , A* is Dijkstra. With , it walks straight down the optimal path and expands nothing else. Real heuristics sit in between, and there are two animations further down, one on a small graph and one in an open field, that show how much work a heuristic saves.
Overestimating breaks it. If ever guesses too high, A* can be scared away from the best path and return a worse one (one of the practice problems builds a case). Sometimes that's done on purpose: "weighted A*" uses , say, to find a route that's at most 50% worse, much faster.
A common mistake. It's tempting to reuse Manhattan distance on every grid. On a grid that also allows diagonal moves at cost 1, it overestimates: one diagonal step fixes a unit of and a unit of at once, so going from to takes 3 moves, but Manhattan says 6. The search loses its guarantee. The right heuristic there is . Check admissibility against the moves your problem actually allows.
See it
Worked examples
Run A* from S to G on the graph with edges S–P 1.5, S–Q 1.5, S–A 2, S–B 3, A–C 2, C–G 2, B–G 3.5, using the straight-line heuristic : S 4.5, P 5.56, Q 5.56, A 3.16, B 2.33, C 1.5, G 0. How many nodes does it expand, compared with Dijkstra?
- 1
Put the start on the frontier with , so . It's the only node there, so expand it.
A robot on a grid moves from to . Is the Manhattan distance an admissible heuristic if it moves (a) only up, down, left and right at cost 1, (b) also diagonally at cost 1?
- 1
Compute the heuristic: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A game character moves one tile at a time in 4 directions at cost 1, toward a goal at . Two frontier nodes: X at with , and Y at with . Using the Manhattan heuristic, which does A* expand next?
What is the Manhattan distance from to ?
In the 8-puzzle with goal rows , the current rows are . What is the sum of the Manhattan distances of the tiles (not the blank) from their goal squares?
A robot moves on a grid in 8 directions (diagonals included), every move costing 1. Which heuristic is admissible and as large as possible?
A network has links S–A 4, S–B 1, B–A 2, A–G 1 and B–G 5 (costs in ms). What is the shortest delay from S to G, in ms?
What does A* do when the heuristic is for every node?
A heuristic has and , and the edge from to costs 3. Is the heuristic consistent on this edge?
The 8-puzzle has arrangements of its 8 tiles and the blank, and exactly half of them can be reached from any given start. How many states can a search from one start reach?
The hardest one here. Get it, and you've got this lesson.
A graph has edges S–A 1, A–G 5, S–B 2 and B–G 2. Someone uses the heuristic , , , . What cost does A* return, and what is the true shortest cost? Enter both.
Where it's used
A* and its relatives plan routes in navigation apps, move characters around obstacles in video games, and steer warehouse robots and planetary rovers across grid maps. Puzzle and scheduling solvers use the same "cost so far plus admissible guess" idea, and self-driving car planners search graphs of possible trajectories many times per second, where expanding fewer nodes is the difference between reacting in time and not.