Menu

1 / 2

Why A* Search Works — Heuristics, Shortest Paths, and Optimality

DEV Community·shangkyu shin·6 months ago
#op4dhbpr
#why#algorithms#ai#pathfinding#search
Reading 0:00
15s threshold

A* looks simple until you implement it.

Then one question appears:

Why does this algorithm find good paths without checking every possible path?

The answer is its scoring structure.

A* does not only ask, “How far have I moved?”

It also asks, “How far do I probably still need to go?”

Core Idea

A* is a shortest-path search algorithm.

But it is not blind search.

It combines:

the real cost so far

the estimated cost to the goal

That combination makes A* useful in pathfinding, games, maps, robotics, and graph search problems.

The Key Structure

A* is built around one simple score:

f(n) = g(n) + h(n)

Where:

g(n) = actual cost from the start node to node n

h(n) = estimated cost from node n to the goal

f(n) = total estimated path cost

So A* chooses the node with the lowest f(n).

Not the closest node.

Not the cheapest node so far.

The node that looks best overall.

Implementation View

At a high level, A* works like this:

start with the start node put it into an open set

while the open set is…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.