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 kpeter@703: #include 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 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. kpeter@701: #ifdef DOXYGEN kpeter@703: template kpeter@701: #else kpeter@703: template > 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 (-1) 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@701: class store; kpeter@701: kpeter@703: std::vector _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 (-1) 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 (-1) 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@701: store st; kpeter@701: st.name=item; 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@701: } kpeter@703: _data[i].prio=value; kpeter@701: kpeter@703: if( 0==_num_items ) { _head=i; _min=i; } kpeter@701: else { merge(i); } kpeter@701: kpeter@703: _min = findMin(); kpeter@701: 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: int prev=-1; kpeter@701: while( child!=-1 ) { kpeter@703: neighb=_data[child].right_neighbor; kpeter@703: _data[child].parent=-1; kpeter@703: _data[child].right_neighbor=prev; kpeter@701: head_child=child; kpeter@701: prev=child; kpeter@701: child=neighb; kpeter@701: } kpeter@701: } kpeter@701: kpeter@701: // The first case is that there are only one root. kpeter@703: if ( -1==_data[_head].right_neighbor ) { kpeter@703: _head=head_child; kpeter@701: } kpeter@701: // The case where there are more roots. kpeter@701: else { kpeter@703: if( _head!=_min ) { unlace(_min); } kpeter@703: else { _head=_data[_head].right_neighbor; } kpeter@701: 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@701: kpeter@703: if( _comp( value,_data[i].prio ) ) { kpeter@703: _data[i].prio=value; kpeter@701: kpeter@703: int p_loc=_data[i].parent, loc=i; kpeter@701: int parent, child, neighb; kpeter@701: kpeter@703: while( -1!=p_loc && _comp(_data[loc].prio,_data[p_loc].prio) ) { kpeter@701: kpeter@701: // parent set for other loc_child kpeter@703: child=_data[loc].child; kpeter@701: while( -1!=child ) { kpeter@703: _data[child].parent=p_loc; kpeter@703: child=_data[child].right_neighbor; kpeter@701: } kpeter@701: kpeter@701: // parent set for other p_loc_child kpeter@703: child=_data[p_loc].child; kpeter@701: while( -1!=child ) { kpeter@703: _data[child].parent=loc; kpeter@703: child=_data[child].right_neighbor; kpeter@701: } kpeter@701: kpeter@703: child=_data[p_loc].child; kpeter@703: _data[p_loc].child=_data[loc].child; kpeter@701: if( child==loc ) kpeter@701: child=p_loc; kpeter@703: _data[loc].child=child; kpeter@701: kpeter@701: // left_neighb set for p_loc kpeter@703: if( _data[loc].child!=p_loc ) { kpeter@703: while( _data[child].right_neighbor!=loc ) kpeter@703: child=_data[child].right_neighbor; kpeter@703: _data[child].right_neighbor=p_loc; kpeter@701: } kpeter@701: kpeter@701: // left_neighb set for loc kpeter@703: parent=_data[p_loc].parent; kpeter@703: if( -1!=parent ) child=_data[parent].child; kpeter@703: else child=_head; kpeter@701: kpeter@701: if( child!=p_loc ) { kpeter@703: while( _data[child].right_neighbor!=p_loc ) kpeter@703: child=_data[child].right_neighbor; kpeter@703: _data[child].right_neighbor=loc; kpeter@701: } kpeter@701: kpeter@703: neighb=_data[p_loc].right_neighbor; kpeter@703: _data[p_loc].right_neighbor=_data[loc].right_neighbor; kpeter@703: _data[loc].right_neighbor=neighb; kpeter@701: kpeter@703: _data[p_loc].parent=loc; kpeter@703: _data[loc].parent=parent; kpeter@701: kpeter@703: if( -1!=parent && _data[parent].child==p_loc ) kpeter@703: _data[parent].child=loc; kpeter@701: kpeter@701: /*if new parent will be the first root*/ kpeter@703: if( _head==p_loc ) kpeter@703: _head=loc; kpeter@701: kpeter@703: p_loc=_data[loc].parent; kpeter@701: } kpeter@701: } kpeter@703: if( _comp(value,_data[_min].prio) ) { kpeter@703: _min=i; kpeter@701: } 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@703: int findMin() { kpeter@701: int min_loc=-1, min_val; kpeter@703: int x=_head; kpeter@701: if( x!=-1 ) { kpeter@703: min_val=_data[x].prio; kpeter@701: min_loc=x; kpeter@703: x=_data[x].right_neighbor; kpeter@701: kpeter@701: while( x!=-1 ) { kpeter@703: if( _comp( _data[x].prio,min_val ) ) { kpeter@703: min_val=_data[x].prio; kpeter@701: min_loc=x; kpeter@701: } kpeter@703: x=_data[x].right_neighbor; kpeter@701: } kpeter@701: } kpeter@701: return min_loc; kpeter@701: } kpeter@701: kpeter@701: void merge(int a) { kpeter@701: interleave(a); kpeter@701: kpeter@703: int x=_head; kpeter@701: if( -1!=x ) { kpeter@703: int x_prev=-1, x_next=_data[x].right_neighbor; kpeter@701: while( -1!=x_next ) { kpeter@703: if( _data[x].degree!=_data[x_next].degree || ( -1!=_data[x_next].right_neighbor && _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) { kpeter@701: x_prev=x; kpeter@701: x=x_next; kpeter@701: } kpeter@701: else { kpeter@703: if( _comp(_data[x].prio,_data[x_next].prio) ) { kpeter@703: _data[x].right_neighbor=_data[x_next].right_neighbor; kpeter@701: fuse(x_next,x); kpeter@701: } kpeter@701: else { kpeter@703: if( -1==x_prev ) { _head=x_next; } kpeter@701: else { kpeter@703: _data[x_prev].right_neighbor=x_next; kpeter@701: } kpeter@701: fuse(x,x_next); kpeter@701: x=x_next; kpeter@701: } kpeter@701: } kpeter@703: x_next=_data[x].right_neighbor; kpeter@701: } kpeter@701: } kpeter@701: } kpeter@701: kpeter@701: void interleave(int a) { kpeter@701: int other=-1, head_other=-1; kpeter@701: kpeter@703: while( -1!=a || -1!=_head ) { kpeter@701: if( -1==a ) { kpeter@701: if( -1==head_other ) { kpeter@703: head_other=_head; kpeter@701: } kpeter@701: else { kpeter@703: _data[other].right_neighbor=_head; kpeter@701: } kpeter@703: _head=-1; kpeter@701: } kpeter@703: else if( -1==_head ) { kpeter@701: if( -1==head_other ) { kpeter@701: head_other=a; kpeter@701: } kpeter@701: else { kpeter@703: _data[other].right_neighbor=a; kpeter@701: } kpeter@701: a=-1; kpeter@701: } kpeter@701: else { kpeter@703: if( _data[a].degree<_data[_head].degree ) { kpeter@701: if( -1==head_other ) { kpeter@701: head_other=a; kpeter@701: } kpeter@701: else { kpeter@703: _data[other].right_neighbor=a; kpeter@701: } kpeter@701: other=a; kpeter@703: a=_data[a].right_neighbor; kpeter@701: } kpeter@701: else { kpeter@701: if( -1==head_other ) { kpeter@703: head_other=_head; kpeter@701: } kpeter@701: else { kpeter@703: _data[other].right_neighbor=_head; kpeter@701: } kpeter@703: other=_head; kpeter@703: _head=_data[_head].right_neighbor; kpeter@701: } kpeter@701: } kpeter@701: } kpeter@703: _head=head_other; kpeter@701: } kpeter@701: kpeter@701: // Lacing a under 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@701: // It is invoked only if a 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@701: 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@701: store() : parent(-1), right_neighbor(-1), child(-1), degree(0), in(true) {} kpeter@701: }; kpeter@701: }; kpeter@701: kpeter@701: } //namespace lemon kpeter@701: kpeter@701: #endif //LEMON_BINOM_HEAP_H kpeter@701: