Understanding the Problem
Topological sorting of a directed graph is a linear ordering of its vertices such that for every directed edge u → v
, vertex u
comes before v
in the ordering. This is only possible if the graph has no cycles, i.e., it is a Directed Acyclic Graph (DAG).
Step-by-Step Explanation
We use Kahn's Algorithm, which is a breadth-first search (BFS) based approach. First, we calculate the indegree (number of incoming edges) for each vertex. Then we enqueue all vertices that have an indegree of 0, as these have no prerequisites and can safely be placed at the beginning of the ordering.
We then process the queue:
- Remove a node
u
from the front of the queue and add it to the result list.
- For each neighbor
v
of u
, decrease its indegree by 1 because the edge u → v
has now been 'used'.
- If
v
's indegree becomes 0, it means all its prerequisites have been processed, and we enqueue v
.
Cycle Detection
If, at the end, our result list does not contain all the vertices, it means there is a cycle in the graph. The cycle prevents some nodes from ever reaching an indegree of 0, thus making topological sort impossible. In that case, we return null
.
Different Cases
Case 1: Normal DAG
Suppose we have a graph with a clear hierarchy of dependencies (like a build system). Kahn’s algorithm will give us one of the valid topological orderings that obey all constraints.
Case 2: Multiple Valid Orders
When more than one node has an indegree of 0 at the same time, the order of their insertion into the queue can vary, leading to multiple valid topological sorts.
Case 3: Graph with a Cycle
If there’s a cycle (like 1 → 2 → 3 → 1
), no node in the cycle will ever reach an indegree of 0 after initialization. The queue becomes empty before we process all nodes. Thus, the result list will not include all vertices, and we detect the cycle.