MediumGraphs · Topological sort

Course Schedule II

There are numCourses courses, 0 to numCourses − 1. A pair [a, b] in prerequisites means you must take b before a. Return any order that finishes every course, or [] if that is impossible (the prerequisites form a cycle).

3, [[1,0]] → [0,1,2]3, [[0,1],[1,2],[2,0]] → []

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Course graph

An arrow b → a means take b before a

current coursein outputcycleedge being followedalready followed

Time and space complexity

V = numCourses, E = number of prerequisite pairs. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Up to 8 courses and 14 prerequisite pairs, written like [[1,0],[2,1]]. Courses caught in a cycle are drawn in a ring on the right.