LeetCode · Graphs

Graphs Problem Set

BFS, DFS, multi-source BFS, topological sort, union-find, and shortest path. Graph problems are about traversal strategy and state tracking.

Concept pages: Graph Basics · BFS · DFS · Topological Sort · Shortest Path
DifficultyPatternProblemKey Insight
MediumBFS/DFSLC 200 · Number of IslandsFlood-fill connected components on grid. O(m·n).
Mediummulti-source BFSLC 994 · Rotting OrangesStart from every rotten orange at once. Each BFS layer is one minute of spread.
Mediummulti-source BFSLC 542 · 01 MatrixReverse the search: BFS from all 0-cells to fill the nearest-zero distance for every 1-cell.
MediumDFSLC 133 · Clone GraphDFS/BFS with HashMap old→new. O(V+E).
MediumBFS/DFSLC 695 · Max Area of IslandDFS counting cells per component. O(m·n).
MediumBFS/DFSLC 417 · Pacific Atlantic Water FlowBFS/DFS from both oceans, intersect reachable sets. O(m·n).
Mediumtopo-sortLC 207 · Course ScheduleCycle detection via topological sort (Kahn's or DFS). O(V+E).
Mediumtopo-sortLC 210 · Course Schedule IITopological ordering via BFS (Kahn's). O(V+E).
Mediumunion-findLC 323 · Number of Connected ComponentsUnion-Find or DFS on adjacency list. O(V+E). (Premium)
Mediumunion-findLC 684 · Redundant ConnectionUnion-Find: first edge that creates a cycle. O(n · α(n)).
Mediumshortest-pathLC 743 · Network Delay TimeDijkstra from source, return max distance. O((V+E) log V).
HardBFSLC 127 · Word LadderBFS on word graph with wildcard patterns. O(m²·n).

← Back to all LeetCode categories