deba@703: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@703: * deba@703: * This file is a part of LEMON, a generic C++ optimization library. deba@703: * deba@703: * Copyright (C) 2003-2009 deba@703: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@703: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@703: * deba@703: * Permission to use, modify and distribute this software is granted deba@703: * provided that this copyright notice appears in all copies. For deba@703: * precise terms see the accompanying LICENSE file. deba@703: * deba@703: * This software is provided "AS IS" with no warranty of any kind, deba@703: * express or implied, and with no claim as to its suitability for any deba@703: * purpose. deba@703: * deba@703: */ deba@703: deba@703: #ifndef LEMON_BUCKET_HEAP_H deba@703: #define LEMON_BUCKET_HEAP_H deba@703: deba@703: ///\ingroup auxdat deba@703: ///\file deba@703: ///\brief Bucket Heap implementation. deba@703: deba@703: #include deba@703: #include deba@703: #include deba@703: deba@703: namespace lemon { deba@703: deba@704: namespace _bucket_heap_bits { deba@704: deba@705: template deba@704: struct DirectionTraits { deba@704: static bool less(int left, int right) { deba@704: return left < right; deba@704: } deba@704: static void increase(int& value) { deba@704: ++value; deba@704: } deba@704: }; deba@704: deba@704: template <> deba@704: struct DirectionTraits { deba@704: static bool less(int left, int right) { deba@704: return left > right; deba@704: } deba@704: static void increase(int& value) { deba@704: --value; deba@704: } deba@704: }; deba@704: deba@704: } deba@704: deba@703: /// \ingroup auxdat deba@703: /// deba@703: /// \brief A Bucket Heap implementation. deba@703: /// deba@703: /// This class implements the \e bucket \e heap data structure. A \e heap deba@703: /// is a data structure for storing items with specified values called \e deba@703: /// priorities in such a way that finding the item with minimum priority is deba@703: /// efficient. The bucket heap is very simple implementation, it can store deba@703: /// only integer priorities and it stores for each priority in the deba@703: /// \f$ [0..C) \f$ range a list of items. So it should be used only when deba@703: /// the priorities are small. It is not intended to use as dijkstra heap. deba@703: /// deba@705: /// \param IM A read and write Item int map, used internally deba@703: /// to handle the cross references. deba@705: /// \param MIN If the given parameter is false then instead of the deba@705: /// minimum value the maximum can be retrivied with the top() and deba@705: /// prio() member functions. deba@705: template deba@703: class BucketHeap { deba@703: deba@703: public: deba@703: /// \e deba@705: typedef typename IM::Key Item; deba@703: /// \e deba@703: typedef int Prio; deba@703: /// \e deba@703: typedef std::pair Pair; deba@703: /// \e deba@705: typedef IM ItemIntMap; deba@703: deba@704: private: deba@704: deba@705: typedef _bucket_heap_bits::DirectionTraits Direction; deba@704: deba@704: public: deba@704: deba@703: /// \brief Type to represent the items states. deba@703: /// deba@703: /// Each Item element have a state associated to it. It may be "in heap", deba@703: /// "pre heap" or "post heap". The latter two are indifferent from the deba@703: /// heap's point of view, but may be useful to the user. deba@703: /// deba@705: /// The item-int map must be initialized in such way that it assigns deba@705: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@703: enum State { deba@705: IN_HEAP = 0, ///< = 0. deba@705: PRE_HEAP = -1, ///< = -1. deba@705: POST_HEAP = -2 ///< = -2. deba@703: }; deba@703: deba@703: public: deba@703: /// \brief The constructor. deba@703: /// deba@703: /// The constructor. deba@705: /// \param map should be given to the constructor, since it is used deba@703: /// internally to handle the cross references. The value of the map deba@703: /// should be PRE_HEAP (-1) for each element. deba@705: explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} deba@703: deba@703: /// The number of items stored in the heap. deba@703: /// deba@703: /// \brief Returns the number of items stored in the heap. deba@705: int size() const { return _data.size(); } deba@703: deba@703: /// \brief Checks if the heap stores no items. deba@703: /// deba@703: /// Returns \c true if and only if the heap stores no items. deba@705: bool empty() const { return _data.empty(); } deba@703: deba@703: /// \brief Make empty this heap. deba@703: /// deba@703: /// Make empty this heap. It does not change the cross reference deba@703: /// map. If you want to reuse a heap what is not surely empty you deba@703: /// should first clear the heap and after that you should set the deba@703: /// cross reference map for each item to \c PRE_HEAP. deba@703: void clear() { deba@705: _data.clear(); _first.clear(); _minimum = 0; deba@703: } deba@703: deba@703: private: deba@703: deba@703: void relocate_last(int idx) { deba@705: if (idx + 1 < int(_data.size())) { deba@705: _data[idx] = _data.back(); deba@705: if (_data[idx].prev != -1) { deba@705: _data[_data[idx].prev].next = idx; deba@703: } else { deba@705: _first[_data[idx].value] = idx; deba@703: } deba@705: if (_data[idx].next != -1) { deba@705: _data[_data[idx].next].prev = idx; deba@703: } deba@705: _iim[_data[idx].item] = idx; deba@703: } deba@705: _data.pop_back(); deba@703: } deba@703: deba@703: void unlace(int idx) { deba@705: if (_data[idx].prev != -1) { deba@705: _data[_data[idx].prev].next = _data[idx].next; deba@703: } else { deba@705: _first[_data[idx].value] = _data[idx].next; deba@703: } deba@705: if (_data[idx].next != -1) { deba@705: _data[_data[idx].next].prev = _data[idx].prev; deba@703: } deba@703: } deba@703: deba@703: void lace(int idx) { deba@705: if (int(_first.size()) <= _data[idx].value) { deba@705: _first.resize(_data[idx].value + 1, -1); deba@703: } deba@705: _data[idx].next = _first[_data[idx].value]; deba@705: if (_data[idx].next != -1) { deba@705: _data[_data[idx].next].prev = idx; deba@703: } deba@705: _first[_data[idx].value] = idx; deba@705: _data[idx].prev = -1; deba@703: } deba@703: deba@703: public: deba@703: /// \brief Insert a pair of item and priority into the heap. deba@703: /// deba@703: /// Adds \c p.first to the heap with priority \c p.second. deba@703: /// \param p The pair to insert. deba@703: void push(const Pair& p) { deba@703: push(p.first, p.second); deba@703: } deba@703: deba@703: /// \brief Insert an item into the heap with the given priority. deba@703: /// deba@703: /// Adds \c i to the heap with priority \c p. deba@703: /// \param i The item to insert. deba@703: /// \param p The priority of the item. deba@703: void push(const Item &i, const Prio &p) { deba@705: int idx = _data.size(); deba@705: _iim[i] = idx; deba@705: _data.push_back(BucketItem(i, p)); deba@703: lace(idx); deba@705: if (Direction::less(p, _minimum)) { deba@705: _minimum = p; deba@703: } deba@703: } deba@703: deba@703: /// \brief Returns the item with minimum priority. deba@703: /// deba@703: /// This method returns the item with minimum priority. deba@703: /// \pre The heap must be nonempty. deba@703: Item top() const { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: return _data[_first[_minimum]].item; deba@703: } deba@703: deba@703: /// \brief Returns the minimum priority. deba@703: /// deba@703: /// It returns the minimum priority. deba@703: /// \pre The heap must be nonempty. deba@703: Prio prio() const { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: return _minimum; deba@703: } deba@703: deba@703: /// \brief Deletes the item with minimum priority. deba@703: /// deba@703: /// This method deletes the item with minimum priority from the heap. deba@703: /// \pre The heap must be non-empty. deba@703: void pop() { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: int idx = _first[_minimum]; deba@705: _iim[_data[idx].item] = -2; deba@703: unlace(idx); deba@703: relocate_last(idx); deba@703: } deba@703: deba@703: /// \brief Deletes \c i from the heap. deba@703: /// deba@703: /// This method deletes item \c i from the heap, if \c i was deba@703: /// already stored in the heap. deba@703: /// \param i The item to erase. deba@703: void erase(const Item &i) { deba@705: int idx = _iim[i]; deba@705: _iim[_data[idx].item] = -2; deba@703: unlace(idx); deba@703: relocate_last(idx); deba@703: } deba@703: deba@703: deba@703: /// \brief Returns the priority of \c i. deba@703: /// deba@703: /// This function returns the priority of item \c i. deba@703: /// \pre \c i must be in the heap. deba@703: /// \param i The item. deba@703: Prio operator[](const Item &i) const { deba@705: int idx = _iim[i]; deba@705: return _data[idx].value; deba@703: } deba@703: deba@703: /// \brief \c i gets to the heap with priority \c p independently deba@703: /// if \c i was already there. deba@703: /// deba@703: /// This method calls \ref push(\c i, \c p) if \c i is not stored deba@703: /// in the heap and sets the priority of \c i to \c p otherwise. deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void set(const Item &i, const Prio &p) { deba@705: int idx = _iim[i]; deba@703: if (idx < 0) { deba@704: push(i, p); deba@705: } else if (Direction::less(p, _data[idx].value)) { deba@704: decrease(i, p); deba@704: } else { deba@703: increase(i, p); deba@703: } deba@703: } deba@703: deba@703: /// \brief Decreases the priority of \c i to \c p. deba@703: /// deba@703: /// This method decreases the priority of item \c i to \c p. deba@703: /// \pre \c i must be stored in the heap with priority at least \c deba@703: /// p relative to \c Compare. deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void decrease(const Item &i, const Prio &p) { deba@705: int idx = _iim[i]; deba@703: unlace(idx); deba@705: _data[idx].value = p; deba@705: if (Direction::less(p, _minimum)) { deba@705: _minimum = p; deba@703: } deba@703: lace(idx); deba@703: } deba@703: deba@703: /// \brief Increases the priority of \c i to \c p. deba@703: /// deba@703: /// This method sets the priority of item \c i to \c p. deba@703: /// \pre \c i must be stored in the heap with priority at most \c deba@703: /// p relative to \c Compare. deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void increase(const Item &i, const Prio &p) { deba@705: int idx = _iim[i]; deba@703: unlace(idx); deba@705: _data[idx].value = p; deba@703: lace(idx); deba@703: } deba@703: deba@703: /// \brief Returns if \c item is in, has already been in, or has deba@703: /// never been in the heap. deba@703: /// deba@703: /// This method returns PRE_HEAP if \c item has never been in the deba@703: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@703: /// otherwise. In the latter case it is possible that \c item will deba@703: /// get back to the heap again. deba@703: /// \param i The item. deba@703: State state(const Item &i) const { deba@705: int idx = _iim[i]; deba@703: if (idx >= 0) idx = 0; deba@703: return State(idx); deba@703: } deba@703: deba@703: /// \brief Sets the state of the \c item in the heap. deba@703: /// deba@703: /// Sets the state of the \c item in the heap. It can be used to deba@703: /// manually clear the heap when it is important to achive the deba@703: /// better time complexity. deba@703: /// \param i The item. deba@703: /// \param st The state. It should not be \c IN_HEAP. deba@703: void state(const Item& i, State st) { deba@703: switch (st) { deba@703: case POST_HEAP: deba@703: case PRE_HEAP: deba@703: if (state(i) == IN_HEAP) { deba@703: erase(i); deba@703: } deba@705: _iim[i] = st; deba@703: break; deba@703: case IN_HEAP: deba@703: break; deba@703: } deba@703: } deba@703: deba@703: private: deba@703: deba@703: struct BucketItem { deba@703: BucketItem(const Item& _item, int _value) deba@703: : item(_item), value(_value) {} deba@703: deba@703: Item item; deba@703: int value; deba@703: deba@703: int prev, next; deba@703: }; deba@703: deba@705: ItemIntMap& _iim; deba@705: std::vector _first; deba@705: std::vector _data; deba@705: mutable int _minimum; deba@703: deba@703: }; // class BucketHeap deba@703: deba@703: /// \ingroup auxdat deba@703: /// deba@703: /// \brief A Simplified Bucket Heap implementation. deba@703: /// deba@703: /// This class implements a simplified \e bucket \e heap data deba@703: /// structure. It does not provide some functionality but it faster deba@703: /// and simplier data structure than the BucketHeap. The main deba@703: /// difference is that the BucketHeap stores for every key a double deba@703: /// linked list while this class stores just simple lists. In the deba@704: /// other way it does not support erasing each elements just the deba@703: /// minimal and it does not supports key increasing, decreasing. deba@703: /// deba@705: /// \param IM A read and write Item int map, used internally deba@703: /// to handle the cross references. deba@705: /// \param MIN If the given parameter is false then instead of the deba@705: /// minimum value the maximum can be retrivied with the top() and deba@705: /// prio() member functions. deba@703: /// deba@703: /// \sa BucketHeap deba@705: template deba@703: class SimpleBucketHeap { deba@703: deba@703: public: deba@705: typedef typename IM::Key Item; deba@703: typedef int Prio; deba@703: typedef std::pair Pair; deba@705: typedef IM ItemIntMap; deba@703: deba@704: private: deba@704: deba@705: typedef _bucket_heap_bits::DirectionTraits Direction; deba@704: deba@704: public: deba@704: deba@703: /// \brief Type to represent the items states. deba@703: /// deba@703: /// Each Item element have a state associated to it. It may be "in heap", deba@703: /// "pre heap" or "post heap". The latter two are indifferent from the deba@703: /// heap's point of view, but may be useful to the user. deba@703: /// deba@705: /// The item-int map must be initialized in such way that it assigns deba@705: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@703: enum State { deba@705: IN_HEAP = 0, ///< = 0. deba@705: PRE_HEAP = -1, ///< = -1. deba@705: POST_HEAP = -2 ///< = -2. deba@703: }; deba@703: deba@703: public: deba@703: deba@703: /// \brief The constructor. deba@703: /// deba@703: /// The constructor. deba@705: /// \param map should be given to the constructor, since it is used deba@703: /// internally to handle the cross references. The value of the map deba@703: /// should be PRE_HEAP (-1) for each element. deba@705: explicit SimpleBucketHeap(ItemIntMap &map) deba@705: : _iim(map), _free(-1), _num(0), _minimum(0) {} deba@703: deba@703: /// \brief Returns the number of items stored in the heap. deba@703: /// deba@703: /// The number of items stored in the heap. deba@705: int size() const { return _num; } deba@703: deba@703: /// \brief Checks if the heap stores no items. deba@703: /// deba@703: /// Returns \c true if and only if the heap stores no items. deba@705: bool empty() const { return _num == 0; } deba@703: deba@703: /// \brief Make empty this heap. deba@703: /// deba@703: /// Make empty this heap. It does not change the cross reference deba@703: /// map. If you want to reuse a heap what is not surely empty you deba@703: /// should first clear the heap and after that you should set the deba@703: /// cross reference map for each item to \c PRE_HEAP. deba@703: void clear() { deba@705: _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; deba@703: } deba@703: deba@703: /// \brief Insert a pair of item and priority into the heap. deba@703: /// deba@703: /// Adds \c p.first to the heap with priority \c p.second. deba@703: /// \param p The pair to insert. deba@703: void push(const Pair& p) { deba@703: push(p.first, p.second); deba@703: } deba@703: deba@703: /// \brief Insert an item into the heap with the given priority. deba@703: /// deba@703: /// Adds \c i to the heap with priority \c p. deba@703: /// \param i The item to insert. deba@703: /// \param p The priority of the item. deba@703: void push(const Item &i, const Prio &p) { deba@703: int idx; deba@705: if (_free == -1) { deba@705: idx = _data.size(); deba@705: _data.push_back(BucketItem(i)); deba@703: } else { deba@705: idx = _free; deba@705: _free = _data[idx].next; deba@705: _data[idx].item = i; deba@703: } deba@705: _iim[i] = idx; deba@705: if (p >= int(_first.size())) _first.resize(p + 1, -1); deba@705: _data[idx].next = _first[p]; deba@705: _first[p] = idx; deba@705: if (Direction::less(p, _minimum)) { deba@705: _minimum = p; deba@703: } deba@705: ++_num; deba@703: } deba@703: deba@703: /// \brief Returns the item with minimum priority. deba@703: /// deba@703: /// This method returns the item with minimum priority. deba@703: /// \pre The heap must be nonempty. deba@703: Item top() const { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: return _data[_first[_minimum]].item; deba@703: } deba@703: deba@703: /// \brief Returns the minimum priority. deba@703: /// deba@703: /// It returns the minimum priority. deba@703: /// \pre The heap must be nonempty. deba@703: Prio prio() const { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: return _minimum; deba@703: } deba@703: deba@703: /// \brief Deletes the item with minimum priority. deba@703: /// deba@703: /// This method deletes the item with minimum priority from the heap. deba@703: /// \pre The heap must be non-empty. deba@703: void pop() { deba@705: while (_first[_minimum] == -1) { deba@705: Direction::increase(_minimum); deba@703: } deba@705: int idx = _first[_minimum]; deba@705: _iim[_data[idx].item] = -2; deba@705: _first[_minimum] = _data[idx].next; deba@705: _data[idx].next = _free; deba@705: _free = idx; deba@705: --_num; deba@703: } deba@703: deba@703: /// \brief Returns the priority of \c i. deba@703: /// deba@703: /// This function returns the priority of item \c i. deba@703: /// \warning This operator is not a constant time function deba@703: /// because it scans the whole data structure to find the proper deba@703: /// value. deba@703: /// \pre \c i must be in the heap. deba@703: /// \param i The item. deba@703: Prio operator[](const Item &i) const { deba@705: for (int k = 0; k < _first.size(); ++k) { deba@705: int idx = _first[k]; deba@703: while (idx != -1) { deba@705: if (_data[idx].item == i) { deba@703: return k; deba@703: } deba@705: idx = _data[idx].next; deba@703: } deba@703: } deba@703: return -1; deba@703: } deba@703: deba@703: /// \brief Returns if \c item is in, has already been in, or has deba@703: /// never been in the heap. deba@703: /// deba@703: /// This method returns PRE_HEAP if \c item has never been in the deba@703: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@703: /// otherwise. In the latter case it is possible that \c item will deba@703: /// get back to the heap again. deba@703: /// \param i The item. deba@703: State state(const Item &i) const { deba@705: int idx = _iim[i]; deba@703: if (idx >= 0) idx = 0; deba@703: return State(idx); deba@703: } deba@703: deba@703: private: deba@703: deba@703: struct BucketItem { deba@703: BucketItem(const Item& _item) deba@703: : item(_item) {} deba@703: deba@703: Item item; deba@703: int next; deba@703: }; deba@703: deba@705: ItemIntMap& _iim; deba@705: std::vector _first; deba@705: std::vector _data; deba@705: int _free, _num; deba@705: mutable int _minimum; deba@703: deba@703: }; // class SimpleBucketHeap deba@703: deba@703: } deba@703: deba@703: #endif