src/include/fib_heap.h
author marci
Tue, 13 Apr 2004 20:35:47 +0000
changeset 317 6e6db1c49bc1
child 373 259ea2d741a2
permissions -rw-r--r--
gw
     1 // -*- C++ -*-
     2 /*
     3  *template <typename Item, 
     4  *          typename Prio, 
     5  *          typename ItemIntMap, 
     6  *          typename Compare = std::less<Prio> >
     7  * 
     8  *constructors:
     9  *
    10  *FibHeap(ItemIntMap),   FibHeap(ItemIntMap, Compare)
    11  *
    12  *Member functions:
    13  *
    14  *int size() : returns the number of elements in the heap
    15  *
    16  *bool empty() : true iff size()=0
    17  *
    18  *void set(Item, Prio) : calls push(Item, Prio) if Item is not
    19  *     in the heap, and calls decrease/increase(Item, Prio) otherwise
    20  *
    21  *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item
    22  *     mustn't be in the heap.
    23  *
    24  *Item top() : returns the Item with least Prio. 
    25  *     Must be called only if heap is nonempty.
    26  *
    27  *Prio prio() : returns the least Prio
    28  *     Must be called only if heap is nonempty.
    29  *
    30  *Prio get(Item) : returns Prio of Item
    31  *     Must be called only if Item is in heap.
    32  *
    33  *void pop() : deletes the Item with least Prio
    34  *
    35  *void erase(Item) : deletes Item from the heap if it was already there
    36  *
    37  *void decrease(Item, P) : decreases prio of Item to P. 
    38  *     Item must be in the heap with prio at least P.
    39  *
    40  *void increase(Item, P) : sets prio of Item to P. 
    41  *
    42  *state_enum state(Item) : returns PRE_HEAP if Item has not been in the 
    43  *     heap until now, IN_HEAP if it is in the heap at the moment, and 
    44  *     POST_HEAP otherwise. In the latter case it is possible that Item
    45  *     will get back to the heap again. 
    46  *
    47  *In Fibonacci heaps, increase and erase are not efficient, in case of
    48  *many calls to these operations, it is better to use bin_heap.
    49  */
    50 
    51 #ifndef FIB_HEAP_H
    52 #define FIB_HEAP_H
    53 
    54 ///\file
    55 ///\brief Fibonacci Heap implementation.
    56 
    57 #include <vector>
    58 #include <functional>
    59 #include <math.h>
    60 
    61 namespace hugo {
    62   
    63   /// A Fibonacci Heap implementation.
    64   template <typename Item, typename Prio, typename ItemIntMap, 
    65 	    typename Compare = std::less<Prio> >
    66   class FibHeap {
    67     
    68     typedef Prio PrioType;
    69     
    70     class store;
    71     
    72     std::vector<store> container;
    73     int minimum;
    74     ItemIntMap &iimap;
    75     Compare comp;
    76     int num_items;
    77 
    78     ///\todo It is use nowhere
    79     ///\todo It doesn't conform to the naming conventions.
    80   public:
    81     enum state_enum {
    82       IN_HEAP = 0,
    83       PRE_HEAP = -1,
    84       POST_HEAP = -2
    85     };
    86     
    87   public :
    88     
    89     FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {} 
    90     FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(), 
    91       iimap(_iimap), comp(_comp), num_items() {}
    92     
    93     
    94     int size() const {
    95       return num_items; 
    96     }
    97 
    98 
    99     bool empty() const { return num_items==0; }
   100 
   101 
   102     void set (Item const it, PrioType const value) {
   103       int i=iimap[it];
   104       if ( i >= 0 && container[i].in ) {
   105 	if ( comp(value, container[i].prio) ) decrease(it, value); 
   106 	if ( comp(container[i].prio, value) ) increase(it, value); 
   107       } else push(it, value);
   108     }
   109     
   110 
   111     void push (Item const it, PrioType const value) {
   112       int i=iimap[it];      
   113       if ( i < 0 ) {
   114 	int s=container.size();
   115 	iimap.set( it, s );	
   116 	store st;
   117 	st.name=it;
   118 	container.push_back(st);
   119 	i=s;
   120       } else {
   121 	container[i].parent=container[i].child=-1;
   122 	container[i].degree=0;
   123 	container[i].in=true;
   124 	container[i].marked=false;
   125       }
   126 
   127       if ( num_items ) {
   128 	container[container[minimum].right_neighbor].left_neighbor=i;
   129 	container[i].right_neighbor=container[minimum].right_neighbor;
   130 	container[minimum].right_neighbor=i;
   131 	container[i].left_neighbor=minimum;
   132 	if ( comp( value, container[minimum].prio) ) minimum=i; 
   133       } else {
   134 	container[i].right_neighbor=container[i].left_neighbor=i;
   135 	minimum=i;	
   136       }
   137       container[i].prio=value;
   138       ++num_items;
   139     }
   140     
   141 
   142     Item top() const {
   143       return container[minimum].name;
   144     }
   145     
   146     
   147     PrioType prio() const {
   148       return container[minimum].prio;
   149     }
   150     
   151 
   152 
   153 
   154     PrioType& operator[](const Item& it) {
   155       return container[iimap[it]].prio;
   156     }
   157     
   158     const PrioType& operator[](const Item& it) const {
   159       return container[iimap[it]].prio;
   160     }
   161 
   162 //     const PrioType get(const Item& it) const {
   163 //       return container[iimap[it]].prio;
   164 //     }
   165 
   166     void pop() {
   167       /*The first case is that there are only one root.*/
   168       if ( container[minimum].left_neighbor==minimum ) {
   169 	container[minimum].in=false;
   170 	if ( container[minimum].degree!=0 ) { 
   171 	  makeroot(container[minimum].child);
   172 	  minimum=container[minimum].child;
   173 	  balance();
   174 	}
   175       } else {
   176 	int right=container[minimum].right_neighbor;
   177 	unlace(minimum);
   178 	container[minimum].in=false;
   179 	if ( container[minimum].degree > 0 ) {
   180 	  int left=container[minimum].left_neighbor;
   181 	  int child=container[minimum].child;
   182 	  int last_child=container[child].left_neighbor;
   183 	
   184 	  makeroot(child);
   185 	  
   186 	  container[left].right_neighbor=child;
   187 	  container[child].left_neighbor=left;
   188 	  container[right].left_neighbor=last_child;
   189 	  container[last_child].right_neighbor=right;
   190 	}
   191 	minimum=right;
   192 	balance();
   193       } // the case where there are more roots
   194       --num_items;   
   195     }
   196 
   197     
   198     void erase (const Item& it) {
   199       int i=iimap[it];
   200       
   201       if ( i >= 0 && container[i].in ) { 	
   202 	if ( container[i].parent!=-1 ) {
   203 	  int p=container[i].parent;
   204 	  cut(i,p);	    
   205 	  cascade(p);
   206 	}
   207 	minimum=i;     //As if its prio would be -infinity
   208 	pop();
   209       }
   210     }
   211     
   212 
   213     void decrease (Item it, PrioType const value) {
   214       int i=iimap[it];
   215       container[i].prio=value;
   216       int p=container[i].parent;
   217       
   218       if ( p!=-1 && comp(value, container[p].prio) ) {
   219 	cut(i,p);	    
   220 	cascade(p);
   221       }      
   222       if ( comp(value, container[minimum].prio) ) minimum=i; 
   223     }
   224    
   225 
   226     void increase (Item it, PrioType const value) {
   227       erase(it);
   228       push(it, value);
   229     }
   230 
   231 
   232     state_enum state(const Item &it) const {
   233       int i=iimap[it];
   234       if( i>=0 ) {
   235 	if ( container[i].in ) i=0;
   236 	else i=-2; 
   237       }
   238       return state_enum(i);
   239     }
   240 
   241 
   242   private:
   243     
   244     void balance() {      
   245 
   246     int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
   247   
   248     std::vector<int> A(maxdeg,-1); 
   249     
   250     /*
   251      *Recall that now minimum does not point to the minimum prio element.
   252      *We set minimum to this during balance().
   253      */
   254     int anchor=container[minimum].left_neighbor; 
   255     int next=minimum; 
   256     bool end=false; 
   257     	
   258        do {
   259 	int active=next;
   260 	if ( anchor==active ) end=true;
   261 	int d=container[active].degree;
   262 	next=container[active].right_neighbor;
   263 
   264 	while (A[d]!=-1) {	  
   265 	  if( comp(container[active].prio, container[A[d]].prio) ) {
   266 	    fuse(active,A[d]); 
   267 	  } else { 
   268 	    fuse(A[d],active);
   269 	    active=A[d];
   270 	  } 
   271 	  A[d]=-1;
   272 	  ++d;
   273 	}	
   274 	A[d]=active;
   275        } while ( !end );
   276 
   277 
   278        while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
   279        int s=minimum;
   280        int m=minimum;
   281        do {  
   282 	 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
   283 	 s=container[s].right_neighbor;
   284        } while ( s != m );
   285     }
   286 
   287 
   288     void makeroot (int c) {
   289       int s=c;
   290       do {  
   291 	container[s].parent=-1;
   292 	s=container[s].right_neighbor;
   293       } while ( s != c );
   294     }
   295     
   296 
   297     void cut (int a, int b) {    
   298       /*
   299        *Replacing a from the children of b.
   300        */
   301       --container[b].degree;
   302       
   303       if ( container[b].degree !=0 ) {
   304 	int child=container[b].child;
   305 	if ( child==a ) 
   306 	  container[b].child=container[child].right_neighbor;
   307 	unlace(a);
   308       }
   309       
   310       
   311       /*Lacing a to the roots.*/
   312       int right=container[minimum].right_neighbor;
   313       container[minimum].right_neighbor=a;
   314       container[a].left_neighbor=minimum;
   315       container[a].right_neighbor=right;
   316       container[right].left_neighbor=a;
   317 
   318       container[a].parent=-1;
   319       container[a].marked=false;
   320     }
   321 
   322 
   323     void cascade (int a) 
   324     {
   325       if ( container[a].parent!=-1 ) {
   326 	int p=container[a].parent;
   327 	
   328 	if ( container[a].marked==false ) container[a].marked=true;
   329 	else {
   330 	  cut(a,p);
   331 	  cascade(p);
   332 	}
   333       }
   334     }
   335 
   336 
   337     void fuse (int a, int b) {
   338       unlace(b);
   339       
   340       /*Lacing b under a.*/
   341       container[b].parent=a;
   342 
   343       if (container[a].degree==0) {
   344 	container[b].left_neighbor=b;
   345 	container[b].right_neighbor=b;
   346 	container[a].child=b;	
   347       } else {
   348 	int child=container[a].child;
   349 	int last_child=container[child].left_neighbor;
   350 	container[child].left_neighbor=b;
   351 	container[b].right_neighbor=child;
   352 	container[last_child].right_neighbor=b;
   353 	container[b].left_neighbor=last_child;
   354       }
   355 
   356       ++container[a].degree;
   357       
   358       container[b].marked=false;
   359     }
   360 
   361 
   362     /*
   363      *It is invoked only if a has siblings.
   364      */
   365     void unlace (int a) {      
   366       int leftn=container[a].left_neighbor;
   367       int rightn=container[a].right_neighbor;
   368       container[leftn].right_neighbor=rightn;
   369       container[rightn].left_neighbor=leftn;
   370     }
   371 
   372 
   373     class store {
   374       friend class FibHeap;
   375       
   376       Item name;
   377       int parent;
   378       int left_neighbor;
   379       int right_neighbor;
   380       int child;
   381       int degree;  
   382       bool marked;
   383       bool in;
   384       PrioType prio;
   385 
   386       store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
   387     };
   388     
   389   };
   390   
   391 } //namespace hugo
   392 #endif