Trémaux's Algorithm maze algorithm

Trémaux's algorithm is the oldest guaranteed way out of any maze: mark every passage you walk, and never walk one more than twice.

How it works

Walk and mark each passage behind you. At a junction, take a passage you haven't marked. If you arrive at a junction you've seen before by a new passage, turn round and go back. If every way is marked, take the one marked once. Never take a passage marked twice.

It's depth-first search done on foot: it really walks back along corridors instead of jumping. It always finds the exit, loops or not, and the passages marked exactly once form a path from start to exit.

What to watch for

Watch it run down a corridor, hit a dead end and walk all the way back, its trail overwriting itself on the way.

Properties

Shortest path
No
Finds the exit
Always
Explores
Like DFS, walking every backtrack
Memory
Marks on the passages

Race it

Pick it in the race and see who reaches the exit first, or run it in the studio.

FAQ

Who was Trémaux?

Charles Pierre Trémaux, a 19th-century French engineer. His method was described by Édouard Lucas in 1882.

How is it different from the wall follower?

It marks where it's been, so it can't loop forever round an island the way a wall follower can.

All maze algorithms

Pro

and undefined are part of Pro

  • All 12 generators and 12 solvers
  • Hexagon, triangle and circle grids; heart, star and 6 more outlines
  • Giant mazes, up to 201×201, any width and height
  • Custom colours, 7 more themes, flood styles, clean no-label videos
  • Any speed from 0.25× to 4×, any amount of loops
  • 4 more scales and 3 more instruments
  • Titles on your videos, custom timing, any start and goal
  • Race and gauntlet videos, with or without the watermark