M4RKYU.SYSEdition 2027
Skip to content
LOCZH/安大略 · 加拿大/▸logs · demystifying heuristic search algorithms 59lh待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 创作创作
    创作Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像影像
    影像Overview
    • 照片影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 写作写作
    写作Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于关于
  • 联系联系
EN

同步 · dev.to / @markyu

A* Search Finally Clicked When I Drew the Grid

A practical developer-friendly explanation of heuristic search, Greedy Best-First Search, and A* using a grid example instead of textbook language.

发布日期
May 3 '24
·
阅读时长
3 min read
·
点赞
15
algorithmsaipythonbeginners
在 dev.to 查看

本页目录

  • The Mental Model
  • Greedy Best-First Search
  • A Search
  • Why the Heuristic Matters
  • Where Developers Actually See This
  • What I Would Avoid
  • Final Thought

Heuristic search did not click for me from formulas.

It clicked when I drew a grid, blocked a few cells, and watched the algorithm make greedy decisions that looked smart until they were not.

Here is the problem:

S . . # .
. # . # .
. # . . .
. . . # G

S is the start. G is the goal. # is blocked.

The question is simple: which cell should we try next?

The Mental Model

A heuristic is a guess.

For grid movement, a common guess is Manhattan distance:

h(n) = abs(n.x - goal.x) + abs(n.y - goal.y)

It says: "How many horizontal/vertical steps would it take if walls did not exist?"

That "if walls did not exist" part matters. The heuristic is useful, but it is not the full truth.

Greedy Best-First Search

Greedy search uses only the guess:

priority = h(n)

It always chases the cell that looks closest to the goal.

That can be fast. It can also walk straight into a wall and waste time because it ignores how expensive the path already was.

Small Python-style sketch:

from heapq import heappush, heappop

def greedy(start, goal, neighbors):
    open_set = []
    heappush(open_set, (0, start))
    visited = set()

    while open_set:
        _, node = heappop(open_set)
        if node == goal:
            return True
        if node in visited:
            continue

        visited.add(node)

        for next_node in neighbors(node):
            if next_node not in visited:
                heappush(open_set, (manhattan(next_node, goal), next_node))

    return False

This is simple, but it is not guaranteed to find the cheapest path.

A* Search

A* uses two numbers:

g(n) = cost from start to current node
h(n) = estimated cost from current node to goal
f(n) = g(n) + h(n)

That is the practical difference.

Greedy says:

Which node looks closest to the goal?

A* says:

Which node gives the best total path estimate?

def astar(start, goal, neighbors):
    open_set = []
    heappush(open_set, (0, start))

    came_from = {}
    g_score = {start: 0}

    while open_set:
        _, node = heappop(open_set)

        if node == goal:
            return rebuild_path(came_from, node)

        for next_node in neighbors(node):
            tentative = g_score[node] + 1

            if tentative < g_score.get(next_node, float("inf")):
                came_from[next_node] = node
                g_score[next_node] = tentative
                f_score = tentative + manhattan(next_node, goal)
                heappush(open_set, (f_score, next_node))

    return None

This is the version I would actually teach first.

Why the Heuristic Matters

If the heuristic is too weak, A* behaves more like Dijkstra's algorithm and explores too much.

If the heuristic overestimates, A* can become fast but lose optimality.

The useful property is admissibility:

h(n) <= real remaining cost

In plain English: the heuristic is allowed to be optimistic, but it should not lie by saying the goal is farther than it really is.

Where Developers Actually See This

Heuristic search shows up in:

  • game pathfinding
  • robot movement
  • route planning
  • scheduling
  • puzzle solving
  • AI agent planning

For 2026 AI agent systems, this mental model is becoming useful again. A planning agent often needs to choose the next tool call or search path without brute-forcing every option. The same old idea applies: a decent heuristic can save a lot of wasted exploration.

What I Would Avoid

I would not start by memorizing every search algorithm.

Start with these three:

AlgorithmUses cost so far?Uses heuristic?Finds shortest path?
Dijkstrayesnoyes
Greedy Best-Firstnoyesnot always
A*yesyesyes, with a good heuristic

That table clears up most confusion.

Final Thought

Heuristic search is not magic AI. It is controlled guessing.

The engineering skill is choosing a guess that is cheap, useful, and honest enough for the problem.

Where have you used A* or heuristic search outside a textbook example?

相关阅读

Frontend Linear Data Structures Deep Dive: Arrays, Stacks, Queues, and Linked Lists

The Big Picture Before diving into stacks, queues, and linked lists, it helps to know...

computerscience

JS Sorting Algorithms Every Developer Should Know

You call .sort() every day without thinking about it. But the sorting algorithm your language's...

algorithms

Network Address Calculation: The Subnet Math That Matters

A practical subnetting guide showing how to calculate a network address from an IP address and mask using binary math and simple examples.

networking

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
返回全部文章
下一篇React Loading Screens Are a State Machine ProblemA practical React loading screen guide using hooks, request states, error states, CSS animation, and why spinners alone are not enough.
返回档案
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话
频道开放

随时打个招呼 · 2026

--:--:--EST加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • 商店
RSSJSON Feed

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
  • 邮件
© 2026 ZhenXiao Mark Yumarkyu0615@gmail.com
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
隐私条款由 Next.js 16 · React 19 · Tailwind 4 构建