This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
djikstras.cpp
513 lines (429 loc) · 14.9 KB
/
djikstras.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
501
502
503
504
505
506
507
508
509
510
511
512
513
#pragma once
#include <vector>
#include <iostream>
// #include "graph.h"
#include "graph-matrix.cpp"
#include <set>
#include <limits>
// #include <map>
#include "hash-map.cpp"
using namespace std;
// move23 a move .
// move23 moveTime t .
// move23 moveTo nodeX .
// This is a dynamic programming problem as we can break problems into an acyclic graph over time
// Assume that infection risk is not increased in hallway traversals
// as they are *spread out* through the hallway and they
// are already at risk in the rooms they have gone to/from
// Add assumption about infection risk having to be
// calculated at a separate stage going directly from the
// RDF file - this makes sense as we should not be calculating
// the infection risk *whilst* working out the safest way
// to get everyone out of the building. This allows us to
// just write the functionality directly from read in triple.
// We can also expand on the complexity of reading in this particlar data.
// First we need
// Add assumption that the compiler being used is
// doing this <https://stackoverflow.com/questions/35052586/is-it-more-efficient-to-store-vector-size-into-an-int-variable-when-using-it-mul>
// optimisation
// Add assumption about the number of nodes begin
// less than the numeric limit of int in the system
// Another assumption is that the RDF file must represent
// a valid building; be correctly serialised; and that there
// is a path
// int Infinity = std::numeric_limits<int>::max();
// template <typename Id = int>
// vector<string> djikstras(Id start, Id end, GraphWithIdInternals<Id> graph)
// {
// // Id of the initial node we are working with
// // Id startId = graph.toId(start);
// // Id endId = graph.toId(end);
// // Initialise an array, with length equal to the number of
// // nodes
// // In order for this to work we do need some way of
// // mapping each node to an integer - TODO: WORK OUT
// // if this is ok
// // Possibly use hash maps here instead when generalising
// vector<bool> visited(graph.nodeCount(), false);
// vector<int> distance(graph.nodeCount(), Infinity);
// int current = start;
// int currentDistance = 0;
// distance[current] = currentDistance;
// // TODO: FIX THE SECOND CONDITION IN THIS LOOP
// while (!visited[endId] && currentDistance != Infinity)
// {
// // vector<weightedEdge> edges = graph.weightedEdges(current);
// int minTentative = Infinity;
// Id nextId;
// for (weightedEdge edge : graph.weightedEdges(current))
// {
// // Check to make sure the node has not yet been visited
// if (!visited[edges[i].object])
// {
// int tentativeDistance = edges[i].weight + currentDistance;
// if (tentativeDistance < minTentative)
// {
// minTentative = tentativeDistance;
// nextId = i;
// };
// // Take the minimium of the current distance and the tentative distance
// distance[i] = min(distance[i], edges[i].weight + currentDistance);
// };
// };
// visited[current] = true;
// currentDistance = minTentative;
// current = i;
// };
// // Now we extract the root from that
// vector<string> path = {start};
// Id focus = start;
// while (focus != end)
// {
// int min = Infinity;
// for (weightedEdge edge : graph.weightedEdges(focus))
// {
// if ((int temp = distance[edge.object]) < min)
// {
// min = temp;
// focus = edge.object;
// };
// };
// path.push_back(graph.fromId(focus));
// };
// return path;
// };
// template <typename Id = int>
// bool isNotEnd(Id n, vector<Id> &endIds)
// {
// for (int i : endIds)
// {
// if (i == n)
// {
// return false;
// };
// };
// return true;
// };
struct nodeInfo
{
bool end = true;
bool visited = false;
int weight = 0;
};
struct Info
{
bool end = true;
bool visited = false;
int weight = 0;
};
// template <typename Id = int>
// Map<Id, vector<Id>> multiStartMultiEndAbstracted(vector<Id> starts, vector<Id> ends, GraphMatrix<Id> graph)
// {
// Map<Id, Id> paths(graph.nodeCount());
// Map<Id, int> weights(graph.nodeCount());
// for (Id e : ends)
// {
// weights.add(e, 0);
// };
// vector<Id> updated = ends;
// // Dynamic programming solution to determine optimal
// // path for *all* nodes in the graph
// while (updated.size() > 0)
// {
// for (Id node : updated)
// {
// int weight = weights.get(node);
// for (_weightedEdge<Id> w : graph._weightedEdgesInto(node))
// {
// if (!weights.hasKey(w.subject) || (weights.get(node) + weight < weights.get(w.subject)))
// {
// paths.add(w.subject, node);
// weights.add(w.subject, weights.get(node) + weight)
// };
// };
// };
// };
// Map<Id, vector<Id>> outPaths;
// for (Id s : starts)
// {
// vector<Id> outPath;
// Id p = s;
// while (paths.hasKey(p))
// {
// outPath.push_back(p);
// p = paths.get(p);
// };
// outPath.push_back(p);
// outPaths.add(s, outPath);
// };
// return outPaths;
// // for (Id end : ends)
// // {
// // };
// // for (Id end : ends)
// // {
// // weights.add(end, 0);
// // };
// // for (Id start : starts)
// // {
// // // Possibly use hash maps here instead when generalising
// // vector<bool> visited(graph.nodeCount(), false);
// // vector<int> distance(graph.nodeCount(), Infinity);
// // int current = start;
// // int currentDistance = 0;
// // distance[current] = currentDistance;
// // // TODO: FIX THE SECOND CONDITION IN THIS LOOP
// // while (
// // !visited[endId] && currentDistance != Infinity)
// // {
// // // vector<weightedEdge> edges = graph.weightedEdges(current);
// // int minTentative = Infinity;
// // Id nextId;
// // for (weightedEdge edge : graph.weightedEdges(current))
// // {
// // // Check to make sure the node has not yet been visited
// // if (!visited[edges[i].object])
// // {
// // int tentativeDistance = edges[i].weight + currentDistance;
// // if (tentativeDistance < minTentative)
// // {
// // minTentative = tentativeDistance;
// // nextId = i;
// // };
// // // Take the minimium of the current distance and the tentative distance
// // distance[i] = min(distance[i], edges[i].weight + currentDistance);
// // };
// // };
// // visited[current] = true;
// // currentDistance = minTentative;
// // current = i;
// // };
// // // Now we extract the root from that
// // vector<string> path = {start};
// // Id focus = startId;
// // while (focus != endId)
// // {
// // int min = Infinity;
// // for (weightedEdge edge : graph.weightedEdges(focus))
// // {
// // if ((int temp = distance[edge.object]) < min)
// // {
// // min = temp;
// // focus = edge.object;
// // };
// // };
// // path.push_back(graph.fromId(focus));
// // };
// // return path;
// // };
// };
template <typename Id = int>
struct NodeAndPrev
{
NodeAndPrev(Id _id, Id _prev)
{
id = _id;
prev = _prev;
};
bool operator<(NodeAndPrev<Id> x)
const {
return x.id < x.id;
};
bool operator==(NodeAndPrev<Id> x)
const {
return x.x.id == x.y.id;
};
Id prev;
Id id;
};
// TODO: Version with things other way around
template <typename Id = int, typename Node = string>
Map<Id, Id> shortestNeightbour(set<Id> ends, GraphMatrix<Node> graph)
{
Map<Id, Id> paths(graph.nodeCount());
Map<int, set<NodeAndPrev<Id>>> distance(graph.nodeCount());
set<NodeAndPrev<Id>> endsTemp;
for (Id e : ends)
{
endsTemp.insert(NodeAndPrev<Id>(e, -1));
};
distance.add(0, endsTemp);
for (int i = 0; distance.size() > 0; i++)
{
if (distance.hasKey(i))
{
for (NodeAndPrev<Id> n : distance.get(i))
{
if (!paths.hasKey(n.id))
{
paths.add(n.id, n.prev);
for (_weightedEdge<Id> e : graph._weightedEdgesInto(n.id))
{
if (!distance.hasKey(i + e.weight))
{
distance.add(i + e.weight, {NodeAndPrev<Id>(e.subject, n.id)});
}
else
{
distance.get(i + e.weight).insert(NodeAndPrev<Id>(e.subject, n.id));
};
};
};
};
distance.remove(i);
};
};
return paths;
};
template <typename Id = int, typename Node = string>
Map<Id, vector<Id>> multiStartMultiEnd(set<Id> starts, set<Id> ends, GraphMatrix<Node> graph, Map<Id, Id> paths)
{
Map<Id, vector<Id>> outPaths;
for (Id s : starts)
{
vector<Id> outPath;
Id p = s;
while (paths.hasKey(p))
{
outPath.push_back(p);
p = paths.get(p);
};
outPaths.add(s, outPath);
};
return outPaths;
};
// template <typename Id = int>
// Map<Id, vector<Id>> multiStartMultiEnd(vector<Id> starts, vector<Id> ends, GraphMatrix<Id> graph)
// {
// Map<Id, Id> paths(graph.nodeCount());
// Map<Id, int> weights(graph.nodeCount());
// for (Id e : ends)
// {
// weights.add(e, 0);
// };
// vector<Id> updated = ends;
// // Dynamic programming solution to determine optimal
// // path for *all* nodes in the graph
// while (updated.size() > 0)
// {
// for (Id node : updated)
// {
// int weight = weights.get(node);
// for (_weightedEdge<Id> w : graph._weightedEdgesInto(node))
// {
// if (!weights.hasKey(w.subject) || (weights.get(node) + weight < weights.get(w.subject)))
// {
// paths.add(w.subject, node);
// weights.add(w.subject, weights.get(node) + weight)
// };
// };
// };
// };
// Map<Id, vector<Id>> outPaths;
// for (Id s : starts)
// {
// vector<Id> outPath;
// Id p = s;
// while (paths.hasKey(p))
// {
// outPath.push_back(p);
// p = paths.get(p);
// };
// outPath.push_back(p);
// outPaths.add(s, outPath);
// };
// return outPaths;
// // for (Id end : ends)
// // {
// // };
// // for (Id end : ends)
// // {
// // weights.add(end, 0);
// // };
// // for (Id start : starts)
// // {
// // // Possibly use hash maps here instead when generalising
// // vector<bool> visited(graph.nodeCount(), false);
// // vector<int> distance(graph.nodeCount(), Infinity);
// // int current = start;
// // int currentDistance = 0;
// // distance[current] = currentDistance;
// // // TODO: FIX THE SECOND CONDITION IN THIS LOOP
// // while (
// // !visited[endId] && currentDistance != Infinity)
// // {
// // // vector<weightedEdge> edges = graph.weightedEdges(current);
// // int minTentative = Infinity;
// // Id nextId;
// // for (weightedEdge edge : graph.weightedEdges(current))
// // {
// // // Check to make sure the node has not yet been visited
// // if (!visited[edges[i].object])
// // {
// // int tentativeDistance = edges[i].weight + currentDistance;
// // if (tentativeDistance < minTentative)
// // {
// // minTentative = tentativeDistance;
// // nextId = i;
// // };
// // // Take the minimium of the current distance and the tentative distance
// // distance[i] = min(distance[i], edges[i].weight + currentDistance);
// // };
// // };
// // visited[current] = true;
// // currentDistance = minTentative;
// // current = i;
// // };
// // // Now we extract the root from that
// // vector<string> path = {start};
// // Id focus = startId;
// // while (focus != endId)
// // {
// // int min = Infinity;
// // for (weightedEdge edge : graph.weightedEdges(focus))
// // {
// // if ((int temp = distance[edge.object]) < min)
// // {
// // min = temp;
// // focus = edge.object;
// // };
// // };
// // path.push_back(graph.fromId(focus));
// // };
// // return path;
// // };
// };
// template <typename Id = int>
// vector<string> djikstrasMultiEndpoint(string start, vector<string> end, GraphWithIdInternals<Id> graph)
// {
// Id room = graph.toId(start);
// int distance = Infinity;
// vector<bool> visited(graph.nodeCount(), false);
// vector<int> dist(graph.nodeCount(), Infinity);
// vector<Id> endIds;
// for (string e : end)
// {
// endIds.push_back(graph.toId(e));
// };
// while (isNotEnd(room))
// {
// }
// dist[room] = 0;
// visited[room] = true;
// };
// Since we are working with multiple exits we extend djikstras algorithm
// here is the first more "naive" method
// template <typename Id = int>
// vector<string> naiveEscape(string start, vector<string> exits, GraphWithIdInternals<Id> graph)
// {
// // In the naive method we just apply djikstras algorithm to each exit
// // and then take the best
// }
// In this less naive method algorithm, we note that we can terminate an iteration of djikstras
// algorithm as soon as we know there is a more expansive path.
// template <typename Id = int>
// vector<string> version2Escape(string start, vector<string> exits, GraphWithIdInternals<Id> graph)
// {
// }
// Run above - then we can look into doing something with computing the MST immediately after we ingest
// the graph