-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
500 lines (404 loc) · 14.2 KB
/
Graph.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "Graph.h"
#include <functional>
Graph::Graph(int index){
if(index < 0 || index > 17){
throw invalid_argument("Invalid graph index");
}
this->index = index;
}
/* FLAGS */
bool Graph::isRW() const {
return index >= 3 && index <= 5;
}
bool Graph::isToy() const {
return index <= 2;
}
bool Graph::isExtra() const {
return index >= 6 && index <= 17;
}
bool Graph::isLoaded() const {
return !edges.empty();
}
/* LOAD */
string Graph::getPath() const {
switch (index) {
case 0:
return "../Data/Toy-Graphs/Toy-Graphs/shipping.csv";
case 1:
return "../Data/Toy-Graphs/Toy-Graphs/stadiums.csv";
case 2:
return "../Data/Toy-Graphs/Toy-Graphs/tourism.csv";
case 3:
return "../Data/Real-world Graphs/Real-world Graphs/graph1";
case 4:
return "../Data/Real-world Graphs/Real-world Graphs/graph2";
case 5:
return "../Data/Real-world Graphs/Real-world Graphs/graph3";
case 6:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_25.csv";
case 7:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_50.csv";
case 8:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_75.csv";
case 9:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_100.csv";
case 10:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_200.csv";
case 11:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_300.csv";
case 12:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_400.csv";
case 13:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_500.csv";
case 14:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_600.csv";
case 15:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_700.csv";
case 16:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_800.csv";
case 17:
return "../Data/Extra_Fully_Connected_Graphs/Extra_Fully_Connected_Graphs/edges_900.csv";
default:
cout << "Invalid graph index" << endl;
return "";
}
}
int Graph::getNumberNodes() const {
switch (index) {
case 0:
return 14;
case 1:
return 11;
case 2:
return 5;
case 3:
return 1000;
case 4:
return 5000;
case 5:
return 10000;
case 6:
return 25;
case 7:
return 50;
case 8:
return 75;
case 9:
return 100;
case 10:
return 200;
case 11:
return 300;
case 12:
return 400;
case 13:
return 500;
case 14:
return 600;
case 15:
return 700;
case 16:
return 800;
case 17:
return 900;
default:
cout << "Invalid graph index" << endl;
return 0;
}
}
void Graph::loadNodesToyAndExtraGraph() {
int numberNodes = getNumberNodes();
for(int i = 0; i < numberNodes; i++){
Node* node = new Node(i);
nodes.push_back(node);
}
}
void Graph::loadNodesRWGraph() {
string path = getPath() + "/nodes.csv";
ifstream file(path);
if(!file.is_open()) {
cout << "Error opening file" << endl;
return;
}
file.ignore(1000, '\n');
string line;
while(getline(file, line)){
vector<string> row;
stringstream ss(line);
string token;
while (getline(ss, token, ',')) {
row.push_back(token);
}
int id = stoi(row[0]);
double longitude = stod(row[1]);
double latitude = stod(row[2]);
Node* node = new Node(id, latitude, longitude);
nodes.push_back(node);
}
}
void Graph::loadEdges() {
string path = getPath();
if (isRW()) {
path += "/edges.csv";
}
ifstream file(path);
if (!file.is_open()) {
cout << "Error opening file" << endl;
return;
}
file.ignore(1000, '\n');
string line;
file.seekg(0, ios::end);
streampos fileSize = file.tellg();
file.seekg(0);
const double estimatedProgressUnit = static_cast<float>(fileSize) / 100.0;
if(!isExtra())
getline(file, line);
float progress = 0.0;
float totalProgress = 0.0;
while (getline(file, line)) {
vector<string> row;
stringstream ss(line);
string token;
while (getline(ss, token, ',')) {
row.push_back(token);
}
int id1 = stoi(row[0]);
int id2 = stoi(row[1]);
double distance = stod(row[2]);
Node* node1 = nodes[id1];
Node* node2 = nodes[id2];
Edge* edge = new Edge(id1, id2, distance);
edges.push_back(edge);
node1->addEdge(edge);
node2->addEdge(edge);
progress += line.size() + 1; // Add the size of the line plus the newline character
//Update progress bar
if (progress >= estimatedProgressUnit && index > 2 && index < 6) {
totalProgress += (progress / estimatedProgressUnit);
int progressBarCount = static_cast<int>(totalProgress);
cout << "\r" << "[";
for (int j = 0; j < progressBarCount; ++j)
cout << "#";
for (int j = progressBarCount; j < 100; ++j)
cout << " ";
cout << "] " << static_cast<int>(totalProgress+1) << "%";
cout.flush();
progress = 0.0;
}
}
cout << endl;
}
void Graph::load() {
cout << "\nLoading graph...";
if(index <= 2 || index >= 6){
loadNodesToyAndExtraGraph();
}
else{
loadNodesRWGraph();
}
loadEdges();
cout << "Graph loaded successfully!\n";
}
/* BACKTRACKING ALGORITHM */
void Graph::backtracking_aux(unsigned int curIndex, unsigned int count, double cost, double &ans, vector<unsigned int> &path, vector<vector<unsigned int>> paths){
if (count == nodes.size()){
for (auto e : nodes[curIndex]->getEdges()){
if (e->getNode1() == 0 || e->getNode2() == 0){
double new_cost = cost + e->getDistance();
if (new_cost < ans){
ans = new_cost;
path = paths[curIndex];
}
}
}
return;
}
for (auto e : nodes[curIndex]->getEdges()){
int nextPos = e->getNode1() == curIndex ? e->getNode2() : e->getNode1();
if (!nodes[nextPos]->isVisited()){
nodes[nextPos]->setVisited(true);
paths[nextPos] = paths[curIndex];
paths[nextPos].push_back(nextPos);
backtracking_aux(nextPos, count + 1, cost + e->getDistance(), ans, path, paths);
nodes[nextPos]->setVisited(false);
}
}
}
pair<double, vector<unsigned int>> Graph::TSP_Backtracking(){
for (auto node : nodes)
node->setVisited(false);
nodes[0]->setVisited(true);
double shortestDistance = std::numeric_limits<double>::max();
vector<vector<unsigned int>> paths(nodes.size());
vector<unsigned int> path(nodes.size());
paths[0].push_back(0);
backtracking_aux(0, 1, 0, shortestDistance, path, paths);
path.push_back(0);
return make_pair(shortestDistance, path);
}
/* TRIANGULAR APPROXIMATION HEURISTICS */
void Graph::prim_generate_MST(){
for (Node* node : nodes) {
node->setVisited(false);
}
for (Edge* edge : edges) {
edge->setSelected(false);
}
priority_queue<Edge*, vector<Edge*>, function<bool(Edge*, Edge*)>> pq
([](Edge* a, Edge* b) {return a->getDistance() > b->getDistance();});
unsigned int sourceId = 0;
nodes[sourceId]->setVisited(true);
for (Edge* edge : nodes[sourceId]->getEdges()) {pq.push(edge);}
while (!pq.empty()) {
Edge *curEdge = pq.top();
pq.pop();
Node *source = curEdge->getNode1() == sourceId ? nodes[curEdge->getNode1()] : nodes[curEdge->getNode2()];
Node *dest = curEdge->getNode1() == sourceId ? nodes[curEdge->getNode2()] : nodes[curEdge->getNode1()];
if (source->isVisited() && dest->isVisited())
continue;
curEdge->setSelected(true);
dest->setVisited(true);
for (Edge *edge: dest->getEdges()) {
int otherId = edge->getNode1() == dest->getId() ? edge->getNode2() : edge->getNode1();
if (!nodes[otherId]->isVisited()) {
pq.push(edge);
}
}
sourceId = dest->getId();
}
for (Node* node : nodes) {
node->setVisited(false);
}
}
void Graph::dfsMST(unsigned int curIndex, list<unsigned int> &path){
Node* curNode = nodes[curIndex];
curNode->setVisited(true);
path.push_back(curNode->getId());
for (Edge* edge : curNode->getEdges()) {
if (edge->isSelected()) {
Node* dest = edge->getNode1() == curNode->getId() ? nodes[edge->getNode2()] : nodes[edge->getNode1()];
if (!dest->isVisited()) {
dfsMST(dest->getId(), path);
}
}
}
}
pair<double, list<unsigned int>> Graph::TSP_TriangularApproximation(){
/*
Triangular Approximation
Step 1. Generate a minimum spanning tree T of G.
Step 2. Perform a depth-first search of T, starting at an arbitrary vertex r.
Step 3. Let L be the list of vertices visited in the order they are visited by the depth-first search.
Step 4. Return the Hamiltonian cycle H = L + r.
*/
prim_generate_MST();
list<unsigned int> order;
dfsMST(0, order);
order.push_back(0);
double cost = getPathCost(order);
return make_pair(cost, order);
}
/* NEAREST INSERTION HEURISTICS */
pair<double, vector<unsigned int>> Graph::TSP_NearestInsertion() {
/*
Step 1. Start with a sub-graph consisting of node i only.
Step 2. Find node r such that cir is minimal and form sub-tour i-r-i. O(n)
Step 3. (Selection step) Given a sub-tour, find node r not in the sub-tour closest to any node j in the sub-tour; i.e. with minimal crj. O(n^2)
Step 4. (Insertion step) Find the arc (i, j) in the sub-tour which minimizes cir + crj - cij . Insert r between i and j. O(n)
Step 5. If all the nodes are added to the tour, stop. Else go to step 3
Information from: https://www2.isye.gatech.edu/~mgoetsch/cali/VEHICLE/TSP/TSP009__.HTM
*/
for(auto node : nodes){
node->setVisited(false);
}
vector<unsigned int> path;
path.push_back(0);
nodes[0]->setVisited(true);
unsigned int r = 0;
double minCost = std::numeric_limits<double>::max();
for (unsigned int i = 1; i < nodes.size(); i++) {
auto edge = nodes[0]->getEdgeTo(nodes[i]);
if(edge == nullptr) continue;
double cost = edge->getDistance();
if (cost < minCost) {
minCost = cost;
r = i;
}
}
path.push_back(r);
nodes[r]->setVisited(true);
while (path.size() < nodes.size()) {
r = 0;
minCost = std::numeric_limits<double>::max();
for (unsigned int k = 0; k < nodes.size(); k++) {
if (!nodes[k]->isVisited()) {
double nearestDist = std::numeric_limits<double>::max();
for (unsigned int j : path) {
auto edge = nodes[j]->getEdgeTo(nodes[k]);
if(edge == nullptr) continue;
double dist = edge->getDistance();
if (dist < nearestDist) {
nearestDist = dist;
r = k;
}
}
if (nearestDist < minCost) {
minCost = nearestDist;
}
}
}
unsigned int insertionIndex = 0;
double minInsertionCost = std::numeric_limits<double>::max();
for (unsigned int i = 0; i < path.size() - 1; i++) {
unsigned int j = i + 1;
auto edge1 = nodes[path[i]]->getEdgeTo(nodes[r]);
auto edge2 = nodes[r]->getEdgeTo(nodes[path[j]]);
auto edge3 = nodes[path[i]]->getEdgeTo(nodes[path[j]]);
if(edge1 == nullptr || edge2 == nullptr || edge3 == nullptr) return make_pair(-1, vector<unsigned int>());
double insertionCost = edge1->getDistance() +
edge2->getDistance() -
edge3->getDistance();
if (insertionCost < minInsertionCost) {
minInsertionCost = insertionCost;
insertionIndex = j;
}
}
path.insert(path.begin() + insertionIndex, r);
nodes[r]->setVisited(true);
}
path.push_back(0);
double cost = getPathCost(path);
return make_pair(cost, path);
}
/* CALCULATE PATH'S TOTAL COST */
double Graph::getPathCost(list<unsigned int> path) const{
double cost = 0;
auto it = path.begin();
auto it2 = path.begin();
it2++;
for (; it2 != path.end(); it++, it2++) {
Node* current = nodes[*it];
Node* next = nodes[*it2];
Edge* edge = current->getEdgeTo(next);
if(edge == nullptr && !this->isRW()){
return -1;
}
cost += edge? edge->getDistance() : current->getHaversineDistanceTo(next);
}
return cost;
}
double Graph::getPathCost(vector<unsigned int> path) const{
double cost = 0;
for (unsigned int i = 0; i < path.size() - 1; i++) {
Node* current = nodes[path[i]];
Node* next = nodes[path[i + 1]];
Edge* edge = current->getEdgeTo(next);
if(edge == nullptr && !this->isRW()){
cout << "Edge not found: " << current->getId() << " -> " << next->getId() << endl;
return -1;
}
cost += edge? edge->getDistance() : current->getHaversineDistanceTo(next);
}
return cost;
}