Topological Sort is a powerful graph algorithm that helps arrange elements in a sequence when some of the elements depend on others.
Imagine you're building a task scheduler—you have got multiple tasks to complete, but some tasks can’t start until others are finished.
How do you determine the correct order to execute them?
This is exactly the type of problem that Topological sort solves.
And if you’re preparing for coding interviews, this is a must-know algorithm!
In this video, I’ll break down:
So, let’s jump in!
Topological sort answers a simple but powerful question: In what order should we process a set of elements when some of them depend on others?
It applies to problems that can be modeled as a Directed Acyclic Graph (DAG)—a graph with directed edges and no cycles.
The goal is to produce a linear ordering of vertices such that: for every directed edge u → v, vertex u appears before vertex v in the final sequence.
For example, consider this dependency graph:
A valid topological ordering would be: C→B→A→D.
Two important things to keep in mind:
Topological sort is used whenever you need to process items in an order that respects dependencies.
Common examples include:
There are two common ways to implement topological sort:
Let’s start with the DFS approach.
DFS-based implementation can be done recursively or iteratively using stack. In this video, we’ll discuss the recursive approach.
Here I am using Java, but you can find the code for other popular programming languages at algomaster.io. Link is in the description.
Here’s how it works:
For this graph, the DFS traversal might visit nodes in the orderA → B → C → D
But the key thing is:
Now that we understand the DFS-based approach, let’s look at a more intuitive and interview-friendly alternative —the Breadth-First Search approach, also known as Kahn’s Algorithm.
Kahn’s Algorithm is an iterative, queue-based method for topological sorting.
It repeatedly removes nodes with no incoming edges (no unmet dependencies), building a valid order as it goes.
Kahn’s Algorithm is based on a simple idea:
If a node has no incoming edges (or prerequisites), it can be processed first. Removing it reduces the indegree of its neighbors; any neighbor for which indegree drops to 0 is processed in the next iteration.
Here’s how it works step-by-step:
Step 1: Compute in-degree for each nodes
The indegree of a node is the number of incoming edges (or dependencies) pointing to it.
For example, in this graph:
Step 2: Identify Nodes with 0 Indegree
Any node with 0 indegree can be processed first, since it has no dependencies.
Add these ready-to-process nodes to a queue.
Step 3: Process Nodes using BFS
While the queue isn't empty:
This ensures each node is only processed after all its prerequisites have been handled.
At the end, if the result contains fewer than V nodes, the graph isn’t a DAG (there’s a cycle), so no topological order exists.
Here’s how to implement it in code:
indegree array to track how many incoming edges each vertex hasThe time complexity is O(V + E), as each vertex and edge is processed exactly once during the traversal.
The space complexity is O(V), required for storing the indegree array, result list, and BFS queue.
By the end of this process, if we have processed all nodes, we get a valid topological order.
However, if some nodes remain unprocessed, it means the graph contains a cycle, making topological sorting impossible.
Kahn’s Algorithm offers two clear advantages over the DFS-based approach: