Sub topic 5
How A* Works Step by Step
This chapter assumes you know the evaluation function f(n) = g(n) + h(n) from the previous chapter. It also assumes you are comfortable with graphs that have weighted edges and with the idea of a priority queue. Here we follow the A* loop by hand on a small graph. The next chapter turns the loop into code.
The Loop
A* keeps two collections:
- The open list holds nodes that have been discovered but not yet expanded. A priority queue orders them by f.
- The closed set holds nodes that have already been expanded. When h is consistent, their g-values are final.
Each round, A removes the open node with the lowest f. If that node is the goal, the search stops. If not, A expands it. For each neighbor, it computes a new g by adding the edge cost to the current node's g. When the new g beats the best g recorded for that neighbor so far, A* records the new g, sets the neighbor's parent to the current node, and pushes the neighbor onto the open list.
A Worked Trace
Here is the example graph, with edge costs and heuristic values:
| Edge | Cost |
|---|---|
| S–A | 1 |
| S–B | 4 |
| A–B | 2 |
| A–G | 6 |
| B–G | 2 |
| Node | h |
|---|---|
| S | 4 |
| A | 3 |
| B | 2 |
| G | 0 |
The shortest path is S → A → B → G, with cost 5. Each row below is one pop from the open list:
| Step | Popped (g, f) | Changes | Open list after (f) |
|---|---|---|---|
| 0 | none | push S | S(4) |
| 1 | S (0, 4) | A: g=1, f=4. B: g=4, f=6 | A(4), B(6) |
| 2 | A (1, 4) | B improves to g=3, f=5. G: g=7, f=7 | B(5), B(6) stale, G(7) |
| 3 | B (3, 5) | G improves to g=5, f=5 | G(5), B(6) stale, G(7) stale |
| 4 | G (5, 5) | goal popped, stop | none |
To rebuild the path, follow the parent links back from the goal: G → B → A → S. Then reverse the list.
Two Details That Cause Bugs
Stale entries. In step 2, B was pushed a second time with a better f. The old entry B(6) is still in the heap, because binary heaps cannot easily change a key. These leftover entries are harmless as long as you skip a popped node that is already closed. This "lazy deletion" approach is usually simpler and faster than a decrease-key operation.
Where the heuristic matters. In step 2, the open list held B(5) and G(7). Uniform-cost search would have ordered them by g alone: B at 3 and G at 7. Greedy search would have ordered them by h alone: G at 0 and B at 2. That means greedy would have popped G and returned the cost-7 path. A* combines both numbers, so it expands B first and finds the cheaper route.
Exercise
Change h(A) to 6 and keep everything else the same. Trace A* again. Which path does it return, and why?
Show answer
After S is expanded, the open list holds A(f = 1 + 6 = 7) and B(f = 4 + 2 = 6). A* pops B (g = 4) and pushes G with g = 6, f = 6. It then pops G at f = 6, which is lower than A's 7, and returns S → B → G with cost 6. The optimal cost is 5. The heuristic h(A) = 6 overestimates the true remaining cost from A, which is 4, so it is not admissible and the optimality guarantee is lost.
Exercise
Why does A* skip a popped node that is already in the closed set instead of expanding it again?
Show answer
The popped entry is stale. It was pushed before a cheaper g for that node was found, and the cheaper copy has already been expanded. With a consistent heuristic, expanding the stale copy again would only repeat work using a worse g.