deba@728: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@728: * deba@728: * This file is a part of LEMON, a generic C++ optimization library. deba@728: * deba@728: * Copyright (C) 2003-2009 deba@728: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@728: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@728: * deba@728: * Permission to use, modify and distribute this software is granted deba@728: * provided that this copyright notice appears in all copies. For deba@728: * precise terms see the accompanying LICENSE file. deba@728: * deba@728: * This software is provided "AS IS" with no warranty of any kind, deba@728: * express or implied, and with no claim as to its suitability for any deba@728: * purpose. deba@728: * deba@728: */ deba@728: deba@728: #ifndef LEMON_FIB_HEAP_H deba@728: #define LEMON_FIB_HEAP_H deba@728: deba@728: ///\file kpeter@757: ///\ingroup heaps kpeter@756: ///\brief Fibonacci heap implementation. deba@728: deba@728: #include kpeter@756: #include deba@728: #include deba@728: #include deba@728: deba@728: namespace lemon { deba@728: kpeter@757: /// \ingroup heaps deba@728: /// kpeter@756: /// \brief Fibonacci heap data structure. deba@728: /// kpeter@756: /// This class implements the \e Fibonacci \e heap data structure. kpeter@756: /// It fully conforms to the \ref concepts::Heap "heap concept". deba@728: /// kpeter@756: /// The methods \ref increase() and \ref erase() are not efficient in a kpeter@756: /// Fibonacci heap. In case of many calls of these operations, it is kpeter@756: /// better to use other heap structure, e.g. \ref BinHeap "binary heap". deba@728: /// kpeter@756: /// \tparam PR Type of the priorities of the items. kpeter@756: /// \tparam IM A read-writable item map with \c int values, used kpeter@756: /// internally to handle the cross references. kpeter@756: /// \tparam CMP A functor class for comparing the priorities. kpeter@756: /// The default is \c std::less. deba@728: #ifdef DOXYGEN kpeter@756: template deba@728: #else kpeter@756: template > deba@728: #endif deba@728: class FibHeap { deba@728: public: kpeter@756: kpeter@756: /// Type of the item-int map. deba@730: typedef IM ItemIntMap; kpeter@756: /// Type of the priorities. kpeter@756: typedef PR Prio; kpeter@756: /// Type of the items stored in the heap. deba@728: typedef typename ItemIntMap::Key Item; kpeter@756: /// Type of the item-priority pairs. deba@728: typedef std::pair Pair; kpeter@756: /// Functor type for comparing the priorities. deba@730: typedef CMP Compare; deba@728: deba@728: private: deba@730: class Store; deba@728: deba@730: std::vector _data; deba@730: int _minimum; deba@730: ItemIntMap &_iim; deba@730: Compare _comp; deba@730: int _num; deba@728: deba@728: public: deba@730: kpeter@756: /// \brief Type to represent the states of the items. deba@730: /// kpeter@756: /// Each item has a state associated to it. It can be "in heap", kpeter@756: /// "pre-heap" or "post-heap". The latter two are indifferent from the deba@730: /// heap's point of view, but may be useful to the user. deba@730: /// deba@730: /// The item-int map must be initialized in such way that it assigns deba@730: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@728: enum State { deba@730: IN_HEAP = 0, ///< = 0. deba@730: PRE_HEAP = -1, ///< = -1. deba@730: POST_HEAP = -2 ///< = -2. deba@728: }; deba@728: kpeter@756: /// \brief Constructor. deba@728: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. deba@730: explicit FibHeap(ItemIntMap &map) deba@730: : _minimum(0), _iim(map), _num() {} deba@728: kpeter@756: /// \brief Constructor. deba@728: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. kpeter@756: /// \param comp The function object used for comparing the priorities. deba@730: FibHeap(ItemIntMap &map, const Compare &comp) deba@730: : _minimum(0), _iim(map), _comp(comp), _num() {} deba@728: deba@728: /// \brief The number of items stored in the heap. deba@728: /// kpeter@756: /// This function returns the number of items stored in the heap. deba@730: int size() const { return _num; } deba@728: kpeter@756: /// \brief Check if the heap is empty. deba@728: /// kpeter@756: /// This function returns \c true if the heap is empty. deba@730: bool empty() const { return _num==0; } deba@728: kpeter@756: /// \brief Make the heap empty. deba@728: /// kpeter@756: /// This functon makes the heap empty. kpeter@756: /// It does not change the cross reference map. If you want to reuse kpeter@756: /// a heap that is not surely empty, you should first clear it and kpeter@756: /// then you should set the cross reference map to \c PRE_HEAP kpeter@756: /// for each item. deba@728: void clear() { deba@730: _data.clear(); _minimum = 0; _num = 0; deba@728: } deba@728: kpeter@756: /// \brief Insert an item into the heap with the given priority. deba@728: /// kpeter@756: /// This function inserts the given item into the heap with the kpeter@756: /// given priority. kpeter@756: /// \param item The item to insert. kpeter@756: /// \param prio The priority of the item. kpeter@756: /// \pre \e item must not be stored in the heap. kpeter@756: void push (const Item& item, const Prio& prio) { deba@730: int i=_iim[item]; deba@728: if ( i < 0 ) { deba@730: int s=_data.size(); deba@730: _iim.set( item, s ); deba@730: Store st; deba@728: st.name=item; deba@730: _data.push_back(st); deba@728: i=s; deba@728: } else { deba@730: _data[i].parent=_data[i].child=-1; deba@730: _data[i].degree=0; deba@730: _data[i].in=true; deba@730: _data[i].marked=false; deba@728: } deba@728: deba@730: if ( _num ) { deba@730: _data[_data[_minimum].right_neighbor].left_neighbor=i; deba@730: _data[i].right_neighbor=_data[_minimum].right_neighbor; deba@730: _data[_minimum].right_neighbor=i; deba@730: _data[i].left_neighbor=_minimum; kpeter@756: if ( _comp( prio, _data[_minimum].prio) ) _minimum=i; deba@728: } else { deba@730: _data[i].right_neighbor=_data[i].left_neighbor=i; deba@730: _minimum=i; deba@728: } kpeter@756: _data[i].prio=prio; deba@730: ++_num; deba@728: } deba@728: kpeter@756: /// \brief Return the item having minimum priority. deba@728: /// kpeter@756: /// This function returns the item having minimum priority. kpeter@756: /// \pre The heap must be non-empty. deba@730: Item top() const { return _data[_minimum].name; } deba@728: kpeter@756: /// \brief The minimum priority. deba@728: /// kpeter@756: /// This function returns the minimum priority. kpeter@756: /// \pre The heap must be non-empty. kpeter@756: Prio prio() const { return _data[_minimum].prio; } deba@728: kpeter@756: /// \brief Remove the item having minimum priority. deba@728: /// kpeter@756: /// This function removes the item having minimum priority. deba@728: /// \pre The heap must be non-empty. deba@728: void pop() { deba@728: /*The first case is that there are only one root.*/ deba@730: if ( _data[_minimum].left_neighbor==_minimum ) { deba@730: _data[_minimum].in=false; deba@730: if ( _data[_minimum].degree!=0 ) { kpeter@758: makeRoot(_data[_minimum].child); deba@730: _minimum=_data[_minimum].child; deba@728: balance(); deba@728: } deba@728: } else { deba@730: int right=_data[_minimum].right_neighbor; deba@730: unlace(_minimum); deba@730: _data[_minimum].in=false; deba@730: if ( _data[_minimum].degree > 0 ) { deba@730: int left=_data[_minimum].left_neighbor; deba@730: int child=_data[_minimum].child; deba@730: int last_child=_data[child].left_neighbor; deba@728: kpeter@758: makeRoot(child); deba@728: deba@730: _data[left].right_neighbor=child; deba@730: _data[child].left_neighbor=left; deba@730: _data[right].left_neighbor=last_child; deba@730: _data[last_child].right_neighbor=right; deba@728: } deba@730: _minimum=right; deba@728: balance(); deba@728: } // the case where there are more roots deba@730: --_num; deba@728: } deba@728: kpeter@756: /// \brief Remove the given item from the heap. deba@728: /// kpeter@756: /// This function removes the given item from the heap if it is kpeter@756: /// already stored. kpeter@756: /// \param item The item to delete. kpeter@756: /// \pre \e item must be in the heap. deba@728: void erase (const Item& item) { deba@730: int i=_iim[item]; deba@728: deba@730: if ( i >= 0 && _data[i].in ) { deba@730: if ( _data[i].parent!=-1 ) { deba@730: int p=_data[i].parent; deba@728: cut(i,p); deba@728: cascade(p); deba@728: } deba@730: _minimum=i; //As if its prio would be -infinity deba@728: pop(); deba@728: } deba@728: } deba@728: kpeter@756: /// \brief The priority of the given item. deba@728: /// kpeter@756: /// This function returns the priority of the given item. kpeter@756: /// \param item The item. kpeter@756: /// \pre \e item must be in the heap. kpeter@756: Prio operator[](const Item& item) const { kpeter@756: return _data[_iim[item]].prio; kpeter@756: } kpeter@756: kpeter@756: /// \brief Set the priority of an item or insert it, if it is kpeter@756: /// not stored in the heap. kpeter@756: /// kpeter@756: /// This method sets the priority of the given item if it is kpeter@756: /// already stored in the heap. Otherwise it inserts the given kpeter@756: /// item into the heap with the given priority. kpeter@756: /// \param item The item. kpeter@756: /// \param prio The priority. kpeter@756: void set (const Item& item, const Prio& prio) { deba@730: int i=_iim[item]; kpeter@756: if ( i >= 0 && _data[i].in ) { kpeter@756: if ( _comp(prio, _data[i].prio) ) decrease(item, prio); kpeter@756: if ( _comp(_data[i].prio, prio) ) increase(item, prio); kpeter@756: } else push(item, prio); kpeter@756: } kpeter@756: kpeter@756: /// \brief Decrease the priority of an item to the given value. kpeter@756: /// kpeter@756: /// This function decreases the priority of an item to the given value. kpeter@756: /// \param item The item. kpeter@756: /// \param prio The priority. kpeter@756: /// \pre \e item must be stored in the heap with priority at least \e prio. kpeter@756: void decrease (const Item& item, const Prio& prio) { kpeter@756: int i=_iim[item]; kpeter@756: _data[i].prio=prio; deba@730: int p=_data[i].parent; deba@728: kpeter@756: if ( p!=-1 && _comp(prio, _data[p].prio) ) { deba@728: cut(i,p); deba@728: cascade(p); deba@728: } kpeter@756: if ( _comp(prio, _data[_minimum].prio) ) _minimum=i; deba@728: } deba@728: kpeter@756: /// \brief Increase the priority of an item to the given value. deba@728: /// kpeter@756: /// This function increases the priority of an item to the given value. kpeter@756: /// \param item The item. kpeter@756: /// \param prio The priority. kpeter@756: /// \pre \e item must be stored in the heap with priority at most \e prio. kpeter@756: void increase (const Item& item, const Prio& prio) { deba@728: erase(item); kpeter@756: push(item, prio); deba@728: } deba@728: kpeter@756: /// \brief Return the state of an item. deba@728: /// kpeter@756: /// This method returns \c PRE_HEAP if the given item has never kpeter@756: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@756: /// and \c POST_HEAP otherwise. kpeter@756: /// In the latter case it is possible that the item will get back kpeter@756: /// to the heap again. kpeter@756: /// \param item The item. deba@728: State state(const Item &item) const { deba@730: int i=_iim[item]; deba@728: if( i>=0 ) { deba@730: if ( _data[i].in ) i=0; deba@728: else i=-2; deba@728: } deba@728: return State(i); deba@728: } deba@728: kpeter@756: /// \brief Set the state of an item in the heap. deba@728: /// kpeter@756: /// This function sets the state of the given item in the heap. kpeter@756: /// It can be used to manually clear the heap when it is important kpeter@756: /// to achive better time complexity. deba@728: /// \param i The item. deba@728: /// \param st The state. It should not be \c IN_HEAP. deba@728: void state(const Item& i, State st) { deba@728: switch (st) { deba@728: case POST_HEAP: deba@728: case PRE_HEAP: deba@728: if (state(i) == IN_HEAP) { deba@728: erase(i); deba@728: } deba@730: _iim[i] = st; deba@728: break; deba@728: case IN_HEAP: deba@728: break; deba@728: } deba@728: } deba@728: deba@728: private: deba@728: deba@728: void balance() { deba@728: deba@730: int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1; deba@728: deba@728: std::vector A(maxdeg,-1); deba@728: deba@728: /* deba@728: *Recall that now minimum does not point to the minimum prio element. deba@728: *We set minimum to this during balance(). deba@728: */ deba@730: int anchor=_data[_minimum].left_neighbor; deba@730: int next=_minimum; deba@728: bool end=false; deba@728: deba@728: do { deba@728: int active=next; deba@728: if ( anchor==active ) end=true; deba@730: int d=_data[active].degree; deba@730: next=_data[active].right_neighbor; deba@728: deba@728: while (A[d]!=-1) { deba@730: if( _comp(_data[active].prio, _data[A[d]].prio) ) { deba@728: fuse(active,A[d]); deba@728: } else { deba@728: fuse(A[d],active); deba@728: active=A[d]; deba@728: } deba@728: A[d]=-1; deba@728: ++d; deba@728: } deba@728: A[d]=active; deba@728: } while ( !end ); deba@728: deba@728: deba@730: while ( _data[_minimum].parent >=0 ) deba@730: _minimum=_data[_minimum].parent; deba@730: int s=_minimum; deba@730: int m=_minimum; deba@728: do { deba@730: if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s; deba@730: s=_data[s].right_neighbor; deba@728: } while ( s != m ); deba@728: } deba@728: kpeter@758: void makeRoot(int c) { deba@728: int s=c; deba@728: do { deba@730: _data[s].parent=-1; deba@730: s=_data[s].right_neighbor; deba@728: } while ( s != c ); deba@728: } deba@728: deba@728: void cut(int a, int b) { deba@728: /* deba@728: *Replacing a from the children of b. deba@728: */ deba@730: --_data[b].degree; deba@728: deba@730: if ( _data[b].degree !=0 ) { deba@730: int child=_data[b].child; deba@728: if ( child==a ) deba@730: _data[b].child=_data[child].right_neighbor; deba@728: unlace(a); deba@728: } deba@728: deba@728: deba@728: /*Lacing a to the roots.*/ deba@730: int right=_data[_minimum].right_neighbor; deba@730: _data[_minimum].right_neighbor=a; deba@730: _data[a].left_neighbor=_minimum; deba@730: _data[a].right_neighbor=right; deba@730: _data[right].left_neighbor=a; deba@728: deba@730: _data[a].parent=-1; deba@730: _data[a].marked=false; deba@728: } deba@728: deba@728: void cascade(int a) { deba@730: if ( _data[a].parent!=-1 ) { deba@730: int p=_data[a].parent; deba@728: deba@730: if ( _data[a].marked==false ) _data[a].marked=true; deba@728: else { deba@728: cut(a,p); deba@728: cascade(p); deba@728: } deba@728: } deba@728: } deba@728: deba@728: void fuse(int a, int b) { deba@728: unlace(b); deba@728: deba@728: /*Lacing b under a.*/ deba@730: _data[b].parent=a; deba@728: deba@730: if (_data[a].degree==0) { deba@730: _data[b].left_neighbor=b; deba@730: _data[b].right_neighbor=b; deba@730: _data[a].child=b; deba@728: } else { deba@730: int child=_data[a].child; deba@730: int last_child=_data[child].left_neighbor; deba@730: _data[child].left_neighbor=b; deba@730: _data[b].right_neighbor=child; deba@730: _data[last_child].right_neighbor=b; deba@730: _data[b].left_neighbor=last_child; deba@728: } deba@728: deba@730: ++_data[a].degree; deba@728: deba@730: _data[b].marked=false; deba@728: } deba@728: deba@728: /* deba@728: *It is invoked only if a has siblings. deba@728: */ deba@728: void unlace(int a) { deba@730: int leftn=_data[a].left_neighbor; deba@730: int rightn=_data[a].right_neighbor; deba@730: _data[leftn].right_neighbor=rightn; deba@730: _data[rightn].left_neighbor=leftn; deba@728: } deba@728: deba@728: deba@730: class Store { deba@728: friend class FibHeap; deba@728: deba@728: Item name; deba@728: int parent; deba@728: int left_neighbor; deba@728: int right_neighbor; deba@728: int child; deba@728: int degree; deba@728: bool marked; deba@728: bool in; deba@728: Prio prio; deba@728: deba@730: Store() : parent(-1), child(-1), degree(), marked(false), in(true) {} deba@728: }; deba@728: }; deba@728: deba@728: } //namespace lemon deba@728: deba@728: #endif //LEMON_FIB_HEAP_H deba@728: