forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
396 lines (323 loc) · 7.11 KB
/
dijkstra.cpp
File metadata and controls
396 lines (323 loc) · 7.11 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
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
/* This software is distributed under the GNU Lesser General Public License */
//==========================================================================
//
// dijkstra.cpp
//
//==========================================================================
//$Id: dijkstra.cpp,v 1.6 2002/12/23 13:46:41 chris Exp $
#include <GTL/dijkstra.h>
#include <GTL/bin_heap.h>
#include <cstdlib>
#include <iostream>
#include <cassert>
__GTL_BEGIN_NAMESPACE
/**
* @internal
* Binary predicate that compares two nodes according to their distance.
*/
class less_dist : public std::binary_function<node, node, bool>
{
public:
/**
* @internal
* Constructor sets pointer to node distances and infimum info.
*/
less_dist(const node_map<double>* dist, const node_map<int>* mark)
{
this->dist = dist;
this->mark = mark;
}
/**
* @internal
* Compares distances of @p n1 and @p n2.
*/
bool operator()(const node n1, const node n2) const
{
if (((*mark)[n1] == dijkstra::black) &&
((*mark)[n2] == dijkstra::black))
{
return false;
}
else if ((*mark)[n1] == dijkstra::black)
{
return false;
}
else if ((*mark)[n2] == dijkstra::black)
{
return true;
}
return (*dist)[n1] < (*dist)[n2];
}
private:
/**
* @internal
* Node distances from source.
*/
const node_map<double>* dist;
/**
* @internal
* Infimum distance info (color of nodes).
*/
const node_map<int>* mark;
};
dijkstra::dijkstra()
{
reset_algorithm();
}
dijkstra::~dijkstra()
{
}
void dijkstra::source(const node& n)
{
s = n;
}
void dijkstra::target(const node& n)
{
t = n;
}
void dijkstra::weights(const edge_map<double>& weight)
{
this->weight = weight;
weights_set = true;
}
void dijkstra::store_preds(bool set)
{
preds_set = set;
}
int dijkstra::check(graph& G)
{
if ((s == node()) || (!weights_set))
{
return GTL_ERROR;
}
bool source_found = false;
graph::node_iterator node_it;
graph::node_iterator nodes_end = G.nodes_end();
for (node_it = G.nodes_begin(); node_it != nodes_end; ++node_it)
{
if (*node_it == s)
{
source_found = true;
break;
}
}
if (!source_found)
{
return(GTL_ERROR);
}
graph::edge_iterator edge_it;
graph::edge_iterator edges_end = G.edges_end();
for(edge_it = G.edges_begin(); edge_it != edges_end; ++edge_it)
{
if (weight[*edge_it] < 0.0)
{
return false;
}
}
return GTL_OK;
}
int dijkstra::run(graph& G)
{
init(G);
less_dist prd(&dist, &mark);
bin_heap<node, less_dist> node_heap(prd, G.number_of_nodes());
mark[s] = grey;
dist[s] = 0.0;
node_heap.push(s);
while (!node_heap.is_empty())
{
// debug:
// node_heap.print_data_container();
node cur_node = node_heap.top();
node_heap.pop();
// debug:
// node_heap.print_data_container();
mark[cur_node] = white;
if (cur_node == t)
{
// if @a t is set through #target we are ready
return GTL_OK;
}
node::adj_edges_iterator adj_edge_it;
node::adj_edges_iterator adj_edges_end = cur_node.adj_edges_end();
for (adj_edge_it = cur_node.adj_edges_begin();
adj_edge_it != adj_edges_end;
++adj_edge_it)
{
node op_node = adj_edge_it->opposite(cur_node);
if (mark[op_node] == black)
{
mark[op_node] = grey;
dist[op_node] = dist[cur_node] + weight[*adj_edge_it];
node_heap.push(op_node);
// debug:
// node_heap.print_data_container();
if (preds_set)
{
pred[op_node] = *adj_edge_it;
}
}
else if (mark[op_node] == grey)
{
if (dist[op_node] > dist[cur_node] + weight[*adj_edge_it])
{
dist[op_node] = dist[cur_node] + weight[*adj_edge_it];
node_heap.changeKey(op_node);
// debug:
// node_heap.print_data_container();
if (preds_set)
{
pred[op_node] = *adj_edge_it;
}
}
}
else // (mark[op_node] == white)
{
// nothing to do: shortest distance to op_node is already
// computed
}
}
}
return GTL_OK;
}
node dijkstra::source() const
{
return s;
}
node dijkstra::target() const
{
return t;
}
bool dijkstra::store_preds() const
{
return preds_set;
}
bool dijkstra::reached(const node& n) const
{
return mark[n] != black;
}
double dijkstra::distance(const node& n) const
{
return dist[n];
}
node dijkstra::predecessor_node(const node& n) const
{
assert(preds_set);
if ((n == s) || (!reached(n)))
{
return node();
}
return pred[n].opposite(n);
}
edge dijkstra::predecessor_edge(const node& n) const
{
assert(preds_set);
return pred[n];
}
dijkstra::shortest_path_node_iterator dijkstra::shortest_path_nodes_begin(
const node& dest)
{
assert(preds_set);
if ((shortest_path_node_list[dest].empty()) &&
(dest != s) &&
(reached(dest)))
{
fill_node_list(dest);
}
return shortest_path_node_list[dest].begin();
}
dijkstra::shortest_path_node_iterator dijkstra::shortest_path_nodes_end(
const node& dest)
{
assert(preds_set);
if ((shortest_path_node_list[dest].empty()) &&
(dest != s) &&
(reached(dest)))
{
fill_node_list(dest);
}
return shortest_path_node_list[dest].end();
}
dijkstra::shortest_path_edge_iterator dijkstra::shortest_path_edges_begin(
const node& dest)
{
assert(preds_set);
if ((shortest_path_edge_list[dest].empty()) &&
(dest != s) &&
(reached(dest)))
{
fill_edge_list(dest);
}
return shortest_path_edge_list[dest].begin();
}
dijkstra::shortest_path_edge_iterator dijkstra::shortest_path_edges_end(
const node& dest)
{
assert(preds_set);
if ((shortest_path_edge_list[dest].empty()) &&
(dest != s) &&
(reached(dest)))
{
fill_edge_list(dest);
}
return shortest_path_edge_list[dest].end();
}
void dijkstra::reset()
{
reset_algorithm();
}
void dijkstra::reset_algorithm()
{
s = node();
t = node();
weights_set = false;
preds_set = false;
}
void dijkstra::init(graph& G)
{
dist.init(G, -1.0);
mark.init(G, black);
if (preds_set)
{
pred.init(G, edge());
graph::node_iterator node_it;
graph::node_iterator nodes_end = G.nodes_end();
for (node_it = G.nodes_begin(); node_it != nodes_end; ++node_it)
{
shortest_path_node_list[(*node_it)].clear();
shortest_path_edge_list[(*node_it)].clear();
}
}
}
void dijkstra::fill_node_list(const node& dest)
{
if ((dest == s) || (!reached(dest)))
{
return;
}
node cur_node = dest;
while (cur_node != node())
{
shortest_path_node_list[dest].push_front(cur_node);
cur_node = predecessor_node(cur_node);
}
}
void dijkstra::fill_edge_list(const node& dest)
{
if ((dest == s) || (!reached(dest)))
{
return;
}
node cur_node = dest;
edge cur_edge = predecessor_edge(dest);
while (cur_edge != edge())
{
shortest_path_edge_list[dest].push_front(cur_edge);
cur_node = predecessor_node(cur_node);
cur_edge = predecessor_edge(cur_node);
}
}
__GTL_END_NAMESPACE
//--------------------------------------------------------------------------
// end of file
//--------------------------------------------------------------------------