DSA Notes
Master topological sorting completely — what it is, why it only works on DAGs, DFS-based algorithm with finish times, Kahn
What Is Topological Sorting?
A topological ordering of a directed graph is an ordering of vertices such that for every directed edge (u, v), vertex u appears before vertex v in the ordering.
Requirements:
- The graph must be a DAG (Directed Acyclic Graph)
- Only directed graphs have topological orderings
Course prerequisites (DAG)
Math101 → Math201 → Math301
↑
CS101 → CS201
Valid topological orders
Math101, CS101, CS201, Math201, Math301
CS101, Math101, CS201, Math201, Math301
CS101, CS201, Math101, Math201, Math301
All orders respect: prerequisite always comes before dependent course.
Algorithm 2 — Kahn's Algorithm (BFS + In-Degrees)
Key insight: In any valid topological ordering, vertices with no predecessors must come first. Process them, remove their edges, repeat.
Step-by-step trace:
| In-degrees | Math101:0, CS101:0, CS201:1, Math201:2, Math301:1 |
| Queue | [Math101, CS101] (in-degree 0) |
| Step 1: Dequeue Math101. Remove edge Math101 | Math201. |
| Math201 in-degree | 2-1=1 |
| Queue | [CS101] |
| Step 2: Dequeue CS101. Remove CS101 | CS201. |
| CS201 in-degree: 1-1=0 | Add to queue |
| Queue | [CS201] |
| Step 3: Dequeue CS201. Remove CS201 | Math201. |
| Math201 in-degree: 1-1=0 | Add to queue |
| Queue | [Math201] |
| Step 4: Dequeue Math201. Remove Math201 | Math301. |
| Math301 in-degree: 1-1=0 | Add to queue |
| Queue | [Math301] |
| Step 5 | Dequeue Math301. |
| Queue | [] |
| Result | [Math101, CS101, CS201, Math201, Math301] ✓ |
DFS vs Kahn's Comparison
| Feature | DFS | Kahn's (BFS) |
|---|---|---|
| Algorithm type | Recursive DFS | Iterative BFS |
| Cycle detection | GRAY vertex → cycle | len(result) < V → cycle |
| Implementation | Clean with recursion | Explicit in-degree tracking |
| Unique ordering | Depends on DFS order | Depends on queue ordering |
| Parallelism | Harder to parallelize | Easier (process 0-in-degree at same time) |
Applications
Course Schedule (LeetCode 207 & 210)
Build System (Compile Order)
Lexicographically Smallest Topological Order
Summary
Topological sort orders vertices of a DAG such that all edges go "forward":
- DFS approach: Add to result after finishing, reverse the result
- Kahn's (BFS): Process 0-in-degree vertices, decrement neighbors' in-degrees
- Cycle detection: GRAY vertex (DFS) or
len(result) < V(Kahn's) - Time: O(V + E) for both approaches
Use when: Task scheduling, course prerequisites, build systems, dependency resolution, compiler compilation order.
*Next Lesson: Shortest Path Algorithms*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Topological Sorting — DFS and BFS (Kahn.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, topological, sorting
Related Data Structures & Algorithms Topics