src/work/jacint/fib_heap.h
author marci
Fri, 12 Mar 2004 16:37:08 +0000
changeset 179 91646df36ffc
parent 167 7949a29a334e
child 211 9222a9b8b323
permissions -rw-r--r--
const
     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 #include <vector>
    55 #include <functional>
    56 #include <math.h>
    57 
    58 namespace hugo {
    59   
    60   template <typename Item, typename Prio, typename ItemIntMap, 
    61     typename Compare = std::less<Prio> >
    62  
    63   class FibHeap {
    64   
    65     typedef Prio PrioType;
    66     
    67     class store;
    68     
    69     std::vector<store> container;
    70     int minimum;
    71     ItemIntMap &iimap;
    72     Compare comp;
    73     int num_items;
    74 
    75     enum state_enum {
    76       IN_HEAP = 0,
    77       PRE_HEAP = -1,
    78       POST_HEAP = -2
    79     };
    80     
    81   public :
    82     
    83     FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {} 
    84     FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(), 
    85       iimap(_iimap), comp(_comp), num_items() {}
    86     
    87     
    88     int size() const {
    89       return num_items; 
    90     }
    91 
    92 
    93     bool empty() const { return num_items==0; }
    94 
    95 
    96     void set (Item const it, PrioType const value) {
    97       int i=iimap.get(it);
    98       if ( i >= 0 && container[i].in ) {
    99 	if ( comp(value, container[i].prio) ) decrease(it, value); 
   100 	if ( comp(container[i].prio, value) ) increase(it, value); 
   101       } else push(it, value);
   102     }
   103     
   104 
   105     void push (Item const it, PrioType const value) {
   106       int i=iimap.get(it);      
   107       if ( i < 0 ) {
   108 	int s=container.size();
   109 	iimap.set( it, s );	
   110 	store st;
   111 	st.name=it;
   112 	container.push_back(st);
   113 	i=s;
   114       } else {
   115 	container[i].parent=container[i].child=-1;
   116 	container[i].degree=0;
   117 	container[i].in=true;
   118 	container[i].marked=false;
   119       }
   120 
   121       if ( num_items ) {
   122 	container[container[minimum].right_neighbor].left_neighbor=i;
   123 	container[i].right_neighbor=container[minimum].right_neighbor;
   124 	container[minimum].right_neighbor=i;
   125 	container[i].left_neighbor=minimum;
   126 	if ( comp( value, container[minimum].prio) ) minimum=i; 
   127       } else {
   128 	container[i].right_neighbor=container[i].left_neighbor=i;
   129 	minimum=i;	
   130       }
   131       container[i].prio=value;
   132       ++num_items;
   133     }
   134     
   135 
   136     Item top() const {
   137       return container[minimum].name;
   138     }
   139     
   140     
   141     PrioType prio() const {
   142       return container[minimum].prio;
   143     }
   144     
   145 
   146     const PrioType get(const Item& it) const {
   147       return container[iimap.get(it)].prio;
   148     }
   149 
   150 
   151     void pop() {
   152       /*The first case is that there are only one root.*/
   153       if ( container[minimum].left_neighbor==minimum ) {
   154 	container[minimum].in=false;
   155 	if ( container[minimum].degree!=0 ) { 
   156 	  makeroot(container[minimum].child);
   157 	  minimum=container[minimum].child;
   158 	  balance();
   159 	}
   160       } else {
   161 	int right=container[minimum].right_neighbor;
   162 	unlace(minimum);
   163 	container[minimum].in=false;
   164 	if ( container[minimum].degree > 0 ) {
   165 	  int left=container[minimum].left_neighbor;
   166 	  int child=container[minimum].child;
   167 	  int last_child=container[child].left_neighbor;
   168 	
   169 	  makeroot(child);
   170 	  
   171 	  container[left].right_neighbor=child;
   172 	  container[child].left_neighbor=left;
   173 	  container[right].left_neighbor=last_child;
   174 	  container[last_child].right_neighbor=right;
   175 	}
   176 	minimum=right;
   177 	balance();
   178       } // the case where there are more roots
   179       --num_items;   
   180     }
   181 
   182     
   183     void erase (const Item& it) {
   184       int i=iimap.get(it);
   185       
   186       if ( i >= 0 && container[i].in ) { 	
   187 	if ( container[i].parent!=-1 ) {
   188 	  int p=container[i].parent;
   189 	  cut(i,p);	    
   190 	  cascade(p);
   191 	}
   192 	minimum=i;     //As if its prio would be -infinity
   193 	pop();
   194       }
   195     }
   196     
   197 
   198     void decrease (Item it, PrioType const value) {
   199       int i=iimap.get(it);
   200       container[i].prio=value;
   201       int p=container[i].parent;
   202       
   203       if ( p!=-1 && comp(value, container[p].prio) ) {
   204 	cut(i,p);	    
   205 	cascade(p);
   206       }      
   207       if ( comp(value, container[minimum].prio) ) minimum=i; 
   208     }
   209    
   210 
   211     void increase (Item it, PrioType const value) {
   212       erase(it);
   213       push(it, value);
   214     }
   215 
   216 
   217     state_enum state(const Item &it) const {
   218       int i=iimap.get(it);
   219       if( i>=0 ) {
   220 	if ( container[i].in ) i=0;
   221 	else i=-2; 
   222       }
   223       return state_enum(i);
   224     }
   225 
   226 
   227   private:
   228     
   229     void balance() {      
   230 
   231     int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
   232   
   233     std::vector<int> A(maxdeg,-1); 
   234     
   235     /*
   236      *Recall that now minimum does not point to the minimum prio element.
   237      *We set minimum to this during balance().
   238      */
   239     int anchor=container[minimum].left_neighbor; 
   240     int next=minimum; 
   241     bool end=false; 
   242     	
   243        do {
   244 	int active=next;
   245 	if ( anchor==active ) end=true;
   246 	int d=container[active].degree;
   247 	next=container[active].right_neighbor;
   248 
   249 	while (A[d]!=-1) {	  
   250 	  if( comp(container[active].prio, container[A[d]].prio) ) {
   251 	    fuse(active,A[d]); 
   252 	  } else { 
   253 	    fuse(A[d],active);
   254 	    active=A[d];
   255 	  } 
   256 	  A[d]=-1;
   257 	  ++d;
   258 	}	
   259 	A[d]=active;
   260        } while ( !end );
   261 
   262 
   263        while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
   264        int s=minimum;
   265        int m=minimum;
   266        do {  
   267 	 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
   268 	 s=container[s].right_neighbor;
   269        } while ( s != m );
   270     }
   271 
   272 
   273     void makeroot (int c) {
   274       int s=c;
   275       do {  
   276 	container[s].parent=-1;
   277 	s=container[s].right_neighbor;
   278       } while ( s != c );
   279     }
   280     
   281 
   282     void cut (int a, int b) {    
   283       /*
   284        *Replacing a from the children of b.
   285        */
   286       --container[b].degree;
   287       
   288       if ( container[b].degree !=0 ) {
   289 	int child=container[b].child;
   290 	if ( child==a ) 
   291 	  container[b].child=container[child].right_neighbor;
   292 	unlace(a);
   293       }
   294       
   295       
   296       /*Lacing a to the roots.*/
   297       int right=container[minimum].right_neighbor;
   298       container[minimum].right_neighbor=a;
   299       container[a].left_neighbor=minimum;
   300       container[a].right_neighbor=right;
   301       container[right].left_neighbor=a;
   302 
   303       container[a].parent=-1;
   304       container[a].marked=false;
   305     }
   306 
   307 
   308     void cascade (int a) 
   309     {
   310       if ( container[a].parent!=-1 ) {
   311 	int p=container[a].parent;
   312 	
   313 	if ( container[a].marked==false ) container[a].marked=true;
   314 	else {
   315 	  cut(a,p);
   316 	  cascade(p);
   317 	}
   318       }
   319     }
   320 
   321 
   322     void fuse (int a, int b) {
   323       unlace(b);
   324       
   325       /*Lacing b under a.*/
   326       container[b].parent=a;
   327 
   328       if (container[a].degree==0) {
   329 	container[b].left_neighbor=b;
   330 	container[b].right_neighbor=b;
   331 	container[a].child=b;	
   332       } else {
   333 	int child=container[a].child;
   334 	int last_child=container[child].left_neighbor;
   335 	container[child].left_neighbor=b;
   336 	container[b].right_neighbor=child;
   337 	container[last_child].right_neighbor=b;
   338 	container[b].left_neighbor=last_child;
   339       }
   340 
   341       ++container[a].degree;
   342       
   343       container[b].marked=false;
   344     }
   345 
   346 
   347     /*
   348      *It is invoked only if a has siblings.
   349      */
   350     void unlace (int a) {      
   351       int leftn=container[a].left_neighbor;
   352       int rightn=container[a].right_neighbor;
   353       container[leftn].right_neighbor=rightn;
   354       container[rightn].left_neighbor=leftn;
   355     }
   356 
   357 
   358     class store {
   359       friend class FibHeap;
   360       
   361       Item name;
   362       int parent;
   363       int left_neighbor;
   364       int right_neighbor;
   365       int child;
   366       int degree;  
   367       bool marked;
   368       bool in;
   369       PrioType prio;
   370 
   371       store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
   372     };
   373     
   374   };
   375   
   376 } //namespace hugo
   377 #endif