0
6
0
... | ... |
@@ -165,149 +165,170 @@ |
165 | 165 |
/** |
166 | 166 |
\defgroup map_adaptors Map Adaptors |
167 | 167 |
\ingroup maps |
168 | 168 |
\brief Tools to create new maps from existing ones |
169 | 169 |
|
170 | 170 |
This group contains map adaptors that are used to create "implicit" |
171 | 171 |
maps from other maps. |
172 | 172 |
|
173 | 173 |
Most of them are \ref concepts::ReadMap "read-only maps". |
174 | 174 |
They can make arithmetic and logical operations between one or two maps |
175 | 175 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
176 | 176 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
177 | 177 |
|
178 | 178 |
The typical usage of this classes is passing implicit maps to |
179 | 179 |
algorithms. If a function type algorithm is called then the function |
180 | 180 |
type map adaptors can be used comfortable. For example let's see the |
181 | 181 |
usage of map adaptors with the \c graphToEps() function. |
182 | 182 |
\code |
183 | 183 |
Color nodeColor(int deg) { |
184 | 184 |
if (deg >= 2) { |
185 | 185 |
return Color(0.5, 0.0, 0.5); |
186 | 186 |
} else if (deg == 1) { |
187 | 187 |
return Color(1.0, 0.5, 1.0); |
188 | 188 |
} else { |
189 | 189 |
return Color(0.0, 0.0, 0.0); |
190 | 190 |
} |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
Digraph::NodeMap<int> degree_map(graph); |
194 | 194 |
|
195 | 195 |
graphToEps(graph, "graph.eps") |
196 | 196 |
.coords(coords).scaleToA4().undirected() |
197 | 197 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
198 | 198 |
.run(); |
199 | 199 |
\endcode |
200 | 200 |
The \c functorToMap() function makes an \c int to \c Color map from the |
201 | 201 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
202 | 202 |
and the previously created map. The composed map is a proper function to |
203 | 203 |
get the color of each node. |
204 | 204 |
|
205 | 205 |
The usage with class type algorithms is little bit harder. In this |
206 | 206 |
case the function type map adaptors can not be used, because the |
207 | 207 |
function map adaptors give back temporary objects. |
208 | 208 |
\code |
209 | 209 |
Digraph graph; |
210 | 210 |
|
211 | 211 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
212 | 212 |
DoubleArcMap length(graph); |
213 | 213 |
DoubleArcMap speed(graph); |
214 | 214 |
|
215 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
216 | 216 |
TimeMap time(length, speed); |
217 | 217 |
|
218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
219 | 219 |
dijkstra.run(source, target); |
220 | 220 |
\endcode |
221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
224 | 224 |
class. We use the implicit minimum time map as the length map of the |
225 | 225 |
\c Dijkstra algorithm. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 |
@defgroup matrices Matrices |
|
230 |
@ingroup datas |
|
231 |
\brief Two dimensional data storages implemented in LEMON. |
|
232 |
|
|
233 |
This group contains two dimensional data storages implemented in LEMON. |
|
234 |
*/ |
|
235 |
|
|
236 |
/** |
|
237 | 229 |
@defgroup paths Path Structures |
238 | 230 |
@ingroup datas |
239 | 231 |
\brief %Path structures implemented in LEMON. |
240 | 232 |
|
241 | 233 |
This group contains the path structures implemented in LEMON. |
242 | 234 |
|
243 | 235 |
LEMON provides flexible data structures to work with paths. |
244 | 236 |
All of them have similar interfaces and they can be copied easily with |
245 | 237 |
assignment operators and copy constructors. This makes it easy and |
246 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
247 | 239 |
any kind of path structure. |
248 | 240 |
|
249 |
\sa |
|
241 |
\sa \ref concepts::Path "Path concept" |
|
242 |
*/ |
|
243 |
|
|
244 |
/** |
|
245 |
@defgroup heaps Heap Structures |
|
246 |
@ingroup datas |
|
247 |
\brief %Heap structures implemented in LEMON. |
|
248 |
|
|
249 |
This group contains the heap structures implemented in LEMON. |
|
250 |
|
|
251 |
LEMON provides several heap classes. They are efficient implementations |
|
252 |
of the abstract data type \e priority \e queue. They store items with |
|
253 |
specified values called \e priorities in such a way that finding and |
|
254 |
removing the item with minimum priority are efficient. |
|
255 |
The basic operations are adding and erasing items, changing the priority |
|
256 |
of an item, etc. |
|
257 |
|
|
258 |
Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
|
259 |
The heap implementations have the same interface, thus any of them can be |
|
260 |
used easily in such algorithms. |
|
261 |
|
|
262 |
\sa \ref concepts::Heap "Heap concept" |
|
263 |
*/ |
|
264 |
|
|
265 |
/** |
|
266 |
@defgroup matrices Matrices |
|
267 |
@ingroup datas |
|
268 |
\brief Two dimensional data storages implemented in LEMON. |
|
269 |
|
|
270 |
This group contains two dimensional data storages implemented in LEMON. |
|
250 | 271 |
*/ |
251 | 272 |
|
252 | 273 |
/** |
253 | 274 |
@defgroup auxdat Auxiliary Data Structures |
254 | 275 |
@ingroup datas |
255 | 276 |
\brief Auxiliary data structures implemented in LEMON. |
256 | 277 |
|
257 | 278 |
This group contains some data structures implemented in LEMON in |
258 | 279 |
order to make it easier to implement combinatorial algorithms. |
259 | 280 |
*/ |
260 | 281 |
|
261 | 282 |
/** |
262 | 283 |
@defgroup algs Algorithms |
263 | 284 |
\brief This group contains the several algorithms |
264 | 285 |
implemented in LEMON. |
265 | 286 |
|
266 | 287 |
This group contains the several algorithms |
267 | 288 |
implemented in LEMON. |
268 | 289 |
*/ |
269 | 290 |
|
270 | 291 |
/** |
271 | 292 |
@defgroup search Graph Search |
272 | 293 |
@ingroup algs |
273 | 294 |
\brief Common graph search algorithms. |
274 | 295 |
|
275 | 296 |
This group contains the common graph search algorithms, namely |
276 | 297 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
277 | 298 |
*/ |
278 | 299 |
|
279 | 300 |
/** |
280 | 301 |
@defgroup shortest_path Shortest Path Algorithms |
281 | 302 |
@ingroup algs |
282 | 303 |
\brief Algorithms for finding shortest paths. |
283 | 304 |
|
284 | 305 |
This group contains the algorithms for finding shortest paths in digraphs. |
285 | 306 |
|
286 | 307 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
287 | 308 |
when all arc lengths are non-negative. |
288 | 309 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
289 | 310 |
from a source node when arc lenghts can be either positive or negative, |
290 | 311 |
but the digraph should not contain directed cycles with negative total |
291 | 312 |
length. |
292 | 313 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
293 | 314 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
294 | 315 |
lenghts can be either positive or negative, but the digraph should |
295 | 316 |
not contain directed cycles with negative total length. |
296 | 317 |
- \ref Suurballe A successive shortest path algorithm for finding |
297 | 318 |
arc-disjoint paths between two nodes having minimum total length. |
298 | 319 |
*/ |
299 | 320 |
|
300 | 321 |
/** |
301 | 322 |
@defgroup max_flow Maximum Flow Algorithms |
302 | 323 |
@ingroup algs |
303 | 324 |
\brief Algorithms for finding maximum flows. |
304 | 325 |
|
305 | 326 |
This group contains the algorithms for finding maximum flows and |
306 | 327 |
feasible circulations. |
307 | 328 |
|
308 | 329 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
309 | 330 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
310 | 331 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
311 | 332 |
\f$s, t \in V\f$ source and target nodes. |
312 | 333 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
313 | 334 |
following optimization problem. |
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_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 | 24 |
///\brief Binary heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 |
/// \ingroup |
|
32 |
/// \ingroup heaps |
|
33 | 33 |
/// |
34 | 34 |
/// \brief Binary heap data structure. |
35 | 35 |
/// |
36 | 36 |
/// This class implements the \e binary \e heap data structure. |
37 | 37 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
38 | 38 |
/// |
39 | 39 |
/// \tparam PR Type of the priorities of the items. |
40 | 40 |
/// \tparam IM A read-writable item map with \c int values, used |
41 | 41 |
/// internally to handle the cross references. |
42 | 42 |
/// \tparam CMP A functor class for comparing the priorities. |
43 | 43 |
/// The default is \c std::less<PR>. |
44 | 44 |
#ifdef DOXYGEN |
45 | 45 |
template <typename PR, typename IM, typename CMP> |
46 | 46 |
#else |
47 | 47 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
48 | 48 |
#endif |
49 | 49 |
class BinHeap { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// Type of the item-int map. |
53 | 53 |
typedef IM ItemIntMap; |
54 | 54 |
/// Type of the priorities. |
55 | 55 |
typedef PR Prio; |
56 | 56 |
/// Type of the items stored in the heap. |
57 | 57 |
typedef typename ItemIntMap::Key Item; |
58 | 58 |
/// Type of the item-priority pairs. |
59 | 59 |
typedef std::pair<Item,Prio> Pair; |
60 | 60 |
/// Functor type for comparing the priorities. |
61 | 61 |
typedef CMP Compare; |
62 | 62 |
|
63 | 63 |
/// \brief Type to represent the states of the items. |
64 | 64 |
/// |
65 | 65 |
/// Each item has a state associated to it. It can be "in heap", |
66 | 66 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
67 | 67 |
/// heap's point of view, but may be useful to the user. |
68 | 68 |
/// |
69 | 69 |
/// The item-int map must be initialized in such way that it assigns |
70 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
71 | 71 |
enum State { |
72 | 72 |
IN_HEAP = 0, ///< = 0. |
73 | 73 |
PRE_HEAP = -1, ///< = -1. |
74 | 74 |
POST_HEAP = -2 ///< = -2. |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
private: |
78 | 78 |
std::vector<Pair> _data; |
79 | 79 |
Compare _comp; |
80 | 80 |
ItemIntMap &_iim; |
81 | 81 |
|
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// \brief Constructor. |
85 | 85 |
/// |
86 | 86 |
/// Constructor. |
87 | 87 |
/// \param map A map that assigns \c int values to the items. |
88 | 88 |
/// It is used internally to handle the cross references. |
89 | 89 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
90 | 90 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
91 | 91 |
|
92 | 92 |
/// \brief Constructor. |
93 | 93 |
/// |
94 | 94 |
/// Constructor. |
95 | 95 |
/// \param map A map that assigns \c int values to the items. |
96 | 96 |
/// It is used internally to handle the cross references. |
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_BUCKET_HEAP_H |
20 | 20 |
#define LEMON_BUCKET_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 | 24 |
///\brief Bucket heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
namespace _bucket_heap_bits { |
33 | 33 |
|
34 | 34 |
template <bool MIN> |
35 | 35 |
struct DirectionTraits { |
36 | 36 |
static bool less(int left, int right) { |
37 | 37 |
return left < right; |
38 | 38 |
} |
39 | 39 |
static void increase(int& value) { |
40 | 40 |
++value; |
41 | 41 |
} |
42 | 42 |
}; |
43 | 43 |
|
44 | 44 |
template <> |
45 | 45 |
struct DirectionTraits<false> { |
46 | 46 |
static bool less(int left, int right) { |
47 | 47 |
return left > right; |
48 | 48 |
} |
49 | 49 |
static void increase(int& value) { |
50 | 50 |
--value; |
51 | 51 |
} |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
} |
55 | 55 |
|
56 |
/// \ingroup |
|
56 |
/// \ingroup heaps |
|
57 | 57 |
/// |
58 | 58 |
/// \brief Bucket heap data structure. |
59 | 59 |
/// |
60 | 60 |
/// This class implements the \e bucket \e heap data structure. |
61 | 61 |
/// It practically conforms to the \ref concepts::Heap "heap concept", |
62 | 62 |
/// but it has some limitations. |
63 | 63 |
/// |
64 | 64 |
/// The bucket heap is a very simple structure. It can store only |
65 | 65 |
/// \c int priorities and it maintains a list of items for each priority |
66 | 66 |
/// in the range <tt>[0..C)</tt>. So it should only be used when the |
67 | 67 |
/// priorities are small. It is not intended to use as a Dijkstra heap. |
68 | 68 |
/// |
69 | 69 |
/// \tparam IM A read-writable item map with \c int values, used |
70 | 70 |
/// internally to handle the cross references. |
71 | 71 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
72 | 72 |
/// The default is \e min-heap. If this parameter is set to \c false, |
73 | 73 |
/// then the comparison is reversed, so the top(), prio() and pop() |
74 | 74 |
/// functions deal with the item having maximum priority instead of the |
75 | 75 |
/// minimum. |
76 | 76 |
/// |
77 | 77 |
/// \sa SimpleBucketHeap |
78 | 78 |
template <typename IM, bool MIN = true> |
79 | 79 |
class BucketHeap { |
80 | 80 |
|
81 | 81 |
public: |
82 | 82 |
|
83 | 83 |
/// Type of the item-int map. |
84 | 84 |
typedef IM ItemIntMap; |
85 | 85 |
/// Type of the priorities. |
86 | 86 |
typedef int Prio; |
87 | 87 |
/// Type of the items stored in the heap. |
88 | 88 |
typedef typename ItemIntMap::Key Item; |
89 | 89 |
/// Type of the item-priority pairs. |
90 | 90 |
typedef std::pair<Item,Prio> Pair; |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
|
94 | 94 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
95 | 95 |
|
96 | 96 |
public: |
97 | 97 |
|
98 | 98 |
/// \brief Type to represent the states of the items. |
99 | 99 |
/// |
100 | 100 |
/// Each item has a state associated to it. It can be "in heap", |
101 | 101 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
102 | 102 |
/// heap's point of view, but may be useful to the user. |
103 | 103 |
/// |
104 | 104 |
/// The item-int map must be initialized in such way that it assigns |
105 | 105 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
106 | 106 |
enum State { |
107 | 107 |
IN_HEAP = 0, ///< = 0. |
108 | 108 |
PRE_HEAP = -1, ///< = -1. |
109 | 109 |
POST_HEAP = -2 ///< = -2. |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
public: |
113 | 113 |
|
114 | 114 |
/// \brief Constructor. |
115 | 115 |
/// |
116 | 116 |
/// Constructor. |
117 | 117 |
/// \param map A map that assigns \c int values to the items. |
118 | 118 |
/// It is used internally to handle the cross references. |
119 | 119 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
120 | 120 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} |
... | ... |
@@ -310,129 +310,129 @@ |
310 | 310 |
/// \param i The item. |
311 | 311 |
/// \param p The priority. |
312 | 312 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
313 | 313 |
void increase(const Item &i, const Prio &p) { |
314 | 314 |
int idx = _iim[i]; |
315 | 315 |
unlace(idx); |
316 | 316 |
_data[idx].value = p; |
317 | 317 |
lace(idx); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
/// \brief Return the state of an item. |
321 | 321 |
/// |
322 | 322 |
/// This method returns \c PRE_HEAP if the given item has never |
323 | 323 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
324 | 324 |
/// and \c POST_HEAP otherwise. |
325 | 325 |
/// In the latter case it is possible that the item will get back |
326 | 326 |
/// to the heap again. |
327 | 327 |
/// \param i The item. |
328 | 328 |
State state(const Item &i) const { |
329 | 329 |
int idx = _iim[i]; |
330 | 330 |
if (idx >= 0) idx = 0; |
331 | 331 |
return State(idx); |
332 | 332 |
} |
333 | 333 |
|
334 | 334 |
/// \brief Set the state of an item in the heap. |
335 | 335 |
/// |
336 | 336 |
/// This function sets the state of the given item in the heap. |
337 | 337 |
/// It can be used to manually clear the heap when it is important |
338 | 338 |
/// to achive better time complexity. |
339 | 339 |
/// \param i The item. |
340 | 340 |
/// \param st The state. It should not be \c IN_HEAP. |
341 | 341 |
void state(const Item& i, State st) { |
342 | 342 |
switch (st) { |
343 | 343 |
case POST_HEAP: |
344 | 344 |
case PRE_HEAP: |
345 | 345 |
if (state(i) == IN_HEAP) { |
346 | 346 |
erase(i); |
347 | 347 |
} |
348 | 348 |
_iim[i] = st; |
349 | 349 |
break; |
350 | 350 |
case IN_HEAP: |
351 | 351 |
break; |
352 | 352 |
} |
353 | 353 |
} |
354 | 354 |
|
355 | 355 |
private: |
356 | 356 |
|
357 | 357 |
struct BucketItem { |
358 | 358 |
BucketItem(const Item& _item, int _value) |
359 | 359 |
: item(_item), value(_value) {} |
360 | 360 |
|
361 | 361 |
Item item; |
362 | 362 |
int value; |
363 | 363 |
|
364 | 364 |
int prev, next; |
365 | 365 |
}; |
366 | 366 |
|
367 | 367 |
ItemIntMap& _iim; |
368 | 368 |
std::vector<int> _first; |
369 | 369 |
std::vector<BucketItem> _data; |
370 | 370 |
mutable int _minimum; |
371 | 371 |
|
372 | 372 |
}; // class BucketHeap |
373 | 373 |
|
374 |
/// \ingroup |
|
374 |
/// \ingroup heaps |
|
375 | 375 |
/// |
376 | 376 |
/// \brief Simplified bucket heap data structure. |
377 | 377 |
/// |
378 | 378 |
/// This class implements a simplified \e bucket \e heap data |
379 | 379 |
/// structure. It does not provide some functionality, but it is |
380 | 380 |
/// faster and simpler than BucketHeap. The main difference is |
381 | 381 |
/// that BucketHeap stores a doubly-linked list for each key while |
382 | 382 |
/// this class stores only simply-linked lists. It supports erasing |
383 | 383 |
/// only for the item having minimum priority and it does not support |
384 | 384 |
/// key increasing and decreasing. |
385 | 385 |
/// |
386 | 386 |
/// Note that this implementation does not conform to the |
387 | 387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
388 | 388 |
/// functionality. |
389 | 389 |
/// |
390 | 390 |
/// \tparam IM A read-writable item map with \c int values, used |
391 | 391 |
/// internally to handle the cross references. |
392 | 392 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
393 | 393 |
/// The default is \e min-heap. If this parameter is set to \c false, |
394 | 394 |
/// then the comparison is reversed, so the top(), prio() and pop() |
395 | 395 |
/// functions deal with the item having maximum priority instead of the |
396 | 396 |
/// minimum. |
397 | 397 |
/// |
398 | 398 |
/// \sa BucketHeap |
399 | 399 |
template <typename IM, bool MIN = true > |
400 | 400 |
class SimpleBucketHeap { |
401 | 401 |
|
402 | 402 |
public: |
403 | 403 |
|
404 | 404 |
/// Type of the item-int map. |
405 | 405 |
typedef IM ItemIntMap; |
406 | 406 |
/// Type of the priorities. |
407 | 407 |
typedef int Prio; |
408 | 408 |
/// Type of the items stored in the heap. |
409 | 409 |
typedef typename ItemIntMap::Key Item; |
410 | 410 |
/// Type of the item-priority pairs. |
411 | 411 |
typedef std::pair<Item,Prio> Pair; |
412 | 412 |
|
413 | 413 |
private: |
414 | 414 |
|
415 | 415 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
416 | 416 |
|
417 | 417 |
public: |
418 | 418 |
|
419 | 419 |
/// \brief Type to represent the states of the items. |
420 | 420 |
/// |
421 | 421 |
/// Each item has a state associated to it. It can be "in heap", |
422 | 422 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
423 | 423 |
/// heap's point of view, but may be useful to the user. |
424 | 424 |
/// |
425 | 425 |
/// The item-int map must be initialized in such way that it assigns |
426 | 426 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
427 | 427 |
enum State { |
428 | 428 |
IN_HEAP = 0, ///< = 0. |
429 | 429 |
PRE_HEAP = -1, ///< = -1. |
430 | 430 |
POST_HEAP = -2 ///< = -2. |
431 | 431 |
}; |
432 | 432 |
|
433 | 433 |
public: |
434 | 434 |
|
435 | 435 |
/// \brief Constructor. |
436 | 436 |
/// |
437 | 437 |
/// Constructor. |
438 | 438 |
/// \param map A map that assigns \c int values to the items. |
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_CONCEPTS_HEAP_H |
20 | 20 |
#define LEMON_CONCEPTS_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup concept |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of heaps. |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief The heap concept. |
37 | 37 |
/// |
38 | 38 |
/// This concept class describes the main interface of heaps. |
39 |
/// The various heap structures are efficient |
|
39 |
/// The various \ref heaps "heap structures" are efficient |
|
40 | 40 |
/// implementations of the abstract data type \e priority \e queue. |
41 | 41 |
/// They store items with specified values called \e priorities |
42 | 42 |
/// in such a way that finding and removing the item with minimum |
43 | 43 |
/// priority are efficient. The basic operations are adding and |
44 | 44 |
/// erasing items, changing the priority of an item, etc. |
45 | 45 |
/// |
46 | 46 |
/// Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
47 | 47 |
/// Any class that conforms to this concept can be used easily in such |
48 | 48 |
/// algorithms. |
49 | 49 |
/// |
50 | 50 |
/// \tparam PR Type of the priorities of the items. |
51 | 51 |
/// \tparam IM A read-writable item map with \c int values, used |
52 | 52 |
/// internally to handle the cross references. |
53 | 53 |
/// \tparam CMP A functor class for comparing the priorities. |
54 | 54 |
/// The default is \c std::less<PR>. |
55 | 55 |
#ifdef DOXYGEN |
56 | 56 |
template <typename PR, typename IM, typename CMP> |
57 | 57 |
#else |
58 | 58 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
59 | 59 |
#endif |
60 | 60 |
class Heap { |
61 | 61 |
public: |
62 | 62 |
|
63 | 63 |
/// Type of the item-int map. |
64 | 64 |
typedef IM ItemIntMap; |
65 | 65 |
/// Type of the priorities. |
66 | 66 |
typedef PR Prio; |
67 | 67 |
/// Type of the items stored in the heap. |
68 | 68 |
typedef typename ItemIntMap::Key Item; |
69 | 69 |
|
70 | 70 |
/// \brief Type to represent the states of the items. |
71 | 71 |
/// |
72 | 72 |
/// Each item has a state associated to it. It can be "in heap", |
73 | 73 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
74 | 74 |
/// heap's point of view, but may be useful to the user. |
75 | 75 |
/// |
76 | 76 |
/// The item-int map must be initialized in such way that it assigns |
77 | 77 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
78 | 78 |
enum State { |
79 | 79 |
IN_HEAP = 0, ///< = 0. The "in heap" state constant. |
80 | 80 |
PRE_HEAP = -1, ///< = -1. The "pre-heap" state constant. |
81 | 81 |
POST_HEAP = -2 ///< = -2. The "post-heap" state constant. |
82 | 82 |
}; |
83 | 83 |
|
84 | 84 |
/// \brief Constructor. |
85 | 85 |
/// |
86 | 86 |
/// Constructor. |
87 | 87 |
/// \param map A map that assigns \c int values to keys of type |
88 | 88 |
/// \c Item. It is used internally by the heap implementations to |
89 | 89 |
/// handle the cross references. The assigned value must be |
90 | 90 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item. |
91 | 91 |
explicit Heap(ItemIntMap &map) {} |
92 | 92 |
|
93 | 93 |
/// \brief Constructor. |
94 | 94 |
/// |
95 | 95 |
/// Constructor. |
96 | 96 |
/// \param map A map that assigns \c int values to keys of type |
97 | 97 |
/// \c Item. It is used internally by the heap implementations to |
98 | 98 |
/// handle the cross references. The assigned value must be |
99 | 99 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item. |
100 | 100 |
/// \param comp The function object used for comparing the priorities. |
101 | 101 |
explicit Heap(ItemIntMap &map, const CMP &comp) {} |
102 | 102 |
|
103 | 103 |
/// \brief The number of items stored in the heap. |
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_FIB_HEAP_H |
20 | 20 |
#define LEMON_FIB_HEAP_H |
21 | 21 |
|
22 | 22 |
///\file |
23 |
///\ingroup |
|
23 |
///\ingroup heaps |
|
24 | 24 |
///\brief Fibonacci heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
#include <lemon/math.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 |
/// \ingroup |
|
33 |
/// \ingroup heaps |
|
34 | 34 |
/// |
35 | 35 |
/// \brief Fibonacci heap data structure. |
36 | 36 |
/// |
37 | 37 |
/// This class implements the \e Fibonacci \e heap data structure. |
38 | 38 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
39 | 39 |
/// |
40 | 40 |
/// The methods \ref increase() and \ref erase() are not efficient in a |
41 | 41 |
/// Fibonacci heap. In case of many calls of these operations, it is |
42 | 42 |
/// better to use other heap structure, e.g. \ref BinHeap "binary heap". |
43 | 43 |
/// |
44 | 44 |
/// \tparam PR Type of the priorities of the items. |
45 | 45 |
/// \tparam IM A read-writable item map with \c int values, used |
46 | 46 |
/// internally to handle the cross references. |
47 | 47 |
/// \tparam CMP A functor class for comparing the priorities. |
48 | 48 |
/// The default is \c std::less<PR>. |
49 | 49 |
#ifdef DOXYGEN |
50 | 50 |
template <typename PR, typename IM, typename CMP> |
51 | 51 |
#else |
52 | 52 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
53 | 53 |
#endif |
54 | 54 |
class FibHeap { |
55 | 55 |
public: |
56 | 56 |
|
57 | 57 |
/// Type of the item-int map. |
58 | 58 |
typedef IM ItemIntMap; |
59 | 59 |
/// Type of the priorities. |
60 | 60 |
typedef PR Prio; |
61 | 61 |
/// Type of the items stored in the heap. |
62 | 62 |
typedef typename ItemIntMap::Key Item; |
63 | 63 |
/// Type of the item-priority pairs. |
64 | 64 |
typedef std::pair<Item,Prio> Pair; |
65 | 65 |
/// Functor type for comparing the priorities. |
66 | 66 |
typedef CMP Compare; |
67 | 67 |
|
68 | 68 |
private: |
69 | 69 |
class Store; |
70 | 70 |
|
71 | 71 |
std::vector<Store> _data; |
72 | 72 |
int _minimum; |
73 | 73 |
ItemIntMap &_iim; |
74 | 74 |
Compare _comp; |
75 | 75 |
int _num; |
76 | 76 |
|
77 | 77 |
public: |
78 | 78 |
|
79 | 79 |
/// \brief Type to represent the states of the items. |
80 | 80 |
/// |
81 | 81 |
/// Each item has a state associated to it. It can be "in heap", |
82 | 82 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
83 | 83 |
/// heap's point of view, but may be useful to the user. |
84 | 84 |
/// |
85 | 85 |
/// The item-int map must be initialized in such way that it assigns |
86 | 86 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
87 | 87 |
enum State { |
88 | 88 |
IN_HEAP = 0, ///< = 0. |
89 | 89 |
PRE_HEAP = -1, ///< = -1. |
90 | 90 |
POST_HEAP = -2 ///< = -2. |
91 | 91 |
}; |
92 | 92 |
|
93 | 93 |
/// \brief Constructor. |
94 | 94 |
/// |
95 | 95 |
/// Constructor. |
96 | 96 |
/// \param map A map that assigns \c int values to the items. |
97 | 97 |
/// It is used internally to handle the cross references. |
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_RADIX_HEAP_H |
20 | 20 |
#define LEMON_RADIX_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 | 24 |
///\brief Radix heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <lemon/error.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
|
32 |
/// \ingroup |
|
32 |
/// \ingroup heaps |
|
33 | 33 |
/// |
34 | 34 |
/// \brief Radix heap data structure. |
35 | 35 |
/// |
36 | 36 |
/// This class implements the \e radix \e heap data structure. |
37 | 37 |
/// It practically conforms to the \ref concepts::Heap "heap concept", |
38 | 38 |
/// but it has some limitations due its special implementation. |
39 | 39 |
/// The type of the priorities must be \c int and the priority of an |
40 | 40 |
/// item cannot be decreased under the priority of the last removed item. |
41 | 41 |
/// |
42 | 42 |
/// \tparam IM A read-writable item map with \c int values, used |
43 | 43 |
/// internally to handle the cross references. |
44 | 44 |
template <typename IM> |
45 | 45 |
class RadixHeap { |
46 | 46 |
|
47 | 47 |
public: |
48 | 48 |
|
49 | 49 |
/// Type of the item-int map. |
50 | 50 |
typedef IM ItemIntMap; |
51 | 51 |
/// Type of the priorities. |
52 | 52 |
typedef int Prio; |
53 | 53 |
/// Type of the items stored in the heap. |
54 | 54 |
typedef typename ItemIntMap::Key Item; |
55 | 55 |
|
56 | 56 |
/// \brief Exception thrown by RadixHeap. |
57 | 57 |
/// |
58 | 58 |
/// This exception is thrown when an item is inserted into a |
59 | 59 |
/// RadixHeap with a priority smaller than the last erased one. |
60 | 60 |
/// \see RadixHeap |
61 | 61 |
class UnderFlowPriorityError : public Exception { |
62 | 62 |
public: |
63 | 63 |
virtual const char* what() const throw() { |
64 | 64 |
return "lemon::RadixHeap::UnderFlowPriorityError"; |
65 | 65 |
} |
66 | 66 |
}; |
67 | 67 |
|
68 | 68 |
/// \brief Type to represent the states of the items. |
69 | 69 |
/// |
70 | 70 |
/// Each item has a state associated to it. It can be "in heap", |
71 | 71 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
72 | 72 |
/// heap's point of view, but may be useful to the user. |
73 | 73 |
/// |
74 | 74 |
/// The item-int map must be initialized in such way that it assigns |
75 | 75 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
76 | 76 |
enum State { |
77 | 77 |
IN_HEAP = 0, ///< = 0. |
78 | 78 |
PRE_HEAP = -1, ///< = -1. |
79 | 79 |
POST_HEAP = -2 ///< = -2. |
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
private: |
83 | 83 |
|
84 | 84 |
struct RadixItem { |
85 | 85 |
int prev, next, box; |
86 | 86 |
Item item; |
87 | 87 |
int prio; |
88 | 88 |
RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {} |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
struct RadixBox { |
92 | 92 |
int first; |
93 | 93 |
int min, size; |
94 | 94 |
RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {} |
95 | 95 |
}; |
96 | 96 |
|
0 comments (0 inline)