Topological Sort (Kahn's Algorithm)
Kahn's algorithm: output vertices as their incoming edges clear
A topological order lists the vertices so every arrow points forward. Kahn's idea: a vertex with no incoming arrows can safely go first, and once it is out, the arrows leaving it no longer matter — so the vertices it pointed at may become safe too. Keep one number per vertex, its remaining incoming count, and a queue of vertices whose count has hit 0. Popping from the front and pushing newly freed vertices at the back is what makes it breadth-first. The picture draws one directed arrow per edge and shows the remaining count as a badge.
Details
Animated on: numVertices = 4, edges = [[3,0],[3,1],[2,1],[1,0]] (a->b means a comes before b) — return a topological ordering using Kahn's BFS algorithm.
Problem:Topological Sort (Kahn's Algorithm)Medium
Kahn's algorithm: output vertices as their incoming edges clear
step 1 / 11
A directed acyclic graph
4 vertices, 4 directed edges, and an arrow a to b means a must come before b. The task asks for an ORDER, so the plan is to output vertices one at a time and only ever output a vertex once everything pointing at it is already out.
1FUNCTION topoSort(n, edges):2 FOR v FROM 0 TO n - 13 indeg[v] <- 04 FOR EACH (a, b) IN edges5 APPEND b TO next[a]6 indeg[b] <- indeg[b] + 17 queue <- EVERY v WHERE indeg[v] = 08 order <- EMPTY LIST9 WHILE queue NOT EMPTY10 v <- REMOVE FIRST FROM queue11 APPEND v TO order12 FOR EACH w IN next[v]13 indeg[w] <- indeg[w] - 114 IF indeg[w] = 015 APPEND w TO queue16 RETURN order
← / → step · space play · Home restart
Every step in words
Show all 11 steps
- 4 vertices, 4 directed edges, and an arrow a to b means a must come before b. The task asks for an ORDER, so the plan is to output vertices one at a time and only ever output a vertex once everything pointing at it is already out.
- Count the arrows pointing INTO each vertex: 0 has 2, 1 has 2, 2 has 0, 3 has 0. A vertex is safe to output exactly when that count is 0, so this one number per vertex is the whole state the algorithm needs.
- 2 and 3 sit at 0 already — nothing points at them, so they can go first. Seed the queue with them in index order; the queue is what makes this breadth-first, since vertices freed by the same round line up behind the ones already waiting.
- Pop 2 from the front and append it to the order — it is the 1st vertex out, safe because its count is 0. Order so far: 2; 3 still waits in the queue.
- Now that 2 is out, each arrow leaving it is settled: 1 drops to 1. Nothing hit 0 yet, so nothing new joins the queue.
- Pop 3 from the front and append it to the order — it is the 2nd vertex out, safe because its count is 0. Order so far: 2, 3; the queue is empty for now.
- Now that 3 is out, each arrow leaving it is settled: 0 drops to 1, 1 drops to 0. 1 hit 0 and joins the back of the queue.
- Pop 1 from the front and append it to the order — it is the 3rd vertex out, safe because its count is 0. Order so far: 2, 3, 1; the queue is empty for now.
- Now that 1 is out, each arrow leaving it is settled: 0 drops to 0. 0 hit 0 and joins the back of the queue.
- Pop 0 from the front and append it to the order — it is the 4th vertex out, safe because its count is 0. Order so far: 2, 3, 1, 0; the queue is empty for now.
- No arrow leaves 0, so no count changes and nothing new becomes ready.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.