How Maze Solvers Find the Shortest Path

A maze solver does not squint at pixels. It reads the same graph the generator built: cells as vertices, open passages as edges of equal cost. Maze Foundry uses breadth-first search (BFS) because every step costs one move. The orange overlay is that reconstructed path, not a freehand decoration.

Graph traversal

Traversal means visiting vertices by following edges, remembering where you have been so you do not loop forever. Two classic orders are the queue (BFS) and the stack (DFS). The workshop generator may have used a stack to carve the maze; the solver uses a queue to measure it. Those roles should not be swapped in conversation: DFS is excellent at digging, unreliable at claiming a shortest route once loops exist.

Starting and ending cells

BFS begins at a chosen start: the entrance, or the player’s current cell when you ask for a hint. The destination is the exit. If start equals end, the path is a single cell and length is zero. If either identifier is missing from the graph, the solver reports failure instead of inventing a line through a wall.

Breadth-first search

Enqueue the start, mark it visited, and set its predecessor to null. While the queue is not empty, dequeue a cell and inspect each neighbor connected by a real passage. Unvisited neighbors are marked, given a predecessor pointer back to the current cell, and enqueued. The first time the exit is reached, the search can stop: in an unweighted graph, the first arrival is a shortest arrival.

The workshop queue is a compact array with a moving head so it does not repeatedly shift every element. That is an implementation detail, not a different algorithm.

Depth-first search as a solver

DFS can still reach the exit by plunging down one corridor. The first path it finds may wander through most of the maze. In a perfect maze that path is the only simple path, so length matches BFS. In a braided or room maze, DFS may return a long walk while a short loop-cutting route exists. Do not treat a DFS trace as a shortest-path certificate when independent cycles are greater than zero.

Visited cells and predecessor relationships

Visited flags prevent reprocessing. They are not the solution. Many more cells may be visited than the number of cells on the answer path. The statistics field “visited cell count” reports how much of the graph BFS touched before hitting the exit. Predecessor links form a tree of shortest routes back to the start. Each cell stores at most one parent: the neighbor from which it was first discovered.

Path reconstruction

From the exit, follow predecessor pointers until you reach null at the start, then reverse the list. Drawing connects cell centers along that list. For polar mazes the segments follow ring and sector geometry; for hex mazes they connect true neighbor centers. A valid overlay never crosses a closed wall because every consecutive pair is a graph edge.

Shortest paths and equal weights

BFS finds shortest paths when every edge has the same traversal cost. Maze Foundry passages are unweighted. If someday a puzzle assigned mud to one corridor and pavement to another, you would need Dijkstra’s algorithm or another weighted method. This solver does not claim to solve every weighted shortest-path problem.

Mazes containing loops

Loops give BFS something to choose among. Independent cycle count E − V + 1 tells you whether choices exist. Braided generators add those extra edges on purpose. The shortest path may skip a tempting long corridor that DFS would have followed. Hints always take the next cell on a BFS path from the player, not a random open neighbor.

Common solving mistakes

Drawing a solution by eye and calling it shortest. Using array graphics instead of edges. Shifting a JavaScript array at index zero on huge mazes until the tab stutters, then blaming the algorithm. Announcing every animation frame to a screen reader. Claiming wall-following always works. Forgetting that undo still traverses an edge, so efficiency uses real move count including backward steps.

Worked BFS example

Cells: S connected to A and B. A connected to C. B connected to C and E (exit). C connected to E.

Queue starts [S]. Visit A and B. Queue [A, B]. From A discover C (pred C = A). From B discover E (pred E = B). Stop.

Reconstruct E ← B ← S. Path S-B-E has length 2. The route S-A-C-E has length 3 and is longer even though it looks “deeper”. Visited count may be 5 even though the path has 3 cells.

Grid showing a start circle, frontier cells, and an exit square with a shortest polyline

Try this on the maze solver with a braided maze so loops exist. Press Solve maze, then Hide solution, then Animate solution. Compare the reported length with a manual walk on the play page.

Second example: start a game, walk two wrong cells, request a hint. The hint should be the first step of BFS from the current cell, which might backtrack toward the true corridor. That is correct behavior, not a bug. Efficiency after finishing can be well below 100% because those extra edges counted.

FAQ

Why not Dijkstra’s algorithm?
All passages cost one move. BFS already returns a shortest path and is simpler.
Does showing the solution change the maze?
No. The overlay reads the graph; it does not add edges.
What if no path exists?
Validation rejects that maze during generation. A failure status would display rather than a fake line.

See also generation algorithms and what makes mazes feel hard.