HTML Maze LogoHTML Maze
Play MazeGeneratorPrintableKidsBlog

Play Online

  • Classic Maze
  • Maze Runner
  • Circle Maze
  • Gravity Maze
  • Color Maze
  • Pac-Man
  • Daily Challenge

More Games

  • Hedge Maze
  • Hex Maze
  • Tilting Maze
  • Interactive Maze
  • JS Maze Game
  • Free Puzzles

Printable

  • All Printable Mazes
  • Kids Mazes
  • Easy Mazes
  • Hard Mazes
  • With Answers
  • Worksheets

Learn

  • Maze Algorithms
  • Glossary
  • How to Play
  • Blog
  • Strategy Guide
  • Maze Solver

About

  • About Us
  • Privacy Policy
  • Downloads
🏰The HTML Maze

© 2026 The HTML Maze. Free interactive browser puzzle game. No download required.

PrivacyAboutBlog
Home›Glossary›Depth-First Search

🔍 What Is Depth-First Search (DFS)?

Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. Imagine walking through a maze and always choosing to go deeper into an unexplored corridor rather than trying a different turn — that's DFS in action.

⚙️ How It Works

  1. 1.Start at the entrance cell. Mark it as visited.
  2. 2.Look at the current cell's unvisited neighbors.
  3. 3.Choose one neighbor (randomly for generation, or systematically for solving).
  4. 4.Move there and push the previous cell onto a stack.
  5. 5.Repeat until you reach the goal (solving) or have visited every cell (generation).
  6. 6.Backtrack by popping from the stack when you hit a dead end with no unvisited neighbors.

🎯 Why It Matters for Mazes

🏗️ Maze Generation

DFS is the engine behind recursive backtracking, the most popular maze generation algorithm. By randomly choosing neighbors, DFS carves long winding corridors that feel organic and challenging. Every cell is visited exactly once, producing a "perfect maze" — one with exactly one solution and no loops.

🔓 Maze Solving

As a solver, DFS works like the classic "keep your hand on the right wall" strategy. It will always find an exit if one exists, but it doesn't promise the shortest route. For shortest paths, use BFS or A*.

📊 Key Characteristics

Time ComplexityO(V + E) — visits each vertex and edge once
Space ComplexityO(h) — where h is the maximum depth of the recursion stack
Finds Shortest Path?❌ No (use BFS or A* instead)
Complete?✅ Yes (in finite graphs)
Data StructureStack (implicit via recursion, or explicit)
Best ForMaze generation, topological sort, cycle detection

🎮 Try It Yourself

Our Classic Maze is generated using DFS-based recursive backtracking. Play it and experience the long, winding corridors that DFS creates.

Play Classic Maze →📖 Explore All Algorithms

❓ Frequently Asked Questions

What is the difference between DFS and BFS in mazes?

DFS dives deep into one path before trying alternatives, while BFS explores all neighbors at the current distance first. BFS always finds the shortest path; DFS uses less memory and is the basis of recursive backtracking maze generation.

Does DFS find the shortest path through a maze?

No. DFS finds a path, but it may be longer than necessary. For the shortest path, use BFS (unweighted) or A* (weighted).

Why is DFS used for maze generation?

DFS creates long, winding corridors with few branches, producing mazes that feel natural and challenging. The recursive backtracking variant randomly chooses neighbors, guaranteeing every cell is reachable with exactly one solution.

📖 Related Terms

🌊
Breadth-First Search (BFS)

Level-by-level exploration for shortest paths

🔄
Recursive Backtracking

DFS-based maze generation algorithm

⭐
A* Algorithm

Heuristic-guided optimal pathfinding

🚫
Dead End

Where DFS triggers backtracking