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