A* Search Algorithm
Prerequisites
- Heuristic Functions — h(n), admissibility, dominance
- Greedy Best-First Search — f(n)=h(n) limitations
- UCS — Uniform-cost search using g(n)
🎯 Learning Objectives
- Understand f(n) = g(n) + h(n) and how A* combines UCS and Greedy
- Prove that A* is optimal when h is admissible (tree search) / consistent (graph search)
- Trace A* on the Romania map with complete f, g, h tables
- Identify A*’s memory limitation and its implications
- Avoid common GATE mistakes in A* problems
1. The A* Algorithm
A* evaluates nodes using: f(n) = g(n) + h(n), where:
- g(n) = actual cost of the path from the start node to n
- h(n) = estimated cost from n to the nearest goal (heuristic)
- f(n) = estimated total cost of the cheapest solution through n
A* combines the strengths of Uniform-Cost Search (which uses only g) and Greedy Best-First Search (which uses only h). By considering both the cost already paid and the estimated remaining cost, A* can find optimal paths while expanding significantly fewer nodes than UCS.
Key Insight
If g(n) = 0 (ignore path cost), A* becomes Greedy BFS. If h(n) = 0 (no heuristic), A* becomes UCS. A* is the “sweet spot” that balances both.
2. Pseudocode
function A-STAR-SEARCH(problem, h):
node = MAKE-NODE(problem.initial_state)
node.g = 0
node.f = h(node.state)
frontier = PRIORITY-QUEUE ordered by f
frontier.INSERT(node)
explored = empty set
while frontier is not empty:
node = frontier.POP() // node with smallest f
if problem.GOAL-TEST(node.state):
return SOLUTION(node) // optimal if h is admissible
explored.ADD(node.state)
for each action in problem.ACTIONS(node.state):
child = CHILD-NODE(problem, node, action)
child.g = node.g + STEP-COST(node, action, child)
child.f = child.g + h(child.state)
if child.state not in explored and child not in frontier:
frontier.INSERT(child)
else if child in frontier with higher f:
frontier.REPLACE(child) // found cheaper path
return FAILURE
3. Optimality of A*
3.1 Tree Search: Admissibility Suffices
A* tree search is optimal if h(n) is admissible (i.e., h(n) ≤ h*(n) for all n, where h*(n) is the true cost to the goal).
Proof sketch: Suppose A* selects a suboptimal goal G2 with f(G2) = g(G2) > C* (optimal cost). Consider any node n on the optimal path to G1 (the optimal goal). Since h is admissible: f(n) = g(n) + h(n) ≤ g(n) + h*(n) = C*. So f(n) ≤ C* < f(G2). But A* would have selected n before G2 (lower f-value), contradicting our assumption. Therefore A* must select the optimal goal first. ∎
3.2 Graph Search: Consistency Required
For graph search (with explored set/closed list), admissibility alone is not sufficient. We need consistency (monotonicity): h(n) ≤ c(n, a, n′) + h(n′) for every successor n′ of n. Consistency ensures that f-values along any path are non-decreasing, so the first time a node is reached is via the cheapest path.
3.3 Optimal Efficiency
A* is optimally efficient among all algorithms that use the same heuristic. No other optimal algorithm can guarantee expanding fewer nodes than A* (except by breaking ties differently). This is a powerful theoretical result.
4. Properties Summary
| Property | A* Search |
|---|---|
| Complete? | Yes (if branching factor is finite and step costs ≥ ε > 0) |
| Optimal? | Yes (with admissible h for tree search; consistent h for graph search) |
| Time | O(bεd) where ε is the relative error of h — exponential in worst case, but far better than uninformed search with a good heuristic |
| Space | O(bd) — keeps all generated nodes in memory — this is A*’s main weakness |
A* stores all nodes in the frontier and explored set. For large problems, it runs out of memory before running out of time. This motivates memory-bounded variants: IDA*, RBFS, and SMA*.
5. Step-by-Step Trace: Arad to Bucharest
h_SLD to Bucharest:
Arad=366, Zerind=374, Sibiu=253, Timisoara=329,
Oradea=380, Fagaras=176, RimnicuVilcea=193,
Pitesti=100, Bucharest=0, Craiova=160, Lugoj=244,
Mehadia=241, Drobeta=242
| Step | Expand | g | h | f | Frontier (state: f-value) |
|---|---|---|---|---|---|
| 0 | — | — | — | — | {Arad: 0+366=366} |
| 1 | Arad | 0 | 366 | 366 | {Sibiu:140+253=393, Timisoara:118+329=447, Zerind:75+374=449} |
| 2 | Sibiu | 140 | 253 | 393 | {RV:220+193=413, Fagaras:239+176=415, Timisoara:447, Zerind:449, Oradea:291+380=671} |
| 3 | R. Vilcea | 220 | 193 | 413 | {Fagaras:415, Pitesti:317+100=417, Timisoara:447, Zerind:449, Craiova:366+160=526, Oradea:671} |
| 4 | Fagaras | 239 | 176 | 415 | {Pitesti:417, Bucharest(via Fag):450+0=450, Timisoara:447, Zerind:449, Craiova:526, Oradea:671} |
| 5 | Pitesti | 317 | 100 | 417 | {Bucharest(via Pit):317+101=418, Timisoara:447, Zerind:449, Bucharest(via Fag):450, Craiova:526, Oradea:671} |
| 6 | Bucharest | 418 | 0 | 418 | GOAL FOUND! |
Optimal path: Arad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest
Optimal cost: 140 + 80 + 97 + 101 = 418 km
Note that A* found the Bucharest node via Fagaras (f=450) but did not select it because Pitesti had f=417 < 450. This is exactly how A* avoids the Greedy BFS mistake.
6. Worked Examples
Graph with Start=S, Goal=G:
Edges: S→A(1), S→B(4), A→B(2), A→G(12), B→G(5)
h: h(S)=7, h(A)=6, h(B)=2, h(G)=0
| Step | Expand | g | h | f=g+h | Frontier |
|---|---|---|---|---|---|
| 0 | — | — | — | — | {S: 0+7=7} |
| 1 | S | 0 | 7 | 7 | {A: 1+6=7, B: 4+2=6} |
| 2 | B | 4 | 2 | 6 | {A: 1+6=7, G(via B): 4+5+0=9} |
| 3 | A | 1 | 6 | 7 | {B(via A):3+2=5*, G(via B):9, G(via A):13} |
*Wait — B was already explored. In graph search, we skip it. In tree search, we consider it. Let’s trace graph search:
Step 3: Expand A (f=7). Children: B (already explored, skip), G (g=1+12=13, f=13). Frontier: {G(via B):9, G(via A):13}.
Step 4: Expand G via B (f=9). Goal found! Path: S → B → G. Cost = 4 + 5 = 9.
Is this optimal? Path S→A→B→G = 1+2+5=8 is cheaper! But B was already explored when we tried to re-reach it via A. This demonstrates that A* graph search needs consistent h (not just admissible) for optimality. Here h(A)=6 but c(A,B)+h(B)=2+2=4, and 6 > 4 violates consistency.
Start: Goal:
2 | 8 | 3 1 | 2 | 3
--------- ---------
1 | 6 | 4 8 | | 4
--------- ---------
7 | | 5 7 | 6 | 5
h(start) = Manhattan distance:
- Tile 2: |0−0|+|0−1| = 1
- Tile 8: |0−1|+|1−0| = 2
- Tile 3: |0−0|+|2−2| = 0
- Tile 1: |1−0|+|0−0| = 1
- Tile 6: |1−2|+|1−1| = 1
- Tile 4: |1−1|+|2−2| = 0
- Tile 7: |2−2|+|0−0| = 0
- Tile 5: |2−2|+|2−2| = 0
h(start) = 1+2+0+1+1+0+0+0 = 5, g(start) = 0, f(start) = 5.
After moving blank up (swap blank with 6): g=1, recompute Manhattan: tile 6 moves to (2,1) goal (2,1), so its contribution drops from 1 to 0. h=4, f=1+4=5.
A* continues expanding the node with smallest f. With Manhattan distance, the 8-puzzle typically requires exploring only about 25 nodes for a depth-5 solution.
Graph: S→A(2), S→B(3), A→G(4), B→G(1)
h: h(S)=4, h(A)=3, h(B)=1, h(G)=0
UCS (f = g only): Expands S(0), A(2), B(3), G via A(6), G via B(4). Selects G via B first. Path S→B→G, cost=4. Optimal.
Greedy BFS (f = h only): Expands S(4), then B(1) before A(3), then G(0). Path S→B→G, cost=4. Happened to find optimal.
A* (f = g+h): S: 0+4=4. Expand S. A: 2+3=5, B: 3+1=4. Expand B (f=4). G via B: 4+0=4. Expand A (f=5)? No, G(f=4) is selected first. Path S→B→G, cost=4. Optimal ✔
All three find the same answer here, but in general only A* guarantees optimality.
7. Common GATE Mistakes
- Confusing g and h: g(n) is the actual cost from start to n; h(n) is the estimated cost from n to goal. Both contribute to f.
- Forgetting to update g: When a shorter path to a node is found, its g (and therefore f) must be updated in the frontier.
- Admissible ≠ Consistent: For tree search, admissibility suffices. For graph search, consistency is needed. GATE loves testing this distinction.
- A* expands nodes in order of f, not h or g alone. Trace problems require tracking all three values.
- A* is NOT the same as branch-and-bound. Branch-and-bound does not use heuristics.
Summary
- A* uses f(n) = g(n) + h(n), combining path cost and heuristic estimate.
- Complete and optimal (admissible h for tree; consistent h for graph search).
- Optimally efficient — no other optimal algorithm expands fewer nodes with the same h.
- Main weakness: O(bd) space — keeps all nodes in memory.
- The most important informed search algorithm for GATE DA.
References
- Russell, S. & Norvig, P. AIMA, 4th Ed. — Section 3.5.2
- Hart, P.E., Nilsson, N.J., Raphael, B. (1968). “A Formal Basis for the Heuristic Determination of Minimum Cost Paths.”