1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    24 ///\brief D-ary heap implementation.
 
    34   ///\brief D-ary heap data structure.
 
    36   /// This class implements the \e D-ary \e heap data structure.
 
    37   /// It fully conforms to the \ref concepts::Heap "heap concept".
 
    39   /// The \ref DHeap "D-ary heap" is a generalization of the
 
    40   /// \ref BinHeap "binary heap" structure, its nodes have at most
 
    41   /// \c D children, instead of two.
 
    42   /// \ref BinHeap and \ref QuadHeap are specialized implementations
 
    43   /// of this structure for <tt>D=2</tt> and <tt>D=4</tt>, respectively.
 
    45   /// \tparam PR Type of the priorities of the items.
 
    46   /// \tparam IM A read-writable item map with \c int values, used
 
    47   /// internally to handle the cross references.
 
    48   /// \tparam D The degree of the heap, each node have at most \e D
 
    49   /// children. The default is 16. Powers of two are suggested to use
 
    50   /// so that the multiplications and divisions needed to traverse the
 
    51   /// nodes of the heap could be performed faster.
 
    52   /// \tparam CMP A functor class for comparing the priorities.
 
    53   /// The default is \c std::less<PR>.
 
    58   template <typename PR, typename IM, int D, typename CMP>
 
    60   template <typename PR, typename IM, int D = 16,
 
    61             typename CMP = std::less<PR> >
 
    65     /// Type of the item-int map.
 
    66     typedef IM ItemIntMap;
 
    67     /// Type of the priorities.
 
    69     /// Type of the items stored in the heap.
 
    70     typedef typename ItemIntMap::Key Item;
 
    71     /// Type of the item-priority pairs.
 
    72     typedef std::pair<Item,Prio> Pair;
 
    73     /// Functor type for comparing the priorities.
 
    76     /// \brief Type to represent the states of the items.
 
    78     /// Each item has a state associated to it. It can be "in heap",
 
    79     /// "pre-heap" or "post-heap". The latter two are indifferent from the
 
    80     /// heap's point of view, but may be useful to the user.
 
    82     /// The item-int map must be initialized in such way that it assigns
 
    83     /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
 
    85       IN_HEAP = 0,    ///< = 0.
 
    86       PRE_HEAP = -1,  ///< = -1.
 
    87       POST_HEAP = -2  ///< = -2.
 
    91     std::vector<Pair> _data;
 
    96     /// \brief Constructor.
 
    99     /// \param map A map that assigns \c int values to the items.
 
   100     /// It is used internally to handle the cross references.
 
   101     /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
 
   102     explicit DHeap(ItemIntMap &map) : _iim(map) {}
 
   104     /// \brief Constructor.
 
   107     /// \param map A map that assigns \c int values to the items.
 
   108     /// It is used internally to handle the cross references.
 
   109     /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
 
   110     /// \param comp The function object used for comparing the priorities.
 
   111     DHeap(ItemIntMap &map, const Compare &comp)
 
   112       : _iim(map), _comp(comp) {}
 
   114     /// \brief The number of items stored in the heap.
 
   116     /// This function returns the number of items stored in the heap.
 
   117     int size() const { return _data.size(); }
 
   119     /// \brief Check if the heap is empty.
 
   121     /// This function returns \c true if the heap is empty.
 
   122     bool empty() const { return _data.empty(); }
 
   124     /// \brief Make the heap empty.
 
   126     /// This functon makes the heap empty.
 
   127     /// It does not change the cross reference map. If you want to reuse
 
   128     /// a heap that is not surely empty, you should first clear it and
 
   129     /// then you should set the cross reference map to \c PRE_HEAP
 
   131     void clear() { _data.clear(); }
 
   134     int parent(int i) { return (i-1)/D; }
 
   135     int firstChild(int i) { return D*i+1; }
 
   137     bool less(const Pair &p1, const Pair &p2) const {
 
   138       return _comp(p1.second, p2.second);
 
   141     void bubbleUp(int hole, Pair p) {
 
   142       int par = parent(hole);
 
   143       while( hole>0 && less(p,_data[par]) ) {
 
   144         move(_data[par],hole);
 
   151     void bubbleDown(int hole, Pair p, int length) {
 
   153         int child = firstChild(hole);
 
   154         while( child+D<=length ) {
 
   156           for (int i=1; i<D; ++i) {
 
   157             if( less(_data[child+i], _data[min]) )
 
   160           if( !less(_data[min], p) )
 
   162           move(_data[min], hole);
 
   164           child = firstChild(hole);
 
   166         if ( child<length ) {
 
   168           while (++child < length) {
 
   169             if( less(_data[child], _data[min]) )
 
   172           if( less(_data[min], p) ) {
 
   173             move(_data[min], hole);
 
   182     void move(const Pair &p, int i) {
 
   184       _iim.set(p.first, i);
 
   188     /// \brief Insert a pair of item and priority into the heap.
 
   190     /// This function inserts \c p.first to the heap with priority
 
   192     /// \param p The pair to insert.
 
   193     /// \pre \c p.first must not be stored in the heap.
 
   194     void push(const Pair &p) {
 
   195       int n = _data.size();
 
   200     /// \brief Insert an item into the heap with the given priority.
 
   202     /// This function inserts the given item into the heap with the
 
   204     /// \param i The item to insert.
 
   205     /// \param p The priority of the item.
 
   206     /// \pre \e i must not be stored in the heap.
 
   207     void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
 
   209     /// \brief Return the item having minimum priority.
 
   211     /// This function returns the item having minimum priority.
 
   212     /// \pre The heap must be non-empty.
 
   213     Item top() const { return _data[0].first; }
 
   215     /// \brief The minimum priority.
 
   217     /// This function returns the minimum priority.
 
   218     /// \pre The heap must be non-empty.
 
   219     Prio prio() const { return _data[0].second; }
 
   221     /// \brief Remove the item having minimum priority.
 
   223     /// This function removes the item having minimum priority.
 
   224     /// \pre The heap must be non-empty.
 
   226       int n = _data.size()-1;
 
   227       _iim.set(_data[0].first, POST_HEAP);
 
   228       if (n>0) bubbleDown(0, _data[n], n);
 
   232     /// \brief Remove the given item from the heap.
 
   234     /// This function removes the given item from the heap if it is
 
   236     /// \param i The item to delete.
 
   237     /// \pre \e i must be in the heap.
 
   238     void erase(const Item &i) {
 
   240       int n = _data.size()-1;
 
   241       _iim.set(_data[h].first, POST_HEAP);
 
   243         if( less(_data[parent(h)], _data[n]) )
 
   244           bubbleDown(h, _data[n], n);
 
   246           bubbleUp(h, _data[n]);
 
   251     /// \brief The priority of the given item.
 
   253     /// This function returns the priority of the given item.
 
   254     /// \param i The item.
 
   255     /// \pre \e i must be in the heap.
 
   256     Prio operator[](const Item &i) const {
 
   258       return _data[idx].second;
 
   261     /// \brief Set the priority of an item or insert it, if it is
 
   262     /// not stored in the heap.
 
   264     /// This method sets the priority of the given item if it is
 
   265     /// already stored in the heap. Otherwise it inserts the given
 
   266     /// item into the heap with the given priority.
 
   267     /// \param i The item.
 
   268     /// \param p The priority.
 
   269     void set(const Item &i, const Prio &p) {
 
   273       else if( _comp(p, _data[idx].second) )
 
   274         bubbleUp(idx, Pair(i,p));
 
   276         bubbleDown(idx, Pair(i,p), _data.size());
 
   279     /// \brief Decrease the priority of an item to the given value.
 
   281     /// This function decreases the priority of an item to the given value.
 
   282     /// \param i The item.
 
   283     /// \param p The priority.
 
   284     /// \pre \e i must be stored in the heap with priority at least \e p.
 
   285     void decrease(const Item &i, const Prio &p) {
 
   287       bubbleUp(idx, Pair(i,p));
 
   290     /// \brief Increase the priority of an item to the given value.
 
   292     /// This function increases the priority of an item to the given value.
 
   293     /// \param i The item.
 
   294     /// \param p The priority.
 
   295     /// \pre \e i must be stored in the heap with priority at most \e p.
 
   296     void increase(const Item &i, const Prio &p) {
 
   298       bubbleDown(idx, Pair(i,p), _data.size());
 
   301     /// \brief Return the state of an item.
 
   303     /// This method returns \c PRE_HEAP if the given item has never
 
   304     /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
 
   305     /// and \c POST_HEAP otherwise.
 
   306     /// In the latter case it is possible that the item will get back
 
   307     /// to the heap again.
 
   308     /// \param i The item.
 
   309     State state(const Item &i) const {
 
   315     /// \brief Set the state of an item in the heap.
 
   317     /// This function sets the state of the given item in the heap.
 
   318     /// It can be used to manually clear the heap when it is important
 
   319     /// to achive better time complexity.
 
   320     /// \param i The item.
 
   321     /// \param st The state. It should not be \c IN_HEAP.
 
   322     void state(const Item& i, State st) {
 
   326           if (state(i) == IN_HEAP) erase(i);
 
   334     /// \brief Replace an item in the heap.
 
   336     /// This function replaces item \c i with item \c j.
 
   337     /// Item \c i must be in the heap, while \c j must be out of the heap.
 
   338     /// After calling this method, item \c i will be out of the
 
   339     /// heap and \c j will be in the heap with the same prioriority
 
   340     /// as item \c i had before.
 
   341     void replace(const Item& i, const Item& j) {
 
   343       _iim.set(i, _iim[j]);
 
   352 #endif // LEMON_DHEAP_H