ENع
←A* search
Lesson S3 · Special topics

A* search

14 min9 steps5 questionsIntermediate
About this lesson

Dijkstra's algorithm finds the shortest route by searching outward in every direction. A* adds a guess of the distance left, so it heads for the goal and leaves most of the map alone. Watch it on a 4 by 5 grid with a wall: every heap entry carries f = g + h, and the search pops 9 cells where Dijkstra's algorithm pops all 18. The loop is eight lines of Python with heapq. The lesson "Dijkstra's algorithm" covers the heap-based loop this lesson starts from, and "Multi-source and 0-1 BFS on grids" covers grids as graphs.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S3 · Interactive
01 / 09

Find the shortest route

Start at S and reach G. Each move goes up, down, left or right and costs 1. The tall blocks are walls. Dijkstra's algorithm would find the shortest route, but it searches in every direction. A* aims at G.

grid: 4 rows x 5 columns, (row, column) from the top left
  . # . . .
  . # . . .
  . . . . .
  . . . . .
S = (0,0), G = (0,4)
each move: 1 step, up / down / left / right
open cells: 18, walls: 2
Loading figure…
or ← →
Read the lesson step by step
  1. Find the shortest route

    Start at S and reach G. Each move goes up, down, left or right and costs 1. The tall blocks are walls. Dijkstra's algorithm would find the shortest route, but it searches in every direction. A* aims at G.

    grid: 4 rows x 5 columns, (row, column) from the top left
      . # . . .
      . # . . .
      . . . . .
      . . . . .
    S = (0,0), G = (0,4)
    each move: 1 step, up / down / left / right
    open cells: 18, walls: 2
  2. h: a guess of moves left

    Give each cell a guess h of the moves still needed: rows apart plus columns apart, ignoring walls. This is the Manhattan distance. S is 4 from G, so h(S) = 4. Walls can only make the real route longer, never shorter.

    h(v) = |row - row of G| + |col - col of G|
    4 # 2 1 0
    5 # 3 2 1
    6 5 4 3 2
    7 6 5 4 3
    walls (#) are ignored by the guess
    h(S) = 4, h(G) = 0
  3. The score: f = g + h

    g counts the moves made from S; h guesses the rest. Their sum f estimates a route through the cell. S has g = 0 and h = 4, so (4, S) goes into a heap, a priority queue that hands out the smallest f.

    f(v) = g(v) + h(v)
    push (0,0)   g=0 h=4 f=4
    heap: [(4, (0,0))]
  4. Pop the smallest f

    heappop takes out the entry with the smallest f: S. Its only open neighbour is the cell below. That move leads away from G, so g rises by 1 and h rises by 1: f = 1 + 5 = 6.

    pop  (0,0)   g=0 h=4 f=4
    push (1,0)   g=1 h=5 f=6
    heap: [(6, (1,0))]
    a move away from G: g + 1, h + 1, so f + 2
  5. Around the wall, f climbs

    The wall pushes the search down. Each move away from G adds 2 to f: the next cell down gets 8, the corner below it 10. The cell right of the 8 is a move toward G, so it also gets 8.

    pop  (1,0)   g=1 h=5 f=6
    push (2,0)   g=2 h=6 f=8
    pop  (2,0)   g=2 h=6 f=8
    push (3,0)   g=3 h=7 f=10
    push (2,1)   g=3 h=5 f=8
    heap: [(8, (2,1)) (10, (3,0))]
  6. Toward G, f stays flat

    A move toward G adds 1 to g and takes 1 from h, so f stays 8. A* keeps popping 8s and walks along the open row. The cells scored 10 lead away from G; they wait at the back of the heap.

    pop  (2,1)   g=3 h=5 f=8
    push (3,1)   g=4 h=6 f=10
    push (2,2)   g=4 h=4 f=8
    pop  (2,2)   g=4 h=4 f=8
    push (1,2)   g=5 h=3 f=8
    push (3,2)   g=5 h=5 f=10
    push (2,3)   g=5 h=3 f=8
    heap: [(8, (1,2)) (8, (2,3)) (10, (3,0)) (10, (3,1)) (10, (3,2))]
    a move toward G: g + 1, h - 1, so f stays the same
  7. Stop when G is popped

    Up the far side, the 8s lead to G. A* stops when G comes out of the heap, not when it goes in. g(G) = 8: the shortest route takes 8 moves. A* popped only 9 of the 18 open cells.

    pop  (1,2)   g=5 h=3 f=8
    push (0,2)   g=6 h=2 f=8
    push (1,3)   g=6 h=2 f=8
    pop  (0,2)   g=6 h=2 f=8
    push (0,3)   g=7 h=1 f=8
    pop  (0,3)   g=7 h=1 f=8
    push (0,4)   g=8 h=0 f=8
    pop  (0,4)   g=8 h=0 f=8
    heap: [(8, (1,3)) (8, (2,3)) (10, (3,0)) (10, (3,1)) (10, (3,2))]
    return g[G] = 8
    route: (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2) (0,3) (0,4)
  8. Dijkstra pops all 18

    Set h = 0 and the same code is Dijkstra's algorithm: f is just g. It pops cells in order of g: all 18 open cells, G last. The dashed cells are the 9 extra pops that A* skipped.

    h = 0 (Dijkstra): pop order by g
    (0,0) g=0
    (1,0) g=1
    (2,0) g=2
    (2,1) g=3
    (3,0) g=3
    (2,2) g=4
    (3,1) g=4
    (1,2) g=5
    (2,3) g=5
    (3,2) g=5
    (0,2) g=6
    (1,3) g=6
    (2,4) g=6
    (3,3) g=6
    (0,3) g=7
    (1,4) g=7
    (3,4) g=7
    (0,4) g=8
    pops: Dijkstra 18, A* 9
  9. Never guess too high

    Manhattan distance never guesses more than the real moves left: it is admissible. So once G is popped, nothing left in the heap can lead to a shorter route. With a guess that can overshoot, A* no longer promises the shortest route.

    admissible: h(v) <= true moves from v to G, for every v
    cell   h  true
    (0,0)   4    8
    (0,2)   2    2
    (0,3)   1    1
    (0,4)   0    0
    (1,0)   5    7
    (1,2)   3    3
    (1,3)   2    2
    (1,4)   1    1
    (2,0)   6    6
    (2,1)   5    5
    (2,2)   4    4
    (2,3)   3    3
    (2,4)   2    2
    (3,0)   7    7
    (3,1)   6    6
    (3,2)   5    5
    (3,3)   4    4
    (3,4)   3    3
    when G is popped, every entry left has f >= g(G)

Quiz Unlocks after the last step.