-
Notifications
You must be signed in to change notification settings - Fork 0
/
207 Course Schedule.cpp
174 lines (138 loc) · 5.21 KB
/
207 Course Schedule.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
/*
// if a node is connected to its ancestor and also present in the recursionStack, then it contains cycle
int n;
vector<int>vis;
vector<int>recStack;
bool dfs(int v,int par, vector<vector<int>>ar){
vis[v]=1;// visit
recStack[v]=1;// add to recStack
for(int i=0;i<ar[v].size();++i){
int child=ar[v][i];
if(vis[child]==0){// if not visited
if(dfs(child,v,ar)==true)// contains backedge in child
return true;
}// backedge condition ,else if(child!=par) for undirected graph
else if(recStack[child]){ // if visited but also must be present in recStack
return true;
}
}
recStack[v]=0;//remove from recStack
return false; // no cycle
}
*/
bool canFinish(int n, vector<vector<int>>& pre) {
// Kahn's Algorithm
vector<vector<int>> adj(n, vector<int>());
vector<int> degree(n, 0);
for (auto &p: pre) {
adj[p[1]].push_back(p[0]);
degree[p[0]]++;
}
// degree contains the indegre of each element
// vector<int>res;// final topological sorted order
queue<int> q;
for (int i = 0; i < n; i++)
if (degree[i] == 0) q.push(i); // find the element with indegree 0
while (!q.empty()) {
int curr = q.front();
q.pop();
// res.push_back(curr); // not needed here
n--; // if this gets executed for all n nodes then all courses can be covered
for (auto next: adj[curr]){ // reduce the indegree of all the elements of adj list pointed by curr
// if (--degree[next] == 0) q.push(next);
--degree[next];
if(degree[next]==0)
q.push(next);
}
}
return n == 0;
// create a graph and then check for cycle if there is cycle then false, else true
// [i,j] j->i
/*
int n=prerequisites.size();
if(n==0)
return true;
// cout<<n;
unordered_set <int> nodeSet ;
// creating adjacency list
vector<vector<int>> ar(numCourses);
for(int i=0;i<n;++i){
ar[prerequisites[i][1]].push_back(prerequisites[i][0]); // directed
nodeSet.insert(prerequisites[i][0]);
nodeSet.insert(prerequisites[i][1]);
}
*/
// for(int i=0;i<ar.size();++i){
// cout<<endl<<i<<" ";
// for(int j=0;j<ar[i].size();++j){
// cout<<ar[i][j]<<" ....";
// }
// }
/*
// visited array
for(int i=0;i<numCourses;++i){
vis.push_back(0);
recStack.push_back(0);
}
// cout<<endl;
// for(int i=0;i<numCourses;++i){
// cout<<vis[i]<<" rS "<<recStack[i];
// cout<<endl;
// }
// for disconnected components, with optimization
for (auto itr = nodeSet.begin(); itr != nodeSet.end(); itr++) {
if (!vis[*itr] && dfs(*itr,-1,ar)==true)
return false;
}
// for(int i=0;i<numCourses;++i){
// if (!vis[i] && dfs(i,-1,ar)==true)
// return false;
// }
return true;
// Topological sorting gives the order of task to be done in a directed graph
*/
// BETTER APPROACH AVOID COMPILER RECURSION STACK
// vector<int>vis(numCourses,0);
// vector<int>recStack(numCourses,0);
// vector<int>helper(numCourses,0);
// stack<pair<int,int>>s;
// // start dfs fromm node i
// for (auto itr = nodeSet.begin(); itr != nodeSet.end(); itr++) {
// if(!vis[*itr]){
// s.push({*itr,-1});
// // insert pair in stack{node,par},
// while(!s.empty()){
// int ele=s.top().first;
// int par=s.top().second;
// s.pop();
// if(vis[ele]==0){
// vis[ele]=1;
// recStack[ele]=1;
// for(int i=0;i<ar[ele].size();++i){
// int child=ar[ele][i];
// s.push({child,ele});
// ++helper[ele];
// }
// }
// else if(recStack[ele]){
// return false;// cycle found true
// }
// if(par!=-1){
// --helper[par];
// if(helper[par]==0)
// recStack[ele]=0;
// }
// }
// }
// }
// return true;// cycle not found
}
};