How A* orders expansion by f equals g plus h

Short answer: A* pops the frontier vertex with the smallest f = g + h, where g is the distance already walked and h estimates the distance still to go. If h never overestimates, it is admissible, and the first time the goal is dequeued under a consistent h the path is optimal. A consistent h also satisfies h(u) ≤ w(u,v) + h(v). Dijkstra is the case h = 0. An admissible h that is not consistent can require a vertex to be reopened.


What the problem actually asks

Dijkstra expands in order of distance from the source and knows nothing about the target. A lower bound on the remainder directs the same settlement argument toward the target. The heap key has to include both the cost already paid and a lower bound on the cost left, or the first time the goal is popped will not be optimal.

Admissible means h(v) is never above the true remaining cost. Consistent, or monotone, means the estimate itself obeys the triangle inequality along every edge. Consistency implies admissibility when h(goal) = 0. The implementation difference is whether a settled vertex can be improved later.


What the algorithm guarantees

With a consistent heuristic, the first time a vertex is dequeued its g value is optimal, exactly as in Dijkstra, and the goal’s first dequeue is a shortest path. Each vertex is expanded once. With a binary heap the time is O((V+E) log V) in the worst case, and a good heuristic reduces the number of expansions far below V. Extra memory is the frontier.

With an admissible heuristic that is not consistent, optimality needs reopening: a later path may arrive at a dequeued vertex with a smaller g, and that vertex must return to the frontier. Skipping the reopen returns a wrong path on some graphs. An inconsistent heuristic can also make the number of reopens exponential in crafted cases. If h overestimates, the first goal dequeue can be a suboptimal path even with reopening disabled or enabled. Weighted A*, which pops g + εh for ε > 1, trades optimality for fewer expansions on purpose.


The working steps

  1. Set g[source] = 0 and g elsewhere to infinity. Push the source with key h(source).
  2. Pop the vertex u of smallest f. If u is the goal, return the parent path. Its g is optimal when h is consistent, or when h is admissible and this is the first pop under a correct reopen policy.
  3. For each edge u→v of weight w ≥ 0, let trial = g[u]+w. If trial < g[v], set g[v] = trial, parent[v] = u, and push v with key trial + h(v).
  4. If h is only admissible, a pop of a vertex that is already settled must be allowed to reopen it when trial improves g.
  5. If h is consistent, ignore a pop of a vertex already settled. The first pop was final.

The optimal-first-pop claim needs a heuristic that never overestimates, and the no-reopen implementation needs consistency as well. A zero heuristic is Dijkstra and expands a shortest-path tree with no bias toward the goal. Negative weights are outside the argument, just as they are for Dijkstra.


Approaches that look simpler and fail

Approach Why it fails when the heuristic overestimates or is inconsistent
An h that sometimes exceeds the true remainder The f order can pop the goal along a path that looked better only because a cheaper path was over-penalized. The returned path need not be optimal.
Discard reopening while h is admissible and not consistent A settled vertex can receive a cheaper g later. Leaving it settled keeps the expensive path.
Pop by h alone The search prefers vertices that look close to the goal even when the path already taken is absurd. That is greedy best-first search, and it is not optimal.
Use a consistent h and still reopen every improvement The result stays optimal and the extra bookkeeping is unnecessary. Consistency was exactly the permission to expand once.

No heuristic and nonnegative weights is Dijkstra. Unit weights and no distance estimate is breadth-first search. Iterative deepening of the same f bound, to save the frontier memory, is IDA*.


A working example

This Python fragment expands the open set by f = g + h. The heuristic is the hop lower bound, which is consistent on this graph, so the first time vertex 3 is dequeued its distance is final. It prints 3.

import heapq
adj = {0: [(1, 1), (2, 4)], 1: [(2, 1), (3, 5)], 2: [(3, 1)], 3: []}
h = {0: 2, 1: 1, 2: 1, 3: 0}
dist = {0: 0}
pq = [(h[0], 0)]
while pq:
    f, u = heapq.heappop(pq)
    if f != dist[u] + h[u]:
        continue
    if u == 3:
        break
    for v, w in adj[u]:
        nd = dist[u] + w
        if nd < dist.get(v, 10**9):
            dist[v] = nd
            heapq.heappush(pq, (nd + h[v], v))
assert dist[3] == 3
print(dist[3])

Where it is used

The A* search is the pathfinder on game navigation meshes and in robotics planners: the heuristic is the remaining distance, and the open set expands the smallest f = g + h. Road engines often replace the plain search with contraction hierarchies. With a zero heuristic the search is Dijkstra.


When you can drop this algorithm

Drop A when no admissible heuristic is available. Dijkstra is the honest special case. Drop the no-reopen code when the heuristic is not known to be consistent. Keep A when an admissible estimate of remaining cost actually prunes the frontier and the edge weights are nonnegative.


Why this stays published

We publish this so a search for A returns the f = g + h order and the split between an admissible heuristic and a consistent one. A expands by g plus h, returns an optimal path when h is admissible, and may settle each vertex once only when h is also consistent.**

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *