0
6
0
| ... | ... |
@@ -181,117 +181,138 @@ |
| 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. |
| 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; |
| 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 |
| ... | ... |
@@ -326,97 +326,97 @@ |
| 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 |
| 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 |
| 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", |
| 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 |
}; |
0 comments (0 inline)