Baseline Graphs · Topo Sort Problems O(N + E) · O(N + E)
A certification body offers numbered training passes, some of which require earlier passes first. Each requirement is one-way: pass A demands pass B beforehand. Before publishing the term plan, the registrar must confirm no circular requirement chain makes the whole program impossible to complete.
Input: An integer numCourses and a list prerequisites where each pair [a, b] means course b must be taken before course a.
Output: Return true if all courses can be completed in some order, otherwise false.
1 <= numCourses <= 20000 <= prerequisites.length <= 5000All prerequisite pairs are uniqueInput: {"numCourses":2,"prerequisites":[[1,0]]}
Output: true
Take 0 first, then 1 — no conflict.
Input: {"numCourses":2,"prerequisites":[[1,0],[0,1]]}
Output: false
Each course demands the other first — an impossible circular requirement.