Pattern Recognition Drill
Easy Graphs
The Problem
Count the number of connected components in an undirected graph.
What approach would you use?
Think about it before scrolling down.
For each unvisited node, run DFS to mark its component. Count the number of DFS calls. O(V+E).
BFS works identically — just a different traversal strategy for the same counting approach.
Common Trap
Don't forget disconnected nodes — iterate over ALL nodes, not just starting from node 0.