๐ Topological Sorting
You're taking computer science courses. Some courses have prerequisites - you must take CS101 before CS201, and CS201 before CS301. In what order should you take the courses? Topological sorting finds a valid order that respects all dependencies!
The Dependency Problem
Many real-world problems involve dependencies that must be ordered correctly:
Real-World Applications
- Course Prerequisites: Order courses respecting prerequisites
- Build Systems: Compile dependencies before dependents
- Task Scheduling: Execute tasks in dependency order
- Package Managers: Install dependencies first
- Event Ordering: Order events with dependencies
What is Topological Sort?
Definition
Given a Directed Acyclic Graph (DAG), topological sort produces a linear ordering of vertices such that:
- For every edge (u, v), u comes before v in the ordering
- All dependencies are satisfied
Note: Only works for DAGs (no cycles)! If cycle exists, topological sort is impossible.
See Topological Sort in Action
Kahn's Algorithm (BFS-based)
Algorithm Steps
- Calculate in-degree for each vertex
- Add all vertices with in-degree 0 to queue
- While queue not empty:
- Dequeue vertex, add to result
- For each neighbor, decrement in-degree
- If in-degree becomes 0, add to queue
- If result size โ total vertices, cycle exists!
Key insight: Process vertices with no dependencies first!
DFS-based Approach
Algorithm Steps
- Perform DFS on all unvisited vertices
- When vertex finishes (all descendants processed), add to stack
- Reverse stack to get topological order
Key insight: Finish time order gives topological sort (reversed)!
Cycle Detection
If topological sort cannot process all vertices, a cycle exists in the graph. This is a natural way to detect cycles in directed graphs!
The Complete Code
Click Run to execute your code
Course Schedule Problem
The Problem
Given n courses and prerequisites, can you finish all courses?
Solution: Topological sort - if all courses can be ordered, yes; if cycle exists, no.
Build Order Problem
The Problem
Given projects and dependencies, in what order should projects be built?
Solution: Topological sort gives valid build order!
Kahn's vs DFS-based
Comparison
| Aspect | Kahn's Algorithm | DFS-based |
|---|---|---|
| Approach | BFS (queue) | DFS (stack/recursion) |
| Complexity | O(V + E) | O(V + E) |
| Intuition | Start with no dependencies | Finish times give order |
| When to Use | When you need level-by-level | When DFS is natural |
Summary
What You've Learned
Topological sort orders vertices in DAG:
- Definition: Linear ordering respecting edge directions
- Requirement: Graph must be DAG (no cycles)
- Kahn's: BFS-based, start with in-degree 0 vertices
- DFS-based: Use finish times (reversed)
- Time: O(V + E) for both approaches
- Applications: Course schedules, build systems, task scheduling
- Cycle Detection: If not all vertices processed, cycle exists
What's Next?
Congratulations! You've completed Module 10: Graphs! You now understand graph representation, BFS, DFS, and topological sorting. These are fundamental algorithms used throughout computer science. You're ready to explore more advanced graph algorithms like shortest paths and minimum spanning trees!
Enjoying these tutorials?