An interactive visualization of Kahn's BFS topological sort. Watch how in-degree tracking processes courses in dependency order, and how cycles are detected when not all courses can be scheduled.
Topological Sort LeetCode 210 ↗ MediumnumCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [a, b] indicates that you must take course b before course a.fun findOrder(numCourses: Int, prerequisites: Array<IntArray>): IntArray { val graph = Array(numCourses) { mutableListOf<Int>() } val inDegree = IntArray(numCourses) for ((course, prereq) in prerequisites) { graph[prereq].add(course) inDegree[course] += 1 } val queue: ArrayDeque<Int> = ArrayDeque() for (i in 0 until numCourses) { if (inDegree[i] == 0) { queue.add(i) } } val order = mutableListOf<Int>() while (queue.isNotEmpty()) { val node = queue.removeFirst() order.add(node) for (neighbor in graph[node]) { inDegree[neighbor] -= 1 if (inDegree[neighbor] == 0) { queue.add(neighbor) } } } return if (order.size == numCourses) order.toIntArray() // success else intArrayOf() // cycle detected}
Course prerequisites form a directed graph. Finding a valid ordering is exactly the topological sort problem: arrange vertices so that for every directed edge u→v, u comes before v. This is only possible if the graph has no cycles (i.e., it is a DAG).
Kahn's algorithm uses in-degree tracking. The in-degree of a node is the number of edges pointing into it — the number of prerequisites it has. A course with in-degree 0 has no unmet prerequisites and can be taken immediately.
numCourses, a cycle exists — some courses have circular dependencies that can never be resolved.At every step, we only process courses whose prerequisites have all been completed (in-degree = 0). Removing a processed course from the graph reduces the in-degrees of its dependents, potentially freeing them up. If a cycle exists, the nodes in the cycle never reach in-degree 0, so they are never enqueued — giving us a clean way to detect impossible schedules.
Where V is the number of courses and E is the number of prerequisite pairs.
Each node and edge is processed exactly once. The adjacency list and in-degree array together use O(V + E) space.