deba@681: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@681: * deba@681: * This file is a part of LEMON, a generic C++ optimization library. deba@681: * deba@681: * Copyright (C) 2003-2009 deba@681: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@681: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@681: * deba@681: * Permission to use, modify and distribute this software is granted deba@681: * provided that this copyright notice appears in all copies. For deba@681: * precise terms see the accompanying LICENSE file. deba@681: * deba@681: * This software is provided "AS IS" with no warranty of any kind, deba@681: * express or implied, and with no claim as to its suitability for any deba@681: * purpose. deba@681: * deba@681: */ deba@681: deba@681: #ifndef LEMON_RADIX_HEAP_H deba@681: #define LEMON_RADIX_HEAP_H deba@681: deba@681: ///\ingroup auxdat deba@681: ///\file deba@681: ///\brief Radix Heap implementation. deba@681: deba@681: #include deba@681: #include deba@681: deba@681: namespace lemon { deba@681: deba@681: deba@681: /// \ingroup auxdata deba@681: /// deba@681: /// \brief A Radix Heap implementation. deba@681: /// deba@681: /// This class implements the \e radix \e heap data structure. A \e heap deba@681: /// is a data structure for storing items with specified values called \e deba@681: /// priorities in such a way that finding the item with minimum priority is deba@681: /// efficient. This heap type can store only items with \e int priority. deba@681: /// In a heap one can change the priority of an item, add or erase an deba@681: /// item, but the priority cannot be decreased under the last removed deba@681: /// item's priority. deba@681: /// deba@681: /// \param _ItemIntMap A read and writable Item int map, used internally deba@681: /// to handle the cross references. deba@681: /// deba@681: /// \see BinHeap deba@681: /// \see Dijkstra deba@681: template deba@681: class RadixHeap { deba@681: deba@681: public: deba@681: typedef typename _ItemIntMap::Key Item; deba@681: typedef int Prio; deba@681: typedef _ItemIntMap ItemIntMap; deba@681: deba@681: /// \brief Exception thrown by RadixHeap. deba@681: /// deba@681: /// This Exception is thrown when a smaller priority deba@681: /// is inserted into the \e RadixHeap then the last time erased. deba@681: /// \see RadixHeap deba@681: deba@681: class UnderFlowPriorityError : public Exception { deba@681: public: deba@681: virtual const char* what() const throw() { deba@681: return "lemon::RadixHeap::UnderFlowPriorityError"; deba@681: } deba@681: }; deba@681: deba@681: /// \brief Type to represent the items states. deba@681: /// deba@681: /// Each Item element have a state associated to it. It may be "in heap", deba@681: /// "pre heap" or "post heap". The latter two are indifferent from the deba@681: /// heap's point of view, but may be useful to the user. deba@681: /// deba@681: /// The ItemIntMap \e should be initialized in such way that it maps deba@681: /// PRE_HEAP (-1) to any element to be put in the heap... deba@681: enum State { deba@681: IN_HEAP = 0, deba@681: PRE_HEAP = -1, deba@681: POST_HEAP = -2 deba@681: }; deba@681: deba@681: private: deba@681: deba@681: struct RadixItem { deba@681: int prev, next, box; deba@681: Item item; deba@681: int prio; deba@681: RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {} deba@681: }; deba@681: deba@681: struct RadixBox { deba@681: int first; deba@681: int min, size; deba@681: RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {} deba@681: }; deba@681: deba@681: std::vector data; deba@681: std::vector boxes; deba@681: deba@681: ItemIntMap &iim; deba@681: deba@681: deba@681: public: deba@681: /// \brief The constructor. deba@681: /// deba@681: /// The constructor. deba@681: /// deba@681: /// \param _iim It should be given to the constructor, since it is used deba@681: /// internally to handle the cross references. The value of the map deba@681: /// should be PRE_HEAP (-1) for each element. deba@681: /// deba@681: /// \param minimal The initial minimal value of the heap. deba@681: /// \param capacity It determines the initial capacity of the heap. deba@681: RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) deba@681: : iim(_iim) { deba@681: boxes.push_back(RadixBox(minimal, 1)); deba@681: boxes.push_back(RadixBox(minimal + 1, 1)); deba@681: while (lower(boxes.size() - 1, capacity + minimal - 1)) { deba@681: extend(); deba@681: } deba@681: } deba@681: deba@681: /// The number of items stored in the heap. deba@681: /// deba@681: /// \brief Returns the number of items stored in the heap. deba@681: int size() const { return data.size(); } deba@681: /// \brief Checks if the heap stores no items. deba@681: /// deba@681: /// Returns \c true if and only if the heap stores no items. deba@681: bool empty() const { return data.empty(); } deba@681: deba@681: /// \brief Make empty this heap. deba@681: /// deba@681: /// Make empty this heap. It does not change the cross reference deba@681: /// map. If you want to reuse a heap what is not surely empty you deba@681: /// should first clear the heap and after that you should set the deba@681: /// cross reference map for each item to \c PRE_HEAP. deba@681: void clear(int minimal = 0, int capacity = 0) { deba@681: data.clear(); boxes.clear(); deba@681: boxes.push_back(RadixBox(minimal, 1)); deba@681: boxes.push_back(RadixBox(minimal + 1, 1)); deba@681: while (lower(boxes.size() - 1, capacity + minimal - 1)) { deba@681: extend(); deba@681: } deba@681: } deba@681: deba@681: private: deba@681: deba@681: bool upper(int box, Prio pr) { deba@681: return pr < boxes[box].min; deba@681: } deba@681: deba@681: bool lower(int box, Prio pr) { deba@681: return pr >= boxes[box].min + boxes[box].size; deba@681: } deba@681: deba@681: /// \brief Remove item from the box list. deba@681: void remove(int index) { deba@681: if (data[index].prev >= 0) { deba@681: data[data[index].prev].next = data[index].next; deba@681: } else { deba@681: boxes[data[index].box].first = data[index].next; deba@681: } deba@681: if (data[index].next >= 0) { deba@681: data[data[index].next].prev = data[index].prev; deba@681: } deba@681: } deba@681: deba@681: /// \brief Insert item into the box list. deba@681: void insert(int box, int index) { deba@681: if (boxes[box].first == -1) { deba@681: boxes[box].first = index; deba@681: data[index].next = data[index].prev = -1; deba@681: } else { deba@681: data[index].next = boxes[box].first; deba@681: data[boxes[box].first].prev = index; deba@681: data[index].prev = -1; deba@681: boxes[box].first = index; deba@681: } deba@681: data[index].box = box; deba@681: } deba@681: deba@681: /// \brief Add a new box to the box list. deba@681: void extend() { deba@681: int min = boxes.back().min + boxes.back().size; deba@681: int bs = 2 * boxes.back().size; deba@681: boxes.push_back(RadixBox(min, bs)); deba@681: } deba@681: deba@681: /// \brief Move an item up into the proper box. deba@681: void bubble_up(int index) { deba@681: if (!lower(data[index].box, data[index].prio)) return; deba@681: remove(index); deba@681: int box = findUp(data[index].box, data[index].prio); deba@681: insert(box, index); deba@681: } deba@681: deba@681: /// \brief Find up the proper box for the item with the given prio. deba@681: int findUp(int start, int pr) { deba@681: while (lower(start, pr)) { deba@681: if (++start == int(boxes.size())) { deba@681: extend(); deba@681: } deba@681: } deba@681: return start; deba@681: } deba@681: deba@681: /// \brief Move an item down into the proper box. deba@681: void bubble_down(int index) { deba@681: if (!upper(data[index].box, data[index].prio)) return; deba@681: remove(index); deba@681: int box = findDown(data[index].box, data[index].prio); deba@681: insert(box, index); deba@681: } deba@681: deba@681: /// \brief Find up the proper box for the item with the given prio. deba@681: int findDown(int start, int pr) { deba@681: while (upper(start, pr)) { deba@681: if (--start < 0) throw UnderFlowPriorityError(); deba@681: } deba@681: return start; deba@681: } deba@681: deba@681: /// \brief Find the first not empty box. deba@681: int findFirst() { deba@681: int first = 0; deba@681: while (boxes[first].first == -1) ++first; deba@681: return first; deba@681: } deba@681: deba@681: /// \brief Gives back the minimal prio of the box. deba@681: int minValue(int box) { deba@681: int min = data[boxes[box].first].prio; deba@681: for (int k = boxes[box].first; k != -1; k = data[k].next) { deba@681: if (data[k].prio < min) min = data[k].prio; deba@681: } deba@681: return min; deba@681: } deba@681: deba@681: /// \brief Rearrange the items of the heap and makes the deba@681: /// first box not empty. deba@681: void moveDown() { deba@681: int box = findFirst(); deba@681: if (box == 0) return; deba@681: int min = minValue(box); deba@681: for (int i = 0; i <= box; ++i) { deba@681: boxes[i].min = min; deba@681: min += boxes[i].size; deba@681: } deba@681: int curr = boxes[box].first, next; deba@681: while (curr != -1) { deba@681: next = data[curr].next; deba@681: bubble_down(curr); deba@681: curr = next; deba@681: } deba@681: } deba@681: deba@681: void relocate_last(int index) { deba@681: if (index != int(data.size()) - 1) { deba@681: data[index] = data.back(); deba@681: if (data[index].prev != -1) { deba@681: data[data[index].prev].next = index; deba@681: } else { deba@681: boxes[data[index].box].first = index; deba@681: } deba@681: if (data[index].next != -1) { deba@681: data[data[index].next].prev = index; deba@681: } deba@681: iim[data[index].item] = index; deba@681: } deba@681: data.pop_back(); deba@681: } deba@681: deba@681: public: deba@681: deba@681: /// \brief Insert an item into the heap with the given priority. deba@681: /// deba@681: /// Adds \c i to the heap with priority \c p. deba@681: /// \param i The item to insert. deba@681: /// \param p The priority of the item. deba@681: void push(const Item &i, const Prio &p) { deba@681: int n = data.size(); deba@681: iim.set(i, n); deba@681: data.push_back(RadixItem(i, p)); deba@681: while (lower(boxes.size() - 1, p)) { deba@681: extend(); deba@681: } deba@681: int box = findDown(boxes.size() - 1, p); deba@681: insert(box, n); deba@681: } deba@681: deba@681: /// \brief Returns the item with minimum priority. deba@681: /// deba@681: /// This method returns the item with minimum priority. deba@681: /// \pre The heap must be nonempty. deba@681: Item top() const { deba@681: const_cast&>(*this).moveDown(); deba@681: return data[boxes[0].first].item; deba@681: } deba@681: deba@681: /// \brief Returns the minimum priority. deba@681: /// deba@681: /// It returns the minimum priority. deba@681: /// \pre The heap must be nonempty. deba@681: Prio prio() const { deba@681: const_cast&>(*this).moveDown(); deba@681: return data[boxes[0].first].prio; deba@681: } deba@681: deba@681: /// \brief Deletes the item with minimum priority. deba@681: /// deba@681: /// This method deletes the item with minimum priority. deba@681: /// \pre The heap must be non-empty. deba@681: void pop() { deba@681: moveDown(); deba@681: int index = boxes[0].first; deba@681: iim[data[index].item] = POST_HEAP; deba@681: remove(index); deba@681: relocate_last(index); deba@681: } deba@681: deba@681: /// \brief Deletes \c i from the heap. deba@681: /// deba@681: /// This method deletes item \c i from the heap, if \c i was deba@681: /// already stored in the heap. deba@681: /// \param i The item to erase. deba@681: void erase(const Item &i) { deba@681: int index = iim[i]; deba@681: iim[i] = POST_HEAP; deba@681: remove(index); deba@681: relocate_last(index); deba@681: } deba@681: deba@681: /// \brief Returns the priority of \c i. deba@681: /// deba@681: /// This function returns the priority of item \c i. deba@681: /// \pre \c i must be in the heap. deba@681: /// \param i The item. deba@681: Prio operator[](const Item &i) const { deba@681: int idx = iim[i]; deba@681: return data[idx].prio; deba@681: } deba@681: deba@681: /// \brief \c i gets to the heap with priority \c p independently deba@681: /// if \c i was already there. deba@681: /// deba@681: /// This method calls \ref push(\c i, \c p) if \c i is not stored deba@681: /// in the heap and sets the priority of \c i to \c p otherwise. deba@681: /// It may throw an \e UnderFlowPriorityException. deba@681: /// \param i The item. deba@681: /// \param p The priority. deba@681: void set(const Item &i, const Prio &p) { deba@681: int idx = iim[i]; deba@681: if( idx < 0 ) { deba@681: push(i, p); deba@681: } deba@681: else if( p >= data[idx].prio ) { deba@681: data[idx].prio = p; deba@681: bubble_up(idx); deba@681: } else { deba@681: data[idx].prio = p; deba@681: bubble_down(idx); deba@681: } deba@681: } deba@681: deba@681: deba@681: /// \brief Decreases the priority of \c i to \c p. deba@681: /// deba@681: /// This method decreases the priority of item \c i to \c p. deba@681: /// \pre \c i must be stored in the heap with priority at least \c p, and deba@681: /// \c should be greater or equal to the last removed item's priority. deba@681: /// \param i The item. deba@681: /// \param p The priority. deba@681: void decrease(const Item &i, const Prio &p) { deba@681: int idx = iim[i]; deba@681: data[idx].prio = p; deba@681: bubble_down(idx); deba@681: } deba@681: deba@681: /// \brief Increases the priority of \c i to \c p. deba@681: /// deba@681: /// This method sets the priority of item \c i to \c p. deba@681: /// \pre \c i must be stored in the heap with priority at most \c p deba@681: /// \param i The item. deba@681: /// \param p The priority. deba@681: void increase(const Item &i, const Prio &p) { deba@681: int idx = iim[i]; deba@681: data[idx].prio = p; deba@681: bubble_up(idx); deba@681: } deba@681: deba@681: /// \brief Returns if \c item is in, has already been in, or has deba@681: /// never been in the heap. deba@681: /// deba@681: /// This method returns PRE_HEAP if \c item has never been in the deba@681: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@681: /// otherwise. In the latter case it is possible that \c item will deba@681: /// get back to the heap again. deba@681: /// \param i The item. deba@681: State state(const Item &i) const { deba@681: int s = iim[i]; deba@681: if( s >= 0 ) s = 0; deba@681: return State(s); deba@681: } deba@681: deba@681: /// \brief Sets the state of the \c item in the heap. deba@681: /// deba@681: /// Sets the state of the \c item in the heap. It can be used to deba@681: /// manually clear the heap when it is important to achive the deba@681: /// better time complexity. deba@681: /// \param i The item. deba@681: /// \param st The state. It should not be \c IN_HEAP. deba@681: void state(const Item& i, State st) { deba@681: switch (st) { deba@681: case POST_HEAP: deba@681: case PRE_HEAP: deba@681: if (state(i) == IN_HEAP) { deba@681: erase(i); deba@681: } deba@681: iim[i] = st; deba@681: break; deba@681: case IN_HEAP: deba@681: break; deba@681: } deba@681: } deba@681: deba@681: }; // class RadixHeap deba@681: deba@681: } // namespace lemon deba@681: deba@681: #endif // LEMON_RADIX_HEAP_H