Pattern #55
Kahn's Algorithm
Important interview questions, thinking patterns, Indegree 0 Ready Queue, Cycle Deadlock validation, Min Heap Topo variants, Parallel Semesters, and Go templates.
Must Solve
15 core questions — solve these first.
- 1.Course ScheduleKahn's Algorithm: return processedCount == n (check for dependency deadlock cycle)medium
- 2.Course Schedule IIReturn valid topological ordering array built from Kahn's Indegree 0 Queuemedium
- 3.Topological SortStandard Kahn's BFS topological sort templatemedium
- 4.Alien DictionaryBuild character dependency graph + Kahn's indegree queue (check prefix rule & cycle deadlock)hard
- 5.Parallel CoursesKahn's BFS level-by-level queue size loop counting minimum semesters requiredmedium
- 6.Parallel Courses IIIKahn + DP on DAG: start[v] = max(start[v], finish[u]) for task completion timeshard
- 7.Find All Possible Recipes From Given SuppliesRecipe ingredient dependency graph initialized with supplies having indegree 0medium
- 8.Sequence ReconstructionVerify unique topological ordering (Kahn's queue must never contain >1 node at any step)medium
- 9.Minimum Semesters to Finish CoursesKahn's level-order queue size loop for semester wave countingmedium
- 10.Build OrderPackage dependency DAG ordering using Kahn's Algorithmmedium
- 11.Task Scheduling with DependenciesParallel task queue execution where indegree 0 represents ready tasksmedium
- 12.Largest Color Value in a Directed GraphKahn Topo Sort + DP state dp[u][color] tracking max color frequencies along pathshard
- 13.Sort Items by Groups Respecting DependenciesDual Kahn's Topo Sort on group DAG & item DAGhard
- 14.Find Eventual Safe StatesReverse graph edges + Kahn's BFS starting from 0 out-degree terminal nodesmedium
- 15.Detect Cycle in Directed Graph Using KahnCycle detected if len(order) < n when queue emptiesmedium
Also Important
8 more questions worth practicing.
- 16.Minimum Time to Complete All TasksTask duration optimization + Kahn's Topo ordermedium
- 17.Critical Path in DAGKahn's Algorithm + DP finding longest duration path in project networkshard
- 18.DAG Layer ProcessingGrouping nodes into parallel dependency waves using level-order Kahnmedium
- 19.Package Installation OrderPackage manager dependency resolutionmedium
- 20.Workflow Dependency ExecutionTask DAG orchestration using ready task queuemedium
- 21.Dependency ResolutionSystem component startup orderingmedium
- 22.Lexicographically Smallest Topological OrderKahn's Algorithm using Min Heap (PriorityQueue) instead of FIFO Queuemedium
- 23.Unique Topological OrderingValidate queue size is strictly 1 at every step during Kahn executionmedium
How to Think
- Need dependency order?Kahn's Algorithm (Indegree 0 Queue)
- Need nodes that are ready now?Indegree == 0 (0 unfinished prerequisites)
- Need detect cycle in directed graph?Kahn fails (processedCount < totalNodes)
- Need process dependency levels / semesters?Kahn + BFS Level loop (size := len(queue))
Go Kahn's Algorithm Code Templates
Standard Kahn Queue, Parallel Semester Levels & Lexicographically Smallest Min Heap in Go
// 1. Standard Kahn's Algorithm (BFS Queue): O(V + E) Time
func topoSortKahn(n int, edges [][]int) []int {
adj := make([][]int, n)
indegree := make([]int, n)
for _, e := range edges {
u, v := e[0], e[1]
adj[u] = append(adj[u], v)
indegree[v]++
}
queue := []int{}
for i := 0; i < n; i++ {
if indegree[i] == 0 {
queue = append(queue, i)
}
}
order := []int{}
head := 0
for head < len(queue) {
u := queue[head]
head++
order = append(order, u)
for _, v := range adj[u] {
indegree[v]--
if indegree[v] == 0 {
queue = append(queue, v) // Unlocked!
}
}
}
if len(order) != n {
return nil // Cycle detected!
}
return order
}
// 2. Parallel Courses / Semesters (Level Loop Kahn): O(V + E) Time
func minSemesters(n int, relations [][]int) int {
adj := make([][]int, n+1)
indegree := make([]int, n+1)
for _, r := range relations {
adj[r[0]] = append(adj[r[0]], r[1])
indegree[r[1]]++
}
queue := []int{}
for i := 1; i <= n; i++ {
if indegree[i] == 0 { queue = append(queue, i) }
}
semesters := 0
processed := 0
for len(queue) > 0 {
size := len(queue)
semesters++
for i := 0; i < size; i++ {
u := queue[0]
queue = queue[1:]
processed++
for _, v := range adj[u] {
indegree[v]--
if indegree[v] == 0 {
queue = append(queue, v)
}
}
}
}
if processed != n { return -1 }
return semesters
}👉 Total Time: O(V + E) | Space: O(V + E) graph + indegrees + queue
The Indegree Unlocking Rule
1. Indegree Mental Model: Indegree represents the number of unfinished prerequisites pointing into a node.
2. Zero Prerequisites = Ready: Nodes with indegree == 0 have no incoming dependencies blocking them and can enter the ready queue immediately.
3. Unlocking Neighbors: When node u is processed, decrement indegree[v]-- for all outgoing edges u -> v. As soon as indegree[v] == 0, node v becomes unlocked and is enqueued!
Parallel Execution Waves & Min Heap Topo
1. Parallel Execution Waves: In task scheduling or semester requirements, wrapping the queue pop in a level loop (size := len(queue)) processes all currently ready tasks in parallel in 1 round.
2. Lexicographically Smallest Order: When an interview requires the smallest possible valid sequence, replace the FIFO Queue with a Min Heap (PriorityQueue). Whenever multiple nodes are ready, the min heap picks the smallest node.
Kahn's Algorithm → Indegree 0 Ready Queue for DAG topological ordering
Indegree → Number of unfinished prerequisites pointing to node
Queue Pop → Append to order -> Decrement neighbors' indegree
Neighbor 0 → Enqueue immediately (Unlocked!)
Cycle Check → processedCount < n when queue empty = Dependency Deadlock!
Min Heap Variant → Replace Queue with Min Heap for smallest numerical order💡 Golden Rule: "Keep processing whatever has zero unfinished dependencies; each completed node may unlock the next set of ready nodes."
Common Interview Mistakes
If "A depends on B", the edge MUST be B -> A (B before A). Reversing direction corrupts indegree counts!
When processing node u with edge u -> v, decrement indegree[v]--, NOT u.
Enqueue a neighbor ONLY when its indegree drops to PRECISELY 0 (indegree[v] == 0).
Nodes with zero incoming and outgoing edges still have indegree == 0 and belong in the topological order!
Interview Rules
- 1. Prerequisites / Dependencies?→ Kahn's Algorithm (Indegree 0 Queue)
- 2. Indegree calculation? → Count incoming edges for each node
0..n-1 - 3. Processing step? → Pop ready node
u, append to order, decrementindegree[v]--for allu -> v - 4. Neighbor unlocking? → Enqueue neighbor
vimmediately whenindegree[v] == 0 - 5. Dependency cycle check? → Cycle exists if
processedCount < nwhen queue empties - 6. Parallel rounds / semesters? → Level-by-level queue size loop (
size := len(queue)) - 7. Smallest valid order? → Replace Queue with Min Heap (PriorityQueue)
- 8. Overall Complexity → Time:
O(V + E)| Space:O(V + E)
Small Rules
- Rule 1:Kahn's algorithm models tasks in two states: Blocked (indegree > 0) and Ready (indegree == 0).
- Rule 2: Completing a ready node decreases the indegree of all its outgoing dependencies.
- Rule 3: Neighbors are enqueued only when their indegree drops to 0.
- Rule 4: Queue emptying before processing all nodes proves a directed cycle deadlock.
- Rule 5: Level-order Kahn loops calculate parallel execution rounds.
Production Thinking
Job Scheduler Ready Queue → Dispatching unblocked tasks (indegree 0) to worker thread pools
CI/CD Pipeline Execution → Executing build, lint, test, and deploy stages in valid dependency waves
Package Installation Resolution → Installing prerequisite libraries before dependent software packages
Workflow Engine Orchestration → Tracking remaining prerequisite counts for state machine transitions
Remember This
Kahn's Algorithm → Indegree 0 Queue
Build Graph + Calculate Indegrees
Indegree 0 → Ready Queue
Pop Ready Node → Add to Order
For Each Neighbor→ indegree--
Becomes 0? → Enqueue (Unlocked!)
Queue Empty → Check processedCount == N (Cycle if < N)
Parallel Rounds → Level loop (size := len(queue))
Smallest Order → Min Heap (PriorityQueue)
Time → O(V + E)💡 Golden Rule: "Keep processing whatever has zero unfinished dependencies; each completed node may unlock the next set of ready nodes."