{"id":122,"date":"2026-11-13T12:31:58","date_gmt":"2026-11-13T04:31:58","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-orders-expansion-by-f-equals-g-plus-h\/"},"modified":"2026-11-13T21:50:48","modified_gmt":"2026-11-13T13:50:48","slug":"how-a-orders-expansion-by-f-equals-g-plus-h","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-orders-expansion-by-f-equals-g-plus-h\/","title":{"rendered":"How A* orders expansion by f equals g plus h"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>A*<\/strong> pops the frontier vertex with the smallest <strong>f = g + h<\/strong>, where g is the distance already walked and h estimates the distance still to go. If h never overestimates, it is <strong>admissible<\/strong>, and the first time the goal is dequeued under a <strong>consistent<\/strong> h the path is optimal. A consistent h also satisfies h(u) \u2264 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">With a consistent heuristic, the first time a vertex is dequeued its g value is optimal, exactly as in Dijkstra, and the goal&#8217;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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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 + \u03b5h for \u03b5 &gt; 1, trades optimality for fewer expansions on purpose.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set g[source] = 0 and g elsewhere to infinity. Push the source with key h(source).<\/li>\n<li>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.<\/li>\n<li>For each edge u\u2192v of weight w \u2265 0, let trial = g[u]+w. If trial &lt; g[v], set g[v] = trial, parent[v] = u, and push v with key trial + h(v).<\/li>\n<li>If h is only admissible, a pop of a vertex that is already settled must be allowed to reopen it when trial improves g.<\/li>\n<li>If h is consistent, ignore a pop of a vertex already settled. The first pop was final.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the heuristic overestimates or is inconsistent<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>An h that sometimes exceeds the true remainder<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Discard reopening while h is admissible and not consistent<\/td>\n<td>A settled vertex can receive a cheaper g later. Leaving it settled keeps the expensive path.<\/td>\n<\/tr>\n<tr>\n<td>Pop by h alone<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Use a consistent h and still reopen every improvement<\/td>\n<td>The result stays optimal and the extra bookkeeping is unnecessary. Consistency was exactly the permission to expand once.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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*.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import heapq\nadj = {0: [(1, 1), (2, 4)], 1: [(2, 1), (3, 5)], 2: [(3, 1)], 3: []}\nh = {0: 2, 1: 1, 2: 1, 3: 0}\ndist = {0: 0}\npq = [(h[0], 0)]\nwhile pq:\n    f, u = heapq.heappop(pq)\n    if f != dist[u] + h[u]:\n        continue\n    if u == 3:\n        break\n    for v, w in adj[u]:\n        nd = dist[u] + w\n        if nd &lt; dist.get(v, 10**9):\n            dist[v] = nd\n            heapq.heappush(pq, (nd + h[v], v))\nassert dist[3] == 3\nprint(dist[3])\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop A<em> 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<\/em> when an admissible estimate of remaining cost actually prunes the frontier and the edge weights are nonnegative.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for A<em> returns the f = g + h order and the split between an admissible heuristic and a consistent one. <\/em><em>A<\/em> 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.**<\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions\/521"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}