An interactive visualization of BFS-based topological sort to find the minimum number of semesters needed to take all courses, processing independent courses in parallel each semester.
Topological Sort LeetCode 1136 Mediumn, which indicates that there are n courses labeled from
1 to n. You are also given an array relations where
relations[i] = [prevCourse, nextCourse] means that course prevCourse must be
taken before course nextCourse.-1.
fun minimumSemesters(n: Int, relations: Array<IntArray>): Int { val graph = Array(n + 1) { mutableListOf<Int>() } val inDegree = IntArray(n + 1) for ((prev, nextC) in relations) { graph[prev].add(nextC) inDegree[nextC]++ } var queue = ArrayDeque<Int>() for (i in 1..n) { if (inDegree[i] == 0) queue.add(i) } var semesters = 0 var taken = 0 while (queue.isNotEmpty()) { semesters++ val nextQueue = ArrayDeque<Int>() for (idx in 0 until queue.size) { val course = queue.removeFirst() taken++ for (neighbor in graph[course]) { inDegree[neighbor]-- if (inDegree[neighbor] == 0) nextQueue.add(neighbor) } } queue = nextQueue } return if (taken == n) semesters else -1 }
This is a classic application of BFS-based topological sort (Kahn's algorithm). Each "level" of the BFS corresponds to one semester. Courses with no remaining prerequisites (in-degree = 0) can all be taken in the same semester. After taking them, we decrement the in-degree of their dependents, potentially unlocking new courses for the next semester.
n courses, return the semester count. Otherwise, a cycle exists and we return -1.A course can only be taken once all its prerequisites are completed. The earliest a course can be taken is one semester after the last of its prerequisites. BFS naturally groups courses by their earliest possible semester: level 0 has courses with no prerequisites, level 1 has courses whose prerequisites were all in level 0, and so on. This gives the minimum number of semesters.
We visit each course (vertex) once and process each prerequisite relation (edge) once. The graph,
in-degree array, and queue together use O(V + E) space, where V = n (courses) and
E = number of relations.