# 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++.

### 13 thoughts on “Topological Sort in C and C++”

1. 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]–;
}

1. 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 1 0 0 0 0
1 1 0 0 0 0
1 0 1 0 0 0

For this adjacency matrix, I am getting a wrong output.

2. For this matrix result of above program is wrong
000000
000000
000100
010000
110000
101000

1. Sorry to prove you wrong but I just traced your input in the program and got the result as the following
5,6,1,3,4,2 which is correct when you refer your DAG.

3. General design is good, but to make the code right,we must remove the last for. Then we must put a for loop inside the if loop.This loop will be like:

for(i=0;i<n;i++){
if(a[k][i]==1){
a[k][i]=0;
indeg[k]–;
}
}

4. 1 2 3 4 5 6
1 0 0 0 0 0 1
2 1 0 0 0 0 0
3 0 0 0 0 0 0
4 0 1 1 0 0 0
5 0 0 1 0 0 1
6 0 0 0 0 0 0

this code didnt give me a correct answer when i input these matrix

1. that’s because you use an adjacency matrix to run the program. This type of matrix contains only 1 and 0 elements. You can create an adjacency matrix on a paper by looking at an oriented graph.

2. i think this one is working

#include

#include

int g={0},flag,indeg,n,count=0;

int main()
{
printf(“enter the value of n\n”);
scanf(“%d”,&n);

int i,j,k;

printf(“enter the matrix\n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{scanf("%d",&g[i][j]);}
}

for(i=0;i<n;i++)
{
flag[i]=0;
indeg[i]=0;
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
indeg[i]=indeg[i]+g[j][i];
}
}

while(count<n)
{
for(i=0;i”,i+1);
flag[i]=1;
}

for(j=0;j<n;j++)
{
if(g[j][i]==1&&flag[i]==0)//<————————————-
{
indeg[i]–;
break; //<—————————————————–
}
}
}
count++;
}
return 0;
}

1. młody atleta

p’s program works, all that was missing was just a break and added condition in last if

5. wrong output for the below input:
Enter the no of vertices:
6
Enter row 1
0 0 0 0 1 1
Enter row 2
0 0 0 1 1 0
Enter row 3
0 0 0 0 0 1
Enter row 4
0 0 1 0 0 0
Enter row 5
0 0 0 0 0 0
Enter row 6
0 0 0 0 0 0
The topological order is: 1 2 3 4 5 6

1. The code is wrong please update it

6. Hey crazy programmer, you are really crazy. This code is not working properly man

7. I need topological sort with c++. This one is onlu c