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@440:  * 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: 
alpar@100: ///\ingroup auxdat
alpar@100: ///\file
alpar@100: ///\brief Binary Heap implementation.
alpar@100: 
alpar@100: #include <vector>
alpar@100: #include <utility>
alpar@100: #include <functional>
alpar@100: 
alpar@100: namespace lemon {
alpar@100: 
alpar@100:   ///\ingroup auxdat
alpar@100:   ///
alpar@100:   ///\brief A Binary Heap implementation.
alpar@100:   ///
kpeter@559:   ///This class implements the \e binary \e heap data structure. 
kpeter@559:   /// 
kpeter@559:   ///A \e heap is a data structure for storing items with specified values
kpeter@559:   ///called \e priorities in such a way that finding the item with minimum
kpeter@559:   ///priority is efficient. \c Comp specifies the ordering of the priorities.
kpeter@559:   ///In a heap one can change the priority of an item, add or erase an
kpeter@559:   ///item, etc.
alpar@100:   ///
kpeter@559:   ///\tparam PR Type of the priority of the items.
kpeter@559:   ///\tparam IM A read and writable item map with int values, used internally
alpar@100:   ///to handle the cross references.
kpeter@559:   ///\tparam Comp A functor class for the ordering of the priorities.
kpeter@559:   ///The default is \c std::less<PR>.
alpar@100:   ///
alpar@100:   ///\sa FibHeap
alpar@100:   ///\sa Dijkstra
kpeter@559:   template <typename PR, typename IM, typename Comp = std::less<PR> >
alpar@100:   class BinHeap {
alpar@100: 
alpar@100:   public:
alpar@100:     ///\e
kpeter@559:     typedef IM ItemIntMap;
alpar@100:     ///\e
kpeter@559:     typedef PR Prio;
alpar@100:     ///\e
alpar@100:     typedef typename ItemIntMap::Key Item;
alpar@100:     ///\e
alpar@100:     typedef std::pair<Item,Prio> Pair;
alpar@100:     ///\e
kpeter@559:     typedef Comp Compare;
alpar@100: 
alpar@100:     /// \brief Type to represent the items states.
alpar@100:     ///
alpar@100:     /// Each Item element have a state associated to it. It may be "in heap",
alpar@100:     /// "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@559:     /// The item-int map must be initialized in such way that it assigns
kpeter@559:     /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
alpar@100:     enum State {
kpeter@584:       IN_HEAP = 0,    ///< = 0.
kpeter@584:       PRE_HEAP = -1,  ///< = -1.
kpeter@584:       POST_HEAP = -2  ///< = -2.
alpar@100:     };
alpar@100: 
alpar@100:   private:
kpeter@559:     std::vector<Pair> _data;
kpeter@559:     Compare _comp;
kpeter@559:     ItemIntMap &_iim;
alpar@100: 
alpar@100:   public:
alpar@100:     /// \brief The constructor.
alpar@100:     ///
alpar@100:     /// The constructor.
kpeter@559:     /// \param map should be given to the constructor, since it is used
alpar@100:     /// internally to handle the cross references. The value of the map
kpeter@559:     /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
kpeter@559:     explicit BinHeap(ItemIntMap &map) : _iim(map) {}
alpar@209: 
alpar@100:     /// \brief The constructor.
alpar@100:     ///
alpar@100:     /// The constructor.
kpeter@559:     /// \param map should be given to the constructor, since it is used
alpar@100:     /// internally to handle the cross references. The value of the map
alpar@100:     /// should be PRE_HEAP (-1) for each element.
alpar@100:     ///
kpeter@559:     /// \param comp The comparator function object.
kpeter@559:     BinHeap(ItemIntMap &map, const Compare &comp)
kpeter@559:       : _iim(map), _comp(comp) {}
alpar@100: 
alpar@100: 
alpar@100:     /// The number of items stored in the heap.
alpar@100:     ///
alpar@100:     /// \brief Returns the number of items stored in the heap.
kpeter@559:     int size() const { return _data.size(); }
alpar@209: 
alpar@100:     /// \brief Checks if the heap stores no items.
alpar@100:     ///
alpar@100:     /// Returns \c true if and only if the heap stores no items.
kpeter@559:     bool empty() const { return _data.empty(); }
alpar@100: 
alpar@100:     /// \brief Make empty this heap.
alpar@209:     ///
alpar@100:     /// Make empty this heap. It does not change the cross reference map.
alpar@100:     /// If you want to reuse what is not surely empty you should first clear
alpar@100:     /// the heap and after that you should set the cross reference map for
alpar@100:     /// each item to \c PRE_HEAP.
alpar@209:     void clear() {
kpeter@559:       _data.clear();
alpar@100:     }
alpar@100: 
alpar@100:   private:
alpar@100:     static int parent(int i) { return (i-1)/2; }
alpar@100: 
alpar@100:     static int second_child(int i) { return 2*i+2; }
alpar@100:     bool less(const Pair &p1, const Pair &p2) const {
kpeter@559:       return _comp(p1.second, p2.second);
alpar@100:     }
alpar@100: 
alpar@100:     int bubble_up(int hole, Pair p) {
alpar@100:       int par = parent(hole);
kpeter@559:       while( hole>0 && less(p,_data[par]) ) {
kpeter@559:         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: 
alpar@100:     int bubble_down(int hole, Pair p, int length) {
alpar@100:       int child = second_child(hole);
alpar@100:       while(child < length) {
kpeter@559:         if( less(_data[child-1], _data[child]) ) {
alpar@209:           --child;
alpar@209:         }
kpeter@559:         if( !less(_data[child], p) )
alpar@209:           goto ok;
kpeter@559:         move(_data[child], hole);
alpar@209:         hole = child;
alpar@209:         child = second_child(hole);
alpar@100:       }
alpar@100:       child--;
kpeter@559:       if( child<length && less(_data[child], p) ) {
kpeter@559:         move(_data[child], hole);
alpar@209:         hole=child;
alpar@100:       }
alpar@100:     ok:
alpar@100:       move(p, hole);
alpar@100:       return hole;
alpar@100:     }
alpar@100: 
alpar@100:     void move(const Pair &p, int i) {
kpeter@559:       _data[i] = p;
kpeter@559:       _iim.set(p.first, i);
alpar@100:     }
alpar@100: 
alpar@100:   public:
alpar@100:     /// \brief Insert a pair of item and priority into the heap.
alpar@100:     ///
alpar@100:     /// Adds \c p.first to the heap with priority \c p.second.
alpar@100:     /// \param p The pair to insert.
alpar@100:     void push(const Pair &p) {
kpeter@559:       int n = _data.size();
kpeter@559:       _data.resize(n+1);
alpar@100:       bubble_up(n, p);
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Insert an item into the heap with the given heap.
alpar@209:     ///
alpar@209:     /// Adds \c i to the heap with priority \c p.
alpar@100:     /// \param i The item to insert.
alpar@100:     /// \param p The priority of the item.
alpar@100:     void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
alpar@100: 
alpar@100:     /// \brief Returns the item with minimum priority relative to \c Compare.
alpar@100:     ///
alpar@100:     /// This method returns the item with minimum priority relative to \c
alpar@209:     /// Compare.
alpar@209:     /// \pre The heap must be nonempty.
alpar@100:     Item top() const {
kpeter@559:       return _data[0].first;
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Returns the minimum priority relative to \c Compare.
alpar@100:     ///
alpar@100:     /// It returns the minimum priority relative to \c Compare.
alpar@100:     /// \pre The heap must be nonempty.
alpar@100:     Prio prio() const {
kpeter@559:       return _data[0].second;
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Deletes the item with minimum priority relative to \c Compare.
alpar@100:     ///
alpar@100:     /// This method deletes the item with minimum priority relative to \c
alpar@209:     /// Compare from the heap.
alpar@209:     /// \pre The heap must be non-empty.
alpar@100:     void pop() {
kpeter@559:       int n = _data.size()-1;
kpeter@559:       _iim.set(_data[0].first, POST_HEAP);
alpar@100:       if (n > 0) {
kpeter@559:         bubble_down(0, _data[n], n);
alpar@100:       }
kpeter@559:       _data.pop_back();
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Deletes \c i from the heap.
alpar@100:     ///
alpar@100:     /// This method deletes item \c i from the heap.
alpar@100:     /// \param i The item to erase.
alpar@100:     /// \pre The item should be in the heap.
alpar@100:     void erase(const Item &i) {
kpeter@559:       int h = _iim[i];
kpeter@559:       int n = _data.size()-1;
kpeter@559:       _iim.set(_data[h].first, POST_HEAP);
alpar@100:       if( h < n ) {
kpeter@559:         if ( bubble_up(h, _data[n]) == h) {
kpeter@559:           bubble_down(h, _data[n], n);
alpar@209:         }
alpar@100:       }
kpeter@559:       _data.pop_back();
alpar@100:     }
alpar@100: 
alpar@209: 
alpar@100:     /// \brief Returns the priority of \c i.
alpar@100:     ///
alpar@209:     /// This function returns the priority of item \c i.
kpeter@559:     /// \param i The item.
alpar@100:     /// \pre \c i must be in the heap.
alpar@100:     Prio operator[](const Item &i) const {
kpeter@559:       int idx = _iim[i];
kpeter@559:       return _data[idx].second;
alpar@100:     }
alpar@100: 
alpar@209:     /// \brief \c i gets to the heap with priority \c p independently
alpar@100:     /// if \c i was already there.
alpar@100:     ///
alpar@100:     /// This method calls \ref push(\c i, \c p) if \c i is not stored
alpar@100:     /// in the heap and sets the priority of \c i to \c p otherwise.
alpar@100:     /// \param i The item.
alpar@100:     /// \param p The priority.
alpar@100:     void set(const Item &i, const Prio &p) {
kpeter@559:       int idx = _iim[i];
alpar@100:       if( idx < 0 ) {
alpar@209:         push(i,p);
alpar@100:       }
kpeter@559:       else if( _comp(p, _data[idx].second) ) {
alpar@209:         bubble_up(idx, Pair(i,p));
alpar@100:       }
alpar@100:       else {
kpeter@559:         bubble_down(idx, Pair(i,p), _data.size());
alpar@100:       }
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Decreases the priority of \c i to \c p.
alpar@100:     ///
alpar@100:     /// This method decreases the priority of item \c i to \c p.
kpeter@559:     /// \param i The item.
kpeter@559:     /// \param p The priority.
alpar@100:     /// \pre \c i must be stored in the heap with priority at least \c
alpar@100:     /// p relative to \c Compare.
alpar@100:     void decrease(const Item &i, const Prio &p) {
kpeter@559:       int idx = _iim[i];
alpar@100:       bubble_up(idx, Pair(i,p));
alpar@100:     }
alpar@209: 
alpar@100:     /// \brief Increases the priority of \c i to \c p.
alpar@100:     ///
alpar@209:     /// This method sets the priority of item \c i to \c p.
kpeter@559:     /// \param i The item.
kpeter@559:     /// \param p The priority.
alpar@100:     /// \pre \c i must be stored in the heap with priority at most \c
alpar@100:     /// p relative to \c Compare.
alpar@100:     void increase(const Item &i, const Prio &p) {
kpeter@559:       int idx = _iim[i];
kpeter@559:       bubble_down(idx, Pair(i,p), _data.size());
alpar@100:     }
alpar@100: 
alpar@209:     /// \brief Returns if \c item is in, has already been in, or has
alpar@100:     /// never been in the heap.
alpar@100:     ///
alpar@100:     /// This method returns PRE_HEAP if \c item has never been in the
alpar@100:     /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
alpar@100:     /// otherwise. In the latter case it is possible that \c item will
alpar@100:     /// get back to the heap again.
alpar@100:     /// \param i The item.
alpar@100:     State state(const Item &i) const {
kpeter@559:       int s = _iim[i];
alpar@100:       if( s>=0 )
alpar@209:         s=0;
alpar@100:       return State(s);
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Sets the state of the \c item in the heap.
alpar@100:     ///
alpar@100:     /// Sets the state of the \c item in the heap. It can be used to
alpar@100:     /// manually clear the heap when it is important to achive the
alpar@100:     /// 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@559:         _iim[i] = st;
alpar@100:         break;
alpar@100:       case IN_HEAP:
alpar@100:         break;
alpar@100:       }
alpar@100:     }
alpar@100: 
alpar@100:     /// \brief Replaces an item in the heap.
alpar@100:     ///
alpar@100:     /// The \c i item is replaced with \c j item. The \c i item should
alpar@100:     /// be in the heap, while the \c j should be out of the heap. The
alpar@100:     /// \c i item will out of the heap and \c j will be in the heap
alpar@100:     /// with the same prioriority as prevoiusly the \c i item.
alpar@100:     void replace(const Item& i, const Item& j) {
kpeter@559:       int idx = _iim[i];
kpeter@559:       _iim.set(i, _iim[j]);
kpeter@559:       _iim.set(j, idx);
kpeter@559:       _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