deba@703: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@703: * deba@703: * This file is a part of LEMON, a generic C++ optimization library. deba@703: * deba@703: * Copyright (C) 2003-2009 deba@703: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@703: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@703: * deba@703: * Permission to use, modify and distribute this software is granted deba@703: * provided that this copyright notice appears in all copies. For deba@703: * precise terms see the accompanying LICENSE file. deba@703: * deba@703: * This software is provided "AS IS" with no warranty of any kind, deba@703: * express or implied, and with no claim as to its suitability for any deba@703: * purpose. deba@703: * deba@703: */ deba@703: deba@703: #ifndef LEMON_FIB_HEAP_H deba@703: #define LEMON_FIB_HEAP_H deba@703: deba@703: ///\file deba@703: ///\ingroup auxdat deba@703: ///\brief Fibonacci Heap implementation. deba@703: deba@703: #include deba@703: #include deba@703: #include deba@703: deba@703: namespace lemon { deba@703: deba@703: /// \ingroup auxdat deba@703: /// deba@703: ///\brief Fibonacci Heap. deba@703: /// deba@703: ///This class implements the \e Fibonacci \e heap data structure. A \e heap deba@703: ///is a data structure for storing items with specified values called \e deba@703: ///priorities in such a way that finding the item with minimum priority is deba@705: ///efficient. \c CMP specifies the ordering of the priorities. In a heap deba@703: ///one can change the priority of an item, add or erase an item, etc. deba@703: /// deba@703: ///The methods \ref increase and \ref erase are not efficient in a Fibonacci deba@703: ///heap. In case of many calls to these operations, it is better to use a deba@703: ///\ref BinHeap "binary heap". deba@703: /// deba@705: ///\param PRIO Type of the priority of the items. deba@705: ///\param IM A read and writable Item int map, used internally deba@703: ///to handle the cross references. deba@705: ///\param CMP A class for the ordering of the priorities. The deba@705: ///default is \c std::less. deba@703: /// deba@703: ///\sa BinHeap deba@703: ///\sa Dijkstra deba@703: #ifdef DOXYGEN deba@705: template deba@703: #else deba@705: template > deba@703: #endif deba@703: class FibHeap { deba@703: public: deba@703: ///\e deba@705: typedef IM ItemIntMap; deba@703: ///\e deba@705: typedef PRIO Prio; deba@703: ///\e deba@703: typedef typename ItemIntMap::Key Item; deba@703: ///\e deba@703: typedef std::pair Pair; deba@703: ///\e deba@705: typedef CMP Compare; deba@703: deba@703: private: deba@705: class Store; deba@703: deba@705: std::vector _data; deba@705: int _minimum; deba@705: ItemIntMap &_iim; deba@705: Compare _comp; deba@705: int _num; deba@703: deba@703: public: deba@705: deba@705: /// \brief Type to represent the items states. deba@705: /// deba@705: /// Each Item element have a state associated to it. It may be "in heap", deba@705: /// "pre heap" or "post heap". The latter two are indifferent from the deba@705: /// heap's point of view, but may be useful to the user. deba@705: /// deba@705: /// The item-int map must be initialized in such way that it assigns deba@705: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@703: enum State { deba@705: IN_HEAP = 0, ///< = 0. deba@705: PRE_HEAP = -1, ///< = -1. deba@705: POST_HEAP = -2 ///< = -2. deba@703: }; deba@703: deba@703: /// \brief The constructor deba@703: /// deba@705: /// \c map should be given to the constructor, since it is deba@703: /// used internally to handle the cross references. deba@705: explicit FibHeap(ItemIntMap &map) deba@705: : _minimum(0), _iim(map), _num() {} deba@703: deba@703: /// \brief The constructor deba@703: /// deba@705: /// \c map should be given to the constructor, since it is used deba@705: /// internally to handle the cross references. \c comp is an deba@703: /// object for ordering of the priorities. deba@705: FibHeap(ItemIntMap &map, const Compare &comp) deba@705: : _minimum(0), _iim(map), _comp(comp), _num() {} deba@703: deba@703: /// \brief The number of items stored in the heap. deba@703: /// deba@703: /// Returns the number of items stored in the heap. deba@705: int size() const { return _num; } deba@703: deba@703: /// \brief Checks if the heap stores no items. deba@703: /// deba@703: /// Returns \c true if and only if the heap stores no items. deba@705: bool empty() const { return _num==0; } deba@703: deba@703: /// \brief Make empty this heap. deba@703: /// deba@703: /// Make empty this heap. It does not change the cross reference deba@703: /// map. If you want to reuse a heap what is not surely empty you deba@703: /// should first clear the heap and after that you should set the deba@703: /// cross reference map for each item to \c PRE_HEAP. deba@703: void clear() { deba@705: _data.clear(); _minimum = 0; _num = 0; deba@703: } deba@703: deba@703: /// \brief \c item gets to the heap with priority \c value independently deba@703: /// if \c item was already there. deba@703: /// deba@703: /// This method calls \ref push(\c item, \c value) if \c item is not deba@703: /// stored in the heap and it calls \ref decrease(\c item, \c value) or deba@703: /// \ref increase(\c item, \c value) otherwise. deba@703: void set (const Item& item, const Prio& value) { deba@705: int i=_iim[item]; deba@705: if ( i >= 0 && _data[i].in ) { deba@705: if ( _comp(value, _data[i].prio) ) decrease(item, value); deba@705: if ( _comp(_data[i].prio, value) ) increase(item, value); deba@703: } else push(item, value); deba@703: } deba@703: deba@703: /// \brief Adds \c item to the heap with priority \c value. deba@703: /// deba@703: /// Adds \c item to the heap with priority \c value. deba@703: /// \pre \c item must not be stored in the heap. deba@703: void push (const Item& item, const Prio& value) { deba@705: int i=_iim[item]; deba@703: if ( i < 0 ) { deba@705: int s=_data.size(); deba@705: _iim.set( item, s ); deba@705: Store st; deba@703: st.name=item; deba@705: _data.push_back(st); deba@703: i=s; deba@703: } else { deba@705: _data[i].parent=_data[i].child=-1; deba@705: _data[i].degree=0; deba@705: _data[i].in=true; deba@705: _data[i].marked=false; deba@703: } deba@703: deba@705: if ( _num ) { deba@705: _data[_data[_minimum].right_neighbor].left_neighbor=i; deba@705: _data[i].right_neighbor=_data[_minimum].right_neighbor; deba@705: _data[_minimum].right_neighbor=i; deba@705: _data[i].left_neighbor=_minimum; deba@705: if ( _comp( value, _data[_minimum].prio) ) _minimum=i; deba@703: } else { deba@705: _data[i].right_neighbor=_data[i].left_neighbor=i; deba@705: _minimum=i; deba@703: } deba@705: _data[i].prio=value; deba@705: ++_num; deba@703: } deba@703: deba@703: /// \brief Returns the item with minimum priority relative to \c Compare. deba@703: /// deba@703: /// This method returns the item with minimum priority relative to \c deba@703: /// Compare. deba@703: /// \pre The heap must be nonempty. deba@705: Item top() const { return _data[_minimum].name; } deba@703: deba@703: /// \brief Returns the minimum priority relative to \c Compare. deba@703: /// deba@703: /// It returns the minimum priority relative to \c Compare. deba@703: /// \pre The heap must be nonempty. deba@705: const Prio& prio() const { return _data[_minimum].prio; } deba@703: deba@703: /// \brief Returns the priority of \c item. deba@703: /// deba@703: /// It returns the priority of \c item. deba@703: /// \pre \c item must be in the heap. deba@703: const Prio& operator[](const Item& item) const { deba@705: return _data[_iim[item]].prio; deba@703: } deba@703: deba@703: /// \brief Deletes the item with minimum priority relative to \c Compare. deba@703: /// deba@703: /// This method deletes the item with minimum priority relative to \c deba@703: /// Compare from the heap. deba@703: /// \pre The heap must be non-empty. deba@703: void pop() { deba@703: /*The first case is that there are only one root.*/ deba@705: if ( _data[_minimum].left_neighbor==_minimum ) { deba@705: _data[_minimum].in=false; deba@705: if ( _data[_minimum].degree!=0 ) { deba@705: makeroot(_data[_minimum].child); deba@705: _minimum=_data[_minimum].child; deba@703: balance(); deba@703: } deba@703: } else { deba@705: int right=_data[_minimum].right_neighbor; deba@705: unlace(_minimum); deba@705: _data[_minimum].in=false; deba@705: if ( _data[_minimum].degree > 0 ) { deba@705: int left=_data[_minimum].left_neighbor; deba@705: int child=_data[_minimum].child; deba@705: int last_child=_data[child].left_neighbor; deba@703: deba@703: makeroot(child); deba@703: deba@705: _data[left].right_neighbor=child; deba@705: _data[child].left_neighbor=left; deba@705: _data[right].left_neighbor=last_child; deba@705: _data[last_child].right_neighbor=right; deba@703: } deba@705: _minimum=right; deba@703: balance(); deba@703: } // the case where there are more roots deba@705: --_num; deba@703: } deba@703: deba@703: /// \brief Deletes \c item from the heap. deba@703: /// deba@703: /// This method deletes \c item from the heap, if \c item was already deba@703: /// stored in the heap. It is quite inefficient in Fibonacci heaps. deba@703: void erase (const Item& item) { deba@705: int i=_iim[item]; deba@703: deba@705: if ( i >= 0 && _data[i].in ) { deba@705: if ( _data[i].parent!=-1 ) { deba@705: int p=_data[i].parent; deba@703: cut(i,p); deba@703: cascade(p); deba@703: } deba@705: _minimum=i; //As if its prio would be -infinity deba@703: pop(); deba@703: } deba@703: } deba@703: deba@703: /// \brief Decreases the priority of \c item to \c value. deba@703: /// deba@703: /// This method decreases the priority of \c item to \c value. deba@703: /// \pre \c item must be stored in the heap with priority at least \c deba@703: /// value relative to \c Compare. deba@703: void decrease (Item item, const Prio& value) { deba@705: int i=_iim[item]; deba@705: _data[i].prio=value; deba@705: int p=_data[i].parent; deba@703: deba@705: if ( p!=-1 && _comp(value, _data[p].prio) ) { deba@703: cut(i,p); deba@703: cascade(p); deba@703: } deba@705: if ( _comp(value, _data[_minimum].prio) ) _minimum=i; deba@703: } deba@703: deba@703: /// \brief Increases the priority of \c item to \c value. deba@703: /// deba@703: /// This method sets the priority of \c item to \c value. Though deba@703: /// there is no precondition on the priority of \c item, this deba@703: /// method should be used only if it is indeed necessary to increase deba@703: /// (relative to \c Compare) the priority of \c item, because this deba@703: /// method is inefficient. deba@703: void increase (Item item, const Prio& value) { deba@703: erase(item); deba@703: push(item, value); deba@703: } deba@703: deba@703: deba@703: /// \brief Returns if \c item is in, has already been in, or has never deba@703: /// been in the heap. deba@703: /// deba@703: /// This method returns PRE_HEAP if \c item has never been in the deba@703: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@703: /// otherwise. In the latter case it is possible that \c item will deba@703: /// get back to the heap again. deba@703: State state(const Item &item) const { deba@705: int i=_iim[item]; deba@703: if( i>=0 ) { deba@705: if ( _data[i].in ) i=0; deba@703: else i=-2; deba@703: } deba@703: return State(i); deba@703: } deba@703: deba@703: /// \brief Sets the state of the \c item in the heap. deba@703: /// deba@703: /// Sets the state of the \c item in the heap. It can be used to deba@703: /// manually clear the heap when it is important to achive the deba@705: /// better time _complexity. deba@703: /// \param i The item. deba@703: /// \param st The state. It should not be \c IN_HEAP. deba@703: void state(const Item& i, State st) { deba@703: switch (st) { deba@703: case POST_HEAP: deba@703: case PRE_HEAP: deba@703: if (state(i) == IN_HEAP) { deba@703: erase(i); deba@703: } deba@705: _iim[i] = st; deba@703: break; deba@703: case IN_HEAP: deba@703: break; deba@703: } deba@703: } deba@703: deba@703: private: deba@703: deba@703: void balance() { deba@703: deba@705: int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1; deba@703: deba@703: std::vector A(maxdeg,-1); deba@703: deba@703: /* deba@703: *Recall that now minimum does not point to the minimum prio element. deba@703: *We set minimum to this during balance(). deba@703: */ deba@705: int anchor=_data[_minimum].left_neighbor; deba@705: int next=_minimum; deba@703: bool end=false; deba@703: deba@703: do { deba@703: int active=next; deba@703: if ( anchor==active ) end=true; deba@705: int d=_data[active].degree; deba@705: next=_data[active].right_neighbor; deba@703: deba@703: while (A[d]!=-1) { deba@705: if( _comp(_data[active].prio, _data[A[d]].prio) ) { deba@703: fuse(active,A[d]); deba@703: } else { deba@703: fuse(A[d],active); deba@703: active=A[d]; deba@703: } deba@703: A[d]=-1; deba@703: ++d; deba@703: } deba@703: A[d]=active; deba@703: } while ( !end ); deba@703: deba@703: deba@705: while ( _data[_minimum].parent >=0 ) deba@705: _minimum=_data[_minimum].parent; deba@705: int s=_minimum; deba@705: int m=_minimum; deba@703: do { deba@705: if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s; deba@705: s=_data[s].right_neighbor; deba@703: } while ( s != m ); deba@703: } deba@703: deba@703: void makeroot(int c) { deba@703: int s=c; deba@703: do { deba@705: _data[s].parent=-1; deba@705: s=_data[s].right_neighbor; deba@703: } while ( s != c ); deba@703: } deba@703: deba@703: void cut(int a, int b) { deba@703: /* deba@703: *Replacing a from the children of b. deba@703: */ deba@705: --_data[b].degree; deba@703: deba@705: if ( _data[b].degree !=0 ) { deba@705: int child=_data[b].child; deba@703: if ( child==a ) deba@705: _data[b].child=_data[child].right_neighbor; deba@703: unlace(a); deba@703: } deba@703: deba@703: deba@703: /*Lacing a to the roots.*/ deba@705: int right=_data[_minimum].right_neighbor; deba@705: _data[_minimum].right_neighbor=a; deba@705: _data[a].left_neighbor=_minimum; deba@705: _data[a].right_neighbor=right; deba@705: _data[right].left_neighbor=a; deba@703: deba@705: _data[a].parent=-1; deba@705: _data[a].marked=false; deba@703: } deba@703: deba@703: void cascade(int a) { deba@705: if ( _data[a].parent!=-1 ) { deba@705: int p=_data[a].parent; deba@703: deba@705: if ( _data[a].marked==false ) _data[a].marked=true; deba@703: else { deba@703: cut(a,p); deba@703: cascade(p); deba@703: } deba@703: } deba@703: } deba@703: deba@703: void fuse(int a, int b) { deba@703: unlace(b); deba@703: deba@703: /*Lacing b under a.*/ deba@705: _data[b].parent=a; deba@703: deba@705: if (_data[a].degree==0) { deba@705: _data[b].left_neighbor=b; deba@705: _data[b].right_neighbor=b; deba@705: _data[a].child=b; deba@703: } else { deba@705: int child=_data[a].child; deba@705: int last_child=_data[child].left_neighbor; deba@705: _data[child].left_neighbor=b; deba@705: _data[b].right_neighbor=child; deba@705: _data[last_child].right_neighbor=b; deba@705: _data[b].left_neighbor=last_child; deba@703: } deba@703: deba@705: ++_data[a].degree; deba@703: deba@705: _data[b].marked=false; deba@703: } deba@703: deba@703: /* deba@703: *It is invoked only if a has siblings. deba@703: */ deba@703: void unlace(int a) { deba@705: int leftn=_data[a].left_neighbor; deba@705: int rightn=_data[a].right_neighbor; deba@705: _data[leftn].right_neighbor=rightn; deba@705: _data[rightn].left_neighbor=leftn; deba@703: } deba@703: deba@703: deba@705: class Store { deba@703: friend class FibHeap; deba@703: deba@703: Item name; deba@703: int parent; deba@703: int left_neighbor; deba@703: int right_neighbor; deba@703: int child; deba@703: int degree; deba@703: bool marked; deba@703: bool in; deba@703: Prio prio; deba@703: deba@705: Store() : parent(-1), child(-1), degree(), marked(false), in(true) {} deba@703: }; deba@703: }; deba@703: deba@703: } //namespace lemon deba@703: deba@703: #endif //LEMON_FIB_HEAP_H deba@703: