Skip to content
Optimal Lab

DSA · interactive

BFS vs DFS on the same graph

Breadth-first search takes the oldest node from a queue, so it spreads out ring by ring. Depth-first search takes the newest from a stack, so it dives down one branch first. Run both side by side.

Both start at A and look for G. Same graph, same neighbours. Only the frontier differs: a queue or a stack.

Step 0 of 9

Breadth-first (queue)

0 visited
ABCDEFGHI

queue (front →): [A]

order: —

Depth-first (stack)

0 visited
ABCDEFGHI

stack (→ top): [A]

order: —

  1. 1

    BFS takes from the front of a queue (oldest first). DFS takes from the top of a stack (newest first). That single choice is the whole difference.

    BFS → — · DFS → —

  2. 2

  3. 3

  4. 4

In production

Tools and technologies

Graph search runs inside route planners, web crawlers, dependency resolvers and garbage collectors.

Not tied to one language — use these names when you search docs or design a system.