-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTOPOLOGICAL SORT.cpp
More file actions
104 lines (78 loc) · 2.24 KB
/
TOPOLOGICAL SORT.cpp
File metadata and controls
104 lines (78 loc) · 2.24 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
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<bits/stdc++.h>
using namespace std;
vector<int> res;
//check given graph is directed acyclic graph (DAG) or not
/*
FOR TOPOLOGICAL SORT
graph should be DAG
*/
bool chkcycl(vector<vector<int> > &adj, vector<int> &vis,int s)
{
if(vis[s]==1) return 1;
if(vis[s]==2) return 0;
vis[s]=1;
for(int i=0;i<adj[s].size();i++)
{
if(chkcycl(adj,vis,adj[s][i])) return 1;
}
vis[s]=2;
return 0;
}
//if DAG find the course schedule
void dfs(vector<vector<int> > &adj, vector<int> &vis,int s)
{
//mark current element as visited
vis[s]=1;
for(int i=0;i<adj[s].size();i++)
{
if(vis[adj[s][i]]==0)
dfs(adj,vis,adj[s][i]);
}
res.push_back(s);
}
void findOrder(int numCourses, vector<vector<int> >& prerequisites) {
vector<vector<int> > adj(numCourses);
cout<<"\n************ GIVEN INPUT ************\n\n";
cout<<"Number of Processes are "<<numCourses<<"\n\n";
cout<<"Prerequisites\n";
for(int i=0;i<prerequisites.size();i++)
{
cout<<"Complete process "<<prerequisites[i][1]<<" before completing process "<<prerequisites[i][0]<<"\n";
adj[prerequisites[i][1]].push_back(prerequisites[i][0]);
}
cout<<"\n\n************ OUTPUT ************\n\n";
vector<int> vis(numCourses,0);
for(int i=0;i<numCourses;i++)
{
if(!vis[i])
if(chkcycl(adj,vis,i)) { cout<<"Given process entered in Deadlock, unable to complete processes";return ;}
}
for(int i=0;i<numCourses;i++)
vis[i]=0;
for(int i=0;i<numCourses;i++)
{ if(vis[i]!=1)
dfs(adj,vis,i);
}
reverse(res.begin(),res.end());
cout<<"Correct order of completing processes is\n\n";
for(int i=0;i<res.size();i++) cout<<res[i]<<" -> ";
cout<<"\n\nJOB Copmpleted\n";
}
int main ()
{
int n,pq;
cout<<"Enter number of processes \n";
cin>>n;
vector<vector<int> > arr;
cout<<"Enter number of prerequisites \n";
cin>>pq;
cout<<"To complete process a you should have finished process b\n\n";
cout<<"Enter prerequisites in following given format:\na b\n\n";
for(int i=0;i<pq;i++)
{
vector<int> temp(2);
cin>>temp[0]>>temp[1];
arr.push_back(temp);
}
findOrder(n, arr);
}