English interface, partly translated: navigation, lab, projects, series, about and contact are; the home page sections, the site search and the content itself (articles, tutorials, research) are still in French.

Interactive lab

Pathfinding: BFS, DFS, Dijkstra, A*

Four algorithms explore the same grid. Draw walls, add costly terrain and compare the visited cells and the cost of the path.

Level
Beginner
Topics
Graphes · Heuristique · File de priorité
Figure 1 — Explored cells in blue, path in orange. Draw walls with the pointer, drag the start (circle) and the goal (square) to move them.

Ready. Run the search or step through it.

The four algorithms on this map
Algorithmcells exploredpath lengthpath cost
Breadth-first (BFS)49839 39
Depth-first (DFS)9695 95
Dijkstra49839 39
A*24439 39
Algorithm
Map
Drawing tool (pointer)
60

Four ways to explore

The four algorithms follow the same scheme: keep a frontier of cells to explore, take one out, add its neighbours. They only differ in the order in which the frontier is emptied.

Algorithm Frontier Takes out first Optimal path?
BFS (breadth-first) queue the cell discovered first yes, in number of steps
DFS (depth-first) stack the cell discovered last no
Dijkstra priority queue the lowest cost from the start g(n)g(n) yes, in cost
A* priority queue the lowest f(n)=g(n)+h(n)f(n) = g(n) + h(n) yes, if hh never overestimates

What to observe

  • BFS spreads in concentric rings: it finds the shortest path in number of cells, but ignores costs. On the "Costly terrain" map, it walks straight through the hard terrain.
  • DFS rushes ahead then backtracks. It finds a path, often a very long one: it is meant for exploring (mazes, connected components, topological sort), not optimising.
  • Dijkstra grows the frontier by increasing cost: it goes around hard terrain when the detour is cheaper. Without terrain, it behaves like BFS.
  • A* adds to the cost already paid a heuristic h(n)h(n), an estimate of the remaining cost — here the Manhattan distance ∣Δx∣+∣Δy∣\lvert \Delta x \rvert + \lvert \Delta y \rvert. It explores towards the goal first and visits far fewer cells, for the same optimal path.

Why A* stays optimal

A heuristic is admissible if it never overestimates the real remaining cost. Manhattan distance is: no move costs less than 1, and at least ∣Δx∣+∣Δy∣\lvert \Delta x \rvert + \lvert \Delta y \rvert moves are needed. With h=0h = 0, A* becomes Dijkstra again; with an overestimating heuristic, it runs faster but may miss the best path.

The table under the figure compares the four algorithms on the current map: same cost for Dijkstra and A*, very different numbers of explored cells.

Other demos

  • Gradient descent

    Artificial intelligence, Beginner

    Pick a function, a starting point and a learning rate, then watch the point walk downhill: convergence, oscillations, divergence, local minima.

  • Optimizers: gradient, momentum, Adam

    Artificial intelligence, Intermediate

    Three optimizers start from the same point on a two-variable surface. Compare their trajectories over the level curves: zigzag, momentum, adaptive step.

  • K-Means step by step

    Artificial intelligence, Beginner

    Assign, update, assign… Follow the centroids until convergence, and discover where K-Means goes wrong: bad start, stretched clusters, rings.