Skip to main content
Medium Topological Sort High frequency

Course Schedule

Open on LeetCode

Approach Summary

Detect cycle in directed graph using DFS (3-color states) or Kahn's BFS topological sort. If cycle exists, impossible.

Full Solution & Approach

This is cycle detection on a directed graph, solved with Kahn's algorithm — topological sort via indegrees. Each course is a node; each prerequisite pair (a, b) is an edge b → a. Compute the indegree of every node. Any course with indegree 0 can be taken immediately — put it in a queue. Repeatedly take a course off the queue, "remove" it by decrementing the indegree of every course that depends on it, and whenever a dependent's indegree hits 0, add it to the queue. Count how many courses you processed. If the count equals numCourses, every course is reachable and the dependency graph is acyclic — return true. If you processed fewer, a cycle remains (all leftover courses depend on each other), so completion is impossible. BFS order does not matter here — only the count.

Each edge is relaxed once when its source is processed — O(V + E) time. The adjacency lists, indegree array, and queue use O(V + E) space.

Solution Code

Solution

def can_finish(num_courses: int, prerequisites: list[list[int]]) -> bool:
    graph = [[] for _ in range(num_courses)]
    indegree = [0] * num_courses
    for course, prereq in prerequisites:
        graph[prereq].append(course)
        indegree[course] += 1
    queue = [c for c in range(num_courses) if indegree[c] == 0]
    processed = 0
    for c in queue:
        processed += 1
        for nxt in graph[c]:
            indegree[nxt] -= 1
            if indegree[nxt] == 0:
                queue.append(nxt)
    return processed == num_courses

Edge Cases to Watch

  • No prerequisites — every course has indegree 0, returns true
  • A direct cycle like a depends on b and b depends on a
  • numCourses larger than the courses referenced in prerequisites — isolated courses are trivially completable
  • Multiple connected components — handled naturally

How to Recognize This Pattern

  • Prerequisite ordering
  • Can all courses be taken?

Complexity Analysis

Time Complexity

O(V + E)

Space Complexity

O(V + E)

Tags

Graph DFS BFS Topological Sort

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee