Topological Sort
Idea Like You Are 10
Sometimes one job must happen before another.
Example:
- wear socks before shoes
- finish homework before playing games
Topological sort gives an order that respects these rules.
It only works on a Directed Acyclic Graph (DAG).
Example
One valid order:
Kahn's Algorithm
Time Complexity
O(V + E)
Why?
- compute indegrees using all edges
- visit every vertex once
- process every edge once
Applications
- job scheduling
- course planning
- dependency resolution