-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrereqCheck.cpp
135 lines (114 loc) · 3.03 KB
/
PrereqCheck.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
#include <bits/stdc++.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
bool isCyclicUtil(string dependency, unordered_set<string>& visited, unordered_set<string>& stack);
class PrereqCheck {
private:
unordered_map<string, vector<string>> uMap;
public:
void add(string key, string value, bool b) {
if(b) {
uMap[key].push_back(value);
} else {
uMap[key];
}
}
unordered_map<string, vector<string>>& returnMap(){
return uMap;
}
void printMap() {
for (const auto& course_pair : uMap) {
const string& course_name = course_pair.first;
const vector<string>& prerequisites = course_pair.second;
cout << course_name << ": ";
for (const auto& prerequisite : prerequisites) {
cout << prerequisite << " ";
}
cout << endl;
}
}
bool isCyclic() {
unordered_set<string> visited;
unordered_set<string> stack;
for(auto& key: uMap){
string dependency = key.first;
if(isCyclicUtil(dependency, visited, stack)) {
return true;
}
}
return false;
}
bool isCyclicUtil(string dependency, unordered_set<string>& visited, unordered_set<string>& stack) {
if (stack.count(dependency)) {
return true; // Found a cycle
}
if (visited.count(dependency)) {
return false; // Already visited this node and found no cycle
}
visited.insert(dependency);
stack.insert(dependency);
for(auto& course : uMap[dependency]){
string loop = course;
if(isCyclicUtil(loop, visited, stack)){
return true;
}
}
stack.erase(dependency);
return false;
}
void readFile(string filename) {
regex course_regex("[A-Z]{2,4}_[0-9]{3}");
ifstream myfile(filename);
if (!myfile.is_open()) {
cout << "Failed to open file: " << filename << endl;
return;
}
string line;
int linenum, wordnum = 0;
while(getline(myfile, line)) {
linenum++;
wordnum = 0;
stringstream location(line);
string cmds;
string currentindex;
while(location >> cmds) {
wordnum++;
if(regex_match(cmds, course_regex)) {
cout << "Valid course name: " << cmds << endl;
if(wordnum == 1){
currentindex = cmds;
add(currentindex, cmds, false);
} else if(wordnum != 1) {
add(currentindex, cmds, true);
if(isCyclic()){
cout << "The prerequisite " << cmds << " is creating a cycle" << endl;
exit(0);
}
}
} else {
cout << "Invalid course name: " << cmds << endl << "Ending program..." << endl;
exit(0);
}
}
}
myfile.close();
}
};
int main(int argc, char* argv[]){
if(argc != 2){
cout << "Incorrect # of args" << endl;
exit(0);
}
PrereqCheck P;
string prereqfile = argv[1];
//cin >> prereqfile;
P.readFile(prereqfile);
return 1;
};