Skip to content

Commit 8e2a125

Browse files
author
Alexis SANTOS
authored
Merge pull request #24 from algosup/dev
2 parents 4ea85b7 + da75eae commit 8e2a125

File tree

17 files changed

+1057
-737
lines changed

17 files changed

+1057
-737
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vscode/settings.json
2+
*.exe
3+
*.bin
4+
*.txt
5+
*.smsh

src/alt.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
* Steps of the Algorithm:
1919
* 1. If `save_alt` is enabled, attempt to load precomputed landmark data from storage.
2020
* 2. Initialize storage structures for distance calculations.
21-
* 3. Select the first landmark as an arbitrary node (first node in the dataset).
21+
* 3. Select the first landmark as the node with the smallest ID (e.g., ID 1 if available).
2222
* 4. Compute shortest-path distances using Dijkstra’s algorithm:
23-
* - `dist_landmark_to[i][j]` stores the shortest path distance **to** node `i` from landmark `j`.
24-
* - `dist_landmark_from[i][j]` stores the shortest path distance **from** node `i` to landmark `j`.
23+
* - `dist_landmark[i][j]` stores the shortest path distance from landmark `j` to node `i`.
2524
* 5. Iteratively select `nb_alt` landmarks using the farthest-node heuristic:
2625
* - Find the node that has the maximum minimum distance from previous landmarks.
2726
* - Recompute shortest-path distances using Dijkstra’s algorithm.
@@ -31,11 +30,10 @@
3130
* Mathematical Background:
3231
* - Landmark heuristics use the triangle inequality:
3332
*
34-
* h(n) = max{ |d_L(goal) - d_L(n)|, |d_F(n) - d_F(goal)| }
33+
* h(n) = max{ |d_L(goal) - d_L(n)| }
3534
*
3635
* where:
3736
* - `d_L(n)` is the precomputed distance from landmark `L` to node `n`.
38-
* - `d_F(n)` is the precomputed distance from node `n` to landmark `L`.
3937
*
4038
* - The farthest-node landmark selection ensures a wide spread of landmarks,
4139
* improving heuristic accuracy in large graphs.
@@ -72,24 +70,31 @@ void preprocessAlt(graph& gdata, config& conf)
7270
std::vector<int> md(n, std::numeric_limits<int>::max());
7371
std::vector<int> d(n, -1);
7472

75-
gdata.dist_landmark_to.resize(n, std::vector<int>(conf.nb_alt, -1));
76-
gdata.dist_landmark_from.resize(n, std::vector<int>(conf.nb_alt, -1));
73+
gdata.dist_landmark.resize(n, std::vector<int>(conf.nb_alt, -1));
7774

78-
int fl = gdata.node_to_index.begin()->first;
75+
int fl = std::numeric_limits<int>::max();
76+
for (const auto& kv : gdata.node_to_index)
77+
{
78+
if (kv.first < fl)
79+
fl = kv.first;
80+
}
81+
7982
landmarks.push_back(fl);
8083
console("info", "processing landmark " + std::to_string(fl) + " (1/" + std::to_string(conf.nb_alt) + ")");
8184
logger("processing landmark " + std::to_string(fl) + " (1/" + std::to_string(conf.nb_alt) + ")");
8285

83-
d = dijkstraSingleSource(gdata.adjacency, fl, n, gdata.node_to_index);
86+
d = dijkstraSingleSource(gdata, fl, n);
8487
{
8588
int i = 0;
8689
for (auto& kv : gdata.node_to_index) {
8790
size_t idx = kv.second;
88-
gdata.dist_landmark_to[idx][i] = d[idx];
89-
gdata.dist_landmark_from[idx][i] = d[idx];
90-
if (d[idx] >= 0 && d[idx] < md[idx]) md[idx] = d[idx];
91+
gdata.dist_landmark[idx][i] = d[idx];
92+
if (d[idx] >= 0 && d[idx] < md[idx])
93+
md[idx] = d[idx];
9194
}
9295
}
96+
97+
md[gdata.node_to_index[fl]] = -1;
9398

9499
for (int i = 1; i < conf.nb_alt; ++i) {
95100
int nl = -1;
@@ -119,12 +124,12 @@ void preprocessAlt(graph& gdata, config& conf)
119124
logger("processing landmark " + std::to_string(nl) +
120125
" (" + std::to_string(i + 1) + "/" + std::to_string(conf.nb_alt) + ")");
121126

122-
d = dijkstraSingleSource(gdata.adjacency, nl, n, gdata.node_to_index);
127+
d = dijkstraSingleSource(gdata, nl, n);
123128
for (auto& kv : gdata.node_to_index) {
124129
size_t idx = kv.second;
125-
gdata.dist_landmark_to[idx][i] = d[idx];
126-
gdata.dist_landmark_from[idx][i] = d[idx];
127-
if (d[idx] >= 0 && d[idx] < md[idx]) md[idx] = d[idx];
130+
gdata.dist_landmark[idx][i] = d[idx];
131+
if (d[idx] >= 0 && d[idx] < md[idx])
132+
md[idx] = d[idx];
128133
}
129134

130135
size_t selected_idx = gdata.node_to_index[nl];
@@ -137,5 +142,4 @@ void preprocessAlt(graph& gdata, config& conf)
137142
if (conf.save_alt) {
138143
saveAltData(gdata, conf);
139144
}
140-
}
141-
145+
}

src/api.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ std::string ipToString(const sockaddr_in& addr)
106106
* - Response Buffer: O(1)
107107
* - Graph Memory Usage: O(V + E).
108108
*/
109-
int launchApiGateway(const graph& gdata, const config& conf)
109+
int launchApiGateway(const graph& gdata, search_buffers& buffers, const config& conf)
110110
{
111111
#ifdef _WIN32
112112
WSADATA wsa_data;
@@ -153,7 +153,7 @@ int launchApiGateway(const graph& gdata, const config& conf)
153153
return 1;
154154
}
155155

156-
console("success", "server is listening on port 80.");
156+
console("success", "server is listening on port 80. press any touch to close it.");
157157
logger("server is listening on port 80.");
158158

159159
while (true)
@@ -224,7 +224,12 @@ int launchApiGateway(const graph& gdata, const config& conf)
224224
try {
225225
size_t amp_pos = query_part.find("&", st_pos);
226226
std::string val = query_part.substr(st_pos + 6, amp_pos - (st_pos + 6));
227-
start_val = std::stoi(val);
227+
size_t consumed = 0;
228+
int parsed = std::stoi(val, &consumed);
229+
if (consumed != val.size()) {
230+
throw std::invalid_argument("invalid integer parameter");
231+
}
232+
start_val = parsed;
228233
start_ok = true;
229234
}
230235
catch (...) {
@@ -238,7 +243,12 @@ int launchApiGateway(const graph& gdata, const config& conf)
238243
try {
239244
size_t amp_pos = query_part.find("&", en_pos);
240245
std::string val = query_part.substr(en_pos + 4, amp_pos - (en_pos + 4));
241-
end_val = std::stoi(val);
246+
size_t consumed = 0;
247+
int parsed = std::stoi(val, &consumed);
248+
if (consumed != val.size()) {
249+
throw std::invalid_argument("invalid integer parameter");
250+
}
251+
end_val = parsed;
242252
end_ok = true;
243253
}
244254
catch (...) {
@@ -324,7 +334,12 @@ int launchApiGateway(const graph& gdata, const config& conf)
324334

325335
path_result pres;
326336
if (success) {
327-
pres = findShortestPath(gdata, conf, start_val, end_val, used_weight);
337+
if(conf.search_engine == 1) {
338+
pres = findShortestPathUnidirectional(gdata, buffers, conf, start_val, end_val, used_weight);
339+
}
340+
else {
341+
pres = findShortestPathBidirectional(gdata, buffers, conf, start_val, end_val, used_weight);
342+
}
328343
if (pres.total_time < 0) {
329344
success = false;
330345
message_response = "NO PATH FOUND";

src/bu_alt.cpp

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,12 @@ void saveAltData(const graph& gdata, const config& conf)
5050
}
5151

5252
{
53-
uint64_t outer_size = static_cast<uint64_t>(gdata.dist_landmark_to.size());
53+
uint64_t outer_size = static_cast<uint64_t>(gdata.dist_landmark.size());
5454
ofs.write(reinterpret_cast<const char*>(&outer_size), sizeof(outer_size));
5555

56-
for (const auto& row : gdata.dist_landmark_to) {
56+
for (const auto& row : gdata.dist_landmark) {
5757
uint64_t inner_size = static_cast<uint64_t>(row.size());
5858
ofs.write(reinterpret_cast<const char*>(&inner_size), sizeof(inner_size));
59-
60-
ofs.write(reinterpret_cast<const char*>(row.data()), inner_size * sizeof(int));
61-
}
62-
}
63-
{
64-
uint64_t outer_size = static_cast<uint64_t>(gdata.dist_landmark_from.size());
65-
ofs.write(reinterpret_cast<const char*>(&outer_size), sizeof(outer_size));
66-
67-
for (const auto& row : gdata.dist_landmark_from) {
68-
uint64_t inner_size = static_cast<uint64_t>(row.size());
69-
ofs.write(reinterpret_cast<const char*>(&inner_size), sizeof(inner_size));
70-
7159
ofs.write(reinterpret_cast<const char*>(row.data()), inner_size * sizeof(int));
7260
}
7361
}
@@ -94,13 +82,10 @@ void saveAltData(const graph& gdata, const config& conf)
9482
* data from a binary file. The format must match the one used by `saveAltData`.
9583
*
9684
* Data Loaded:
97-
* - dist_landmark_to:
85+
* - dist_landmark:
9886
* - Reads the number of nodes (outer size).
9987
* - For each node, reads the number of landmarks, then reads `int` distances.
10088
*
101-
* - dist_landmark_from:
102-
* - Same pattern as dist_landmark_to.
103-
*
10489
* Steps of Execution:
10590
* 1. Open the input file in binary mode.
10691
* 2. Check if the file is empty (size == 0); if empty, return `false` to force preprocessing.
@@ -144,56 +129,27 @@ bool loadAltData(graph& gdata, config& conf)
144129
uint64_t outer_size = 0;
145130
ifs.read(reinterpret_cast<char*>(&outer_size), sizeof(outer_size));
146131
if (!ifs.good()) {
147-
console("error", "failed to read the size of dist_landmark_to, processing landmarks again");
148-
logger("error: failed to read the size of dist_landmark_to, processing landmarks again.");
149-
return false;
150-
}
151-
152-
gdata.dist_landmark_to.resize(static_cast<size_t>(outer_size));
153-
for (size_t i = 0; i < outer_size; ++i) {
154-
uint64_t inner_size = 0;
155-
ifs.read(reinterpret_cast<char*>(&inner_size), sizeof(inner_size));
156-
if (!ifs.good()) {
157-
console("error", "failed to read the size of a dist_landmark_to row, processing landmarks again");
158-
logger("error: failed to read the size of a dist_landmark_to row, processing landmarks again.");
159-
return false;
160-
}
161-
162-
gdata.dist_landmark_to[i].resize(static_cast<size_t>(inner_size));
163-
ifs.read(reinterpret_cast<char*>(gdata.dist_landmark_to[i].data()),
164-
static_cast<std::streamsize>(inner_size * sizeof(int)));
165-
if (!ifs.good()) {
166-
console("error", "failed to read dist_landmark_to row data, processing landmarks again");
167-
logger("error: failed to read dist_landmark_to row data, processing landmarks again.");
168-
return false;
169-
}
170-
}
171-
}
172-
{
173-
uint64_t outer_size = 0;
174-
ifs.read(reinterpret_cast<char*>(&outer_size), sizeof(outer_size));
175-
if (!ifs.good()) {
176-
console("error", "failed to read the size of dist_landmark_from, processing landmarks again");
177-
logger("error: failed to read the size of dist_landmark_from, processing landmarks again.");
132+
console("error", "failed to read the size of dist_landmark, processing landmarks again");
133+
logger("error: failed to read the size of dist_landmark, processing landmarks again.");
178134
return false;
179135
}
180136

181-
gdata.dist_landmark_from.resize(static_cast<size_t>(outer_size));
137+
gdata.dist_landmark.resize(static_cast<size_t>(outer_size));
182138
for (size_t i = 0; i < outer_size; ++i) {
183139
uint64_t inner_size = 0;
184140
ifs.read(reinterpret_cast<char*>(&inner_size), sizeof(inner_size));
185141
if (!ifs.good()) {
186-
console("error", "failed to read the size of a dist_landmark_from row, processing landmarks again");
187-
logger("error: failed to read the size of a dist_landmark_from row, processing landmarks again.");
142+
console("error", "failed to read the size of a dist_landmark row, processing landmarks again");
143+
logger("error: failed to read the size of a dist_landmark row, processing landmarks again.");
188144
return false;
189145
}
190146

191-
gdata.dist_landmark_from[i].resize(static_cast<size_t>(inner_size));
192-
ifs.read(reinterpret_cast<char*>(gdata.dist_landmark_from[i].data()),
193-
static_cast<std::streamsize>(inner_size * sizeof(int)));
147+
gdata.dist_landmark[i].resize(static_cast<size_t>(inner_size));
148+
ifs.read(reinterpret_cast<char*>(gdata.dist_landmark[i].data()),
149+
static_cast<std::streamsize>(inner_size * sizeof(int)));
194150
if (!ifs.good()) {
195-
console("error", "failed to read dist_landmark_from row data, processing landmarks again");
196-
logger("error: failed to read dist_landmark_from row data, processing landmarks again");
151+
console("error", "failed to read dist_landmark row data, processing landmarks again");
152+
logger("error: failed to read dist_landmark row data, processing landmarks again.");
197153
return false;
198154
}
199155
}

0 commit comments

Comments
 (0)