M4RKYU.SYSEdition 2027
Skip to content
LOCEN/Ontario · CA/▸logs · demystifying heuristic search algorithms 59lhStandbyOK/--:--:--EST
M4M4RK_YUportfolio
  • BuildBuild
    BuildOverview
    • WorkSelected case studies and write-ups
    • GamesPlayable prototypes and game-dev logs
  • GalleryGallery
    GalleryOverview
    • PhotosPhoto collections and visual experiments
    • ShopPrints, posters, and one-off objects
  • WritingWriting
    WritingOverview
    • BlogLong-form devlogs and field notes
    • NotesShort observations, links, snippets
  • ResourcesResources
    ResourcesOverview
    • Tools38 in-browser developer utilities
    • LinksDaily-use dev and design bookmarks
  • AboutAbout
  • ContactContact
中文

syndicated · 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.

Published
May 3 '24
·
Reading time
3 min read
·
Reactions
15
algorithmsaipythonbeginners
View on dev.to

On this page

  • 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?

Related reading

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

originally published

This post first ran on dev.to. Comments and reactions live there.

Continue on dev.to
Back to all posts
NextReact 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.
Back to archive
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
Crafted since 2024
ZhenXiao Mark YuZhenXiao Mark Yu
get in touch

Saw something here?Tell me about it.

It's a portfolio, not a service · but I read every note — drop a line if anything here resonated, or just to say hi.

Start a conversation
open channel

say hi anytime · 2026

--:--:--ESTOntario, Canada
  • Email
  • GitHub
  • dev.to
  • LinkedIn
  • Twitter / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat

Newsletter

Get the occasional dispatch

Notes and logs from m4rkyu.com — short, dated, no noise. Unsubscribe anytime.

Work

Production builds, games, and visual archives.

  • Projects
  • Games
  • Archive
  • Logs

Resources

Daily-use tools and a personal link library.

  • Search
  • Latest
  • Tools
  • Links
  • Notes
  • Topics
  • Shop
RSSJSON feed

Studio

Background, contact, and channels for collaboration.

  • About
  • Contact
  • Changelog
  • Colophon
  • Resumepending

Socials

Find me on the usual feeds.

  • GitHub
  • dev.to
  • LinkedIn
  • Twitter / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
  • Email
© 2026 ZhenXiao Mark Yumarkyu0615@gmail.com
  • Email
  • GitHub
  • dev.to
  • LinkedIn
  • Twitter / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
PrivacyTermsBuilt with Next.js 16 · React 19 · Tailwind 4