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
  • Terms of Service
  • Downloads
🏰The HTML Maze

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

TermsPrivacyAboutBlog
Home›Glossary›Breadth-First Search

🌊 What Is Breadth-First Search (BFS)?

Breadth-First Search (BFS) is a graph traversal algorithm that explores all nodes at the current distance before moving deeper. Picture dropping a pebble in water — waves ripple outward evenly in every direction. That's exactly how BFS spreads through a maze, making it the go-to algorithm when you need the shortest path.

⚙️ How It Works

  1. 1.Enqueue the starting cell and mark it as visited.
  2. 2.Dequeue the front cell — this is your current position.
  3. 3.Check if it's the exit. If yes, you're done — trace back the path.
  4. 4.Enqueue all unvisited, reachable neighbors and mark them as visited.
  5. 5.Repeat from step 2. Because the queue is FIFO, cells closer to the start are always processed first.
💡 Key insight: The FIFO (first-in-first-out) queue ensures BFS processes cells in order of their distance from the start. That's why it always finds the shortest path in unweighted graphs.

🎯 Why It Matters for Mazes

🏆 Shortest Path Guarantee

When you need the optimal route through a maze where every step costs the same, BFS is your best bet. Unlike DFS, which may wander down long corridors, BFS systematically finds the shortest solution. This makes it the benchmark for evaluating maze complexity.

📏 Measuring Difficulty

Game developers use BFS to measure how hard a maze is. The BFS solution length divided by the maze dimensions gives a "difficulty ratio." A higher ratio means more twisting, more wrong turns, and a harder puzzle. Our Classic Maze uses this metric to calibrate difficulty levels.

📊 Key Characteristics

Time ComplexityO(V + E) — visits each vertex and edge once
Space ComplexityO(V) — stores the entire frontier in a queue
Finds Shortest Path?✅ Yes (unweighted graphs)
Complete?✅ Yes
Data StructureQueue (FIFO)
Best ForShortest path in unweighted mazes, maze difficulty evaluation

🎮 Try It Yourself

Our maze solver uses BFS to show you the optimal path. Play a maze, then activate the solver to see BFS in action as it discovers the shortest route.

Try Maze Solver →📖 Explore All Algorithms

❓ Frequently Asked Questions

Does BFS always find the shortest path in a maze?

Yes — in unweighted mazes where every step costs the same. For weighted mazes, use Dijkstra's or A*.

Why does BFS use more memory than DFS?

BFS stores the entire frontier (all cells at the current distance) in a queue. In a wide-open area this can be huge. DFS only stores the current path, which is typically much smaller.

Is BFS used for maze generation?

Rarely on its own. BFS-based generation produces mazes with very short dead ends and a "bushy" structure. Most generators prefer recursive backtracking or Prim's algorithm.

📖 Related Terms

🔍
Depth-First Search (DFS)

Explore as deep as possible first

⭐
A* Algorithm

BFS + heuristic = faster optimal paths

📐
Dijkstra's Algorithm

BFS generalized for weighted graphs

📊
Maze Complexity

How BFS measures maze difficulty