Topological sorting is a fundamental algorithm pattern in computer science, essential for processing tasks with dependencies. It produces a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge from vertex U to vertex V, U comes before V in the ordering.
This pattern is widely used in scheduling problems, build systems (like Make), course prerequisite planning, and resolving symbol dependencies in linkers. The algorithm can be implemented using Kahn's algorithm (BFS-based) or DFS with a stack.
Key characteristics:
- Only works on DAGs
- Produces a valid ordering if the graph has no cycles
- Time complexity: O(V + E)
Mastering topological sort is crucial for coding interviews and system design, as it provides a clean way to handle dependency graphs.