alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@100: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. alpar@100: * alpar@463: * Copyright (C) 2003-2009 alpar@100: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@100: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@100: * alpar@100: * Permission to use, modify and distribute this software is granted alpar@100: * provided that this copyright notice appears in all copies. For alpar@100: * precise terms see the accompanying LICENSE file. alpar@100: * alpar@100: * This software is provided "AS IS" with no warranty of any kind, alpar@100: * express or implied, and with no claim as to its suitability for any alpar@100: * purpose. alpar@100: * alpar@100: */ alpar@100: alpar@100: #ifndef LEMON_BIN_HEAP_H alpar@100: #define LEMON_BIN_HEAP_H alpar@100: kpeter@757: ///\ingroup heaps alpar@100: ///\file kpeter@756: ///\brief Binary heap implementation. alpar@100: alpar@100: #include alpar@100: #include alpar@100: #include alpar@100: alpar@100: namespace lemon { alpar@100: kpeter@757: /// \ingroup heaps alpar@100: /// kpeter@756: /// \brief Binary heap data structure. alpar@100: /// kpeter@756: /// This class implements the \e binary \e heap data structure. kpeter@756: /// It fully conforms to the \ref concepts::Heap "heap concept". deba@730: /// kpeter@756: /// \tparam PR Type of the priorities of the items. kpeter@756: /// \tparam IM A read-writable item map with \c int values, used kpeter@756: /// internally to handle the cross references. kpeter@756: /// \tparam CMP A functor class for comparing the priorities. kpeter@756: /// The default is \c std::less. kpeter@756: #ifdef DOXYGEN kpeter@756: template kpeter@756: #else deba@730: template > kpeter@756: #endif alpar@100: class BinHeap { kpeter@756: public: alpar@100: kpeter@756: /// Type of the item-int map. kpeter@606: typedef IM ItemIntMap; kpeter@756: /// Type of the priorities. kpeter@606: typedef PR Prio; kpeter@756: /// Type of the items stored in the heap. alpar@100: typedef typename ItemIntMap::Key Item; kpeter@756: /// Type of the item-priority pairs. alpar@100: typedef std::pair Pair; kpeter@756: /// Functor type for comparing the priorities. deba@730: typedef CMP Compare; alpar@100: kpeter@756: /// \brief Type to represent the states of the items. alpar@100: /// kpeter@756: /// Each item has a state associated to it. It can be "in heap", kpeter@756: /// "pre-heap" or "post-heap". The latter two are indifferent from the alpar@100: /// heap's point of view, but may be useful to the user. alpar@100: /// kpeter@606: /// The item-int map must be initialized in such way that it assigns kpeter@606: /// \c PRE_HEAP (-1) to any element to be put in the heap. alpar@100: enum State { kpeter@631: IN_HEAP = 0, ///< = 0. kpeter@631: PRE_HEAP = -1, ///< = -1. kpeter@631: POST_HEAP = -2 ///< = -2. alpar@100: }; alpar@100: alpar@100: private: kpeter@606: std::vector _data; kpeter@606: Compare _comp; kpeter@606: ItemIntMap &_iim; alpar@100: alpar@100: public: kpeter@756: kpeter@756: /// \brief Constructor. alpar@100: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. kpeter@606: explicit BinHeap(ItemIntMap &map) : _iim(map) {} alpar@209: kpeter@756: /// \brief Constructor. alpar@100: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. kpeter@756: /// \param comp The function object used for comparing the priorities. kpeter@606: BinHeap(ItemIntMap &map, const Compare &comp) kpeter@606: : _iim(map), _comp(comp) {} alpar@100: alpar@100: kpeter@756: /// \brief The number of items stored in the heap. alpar@100: /// kpeter@756: /// This function returns the number of items stored in the heap. kpeter@606: int size() const { return _data.size(); } alpar@209: kpeter@756: /// \brief Check if the heap is empty. alpar@100: /// kpeter@756: /// This function returns \c true if the heap is empty. kpeter@606: bool empty() const { return _data.empty(); } alpar@100: kpeter@756: /// \brief Make the heap empty. alpar@209: /// kpeter@756: /// This functon makes the heap empty. kpeter@756: /// It does not change the cross reference map. If you want to reuse kpeter@756: /// a heap that is not surely empty, you should first clear it and kpeter@756: /// then you should set the cross reference map to \c PRE_HEAP kpeter@756: /// for each item. alpar@209: void clear() { kpeter@606: _data.clear(); alpar@100: } alpar@100: alpar@100: private: alpar@100: static int parent(int i) { return (i-1)/2; } alpar@100: kpeter@758: static int secondChild(int i) { return 2*i+2; } alpar@100: bool less(const Pair &p1, const Pair &p2) const { kpeter@606: return _comp(p1.second, p2.second); alpar@100: } alpar@100: kpeter@758: int bubbleUp(int hole, Pair p) { alpar@100: int par = parent(hole); kpeter@606: while( hole>0 && less(p,_data[par]) ) { kpeter@606: move(_data[par],hole); alpar@209: hole = par; alpar@209: par = parent(hole); alpar@100: } alpar@100: move(p, hole); alpar@100: return hole; alpar@100: } alpar@100: kpeter@758: int bubbleDown(int hole, Pair p, int length) { kpeter@758: int child = secondChild(hole); alpar@100: while(child < length) { kpeter@606: if( less(_data[child-1], _data[child]) ) { alpar@209: --child; alpar@209: } kpeter@606: if( !less(_data[child], p) ) alpar@209: goto ok; kpeter@606: move(_data[child], hole); alpar@209: hole = child; kpeter@758: child = secondChild(hole); alpar@100: } alpar@100: child--; kpeter@606: if( child 0) { kpeter@758: bubbleDown(0, _data[n], n); alpar@100: } kpeter@606: _data.pop_back(); alpar@100: } alpar@100: kpeter@756: /// \brief Remove the given item from the heap. alpar@100: /// kpeter@756: /// This function removes the given item from the heap if it is kpeter@756: /// already stored. kpeter@756: /// \param i The item to delete. kpeter@756: /// \pre \e i must be in the heap. alpar@100: void erase(const Item &i) { kpeter@606: int h = _iim[i]; kpeter@606: int n = _data.size()-1; kpeter@606: _iim.set(_data[h].first, POST_HEAP); alpar@100: if( h < n ) { kpeter@758: if ( bubbleUp(h, _data[n]) == h) { kpeter@758: bubbleDown(h, _data[n], n); alpar@209: } alpar@100: } kpeter@606: _data.pop_back(); alpar@100: } alpar@100: kpeter@756: /// \brief The priority of the given item. alpar@100: /// kpeter@756: /// This function returns the priority of the given item. kpeter@606: /// \param i The item. kpeter@756: /// \pre \e i must be in the heap. alpar@100: Prio operator[](const Item &i) const { kpeter@606: int idx = _iim[i]; kpeter@606: return _data[idx].second; alpar@100: } alpar@100: kpeter@756: /// \brief Set the priority of an item or insert it, if it is kpeter@756: /// not stored in the heap. alpar@100: /// kpeter@756: /// This method sets the priority of the given item if it is kpeter@756: /// already stored in the heap. Otherwise it inserts the given kpeter@756: /// item into the heap with the given priority. alpar@100: /// \param i The item. alpar@100: /// \param p The priority. alpar@100: void set(const Item &i, const Prio &p) { kpeter@606: int idx = _iim[i]; alpar@100: if( idx < 0 ) { alpar@209: push(i,p); alpar@100: } kpeter@606: else if( _comp(p, _data[idx].second) ) { kpeter@758: bubbleUp(idx, Pair(i,p)); alpar@100: } alpar@100: else { kpeter@758: bubbleDown(idx, Pair(i,p), _data.size()); alpar@100: } alpar@100: } alpar@100: kpeter@756: /// \brief Decrease the priority of an item to the given value. alpar@100: /// kpeter@756: /// This function decreases the priority of an item to the given value. kpeter@606: /// \param i The item. kpeter@606: /// \param p The priority. kpeter@756: /// \pre \e i must be stored in the heap with priority at least \e p. alpar@100: void decrease(const Item &i, const Prio &p) { kpeter@606: int idx = _iim[i]; kpeter@758: bubbleUp(idx, Pair(i,p)); alpar@100: } alpar@209: kpeter@756: /// \brief Increase the priority of an item to the given value. alpar@100: /// kpeter@756: /// This function increases the priority of an item to the given value. kpeter@606: /// \param i The item. kpeter@606: /// \param p The priority. kpeter@756: /// \pre \e i must be stored in the heap with priority at most \e p. alpar@100: void increase(const Item &i, const Prio &p) { kpeter@606: int idx = _iim[i]; kpeter@758: bubbleDown(idx, Pair(i,p), _data.size()); alpar@100: } alpar@100: kpeter@756: /// \brief Return the state of an item. alpar@100: /// kpeter@756: /// This method returns \c PRE_HEAP if the given item has never kpeter@756: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@756: /// and \c POST_HEAP otherwise. kpeter@756: /// In the latter case it is possible that the item will get back kpeter@756: /// to the heap again. alpar@100: /// \param i The item. alpar@100: State state(const Item &i) const { kpeter@606: int s = _iim[i]; alpar@100: if( s>=0 ) alpar@209: s=0; alpar@100: return State(s); alpar@100: } alpar@100: kpeter@756: /// \brief Set the state of an item in the heap. alpar@100: /// kpeter@756: /// This function sets the state of the given item in the heap. kpeter@756: /// It can be used to manually clear the heap when it is important kpeter@756: /// to achive better time complexity. alpar@100: /// \param i The item. alpar@209: /// \param st The state. It should not be \c IN_HEAP. alpar@100: void state(const Item& i, State st) { alpar@100: switch (st) { alpar@100: case POST_HEAP: alpar@100: case PRE_HEAP: alpar@100: if (state(i) == IN_HEAP) { alpar@100: erase(i); alpar@100: } kpeter@606: _iim[i] = st; alpar@100: break; alpar@100: case IN_HEAP: alpar@100: break; alpar@100: } alpar@100: } alpar@100: kpeter@756: /// \brief Replace an item in the heap. alpar@100: /// kpeter@756: /// This function replaces item \c i with item \c j. kpeter@756: /// Item \c i must be in the heap, while \c j must be out of the heap. kpeter@756: /// After calling this method, item \c i will be out of the kpeter@756: /// heap and \c j will be in the heap with the same prioriority kpeter@756: /// as item \c i had before. alpar@100: void replace(const Item& i, const Item& j) { kpeter@606: int idx = _iim[i]; kpeter@606: _iim.set(i, _iim[j]); kpeter@606: _iim.set(j, idx); kpeter@606: _data[idx].first = j; alpar@100: } alpar@100: alpar@100: }; // class BinHeap alpar@209: alpar@100: } // namespace lemon alpar@100: alpar@100: #endif // LEMON_BIN_HEAP_H