-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
425 lines (337 loc) · 13.9 KB
/
main.cpp
File metadata and controls
425 lines (337 loc) · 13.9 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <iostream>
#include "Parse.h"
#include "Graph.h"
using namespace std;
void print_usage() {
cerr << "Usage : " << endl <<
"\t" << "-i <input_file>, --infile <input_file> to pass an input filename\n\n" <<
"\t" << "-o <output_file>, --ofile <output_file> to pass an output filename\n\n" <<
"\t" << "use --strongly-connected to get strongly connected component analysis\n\n"<<
"\t" << "use --bfs-search <start_index> <end_index> to conduct a breadth-first search starting at start and searching for end\n\n"<<
"\t" << "use --bfs-traversal <start_index> to conduct a traversal starting at start and reaching every connected node\n\n"<<
"\t" << "use --bfs-comprehensive to conduct a traversal reaching every node on graph\n\n"<<
"\t" << "use --dijkstra-search <start_index> <end_index> to run dijkstras and get shortest path,distance from start to end\n\n"<<
"\t" << "use --dijkstra-traversal <start_index> to run dijkstras and get shortest path,distance from start to every single node\n\n"<<
"\t" << "use --predict-rating <start_index> <end_index> to predict what the start node would rate the end node\n\n" ;
}
enum Options {
Help,
InputFile,
OutputFile,
StronglyConnected,
BFSSearch,
BFSTraverse,
BFSComprehensive,
DijkstraSearch,
DijkstraTraverse,
predictRating,
Option_Invalid
};
Options resolveOption(string argument) {
if (argument == "-h") return Help;
if (argument == "-i" || argument == "--infile") return InputFile;
if (argument == "-o" || argument == "--ofile") return OutputFile;
if (argument == "--strongly-connected") return StronglyConnected;
if (argument == "--bfs-search") return BFSSearch;
if (argument == "--bfs-traversal") return BFSTraverse;
if (argument == "--bfs-comprehensive") return BFSComprehensive;
if (argument == "--dijkstra-search") return DijkstraSearch;
if (argument == "--dijkstra-traversal") return DijkstraTraverse;
if(argument == "--predict-rating") return predictRating;
return Option_Invalid;
}
int main(int argCount , char * argv[]) {
Graph g;
Parse parser;
string ifile, ofile;
bool hasInFile = false ;
bool hasOutFile = false ;
map<string,bool> args;
int start = 0, end = 0;
if(argCount < 2 ) {
print_usage();
exit(2);
} else {
for(int i = 1 ; i < argCount ; i++) {
string argument = argv[i] ;
switch(resolveOption(argument)) {
case Help: {
print_usage();
exit(0);
break;
}
case InputFile: {
if (i + 1 < argCount) {
string filename = argv[i + 1];
fstream infile(filename);
if (infile.is_open()) {
hasInFile = true;
ifile = filename;
i++;
} else {
cerr << "Please provide a valid inputfile name\n" << endl;
exit(1);
}
} else {
cerr << "Must include inputfile name after the -i or --infile flag\n" << endl;
exit(1);
}
break;
}
case OutputFile: {
if (i + 1 < argCount) {
string filename = argv[i + 1];
hasOutFile = true;
ofile = filename;
i++;
} else {
cerr << "Must include the outputfile name after the -o or --ofile flag\n" << endl;
exit(1);
}
break;
}
case StronglyConnected: {
args["strongly-connected"] = true;
break;
}
case BFSSearch: {
if (i + 2 < argCount) {
args["bfs-search"] = true;
start = stoi(argv[i + 1]);
end = stoi(argv[i + 2]);
} else {
cerr << "Must provide start and end indexes for --bfs-search\n" << endl;
}
i += 2;
break;
}
case BFSTraverse: {
if (i + 1 < argCount) {
args["bfs-traversal"] = true;
start = stoi(argv[i + 1]);
} else {
cerr << "Must provide start index for --bfs-traversal\n" << endl;
exit(1);
}
i++;
break;
}
case BFSComprehensive: {
args["bfs-comprehensive"] = true;
break;
}
case DijkstraSearch: {
if (i + 2 < argCount) {
args["dijkstra-search"] = true;
start = stoi(argv[i + 1]);
end = stoi(argv[i + 2]);
} else {
cerr << "Must provide start index and end index for --dijkstra-search\n" << endl;
exit(1);
}
i += 2;
break;
}
case DijkstraTraverse: {
if (i + 1 < argCount) {
args["dijkstra-traversal"] = true;
start = stoi(argv[i + 1]);
} else {
cerr << "Must provide start index for --dijkstra-traversal\n" << endl;
exit(1);
}
i++;
break;
}
case predictRating : {
if(i+2 < argCount) {
args["predict-rating"] = true ;
start = stoi(argv[i+1]) ;
end = stoi(argv[i+2]) ;
}
else {
cerr << "Must provide start and end indexes for the --predict-rating flag \n" ;
exit(1) ;
}
i+=2 ;
break ;
}
case Option_Invalid: {
cerr << "Invalid flag" << endl;
exit(1);
break;
}
}
}
}
//default infile if one isn't provided
if(!hasInFile) {
ifile = "graph.csv" ;
fstream def(ifile) ;
if(!def.is_open()) {
cerr<< "No input filename provided, default filename -graph.csv- does not exist in current directory\n" ;
exit(2) ;
}
cout << "No input filename provided, using graph.csv\n" << endl ;
}
//default outfile if one isn't provided
if(!hasOutFile) {
ofile = "graph_output" ;
cout << "No output filename provided, using graph_output\n" ;
}
parser.readFromFile(ifile,g) ;
if(start < 0 || end < 0 || start > int(g.getSize()) || end > int(g.getSize())) {
cerr << "Please enter valid start/end node indexes\n" << endl ;
exit(2) ;
}
for(auto & p : args) {
if(p.first == "bfs-search" && p.second == true) {
if(start == 0 || end == 0 ){
cerr << "Must provide start valid and end node indexes after the --bfs-search flag " << endl ;
exit(1) ;
}
vector<int> path = g.bfsSearch(start,end) ;
fstream out(ofile+"_bfs_search.txt", ios_base::out) ;
cout << "{ " ;
for(int & i : path) {
cout << i << ", " ;
}
cout << "}" << endl ;
out << "{ " ;
for(int & i : path) {
out << i << ", " ;
}
out << "}" << endl ;
out.close() ;
}
if(p.first == "bfs-traversal" && p.second == true) {
if(start == 0) {
cerr << "Must provide valid start index after --bfs-traversal" << endl ;
exit(1) ;
}
vector<int> backtrack = g.bfsTraversal(start) ;
fstream out(ofile+"_bfs_traversal.txt", ios_base::out) ;
vector<vector<int>> allPaths = {{-1}};
for(size_t i = 1 ; i < g.getSize() ; i++) {
vector<int> shortestPath;
if (backtrack[i] == -1) {
shortestPath = {-1};
} else {
shortestPath = {(int) i};
int currentNode = i;
while (currentNode != start) {
int previousNode = backtrack[currentNode];
shortestPath.insert(shortestPath.begin(), previousNode);
currentNode = previousNode;
}
}
allPaths.push_back(shortestPath);
}
//out << "Shortest path from " << start << " to each other node" << endl;
for (unsigned i = 1; i < allPaths.size(); i++) {
out << "{";
for (unsigned j = 0; j < allPaths[i].size(); j++) {
out << allPaths[i][j];
if (j < allPaths[i].size() - 1) {
out << ", ";
}
}
out << "}" << endl;
}
out.close();
}
if(p.first == "bfs-comprehensive" && p.second == true) {
vector<int> path = g.comprehensiveBFSTraversal() ;
fstream out(ofile+"_bfs_comprehensive.txt",ios_base::out) ;
out << "Every traversal represented as one (backtrackable) path vector" << endl ;
out << "{ " ;
for(int & i : path) {
out << i << ", " ;
}
out << "}" << endl ;
out.close() ;
}
if(p.first == "dijkstra-search" && p.second == true) {
if(start == 0 || end == 0 ){
cerr << "Must input both valid start and end node indexes after the --dijkstra-search flag" << endl ;
exit(1) ;
}
pair<vector<int>,int> djk = g.dijkstraSearch(start,end) ;
fstream out(ofile + "_dijkstra_search.txt", ios_base::out) ;
vector<int>& path = djk.first ;
int & distance = djk.second ;
cout << "Shortest path from " << start << " to " << end << " has distance: " << distance << endl ;
cout << "{" ;
for(auto & v : path) {
cout << v << ", " ;
}
cout << "}" << endl ;
out<< "Shortest path from " << start << " to " << end << endl ;
out<< "{" ;
for(auto & v : path) {
out<< v << ", " ;
}
out<< "}" << endl ;
out.close() ;
}
if(p.first == "dijkstra-traversal" && p.second == true) {
if(start == 0 ) {
cerr << "Must provide valid start index after --dijkstra-traversal" << endl ;
exit(1) ;
}
pair<vector<int>, vector<int>> djk = g.dijkstraTraverse(start) ;
vector<int> backtrack = djk.first ;
vector<int> distances = djk.second ;
fstream out(ofile+"_dijkstra_traversal.txt", ios_base::out) ;
vector<vector<int>> allPaths = {{-1}};
for(size_t i = 1 ; i < g.getSize() ; i++) {
vector<int> shortestPath;
if (backtrack[i] == -1) {
shortestPath = {-1};
} else {
shortestPath = {(int) i};
int currentNode = i;
while (currentNode != start) {
int previousNode = backtrack[currentNode];
shortestPath.insert(shortestPath.begin(), previousNode);
currentNode = previousNode;
}
}
allPaths.push_back(shortestPath);
}
//out << "Shortest path from " << start << " to each other node: " << endl;
for (unsigned i = 1; i < allPaths.size(); i++) {
out << "{";
for (unsigned j = 0; j < allPaths[i].size(); j++) {
out << allPaths[i][j];
if (j < allPaths[i].size() - 1) {
out << ", ";
}
}
out << "} : " << distances[i] << endl;
}
out.close();
}
if(p.first == "strongly-connected" && p.second == true) {
g.SCCAlgorithm() ;
vector<vector<int>> sccs = g.scc ;
fstream out(ofile+"_sccs.txt",ios_base::out) ;
for(auto & scc : sccs) {
out << "{ " ;
for(auto & v : scc) {
out << v << ", " ;
}
out << "} " << endl;
}
out.close() ;
}
if(p.first == "predict-rating" && p.second == true) {
if(start == 0 || end == 0 ) {
cerr << "Must input valid node indexes for the --predict-rating flag\n" ;
exit(1) ;
}
double predicted_rat = g.predictRating(start,end) ;
std::cout << "Prediction says " << start << " would rate " << end << " : " << predicted_rat << endl ;
}
}
}