Pathfinding Algorithm Visualizer

watch different search algorithms race across the same grid, frontier by frontier

Loading interactive simulation...

Lesson

The theory โ€” Pathfinding Algorithm Visualizer

Three of the four algorithms in the dropdown are the same algorithm. They all rank the cells waiting to be explored with one expression, f(n) = g(n) + h(n) โ€” cost so far plus estimated cost remaining โ€” and they differ only in which of the two terms they keep. Drop h and you have Dijkstra. Ignore g and you have Greedy Best-First. The fourth, breadth-first search, is what Dijkstra collapses into when every step costs the same.

What each symbol means

g(n)
the cost of the cheapest route found so far from the start to cell n. A sideways or vertical step costs 1; a diagonal costs โˆš2, because the step is measured as a length rather than counted as one move.
h(n)
the estimate of the cost still to come. For Dijkstra and breadth-first search it is not merely unused โ€” it is zero, and that is precisely what makes them the same expression with a term missing.
f(n)
the ranking key: the cell with the smallest f is the one explored next. It is printed above the grid with the current cell's own numbers substituted, so the choice the algorithm just made is visible rather than asserted.
frontier
the amber cells โ€” discovered, but not yet explored. Expanded nodes counts what has left this set, which makes it a measure of work done, not of answer quality.

Where the formula comes from

  1. Fix what "shortest" means before choosing an algorithm. A path's cost is the sum of its step lengths: 1 for a sideways or vertical move, โˆš2 for a diagonal. Cells are not counted; distances are added.
  2. Now the loop, which is identical for all four: take a cell out of the frontier, mark it explored, and offer each passable neighbour back to the frontier with an updated g. Notice what this loop never mentions โ€” a heuristic, a goal direction, or which algorithm you picked.
  3. So the only decision left is which cell to take out, and it is a single expression. Rank by g + h and you have A*. Set h = 0 and the ranking is by cost-so-far alone โ€” that is Dijkstra, expanding outward in rings because it has no idea where the goal is. Ignore g instead and the ranking is by estimate alone โ€” Greedy Best-First, running at the goal and accepting whatever route it arrived by.
  4. Breadth-first search does not rank at all: first in, first out. With Allow diagonal off, every step costs exactly 1, so the order cells arrive in is the order of increasing g โ€” and breadth-first search therefore explores precisely what Dijkstra explores. You can confirm it without regenerating the grid: switch between the two and Expanded nodes does not budge. Turn diagonals on and the argument dies with it, because a diagonal costs โˆš2 rather than 1 and arrival order stops tracking cost.

How to read what you see

The priority score sits above the grid with the current cell's own g and h filled in, so you can read the ranking that produced the last choice. Below it are two counts: Expanded nodes and Path length. Amber is the frontier, indigo is already explored. Changing the algorithm does not generate a new grid, and that is what makes the comparison worth anything โ€” on the same four-way layout Dijkstra's Expanded nodes is never smaller than A*'s, and Greedy Best-First's is a small fraction of either.

Assumes
Step costs that are never negative, and a grid that does not change while the search runs. The start is always the top-left cell and the goal the bottom-right. Closing a cell is final โ€” the loop never reconsiders one โ€” and that is only safe because no later route can arrive more cheaply when every step adds a non-negative amount.
Breaks when
Expanded nodes is the number that moves most dramatically, and it is not a measure of quality. Choose Greedy Best-First: on the same grid it explores a small fraction of what A* does, and on most layouts it hands back a longer Path length. Generate a few fresh grids and watch the two counts move in opposite directions. Examining fewer cells is a claim about how hard the algorithm worked, never about how good its answer was.

why heuristics matter 🖖

The formula f(n)=g(n)+h(n) makes A* prefer nodes that are cheap so far and still look close to the goal. If h never overestimates the remaining cost, A* keeps its shortest-path guarantee; a stronger admissible h explores fewer unnecessary cells. Switch the algorithm dropdown to see what happens when you drop the heuristic (Dijkstra), drop the cost-so-far (Greedy), or drop scoring entirely (BFS).

One loop, four personalities 🖖

All four algorithms run the exact same loop: pull the most promising cell off a waiting list (the amber frontier), mark it visited (indigo), then add its open neighbours back to the list. They differ only in how they decide which cell looks most promising. The expanded nodes count is the scoreboard, and it does not move the way you expect. On an empty grid A* expands every single cell, because with Manhattan distance every monotone route to the corner scores the same f and there is nothing to prefer. Add obstacles and the count falls.

Diagonals can break the guarantee 🖖

Turn on 'Allow diagonal' with the Manhattan heuristic and A* can quietly return a path that isn't the shortest. A diagonal step here costs only โˆš2 โ‰ˆ 1.41, but Manhattan distance charges it as 2 โ€” so the heuristic overestimates, becomes inadmissible, and the optimality guarantee is lost. Switch to Euclidean, which never overshoots the straight-line distance, and the shortest path comes back.

Learning path

Counting work, not seconds

References (3)

Example problems

  • open grid - A 20x20 grid with a tenth of it blocked, and A* still expands most of it: every monotone route scores f = 38, so nothing breaks the tie.
  • maze-like - Clutter makes A* faster, not slower. Blocking density 0.28 destroys the ties that make an open grid expensive, and the search settles for about half the free cells.
  • weak heuristic - Manhattan is exact on an open 4-connected grid and Euclidean always undershoots it, so Euclidean is the weaker of the two admissible heuristics. Same path, about a third more cells.
  • dijkstra explores - The same 24x24 grid at the same density 0.22 as Bad heuristic, with the heuristic removed altogether. Blind to the goal, Dijkstra expands about half again as many cells as A*.
  • greedy trap - Greedy scores on h alone, so it dives at the goal and expands under half of what A* does. It also comes back with a longer path most of the time, which is the whole of the bargain.
  • bfs + diagonals - BFS has no goal information, so it reaches nearly every open cell. With diagonals allowed it counts a diagonal as one step, so it is minimising hops rather than distance.