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