-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.cpp
More file actions
207 lines (185 loc) · 5.26 KB
/
classes.cpp
File metadata and controls
207 lines (185 loc) · 5.26 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
#include "classes.h"
#include <iostream>
#include <climits>
#include <cmath>
#include <queue>
using std::cout;
using std::endl;
Route::Route(){
line_= 0;
stops_ = 0;
source_ = 0;
destination_ = 0;
code_ = false;
equipment_ = vector<string>();
}
Route::Route(int line,bool code,int source, int destination,int stops, vector<string> equipment){
line_= line;
stops_ = stops;
source_ = source;
destination_ = destination;
code_ = code;
equipment_ = equipment;
}
Plane::Plane(){
name_ = "";
IATA_ = "";
ICAO_ = "";
}
Plane::Plane(string name,string IATA,string ICAO){
name_ = name;
IATA_ = IATA;
ICAO_ = ICAO;
}
Airline::Airline() {
ID_ = 0;
name_ = "";
alias_ = "";
IATA_ = "";
ICAO_ = "";
country_ = "";
active_ = false;
}
Airline::Airline(int ID,string name,string alias, string IATA,string ICAO,string country,bool active){
ID_ = ID;
name_ = name;
alias_ = alias;
IATA_ = IATA;
ICAO_ = ICAO;
country_ = country;
active_ = active;
}
Airport::Airport(){
ID_ = -1;
name_ = "";
city_ = "";
country_ = "";
IATA_ = "";
ICAO_ = "";
latitude_ = 0;
longitude_ = 0;
}
Airport::Airport(int ID,string name,string city,string country, string IATA,string ICAO,double latitude,double longitude){
ID_ = ID;
name_ = name;
city_ = city;
country_ = country;
IATA_ = IATA;
ICAO_ = ICAO;
latitude_ = latitude;
longitude_ = longitude;
}
void Airport::addRoute(Route route){
routes_.push_back(route);
}
Graph::Graph(){
airports_ = vector<Airport>();
count = 0;
for(unsigned i =0;i<airports_.size();i++){
visited_.push_back(false);
}
}
Graph::Graph(vector<Airport> airports){
airports_ = airports;
count = 0 ;
for(unsigned i =0;i<airports_.size();i++){
visited_.push_back(false);
}
}
void Graph::DFT(Airport start){
visited_[start.ID_] = true;
for (unsigned i = 0; i < start.routes_.size(); i++) {
//departing routes
//map openFlights id to airport ID
int idx = start.routes_[i].destination_;
if(!visited_[idx]){
count++;
cout<<airports_[idx].name_<<endl;
//recurse through all nodes
DFT(airports_[idx]);
}
}
}
//disconnected implementation of Depth First Traversal
void Graph::DFTDis() {
//loops through all elements
for(unsigned i =0;i<airports_.size();i++){
//visits only the ones not already visited
//this
if(visited_[i] == false){
DFT(airports_[i]);
}
}
}
//Djikstra's algorithm using a priority queue
std::pair<double, vector<Airport>> Graph::shortestPath(Airport src,
Airport dest) {
vector<double> dist; //current distance to all other nodes
vector<Airport> previous; //for each node, stores the previous node in the traversal
vector<bool> visited; //if a node is visited
// priority queue used to store nodes, max head by default, so weights
// are inverted
std::priority_queue<std::pair<double, int>> q;
//loop through all airports
for (unsigned i = 0; i < airports_.size(); i++) {
if (airports_[i].ID_ != src.ID_) {
dist.push_back(-INT_MAX); //push back -infinity
previous.push_back(Airport()); //empty airport
} else {
dist.push_back(0);//push zero on source airport
previous.push_back(Airport()); //push empty airport
}
visited.push_back(false); //clear visited
}
//pushes soure and its distance to queue
q.push(std::pair<double, int>(0, src.ID_));
//until we've reached the destination
while (q.top().second != dest.ID_) {
//get top and pop it
int u = q.top().second;
q.pop();
visited[u] = true; //visit node
//for each edge in current node
for (unsigned i = 0; i < airports_[u].routes_.size(); i++) {
int dest_idx = airports_[u].routes_[i].destination_; //get destination index
//get from current node to desination
double currLength = linDist(airports_[u], airports_[dest_idx]);
//if calculated distance is smaller than current calculated distance for destination and not visited
if (dist[u] + currLength < -dist[dest_idx] && !visited[dest_idx]) {
//calculate new best distance
dist[dest_idx] = (dist[u] + currLength);
previous[dest_idx] = airports_[u]; //pass current node into previous
//push node into queue
//queue will sort from closest to farthest
q.push(std::pair<double, int>(-dist[dest_idx], dest_idx));
}
}
}
//start at destination
Airport curr = dest;
vector<Airport> path;
//extract all airports in previous vector
while (curr.ID_ != -1) {
path.push_back(curr);
curr = previous[curr.ID_];
}
//return shortest distance and path of airports
return make_pair(-q.top().first, path);
}
//helper function for linDist
double Graph::haversine(double theta){
return pow(sin(theta/2.0),2);
}
//great Circle distance between two airports
double Graph::linDist(Airport u,Airport v){
double lat1 = u.latitude_;
double lon1 = u.longitude_;
double lat2 = v.latitude_;
double lon2 = v.longitude_;
const double rad = 0.01745329; // conversion degrees to radians
double a = haversine(rad * (lat2 - lat1));
double b = 1 - a - haversine(rad * (lat1 + lat2));
double c = haversine(rad * (lon2 - lon1));
const double r = 6371; // radius of the earth in KM
return 2 * r * asin(sqrt(a + (b * c)));
}