Patterns
GB · Pattern 16
Graph BFS / DFS
Traverse graphs using BFS (shortest path, level-by-level) or DFS (connected components, path existence). Always track visited nodes to avoid cycles.
- Time
- O(V + E)
- Space
- O(V)
Spot it when
- Count connected components / islands
- Shortest path in unweighted graph (BFS)
- Flood fill, bipartite check
- Multi-source BFS (rotting oranges)
Problems
Easy to hard · 0/13 solved
#200Number of IslandsMedium#133Clone GraphMedium#417Pacific Atlantic Water FlowMedium#994Rotting OrangesMedium#286Walls and GatesMedium#127Word LadderHard#684Redundant ConnectionMedium#721Accounts MergeMedium#1192Critical Connections in a NetworkHard#332Reconstruct ItineraryHard#778Swim in Rising WaterHard#323Number of Connected Components in Undirected GraphMedium#130Surrounded RegionsMedium