-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinding.c
365 lines (327 loc) · 8.02 KB
/
pathfinding.c
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
#include "pathfinding.h"
#include <math.h>
#include <stdlib.h>
#include "map.h"
#include "priority-queue.h"
#include "set.h"
#include "graph.h"
// Global to get around limited graph_neighbors function
// Use only with path_dijkstra, not any other function!
static pqueue *to_process;
static set *processed;
static map *previous;
// Global to get around limited graph_neighbors function
static double curr_distance;
static uint16_t curr_node;
static struct step *path_clone(struct step *path);
static void visit_node(double neigh_distance, uint16_t data)
{
if (set_contains(processed, data)) {
return;
}
double known_distance = pqueue_get_priority(to_process, data);
if (isnan(known_distance)
|| known_distance > curr_distance + neigh_distance) {
if (data != curr_node) {
struct step *curr_step = malloc(sizeof(*curr_step));
curr_step->data = curr_node;
curr_step->distance = neigh_distance;
curr_step->next = NULL;
// if a faster route was found to ID, free the old step
if (map_contains(previous, data)) {
free(map_get(previous, data));
}
map_put(previous, data, curr_step);
pqueue_enqueue(to_process, curr_distance +
neigh_distance, data);
}
}
}
struct step *path_dijkstra(const graph * g, uint16_t start, uint16_t end)
{
to_process = pqueue_create(MIN);
if (!to_process) {
return NULL;
}
processed = set_create();
if (!processed) {
return NULL;
}
previous = map_create();
if (!previous) {
return NULL;
}
map_put(previous, start, NULL);
pqueue_enqueue(to_process, 0, start);
while (!pqueue_is_empty(to_process)) {
curr_distance = pqueue_get_next_priority(to_process);
curr_node = pqueue_dequeue(to_process);
graph_neighbors((graph *) g, curr_node, visit_node);
set_add(processed, curr_node);
if (curr_node == end) {
break;
}
}
struct step *root = NULL;
uint16_t curr_node = end;
while (map_contains(previous, curr_node)) {
struct step *curr_step = map_get(previous, curr_node);
struct step *copy = path_clone(curr_step);
if (!curr_step) {
struct step *final = malloc(sizeof(*final));
if (!final) {
pqueue_dequeue(to_process);
map_destroy(previous);
set_destroy(processed);
return NULL;
}
final->data = curr_node;
final->distance = 0;
final->next = root;
root = final;
break;
}
const uint16_t tmp = curr_node;
curr_node = copy->data;
copy->data = tmp;
copy->next = root;
root = copy;
}
pqueue_destroy(to_process);
map_destroy(previous);
set_destroy(processed);
return root;
}
struct step *path_clone(struct step *path)
{
if (!path) {
return NULL;
}
struct step *new = malloc(sizeof(*new));
new->data = path->data;
new->distance = path->distance;
new->next = NULL;
return new;
}
bool suurballe(graph * g, uint16_t start, uint16_t end)
{
if (!g) {
return false;
}
struct step *P1 = path_dijkstra(g, start, end);
if (!P1) {
return false;
}
set *path_nodes = set_create();
if (!path_nodes) {
path_destroy(P1);
return false;
}
struct step *path1 = P1;
// add nodes of shortest path to a set
// however, we don't want the starting node in the set
path1 = path1->next;
while (path1) {
// we also don't want the ending node in the set
if (path1->next != NULL) {
set_add(path_nodes, path1->data);
}
path1 = path1->next;
}
// reset local path variable to start of shortest path
// for next iteration
path1 = P1;
while (path1 && path1->next) {
// Make the distance on path1 expensive to travel
change_edge_distance(g, path1->data, path1->next->data, 9999);
path1 = path1->next;
}
struct step *P2 = path_dijkstra(g, start, end);
if (!P2) {
path_destroy(P1);
set_destroy(path_nodes);
return false;
}
path1 = P1;
// reset the distances in the graph to what they were before making
// them expensive, now that we have the two paths
while (path1 && path1->next) {
change_edge_distance(g, path1->data, path1->next->data,
path1->next->distance);
path1 = path1->next;
}
struct step *path2 = P2;
bool overlap = false;
while (path2) {
if (set_contains(path_nodes, path2->data)) {
overlap = true;
}
path2 = path2->next;
}
// if there wasn't overlap, we already have two disjoint paths
// no need to continue
if (!overlap) {
path_destroy(P1);
path_destroy(P2);
set_destroy(path_nodes);
return true;
}
// modify overlapping edges to find disjoint paths, if they exist
path1 = P1->next;
path2 = P2->next;
while (path1) {
// iterate through path2 while holding path1 on a single node
// modify the edges to overlapping nodes
while (path2 && path1->next) {
if (path1->next->data == path2->data
&& path2->next != NULL) {
change_edge_distance(g, path1->data,
path1->next->data, 9999);
}
path2 = path2->next;
}
// increment path1 to the next node; reset path2
path1 = path1->next;
path2 = P2->next;
}
path1 = P1->next;
path2 = P2->next;
while (path2) {
// iterate through path1 while holding path1 on a single node
// modify the edges to overlapping nodes
while (path1 && path2->next) {
if (path2->next->data == path1->data
&& path1->next != NULL) {
change_edge_distance(g, path2->data,
path2->next->data, 9999);
}
path1 = path1->next;
}
// increment path2 to next step; reset path1
path2 = path2->next;
path1 = P1->next;
}
// new shortest path based on changing the edges of overlapping nodes
struct step *P3 = path_dijkstra(g, start, end);
if (!P3) {
path_destroy(P1);
path_destroy(P2);
set_destroy(path_nodes);
return false;
}
// new set for the new shortest path
set *new_path = set_create();
if (!new_path) {
path_destroy(P1);
path_destroy(P2);
path_destroy(P3);
set_destroy(path_nodes);
return false;
}
// add path3 nodes to set, minus the start and end nodes
struct step *path3 = P3;
path3 = path3->next;
while (path3) {
if (path3->next != NULL) {
set_add(new_path, path3->data);
}
path3 = path3->next;
}
// make path3 expensive to travel
path3 = P3;
while (path3 && path3->next) {
change_edge_distance(g, path3->data, path3->next->data, 9999);
path3 = path3->next;
}
// create fourth path, which avoids the expensive path3
struct step *P4 = path_dijkstra(g, start, end);
if (!P4) {
path_destroy(P1);
path_destroy(P2);
path_destroy(P3);
set_destroy(path_nodes);
set_destroy(new_path);
return false;
}
// change all expensive edges back to what they were before the final
// check for overlapping nodes
path3 = P3;
while (path3 && path3->next) {
change_edge_distance(g, path3->data, path3->next->data,
path3->next->distance);
path3 = path3->next;
}
path1 = P1->next;
path2 = P2->next;
while (path1) {
while (path2 && path1->next) {
if (path1->next->data == path2->data
&& path2->next != NULL) {
change_edge_distance(g, path1->data,
path1->next->data,
path1->next->distance);
}
path2 = path2->next;
}
path1 = path1->next;
path2 = P2->next;
}
path1 = P1->next;
path2 = P2->next;
while (path2) {
while (path1 && path2->next) {
if (path2->next->data == path1->data
&& path1->next != NULL) {
change_edge_distance(g, path2->data,
path2->next->data,
path2->next->distance);
}
path1 = path1->next;
}
path2 = path2->next;
path1 = P1->next;
}
// Final check: if there is overlap between path3 and path4
// then two disjoint paths are not possible
struct step *path4 = P4;
while (path4) {
if (set_contains(new_path, path4->data)) {
path_destroy(P1);
path_destroy(P2);
path_destroy(P3);
path_destroy(P4);
set_destroy(path_nodes);
set_destroy(new_path);
return false;
}
path4 = path4->next;
}
path_destroy(P1);
path_destroy(P2);
path_destroy(P3);
path_destroy(P4);
set_destroy(path_nodes);
set_destroy(new_path);
return true;
}
// used for debugging purposes
void print_path(struct step *path)
{
if (!path) {
return;
}
struct step *curr = path;
while (curr) {
printf("%u ", curr->data);
curr = curr->next;
}
puts("\n");
}
void path_destroy(struct step *path)
{
while (path) {
struct step *to_delete = path;
path = path->next;
free(to_delete);
}
}