-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesschanges_graph.cpp
More file actions
302 lines (270 loc) · 9.06 KB
/
Copy pathlesschanges_graph.cpp
File metadata and controls
302 lines (270 loc) · 9.06 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
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
#include "lesschanges_graph.h"
#include "distance_graph.h"
#include <iostream>
#include <fstream>
#include "filereader.h"
#include <cmath>
#include <climits>
#include "minHeap.h"
#include "lesschanges_graph.h"
#include <queue>
#include <vector>
#include <stack>
#define INF (INT_MAX/2)
filereader f3;
lesschanges_graph::lesschanges_graph(int stops, bool dir): n(stops), hasDir(dir), nodes(stops + 1){}
void lesschanges_graph::stops() {
int n = f3.number_of_stops();
std::string filename = "../dataset/stops.csv";
std::string first_line;
std::ifstream stops;
stops.open(filename);
if(!stops.is_open()){
std::cout << filename << " doesn't exist!";
}
getline(stops,first_line);
for(int i=1;i<=n;i++){
map_stops=f3.stops_code(stops,i,map_stops);
nodes[i].name=f3.stops_name(stops);
nodes[i].zone=f3.stops_zone(stops);
nodes[i].latitude=f3.stops_latitude(stops);
nodes[i].longitude=f3.stops_longitude(stops);
}
stops.close();
}
void lesschanges_graph::addEdge(int src, int dest) {
if (src<1 || src>n || dest<1 || dest>n) return;
nodes[src].adj.push_back({dest});
if (!hasDir) nodes[dest].adj.push_back({src});
}
void lesschanges_graph::edges() {
std::vector<std::string> codes;
std::string string_n, line, first_stop, second_stop;
double distance;
int n, node, node_1, node_2;
codes = f3.lines();
for (int i = 0; i < codes.size(); i++) {
std::string filename = "../dataset/line_" + codes[i] + "_0.csv";
std::ifstream edges{filename};
getline(edges, string_n);
line = f3.stops(edges);
n = stoi(string_n);
for (int i = 2; i < n + 1; i++) { //if something goes wrong, we should check here
first_stop = line;
second_stop = f3.stops(edges);
line = second_stop;
node_1 = map_stops[first_stop];
node_2 = map_stops[second_stop];
// distance = haversine(nodes[node_1].latitude, nodes[node_1].longitude, nodes[node_2].latitude,nodes[node_2].longitude);
addEdge(node_1, node_2);
}
edges.close();
}
for (int i = 0; i < codes.size(); i++) {
std::string filename = "../dataset/line_" + codes[i] + "_1.csv";
std::ifstream edges{filename};
getline(edges, string_n);
line = f3.stops(edges);
n = stoi(string_n);
for (int i = 2;i < n + 1; i++) { //if something goes wrong, we should check here
first_stop = line;
second_stop = f3.stops(edges);
line = second_stop;
node_1 = map_stops[first_stop];
node_2 = map_stops[second_stop];
//distance = haversine(nodes[node_1].latitude, nodes[node_1].longitude, nodes[node_2].latitude,nodes[node_2].longitude);
addEdge(node_1, node_2);
}
edges.close();
}
}
double lesschanges_graph::haversine(double lat1, double lon1, double lat2, double lon2) {
// distance between latitudes
// and longitudes
double dLat = (lat2 - lat1) * M_PI / 180.0;
double dLon = (lon2 - lon1) * M_PI / 180.0;
// convert to radians
lat1 = (lat1) * M_PI / 180.0;
lat2 = (lat2) * M_PI / 180.0;
// apply formulae
double a = pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1) * cos(lat2);
double rad = 6371;
double c = 2 * asin(sqrt(a));
return rad * c;
}
int lesschanges_graph::get_index(std::string stop) {
int index;
index = map_stops[stop];
return index;
}
int lesschanges_graph::closeststop(double latitude, double longitude) {
int index;
double distance;
double min_dist = INF;
for (int i=1; i<nodes.size();i++){
distance=haversine(latitude,longitude,nodes[i].latitude,nodes[i].longitude);
if(distance<min_dist){
min_dist=distance;
index=i;
}
}
return index;
}
void lesschanges_graph::bfs(int start, int end){
queue<int> q;
for(int v = 1; v <= n; v++){
nodes[v].visited = false;
nodes[v].pred = -1;
}
q.push(start);
nodes[start].visited = true;
while(!q.empty()){
int temp = q.front();
q.pop();
for(auto k: nodes[temp].adj){
int w = k.dest;
if(nodes[w].visited == false){
q.push(w);
nodes[w].visited = true;
nodes[w].pred = temp;
}
}
}
}
stack<int> lesschanges_graph::shortest_path(int s, int d) {
bfs(s,d);
stack<int> st;
while(nodes[d].pred != -1){
st.push(d);
d = nodes[d].pred;
}
return st;
}
std::string lesschanges_graph::mappers(int a) {
string name = nodes[a].name;
return name;
}
/*
void lesschanges_graph::stops(){
int n=f1.number_of_stops();
std::string filename = "../dataset/stops.csv";
std::ifstream stops;
stops.open(filename);
if(!stops.is_open()){
std::cout << filename << " doesn't exist!";
}
for(int i=1;i<=n;i++){
map_stops=f1.stops_code(stops,n,map_stops);
nodes[i].name=f1.stops_name(stops);
nodes[i].zone=f1.stops_zone(stops);
nodes[i].latitude=f1.stops_latitude(stops);
nodes[i].longitude=f1.stops_longitude(stops);
}
stops.close();
}
void lesschanges_graph::addEdge(int src, int dest, double distance) {
if (src<1 || src>n || dest<1 || dest>n) return;
nodes[src].adj.push_back({dest, distance});
if (!hasDir) nodes[dest].adj.push_back({src, distance};
}
void lesschanges_graph::edges() {
std::vector<std::string> codes;
std::string string_n, line, first_stop, second_stop;
double distance;
int n, node, node_1, node_2;
codes = f1.lines();
for (int i = 0; i < codes.size(); i++) {
std::string filename = "../dataset/line_" + codes[i] + "_0.csv";
std::ifstream edges{filename};
getline(edges, string_n);
line = f1.stops(edges);
n = stoi(string_n);
for (int i = 2;
i <= n + 1; i++) { //if something goes wrong, we should check here
first_stop = line;
second_stop = f1.stops(edges);
line = second_stop;
node_1 = map_stops[first_stop];
node_2 = map_stops[second_stop];
distance = haversine(nodes[node_1].latitude, nodes[node_1].longitude, nodes[node_2].latitude,
nodes[node_2].longitude);
addEdge(node_1, node_2, distance);
}
edges.close();
}
for (int i = 0; i < codes.size(); i++) {
std::string filename = "../dataset/line_" + codes[i] + "_1.csv";
std::ifstream edges{filename};
getline(edges, string_n);
line = f1.stops(edges);
n = stoi(string_n);
for (int i = 2;
i <= n + 1; i++) { //if something goes wrong, we should check here
first_stop = line;
second_stop = f1.stops(edges);
line = second_stop;
node_1 = map_stops[first_stop];
node_2 = map_stops[second_stop];
distance = haversine(nodes[node_1].latitude, nodes[node_1].longitude, nodes[node_2].latitude,
nodes[node_2].longitude);
addEdge(node_1, node_2, distance);
}
edges.close();
}
}
double lesschanges_graph::haversine(double lat1, double lon1, double lat2, double lon2){
// distance between latitudes
// and longitudes
double dLat = (lat2 - lat1) * M_PI / 180.0;
double dLon = (lon2 - lon1) * M_PI / 180.0;
// convert to radians
lat1 = (lat1) * M_PI / 180.0;
lat2 = (lat2) * M_PI / 180.0;
// apply formulae
double a = pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1) * cos(lat2);
double rad = 6371;
double c = 2 * asin(sqrt(a));
return rad * c;
}
int lesschanges_graph::dijkstra_distance(int origin, int destination) {
dijkstra(origin);
if(nodes[destination].dist == INT_MAX) return -1;
return nodes[destination].dist;
}
std::list<int> lesschanges_graph::dijkstra_path(int origin, int destination) {
dijkstra(origin);
std::list<int> path;
if(nodes[destination].dist == INT_MAX) return path;
path.push_back(destination);
int v = destination;
while(v != origin){
v = nodes[v].pred;
path.push_front(v);
}
return path;
}
void lesschanges_graph::dijkstra(int s) {
MinHeap<int, int> q(n, -1);
for (int v= 1; v <= n; v++){
nodes[v].dist = INT_MAX;
q.insert(v, INT_MAX);
nodes[v].visited = false;
}
nodes[s].dist = 0;
q.decreaseKey(s,0);
nodes[s].pred = s;
while(q.getSize() > 0){
int u = q.removeMin();
nodes[u].visited = true;
for(auto edge: nodes[u].adj){
int v = edge.dest;
int w = edge.distance;
if(!nodes[u].visited && nodes[u].dist + w < nodes[v].dist){
nodes[v].dist = nodes[u].dist + w;
q.decreaseKey(v, nodes[v].dist);
nodes[v].pred = u;
}
}
}
}
*/