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_BINOM_HEAP_H
kpeter@701: #define LEMON_BINOM_HEAP_H
kpeter@701: 
kpeter@701: ///\file
kpeter@703: ///\ingroup heaps
kpeter@701: ///\brief Binomial Heap implementation.
kpeter@701: 
kpeter@701: #include <vector>
kpeter@703: #include <utility>
kpeter@701: #include <functional>
kpeter@701: #include <lemon/math.h>
kpeter@701: #include <lemon/counter.h>
kpeter@701: 
kpeter@701: namespace lemon {
kpeter@701: 
kpeter@703:   /// \ingroup heaps
kpeter@701:   ///
kpeter@703:   ///\brief Binomial heap data structure.
kpeter@701:   ///
kpeter@703:   /// This class implements the \e binomial \e heap data structure.
kpeter@703:   /// It fully conforms to the \ref concepts::Heap "heap concept".
kpeter@701:   ///
kpeter@703:   /// The methods \ref increase() and \ref erase() are not efficient
kpeter@703:   /// in a binomial heap. In case of many calls of these operations,
kpeter@703:   /// it is better to use other heap structure, e.g. \ref BinHeap
kpeter@703:   /// "binary heap".
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@703:   /// \tparam CMP A functor class for comparing the priorities.
kpeter@703:   /// The default is \c std::less<PR>.
kpeter@701: #ifdef DOXYGEN
kpeter@703:   template <typename PR, typename IM, typename CMP>
kpeter@701: #else
kpeter@703:   template <typename PR, typename IM, typename CMP = std::less<PR> >
kpeter@701: #endif
kpeter@701:   class BinomHeap {
kpeter@701:   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@701:     typedef typename ItemIntMap::Key Item;
kpeter@703:     /// Functor type for comparing the priorities.
kpeter@703:     typedef CMP Compare;
kpeter@703: 
kpeter@703:     /// \brief Type to represent the states of the items.
kpeter@703:     ///
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@703:     /// heap's point of view, but may be useful to the user.
kpeter@703:     ///
kpeter@703:     /// The item-int map must be initialized in such way that it assigns
kpeter@703:     /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
kpeter@703:     enum State {
kpeter@703:       IN_HEAP = 0,    ///< = 0.
kpeter@703:       PRE_HEAP = -1,  ///< = -1.
kpeter@703:       POST_HEAP = -2  ///< = -2.
kpeter@703:     };
kpeter@701: 
kpeter@701:   private:
kpeter@707:     class Store;
kpeter@701: 
kpeter@707:     std::vector<Store> _data;
kpeter@703:     int _min, _head;
kpeter@703:     ItemIntMap &_iim;
kpeter@703:     Compare _comp;
kpeter@703:     int _num_items;
kpeter@701: 
kpeter@701:   public:
kpeter@703:     /// \brief Constructor.
kpeter@703:     ///
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 (<tt>-1</tt>) for each item.
kpeter@703:     explicit BinomHeap(ItemIntMap &map)
kpeter@703:       : _min(0), _head(-1), _iim(map), _num_items(0) {}
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 (<tt>-1</tt>) for each item.
kpeter@703:     /// \param comp The function object used for comparing the priorities.
kpeter@703:     BinomHeap(ItemIntMap &map, const Compare &comp)
kpeter@703:       : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
kpeter@701: 
kpeter@701:     /// \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 _num_items; }
kpeter@701: 
kpeter@703:     /// \brief Check if the heap is empty.
kpeter@701:     ///
kpeter@703:     /// This function returns \c true if the heap is empty.
kpeter@703:     bool empty() const { return _num_items==0; }
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@701:     void clear() {
kpeter@703:       _data.clear(); _min=0; _num_items=0; _head=-1;
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Set the priority of an item or insert it, if it is
kpeter@703:     /// not stored in the heap.
kpeter@701:     ///
kpeter@703:     /// This method sets the priority of the given item if it is
kpeter@703:     /// already stored in the heap. Otherwise it inserts the given
kpeter@703:     /// item into the heap with the given priority.
kpeter@703:     /// \param item The item.
kpeter@703:     /// \param value The priority.
kpeter@701:     void set (const Item& item, const Prio& value) {
kpeter@703:       int i=_iim[item];
kpeter@703:       if ( i >= 0 && _data[i].in ) {
kpeter@703:         if ( _comp(value, _data[i].prio) ) decrease(item, value);
kpeter@703:         if ( _comp(_data[i].prio, value) ) increase(item, value);
kpeter@701:       } else push(item, value);
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Insert an item into the heap with the given priority.
kpeter@701:     ///
kpeter@703:     /// This function inserts the given item into the heap with the
kpeter@703:     /// given priority.
kpeter@703:     /// \param item The item to insert.
kpeter@703:     /// \param value The priority of the item.
kpeter@703:     /// \pre \e item must not be stored in the heap.
kpeter@701:     void push (const Item& item, const Prio& value) {
kpeter@703:       int i=_iim[item];
kpeter@701:       if ( i<0 ) {
kpeter@703:         int s=_data.size();
kpeter@703:         _iim.set( item,s );
kpeter@707:         Store st;
kpeter@701:         st.name=item;
kpeter@707:         st.prio=value;
kpeter@703:         _data.push_back(st);
kpeter@701:         i=s;
kpeter@701:       }
kpeter@701:       else {
kpeter@703:         _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
kpeter@703:         _data[i].degree=0;
kpeter@703:         _data[i].in=true;
kpeter@707:         _data[i].prio=value;
kpeter@701:       }
kpeter@701: 
kpeter@707:       if( 0==_num_items ) {
kpeter@707:         _head=i;
kpeter@707:         _min=i;
kpeter@707:       } else {
kpeter@707:         merge(i);
kpeter@707:         if( _comp(_data[i].prio, _data[_min].prio) ) _min=i;
kpeter@707:       }
kpeter@703:       ++_num_items;
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Return the item having minimum priority.
kpeter@701:     ///
kpeter@703:     /// This function returns the item having minimum priority.
kpeter@703:     /// \pre The heap must be non-empty.
kpeter@703:     Item top() const { return _data[_min].name; }
kpeter@701: 
kpeter@703:     /// \brief The minimum priority.
kpeter@701:     ///
kpeter@703:     /// This function returns the minimum priority.
kpeter@703:     /// \pre The heap must be non-empty.
kpeter@703:     Prio prio() const { return _data[_min].prio; }
kpeter@701: 
kpeter@703:     /// \brief The priority of the given item.
kpeter@701:     ///
kpeter@703:     /// This function returns the priority of the given item.
kpeter@703:     /// \param item The item.
kpeter@703:     /// \pre \e item must be in the heap.
kpeter@701:     const Prio& operator[](const Item& item) const {
kpeter@703:       return _data[_iim[item]].prio;
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Remove the item having minimum priority.
kpeter@701:     ///
kpeter@703:     /// This function removes the item having minimum priority.
kpeter@701:     /// \pre The heap must be non-empty.
kpeter@701:     void pop() {
kpeter@703:       _data[_min].in=false;
kpeter@701: 
kpeter@701:       int head_child=-1;
kpeter@703:       if ( _data[_min].child!=-1 ) {
kpeter@703:         int child=_data[_min].child;
kpeter@701:         int neighb;
kpeter@701:         while( child!=-1 ) {
kpeter@703:           neighb=_data[child].right_neighbor;
kpeter@703:           _data[child].parent=-1;
kpeter@707:           _data[child].right_neighbor=head_child;
kpeter@701:           head_child=child;
kpeter@701:           child=neighb;
kpeter@701:         }
kpeter@701:       }
kpeter@701: 
kpeter@707:       if ( _data[_head].right_neighbor==-1 ) {
kpeter@707:         // there was only one root
kpeter@703:         _head=head_child;
kpeter@701:       }
kpeter@701:       else {
kpeter@707:         // there were more roots
kpeter@703:         if( _head!=_min )  { unlace(_min); }
kpeter@703:         else { _head=_data[_head].right_neighbor; }
kpeter@701:         merge(head_child);
kpeter@701:       }
kpeter@703:       _min=findMin();
kpeter@703:       --_num_items;
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 item The item to delete.
kpeter@703:     /// \pre \e item must be in the heap.
kpeter@701:     void erase (const Item& item) {
kpeter@703:       int i=_iim[item];
kpeter@703:       if ( i >= 0 && _data[i].in ) {
kpeter@703:         decrease( item, _data[_min].prio-1 );
kpeter@701:         pop();
kpeter@701:       }
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Decrease the priority of an item to the given value.
kpeter@701:     ///
kpeter@703:     /// This function decreases the priority of an item to the given value.
kpeter@703:     /// \param item The item.
kpeter@703:     /// \param value The priority.
kpeter@703:     /// \pre \e item must be stored in the heap with priority at least \e value.
kpeter@701:     void decrease (Item item, const Prio& value) {
kpeter@703:       int i=_iim[item];
kpeter@707:       int p=_data[i].parent;
kpeter@707:       _data[i].prio=value;
kpeter@707:       
kpeter@707:       while( p!=-1 && _comp(value, _data[p].prio) ) {
kpeter@707:         _data[i].name=_data[p].name;
kpeter@707:         _data[i].prio=_data[p].prio;
kpeter@707:         _data[p].name=item;
kpeter@707:         _data[p].prio=value;
kpeter@707:         _iim[_data[i].name]=i;
kpeter@707:         i=p;
kpeter@707:         p=_data[p].parent;
kpeter@701:       }
kpeter@707:       _iim[item]=i;
kpeter@707:       if ( _comp(value, _data[_min].prio) ) _min=i;
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Increase the priority of an item to the given value.
kpeter@701:     ///
kpeter@703:     /// This function increases the priority of an item to the given value.
kpeter@703:     /// \param item The item.
kpeter@703:     /// \param value The priority.
kpeter@703:     /// \pre \e item must be stored in the heap with priority at most \e value.
kpeter@701:     void increase (Item item, const Prio& value) {
kpeter@701:       erase(item);
kpeter@701:       push(item, value);
kpeter@701:     }
kpeter@701: 
kpeter@703:     /// \brief Return the state of an item.
kpeter@701:     ///
kpeter@703:     /// This method returns \c PRE_HEAP if the given item has never
kpeter@703:     /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
kpeter@703:     /// and \c POST_HEAP otherwise.
kpeter@703:     /// In the latter case it is possible that the item will get back
kpeter@703:     /// to the heap again.
kpeter@703:     /// \param item The item.
kpeter@701:     State state(const Item &item) const {
kpeter@703:       int i=_iim[item];
kpeter@701:       if( i>=0 ) {
kpeter@703:         if ( _data[i].in ) i=0;
kpeter@701:         else i=-2;
kpeter@701:       }
kpeter@701:       return State(i);
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@701:       case POST_HEAP:
kpeter@701:       case PRE_HEAP:
kpeter@701:         if (state(i) == IN_HEAP) {
kpeter@701:           erase(i);
kpeter@701:         }
kpeter@703:         _iim[i] = st;
kpeter@701:         break;
kpeter@701:       case IN_HEAP:
kpeter@701:         break;
kpeter@701:       }
kpeter@701:     }
kpeter@701: 
kpeter@701:   private:
kpeter@707:     
kpeter@707:     // Find the minimum of the roots
kpeter@703:     int findMin() {
kpeter@707:       if( _head!=-1 ) {
kpeter@707:         int min_loc=_head, min_val=_data[_head].prio;
kpeter@707:         for( int x=_data[_head].right_neighbor; x!=-1;
kpeter@707:              x=_data[x].right_neighbor ) {
kpeter@703:           if( _comp( _data[x].prio,min_val ) ) {
kpeter@703:             min_val=_data[x].prio;
kpeter@701:             min_loc=x;
kpeter@701:           }
kpeter@701:         }
kpeter@707:         return min_loc;
kpeter@701:       }
kpeter@707:       else return -1;
kpeter@701:     }
kpeter@701: 
kpeter@707:     // Merge the heap with another heap starting at the given position
kpeter@701:     void merge(int a) {
kpeter@707:       if( _head==-1 || a==-1 ) return;
kpeter@707:       if( _data[a].right_neighbor==-1 &&
kpeter@707:           _data[a].degree<=_data[_head].degree ) {
kpeter@707:         _data[a].right_neighbor=_head;
kpeter@707:         _head=a;
kpeter@707:       } else {
kpeter@707:         interleave(a);
kpeter@707:       }
kpeter@707:       if( _data[_head].right_neighbor==-1 ) return;
kpeter@707:       
kpeter@703:       int x=_head;
kpeter@707:       int x_prev=-1, x_next=_data[x].right_neighbor;
kpeter@707:       while( x_next!=-1 ) {
kpeter@707:         if( _data[x].degree!=_data[x_next].degree ||
kpeter@707:             ( _data[x_next].right_neighbor!=-1 &&
kpeter@707:               _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
kpeter@707:           x_prev=x;
kpeter@707:           x=x_next;
kpeter@707:         }
kpeter@707:         else {
kpeter@707:           if( _comp(_data[x_next].prio,_data[x].prio) ) {
kpeter@707:             if( x_prev==-1 ) {
kpeter@707:               _head=x_next;
kpeter@707:             } else {
kpeter@707:               _data[x_prev].right_neighbor=x_next;
kpeter@707:             }
kpeter@707:             fuse(x,x_next);
kpeter@701:             x=x_next;
kpeter@701:           }
kpeter@701:           else {
kpeter@707:             _data[x].right_neighbor=_data[x_next].right_neighbor;
kpeter@707:             fuse(x_next,x);
kpeter@701:           }
kpeter@701:         }
kpeter@707:         x_next=_data[x].right_neighbor;
kpeter@701:       }
kpeter@701:     }
kpeter@701: 
kpeter@707:     // Interleave the elements of the given list into the list of the roots
kpeter@701:     void interleave(int a) {
kpeter@707:       int p=_head, q=a;
kpeter@707:       int curr=_data.size();
kpeter@707:       _data.push_back(Store());
kpeter@707:       
kpeter@707:       while( p!=-1 || q!=-1 ) {
kpeter@707:         if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
kpeter@707:           _data[curr].right_neighbor=p;
kpeter@707:           curr=p;
kpeter@707:           p=_data[p].right_neighbor;
kpeter@701:         }
kpeter@701:         else {
kpeter@707:           _data[curr].right_neighbor=q;
kpeter@707:           curr=q;
kpeter@707:           q=_data[q].right_neighbor;
kpeter@701:         }
kpeter@701:       }
kpeter@707:       
kpeter@707:       _head=_data.back().right_neighbor;
kpeter@707:       _data.pop_back();
kpeter@701:     }
kpeter@701: 
kpeter@707:     // Lace node a under node b
kpeter@701:     void fuse(int a, int b) {
kpeter@703:       _data[a].parent=b;
kpeter@703:       _data[a].right_neighbor=_data[b].child;
kpeter@703:       _data[b].child=a;
kpeter@701: 
kpeter@703:       ++_data[b].degree;
kpeter@701:     }
kpeter@701: 
kpeter@707:     // Unlace node a (if it has siblings)
kpeter@701:     void unlace(int a) {
kpeter@703:       int neighb=_data[a].right_neighbor;
kpeter@703:       int other=_head;
kpeter@701: 
kpeter@703:       while( _data[other].right_neighbor!=a )
kpeter@703:         other=_data[other].right_neighbor;
kpeter@703:       _data[other].right_neighbor=neighb;
kpeter@701:     }
kpeter@701: 
kpeter@701:   private:
kpeter@701: 
kpeter@707:     class Store {
kpeter@701:       friend class BinomHeap;
kpeter@701: 
kpeter@701:       Item name;
kpeter@701:       int parent;
kpeter@701:       int right_neighbor;
kpeter@701:       int child;
kpeter@701:       int degree;
kpeter@701:       bool in;
kpeter@701:       Prio prio;
kpeter@701: 
kpeter@707:       Store() : parent(-1), right_neighbor(-1), child(-1), degree(0),
kpeter@707:         in(true) {}
kpeter@701:     };
kpeter@701:   };
kpeter@701: 
kpeter@701: } //namespace lemon
kpeter@701: 
kpeter@701: #endif //LEMON_BINOM_HEAP_H
kpeter@701: