Topological Sort in C and C++

Here you will learn and get program for topological sort in C and C++.

We know many sorting algorithms used to sort the given data. It may be numeric data or strings. Take a situation that our data items have relation. They are related with some condition that one should happen only after other one happened.

For example shoe should wear after socks only. Another example, in academic course one should study Applications of Algorithms subject only after studying Designing of Algorithms. And Designing of Algorithms should study only after learning any programming language. We can observe that a work requires pre-requisite. In these situations we represent our data in a graph and we use directed edges from pre-requisite to next one. And we apply Topological sorting to solve.

Most important condition to do Topological sorting on any graph is that Graph should be Connected Directed Acyclic graph. It is not possible to apply Topological sorting either graph is not directed or it have a Cycle.

One more condition is that graph should contain a sink vertex. The vertex which doesn’t have any outgoing edge is called sink vertex. Idea behind this sink vertex is that if every vertex has an outgoing edge in a directed graph it definitely forms a cycle, which violates the condition.

Note: Topological sorting on a graph results non-unique solution

Topological Sort Example

Solving Using In-degree Method

Step 1: Write in-degree of all vertices:

 Vertex in-degree 1 0 2 1 3 1 4 2

Step 2: Write the vertex which has in-degree 0 (zero) in solution. Here vertex 1 has in-degree 0. So,

Solution is: 1 -> (not yet completed )

Decrease in-degree count of vertices who are adjacent to the vertex which recently added to the solution.

Here vertex 1 is recently added to the solution. Vertices 2 and 3 are adjacent to vertex 1. So decrease the in-degree count of those and update.

Updated result is:

 Vertex in-degree 1 Already added to solution 2 0 3 0 4 2

Again repeat the same thing which we have done in step1 that, write the vertices which have degree 0 in solution.

Here we can observe that two vertices (2 and 3) have in-degree 0 (zero). Add any vertex into the solution.

Note that, you may add vertex 2 into solution, and I may add vertex 3 to solution. That means the solution to topological sorting is not unique.

Solution is: 1->3->

Again decrease the in-degree count of vertices which are adjacent to vertex 3.

Updated result is:

Now add vertex 2 to solution because it only has in-degree 0.

Solution is: 1->3->2->

Updated result is:

Final solution is: 1->3->2->4

Program for Topological Sort in C

Output

Enter the no of vertices:
4
Enter row 1
0 1 1 0
Enter row 2
0 0 0 1
Enter row 3
0 0 0 1
Enter row 4
0 0 0 0

The topological order is:1 2 3 4

Program for Topological Sort in C++

Comment below if you have any queries related to above program for topological sort in C and C++.

Category: DSA

One thought on “Topological Sort in C and C++”

1. Luis Salazar

Question, shouldn’t the following code must be inside the “if((indeg[k]==0) &&..” condition?
for(i=0;i<n;i++){
if(a[i][k]==1)
indeg[k]–;
}