Topological Sort
Topological sort orders the nodes of a directed acyclic graph so every edge points forward — the valid sequence for tasks with dependencies. Use it for build orders, course schedules, and any 'do A before B' problem; it also detects cycles.
4 LeetCode problems solved with the Topological Sort pattern. Practice them with spaced repetition so the pattern sticks.
- Alien DictionaryHARD · O(C)
- Build a Matrix With ConditionsHARD · O(k + R + C)
- Course ScheduleMEDIUM · O(V + E)
- Course Schedule IIMEDIUM · O(V + E)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →