call(back)
Algorithms & Data StructuresmediumSign in to star problems

Course Schedule

There are n courses labeled 0 to n-1, and a list of prerequisite pairs [a, b] meaning you must take course b before course a. Determine whether it is possible to finish all courses.

Hints

Hint 1

Model courses as a directed graph. Completion is impossible exactly when the graph has a cycle.

Hint 2

Kahn's algorithm: repeatedly remove nodes with in-degree zero. If anything is left over, you found a cycle.

Asked at