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_PAIRING_HEAP_H kpeter@701: #define LEMON_PAIRING_HEAP_H kpeter@701: kpeter@701: ///\file kpeter@703: ///\ingroup heaps kpeter@703: ///\brief Pairing heap implementation. kpeter@701: kpeter@701: #include kpeter@703: #include kpeter@701: #include kpeter@701: #include kpeter@701: kpeter@701: namespace lemon { kpeter@701: kpeter@703: /// \ingroup heaps kpeter@701: /// kpeter@701: ///\brief Pairing Heap. kpeter@701: /// kpeter@703: /// This class implements the \e pairing \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 pairing 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 PairingHeap { 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; 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 PairingHeap(ItemIntMap &map) kpeter@703: : _min(0), _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: PairingHeap(ItemIntMap &map, const Compare &comp) kpeter@703: : _min(0), _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(); kpeter@703: _min = 0; kpeter@703: _num_items = 0; 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: } else { kpeter@703: _data[i].parent=_data[i].child=-1; kpeter@703: _data[i].left_child=false; kpeter@703: _data[i].degree=0; kpeter@703: _data[i].in=true; kpeter@701: } kpeter@701: kpeter@703: _data[i].prio=value; kpeter@701: kpeter@703: if ( _num_items!=0 ) { kpeter@703: if ( _comp( value, _data[_min].prio) ) { kpeter@703: fuse(i,_min); kpeter@703: _min=i; kpeter@701: } kpeter@703: else fuse(_min,i); kpeter@701: } kpeter@703: else _min=i; 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: const 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@705: std::vector trees; kpeter@705: int i=0, child_right = 0; kpeter@703: _data[_min].in=false; kpeter@701: kpeter@703: if( -1!=_data[_min].child ) { kpeter@703: i=_data[_min].child; kpeter@705: trees.push_back(i); kpeter@703: _data[i].parent = -1; kpeter@703: _data[_min].child = -1; kpeter@701: kpeter@701: int ch=-1; kpeter@703: while( _data[i].child!=-1 ) { kpeter@703: ch=_data[i].child; kpeter@703: if( _data[ch].left_child && i==_data[ch].parent ) { kpeter@705: break; kpeter@701: } else { kpeter@703: if( _data[ch].left_child ) { kpeter@703: child_right=_data[ch].parent; kpeter@703: _data[ch].parent = i; kpeter@703: --_data[i].degree; kpeter@701: } kpeter@701: else { kpeter@701: child_right=ch; kpeter@703: _data[i].child=-1; kpeter@703: _data[i].degree=0; kpeter@701: } kpeter@703: _data[child_right].parent = -1; kpeter@705: trees.push_back(child_right); kpeter@701: i = child_right; kpeter@701: } kpeter@701: } kpeter@701: kpeter@705: int num_child = trees.size(); kpeter@701: int other; kpeter@701: for( i=0; i=2) { kpeter@705: if ( _comp(_data[trees[i]].prio, _data[trees[i-2]].prio) ) { kpeter@705: other=trees[i]; kpeter@705: trees[i]=trees[i-2]; kpeter@705: trees[i-2]=other; kpeter@701: } kpeter@705: fuse( trees[i-2], trees[i] ); kpeter@701: i-=2; kpeter@701: } kpeter@705: _min = trees[0]; kpeter@701: } kpeter@705: else { kpeter@703: _min = _data[_min].child; kpeter@701: } kpeter@701: kpeter@703: if (_min >= 0) _data[_min].left_child = false; 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@703: _data[i].prio=value; kpeter@703: int p=_data[i].parent; kpeter@701: kpeter@703: if( _data[i].left_child && i!=_data[p].child ) { kpeter@703: p=_data[p].parent; kpeter@701: } kpeter@701: kpeter@703: if ( p!=-1 && _comp(value,_data[p].prio) ) { kpeter@701: cut(i,p); kpeter@703: if ( _comp(_data[_min].prio,value) ) { kpeter@703: fuse(_min,i); kpeter@701: } else { kpeter@703: fuse(i,_min); kpeter@703: _min=i; kpeter@701: } 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) erase(i); 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@701: kpeter@701: void cut(int a, int b) { kpeter@701: int child_a; kpeter@703: switch (_data[a].degree) { kpeter@701: case 2: kpeter@703: child_a = _data[_data[a].child].parent; kpeter@703: if( _data[a].left_child ) { kpeter@703: _data[child_a].left_child=true; kpeter@703: _data[b].child=child_a; kpeter@703: _data[child_a].parent=_data[a].parent; kpeter@701: } kpeter@701: else { kpeter@703: _data[child_a].left_child=false; kpeter@703: _data[child_a].parent=b; kpeter@703: if( a!=_data[b].child ) kpeter@703: _data[_data[b].child].parent=child_a; kpeter@701: else kpeter@703: _data[b].child=child_a; kpeter@701: } kpeter@703: --_data[a].degree; kpeter@703: _data[_data[a].child].parent=a; kpeter@701: break; kpeter@701: kpeter@701: case 1: kpeter@703: child_a = _data[a].child; kpeter@703: if( !_data[child_a].left_child ) { kpeter@703: --_data[a].degree; kpeter@703: if( _data[a].left_child ) { kpeter@703: _data[child_a].left_child=true; kpeter@703: _data[child_a].parent=_data[a].parent; kpeter@703: _data[b].child=child_a; kpeter@701: } kpeter@701: else { kpeter@703: _data[child_a].left_child=false; kpeter@703: _data[child_a].parent=b; kpeter@703: if( a!=_data[b].child ) kpeter@703: _data[_data[b].child].parent=child_a; kpeter@701: else kpeter@703: _data[b].child=child_a; kpeter@701: } kpeter@703: _data[a].child=-1; kpeter@701: } kpeter@701: else { kpeter@703: --_data[b].degree; kpeter@703: if( _data[a].left_child ) { kpeter@703: _data[b].child = kpeter@703: (1==_data[b].degree) ? _data[a].parent : -1; kpeter@701: } else { kpeter@703: if (1==_data[b].degree) kpeter@703: _data[_data[b].child].parent=b; kpeter@701: else kpeter@703: _data[b].child=-1; kpeter@701: } kpeter@701: } kpeter@701: break; kpeter@701: kpeter@701: case 0: kpeter@703: --_data[b].degree; kpeter@703: if( _data[a].left_child ) { kpeter@703: _data[b].child = kpeter@703: (0!=_data[b].degree) ? _data[a].parent : -1; kpeter@701: } else { kpeter@703: if( 0!=_data[b].degree ) kpeter@703: _data[_data[b].child].parent=b; kpeter@701: else kpeter@703: _data[b].child=-1; kpeter@701: } kpeter@701: break; kpeter@701: } kpeter@703: _data[a].parent=-1; kpeter@703: _data[a].left_child=false; kpeter@701: } kpeter@701: kpeter@701: void fuse(int a, int b) { kpeter@703: int child_a = _data[a].child; kpeter@703: int child_b = _data[b].child; kpeter@703: _data[a].child=b; kpeter@703: _data[b].parent=a; kpeter@703: _data[b].left_child=true; kpeter@701: kpeter@701: if( -1!=child_a ) { kpeter@703: _data[b].child=child_a; kpeter@703: _data[child_a].parent=b; kpeter@703: _data[child_a].left_child=false; kpeter@703: ++_data[b].degree; kpeter@701: kpeter@701: if( -1!=child_b ) { kpeter@703: _data[b].child=child_b; kpeter@703: _data[child_b].parent=child_a; kpeter@701: } kpeter@701: } kpeter@703: else { ++_data[a].degree; } kpeter@701: } kpeter@701: kpeter@701: class store { kpeter@701: friend class PairingHeap; kpeter@701: kpeter@701: Item name; kpeter@701: int parent; kpeter@701: int child; kpeter@701: bool left_child; kpeter@701: int degree; kpeter@701: bool in; kpeter@701: Prio prio; kpeter@701: kpeter@701: store() : parent(-1), child(-1), left_child(false), degree(0), in(true) {} kpeter@701: }; kpeter@701: }; kpeter@701: kpeter@701: } //namespace lemon kpeter@701: kpeter@701: #endif //LEMON_PAIRING_HEAP_H kpeter@701: