1.1 --- a/lemon/bucket_heap.h Fri Aug 09 11:07:27 2013 +0200
1.2 +++ b/lemon/bucket_heap.h Sun Aug 11 15:28:12 2013 +0200
1.3 @@ -2,7 +2,7 @@
1.4 *
1.5 * This file is a part of LEMON, a generic C++ optimization library.
1.6 *
1.7 - * Copyright (C) 2003-2009
1.8 + * Copyright (C) 2003-2010
1.9 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 *
1.12 @@ -19,9 +19,9 @@
1.13 #ifndef LEMON_BUCKET_HEAP_H
1.14 #define LEMON_BUCKET_HEAP_H
1.15
1.16 -///\ingroup auxdat
1.17 +///\ingroup heaps
1.18 ///\file
1.19 -///\brief Bucket Heap implementation.
1.20 +///\brief Bucket heap implementation.
1.21
1.22 #include <vector>
1.23 #include <utility>
1.24 @@ -53,35 +53,41 @@
1.25
1.26 }
1.27
1.28 - /// \ingroup auxdat
1.29 + /// \ingroup heaps
1.30 ///
1.31 - /// \brief A Bucket Heap implementation.
1.32 + /// \brief Bucket heap data structure.
1.33 ///
1.34 - /// This class implements the \e bucket \e heap data structure. A \e heap
1.35 - /// is a data structure for storing items with specified values called \e
1.36 - /// priorities in such a way that finding the item with minimum priority is
1.37 - /// efficient. The bucket heap is very simple implementation, it can store
1.38 - /// only integer priorities and it stores for each priority in the
1.39 - /// \f$ [0..C) \f$ range a list of items. So it should be used only when
1.40 - /// the priorities are small. It is not intended to use as dijkstra heap.
1.41 + /// This class implements the \e bucket \e heap data structure.
1.42 + /// It practically conforms to the \ref concepts::Heap "heap concept",
1.43 + /// but it has some limitations.
1.44 ///
1.45 - /// \param IM A read and write Item int map, used internally
1.46 - /// to handle the cross references.
1.47 - /// \param MIN If the given parameter is false then instead of the
1.48 - /// minimum value the maximum can be retrivied with the top() and
1.49 - /// prio() member functions.
1.50 + /// The bucket heap is a very simple structure. It can store only
1.51 + /// \c int priorities and it maintains a list of items for each priority
1.52 + /// in the range <tt>[0..C)</tt>. So it should only be used when the
1.53 + /// priorities are small. It is not intended to use as a Dijkstra heap.
1.54 + ///
1.55 + /// \tparam IM A read-writable item map with \c int values, used
1.56 + /// internally to handle the cross references.
1.57 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
1.58 + /// The default is \e min-heap. If this parameter is set to \c false,
1.59 + /// then the comparison is reversed, so the top(), prio() and pop()
1.60 + /// functions deal with the item having maximum priority instead of the
1.61 + /// minimum.
1.62 + ///
1.63 + /// \sa SimpleBucketHeap
1.64 template <typename IM, bool MIN = true>
1.65 class BucketHeap {
1.66
1.67 public:
1.68 - /// \e
1.69 - typedef typename IM::Key Item;
1.70 - /// \e
1.71 +
1.72 + /// Type of the item-int map.
1.73 + typedef IM ItemIntMap;
1.74 + /// Type of the priorities.
1.75 typedef int Prio;
1.76 - /// \e
1.77 - typedef std::pair<Item, Prio> Pair;
1.78 - /// \e
1.79 - typedef IM ItemIntMap;
1.80 + /// Type of the items stored in the heap.
1.81 + typedef typename ItemIntMap::Key Item;
1.82 + /// Type of the item-priority pairs.
1.83 + typedef std::pair<Item,Prio> Pair;
1.84
1.85 private:
1.86
1.87 @@ -89,10 +95,10 @@
1.88
1.89 public:
1.90
1.91 - /// \brief Type to represent the items states.
1.92 + /// \brief Type to represent the states of the items.
1.93 ///
1.94 - /// Each Item element have a state associated to it. It may be "in heap",
1.95 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.96 + /// Each item has a state associated to it. It can be "in heap",
1.97 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.98 /// heap's point of view, but may be useful to the user.
1.99 ///
1.100 /// The item-int map must be initialized in such way that it assigns
1.101 @@ -104,37 +110,39 @@
1.102 };
1.103
1.104 public:
1.105 - /// \brief The constructor.
1.106 +
1.107 + /// \brief Constructor.
1.108 ///
1.109 - /// The constructor.
1.110 - /// \param map should be given to the constructor, since it is used
1.111 - /// internally to handle the cross references. The value of the map
1.112 - /// should be PRE_HEAP (-1) for each element.
1.113 + /// Constructor.
1.114 + /// \param map A map that assigns \c int values to the items.
1.115 + /// It is used internally to handle the cross references.
1.116 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.117 explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
1.118
1.119 - /// The number of items stored in the heap.
1.120 + /// \brief The number of items stored in the heap.
1.121 ///
1.122 - /// \brief Returns the number of items stored in the heap.
1.123 + /// This function returns the number of items stored in the heap.
1.124 int size() const { return _data.size(); }
1.125
1.126 - /// \brief Checks if the heap stores no items.
1.127 + /// \brief Check if the heap is empty.
1.128 ///
1.129 - /// Returns \c true if and only if the heap stores no items.
1.130 + /// This function returns \c true if the heap is empty.
1.131 bool empty() const { return _data.empty(); }
1.132
1.133 - /// \brief Make empty this heap.
1.134 + /// \brief Make the heap empty.
1.135 ///
1.136 - /// Make empty this heap. It does not change the cross reference
1.137 - /// map. If you want to reuse a heap what is not surely empty you
1.138 - /// should first clear the heap and after that you should set the
1.139 - /// cross reference map for each item to \c PRE_HEAP.
1.140 + /// This functon makes the heap empty.
1.141 + /// It does not change the cross reference map. If you want to reuse
1.142 + /// a heap that is not surely empty, you should first clear it and
1.143 + /// then you should set the cross reference map to \c PRE_HEAP
1.144 + /// for each item.
1.145 void clear() {
1.146 _data.clear(); _first.clear(); _minimum = 0;
1.147 }
1.148
1.149 private:
1.150
1.151 - void relocate_last(int idx) {
1.152 + void relocateLast(int idx) {
1.153 if (idx + 1 < int(_data.size())) {
1.154 _data[idx] = _data.back();
1.155 if (_data[idx].prev != -1) {
1.156 @@ -174,19 +182,24 @@
1.157 }
1.158
1.159 public:
1.160 +
1.161 /// \brief Insert a pair of item and priority into the heap.
1.162 ///
1.163 - /// Adds \c p.first to the heap with priority \c p.second.
1.164 + /// This function inserts \c p.first to the heap with priority
1.165 + /// \c p.second.
1.166 /// \param p The pair to insert.
1.167 + /// \pre \c p.first must not be stored in the heap.
1.168 void push(const Pair& p) {
1.169 push(p.first, p.second);
1.170 }
1.171
1.172 /// \brief Insert an item into the heap with the given priority.
1.173 ///
1.174 - /// Adds \c i to the heap with priority \c p.
1.175 + /// This function inserts the given item into the heap with the
1.176 + /// given priority.
1.177 /// \param i The item to insert.
1.178 /// \param p The priority of the item.
1.179 + /// \pre \e i must not be stored in the heap.
1.180 void push(const Item &i, const Prio &p) {
1.181 int idx = _data.size();
1.182 _iim[i] = idx;
1.183 @@ -197,10 +210,10 @@
1.184 }
1.185 }
1.186
1.187 - /// \brief Returns the item with minimum priority.
1.188 + /// \brief Return the item having minimum priority.
1.189 ///
1.190 - /// This method returns the item with minimum priority.
1.191 - /// \pre The heap must be nonempty.
1.192 + /// This function returns the item having minimum priority.
1.193 + /// \pre The heap must be non-empty.
1.194 Item top() const {
1.195 while (_first[_minimum] == -1) {
1.196 Direction::increase(_minimum);
1.197 @@ -208,10 +221,10 @@
1.198 return _data[_first[_minimum]].item;
1.199 }
1.200
1.201 - /// \brief Returns the minimum priority.
1.202 + /// \brief The minimum priority.
1.203 ///
1.204 - /// It returns the minimum priority.
1.205 - /// \pre The heap must be nonempty.
1.206 + /// This function returns the minimum priority.
1.207 + /// \pre The heap must be non-empty.
1.208 Prio prio() const {
1.209 while (_first[_minimum] == -1) {
1.210 Direction::increase(_minimum);
1.211 @@ -219,9 +232,9 @@
1.212 return _minimum;
1.213 }
1.214
1.215 - /// \brief Deletes the item with minimum priority.
1.216 + /// \brief Remove the item having minimum priority.
1.217 ///
1.218 - /// This method deletes the item with minimum priority from the heap.
1.219 + /// This function removes the item having minimum priority.
1.220 /// \pre The heap must be non-empty.
1.221 void pop() {
1.222 while (_first[_minimum] == -1) {
1.223 @@ -230,37 +243,38 @@
1.224 int idx = _first[_minimum];
1.225 _iim[_data[idx].item] = -2;
1.226 unlace(idx);
1.227 - relocate_last(idx);
1.228 + relocateLast(idx);
1.229 }
1.230
1.231 - /// \brief Deletes \c i from the heap.
1.232 + /// \brief Remove the given item from the heap.
1.233 ///
1.234 - /// This method deletes item \c i from the heap, if \c i was
1.235 - /// already stored in the heap.
1.236 - /// \param i The item to erase.
1.237 + /// This function removes the given item from the heap if it is
1.238 + /// already stored.
1.239 + /// \param i The item to delete.
1.240 + /// \pre \e i must be in the heap.
1.241 void erase(const Item &i) {
1.242 int idx = _iim[i];
1.243 _iim[_data[idx].item] = -2;
1.244 unlace(idx);
1.245 - relocate_last(idx);
1.246 + relocateLast(idx);
1.247 }
1.248
1.249 -
1.250 - /// \brief Returns the priority of \c i.
1.251 + /// \brief The priority of the given item.
1.252 ///
1.253 - /// This function returns the priority of item \c i.
1.254 - /// \pre \c i must be in the heap.
1.255 + /// This function returns the priority of the given item.
1.256 /// \param i The item.
1.257 + /// \pre \e i must be in the heap.
1.258 Prio operator[](const Item &i) const {
1.259 int idx = _iim[i];
1.260 return _data[idx].value;
1.261 }
1.262
1.263 - /// \brief \c i gets to the heap with priority \c p independently
1.264 - /// if \c i was already there.
1.265 + /// \brief Set the priority of an item or insert it, if it is
1.266 + /// not stored in the heap.
1.267 ///
1.268 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.269 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.270 + /// This method sets the priority of the given item if it is
1.271 + /// already stored in the heap. Otherwise it inserts the given
1.272 + /// item into the heap with the given priority.
1.273 /// \param i The item.
1.274 /// \param p The priority.
1.275 void set(const Item &i, const Prio &p) {
1.276 @@ -274,13 +288,12 @@
1.277 }
1.278 }
1.279
1.280 - /// \brief Decreases the priority of \c i to \c p.
1.281 + /// \brief Decrease the priority of an item to the given value.
1.282 ///
1.283 - /// This method decreases the priority of item \c i to \c p.
1.284 - /// \pre \c i must be stored in the heap with priority at least \c
1.285 - /// p relative to \c Compare.
1.286 + /// This function decreases the priority of an item to the given value.
1.287 /// \param i The item.
1.288 /// \param p The priority.
1.289 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.290 void decrease(const Item &i, const Prio &p) {
1.291 int idx = _iim[i];
1.292 unlace(idx);
1.293 @@ -291,13 +304,12 @@
1.294 lace(idx);
1.295 }
1.296
1.297 - /// \brief Increases the priority of \c i to \c p.
1.298 + /// \brief Increase the priority of an item to the given value.
1.299 ///
1.300 - /// This method sets the priority of item \c i to \c p.
1.301 - /// \pre \c i must be stored in the heap with priority at most \c
1.302 - /// p relative to \c Compare.
1.303 + /// This function increases the priority of an item to the given value.
1.304 /// \param i The item.
1.305 /// \param p The priority.
1.306 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.307 void increase(const Item &i, const Prio &p) {
1.308 int idx = _iim[i];
1.309 unlace(idx);
1.310 @@ -305,13 +317,13 @@
1.311 lace(idx);
1.312 }
1.313
1.314 - /// \brief Returns if \c item is in, has already been in, or has
1.315 - /// never been in the heap.
1.316 + /// \brief Return the state of an item.
1.317 ///
1.318 - /// This method returns PRE_HEAP if \c item has never been in the
1.319 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.320 - /// otherwise. In the latter case it is possible that \c item will
1.321 - /// get back to the heap again.
1.322 + /// This method returns \c PRE_HEAP if the given item has never
1.323 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.324 + /// and \c POST_HEAP otherwise.
1.325 + /// In the latter case it is possible that the item will get back
1.326 + /// to the heap again.
1.327 /// \param i The item.
1.328 State state(const Item &i) const {
1.329 int idx = _iim[i];
1.330 @@ -319,11 +331,11 @@
1.331 return State(idx);
1.332 }
1.333
1.334 - /// \brief Sets the state of the \c item in the heap.
1.335 + /// \brief Set the state of an item in the heap.
1.336 ///
1.337 - /// Sets the state of the \c item in the heap. It can be used to
1.338 - /// manually clear the heap when it is important to achive the
1.339 - /// better time complexity.
1.340 + /// This function sets the state of the given item in the heap.
1.341 + /// It can be used to manually clear the heap when it is important
1.342 + /// to achive better time complexity.
1.343 /// \param i The item.
1.344 /// \param st The state. It should not be \c IN_HEAP.
1.345 void state(const Item& i, State st) {
1.346 @@ -359,33 +371,44 @@
1.347
1.348 }; // class BucketHeap
1.349
1.350 - /// \ingroup auxdat
1.351 + /// \ingroup heaps
1.352 ///
1.353 - /// \brief A Simplified Bucket Heap implementation.
1.354 + /// \brief Simplified bucket heap data structure.
1.355 ///
1.356 /// This class implements a simplified \e bucket \e heap data
1.357 - /// structure. It does not provide some functionality but it faster
1.358 - /// and simplier data structure than the BucketHeap. The main
1.359 - /// difference is that the BucketHeap stores for every key a double
1.360 - /// linked list while this class stores just simple lists. In the
1.361 - /// other way it does not support erasing each elements just the
1.362 - /// minimal and it does not supports key increasing, decreasing.
1.363 + /// structure. It does not provide some functionality, but it is
1.364 + /// faster and simpler than BucketHeap. The main difference is
1.365 + /// that BucketHeap stores a doubly-linked list for each key while
1.366 + /// this class stores only simply-linked lists. It supports erasing
1.367 + /// only for the item having minimum priority and it does not support
1.368 + /// key increasing and decreasing.
1.369 ///
1.370 - /// \param IM A read and write Item int map, used internally
1.371 - /// to handle the cross references.
1.372 - /// \param MIN If the given parameter is false then instead of the
1.373 - /// minimum value the maximum can be retrivied with the top() and
1.374 - /// prio() member functions.
1.375 + /// Note that this implementation does not conform to the
1.376 + /// \ref concepts::Heap "heap concept" due to the lack of some
1.377 + /// functionality.
1.378 + ///
1.379 + /// \tparam IM A read-writable item map with \c int values, used
1.380 + /// internally to handle the cross references.
1.381 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
1.382 + /// The default is \e min-heap. If this parameter is set to \c false,
1.383 + /// then the comparison is reversed, so the top(), prio() and pop()
1.384 + /// functions deal with the item having maximum priority instead of the
1.385 + /// minimum.
1.386 ///
1.387 /// \sa BucketHeap
1.388 template <typename IM, bool MIN = true >
1.389 class SimpleBucketHeap {
1.390
1.391 public:
1.392 - typedef typename IM::Key Item;
1.393 +
1.394 + /// Type of the item-int map.
1.395 + typedef IM ItemIntMap;
1.396 + /// Type of the priorities.
1.397 typedef int Prio;
1.398 - typedef std::pair<Item, Prio> Pair;
1.399 - typedef IM ItemIntMap;
1.400 + /// Type of the items stored in the heap.
1.401 + typedef typename ItemIntMap::Key Item;
1.402 + /// Type of the item-priority pairs.
1.403 + typedef std::pair<Item,Prio> Pair;
1.404
1.405 private:
1.406
1.407 @@ -393,10 +416,10 @@
1.408
1.409 public:
1.410
1.411 - /// \brief Type to represent the items states.
1.412 + /// \brief Type to represent the states of the items.
1.413 ///
1.414 - /// Each Item element have a state associated to it. It may be "in heap",
1.415 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.416 + /// Each item has a state associated to it. It can be "in heap",
1.417 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.418 /// heap's point of view, but may be useful to the user.
1.419 ///
1.420 /// The item-int map must be initialized in such way that it assigns
1.421 @@ -409,48 +432,53 @@
1.422
1.423 public:
1.424
1.425 - /// \brief The constructor.
1.426 + /// \brief Constructor.
1.427 ///
1.428 - /// The constructor.
1.429 - /// \param map should be given to the constructor, since it is used
1.430 - /// internally to handle the cross references. The value of the map
1.431 - /// should be PRE_HEAP (-1) for each element.
1.432 + /// Constructor.
1.433 + /// \param map A map that assigns \c int values to the items.
1.434 + /// It is used internally to handle the cross references.
1.435 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.436 explicit SimpleBucketHeap(ItemIntMap &map)
1.437 : _iim(map), _free(-1), _num(0), _minimum(0) {}
1.438
1.439 - /// \brief Returns the number of items stored in the heap.
1.440 + /// \brief The number of items stored in the heap.
1.441 ///
1.442 - /// The number of items stored in the heap.
1.443 + /// This function returns the number of items stored in the heap.
1.444 int size() const { return _num; }
1.445
1.446 - /// \brief Checks if the heap stores no items.
1.447 + /// \brief Check if the heap is empty.
1.448 ///
1.449 - /// Returns \c true if and only if the heap stores no items.
1.450 + /// This function returns \c true if the heap is empty.
1.451 bool empty() const { return _num == 0; }
1.452
1.453 - /// \brief Make empty this heap.
1.454 + /// \brief Make the heap empty.
1.455 ///
1.456 - /// Make empty this heap. It does not change the cross reference
1.457 - /// map. If you want to reuse a heap what is not surely empty you
1.458 - /// should first clear the heap and after that you should set the
1.459 - /// cross reference map for each item to \c PRE_HEAP.
1.460 + /// This functon makes the heap empty.
1.461 + /// It does not change the cross reference map. If you want to reuse
1.462 + /// a heap that is not surely empty, you should first clear it and
1.463 + /// then you should set the cross reference map to \c PRE_HEAP
1.464 + /// for each item.
1.465 void clear() {
1.466 _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
1.467 }
1.468
1.469 /// \brief Insert a pair of item and priority into the heap.
1.470 ///
1.471 - /// Adds \c p.first to the heap with priority \c p.second.
1.472 + /// This function inserts \c p.first to the heap with priority
1.473 + /// \c p.second.
1.474 /// \param p The pair to insert.
1.475 + /// \pre \c p.first must not be stored in the heap.
1.476 void push(const Pair& p) {
1.477 push(p.first, p.second);
1.478 }
1.479
1.480 /// \brief Insert an item into the heap with the given priority.
1.481 ///
1.482 - /// Adds \c i to the heap with priority \c p.
1.483 + /// This function inserts the given item into the heap with the
1.484 + /// given priority.
1.485 /// \param i The item to insert.
1.486 /// \param p The priority of the item.
1.487 + /// \pre \e i must not be stored in the heap.
1.488 void push(const Item &i, const Prio &p) {
1.489 int idx;
1.490 if (_free == -1) {
1.491 @@ -471,10 +499,10 @@
1.492 ++_num;
1.493 }
1.494
1.495 - /// \brief Returns the item with minimum priority.
1.496 + /// \brief Return the item having minimum priority.
1.497 ///
1.498 - /// This method returns the item with minimum priority.
1.499 - /// \pre The heap must be nonempty.
1.500 + /// This function returns the item having minimum priority.
1.501 + /// \pre The heap must be non-empty.
1.502 Item top() const {
1.503 while (_first[_minimum] == -1) {
1.504 Direction::increase(_minimum);
1.505 @@ -482,10 +510,10 @@
1.506 return _data[_first[_minimum]].item;
1.507 }
1.508
1.509 - /// \brief Returns the minimum priority.
1.510 + /// \brief The minimum priority.
1.511 ///
1.512 - /// It returns the minimum priority.
1.513 - /// \pre The heap must be nonempty.
1.514 + /// This function returns the minimum priority.
1.515 + /// \pre The heap must be non-empty.
1.516 Prio prio() const {
1.517 while (_first[_minimum] == -1) {
1.518 Direction::increase(_minimum);
1.519 @@ -493,9 +521,9 @@
1.520 return _minimum;
1.521 }
1.522
1.523 - /// \brief Deletes the item with minimum priority.
1.524 + /// \brief Remove the item having minimum priority.
1.525 ///
1.526 - /// This method deletes the item with minimum priority from the heap.
1.527 + /// This function removes the item having minimum priority.
1.528 /// \pre The heap must be non-empty.
1.529 void pop() {
1.530 while (_first[_minimum] == -1) {
1.531 @@ -509,16 +537,15 @@
1.532 --_num;
1.533 }
1.534
1.535 - /// \brief Returns the priority of \c i.
1.536 + /// \brief The priority of the given item.
1.537 ///
1.538 - /// This function returns the priority of item \c i.
1.539 - /// \warning This operator is not a constant time function
1.540 - /// because it scans the whole data structure to find the proper
1.541 - /// value.
1.542 - /// \pre \c i must be in the heap.
1.543 + /// This function returns the priority of the given item.
1.544 /// \param i The item.
1.545 + /// \pre \e i must be in the heap.
1.546 + /// \warning This operator is not a constant time function because
1.547 + /// it scans the whole data structure to find the proper value.
1.548 Prio operator[](const Item &i) const {
1.549 - for (int k = 0; k < _first.size(); ++k) {
1.550 + for (int k = 0; k < int(_first.size()); ++k) {
1.551 int idx = _first[k];
1.552 while (idx != -1) {
1.553 if (_data[idx].item == i) {
1.554 @@ -530,13 +557,13 @@
1.555 return -1;
1.556 }
1.557
1.558 - /// \brief Returns if \c item is in, has already been in, or has
1.559 - /// never been in the heap.
1.560 + /// \brief Return the state of an item.
1.561 ///
1.562 - /// This method returns PRE_HEAP if \c item has never been in the
1.563 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.564 - /// otherwise. In the latter case it is possible that \c item will
1.565 - /// get back to the heap again.
1.566 + /// This method returns \c PRE_HEAP if the given item has never
1.567 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.568 + /// and \c POST_HEAP otherwise.
1.569 + /// In the latter case it is possible that the item will get back
1.570 + /// to the heap again.
1.571 /// \param i The item.
1.572 State state(const Item &i) const {
1.573 int idx = _iim[i];