Home
Day 15 · Week 3

Topological Sort + MST Consolidation

Levels 4–5 · Review  ·  2 hrs: ~40 min synthesis · ~80 min timed drills

🧠 The Big Picture

Week 3 gave you two greedy/ordering paradigms. Topological sort = linearize a DAG by dependencies (Kahn's peel-from-front, or DFS finish-time). MST = connect everything at minimum cost (Kruskal's global-greedy, or Prim's grow-a-tree). Today is about fast recognition — spotting which paradigm a disguised problem needs.

🧭 Recognition Flowchart

Signal in the problemAlgorithm
"Prerequisites," "must come before," "build/compile order," "dependencies"Topological sort (Kahn's or DFS)
"Circular dependency," "deadlock," "can everything finish?"Directed cycle detection (Kahn count / 3-color DFS)
"Minimum semesters / rounds," "longest chain in a DAG"Layered Kahn's / DP over topo order
"Connect all X at minimum total cost," "no redundant links"MST (Kruskal / Prim)
"Complete/geometric graph, connect points"MST — Prim O(V²) (avoid building all edges)
"Some connections free / pre-built"MST — union free edges first
"Minimize the maximum edge on a path"MST (bottleneck property)
"Which edges are essential to the MST?"Critical/pseudo-critical edge analysis

🎭 Master Variance Cheat-Sheet (Big Tech favorites)

Topological Sort family

VarianceKey adaptation
Just feasibility (cycle?)Kahn count == V, or 3-color DFS
Return the orderCollect output; empty if cycle
Unique order?Kahn's queue must hold ≤1 node at all times
Lexicographically smallestReplace queue with min-heap
Min rounds / parallel schedulingCount Kahn BFS layers (longest path)
Order inferred from data (alien dict)Extract edges from adjacent pairs first
Eventual safe / doomed nodesColor DFS or reverse-graph Kahn

MST family

VarianceKey adaptation
Plain min total costKruskal or Prim, return weight
Dense/complete graphPrim O(V²), don't materialize all edges
Virtual super-source (wells/hubs)Add node 0 with special edges
Pre-connected componentsUnion free edges before Kruskal
Maximum spanning treeSort edges descending
Bottleneck (minimize max edge)MST already minimizes it
K clusters remainingStop after V−k safe edges
Critical / pseudo-critical edgesForce/exclude each edge, compare MST weight

🔁 Recall Drill (~40 min)

From memory, on a blank file, reproduce:

If Kruskal and Prim blur together, anchor on: Kruskal sorts edges + Union-Find; Prim grows a tree + heap. Both rely on the cut property.

🎯 Timed Problems (~80 min)

LC 1489 Find Critical and Pseudo-Critical Edges in MST The premier MST-variance question — force/exclude each edge, compare weights. Hard
LC 444 Sequence Reconstruction Uniqueness variance — Kahn's queue must never exceed size 1. Med
LC 1203 Sort Items by Groups Respecting Dependencies Two-level topo sort (groups, then items) — a hard composition. Optional stretch. Hard

Week 3 Exit Check

Ready for Week 4 (Advanced) if you can, without notes:

Next up: Week 4 goes advanced — Tarjan's SCC/bridges, bipartite checking, Eulerian paths, A*, and a taste of network flow.