Given numCourses and prerequisite pairs [a, b] (b must be taken before a), can you finish all courses?
Modeling dependencies as a directed graph and detecting a cycle via topological sort (Kahn's algorithm).
Model courses as nodes and each prerequisite as a directed edge b → a. Build an adjacency list and an indegree count per node. Enqueue every node with indegree 0 (no prerequisites); repeatedly pop one, 'complete' it, and decrement each neighbor's indegree, enqueuing any that drop to 0. If you process all nodes, a valid ordering exists → true; if some remain, they sit in a cycle that can never start → false. Signal: 'ordering with dependencies', 'can this be scheduled', or 'detect a cycle in a directed graph' → topological sort (Kahn's BFS with indegrees, or DFS with a visiting/visited coloring).
Build/task ordering, dependency resolution, package managers, and cycle detection in DAGs.
Time O(V + E), Space O(V + E)