forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgraph.cpp
369 lines (302 loc) · 9.9 KB
/
mapgraph.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Functions for basic undirected, weighted graph support.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#include "mapgraph.h"
#include <algorithm> // for std::swap
graphObj *msCreateGraph(signed int numnodes)
{
graphObj *graph=nullptr;
if(numnodes <= 0) return nullptr;
graph = (graphObj *) malloc(sizeof(graphObj));
if(!graph) return nullptr;
graph->head = (graphNodeObj **) calloc(numnodes, sizeof(graphNodeObj*));
if(!graph->head) {
free(graph);
return nullptr;
}
graph->numnodes = numnodes;
return graph;
}
void msFreeGraph(graphObj *graph)
{
if(!graph) return;
graphNodeObj *tmp=nullptr;
for(int i=0; i<graph->numnodes; i++) {
while(graph->head[i] != nullptr) {
tmp = graph->head[i];
graph->head[i] = graph->head[i]->next;
free(tmp);
}
}
free(graph->head);
free(graph);
}
int msGraphAddEdge(graphObj *graph, int src, int dest, double weight)
{
graphNodeObj *node=nullptr;
if(!graph) return MS_FAILURE;
// src -> dest
node = (graphNodeObj *) malloc(sizeof(graphNodeObj));
if(!node) return MS_FAILURE;
node->dest = dest;
node->weight = weight;
node->next = graph->head[src];
graph->head[src] = node;
// dest -> src
node = (graphNodeObj *) malloc(sizeof(graphNodeObj));
if(!node) return MS_FAILURE;
node->dest = src;
node->weight = weight;
node->next = graph->head[dest];
graph->head[dest] = node;
return MS_SUCCESS;
}
void msPrintGraph(graphObj *graph)
{
int i;
if(!graph) return;
for(i=0; i<graph->numnodes; i++) {
graphNodeObj *node = graph->head[i];
if(node != nullptr) {
do {
msDebug("%d -> %d (%.6f)\t", i, node->dest, node->weight);
node = node->next;
} while (node != nullptr);
msDebug("\n");
}
}
}
/*
** Derived from an number web resources including:
**
** https://www.geeksforgeeks.org/dijkstras-algorithm-for-adjacency-list-representation-greedy-algo-8/
** https://youtube.com/watch?v=pSqmAO-m7Lk
** https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
**
** Much of the bulk here is for min-heap (key,value) management.
*/
typedef struct {
int idx;
double dist;
} minHeapNodeObj;
typedef struct {
int size;
int capacity;
int *pos;
minHeapNodeObj **nodes;
} minHeapObj;
static minHeapNodeObj *newMinHeapNode(int idx, double dist)
{
minHeapNodeObj *node = (minHeapNodeObj *) malloc(sizeof(minHeapNodeObj));
if(!node) return nullptr;
node->idx = idx;
node->dist = dist;
return node;
}
static void freeMinHeap(minHeapObj *minHeap)
{
if(!minHeap) return;
free(minHeap->pos);
for(int i=0; i<minHeap->size; i++) {
free(minHeap->nodes[i]);
}
free(minHeap->nodes);
free(minHeap);
}
static minHeapObj *createMinHeap(signed int capacity)
{
minHeapObj *minHeap = (minHeapObj *) malloc(sizeof(minHeapObj));
if(!minHeap) return nullptr;
minHeap->pos = (int *) malloc(capacity * sizeof(int));
if(!minHeap->pos) {
free(minHeap);
return nullptr;
}
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->nodes = (minHeapNodeObj **) malloc(capacity * sizeof(minHeapNodeObj *));
if(!minHeap->nodes) {
free(minHeap->pos);
free(minHeap);
return nullptr;
}
return minHeap;
}
static void minHeapify(minHeapObj *minHeap, int idx)
{
int smallest = idx;
const int left = 2*idx + 1;
const int right = 2*idx + 2;
if (left < minHeap->size && minHeap->nodes[left]->dist < minHeap->nodes[smallest]->dist)
smallest = left;
if (right < minHeap->size && minHeap->nodes[right]->dist < minHeap->nodes[smallest]->dist)
smallest = right;
if (smallest != idx) {
minHeapNodeObj *smallestNode = minHeap->nodes[smallest];
minHeapNodeObj *idxNode = minHeap->nodes[idx];
minHeap->pos[smallestNode->idx] = idx; // swap positions
minHeap->pos[idxNode->idx] = smallest;
std::swap(minHeap->nodes[smallest], minHeap->nodes[idx]); // swap nodes
minHeapify(minHeap, smallest);
}
}
static bool isEmpty(const minHeapObj *minHeap)
{
return minHeap->size == 0;
}
static minHeapNodeObj *extractMin(minHeapObj *minHeap)
{
if (isEmpty(minHeap)) return nullptr;
// store root node
minHeapNodeObj *root = minHeap->nodes[0];
// replace root node with last node
minHeapNodeObj *lastNode = minHeap->nodes[minHeap->size - 1];
minHeap->nodes[0] = lastNode;
// update position of last node
minHeap->pos[root->idx] = minHeap->size - 1;
minHeap->pos[lastNode->idx] = 0;
// Reduce heap size and heapify root
--minHeap->size;
minHeapify(minHeap, 0);
return root;
}
static void decreaseKey(minHeapObj *minHeap, int idx, int dist)
{
// get the index of idx in min heap nodes
int i = minHeap->pos[idx];
// get the node and update its dist value
minHeap->nodes[i]->dist = dist;
// travel up while the complete tree is not hepified (this is a O(Logn) loop)
while (i && minHeap->nodes[i]->dist < minHeap->nodes[(i - 1) / 2]->dist) {
// swap this node with its parent
minHeap->pos[minHeap->nodes[i]->idx] = (i-1)/2;
minHeap->pos[minHeap->nodes[(i-1)/2]->idx] = i;
std::swap(minHeap->nodes[i], minHeap->nodes[(i - 1) / 2]);
// move to parent index
i = (i - 1) / 2;
}
}
static bool isInMinHeap(const minHeapObj *minHeap, int idx)
{
return minHeap->pos[idx] < minHeap->size;
}
typedef struct {
double *dist;
int *prev;
} dijkstraOutputObj;
static dijkstraOutputObj *dijkstra(graphObj *graph, int src)
{
int n = graph->numnodes;
minHeapObj *minHeap = createMinHeap(n); // priority queue implemented as a min heap structure
if(!minHeap) return nullptr;
dijkstraOutputObj *output = nullptr;
output = (dijkstraOutputObj *) malloc(sizeof(dijkstraOutputObj));
output->dist = (double *) malloc(n * sizeof(double));
output->prev = (int *) malloc(n * sizeof(int));
if(!output->dist || !output->prev) {
msFree(output->dist);
msFree(output->prev);
free(output);
freeMinHeap(minHeap);
return nullptr;
}
// initialize
for (int i=0; i<n; i++) {
output->dist[i] = HUGE_VAL;
output->prev[i] = -1;
minHeap->nodes[i] = newMinHeapNode(i, output->dist[i]); // allocate a min heap node for each graph node
minHeap->pos[i] = i;
}
// make dist value of src vertex as 0 so that it is extracted first
minHeap->pos[src] = src;
output->dist[src] = 0;
decreaseKey(minHeap, src, output->dist[src]);
// initially size of min heap is equal to graph->numnodes (n)
minHeap->size = n;
// In the following loop, minHeap contains all nodes
// whose shortest distance is not yet finalized.
while (!isEmpty(minHeap)) {
// extract the vertex with minimum distance value and store the node index
minHeapNodeObj *minHeapNode = extractMin(minHeap);
int u = minHeapNode->idx;
free(minHeapNode); // done with this node
// traverse through all adjacent nodes of u and update their distance values
graphNodeObj *node = graph->head[u];
while (node != nullptr) {
int v = node->dest;
// if shortest distance to v is not finalized yet, and distance to v
// through u is less than its previously calculated distance
if (isInMinHeap(minHeap, v) && output->dist[u] != HUGE_VAL && node->weight + output->dist[u] < output->dist[v]) {
output->dist[v] = output->dist[u] + node->weight;
output->prev[v] = u;
decreaseKey(minHeap, v, output->dist[v]);
}
node = node->next;
}
}
freeMinHeap(minHeap);
return output;
}
int *msGraphGetLongestShortestPath(graphObj *graph, int src, int *path_size, double *path_dist)
{
if(!graph || src < 0 || src > graph->numnodes) return nullptr;
int* path = (int *) malloc((graph->numnodes)*sizeof(int)); // worst case is path traverses all nodes
if(!path) return nullptr;
dijkstraOutputObj *output = dijkstra(graph, src);
if(!output) {
free(path);
return nullptr; // algorithm failed for some reason
}
// get longest shortest distance from src to another node (our dest)
*path_dist = -1;
int dest = -1;
for(int i=0; i<graph->numnodes; i++) {
if(output->dist[i] != HUGE_VAL && *path_dist < output->dist[i]) {
*path_dist = output->dist[i];
dest = i;
}
}
if(dest == -1) { // unable to determine destination node
free(path);
free(output->dist);
free(output->prev);
free(output);
return nullptr;
}
// construct the path from src to dest
int j = 0;
for(int i=dest; i!=-1; i=output->prev[i],j++) path[j] = i;
std::reverse(path, path + j);
*path_size = j;
// clean up dijkstra output
free(output->dist);
free(output->prev);
free(output);
return path;
}