A maze is just a graph in disguise — cells are nodes, open passages are edges. A perfect maze has exactly one path between any two cells: no loops, no isolated pockets. Mathematically that's a spanning tree of the grid. Below, watch three different algorithms carve one — and see how the choice of algorithm gives the maze its own distinctive texture — then let a solver find the single true path home.
Every cell starts walled off. The algorithm knocks down walls one at a time, but never in a way that would create a loop — so the open passages always form a tree. The orange cell is the frontier the algorithm is working on right now.
Because the maze is a spanning tree, exactly one route connects the start (top-left) to the finish (bottom-right). A breadth-first flood-fill spreads out from the start; the first time it touches the finish, it has found the shortest — and only — path. Watch the flood wash across, the solution light up, then every dead-end branch fade away.
Lay a grid of cells and connect each to its neighbours: that's the grid graph. A maze is a subset of those connections (the open passages). Two properties make a maze "perfect":
A connected, acyclic graph on N nodes is exactly a spanning tree: it has precisely N − 1 edges, and there is a unique path between any pair of nodes. That uniqueness is why the solver in scene 2 always finds one and only one answer.
The three generators all build a spanning tree, but with different bias:
Once you have the graph, finding a route is a pathfinding problem — the same search that powers game AI and GPS. The flood-fill here is BFS; for weighted or large grids you'd reach for A*. See the companion explainer: A* Pathfinding →.