1. Home
  2. AI & Machine Learning
  3. A* Search

A* Search

The path-finding algorithm behind games and maps. See how f = g + h focuses the search on the goal — and compare it with Dijkstra and greedy search on a 3D maze.

Interactive 3DIntermediate13 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A* finds shortest paths in games and maps. It scores each cell with f = g + h: g = steps taken so far, h = a guess of the steps left (here the Manhattan distance). Press Find path, then compare with Dijkstra.

Pseudocode

  1. open ← {start}; g[start] ← 0
  2. while open is not empty:
  3. current ← node in open with the smallest f = g + h
  4. if current == goal: rebuild the path and stop
  5. move current from open to closed
  6. for each neighbour n of current (not a wall, not closed):
  7. if g[current] + 1 < g[n]:
  8. g[n] ← g[current] + 1; parent[n] ← current; add n to open
Read this model as text · 38 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A* finds shortest paths in games and maps. It scores each cell with f = g + h: g = steps taken so far, h = a guess of the steps left (here the Manhattan distance). Press Find path, then compare with Dijkstra.
  2. A*: find the shortest route from S to G moving up, down, left or right. Grey blocks are walls.
  3. Expand (5, 1): f = g + h = 0 + 14 = 14, the smallest in the open set.
  4. Expand (5, 2): f = g + h = 1 + 13 = 14, the smallest in the open set.
  5. Expand (4, 2): f = g + h = 2 + 12 = 14, the smallest in the open set.
  6. Expand (4, 3): f = g + h = 3 + 11 = 14, the smallest in the open set.
  7. Expand (4, 4): f = g + h = 4 + 10 = 14, the smallest in the open set.
  8. Expand (5, 3): f = g + h = 2 + 12 = 14, the smallest in the open set.
  9. Expand (3, 4): f = g + h = 5 + 11 = 16, the smallest in the open set.
  10. Expand (3, 5): f = g + h = 6 + 10 = 16, the smallest in the open set.
  11. Expand (3, 6): f = g + h = 7 + 9 = 16, the smallest in the open set.
  12. Expand (4, 6): f = g + h = 8 + 8 = 16, the smallest in the open set.
  13. Expand (4, 7): f = g + h = 9 + 7 = 16, the smallest in the open set.
  14. Expand (3, 7): f = g + h = 8 + 8 = 16, the smallest in the open set.
  15. Expand (3, 8): f = g + h = 9 + 7 = 16, the smallest in the open set.
  16. Expand (3, 9): f = g + h = 10 + 6 = 16, the smallest in the open set.
  17. Expand (4, 9): f = g + h = 11 + 5 = 16, the smallest in the open set.
  18. Expand (4, 10): f = g + h = 12 + 4 = 16, the smallest in the open set.
  19. Expand (3, 10): f = g + h = 11 + 5 = 16, the smallest in the open set.
  20. Expand (3, 3): f = g + h = 4 + 12 = 16, the smallest in the open set.
  21. Expand (3, 2): f = g + h = 3 + 13 = 16, the smallest in the open set.
  22. Expand (6, 3): f = g + h = 3 + 13 = 16, the smallest in the open set.
  23. Expand (6, 4): f = g + h = 4 + 12 = 16, the smallest in the open set.
  24. Expand (6, 5): f = g + h = 5 + 11 = 16, the smallest in the open set.
  25. Expand (6, 2): f = g + h = 2 + 14 = 16, the smallest in the open set.
  26. Expand (6, 1): f = g + h = 1 + 15 = 16, the smallest in the open set.
  27. Expand (5, 0): f = g + h = 1 + 15 = 16, the smallest in the open set.
  28. Expand (5, 10): f = g + h = 13 + 5 = 18, the smallest in the open set.
  29. Expand (5, 11): f = g + h = 14 + 4 = 18, the smallest in the open set.
  30. Expand (5, 9): f = g + h = 12 + 6 = 18, the smallest in the open set.
  31. Expand (2, 10): f = g + h = 12 + 6 = 18, the smallest in the open set.
  32. Expand (2, 11): f = g + h = 13 + 5 = 18, the smallest in the open set.
  33. Expand (2, 12): f = g + h = 14 + 4 = 18, the smallest in the open set.
  34. Expand (3, 12): f = g + h = 15 + 3 = 18, the smallest in the open set.
  35. Expand (3, 13): f = g + h = 16 + 2 = 18, the smallest in the open set.
  36. Expand (4, 13): f = g + h = 17 + 1 = 18, the smallest in the open set.
  37. Expand (4, 14): f = g + h = 18 + 0 = 18, the smallest in the open set.
  38. Reached G! Path length 18 steps, after expanding 35 cells. A* is optimal and focused.

Try this in the 3D model

  • Run A* and count the explored (purple) cells. Then run Dijkstra on the same maze.
  • Run Greedy best-first. Is its path always the shortest?
  • Press New maze until the goal is hidden behind a long wall, then compare all three algorithms.

The problem

A game character must walk to a target; a delivery app must route a driver. We want the shortest path from a start to a goal, and we want to find it fast.

Dijkstra’s algorithm finds the shortest path but explores in every direction like ripples — even directly away from the goal. A* adds a sense of direction.

f = g + h

For every cell n, A* computes:

  • g(n) — the exact cost of the best path found so far from the start to n.
  • h(n) — a heuristic: an estimate of the cost from n to the goal.
  • f(n) = g(n) + h(n) — the estimated total length of a path through n.

It always expands the cell with the smallest f. Cells that move towards the goal get small f values and are explored first.

The algorithm

open   ← {start}        (cells waiting to be explored)
closed ← {}             (cells already explored)
g[start] ← 0
while open is not empty:
    current ← the cell in open with the smallest f
    if current is the goal: follow the parent pointers back → path
    move current from open to closed
    for each neighbour n of current:
        if n is a wall or in closed: skip
        if g[current] + cost < g[n]:
            g[n] ← g[current] + cost
            parent[n] ← current
            add n to open

In the 3D model: cyan cells are in the open set, purple cells are closed (explored), the yellow raised cell is the current one, and the final path is orange.

Heuristics

On a grid where you can move up, down, left and right, the classic heuristic is the Manhattan distance:

h(n) = |n.row − goal.row| + |n.col − goal.col|

A heuristic is admissible if it never overestimates the true remaining cost. With an admissible heuristic, A* is guaranteed to return a shortest path. Manhattan distance is admissible here because walls can only make the real path longer.

Moves allowed Good heuristic
4 directions Manhattan distance
8 directions (diagonals) Chebyshev / octile distance
Any direction Euclidean (straight-line) distance

A* vs Dijkstra vs greedy

Algorithm Expands smallest… Shortest path? Speed
Dijkstra g ✅ Explores the most
Greedy best-first h ❌ not guaranteed Fast, but can be fooled
A* g + h ✅ (admissible h) Focused — usually the best balance

Try all three in the model on the same maze.

Code

import heapq

def a_star(grid, start, goal):
    """grid: list of strings, '#' = wall. start/goal: (row, col)."""
    rows, cols = len(grid), len(grid[0])
    h = lambda p: abs(p[0] - goal[0]) + abs(p[1] - goal[1])
    g = {start: 0}
    parent = {}
    open_heap = [(h(start), 0, start)]
    closed = set()
    while open_heap:
        f, cost, cur = heapq.heappop(open_heap)
        if cur in closed:
            continue
        if cur == goal:
            path = [cur]
            while cur in parent:
                cur = parent[cur]
                path.append(cur)
            return path[::-1]
        closed.add(cur)
        r, c = cur
        for nr, nc in ((r-1, c), (r+1, c), (r, c-1), (r, c+1)):
            if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] != '#':
                ng = cost + 1
                if ng < g.get((nr, nc), float('inf')):
                    g[(nr, nc)] = ng
                    parent[(nr, nc)] = cur
                    heapq.heappush(open_heap, (ng + h((nr, nc)), ng, (nr, nc)))
    return None

maze = ["S..#....",
        ".#.#.##.",
        ".#...#..",
        ".####.#.",
        "......#G"]
print(a_star(maze, (0, 0), (4, 7)))

Where is A* used?

Video games (NPC movement), robot navigation, GPS routing (with extra speed-ups), puzzle solving (8-puzzle, Rubik’s cube with pattern-database heuristics) and AI planning.

Common mistakes

  • Using a heuristic that overestimates (e.g. Euclidean distance × 2) — faster, but no longer guaranteed optimal.
  • Forgetting the closed set, so cells are expanded again and again.
  • Using Manhattan distance when diagonal moves are allowed (it then overestimates).

Complexity at a glance

Case / operationTimeWhy
Worst case (grid with V cells)O(V log V)With a priority queue for the open set.
With a perfect heuristicO(path length)It walks straight to the goal.
Extra spaceO(V)

Quick check

Test yourself — pick an answer to see if you got it.

1. In A*, f(n) = g(n) + h(n). What is g(n)?

2. What does it mean for a heuristic to be admissible?

3. If h(n) = 0 for every node, A* behaves like…

4. Greedy best-first search (f = h only) is…

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in A* Search. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.