BFS vs DFS Graph Traversal Visualizer — Free Side-by-Side Algorithm Demo

BFS vs DFS Graph Traversal Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Side-by-side execution
Same graph, same start, same neighbour order.
Live queue and stack
The frontier shown as it changes.
Visit order badges
Numbered nodes in each pane.
Search tree edges
Which edge first reached each node.
Recursive-equivalent DFS
Reverse push order matches recursion.
Duplicate-safe
BFS marks on enqueue; DFS skips stale pops.
Step log
What was visited and what was added.
No libraries
SVG and a few dozen lines of JS.

About this UI Snippet

BFS vs DFS — The Same Algorithm With a Queue or a Stack

Screenshot of the BFS vs DFS Graph Traversal Visualizer snippet rendered live

Breadth-first search and depth-first search are usually taught as two separate algorithms. They are really one algorithm — "take a node from the frontier, add its unvisited neighbours" — with a different container for the frontier. BFS uses a queue and spreads out level by level; DFS uses a stack and dives down one branch before backing up. Running both side by side on the same graph makes that single difference visible.

Fair comparison

Both searches start at A and visit neighbours in the same alphabetical order. Any difference in the result comes from the queue versus the stack.

BFS: first in, first out

BFS takes the oldest node from the front of the queue and appends new neighbours to the back. Nodes are marked as seen when they are enqueued, so no node enters the queue twice. The result visits A, then everything one edge away, then everything two edges away. In an unweighted graph, the tree edges it follows give the shortest path (fewest edges) from A to every node.

DFS: last in, first out

DFS pops the newest node from the top of the stack. Neighbours are pushed in reverse order so the first listed neighbour ends up on top and is visited next, matching what a recursive DFS would do. A node can be pushed more than once through different routes; stale entries are skipped when popped. The result follows one path as deep as possible before backtracking.

A subtle DFS bug this avoids

Because a node can sit on the stack several times, the edge that actually reached it is only known when it is *popped*, not when it was first pushed. Each frontier entry therefore stores { n, from }, and the search-tree edge is recorded on visit. Recording it at push time — a common shortcut that is harmless for BFS — draws the wrong DFS tree.

Reading the panes

Numbered badges show the order each node was visited, green edges show the search tree (which edge first reached each node), and the queue and stack are shown as they change.

Where each is used

BFS: shortest paths in unweighted graphs, "degrees of separation", level-order traversal. DFS: cycle detection, topological sorting, maze generation, and exploring every configuration in puzzles.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this visualizer into an AI assistant like Claude and ask it to explain, step by step, why BFS and DFS diverge after the first two visits. Ask it to add a target node with early exit, show the BFS distance levels as rings, add a recursive DFS with a call-stack display, or generate random graphs. It can also build exercises: hide the order badges and ask you to predict them.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a side-by-side BFS vs DFS traversal visualizer in plain HTML, CSS and JavaScript with SVG graphs.

Requirements:
- One undirected graph of nine nodes with fixed positions and adjacency lists in alphabetical order, drawn identically in two panes.
- Both searches start at A and visit neighbours in the same order.
- BFS uses a queue, marking nodes as seen when enqueued.
- DFS uses an explicit stack, pushing neighbours in reverse order so it matches recursive DFS, and skipping nodes already visited when they are popped.
- A "Step both" button that advances each search by one visit, plus Run/Pause and Reset.
- In each pane show the current node, visited nodes with numbered order badges, frontier nodes, the edges of the search tree, the live queue or stack as chips, the visit order so far and a one-line log of what was visited and added.
- When finished, show the complete visit order.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="bd">
  <div class="bd-top">
    <div>
      <h2>Breadth-first vs depth-first search</h2>
      <p>Same graph, same start, same neighbour order. The only difference is a <b>queue</b> versus a <b>stack</b>.</p>
    </div>
    <div class="bd-btns">
      <button type="button" id="bdStep">Step both</button>
      <button type="button" id="bdRun">Run</button>
      <button type="button" id="bdReset" class="ghost">Reset</button>
    </div>
  </div>
  <div class="bd-grid">
    <section class="bd-pane" id="paneBfs" aria-label="Breadth-first search"></section>
    <section class="bd-pane" id="paneDfs" aria-label="Depth-first search"></section>
  </div>
</div>

Step by step

How to Use

  1. 1
    Step bothEach click visits one node in each search.
  2. 2
    Watch the frontierBFS takes from the front of its queue; DFS from the top of its stack.
  3. 3
    Compare the orderNumbered badges show when each node was visited.
  4. 4
    Compare the treesGreen edges show how each search reached each node.
  5. 5
    Run or resetAuto-step with Run, start over with Reset.
  6. 6
    Edit the graphChange POS and ADJ to try other shapes.

Real-world uses

Common Use Cases

Algorithms courses
The clearest way to compare the two.
Interview practice
Trace both orders by hand, then check.
Game and maze development
Choose the right traversal for the job.
Web crawlers and social graphs
Understand level-by-level exploration.
Compiler and build tools
DFS underlies topological sort.
Related: Dijkstra's Shortest Path
Weighted graphs need more than BFS: Dijkstra's Shortest Path Visualizer.
Related: Stack vs Queue Visualizer
The two containers on their own: Stack vs Queue Visualizer.

Got questions?

Frequently Asked Questions

Both repeatedly take a node from a frontier and add its unvisited neighbours. BFS uses a first-in-first-out queue and explores level by level; DFS uses a last-in-first-out stack (or recursion) and follows one branch as deep as possible before backtracking.

BFS finds the path with the fewest edges in an unweighted graph. DFS does not guarantee a shortest path. For weighted graphs, use Dijkstra's algorithm or A*.

A stack returns the last item pushed first. Pushing neighbours in reverse puts the first neighbour on top, so the iterative version visits nodes in the same order as recursive DFS.

If nodes were only marked when dequeued, the same node could be added to the queue several times from different neighbours, wasting time and memory.

Both are O(V + E): every vertex is visited once and every edge is examined a constant number of times.