πΈοΈ Graph
Graphs unify trees, grids, social networks, and dependency systems. The two workhorses are BFS and DFS; everything else (shortest path, MST, SCC) is a variation.
This category contains 21 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.
π§ Key Patternsβ
- BFS β Shortest path in unweighted graph. Level-by-level traversal.
- DFS β Connectivity, cycle detection, topological order, path enumeration.
- Topological Sort β DAG dependency resolution: Kahn (BFS) or DFS post-order.
- Union-Find (DSU) β Dynamic connectivity, MST (Kruskal), grouping.
- Dijkstra / Bellman-Ford β Shortest path with non-negative / negative weights.
- Bipartite Check β Two-color BFS. Equivalent to: no odd cycle.
- Grid-as-Graph β Islands, flood fill, walls-and-gates. 4 or 8 directions.
β οΈ Common Pitfallsβ
- Forgetting to mark visited before enqueueing β exponential blowup.
- Using Dijkstra on graphs with negative weights β undefined behavior.
- Union-find without path compression OR union-by-rank is per op.
π Study Resourcesβ
πΊ Videosβ
- William Fiset β Graph Theory Full Course
- NeetCode β Graphs playlist
- Errichto β Dijkstra explained
π Booksβ
- Introduction to Algorithms (CLRS) β Ch. 22 (Elementary), Ch. 23β26 (MST, SSSP, APSP)
- Algorithm Design β Kleinberg & Tardos β Ch. 3
π Articles & Referencesβ
π» All Graph Problemsβ
Clone Graph
LeetCode 133 | Difficulty: Medium
Count Sub Islands
LeetCode 2035 | Difficulty: Medium
Course Schedule
LeetCode 207 | Difficulty: Medium
Course Schedule II
LeetCode 210 | Difficulty: Medium
Find if Path Exists in Graph
LeetCode 2121 | Difficulty: Easy
Find the Town Judge
LeetCode 1039 | Difficulty: Easy
Flood Fill
LeetCode 733 | Difficulty: Easy
Graph Valid Tree
LeetCode Link
Island Problems
A collection of classic grid/matrix traversal problems using DFS/BFS. These problems share a common pattern of exploring connected components in a 2D grid.
Keys and Rooms
LeetCode 871 | Difficulty: Medium
Max Area of Island
LeetCode 695 | Difficulty: Medium
Maximal Network Rank
LeetCode 1738 | Difficulty: Medium
Number of Closed Islands
LeetCode 1380 | Difficulty: Medium
Number of Connected Components in an Undirected Graph
LeetCode Link
Number of Distinct Islands
LeetCode Link
Number of Islands
LeetCode 200 | Difficulty: Medium
Number of Provinces
LeetCode 547 | Difficulty: Medium
Open the Lock
LeetCode 753 | Difficulty: Medium
Reconstruct Itinerary
LeetCode 332 | Difficulty: Hard
The Maze
LeetCode Link
Word Ladder
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words such that: