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_RADIX_HEAP_H deba@703: #define LEMON_RADIX_HEAP_H deba@703: deba@703: ///\ingroup auxdat deba@703: ///\file deba@703: ///\brief Radix Heap implementation. deba@703: deba@703: #include deba@703: #include deba@703: deba@703: namespace lemon { deba@703: deba@703: deba@703: /// \ingroup auxdata deba@703: /// deba@703: /// \brief A Radix Heap implementation. deba@703: /// deba@703: /// This class implements the \e radix \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. This heap type can store only items with \e int priority. deba@703: /// In a heap one can change the priority of an item, add or erase an deba@703: /// item, but the priority cannot be decreased under the last removed deba@703: /// item's priority. deba@703: /// deba@703: /// \param _ItemIntMap A read and writable Item int map, used internally deba@703: /// to handle the cross references. deba@703: /// deba@703: /// \see BinHeap deba@703: /// \see Dijkstra deba@703: template deba@703: class RadixHeap { deba@703: deba@703: public: deba@703: typedef typename _ItemIntMap::Key Item; deba@703: typedef int Prio; deba@703: typedef _ItemIntMap ItemIntMap; deba@703: deba@703: /// \brief Exception thrown by RadixHeap. deba@703: /// deba@703: /// This Exception is thrown when a smaller priority deba@703: /// is inserted into the \e RadixHeap then the last time erased. deba@703: /// \see RadixHeap deba@703: deba@703: class UnderFlowPriorityError : public Exception { deba@703: public: deba@703: virtual const char* what() const throw() { deba@703: return "lemon::RadixHeap::UnderFlowPriorityError"; deba@703: } deba@703: }; deba@703: 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@703: /// The ItemIntMap \e should be initialized in such way that it maps deba@703: /// PRE_HEAP (-1) to any element to be put in the heap... deba@703: enum State { deba@703: IN_HEAP = 0, deba@703: PRE_HEAP = -1, deba@703: POST_HEAP = -2 deba@703: }; deba@703: deba@703: private: deba@703: deba@703: struct RadixItem { deba@703: int prev, next, box; deba@703: Item item; deba@703: int prio; deba@703: RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {} deba@703: }; deba@703: deba@703: struct RadixBox { deba@703: int first; deba@703: int min, size; deba@703: RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {} deba@703: }; deba@703: deba@703: std::vector data; deba@703: std::vector boxes; deba@703: deba@703: ItemIntMap &iim; deba@703: deba@703: deba@703: public: deba@703: /// \brief The constructor. deba@703: /// deba@703: /// The constructor. deba@703: /// deba@703: /// \param _iim It 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@703: /// deba@703: /// \param minimal The initial minimal value of the heap. deba@703: /// \param capacity It determines the initial capacity of the heap. deba@703: RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) deba@703: : iim(_iim) { deba@703: boxes.push_back(RadixBox(minimal, 1)); deba@703: boxes.push_back(RadixBox(minimal + 1, 1)); deba@703: while (lower(boxes.size() - 1, capacity + minimal - 1)) { deba@703: extend(); deba@703: } deba@703: } 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@703: int size() const { return data.size(); } 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@703: 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(int minimal = 0, int capacity = 0) { deba@703: data.clear(); boxes.clear(); deba@703: boxes.push_back(RadixBox(minimal, 1)); deba@703: boxes.push_back(RadixBox(minimal + 1, 1)); deba@703: while (lower(boxes.size() - 1, capacity + minimal - 1)) { deba@703: extend(); deba@703: } deba@703: } deba@703: deba@703: private: deba@703: deba@703: bool upper(int box, Prio pr) { deba@703: return pr < boxes[box].min; deba@703: } deba@703: deba@703: bool lower(int box, Prio pr) { deba@703: return pr >= boxes[box].min + boxes[box].size; deba@703: } deba@703: deba@703: /// \brief Remove item from the box list. deba@703: void remove(int index) { deba@703: if (data[index].prev >= 0) { deba@703: data[data[index].prev].next = data[index].next; deba@703: } else { deba@703: boxes[data[index].box].first = data[index].next; deba@703: } deba@703: if (data[index].next >= 0) { deba@703: data[data[index].next].prev = data[index].prev; deba@703: } deba@703: } deba@703: deba@703: /// \brief Insert item into the box list. deba@703: void insert(int box, int index) { deba@703: if (boxes[box].first == -1) { deba@703: boxes[box].first = index; deba@703: data[index].next = data[index].prev = -1; deba@703: } else { deba@703: data[index].next = boxes[box].first; deba@703: data[boxes[box].first].prev = index; deba@703: data[index].prev = -1; deba@703: boxes[box].first = index; deba@703: } deba@703: data[index].box = box; deba@703: } deba@703: deba@703: /// \brief Add a new box to the box list. deba@703: void extend() { deba@703: int min = boxes.back().min + boxes.back().size; deba@703: int bs = 2 * boxes.back().size; deba@703: boxes.push_back(RadixBox(min, bs)); deba@703: } deba@703: deba@703: /// \brief Move an item up into the proper box. deba@703: void bubble_up(int index) { deba@703: if (!lower(data[index].box, data[index].prio)) return; deba@703: remove(index); deba@703: int box = findUp(data[index].box, data[index].prio); deba@703: insert(box, index); deba@703: } deba@703: deba@703: /// \brief Find up the proper box for the item with the given prio. deba@703: int findUp(int start, int pr) { deba@703: while (lower(start, pr)) { deba@703: if (++start == int(boxes.size())) { deba@703: extend(); deba@703: } deba@703: } deba@703: return start; deba@703: } deba@703: deba@703: /// \brief Move an item down into the proper box. deba@703: void bubble_down(int index) { deba@703: if (!upper(data[index].box, data[index].prio)) return; deba@703: remove(index); deba@703: int box = findDown(data[index].box, data[index].prio); deba@703: insert(box, index); deba@703: } deba@703: deba@703: /// \brief Find up the proper box for the item with the given prio. deba@703: int findDown(int start, int pr) { deba@703: while (upper(start, pr)) { deba@703: if (--start < 0) throw UnderFlowPriorityError(); deba@703: } deba@703: return start; deba@703: } deba@703: deba@703: /// \brief Find the first not empty box. deba@703: int findFirst() { deba@703: int first = 0; deba@703: while (boxes[first].first == -1) ++first; deba@703: return first; deba@703: } deba@703: deba@703: /// \brief Gives back the minimal prio of the box. deba@703: int minValue(int box) { deba@703: int min = data[boxes[box].first].prio; deba@703: for (int k = boxes[box].first; k != -1; k = data[k].next) { deba@703: if (data[k].prio < min) min = data[k].prio; deba@703: } deba@703: return min; deba@703: } deba@703: deba@703: /// \brief Rearrange the items of the heap and makes the deba@703: /// first box not empty. deba@703: void moveDown() { deba@703: int box = findFirst(); deba@703: if (box == 0) return; deba@703: int min = minValue(box); deba@703: for (int i = 0; i <= box; ++i) { deba@703: boxes[i].min = min; deba@703: min += boxes[i].size; deba@703: } deba@703: int curr = boxes[box].first, next; deba@703: while (curr != -1) { deba@703: next = data[curr].next; deba@703: bubble_down(curr); deba@703: curr = next; deba@703: } deba@703: } deba@703: deba@703: void relocate_last(int index) { deba@703: if (index != int(data.size()) - 1) { deba@703: data[index] = data.back(); deba@703: if (data[index].prev != -1) { deba@703: data[data[index].prev].next = index; deba@703: } else { deba@703: boxes[data[index].box].first = index; deba@703: } deba@703: if (data[index].next != -1) { deba@703: data[data[index].next].prev = index; deba@703: } deba@703: iim[data[index].item] = index; deba@703: } deba@703: data.pop_back(); deba@703: } deba@703: deba@703: public: 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 n = data.size(); deba@703: iim.set(i, n); deba@703: data.push_back(RadixItem(i, p)); deba@703: while (lower(boxes.size() - 1, p)) { deba@703: extend(); deba@703: } deba@703: int box = findDown(boxes.size() - 1, p); deba@703: insert(box, n); 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@703: const_cast&>(*this).moveDown(); deba@703: return data[boxes[0].first].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@703: const_cast&>(*this).moveDown(); deba@703: return data[boxes[0].first].prio; 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. deba@703: /// \pre The heap must be non-empty. deba@703: void pop() { deba@703: moveDown(); deba@703: int index = boxes[0].first; deba@703: iim[data[index].item] = POST_HEAP; deba@703: remove(index); deba@703: relocate_last(index); 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@703: int index = iim[i]; deba@703: iim[i] = POST_HEAP; deba@703: remove(index); deba@703: relocate_last(index); 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@703: int idx = iim[i]; deba@703: return data[idx].prio; 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: /// It may throw an \e UnderFlowPriorityException. deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void set(const Item &i, const Prio &p) { deba@703: int idx = iim[i]; deba@703: if( idx < 0 ) { deba@703: push(i, p); deba@703: } deba@703: else if( p >= data[idx].prio ) { deba@703: data[idx].prio = p; deba@703: bubble_up(idx); deba@703: } else { deba@703: data[idx].prio = p; deba@703: bubble_down(idx); deba@703: } 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 p, and deba@703: /// \c should be greater or equal to the last removed item's priority. deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void decrease(const Item &i, const Prio &p) { deba@703: int idx = iim[i]; deba@703: data[idx].prio = p; deba@703: bubble_down(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 p deba@703: /// \param i The item. deba@703: /// \param p The priority. deba@703: void increase(const Item &i, const Prio &p) { deba@703: int idx = iim[i]; deba@703: data[idx].prio = p; deba@703: bubble_up(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@703: int s = iim[i]; deba@703: if( s >= 0 ) s = 0; deba@703: return State(s); 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@703: iim[i] = st; deba@703: break; deba@703: case IN_HEAP: deba@703: break; deba@703: } deba@703: } deba@703: deba@703: }; // class RadixHeap deba@703: deba@703: } // namespace lemon deba@703: deba@703: #endif // LEMON_RADIX_HEAP_H