-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteGraph.cpp
More file actions
162 lines (125 loc) · 5.29 KB
/
RouteGraph.cpp
File metadata and controls
162 lines (125 loc) · 5.29 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
#include "RouteGraph.h"
#include "readFromFile.hpp"
#include <string>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
RouteGraph::RouteGraph() { }
RouteGraph::RouteGraph(string fileName, AirportList airportList) {
/* gets map of airport locations for weighting */
airportLocations_ = airportList.getMap();
/* creates the graph */
parseRoutes(fileName);
}
vector<RouteDistance> RouteGraph::getAllRoutes() {
/* intiialize vector */
vector<RouteDistance> routes;
/* iterate through vertices */
for (const Vertex& vertex : graph_.getVertices()) {
/* check for next connected component and call BFS */
if (!visitedMap_[vertex]) {
BFS(vertex, routes);
}
}
/* return routes */
return routes;
}
void RouteGraph::BFS(Vertex vertex, vector<RouteDistance>& routes) {
/* declare queue */
queue<Vertex> vertexStorage;
/* initialize beginning values for BFS*/
vertexStorage.push(vertex);
visitedMap_[vertex] = true;
/* continue while there's still vertices to traverse in this component */
while (!vertexStorage.empty()) {
/* get next vertex */
Vertex currVertex = vertexStorage.front();
vertexStorage.pop();
/* iterate through adjacent vertices */
for (const Vertex& adjVertex : graph_.getAdjacent(currVertex)) {
/* check if not visited */
if (!visitedMap_[adjVertex]) {
/* update edge and visited state */
graph_.setEdgeLabel(currVertex, adjVertex, "DISCOVERY");
visitedMap_[adjVertex] = true;
/* add to queue */
vertexStorage.push(adjVertex);
/* add to list of routes */
Edge currEdge = graph_.getEdge(currVertex, adjVertex);
pair<Location, Location> locations = pair<Location, Location>(airportLocations_[currEdge.source], airportLocations_[currEdge.dest]);
routes.push_back(RouteDistance(Route(currEdge.source, currEdge.dest), locations));
}
/* check for and update cross edges */
else if (graph_.getEdgeLabel(currVertex, adjVertex) == "UNEXPLORED") {
/* update label */
graph_.setEdgeLabel(currVertex, adjVertex, "CROSS");
/* add to list of routes */
Edge currEdge = graph_.getEdge(currVertex, adjVertex);
pair<Location, Location> locations = pair<Location, Location>(airportLocations_[currEdge.source], airportLocations_[currEdge.dest]);
routes.push_back(RouteDistance(Route(currEdge.source, currEdge.dest), locations));
}
}
}
}
void RouteGraph::parseRoutes(string fileName) {
/* creates a vector of strings */
vector<string> data = file_to_vector(fileName);
/* iterates through each entry */
for (const string& entry : data) {
/* creates a route object for parsing */
Route route = parseEntry(entry);
/* gets the source and destination */
string source = route.first;
string dest = route.second;
/* edge case check for validity */
if (source == "INVALID" && dest == "INVALID") {
continue;
}
/* adds the vertices to the graph and map if nonexistant in the graph */
if (!graph_.vertexExists(source)) {
graph_.insertVertex(source);
visitedMap_[source] = false;
}
if (!graph_.vertexExists(dest)) {
graph_.insertVertex(dest);
visitedMap_[dest] = false;
}
/* inserts the edge and sets its label */
graph_.insertEdge(source, dest);
graph_.setEdgeLabel(source, dest, "UNEXPLORED");
/* use the map to get the location of the airports */
Location sourceLoc = airportLocations_[source];
Location destLoc = airportLocations_[dest];
/* set the weight of the edge to the distance between the locations */
graph_.setEdgeWeight(source, dest, distance(sourceLoc, destLoc));
}
//std::cout << "there were " << graph_.getEdges().size() << " edges added" << endl;
//std::cout << "there were " << graph_.getVertices().size() << " vertices added" << endl;
}
Route RouteGraph::parseEntry(string entry) {
/* declare needed variables */
string delimiter = ",";
size_t pos = 0;
string token;
vector<string> vals;
/* split by "," and add to vector
from: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c */
while ((pos = entry.find(delimiter)) != std::string::npos) {
token = entry.substr(0, pos);
vals.push_back(token);
entry.erase(0, pos + delimiter.length());
}
/* add final entry */
vals.push_back(entry);
/* return an invalid route if the entry is invalid */
if (vals.size() <= 5) {
return Route("INVALID", "INVALID");
}
/* return the correct route otherwise */
return Route(vals[3], vals[5]);
}
/* --------------------- getters and setters --------------------- */
int RouteGraph::getNumAirports() { return (int) graph_.getVertices().size(); }
int RouteGraph::getNumConnections() { return (int) graph_.getEdges().size(); }
Graph RouteGraph::getGraph() { return graph_; }