-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (70 loc) · 2.26 KB
/
Copy pathmain.cpp
File metadata and controls
82 lines (70 loc) · 2.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
#include "Bidirectional.h"
#include "Dijkstra.h"
#include "Graph.h"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <limits>
#include <unordered_map>
#include <vector>
#ifdef __linux__
#include <sys/resource.h>
#endif
using namespace std;
double getMemoryUsageMB() {
#ifdef __linux__
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return usage.ru_maxrss / 1024.0;
#else
return 0.0;
#endif
}
int main() {
Graph graph;
cout << "Loading road network graph..." << endl;
graph.loadGraphFromFile("roadNet-CA.txt");
// start and goal
int start = 0;
int goal = 1965206;
cout << "===============================\n";
cout << " Comparing Dijkstra Algorithms\n";
cout << "===============================\n";
// --- Dijkstra ---
double mem_before = getMemoryUsageMB();
auto t1 = chrono::high_resolution_clock::now();
auto distMap = Dijkstra::shortestPath(graph, start);
auto t2 = chrono::high_resolution_clock::now();
double mem_after = getMemoryUsageMB();
auto duration = chrono::duration<double, milli>(t2 - t1).count();
cout << "\n[Dijkstra]\n";
if (distMap.find(goal) == distMap.end() ||
distMap[goal] == numeric_limits<double>::infinity()) {
cout << "No path found.\n";
} else {
cout << "Shortest distance: " << distMap[goal] << "\n";
cout << "Runtime: " << duration << " ms\n";
cout << "Approx. memory used: " << (mem_after - mem_before) << " MB\n";
}
// --- Bidirectional Dijkstra ---
mem_before = getMemoryUsageMB();
t1 = chrono::high_resolution_clock::now();
auto [distB, pathB] =
Bidirectional::bidirectional_dijkstra(graph, start, goal);
t2 = chrono::high_resolution_clock::now();
mem_after = getMemoryUsageMB();
duration = chrono::duration<double, milli>(t2 - t1).count();
cout << "\n[Bidirectional Dijkstra]\n";
if (distB == numeric_limits<double>::infinity()) {
cout << "No path found.\n";
} else {
cout << "Path: ";
for (size_t i = 0; i < pathB.size(); ++i)
cout << pathB[i] << (i + 1 < pathB.size() ? " -> " : "\n");
cout << "Total cost: " << distB << "\n";
cout << "Runtime: " << duration << " ms\n";
cout << "Approx. memory used: " << (mem_after - mem_before) << " MB\n";
}
cout << "===============================\n";
return 0;
}