CS Visual Lab
Explore
Learning Paths
5 modules live
Get Started
Topological Sort
Module 4.11 — Kahn's algorithm (BFS-based) on DAGs
Graph:
Course Prerequisites
Build System
Complex DAG
Step
Reset
Speed:
0.5x
1x
2x
Step 0 / 0
Math
CS101
Stats
CS201
ML
DB
Unprocessed
In Queue
Processing
Done
Number = in-degree
Topological Order
Press Play to start
Queue (in-degree = 0)
(empty)
In-Degree Table
Math
—
CS101
—
Stats
—
CS201
—
ML
—
DB
—
Kahn's Algorithm
1. Calculate in-degree of all vertices
2. Enqueue all vertices with in-degree 0
3. While queue not empty:
a. Dequeue vertex u → output
b. For each neighbor v: in-degree[v]--
c. If in-degree[v] == 0: enqueue v
4. If output.length < V → cycle exists!
Use Cases
• Course prerequisite ordering
• Build dependency resolution
• Task scheduling
• Package manager installs