Web Analytics

๐Ÿ“‘ Topological Sorting

Intermediate ~30 min read

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

  1. Calculate in-degree for each vertex
  2. Add all vertices with in-degree 0 to queue
  3. While queue not empty:
    • Dequeue vertex, add to result
    • For each neighbor, decrement in-degree
    • If in-degree becomes 0, add to queue
  4. If result size โ‰  total vertices, cycle exists!

Key insight: Process vertices with no dependencies first!

DFS-based Approach

Algorithm Steps

  1. Perform DFS on all unvisited vertices
  2. When vertex finishes (all descendants processed), add to stack
  3. 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

Output
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:

  1. Definition: Linear ordering respecting edge directions
  2. Requirement: Graph must be DAG (no cycles)
  3. Kahn's: BFS-based, start with in-degree 0 vertices
  4. DFS-based: Use finish times (reversed)
  5. Time: O(V + E) for both approaches
  6. Applications: Course schedules, build systems, task scheduling
  7. 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!