Stage 1 · Code
Graph Algorithms
Topological Sort
Kahn's algorithm and DFS-based topological ordering for DAGs.
Kahn's Algorithm (BFS)
Compute indegree of all nodes. Queue nodes with indegree 0. Process queue: remove node, add to result, decrement indegree of neighbors. If neighbor indegree becomes 0, enqueue. If result size < V, cycle exists.
DFS-based Topological Sort
DFS post-order traversal. Add node to result after visiting all children. Reverse result. Need cycle detection (Gray state).
Applications
- Course scheduling: Prerequisites → topological order.
- Build systems: Dependencies between modules.
- Task scheduling: Dependencies between tasks.
- Alien dictionary: Infer alphabet order from sorted words.
- Parallel execution: Independent tasks can run in parallel.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.