-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
351 lines (265 loc) · 11.4 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
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
// D O W N W I T H M A T T H E W A H N
#include <fstream>
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include "line.h"
#include "station.h"
#include "time.h"
#include "algos.h"
// Ahn's starting point: Far Rockaway Mott (id: 54) 2:02am
// Ahn's ending point: Flushing Main Street
//CHANGED STATION INITIALIZATON BECAUSE STATION ID WAS ARBITRARY
using std::size_t;
void dfs(std::vector<std::vector<int>> adj_matrix, int current, std::vector<bool> &visited, std::vector<int> &path) {
visited[current] = true;
path.push_back(current);
// 202 -> 209
// if (current == 202 && !visited[209] && !visited[201]) { dfs(adj_matrix, 209, visited, path); }
// else{
for (int i = 0; i < adj_matrix[current].size(); i++) {
if (adj_matrix[current][i] == 1 && !visited[i]) {
dfs(adj_matrix, i, visited, path);
}
}
// }
}
std::vector<int> dfsPath(int start, std::vector<std::vector<int>> adj_matrix) {
std::vector<bool> visited(adj_matrix.size(), false);
std::vector<int> path;
dfs(adj_matrix, start, visited, path);
return path;
}
int main(int argc, char* argv[]) {
// check for 4 arguments
if (argc != 3) {
std::cerr << "You provided " << argc << "arguments intead of 3.\n";
exit(1);
}
//Rewritten station file read into a vector
std::ifstream myfile(argv[2]);
std::vector<std::string> lineData;
std::string line;
while (std::getline(myfile, line)) {
lineData.push_back(line);
}
//Map to store station id's and names for easy access
std::map<int, std::string> stationInfo;
//Parse station info
for (int i = 0; i < lineData.size(); i++) {
if (lineData[i].find("station") != std::string::npos) {
//Find index of station
std::string indexStr = lineData[i].substr(lineData[i].rfind(": ") + 2);
int index = std::stoi(indexStr);
//Find name of station
int endIndex = lineData[i].find(",") - lineData[i].find(": ") - 2;
std::string name = lineData[i].substr(lineData[i].find(": ") + 2, endIndex);
//Add station information to map
if (stationInfo.find(index) == stationInfo.end()) {
stationInfo.insert({index, name});
}
}
}
// //Test print station info
// for (auto i = stationInfo.begin(); i != stationInfo.end(); ++i) {
// if (i->first != counter) {
// std::cout << i->first << " " << i->second << std::endl;
// }
// }
// //Missing stations test print
// int counter = 0;
// for (int i = 1; i < 451; i++) {
// if (stationInfo.find(i) == stationInfo.end()) {
// std::cout << i << std::endl;
// counter++;
// }
// }
// std::cout << "size: " << stationInfo.size() << " total skipped: " <<
// counter << std::endl;
//KNOWN ISSUES: =====================================================
// SKIPPED STATION IDS: 82, 122, 234-243(inclusive), 261-269(inclusive), 377
// total skipped id count: 22 | actual station count = 428
// Park Pl on the S line in Brooklyn also DOES NOT EXIST -> can add manually
//Matrix representation of stations, edges set to zero
//Zeroth row is empty, using 1-based indexing
std::vector<std::vector<int>> stations (
451,
std::vector<int>(451));
//Set edges
for (int i = 0; i < lineData.size(); i++) {
//Two adjacent stations
if (lineData[i].find("station") != std::string::npos) {
if (lineData[i+1].find("station") != std::string::npos) {
//Find indices of stations
std::string indexStr = lineData[i].substr(lineData[i].rfind(": ") + 2);
int index = std::stoi(indexStr);
std::string indexStr2 = lineData[i+1].substr(lineData[i+1].rfind(": ") + 2);
int indexNext = std::stoi(indexStr2);
//Set edges between adjacent stations
stations[index][indexNext] = 1;
stations[indexNext][index] = 1;
}
}
}
// Manually set walking route possibilities: 207St(A)-207St(1),
// BedfordPkBlvd(4)-BedfordPkBlvd(B/D), 96St(6)-96St(Q)
// 207St - 207St
stations[172][6] = 1;
stations[6][172] = 1;
//BedfordPkBlvd
stations[97][213] = 1;
stations[213][97] = 1;
// 96St - 96St
stations[422][142] = 1;
stations[142][422] = 1;
// New Lots - Van Siclen
stations[385][93] = 1;
stations[93][385] = 1;
// Print adjacency matrix for Gephi visualization
// std::ofstream gephiFile;
// gephiFile.open("gephiSubway.txt");
// gephiFile << "Source,Target" << std::endl;
// for (int i = 0; i != 451; i++){
// for (int j = 0; j != 451; j++){
// if(stations[i][j] == 1){
// gephiFile << i << "," << j << std::endl;
// }
// }
// }
// M A N U A L T R A V E R S A L
//TESTING
// //starting id for testing: 1
// std::vector<int> path = testAlg(stations, 1);
// for (int i = 0; i < path.size(); i++) {
// std::cout << path[i];
// if (i != path.size()-1) { std::cout << " -> "; }
// }
// std::cout << "\n" << std::endl;
// std::cout << "Path size: " << path.size() << std::endl;
// R U N A L G O R I T H M
//Actual start for route: 208, 212, 95, 403, 115
int startStation = 208;
// std::vector<int> sim = aToB(stations, 212, 400);
// std::cout << sim[0] << " :::::: " << sim[sim.size()-1] << std::endl;
// for(int i = 0; i != sim.size(); i++){
// std::cout << i << " : " << sim[i] << std::endl;
// }
// std::vector<int> path = bfs(startStation, stations);
std::vector<int> path = dfsPath(startStation, stations);
std::vector<int> fullPath;
fullPath.push_back(startStation);
for(int i = 1; i != path.size(); i++){
std::vector<int> pathToNew = aToB(stations, path[i-1], path[i]);
for(int j = 0; j != pathToNew.size(); j++){
if(pathToNew[j] != path[i-1] && pathToNew[j] != path[i]){
fullPath.push_back(pathToNew[j]);
}
}
fullPath.push_back(path[i]);
}
// turn path into full path, including revisited stations
// std::vector<int> fullPath;
// for(int i = 0; i != path.size()-1; i++){
// std::vector<int> segment = aToB(path[i], path[i+1], stations);
// fullPath.insert(fullPath.begin(), segment.begin(), segment.end());
// }
std::cout << stations.size() << std::endl;
std::cout << path.size() << std::endl;
for (int i : path) {
std::cout << i << " ";
}
std::cout << std::endl << std::endl << std::endl;
for (int i : fullPath) {
std::cout << i << " ";
}
std::cout << std::endl;
// revisit check
int revisitedCount = 0;
for(int i = 0; i != stations.size(); i++){
int occurCount = 0;
for(int j = 0; j != fullPath.size(); j++){
if(fullPath[j] == i){
occurCount++;
}
}
// std::cout << i << " : " << occurCount << std::endl;
if(occurCount > 1){revisitedCount += occurCount-1;}
}
std::cout << "REVISITED : " << revisitedCount << std::endl;
// ! uncomment for old printing
// //Print result
// for (int i = 0; i < path.size(); i++) {
// std::cout << i+1 << "/" << path.size() << " | " ;
// std::cout << path[i] << " | " << stationInfo[path[i]];
// if (i != path.size()-1 && stations[path[i]][path[i+1]] == 0) {
// std::cout << " => backtrack";
// }
// // if (i != path.size()-1) {
// // std::cout << " => ";
// // }
// std::cout << std::endl;
// }
// std::cout << std::endl;
// int c;
// std::cout << "Please enter the id of a starting station\n";
// int curr;
// std::cin >> curr;
// int timeTravel = 0;
// do {
// stations[curr].setVisited(true); // the station is set as visited now
// std::cout << "\n\nTOTAL TIME ELAPSED: " << timeTravel << "\n";
// std::cout << "Enter -1 to quit\n";
// std::cout << "Your current location is " << stations[curr].getName() << " with ID " << curr << "\n";
// std::cout << "Here are all of the following trips you can take!"<<std::endl<<std::endl;
// // Printing out the possible trip you can take
// std::list<Trip> canGo = stations[curr].getTrips();
// for (const auto &trip: canGo){
// std::cout << "\tStart ID: " << trip.getStart()
// << " End ID: " << trip.getEnd()
// << " Time: " << trip.getDuration()
// << " Line: " << trip.getLineName() // ! what if its a walking route
// << " Visited: " << stations[trip.getEnd()].isVisited()
// // HEURISTIC NEEDS TWO ARGUMENTS, STARTING STATION AND ENDING STATION
// // manual traversal currently passes in the same station for both args
// // WILL NEED TO CHANGE FOR ACTUAL ALGORITHM
// << " Heuristic: " << heuristic(stations, trip.getStart(), trip.getStart()) << "\n";
// }
// std::cout << "\nEnter the ID of the station you would like to travel to"<<std::endl;
// std::cin >> c;
// if (c != -1) {
// bool isInputValid = false; // used to check if user's choice is a trip option suggested
// for (const auto &trip: canGo){
// if (trip.getEnd() == c) {
// timeTravel += trip.getDuration();
// isInputValid = true;
// break;
// }
// }
// if (isInputValid == false){
// std::cout<<"---this station number is not a trip suggested above.---"<<std::endl;
// }
// }
// curr = c;
// } while(c != -1);
}
// void createTrips(){
// for(const auto &line: lines){
// for(size_t i = 0; i < line.getNumStations()-1; i++){
// //get each station
// int stationOne = line.getStation(i);
// int stationTwo = line.getStation(i+1);
// double distance = realDistance(stations[stationOne],stations[stationTwo]);
// double duration = (1.2*60*distance)/17.4; // arbitrary until we figure out normal speed
// Trip f (stationOne, stationTwo, duration, 't');
// Trip b (stationTwo, stationOne, duration, 't');
// f.setLine(line.getName());
// b.setLine(line.getName());
// // add trips to the first station.
// stations[stationOne].addTrip(f);
// stations[stationTwo].addTrip(b);
// stations[stationOne].addLine(line.getName(), i);
// }
// }
// }