Graphs Pattern Hub
11 problemsClone Graph
mediumMaintain `clones = { origNode: newNode }` *before* recursing into neighbors so cycles short-circuit immediately to the already allocated copy without infinite loops.
Course Schedule
mediumKahn’s algorithm: track `indegree[v]` and push all `0`-indegree vertices into a queue. Repeatedly pop `u`, decrement neighbors’ indegrees, enqueue newly zeroed vertices, and verify `visited == numCourses`.
Course Schedule II
mediumSame Kahn’s topological sort as Course Schedule, collecting each dequeued vertex into `order`. Return `order` iff `len(order) == numCourses`, else `[]`.
Graph Valid Tree
mediumGraph theorem: an undirected graph on `n` vertices is a tree iff `len(edges) == n - 1` AND union-find joins all edges without ever seeing an internal cycle (`find(u) == find(v)`).
Longest Increasing Path in a Matrix
hardStrictly increasing edges `matrix[nr][nc] > matrix[r][c]` guarantee the implicit graph is a **DAG** (no cycles are possible!). Therefore no `visited` set is needed — plain memoized DFS (`cache[r][c]`) computes every cell’s longest path once.
Max Area of Island
mediumDFS returns exact area contributed by a connected component (`1 + sum(4 neighbors)`), sinking `1 -> 0` upon arrival so each cell counts once.
Number of Connected Components in an Undirected Graph
mediumDisjoint Set Union (DSU) with path compression + union-by-rank/size: initialize `components = n`, decrement `components` on every successful union between distinct roots.
Number of Islands
mediumIterate every cell `(r, c)`. Whenever `grid[r][c] == "1"`, increment `islands` and immediately flood-fill (DFS/BFS) all 4-connected `"1"` cells to `"0"` in-place so each component is counted once.
Pacific Atlantic Water Flow
mediumReverse flow from the coastlines uphill (`nextHeight >= currHeight`) instead of running downhill DFS from every cell. Intersect the Pacific-reachable set (`pac`) with the Atlantic-reachable set (`atl`).
Rotting Oranges
mediumSeed one **multi-source BFS** queue simultaneously with ALL initially rotten cells (`2`) while counting `fresh` cells (`1`). Process level-by-level while `q and fresh > 0`, decrementing `fresh` on each rot.
Word Ladder
hardAvoid O(N^2 * L) pairwise comparisons by indexing dictionary words onto wildcard patterns (`h*t -> ["hot", "hit"]`), or run **bidirectional BFS** generating 26 char substitutions per position on the smaller frontier.