BFS vs. DFS (Graphs)

Compare the two fundamental graph traversal algorithms: Breadth-First Search and Depth-First Search. See how they explore the same graph differently using Queues and Stacks.

Graph Search

BFS and DFS explore the same graph with different priorities

Breadth-first search and depth-first search both start from one node and work outward through a graph. The big difference is what they do next: BFS explores the closest frontier first, while DFS keeps following one branch until it has to backtrack.

How to read the demo
First pick a start node, then watch the frontier structure change.

The current node and discovered nodes are highlighted directly on the graph.

The side structure shows the active queue for BFS or stack for DFS, which explains why the visit order changes.

The comparison mode lets you watch both patterns side by side.

What to try in the demo
Use the same start node and compare the order produced by each traversal.

Start from one node and run BFS to see the search expand level by level.

Reset, then run DFS from the same node to watch it go deep down one branch first.

Use Comparison mode when you want both traversals visible at the same time.

Compare Graph Traversal strategies on the same topology.

Select a start node on any graph to begin
Breadth-First Search
ABCDEFGH

Queue (FIFO)

Nodes waiting to be visited next.

Empty

Visit Order

Breadth-first visit sequence.

Depth-First Search
ABCDEFGH

Stack (LIFO)

Nodes waiting while DFS explores deeper.

Empty

Visit Order

Depth-first visit sequence.

BFS Intuition

BFS explores level-by-level using a Queue. It is guaranteed to find the shortest path in unweighted graphs.

DFS Intuition

DFS dives deep into a branch using a Stack before backtracking. It explores one path until a dead end before returning.

BFS vs DFS

Queue-first and stack-first searches feel different immediately

These traversals are often taught together because they solve related graph tasks, but they prioritize different information while the search is running.

Breadth-first search
Best when you care about levels or shortest unweighted paths.

Explores all nodes at distance 1 before distance 2, then distance 3.

A natural fit for queue-based frontier expansion.

Depth-first search
Best when you want to explore structure deeply before trying alternatives.

Pushes deeper into one branch, then backtracks when needed.

A natural fit for recursion, stacks, and path-style exploration.

AspectBFSDFS
Main data structureQueueStack or recursion
Exploration patternVisits neighbors layer by layer.Dives down one path before backtracking.
Shortest-path intuitionFinds shortest path by edge count in an unweighted graph.Does not guarantee the shortest path.
Best mental modelA wave spreading outward from the start node.A path explorer going as deep as possible first.
Key Takeaways

The short version

  • BFS and DFS can visit the same graph in very different orders.
  • BFS uses a queue and explores one layer at a time.
  • DFS uses a stack-like process and explores one branch deeply before backtracking.
  • The right traversal depends on whether you care more about levels or deep path exploration.