Graphs: Graph traversal, topological order, shortest paths, and spanning trees
Goal: Learn the pattern first, then apply it to the 16 roadmap problems in this chapter.
When this pattern appears
Relationships form a network; connectivity, prerequisites, transformations, or weighted routes are involved.
The reusable idea
Choose BFS for unweighted shortest paths, DFS for reachability/cycles, Dijkstra for nonnegative weights, and DSU for incremental connectivity.
A deliberate problem-solving routine
- Write the input and output in plain language. Identify whether order, duplicates, or mutation matter.
- Work a tiny example by hand; label the state you wish you had at each step.
- Choose the structure or invariant that stores that state.
- Establish a brute-force baseline, then show exactly which repeated work the pattern removes.
- State the invariant before the loop, after each iteration, and at termination.
- Test smallest, boundary, duplicate, and adversarial cases.
Watch the animated pattern
The diagram above this lesson animates the core state changes for this chapter. Use pause and step to predict what moves next. The same invariant should explain each application, even when the problem story changes.
Questions to ask yourself
What does a node/edge represent? Which algorithm matches the objective and edge weights?
Common failure modes
Directed edge meaning, visited timing, multiple sources, cycles, negative weights, and path constraints.
Chapter problems from the workbook
- #85 · Number of Islands — Day 50, Medium. Open problem ↗
- #86 · Max Area of Island — Day 51, Medium. Open problem ↗
- #87 · Clone Graph — Day 51, Medium. Open problem ↗
- #88 · Number of Provinces — Day 52, Medium. Open problem ↗
- #89 · Rotting Oranges — Day 52, Medium. Open problem ↗
- #90 · Pacific Atlantic Water Flow — Day 53, Medium. Open problem ↗
- #91 · Surrounded Regions — Day 53, Medium. Open problem ↗
- #92 · Course Schedule — Day 54, Medium. Open problem ↗
- #93 · Course Schedule II — Day 54, Medium. Open problem ↗
- #94 · Redundant Connection — Day 55, Medium. Open problem ↗
- #95 · Word Ladder — Day 55, Hard. Open problem ↗
- #96 · Network Delay Time — Day 57, Medium. Open problem ↗
- #97 · Min Cost to Connect All Points — Day 57, Medium. Open problem ↗
- #98 · Cheapest Flights Within K Stops — Day 58, Medium. Open problem ↗
- #99 · Swim in Rising Water — Day 58, Hard. Open problem ↗
- #100 · Reconstruct Itinerary — Day 59, Hard. Open problem ↗
Interview explanation
A concise explanation has four parts: the key observation, the maintained invariant, why each step preserves it, and the time/space complexity. Avoid naming a pattern as a substitute for proving the step is safe.