Topological sort orders vertices in a directed acyclic graph (DAG) so that for every edge u → v, u comes before v. It's used for dependency resolution: tasks, build systems, course prerequisites.
Example
Kahn's Algorithm (BFS)
Process vertices with no incoming edges first. Remove them, update in-degrees, repeat.
DFS-Based Approach
Do a DFS, add nodes to result after processing all their dependencies. Reverse at the end.
Cycle Detection
Topological sort only works on DAGs. If there's a cycle, no valid ordering exists.
Course Schedule Problem
Classic interview problem: can you finish all courses given prerequisites?