kpeter@703: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@701: * kpeter@703: * This file is a part of LEMON, a generic C++ optimization library. kpeter@701: * kpeter@703: * Copyright (C) 2003-2009 kpeter@701: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@701: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@701: * kpeter@701: * Permission to use, modify and distribute this software is granted kpeter@701: * provided that this copyright notice appears in all copies. For kpeter@701: * precise terms see the accompanying LICENSE file. kpeter@701: * kpeter@701: * This software is provided "AS IS" with no warranty of any kind, kpeter@701: * express or implied, and with no claim as to its suitability for any kpeter@701: * purpose. kpeter@701: * kpeter@701: */ kpeter@701: kpeter@701: #ifndef LEMON_KARY_HEAP_H kpeter@701: #define LEMON_KARY_HEAP_H kpeter@701: kpeter@703: ///\ingroup heaps kpeter@701: ///\file kpeter@703: ///\brief Fourary heap implementation. kpeter@701: kpeter@701: #include kpeter@701: #include kpeter@701: #include kpeter@701: kpeter@701: namespace lemon { kpeter@701: kpeter@703: /// \ingroup heaps kpeter@701: /// kpeter@703: ///\brief K-ary heap data structure. kpeter@701: /// kpeter@703: /// This class implements the \e K-ary \e heap data structure. kpeter@703: /// It fully conforms to the \ref concepts::Heap "heap concept". kpeter@701: /// kpeter@703: /// The \ref KaryHeap "K-ary heap" is a generalization of the kpeter@703: /// \ref BinHeap "binary heap" structure, its nodes have at most kpeter@703: /// \c K children, instead of two. kpeter@703: /// \ref BinHeap and \ref FouraryHeap are specialized implementations kpeter@703: /// of this structure for K=2 and K=4, respectively. kpeter@701: /// kpeter@703: /// \tparam PR Type of the priorities of the items. kpeter@703: /// \tparam IM A read-writable item map with \c int values, used kpeter@703: /// internally to handle the cross references. kpeter@704: /// \tparam K The degree of the heap, each node have at most \e K kpeter@704: /// children. The default is 16. Powers of two are suggested to use kpeter@704: /// so that the multiplications and divisions needed to traverse the kpeter@704: /// nodes of the heap could be performed faster. kpeter@703: /// \tparam CMP A functor class for comparing the priorities. kpeter@703: /// The default is \c std::less. kpeter@703: /// kpeter@703: ///\sa BinHeap kpeter@703: ///\sa FouraryHeap kpeter@703: #ifdef DOXYGEN kpeter@704: template kpeter@703: #else kpeter@704: template > kpeter@703: #endif kpeter@703: class KaryHeap { kpeter@703: public: kpeter@703: /// Type of the item-int map. kpeter@703: typedef IM ItemIntMap; kpeter@703: /// Type of the priorities. kpeter@703: typedef PR Prio; kpeter@703: /// Type of the items stored in the heap. kpeter@703: typedef typename ItemIntMap::Key Item; kpeter@703: /// Type of the item-priority pairs. kpeter@703: typedef std::pair Pair; kpeter@703: /// Functor type for comparing the priorities. kpeter@703: typedef CMP Compare; kpeter@701: kpeter@703: /// \brief Type to represent the states of the items. kpeter@701: /// kpeter@703: /// Each item has a state associated to it. It can be "in heap", kpeter@703: /// "pre-heap" or "post-heap". The latter two are indifferent from the kpeter@701: /// heap's point of view, but may be useful to the user. kpeter@701: /// kpeter@703: /// The item-int map must be initialized in such way that it assigns kpeter@703: /// \c PRE_HEAP (-1) to any element to be put in the heap. kpeter@701: enum State { kpeter@703: IN_HEAP = 0, ///< = 0. kpeter@703: PRE_HEAP = -1, ///< = -1. kpeter@703: POST_HEAP = -2 ///< = -2. kpeter@701: }; kpeter@701: kpeter@701: private: kpeter@703: std::vector _data; kpeter@703: Compare _comp; kpeter@703: ItemIntMap &_iim; kpeter@701: kpeter@701: public: kpeter@703: /// \brief Constructor. kpeter@701: /// kpeter@703: /// Constructor. kpeter@703: /// \param map A map that assigns \c int values to the items. kpeter@703: /// It is used internally to handle the cross references. kpeter@703: /// The assigned value must be \c PRE_HEAP (-1) for each item. kpeter@704: explicit KaryHeap(ItemIntMap &map) : _iim(map) {} kpeter@701: kpeter@703: /// \brief Constructor. kpeter@701: /// kpeter@703: /// Constructor. kpeter@703: /// \param map A map that assigns \c int values to the items. kpeter@703: /// It is used internally to handle the cross references. kpeter@703: /// The assigned value must be \c PRE_HEAP (-1) for each item. kpeter@703: /// \param comp The function object used for comparing the priorities. kpeter@704: KaryHeap(ItemIntMap &map, const Compare &comp) kpeter@704: : _iim(map), _comp(comp) {} kpeter@703: kpeter@703: /// \brief The number of items stored in the heap. kpeter@701: /// kpeter@703: /// This function returns the number of items stored in the heap. kpeter@703: int size() const { return _data.size(); } kpeter@701: kpeter@703: /// \brief Check if the heap is empty. kpeter@703: /// kpeter@703: /// This function returns \c true if the heap is empty. kpeter@703: bool empty() const { return _data.empty(); } kpeter@701: kpeter@703: /// \brief Make the heap empty. kpeter@701: /// kpeter@703: /// This functon makes the heap empty. kpeter@703: /// It does not change the cross reference map. If you want to reuse kpeter@703: /// a heap that is not surely empty, you should first clear it and kpeter@703: /// then you should set the cross reference map to \c PRE_HEAP kpeter@703: /// for each item. kpeter@703: void clear() { _data.clear(); } kpeter@701: kpeter@701: private: kpeter@704: int parent(int i) { return (i-1)/K; } kpeter@704: int firstChild(int i) { return K*i+1; } kpeter@701: kpeter@701: bool less(const Pair &p1, const Pair &p2) const { kpeter@703: return _comp(p1.second, p2.second); kpeter@701: } kpeter@701: kpeter@703: int findMin(const int child, const int length) { kpeter@701: int min=child, i=1; kpeter@704: while( i0 && less(p,_data[par]) ) { kpeter@703: move(_data[par],hole); kpeter@701: hole = par; kpeter@701: par = parent(hole); kpeter@701: } kpeter@701: move(p, hole); kpeter@701: } kpeter@701: kpeter@703: void bubbleDown(int hole, Pair p, int length) { kpeter@701: if( length>1 ) { kpeter@703: int child = firstChild(hole); kpeter@701: while( child0) bubbleDown(0, _data[n], n); kpeter@703: _data.pop_back(); kpeter@701: } kpeter@701: kpeter@703: /// \brief Remove the given item from the heap. kpeter@701: /// kpeter@703: /// This function removes the given item from the heap if it is kpeter@703: /// already stored. kpeter@703: /// \param i The item to delete. kpeter@703: /// \pre \e i must be in the heap. kpeter@701: void erase(const Item &i) { kpeter@703: int h = _iim[i]; kpeter@703: int n = _data.size()-1; kpeter@703: _iim.set(_data[h].first, POST_HEAP); kpeter@701: if( h=0) s=0; kpeter@701: return State(s); kpeter@701: } kpeter@701: kpeter@703: /// \brief Set the state of an item in the heap. kpeter@701: /// kpeter@703: /// This function sets the state of the given item in the heap. kpeter@703: /// It can be used to manually clear the heap when it is important kpeter@703: /// to achive better time complexity. kpeter@701: /// \param i The item. kpeter@701: /// \param st The state. It should not be \c IN_HEAP. kpeter@701: void state(const Item& i, State st) { kpeter@701: switch (st) { kpeter@703: case POST_HEAP: kpeter@703: case PRE_HEAP: kpeter@703: if (state(i) == IN_HEAP) erase(i); kpeter@703: _iim[i] = st; kpeter@703: break; kpeter@703: case IN_HEAP: kpeter@703: break; kpeter@701: } kpeter@701: } kpeter@701: kpeter@703: /// \brief Replace an item in the heap. kpeter@701: /// kpeter@703: /// This function replaces item \c i with item \c j. kpeter@703: /// Item \c i must be in the heap, while \c j must be out of the heap. kpeter@703: /// After calling this method, item \c i will be out of the kpeter@703: /// heap and \c j will be in the heap with the same prioriority kpeter@703: /// as item \c i had before. kpeter@701: void replace(const Item& i, const Item& j) { kpeter@703: int idx=_iim[i]; kpeter@703: _iim.set(i, _iim[j]); kpeter@703: _iim.set(j, idx); kpeter@703: _data[idx].first=j; kpeter@701: } kpeter@701: kpeter@701: }; // class KaryHeap kpeter@701: kpeter@701: } // namespace lemon kpeter@701: kpeter@701: #endif // LEMON_KARY_HEAP_H