Graph Theory37 sections · 1633 units
Open in Course

Practice - Course Schedule II

LeetCode 210

Try this practice problem: you have nn courses and mm prerequisites. Each prerequisite [a,b][a, b] means you must take course bb before course aa. Return any valid ordering of courses, or an empty array if it is impossible. This is topological sort. Build a directed graph where bab \to a for each prerequisite.

Run Kahn's or DFS-based topological sort. If the graph has a cycle, return empty. Otherwise, return the topological order. This problem tests your ability to recognize DAG structure and apply topological sort.