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@703: ///efficient. \c Compare 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@703: ///\param _Prio Type of the priority of the items. deba@703: ///\param _ItemIntMap A read and writable Item int map, used internally deba@703: ///to handle the cross references. deba@703: ///\param _Compare A class for the ordering of the priorities. The deba@703: ///default is \c std::less<_Prio>. deba@703: /// deba@703: ///\sa BinHeap deba@703: ///\sa Dijkstra deba@703: #ifdef DOXYGEN deba@703: template deba@703: #else deba@703: template > deba@703: #endif deba@703: class FibHeap { deba@703: public: deba@703: ///\e deba@703: typedef _ItemIntMap ItemIntMap; deba@703: ///\e deba@703: 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@703: typedef _Compare Compare; deba@703: deba@703: private: deba@703: class store; deba@703: deba@703: std::vector container; deba@703: int minimum; deba@703: ItemIntMap &iimap; deba@703: Compare comp; deba@703: int num_items; deba@703: deba@703: public: deba@703: ///Status of the nodes deba@703: enum State { deba@703: ///The node is in the heap deba@703: IN_HEAP = 0, deba@703: ///The node has never been in the heap deba@703: PRE_HEAP = -1, deba@703: ///The node was in the heap but it got out of it deba@703: POST_HEAP = -2 deba@703: }; deba@703: deba@703: /// \brief The constructor deba@703: /// deba@703: /// \c _iimap should be given to the constructor, since it is deba@703: /// used internally to handle the cross references. deba@703: explicit FibHeap(ItemIntMap &_iimap) deba@703: : minimum(0), iimap(_iimap), num_items() {} deba@703: deba@703: /// \brief The constructor deba@703: /// deba@703: /// \c _iimap should be given to the constructor, since it is used deba@703: /// internally to handle the cross references. \c _comp is an deba@703: /// object for ordering of the priorities. deba@703: FibHeap(ItemIntMap &_iimap, const Compare &_comp) deba@703: : minimum(0), iimap(_iimap), comp(_comp), num_items() {} 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@703: int size() const { return num_items; } 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@703: bool empty() const { return num_items==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@703: container.clear(); minimum = 0; num_items = 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@703: int i=iimap[item]; deba@703: if ( i >= 0 && container[i].in ) { deba@703: if ( comp(value, container[i].prio) ) decrease(item, value); deba@703: if ( comp(container[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@703: int i=iimap[item]; deba@703: if ( i < 0 ) { deba@703: int s=container.size(); deba@703: iimap.set( item, s ); deba@703: store st; deba@703: st.name=item; deba@703: container.push_back(st); deba@703: i=s; deba@703: } else { deba@703: container[i].parent=container[i].child=-1; deba@703: container[i].degree=0; deba@703: container[i].in=true; deba@703: container[i].marked=false; deba@703: } deba@703: deba@703: if ( num_items ) { deba@703: container[container[minimum].right_neighbor].left_neighbor=i; deba@703: container[i].right_neighbor=container[minimum].right_neighbor; deba@703: container[minimum].right_neighbor=i; deba@703: container[i].left_neighbor=minimum; deba@703: if ( comp( value, container[minimum].prio) ) minimum=i; deba@703: } else { deba@703: container[i].right_neighbor=container[i].left_neighbor=i; deba@703: minimum=i; deba@703: } deba@703: container[i].prio=value; deba@703: ++num_items; 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@703: Item top() const { return container[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@703: const Prio& prio() const { return container[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@703: return container[iimap[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@703: if ( container[minimum].left_neighbor==minimum ) { deba@703: container[minimum].in=false; deba@703: if ( container[minimum].degree!=0 ) { deba@703: makeroot(container[minimum].child); deba@703: minimum=container[minimum].child; deba@703: balance(); deba@703: } deba@703: } else { deba@703: int right=container[minimum].right_neighbor; deba@703: unlace(minimum); deba@703: container[minimum].in=false; deba@703: if ( container[minimum].degree > 0 ) { deba@703: int left=container[minimum].left_neighbor; deba@703: int child=container[minimum].child; deba@703: int last_child=container[child].left_neighbor; deba@703: deba@703: makeroot(child); deba@703: deba@703: container[left].right_neighbor=child; deba@703: container[child].left_neighbor=left; deba@703: container[right].left_neighbor=last_child; deba@703: container[last_child].right_neighbor=right; deba@703: } deba@703: minimum=right; deba@703: balance(); deba@703: } // the case where there are more roots deba@703: --num_items; 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@703: int i=iimap[item]; deba@703: deba@703: if ( i >= 0 && container[i].in ) { deba@703: if ( container[i].parent!=-1 ) { deba@703: int p=container[i].parent; deba@703: cut(i,p); deba@703: cascade(p); deba@703: } deba@703: 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@703: int i=iimap[item]; deba@703: container[i].prio=value; deba@703: int p=container[i].parent; deba@703: deba@703: if ( p!=-1 && comp(value, container[p].prio) ) { deba@703: cut(i,p); deba@703: cascade(p); deba@703: } deba@703: if ( comp(value, container[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@703: int i=iimap[item]; deba@703: if( i>=0 ) { deba@703: if ( container[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@703: /// 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@703: iimap[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@703: int maxdeg=int( std::floor( 2.08*log(double(container.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@703: int anchor=container[minimum].left_neighbor; deba@703: 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@703: int d=container[active].degree; deba@703: next=container[active].right_neighbor; deba@703: deba@703: while (A[d]!=-1) { deba@703: if( comp(container[active].prio, container[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@703: while ( container[minimum].parent >=0 ) deba@703: minimum=container[minimum].parent; deba@703: int s=minimum; deba@703: int m=minimum; deba@703: do { deba@703: if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; deba@703: s=container[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@703: container[s].parent=-1; deba@703: s=container[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@703: --container[b].degree; deba@703: deba@703: if ( container[b].degree !=0 ) { deba@703: int child=container[b].child; deba@703: if ( child==a ) deba@703: container[b].child=container[child].right_neighbor; deba@703: unlace(a); deba@703: } deba@703: deba@703: deba@703: /*Lacing a to the roots.*/ deba@703: int right=container[minimum].right_neighbor; deba@703: container[minimum].right_neighbor=a; deba@703: container[a].left_neighbor=minimum; deba@703: container[a].right_neighbor=right; deba@703: container[right].left_neighbor=a; deba@703: deba@703: container[a].parent=-1; deba@703: container[a].marked=false; deba@703: } deba@703: deba@703: void cascade(int a) { deba@703: if ( container[a].parent!=-1 ) { deba@703: int p=container[a].parent; deba@703: deba@703: if ( container[a].marked==false ) container[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@703: container[b].parent=a; deba@703: deba@703: if (container[a].degree==0) { deba@703: container[b].left_neighbor=b; deba@703: container[b].right_neighbor=b; deba@703: container[a].child=b; deba@703: } else { deba@703: int child=container[a].child; deba@703: int last_child=container[child].left_neighbor; deba@703: container[child].left_neighbor=b; deba@703: container[b].right_neighbor=child; deba@703: container[last_child].right_neighbor=b; deba@703: container[b].left_neighbor=last_child; deba@703: } deba@703: deba@703: ++container[a].degree; deba@703: deba@703: container[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@703: int leftn=container[a].left_neighbor; deba@703: int rightn=container[a].right_neighbor; deba@703: container[leftn].right_neighbor=rightn; deba@703: container[rightn].left_neighbor=leftn; deba@703: } deba@703: deba@703: deba@703: 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@703: 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: