-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopological Sort (List).cpp
53 lines (53 loc) · 1.07 KB
/
Topological Sort (List).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<list>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
vector<int>v[100];
int i,j,k,n,indegree[100],edge,node,node1,node2,x,count;
printf("Enter number of nodes and edges: ");
scanf("%d %d",&node,&edge);
for(i=0; i<100; i++)
{
v[i].clear();
indegree[i]=0;
}
for(i=1; i<=edge; i++)
{
scanf("%d %d",&node1,&node2);
v[node1].push_back(node2);
indegree[node2]++;
}
count=0;
while(count<node)
{
for(i=1; i<=node; i++)
{
if(indegree[i]==0)
{
printf("%d ",i);
indegree[i]=-1;
break;
}
}
for(j=0; j<v[i].size(); j++)
{
x=v[i][j];
indegree[x]--;
}
count++;
}
return 0;
}