Problem. n courses, and pairs [a, b] meaning b must be taken before a. Return any order of all courses that
respects every prerequisite, or [] if none exists.
n = 4, prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]] → [0, 1, 2, 3] or [0, 2, 1, 3]
n = 2, prerequisites = [[1, 0], [0, 1]] → []
Such an order is a topological order of the graph with edges b → a. Lesson 11.6 checked whether one exists; here we produce it, and look at which order to produce when several are valid.
ML context. Running a pipeline means executing its tasks in a topological order: preprocessing before training, training before evaluation. Independent tasks can run in parallel, and the minimum number of parallel "waves" is the length of the longest dependency chain (Change B), which bounds the wall-clock time of a pipeline even with unlimited workers.