Repository navigation
Expand file tree
/
Copy pathTopologicalSort.cpp
More file actions
91 lines (85 loc) · 1.45 KB
/
Copy pathTopologicalSort.cpp
File metadata and controls
91 lines (85 loc) · 1.45 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
#define MAX 1000
using namespace std;
bool adj[MAX][MAX];
struct node{
int data;
node* next;
}*head=NULL, *last=NULL;
void enqueue(int num){
node *rf=new node;
rf->next=NULL;
rf->data=num;
if(head==NULL){
head=last=rf;
}else{
last->next=rf;
last=rf;
}
}
int dequeue(){
node *rf=head;
int num=rf->data;
head=head->next;
free(rf);
return num;
}
bool isEmpty(){
if(head==NULL)return true;
else return false;
}
int main(){
int num;
cout<<"Enter the number of vertices\n";
cin>>num;
for(int i=1;i<=num;++i){
for(int j=1;j<=num;++j)adj[i][j]=false;
}
int edges=0;
int inDegree[num+1];
for(int i=1;i<=num;++i)inDegree[i]=0;
cout<<"Enter the edges(-1 -1 to stop)\n";
while(true){
int a,b;
cin>>a>>b;
if(a==-1||b==-1)break;
adj[a][b]=true;
inDegree[b]++;
++edges;
}
for(int i=1;i<=num;++i){
if(inDegree[i]==0)enqueue(i);
}
bool visited[num+1];
for(int i=1;i<=num;++i)visited[i]=false;
std::vector<int> ans;
while(true){
if(isEmpty()==true)break;
int node=dequeue();
visited[node]=true;
ans.push_back(node);
for(int i=1;i<=num;++i){
if(adj[node][i]==true){
if(inDegree[i]>0){
if(--inDegree[i]==0)enqueue(i);
}
}
}
}
bool flag=true;
for(int i=1;i<=num;++i){
if(visited[i]==false){
flag=false;
cout<<"Not possible\n";
break;
}
}
if(flag){
cout<<"Topological Order is :\n";
for(int i=0;i<ans.size();++i){
cout<<ans[i]<<" ";
}
cout<<"\n";
}
return 0;
}