Weighted A* Search (WA*)

A* with inflated heuristics — faster but suboptimal, with bounded error

The Simplest Explanation

A* is optimal but slow. What if you're willing to accept a solution that's a little bit worse than optimal, as long as you get it much faster?

WA* says: inflate the heuristic by a weight w>1w > 1. This makes the algorithm greedier — it rushes toward the goal more aggressively, exploring fewer nodes. The tradeoff: the solution may not be optimal, but its cost is bounded by w×optimal costw \times \text{optimal cost}.

f(n)=g(n)+w⋅h(n),w≥1f(n) = g(n) + w \cdot h(n), \quad w \geq 1

  • w=1w = 1: standard A* (optimal, slow)
  • w>1w > 1: WA* (faster, suboptimal but bounded)
  • w=∞w = \infty: Greedy Best-First Search (fastest, no guarantee)
The Bounded Suboptimality Guarantee

If the heuristic hh is admissible, then WA* with weight ww finds a solution with cost ≤w×C∗\leq w \times C^*, where C∗C^* is the optimal cost. For example, with w=2w = 2, the solution cost is at most twice the optimal. You always know the worst-case error.

Why WA* Works (Intuition)

Think of ww as a dial that controls how much you trust the heuristic vs. the actual cost:

  • Low ww (close to 1): you trust g(n)g(n) more, explore carefully, find near-optimal solutions
  • High ww: you trust h(n)h(n) more, rush toward the goal, find solutions fast but they may be costly
  • The error bound grows linearly with ww, so you always know the worst case

The Algorithm

WA* is identical to A* except for the evaluation function:

  1. Put the start node in the open list with g=0g = 0, f=0+w⋅h(start)f = 0 + w \cdot h(\text{start})
  2. While the open list is not empty:
    • Pick the node with the lowest f(n)=g(n)+w⋅h(n)f(n) = g(n) + w \cdot h(n) from the open list
    • If it's the goal → done!
    • Move it to the closed list
    • For each neighbor:
      • Calculate g(neighbor)=g(current)+edge costg(\text{neighbor}) = g(\text{current}) + \text{edge cost}
      • Calculate f(neighbor)=g(neighbor)+w⋅h(neighbor)f(\text{neighbor}) = g(\text{neighbor}) + w \cdot h(\text{neighbor})
      • Standard open/closed list checks (same as A*)
  3. If open list is empty → no path exists
What Changes vs A*?

Only one thing changes: f(n)=g(n)+h(n)f(n) = g(n) + h(n) becomes f(n)=g(n)+w⋅h(n)f(n) = g(n) + w \cdot h(n). The entire algorithm is the same. The weight ww simply makes the heuristic "count more" when deciding which node to expand next.

Bounded Suboptimality

The key theorem for WA*:

If h is admissible, then WA* with weight w finds a path with cost≤w×C∗\text{If } h \text{ is admissible, then WA* with weight } w \text{ finds a path with cost} \leq w \times C^*

where C∗C^* is the optimal (cheapest possible) path cost.

Why the Bound Holds (Sketch)

With w>1w > 1, WA* may expand nodes in a different order than A*. But it will never expand a node with f(n)>w⋅C∗f(n) > w \cdot C^* before finding a solution, because the goal node has f=g(goal)+0=g(goal)f = g(\text{goal}) + 0 = g(\text{goal}), and the first goal found satisfies g(goal)≤w⋅C∗g(\text{goal}) \leq w \cdot C^*.

The Weight-Error Tradeoff

Weight wwMax ErrorSpeedUse Case
1.00% (optimal)SlowestWhen optimality is required
1.5≤ 50% over optimalFasterNear-optimal solutions
2.0≤ 100% over optimalFastWhen speed matters more
3.0≤ 200% over optimalVery fastReal-time systems
→∞\to \inftyNo boundFastestWhen any solution will do

Properties Summary

PropertyValueCondition
Complete?YesFinite branching factor
Optimal?No (bounded)Cost ≤w⋅C∗\leq w \cdot C^* if hh admissible
Time complexityBetter than A*Fewer nodes expanded
Space complexitySame as A*Still keeps all nodes in memory
WA* vs A*: The Node Count Difference

WA* with w=2w = 2 typically expands far fewer nodes than A*. In practice, the reduction can be exponential — especially when the heuristic is good. The price is a solution that's at most twice the optimal cost, which is often acceptable in real-time applications like video game pathfinding or robotics.

Anytime A*

A powerful pattern: run WA* with decreasing weights.

  1. Start with w=5w = 5 → get a quick but rough solution (cost ≤ 5 × C*)
  2. Lower to w=2w = 2 → get a better solution (cost ≤ 2 × C*)
  3. Lower to w=1w = 1 → get the optimal solution (cost = C*)

Each iteration reuses the search from the previous one. You can stop at any time and still have a valid solution with a known error bound. This is called Anytime A* — it gives you the best solution found so far, with improving quality over time.

Worked Example: WA* with w=2

Here's the same 8-node graph. Start at S, goal is G. We use weight w=2w = 2.

Nodeh(n)h(n)
S6
A4
B2
C2
D3
E1
F1
G0

Compare WA* (w=2) with A* (w=1) — notice how WA* rushes toward B because its low h=2 gets inflated to 4:

WA* (w=2) TraceStep 0 / 4
Start / Path
Current
Frontier (Open)
Explored (Closed)
Goal

Initialize: Open = {S}. f(S) = g(S) + w×h(S) = 0 + 2×6 = 12. Weight w = 2 inflates the heuristic.

Current
—
Open List
S
Closed List
—

WA* vs A* on This Graph

A* (w=1)WA* (w=2)
Path foundS→A→D→F→GS→B→E→G
Cost5 (optimal)6 (suboptimal)
Nodes expanded43
Within bound?—6 ≤ 2×5 = 10 ✓

WA* found a solution 25% worse than optimal, but expanded 25% fewer nodes. In larger graphs, the node savings are far more dramatic.

Exam Practice: WA* vs A* Comparison

This question tests whether you can trace WA* and identify when it finds a different (suboptimal) path compared to A*. The graph is designed so that WA* with w=2 takes a completely different route.

Given: Find the shortest path from P to Z using WA* with w=2. Heuristic values:

Nodeh(n)h(n)
P7
Q5
R6
T3
S2
U1
Z0
Exam PracticeTry it yourself first!

Trace WA* with w=2 on paper, then click "Show Solution" to verify. Compare with A* — does WA* find the same path?

Key Observation

A* (w=1) would expand P → R → S → U → Z (cost 6). WA* (w=2) expands P → Q → T → Z (cost 8). The entire path is different because the inflated heuristic makes Q (h=5, inflated to 10) look better than R (h=6, inflated to 12), even though R is on the optimal path.

Error bound verification: 8 ≤ 2 × 6 = 12 ✓

QuizIf w = 1.5 and A* finds an optimal path of cost 10, what is the maximum cost WA* could return?
QuizWhat happens to WA* as w → ∞?
QuizWA* with w=2 finds a path of cost 14. A* with w=1 finds cost 12 on the same graph. Is the WA* result valid according to the error bound?