ALGORITHMS · 05  /  BREADTH-FIRST SEARCH

The Flood.

Find your way through the maze by hand — then release a flood from the start that spreads out in rings. The moment it touches the exit, it has found the shortest path.

THE GIST · 20 SECONDS

Breadth-first search explores a graph level by level: every neighbour of the start first, then their neighbours, spreading out like water. It uses a FIFO queue, and because it reaches nodes in order of distance, the first time the flood touches a node is the shortest path there. On an unweighted maze, that's a guarantee — and it runs in O(V + E).

  • Frontierthe current ring being explored
  • QueueFIFO — first in, first out
  • Visitedseen already — never re-add
  • Layerall cells at the same distance

Find your own way  →  Release the flood  →  Race BFS vs DFS

start exit wall flooded (by distance) shortest path

The flood spreads one ring at a time, so cells are reached in distance order. That ordering is exactly why the first time BFS touches the exit, it's the shortest way there.

UNDER THE HOOD

What you just played, written down

You walked it, watched the flood spread in rings, and saw DFS dive past the shortest path. Here's the same thing as code.

How the Tide thinks — five steps

  1. Seed the queue. Put the start in a FIFO queue; mark it visited at distance 0.
  2. Dequeue the front. Take the oldest cell — the closest unprocessed one.
  3. Visit neighbours. For each unvisited neighbour, mark it visited, set its distance to current + 1, record where it came from, and enqueue it.
  4. Repeat until the queue is empty (or you dequeue the goal).
  5. Rebuild the path. Walk the "came-from" links back from the goal to the start.
WHY IT FINDS THE SHORTEST PATH

A FIFO queue drains cells in the order they were added — which is distance order. So the first time a cell is reached, no shorter path can exist. This holds only when every step costs the same (unweighted); add weights and you need Dijkstra.

The algorithm

function bfs(graph, start, goal):
    queue = [start]                  # FIFO
    visited = {start}
    dist[start] = 0
    while queue not empty:
        u = queue.dequeue()          # oldest first
        if u == goal: break
        for v in neighbours(u):
            if v not in visited:
                visited.add(v)
                dist[v] = dist[u] + 1
                prev[v] = u
                queue.enqueue(v)
    return rebuild(prev, goal)
TIMEO(V + E)every node + edge once
SPACEO(V)queue + visited set

Solved by hand — the layers for the maze above

Each ring the flood reaches, how far it is, and how many new cells join it.

↔ BFS vs DFS

BFS (queue) explores level by level and finds the shortest path. DFS (stack) dives deep first — great for exhaustive search, cycles and topological sort, but it does not guarantee the shortest path.

↔ BFS vs Dijkstra

BFS is just Dijkstra with every edge weight = 1. The moment steps have different costs, swap the FIFO queue for a priority queue and you have Dijkstra.

★ Where it's used

Shortest paths on unweighted maps, "people within N connections", web crawling, fewest-moves puzzle solvers, and flood-fill in paint tools.

QUICK CHECK

Did it stick?

FAQ

Breadth-first search, answered

What is breadth-first search (BFS)?

BFS explores a graph level by level from the start, using a FIFO queue. Because it reaches cells in distance order, the first time it touches a cell is the shortest path there (on an unweighted graph).

What is the time complexity of BFS?

O(V + E) — every vertex and edge is examined once. Space is O(V) for the queue and visited set; on an R×C grid that's O(R×C).

Why does BFS find the shortest path?

It expands cells in order of distance — all at distance 1, then 2, and so on. So the first time a cell is reached, no shorter route can exist. This holds only on unweighted graphs; weighted graphs need Dijkstra.

What is the difference between BFS and DFS?

BFS (queue) goes level by level and finds the shortest path. DFS (stack/recursion) dives deep first — good for exhaustive search, cycle detection and topological sort, but it doesn't guarantee the shortest path.

BFS vs Dijkstra's algorithm?

BFS finds the fewest-edges path on an unweighted graph with a FIFO queue. Dijkstra finds the lowest-weight path with a priority queue. BFS is the special case of Dijkstra where all edge weights are 1.

Where is BFS used in real life?

Shortest paths on unweighted maps, "people within N connections", web crawling, fewest-moves puzzle solvers, and flood-fill in paint tools.

RUN CARD

Next → give every street a different cost and the flood becomes a weighted search: that's Dijkstra.

Finished this one? 0 / 5 Algorithms done

More Algorithms