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
fis 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
- Fix what "shortest" means before choosing an algorithm. A path's cost is the sum of its step lengths:
1for a sideways or vertical move,โ2for a diagonal. Cells are not counted; distances are added. - 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. - So the only decision left is which cell to take out, and it is a single expression. Rank by
g + hand you have A*. Seth = 0and the ranking is by cost-so-far alone โ that is Dijkstra, expanding outward in rings because it has no idea where the goal is. Ignoreginstead and the ranking is by estimate alone โ Greedy Best-First, running at the goal and accepting whatever route it arrived by. - 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 increasinggโ 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โ2rather than1and 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.
Learning path
Counting work, not seconds
References (3)
- Where A*, the f = g + h scoring and the admissibility condition were introduced: P. E. Hart, N. J. Nilsson & B. Raphael, "A Formal Basis for the Heuristic Determination of Minimum Cost Paths." IEEE Transactions on Systems Science and Cybernetics 4, 100โ107, 1968.
- And the algorithm A* generalises, for comparison in the dropdown: E. W. Dijkstra, "A note on two problems in connexion with graphs." Numerische Mathematik 1, 269โ271, 1959.
- The fourth option in the dropdown, and the wavefront it spreads: E. F. Moore, "The shortest path through a maze." Proceedings of an International Symposium on the Theory of Switching, Part II, 285โ292. Harvard University Press, 1959.