Knowledge Guide
HomeDSAGraphs

Graph Traversal — BFS vs DFS, Traced (and when to use which)

BFS and DFS: the two ways to visit a graph

Both visit every reachable node once (O(V+E)); they differ in order, and the order is what makes each suited to different problems. BFS spreads out level by level (a queue); DFS plunges deep then backtracks (a stack / recursion).

A small graph traversed two ways: BFS visits A then B C then D E by layers; DFS goes A B D deep then backtracks to C E
A small graph traversed two ways: BFS visits A then B C then D E by layers; DFS goes A B D deep then backtracks to C E

The two templates

// BFS — shortest path in an UNWEIGHTED graph, level order
Queue<Node> q = new ArrayDeque<>(); q.add(start);
Set<Node> seen = new HashSet<>(); seen.add(start);   // mark on ENQUEUE, not dequeue
while (!q.isEmpty()) {
    Node u = q.poll();
    for (Node v : adj(u)) if (seen.add(v)) q.add(v);
}

// DFS — recursive (paths, cycles, topological sort)
void dfs(Node u, Set<Node> seen) {
    seen.add(u);
    for (Node v : adj(u)) if (!seen.contains(v)) dfs(v, seen);
}

Which one, when

BFSDFS
Structurequeue, level by levelstack/recursion, deep first
Use forshortest path (unweighted), level/min-stepspaths, cycle detection, topological sort, connected components
MemoryO(width) — can be largeO(depth) — the call stack

Graph pattern family to recognise: shortest-unweighted → BFS · topo order / cycle → DFS (3-colour) or Kahn's (BFS indegree) · connectivity/islands → either + visited · weighted shortest → Dijkstra (a BFS with a priority queue).

Pitfalls

Takeaways


Re-authored for this guide; traversal diagram hand-authored as SVG. See also: Graphs, Trees, the Pattern Recognition index.

🤖 Don't fully get this? Learn it with Claude

Stuck on Graph Traversal — BFS vs DFS, Traced (and when to use which)? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Graph Traversal — BFS vs DFS, Traced (and when to use which)** (DSA) and want to truly understand it. Explain Graph Traversal — BFS vs DFS, Traced (and when to use which) from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Graph Traversal — BFS vs DFS, Traced (and when to use which)** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Graph Traversal — BFS vs DFS, Traced (and when to use which)** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Graph Traversal — BFS vs DFS, Traced (and when to use which)** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes