0
2
0
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BELLMAN_FORD_H |
20 | 20 |
#define LEMON_BELLMAN_FORD_H |
21 | 21 |
|
22 | 22 |
/// \ingroup shortest_path |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Bellman-Ford algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 |
#include <lemon/tolerance.h> |
|
32 | 31 |
#include <lemon/path.h> |
33 | 32 |
|
34 | 33 |
#include <limits> |
35 | 34 |
|
36 | 35 |
namespace lemon { |
37 | 36 |
|
38 |
/// \brief Default |
|
37 |
/// \brief Default OperationTraits for the BellmanFord algorithm class. |
|
39 | 38 |
/// |
40 | 39 |
/// This operation traits class defines all computational operations |
41 | 40 |
/// and constants that are used in the Bellman-Ford algorithm. |
42 | 41 |
/// The default implementation is based on the \c numeric_limits class. |
43 | 42 |
/// If the numeric type does not have infinity value, then the maximum |
44 | 43 |
/// value is used as extremal infinity value. |
45 |
/// |
|
46 |
/// \see BellmanFordToleranceOperationTraits |
|
47 | 44 |
template < |
48 | 45 |
typename V, |
49 | 46 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
50 | 47 |
struct BellmanFordDefaultOperationTraits { |
51 |
/// \ |
|
48 |
/// \e |
|
52 | 49 |
typedef V Value; |
53 | 50 |
/// \brief Gives back the zero value of the type. |
54 | 51 |
static Value zero() { |
55 | 52 |
return static_cast<Value>(0); |
56 | 53 |
} |
57 | 54 |
/// \brief Gives back the positive infinity value of the type. |
58 | 55 |
static Value infinity() { |
59 | 56 |
return std::numeric_limits<Value>::infinity(); |
60 | 57 |
} |
61 | 58 |
/// \brief Gives back the sum of the given two elements. |
62 | 59 |
static Value plus(const Value& left, const Value& right) { |
63 | 60 |
return left + right; |
64 | 61 |
} |
65 | 62 |
/// \brief Gives back \c true only if the first value is less than |
66 | 63 |
/// the second. |
67 | 64 |
static bool less(const Value& left, const Value& right) { |
68 | 65 |
return left < right; |
69 | 66 |
} |
70 | 67 |
}; |
71 | 68 |
|
72 | 69 |
template <typename V> |
73 | 70 |
struct BellmanFordDefaultOperationTraits<V, false> { |
74 | 71 |
typedef V Value; |
75 | 72 |
static Value zero() { |
76 | 73 |
return static_cast<Value>(0); |
77 | 74 |
} |
78 | 75 |
static Value infinity() { |
79 | 76 |
return std::numeric_limits<Value>::max(); |
80 | 77 |
} |
81 | 78 |
static Value plus(const Value& left, const Value& right) { |
82 | 79 |
if (left == infinity() || right == infinity()) return infinity(); |
83 | 80 |
return left + right; |
84 | 81 |
} |
85 | 82 |
static bool less(const Value& left, const Value& right) { |
86 | 83 |
return left < right; |
87 | 84 |
} |
88 | 85 |
}; |
89 | 86 |
|
90 |
/// \brief Operation traits for the BellmanFord algorithm class |
|
91 |
/// using tolerance. |
|
92 |
/// |
|
93 |
/// This operation traits class defines all computational operations |
|
94 |
/// and constants that are used in the Bellman-Ford algorithm. |
|
95 |
/// The only difference between this implementation and |
|
96 |
/// \ref BellmanFordDefaultOperationTraits is that this class uses |
|
97 |
/// the \ref Tolerance "tolerance technique" in its \ref less() |
|
98 |
/// function. |
|
99 |
/// |
|
100 |
/// \tparam V The value type. |
|
101 |
/// \tparam eps The epsilon value for the \ref less() function. |
|
102 |
/// By default, it is the epsilon value used by \ref Tolerance |
|
103 |
/// "Tolerance<V>". |
|
104 |
/// |
|
105 |
/// \see BellmanFordDefaultOperationTraits |
|
106 |
#ifdef DOXYGEN |
|
107 |
template <typename V, V eps> |
|
108 |
#else |
|
109 |
template < |
|
110 |
typename V, |
|
111 |
V eps = Tolerance<V>::def_epsilon> |
|
112 |
#endif |
|
113 |
struct BellmanFordToleranceOperationTraits { |
|
114 |
/// \brief Value type for the algorithm. |
|
115 |
typedef V Value; |
|
116 |
/// \brief Gives back the zero value of the type. |
|
117 |
static Value zero() { |
|
118 |
return static_cast<Value>(0); |
|
119 |
} |
|
120 |
/// \brief Gives back the positive infinity value of the type. |
|
121 |
static Value infinity() { |
|
122 |
return std::numeric_limits<Value>::infinity(); |
|
123 |
} |
|
124 |
/// \brief Gives back the sum of the given two elements. |
|
125 |
static Value plus(const Value& left, const Value& right) { |
|
126 |
return left + right; |
|
127 |
} |
|
128 |
/// \brief Gives back \c true only if the first value is less than |
|
129 |
/// the second. |
|
130 |
static bool less(const Value& left, const Value& right) { |
|
131 |
return left + eps < right; |
|
132 |
} |
|
133 |
}; |
|
134 |
|
|
135 | 87 |
/// \brief Default traits class of BellmanFord class. |
136 | 88 |
/// |
137 | 89 |
/// Default traits class of BellmanFord class. |
138 | 90 |
/// \param GR The type of the digraph. |
139 | 91 |
/// \param LEN The type of the length map. |
140 | 92 |
template<typename GR, typename LEN> |
141 | 93 |
struct BellmanFordDefaultTraits { |
142 | 94 |
/// The type of the digraph the algorithm runs on. |
143 | 95 |
typedef GR Digraph; |
144 | 96 |
|
145 | 97 |
/// \brief The type of the map that stores the arc lengths. |
146 | 98 |
/// |
147 | 99 |
/// The type of the map that stores the arc lengths. |
148 | 100 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
149 | 101 |
typedef LEN LengthMap; |
150 | 102 |
|
151 | 103 |
/// The type of the arc lengths. |
152 | 104 |
typedef typename LEN::Value Value; |
153 | 105 |
|
154 | 106 |
/// \brief Operation traits for Bellman-Ford algorithm. |
155 | 107 |
/// |
156 | 108 |
/// It defines the used operations and the infinity value for the |
157 | 109 |
/// given \c Value type. |
158 |
/// \see BellmanFordDefaultOperationTraits, |
|
159 |
/// BellmanFordToleranceOperationTraits |
|
110 |
/// \see BellmanFordDefaultOperationTraits |
|
160 | 111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
161 | 112 |
|
162 | 113 |
/// \brief The type of the map that stores the last arcs of the |
163 | 114 |
/// shortest paths. |
164 | 115 |
/// |
165 | 116 |
/// The type of the map that stores the last |
166 | 117 |
/// arcs of the shortest paths. |
167 | 118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
168 | 119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
169 | 120 |
|
170 | 121 |
/// \brief Instantiates a \c PredMap. |
171 | 122 |
/// |
172 | 123 |
/// This function instantiates a \ref PredMap. |
173 | 124 |
/// \param g is the digraph to which we would like to define the |
174 | 125 |
/// \ref PredMap. |
175 | 126 |
static PredMap *createPredMap(const GR& g) { |
176 | 127 |
return new PredMap(g); |
177 | 128 |
} |
178 | 129 |
|
179 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
180 | 131 |
/// |
181 | 132 |
/// The type of the map that stores the distances of the nodes. |
182 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
183 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
184 | 135 |
|
185 | 136 |
/// \brief Instantiates a \c DistMap. |
186 | 137 |
/// |
187 | 138 |
/// This function instantiates a \ref DistMap. |
188 | 139 |
/// \param g is the digraph to which we would like to define the |
189 | 140 |
/// \ref DistMap. |
190 | 141 |
static DistMap *createDistMap(const GR& g) { |
191 | 142 |
return new DistMap(g); |
192 | 143 |
} |
193 | 144 |
|
194 | 145 |
}; |
195 | 146 |
|
196 | 147 |
/// \brief %BellmanFord algorithm class. |
197 | 148 |
/// |
198 | 149 |
/// \ingroup shortest_path |
199 | 150 |
/// This class provides an efficient implementation of the Bellman-Ford |
200 | 151 |
/// algorithm. The maximum time complexity of the algorithm is |
201 | 152 |
/// <tt>O(ne)</tt>. |
202 | 153 |
/// |
203 | 154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
204 | 155 |
/// problem when the arcs can have negative lengths, but the digraph |
205 | 156 |
/// should not contain directed cycles with negative total length. |
206 | 157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
207 | 158 |
/// algorithm instead, since it is more efficient. |
208 | 159 |
/// |
209 | 160 |
/// The arc lengths are passed to the algorithm using a |
210 | 161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
211 | 162 |
/// kind of length. The type of the length values is determined by the |
212 | 163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
213 | 164 |
/// |
214 | 165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
215 | 166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
216 | 167 |
/// it can be used easier. |
217 | 168 |
/// |
218 | 169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
219 | 170 |
/// The default type is \ref ListDigraph. |
220 | 171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
221 | 172 |
/// the lengths of the arcs. The default map type is |
222 | 173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
223 | 174 |
/// \tparam TR The traits class that defines various types used by the |
224 | 175 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
225 | 176 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
226 | 177 |
/// In most cases, this parameter should not be set directly, |
227 | 178 |
/// consider to use the named template parameters instead. |
228 | 179 |
#ifdef DOXYGEN |
229 | 180 |
template <typename GR, typename LEN, typename TR> |
230 | 181 |
#else |
231 | 182 |
template <typename GR=ListDigraph, |
232 | 183 |
typename LEN=typename GR::template ArcMap<int>, |
233 | 184 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
234 | 185 |
#endif |
235 | 186 |
class BellmanFord { |
236 | 187 |
public: |
237 | 188 |
|
238 | 189 |
///The type of the underlying digraph. |
239 | 190 |
typedef typename TR::Digraph Digraph; |
240 | 191 |
|
241 | 192 |
/// \brief The type of the arc lengths. |
242 | 193 |
typedef typename TR::LengthMap::Value Value; |
243 | 194 |
/// \brief The type of the map that stores the arc lengths. |
244 | 195 |
typedef typename TR::LengthMap LengthMap; |
245 | 196 |
/// \brief The type of the map that stores the last |
246 | 197 |
/// arcs of the shortest paths. |
247 | 198 |
typedef typename TR::PredMap PredMap; |
248 | 199 |
/// \brief The type of the map that stores the distances of the nodes. |
249 | 200 |
typedef typename TR::DistMap DistMap; |
250 | 201 |
/// The type of the paths. |
251 | 202 |
typedef PredMapPath<Digraph, PredMap> Path; |
252 | 203 |
///\brief The \ref BellmanFordDefaultOperationTraits |
253 | 204 |
/// "operation traits class" of the algorithm. |
254 | 205 |
typedef typename TR::OperationTraits OperationTraits; |
255 | 206 |
|
256 | 207 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
257 | 208 |
typedef TR Traits; |
258 | 209 |
|
259 | 210 |
private: |
260 | 211 |
|
261 | 212 |
typedef typename Digraph::Node Node; |
262 | 213 |
typedef typename Digraph::NodeIt NodeIt; |
263 | 214 |
typedef typename Digraph::Arc Arc; |
264 | 215 |
typedef typename Digraph::OutArcIt OutArcIt; |
265 | 216 |
|
266 | 217 |
// Pointer to the underlying digraph. |
267 | 218 |
const Digraph *_gr; |
268 | 219 |
// Pointer to the length map |
269 | 220 |
const LengthMap *_length; |
270 | 221 |
// Pointer to the map of predecessors arcs. |
271 | 222 |
PredMap *_pred; |
272 | 223 |
// Indicates if _pred is locally allocated (true) or not. |
273 | 224 |
bool _local_pred; |
274 | 225 |
// Pointer to the map of distances. |
275 | 226 |
DistMap *_dist; |
276 | 227 |
// Indicates if _dist is locally allocated (true) or not. |
277 | 228 |
bool _local_dist; |
278 | 229 |
|
279 | 230 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
280 | 231 |
MaskMap *_mask; |
281 | 232 |
|
282 | 233 |
std::vector<Node> _process; |
283 | 234 |
|
284 | 235 |
// Creates the maps if necessary. |
285 | 236 |
void create_maps() { |
286 | 237 |
if(!_pred) { |
287 | 238 |
_local_pred = true; |
288 | 239 |
_pred = Traits::createPredMap(*_gr); |
289 | 240 |
} |
290 | 241 |
if(!_dist) { |
291 | 242 |
_local_dist = true; |
292 | 243 |
_dist = Traits::createDistMap(*_gr); |
293 | 244 |
} |
294 | 245 |
if(!_mask) { |
295 | 246 |
_mask = new MaskMap(*_gr); |
296 | 247 |
} |
297 | 248 |
} |
298 | 249 |
|
299 | 250 |
public : |
300 | 251 |
|
301 | 252 |
typedef BellmanFord Create; |
302 | 253 |
|
303 | 254 |
/// \name Named Template Parameters |
304 | 255 |
|
305 | 256 |
///@{ |
306 | 257 |
|
307 | 258 |
template <class T> |
308 | 259 |
struct SetPredMapTraits : public Traits { |
309 | 260 |
typedef T PredMap; |
310 | 261 |
static PredMap *createPredMap(const Digraph&) { |
311 | 262 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
312 | 263 |
return 0; // ignore warnings |
313 | 264 |
} |
314 | 265 |
}; |
315 | 266 |
|
316 | 267 |
/// \brief \ref named-templ-param "Named parameter" for setting |
317 | 268 |
/// \c PredMap type. |
318 | 269 |
/// |
319 | 270 |
/// \ref named-templ-param "Named parameter" for setting |
320 | 271 |
/// \c PredMap type. |
321 | 272 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
322 | 273 |
template <class T> |
323 | 274 |
struct SetPredMap |
324 | 275 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
325 | 276 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
326 | 277 |
}; |
327 | 278 |
|
328 | 279 |
template <class T> |
329 | 280 |
struct SetDistMapTraits : public Traits { |
330 | 281 |
typedef T DistMap; |
331 | 282 |
static DistMap *createDistMap(const Digraph&) { |
332 | 283 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
333 | 284 |
return 0; // ignore warnings |
334 | 285 |
} |
335 | 286 |
}; |
336 | 287 |
|
337 | 288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
338 | 289 |
/// \c DistMap type. |
339 | 290 |
/// |
340 | 291 |
/// \ref named-templ-param "Named parameter" for setting |
341 | 292 |
/// \c DistMap type. |
342 | 293 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
343 | 294 |
template <class T> |
344 | 295 |
struct SetDistMap |
345 | 296 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
346 | 297 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
347 | 298 |
}; |
348 | 299 |
|
349 | 300 |
template <class T> |
350 | 301 |
struct SetOperationTraitsTraits : public Traits { |
351 | 302 |
typedef T OperationTraits; |
352 | 303 |
}; |
353 | 304 |
|
354 | 305 |
/// \brief \ref named-templ-param "Named parameter" for setting |
355 | 306 |
/// \c OperationTraits type. |
356 | 307 |
/// |
357 | 308 |
/// \ref named-templ-param "Named parameter" for setting |
358 | 309 |
/// \c OperationTraits type. |
359 | 310 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
360 | 311 |
template <class T> |
361 | 312 |
struct SetOperationTraits |
362 | 313 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
363 | 314 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
364 | 315 |
Create; |
365 | 316 |
}; |
366 | 317 |
|
367 | 318 |
///@} |
368 | 319 |
|
369 | 320 |
protected: |
370 | 321 |
|
371 | 322 |
BellmanFord() {} |
372 | 323 |
|
373 | 324 |
public: |
374 | 325 |
|
375 | 326 |
/// \brief Constructor. |
376 | 327 |
/// |
377 | 328 |
/// Constructor. |
378 | 329 |
/// \param g The digraph the algorithm runs on. |
379 | 330 |
/// \param length The length map used by the algorithm. |
380 | 331 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
381 | 332 |
_gr(&g), _length(&length), |
382 | 333 |
_pred(0), _local_pred(false), |
383 | 334 |
_dist(0), _local_dist(false), _mask(0) {} |
384 | 335 |
|
385 | 336 |
///Destructor. |
386 | 337 |
~BellmanFord() { |
387 | 338 |
if(_local_pred) delete _pred; |
388 | 339 |
if(_local_dist) delete _dist; |
389 | 340 |
if(_mask) delete _mask; |
390 | 341 |
} |
391 | 342 |
|
392 | 343 |
/// \brief Sets the length map. |
393 | 344 |
/// |
394 | 345 |
/// Sets the length map. |
395 | 346 |
/// \return <tt>(*this)</tt> |
396 | 347 |
BellmanFord &lengthMap(const LengthMap &map) { |
397 | 348 |
_length = ↦ |
398 | 349 |
return *this; |
399 | 350 |
} |
400 | 351 |
|
401 | 352 |
/// \brief Sets the map that stores the predecessor arcs. |
402 | 353 |
/// |
403 | 354 |
/// Sets the map that stores the predecessor arcs. |
404 | 355 |
/// If you don't use this function before calling \ref run() |
405 | 356 |
/// or \ref init(), an instance will be allocated automatically. |
406 | 357 |
/// The destructor deallocates this automatically allocated map, |
407 | 358 |
/// of course. |
408 | 359 |
/// \return <tt>(*this)</tt> |
409 | 360 |
BellmanFord &predMap(PredMap &map) { |
410 | 361 |
if(_local_pred) { |
411 | 362 |
delete _pred; |
412 | 363 |
_local_pred=false; |
413 | 364 |
} |
414 | 365 |
_pred = ↦ |
415 | 366 |
return *this; |
... | ... |
@@ -633,514 +584,513 @@ |
633 | 584 |
} |
634 | 585 |
} |
635 | 586 |
|
636 | 587 |
/// \brief Runs the algorithm from the given root node. |
637 | 588 |
/// |
638 | 589 |
/// This method runs the Bellman-Ford algorithm from the given root |
639 | 590 |
/// node \c s in order to compute the shortest path to each node. |
640 | 591 |
/// |
641 | 592 |
/// The algorithm computes |
642 | 593 |
/// - the shortest path tree (forest), |
643 | 594 |
/// - the distance of each node from the root(s). |
644 | 595 |
/// |
645 | 596 |
/// \note bf.run(s) is just a shortcut of the following code. |
646 | 597 |
/// \code |
647 | 598 |
/// bf.init(); |
648 | 599 |
/// bf.addSource(s); |
649 | 600 |
/// bf.start(); |
650 | 601 |
/// \endcode |
651 | 602 |
void run(Node s) { |
652 | 603 |
init(); |
653 | 604 |
addSource(s); |
654 | 605 |
start(); |
655 | 606 |
} |
656 | 607 |
|
657 | 608 |
/// \brief Runs the algorithm from the given root node with arc |
658 | 609 |
/// number limit. |
659 | 610 |
/// |
660 | 611 |
/// This method runs the Bellman-Ford algorithm from the given root |
661 | 612 |
/// node \c s in order to compute the shortest path distance for each |
662 | 613 |
/// node using only the paths consisting of at most \c num arcs. |
663 | 614 |
/// |
664 | 615 |
/// The algorithm computes |
665 | 616 |
/// - the limited distance of each node from the root(s), |
666 | 617 |
/// - the predecessor arc for each node. |
667 | 618 |
/// |
668 | 619 |
/// \warning The paths with limited arc number cannot be retrieved |
669 | 620 |
/// easily with \ref path() or \ref predArc() functions. If you also |
670 | 621 |
/// need the shortest paths and not only the distances, you should |
671 | 622 |
/// store the \ref predMap() "predecessor map" after each iteration |
672 | 623 |
/// and build the path manually. |
673 | 624 |
/// |
674 | 625 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
675 | 626 |
/// \code |
676 | 627 |
/// bf.init(); |
677 | 628 |
/// bf.addSource(s); |
678 | 629 |
/// bf.limitedStart(num); |
679 | 630 |
/// \endcode |
680 | 631 |
void run(Node s, int num) { |
681 | 632 |
init(); |
682 | 633 |
addSource(s); |
683 | 634 |
limitedStart(num); |
684 | 635 |
} |
685 | 636 |
|
686 | 637 |
///@} |
687 | 638 |
|
688 | 639 |
/// \brief LEMON iterator for getting the active nodes. |
689 | 640 |
/// |
690 | 641 |
/// This class provides a common style LEMON iterator that traverses |
691 | 642 |
/// the active nodes of the Bellman-Ford algorithm after the last |
692 | 643 |
/// phase. These nodes should be checked in the next phase to |
693 | 644 |
/// find augmenting arcs outgoing from them. |
694 | 645 |
class ActiveIt { |
695 | 646 |
public: |
696 | 647 |
|
697 | 648 |
/// \brief Constructor. |
698 | 649 |
/// |
699 | 650 |
/// Constructor for getting the active nodes of the given BellmanFord |
700 | 651 |
/// instance. |
701 | 652 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
702 | 653 |
{ |
703 | 654 |
_index = _algorithm->_process.size() - 1; |
704 | 655 |
} |
705 | 656 |
|
706 | 657 |
/// \brief Invalid constructor. |
707 | 658 |
/// |
708 | 659 |
/// Invalid constructor. |
709 | 660 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
710 | 661 |
|
711 | 662 |
/// \brief Conversion to \c Node. |
712 | 663 |
/// |
713 | 664 |
/// Conversion to \c Node. |
714 | 665 |
operator Node() const { |
715 | 666 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
716 | 667 |
} |
717 | 668 |
|
718 | 669 |
/// \brief Increment operator. |
719 | 670 |
/// |
720 | 671 |
/// Increment operator. |
721 | 672 |
ActiveIt& operator++() { |
722 | 673 |
--_index; |
723 | 674 |
return *this; |
724 | 675 |
} |
725 | 676 |
|
726 | 677 |
bool operator==(const ActiveIt& it) const { |
727 | 678 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
728 | 679 |
} |
729 | 680 |
bool operator!=(const ActiveIt& it) const { |
730 | 681 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
731 | 682 |
} |
732 | 683 |
bool operator<(const ActiveIt& it) const { |
733 | 684 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
734 | 685 |
} |
735 | 686 |
|
736 | 687 |
private: |
737 | 688 |
const BellmanFord* _algorithm; |
738 | 689 |
int _index; |
739 | 690 |
}; |
740 | 691 |
|
741 | 692 |
/// \name Query Functions |
742 | 693 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
743 | 694 |
/// functions.\n |
744 | 695 |
/// Either \ref run() or \ref init() should be called before using them. |
745 | 696 |
|
746 | 697 |
///@{ |
747 | 698 |
|
748 | 699 |
/// \brief The shortest path to the given node. |
749 | 700 |
/// |
750 | 701 |
/// Gives back the shortest path to the given node from the root(s). |
751 | 702 |
/// |
752 | 703 |
/// \warning \c t should be reached from the root(s). |
753 | 704 |
/// |
754 | 705 |
/// \pre Either \ref run() or \ref init() must be called before |
755 | 706 |
/// using this function. |
756 | 707 |
Path path(Node t) const |
757 | 708 |
{ |
758 | 709 |
return Path(*_gr, *_pred, t); |
759 | 710 |
} |
760 | 711 |
|
761 | 712 |
/// \brief The distance of the given node from the root(s). |
762 | 713 |
/// |
763 | 714 |
/// Returns the distance of the given node from the root(s). |
764 | 715 |
/// |
765 | 716 |
/// \warning If node \c v is not reached from the root(s), then |
766 | 717 |
/// the return value of this function is undefined. |
767 | 718 |
/// |
768 | 719 |
/// \pre Either \ref run() or \ref init() must be called before |
769 | 720 |
/// using this function. |
770 | 721 |
Value dist(Node v) const { return (*_dist)[v]; } |
771 | 722 |
|
772 | 723 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
773 | 724 |
/// the given node. |
774 | 725 |
/// |
775 | 726 |
/// This function returns the 'previous arc' of the shortest path |
776 | 727 |
/// tree for node \c v, i.e. it returns the last arc of a |
777 | 728 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
778 | 729 |
/// is not reached from the root(s) or if \c v is a root. |
779 | 730 |
/// |
780 | 731 |
/// The shortest path tree used here is equal to the shortest path |
781 | 732 |
/// tree used in \ref predNode() and \ref predMap(). |
782 | 733 |
/// |
783 | 734 |
/// \pre Either \ref run() or \ref init() must be called before |
784 | 735 |
/// using this function. |
785 | 736 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
786 | 737 |
|
787 | 738 |
/// \brief Returns the 'previous node' of the shortest path tree for |
788 | 739 |
/// the given node. |
789 | 740 |
/// |
790 | 741 |
/// This function returns the 'previous node' of the shortest path |
791 | 742 |
/// tree for node \c v, i.e. it returns the last but one node of |
792 | 743 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
793 | 744 |
/// is not reached from the root(s) or if \c v is a root. |
794 | 745 |
/// |
795 | 746 |
/// The shortest path tree used here is equal to the shortest path |
796 | 747 |
/// tree used in \ref predArc() and \ref predMap(). |
797 | 748 |
/// |
798 | 749 |
/// \pre Either \ref run() or \ref init() must be called before |
799 | 750 |
/// using this function. |
800 | 751 |
Node predNode(Node v) const { |
801 | 752 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
802 | 753 |
} |
803 | 754 |
|
804 | 755 |
/// \brief Returns a const reference to the node map that stores the |
805 | 756 |
/// distances of the nodes. |
806 | 757 |
/// |
807 | 758 |
/// Returns a const reference to the node map that stores the distances |
808 | 759 |
/// of the nodes calculated by the algorithm. |
809 | 760 |
/// |
810 | 761 |
/// \pre Either \ref run() or \ref init() must be called before |
811 | 762 |
/// using this function. |
812 | 763 |
const DistMap &distMap() const { return *_dist;} |
813 | 764 |
|
814 | 765 |
/// \brief Returns a const reference to the node map that stores the |
815 | 766 |
/// predecessor arcs. |
816 | 767 |
/// |
817 | 768 |
/// Returns a const reference to the node map that stores the predecessor |
818 | 769 |
/// arcs, which form the shortest path tree (forest). |
819 | 770 |
/// |
820 | 771 |
/// \pre Either \ref run() or \ref init() must be called before |
821 | 772 |
/// using this function. |
822 | 773 |
const PredMap &predMap() const { return *_pred; } |
823 | 774 |
|
824 | 775 |
/// \brief Checks if a node is reached from the root(s). |
825 | 776 |
/// |
826 | 777 |
/// Returns \c true if \c v is reached from the root(s). |
827 | 778 |
/// |
828 | 779 |
/// \pre Either \ref run() or \ref init() must be called before |
829 | 780 |
/// using this function. |
830 | 781 |
bool reached(Node v) const { |
831 | 782 |
return (*_dist)[v] != OperationTraits::infinity(); |
832 | 783 |
} |
833 | 784 |
|
834 | 785 |
/// \brief Gives back a negative cycle. |
835 | 786 |
/// |
836 | 787 |
/// This function gives back a directed cycle with negative total |
837 | 788 |
/// length if the algorithm has already found one. |
838 | 789 |
/// Otherwise it gives back an empty path. |
839 | 790 |
lemon::Path<Digraph> negativeCycle() const { |
840 | 791 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
841 | 792 |
lemon::Path<Digraph> cycle; |
842 | 793 |
for (int i = 0; i < int(_process.size()); ++i) { |
843 | 794 |
if (state[_process[i]] != -1) continue; |
844 | 795 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
845 | 796 |
v = _gr->source((*_pred)[v])) { |
846 | 797 |
if (state[v] == i) { |
847 | 798 |
cycle.addFront((*_pred)[v]); |
848 | 799 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
849 | 800 |
u = _gr->source((*_pred)[u])) { |
850 | 801 |
cycle.addFront((*_pred)[u]); |
851 | 802 |
} |
852 | 803 |
return cycle; |
853 | 804 |
} |
854 | 805 |
else if (state[v] >= 0) { |
855 | 806 |
break; |
856 | 807 |
} |
857 | 808 |
state[v] = i; |
858 | 809 |
} |
859 | 810 |
} |
860 | 811 |
return cycle; |
861 | 812 |
} |
862 | 813 |
|
863 | 814 |
///@} |
864 | 815 |
}; |
865 | 816 |
|
866 | 817 |
/// \brief Default traits class of bellmanFord() function. |
867 | 818 |
/// |
868 | 819 |
/// Default traits class of bellmanFord() function. |
869 | 820 |
/// \tparam GR The type of the digraph. |
870 | 821 |
/// \tparam LEN The type of the length map. |
871 | 822 |
template <typename GR, typename LEN> |
872 | 823 |
struct BellmanFordWizardDefaultTraits { |
873 | 824 |
/// The type of the digraph the algorithm runs on. |
874 | 825 |
typedef GR Digraph; |
875 | 826 |
|
876 | 827 |
/// \brief The type of the map that stores the arc lengths. |
877 | 828 |
/// |
878 | 829 |
/// The type of the map that stores the arc lengths. |
879 | 830 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
880 | 831 |
typedef LEN LengthMap; |
881 | 832 |
|
882 | 833 |
/// The type of the arc lengths. |
883 | 834 |
typedef typename LEN::Value Value; |
884 | 835 |
|
885 | 836 |
/// \brief Operation traits for Bellman-Ford algorithm. |
886 | 837 |
/// |
887 | 838 |
/// It defines the used operations and the infinity value for the |
888 | 839 |
/// given \c Value type. |
889 |
/// \see BellmanFordDefaultOperationTraits, |
|
890 |
/// BellmanFordToleranceOperationTraits |
|
840 |
/// \see BellmanFordDefaultOperationTraits |
|
891 | 841 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
892 | 842 |
|
893 | 843 |
/// \brief The type of the map that stores the last |
894 | 844 |
/// arcs of the shortest paths. |
895 | 845 |
/// |
896 | 846 |
/// The type of the map that stores the last arcs of the shortest paths. |
897 | 847 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
898 | 848 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
899 | 849 |
|
900 | 850 |
/// \brief Instantiates a \c PredMap. |
901 | 851 |
/// |
902 | 852 |
/// This function instantiates a \ref PredMap. |
903 | 853 |
/// \param g is the digraph to which we would like to define the |
904 | 854 |
/// \ref PredMap. |
905 | 855 |
static PredMap *createPredMap(const GR &g) { |
906 | 856 |
return new PredMap(g); |
907 | 857 |
} |
908 | 858 |
|
909 | 859 |
/// \brief The type of the map that stores the distances of the nodes. |
910 | 860 |
/// |
911 | 861 |
/// The type of the map that stores the distances of the nodes. |
912 | 862 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
913 | 863 |
typedef typename GR::template NodeMap<Value> DistMap; |
914 | 864 |
|
915 | 865 |
/// \brief Instantiates a \c DistMap. |
916 | 866 |
/// |
917 | 867 |
/// This function instantiates a \ref DistMap. |
918 | 868 |
/// \param g is the digraph to which we would like to define the |
919 | 869 |
/// \ref DistMap. |
920 | 870 |
static DistMap *createDistMap(const GR &g) { |
921 | 871 |
return new DistMap(g); |
922 | 872 |
} |
923 | 873 |
|
924 | 874 |
///The type of the shortest paths. |
925 | 875 |
|
926 | 876 |
///The type of the shortest paths. |
927 | 877 |
///It must meet the \ref concepts::Path "Path" concept. |
928 | 878 |
typedef lemon::Path<Digraph> Path; |
929 | 879 |
}; |
930 | 880 |
|
931 | 881 |
/// \brief Default traits class used by BellmanFordWizard. |
932 | 882 |
/// |
933 | 883 |
/// Default traits class used by BellmanFordWizard. |
934 | 884 |
/// \tparam GR The type of the digraph. |
935 | 885 |
/// \tparam LEN The type of the length map. |
936 | 886 |
template <typename GR, typename LEN> |
937 | 887 |
class BellmanFordWizardBase |
938 | 888 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
939 | 889 |
|
940 | 890 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
941 | 891 |
protected: |
942 | 892 |
// Type of the nodes in the digraph. |
943 | 893 |
typedef typename Base::Digraph::Node Node; |
944 | 894 |
|
945 | 895 |
// Pointer to the underlying digraph. |
946 | 896 |
void *_graph; |
947 | 897 |
// Pointer to the length map |
948 | 898 |
void *_length; |
949 | 899 |
// Pointer to the map of predecessors arcs. |
950 | 900 |
void *_pred; |
951 | 901 |
// Pointer to the map of distances. |
952 | 902 |
void *_dist; |
953 | 903 |
//Pointer to the shortest path to the target node. |
954 | 904 |
void *_path; |
955 | 905 |
//Pointer to the distance of the target node. |
956 | 906 |
void *_di; |
957 | 907 |
|
958 | 908 |
public: |
959 | 909 |
/// Constructor. |
960 | 910 |
|
961 | 911 |
/// This constructor does not require parameters, it initiates |
962 | 912 |
/// all of the attributes to default values \c 0. |
963 | 913 |
BellmanFordWizardBase() : |
964 | 914 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
965 | 915 |
|
966 | 916 |
/// Constructor. |
967 | 917 |
|
968 | 918 |
/// This constructor requires two parameters, |
969 | 919 |
/// others are initiated to \c 0. |
970 | 920 |
/// \param gr The digraph the algorithm runs on. |
971 | 921 |
/// \param len The length map. |
972 | 922 |
BellmanFordWizardBase(const GR& gr, |
973 | 923 |
const LEN& len) : |
974 | 924 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
975 | 925 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
976 | 926 |
_pred(0), _dist(0), _path(0), _di(0) {} |
977 | 927 |
|
978 | 928 |
}; |
979 | 929 |
|
980 | 930 |
/// \brief Auxiliary class for the function-type interface of the |
981 | 931 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
982 | 932 |
/// |
983 | 933 |
/// This auxiliary class is created to implement the |
984 | 934 |
/// \ref bellmanFord() "function-type interface" of the |
985 | 935 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
986 | 936 |
/// It does not have own \ref run() method, it uses the |
987 | 937 |
/// functions and features of the plain \ref BellmanFord. |
988 | 938 |
/// |
989 | 939 |
/// This class should only be used through the \ref bellmanFord() |
990 | 940 |
/// function, which makes it easier to use the algorithm. |
991 | 941 |
/// |
992 | 942 |
/// \tparam TR The traits class that defines various types used by the |
993 | 943 |
/// algorithm. |
994 | 944 |
template<class TR> |
995 | 945 |
class BellmanFordWizard : public TR { |
996 | 946 |
typedef TR Base; |
997 | 947 |
|
998 | 948 |
typedef typename TR::Digraph Digraph; |
999 | 949 |
|
1000 | 950 |
typedef typename Digraph::Node Node; |
1001 | 951 |
typedef typename Digraph::NodeIt NodeIt; |
1002 | 952 |
typedef typename Digraph::Arc Arc; |
1003 | 953 |
typedef typename Digraph::OutArcIt ArcIt; |
1004 | 954 |
|
1005 | 955 |
typedef typename TR::LengthMap LengthMap; |
1006 | 956 |
typedef typename LengthMap::Value Value; |
1007 | 957 |
typedef typename TR::PredMap PredMap; |
1008 | 958 |
typedef typename TR::DistMap DistMap; |
1009 | 959 |
typedef typename TR::Path Path; |
1010 | 960 |
|
1011 | 961 |
public: |
1012 | 962 |
/// Constructor. |
1013 | 963 |
BellmanFordWizard() : TR() {} |
1014 | 964 |
|
1015 | 965 |
/// \brief Constructor that requires parameters. |
1016 | 966 |
/// |
1017 | 967 |
/// Constructor that requires parameters. |
1018 | 968 |
/// These parameters will be the default values for the traits class. |
1019 | 969 |
/// \param gr The digraph the algorithm runs on. |
1020 | 970 |
/// \param len The length map. |
1021 | 971 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
1022 | 972 |
: TR(gr, len) {} |
1023 | 973 |
|
1024 | 974 |
/// \brief Copy constructor |
1025 | 975 |
BellmanFordWizard(const TR &b) : TR(b) {} |
1026 | 976 |
|
1027 | 977 |
~BellmanFordWizard() {} |
1028 | 978 |
|
1029 | 979 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
1030 | 980 |
/// |
1031 | 981 |
/// This method runs the Bellman-Ford algorithm from the given source |
1032 | 982 |
/// node in order to compute the shortest path to each node. |
1033 | 983 |
void run(Node s) { |
1034 | 984 |
BellmanFord<Digraph,LengthMap,TR> |
1035 | 985 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
1036 | 986 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1037 | 987 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1038 | 988 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1039 | 989 |
bf.run(s); |
1040 | 990 |
} |
1041 | 991 |
|
1042 | 992 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
1043 | 993 |
/// between \c s and \c t. |
1044 | 994 |
/// |
1045 | 995 |
/// This method runs the Bellman-Ford algorithm from node \c s |
1046 | 996 |
/// in order to compute the shortest path to node \c t. |
1047 | 997 |
/// Actually, it computes the shortest path to each node, but using |
1048 | 998 |
/// this function you can retrieve the distance and the shortest path |
1049 | 999 |
/// for a single target node easier. |
1050 | 1000 |
/// |
1051 | 1001 |
/// \return \c true if \c t is reachable form \c s. |
1052 | 1002 |
bool run(Node s, Node t) { |
1053 | 1003 |
BellmanFord<Digraph,LengthMap,TR> |
1054 | 1004 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
1055 | 1005 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1056 | 1006 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1057 | 1007 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1058 | 1008 |
bf.run(s); |
1059 | 1009 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
1060 | 1010 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
1061 | 1011 |
return bf.reached(t); |
1062 | 1012 |
} |
1063 | 1013 |
|
1064 | 1014 |
template<class T> |
1065 | 1015 |
struct SetPredMapBase : public Base { |
1066 | 1016 |
typedef T PredMap; |
1067 | 1017 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1068 | 1018 |
SetPredMapBase(const TR &b) : TR(b) {} |
1069 | 1019 |
}; |
1070 | 1020 |
|
1071 | 1021 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1072 | 1022 |
/// the predecessor map. |
1073 | 1023 |
/// |
1074 | 1024 |
/// \ref named-templ-param "Named parameter" for setting |
1075 | 1025 |
/// the map that stores the predecessor arcs of the nodes. |
1076 | 1026 |
template<class T> |
1077 | 1027 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) { |
1078 | 1028 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1079 | 1029 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
1080 | 1030 |
} |
1081 | 1031 |
|
1082 | 1032 |
template<class T> |
1083 | 1033 |
struct SetDistMapBase : public Base { |
1084 | 1034 |
typedef T DistMap; |
1085 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1086 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {} |
1087 | 1037 |
}; |
1088 | 1038 |
|
1089 | 1039 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1090 | 1040 |
/// the distance map. |
1091 | 1041 |
/// |
1092 | 1042 |
/// \ref named-templ-param "Named parameter" for setting |
1093 | 1043 |
/// the map that stores the distances of the nodes calculated |
1094 | 1044 |
/// by the algorithm. |
1095 | 1045 |
template<class T> |
1096 | 1046 |
BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) { |
1097 | 1047 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1098 | 1048 |
return BellmanFordWizard<SetDistMapBase<T> >(*this); |
1099 | 1049 |
} |
1100 | 1050 |
|
1101 | 1051 |
template<class T> |
1102 | 1052 |
struct SetPathBase : public Base { |
1103 | 1053 |
typedef T Path; |
1104 | 1054 |
SetPathBase(const TR &b) : TR(b) {} |
1105 | 1055 |
}; |
1106 | 1056 |
|
1107 | 1057 |
/// \brief \ref named-func-param "Named parameter" for getting |
1108 | 1058 |
/// the shortest path to the target node. |
1109 | 1059 |
/// |
1110 | 1060 |
/// \ref named-func-param "Named parameter" for getting |
1111 | 1061 |
/// the shortest path to the target node. |
1112 | 1062 |
template<class T> |
1113 | 1063 |
BellmanFordWizard<SetPathBase<T> > path(const T &t) |
1114 | 1064 |
{ |
1115 | 1065 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1116 | 1066 |
return BellmanFordWizard<SetPathBase<T> >(*this); |
1117 | 1067 |
} |
1118 | 1068 |
|
1119 | 1069 |
/// \brief \ref named-func-param "Named parameter" for getting |
1120 | 1070 |
/// the distance of the target node. |
1121 | 1071 |
/// |
1122 | 1072 |
/// \ref named-func-param "Named parameter" for getting |
1123 | 1073 |
/// the distance of the target node. |
1124 | 1074 |
BellmanFordWizard dist(const Value &d) |
1125 | 1075 |
{ |
1126 | 1076 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1127 | 1077 |
return *this; |
1128 | 1078 |
} |
1129 | 1079 |
|
1130 | 1080 |
}; |
1131 | 1081 |
|
1132 | 1082 |
/// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
1133 | 1083 |
/// algorithm. |
1134 | 1084 |
/// |
1135 | 1085 |
/// \ingroup shortest_path |
1136 | 1086 |
/// Function type interface for the \ref BellmanFord "Bellman-Ford" |
1137 | 1087 |
/// algorithm. |
1138 | 1088 |
/// |
1139 | 1089 |
/// This function also has several \ref named-templ-func-param |
1140 | 1090 |
/// "named parameters", they are declared as the members of class |
1141 | 1091 |
/// \ref BellmanFordWizard. |
1142 | 1092 |
/// The following examples show how to use these parameters. |
1143 | 1093 |
/// \code |
1144 | 1094 |
/// // Compute shortest path from node s to each node |
1145 | 1095 |
/// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
1146 | 1096 |
/// |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/bellman_ford.h> |
24 | 24 |
#include <lemon/path.h> |
25 | 25 |
|
26 | 26 |
#include "graph_test.h" |
27 | 27 |
#include "test_tools.h" |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"@arcs\n" |
40 | 40 |
" length\n" |
41 | 41 |
"0 1 3\n" |
42 | 42 |
"1 2 -3\n" |
43 | 43 |
"1 2 -5\n" |
44 | 44 |
"1 3 -2\n" |
45 | 45 |
"0 2 -1\n" |
46 | 46 |
"1 2 -4\n" |
47 | 47 |
"0 3 2\n" |
48 | 48 |
"4 2 -5\n" |
49 | 49 |
"2 3 1\n" |
50 | 50 |
"@attributes\n" |
51 | 51 |
"source 0\n" |
52 | 52 |
"target 3\n"; |
53 | 53 |
|
54 | 54 |
|
55 | 55 |
void checkBellmanFordCompile() |
56 | 56 |
{ |
57 | 57 |
typedef int Value; |
58 | 58 |
typedef concepts::Digraph Digraph; |
59 | 59 |
typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap; |
60 | 60 |
typedef BellmanFord<Digraph, LengthMap> BF; |
61 | 61 |
typedef Digraph::Node Node; |
62 | 62 |
typedef Digraph::Arc Arc; |
63 | 63 |
|
64 | 64 |
Digraph gr; |
65 | 65 |
Node s, t, n; |
66 | 66 |
Arc e; |
67 | 67 |
Value l; |
68 | 68 |
int k=3; |
69 | 69 |
bool b; |
70 | 70 |
BF::DistMap d(gr); |
71 | 71 |
BF::PredMap p(gr); |
72 | 72 |
LengthMap length; |
73 | 73 |
concepts::Path<Digraph> pp; |
74 | 74 |
|
75 | 75 |
{ |
76 | 76 |
BF bf_test(gr,length); |
77 | 77 |
const BF& const_bf_test = bf_test; |
78 | 78 |
|
79 | 79 |
bf_test.run(s); |
80 | 80 |
bf_test.run(s,k); |
81 | 81 |
|
82 | 82 |
bf_test.init(); |
83 | 83 |
bf_test.addSource(s); |
84 | 84 |
bf_test.addSource(s, 1); |
85 | 85 |
b = bf_test.processNextRound(); |
86 | 86 |
b = bf_test.processNextWeakRound(); |
87 | 87 |
|
88 | 88 |
bf_test.start(); |
89 | 89 |
bf_test.checkedStart(); |
90 | 90 |
bf_test.limitedStart(k); |
91 | 91 |
|
92 | 92 |
l = const_bf_test.dist(t); |
93 | 93 |
e = const_bf_test.predArc(t); |
94 | 94 |
s = const_bf_test.predNode(t); |
95 | 95 |
b = const_bf_test.reached(t); |
96 | 96 |
d = const_bf_test.distMap(); |
97 | 97 |
p = const_bf_test.predMap(); |
98 | 98 |
pp = const_bf_test.path(t); |
99 | 99 |
pp = const_bf_test.negativeCycle(); |
100 | 100 |
|
101 | 101 |
for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {} |
102 | 102 |
} |
103 | 103 |
{ |
104 | 104 |
BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
105 | 105 |
::SetDistMap<concepts::ReadWriteMap<Node,Value> > |
106 | 106 |
::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> > |
107 |
::SetOperationTraits<BellmanFordToleranceOperationTraits<Value, 0> > |
|
108 | 107 |
::Create bf_test(gr,length); |
109 | 108 |
|
110 | 109 |
LengthMap length_map; |
111 | 110 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
112 | 111 |
concepts::ReadWriteMap<Node,Value> dist_map; |
113 | 112 |
|
114 | 113 |
bf_test |
115 | 114 |
.lengthMap(length_map) |
116 | 115 |
.predMap(pred_map) |
117 | 116 |
.distMap(dist_map); |
118 | 117 |
|
119 | 118 |
bf_test.run(s); |
120 | 119 |
bf_test.run(s,k); |
121 | 120 |
|
122 | 121 |
bf_test.init(); |
123 | 122 |
bf_test.addSource(s); |
124 | 123 |
bf_test.addSource(s, 1); |
125 | 124 |
b = bf_test.processNextRound(); |
126 | 125 |
b = bf_test.processNextWeakRound(); |
127 | 126 |
|
128 | 127 |
bf_test.start(); |
129 | 128 |
bf_test.checkedStart(); |
130 | 129 |
bf_test.limitedStart(k); |
131 | 130 |
|
132 | 131 |
l = bf_test.dist(t); |
133 | 132 |
e = bf_test.predArc(t); |
134 | 133 |
s = bf_test.predNode(t); |
135 | 134 |
b = bf_test.reached(t); |
136 | 135 |
pp = bf_test.path(t); |
137 | 136 |
pp = bf_test.negativeCycle(); |
138 | 137 |
} |
139 | 138 |
} |
140 | 139 |
|
141 | 140 |
void checkBellmanFordFunctionCompile() |
142 | 141 |
{ |
143 | 142 |
typedef int Value; |
144 | 143 |
typedef concepts::Digraph Digraph; |
145 | 144 |
typedef Digraph::Arc Arc; |
146 | 145 |
typedef Digraph::Node Node; |
147 | 146 |
typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap; |
148 | 147 |
|
149 | 148 |
Digraph g; |
150 | 149 |
bool b; |
151 | 150 |
bellmanFord(g,LengthMap()).run(Node()); |
152 | 151 |
b = bellmanFord(g,LengthMap()).run(Node(),Node()); |
153 | 152 |
bellmanFord(g,LengthMap()) |
154 | 153 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
155 | 154 |
.distMap(concepts::ReadWriteMap<Node,Value>()) |
156 | 155 |
.run(Node()); |
157 | 156 |
b=bellmanFord(g,LengthMap()) |
158 | 157 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
159 | 158 |
.distMap(concepts::ReadWriteMap<Node,Value>()) |
160 | 159 |
.path(concepts::Path<Digraph>()) |
161 | 160 |
.dist(Value()) |
162 | 161 |
.run(Node(),Node()); |
163 | 162 |
} |
164 | 163 |
|
165 | 164 |
|
166 | 165 |
template <typename Digraph, typename Value> |
167 | 166 |
void checkBellmanFord() { |
168 | 167 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
169 | 168 |
typedef typename Digraph::template ArcMap<Value> LengthMap; |
170 | 169 |
|
171 | 170 |
Digraph gr; |
172 | 171 |
Node s, t; |
173 | 172 |
LengthMap length(gr); |
174 | 173 |
|
175 | 174 |
std::istringstream input(test_lgf); |
176 | 175 |
digraphReader(gr, input). |
177 | 176 |
arcMap("length", length). |
178 | 177 |
node("source", s). |
179 | 178 |
node("target", t). |
180 | 179 |
run(); |
181 | 180 |
|
182 | 181 |
BellmanFord<Digraph, LengthMap> |
183 | 182 |
bf(gr, length); |
184 | 183 |
bf.run(s); |
185 | 184 |
Path<Digraph> p = bf.path(t); |
186 | 185 |
|
187 | 186 |
check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path."); |
188 | 187 |
check(p.length() == 3, "path() found a wrong path."); |
189 | 188 |
check(checkPath(gr, p), "path() found a wrong path."); |
190 | 189 |
check(pathSource(gr, p) == s, "path() found a wrong path."); |
191 | 190 |
check(pathTarget(gr, p) == t, "path() found a wrong path."); |
192 | 191 |
|
193 | 192 |
ListPath<Digraph> path; |
194 | 193 |
Value dist; |
195 | 194 |
bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t); |
196 | 195 |
|
197 | 196 |
check(reached && dist == -1, "Bellman-Ford found a wrong path."); |
198 | 197 |
check(path.length() == 3, "path() found a wrong path."); |
199 | 198 |
check(checkPath(gr, path), "path() found a wrong path."); |
200 | 199 |
check(pathSource(gr, path) == s, "path() found a wrong path."); |
201 | 200 |
check(pathTarget(gr, path) == t, "path() found a wrong path."); |
202 | 201 |
|
203 | 202 |
for(ArcIt e(gr); e!=INVALID; ++e) { |
204 | 203 |
Node u=gr.source(e); |
205 | 204 |
Node v=gr.target(e); |
206 | 205 |
check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]), |
207 | 206 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
208 | 207 |
bf.dist(v) - bf.dist(u) - length[e]); |
209 | 208 |
} |
210 | 209 |
|
211 | 210 |
for(NodeIt v(gr); v!=INVALID; ++v) { |
212 | 211 |
if (bf.reached(v)) { |
213 | 212 |
check(v==s || bf.predArc(v)!=INVALID, "Wrong tree."); |
214 | 213 |
if (bf.predArc(v)!=INVALID ) { |
215 | 214 |
Arc e=bf.predArc(v); |
216 | 215 |
Node u=gr.source(e); |
217 | 216 |
check(u==bf.predNode(v),"Wrong tree."); |
218 | 217 |
check(bf.dist(v) - bf.dist(u) == length[e], |
219 | 218 |
"Wrong distance! Difference: " << |
220 | 219 |
bf.dist(v) - bf.dist(u) - length[e]); |
221 | 220 |
} |
222 | 221 |
} |
223 | 222 |
} |
224 | 223 |
} |
225 | 224 |
|
226 | 225 |
void checkBellmanFordNegativeCycle() { |
227 | 226 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
228 | 227 |
|
229 | 228 |
SmartDigraph gr; |
230 | 229 |
IntArcMap length(gr); |
231 | 230 |
|
232 | 231 |
Node n1 = gr.addNode(); |
233 | 232 |
Node n2 = gr.addNode(); |
234 | 233 |
Node n3 = gr.addNode(); |
235 | 234 |
Node n4 = gr.addNode(); |
236 | 235 |
|
237 | 236 |
Arc a1 = gr.addArc(n1, n2); |
238 | 237 |
Arc a2 = gr.addArc(n2, n2); |
239 | 238 |
|
240 | 239 |
length[a1] = 2; |
241 | 240 |
length[a2] = -1; |
242 | 241 |
|
243 | 242 |
{ |
244 | 243 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
245 | 244 |
bf.run(n1); |
246 | 245 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
247 | 246 |
check(p.length() == 1 && p.front() == p.back() && p.front() == a2, |
248 | 247 |
"Wrong negative cycle."); |
249 | 248 |
} |
250 | 249 |
|
251 | 250 |
length[a2] = 0; |
252 | 251 |
|
253 | 252 |
{ |
254 | 253 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
255 | 254 |
bf.run(n1); |
256 | 255 |
check(bf.negativeCycle().empty(), |
257 | 256 |
"Negative cycle should not be found."); |
258 | 257 |
} |
259 | 258 |
|
260 | 259 |
length[gr.addArc(n1, n3)] = 5; |
261 | 260 |
length[gr.addArc(n4, n3)] = 1; |
262 | 261 |
length[gr.addArc(n2, n4)] = 2; |
263 | 262 |
length[gr.addArc(n3, n2)] = -4; |
264 | 263 |
|
265 | 264 |
{ |
266 | 265 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
267 | 266 |
bf.init(); |
268 | 267 |
bf.addSource(n1); |
269 | 268 |
for (int i = 0; i < 4; ++i) { |
270 | 269 |
check(bf.negativeCycle().empty(), |
271 | 270 |
"Negative cycle should not be found."); |
272 | 271 |
bf.processNextRound(); |
273 | 272 |
} |
274 | 273 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
275 | 274 |
check(p.length() == 3, "Wrong negative cycle."); |
276 | 275 |
check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1, |
277 | 276 |
"Wrong negative cycle."); |
278 | 277 |
} |
279 | 278 |
} |
280 | 279 |
|
281 | 280 |
int main() { |
282 | 281 |
checkBellmanFord<ListDigraph, int>(); |
283 | 282 |
checkBellmanFord<SmartDigraph, double>(); |
284 | 283 |
checkBellmanFordNegativeCycle(); |
285 | 284 |
return 0; |
286 | 285 |
} |
0 comments (0 inline)