Topological Sort
A topological sort, a.k.a Topo-sort, is performed on DAG (Directed Acyclic Graph) to compute a linear ordering of all the vertices in graph. It satisfies the following condition: if a graph G contains an edge (u, v), then the vertex u must appear before the vertex v in the sorted ordering.
Unlike the general discussions in sorting section, Topo-sort tries to layout a graph structure into a flatten distribution of vertices. This method is widely adopted in the industry, e.g. a manufacturing process of specific products might have numerous small procedures combined together and to compute a topological ordering of such procedures are critical for pipelining the manufacturing process as to reduce time & money cost.
There are many algorithms that can achieve the goal, here a version of Kahn's algorithm is discussed.
Kahn's Algorithm
The idea of this algorithm takes advantage of BFS and using in-degree of nodes and it is the easiest method to understand.
Compute the in-degree for each of the nodes within the graph, initialize a count for visited nodes to 0.
Enqueue all the nodes with in-degree of 0 to queue.
Dequeue a node from queue, increase the count for visited nodes by 1, decrease the in-degree by 1 for all its neighboring nodes; If any of these nodes happen to have in-degree of 0, enqueue them to queue.
Repeat step 3 until queue is empty.
If the count of visited nodes is not equal to the number of nodes in the graph, then it is not DAG and topo-sort is impossible to complete.
Unlike traversing the solution using DFS that revisit some of the nodes that share the same routes from the source to the destination, this algorithm takes Ο(n) time to visit all nodes, which is more favorable and easier to design.
Last updated