-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_210_CourseScheduleII.cpp
153 lines (114 loc) · 3.69 KB
/
_210_CourseScheduleII.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
/* Source - https://leetcode.com/problems/course-schedule-ii/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
bool dfs(vector<vector<int>>& graph, unordered_set<int>& visited, vector<bool>& recStack, stack<int>& st, int src) {
visited.insert(src);
recStack[src] = true;
for(int i = 0; i < graph[src].size(); i++) {
int nbr = graph[src][i];
if(recStack[nbr])
return true;
if(visited.find(nbr) == visited.end()) {
bool answer = dfs(graph, visited, recStack, st, nbr);
if(answer)
return true;
}
}
st.push(src);
recStack[src] = false;
return false;
}
// approach 1
vector<int> findOrderUsingDFS(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
for(int i = 0; i < prerequisites.size(); i++)
graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
unordered_set<int> visited;
stack<int> st;
vector<bool> recStack(numCourses);
for(int i = 0; i < graph.size(); i++) {
if(visited.find(i) == visited.end()) {
bool answer = dfs(graph, visited, recStack, st, i);
if(answer == true)
return {};
}
}
if(visited.size() != numCourses)
return {};
vector<int> result;
while(!st.empty()) {
result.push_back(st.top());
st.pop();
}
return result;
}
// approach 2
vector<int> findOrderUsingKahnsAlgo(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
for(int i = 0; i < prerequisites.size(); i++)
graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
vector<int> indegree(numCourses);
for(int i = 0; i < graph.size(); i++) {
for(int j = 0; j < graph[i].size(); j++)
indegree[graph[i][j]]++;
}
queue<int> q;
int count = 0;
for(int i = 0; i < indegree.size(); i++) {
if(indegree[i] == 0) {
q.push(i);
count++;
}
}
vector<int> result;
while(!q.empty()) {
int front = q.front();
q.pop();
result.push_back(front);
for(int i = 0; i < graph[front].size(); i++) {
int nbr = graph[front][i];
if(--indegree[nbr] == 0) {
q.push(nbr);
count++;
}
}
}
if(count != numCourses)
return {};
return result;
}
int main()
{
int numCourses;
cout<<"Enter number of courses: ";
cin>>numCourses;
int p;
cout<<"Enter number of prerequisite pairs: ";
cin>>p;
vector<vector<int>> prerequisites(p);
int f, s;
cout<<"Enter prerequisite pairs: "<<endl;
for(int i = 0; i < p; i++) {
cin>>f>>s;
prerequisites[i].push_back(f);
prerequisites[i].push_back(s);
}
vector<int> result = findOrderUsingDFS(numCourses, prerequisites);
cout<<"Correct ordering of courses to finish all courses (using DFS - recursive): [";
if(result.size() > 0) {
for(int i = 0; i < result.size() - 1; i++)
cout<<result[i]<<", ";
cout<<result[result.size() - 1];
}
cout<<"]"<<endl;
result = findOrderUsingKahnsAlgo(numCourses, prerequisites);
cout<<"Correct ordering of courses to finish all courses (using Kahn's Algo - iterative): [";
if(result.size() > 0) {
for(int i = 0; i < result.size() - 1; i++)
cout<<result[i]<<", ";
cout<<result[result.size() - 1];
}
cout<<"]"<<endl;
}