-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_func.cpp
94 lines (83 loc) · 2.78 KB
/
BFS_func.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
#include<fstream>
#include"support_functions.hpp"
#include<unistd.h>
#include"json.hpp"
using json = nlohmann::json;
int main(int argc, char** argv){
map<string, vector<string>> graph;
argument vm = parsing_argv(argc, argv);
string startNode = vm.start;
string endNode = vm.end;
string filename = vm.graph;
string outfile = vm.outfile;
string distance_information = vm.distance_information;
int distance_only = vm.flag_d;
json dict_dist;
try{
if(vm.start == "NULL" || vm.end == "NULL"){
cout << "Please insert both start and end nodes!\n";
return 1;
}
if(filename != "NULL"){
ifstream file(filename);
if(!file){
cout << "Filename of graph does not exist!" << endl;
return 1;
}
else{
ifstream f(filename);
json json_dict = json::parse(f);
for(auto i :json_dict.items()){
graph[i.key()] = json_value_to_vector(json_dict[i.key()]);
}
}
}
else{
cout << "Using demo graph" << endl;
filename = "demo_graph.json";
ifstream f(filename);
if(!f){
cout << "'demo_graph.json' file is not in this directory!" << endl;
return 1;
}
else{
json json_dict = json::parse(f);
for(auto i :json_dict.items()){
graph[i.key()] = json_value_to_vector(json_dict[i.key()]);
}
}
}
if(distance_information != "NULL"){
ifstream dist_file(distance_information);
if(!dist_file){
cout << "Filename of distance information does not exist!" << endl;
return 1;
}
else{
cout << "Using distance dictionary to accelerate the BFS" << endl;
dict_dist = json::parse(dist_file);
}
}
}
catch(exception& e){
cerr << "error: " << e.what() << "\n";
return 1;
}
cout << "Start node:\t" << startNode << endl << "End node:\t" << endNode << endl;
Shortest_path x = BFS(graph, startNode, endNode, distance_only, dict_dist);
cout << "\nShortest distance is:\t" << x.distance << endl;
if(distance_only){
cout << "Searching shortest paths is not excecuted.\n";
}
else{
cout << "Shortest paths are: \n";
print_2d_array(x.path);
}
if(outfile != "NULL"){
nlohmann::ordered_json output = result_to_json(x);
ofstream file(outfile);
file << output.dump(2);
cout << "Exported result to file name:\t" << outfile << endl;
}
return 0;
}