-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
282 lines (234 loc) · 8.46 KB
/
main.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
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
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include "Airport.h"
#include "Route.h"
#include "bfs.h"
#include "Dijkstra.h"
#include "Centrality.h"
#include "Projection.h"
#include "test.h"
using namespace std;
//helper function to get Index of a given element in a vector
int getintIndex(vector<int> v, int k) {
auto it = find(v.begin(), v.end(), k);
if(it != v.end()) {
int index = it - v.begin();
return index;
}
else {
return -1;
}
}
int getIndexName(vector<string> v, string k) {
auto it = find(v.begin(), v.end(), k);
if(it != v.end()) {
int index = it - v.begin();
return index;
}
else {
return -1;
}
}
int main()
{
testBFS1();
testBFS2();
testBFS3();
testDijkstra1();
testDijkstra2();
testDijkstra3();
testCentrality1();
testCentrality2();
//read the file directly
vector<string> airportNames;
vector<double> latitudeValues;
vector<double> longtitudeValues;
vector<int> airportID;
vector<int> SourceID;
vector<int> DestinationID;
//vectors that used to set up graph
vector< vector <double> > adjacencyMatrix;
//vectors that used to set up for centrality
vector<int> traversed;
vector<int> centrality;
vector<double> display;
vector< vector <double> > largestComponent;
ifstream airportsFile("airports.dat");
ifstream routesFile("routes.dat");
//set up some string for reading
string AirportID, Name, City, Country, IATA, ICAO, Latitude, Longitude;
string myString, line;
string Airline, AirlineID, SourceAirport, SourceIDs, DestinationAirport, DestinationIDs;
if (airportsFile.is_open()) {
/* Reads a line from `wordsFile` into `word` until the file ends. */
while (getline(airportsFile, line)) {
stringstream ss(line);
getline(ss, AirportID, ',');
getline(ss, Name, ',');
getline(ss, City, ',');
getline(ss, Country, ',');
getline(ss, IATA, ',');
getline(ss, ICAO, ',');
getline(ss, Latitude, ',');
getline(ss, Longitude, ',');
stringstream la(Latitude);
stringstream lo(Longitude);
stringstream id(AirportID);
double Lati;
la >> Lati;
double Longi;
lo >> Longi;
int ID;
id >> ID;
airportNames.push_back(Name.substr(1, Name.size() - 2));
latitudeValues.push_back(Lati);
longtitudeValues.push_back(Longi);
airportID.push_back(ID);
}
airportsFile.close();
}
if (routesFile.is_open()) {
while (getline(routesFile, line)) {
stringstream ss(line);
getline(ss, Airline, ',');
getline(ss, AirlineID, ',');
getline(ss, SourceAirport, ',');
getline(ss, SourceIDs, ',');
getline(ss, DestinationAirport, ',');
getline(ss, DestinationIDs, ',');
stringstream sID(SourceIDs);
stringstream dID(DestinationIDs);
int sourceID;
sID >> sourceID;
int destinationID;
dID >> destinationID;
SourceID.push_back(sourceID);
DestinationID.push_back(destinationID);
}
routesFile.close();
}
//Graph Establishment Part
//set up a 7698 * 7698 square matrix
int airportsNum = airportID.size();
adjacencyMatrix.resize(airportsNum);
for(unsigned int i = 0; i < adjacencyMatrix.size(); i++) {
adjacencyMatrix[i].resize(airportsNum);
}
//now fill the adjacency matrix with distance, successfully establish the graph
for (unsigned int i = 0; i < SourceID.size(); i++) {
int sourceIndex = getintIndex(airportID, SourceID[i]);
int destinationIndex = getintIndex(airportID, DestinationID[i]);
if(sourceIndex == -1 || destinationIndex == -1) {
continue;
}
Airport a1(airportNames[sourceIndex], airportID[sourceIndex], latitudeValues[sourceIndex], longtitudeValues[sourceIndex]);
Airport a2(airportNames[destinationIndex], airportID[destinationIndex], latitudeValues[destinationIndex], longtitudeValues[destinationIndex]);
Route r(a1, a2);
adjacencyMatrix[sourceIndex][destinationIndex] = r.getDist();
}
//BFS I/O part
int ID;
cout << "Please enter an airport ID that you want to traverse: ";
cin >> ID;
//error handling part
if(getintIndex(airportID, ID) == -1) {
cout << "Please input a valid airport ID! " << endl;
return -1;
} else {
BFS bfs(adjacencyMatrix, airportNames);
bfs.traverse(getintIndex(airportID, ID));
bfs.printBFS();
cout << "There are " << bfs.returnNum() << " airports you have traversed using BFS" << endl;
traversed = bfs.output();
}
//Dij I/O part
int startID;
int destinationID;
cout << "Welcome to the airports search progrom" << endl;
cout << "Please enter an airport ID as start: ";
cin >> startID;
int start = getintIndex(airportID, startID);
cout << "Please enter an airport ID as destination: ";
cin >> destinationID;
int end = getintIndex(airportID, destinationID);
Dijkstra dij(adjacencyMatrix, airportNames, start);
dij.DijkstraAlgo(start);
dij.returnDistance(end);
vector<string> path = dij.printPath(end);
vector<Airport> airport_s;
for (unsigned int i = 0; i < path.size(); ++i) {
auto index = getIndexName(airportNames, path[i]);
Airport air(path[i], airportID[index], latitudeValues[index], longtitudeValues[index]);
airport_s.push_back(air);
}
/*
int a = getintIndex(airportID,3364);
int b = getintIndex(airportID,2560);
cout << adjacencyMatrix[a][b] << endl;
cout << " " <<endl;
*/
//Centrality I/O part
cout<< "Starting test matrix for betweeness centrality, expected impact of each node is {3, 4, and 2} for given test case graph" << endl;
vector< vector <double> > testMatrix;
testMatrix.resize(3);
for(unsigned int i = 0; i < testMatrix.size(); i++) {
testMatrix[i].resize(3);
}
testMatrix = {
// {0,10,3,0,0},
// {0,0,1,2,0},
// {0,4,0,8,2},
// {0,0,0,0,7},
// {0,0,0,9,0}
{0,1,0},
{1,0,0},
{0,1,0}
};
Centrality central(testMatrix);
central.betweenCentral(0);
centrality = central.returnCentral();
for(unsigned int i = 0; i < centrality.size(); i++) {
cout << centrality[i] << endl;
}
int switch_;
cout << "Please enter 1 to start, it may take a while: ";
cin >> switch_;
if(switch_ == 1){
cout << "Constructing the largestcomponent matrix, largest CC known contains 3166 vertices, please check at BFS count" << endl;
largestComponent.resize(traversed.size());
for(unsigned int j = 0; j < largestComponent.size(); j++) {
largestComponent[j].resize(traversed.size());
}
for(unsigned int m = 0; m < largestComponent.size(); m++) {
for(unsigned int n = 0; n < largestComponent[m].size(); n++) {
largestComponent[m][n] = adjacencyMatrix[traversed[m]][traversed[n]];
}
}
cout << "construction complete with dimensions" << largestComponent.size() << "," << largestComponent[0].size()<< endl;
Centrality centralnew1(largestComponent);
centralnew1.betweenCentral(0);
centrality = centralnew1.returnCentral();
display.resize(centrality.size());
for(unsigned int a = 0; a < display.size(); a++) {
display[a] = centrality[a];
}
cout << "Among the airports you choose, Here are the results for each airport's time of includes in shortest paths: " << endl;
for(unsigned int i = 0; i < display.size(); i++) {
cout << airportNames[traversed[i]] << " : " << display[i] << endl;
}
}
//cout << "bfs count" << endl;
Projection prj;
for (unsigned int i = 0; i < path.size(); ++i) {
prj.drawPoint(airport_s[i], (double)i / 10.0);
}
for (unsigned int j = 0; j < path.size() - 1; ++j) {
prj.drawLine(airport_s[j], airport_s[j + 1]);
}
prj.writeToMap();
return 0;
}