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