All Patterns
Pattern 15
Topological Sort
Order nodes in a DAG such that every directed edge goes from earlier to later. Use BFS (Kahn's algorithm) with in-degree tracking or DFS with a finish-time stack.
Time
O(V + E)
Space
O(V + E)
Recognize it when
- Task scheduling with prerequisites
- Course prerequisites / build order
- Detect cycles in directed graph
Progress0/4
0 solved0 attempted
Questions — ordered by difficulty
#207Medium
Course Schedule
Determine if you can finish all courses given their prerequisites (detect cycle in directed graph).
graphtopological-sortbfs
#210Medium
Course Schedule II
Return the order in which to take courses to finish all of them.
graphtopological-sortbfs
#310Medium
Minimum Height Trees
Find all roots that minimize the height of the resulting tree.
graphtopological-sorttree
#269Hard
Alien Dictionary
Given a sorted dictionary of an alien language, determine the order of characters.
graphtopological-sortbfs