Skip to main content
GraphsBFSDFS

Graph Problems Cheat Sheet: BFS vs DFS vs Union-Find

· 5 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Not sure which graph algorithm to reach for? This cheat sheet maps problem types to the right tool in under two minutes.

Use BFS when…

The problem asks for shortest path in an unweighted graph, level-by-level traversal, minimum number of steps, or multi-source spread (like Rotting Oranges). BFS guarantees the shortest path because it explores neighbors layer by layer. The queue naturally enforces this order.

Use DFS when…

The problem asks whether a path exists, asks you to explore all possibilities (backtracking), involves topological sort, or requires detecting cycles. DFS is also simpler to implement recursively for tree problems and uses less memory when the graph is wide and shallow.

Use Union-Find when…

The problem involves repeatedly merging groups (accounts merge, satisfiability of equality equations) or detecting whether adding an edge creates a cycle (redundant connection). Union-Find with path compression and union by rank gives near-O(1) operations — far faster than running BFS/DFS repeatedly.

Quick reference table

Shortest path (unweighted) → BFS. Shortest path (weighted, non-negative) → Dijkstra (min-heap + BFS). Cycle detection (undirected) → Union-Find. Cycle detection (directed) → DFS with 3-colour states. Connected components → BFS/DFS or Union-Find. Topological order → Kahn's BFS or DFS post-order. All paths / backtracking → DFS.

Frequently Asked Questions

What is the difference between BFS and DFS in graphs?
BFS (Breadth-First Search) uses a queue and explores all neighbors at the current depth before going deeper — it guarantees the shortest path in unweighted graphs. DFS (Depth-First Search) uses a stack (or recursion) and goes as deep as possible before backtracking. Use BFS for shortest path; use DFS for path existence, cycle detection, and topological sort.
What is Union-Find and when should I use it?
Union-Find (Disjoint Set Union) is a data structure that tracks which elements belong to the same connected component. It supports two operations: find (which component does this element belong to?) and union (merge two components). Use it when you need to repeatedly merge groups or check connectivity, especially when edges are added dynamically. It is faster than BFS/DFS for these operations.
How do you detect a cycle in a graph?
For undirected graphs: use Union-Find (if adding an edge connects two nodes already in the same component, it forms a cycle) or DFS with a parent tracker. For directed graphs: use DFS with three-color marking (white=unvisited, gray=in current path, black=done). If you encounter a gray node during DFS, there is a back edge and thus a cycle.
What graph problems appear most in FAANG interviews?
The most frequently asked graph problems are: Number of Islands (LC 200), Course Schedule I and II (LC 207, 210), Pacific Atlantic Water Flow (LC 417), Word Ladder (LC 127), Clone Graph (LC 133), Rotting Oranges (LC 994), and Network Delay Time (LC 743). Word Ladder is particularly common at Google. Course Schedule tests topological sort, which is a must-know for senior-level interviews.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee