43224315423ABCDEFGH
AlgoPlus//graph / astar
Read the theory

A* Pathfinding

Dijkstra guided by a heuristic toward the target.

Stability
In-Place
Space Complexity
Avg Time
Source
Target
Legend
Current
Frontier
Visited
Path / MST
AI Tutor Workspace
In a nutshell
A* is Dijkstra with a sense of direction. Alongside the real cost from the start (g) it adds a heuristic guess of the cost still to go (h), and always expands the node with the smallest total f = g + h. That guess steers it toward the goal, so it explores far fewer nodes — and as long as the heuristic never overestimates, the path it returns is still a genuine shortest path.
Ready
Press play to begin the cinematic walkthrough.
Like Dijkstra, but biased toward the target using a heuristic — fewer detours, faster arrival.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for A* Pathfinding.
Code Simulator
1
def astar(graph, start, target):
2
    pq = [(h(start), start)]
3
    g_score = {n: inf for n in graph}
4
    g_score[start] = 0
5
    while pq:
6
        f, u = heappop(pq)  # Visit
7
        if u == target: break  # Reached
8
        for v, w in graph[u]:
9
            tentative = g_score[u] + w
10
            if tentative < g_score[v]:
11
                g_score[v] = tentative
12
                heappush(pq, (tentative + h(v), v))
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.