0
12
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 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
#include <limits> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default OperationTraits for the BellmanFord algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations |
40 | 40 |
/// and constants that are used in the Bellman-Ford algorithm. |
41 | 41 |
/// The default implementation is based on the \c numeric_limits class. |
42 | 42 |
/// If the numeric type does not have infinity value, then the maximum |
43 | 43 |
/// value is used as extremal infinity value. |
44 | 44 |
template < |
45 | 45 |
typename V, |
46 | 46 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
47 | 47 |
struct BellmanFordDefaultOperationTraits { |
48 | 48 |
/// \e |
49 | 49 |
typedef V Value; |
50 | 50 |
/// \brief Gives back the zero value of the type. |
51 | 51 |
static Value zero() { |
52 | 52 |
return static_cast<Value>(0); |
53 | 53 |
} |
54 | 54 |
/// \brief Gives back the positive infinity value of the type. |
55 | 55 |
static Value infinity() { |
56 | 56 |
return std::numeric_limits<Value>::infinity(); |
57 | 57 |
} |
58 | 58 |
/// \brief Gives back the sum of the given two elements. |
59 | 59 |
static Value plus(const Value& left, const Value& right) { |
60 | 60 |
return left + right; |
61 | 61 |
} |
62 | 62 |
/// \brief Gives back \c true only if the first value is less than |
63 | 63 |
/// the second. |
64 | 64 |
static bool less(const Value& left, const Value& right) { |
65 | 65 |
return left < right; |
66 | 66 |
} |
67 | 67 |
}; |
68 | 68 |
|
69 | 69 |
template <typename V> |
70 | 70 |
struct BellmanFordDefaultOperationTraits<V, false> { |
71 | 71 |
typedef V Value; |
72 | 72 |
static Value zero() { |
73 | 73 |
return static_cast<Value>(0); |
74 | 74 |
} |
75 | 75 |
static Value infinity() { |
76 | 76 |
return std::numeric_limits<Value>::max(); |
77 | 77 |
} |
78 | 78 |
static Value plus(const Value& left, const Value& right) { |
79 | 79 |
if (left == infinity() || right == infinity()) return infinity(); |
80 | 80 |
return left + right; |
81 | 81 |
} |
82 | 82 |
static bool less(const Value& left, const Value& right) { |
83 | 83 |
return left < right; |
84 | 84 |
} |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
/// \brief Default traits class of BellmanFord class. |
88 | 88 |
/// |
89 | 89 |
/// Default traits class of BellmanFord class. |
90 | 90 |
/// \param GR The type of the digraph. |
91 | 91 |
/// \param LEN The type of the length map. |
92 | 92 |
template<typename GR, typename LEN> |
93 | 93 |
struct BellmanFordDefaultTraits { |
94 | 94 |
/// The type of the digraph the algorithm runs on. |
95 | 95 |
typedef GR Digraph; |
96 | 96 |
|
97 | 97 |
/// \brief The type of the map that stores the arc lengths. |
98 | 98 |
/// |
99 | 99 |
/// The type of the map that stores the arc lengths. |
100 | 100 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
101 | 101 |
typedef LEN LengthMap; |
102 | 102 |
|
103 | 103 |
/// The type of the arc lengths. |
104 | 104 |
typedef typename LEN::Value Value; |
105 | 105 |
|
106 | 106 |
/// \brief Operation traits for Bellman-Ford algorithm. |
107 | 107 |
/// |
108 | 108 |
/// It defines the used operations and the infinity value for the |
109 | 109 |
/// given \c Value type. |
110 | 110 |
/// \see BellmanFordDefaultOperationTraits |
111 | 111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
112 | 112 |
|
113 | 113 |
/// \brief The type of the map that stores the last arcs of the |
114 | 114 |
/// shortest paths. |
115 | 115 |
/// |
116 | 116 |
/// The type of the map that stores the last |
117 | 117 |
/// arcs of the shortest paths. |
118 | 118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
119 | 119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
120 | 120 |
|
121 | 121 |
/// \brief Instantiates a \c PredMap. |
122 | 122 |
/// |
123 | 123 |
/// This function instantiates a \ref PredMap. |
124 | 124 |
/// \param g is the digraph to which we would like to define the |
125 | 125 |
/// \ref PredMap. |
126 | 126 |
static PredMap *createPredMap(const GR& g) { |
127 | 127 |
return new PredMap(g); |
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
131 | 131 |
/// |
132 | 132 |
/// The type of the map that stores the distances of the nodes. |
133 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
134 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
135 | 135 |
|
136 | 136 |
/// \brief Instantiates a \c DistMap. |
137 | 137 |
/// |
138 | 138 |
/// This function instantiates a \ref DistMap. |
139 | 139 |
/// \param g is the digraph to which we would like to define the |
140 | 140 |
/// \ref DistMap. |
141 | 141 |
static DistMap *createDistMap(const GR& g) { |
142 | 142 |
return new DistMap(g); |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
/// \brief %BellmanFord algorithm class. |
148 | 148 |
/// |
149 | 149 |
/// \ingroup shortest_path |
150 | 150 |
/// This class provides an efficient implementation of the Bellman-Ford |
151 | 151 |
/// algorithm. The maximum time complexity of the algorithm is |
152 | 152 |
/// <tt>O(ne)</tt>. |
153 | 153 |
/// |
154 | 154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
155 | 155 |
/// problem when the arcs can have negative lengths, but the digraph |
156 | 156 |
/// should not contain directed cycles with negative total length. |
157 | 157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
158 | 158 |
/// algorithm instead, since it is more efficient. |
159 | 159 |
/// |
160 | 160 |
/// The arc lengths are passed to the algorithm using a |
161 | 161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
162 | 162 |
/// kind of length. The type of the length values is determined by the |
163 | 163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
164 | 164 |
/// |
165 | 165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
166 | 166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
167 | 167 |
/// it can be used easier. |
168 | 168 |
/// |
169 | 169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
/// The default type is \ref ListDigraph. |
171 | 171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
172 | 172 |
/// the lengths of the arcs. The default map type is |
173 | 173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
174 |
/// \tparam TR The traits class that defines various types used by the |
|
175 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
|
176 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
|
177 |
/// In most cases, this parameter should not be set directly, |
|
178 |
/// consider to use the named template parameters instead. |
|
174 | 179 |
#ifdef DOXYGEN |
175 | 180 |
template <typename GR, typename LEN, typename TR> |
176 | 181 |
#else |
177 | 182 |
template <typename GR=ListDigraph, |
178 | 183 |
typename LEN=typename GR::template ArcMap<int>, |
179 | 184 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
180 | 185 |
#endif |
181 | 186 |
class BellmanFord { |
182 | 187 |
public: |
183 | 188 |
|
184 | 189 |
///The type of the underlying digraph. |
185 | 190 |
typedef typename TR::Digraph Digraph; |
186 | 191 |
|
187 | 192 |
/// \brief The type of the arc lengths. |
188 | 193 |
typedef typename TR::LengthMap::Value Value; |
189 | 194 |
/// \brief The type of the map that stores the arc lengths. |
190 | 195 |
typedef typename TR::LengthMap LengthMap; |
191 | 196 |
/// \brief The type of the map that stores the last |
192 | 197 |
/// arcs of the shortest paths. |
193 | 198 |
typedef typename TR::PredMap PredMap; |
194 | 199 |
/// \brief The type of the map that stores the distances of the nodes. |
195 | 200 |
typedef typename TR::DistMap DistMap; |
196 | 201 |
/// The type of the paths. |
197 | 202 |
typedef PredMapPath<Digraph, PredMap> Path; |
198 | 203 |
///\brief The \ref BellmanFordDefaultOperationTraits |
199 | 204 |
/// "operation traits class" of the algorithm. |
200 | 205 |
typedef typename TR::OperationTraits OperationTraits; |
201 | 206 |
|
202 | 207 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
203 | 208 |
typedef TR Traits; |
204 | 209 |
|
205 | 210 |
private: |
206 | 211 |
|
207 | 212 |
typedef typename Digraph::Node Node; |
208 | 213 |
typedef typename Digraph::NodeIt NodeIt; |
209 | 214 |
typedef typename Digraph::Arc Arc; |
210 | 215 |
typedef typename Digraph::OutArcIt OutArcIt; |
211 | 216 |
|
212 | 217 |
// Pointer to the underlying digraph. |
213 | 218 |
const Digraph *_gr; |
214 | 219 |
// Pointer to the length map |
215 | 220 |
const LengthMap *_length; |
216 | 221 |
// Pointer to the map of predecessors arcs. |
217 | 222 |
PredMap *_pred; |
218 | 223 |
// Indicates if _pred is locally allocated (true) or not. |
219 | 224 |
bool _local_pred; |
220 | 225 |
// Pointer to the map of distances. |
221 | 226 |
DistMap *_dist; |
222 | 227 |
// Indicates if _dist is locally allocated (true) or not. |
223 | 228 |
bool _local_dist; |
224 | 229 |
|
225 | 230 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
226 | 231 |
MaskMap *_mask; |
227 | 232 |
|
228 | 233 |
std::vector<Node> _process; |
229 | 234 |
|
230 | 235 |
// Creates the maps if necessary. |
231 | 236 |
void create_maps() { |
232 | 237 |
if(!_pred) { |
233 | 238 |
_local_pred = true; |
234 | 239 |
_pred = Traits::createPredMap(*_gr); |
235 | 240 |
} |
236 | 241 |
if(!_dist) { |
237 | 242 |
_local_dist = true; |
238 | 243 |
_dist = Traits::createDistMap(*_gr); |
239 | 244 |
} |
240 | 245 |
if(!_mask) { |
241 | 246 |
_mask = new MaskMap(*_gr); |
242 | 247 |
} |
243 | 248 |
} |
244 | 249 |
|
245 | 250 |
public : |
246 | 251 |
|
247 | 252 |
typedef BellmanFord Create; |
248 | 253 |
|
249 | 254 |
/// \name Named Template Parameters |
250 | 255 |
|
251 | 256 |
///@{ |
252 | 257 |
|
253 | 258 |
template <class T> |
254 | 259 |
struct SetPredMapTraits : public Traits { |
255 | 260 |
typedef T PredMap; |
256 | 261 |
static PredMap *createPredMap(const Digraph&) { |
257 | 262 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
258 | 263 |
return 0; // ignore warnings |
259 | 264 |
} |
260 | 265 |
}; |
261 | 266 |
|
262 | 267 |
/// \brief \ref named-templ-param "Named parameter" for setting |
263 | 268 |
/// \c PredMap type. |
264 | 269 |
/// |
265 | 270 |
/// \ref named-templ-param "Named parameter" for setting |
266 | 271 |
/// \c PredMap type. |
267 | 272 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
268 | 273 |
template <class T> |
269 | 274 |
struct SetPredMap |
270 | 275 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
271 | 276 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
272 | 277 |
}; |
273 | 278 |
|
274 | 279 |
template <class T> |
275 | 280 |
struct SetDistMapTraits : public Traits { |
276 | 281 |
typedef T DistMap; |
277 | 282 |
static DistMap *createDistMap(const Digraph&) { |
278 | 283 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
279 | 284 |
return 0; // ignore warnings |
280 | 285 |
} |
281 | 286 |
}; |
282 | 287 |
|
283 | 288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
284 | 289 |
/// \c DistMap type. |
285 | 290 |
/// |
286 | 291 |
/// \ref named-templ-param "Named parameter" for setting |
287 | 292 |
/// \c DistMap type. |
288 | 293 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
289 | 294 |
template <class T> |
290 | 295 |
struct SetDistMap |
291 | 296 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
292 | 297 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
293 | 298 |
}; |
294 | 299 |
|
295 | 300 |
template <class T> |
296 | 301 |
struct SetOperationTraitsTraits : public Traits { |
297 | 302 |
typedef T OperationTraits; |
298 | 303 |
}; |
299 | 304 |
|
300 | 305 |
/// \brief \ref named-templ-param "Named parameter" for setting |
301 | 306 |
/// \c OperationTraits type. |
302 | 307 |
/// |
303 | 308 |
/// \ref named-templ-param "Named parameter" for setting |
304 | 309 |
/// \c OperationTraits type. |
305 | 310 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
306 | 311 |
template <class T> |
307 | 312 |
struct SetOperationTraits |
308 | 313 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
309 | 314 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
310 | 315 |
Create; |
311 | 316 |
}; |
312 | 317 |
|
313 | 318 |
///@} |
314 | 319 |
|
315 | 320 |
protected: |
316 | 321 |
|
317 | 322 |
BellmanFord() {} |
318 | 323 |
|
319 | 324 |
public: |
320 | 325 |
|
321 | 326 |
/// \brief Constructor. |
322 | 327 |
/// |
323 | 328 |
/// Constructor. |
324 | 329 |
/// \param g The digraph the algorithm runs on. |
325 | 330 |
/// \param length The length map used by the algorithm. |
326 | 331 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
327 | 332 |
_gr(&g), _length(&length), |
328 | 333 |
_pred(0), _local_pred(false), |
329 | 334 |
_dist(0), _local_dist(false), _mask(0) {} |
330 | 335 |
|
331 | 336 |
///Destructor. |
332 | 337 |
~BellmanFord() { |
333 | 338 |
if(_local_pred) delete _pred; |
334 | 339 |
if(_local_dist) delete _dist; |
335 | 340 |
if(_mask) delete _mask; |
336 | 341 |
} |
337 | 342 |
|
338 | 343 |
/// \brief Sets the length map. |
339 | 344 |
/// |
340 | 345 |
/// Sets the length map. |
341 | 346 |
/// \return <tt>(*this)</tt> |
342 | 347 |
BellmanFord &lengthMap(const LengthMap &map) { |
343 | 348 |
_length = ↦ |
344 | 349 |
return *this; |
345 | 350 |
} |
346 | 351 |
|
347 | 352 |
/// \brief Sets the map that stores the predecessor arcs. |
348 | 353 |
/// |
349 | 354 |
/// Sets the map that stores the predecessor arcs. |
350 | 355 |
/// If you don't use this function before calling \ref run() |
351 | 356 |
/// or \ref init(), an instance will be allocated automatically. |
352 | 357 |
/// The destructor deallocates this automatically allocated map, |
353 | 358 |
/// of course. |
354 | 359 |
/// \return <tt>(*this)</tt> |
355 | 360 |
BellmanFord &predMap(PredMap &map) { |
356 | 361 |
if(_local_pred) { |
357 | 362 |
delete _pred; |
358 | 363 |
_local_pred=false; |
359 | 364 |
} |
360 | 365 |
_pred = ↦ |
361 | 366 |
return *this; |
362 | 367 |
} |
363 | 368 |
|
364 | 369 |
/// \brief Sets the map that stores the distances of the nodes. |
365 | 370 |
/// |
... | ... |
@@ -744,364 +749,367 @@ |
744 | 749 |
/// \pre Either \ref run() or \ref init() must be called before |
745 | 750 |
/// using this function. |
746 | 751 |
Node predNode(Node v) const { |
747 | 752 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
748 | 753 |
} |
749 | 754 |
|
750 | 755 |
/// \brief Returns a const reference to the node map that stores the |
751 | 756 |
/// distances of the nodes. |
752 | 757 |
/// |
753 | 758 |
/// Returns a const reference to the node map that stores the distances |
754 | 759 |
/// of the nodes calculated by the algorithm. |
755 | 760 |
/// |
756 | 761 |
/// \pre Either \ref run() or \ref init() must be called before |
757 | 762 |
/// using this function. |
758 | 763 |
const DistMap &distMap() const { return *_dist;} |
759 | 764 |
|
760 | 765 |
/// \brief Returns a const reference to the node map that stores the |
761 | 766 |
/// predecessor arcs. |
762 | 767 |
/// |
763 | 768 |
/// Returns a const reference to the node map that stores the predecessor |
764 | 769 |
/// arcs, which form the shortest path tree (forest). |
765 | 770 |
/// |
766 | 771 |
/// \pre Either \ref run() or \ref init() must be called before |
767 | 772 |
/// using this function. |
768 | 773 |
const PredMap &predMap() const { return *_pred; } |
769 | 774 |
|
770 | 775 |
/// \brief Checks if a node is reached from the root(s). |
771 | 776 |
/// |
772 | 777 |
/// Returns \c true if \c v is reached from the root(s). |
773 | 778 |
/// |
774 | 779 |
/// \pre Either \ref run() or \ref init() must be called before |
775 | 780 |
/// using this function. |
776 | 781 |
bool reached(Node v) const { |
777 | 782 |
return (*_dist)[v] != OperationTraits::infinity(); |
778 | 783 |
} |
779 | 784 |
|
780 | 785 |
/// \brief Gives back a negative cycle. |
781 | 786 |
/// |
782 | 787 |
/// This function gives back a directed cycle with negative total |
783 | 788 |
/// length if the algorithm has already found one. |
784 | 789 |
/// Otherwise it gives back an empty path. |
785 | 790 |
lemon::Path<Digraph> negativeCycle() const { |
786 | 791 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
787 | 792 |
lemon::Path<Digraph> cycle; |
788 | 793 |
for (int i = 0; i < int(_process.size()); ++i) { |
789 | 794 |
if (state[_process[i]] != -1) continue; |
790 | 795 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
791 | 796 |
v = _gr->source((*_pred)[v])) { |
792 | 797 |
if (state[v] == i) { |
793 | 798 |
cycle.addFront((*_pred)[v]); |
794 | 799 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
795 | 800 |
u = _gr->source((*_pred)[u])) { |
796 | 801 |
cycle.addFront((*_pred)[u]); |
797 | 802 |
} |
798 | 803 |
return cycle; |
799 | 804 |
} |
800 | 805 |
else if (state[v] >= 0) { |
801 | 806 |
break; |
802 | 807 |
} |
803 | 808 |
state[v] = i; |
804 | 809 |
} |
805 | 810 |
} |
806 | 811 |
return cycle; |
807 | 812 |
} |
808 | 813 |
|
809 | 814 |
///@} |
810 | 815 |
}; |
811 | 816 |
|
812 | 817 |
/// \brief Default traits class of bellmanFord() function. |
813 | 818 |
/// |
814 | 819 |
/// Default traits class of bellmanFord() function. |
815 | 820 |
/// \tparam GR The type of the digraph. |
816 | 821 |
/// \tparam LEN The type of the length map. |
817 | 822 |
template <typename GR, typename LEN> |
818 | 823 |
struct BellmanFordWizardDefaultTraits { |
819 | 824 |
/// The type of the digraph the algorithm runs on. |
820 | 825 |
typedef GR Digraph; |
821 | 826 |
|
822 | 827 |
/// \brief The type of the map that stores the arc lengths. |
823 | 828 |
/// |
824 | 829 |
/// The type of the map that stores the arc lengths. |
825 | 830 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
826 | 831 |
typedef LEN LengthMap; |
827 | 832 |
|
828 | 833 |
/// The type of the arc lengths. |
829 | 834 |
typedef typename LEN::Value Value; |
830 | 835 |
|
831 | 836 |
/// \brief Operation traits for Bellman-Ford algorithm. |
832 | 837 |
/// |
833 | 838 |
/// It defines the used operations and the infinity value for the |
834 | 839 |
/// given \c Value type. |
835 | 840 |
/// \see BellmanFordDefaultOperationTraits |
836 | 841 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
837 | 842 |
|
838 | 843 |
/// \brief The type of the map that stores the last |
839 | 844 |
/// arcs of the shortest paths. |
840 | 845 |
/// |
841 | 846 |
/// The type of the map that stores the last arcs of the shortest paths. |
842 | 847 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
843 | 848 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
844 | 849 |
|
845 | 850 |
/// \brief Instantiates a \c PredMap. |
846 | 851 |
/// |
847 | 852 |
/// This function instantiates a \ref PredMap. |
848 | 853 |
/// \param g is the digraph to which we would like to define the |
849 | 854 |
/// \ref PredMap. |
850 | 855 |
static PredMap *createPredMap(const GR &g) { |
851 | 856 |
return new PredMap(g); |
852 | 857 |
} |
853 | 858 |
|
854 | 859 |
/// \brief The type of the map that stores the distances of the nodes. |
855 | 860 |
/// |
856 | 861 |
/// The type of the map that stores the distances of the nodes. |
857 | 862 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
858 | 863 |
typedef typename GR::template NodeMap<Value> DistMap; |
859 | 864 |
|
860 | 865 |
/// \brief Instantiates a \c DistMap. |
861 | 866 |
/// |
862 | 867 |
/// This function instantiates a \ref DistMap. |
863 | 868 |
/// \param g is the digraph to which we would like to define the |
864 | 869 |
/// \ref DistMap. |
865 | 870 |
static DistMap *createDistMap(const GR &g) { |
866 | 871 |
return new DistMap(g); |
867 | 872 |
} |
868 | 873 |
|
869 | 874 |
///The type of the shortest paths. |
870 | 875 |
|
871 | 876 |
///The type of the shortest paths. |
872 | 877 |
///It must meet the \ref concepts::Path "Path" concept. |
873 | 878 |
typedef lemon::Path<Digraph> Path; |
874 | 879 |
}; |
875 | 880 |
|
876 | 881 |
/// \brief Default traits class used by BellmanFordWizard. |
877 | 882 |
/// |
878 | 883 |
/// Default traits class used by BellmanFordWizard. |
879 | 884 |
/// \tparam GR The type of the digraph. |
880 | 885 |
/// \tparam LEN The type of the length map. |
881 | 886 |
template <typename GR, typename LEN> |
882 | 887 |
class BellmanFordWizardBase |
883 | 888 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
884 | 889 |
|
885 | 890 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
886 | 891 |
protected: |
887 | 892 |
// Type of the nodes in the digraph. |
888 | 893 |
typedef typename Base::Digraph::Node Node; |
889 | 894 |
|
890 | 895 |
// Pointer to the underlying digraph. |
891 | 896 |
void *_graph; |
892 | 897 |
// Pointer to the length map |
893 | 898 |
void *_length; |
894 | 899 |
// Pointer to the map of predecessors arcs. |
895 | 900 |
void *_pred; |
896 | 901 |
// Pointer to the map of distances. |
897 | 902 |
void *_dist; |
898 | 903 |
//Pointer to the shortest path to the target node. |
899 | 904 |
void *_path; |
900 | 905 |
//Pointer to the distance of the target node. |
901 | 906 |
void *_di; |
902 | 907 |
|
903 | 908 |
public: |
904 | 909 |
/// Constructor. |
905 | 910 |
|
906 | 911 |
/// This constructor does not require parameters, it initiates |
907 | 912 |
/// all of the attributes to default values \c 0. |
908 | 913 |
BellmanFordWizardBase() : |
909 | 914 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
910 | 915 |
|
911 | 916 |
/// Constructor. |
912 | 917 |
|
913 | 918 |
/// This constructor requires two parameters, |
914 | 919 |
/// others are initiated to \c 0. |
915 | 920 |
/// \param gr The digraph the algorithm runs on. |
916 | 921 |
/// \param len The length map. |
917 | 922 |
BellmanFordWizardBase(const GR& gr, |
918 | 923 |
const LEN& len) : |
919 | 924 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
920 | 925 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
921 | 926 |
_pred(0), _dist(0), _path(0), _di(0) {} |
922 | 927 |
|
923 | 928 |
}; |
924 | 929 |
|
925 | 930 |
/// \brief Auxiliary class for the function-type interface of the |
926 | 931 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
927 | 932 |
/// |
928 | 933 |
/// This auxiliary class is created to implement the |
929 | 934 |
/// \ref bellmanFord() "function-type interface" of the |
930 | 935 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
931 | 936 |
/// It does not have own \ref run() method, it uses the |
932 | 937 |
/// functions and features of the plain \ref BellmanFord. |
933 | 938 |
/// |
934 | 939 |
/// This class should only be used through the \ref bellmanFord() |
935 | 940 |
/// function, which makes it easier to use the algorithm. |
941 |
/// |
|
942 |
/// \tparam TR The traits class that defines various types used by the |
|
943 |
/// algorithm. |
|
936 | 944 |
template<class TR> |
937 | 945 |
class BellmanFordWizard : public TR { |
938 | 946 |
typedef TR Base; |
939 | 947 |
|
940 | 948 |
typedef typename TR::Digraph Digraph; |
941 | 949 |
|
942 | 950 |
typedef typename Digraph::Node Node; |
943 | 951 |
typedef typename Digraph::NodeIt NodeIt; |
944 | 952 |
typedef typename Digraph::Arc Arc; |
945 | 953 |
typedef typename Digraph::OutArcIt ArcIt; |
946 | 954 |
|
947 | 955 |
typedef typename TR::LengthMap LengthMap; |
948 | 956 |
typedef typename LengthMap::Value Value; |
949 | 957 |
typedef typename TR::PredMap PredMap; |
950 | 958 |
typedef typename TR::DistMap DistMap; |
951 | 959 |
typedef typename TR::Path Path; |
952 | 960 |
|
953 | 961 |
public: |
954 | 962 |
/// Constructor. |
955 | 963 |
BellmanFordWizard() : TR() {} |
956 | 964 |
|
957 | 965 |
/// \brief Constructor that requires parameters. |
958 | 966 |
/// |
959 | 967 |
/// Constructor that requires parameters. |
960 | 968 |
/// These parameters will be the default values for the traits class. |
961 | 969 |
/// \param gr The digraph the algorithm runs on. |
962 | 970 |
/// \param len The length map. |
963 | 971 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
964 | 972 |
: TR(gr, len) {} |
965 | 973 |
|
966 | 974 |
/// \brief Copy constructor |
967 | 975 |
BellmanFordWizard(const TR &b) : TR(b) {} |
968 | 976 |
|
969 | 977 |
~BellmanFordWizard() {} |
970 | 978 |
|
971 | 979 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
972 | 980 |
/// |
973 | 981 |
/// This method runs the Bellman-Ford algorithm from the given source |
974 | 982 |
/// node in order to compute the shortest path to each node. |
975 | 983 |
void run(Node s) { |
976 | 984 |
BellmanFord<Digraph,LengthMap,TR> |
977 | 985 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
978 | 986 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
979 | 987 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
980 | 988 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
981 | 989 |
bf.run(s); |
982 | 990 |
} |
983 | 991 |
|
984 | 992 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
985 | 993 |
/// between \c s and \c t. |
986 | 994 |
/// |
987 | 995 |
/// This method runs the Bellman-Ford algorithm from node \c s |
988 | 996 |
/// in order to compute the shortest path to node \c t. |
989 | 997 |
/// Actually, it computes the shortest path to each node, but using |
990 | 998 |
/// this function you can retrieve the distance and the shortest path |
991 | 999 |
/// for a single target node easier. |
992 | 1000 |
/// |
993 | 1001 |
/// \return \c true if \c t is reachable form \c s. |
994 | 1002 |
bool run(Node s, Node t) { |
995 | 1003 |
BellmanFord<Digraph,LengthMap,TR> |
996 | 1004 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
997 | 1005 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
998 | 1006 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
999 | 1007 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1000 | 1008 |
bf.run(s); |
1001 | 1009 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
1002 | 1010 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
1003 | 1011 |
return bf.reached(t); |
1004 | 1012 |
} |
1005 | 1013 |
|
1006 | 1014 |
template<class T> |
1007 | 1015 |
struct SetPredMapBase : public Base { |
1008 | 1016 |
typedef T PredMap; |
1009 | 1017 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1010 | 1018 |
SetPredMapBase(const TR &b) : TR(b) {} |
1011 | 1019 |
}; |
1012 | 1020 |
|
1013 | 1021 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1014 | 1022 |
/// the predecessor map. |
1015 | 1023 |
/// |
1016 | 1024 |
/// \ref named-templ-param "Named parameter" for setting |
1017 | 1025 |
/// the map that stores the predecessor arcs of the nodes. |
1018 | 1026 |
template<class T> |
1019 | 1027 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) { |
1020 | 1028 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1021 | 1029 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
1022 | 1030 |
} |
1023 | 1031 |
|
1024 | 1032 |
template<class T> |
1025 | 1033 |
struct SetDistMapBase : public Base { |
1026 | 1034 |
typedef T DistMap; |
1027 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1028 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {} |
1029 | 1037 |
}; |
1030 | 1038 |
|
1031 | 1039 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1032 | 1040 |
/// the distance map. |
1033 | 1041 |
/// |
1034 | 1042 |
/// \ref named-templ-param "Named parameter" for setting |
1035 | 1043 |
/// the map that stores the distances of the nodes calculated |
1036 | 1044 |
/// by the algorithm. |
1037 | 1045 |
template<class T> |
1038 | 1046 |
BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) { |
1039 | 1047 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1040 | 1048 |
return BellmanFordWizard<SetDistMapBase<T> >(*this); |
1041 | 1049 |
} |
1042 | 1050 |
|
1043 | 1051 |
template<class T> |
1044 | 1052 |
struct SetPathBase : public Base { |
1045 | 1053 |
typedef T Path; |
1046 | 1054 |
SetPathBase(const TR &b) : TR(b) {} |
1047 | 1055 |
}; |
1048 | 1056 |
|
1049 | 1057 |
/// \brief \ref named-func-param "Named parameter" for getting |
1050 | 1058 |
/// the shortest path to the target node. |
1051 | 1059 |
/// |
1052 | 1060 |
/// \ref named-func-param "Named parameter" for getting |
1053 | 1061 |
/// the shortest path to the target node. |
1054 | 1062 |
template<class T> |
1055 | 1063 |
BellmanFordWizard<SetPathBase<T> > path(const T &t) |
1056 | 1064 |
{ |
1057 | 1065 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1058 | 1066 |
return BellmanFordWizard<SetPathBase<T> >(*this); |
1059 | 1067 |
} |
1060 | 1068 |
|
1061 | 1069 |
/// \brief \ref named-func-param "Named parameter" for getting |
1062 | 1070 |
/// the distance of the target node. |
1063 | 1071 |
/// |
1064 | 1072 |
/// \ref named-func-param "Named parameter" for getting |
1065 | 1073 |
/// the distance of the target node. |
1066 | 1074 |
BellmanFordWizard dist(const Value &d) |
1067 | 1075 |
{ |
1068 | 1076 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1069 | 1077 |
return *this; |
1070 | 1078 |
} |
1071 | 1079 |
|
1072 | 1080 |
}; |
1073 | 1081 |
|
1074 | 1082 |
/// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
1075 | 1083 |
/// algorithm. |
1076 | 1084 |
/// |
1077 | 1085 |
/// \ingroup shortest_path |
1078 | 1086 |
/// Function type interface for the \ref BellmanFord "Bellman-Ford" |
1079 | 1087 |
/// algorithm. |
1080 | 1088 |
/// |
1081 | 1089 |
/// This function also has several \ref named-templ-func-param |
1082 | 1090 |
/// "named parameters", they are declared as the members of class |
1083 | 1091 |
/// \ref BellmanFordWizard. |
1084 | 1092 |
/// The following examples show how to use these parameters. |
1085 | 1093 |
/// \code |
1086 | 1094 |
/// // Compute shortest path from node s to each node |
1087 | 1095 |
/// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
1088 | 1096 |
/// |
1089 | 1097 |
/// // Compute shortest path from s to t |
1090 | 1098 |
/// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t); |
1091 | 1099 |
/// \endcode |
1092 | 1100 |
/// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
1093 | 1101 |
/// to the end of the parameter list. |
1094 | 1102 |
/// \sa BellmanFordWizard |
1095 | 1103 |
/// \sa BellmanFord |
1096 | 1104 |
template<typename GR, typename LEN> |
1097 | 1105 |
BellmanFordWizard<BellmanFordWizardBase<GR,LEN> > |
1098 | 1106 |
bellmanFord(const GR& digraph, |
1099 | 1107 |
const LEN& length) |
1100 | 1108 |
{ |
1101 | 1109 |
return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length); |
1102 | 1110 |
} |
1103 | 1111 |
|
1104 | 1112 |
} //END OF NAMESPACE LEMON |
1105 | 1113 |
|
1106 | 1114 |
#endif |
1107 | 1115 |
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 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS 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 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
///By default, it is a NullMap. |
67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 | 68 |
///Instantiates a \c ProcessedMap. |
69 | 69 |
|
70 | 70 |
///This function instantiates a \ref ProcessedMap. |
71 | 71 |
///\param g is the digraph, to which |
72 | 72 |
///we would like to define the \ref ProcessedMap |
73 | 73 |
#ifdef DOXYGEN |
74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
75 | 75 |
#else |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref bfs() "function-type interface" for the BFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 |
///\tparam TR The traits class that defines various types used by the |
|
125 |
///algorithm. By default, it is \ref BfsDefaultTraits |
|
126 |
///"BfsDefaultTraits<GR>". |
|
127 |
///In most cases, this parameter should not be set directly, |
|
128 |
///consider to use the named template parameters instead. |
|
124 | 129 |
#ifdef DOXYGEN |
125 | 130 |
template <typename GR, |
126 | 131 |
typename TR> |
127 | 132 |
#else |
128 | 133 |
template <typename GR=ListDigraph, |
129 | 134 |
typename TR=BfsDefaultTraits<GR> > |
130 | 135 |
#endif |
131 | 136 |
class Bfs { |
132 | 137 |
public: |
133 | 138 |
|
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename TR::Digraph Digraph; |
136 | 141 |
|
137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 143 |
///shortest paths. |
139 | 144 |
typedef typename TR::PredMap PredMap; |
140 | 145 |
///The type of the map that stores the distances of the nodes. |
141 | 146 |
typedef typename TR::DistMap DistMap; |
142 | 147 |
///The type of the map that indicates which nodes are reached. |
143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 149 |
///The type of the map that indicates which nodes are processed. |
145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 151 |
///The type of the paths. |
147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 153 |
|
149 | 154 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
150 | 155 |
typedef TR Traits; |
151 | 156 |
|
152 | 157 |
private: |
153 | 158 |
|
154 | 159 |
typedef typename Digraph::Node Node; |
155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 161 |
typedef typename Digraph::Arc Arc; |
157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 163 |
|
159 | 164 |
//Pointer to the underlying digraph. |
160 | 165 |
const Digraph *G; |
161 | 166 |
//Pointer to the map of predecessor arcs. |
162 | 167 |
PredMap *_pred; |
163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 169 |
bool local_pred; |
165 | 170 |
//Pointer to the map of distances. |
166 | 171 |
DistMap *_dist; |
167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 173 |
bool local_dist; |
169 | 174 |
//Pointer to the map of reached status of the nodes. |
170 | 175 |
ReachedMap *_reached; |
171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
172 | 177 |
bool local_reached; |
173 | 178 |
//Pointer to the map of processed status of the nodes. |
174 | 179 |
ProcessedMap *_processed; |
175 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
176 | 181 |
bool local_processed; |
177 | 182 |
|
178 | 183 |
std::vector<typename Digraph::Node> _queue; |
179 | 184 |
int _queue_head,_queue_tail,_queue_next_dist; |
180 | 185 |
int _curr_dist; |
181 | 186 |
|
182 | 187 |
//Creates the maps if necessary. |
183 | 188 |
void create_maps() |
184 | 189 |
{ |
185 | 190 |
if(!_pred) { |
186 | 191 |
local_pred = true; |
187 | 192 |
_pred = Traits::createPredMap(*G); |
188 | 193 |
} |
189 | 194 |
if(!_dist) { |
190 | 195 |
local_dist = true; |
191 | 196 |
_dist = Traits::createDistMap(*G); |
192 | 197 |
} |
193 | 198 |
if(!_reached) { |
194 | 199 |
local_reached = true; |
195 | 200 |
_reached = Traits::createReachedMap(*G); |
196 | 201 |
} |
197 | 202 |
if(!_processed) { |
198 | 203 |
local_processed = true; |
199 | 204 |
_processed = Traits::createProcessedMap(*G); |
200 | 205 |
} |
201 | 206 |
} |
202 | 207 |
|
203 | 208 |
protected: |
204 | 209 |
|
205 | 210 |
Bfs() {} |
206 | 211 |
|
207 | 212 |
public: |
208 | 213 |
|
209 | 214 |
typedef Bfs Create; |
210 | 215 |
|
211 | 216 |
///\name Named Template Parameters |
212 | 217 |
|
213 | 218 |
///@{ |
214 | 219 |
|
215 | 220 |
template <class T> |
216 | 221 |
struct SetPredMapTraits : public Traits { |
217 | 222 |
typedef T PredMap; |
218 | 223 |
static PredMap *createPredMap(const Digraph &) |
219 | 224 |
{ |
220 | 225 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
221 | 226 |
return 0; // ignore warnings |
222 | 227 |
} |
223 | 228 |
}; |
224 | 229 |
///\brief \ref named-templ-param "Named parameter" for setting |
225 | 230 |
///\c PredMap type. |
226 | 231 |
/// |
227 | 232 |
///\ref named-templ-param "Named parameter" for setting |
228 | 233 |
///\c PredMap type. |
229 | 234 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
230 | 235 |
template <class T> |
231 | 236 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
232 | 237 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
233 | 238 |
}; |
234 | 239 |
|
235 | 240 |
template <class T> |
236 | 241 |
struct SetDistMapTraits : public Traits { |
237 | 242 |
typedef T DistMap; |
238 | 243 |
static DistMap *createDistMap(const Digraph &) |
239 | 244 |
{ |
240 | 245 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
241 | 246 |
return 0; // ignore warnings |
242 | 247 |
} |
243 | 248 |
}; |
244 | 249 |
///\brief \ref named-templ-param "Named parameter" for setting |
245 | 250 |
///\c DistMap type. |
246 | 251 |
/// |
247 | 252 |
///\ref named-templ-param "Named parameter" for setting |
248 | 253 |
///\c DistMap type. |
249 | 254 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
250 | 255 |
template <class T> |
251 | 256 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
252 | 257 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
253 | 258 |
}; |
254 | 259 |
|
255 | 260 |
template <class T> |
256 | 261 |
struct SetReachedMapTraits : public Traits { |
257 | 262 |
typedef T ReachedMap; |
258 | 263 |
static ReachedMap *createReachedMap(const Digraph &) |
259 | 264 |
{ |
260 | 265 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
261 | 266 |
return 0; // ignore warnings |
262 | 267 |
} |
263 | 268 |
}; |
264 | 269 |
///\brief \ref named-templ-param "Named parameter" for setting |
265 | 270 |
///\c ReachedMap type. |
266 | 271 |
/// |
267 | 272 |
///\ref named-templ-param "Named parameter" for setting |
268 | 273 |
///\c ReachedMap type. |
269 | 274 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
270 | 275 |
template <class T> |
271 | 276 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
272 | 277 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
273 | 278 |
}; |
274 | 279 |
|
275 | 280 |
template <class T> |
276 | 281 |
struct SetProcessedMapTraits : public Traits { |
277 | 282 |
typedef T ProcessedMap; |
278 | 283 |
static ProcessedMap *createProcessedMap(const Digraph &) |
279 | 284 |
{ |
280 | 285 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
281 | 286 |
return 0; // ignore warnings |
282 | 287 |
} |
283 | 288 |
}; |
284 | 289 |
///\brief \ref named-templ-param "Named parameter" for setting |
285 | 290 |
///\c ProcessedMap type. |
286 | 291 |
/// |
287 | 292 |
///\ref named-templ-param "Named parameter" for setting |
288 | 293 |
///\c ProcessedMap type. |
289 | 294 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
290 | 295 |
template <class T> |
291 | 296 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
292 | 297 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
293 | 298 |
}; |
294 | 299 |
|
295 | 300 |
struct SetStandardProcessedMapTraits : public Traits { |
296 | 301 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
297 | 302 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
298 | 303 |
{ |
299 | 304 |
return new ProcessedMap(g); |
300 | 305 |
return 0; // ignore warnings |
301 | 306 |
} |
302 | 307 |
}; |
303 | 308 |
///\brief \ref named-templ-param "Named parameter" for setting |
304 | 309 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
305 | 310 |
/// |
306 | 311 |
///\ref named-templ-param "Named parameter" for setting |
307 | 312 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
308 | 313 |
///If you don't set it explicitly, it will be automatically allocated. |
309 | 314 |
struct SetStandardProcessedMap : |
310 | 315 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
311 | 316 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
312 | 317 |
}; |
313 | 318 |
|
314 | 319 |
///@} |
315 | 320 |
|
... | ... |
@@ -768,727 +773,730 @@ |
768 | 773 |
/// |
769 | 774 |
///\pre Either \ref run(Node) "run()" or \ref init() |
770 | 775 |
///must be called before using this function. |
771 | 776 |
Arc predArc(Node v) const { return (*_pred)[v];} |
772 | 777 |
|
773 | 778 |
///\brief Returns the 'previous node' of the shortest path tree for |
774 | 779 |
///the given node. |
775 | 780 |
/// |
776 | 781 |
///This function returns the 'previous node' of the shortest path |
777 | 782 |
///tree for the node \c v, i.e. it returns the last but one node |
778 | 783 |
///of a shortest path from a root to \c v. It is \c INVALID |
779 | 784 |
///if \c v is not reached from the root(s) or if \c v is a root. |
780 | 785 |
/// |
781 | 786 |
///The shortest path tree used here is equal to the shortest path |
782 | 787 |
///tree used in \ref predArc() and \ref predMap(). |
783 | 788 |
/// |
784 | 789 |
///\pre Either \ref run(Node) "run()" or \ref init() |
785 | 790 |
///must be called before using this function. |
786 | 791 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
787 | 792 |
G->source((*_pred)[v]); } |
788 | 793 |
|
789 | 794 |
///\brief Returns a const reference to the node map that stores the |
790 | 795 |
/// distances of the nodes. |
791 | 796 |
/// |
792 | 797 |
///Returns a const reference to the node map that stores the distances |
793 | 798 |
///of the nodes calculated by the algorithm. |
794 | 799 |
/// |
795 | 800 |
///\pre Either \ref run(Node) "run()" or \ref init() |
796 | 801 |
///must be called before using this function. |
797 | 802 |
const DistMap &distMap() const { return *_dist;} |
798 | 803 |
|
799 | 804 |
///\brief Returns a const reference to the node map that stores the |
800 | 805 |
///predecessor arcs. |
801 | 806 |
/// |
802 | 807 |
///Returns a const reference to the node map that stores the predecessor |
803 | 808 |
///arcs, which form the shortest path tree (forest). |
804 | 809 |
/// |
805 | 810 |
///\pre Either \ref run(Node) "run()" or \ref init() |
806 | 811 |
///must be called before using this function. |
807 | 812 |
const PredMap &predMap() const { return *_pred;} |
808 | 813 |
|
809 | 814 |
///Checks if the given node is reached from the root(s). |
810 | 815 |
|
811 | 816 |
///Returns \c true if \c v is reached from the root(s). |
812 | 817 |
/// |
813 | 818 |
///\pre Either \ref run(Node) "run()" or \ref init() |
814 | 819 |
///must be called before using this function. |
815 | 820 |
bool reached(Node v) const { return (*_reached)[v]; } |
816 | 821 |
|
817 | 822 |
///@} |
818 | 823 |
}; |
819 | 824 |
|
820 | 825 |
///Default traits class of bfs() function. |
821 | 826 |
|
822 | 827 |
///Default traits class of bfs() function. |
823 | 828 |
///\tparam GR Digraph type. |
824 | 829 |
template<class GR> |
825 | 830 |
struct BfsWizardDefaultTraits |
826 | 831 |
{ |
827 | 832 |
///The type of the digraph the algorithm runs on. |
828 | 833 |
typedef GR Digraph; |
829 | 834 |
|
830 | 835 |
///\brief The type of the map that stores the predecessor |
831 | 836 |
///arcs of the shortest paths. |
832 | 837 |
/// |
833 | 838 |
///The type of the map that stores the predecessor |
834 | 839 |
///arcs of the shortest paths. |
835 | 840 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
836 | 841 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
837 | 842 |
///Instantiates a PredMap. |
838 | 843 |
|
839 | 844 |
///This function instantiates a PredMap. |
840 | 845 |
///\param g is the digraph, to which we would like to define the |
841 | 846 |
///PredMap. |
842 | 847 |
static PredMap *createPredMap(const Digraph &g) |
843 | 848 |
{ |
844 | 849 |
return new PredMap(g); |
845 | 850 |
} |
846 | 851 |
|
847 | 852 |
///The type of the map that indicates which nodes are processed. |
848 | 853 |
|
849 | 854 |
///The type of the map that indicates which nodes are processed. |
850 | 855 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
851 | 856 |
///By default, it is a NullMap. |
852 | 857 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
853 | 858 |
///Instantiates a ProcessedMap. |
854 | 859 |
|
855 | 860 |
///This function instantiates a ProcessedMap. |
856 | 861 |
///\param g is the digraph, to which |
857 | 862 |
///we would like to define the ProcessedMap. |
858 | 863 |
#ifdef DOXYGEN |
859 | 864 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
860 | 865 |
#else |
861 | 866 |
static ProcessedMap *createProcessedMap(const Digraph &) |
862 | 867 |
#endif |
863 | 868 |
{ |
864 | 869 |
return new ProcessedMap(); |
865 | 870 |
} |
866 | 871 |
|
867 | 872 |
///The type of the map that indicates which nodes are reached. |
868 | 873 |
|
869 | 874 |
///The type of the map that indicates which nodes are reached. |
870 | 875 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
871 | 876 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
872 | 877 |
///Instantiates a ReachedMap. |
873 | 878 |
|
874 | 879 |
///This function instantiates a ReachedMap. |
875 | 880 |
///\param g is the digraph, to which |
876 | 881 |
///we would like to define the ReachedMap. |
877 | 882 |
static ReachedMap *createReachedMap(const Digraph &g) |
878 | 883 |
{ |
879 | 884 |
return new ReachedMap(g); |
880 | 885 |
} |
881 | 886 |
|
882 | 887 |
///The type of the map that stores the distances of the nodes. |
883 | 888 |
|
884 | 889 |
///The type of the map that stores the distances of the nodes. |
885 | 890 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
886 | 891 |
typedef typename Digraph::template NodeMap<int> DistMap; |
887 | 892 |
///Instantiates a DistMap. |
888 | 893 |
|
889 | 894 |
///This function instantiates a DistMap. |
890 | 895 |
///\param g is the digraph, to which we would like to define |
891 | 896 |
///the DistMap |
892 | 897 |
static DistMap *createDistMap(const Digraph &g) |
893 | 898 |
{ |
894 | 899 |
return new DistMap(g); |
895 | 900 |
} |
896 | 901 |
|
897 | 902 |
///The type of the shortest paths. |
898 | 903 |
|
899 | 904 |
///The type of the shortest paths. |
900 | 905 |
///It must conform to the \ref concepts::Path "Path" concept. |
901 | 906 |
typedef lemon::Path<Digraph> Path; |
902 | 907 |
}; |
903 | 908 |
|
904 | 909 |
/// Default traits class used by BfsWizard |
905 | 910 |
|
906 | 911 |
/// Default traits class used by BfsWizard. |
907 | 912 |
/// \tparam GR The type of the digraph. |
908 | 913 |
template<class GR> |
909 | 914 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
910 | 915 |
{ |
911 | 916 |
|
912 | 917 |
typedef BfsWizardDefaultTraits<GR> Base; |
913 | 918 |
protected: |
914 | 919 |
//The type of the nodes in the digraph. |
915 | 920 |
typedef typename Base::Digraph::Node Node; |
916 | 921 |
|
917 | 922 |
//Pointer to the digraph the algorithm runs on. |
918 | 923 |
void *_g; |
919 | 924 |
//Pointer to the map of reached nodes. |
920 | 925 |
void *_reached; |
921 | 926 |
//Pointer to the map of processed nodes. |
922 | 927 |
void *_processed; |
923 | 928 |
//Pointer to the map of predecessors arcs. |
924 | 929 |
void *_pred; |
925 | 930 |
//Pointer to the map of distances. |
926 | 931 |
void *_dist; |
927 | 932 |
//Pointer to the shortest path to the target node. |
928 | 933 |
void *_path; |
929 | 934 |
//Pointer to the distance of the target node. |
930 | 935 |
int *_di; |
931 | 936 |
|
932 | 937 |
public: |
933 | 938 |
/// Constructor. |
934 | 939 |
|
935 | 940 |
/// This constructor does not require parameters, it initiates |
936 | 941 |
/// all of the attributes to \c 0. |
937 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
938 | 943 |
_dist(0), _path(0), _di(0) {} |
939 | 944 |
|
940 | 945 |
/// Constructor. |
941 | 946 |
|
942 | 947 |
/// This constructor requires one parameter, |
943 | 948 |
/// others are initiated to \c 0. |
944 | 949 |
/// \param g The digraph the algorithm runs on. |
945 | 950 |
BfsWizardBase(const GR &g) : |
946 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
947 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
948 | 953 |
|
949 | 954 |
}; |
950 | 955 |
|
951 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
952 | 957 |
|
953 | 958 |
/// This auxiliary class is created to implement the |
954 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
955 | 960 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
956 | 961 |
/// functions and features of the plain \ref Bfs. |
957 | 962 |
/// |
958 | 963 |
/// This class should only be used through the \ref bfs() function, |
959 | 964 |
/// which makes it easier to use the algorithm. |
965 |
/// |
|
966 |
/// \tparam TR The traits class that defines various types used by the |
|
967 |
/// algorithm. |
|
960 | 968 |
template<class TR> |
961 | 969 |
class BfsWizard : public TR |
962 | 970 |
{ |
963 | 971 |
typedef TR Base; |
964 | 972 |
|
965 | 973 |
typedef typename TR::Digraph Digraph; |
966 | 974 |
|
967 | 975 |
typedef typename Digraph::Node Node; |
968 | 976 |
typedef typename Digraph::NodeIt NodeIt; |
969 | 977 |
typedef typename Digraph::Arc Arc; |
970 | 978 |
typedef typename Digraph::OutArcIt OutArcIt; |
971 | 979 |
|
972 | 980 |
typedef typename TR::PredMap PredMap; |
973 | 981 |
typedef typename TR::DistMap DistMap; |
974 | 982 |
typedef typename TR::ReachedMap ReachedMap; |
975 | 983 |
typedef typename TR::ProcessedMap ProcessedMap; |
976 | 984 |
typedef typename TR::Path Path; |
977 | 985 |
|
978 | 986 |
public: |
979 | 987 |
|
980 | 988 |
/// Constructor. |
981 | 989 |
BfsWizard() : TR() {} |
982 | 990 |
|
983 | 991 |
/// Constructor that requires parameters. |
984 | 992 |
|
985 | 993 |
/// Constructor that requires parameters. |
986 | 994 |
/// These parameters will be the default values for the traits class. |
987 | 995 |
/// \param g The digraph the algorithm runs on. |
988 | 996 |
BfsWizard(const Digraph &g) : |
989 | 997 |
TR(g) {} |
990 | 998 |
|
991 | 999 |
///Copy constructor |
992 | 1000 |
BfsWizard(const TR &b) : TR(b) {} |
993 | 1001 |
|
994 | 1002 |
~BfsWizard() {} |
995 | 1003 |
|
996 | 1004 |
///Runs BFS algorithm from the given source node. |
997 | 1005 |
|
998 | 1006 |
///This method runs BFS algorithm from node \c s |
999 | 1007 |
///in order to compute the shortest path to each node. |
1000 | 1008 |
void run(Node s) |
1001 | 1009 |
{ |
1002 | 1010 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1003 | 1011 |
if (Base::_pred) |
1004 | 1012 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1005 | 1013 |
if (Base::_dist) |
1006 | 1014 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1007 | 1015 |
if (Base::_reached) |
1008 | 1016 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1009 | 1017 |
if (Base::_processed) |
1010 | 1018 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1011 | 1019 |
if (s!=INVALID) |
1012 | 1020 |
alg.run(s); |
1013 | 1021 |
else |
1014 | 1022 |
alg.run(); |
1015 | 1023 |
} |
1016 | 1024 |
|
1017 | 1025 |
///Finds the shortest path between \c s and \c t. |
1018 | 1026 |
|
1019 | 1027 |
///This method runs BFS algorithm from node \c s |
1020 | 1028 |
///in order to compute the shortest path to node \c t |
1021 | 1029 |
///(it stops searching when \c t is processed). |
1022 | 1030 |
/// |
1023 | 1031 |
///\return \c true if \c t is reachable form \c s. |
1024 | 1032 |
bool run(Node s, Node t) |
1025 | 1033 |
{ |
1026 | 1034 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1027 | 1035 |
if (Base::_pred) |
1028 | 1036 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1029 | 1037 |
if (Base::_dist) |
1030 | 1038 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1031 | 1039 |
if (Base::_reached) |
1032 | 1040 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1033 | 1041 |
if (Base::_processed) |
1034 | 1042 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1035 | 1043 |
alg.run(s,t); |
1036 | 1044 |
if (Base::_path) |
1037 | 1045 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1038 | 1046 |
if (Base::_di) |
1039 | 1047 |
*Base::_di = alg.dist(t); |
1040 | 1048 |
return alg.reached(t); |
1041 | 1049 |
} |
1042 | 1050 |
|
1043 | 1051 |
///Runs BFS algorithm to visit all nodes in the digraph. |
1044 | 1052 |
|
1045 | 1053 |
///This method runs BFS algorithm in order to visit all nodes |
1046 | 1054 |
///in the digraph. |
1047 | 1055 |
void run() |
1048 | 1056 |
{ |
1049 | 1057 |
run(INVALID); |
1050 | 1058 |
} |
1051 | 1059 |
|
1052 | 1060 |
template<class T> |
1053 | 1061 |
struct SetPredMapBase : public Base { |
1054 | 1062 |
typedef T PredMap; |
1055 | 1063 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1056 | 1064 |
SetPredMapBase(const TR &b) : TR(b) {} |
1057 | 1065 |
}; |
1058 | 1066 |
|
1059 | 1067 |
///\brief \ref named-templ-param "Named parameter" for setting |
1060 | 1068 |
///the predecessor map. |
1061 | 1069 |
/// |
1062 | 1070 |
///\ref named-templ-param "Named parameter" function for setting |
1063 | 1071 |
///the map that stores the predecessor arcs of the nodes. |
1064 | 1072 |
template<class T> |
1065 | 1073 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1066 | 1074 |
{ |
1067 | 1075 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1068 | 1076 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1069 | 1077 |
} |
1070 | 1078 |
|
1071 | 1079 |
template<class T> |
1072 | 1080 |
struct SetReachedMapBase : public Base { |
1073 | 1081 |
typedef T ReachedMap; |
1074 | 1082 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1075 | 1083 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1076 | 1084 |
}; |
1077 | 1085 |
|
1078 | 1086 |
///\brief \ref named-templ-param "Named parameter" for setting |
1079 | 1087 |
///the reached map. |
1080 | 1088 |
/// |
1081 | 1089 |
///\ref named-templ-param "Named parameter" function for setting |
1082 | 1090 |
///the map that indicates which nodes are reached. |
1083 | 1091 |
template<class T> |
1084 | 1092 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1085 | 1093 |
{ |
1086 | 1094 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1087 | 1095 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1088 | 1096 |
} |
1089 | 1097 |
|
1090 | 1098 |
template<class T> |
1091 | 1099 |
struct SetDistMapBase : public Base { |
1092 | 1100 |
typedef T DistMap; |
1093 | 1101 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1094 | 1102 |
SetDistMapBase(const TR &b) : TR(b) {} |
1095 | 1103 |
}; |
1096 | 1104 |
|
1097 | 1105 |
///\brief \ref named-templ-param "Named parameter" for setting |
1098 | 1106 |
///the distance map. |
1099 | 1107 |
/// |
1100 | 1108 |
///\ref named-templ-param "Named parameter" function for setting |
1101 | 1109 |
///the map that stores the distances of the nodes calculated |
1102 | 1110 |
///by the algorithm. |
1103 | 1111 |
template<class T> |
1104 | 1112 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1105 | 1113 |
{ |
1106 | 1114 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1107 | 1115 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1108 | 1116 |
} |
1109 | 1117 |
|
1110 | 1118 |
template<class T> |
1111 | 1119 |
struct SetProcessedMapBase : public Base { |
1112 | 1120 |
typedef T ProcessedMap; |
1113 | 1121 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1114 | 1122 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1115 | 1123 |
}; |
1116 | 1124 |
|
1117 | 1125 |
///\brief \ref named-func-param "Named parameter" for setting |
1118 | 1126 |
///the processed map. |
1119 | 1127 |
/// |
1120 | 1128 |
///\ref named-templ-param "Named parameter" function for setting |
1121 | 1129 |
///the map that indicates which nodes are processed. |
1122 | 1130 |
template<class T> |
1123 | 1131 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1124 | 1132 |
{ |
1125 | 1133 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1126 | 1134 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1127 | 1135 |
} |
1128 | 1136 |
|
1129 | 1137 |
template<class T> |
1130 | 1138 |
struct SetPathBase : public Base { |
1131 | 1139 |
typedef T Path; |
1132 | 1140 |
SetPathBase(const TR &b) : TR(b) {} |
1133 | 1141 |
}; |
1134 | 1142 |
///\brief \ref named-func-param "Named parameter" |
1135 | 1143 |
///for getting the shortest path to the target node. |
1136 | 1144 |
/// |
1137 | 1145 |
///\ref named-func-param "Named parameter" |
1138 | 1146 |
///for getting the shortest path to the target node. |
1139 | 1147 |
template<class T> |
1140 | 1148 |
BfsWizard<SetPathBase<T> > path(const T &t) |
1141 | 1149 |
{ |
1142 | 1150 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1143 | 1151 |
return BfsWizard<SetPathBase<T> >(*this); |
1144 | 1152 |
} |
1145 | 1153 |
|
1146 | 1154 |
///\brief \ref named-func-param "Named parameter" |
1147 | 1155 |
///for getting the distance of the target node. |
1148 | 1156 |
/// |
1149 | 1157 |
///\ref named-func-param "Named parameter" |
1150 | 1158 |
///for getting the distance of the target node. |
1151 | 1159 |
BfsWizard dist(const int &d) |
1152 | 1160 |
{ |
1153 | 1161 |
Base::_di=const_cast<int*>(&d); |
1154 | 1162 |
return *this; |
1155 | 1163 |
} |
1156 | 1164 |
|
1157 | 1165 |
}; |
1158 | 1166 |
|
1159 | 1167 |
///Function-type interface for BFS algorithm. |
1160 | 1168 |
|
1161 | 1169 |
/// \ingroup search |
1162 | 1170 |
///Function-type interface for BFS algorithm. |
1163 | 1171 |
/// |
1164 | 1172 |
///This function also has several \ref named-func-param "named parameters", |
1165 | 1173 |
///they are declared as the members of class \ref BfsWizard. |
1166 | 1174 |
///The following examples show how to use these parameters. |
1167 | 1175 |
///\code |
1168 | 1176 |
/// // Compute shortest path from node s to each node |
1169 | 1177 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
1170 | 1178 |
/// |
1171 | 1179 |
/// // Compute shortest path from s to t |
1172 | 1180 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
1173 | 1181 |
///\endcode |
1174 | 1182 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
1175 | 1183 |
///to the end of the parameter list. |
1176 | 1184 |
///\sa BfsWizard |
1177 | 1185 |
///\sa Bfs |
1178 | 1186 |
template<class GR> |
1179 | 1187 |
BfsWizard<BfsWizardBase<GR> > |
1180 | 1188 |
bfs(const GR &digraph) |
1181 | 1189 |
{ |
1182 | 1190 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
1183 | 1191 |
} |
1184 | 1192 |
|
1185 | 1193 |
#ifdef DOXYGEN |
1186 | 1194 |
/// \brief Visitor class for BFS. |
1187 | 1195 |
/// |
1188 | 1196 |
/// This class defines the interface of the BfsVisit events, and |
1189 | 1197 |
/// it could be the base of a real visitor class. |
1190 | 1198 |
template <typename GR> |
1191 | 1199 |
struct BfsVisitor { |
1192 | 1200 |
typedef GR Digraph; |
1193 | 1201 |
typedef typename Digraph::Arc Arc; |
1194 | 1202 |
typedef typename Digraph::Node Node; |
1195 | 1203 |
/// \brief Called for the source node(s) of the BFS. |
1196 | 1204 |
/// |
1197 | 1205 |
/// This function is called for the source node(s) of the BFS. |
1198 | 1206 |
void start(const Node& node) {} |
1199 | 1207 |
/// \brief Called when a node is reached first time. |
1200 | 1208 |
/// |
1201 | 1209 |
/// This function is called when a node is reached first time. |
1202 | 1210 |
void reach(const Node& node) {} |
1203 | 1211 |
/// \brief Called when a node is processed. |
1204 | 1212 |
/// |
1205 | 1213 |
/// This function is called when a node is processed. |
1206 | 1214 |
void process(const Node& node) {} |
1207 | 1215 |
/// \brief Called when an arc reaches a new node. |
1208 | 1216 |
/// |
1209 | 1217 |
/// This function is called when the BFS finds an arc whose target node |
1210 | 1218 |
/// is not reached yet. |
1211 | 1219 |
void discover(const Arc& arc) {} |
1212 | 1220 |
/// \brief Called when an arc is examined but its target node is |
1213 | 1221 |
/// already discovered. |
1214 | 1222 |
/// |
1215 | 1223 |
/// This function is called when an arc is examined but its target node is |
1216 | 1224 |
/// already discovered. |
1217 | 1225 |
void examine(const Arc& arc) {} |
1218 | 1226 |
}; |
1219 | 1227 |
#else |
1220 | 1228 |
template <typename GR> |
1221 | 1229 |
struct BfsVisitor { |
1222 | 1230 |
typedef GR Digraph; |
1223 | 1231 |
typedef typename Digraph::Arc Arc; |
1224 | 1232 |
typedef typename Digraph::Node Node; |
1225 | 1233 |
void start(const Node&) {} |
1226 | 1234 |
void reach(const Node&) {} |
1227 | 1235 |
void process(const Node&) {} |
1228 | 1236 |
void discover(const Arc&) {} |
1229 | 1237 |
void examine(const Arc&) {} |
1230 | 1238 |
|
1231 | 1239 |
template <typename _Visitor> |
1232 | 1240 |
struct Constraints { |
1233 | 1241 |
void constraints() { |
1234 | 1242 |
Arc arc; |
1235 | 1243 |
Node node; |
1236 | 1244 |
visitor.start(node); |
1237 | 1245 |
visitor.reach(node); |
1238 | 1246 |
visitor.process(node); |
1239 | 1247 |
visitor.discover(arc); |
1240 | 1248 |
visitor.examine(arc); |
1241 | 1249 |
} |
1242 | 1250 |
_Visitor& visitor; |
1243 | 1251 |
}; |
1244 | 1252 |
}; |
1245 | 1253 |
#endif |
1246 | 1254 |
|
1247 | 1255 |
/// \brief Default traits class of BfsVisit class. |
1248 | 1256 |
/// |
1249 | 1257 |
/// Default traits class of BfsVisit class. |
1250 | 1258 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1251 | 1259 |
template<class GR> |
1252 | 1260 |
struct BfsVisitDefaultTraits { |
1253 | 1261 |
|
1254 | 1262 |
/// \brief The type of the digraph the algorithm runs on. |
1255 | 1263 |
typedef GR Digraph; |
1256 | 1264 |
|
1257 | 1265 |
/// \brief The type of the map that indicates which nodes are reached. |
1258 | 1266 |
/// |
1259 | 1267 |
/// The type of the map that indicates which nodes are reached. |
1260 | 1268 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1261 | 1269 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1262 | 1270 |
|
1263 | 1271 |
/// \brief Instantiates a ReachedMap. |
1264 | 1272 |
/// |
1265 | 1273 |
/// This function instantiates a ReachedMap. |
1266 | 1274 |
/// \param digraph is the digraph, to which |
1267 | 1275 |
/// we would like to define the ReachedMap. |
1268 | 1276 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1269 | 1277 |
return new ReachedMap(digraph); |
1270 | 1278 |
} |
1271 | 1279 |
|
1272 | 1280 |
}; |
1273 | 1281 |
|
1274 | 1282 |
/// \ingroup search |
1275 | 1283 |
/// |
1276 | 1284 |
/// \brief BFS algorithm class with visitor interface. |
1277 | 1285 |
/// |
1278 | 1286 |
/// This class provides an efficient implementation of the BFS algorithm |
1279 | 1287 |
/// with visitor interface. |
1280 | 1288 |
/// |
1281 | 1289 |
/// The BfsVisit class provides an alternative interface to the Bfs |
1282 | 1290 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1283 | 1291 |
/// the member functions of the \c Visitor class on every BFS event. |
1284 | 1292 |
/// |
1285 | 1293 |
/// This interface of the BFS algorithm should be used in special cases |
1286 | 1294 |
/// when extra actions have to be performed in connection with certain |
1287 | 1295 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1288 | 1296 |
/// instead. |
1289 | 1297 |
/// |
1290 | 1298 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1291 | 1299 |
/// The default type is \ref ListDigraph. |
1292 | 1300 |
/// The value of GR is not used directly by \ref BfsVisit, |
1293 | 1301 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
1294 | 1302 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1295 | 1303 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
1296 | 1304 |
/// does not observe the BFS events. If you want to observe the BFS |
1297 | 1305 |
/// events, you should implement your own visitor class. |
1298 |
/// \tparam TR Traits class to set various data types used by the |
|
1299 |
/// algorithm. The default traits class is |
|
1300 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
1301 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
|
1302 |
/// |
|
1306 |
/// \tparam TR The traits class that defines various types used by the |
|
1307 |
/// algorithm. By default, it is \ref BfsVisitDefaultTraits |
|
1308 |
/// "BfsVisitDefaultTraits<GR>". |
|
1309 |
/// In most cases, this parameter should not be set directly, |
|
1310 |
/// consider to use the named template parameters instead. |
|
1303 | 1311 |
#ifdef DOXYGEN |
1304 | 1312 |
template <typename GR, typename VS, typename TR> |
1305 | 1313 |
#else |
1306 | 1314 |
template <typename GR = ListDigraph, |
1307 | 1315 |
typename VS = BfsVisitor<GR>, |
1308 | 1316 |
typename TR = BfsVisitDefaultTraits<GR> > |
1309 | 1317 |
#endif |
1310 | 1318 |
class BfsVisit { |
1311 | 1319 |
public: |
1312 | 1320 |
|
1313 | 1321 |
///The traits class. |
1314 | 1322 |
typedef TR Traits; |
1315 | 1323 |
|
1316 | 1324 |
///The type of the digraph the algorithm runs on. |
1317 | 1325 |
typedef typename Traits::Digraph Digraph; |
1318 | 1326 |
|
1319 | 1327 |
///The visitor type used by the algorithm. |
1320 | 1328 |
typedef VS Visitor; |
1321 | 1329 |
|
1322 | 1330 |
///The type of the map that indicates which nodes are reached. |
1323 | 1331 |
typedef typename Traits::ReachedMap ReachedMap; |
1324 | 1332 |
|
1325 | 1333 |
private: |
1326 | 1334 |
|
1327 | 1335 |
typedef typename Digraph::Node Node; |
1328 | 1336 |
typedef typename Digraph::NodeIt NodeIt; |
1329 | 1337 |
typedef typename Digraph::Arc Arc; |
1330 | 1338 |
typedef typename Digraph::OutArcIt OutArcIt; |
1331 | 1339 |
|
1332 | 1340 |
//Pointer to the underlying digraph. |
1333 | 1341 |
const Digraph *_digraph; |
1334 | 1342 |
//Pointer to the visitor object. |
1335 | 1343 |
Visitor *_visitor; |
1336 | 1344 |
//Pointer to the map of reached status of the nodes. |
1337 | 1345 |
ReachedMap *_reached; |
1338 | 1346 |
//Indicates if _reached is locally allocated (true) or not. |
1339 | 1347 |
bool local_reached; |
1340 | 1348 |
|
1341 | 1349 |
std::vector<typename Digraph::Node> _list; |
1342 | 1350 |
int _list_front, _list_back; |
1343 | 1351 |
|
1344 | 1352 |
//Creates the maps if necessary. |
1345 | 1353 |
void create_maps() { |
1346 | 1354 |
if(!_reached) { |
1347 | 1355 |
local_reached = true; |
1348 | 1356 |
_reached = Traits::createReachedMap(*_digraph); |
1349 | 1357 |
} |
1350 | 1358 |
} |
1351 | 1359 |
|
1352 | 1360 |
protected: |
1353 | 1361 |
|
1354 | 1362 |
BfsVisit() {} |
1355 | 1363 |
|
1356 | 1364 |
public: |
1357 | 1365 |
|
1358 | 1366 |
typedef BfsVisit Create; |
1359 | 1367 |
|
1360 | 1368 |
/// \name Named Template Parameters |
1361 | 1369 |
|
1362 | 1370 |
///@{ |
1363 | 1371 |
template <class T> |
1364 | 1372 |
struct SetReachedMapTraits : public Traits { |
1365 | 1373 |
typedef T ReachedMap; |
1366 | 1374 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1367 | 1375 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1368 | 1376 |
return 0; // ignore warnings |
1369 | 1377 |
} |
1370 | 1378 |
}; |
1371 | 1379 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1372 | 1380 |
/// ReachedMap type. |
1373 | 1381 |
/// |
1374 | 1382 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1375 | 1383 |
template <class T> |
1376 | 1384 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1377 | 1385 |
SetReachedMapTraits<T> > { |
1378 | 1386 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1379 | 1387 |
}; |
1380 | 1388 |
///@} |
1381 | 1389 |
|
1382 | 1390 |
public: |
1383 | 1391 |
|
1384 | 1392 |
/// \brief Constructor. |
1385 | 1393 |
/// |
1386 | 1394 |
/// Constructor. |
1387 | 1395 |
/// |
1388 | 1396 |
/// \param digraph The digraph the algorithm runs on. |
1389 | 1397 |
/// \param visitor The visitor object of the algorithm. |
1390 | 1398 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1391 | 1399 |
: _digraph(&digraph), _visitor(&visitor), |
1392 | 1400 |
_reached(0), local_reached(false) {} |
1393 | 1401 |
|
1394 | 1402 |
/// \brief Destructor. |
1395 | 1403 |
~BfsVisit() { |
1396 | 1404 |
if(local_reached) delete _reached; |
1397 | 1405 |
} |
1398 | 1406 |
|
1399 | 1407 |
/// \brief Sets the map that indicates which nodes are reached. |
1400 | 1408 |
/// |
1401 | 1409 |
/// Sets the map that indicates which nodes are reached. |
1402 | 1410 |
/// If you don't use this function before calling \ref run(Node) "run()" |
1403 | 1411 |
/// or \ref init(), an instance will be allocated automatically. |
1404 | 1412 |
/// The destructor deallocates this automatically allocated map, |
1405 | 1413 |
/// of course. |
1406 | 1414 |
/// \return <tt> (*this) </tt> |
1407 | 1415 |
BfsVisit &reachedMap(ReachedMap &m) { |
1408 | 1416 |
if(local_reached) { |
1409 | 1417 |
delete _reached; |
1410 | 1418 |
local_reached = false; |
1411 | 1419 |
} |
1412 | 1420 |
_reached = &m; |
1413 | 1421 |
return *this; |
1414 | 1422 |
} |
1415 | 1423 |
|
1416 | 1424 |
public: |
1417 | 1425 |
|
1418 | 1426 |
/// \name Execution Control |
1419 | 1427 |
/// The simplest way to execute the BFS algorithm is to use one of the |
1420 | 1428 |
/// member functions called \ref run(Node) "run()".\n |
1421 | 1429 |
/// If you need better control on the execution, you have to call |
1422 | 1430 |
/// \ref init() first, then you can add several source nodes with |
1423 | 1431 |
/// \ref addSource(). Finally the actual path computation can be |
1424 | 1432 |
/// performed with one of the \ref start() functions. |
1425 | 1433 |
|
1426 | 1434 |
/// @{ |
1427 | 1435 |
|
1428 | 1436 |
/// \brief Initializes the internal data structures. |
1429 | 1437 |
/// |
1430 | 1438 |
/// Initializes the internal data structures. |
1431 | 1439 |
void init() { |
1432 | 1440 |
create_maps(); |
1433 | 1441 |
_list.resize(countNodes(*_digraph)); |
1434 | 1442 |
_list_front = _list_back = -1; |
1435 | 1443 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1436 | 1444 |
_reached->set(u, false); |
1437 | 1445 |
} |
1438 | 1446 |
} |
1439 | 1447 |
|
1440 | 1448 |
/// \brief Adds a new source node. |
1441 | 1449 |
/// |
1442 | 1450 |
/// Adds a new source node to the set of nodes to be processed. |
1443 | 1451 |
void addSource(Node s) { |
1444 | 1452 |
if(!(*_reached)[s]) { |
1445 | 1453 |
_reached->set(s,true); |
1446 | 1454 |
_visitor->start(s); |
1447 | 1455 |
_visitor->reach(s); |
1448 | 1456 |
_list[++_list_back] = s; |
1449 | 1457 |
} |
1450 | 1458 |
} |
1451 | 1459 |
|
1452 | 1460 |
/// \brief Processes the next node. |
1453 | 1461 |
/// |
1454 | 1462 |
/// Processes the next node. |
1455 | 1463 |
/// |
1456 | 1464 |
/// \return The processed node. |
1457 | 1465 |
/// |
1458 | 1466 |
/// \pre The queue must not be empty. |
1459 | 1467 |
Node processNextNode() { |
1460 | 1468 |
Node n = _list[++_list_front]; |
1461 | 1469 |
_visitor->process(n); |
1462 | 1470 |
Arc e; |
1463 | 1471 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1464 | 1472 |
Node m = _digraph->target(e); |
1465 | 1473 |
if (!(*_reached)[m]) { |
1466 | 1474 |
_visitor->discover(e); |
1467 | 1475 |
_visitor->reach(m); |
1468 | 1476 |
_reached->set(m, true); |
1469 | 1477 |
_list[++_list_back] = m; |
1470 | 1478 |
} else { |
1471 | 1479 |
_visitor->examine(e); |
1472 | 1480 |
} |
1473 | 1481 |
} |
1474 | 1482 |
return n; |
1475 | 1483 |
} |
1476 | 1484 |
|
1477 | 1485 |
/// \brief Processes the next node. |
1478 | 1486 |
/// |
1479 | 1487 |
/// Processes the next node and checks if the given target node |
1480 | 1488 |
/// is reached. If the target node is reachable from the processed |
1481 | 1489 |
/// node, then the \c reach parameter will be set to \c true. |
1482 | 1490 |
/// |
1483 | 1491 |
/// \param target The target node. |
1484 | 1492 |
/// \retval reach Indicates if the target node is reached. |
1485 | 1493 |
/// It should be initially \c false. |
1486 | 1494 |
/// |
1487 | 1495 |
/// \return The processed node. |
1488 | 1496 |
/// |
1489 | 1497 |
/// \pre The queue must not be empty. |
1490 | 1498 |
Node processNextNode(Node target, bool& reach) { |
1491 | 1499 |
Node n = _list[++_list_front]; |
1492 | 1500 |
_visitor->process(n); |
1493 | 1501 |
Arc e; |
1494 | 1502 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
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_CAPACITY_SCALING_H |
20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/bin_heap.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
35 | 35 |
/// |
36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
37 | 37 |
/// \tparam GR Digraph type. |
38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
39 | 39 |
/// and supply values. By default it is \c int. |
40 | 40 |
/// \tparam C The number type used for costs and potentials. |
41 | 41 |
/// By default it is the same as \c V. |
42 | 42 |
template <typename GR, typename V = int, typename C = V> |
43 | 43 |
struct CapacityScalingDefaultTraits |
44 | 44 |
{ |
45 | 45 |
/// The type of the digraph |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
48 | 48 |
typedef V Value; |
49 | 49 |
/// The type of the arc costs |
50 | 50 |
typedef C Cost; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
53 | 53 |
/// |
54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// \addtogroup min_cost_flow_algs |
62 | 62 |
/// @{ |
63 | 63 |
|
64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
66 | 66 |
/// |
67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
68 | 68 |
/// of the successive shortest path algorithm for finding a |
69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
71 | 71 |
/// solution method. |
72 | 72 |
/// |
73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
74 | 74 |
/// can be given using separate functions, and the algorithm can be |
75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
76 | 76 |
/// specified, then default values will be used. |
77 | 77 |
/// |
78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
80 |
/// and supply values in the algorithm. By default it is \c int. |
|
80 |
/// and supply values in the algorithm. By default, it is \c int. |
|
81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
82 |
/// algorithm. By default it is the same as \c V. |
|
82 |
/// algorithm. By default, it is the same as \c V. |
|
83 |
/// \tparam TR The traits class that defines various types used by the |
|
84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
|
85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
|
86 |
/// In most cases, this parameter should not be set directly, |
|
87 |
/// consider to use the named template parameters instead. |
|
83 | 88 |
/// |
84 | 89 |
/// \warning Both number types must be signed and all input data must |
85 | 90 |
/// be integer. |
86 | 91 |
/// \warning This algorithm does not support negative costs for such |
87 | 92 |
/// arcs that have infinite upper bound. |
88 | 93 |
#ifdef DOXYGEN |
89 | 94 |
template <typename GR, typename V, typename C, typename TR> |
90 | 95 |
#else |
91 | 96 |
template < typename GR, typename V = int, typename C = V, |
92 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
93 | 98 |
#endif |
94 | 99 |
class CapacityScaling |
95 | 100 |
{ |
96 | 101 |
public: |
97 | 102 |
|
98 | 103 |
/// The type of the digraph |
99 | 104 |
typedef typename TR::Digraph Digraph; |
100 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
101 | 106 |
typedef typename TR::Value Value; |
102 | 107 |
/// The type of the arc costs |
103 | 108 |
typedef typename TR::Cost Cost; |
104 | 109 |
|
105 | 110 |
/// The type of the heap used for internal Dijkstra computations |
106 | 111 |
typedef typename TR::Heap Heap; |
107 | 112 |
|
108 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
109 | 114 |
typedef TR Traits; |
110 | 115 |
|
111 | 116 |
public: |
112 | 117 |
|
113 | 118 |
/// \brief Problem type constants for the \c run() function. |
114 | 119 |
/// |
115 | 120 |
/// Enum type containing the problem type constants that can be |
116 | 121 |
/// returned by the \ref run() function of the algorithm. |
117 | 122 |
enum ProblemType { |
118 | 123 |
/// The problem has no feasible solution (flow). |
119 | 124 |
INFEASIBLE, |
120 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
121 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
122 | 127 |
/// potentials (primal and dual solutions). |
123 | 128 |
OPTIMAL, |
124 | 129 |
/// The digraph contains an arc of negative cost and infinite |
125 | 130 |
/// upper bound. It means that the objective function is unbounded |
126 | 131 |
/// on that arc, however, note that it could actually be bounded |
127 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
128 | 133 |
/// these cases. |
129 | 134 |
UNBOUNDED |
130 | 135 |
}; |
131 | 136 |
|
132 | 137 |
private: |
133 | 138 |
|
134 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
135 | 140 |
|
136 | 141 |
typedef std::vector<int> IntVector; |
137 | 142 |
typedef std::vector<char> BoolVector; |
138 | 143 |
typedef std::vector<Value> ValueVector; |
139 | 144 |
typedef std::vector<Cost> CostVector; |
140 | 145 |
|
141 | 146 |
private: |
142 | 147 |
|
143 | 148 |
// Data related to the underlying digraph |
144 | 149 |
const GR &_graph; |
145 | 150 |
int _node_num; |
146 | 151 |
int _arc_num; |
147 | 152 |
int _res_arc_num; |
148 | 153 |
int _root; |
149 | 154 |
|
150 | 155 |
// Parameters of the problem |
151 | 156 |
bool _have_lower; |
152 | 157 |
Value _sum_supply; |
153 | 158 |
|
154 | 159 |
// Data structures for storing the digraph |
155 | 160 |
IntNodeMap _node_id; |
156 | 161 |
IntArcMap _arc_idf; |
157 | 162 |
IntArcMap _arc_idb; |
158 | 163 |
IntVector _first_out; |
159 | 164 |
BoolVector _forward; |
160 | 165 |
IntVector _source; |
161 | 166 |
IntVector _target; |
162 | 167 |
IntVector _reverse; |
163 | 168 |
|
164 | 169 |
// Node and arc data |
165 | 170 |
ValueVector _lower; |
166 | 171 |
ValueVector _upper; |
167 | 172 |
CostVector _cost; |
168 | 173 |
ValueVector _supply; |
169 | 174 |
|
170 | 175 |
ValueVector _res_cap; |
171 | 176 |
CostVector _pi; |
172 | 177 |
ValueVector _excess; |
173 | 178 |
IntVector _excess_nodes; |
174 | 179 |
IntVector _deficit_nodes; |
175 | 180 |
|
176 | 181 |
Value _delta; |
177 | 182 |
int _factor; |
178 | 183 |
IntVector _pred; |
179 | 184 |
|
180 | 185 |
public: |
181 | 186 |
|
182 | 187 |
/// \brief Constant for infinite upper bounds (capacities). |
183 | 188 |
/// |
184 | 189 |
/// Constant for infinite upper bounds (capacities). |
185 | 190 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
186 | 191 |
/// \c std::numeric_limits<Value>::max() otherwise. |
187 | 192 |
const Value INF; |
188 | 193 |
|
189 | 194 |
private: |
190 | 195 |
|
191 | 196 |
// Special implementation of the Dijkstra algorithm for finding |
192 | 197 |
// shortest paths in the residual network of the digraph with |
193 | 198 |
// respect to the reduced arc costs and modifying the node |
194 | 199 |
// potentials according to the found distance labels. |
195 | 200 |
class ResidualDijkstra |
196 | 201 |
{ |
197 | 202 |
private: |
198 | 203 |
|
199 | 204 |
int _node_num; |
200 | 205 |
bool _geq; |
201 | 206 |
const IntVector &_first_out; |
202 | 207 |
const IntVector &_target; |
203 | 208 |
const CostVector &_cost; |
204 | 209 |
const ValueVector &_res_cap; |
205 | 210 |
const ValueVector &_excess; |
206 | 211 |
CostVector &_pi; |
207 | 212 |
IntVector &_pred; |
208 | 213 |
|
209 | 214 |
IntVector _proc_nodes; |
210 | 215 |
CostVector _dist; |
211 | 216 |
|
212 | 217 |
public: |
213 | 218 |
|
214 | 219 |
ResidualDijkstra(CapacityScaling& cs) : |
215 | 220 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
216 | 221 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
217 | 222 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
218 | 223 |
_pred(cs._pred), _dist(cs._node_num) |
219 | 224 |
{} |
220 | 225 |
|
221 | 226 |
int run(int s, Value delta = 1) { |
222 | 227 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
223 | 228 |
Heap heap(heap_cross_ref); |
224 | 229 |
heap.push(s, 0); |
225 | 230 |
_pred[s] = -1; |
226 | 231 |
_proc_nodes.clear(); |
227 | 232 |
|
228 | 233 |
// Process nodes |
229 | 234 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
230 | 235 |
int u = heap.top(), v; |
231 | 236 |
Cost d = heap.prio() + _pi[u], dn; |
232 | 237 |
_dist[u] = heap.prio(); |
233 | 238 |
_proc_nodes.push_back(u); |
234 | 239 |
heap.pop(); |
235 | 240 |
|
236 | 241 |
// Traverse outgoing residual arcs |
237 | 242 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
238 | 243 |
for (int a = _first_out[u]; a != last_out; ++a) { |
239 | 244 |
if (_res_cap[a] < delta) continue; |
240 | 245 |
v = _target[a]; |
241 | 246 |
switch (heap.state(v)) { |
242 | 247 |
case Heap::PRE_HEAP: |
243 | 248 |
heap.push(v, d + _cost[a] - _pi[v]); |
244 | 249 |
_pred[v] = a; |
245 | 250 |
break; |
246 | 251 |
case Heap::IN_HEAP: |
247 | 252 |
dn = d + _cost[a] - _pi[v]; |
248 | 253 |
if (dn < heap[v]) { |
249 | 254 |
heap.decrease(v, dn); |
250 | 255 |
_pred[v] = a; |
251 | 256 |
} |
252 | 257 |
break; |
253 | 258 |
case Heap::POST_HEAP: |
254 | 259 |
break; |
255 | 260 |
} |
256 | 261 |
} |
257 | 262 |
} |
258 | 263 |
if (heap.empty()) return -1; |
259 | 264 |
|
260 | 265 |
// Update potentials of processed nodes |
261 | 266 |
int t = heap.top(); |
262 | 267 |
Cost dt = heap.prio(); |
263 | 268 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
264 | 269 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
265 | 270 |
} |
266 | 271 |
|
267 | 272 |
return t; |
268 | 273 |
} |
269 | 274 |
|
270 | 275 |
}; //class ResidualDijkstra |
271 | 276 |
|
272 | 277 |
public: |
273 | 278 |
|
274 | 279 |
/// \name Named Template Parameters |
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 |
#ifndef LEMON_CIRCULATION_H |
20 | 20 |
#define LEMON_CIRCULATION_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
#include <limits> |
25 | 25 |
|
26 | 26 |
///\ingroup max_flow |
27 | 27 |
///\file |
28 | 28 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
29 | 29 |
/// |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
/// \brief Default traits class of Circulation class. |
33 | 33 |
/// |
34 | 34 |
/// Default traits class of Circulation class. |
35 | 35 |
/// |
36 | 36 |
/// \tparam GR Type of the digraph the algorithm runs on. |
37 | 37 |
/// \tparam LM The type of the lower bound map. |
38 | 38 |
/// \tparam UM The type of the upper bound (capacity) map. |
39 | 39 |
/// \tparam SM The type of the supply map. |
40 | 40 |
template <typename GR, typename LM, |
41 | 41 |
typename UM, typename SM> |
42 | 42 |
struct CirculationDefaultTraits { |
43 | 43 |
|
44 | 44 |
/// \brief The type of the digraph the algorithm runs on. |
45 | 45 |
typedef GR Digraph; |
46 | 46 |
|
47 | 47 |
/// \brief The type of the lower bound map. |
48 | 48 |
/// |
49 | 49 |
/// The type of the map that stores the lower bounds on the arcs. |
50 | 50 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
51 | 51 |
typedef LM LowerMap; |
52 | 52 |
|
53 | 53 |
/// \brief The type of the upper bound (capacity) map. |
54 | 54 |
/// |
55 | 55 |
/// The type of the map that stores the upper bounds (capacities) |
56 | 56 |
/// on the arcs. |
57 | 57 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
58 | 58 |
typedef UM UpperMap; |
59 | 59 |
|
60 | 60 |
/// \brief The type of supply map. |
61 | 61 |
/// |
62 | 62 |
/// The type of the map that stores the signed supply values of the |
63 | 63 |
/// nodes. |
64 | 64 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
65 | 65 |
typedef SM SupplyMap; |
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow and supply values. |
68 | 68 |
typedef typename SupplyMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
74 | 74 |
/// concept. |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef GR::ArcMap<Value> FlowMap; |
77 | 77 |
#else |
78 | 78 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates a FlowMap. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates a \ref FlowMap. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the flow map. |
86 | 86 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
87 | 87 |
return new FlowMap(digraph); |
88 | 88 |
} |
89 | 89 |
|
90 | 90 |
/// \brief The elevator type used by the algorithm. |
91 | 91 |
/// |
92 | 92 |
/// The elevator type used by the algorithm. |
93 | 93 |
/// |
94 | 94 |
/// \sa Elevator, LinkedElevator |
95 | 95 |
#ifdef DOXYGEN |
96 | 96 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
97 | 97 |
#else |
98 | 98 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
99 | 99 |
#endif |
100 | 100 |
|
101 | 101 |
/// \brief Instantiates an Elevator. |
102 | 102 |
/// |
103 | 103 |
/// This function instantiates an \ref Elevator. |
104 | 104 |
/// \param digraph The digraph for which we would like to define |
105 | 105 |
/// the elevator. |
106 | 106 |
/// \param max_level The maximum level of the elevator. |
107 | 107 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
108 | 108 |
return new Elevator(digraph, max_level); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/// \brief The tolerance used by the algorithm |
112 | 112 |
/// |
113 | 113 |
/// The tolerance used by the algorithm to handle inexact computation. |
114 | 114 |
typedef lemon::Tolerance<Value> Tolerance; |
115 | 115 |
|
116 | 116 |
}; |
117 | 117 |
|
118 | 118 |
/** |
119 | 119 |
\brief Push-relabel algorithm for the network circulation problem. |
120 | 120 |
|
121 | 121 |
\ingroup max_flow |
122 | 122 |
This class implements a push-relabel algorithm for the \e network |
123 | 123 |
\e circulation problem. |
124 | 124 |
It is to find a feasible circulation when lower and upper bounds |
125 | 125 |
are given for the flow values on the arcs and lower bounds are |
126 | 126 |
given for the difference between the outgoing and incoming flow |
127 | 127 |
at the nodes. |
128 | 128 |
|
129 | 129 |
The exact formulation of this problem is the following. |
130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$ |
131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and |
132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$ |
134 | 134 |
denotes the signed supply values of the nodes. |
135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
137 | 137 |
\f$-sup(u)\f$ demand. |
138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$ |
139 | 139 |
solution of the following problem. |
140 | 140 |
|
141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) |
142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
144 | 144 |
|
145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
148 | 148 |
It means that the total demand must be greater or equal to the total |
149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
150 | 150 |
but there could be demands that are not satisfied. |
151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
153 | 153 |
have to be satisfied and all supplies have to be used. |
154 | 154 |
|
155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
157 | 157 |
have to be satisfied while there could be supplies that are not used), |
158 | 158 |
then you could easily transform the problem to the above form by reversing |
159 | 159 |
the direction of the arcs and taking the negative of the supply values |
160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
161 | 161 |
|
162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
164 | 164 |
cannot exist. |
165 | 165 |
|
166 | 166 |
Note that this algorithm also provides a feasible solution for the |
167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
168 | 168 |
|
169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
\tparam LM The type of the lower bound map. The default |
171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
173 | 173 |
The default map type is \c LM. |
174 | 174 |
\tparam SM The type of the supply map. The default map type is |
175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
176 |
\tparam TR The traits class that defines various types used by the |
|
177 |
algorithm. By default, it is \ref CirculationDefaultTraits |
|
178 |
"CirculationDefaultTraits<GR, LM, UM, SM>". |
|
179 |
In most cases, this parameter should not be set directly, |
|
180 |
consider to use the named template parameters instead. |
|
176 | 181 |
*/ |
177 | 182 |
#ifdef DOXYGEN |
178 | 183 |
template< typename GR, |
179 | 184 |
typename LM, |
180 | 185 |
typename UM, |
181 | 186 |
typename SM, |
182 | 187 |
typename TR > |
183 | 188 |
#else |
184 | 189 |
template< typename GR, |
185 | 190 |
typename LM = typename GR::template ArcMap<int>, |
186 | 191 |
typename UM = LM, |
187 | 192 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
188 | 193 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
189 | 194 |
#endif |
190 | 195 |
class Circulation { |
191 | 196 |
public: |
192 | 197 |
|
193 | 198 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
194 | 199 |
typedef TR Traits; |
195 | 200 |
///The type of the digraph the algorithm runs on. |
196 | 201 |
typedef typename Traits::Digraph Digraph; |
197 | 202 |
///The type of the flow and supply values. |
198 | 203 |
typedef typename Traits::Value Value; |
199 | 204 |
|
200 | 205 |
///The type of the lower bound map. |
201 | 206 |
typedef typename Traits::LowerMap LowerMap; |
202 | 207 |
///The type of the upper bound (capacity) map. |
203 | 208 |
typedef typename Traits::UpperMap UpperMap; |
204 | 209 |
///The type of the supply map. |
205 | 210 |
typedef typename Traits::SupplyMap SupplyMap; |
206 | 211 |
///The type of the flow map. |
207 | 212 |
typedef typename Traits::FlowMap FlowMap; |
208 | 213 |
|
209 | 214 |
///The type of the elevator. |
210 | 215 |
typedef typename Traits::Elevator Elevator; |
211 | 216 |
///The type of the tolerance. |
212 | 217 |
typedef typename Traits::Tolerance Tolerance; |
213 | 218 |
|
214 | 219 |
private: |
215 | 220 |
|
216 | 221 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
217 | 222 |
|
218 | 223 |
const Digraph &_g; |
219 | 224 |
int _node_num; |
220 | 225 |
|
221 | 226 |
const LowerMap *_lo; |
222 | 227 |
const UpperMap *_up; |
223 | 228 |
const SupplyMap *_supply; |
224 | 229 |
|
225 | 230 |
FlowMap *_flow; |
226 | 231 |
bool _local_flow; |
227 | 232 |
|
228 | 233 |
Elevator* _level; |
229 | 234 |
bool _local_level; |
230 | 235 |
|
231 | 236 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
232 | 237 |
ExcessMap* _excess; |
233 | 238 |
|
234 | 239 |
Tolerance _tol; |
235 | 240 |
int _el; |
236 | 241 |
|
237 | 242 |
public: |
238 | 243 |
|
239 | 244 |
typedef Circulation Create; |
240 | 245 |
|
241 | 246 |
///\name Named Template Parameters |
242 | 247 |
|
243 | 248 |
///@{ |
244 | 249 |
|
245 | 250 |
template <typename T> |
246 | 251 |
struct SetFlowMapTraits : public Traits { |
247 | 252 |
typedef T FlowMap; |
248 | 253 |
static FlowMap *createFlowMap(const Digraph&) { |
249 | 254 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
250 | 255 |
return 0; // ignore warnings |
251 | 256 |
} |
252 | 257 |
}; |
253 | 258 |
|
254 | 259 |
/// \brief \ref named-templ-param "Named parameter" for setting |
255 | 260 |
/// FlowMap type |
256 | 261 |
/// |
257 | 262 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
258 | 263 |
/// type. |
259 | 264 |
template <typename T> |
260 | 265 |
struct SetFlowMap |
261 | 266 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
262 | 267 |
SetFlowMapTraits<T> > { |
263 | 268 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
264 | 269 |
SetFlowMapTraits<T> > Create; |
265 | 270 |
}; |
266 | 271 |
|
267 | 272 |
template <typename T> |
268 | 273 |
struct SetElevatorTraits : public Traits { |
269 | 274 |
typedef T Elevator; |
270 | 275 |
static Elevator *createElevator(const Digraph&, int) { |
271 | 276 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
272 | 277 |
return 0; // ignore warnings |
273 | 278 |
} |
274 | 279 |
}; |
275 | 280 |
|
276 | 281 |
/// \brief \ref named-templ-param "Named parameter" for setting |
277 | 282 |
/// Elevator type |
278 | 283 |
/// |
279 | 284 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
280 | 285 |
/// type. If this named parameter is used, then an external |
281 | 286 |
/// elevator object must be passed to the algorithm using the |
282 | 287 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
283 | 288 |
/// \ref run() or \ref init(). |
284 | 289 |
/// \sa SetStandardElevator |
285 | 290 |
template <typename T> |
286 | 291 |
struct SetElevator |
287 | 292 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
288 | 293 |
SetElevatorTraits<T> > { |
289 | 294 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
290 | 295 |
SetElevatorTraits<T> > Create; |
291 | 296 |
}; |
292 | 297 |
|
293 | 298 |
template <typename T> |
294 | 299 |
struct SetStandardElevatorTraits : public Traits { |
295 | 300 |
typedef T Elevator; |
296 | 301 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
297 | 302 |
return new Elevator(digraph, max_level); |
298 | 303 |
} |
299 | 304 |
}; |
300 | 305 |
|
301 | 306 |
/// \brief \ref named-templ-param "Named parameter" for setting |
302 | 307 |
/// Elevator type with automatic allocation |
303 | 308 |
/// |
304 | 309 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
305 | 310 |
/// type with automatic allocation. |
306 | 311 |
/// The Elevator should have standard constructor interface to be |
307 | 312 |
/// able to automatically created by the algorithm (i.e. the |
308 | 313 |
/// digraph and the maximum level should be passed to it). |
309 | 314 |
/// However, an external elevator object could also be passed to the |
310 | 315 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
311 | 316 |
/// before calling \ref run() or \ref init(). |
312 | 317 |
/// \sa SetElevator |
313 | 318 |
template <typename T> |
314 | 319 |
struct SetStandardElevator |
315 | 320 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
316 | 321 |
SetStandardElevatorTraits<T> > { |
317 | 322 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
318 | 323 |
SetStandardElevatorTraits<T> > Create; |
319 | 324 |
}; |
320 | 325 |
|
321 | 326 |
/// @} |
322 | 327 |
|
323 | 328 |
protected: |
324 | 329 |
|
325 | 330 |
Circulation() {} |
326 | 331 |
|
327 | 332 |
public: |
328 | 333 |
|
329 | 334 |
/// Constructor. |
330 | 335 |
|
331 | 336 |
/// The constructor of the class. |
332 | 337 |
/// |
333 | 338 |
/// \param graph The digraph the algorithm runs on. |
334 | 339 |
/// \param lower The lower bounds for the flow values on the arcs. |
335 | 340 |
/// \param upper The upper bounds (capacities) for the flow values |
336 | 341 |
/// on the arcs. |
337 | 342 |
/// \param supply The signed supply values of the nodes. |
338 | 343 |
Circulation(const Digraph &graph, const LowerMap &lower, |
339 | 344 |
const UpperMap &upper, const SupplyMap &supply) |
340 | 345 |
: _g(graph), _lo(&lower), _up(&upper), _supply(&supply), |
341 | 346 |
_flow(NULL), _local_flow(false), _level(NULL), _local_level(false), |
342 | 347 |
_excess(NULL) {} |
343 | 348 |
|
344 | 349 |
/// Destructor. |
345 | 350 |
~Circulation() { |
346 | 351 |
destroyStructures(); |
347 | 352 |
} |
348 | 353 |
|
349 | 354 |
|
350 | 355 |
private: |
351 | 356 |
|
352 | 357 |
bool checkBoundMaps() { |
353 | 358 |
for (ArcIt e(_g);e!=INVALID;++e) { |
354 | 359 |
if (_tol.less((*_up)[e], (*_lo)[e])) return false; |
355 | 360 |
} |
356 | 361 |
return true; |
357 | 362 |
} |
358 | 363 |
|
359 | 364 |
void createStructures() { |
360 | 365 |
_node_num = _el = countNodes(_g); |
361 | 366 |
|
362 | 367 |
if (!_flow) { |
363 | 368 |
_flow = Traits::createFlowMap(_g); |
364 | 369 |
_local_flow = true; |
365 | 370 |
} |
366 | 371 |
if (!_level) { |
367 | 372 |
_level = Traits::createElevator(_g, _node_num); |
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_COST_SCALING_H |
20 | 20 |
#define LEMON_COST_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <deque> |
28 | 28 |
#include <limits> |
29 | 29 |
|
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/circulation.h> |
35 | 35 |
#include <lemon/bellman_ford.h> |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
40 | 40 |
/// |
41 | 41 |
/// Default traits class of CostScaling algorithm. |
42 | 42 |
/// \tparam GR Digraph type. |
43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
44 | 44 |
/// and supply values. By default it is \c int. |
45 | 45 |
/// \tparam C The number type used for costs and potentials. |
46 | 46 |
/// By default it is the same as \c V. |
47 | 47 |
#ifdef DOXYGEN |
48 | 48 |
template <typename GR, typename V = int, typename C = V> |
49 | 49 |
#else |
50 | 50 |
template < typename GR, typename V = int, typename C = V, |
51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
52 | 52 |
#endif |
53 | 53 |
struct CostScalingDefaultTraits |
54 | 54 |
{ |
55 | 55 |
/// The type of the digraph |
56 | 56 |
typedef GR Digraph; |
57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
58 | 58 |
typedef V Value; |
59 | 59 |
/// The type of the arc costs |
60 | 60 |
typedef C Cost; |
61 | 61 |
|
62 | 62 |
/// \brief The large cost type used for internal computations |
63 | 63 |
/// |
64 | 64 |
/// The large cost type used for internal computations. |
65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
66 | 66 |
/// otherwise it is \c double. |
67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
68 | 68 |
typedef double LargeCost; |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
// Default traits class for integer cost types |
72 | 72 |
template <typename GR, typename V, typename C> |
73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
74 | 74 |
{ |
75 | 75 |
typedef GR Digraph; |
76 | 76 |
typedef V Value; |
77 | 77 |
typedef C Cost; |
78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
79 | 79 |
typedef long long LargeCost; |
80 | 80 |
#else |
81 | 81 |
typedef long LargeCost; |
82 | 82 |
#endif |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// \addtogroup min_cost_flow_algs |
87 | 87 |
/// @{ |
88 | 88 |
|
89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
91 | 91 |
/// |
92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
99 | 99 |
/// |
100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
101 | 101 |
/// can be given using separate functions, and the algorithm can be |
102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
103 | 103 |
/// specified, then default values will be used. |
104 | 104 |
/// |
105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
107 |
/// and supply values in the algorithm. By default it is \c int. |
|
107 |
/// and supply values in the algorithm. By default, it is \c int. |
|
108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
109 |
/// algorithm. By default it is the same as \c V. |
|
109 |
/// algorithm. By default, it is the same as \c V. |
|
110 |
/// \tparam TR The traits class that defines various types used by the |
|
111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
|
112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
|
113 |
/// In most cases, this parameter should not be set directly, |
|
114 |
/// consider to use the named template parameters instead. |
|
110 | 115 |
/// |
111 | 116 |
/// \warning Both number types must be signed and all input data must |
112 | 117 |
/// be integer. |
113 | 118 |
/// \warning This algorithm does not support negative costs for such |
114 | 119 |
/// arcs that have infinite upper bound. |
115 | 120 |
/// |
116 | 121 |
/// \note %CostScaling provides three different internal methods, |
117 | 122 |
/// from which the most efficient one is used by default. |
118 | 123 |
/// For more information, see \ref Method. |
119 | 124 |
#ifdef DOXYGEN |
120 | 125 |
template <typename GR, typename V, typename C, typename TR> |
121 | 126 |
#else |
122 | 127 |
template < typename GR, typename V = int, typename C = V, |
123 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
124 | 129 |
#endif |
125 | 130 |
class CostScaling |
126 | 131 |
{ |
127 | 132 |
public: |
128 | 133 |
|
129 | 134 |
/// The type of the digraph |
130 | 135 |
typedef typename TR::Digraph Digraph; |
131 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
132 | 137 |
typedef typename TR::Value Value; |
133 | 138 |
/// The type of the arc costs |
134 | 139 |
typedef typename TR::Cost Cost; |
135 | 140 |
|
136 | 141 |
/// \brief The large cost type |
137 | 142 |
/// |
138 | 143 |
/// The large cost type used for internal computations. |
139 |
/// Using the \ref CostScalingDefaultTraits "default traits class", |
|
140 |
/// it is \c long \c long if the \c Cost type is integer, |
|
144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
|
141 | 145 |
/// otherwise it is \c double. |
142 | 146 |
typedef typename TR::LargeCost LargeCost; |
143 | 147 |
|
144 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
145 | 149 |
typedef TR Traits; |
146 | 150 |
|
147 | 151 |
public: |
148 | 152 |
|
149 | 153 |
/// \brief Problem type constants for the \c run() function. |
150 | 154 |
/// |
151 | 155 |
/// Enum type containing the problem type constants that can be |
152 | 156 |
/// returned by the \ref run() function of the algorithm. |
153 | 157 |
enum ProblemType { |
154 | 158 |
/// The problem has no feasible solution (flow). |
155 | 159 |
INFEASIBLE, |
156 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
157 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
158 | 162 |
/// potentials (primal and dual solutions). |
159 | 163 |
OPTIMAL, |
160 | 164 |
/// The digraph contains an arc of negative cost and infinite |
161 | 165 |
/// upper bound. It means that the objective function is unbounded |
162 | 166 |
/// on that arc, however, note that it could actually be bounded |
163 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
164 | 168 |
/// these cases. |
165 | 169 |
UNBOUNDED |
166 | 170 |
}; |
167 | 171 |
|
168 | 172 |
/// \brief Constants for selecting the internal method. |
169 | 173 |
/// |
170 | 174 |
/// Enum type containing constants for selecting the internal method |
171 | 175 |
/// for the \ref run() function. |
172 | 176 |
/// |
173 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
174 | 178 |
/// in their base operations, which are used in conjunction with the |
175 | 179 |
/// relabel operation. |
176 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
177 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
178 | 182 |
/// the most efficient and the most robust on various test inputs. |
179 | 183 |
/// However, the other methods can be selected using the \ref run() |
180 | 184 |
/// function with the proper parameter. |
181 | 185 |
enum Method { |
182 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
183 | 187 |
/// admissible arc at once. |
184 | 188 |
PUSH, |
185 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
186 | 190 |
/// paths from a node with excess to a node with deficit. |
187 | 191 |
AUGMENT, |
188 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
189 | 193 |
/// admissible paths started from a node with excess, but the |
190 | 194 |
/// lengths of these paths are limited. This method can be viewed |
191 | 195 |
/// as a combined version of the previous two operations. |
192 | 196 |
PARTIAL_AUGMENT |
193 | 197 |
}; |
194 | 198 |
|
195 | 199 |
private: |
196 | 200 |
|
197 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
198 | 202 |
|
199 | 203 |
typedef std::vector<int> IntVector; |
200 | 204 |
typedef std::vector<char> BoolVector; |
201 | 205 |
typedef std::vector<Value> ValueVector; |
202 | 206 |
typedef std::vector<Cost> CostVector; |
203 | 207 |
typedef std::vector<LargeCost> LargeCostVector; |
204 | 208 |
|
205 | 209 |
private: |
206 | 210 |
|
207 | 211 |
template <typename KT, typename VT> |
208 | 212 |
class StaticVectorMap { |
209 | 213 |
public: |
210 | 214 |
typedef KT Key; |
211 | 215 |
typedef VT Value; |
212 | 216 |
|
213 | 217 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
214 | 218 |
|
215 | 219 |
const Value& operator[](const Key& key) const { |
216 | 220 |
return _v[StaticDigraph::id(key)]; |
217 | 221 |
} |
218 | 222 |
|
219 | 223 |
Value& operator[](const Key& key) { |
220 | 224 |
return _v[StaticDigraph::id(key)]; |
221 | 225 |
} |
222 | 226 |
|
223 | 227 |
void set(const Key& key, const Value& val) { |
224 | 228 |
_v[StaticDigraph::id(key)] = val; |
225 | 229 |
} |
226 | 230 |
|
227 | 231 |
private: |
228 | 232 |
std::vector<Value>& _v; |
229 | 233 |
}; |
230 | 234 |
|
231 | 235 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
232 | 236 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
233 | 237 |
|
234 | 238 |
private: |
235 | 239 |
|
236 | 240 |
// Data related to the underlying digraph |
237 | 241 |
const GR &_graph; |
238 | 242 |
int _node_num; |
239 | 243 |
int _arc_num; |
240 | 244 |
int _res_node_num; |
241 | 245 |
int _res_arc_num; |
242 | 246 |
int _root; |
243 | 247 |
|
244 | 248 |
// Parameters of the problem |
245 | 249 |
bool _have_lower; |
246 | 250 |
Value _sum_supply; |
247 | 251 |
|
248 | 252 |
// Data structures for storing the digraph |
249 | 253 |
IntNodeMap _node_id; |
250 | 254 |
IntArcMap _arc_idf; |
251 | 255 |
IntArcMap _arc_idb; |
252 | 256 |
IntVector _first_out; |
253 | 257 |
BoolVector _forward; |
254 | 258 |
IntVector _source; |
255 | 259 |
IntVector _target; |
256 | 260 |
IntVector _reverse; |
257 | 261 |
|
258 | 262 |
// Node and arc data |
259 | 263 |
ValueVector _lower; |
260 | 264 |
ValueVector _upper; |
261 | 265 |
CostVector _scost; |
262 | 266 |
ValueVector _supply; |
263 | 267 |
|
264 | 268 |
ValueVector _res_cap; |
265 | 269 |
LargeCostVector _cost; |
266 | 270 |
LargeCostVector _pi; |
267 | 271 |
ValueVector _excess; |
268 | 272 |
IntVector _next_out; |
269 | 273 |
std::deque<int> _active_nodes; |
270 | 274 |
|
271 | 275 |
// Data for scaling |
272 | 276 |
LargeCost _epsilon; |
273 | 277 |
int _alpha; |
274 | 278 |
|
275 | 279 |
// Data for a StaticDigraph structure |
276 | 280 |
typedef std::pair<int, int> IntPair; |
277 | 281 |
StaticDigraph _sgr; |
278 | 282 |
std::vector<IntPair> _arc_vec; |
279 | 283 |
std::vector<LargeCost> _cost_vec; |
280 | 284 |
LargeCostArcMap _cost_map; |
281 | 285 |
LargeCostNodeMap _pi_map; |
282 | 286 |
|
283 | 287 |
public: |
284 | 288 |
|
285 | 289 |
/// \brief Constant for infinite upper bounds (capacities). |
286 | 290 |
/// |
287 | 291 |
/// Constant for infinite upper bounds (capacities). |
288 | 292 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
289 | 293 |
/// \c std::numeric_limits<Value>::max() otherwise. |
290 | 294 |
const Value INF; |
291 | 295 |
|
292 | 296 |
public: |
293 | 297 |
|
294 | 298 |
/// \name Named Template Parameters |
295 | 299 |
/// @{ |
296 | 300 |
|
297 | 301 |
template <typename T> |
298 | 302 |
struct SetLargeCostTraits : public Traits { |
299 | 303 |
typedef T LargeCost; |
300 | 304 |
}; |
301 | 305 |
|
302 | 306 |
/// \brief \ref named-templ-param "Named parameter" for setting |
303 | 307 |
/// \c LargeCost type. |
304 | 308 |
/// |
305 | 309 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
306 | 310 |
/// type, which is used for internal computations in the algorithm. |
307 | 311 |
/// \c Cost must be convertible to \c LargeCost. |
308 | 312 |
template <typename T> |
309 | 313 |
struct SetLargeCost |
310 | 314 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > { |
311 | 315 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
312 | 316 |
}; |
313 | 317 |
|
314 | 318 |
/// @} |
315 | 319 |
|
316 | 320 |
public: |
317 | 321 |
|
318 | 322 |
/// \brief Constructor. |
319 | 323 |
/// |
320 | 324 |
/// The constructor of the class. |
321 | 325 |
/// |
322 | 326 |
/// \param graph The digraph the algorithm runs on. |
323 | 327 |
CostScaling(const GR& graph) : |
324 | 328 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
325 | 329 |
_cost_map(_cost_vec), _pi_map(_pi), |
326 | 330 |
INF(std::numeric_limits<Value>::has_infinity ? |
327 | 331 |
std::numeric_limits<Value>::infinity() : |
328 | 332 |
std::numeric_limits<Value>::max()) |
329 | 333 |
{ |
330 | 334 |
// Check the number types |
331 | 335 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
332 | 336 |
"The flow type of CostScaling must be signed"); |
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 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS 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 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
///By default, it is a NullMap. |
67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 | 68 |
///Instantiates a \c ProcessedMap. |
69 | 69 |
|
70 | 70 |
///This function instantiates a \ref ProcessedMap. |
71 | 71 |
///\param g is the digraph, to which |
72 | 72 |
///we would like to define the \ref ProcessedMap. |
73 | 73 |
#ifdef DOXYGEN |
74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
75 | 75 |
#else |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%DFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 |
///\tparam TR The traits class that defines various types used by the |
|
125 |
///algorithm. By default, it is \ref DfsDefaultTraits |
|
126 |
///"DfsDefaultTraits<GR>". |
|
127 |
///In most cases, this parameter should not be set directly, |
|
128 |
///consider to use the named template parameters instead. |
|
124 | 129 |
#ifdef DOXYGEN |
125 | 130 |
template <typename GR, |
126 | 131 |
typename TR> |
127 | 132 |
#else |
128 | 133 |
template <typename GR=ListDigraph, |
129 | 134 |
typename TR=DfsDefaultTraits<GR> > |
130 | 135 |
#endif |
131 | 136 |
class Dfs { |
132 | 137 |
public: |
133 | 138 |
|
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename TR::Digraph Digraph; |
136 | 141 |
|
137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 143 |
///DFS paths. |
139 | 144 |
typedef typename TR::PredMap PredMap; |
140 | 145 |
///The type of the map that stores the distances of the nodes. |
141 | 146 |
typedef typename TR::DistMap DistMap; |
142 | 147 |
///The type of the map that indicates which nodes are reached. |
143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 149 |
///The type of the map that indicates which nodes are processed. |
145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 151 |
///The type of the paths. |
147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 153 |
|
149 | 154 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
150 | 155 |
typedef TR Traits; |
151 | 156 |
|
152 | 157 |
private: |
153 | 158 |
|
154 | 159 |
typedef typename Digraph::Node Node; |
155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 161 |
typedef typename Digraph::Arc Arc; |
157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 163 |
|
159 | 164 |
//Pointer to the underlying digraph. |
160 | 165 |
const Digraph *G; |
161 | 166 |
//Pointer to the map of predecessor arcs. |
162 | 167 |
PredMap *_pred; |
163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 169 |
bool local_pred; |
165 | 170 |
//Pointer to the map of distances. |
166 | 171 |
DistMap *_dist; |
167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 173 |
bool local_dist; |
169 | 174 |
//Pointer to the map of reached status of the nodes. |
170 | 175 |
ReachedMap *_reached; |
171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
172 | 177 |
bool local_reached; |
173 | 178 |
//Pointer to the map of processed status of the nodes. |
174 | 179 |
ProcessedMap *_processed; |
175 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
176 | 181 |
bool local_processed; |
177 | 182 |
|
178 | 183 |
std::vector<typename Digraph::OutArcIt> _stack; |
179 | 184 |
int _stack_head; |
180 | 185 |
|
181 | 186 |
//Creates the maps if necessary. |
182 | 187 |
void create_maps() |
183 | 188 |
{ |
184 | 189 |
if(!_pred) { |
185 | 190 |
local_pred = true; |
186 | 191 |
_pred = Traits::createPredMap(*G); |
187 | 192 |
} |
188 | 193 |
if(!_dist) { |
189 | 194 |
local_dist = true; |
190 | 195 |
_dist = Traits::createDistMap(*G); |
191 | 196 |
} |
192 | 197 |
if(!_reached) { |
193 | 198 |
local_reached = true; |
194 | 199 |
_reached = Traits::createReachedMap(*G); |
195 | 200 |
} |
196 | 201 |
if(!_processed) { |
197 | 202 |
local_processed = true; |
198 | 203 |
_processed = Traits::createProcessedMap(*G); |
199 | 204 |
} |
200 | 205 |
} |
201 | 206 |
|
202 | 207 |
protected: |
203 | 208 |
|
204 | 209 |
Dfs() {} |
205 | 210 |
|
206 | 211 |
public: |
207 | 212 |
|
208 | 213 |
typedef Dfs Create; |
209 | 214 |
|
210 | 215 |
///\name Named Template Parameters |
211 | 216 |
|
212 | 217 |
///@{ |
213 | 218 |
|
214 | 219 |
template <class T> |
215 | 220 |
struct SetPredMapTraits : public Traits { |
216 | 221 |
typedef T PredMap; |
217 | 222 |
static PredMap *createPredMap(const Digraph &) |
218 | 223 |
{ |
219 | 224 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 225 |
return 0; // ignore warnings |
221 | 226 |
} |
222 | 227 |
}; |
223 | 228 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 | 229 |
///\c PredMap type. |
225 | 230 |
/// |
226 | 231 |
///\ref named-templ-param "Named parameter" for setting |
227 | 232 |
///\c PredMap type. |
228 | 233 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
229 | 234 |
template <class T> |
230 | 235 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
231 | 236 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
232 | 237 |
}; |
233 | 238 |
|
234 | 239 |
template <class T> |
235 | 240 |
struct SetDistMapTraits : public Traits { |
236 | 241 |
typedef T DistMap; |
237 | 242 |
static DistMap *createDistMap(const Digraph &) |
238 | 243 |
{ |
239 | 244 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 245 |
return 0; // ignore warnings |
241 | 246 |
} |
242 | 247 |
}; |
243 | 248 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 | 249 |
///\c DistMap type. |
245 | 250 |
/// |
246 | 251 |
///\ref named-templ-param "Named parameter" for setting |
247 | 252 |
///\c DistMap type. |
248 | 253 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
249 | 254 |
template <class T> |
250 | 255 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
251 | 256 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
252 | 257 |
}; |
253 | 258 |
|
254 | 259 |
template <class T> |
255 | 260 |
struct SetReachedMapTraits : public Traits { |
256 | 261 |
typedef T ReachedMap; |
257 | 262 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 263 |
{ |
259 | 264 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
260 | 265 |
return 0; // ignore warnings |
261 | 266 |
} |
262 | 267 |
}; |
263 | 268 |
///\brief \ref named-templ-param "Named parameter" for setting |
264 | 269 |
///\c ReachedMap type. |
265 | 270 |
/// |
266 | 271 |
///\ref named-templ-param "Named parameter" for setting |
267 | 272 |
///\c ReachedMap type. |
268 | 273 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
269 | 274 |
template <class T> |
270 | 275 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
271 | 276 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
272 | 277 |
}; |
273 | 278 |
|
274 | 279 |
template <class T> |
275 | 280 |
struct SetProcessedMapTraits : public Traits { |
276 | 281 |
typedef T ProcessedMap; |
277 | 282 |
static ProcessedMap *createProcessedMap(const Digraph &) |
278 | 283 |
{ |
279 | 284 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
280 | 285 |
return 0; // ignore warnings |
281 | 286 |
} |
282 | 287 |
}; |
283 | 288 |
///\brief \ref named-templ-param "Named parameter" for setting |
284 | 289 |
///\c ProcessedMap type. |
285 | 290 |
/// |
286 | 291 |
///\ref named-templ-param "Named parameter" for setting |
287 | 292 |
///\c ProcessedMap type. |
288 | 293 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
289 | 294 |
template <class T> |
290 | 295 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
291 | 296 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
292 | 297 |
}; |
293 | 298 |
|
294 | 299 |
struct SetStandardProcessedMapTraits : public Traits { |
295 | 300 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
296 | 301 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
297 | 302 |
{ |
298 | 303 |
return new ProcessedMap(g); |
299 | 304 |
} |
300 | 305 |
}; |
301 | 306 |
///\brief \ref named-templ-param "Named parameter" for setting |
302 | 307 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
303 | 308 |
/// |
304 | 309 |
///\ref named-templ-param "Named parameter" for setting |
305 | 310 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
306 | 311 |
///If you don't set it explicitly, it will be automatically allocated. |
307 | 312 |
struct SetStandardProcessedMap : |
308 | 313 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
309 | 314 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
310 | 315 |
}; |
311 | 316 |
|
312 | 317 |
///@} |
313 | 318 |
|
314 | 319 |
public: |
315 | 320 |
|
... | ... |
@@ -698,739 +703,742 @@ |
698 | 703 |
///\ref predNode() and \ref predMap(). |
699 | 704 |
/// |
700 | 705 |
///\pre Either \ref run(Node) "run()" or \ref init() |
701 | 706 |
///must be called before using this function. |
702 | 707 |
Arc predArc(Node v) const { return (*_pred)[v];} |
703 | 708 |
|
704 | 709 |
///Returns the 'previous node' of the %DFS tree for the given node. |
705 | 710 |
|
706 | 711 |
///This function returns the 'previous node' of the %DFS |
707 | 712 |
///tree for the node \c v, i.e. it returns the last but one node |
708 | 713 |
///of a %DFS path from a root to \c v. It is \c INVALID |
709 | 714 |
///if \c v is not reached from the root(s) or if \c v is a root. |
710 | 715 |
/// |
711 | 716 |
///The %DFS tree used here is equal to the %DFS tree used in |
712 | 717 |
///\ref predArc() and \ref predMap(). |
713 | 718 |
/// |
714 | 719 |
///\pre Either \ref run(Node) "run()" or \ref init() |
715 | 720 |
///must be called before using this function. |
716 | 721 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
717 | 722 |
G->source((*_pred)[v]); } |
718 | 723 |
|
719 | 724 |
///\brief Returns a const reference to the node map that stores the |
720 | 725 |
///distances of the nodes. |
721 | 726 |
/// |
722 | 727 |
///Returns a const reference to the node map that stores the |
723 | 728 |
///distances of the nodes calculated by the algorithm. |
724 | 729 |
/// |
725 | 730 |
///\pre Either \ref run(Node) "run()" or \ref init() |
726 | 731 |
///must be called before using this function. |
727 | 732 |
const DistMap &distMap() const { return *_dist;} |
728 | 733 |
|
729 | 734 |
///\brief Returns a const reference to the node map that stores the |
730 | 735 |
///predecessor arcs. |
731 | 736 |
/// |
732 | 737 |
///Returns a const reference to the node map that stores the predecessor |
733 | 738 |
///arcs, which form the DFS tree (forest). |
734 | 739 |
/// |
735 | 740 |
///\pre Either \ref run(Node) "run()" or \ref init() |
736 | 741 |
///must be called before using this function. |
737 | 742 |
const PredMap &predMap() const { return *_pred;} |
738 | 743 |
|
739 | 744 |
///Checks if the given node. node is reached from the root(s). |
740 | 745 |
|
741 | 746 |
///Returns \c true if \c v is reached from the root(s). |
742 | 747 |
/// |
743 | 748 |
///\pre Either \ref run(Node) "run()" or \ref init() |
744 | 749 |
///must be called before using this function. |
745 | 750 |
bool reached(Node v) const { return (*_reached)[v]; } |
746 | 751 |
|
747 | 752 |
///@} |
748 | 753 |
}; |
749 | 754 |
|
750 | 755 |
///Default traits class of dfs() function. |
751 | 756 |
|
752 | 757 |
///Default traits class of dfs() function. |
753 | 758 |
///\tparam GR Digraph type. |
754 | 759 |
template<class GR> |
755 | 760 |
struct DfsWizardDefaultTraits |
756 | 761 |
{ |
757 | 762 |
///The type of the digraph the algorithm runs on. |
758 | 763 |
typedef GR Digraph; |
759 | 764 |
|
760 | 765 |
///\brief The type of the map that stores the predecessor |
761 | 766 |
///arcs of the %DFS paths. |
762 | 767 |
/// |
763 | 768 |
///The type of the map that stores the predecessor |
764 | 769 |
///arcs of the %DFS paths. |
765 | 770 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
766 | 771 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
767 | 772 |
///Instantiates a PredMap. |
768 | 773 |
|
769 | 774 |
///This function instantiates a PredMap. |
770 | 775 |
///\param g is the digraph, to which we would like to define the |
771 | 776 |
///PredMap. |
772 | 777 |
static PredMap *createPredMap(const Digraph &g) |
773 | 778 |
{ |
774 | 779 |
return new PredMap(g); |
775 | 780 |
} |
776 | 781 |
|
777 | 782 |
///The type of the map that indicates which nodes are processed. |
778 | 783 |
|
779 | 784 |
///The type of the map that indicates which nodes are processed. |
780 | 785 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
781 | 786 |
///By default, it is a NullMap. |
782 | 787 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
783 | 788 |
///Instantiates a ProcessedMap. |
784 | 789 |
|
785 | 790 |
///This function instantiates a ProcessedMap. |
786 | 791 |
///\param g is the digraph, to which |
787 | 792 |
///we would like to define the ProcessedMap. |
788 | 793 |
#ifdef DOXYGEN |
789 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
790 | 795 |
#else |
791 | 796 |
static ProcessedMap *createProcessedMap(const Digraph &) |
792 | 797 |
#endif |
793 | 798 |
{ |
794 | 799 |
return new ProcessedMap(); |
795 | 800 |
} |
796 | 801 |
|
797 | 802 |
///The type of the map that indicates which nodes are reached. |
798 | 803 |
|
799 | 804 |
///The type of the map that indicates which nodes are reached. |
800 | 805 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
801 | 806 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
802 | 807 |
///Instantiates a ReachedMap. |
803 | 808 |
|
804 | 809 |
///This function instantiates a ReachedMap. |
805 | 810 |
///\param g is the digraph, to which |
806 | 811 |
///we would like to define the ReachedMap. |
807 | 812 |
static ReachedMap *createReachedMap(const Digraph &g) |
808 | 813 |
{ |
809 | 814 |
return new ReachedMap(g); |
810 | 815 |
} |
811 | 816 |
|
812 | 817 |
///The type of the map that stores the distances of the nodes. |
813 | 818 |
|
814 | 819 |
///The type of the map that stores the distances of the nodes. |
815 | 820 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
816 | 821 |
typedef typename Digraph::template NodeMap<int> DistMap; |
817 | 822 |
///Instantiates a DistMap. |
818 | 823 |
|
819 | 824 |
///This function instantiates a DistMap. |
820 | 825 |
///\param g is the digraph, to which we would like to define |
821 | 826 |
///the DistMap |
822 | 827 |
static DistMap *createDistMap(const Digraph &g) |
823 | 828 |
{ |
824 | 829 |
return new DistMap(g); |
825 | 830 |
} |
826 | 831 |
|
827 | 832 |
///The type of the DFS paths. |
828 | 833 |
|
829 | 834 |
///The type of the DFS paths. |
830 | 835 |
///It must conform to the \ref concepts::Path "Path" concept. |
831 | 836 |
typedef lemon::Path<Digraph> Path; |
832 | 837 |
}; |
833 | 838 |
|
834 | 839 |
/// Default traits class used by DfsWizard |
835 | 840 |
|
836 | 841 |
/// Default traits class used by DfsWizard. |
837 | 842 |
/// \tparam GR The type of the digraph. |
838 | 843 |
template<class GR> |
839 | 844 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
840 | 845 |
{ |
841 | 846 |
|
842 | 847 |
typedef DfsWizardDefaultTraits<GR> Base; |
843 | 848 |
protected: |
844 | 849 |
//The type of the nodes in the digraph. |
845 | 850 |
typedef typename Base::Digraph::Node Node; |
846 | 851 |
|
847 | 852 |
//Pointer to the digraph the algorithm runs on. |
848 | 853 |
void *_g; |
849 | 854 |
//Pointer to the map of reached nodes. |
850 | 855 |
void *_reached; |
851 | 856 |
//Pointer to the map of processed nodes. |
852 | 857 |
void *_processed; |
853 | 858 |
//Pointer to the map of predecessors arcs. |
854 | 859 |
void *_pred; |
855 | 860 |
//Pointer to the map of distances. |
856 | 861 |
void *_dist; |
857 | 862 |
//Pointer to the DFS path to the target node. |
858 | 863 |
void *_path; |
859 | 864 |
//Pointer to the distance of the target node. |
860 | 865 |
int *_di; |
861 | 866 |
|
862 | 867 |
public: |
863 | 868 |
/// Constructor. |
864 | 869 |
|
865 | 870 |
/// This constructor does not require parameters, it initiates |
866 | 871 |
/// all of the attributes to \c 0. |
867 | 872 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
868 | 873 |
_dist(0), _path(0), _di(0) {} |
869 | 874 |
|
870 | 875 |
/// Constructor. |
871 | 876 |
|
872 | 877 |
/// This constructor requires one parameter, |
873 | 878 |
/// others are initiated to \c 0. |
874 | 879 |
/// \param g The digraph the algorithm runs on. |
875 | 880 |
DfsWizardBase(const GR &g) : |
876 | 881 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
877 | 882 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
878 | 883 |
|
879 | 884 |
}; |
880 | 885 |
|
881 | 886 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
882 | 887 |
|
883 | 888 |
/// This auxiliary class is created to implement the |
884 | 889 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
885 | 890 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
886 | 891 |
/// functions and features of the plain \ref Dfs. |
887 | 892 |
/// |
888 | 893 |
/// This class should only be used through the \ref dfs() function, |
889 | 894 |
/// which makes it easier to use the algorithm. |
895 |
/// |
|
896 |
/// \tparam TR The traits class that defines various types used by the |
|
897 |
/// algorithm. |
|
890 | 898 |
template<class TR> |
891 | 899 |
class DfsWizard : public TR |
892 | 900 |
{ |
893 | 901 |
typedef TR Base; |
894 | 902 |
|
895 | 903 |
typedef typename TR::Digraph Digraph; |
896 | 904 |
|
897 | 905 |
typedef typename Digraph::Node Node; |
898 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
899 | 907 |
typedef typename Digraph::Arc Arc; |
900 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
901 | 909 |
|
902 | 910 |
typedef typename TR::PredMap PredMap; |
903 | 911 |
typedef typename TR::DistMap DistMap; |
904 | 912 |
typedef typename TR::ReachedMap ReachedMap; |
905 | 913 |
typedef typename TR::ProcessedMap ProcessedMap; |
906 | 914 |
typedef typename TR::Path Path; |
907 | 915 |
|
908 | 916 |
public: |
909 | 917 |
|
910 | 918 |
/// Constructor. |
911 | 919 |
DfsWizard() : TR() {} |
912 | 920 |
|
913 | 921 |
/// Constructor that requires parameters. |
914 | 922 |
|
915 | 923 |
/// Constructor that requires parameters. |
916 | 924 |
/// These parameters will be the default values for the traits class. |
917 | 925 |
/// \param g The digraph the algorithm runs on. |
918 | 926 |
DfsWizard(const Digraph &g) : |
919 | 927 |
TR(g) {} |
920 | 928 |
|
921 | 929 |
///Copy constructor |
922 | 930 |
DfsWizard(const TR &b) : TR(b) {} |
923 | 931 |
|
924 | 932 |
~DfsWizard() {} |
925 | 933 |
|
926 | 934 |
///Runs DFS algorithm from the given source node. |
927 | 935 |
|
928 | 936 |
///This method runs DFS algorithm from node \c s |
929 | 937 |
///in order to compute the DFS path to each node. |
930 | 938 |
void run(Node s) |
931 | 939 |
{ |
932 | 940 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
933 | 941 |
if (Base::_pred) |
934 | 942 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
935 | 943 |
if (Base::_dist) |
936 | 944 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
937 | 945 |
if (Base::_reached) |
938 | 946 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
939 | 947 |
if (Base::_processed) |
940 | 948 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
941 | 949 |
if (s!=INVALID) |
942 | 950 |
alg.run(s); |
943 | 951 |
else |
944 | 952 |
alg.run(); |
945 | 953 |
} |
946 | 954 |
|
947 | 955 |
///Finds the DFS path between \c s and \c t. |
948 | 956 |
|
949 | 957 |
///This method runs DFS algorithm from node \c s |
950 | 958 |
///in order to compute the DFS path to node \c t |
951 | 959 |
///(it stops searching when \c t is processed). |
952 | 960 |
/// |
953 | 961 |
///\return \c true if \c t is reachable form \c s. |
954 | 962 |
bool run(Node s, Node t) |
955 | 963 |
{ |
956 | 964 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
957 | 965 |
if (Base::_pred) |
958 | 966 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
959 | 967 |
if (Base::_dist) |
960 | 968 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
961 | 969 |
if (Base::_reached) |
962 | 970 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
963 | 971 |
if (Base::_processed) |
964 | 972 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
965 | 973 |
alg.run(s,t); |
966 | 974 |
if (Base::_path) |
967 | 975 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
968 | 976 |
if (Base::_di) |
969 | 977 |
*Base::_di = alg.dist(t); |
970 | 978 |
return alg.reached(t); |
971 | 979 |
} |
972 | 980 |
|
973 | 981 |
///Runs DFS algorithm to visit all nodes in the digraph. |
974 | 982 |
|
975 | 983 |
///This method runs DFS algorithm in order to visit all nodes |
976 | 984 |
///in the digraph. |
977 | 985 |
void run() |
978 | 986 |
{ |
979 | 987 |
run(INVALID); |
980 | 988 |
} |
981 | 989 |
|
982 | 990 |
template<class T> |
983 | 991 |
struct SetPredMapBase : public Base { |
984 | 992 |
typedef T PredMap; |
985 | 993 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
986 | 994 |
SetPredMapBase(const TR &b) : TR(b) {} |
987 | 995 |
}; |
988 | 996 |
|
989 | 997 |
///\brief \ref named-templ-param "Named parameter" for setting |
990 | 998 |
///the predecessor map. |
991 | 999 |
/// |
992 | 1000 |
///\ref named-templ-param "Named parameter" function for setting |
993 | 1001 |
///the map that stores the predecessor arcs of the nodes. |
994 | 1002 |
template<class T> |
995 | 1003 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
996 | 1004 |
{ |
997 | 1005 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
998 | 1006 |
return DfsWizard<SetPredMapBase<T> >(*this); |
999 | 1007 |
} |
1000 | 1008 |
|
1001 | 1009 |
template<class T> |
1002 | 1010 |
struct SetReachedMapBase : public Base { |
1003 | 1011 |
typedef T ReachedMap; |
1004 | 1012 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1005 | 1013 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1006 | 1014 |
}; |
1007 | 1015 |
|
1008 | 1016 |
///\brief \ref named-templ-param "Named parameter" for setting |
1009 | 1017 |
///the reached map. |
1010 | 1018 |
/// |
1011 | 1019 |
///\ref named-templ-param "Named parameter" function for setting |
1012 | 1020 |
///the map that indicates which nodes are reached. |
1013 | 1021 |
template<class T> |
1014 | 1022 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1015 | 1023 |
{ |
1016 | 1024 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1017 | 1025 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1018 | 1026 |
} |
1019 | 1027 |
|
1020 | 1028 |
template<class T> |
1021 | 1029 |
struct SetDistMapBase : public Base { |
1022 | 1030 |
typedef T DistMap; |
1023 | 1031 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1024 | 1032 |
SetDistMapBase(const TR &b) : TR(b) {} |
1025 | 1033 |
}; |
1026 | 1034 |
|
1027 | 1035 |
///\brief \ref named-templ-param "Named parameter" for setting |
1028 | 1036 |
///the distance map. |
1029 | 1037 |
/// |
1030 | 1038 |
///\ref named-templ-param "Named parameter" function for setting |
1031 | 1039 |
///the map that stores the distances of the nodes calculated |
1032 | 1040 |
///by the algorithm. |
1033 | 1041 |
template<class T> |
1034 | 1042 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1035 | 1043 |
{ |
1036 | 1044 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1037 | 1045 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1038 | 1046 |
} |
1039 | 1047 |
|
1040 | 1048 |
template<class T> |
1041 | 1049 |
struct SetProcessedMapBase : public Base { |
1042 | 1050 |
typedef T ProcessedMap; |
1043 | 1051 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1044 | 1052 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1045 | 1053 |
}; |
1046 | 1054 |
|
1047 | 1055 |
///\brief \ref named-func-param "Named parameter" for setting |
1048 | 1056 |
///the processed map. |
1049 | 1057 |
/// |
1050 | 1058 |
///\ref named-templ-param "Named parameter" function for setting |
1051 | 1059 |
///the map that indicates which nodes are processed. |
1052 | 1060 |
template<class T> |
1053 | 1061 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1054 | 1062 |
{ |
1055 | 1063 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1056 | 1064 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1057 | 1065 |
} |
1058 | 1066 |
|
1059 | 1067 |
template<class T> |
1060 | 1068 |
struct SetPathBase : public Base { |
1061 | 1069 |
typedef T Path; |
1062 | 1070 |
SetPathBase(const TR &b) : TR(b) {} |
1063 | 1071 |
}; |
1064 | 1072 |
///\brief \ref named-func-param "Named parameter" |
1065 | 1073 |
///for getting the DFS path to the target node. |
1066 | 1074 |
/// |
1067 | 1075 |
///\ref named-func-param "Named parameter" |
1068 | 1076 |
///for getting the DFS path to the target node. |
1069 | 1077 |
template<class T> |
1070 | 1078 |
DfsWizard<SetPathBase<T> > path(const T &t) |
1071 | 1079 |
{ |
1072 | 1080 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1073 | 1081 |
return DfsWizard<SetPathBase<T> >(*this); |
1074 | 1082 |
} |
1075 | 1083 |
|
1076 | 1084 |
///\brief \ref named-func-param "Named parameter" |
1077 | 1085 |
///for getting the distance of the target node. |
1078 | 1086 |
/// |
1079 | 1087 |
///\ref named-func-param "Named parameter" |
1080 | 1088 |
///for getting the distance of the target node. |
1081 | 1089 |
DfsWizard dist(const int &d) |
1082 | 1090 |
{ |
1083 | 1091 |
Base::_di=const_cast<int*>(&d); |
1084 | 1092 |
return *this; |
1085 | 1093 |
} |
1086 | 1094 |
|
1087 | 1095 |
}; |
1088 | 1096 |
|
1089 | 1097 |
///Function-type interface for DFS algorithm. |
1090 | 1098 |
|
1091 | 1099 |
///\ingroup search |
1092 | 1100 |
///Function-type interface for DFS algorithm. |
1093 | 1101 |
/// |
1094 | 1102 |
///This function also has several \ref named-func-param "named parameters", |
1095 | 1103 |
///they are declared as the members of class \ref DfsWizard. |
1096 | 1104 |
///The following examples show how to use these parameters. |
1097 | 1105 |
///\code |
1098 | 1106 |
/// // Compute the DFS tree |
1099 | 1107 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1100 | 1108 |
/// |
1101 | 1109 |
/// // Compute the DFS path from s to t |
1102 | 1110 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1103 | 1111 |
///\endcode |
1104 | 1112 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
1105 | 1113 |
///to the end of the parameter list. |
1106 | 1114 |
///\sa DfsWizard |
1107 | 1115 |
///\sa Dfs |
1108 | 1116 |
template<class GR> |
1109 | 1117 |
DfsWizard<DfsWizardBase<GR> > |
1110 | 1118 |
dfs(const GR &digraph) |
1111 | 1119 |
{ |
1112 | 1120 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1113 | 1121 |
} |
1114 | 1122 |
|
1115 | 1123 |
#ifdef DOXYGEN |
1116 | 1124 |
/// \brief Visitor class for DFS. |
1117 | 1125 |
/// |
1118 | 1126 |
/// This class defines the interface of the DfsVisit events, and |
1119 | 1127 |
/// it could be the base of a real visitor class. |
1120 | 1128 |
template <typename GR> |
1121 | 1129 |
struct DfsVisitor { |
1122 | 1130 |
typedef GR Digraph; |
1123 | 1131 |
typedef typename Digraph::Arc Arc; |
1124 | 1132 |
typedef typename Digraph::Node Node; |
1125 | 1133 |
/// \brief Called for the source node of the DFS. |
1126 | 1134 |
/// |
1127 | 1135 |
/// This function is called for the source node of the DFS. |
1128 | 1136 |
void start(const Node& node) {} |
1129 | 1137 |
/// \brief Called when the source node is leaved. |
1130 | 1138 |
/// |
1131 | 1139 |
/// This function is called when the source node is leaved. |
1132 | 1140 |
void stop(const Node& node) {} |
1133 | 1141 |
/// \brief Called when a node is reached first time. |
1134 | 1142 |
/// |
1135 | 1143 |
/// This function is called when a node is reached first time. |
1136 | 1144 |
void reach(const Node& node) {} |
1137 | 1145 |
/// \brief Called when an arc reaches a new node. |
1138 | 1146 |
/// |
1139 | 1147 |
/// This function is called when the DFS finds an arc whose target node |
1140 | 1148 |
/// is not reached yet. |
1141 | 1149 |
void discover(const Arc& arc) {} |
1142 | 1150 |
/// \brief Called when an arc is examined but its target node is |
1143 | 1151 |
/// already discovered. |
1144 | 1152 |
/// |
1145 | 1153 |
/// This function is called when an arc is examined but its target node is |
1146 | 1154 |
/// already discovered. |
1147 | 1155 |
void examine(const Arc& arc) {} |
1148 | 1156 |
/// \brief Called when the DFS steps back from a node. |
1149 | 1157 |
/// |
1150 | 1158 |
/// This function is called when the DFS steps back from a node. |
1151 | 1159 |
void leave(const Node& node) {} |
1152 | 1160 |
/// \brief Called when the DFS steps back on an arc. |
1153 | 1161 |
/// |
1154 | 1162 |
/// This function is called when the DFS steps back on an arc. |
1155 | 1163 |
void backtrack(const Arc& arc) {} |
1156 | 1164 |
}; |
1157 | 1165 |
#else |
1158 | 1166 |
template <typename GR> |
1159 | 1167 |
struct DfsVisitor { |
1160 | 1168 |
typedef GR Digraph; |
1161 | 1169 |
typedef typename Digraph::Arc Arc; |
1162 | 1170 |
typedef typename Digraph::Node Node; |
1163 | 1171 |
void start(const Node&) {} |
1164 | 1172 |
void stop(const Node&) {} |
1165 | 1173 |
void reach(const Node&) {} |
1166 | 1174 |
void discover(const Arc&) {} |
1167 | 1175 |
void examine(const Arc&) {} |
1168 | 1176 |
void leave(const Node&) {} |
1169 | 1177 |
void backtrack(const Arc&) {} |
1170 | 1178 |
|
1171 | 1179 |
template <typename _Visitor> |
1172 | 1180 |
struct Constraints { |
1173 | 1181 |
void constraints() { |
1174 | 1182 |
Arc arc; |
1175 | 1183 |
Node node; |
1176 | 1184 |
visitor.start(node); |
1177 | 1185 |
visitor.stop(arc); |
1178 | 1186 |
visitor.reach(node); |
1179 | 1187 |
visitor.discover(arc); |
1180 | 1188 |
visitor.examine(arc); |
1181 | 1189 |
visitor.leave(node); |
1182 | 1190 |
visitor.backtrack(arc); |
1183 | 1191 |
} |
1184 | 1192 |
_Visitor& visitor; |
1185 | 1193 |
}; |
1186 | 1194 |
}; |
1187 | 1195 |
#endif |
1188 | 1196 |
|
1189 | 1197 |
/// \brief Default traits class of DfsVisit class. |
1190 | 1198 |
/// |
1191 | 1199 |
/// Default traits class of DfsVisit class. |
1192 | 1200 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1193 | 1201 |
template<class GR> |
1194 | 1202 |
struct DfsVisitDefaultTraits { |
1195 | 1203 |
|
1196 | 1204 |
/// \brief The type of the digraph the algorithm runs on. |
1197 | 1205 |
typedef GR Digraph; |
1198 | 1206 |
|
1199 | 1207 |
/// \brief The type of the map that indicates which nodes are reached. |
1200 | 1208 |
/// |
1201 | 1209 |
/// The type of the map that indicates which nodes are reached. |
1202 | 1210 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1203 | 1211 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1204 | 1212 |
|
1205 | 1213 |
/// \brief Instantiates a ReachedMap. |
1206 | 1214 |
/// |
1207 | 1215 |
/// This function instantiates a ReachedMap. |
1208 | 1216 |
/// \param digraph is the digraph, to which |
1209 | 1217 |
/// we would like to define the ReachedMap. |
1210 | 1218 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1211 | 1219 |
return new ReachedMap(digraph); |
1212 | 1220 |
} |
1213 | 1221 |
|
1214 | 1222 |
}; |
1215 | 1223 |
|
1216 | 1224 |
/// \ingroup search |
1217 | 1225 |
/// |
1218 | 1226 |
/// \brief DFS algorithm class with visitor interface. |
1219 | 1227 |
/// |
1220 | 1228 |
/// This class provides an efficient implementation of the DFS algorithm |
1221 | 1229 |
/// with visitor interface. |
1222 | 1230 |
/// |
1223 | 1231 |
/// The DfsVisit class provides an alternative interface to the Dfs |
1224 | 1232 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1225 | 1233 |
/// the member functions of the \c Visitor class on every DFS event. |
1226 | 1234 |
/// |
1227 | 1235 |
/// This interface of the DFS algorithm should be used in special cases |
1228 | 1236 |
/// when extra actions have to be performed in connection with certain |
1229 | 1237 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1230 | 1238 |
/// instead. |
1231 | 1239 |
/// |
1232 | 1240 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1233 | 1241 |
/// The default type is \ref ListDigraph. |
1234 | 1242 |
/// The value of GR is not used directly by \ref DfsVisit, |
1235 | 1243 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
1236 | 1244 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1237 | 1245 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
1238 | 1246 |
/// does not observe the DFS events. If you want to observe the DFS |
1239 | 1247 |
/// events, you should implement your own visitor class. |
1240 |
/// \tparam TR Traits class to set various data types used by the |
|
1241 |
/// algorithm. The default traits class is |
|
1242 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
|
1243 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
|
1244 |
/// |
|
1248 |
/// \tparam TR The traits class that defines various types used by the |
|
1249 |
/// algorithm. By default, it is \ref DfsVisitDefaultTraits |
|
1250 |
/// "DfsVisitDefaultTraits<GR>". |
|
1251 |
/// In most cases, this parameter should not be set directly, |
|
1252 |
/// consider to use the named template parameters instead. |
|
1245 | 1253 |
#ifdef DOXYGEN |
1246 | 1254 |
template <typename GR, typename VS, typename TR> |
1247 | 1255 |
#else |
1248 | 1256 |
template <typename GR = ListDigraph, |
1249 | 1257 |
typename VS = DfsVisitor<GR>, |
1250 | 1258 |
typename TR = DfsVisitDefaultTraits<GR> > |
1251 | 1259 |
#endif |
1252 | 1260 |
class DfsVisit { |
1253 | 1261 |
public: |
1254 | 1262 |
|
1255 | 1263 |
///The traits class. |
1256 | 1264 |
typedef TR Traits; |
1257 | 1265 |
|
1258 | 1266 |
///The type of the digraph the algorithm runs on. |
1259 | 1267 |
typedef typename Traits::Digraph Digraph; |
1260 | 1268 |
|
1261 | 1269 |
///The visitor type used by the algorithm. |
1262 | 1270 |
typedef VS Visitor; |
1263 | 1271 |
|
1264 | 1272 |
///The type of the map that indicates which nodes are reached. |
1265 | 1273 |
typedef typename Traits::ReachedMap ReachedMap; |
1266 | 1274 |
|
1267 | 1275 |
private: |
1268 | 1276 |
|
1269 | 1277 |
typedef typename Digraph::Node Node; |
1270 | 1278 |
typedef typename Digraph::NodeIt NodeIt; |
1271 | 1279 |
typedef typename Digraph::Arc Arc; |
1272 | 1280 |
typedef typename Digraph::OutArcIt OutArcIt; |
1273 | 1281 |
|
1274 | 1282 |
//Pointer to the underlying digraph. |
1275 | 1283 |
const Digraph *_digraph; |
1276 | 1284 |
//Pointer to the visitor object. |
1277 | 1285 |
Visitor *_visitor; |
1278 | 1286 |
//Pointer to the map of reached status of the nodes. |
1279 | 1287 |
ReachedMap *_reached; |
1280 | 1288 |
//Indicates if _reached is locally allocated (true) or not. |
1281 | 1289 |
bool local_reached; |
1282 | 1290 |
|
1283 | 1291 |
std::vector<typename Digraph::Arc> _stack; |
1284 | 1292 |
int _stack_head; |
1285 | 1293 |
|
1286 | 1294 |
//Creates the maps if necessary. |
1287 | 1295 |
void create_maps() { |
1288 | 1296 |
if(!_reached) { |
1289 | 1297 |
local_reached = true; |
1290 | 1298 |
_reached = Traits::createReachedMap(*_digraph); |
1291 | 1299 |
} |
1292 | 1300 |
} |
1293 | 1301 |
|
1294 | 1302 |
protected: |
1295 | 1303 |
|
1296 | 1304 |
DfsVisit() {} |
1297 | 1305 |
|
1298 | 1306 |
public: |
1299 | 1307 |
|
1300 | 1308 |
typedef DfsVisit Create; |
1301 | 1309 |
|
1302 | 1310 |
/// \name Named Template Parameters |
1303 | 1311 |
|
1304 | 1312 |
///@{ |
1305 | 1313 |
template <class T> |
1306 | 1314 |
struct SetReachedMapTraits : public Traits { |
1307 | 1315 |
typedef T ReachedMap; |
1308 | 1316 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1309 | 1317 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1310 | 1318 |
return 0; // ignore warnings |
1311 | 1319 |
} |
1312 | 1320 |
}; |
1313 | 1321 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1314 | 1322 |
/// ReachedMap type. |
1315 | 1323 |
/// |
1316 | 1324 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1317 | 1325 |
template <class T> |
1318 | 1326 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1319 | 1327 |
SetReachedMapTraits<T> > { |
1320 | 1328 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1321 | 1329 |
}; |
1322 | 1330 |
///@} |
1323 | 1331 |
|
1324 | 1332 |
public: |
1325 | 1333 |
|
1326 | 1334 |
/// \brief Constructor. |
1327 | 1335 |
/// |
1328 | 1336 |
/// Constructor. |
1329 | 1337 |
/// |
1330 | 1338 |
/// \param digraph The digraph the algorithm runs on. |
1331 | 1339 |
/// \param visitor The visitor object of the algorithm. |
1332 | 1340 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1333 | 1341 |
: _digraph(&digraph), _visitor(&visitor), |
1334 | 1342 |
_reached(0), local_reached(false) {} |
1335 | 1343 |
|
1336 | 1344 |
/// \brief Destructor. |
1337 | 1345 |
~DfsVisit() { |
1338 | 1346 |
if(local_reached) delete _reached; |
1339 | 1347 |
} |
1340 | 1348 |
|
1341 | 1349 |
/// \brief Sets the map that indicates which nodes are reached. |
1342 | 1350 |
/// |
1343 | 1351 |
/// Sets the map that indicates which nodes are reached. |
1344 | 1352 |
/// If you don't use this function before calling \ref run(Node) "run()" |
1345 | 1353 |
/// or \ref init(), an instance will be allocated automatically. |
1346 | 1354 |
/// The destructor deallocates this automatically allocated map, |
1347 | 1355 |
/// of course. |
1348 | 1356 |
/// \return <tt> (*this) </tt> |
1349 | 1357 |
DfsVisit &reachedMap(ReachedMap &m) { |
1350 | 1358 |
if(local_reached) { |
1351 | 1359 |
delete _reached; |
1352 | 1360 |
local_reached=false; |
1353 | 1361 |
} |
1354 | 1362 |
_reached = &m; |
1355 | 1363 |
return *this; |
1356 | 1364 |
} |
1357 | 1365 |
|
1358 | 1366 |
public: |
1359 | 1367 |
|
1360 | 1368 |
/// \name Execution Control |
1361 | 1369 |
/// The simplest way to execute the DFS algorithm is to use one of the |
1362 | 1370 |
/// member functions called \ref run(Node) "run()".\n |
1363 | 1371 |
/// If you need better control on the execution, you have to call |
1364 | 1372 |
/// \ref init() first, then you can add a source node with \ref addSource() |
1365 | 1373 |
/// and perform the actual computation with \ref start(). |
1366 | 1374 |
/// This procedure can be repeated if there are nodes that have not |
1367 | 1375 |
/// been reached. |
1368 | 1376 |
|
1369 | 1377 |
/// @{ |
1370 | 1378 |
|
1371 | 1379 |
/// \brief Initializes the internal data structures. |
1372 | 1380 |
/// |
1373 | 1381 |
/// Initializes the internal data structures. |
1374 | 1382 |
void init() { |
1375 | 1383 |
create_maps(); |
1376 | 1384 |
_stack.resize(countNodes(*_digraph)); |
1377 | 1385 |
_stack_head = -1; |
1378 | 1386 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1379 | 1387 |
_reached->set(u, false); |
1380 | 1388 |
} |
1381 | 1389 |
} |
1382 | 1390 |
|
1383 | 1391 |
/// \brief Adds a new source node. |
1384 | 1392 |
/// |
1385 | 1393 |
/// Adds a new source node to the set of nodes to be processed. |
1386 | 1394 |
/// |
1387 | 1395 |
/// \pre The stack must be empty. Otherwise the algorithm gives |
1388 | 1396 |
/// wrong results. (One of the outgoing arcs of all the source nodes |
1389 | 1397 |
/// except for the last one will not be visited and distances will |
1390 | 1398 |
/// also be wrong.) |
1391 | 1399 |
void addSource(Node s) |
1392 | 1400 |
{ |
1393 | 1401 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1394 | 1402 |
if(!(*_reached)[s]) { |
1395 | 1403 |
_reached->set(s,true); |
1396 | 1404 |
_visitor->start(s); |
1397 | 1405 |
_visitor->reach(s); |
1398 | 1406 |
Arc e; |
1399 | 1407 |
_digraph->firstOut(e, s); |
1400 | 1408 |
if (e != INVALID) { |
1401 | 1409 |
_stack[++_stack_head] = e; |
1402 | 1410 |
} else { |
1403 | 1411 |
_visitor->leave(s); |
1404 | 1412 |
_visitor->stop(s); |
1405 | 1413 |
} |
1406 | 1414 |
} |
1407 | 1415 |
} |
1408 | 1416 |
|
1409 | 1417 |
/// \brief Processes the next arc. |
1410 | 1418 |
/// |
1411 | 1419 |
/// Processes the next arc. |
1412 | 1420 |
/// |
1413 | 1421 |
/// \return The processed arc. |
1414 | 1422 |
/// |
1415 | 1423 |
/// \pre The stack must not be empty. |
1416 | 1424 |
Arc processNextArc() { |
1417 | 1425 |
Arc e = _stack[_stack_head]; |
1418 | 1426 |
Node m = _digraph->target(e); |
1419 | 1427 |
if(!(*_reached)[m]) { |
1420 | 1428 |
_visitor->discover(e); |
1421 | 1429 |
_visitor->reach(m); |
1422 | 1430 |
_reached->set(m, true); |
1423 | 1431 |
_digraph->firstOut(_stack[++_stack_head], m); |
1424 | 1432 |
} else { |
1425 | 1433 |
_visitor->examine(e); |
1426 | 1434 |
m = _digraph->source(e); |
1427 | 1435 |
_digraph->nextOut(_stack[_stack_head]); |
1428 | 1436 |
} |
1429 | 1437 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1430 | 1438 |
_visitor->leave(m); |
1431 | 1439 |
--_stack_head; |
1432 | 1440 |
if (_stack_head >= 0) { |
1433 | 1441 |
_visitor->backtrack(_stack[_stack_head]); |
1434 | 1442 |
m = _digraph->source(_stack[_stack_head]); |
1435 | 1443 |
_digraph->nextOut(_stack[_stack_head]); |
1436 | 1444 |
} else { |
... | ... |
@@ -3,384 +3,389 @@ |
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 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations and |
40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
41 | 41 |
template <typename V> |
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 | 43 |
/// \e |
44 | 44 |
typedef V Value; |
45 | 45 |
/// \brief Gives back the zero value of the type. |
46 | 46 |
static Value zero() { |
47 | 47 |
return static_cast<Value>(0); |
48 | 48 |
} |
49 | 49 |
/// \brief Gives back the sum of the given two elements. |
50 | 50 |
static Value plus(const Value& left, const Value& right) { |
51 | 51 |
return left + right; |
52 | 52 |
} |
53 | 53 |
/// \brief Gives back true only if the first value is less than the second. |
54 | 54 |
static bool less(const Value& left, const Value& right) { |
55 | 55 |
return left < right; |
56 | 56 |
} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
///Default traits class of Dijkstra class. |
60 | 60 |
|
61 | 61 |
///Default traits class of Dijkstra class. |
62 | 62 |
///\tparam GR The type of the digraph. |
63 | 63 |
///\tparam LEN The type of the length map. |
64 | 64 |
template<typename GR, typename LEN> |
65 | 65 |
struct DijkstraDefaultTraits |
66 | 66 |
{ |
67 | 67 |
///The type of the digraph the algorithm runs on. |
68 | 68 |
typedef GR Digraph; |
69 | 69 |
|
70 | 70 |
///The type of the map that stores the arc lengths. |
71 | 71 |
|
72 | 72 |
///The type of the map that stores the arc lengths. |
73 | 73 |
///It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
74 | 74 |
typedef LEN LengthMap; |
75 | 75 |
///The type of the arc lengths. |
76 | 76 |
typedef typename LEN::Value Value; |
77 | 77 |
|
78 | 78 |
/// Operation traits for %Dijkstra algorithm. |
79 | 79 |
|
80 | 80 |
/// This class defines the operations that are used in the algorithm. |
81 | 81 |
/// \see DijkstraDefaultOperationTraits |
82 | 82 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
83 | 83 |
|
84 | 84 |
/// The cross reference type used by the heap. |
85 | 85 |
|
86 | 86 |
/// The cross reference type used by the heap. |
87 | 87 |
/// Usually it is \c Digraph::NodeMap<int>. |
88 | 88 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
89 | 89 |
///Instantiates a \c HeapCrossRef. |
90 | 90 |
|
91 | 91 |
///This function instantiates a \ref HeapCrossRef. |
92 | 92 |
/// \param g is the digraph, to which we would like to define the |
93 | 93 |
/// \ref HeapCrossRef. |
94 | 94 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
95 | 95 |
{ |
96 | 96 |
return new HeapCrossRef(g); |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
///The heap type used by the %Dijkstra algorithm. |
100 | 100 |
|
101 | 101 |
///The heap type used by the Dijkstra algorithm. |
102 | 102 |
/// |
103 | 103 |
///\sa BinHeap |
104 | 104 |
///\sa Dijkstra |
105 | 105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
106 | 106 |
///Instantiates a \c Heap. |
107 | 107 |
|
108 | 108 |
///This function instantiates a \ref Heap. |
109 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
110 | 110 |
{ |
111 | 111 |
return new Heap(r); |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
///\brief The type of the map that stores the predecessor |
115 | 115 |
///arcs of the shortest paths. |
116 | 116 |
/// |
117 | 117 |
///The type of the map that stores the predecessor |
118 | 118 |
///arcs of the shortest paths. |
119 | 119 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
120 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
121 | 121 |
///Instantiates a \c PredMap. |
122 | 122 |
|
123 | 123 |
///This function instantiates a \ref PredMap. |
124 | 124 |
///\param g is the digraph, to which we would like to define the |
125 | 125 |
///\ref PredMap. |
126 | 126 |
static PredMap *createPredMap(const Digraph &g) |
127 | 127 |
{ |
128 | 128 |
return new PredMap(g); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
///The type of the map that indicates which nodes are processed. |
132 | 132 |
|
133 | 133 |
///The type of the map that indicates which nodes are processed. |
134 | 134 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
135 | 135 |
///By default, it is a NullMap. |
136 | 136 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
137 | 137 |
///Instantiates a \c ProcessedMap. |
138 | 138 |
|
139 | 139 |
///This function instantiates a \ref ProcessedMap. |
140 | 140 |
///\param g is the digraph, to which |
141 | 141 |
///we would like to define the \ref ProcessedMap. |
142 | 142 |
#ifdef DOXYGEN |
143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
144 | 144 |
#else |
145 | 145 |
static ProcessedMap *createProcessedMap(const Digraph &) |
146 | 146 |
#endif |
147 | 147 |
{ |
148 | 148 |
return new ProcessedMap(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
///The type of the map that stores the distances of the nodes. |
152 | 152 |
|
153 | 153 |
///The type of the map that stores the distances of the nodes. |
154 | 154 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
155 | 155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
156 | 156 |
///Instantiates a \c DistMap. |
157 | 157 |
|
158 | 158 |
///This function instantiates a \ref DistMap. |
159 | 159 |
///\param g is the digraph, to which we would like to define |
160 | 160 |
///the \ref DistMap. |
161 | 161 |
static DistMap *createDistMap(const Digraph &g) |
162 | 162 |
{ |
163 | 163 |
return new DistMap(g); |
164 | 164 |
} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
///%Dijkstra algorithm class. |
168 | 168 |
|
169 | 169 |
/// \ingroup shortest_path |
170 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
171 | 171 |
/// |
172 | 172 |
///The %Dijkstra algorithm solves the single-source shortest path problem |
173 | 173 |
///when all arc lengths are non-negative. If there are negative lengths, |
174 | 174 |
///the BellmanFord algorithm should be used instead. |
175 | 175 |
/// |
176 | 176 |
///The arc lengths are passed to the algorithm using a |
177 | 177 |
///\ref concepts::ReadMap "ReadMap", |
178 | 178 |
///so it is easy to change it to any kind of length. |
179 | 179 |
///The type of the length is determined by the |
180 | 180 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
181 | 181 |
///It is also possible to change the underlying priority heap. |
182 | 182 |
/// |
183 | 183 |
///There is also a \ref dijkstra() "function-type interface" for the |
184 | 184 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
185 | 185 |
///it can be used easier. |
186 | 186 |
/// |
187 | 187 |
///\tparam GR The type of the digraph the algorithm runs on. |
188 | 188 |
///The default type is \ref ListDigraph. |
189 | 189 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
190 | 190 |
///the lengths of the arcs. |
191 | 191 |
///It is read once for each arc, so the map may involve in |
192 | 192 |
///relatively time consuming process to compute the arc lengths if |
193 | 193 |
///it is necessary. The default map type is \ref |
194 | 194 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
195 |
///\tparam TR The traits class that defines various types used by the |
|
196 |
///algorithm. By default, it is \ref DijkstraDefaultTraits |
|
197 |
///"DijkstraDefaultTraits<GR, LEN>". |
|
198 |
///In most cases, this parameter should not be set directly, |
|
199 |
///consider to use the named template parameters instead. |
|
195 | 200 |
#ifdef DOXYGEN |
196 | 201 |
template <typename GR, typename LEN, typename TR> |
197 | 202 |
#else |
198 | 203 |
template <typename GR=ListDigraph, |
199 | 204 |
typename LEN=typename GR::template ArcMap<int>, |
200 | 205 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
201 | 206 |
#endif |
202 | 207 |
class Dijkstra { |
203 | 208 |
public: |
204 | 209 |
|
205 | 210 |
///The type of the digraph the algorithm runs on. |
206 | 211 |
typedef typename TR::Digraph Digraph; |
207 | 212 |
|
208 | 213 |
///The type of the arc lengths. |
209 | 214 |
typedef typename TR::Value Value; |
210 | 215 |
///The type of the map that stores the arc lengths. |
211 | 216 |
typedef typename TR::LengthMap LengthMap; |
212 | 217 |
///\brief The type of the map that stores the predecessor arcs of the |
213 | 218 |
///shortest paths. |
214 | 219 |
typedef typename TR::PredMap PredMap; |
215 | 220 |
///The type of the map that stores the distances of the nodes. |
216 | 221 |
typedef typename TR::DistMap DistMap; |
217 | 222 |
///The type of the map that indicates which nodes are processed. |
218 | 223 |
typedef typename TR::ProcessedMap ProcessedMap; |
219 | 224 |
///The type of the paths. |
220 | 225 |
typedef PredMapPath<Digraph, PredMap> Path; |
221 | 226 |
///The cross reference type used for the current heap. |
222 | 227 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
223 | 228 |
///The heap type used by the algorithm. |
224 | 229 |
typedef typename TR::Heap Heap; |
225 | 230 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
226 | 231 |
///of the algorithm. |
227 | 232 |
typedef typename TR::OperationTraits OperationTraits; |
228 | 233 |
|
229 | 234 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
230 | 235 |
typedef TR Traits; |
231 | 236 |
|
232 | 237 |
private: |
233 | 238 |
|
234 | 239 |
typedef typename Digraph::Node Node; |
235 | 240 |
typedef typename Digraph::NodeIt NodeIt; |
236 | 241 |
typedef typename Digraph::Arc Arc; |
237 | 242 |
typedef typename Digraph::OutArcIt OutArcIt; |
238 | 243 |
|
239 | 244 |
//Pointer to the underlying digraph. |
240 | 245 |
const Digraph *G; |
241 | 246 |
//Pointer to the length map. |
242 | 247 |
const LengthMap *_length; |
243 | 248 |
//Pointer to the map of predecessors arcs. |
244 | 249 |
PredMap *_pred; |
245 | 250 |
//Indicates if _pred is locally allocated (true) or not. |
246 | 251 |
bool local_pred; |
247 | 252 |
//Pointer to the map of distances. |
248 | 253 |
DistMap *_dist; |
249 | 254 |
//Indicates if _dist is locally allocated (true) or not. |
250 | 255 |
bool local_dist; |
251 | 256 |
//Pointer to the map of processed status of the nodes. |
252 | 257 |
ProcessedMap *_processed; |
253 | 258 |
//Indicates if _processed is locally allocated (true) or not. |
254 | 259 |
bool local_processed; |
255 | 260 |
//Pointer to the heap cross references. |
256 | 261 |
HeapCrossRef *_heap_cross_ref; |
257 | 262 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
258 | 263 |
bool local_heap_cross_ref; |
259 | 264 |
//Pointer to the heap. |
260 | 265 |
Heap *_heap; |
261 | 266 |
//Indicates if _heap is locally allocated (true) or not. |
262 | 267 |
bool local_heap; |
263 | 268 |
|
264 | 269 |
//Creates the maps if necessary. |
265 | 270 |
void create_maps() |
266 | 271 |
{ |
267 | 272 |
if(!_pred) { |
268 | 273 |
local_pred = true; |
269 | 274 |
_pred = Traits::createPredMap(*G); |
270 | 275 |
} |
271 | 276 |
if(!_dist) { |
272 | 277 |
local_dist = true; |
273 | 278 |
_dist = Traits::createDistMap(*G); |
274 | 279 |
} |
275 | 280 |
if(!_processed) { |
276 | 281 |
local_processed = true; |
277 | 282 |
_processed = Traits::createProcessedMap(*G); |
278 | 283 |
} |
279 | 284 |
if (!_heap_cross_ref) { |
280 | 285 |
local_heap_cross_ref = true; |
281 | 286 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
282 | 287 |
} |
283 | 288 |
if (!_heap) { |
284 | 289 |
local_heap = true; |
285 | 290 |
_heap = Traits::createHeap(*_heap_cross_ref); |
286 | 291 |
} |
287 | 292 |
} |
288 | 293 |
|
289 | 294 |
public: |
290 | 295 |
|
291 | 296 |
typedef Dijkstra Create; |
292 | 297 |
|
293 | 298 |
///\name Named Template Parameters |
294 | 299 |
|
295 | 300 |
///@{ |
296 | 301 |
|
297 | 302 |
template <class T> |
298 | 303 |
struct SetPredMapTraits : public Traits { |
299 | 304 |
typedef T PredMap; |
300 | 305 |
static PredMap *createPredMap(const Digraph &) |
301 | 306 |
{ |
302 | 307 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
303 | 308 |
return 0; // ignore warnings |
304 | 309 |
} |
305 | 310 |
}; |
306 | 311 |
///\brief \ref named-templ-param "Named parameter" for setting |
307 | 312 |
///\c PredMap type. |
308 | 313 |
/// |
309 | 314 |
///\ref named-templ-param "Named parameter" for setting |
310 | 315 |
///\c PredMap type. |
311 | 316 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
312 | 317 |
template <class T> |
313 | 318 |
struct SetPredMap |
314 | 319 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
315 | 320 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
316 | 321 |
}; |
317 | 322 |
|
318 | 323 |
template <class T> |
319 | 324 |
struct SetDistMapTraits : public Traits { |
320 | 325 |
typedef T DistMap; |
321 | 326 |
static DistMap *createDistMap(const Digraph &) |
322 | 327 |
{ |
323 | 328 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
324 | 329 |
return 0; // ignore warnings |
325 | 330 |
} |
326 | 331 |
}; |
327 | 332 |
///\brief \ref named-templ-param "Named parameter" for setting |
328 | 333 |
///\c DistMap type. |
329 | 334 |
/// |
330 | 335 |
///\ref named-templ-param "Named parameter" for setting |
331 | 336 |
///\c DistMap type. |
332 | 337 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
333 | 338 |
template <class T> |
334 | 339 |
struct SetDistMap |
335 | 340 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
336 | 341 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
337 | 342 |
}; |
338 | 343 |
|
339 | 344 |
template <class T> |
340 | 345 |
struct SetProcessedMapTraits : public Traits { |
341 | 346 |
typedef T ProcessedMap; |
342 | 347 |
static ProcessedMap *createProcessedMap(const Digraph &) |
343 | 348 |
{ |
344 | 349 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
345 | 350 |
return 0; // ignore warnings |
346 | 351 |
} |
347 | 352 |
}; |
348 | 353 |
///\brief \ref named-templ-param "Named parameter" for setting |
349 | 354 |
///\c ProcessedMap type. |
350 | 355 |
/// |
351 | 356 |
///\ref named-templ-param "Named parameter" for setting |
352 | 357 |
///\c ProcessedMap type. |
353 | 358 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
354 | 359 |
template <class T> |
355 | 360 |
struct SetProcessedMap |
356 | 361 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
357 | 362 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
358 | 363 |
}; |
359 | 364 |
|
360 | 365 |
struct SetStandardProcessedMapTraits : public Traits { |
361 | 366 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
362 | 367 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
363 | 368 |
{ |
364 | 369 |
return new ProcessedMap(g); |
365 | 370 |
} |
366 | 371 |
}; |
367 | 372 |
///\brief \ref named-templ-param "Named parameter" for setting |
368 | 373 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
369 | 374 |
/// |
370 | 375 |
///\ref named-templ-param "Named parameter" for setting |
371 | 376 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
372 | 377 |
///If you don't set it explicitly, it will be automatically allocated. |
373 | 378 |
struct SetStandardProcessedMap |
374 | 379 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
375 | 380 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
376 | 381 |
Create; |
377 | 382 |
}; |
378 | 383 |
|
379 | 384 |
template <class H, class CR> |
380 | 385 |
struct SetHeapTraits : public Traits { |
381 | 386 |
typedef CR HeapCrossRef; |
382 | 387 |
typedef H Heap; |
383 | 388 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
384 | 389 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
385 | 390 |
return 0; // ignore warnings |
386 | 391 |
} |
... | ... |
@@ -903,384 +908,387 @@ |
903 | 908 |
Heap::POST_HEAP; } |
904 | 909 |
|
905 | 910 |
///The current distance of the given node from the root(s). |
906 | 911 |
|
907 | 912 |
///Returns the current distance of the given node from the root(s). |
908 | 913 |
///It may be decreased in the following processes. |
909 | 914 |
/// |
910 | 915 |
///\pre Either \ref run(Node) "run()" or \ref init() |
911 | 916 |
///must be called before using this function and |
912 | 917 |
///node \c v must be reached but not necessarily processed. |
913 | 918 |
Value currentDist(Node v) const { |
914 | 919 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
915 | 920 |
} |
916 | 921 |
|
917 | 922 |
///@} |
918 | 923 |
}; |
919 | 924 |
|
920 | 925 |
|
921 | 926 |
///Default traits class of dijkstra() function. |
922 | 927 |
|
923 | 928 |
///Default traits class of dijkstra() function. |
924 | 929 |
///\tparam GR The type of the digraph. |
925 | 930 |
///\tparam LEN The type of the length map. |
926 | 931 |
template<class GR, class LEN> |
927 | 932 |
struct DijkstraWizardDefaultTraits |
928 | 933 |
{ |
929 | 934 |
///The type of the digraph the algorithm runs on. |
930 | 935 |
typedef GR Digraph; |
931 | 936 |
///The type of the map that stores the arc lengths. |
932 | 937 |
|
933 | 938 |
///The type of the map that stores the arc lengths. |
934 | 939 |
///It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
935 | 940 |
typedef LEN LengthMap; |
936 | 941 |
///The type of the arc lengths. |
937 | 942 |
typedef typename LEN::Value Value; |
938 | 943 |
|
939 | 944 |
/// Operation traits for Dijkstra algorithm. |
940 | 945 |
|
941 | 946 |
/// This class defines the operations that are used in the algorithm. |
942 | 947 |
/// \see DijkstraDefaultOperationTraits |
943 | 948 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
944 | 949 |
|
945 | 950 |
/// The cross reference type used by the heap. |
946 | 951 |
|
947 | 952 |
/// The cross reference type used by the heap. |
948 | 953 |
/// Usually it is \c Digraph::NodeMap<int>. |
949 | 954 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
950 | 955 |
///Instantiates a \ref HeapCrossRef. |
951 | 956 |
|
952 | 957 |
///This function instantiates a \ref HeapCrossRef. |
953 | 958 |
/// \param g is the digraph, to which we would like to define the |
954 | 959 |
/// HeapCrossRef. |
955 | 960 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
956 | 961 |
{ |
957 | 962 |
return new HeapCrossRef(g); |
958 | 963 |
} |
959 | 964 |
|
960 | 965 |
///The heap type used by the Dijkstra algorithm. |
961 | 966 |
|
962 | 967 |
///The heap type used by the Dijkstra algorithm. |
963 | 968 |
/// |
964 | 969 |
///\sa BinHeap |
965 | 970 |
///\sa Dijkstra |
966 | 971 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
967 | 972 |
std::less<Value> > Heap; |
968 | 973 |
|
969 | 974 |
///Instantiates a \ref Heap. |
970 | 975 |
|
971 | 976 |
///This function instantiates a \ref Heap. |
972 | 977 |
/// \param r is the HeapCrossRef which is used. |
973 | 978 |
static Heap *createHeap(HeapCrossRef& r) |
974 | 979 |
{ |
975 | 980 |
return new Heap(r); |
976 | 981 |
} |
977 | 982 |
|
978 | 983 |
///\brief The type of the map that stores the predecessor |
979 | 984 |
///arcs of the shortest paths. |
980 | 985 |
/// |
981 | 986 |
///The type of the map that stores the predecessor |
982 | 987 |
///arcs of the shortest paths. |
983 | 988 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
984 | 989 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
985 | 990 |
///Instantiates a PredMap. |
986 | 991 |
|
987 | 992 |
///This function instantiates a PredMap. |
988 | 993 |
///\param g is the digraph, to which we would like to define the |
989 | 994 |
///PredMap. |
990 | 995 |
static PredMap *createPredMap(const Digraph &g) |
991 | 996 |
{ |
992 | 997 |
return new PredMap(g); |
993 | 998 |
} |
994 | 999 |
|
995 | 1000 |
///The type of the map that indicates which nodes are processed. |
996 | 1001 |
|
997 | 1002 |
///The type of the map that indicates which nodes are processed. |
998 | 1003 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
999 | 1004 |
///By default, it is a NullMap. |
1000 | 1005 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1001 | 1006 |
///Instantiates a ProcessedMap. |
1002 | 1007 |
|
1003 | 1008 |
///This function instantiates a ProcessedMap. |
1004 | 1009 |
///\param g is the digraph, to which |
1005 | 1010 |
///we would like to define the ProcessedMap. |
1006 | 1011 |
#ifdef DOXYGEN |
1007 | 1012 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1008 | 1013 |
#else |
1009 | 1014 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1010 | 1015 |
#endif |
1011 | 1016 |
{ |
1012 | 1017 |
return new ProcessedMap(); |
1013 | 1018 |
} |
1014 | 1019 |
|
1015 | 1020 |
///The type of the map that stores the distances of the nodes. |
1016 | 1021 |
|
1017 | 1022 |
///The type of the map that stores the distances of the nodes. |
1018 | 1023 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
1019 | 1024 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
1020 | 1025 |
///Instantiates a DistMap. |
1021 | 1026 |
|
1022 | 1027 |
///This function instantiates a DistMap. |
1023 | 1028 |
///\param g is the digraph, to which we would like to define |
1024 | 1029 |
///the DistMap |
1025 | 1030 |
static DistMap *createDistMap(const Digraph &g) |
1026 | 1031 |
{ |
1027 | 1032 |
return new DistMap(g); |
1028 | 1033 |
} |
1029 | 1034 |
|
1030 | 1035 |
///The type of the shortest paths. |
1031 | 1036 |
|
1032 | 1037 |
///The type of the shortest paths. |
1033 | 1038 |
///It must conform to the \ref concepts::Path "Path" concept. |
1034 | 1039 |
typedef lemon::Path<Digraph> Path; |
1035 | 1040 |
}; |
1036 | 1041 |
|
1037 | 1042 |
/// Default traits class used by DijkstraWizard |
1038 | 1043 |
|
1039 | 1044 |
/// Default traits class used by DijkstraWizard. |
1040 | 1045 |
/// \tparam GR The type of the digraph. |
1041 | 1046 |
/// \tparam LEN The type of the length map. |
1042 | 1047 |
template<typename GR, typename LEN> |
1043 | 1048 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN> |
1044 | 1049 |
{ |
1045 | 1050 |
typedef DijkstraWizardDefaultTraits<GR,LEN> Base; |
1046 | 1051 |
protected: |
1047 | 1052 |
//The type of the nodes in the digraph. |
1048 | 1053 |
typedef typename Base::Digraph::Node Node; |
1049 | 1054 |
|
1050 | 1055 |
//Pointer to the digraph the algorithm runs on. |
1051 | 1056 |
void *_g; |
1052 | 1057 |
//Pointer to the length map. |
1053 | 1058 |
void *_length; |
1054 | 1059 |
//Pointer to the map of processed nodes. |
1055 | 1060 |
void *_processed; |
1056 | 1061 |
//Pointer to the map of predecessors arcs. |
1057 | 1062 |
void *_pred; |
1058 | 1063 |
//Pointer to the map of distances. |
1059 | 1064 |
void *_dist; |
1060 | 1065 |
//Pointer to the shortest path to the target node. |
1061 | 1066 |
void *_path; |
1062 | 1067 |
//Pointer to the distance of the target node. |
1063 | 1068 |
void *_di; |
1064 | 1069 |
|
1065 | 1070 |
public: |
1066 | 1071 |
/// Constructor. |
1067 | 1072 |
|
1068 | 1073 |
/// This constructor does not require parameters, therefore it initiates |
1069 | 1074 |
/// all of the attributes to \c 0. |
1070 | 1075 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1071 | 1076 |
_dist(0), _path(0), _di(0) {} |
1072 | 1077 |
|
1073 | 1078 |
/// Constructor. |
1074 | 1079 |
|
1075 | 1080 |
/// This constructor requires two parameters, |
1076 | 1081 |
/// others are initiated to \c 0. |
1077 | 1082 |
/// \param g The digraph the algorithm runs on. |
1078 | 1083 |
/// \param l The length map. |
1079 | 1084 |
DijkstraWizardBase(const GR &g,const LEN &l) : |
1080 | 1085 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1081 | 1086 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&l))), |
1082 | 1087 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1083 | 1088 |
|
1084 | 1089 |
}; |
1085 | 1090 |
|
1086 | 1091 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1087 | 1092 |
|
1088 | 1093 |
/// This auxiliary class is created to implement the |
1089 | 1094 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1090 | 1095 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
1091 | 1096 |
/// functions and features of the plain \ref Dijkstra. |
1092 | 1097 |
/// |
1093 | 1098 |
/// This class should only be used through the \ref dijkstra() function, |
1094 | 1099 |
/// which makes it easier to use the algorithm. |
1100 |
/// |
|
1101 |
/// \tparam TR The traits class that defines various types used by the |
|
1102 |
/// algorithm. |
|
1095 | 1103 |
template<class TR> |
1096 | 1104 |
class DijkstraWizard : public TR |
1097 | 1105 |
{ |
1098 | 1106 |
typedef TR Base; |
1099 | 1107 |
|
1100 | 1108 |
typedef typename TR::Digraph Digraph; |
1101 | 1109 |
|
1102 | 1110 |
typedef typename Digraph::Node Node; |
1103 | 1111 |
typedef typename Digraph::NodeIt NodeIt; |
1104 | 1112 |
typedef typename Digraph::Arc Arc; |
1105 | 1113 |
typedef typename Digraph::OutArcIt OutArcIt; |
1106 | 1114 |
|
1107 | 1115 |
typedef typename TR::LengthMap LengthMap; |
1108 | 1116 |
typedef typename LengthMap::Value Value; |
1109 | 1117 |
typedef typename TR::PredMap PredMap; |
1110 | 1118 |
typedef typename TR::DistMap DistMap; |
1111 | 1119 |
typedef typename TR::ProcessedMap ProcessedMap; |
1112 | 1120 |
typedef typename TR::Path Path; |
1113 | 1121 |
typedef typename TR::Heap Heap; |
1114 | 1122 |
|
1115 | 1123 |
public: |
1116 | 1124 |
|
1117 | 1125 |
/// Constructor. |
1118 | 1126 |
DijkstraWizard() : TR() {} |
1119 | 1127 |
|
1120 | 1128 |
/// Constructor that requires parameters. |
1121 | 1129 |
|
1122 | 1130 |
/// Constructor that requires parameters. |
1123 | 1131 |
/// These parameters will be the default values for the traits class. |
1124 | 1132 |
/// \param g The digraph the algorithm runs on. |
1125 | 1133 |
/// \param l The length map. |
1126 | 1134 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
1127 | 1135 |
TR(g,l) {} |
1128 | 1136 |
|
1129 | 1137 |
///Copy constructor |
1130 | 1138 |
DijkstraWizard(const TR &b) : TR(b) {} |
1131 | 1139 |
|
1132 | 1140 |
~DijkstraWizard() {} |
1133 | 1141 |
|
1134 | 1142 |
///Runs Dijkstra algorithm from the given source node. |
1135 | 1143 |
|
1136 | 1144 |
///This method runs %Dijkstra algorithm from the given source node |
1137 | 1145 |
///in order to compute the shortest path to each node. |
1138 | 1146 |
void run(Node s) |
1139 | 1147 |
{ |
1140 | 1148 |
Dijkstra<Digraph,LengthMap,TR> |
1141 | 1149 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1142 | 1150 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1143 | 1151 |
if (Base::_pred) |
1144 | 1152 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1145 | 1153 |
if (Base::_dist) |
1146 | 1154 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1147 | 1155 |
if (Base::_processed) |
1148 | 1156 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1149 | 1157 |
dijk.run(s); |
1150 | 1158 |
} |
1151 | 1159 |
|
1152 | 1160 |
///Finds the shortest path between \c s and \c t. |
1153 | 1161 |
|
1154 | 1162 |
///This method runs the %Dijkstra algorithm from node \c s |
1155 | 1163 |
///in order to compute the shortest path to node \c t |
1156 | 1164 |
///(it stops searching when \c t is processed). |
1157 | 1165 |
/// |
1158 | 1166 |
///\return \c true if \c t is reachable form \c s. |
1159 | 1167 |
bool run(Node s, Node t) |
1160 | 1168 |
{ |
1161 | 1169 |
Dijkstra<Digraph,LengthMap,TR> |
1162 | 1170 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1163 | 1171 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1164 | 1172 |
if (Base::_pred) |
1165 | 1173 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1166 | 1174 |
if (Base::_dist) |
1167 | 1175 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1168 | 1176 |
if (Base::_processed) |
1169 | 1177 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1170 | 1178 |
dijk.run(s,t); |
1171 | 1179 |
if (Base::_path) |
1172 | 1180 |
*reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
1173 | 1181 |
if (Base::_di) |
1174 | 1182 |
*reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
1175 | 1183 |
return dijk.reached(t); |
1176 | 1184 |
} |
1177 | 1185 |
|
1178 | 1186 |
template<class T> |
1179 | 1187 |
struct SetPredMapBase : public Base { |
1180 | 1188 |
typedef T PredMap; |
1181 | 1189 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1182 | 1190 |
SetPredMapBase(const TR &b) : TR(b) {} |
1183 | 1191 |
}; |
1184 | 1192 |
|
1185 | 1193 |
///\brief \ref named-templ-param "Named parameter" for setting |
1186 | 1194 |
///the predecessor map. |
1187 | 1195 |
/// |
1188 | 1196 |
///\ref named-templ-param "Named parameter" function for setting |
1189 | 1197 |
///the map that stores the predecessor arcs of the nodes. |
1190 | 1198 |
template<class T> |
1191 | 1199 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1192 | 1200 |
{ |
1193 | 1201 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1194 | 1202 |
return DijkstraWizard<SetPredMapBase<T> >(*this); |
1195 | 1203 |
} |
1196 | 1204 |
|
1197 | 1205 |
template<class T> |
1198 | 1206 |
struct SetDistMapBase : public Base { |
1199 | 1207 |
typedef T DistMap; |
1200 | 1208 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1201 | 1209 |
SetDistMapBase(const TR &b) : TR(b) {} |
1202 | 1210 |
}; |
1203 | 1211 |
|
1204 | 1212 |
///\brief \ref named-templ-param "Named parameter" for setting |
1205 | 1213 |
///the distance map. |
1206 | 1214 |
/// |
1207 | 1215 |
///\ref named-templ-param "Named parameter" function for setting |
1208 | 1216 |
///the map that stores the distances of the nodes calculated |
1209 | 1217 |
///by the algorithm. |
1210 | 1218 |
template<class T> |
1211 | 1219 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
1212 | 1220 |
{ |
1213 | 1221 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1214 | 1222 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
1215 | 1223 |
} |
1216 | 1224 |
|
1217 | 1225 |
template<class T> |
1218 | 1226 |
struct SetProcessedMapBase : public Base { |
1219 | 1227 |
typedef T ProcessedMap; |
1220 | 1228 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1221 | 1229 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1222 | 1230 |
}; |
1223 | 1231 |
|
1224 | 1232 |
///\brief \ref named-func-param "Named parameter" for setting |
1225 | 1233 |
///the processed map. |
1226 | 1234 |
/// |
1227 | 1235 |
///\ref named-templ-param "Named parameter" function for setting |
1228 | 1236 |
///the map that indicates which nodes are processed. |
1229 | 1237 |
template<class T> |
1230 | 1238 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1231 | 1239 |
{ |
1232 | 1240 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1233 | 1241 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1234 | 1242 |
} |
1235 | 1243 |
|
1236 | 1244 |
template<class T> |
1237 | 1245 |
struct SetPathBase : public Base { |
1238 | 1246 |
typedef T Path; |
1239 | 1247 |
SetPathBase(const TR &b) : TR(b) {} |
1240 | 1248 |
}; |
1241 | 1249 |
|
1242 | 1250 |
///\brief \ref named-func-param "Named parameter" |
1243 | 1251 |
///for getting the shortest path to the target node. |
1244 | 1252 |
/// |
1245 | 1253 |
///\ref named-func-param "Named parameter" |
1246 | 1254 |
///for getting the shortest path to the target node. |
1247 | 1255 |
template<class T> |
1248 | 1256 |
DijkstraWizard<SetPathBase<T> > path(const T &t) |
1249 | 1257 |
{ |
1250 | 1258 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1251 | 1259 |
return DijkstraWizard<SetPathBase<T> >(*this); |
1252 | 1260 |
} |
1253 | 1261 |
|
1254 | 1262 |
///\brief \ref named-func-param "Named parameter" |
1255 | 1263 |
///for getting the distance of the target node. |
1256 | 1264 |
/// |
1257 | 1265 |
///\ref named-func-param "Named parameter" |
1258 | 1266 |
///for getting the distance of the target node. |
1259 | 1267 |
DijkstraWizard dist(const Value &d) |
1260 | 1268 |
{ |
1261 | 1269 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1262 | 1270 |
return *this; |
1263 | 1271 |
} |
1264 | 1272 |
|
1265 | 1273 |
}; |
1266 | 1274 |
|
1267 | 1275 |
///Function-type interface for Dijkstra algorithm. |
1268 | 1276 |
|
1269 | 1277 |
/// \ingroup shortest_path |
1270 | 1278 |
///Function-type interface for Dijkstra algorithm. |
1271 | 1279 |
/// |
1272 | 1280 |
///This function also has several \ref named-func-param "named parameters", |
1273 | 1281 |
///they are declared as the members of class \ref DijkstraWizard. |
1274 | 1282 |
///The following examples show how to use these parameters. |
1275 | 1283 |
///\code |
1276 | 1284 |
/// // Compute shortest path from node s to each node |
1277 | 1285 |
/// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
1278 | 1286 |
/// |
1279 | 1287 |
/// // Compute shortest path from s to t |
1280 | 1288 |
/// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
1281 | 1289 |
///\endcode |
1282 | 1290 |
///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()" |
1283 | 1291 |
///to the end of the parameter list. |
1284 | 1292 |
///\sa DijkstraWizard |
1285 | 1293 |
///\sa Dijkstra |
1286 | 1294 |
template<typename GR, typename LEN> |
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_HARTMANN_ORLIN_H |
20 | 20 |
#define LEMON_HARTMANN_ORLIN_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of HartmannOrlin algorithm. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of HartmannOrlin algorithm. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam LEN The type of the length map. |
41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename LEN> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename LEN, |
46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct HartmannOrlinDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the length map |
53 | 53 |
typedef LEN LengthMap; |
54 | 54 |
/// The type of the arc lengths |
55 | 55 |
typedef typename LengthMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The large value type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large value type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct HartmannOrlinDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
97 | 97 |
/// a minimum mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
103 | 103 |
/// it applies an efficient early termination scheme. |
104 | 104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 | 107 |
/// \tparam LEN The type of the length map. The default |
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 |
/// \tparam TR The traits class that defines various types used by the |
|
110 |
/// algorithm. By default, it is \ref HartmannOrlinDefaultTraits |
|
111 |
/// "HartmannOrlinDefaultTraits<GR, LEN>". |
|
112 |
/// In most cases, this parameter should not be set directly, |
|
113 |
/// consider to use the named template parameters instead. |
|
109 | 114 |
#ifdef DOXYGEN |
110 | 115 |
template <typename GR, typename LEN, typename TR> |
111 | 116 |
#else |
112 | 117 |
template < typename GR, |
113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
114 | 119 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
115 | 120 |
#endif |
116 | 121 |
class HartmannOrlin |
117 | 122 |
{ |
118 | 123 |
public: |
119 | 124 |
|
120 | 125 |
/// The type of the digraph |
121 | 126 |
typedef typename TR::Digraph Digraph; |
122 | 127 |
/// The type of the length map |
123 | 128 |
typedef typename TR::LengthMap LengthMap; |
124 | 129 |
/// The type of the arc lengths |
125 | 130 |
typedef typename TR::Value Value; |
126 | 131 |
|
127 | 132 |
/// \brief The large value type |
128 | 133 |
/// |
129 | 134 |
/// The large value type used for internal computations. |
130 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
|
131 |
/// it is \c long \c long if the \c Value type is integer, |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
132 | 136 |
/// otherwise it is \c double. |
133 | 137 |
typedef typename TR::LargeValue LargeValue; |
134 | 138 |
|
135 | 139 |
/// The tolerance type |
136 | 140 |
typedef typename TR::Tolerance Tolerance; |
137 | 141 |
|
138 | 142 |
/// \brief The path type of the found cycles |
139 | 143 |
/// |
140 | 144 |
/// The path type of the found cycles. |
141 | 145 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
143 | 147 |
typedef typename TR::Path Path; |
144 | 148 |
|
145 | 149 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
146 | 150 |
typedef TR Traits; |
147 | 151 |
|
148 | 152 |
private: |
149 | 153 |
|
150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 155 |
|
152 | 156 |
// Data sturcture for path data |
153 | 157 |
struct PathData |
154 | 158 |
{ |
155 | 159 |
LargeValue dist; |
156 | 160 |
Arc pred; |
157 | 161 |
PathData(LargeValue d, Arc p = INVALID) : |
158 | 162 |
dist(d), pred(p) {} |
159 | 163 |
}; |
160 | 164 |
|
161 | 165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
162 | 166 |
PathDataNodeMap; |
163 | 167 |
|
164 | 168 |
private: |
165 | 169 |
|
166 | 170 |
// The digraph the algorithm runs on |
167 | 171 |
const Digraph &_gr; |
168 | 172 |
// The length of the arcs |
169 | 173 |
const LengthMap &_length; |
170 | 174 |
|
171 | 175 |
// Data for storing the strongly connected components |
172 | 176 |
int _comp_num; |
173 | 177 |
typename Digraph::template NodeMap<int> _comp; |
174 | 178 |
std::vector<std::vector<Node> > _comp_nodes; |
175 | 179 |
std::vector<Node>* _nodes; |
176 | 180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
177 | 181 |
|
178 | 182 |
// Data for the found cycles |
179 | 183 |
bool _curr_found, _best_found; |
180 | 184 |
LargeValue _curr_length, _best_length; |
181 | 185 |
int _curr_size, _best_size; |
182 | 186 |
Node _curr_node, _best_node; |
183 | 187 |
int _curr_level, _best_level; |
184 | 188 |
|
185 | 189 |
Path *_cycle_path; |
186 | 190 |
bool _local_path; |
187 | 191 |
|
188 | 192 |
// Node map for storing path data |
189 | 193 |
PathDataNodeMap _data; |
190 | 194 |
// The processed nodes in the last round |
191 | 195 |
std::vector<Node> _process; |
192 | 196 |
|
193 | 197 |
Tolerance _tolerance; |
194 | 198 |
|
195 | 199 |
// Infinite constant |
196 | 200 |
const LargeValue INF; |
197 | 201 |
|
198 | 202 |
public: |
199 | 203 |
|
200 | 204 |
/// \name Named Template Parameters |
201 | 205 |
/// @{ |
202 | 206 |
|
203 | 207 |
template <typename T> |
204 | 208 |
struct SetLargeValueTraits : public Traits { |
205 | 209 |
typedef T LargeValue; |
206 | 210 |
typedef lemon::Tolerance<T> Tolerance; |
207 | 211 |
}; |
208 | 212 |
|
209 | 213 |
/// \brief \ref named-templ-param "Named parameter" for setting |
210 | 214 |
/// \c LargeValue type. |
211 | 215 |
/// |
212 | 216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
213 | 217 |
/// type. It is used for internal computations in the algorithm. |
214 | 218 |
template <typename T> |
215 | 219 |
struct SetLargeValue |
216 | 220 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > { |
217 | 221 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
218 | 222 |
}; |
219 | 223 |
|
220 | 224 |
template <typename T> |
221 | 225 |
struct SetPathTraits : public Traits { |
222 | 226 |
typedef T Path; |
223 | 227 |
}; |
224 | 228 |
|
225 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
226 | 230 |
/// \c %Path type. |
227 | 231 |
/// |
228 | 232 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
229 | 233 |
/// type of the found cycles. |
230 | 234 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
231 | 235 |
/// and it must have an \c addFront() function. |
232 | 236 |
template <typename T> |
233 | 237 |
struct SetPath |
234 | 238 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > { |
235 | 239 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
236 | 240 |
}; |
237 | 241 |
|
238 | 242 |
/// @} |
239 | 243 |
|
240 | 244 |
public: |
241 | 245 |
|
242 | 246 |
/// \brief Constructor. |
243 | 247 |
/// |
244 | 248 |
/// The constructor of the class. |
245 | 249 |
/// |
246 | 250 |
/// \param digraph The digraph the algorithm runs on. |
247 | 251 |
/// \param length The lengths (costs) of the arcs. |
248 | 252 |
HartmannOrlin( const Digraph &digraph, |
249 | 253 |
const LengthMap &length ) : |
250 | 254 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
251 | 255 |
_best_found(false), _best_length(0), _best_size(1), |
252 | 256 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
253 | 257 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
254 | 258 |
std::numeric_limits<LargeValue>::infinity() : |
255 | 259 |
std::numeric_limits<LargeValue>::max()) |
256 | 260 |
{} |
257 | 261 |
|
258 | 262 |
/// Destructor. |
259 | 263 |
~HartmannOrlin() { |
260 | 264 |
if (_local_path) delete _cycle_path; |
261 | 265 |
} |
262 | 266 |
|
263 | 267 |
/// \brief Set the path structure for storing the found cycle. |
264 | 268 |
/// |
265 | 269 |
/// This function sets an external path structure for storing the |
266 | 270 |
/// found cycle. |
267 | 271 |
/// |
268 | 272 |
/// If you don't call this function before calling \ref run() or |
269 | 273 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
270 | 274 |
/// structure. The destuctor deallocates this automatically |
271 | 275 |
/// allocated object, of course. |
272 | 276 |
/// |
273 | 277 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
274 | 278 |
/// "addFront()" function of the given path structure. |
275 | 279 |
/// |
276 | 280 |
/// \return <tt>(*this)</tt> |
277 | 281 |
HartmannOrlin& cycle(Path &path) { |
278 | 282 |
if (_local_path) { |
279 | 283 |
delete _cycle_path; |
280 | 284 |
_local_path = false; |
281 | 285 |
} |
282 | 286 |
_cycle_path = &path; |
283 | 287 |
return *this; |
284 | 288 |
} |
285 | 289 |
|
286 | 290 |
/// \brief Set the tolerance used by the algorithm. |
287 | 291 |
/// |
288 | 292 |
/// This function sets the tolerance object used by the algorithm. |
289 | 293 |
/// |
290 | 294 |
/// \return <tt>(*this)</tt> |
291 | 295 |
HartmannOrlin& tolerance(const Tolerance& tolerance) { |
292 | 296 |
_tolerance = tolerance; |
293 | 297 |
return *this; |
294 | 298 |
} |
295 | 299 |
|
296 | 300 |
/// \brief Return a const reference to the tolerance. |
297 | 301 |
/// |
298 | 302 |
/// This function returns a const reference to the tolerance object |
299 | 303 |
/// used by the algorithm. |
300 | 304 |
const Tolerance& tolerance() const { |
301 | 305 |
return _tolerance; |
302 | 306 |
} |
303 | 307 |
|
304 | 308 |
/// \name Execution control |
305 | 309 |
/// The simplest way to execute the algorithm is to call the \ref run() |
306 | 310 |
/// function.\n |
307 | 311 |
/// If you only need the minimum mean length, you may call |
308 | 312 |
/// \ref findMinMean(). |
309 | 313 |
|
310 | 314 |
/// @{ |
311 | 315 |
|
312 | 316 |
/// \brief Run the algorithm. |
313 | 317 |
/// |
314 | 318 |
/// This function runs the algorithm. |
315 | 319 |
/// It can be called more than once (e.g. if the underlying digraph |
316 | 320 |
/// and/or the arc lengths have been modified). |
317 | 321 |
/// |
318 | 322 |
/// \return \c true if a directed cycle exists in the digraph. |
319 | 323 |
/// |
320 | 324 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
321 | 325 |
/// \code |
322 | 326 |
/// return mmc.findMinMean() && mmc.findCycle(); |
323 | 327 |
/// \endcode |
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_HOWARD_H |
20 | 20 |
#define LEMON_HOWARD_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of Howard class. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of Howard class. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam LEN The type of the length map. |
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename LEN> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename LEN, |
46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct HowardDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the length map |
53 | 53 |
typedef LEN LengthMap; |
54 | 54 |
/// The type of the arc lengths |
55 | 55 |
typedef typename LengthMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The large value type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large value type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addBack() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct HowardDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// This class provides the most efficient algorithm for the |
103 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
104 | 104 |
/// bound on its running time is exponential. |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 | 107 |
/// \tparam LEN The type of the length map. The default |
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 |
/// \tparam TR The traits class that defines various types used by the |
|
110 |
/// algorithm. By default, it is \ref HowardDefaultTraits |
|
111 |
/// "HowardDefaultTraits<GR, LEN>". |
|
112 |
/// In most cases, this parameter should not be set directly, |
|
113 |
/// consider to use the named template parameters instead. |
|
109 | 114 |
#ifdef DOXYGEN |
110 | 115 |
template <typename GR, typename LEN, typename TR> |
111 | 116 |
#else |
112 | 117 |
template < typename GR, |
113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
114 | 119 |
typename TR = HowardDefaultTraits<GR, LEN> > |
115 | 120 |
#endif |
116 | 121 |
class Howard |
117 | 122 |
{ |
118 | 123 |
public: |
119 | 124 |
|
120 | 125 |
/// The type of the digraph |
121 | 126 |
typedef typename TR::Digraph Digraph; |
122 | 127 |
/// The type of the length map |
123 | 128 |
typedef typename TR::LengthMap LengthMap; |
124 | 129 |
/// The type of the arc lengths |
125 | 130 |
typedef typename TR::Value Value; |
126 | 131 |
|
127 | 132 |
/// \brief The large value type |
128 | 133 |
/// |
129 | 134 |
/// The large value type used for internal computations. |
130 |
/// Using the \ref HowardDefaultTraits "default traits class", |
|
131 |
/// it is \c long \c long if the \c Value type is integer, |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
132 | 136 |
/// otherwise it is \c double. |
133 | 137 |
typedef typename TR::LargeValue LargeValue; |
134 | 138 |
|
135 | 139 |
/// The tolerance type |
136 | 140 |
typedef typename TR::Tolerance Tolerance; |
137 | 141 |
|
138 | 142 |
/// \brief The path type of the found cycles |
139 | 143 |
/// |
140 | 144 |
/// The path type of the found cycles. |
141 | 145 |
/// Using the \ref HowardDefaultTraits "default traits class", |
142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
143 | 147 |
typedef typename TR::Path Path; |
144 | 148 |
|
145 | 149 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
146 | 150 |
typedef TR Traits; |
147 | 151 |
|
148 | 152 |
private: |
149 | 153 |
|
150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 155 |
|
152 | 156 |
// The digraph the algorithm runs on |
153 | 157 |
const Digraph &_gr; |
154 | 158 |
// The length of the arcs |
155 | 159 |
const LengthMap &_length; |
156 | 160 |
|
157 | 161 |
// Data for the found cycles |
158 | 162 |
bool _curr_found, _best_found; |
159 | 163 |
LargeValue _curr_length, _best_length; |
160 | 164 |
int _curr_size, _best_size; |
161 | 165 |
Node _curr_node, _best_node; |
162 | 166 |
|
163 | 167 |
Path *_cycle_path; |
164 | 168 |
bool _local_path; |
165 | 169 |
|
166 | 170 |
// Internal data used by the algorithm |
167 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
168 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
169 | 173 |
typename Digraph::template NodeMap<int> _level; |
170 | 174 |
typename Digraph::template NodeMap<LargeValue> _dist; |
171 | 175 |
|
172 | 176 |
// Data for storing the strongly connected components |
173 | 177 |
int _comp_num; |
174 | 178 |
typename Digraph::template NodeMap<int> _comp; |
175 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
176 | 180 |
std::vector<Node>* _nodes; |
177 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
178 | 182 |
|
179 | 183 |
// Queue used for BFS search |
180 | 184 |
std::vector<Node> _queue; |
181 | 185 |
int _qfront, _qback; |
182 | 186 |
|
183 | 187 |
Tolerance _tolerance; |
184 | 188 |
|
185 | 189 |
// Infinite constant |
186 | 190 |
const LargeValue INF; |
187 | 191 |
|
188 | 192 |
public: |
189 | 193 |
|
190 | 194 |
/// \name Named Template Parameters |
191 | 195 |
/// @{ |
192 | 196 |
|
193 | 197 |
template <typename T> |
194 | 198 |
struct SetLargeValueTraits : public Traits { |
195 | 199 |
typedef T LargeValue; |
196 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
197 | 201 |
}; |
198 | 202 |
|
199 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
200 | 204 |
/// \c LargeValue type. |
201 | 205 |
/// |
202 | 206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
203 | 207 |
/// type. It is used for internal computations in the algorithm. |
204 | 208 |
template <typename T> |
205 | 209 |
struct SetLargeValue |
206 | 210 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > { |
207 | 211 |
typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create; |
208 | 212 |
}; |
209 | 213 |
|
210 | 214 |
template <typename T> |
211 | 215 |
struct SetPathTraits : public Traits { |
212 | 216 |
typedef T Path; |
213 | 217 |
}; |
214 | 218 |
|
215 | 219 |
/// \brief \ref named-templ-param "Named parameter" for setting |
216 | 220 |
/// \c %Path type. |
217 | 221 |
/// |
218 | 222 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
219 | 223 |
/// type of the found cycles. |
220 | 224 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
221 | 225 |
/// and it must have an \c addBack() function. |
222 | 226 |
template <typename T> |
223 | 227 |
struct SetPath |
224 | 228 |
: public Howard<GR, LEN, SetPathTraits<T> > { |
225 | 229 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
226 | 230 |
}; |
227 | 231 |
|
228 | 232 |
/// @} |
229 | 233 |
|
230 | 234 |
public: |
231 | 235 |
|
232 | 236 |
/// \brief Constructor. |
233 | 237 |
/// |
234 | 238 |
/// The constructor of the class. |
235 | 239 |
/// |
236 | 240 |
/// \param digraph The digraph the algorithm runs on. |
237 | 241 |
/// \param length The lengths (costs) of the arcs. |
238 | 242 |
Howard( const Digraph &digraph, |
239 | 243 |
const LengthMap &length ) : |
240 | 244 |
_gr(digraph), _length(length), _best_found(false), |
241 | 245 |
_best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
242 | 246 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
243 | 247 |
_comp(digraph), _in_arcs(digraph), |
244 | 248 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
245 | 249 |
std::numeric_limits<LargeValue>::infinity() : |
246 | 250 |
std::numeric_limits<LargeValue>::max()) |
247 | 251 |
{} |
248 | 252 |
|
249 | 253 |
/// Destructor. |
250 | 254 |
~Howard() { |
251 | 255 |
if (_local_path) delete _cycle_path; |
252 | 256 |
} |
253 | 257 |
|
254 | 258 |
/// \brief Set the path structure for storing the found cycle. |
255 | 259 |
/// |
256 | 260 |
/// This function sets an external path structure for storing the |
257 | 261 |
/// found cycle. |
258 | 262 |
/// |
259 | 263 |
/// If you don't call this function before calling \ref run() or |
260 | 264 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
261 | 265 |
/// structure. The destuctor deallocates this automatically |
262 | 266 |
/// allocated object, of course. |
263 | 267 |
/// |
264 | 268 |
/// \note The algorithm calls only the \ref lemon::Path::addBack() |
265 | 269 |
/// "addBack()" function of the given path structure. |
266 | 270 |
/// |
267 | 271 |
/// \return <tt>(*this)</tt> |
268 | 272 |
Howard& cycle(Path &path) { |
269 | 273 |
if (_local_path) { |
270 | 274 |
delete _cycle_path; |
271 | 275 |
_local_path = false; |
272 | 276 |
} |
273 | 277 |
_cycle_path = &path; |
274 | 278 |
return *this; |
275 | 279 |
} |
276 | 280 |
|
277 | 281 |
/// \brief Set the tolerance used by the algorithm. |
278 | 282 |
/// |
279 | 283 |
/// This function sets the tolerance object used by the algorithm. |
280 | 284 |
/// |
281 | 285 |
/// \return <tt>(*this)</tt> |
282 | 286 |
Howard& tolerance(const Tolerance& tolerance) { |
283 | 287 |
_tolerance = tolerance; |
284 | 288 |
return *this; |
285 | 289 |
} |
286 | 290 |
|
287 | 291 |
/// \brief Return a const reference to the tolerance. |
288 | 292 |
/// |
289 | 293 |
/// This function returns a const reference to the tolerance object |
290 | 294 |
/// used by the algorithm. |
291 | 295 |
const Tolerance& tolerance() const { |
292 | 296 |
return _tolerance; |
293 | 297 |
} |
294 | 298 |
|
295 | 299 |
/// \name Execution control |
296 | 300 |
/// The simplest way to execute the algorithm is to call the \ref run() |
297 | 301 |
/// function.\n |
298 | 302 |
/// If you only need the minimum mean length, you may call |
299 | 303 |
/// \ref findMinMean(). |
300 | 304 |
|
301 | 305 |
/// @{ |
302 | 306 |
|
303 | 307 |
/// \brief Run the algorithm. |
304 | 308 |
/// |
305 | 309 |
/// This function runs the algorithm. |
306 | 310 |
/// It can be called more than once (e.g. if the underlying digraph |
307 | 311 |
/// and/or the arc lengths have been modified). |
308 | 312 |
/// |
309 | 313 |
/// \return \c true if a directed cycle exists in the digraph. |
310 | 314 |
/// |
311 | 315 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
312 | 316 |
/// \code |
313 | 317 |
/// return mmc.findMinMean() && mmc.findCycle(); |
314 | 318 |
/// \endcode |
315 | 319 |
bool run() { |
316 | 320 |
return findMinMean() && findCycle(); |
317 | 321 |
} |
318 | 322 |
|
319 | 323 |
/// \brief Find the minimum cycle mean. |
320 | 324 |
/// |
321 | 325 |
/// This function finds the minimum mean length of the directed |
322 | 326 |
/// cycles in the digraph. |
323 | 327 |
/// |
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_KARP_H |
20 | 20 |
#define LEMON_KARP_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of Karp algorithm. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of Karp algorithm. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam LEN The type of the length map. |
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename LEN> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename LEN, |
46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct KarpDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the length map |
53 | 53 |
typedef LEN LengthMap; |
54 | 54 |
/// The type of the arc lengths |
55 | 55 |
typedef typename LengthMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The large value type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large value type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct KarpDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
100 | 100 |
/// cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
103 | 103 |
/// |
104 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
105 | 105 |
/// \tparam LEN The type of the length map. The default |
106 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
107 |
/// \tparam TR The traits class that defines various types used by the |
|
108 |
/// algorithm. By default, it is \ref KarpDefaultTraits |
|
109 |
/// "KarpDefaultTraits<GR, LEN>". |
|
110 |
/// In most cases, this parameter should not be set directly, |
|
111 |
/// consider to use the named template parameters instead. |
|
107 | 112 |
#ifdef DOXYGEN |
108 | 113 |
template <typename GR, typename LEN, typename TR> |
109 | 114 |
#else |
110 | 115 |
template < typename GR, |
111 | 116 |
typename LEN = typename GR::template ArcMap<int>, |
112 | 117 |
typename TR = KarpDefaultTraits<GR, LEN> > |
113 | 118 |
#endif |
114 | 119 |
class Karp |
115 | 120 |
{ |
116 | 121 |
public: |
117 | 122 |
|
118 | 123 |
/// The type of the digraph |
119 | 124 |
typedef typename TR::Digraph Digraph; |
120 | 125 |
/// The type of the length map |
121 | 126 |
typedef typename TR::LengthMap LengthMap; |
122 | 127 |
/// The type of the arc lengths |
123 | 128 |
typedef typename TR::Value Value; |
124 | 129 |
|
125 | 130 |
/// \brief The large value type |
126 | 131 |
/// |
127 | 132 |
/// The large value type used for internal computations. |
128 |
/// Using the \ref KarpDefaultTraits "default traits class", |
|
129 |
/// it is \c long \c long if the \c Value type is integer, |
|
133 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
130 | 134 |
/// otherwise it is \c double. |
131 | 135 |
typedef typename TR::LargeValue LargeValue; |
132 | 136 |
|
133 | 137 |
/// The tolerance type |
134 | 138 |
typedef typename TR::Tolerance Tolerance; |
135 | 139 |
|
136 | 140 |
/// \brief The path type of the found cycles |
137 | 141 |
/// |
138 | 142 |
/// The path type of the found cycles. |
139 | 143 |
/// Using the \ref KarpDefaultTraits "default traits class", |
140 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
141 | 145 |
typedef typename TR::Path Path; |
142 | 146 |
|
143 | 147 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
144 | 148 |
typedef TR Traits; |
145 | 149 |
|
146 | 150 |
private: |
147 | 151 |
|
148 | 152 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
149 | 153 |
|
150 | 154 |
// Data sturcture for path data |
151 | 155 |
struct PathData |
152 | 156 |
{ |
153 | 157 |
LargeValue dist; |
154 | 158 |
Arc pred; |
155 | 159 |
PathData(LargeValue d, Arc p = INVALID) : |
156 | 160 |
dist(d), pred(p) {} |
157 | 161 |
}; |
158 | 162 |
|
159 | 163 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
160 | 164 |
PathDataNodeMap; |
161 | 165 |
|
162 | 166 |
private: |
163 | 167 |
|
164 | 168 |
// The digraph the algorithm runs on |
165 | 169 |
const Digraph &_gr; |
166 | 170 |
// The length of the arcs |
167 | 171 |
const LengthMap &_length; |
168 | 172 |
|
169 | 173 |
// Data for storing the strongly connected components |
170 | 174 |
int _comp_num; |
171 | 175 |
typename Digraph::template NodeMap<int> _comp; |
172 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
173 | 177 |
std::vector<Node>* _nodes; |
174 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
175 | 179 |
|
176 | 180 |
// Data for the found cycle |
177 | 181 |
LargeValue _cycle_length; |
178 | 182 |
int _cycle_size; |
179 | 183 |
Node _cycle_node; |
180 | 184 |
|
181 | 185 |
Path *_cycle_path; |
182 | 186 |
bool _local_path; |
183 | 187 |
|
184 | 188 |
// Node map for storing path data |
185 | 189 |
PathDataNodeMap _data; |
186 | 190 |
// The processed nodes in the last round |
187 | 191 |
std::vector<Node> _process; |
188 | 192 |
|
189 | 193 |
Tolerance _tolerance; |
190 | 194 |
|
191 | 195 |
// Infinite constant |
192 | 196 |
const LargeValue INF; |
193 | 197 |
|
194 | 198 |
public: |
195 | 199 |
|
196 | 200 |
/// \name Named Template Parameters |
197 | 201 |
/// @{ |
198 | 202 |
|
199 | 203 |
template <typename T> |
200 | 204 |
struct SetLargeValueTraits : public Traits { |
201 | 205 |
typedef T LargeValue; |
202 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
203 | 207 |
}; |
204 | 208 |
|
205 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
206 | 210 |
/// \c LargeValue type. |
207 | 211 |
/// |
208 | 212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
209 | 213 |
/// type. It is used for internal computations in the algorithm. |
210 | 214 |
template <typename T> |
211 | 215 |
struct SetLargeValue |
212 | 216 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > { |
213 | 217 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
214 | 218 |
}; |
215 | 219 |
|
216 | 220 |
template <typename T> |
217 | 221 |
struct SetPathTraits : public Traits { |
218 | 222 |
typedef T Path; |
219 | 223 |
}; |
220 | 224 |
|
221 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
222 | 226 |
/// \c %Path type. |
223 | 227 |
/// |
224 | 228 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
225 | 229 |
/// type of the found cycles. |
226 | 230 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
227 | 231 |
/// and it must have an \c addFront() function. |
228 | 232 |
template <typename T> |
229 | 233 |
struct SetPath |
230 | 234 |
: public Karp<GR, LEN, SetPathTraits<T> > { |
231 | 235 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
232 | 236 |
}; |
233 | 237 |
|
234 | 238 |
/// @} |
235 | 239 |
|
236 | 240 |
public: |
237 | 241 |
|
238 | 242 |
/// \brief Constructor. |
239 | 243 |
/// |
240 | 244 |
/// The constructor of the class. |
241 | 245 |
/// |
242 | 246 |
/// \param digraph The digraph the algorithm runs on. |
243 | 247 |
/// \param length The lengths (costs) of the arcs. |
244 | 248 |
Karp( const Digraph &digraph, |
245 | 249 |
const LengthMap &length ) : |
246 | 250 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
247 | 251 |
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID), |
248 | 252 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
249 | 253 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
250 | 254 |
std::numeric_limits<LargeValue>::infinity() : |
251 | 255 |
std::numeric_limits<LargeValue>::max()) |
252 | 256 |
{} |
253 | 257 |
|
254 | 258 |
/// Destructor. |
255 | 259 |
~Karp() { |
256 | 260 |
if (_local_path) delete _cycle_path; |
257 | 261 |
} |
258 | 262 |
|
259 | 263 |
/// \brief Set the path structure for storing the found cycle. |
260 | 264 |
/// |
261 | 265 |
/// This function sets an external path structure for storing the |
262 | 266 |
/// found cycle. |
263 | 267 |
/// |
264 | 268 |
/// If you don't call this function before calling \ref run() or |
265 | 269 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
266 | 270 |
/// structure. The destuctor deallocates this automatically |
267 | 271 |
/// allocated object, of course. |
268 | 272 |
/// |
269 | 273 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
270 | 274 |
/// "addFront()" function of the given path structure. |
271 | 275 |
/// |
272 | 276 |
/// \return <tt>(*this)</tt> |
273 | 277 |
Karp& cycle(Path &path) { |
274 | 278 |
if (_local_path) { |
275 | 279 |
delete _cycle_path; |
276 | 280 |
_local_path = false; |
277 | 281 |
} |
278 | 282 |
_cycle_path = &path; |
279 | 283 |
return *this; |
280 | 284 |
} |
281 | 285 |
|
282 | 286 |
/// \brief Set the tolerance used by the algorithm. |
283 | 287 |
/// |
284 | 288 |
/// This function sets the tolerance object used by the algorithm. |
285 | 289 |
/// |
286 | 290 |
/// \return <tt>(*this)</tt> |
287 | 291 |
Karp& tolerance(const Tolerance& tolerance) { |
288 | 292 |
_tolerance = tolerance; |
289 | 293 |
return *this; |
290 | 294 |
} |
291 | 295 |
|
292 | 296 |
/// \brief Return a const reference to the tolerance. |
293 | 297 |
/// |
294 | 298 |
/// This function returns a const reference to the tolerance object |
295 | 299 |
/// used by the algorithm. |
296 | 300 |
const Tolerance& tolerance() const { |
297 | 301 |
return _tolerance; |
298 | 302 |
} |
299 | 303 |
|
300 | 304 |
/// \name Execution control |
301 | 305 |
/// The simplest way to execute the algorithm is to call the \ref run() |
302 | 306 |
/// function.\n |
303 | 307 |
/// If you only need the minimum mean length, you may call |
304 | 308 |
/// \ref findMinMean(). |
305 | 309 |
|
306 | 310 |
/// @{ |
307 | 311 |
|
308 | 312 |
/// \brief Run the algorithm. |
309 | 313 |
/// |
310 | 314 |
/// This function runs the algorithm. |
311 | 315 |
/// It can be called more than once (e.g. if the underlying digraph |
312 | 316 |
/// and/or the arc lengths have been modified). |
313 | 317 |
/// |
314 | 318 |
/// \return \c true if a directed cycle exists in the digraph. |
315 | 319 |
/// |
316 | 320 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
317 | 321 |
/// \code |
318 | 322 |
/// return mmc.findMinMean() && mmc.findCycle(); |
319 | 323 |
/// \endcode |
320 | 324 |
bool run() { |
321 | 325 |
return findMinMean() && findCycle(); |
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-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_MIN_COST_ARBORESCENCE_H |
20 | 20 |
#define LEMON_MIN_COST_ARBORESCENCE_H |
21 | 21 |
|
22 | 22 |
///\ingroup spantree |
23 | 23 |
///\file |
24 | 24 |
///\brief Minimum Cost Arborescence algorithm. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 | 28 |
#include <lemon/list_graph.h> |
29 | 29 |
#include <lemon/bin_heap.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
/// \brief Default traits class for MinCostArborescence class. |
36 | 36 |
/// |
37 | 37 |
/// Default traits class for MinCostArborescence class. |
38 | 38 |
/// \param GR Digraph type. |
39 | 39 |
/// \param CM Type of the cost map. |
40 | 40 |
template <class GR, class CM> |
41 | 41 |
struct MinCostArborescenceDefaultTraits{ |
42 | 42 |
|
43 | 43 |
/// \brief The digraph type the algorithm runs on. |
44 | 44 |
typedef GR Digraph; |
45 | 45 |
|
46 | 46 |
/// \brief The type of the map that stores the arc costs. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the arc costs. |
49 | 49 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
50 | 50 |
typedef CM CostMap; |
51 | 51 |
|
52 | 52 |
/// \brief The value type of the costs. |
53 | 53 |
/// |
54 | 54 |
/// The value type of the costs. |
55 | 55 |
typedef typename CostMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The type of the map that stores which arcs are in the |
58 | 58 |
/// arborescence. |
59 | 59 |
/// |
60 | 60 |
/// The type of the map that stores which arcs are in the |
61 | 61 |
/// arborescence. It must conform to the \ref concepts::WriteMap |
62 | 62 |
/// "WriteMap" concept, and its value type must be \c bool |
63 | 63 |
/// (or convertible). Initially it will be set to \c false on each |
64 | 64 |
/// arc, then it will be set on each arborescence arc once. |
65 | 65 |
typedef typename Digraph::template ArcMap<bool> ArborescenceMap; |
66 | 66 |
|
67 | 67 |
/// \brief Instantiates a \c ArborescenceMap. |
68 | 68 |
/// |
69 | 69 |
/// This function instantiates a \c ArborescenceMap. |
70 | 70 |
/// \param digraph The digraph to which we would like to calculate |
71 | 71 |
/// the \c ArborescenceMap. |
72 | 72 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){ |
73 | 73 |
return new ArborescenceMap(digraph); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief The type of the \c PredMap |
77 | 77 |
/// |
78 | 78 |
/// The type of the \c PredMap. It must confrom to the |
79 | 79 |
/// \ref concepts::WriteMap "WriteMap" concept, and its value type |
80 | 80 |
/// must be the \c Arc type of the digraph. |
81 | 81 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
82 | 82 |
|
83 | 83 |
/// \brief Instantiates a \c PredMap. |
84 | 84 |
/// |
85 | 85 |
/// This function instantiates a \c PredMap. |
86 | 86 |
/// \param digraph The digraph to which we would like to define the |
87 | 87 |
/// \c PredMap. |
88 | 88 |
static PredMap *createPredMap(const Digraph &digraph){ |
89 | 89 |
return new PredMap(digraph); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
/// \ingroup spantree |
95 | 95 |
/// |
96 | 96 |
/// \brief Minimum Cost Arborescence algorithm class. |
97 | 97 |
/// |
98 | 98 |
/// This class provides an efficient implementation of the |
99 | 99 |
/// Minimum Cost Arborescence algorithm. The arborescence is a tree |
100 | 100 |
/// which is directed from a given source node of the digraph. One or |
101 | 101 |
/// more sources should be given to the algorithm and it will calculate |
102 | 102 |
/// the minimum cost subgraph that is the union of arborescences with the |
103 | 103 |
/// given sources and spans all the nodes which are reachable from the |
104 | 104 |
/// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e). |
105 | 105 |
/// |
106 | 106 |
/// The algorithm also provides an optimal dual solution, therefore |
107 | 107 |
/// the optimality of the solution can be checked. |
108 | 108 |
/// |
109 | 109 |
/// \param GR The digraph type the algorithm runs on. |
110 | 110 |
/// \param CM A read-only arc map storing the costs of the |
111 | 111 |
/// arcs. It is read once for each arc, so the map may involve in |
112 | 112 |
/// relatively time consuming process to compute the arc costs if |
113 | 113 |
/// it is necessary. The default map type is \ref |
114 | 114 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
115 |
/// \param TR Traits class to set various data types used |
|
116 |
/// by the algorithm. The default traits class is |
|
117 |
/// \ |
|
115 |
/// \tparam TR The traits class that defines various types used by the |
|
116 |
/// algorithm. By default, it is \ref MinCostArborescenceDefaultTraits |
|
118 | 117 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". |
118 |
/// In most cases, this parameter should not be set directly, |
|
119 |
/// consider to use the named template parameters instead. |
|
119 | 120 |
#ifndef DOXYGEN |
120 | 121 |
template <typename GR, |
121 | 122 |
typename CM = typename GR::template ArcMap<int>, |
122 | 123 |
typename TR = |
123 | 124 |
MinCostArborescenceDefaultTraits<GR, CM> > |
124 | 125 |
#else |
125 |
template <typename GR, typename CM, |
|
126 |
template <typename GR, typename CM, typename TR> |
|
126 | 127 |
#endif |
127 | 128 |
class MinCostArborescence { |
128 | 129 |
public: |
129 | 130 |
|
130 | 131 |
/// \brief The \ref MinCostArborescenceDefaultTraits "traits class" |
131 | 132 |
/// of the algorithm. |
132 | 133 |
typedef TR Traits; |
133 | 134 |
/// The type of the underlying digraph. |
134 | 135 |
typedef typename Traits::Digraph Digraph; |
135 | 136 |
/// The type of the map that stores the arc costs. |
136 | 137 |
typedef typename Traits::CostMap CostMap; |
137 | 138 |
///The type of the costs of the arcs. |
138 | 139 |
typedef typename Traits::Value Value; |
139 | 140 |
///The type of the predecessor map. |
140 | 141 |
typedef typename Traits::PredMap PredMap; |
141 | 142 |
///The type of the map that stores which arcs are in the arborescence. |
142 | 143 |
typedef typename Traits::ArborescenceMap ArborescenceMap; |
143 | 144 |
|
144 | 145 |
typedef MinCostArborescence Create; |
145 | 146 |
|
146 | 147 |
private: |
147 | 148 |
|
148 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
149 | 150 |
|
150 | 151 |
struct CostArc { |
151 | 152 |
|
152 | 153 |
Arc arc; |
153 | 154 |
Value value; |
154 | 155 |
|
155 | 156 |
CostArc() {} |
156 | 157 |
CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {} |
157 | 158 |
|
158 | 159 |
}; |
159 | 160 |
|
160 | 161 |
const Digraph *_digraph; |
161 | 162 |
const CostMap *_cost; |
162 | 163 |
|
163 | 164 |
PredMap *_pred; |
164 | 165 |
bool local_pred; |
165 | 166 |
|
166 | 167 |
ArborescenceMap *_arborescence; |
167 | 168 |
bool local_arborescence; |
168 | 169 |
|
169 | 170 |
typedef typename Digraph::template ArcMap<int> ArcOrder; |
170 | 171 |
ArcOrder *_arc_order; |
171 | 172 |
|
172 | 173 |
typedef typename Digraph::template NodeMap<int> NodeOrder; |
173 | 174 |
NodeOrder *_node_order; |
174 | 175 |
|
175 | 176 |
typedef typename Digraph::template NodeMap<CostArc> CostArcMap; |
176 | 177 |
CostArcMap *_cost_arcs; |
177 | 178 |
|
178 | 179 |
struct StackLevel { |
179 | 180 |
|
180 | 181 |
std::vector<CostArc> arcs; |
181 | 182 |
int node_level; |
182 | 183 |
|
183 | 184 |
}; |
184 | 185 |
|
185 | 186 |
std::vector<StackLevel> level_stack; |
186 | 187 |
std::vector<Node> queue; |
187 | 188 |
|
188 | 189 |
typedef std::vector<typename Digraph::Node> DualNodeList; |
189 | 190 |
|
190 | 191 |
DualNodeList _dual_node_list; |
191 | 192 |
|
192 | 193 |
struct DualVariable { |
193 | 194 |
int begin, end; |
194 | 195 |
Value value; |
195 | 196 |
|
196 | 197 |
DualVariable(int _begin, int _end, Value _value) |
197 | 198 |
: begin(_begin), end(_end), value(_value) {} |
198 | 199 |
|
199 | 200 |
}; |
200 | 201 |
|
201 | 202 |
typedef std::vector<DualVariable> DualVariables; |
202 | 203 |
|
203 | 204 |
DualVariables _dual_variables; |
204 | 205 |
|
205 | 206 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
206 | 207 |
|
207 | 208 |
HeapCrossRef *_heap_cross_ref; |
208 | 209 |
|
209 | 210 |
typedef BinHeap<int, HeapCrossRef> Heap; |
210 | 211 |
|
211 | 212 |
Heap *_heap; |
212 | 213 |
|
213 | 214 |
protected: |
214 | 215 |
|
215 | 216 |
MinCostArborescence() {} |
216 | 217 |
|
217 | 218 |
private: |
218 | 219 |
|
219 | 220 |
void createStructures() { |
220 | 221 |
if (!_pred) { |
221 | 222 |
local_pred = true; |
222 | 223 |
_pred = Traits::createPredMap(*_digraph); |
223 | 224 |
} |
224 | 225 |
if (!_arborescence) { |
225 | 226 |
local_arborescence = true; |
226 | 227 |
_arborescence = Traits::createArborescenceMap(*_digraph); |
227 | 228 |
} |
228 | 229 |
if (!_arc_order) { |
229 | 230 |
_arc_order = new ArcOrder(*_digraph); |
230 | 231 |
} |
231 | 232 |
if (!_node_order) { |
232 | 233 |
_node_order = new NodeOrder(*_digraph); |
233 | 234 |
} |
234 | 235 |
if (!_cost_arcs) { |
235 | 236 |
_cost_arcs = new CostArcMap(*_digraph); |
236 | 237 |
} |
237 | 238 |
if (!_heap_cross_ref) { |
238 | 239 |
_heap_cross_ref = new HeapCrossRef(*_digraph, -1); |
239 | 240 |
} |
240 | 241 |
if (!_heap) { |
241 | 242 |
_heap = new Heap(*_heap_cross_ref); |
242 | 243 |
} |
243 | 244 |
} |
244 | 245 |
|
245 | 246 |
void destroyStructures() { |
246 | 247 |
if (local_arborescence) { |
247 | 248 |
delete _arborescence; |
248 | 249 |
} |
249 | 250 |
if (local_pred) { |
250 | 251 |
delete _pred; |
251 | 252 |
} |
252 | 253 |
if (_arc_order) { |
253 | 254 |
delete _arc_order; |
254 | 255 |
} |
255 | 256 |
if (_node_order) { |
256 | 257 |
delete _node_order; |
257 | 258 |
} |
258 | 259 |
if (_cost_arcs) { |
259 | 260 |
delete _cost_arcs; |
260 | 261 |
} |
261 | 262 |
if (_heap) { |
262 | 263 |
delete _heap; |
263 | 264 |
} |
264 | 265 |
if (_heap_cross_ref) { |
265 | 266 |
delete _heap_cross_ref; |
266 | 267 |
} |
267 | 268 |
} |
268 | 269 |
|
269 | 270 |
Arc prepare(Node node) { |
270 | 271 |
std::vector<Node> nodes; |
271 | 272 |
(*_node_order)[node] = _dual_node_list.size(); |
272 | 273 |
StackLevel level; |
273 | 274 |
level.node_level = _dual_node_list.size(); |
274 | 275 |
_dual_node_list.push_back(node); |
275 | 276 |
for (InArcIt it(*_digraph, node); it != INVALID; ++it) { |
276 | 277 |
Arc arc = it; |
277 | 278 |
Node source = _digraph->source(arc); |
278 | 279 |
Value value = (*_cost)[it]; |
279 | 280 |
if (source == node || (*_node_order)[source] == -3) continue; |
280 | 281 |
if ((*_cost_arcs)[source].arc == INVALID) { |
281 | 282 |
(*_cost_arcs)[source].arc = arc; |
282 | 283 |
(*_cost_arcs)[source].value = value; |
283 | 284 |
nodes.push_back(source); |
284 | 285 |
} else { |
285 | 286 |
if ((*_cost_arcs)[source].value > value) { |
286 | 287 |
(*_cost_arcs)[source].arc = arc; |
287 | 288 |
(*_cost_arcs)[source].value = value; |
288 | 289 |
} |
289 | 290 |
} |
290 | 291 |
} |
291 | 292 |
CostArc minimum = (*_cost_arcs)[nodes[0]]; |
292 | 293 |
for (int i = 1; i < int(nodes.size()); ++i) { |
293 | 294 |
if ((*_cost_arcs)[nodes[i]].value < minimum.value) { |
294 | 295 |
minimum = (*_cost_arcs)[nodes[i]]; |
295 | 296 |
} |
296 | 297 |
} |
297 | 298 |
(*_arc_order)[minimum.arc] = _dual_variables.size(); |
298 | 299 |
DualVariable var(_dual_node_list.size() - 1, |
299 | 300 |
_dual_node_list.size(), minimum.value); |
300 | 301 |
_dual_variables.push_back(var); |
301 | 302 |
for (int i = 0; i < int(nodes.size()); ++i) { |
302 | 303 |
(*_cost_arcs)[nodes[i]].value -= minimum.value; |
303 | 304 |
level.arcs.push_back((*_cost_arcs)[nodes[i]]); |
304 | 305 |
(*_cost_arcs)[nodes[i]].arc = INVALID; |
305 | 306 |
} |
306 | 307 |
level_stack.push_back(level); |
307 | 308 |
return minimum.arc; |
308 | 309 |
} |
309 | 310 |
|
310 | 311 |
Arc contract(Node node) { |
311 | 312 |
int node_bottom = bottom(node); |
312 | 313 |
std::vector<Node> nodes; |
313 | 314 |
while (!level_stack.empty() && |
314 | 315 |
level_stack.back().node_level >= node_bottom) { |
315 | 316 |
for (int i = 0; i < int(level_stack.back().arcs.size()); ++i) { |
316 | 317 |
Arc arc = level_stack.back().arcs[i].arc; |
317 | 318 |
Node source = _digraph->source(arc); |
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 |
#ifndef LEMON_PREFLOW_H |
20 | 20 |
#define LEMON_PREFLOW_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
/// \file |
26 | 26 |
/// \ingroup max_flow |
27 | 27 |
/// \brief Implementation of the preflow algorithm. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Preflow class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Preflow class. |
34 | 34 |
/// \tparam GR Digraph type. |
35 | 35 |
/// \tparam CAP Capacity map type. |
36 | 36 |
template <typename GR, typename CAP> |
37 | 37 |
struct PreflowDefaultTraits { |
38 | 38 |
|
39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
40 | 40 |
typedef GR Digraph; |
41 | 41 |
|
42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
43 | 43 |
/// |
44 | 44 |
/// The type of the map that stores the arc capacities. |
45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 | 46 |
typedef CAP CapacityMap; |
47 | 47 |
|
48 | 48 |
/// \brief The type of the flow values. |
49 | 49 |
typedef typename CapacityMap::Value Value; |
50 | 50 |
|
51 | 51 |
/// \brief The type of the map that stores the flow values. |
52 | 52 |
/// |
53 | 53 |
/// The type of the map that stores the flow values. |
54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 |
#ifdef DOXYGEN |
56 | 56 |
typedef GR::ArcMap<Value> FlowMap; |
57 | 57 |
#else |
58 | 58 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
59 | 59 |
#endif |
60 | 60 |
|
61 | 61 |
/// \brief Instantiates a FlowMap. |
62 | 62 |
/// |
63 | 63 |
/// This function instantiates a \ref FlowMap. |
64 | 64 |
/// \param digraph The digraph for which we would like to define |
65 | 65 |
/// the flow map. |
66 | 66 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
67 | 67 |
return new FlowMap(digraph); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
/// \brief The elevator type used by Preflow algorithm. |
71 | 71 |
/// |
72 | 72 |
/// The elevator type used by Preflow algorithm. |
73 | 73 |
/// |
74 | 74 |
/// \sa Elevator, LinkedElevator |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
77 | 77 |
#else |
78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates an Elevator. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates an \ref Elevator. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the elevator. |
86 | 86 |
/// \param max_level The maximum level of the elevator. |
87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
88 | 88 |
return new Elevator(digraph, max_level); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
/// \brief The tolerance used by the algorithm |
92 | 92 |
/// |
93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
95 | 95 |
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
/// \ingroup max_flow |
100 | 100 |
/// |
101 | 101 |
/// \brief %Preflow algorithm class. |
102 | 102 |
/// |
103 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
104 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
105 | 105 |
/// "flow of maximum value" in a digraph \ref clrs01algorithms, |
106 | 106 |
/// \ref amo93networkflows, \ref goldberg88newapproach. |
107 | 107 |
/// The preflow algorithms are the fastest known maximum |
108 | 108 |
/// flow algorithms. The current implementation uses a mixture of the |
109 | 109 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
110 | 110 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
111 | 111 |
/// |
112 | 112 |
/// The algorithm consists of two phases. After the first phase |
113 | 113 |
/// the maximum flow value and the minimum cut is obtained. The |
114 | 114 |
/// second phase constructs a feasible maximum flow on each arc. |
115 | 115 |
/// |
116 | 116 |
/// \warning This implementation cannot handle infinite or very large |
117 | 117 |
/// capacities (e.g. the maximum value of \c CAP::Value). |
118 | 118 |
/// |
119 | 119 |
/// \tparam GR The type of the digraph the algorithm runs on. |
120 | 120 |
/// \tparam CAP The type of the capacity map. The default map |
121 | 121 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
122 |
/// \tparam TR The traits class that defines various types used by the |
|
123 |
/// algorithm. By default, it is \ref PreflowDefaultTraits |
|
124 |
/// "PreflowDefaultTraits<GR, CAP>". |
|
125 |
/// In most cases, this parameter should not be set directly, |
|
126 |
/// consider to use the named template parameters instead. |
|
122 | 127 |
#ifdef DOXYGEN |
123 | 128 |
template <typename GR, typename CAP, typename TR> |
124 | 129 |
#else |
125 | 130 |
template <typename GR, |
126 | 131 |
typename CAP = typename GR::template ArcMap<int>, |
127 | 132 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
128 | 133 |
#endif |
129 | 134 |
class Preflow { |
130 | 135 |
public: |
131 | 136 |
|
132 | 137 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
133 | 138 |
typedef TR Traits; |
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename Traits::Digraph Digraph; |
136 | 141 |
///The type of the capacity map. |
137 | 142 |
typedef typename Traits::CapacityMap CapacityMap; |
138 | 143 |
///The type of the flow values. |
139 | 144 |
typedef typename Traits::Value Value; |
140 | 145 |
|
141 | 146 |
///The type of the flow map. |
142 | 147 |
typedef typename Traits::FlowMap FlowMap; |
143 | 148 |
///The type of the elevator. |
144 | 149 |
typedef typename Traits::Elevator Elevator; |
145 | 150 |
///The type of the tolerance. |
146 | 151 |
typedef typename Traits::Tolerance Tolerance; |
147 | 152 |
|
148 | 153 |
private: |
149 | 154 |
|
150 | 155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 156 |
|
152 | 157 |
const Digraph& _graph; |
153 | 158 |
const CapacityMap* _capacity; |
154 | 159 |
|
155 | 160 |
int _node_num; |
156 | 161 |
|
157 | 162 |
Node _source, _target; |
158 | 163 |
|
159 | 164 |
FlowMap* _flow; |
160 | 165 |
bool _local_flow; |
161 | 166 |
|
162 | 167 |
Elevator* _level; |
163 | 168 |
bool _local_level; |
164 | 169 |
|
165 | 170 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
166 | 171 |
ExcessMap* _excess; |
167 | 172 |
|
168 | 173 |
Tolerance _tolerance; |
169 | 174 |
|
170 | 175 |
bool _phase; |
171 | 176 |
|
172 | 177 |
|
173 | 178 |
void createStructures() { |
174 | 179 |
_node_num = countNodes(_graph); |
175 | 180 |
|
176 | 181 |
if (!_flow) { |
177 | 182 |
_flow = Traits::createFlowMap(_graph); |
178 | 183 |
_local_flow = true; |
179 | 184 |
} |
180 | 185 |
if (!_level) { |
181 | 186 |
_level = Traits::createElevator(_graph, _node_num); |
182 | 187 |
_local_level = true; |
183 | 188 |
} |
184 | 189 |
if (!_excess) { |
185 | 190 |
_excess = new ExcessMap(_graph); |
186 | 191 |
} |
187 | 192 |
} |
188 | 193 |
|
189 | 194 |
void destroyStructures() { |
190 | 195 |
if (_local_flow) { |
191 | 196 |
delete _flow; |
192 | 197 |
} |
193 | 198 |
if (_local_level) { |
194 | 199 |
delete _level; |
195 | 200 |
} |
196 | 201 |
if (_excess) { |
197 | 202 |
delete _excess; |
198 | 203 |
} |
199 | 204 |
} |
200 | 205 |
|
201 | 206 |
public: |
202 | 207 |
|
203 | 208 |
typedef Preflow Create; |
204 | 209 |
|
205 | 210 |
///\name Named Template Parameters |
206 | 211 |
|
207 | 212 |
///@{ |
208 | 213 |
|
209 | 214 |
template <typename T> |
210 | 215 |
struct SetFlowMapTraits : public Traits { |
211 | 216 |
typedef T FlowMap; |
212 | 217 |
static FlowMap *createFlowMap(const Digraph&) { |
213 | 218 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
214 | 219 |
return 0; // ignore warnings |
215 | 220 |
} |
216 | 221 |
}; |
217 | 222 |
|
218 | 223 |
/// \brief \ref named-templ-param "Named parameter" for setting |
219 | 224 |
/// FlowMap type |
220 | 225 |
/// |
221 | 226 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
222 | 227 |
/// type. |
223 | 228 |
template <typename T> |
224 | 229 |
struct SetFlowMap |
225 | 230 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
226 | 231 |
typedef Preflow<Digraph, CapacityMap, |
227 | 232 |
SetFlowMapTraits<T> > Create; |
228 | 233 |
}; |
229 | 234 |
|
230 | 235 |
template <typename T> |
231 | 236 |
struct SetElevatorTraits : public Traits { |
232 | 237 |
typedef T Elevator; |
233 | 238 |
static Elevator *createElevator(const Digraph&, int) { |
234 | 239 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
235 | 240 |
return 0; // ignore warnings |
236 | 241 |
} |
237 | 242 |
}; |
238 | 243 |
|
239 | 244 |
/// \brief \ref named-templ-param "Named parameter" for setting |
240 | 245 |
/// Elevator type |
241 | 246 |
/// |
242 | 247 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
243 | 248 |
/// type. If this named parameter is used, then an external |
244 | 249 |
/// elevator object must be passed to the algorithm using the |
245 | 250 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
246 | 251 |
/// \ref run() or \ref init(). |
247 | 252 |
/// \sa SetStandardElevator |
248 | 253 |
template <typename T> |
249 | 254 |
struct SetElevator |
250 | 255 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > { |
251 | 256 |
typedef Preflow<Digraph, CapacityMap, |
252 | 257 |
SetElevatorTraits<T> > Create; |
253 | 258 |
}; |
254 | 259 |
|
255 | 260 |
template <typename T> |
256 | 261 |
struct SetStandardElevatorTraits : public Traits { |
257 | 262 |
typedef T Elevator; |
258 | 263 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
259 | 264 |
return new Elevator(digraph, max_level); |
260 | 265 |
} |
261 | 266 |
}; |
262 | 267 |
|
263 | 268 |
/// \brief \ref named-templ-param "Named parameter" for setting |
264 | 269 |
/// Elevator type with automatic allocation |
265 | 270 |
/// |
266 | 271 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
267 | 272 |
/// type with automatic allocation. |
268 | 273 |
/// The Elevator should have standard constructor interface to be |
269 | 274 |
/// able to automatically created by the algorithm (i.e. the |
270 | 275 |
/// digraph and the maximum level should be passed to it). |
271 | 276 |
/// However, an external elevator object could also be passed to the |
272 | 277 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
273 | 278 |
/// before calling \ref run() or \ref init(). |
274 | 279 |
/// \sa SetElevator |
275 | 280 |
template <typename T> |
276 | 281 |
struct SetStandardElevator |
277 | 282 |
: public Preflow<Digraph, CapacityMap, |
278 | 283 |
SetStandardElevatorTraits<T> > { |
279 | 284 |
typedef Preflow<Digraph, CapacityMap, |
280 | 285 |
SetStandardElevatorTraits<T> > Create; |
281 | 286 |
}; |
282 | 287 |
|
283 | 288 |
/// @} |
284 | 289 |
|
285 | 290 |
protected: |
286 | 291 |
|
287 | 292 |
Preflow() {} |
288 | 293 |
|
289 | 294 |
public: |
290 | 295 |
|
291 | 296 |
|
292 | 297 |
/// \brief The constructor of the class. |
293 | 298 |
/// |
294 | 299 |
/// The constructor of the class. |
295 | 300 |
/// \param digraph The digraph the algorithm runs on. |
296 | 301 |
/// \param capacity The capacity of the arcs. |
297 | 302 |
/// \param source The source node. |
298 | 303 |
/// \param target The target node. |
299 | 304 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
300 | 305 |
Node source, Node target) |
301 | 306 |
: _graph(digraph), _capacity(&capacity), |
302 | 307 |
_node_num(0), _source(source), _target(target), |
303 | 308 |
_flow(0), _local_flow(false), |
304 | 309 |
_level(0), _local_level(false), |
305 | 310 |
_excess(0), _tolerance(), _phase() {} |
306 | 311 |
|
307 | 312 |
/// \brief Destructor. |
308 | 313 |
/// |
309 | 314 |
/// Destructor. |
310 | 315 |
~Preflow() { |
311 | 316 |
destroyStructures(); |
312 | 317 |
} |
313 | 318 |
|
0 comments (0 inline)