A* Search Algorithm
The smartest way to find the shortest path — explained so simply you'll never forget it
The Simplest Explanation
Imagine you're lost in a city and want to reach your hotel. You have two pieces of information:
- How far you've already walked (you know this exactly)
- How far the hotel looks from where you are (you can see the skyline and estimate)
A* says: always pick the next move that minimizes (distance walked so far) + (estimated distance remaining).
That's it. If you remember nothing else, remember this: pick the node with the smallest "cost so far + guess to goal".
- = exact cost from start to (this is known, no guessing)
- = estimated cost from to goal (this is the guess/heuristic)
- = total estimated cost of the best path through
Your guess must never overestimate the real remaining distance. If the hotel is 5 km away, you can guess 3 km (underestimate = fine) or 5 km (exact = fine), but never 7 km (overestimate = breaks optimality). This is called admissibility.
Why A* Works (Intuition)
Think of it this way:
- If you only use (ignore the guess), you get Dijkstra's algorithm — it explores in every direction equally, like flood fill. It works but is slow because it wastes time going the wrong way.
- If you only use (ignore the cost so far), you get Greedy Best-First Search — it beelines toward the goal but might find a suboptimal path because it doesn't account for how expensive its route has been.
- A combines both* — it heads toward the goal (thanks to ) but doesn't take unnecessarily expensive routes (thanks to ).
The Algorithm (Step by Step)
- Put the start node in the open list with ,
- While the open list is not empty:
- Pick the node with the lowest from the open list — call it
current - If
currentis the goal → done! Reconstruct path - Move
currentto the closed list - For each neighbor of
current:- Calculate
- Calculate
- If neighbor is in closed list with a lower → skip
- If neighbor is in open list with a lower → skip
- Otherwise → add (or update) neighbor in the open list
- Pick the node with the lowest from the open list — call it
- If open list is empty and goal not found → no path exists
When multiple nodes have the same -value, you can break ties by:
- Preferring the node with higher (i.e., closer to the goal — this prefers depth-first exploration and is the standard approach)
- Preferring the node with lower (closer to start — shallower exploration)
- Alphabetical order (for exam questions where you need a deterministic answer — always check the question's convention)
Admissibility and Consistency
Admissibility
A heuristic is admissible if it never overestimates the true cost to the goal:
where is the actual optimal cost from to the goal.
Why it matters: If is admissible, A* is guaranteed to find the optimal solution. If overestimates even once, A* might skip the optimal path.
Consistency (Monotonicity)
A heuristic is consistent if for every node and its successor reached by action :
This means: the heuristic drop from to is never more than the actual step cost.
Consistency implies admissibility (but not vice versa). Consistency also guarantees that A* never needs to re-open nodes from the closed list.
Properties Summary
| Property | Value | Condition |
|---|---|---|
| Complete? | Yes | Finite branching factor |
| Optimal? | Yes | admissible |
| Time complexity | worst case | Same as BFS in worst case |
| Space complexity | Keeps all nodes in memory |
Where = branching factor, = depth of the shallowest goal.
In the worst case (e.g., ), A* degenerates to Dijkstra's and explores everything. But with a good heuristic, A* can be exponentially faster — it only explores nodes where (optimal cost), pruning the rest.
How to Design Good Heuristics
Method 1: Relaxed Problems
Remove constraints from the original problem. The optimal cost in the relaxed problem is an admissible heuristic for the original.
Example: In the 8-puzzle, if you relax "only one tile can move into the blank" to "tiles can move anywhere", you get the Misplaced Tiles heuristic. If you further relax "tiles move one step at a time" to "tiles teleport", you get the Manhattan Distance heuristic.
A more informed heuristic (higher but still ) dominates a less informed one and leads to fewer node expansions.
Method 2: Pattern Databases
Precompute optimal solution costs for sub-problems and store in a lookup table. The maximum of multiple pattern database heuristics is also admissible and more informed.
Dominance
If for all and both are admissible, then dominates . A* with will never expand more nodes than with .
Worked Example: Step-by-Step Trace
Here's an 8-node graph. Start at S, goal is G. Heuristic values are shown in the table.
| Node | S | A | B | C | D | E | F | G |
|---|---|---|---|---|---|---|---|---|
| 10 | 7 | 5 | 4 | 3 | 2 | 1 | 0 |
The edge costs are shown on the graph below. Click through each step to see how A* expands nodes:
Start: Open list = {S}, g(S)=0, h(S)=10, f(S)=0+10=10
Key Observations from This Trace
- Tie at step 1: After expanding S, nodes A, B, C all have . We break ties by preferring lower (closer to start → more progress made), so A is expanded first.
- Path update at step 3: When B is expanded, node E already has (via A), but the path S→B→E gives (cheaper). A* updates E's -value. This is why we check the open list before skipping.
- Suboptimal path rejection: The path S→A→E→G costs , but A* finds S→A→D→G costs . Both are explored but only the optimal one is returned.
- Goal not expanded immediately: Even after G enters the open list, A* continues expanding nodes with (like E with ). Only when G has the lowest is it selected — guaranteeing optimality.
Exam Practice: Harder Question
This is a Stanford/IIT-level question. The graph has a non-trivial structure where the "obvious" greedy path is not optimal. Try to solve it yourself before clicking "Show Solution".
Given: Find the shortest path from P to Z using A* search.
| Node | P | Q | R | T | U | V | W | Z |
|---|---|---|---|---|---|---|---|---|
| 9 | 6 | 7 | 4 | 3 | 2 | 5 | 0 |
Work through A* step-by-step on paper, then click "Show Solution" to verify your answer.
Why This Question Is Tricky
- The greedy trap: Going P→Q seems attractive because is lower than , but the optimal path goes through R first.
- Multiple paths to same node: Both Q and R can reach U. A* must correctly determine that R→U is cheaper than Q→T→U.
- Path cost verification: P→Q→T→V→Z = 4+3+3+3 = 13 vs P→R→U→V→Z = 2+5+2+3 = 12. The first path looks "more direct" in the graph but is 1 unit more expensive.