Topological Sort (BFS - Kahn's)
Kahn's algorithm: output vertices as their incoming edges clear
A topological order lists the vertices so that 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 = 6, edges = [[5,0],[5,2],[4,0],[4,1],[2,3],[3,1]] (a->b means a comes before b) — return a topological ordering using BFS.
Problem:Topological Sort (BFS - Kahn's)Medium
Kahn's algorithm: output vertices as their incoming edges clear
step 1 / 16
A directed acyclic graph
6 vertices, 6 directed edges, and an arrow a to b means a must come before b in the answer. We are asked 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 16 steps
- 6 vertices, 6 directed edges, and an arrow a to b means a must come before b in the answer. We are asked 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 1, 3 has 1, 4 has 0, 5 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.
- Vertices 4 and 5 both sit at 0 — 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 every vertex freed by the same round lines up behind the ones already waiting.
- Pop 4 from the front and append it to the order — it is the 1st vertex out, and it is safe because its count is 0. Order so far: 4; 5 still wait in the queue.
- Now that 4 is out, each arrow leaving it is settled, so 0 drops to 1 and 1 drops to 1. Nothing hit 0 yet — 0 and 1 still have another arrow to clear, so they must keep waiting.
- Pop 5 from the front and append it to the order — it is the 2nd vertex out, and it is safe because its count is 0. Order so far: 4, 5; the queue is empty for now.
- Now that 5 is out, each arrow leaving it is settled, so 0 drops to 0 and 2 drops to 0. 0 and 2 hit 0 and join the back of the queue.
- Pop 0 from the front and append it to the order — it is the 3rd vertex out, and it is safe because its count is 0. Order so far: 4, 5, 0; 2 still wait in the queue.
- No arrow leaves 0, so no count changes and nothing new becomes ready.
- Pop 2 from the front and append it to the order — it is the 4th vertex out, and it is safe because its count is 0. Order so far: 4, 5, 0, 2; the queue is empty for now.
- Now that 2 is out, each arrow leaving it is settled, so 3 drops to 0. 3 hit 0 and joins the back of the queue.
- Pop 3 from the front and append it to the order — it is the 5th vertex out, and it is safe because its count is 0. Order so far: 4, 5, 0, 2, 3; the queue is empty for now.
- Now that 3 is out, each arrow leaving it is settled, so 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 6th vertex out, and it is safe because its count is 0. Order so far: 4, 5, 0, 2, 3, 1; the queue is empty for now.
- No arrow leaves 1, so no count changes and nothing new becomes ready.
- The queue drained after all 6 vertices, so the order is [4, 5, 0, 2, 3, 1]. Every arrow in the picture points from an earlier vertex to a later one — that is the definition of a topological order. Had the count come up short, the missing vertices would form a cycle.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.