Week 12 — Foundational DP + Graphs Bridge
Pivot week. Two threads in one push:
- Foundational DP arc (p56–p61): the six canonical recurrences every senior must derive cold — House Robber, Coin Change, LIS (patience-sort O(n log n)), LCS, Edit Distance, Partition Equal Subset Sum. After this week, the vocabulary of DP is yours.
- Graphs bridge (p62–p65): the four “must-know-before-Month-3-Graphs-deep” algorithms — topological sort with order output (Course Schedule II), Dijkstra, Bellman-Ford / SSSP with edge constraint (Cheapest Flights K Stops), Union-Find (Number of Connected Components).
This is the biggest week in the track. Ten problems. Done deliberately — the reader who completes Week 12 has crossed from “pattern-aware” to “algorithm-fluent.”
Why these ten problems together?
DP and Graphs are taught as separate worlds in textbooks. They are not. Graphs are DP on a DAG. Topological sort is the evaluation order for a graph DP. Dijkstra is a DP over “shortest path so far,” relaxed via a priority queue. Bellman-Ford is the literal recurrence dist[v] = min(dist[u] + w(u,v)) evaluated V-1 times.
By interleaving the two threads in one week, you’ll see — by week’s end — that Cheapest Flights K Stops is just Coin Change on a graph (knapsack-flavored relaxation under a step bound), and LIS with patience-sort is a hidden binary-search application that lifts the curtain on why O(n log n) DPs exist at all.
Mental models you’ll internalize
- The DP recurrence ritual: state → transitions → base case → evaluation order → answer extraction. Apply it to every problem. Cold. In 60 seconds.
- Knapsack family (unbounded vs 0/1, weight as DP dimension): Coin Change is unbounded; Partition Equal Subset Sum is 0/1; both reduce to “fill a sack.”
- String-DP grid: LCS, Edit Distance —
dp[i][j]over two string prefixes; the diagonal/up/left transitions are the three universal moves. - Patience-sort LIS: the elegant O(n log n) algorithm that turns LIS into “binary search on tails.” Reveals that some DPs collapse into much faster data-structure-driven algorithms.
- Topological sort as DAG-DP evaluation order: Kahn’s BFS or DFS post-order. Both produce the order in which
dp[node]can be safely computed. - Dijkstra ≡ DP relaxation + greedy ordering: when all edge weights ≥ 0, the priority queue is “evaluate states in order of best-so-far distance.” Proof rests on the cut property.
- Bellman-Ford ≡ literal recurrence with bounded relaxation depth: the natural fit when you need “shortest path with at most K edges” — the K-stops constraint is the giveaway.
- Union-Find: amortized α(N) “merge connectivity” oracle. Connected components is the simplest application; Kruskal MST and Tarjan offline LCA are the deeper ones.
Problem table
| # | Problem | LC | Pattern | Day |
|---|---|---|---|---|
| p56 | House Robber | 198 | 1D DP, “include or skip” | Mon |
| p57 | Coin Change | 322 | Unbounded knapsack | Mon |
| p58 | Longest Increasing Subsequence | 300 | O(n²) DP + O(n log n) patience | Tue |
| p59 | Longest Common Subsequence | 1143 | 2D string DP | Tue |
| p60 | Edit Distance | 72 | 2D string DP, three transitions | Wed |
| p61 | Partition Equal Subset Sum | 416 | 0/1 knapsack reduction | Wed |
| p62 | Course Schedule II | 210 | Topological sort, Kahn + DFS | Thu |
| p63 | Network Delay Time | 743 | Dijkstra (binary heap) | Thu |
| p64 | Cheapest Flights Within K Stops | 787 | Bellman-Ford / BFS-with-state | Fri |
| p65 | Number of Connected Components | 323 | Union-Find (rank + path compression) | Fri |
Daily schedule
- Mon: p56 House Robber → p57 Coin Change. Internalize “include vs skip” and “unbounded knapsack” templates.
- Tue: p58 LIS (BOTH O(n²) and patience-sort O(n log n) — they’re the same problem; the patience-sort lens is non-obvious). Then p59 LCS as your first 2D string DP to anchor the grid pattern.
- Wed: p60 Edit Distance — the capstone of 2D string DP; three transitions = insert, delete, substitute. Then p61 Partition Equal Subset Sum — translates “subset sum = half-total” into a 0/1 knapsack.
- Thu: p62 Course Schedule II — topological order extraction with BOTH Kahn and DFS post-order. Then p63 Network Delay Time — your first Dijkstra; binary-heap relaxation; correctness from the cut property.
- Fri: p64 Cheapest Flights K Stops — Bellman-Ford or BFS-with-state; key insight: the K-edges-bound makes this NOT Dijkstra. Then p65 Number of Connected Components — Union-Find with rank + path compression.
Readiness gate
By Sunday night, you can answer cold (in under 60 seconds each):
- State the DP recurrence for Coin Change (unbounded) vs Partition Equal Subset Sum (0/1) without looking.
- Write
dp[i][j]for LCS and Edit Distance — state, transitions, base case, answer location. - Implement LIS in O(n log n) with
bisect_leftover the tails array; explain why patience-sort gives correct LIS length. - Distinguish when to use Dijkstra vs Bellman-Ford vs BFS-with-state. (Hint: edge weights, edge-count bound, negative weights.)
- Sketch Kahn’s algorithm AND the DFS-post-order variant of topological sort; explain why both yield valid orderings.
- Implement Union-Find with rank + path compression; prove (informally) why find is α(N) amortized.
If you can do all six cold, Month 3 Graphs Deep is open.