-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
250 lines (213 loc) · 6.66 KB
/
graph.h
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
/**
* This is graph.h file which is used to construct the basic variables used for functions
*/
#pragma once
#include <vector>
#include <list>
#include <string>
#include <map>
#include <iostream>
#include "read.h"
using namespace std;
class Graph {
public:
struct Vertex {
string vertex_id;
double latitude;
double longitude;
Vertex()
: vertex_id(""), latitude(0), longitude(0)
{/* empty*/}
Vertex(string airportId)
: vertex_id(airportId), latitude(0), longitude(0)
{/* empty*/}
/**
* Constructor with vertex
* @param sourceAirport - sourceAirport
* @param destinationAirport - destinationAirport
*/
Vertex(string airportId, double latitudeArg, double longitudeArg) {
vertex_id = airportId;
latitude = latitudeArg;
longitude = longitudeArg;
}
/**
* Get vertex_id of Vertex
*/
string getVertexId() {
return vertex_id;
}
/**
* Get latitude of Vertex
*/
double getLatitude() {
return latitude;
}
/**
* Get longitude of Vertex
*/
double getLongitude() {
return longitude;
}
/**
* operator== of Vertex
* @param other - the other vertex to compare
*/
bool operator==(const Vertex & other) {
if (this->vertex_id == other.vertex_id) {
return true;
} else {
return false;
}
}
string vertexToString() {
string str = vertex_id + ", " + to_string(latitude) + ", " + to_string(longitude);
return str;
}
};
struct Edge {
string source_id;
string destination_id;
double distance;
/**
* Constructor with unweighted edge
* @param sourceAirport - sourceAirport
* @param destinationAirport - destinationAirport
*/
Edge(string sourceAirport, string destinationAirport)
: source_id(sourceAirport), destination_id(destinationAirport), distance(-1)
{/* empty*/}
/**
* Constructor with weighted edge
* @param sourceAirport - sourceAirport
* @param destinationAirport - destinationAirport
* @param d - the distance between the two airports(the weight of the edge)
*/
Edge(string sourceAirport, string destinationAirport, double d)
: source_id(sourceAirport), destination_id(destinationAirport), distance(d)
{/* empty*/}
/**
* Default Constructor
*/
Edge()
: source_id(""), destination_id(""), distance(-1)
{/* empty*/}
/**
* operator< of Edge
* @param other - the other edge to compare
*/
bool operator<(const Edge & other) {
return distance < other.distance;
}
/**
* operator== of Edge
* @param other - the other edge to compare
*/
bool operator==(const Edge & other) {
if (this->source_id == other.source_id && this->destination_id == other.destination_id) {
return true;
} else {
return false;
}
}
/**
* Get distance of edge(weight)
*/
double getDistance() {
return distance;
}
/**
* Get sourseAirportId of edge
*/
string getSource() {
return source_id;
}
/**
* Get DestinationAirportId of edge
*/
string getDestination() {
return destination_id;
}
string edgeToString() {
string str = source_id + ", " + destination_id + ", " + to_string(distance);
return str;
}
};
vector<vector<string>> route;
map<string, vector<string>> airportPosition;
map<string, string> airportIdDictionary;
/**
* Default Constructor
*/
Graph();
/**
* Default Constructor
* @param routefile - Route file
* @param airportfile - Airport file
*/
Graph(const string & routefile, const string & airportfile);
/**
* Get first vertex of graph
*/
Vertex getFirstVertex();
/**
* Get specific vertex of graph
*/
Vertex getVertex(string vertexId);
/**
* Get all vertices of graph
*/
vector<Vertex> getAllVertices();
/**
* Get first vertex of graph
*/
vector<Vertex> getAdjacentVertex(Vertex v);
/**
* Insert Vertex
* @param v - the vertex need to insert
*/
void insertVertex(Vertex v);
/**
* Remove Vertex
* @param v - the vertex need to remove
*/
void removeVertex(Vertex v);
/**
* Get Edge
* @param source - the source airport of Edge
* @param destination - the destination airport of Edge
*/
Edge getEdge(Vertex source, Vertex destination);
/**
* Get Incident Edge
* @param v - the central vertex
*/
vector<Edge> getIncidentEdge(Vertex v);
/**
* Insert edge
* @param e - the edge need to insert
*/
void insertEdge(Edge e);
/**
* Remove edge
* @param e - the edge need to remove
*/
void removeEdge(Edge e);
/**
* Check if there is any edge that goes into the vertex (required for directed)
* Return a true if there is an edge that goes into the vertex, false otherwise.
* @param v - the vertex that is going to be checked
*/
bool routeToVertex(Vertex v);
/**
* Distructor of Graph
*/
~Graph() {
vertices.clear();
adjacencyList.clear();
}
private:
map<string, Vertex> vertices;
map<string, list<Edge>> adjacencyList;
double calculateDistance(double sourceLat, double sourceLong, double destLat, double destLong);
};