Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
For example, a topological sorting of the following graph is “5 4 2 3 1 0?. There can be more than one topological sorting for a graph. For example, another topological sorting of the following graph is “4 5 2 0 3 1″. The first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no in-coming edges).
Let’s look at few examples with proper explanation,
Output: 5 4 2 3 1 0
Explanation: The topological sorting of a DAG is done in a order such that for every directed edge uv, vertex u comes before v in the ordering. 5 has no incoming edge. 4 has no incoming edge, 2 and 0 have incoming edge from 4 and 5 and 1 is placed at last.
Output: 0 3 4 1 2
Explanation: 0 and 3 have no incoming edge, 4 and 1 has incoming edge from 0 and 3. 2 is placed at last.
A DFS based solution to find a topological sort has already been discussed.
Solution: In this article we will see another way to find the linear ordering of vertices in a directed acyclic graph (DAG). The approach is based on the below fact:
A DAG G has at least one vertex with in-degree 0 and one vertex with out-degree 0.
Proof: There’s a simple proof to the above fact is that a DAG does not contain a cycle which means that all paths will be of finite length. Now let S be the longest path from u(source) to v(destination). Since S is the longest path there can be no incoming edge to u and no outgoing edge from v, if this situation had occurred then S would not have been the longest path
=> indegree(u) = 0 and outdegree(v) = 0
Algorithm: Steps involved in finding the topological ordering of a DAG:
Step-1: Compute in-degree (number of incoming edges) for each of the vertex present in the DAG and initialize the count of visited nodes as 0.
Step-2: Pick all the vertices with in-degree as 0 and add them into a queue (Enqueue operation)
Step-3: Remove a vertex from the queue (Dequeue operation) and then.
- Increment count of visited nodes by 1.
- Decrease in-degree by 1 for all its neighboring nodes.
- If in-degree of a neighboring nodes is reduced to zero, then add it to the queue.
Step 5: Repeat Step 3 until the queue is empty.
Step 5: If count of visited nodes is not equal to the number of nodes in the graph then the topological sort is not possible for the given graph.
How to find in-degree of each node?
There are 2 ways to calculate in-degree of every vertex:
- Take an in-degree array which will keep track of
Traverse the array of edges and simply increase the counter of the destination node by 1.
for each node in Nodes indegree[node] = 0; for each edge(src, dest) in Edges indegree[dest]++
Time Complexity: O(V+E)
- Traverse the list for every node and then increment the in-degree of all the nodes connected to it by 1.
for each node in Nodes If (list[node].size()!=0) then for each dest in list indegree[dest]++;
Time Complexity: The outer for loop will be executed V number of times and the inner for loop will be executed E number of times, Thus overall time complexity is O(V+E).
The overall time complexity of the algorithm is O(V+E)
Below is C++ implementation of above algorithm. The implementation uses method 2 discussed above for finding indegrees.
Following is a Topological Sort 4 5 2 0 3 1
- Time Complexity: O(V+E).
The outer for loop will be executed V number of times and the inner for loop will be executed E number of times.
- Auxillary Space: O(V).
The queue needs to store all the vertices of the graph. So the space required is O(V)
This article is contributed by Chirag Agarwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Topological Sorting
- Lexicographically Smallest Topological Ordering
- All Topological Sorts of a Directed Acyclic Graph
- Topological Sort of a graph using departure time of vertex
- Relabel-to-front Algorithm
- Prim's algorithm using priority_queue in STL
- Bellman–Ford Algorithm | DP-23
- KMP Algorithm for Pattern Searching
- Jump Pointer Algorithm
- Floyd Warshall Algorithm | DP-16
- Boruvka's algorithm | Greedy Algo-9
- Push Relabel Algorithm | Set 2 (Implementation)
- Dijkstra’s shortest path algorithm using set in STL
- Dinic's algorithm for Maximum Flow
- Hierholzer's Algorithm for directed graph
- Graph Coloring | Set 2 (Greedy Algorithm)
- BFS using vectors & queue as per the algorithm of CLRS
- Shortest Path Faster Algorithm
- Bellman Ford Algorithm (Simple Implementation)
- Welsh Powell Graph colouring Algorithm
Improved By : andrew1234