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