alpar@255: // -*- C++ -*- alpar@255: /* alpar@255: *template > alpar@255: * alpar@255: *constructors: alpar@255: * alpar@255: *FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare) alpar@255: * alpar@255: *Member functions: alpar@255: * alpar@255: *int size() : returns the number of elements in the heap alpar@255: * alpar@255: *bool empty() : true iff size()=0 alpar@255: * alpar@255: *void set(Item, Prio) : calls push(Item, Prio) if Item is not alpar@255: * in the heap, and calls decrease/increase(Item, Prio) otherwise alpar@255: * alpar@255: *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item alpar@255: * mustn't be in the heap. alpar@255: * alpar@255: *Item top() : returns the Item with least Prio. alpar@255: * Must be called only if heap is nonempty. alpar@255: * alpar@255: *Prio prio() : returns the least Prio alpar@255: * Must be called only if heap is nonempty. alpar@255: * alpar@255: *Prio get(Item) : returns Prio of Item alpar@255: * Must be called only if Item is in heap. alpar@255: * alpar@255: *void pop() : deletes the Item with least Prio alpar@255: * alpar@255: *void erase(Item) : deletes Item from the heap if it was already there alpar@255: * alpar@255: *void decrease(Item, P) : decreases prio of Item to P. alpar@255: * Item must be in the heap with prio at least P. alpar@255: * alpar@255: *void increase(Item, P) : sets prio of Item to P. alpar@255: * alpar@255: *state_enum state(Item) : returns PRE_HEAP if Item has not been in the alpar@255: * heap until now, IN_HEAP if it is in the heap at the moment, and alpar@255: * POST_HEAP otherwise. In the latter case it is possible that Item alpar@255: * will get back to the heap again. alpar@255: * alpar@255: *In Fibonacci heaps, increase and erase are not efficient, in case of alpar@255: *many calls to these operations, it is better to use bin_heap. alpar@255: */ alpar@255: alpar@255: #ifndef FIB_HEAP_H alpar@255: #define FIB_HEAP_H alpar@255: alpar@255: ///\file alpar@255: ///\brief Fibonacci Heap implementation. alpar@255: alpar@255: #include alpar@255: #include alpar@255: #include alpar@255: alpar@255: namespace hugo { alpar@255: alpar@255: /// A Fibonacci Heap implementation. alpar@255: template > alpar@255: class FibHeap { alpar@255: alpar@255: typedef Prio PrioType; alpar@255: alpar@255: class store; alpar@255: alpar@255: std::vector container; alpar@255: int minimum; alpar@255: ItemIntMap &iimap; alpar@255: Compare comp; alpar@255: int num_items; alpar@255: alpar@255: ///\todo It is use nowhere alpar@255: ///\todo It doesn't conform to the naming conventions. alpar@255: public: alpar@255: enum state_enum { alpar@255: IN_HEAP = 0, alpar@255: PRE_HEAP = -1, alpar@255: POST_HEAP = -2 alpar@255: }; alpar@255: alpar@255: public : alpar@255: alpar@255: FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {} alpar@255: FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(), alpar@255: iimap(_iimap), comp(_comp), num_items() {} alpar@255: alpar@255: alpar@255: int size() const { alpar@255: return num_items; alpar@255: } alpar@255: alpar@255: alpar@255: bool empty() const { return num_items==0; } alpar@255: alpar@255: alpar@255: void set (Item const it, PrioType const value) { alpar@255: int i=iimap[it]; alpar@255: if ( i >= 0 && container[i].in ) { alpar@255: if ( comp(value, container[i].prio) ) decrease(it, value); alpar@255: if ( comp(container[i].prio, value) ) increase(it, value); alpar@255: } else push(it, value); alpar@255: } alpar@255: alpar@255: alpar@255: void push (Item const it, PrioType const value) { alpar@255: int i=iimap[it]; alpar@255: if ( i < 0 ) { alpar@255: int s=container.size(); alpar@255: iimap.set( it, s ); alpar@255: store st; alpar@255: st.name=it; alpar@255: container.push_back(st); alpar@255: i=s; alpar@255: } else { alpar@255: container[i].parent=container[i].child=-1; alpar@255: container[i].degree=0; alpar@255: container[i].in=true; alpar@255: container[i].marked=false; alpar@255: } alpar@255: alpar@255: if ( num_items ) { alpar@255: container[container[minimum].right_neighbor].left_neighbor=i; alpar@255: container[i].right_neighbor=container[minimum].right_neighbor; alpar@255: container[minimum].right_neighbor=i; alpar@255: container[i].left_neighbor=minimum; alpar@255: if ( comp( value, container[minimum].prio) ) minimum=i; alpar@255: } else { alpar@255: container[i].right_neighbor=container[i].left_neighbor=i; alpar@255: minimum=i; alpar@255: } alpar@255: container[i].prio=value; alpar@255: ++num_items; alpar@255: } alpar@255: alpar@255: alpar@255: Item top() const { alpar@255: return container[minimum].name; alpar@255: } alpar@255: alpar@255: alpar@255: PrioType prio() const { alpar@255: return container[minimum].prio; alpar@255: } alpar@255: alpar@255: alpar@255: alpar@255: alpar@255: PrioType& operator[](const Item& it) { alpar@255: return container[iimap[it]].prio; alpar@255: } alpar@255: alpar@255: const PrioType& operator[](const Item& it) const { alpar@255: return container[iimap[it]].prio; alpar@255: } alpar@255: alpar@255: // const PrioType get(const Item& it) const { alpar@255: // return container[iimap[it]].prio; alpar@255: // } alpar@255: alpar@255: void pop() { alpar@255: /*The first case is that there are only one root.*/ alpar@255: if ( container[minimum].left_neighbor==minimum ) { alpar@255: container[minimum].in=false; alpar@255: if ( container[minimum].degree!=0 ) { alpar@255: makeroot(container[minimum].child); alpar@255: minimum=container[minimum].child; alpar@255: balance(); alpar@255: } alpar@255: } else { alpar@255: int right=container[minimum].right_neighbor; alpar@255: unlace(minimum); alpar@255: container[minimum].in=false; alpar@255: if ( container[minimum].degree > 0 ) { alpar@255: int left=container[minimum].left_neighbor; alpar@255: int child=container[minimum].child; alpar@255: int last_child=container[child].left_neighbor; alpar@255: alpar@255: makeroot(child); alpar@255: alpar@255: container[left].right_neighbor=child; alpar@255: container[child].left_neighbor=left; alpar@255: container[right].left_neighbor=last_child; alpar@255: container[last_child].right_neighbor=right; alpar@255: } alpar@255: minimum=right; alpar@255: balance(); alpar@255: } // the case where there are more roots alpar@255: --num_items; alpar@255: } alpar@255: alpar@255: alpar@255: void erase (const Item& it) { alpar@255: int i=iimap[it]; alpar@255: alpar@255: if ( i >= 0 && container[i].in ) { alpar@255: if ( container[i].parent!=-1 ) { alpar@255: int p=container[i].parent; alpar@255: cut(i,p); alpar@255: cascade(p); alpar@255: } alpar@255: minimum=i; //As if its prio would be -infinity alpar@255: pop(); alpar@255: } alpar@255: } alpar@255: alpar@255: alpar@255: void decrease (Item it, PrioType const value) { alpar@255: int i=iimap[it]; alpar@255: container[i].prio=value; alpar@255: int p=container[i].parent; alpar@255: alpar@255: if ( p!=-1 && comp(value, container[p].prio) ) { alpar@255: cut(i,p); alpar@255: cascade(p); alpar@255: } alpar@255: if ( comp(value, container[minimum].prio) ) minimum=i; alpar@255: } alpar@255: alpar@255: alpar@255: void increase (Item it, PrioType const value) { alpar@255: erase(it); alpar@255: push(it, value); alpar@255: } alpar@255: alpar@255: alpar@255: state_enum state(const Item &it) const { alpar@255: int i=iimap[it]; alpar@255: if( i>=0 ) { alpar@255: if ( container[i].in ) i=0; alpar@255: else i=-2; alpar@255: } alpar@255: return state_enum(i); alpar@255: } alpar@255: alpar@255: alpar@255: private: alpar@255: alpar@255: void balance() { alpar@255: alpar@255: int maxdeg=int( floor( 2.08*log(double(container.size()))))+1; alpar@255: alpar@255: std::vector A(maxdeg,-1); alpar@255: alpar@255: /* alpar@255: *Recall that now minimum does not point to the minimum prio element. alpar@255: *We set minimum to this during balance(). alpar@255: */ alpar@255: int anchor=container[minimum].left_neighbor; alpar@255: int next=minimum; alpar@255: bool end=false; alpar@255: alpar@255: do { alpar@255: int active=next; alpar@255: if ( anchor==active ) end=true; alpar@255: int d=container[active].degree; alpar@255: next=container[active].right_neighbor; alpar@255: alpar@255: while (A[d]!=-1) { alpar@255: if( comp(container[active].prio, container[A[d]].prio) ) { alpar@255: fuse(active,A[d]); alpar@255: } else { alpar@255: fuse(A[d],active); alpar@255: active=A[d]; alpar@255: } alpar@255: A[d]=-1; alpar@255: ++d; alpar@255: } alpar@255: A[d]=active; alpar@255: } while ( !end ); alpar@255: alpar@255: alpar@255: while ( container[minimum].parent >=0 ) minimum=container[minimum].parent; alpar@255: int s=minimum; alpar@255: int m=minimum; alpar@255: do { alpar@255: if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; alpar@255: s=container[s].right_neighbor; alpar@255: } while ( s != m ); alpar@255: } alpar@255: alpar@255: alpar@255: void makeroot (int c) { alpar@255: int s=c; alpar@255: do { alpar@255: container[s].parent=-1; alpar@255: s=container[s].right_neighbor; alpar@255: } while ( s != c ); alpar@255: } alpar@255: alpar@255: alpar@255: void cut (int a, int b) { alpar@255: /* alpar@255: *Replacing a from the children of b. alpar@255: */ alpar@255: --container[b].degree; alpar@255: alpar@255: if ( container[b].degree !=0 ) { alpar@255: int child=container[b].child; alpar@255: if ( child==a ) alpar@255: container[b].child=container[child].right_neighbor; alpar@255: unlace(a); alpar@255: } alpar@255: alpar@255: alpar@255: /*Lacing a to the roots.*/ alpar@255: int right=container[minimum].right_neighbor; alpar@255: container[minimum].right_neighbor=a; alpar@255: container[a].left_neighbor=minimum; alpar@255: container[a].right_neighbor=right; alpar@255: container[right].left_neighbor=a; alpar@255: alpar@255: container[a].parent=-1; alpar@255: container[a].marked=false; alpar@255: } alpar@255: alpar@255: alpar@255: void cascade (int a) alpar@255: { alpar@255: if ( container[a].parent!=-1 ) { alpar@255: int p=container[a].parent; alpar@255: alpar@255: if ( container[a].marked==false ) container[a].marked=true; alpar@255: else { alpar@255: cut(a,p); alpar@255: cascade(p); alpar@255: } alpar@255: } alpar@255: } alpar@255: alpar@255: alpar@255: void fuse (int a, int b) { alpar@255: unlace(b); alpar@255: alpar@255: /*Lacing b under a.*/ alpar@255: container[b].parent=a; alpar@255: alpar@255: if (container[a].degree==0) { alpar@255: container[b].left_neighbor=b; alpar@255: container[b].right_neighbor=b; alpar@255: container[a].child=b; alpar@255: } else { alpar@255: int child=container[a].child; alpar@255: int last_child=container[child].left_neighbor; alpar@255: container[child].left_neighbor=b; alpar@255: container[b].right_neighbor=child; alpar@255: container[last_child].right_neighbor=b; alpar@255: container[b].left_neighbor=last_child; alpar@255: } alpar@255: alpar@255: ++container[a].degree; alpar@255: alpar@255: container[b].marked=false; alpar@255: } alpar@255: alpar@255: alpar@255: /* alpar@255: *It is invoked only if a has siblings. alpar@255: */ alpar@255: void unlace (int a) { alpar@255: int leftn=container[a].left_neighbor; alpar@255: int rightn=container[a].right_neighbor; alpar@255: container[leftn].right_neighbor=rightn; alpar@255: container[rightn].left_neighbor=leftn; alpar@255: } alpar@255: alpar@255: alpar@255: class store { alpar@255: friend class FibHeap; alpar@255: alpar@255: Item name; alpar@255: int parent; alpar@255: int left_neighbor; alpar@255: int right_neighbor; alpar@255: int child; alpar@255: int degree; alpar@255: bool marked; alpar@255: bool in; alpar@255: PrioType prio; alpar@255: alpar@255: store() : parent(-1), child(-1), degree(), marked(false), in(true) {} alpar@255: }; alpar@255: alpar@255: }; alpar@255: alpar@255: } //namespace hugo alpar@255: #endif