1. Home
  2. Design & Analysis of Algorithms
  3. Graph Traversal — BFS & DFS

Graph Traversal — BFS & DFS

Two ways to explore every node of a graph. BFS spreads out like ripples using a queue; DFS dives deep like a maze explorer using a stack. Compare them on a real 3D graph.

Interactive 3DIntermediate14 min readDAAUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A graph is a set of nodes connected by edges. Traversal means visiting every node exactly once. BFS spreads out like ripples in water; DFS goes deep like exploring a maze. Choose one and press Run.

Pseudocode

  1. procedure BFS(start):
  2. mark start; queue ← [start]
  3. while queue is not empty:
  4. u ← dequeue() // oldest first
  5. visit(u)
  6. for each neighbour v of u:
  7. if v is not marked:
  8. mark v; enqueue(v)
Read this model as text · 33 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A graph is a set of nodes connected by edges. Traversal means visiting every node exactly once. BFS spreads out like ripples in water; DFS goes deep like exploring a maze. Choose one and press Run.
  2. Start at A: mark it and put it in the queue.
  3. Dequeue A (the oldest item in the queue) and visit it.
  4. Neighbour B has not been seen → mark it (level 1) and enqueue it.
  5. Neighbour C has not been seen → mark it (level 1) and enqueue it.
  6. Neighbour D has not been seen → mark it (level 1) and enqueue it.
  7. Dequeue B (the oldest item in the queue) and visit it.
  8. Neighbour A is already marked → skip it.
  9. Neighbour E has not been seen → mark it (level 2) and enqueue it.
  10. Neighbour F has not been seen → mark it (level 2) and enqueue it.
  11. Dequeue C (the oldest item in the queue) and visit it.
  12. Neighbour A is already marked → skip it.
  13. Neighbour F is already marked → skip it.
  14. Neighbour G has not been seen → mark it (level 2) and enqueue it.
  15. Dequeue D (the oldest item in the queue) and visit it.
  16. Neighbour A is already marked → skip it.
  17. Neighbour G is already marked → skip it.
  18. Neighbour H has not been seen → mark it (level 2) and enqueue it.
  19. Dequeue E (the oldest item in the queue) and visit it.
  20. Neighbour B is already marked → skip it.
  21. Neighbour F is already marked → skip it.
  22. Dequeue F (the oldest item in the queue) and visit it.
  23. Neighbour B is already marked → skip it.
  24. Neighbour C is already marked → skip it.
  25. Neighbour E is already marked → skip it.
  26. Dequeue G (the oldest item in the queue) and visit it.
  27. Neighbour C is already marked → skip it.
  28. Neighbour D is already marked → skip it.
  29. Neighbour H is already marked → skip it.
  30. Dequeue H (the oldest item in the queue) and visit it.
  31. Neighbour D is already marked → skip it.
  32. Neighbour G is already marked → skip it.
  33. BFS finished. Visit order: A → B → C → D → E → F → G → H. BFS explores level by level — the "level" of each node is its shortest distance (in edges) from A. The orange edges form the BFS tree.

Try this in the 3D model

  • Run BFS from A and read the "level" labels. They are the shortest number of edges from A.
  • Switch to DFS with the same start and compare the visit order.
  • Load the Cube graph and run BFS from A. Which corner is visited last? Why?
  • Watch the queue/stack row at the front — the whole difference between BFS and DFS is which end items are taken from.

What is a graph?

A graph is a set of vertices (nodes) connected by edges (links). Unlike a tree, a graph can have cycles and any node can connect to any other.

Graphs are everywhere: cities and roads, people and friendships, web pages and links, computers in a network.

Traversing a graph means visiting every reachable node exactly once. There are two classic ways to do it.

Breadth-First Search (BFS) — ripples in water

BFS explores in rings: first the start node, then all its neighbours (distance 1), then all their new neighbours (distance 2), and so on.

It uses a queue (first in, first out):

  1. Mark the start node and enqueue it.
  2. Dequeue the oldest node u and visit it.
  3. For each neighbour v of u that isn’t marked yet: mark it and enqueue it.
  4. Repeat until the queue is empty.

Because nodes are processed in the order they were discovered, BFS finds the shortest path in edges from the start to every node. In the 3D model, the “level” tag on each node is exactly that distance. On a tree, BFS is the same as level-order traversal.

Analogy: spreading news. You tell all your friends first; then each of them tells their friends; and so on outward.

Depth-First Search (DFS) — exploring a maze

DFS follows one path as far as possible, and only when it hits a dead end does it backtrack to the last place it had a choice.

It uses a stack (last in, first out) — or simply recursion, which uses the call stack:

  1. Push the start node.
  2. Pop the newest node u. If already visited, skip it. Otherwise visit it.
  3. Push all unvisited neighbours of u.
  4. Repeat until the stack is empty.

Analogy: exploring a maze by always taking the next corridor until you hit a wall, then walking back to the last junction.

BFS vs DFS — the only real difference

Look at the row of boxes in front of the graph. Both algorithms keep a list of “nodes to visit later”. BFS takes from the front (oldest first), DFS takes from the top (newest first). That one choice produces completely different exploration orders.

BFS DFS
Data structure Queue Stack / recursion
Explores Level by level Deep along one path first
Shortest path (unweighted) ✅ Yes ❌ No
Memory Can be large for wide graphs Proportional to the path depth
Typical uses Shortest path, level order, nearest friends Cycle detection, topological sort, maze solving, connected components

Code

from collections import deque

graph = {
    'A': ['B', 'C', 'D'], 'B': ['A', 'E', 'F'], 'C': ['A', 'F', 'G'],
    'D': ['A', 'G', 'H'], 'E': ['B', 'F'],      'F': ['B', 'C', 'E'],
    'G': ['C', 'D', 'H'], 'H': ['D', 'G'],
}

def bfs(start):
    order, seen = [], {start}
    queue = deque([start])
    while queue:
        u = queue.popleft()                 # oldest first
        order.append(u)
        for v in graph[u]:
            if v not in seen:
                seen.add(v)
                queue.append(v)
    return order

def dfs(start):
    order, visited = [], set()
    stack = [start]
    while stack:
        u = stack.pop()                     # newest first
        if u in visited:
            continue
        visited.add(u)
        order.append(u)
        for v in reversed(graph[u]):
            if v not in visited:
                stack.append(v)
    return order

print(bfs('A'))   # ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
print(dfs('A'))   # ['A', 'B', 'E', 'F', 'C', 'G', 'D', 'H']
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector<vector<int>> adj;           // adjacency list
vector<bool> visited;

void dfs(int u) {                  // recursive DFS
    visited[u] = true;
    cout << char('A' + u) << " ";
    for (int v : adj[u])
        if (!visited[v]) dfs(v);
}

void bfs(int start) {
    vector<bool> seen(adj.size(), false);
    queue<int> q;
    seen[start] = true;
    q.push(start);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        cout << char('A' + u) << " ";
        for (int v : adj[u])
            if (!seen[v]) { seen[v] = true; q.push(v); }
    }
}

int main() {
    adj = {{1,2,3}, {0,4,5}, {0,5,6}, {0,6,7}, {1,5}, {1,2,4}, {2,3,7}, {3,6}};
    bfs(0); cout << "\n";          // A B C D E F G H
    visited.assign(adj.size(), false);
    dfs(0); cout << "\n";          // A B E F C G D H
}

Where are BFS and DFS used?

  • GPS / games: BFS finds the fewest moves in a grid or maze.
  • Social networks: “people you may know” = friends at distance 2 (BFS).
  • Web crawlers explore links with BFS.
  • Compilers and build tools order tasks with a DFS-based topological sort.
  • Detecting cycles and finding connected components with DFS.
  • Solving puzzles like Sudoku with DFS + backtracking.

Common mistakes

  • Forgetting the visited/marked set — the program loops forever on a cycle.
  • In BFS, marking a node when it is dequeued instead of when it is enqueued, which lets the same node enter the queue many times.
  • Assuming DFS gives shortest paths. It doesn’t.

Complexity at a glance

Case / operationTimeWhy
BFS / DFS with an adjacency listO(V + E)Every vertex is visited once and every edge checked at most twice.
BFS / DFS with an adjacency matrixO(V²)Finding neighbours means scanning a whole row.
Extra spaceO(V)

Quick check

Test yourself — pick an answer to see if you got it.

1. Which data structure does BFS use to decide what to visit next?

2. In an unweighted graph, which traversal finds the shortest path (fewest edges) from the start?

3. What is the time complexity of BFS on a graph with V vertices and E edges stored as an adjacency list?

4. Why do we mark nodes as visited/discovered?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Graph Traversal — BFS & DFS. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.