src/include/fib_heap.h
author marci
Mon, 26 Apr 2004 09:54:24 +0000
changeset 409 7ab7f083760a
parent 373 259ea2d741a2
child 430 60e4627e8c74
permissions -rw-r--r--
stGraphWrapper is almost working
alpar@255
     1
// -*- C++ -*-
alpar@255
     2
jacint@373
     3
#ifndef HUGO_FIB_HEAP_H
jacint@373
     4
#define HUGO_FIB_HEAP_H
alpar@255
     5
alpar@255
     6
///\file
alpar@255
     7
///\brief Fibonacci Heap implementation.
alpar@255
     8
alpar@255
     9
#include <vector>
alpar@255
    10
#include <functional>
alpar@255
    11
#include <math.h>
alpar@255
    12
alpar@255
    13
namespace hugo {
alpar@255
    14
  
jacint@373
    15
  /// An implementation of the Fibonacci Heap.
jacint@373
    16
jacint@373
    17
  /**
jacint@387
    18
     This class implements the \e Fibonacci \e heap data structure. A \e heap
jacint@387
    19
     is a data structure for storing items with specified values called \e
jacint@387
    20
     priorities, such that finding the item with minimum priority with respect
jacint@387
    21
     to \e Compare is efficient. In a heap one can change the priority of an
jacint@387
    22
     item, add or erase an item, etc.
jacint@373
    23
jacint@387
    24
     The methods \ref increase and \ref erase are not efficient in a Fibonacci
jacint@387
    25
     heap. In case of many calls to these operations, it is better to use a
jacint@387
    26
     \e binary \e heap.
jacint@373
    27
     
jacint@387
    28
     \param Item The type of the items to be stored.  
jacint@387
    29
     \param Prio The type of the priority of the items.
jacint@387
    30
     \param ItemIntMap A read and writable Item int map, for the usage of
jacint@373
    31
     the heap.
jacint@387
    32
     \param Compare A class for the comparison of the priorities. The
jacint@373
    33
     default is \c std::less<Prio>.
jacint@373
    34
jacint@373
    35
  */
jacint@373
    36
jacint@373
    37
#ifdef DOXYGEN
jacint@373
    38
  template <typename Item, 
jacint@373
    39
	    typename Prio, 
jacint@373
    40
	    typename ItemIntMap, 
jacint@373
    41
	    typename Compare>
jacint@373
    42
#else
jacint@373
    43
  template <typename Item, 
jacint@373
    44
	    typename Prio, 
jacint@373
    45
	    typename ItemIntMap, 
alpar@255
    46
	    typename Compare = std::less<Prio> >
jacint@373
    47
#endif
alpar@255
    48
  class FibHeap {
jacint@387
    49
  public:     
alpar@255
    50
    typedef Prio PrioType;
alpar@255
    51
    
jacint@373
    52
  private:
alpar@255
    53
    class store;
alpar@255
    54
    
alpar@255
    55
    std::vector<store> container;
alpar@255
    56
    int minimum;
alpar@255
    57
    ItemIntMap &iimap;
alpar@255
    58
    Compare comp;
alpar@255
    59
    int num_items;
jacint@373
    60
    
alpar@255
    61
    ///\todo It is use nowhere
alpar@255
    62
    ///\todo It doesn't conform to the naming conventions.
alpar@255
    63
  public:
alpar@255
    64
    enum state_enum {
alpar@255
    65
      IN_HEAP = 0,
alpar@255
    66
      PRE_HEAP = -1,
alpar@255
    67
      POST_HEAP = -2
alpar@255
    68
    };
alpar@255
    69
    
jacint@373
    70
    FibHeap(ItemIntMap &_iimap) : minimum(0), iimap(_iimap), num_items() {} 
jacint@373
    71
    FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(0), 
alpar@255
    72
      iimap(_iimap), comp(_comp), num_items() {}
alpar@255
    73
    
jacint@373
    74
    ///The number of items stored in the heap.
jacint@373
    75
jacint@373
    76
    /**
jacint@387
    77
       Returns the number of items stored in the heap.
jacint@373
    78
    */
jacint@373
    79
    int size() const { return num_items; }
jacint@373
    80
jacint@373
    81
    ///Checks if the heap stores no items.
alpar@255
    82
    
jacint@373
    83
    /**
jacint@387
    84
       Returns \c true iff the heap stores no items.
jacint@373
    85
    */
jacint@373
    86
    bool empty() const { return num_items==0; }
jacint@373
    87
jacint@387
    88
    ///\c item gets to the heap with priority \c value independently if \c item was already there.
jacint@373
    89
jacint@373
    90
    /**
jacint@387
    91
       This method calls \ref push(\c item, \c value) if \c item is not
jacint@387
    92
       stored in the heap and it calls \ref decrease(\c item, \c value) or
jacint@387
    93
       \ref increase(\c item, \c value) otherwise.
jacint@373
    94
    */
jacint@387
    95
    void set (Item const item, PrioType const value); 
jacint@373
    96
    
jacint@373
    97
    ///Adds \c item to the heap with priority \c value. 
jacint@373
    98
    
jacint@373
    99
    /**
jacint@373
   100
       Adds \c item to the heap with priority \c value. 
jacint@373
   101
       \pre \c item must not be stored in the heap. 
jacint@373
   102
    */
jacint@387
   103
    void push (Item const item, PrioType const value);
jacint@373
   104
    
jacint@373
   105
    
jacint@373
   106
    ///Returns the item having the minimum priority w.r.t.  Compare.
jacint@373
   107
    
jacint@373
   108
    /**
jacint@373
   109
       This method returns the item having the minimum priority w.r.t.  Compare. 
jacint@373
   110
       \pre The heap must be nonempty.
jacint@373
   111
    */
jacint@373
   112
    Item top() const { return container[minimum].name; }
jacint@373
   113
    
jacint@373
   114
jacint@373
   115
    ///Returns the minimum priority w.r.t.  Compare.
jacint@373
   116
jacint@373
   117
    /**
jacint@373
   118
       It returns the minimum priority w.r.t.  Compare.
jacint@373
   119
       \pre The heap must be nonempty.
jacint@373
   120
    */
jacint@373
   121
    PrioType prio() const { return container[minimum].prio; }
jacint@373
   122
    
jacint@373
   123
jacint@373
   124
    ///Returns the priority of \c item.
jacint@373
   125
jacint@373
   126
    /**
jacint@373
   127
       It returns the priority of \c item.
jacint@373
   128
       \pre \c item must be in the heap.
jacint@373
   129
    */
jacint@387
   130
    PrioType& operator[](const Item& item) { 
jacint@387
   131
      return container[iimap[item]].prio; 
jacint@387
   132
    }
jacint@373
   133
    
jacint@373
   134
    ///Returns the priority of \c item.
jacint@373
   135
    
jacint@373
   136
    /**
jacint@373
   137
       It returns the priority of \c item.
jacint@373
   138
       \pre \c item must be in the heap.
jacint@373
   139
    */
jacint@387
   140
    const PrioType& operator[](const Item& item) const { 
jacint@387
   141
      return container[iimap[item]].prio; 
alpar@255
   142
    }
alpar@255
   143
alpar@255
   144
jacint@373
   145
    ///Deletes the item with minimum priority w.r.t.  Compare.
alpar@255
   146
jacint@373
   147
    /**
jacint@373
   148
    This method deletes the item with minimum priority w.r.t. 
jacint@373
   149
    Compare from the heap.
jacint@373
   150
    \pre The heap must be non-empty.
jacint@373
   151
    */
jacint@373
   152
    void pop();
jacint@373
   153
jacint@373
   154
    ///Deletes \c item from the heap.
jacint@373
   155
jacint@373
   156
    /**
jacint@373
   157
       This method deletes \c item from the heap, if \c item was already
jacint@373
   158
       stored in the heap. It is quite inefficient in Fibonacci heaps.
jacint@373
   159
    */
jacint@387
   160
    void erase (const Item& item); 
jacint@373
   161
jacint@373
   162
    ///Decreases the priority of \c item to \c value.
jacint@373
   163
jacint@373
   164
    /**
jacint@373
   165
       This method decreases the priority of \c item to \c value.
jacint@373
   166
       \pre \c item must be stored in the heap with priority at least \c
jacint@373
   167
       value w.r.t.  Compare.
jacint@373
   168
    */
jacint@387
   169
    void decrease (Item item, PrioType const value); 
jacint@373
   170
jacint@373
   171
jacint@373
   172
    ///Increases the priority of \c item to \c value.
jacint@373
   173
jacint@373
   174
    /**
jacint@373
   175
       This method sets the priority of \c item to \c value. Though
jacint@373
   176
       there is no precondition on the priority of \c item, this
jacint@387
   177
       method should be used only if there is a need to really \e increase
jacint@373
   178
       (w.r.t.  Compare) the priority of \c item, because this
jacint@373
   179
       method is inefficient.
jacint@373
   180
    */
jacint@387
   181
    void increase (Item item, PrioType const value) {
jacint@387
   182
      erase(item);
jacint@387
   183
      push(item, value);
jacint@373
   184
    }
jacint@373
   185
jacint@373
   186
jacint@387
   187
    ///Tells if \c item is in, was already in, or has never been in the heap.
jacint@373
   188
jacint@373
   189
    /**
jacint@373
   190
       This method returns PRE_HEAP if \c item has never been in the
jacint@373
   191
       heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
jacint@373
   192
       otherwise. In the latter case it is possible that \c item will
jacint@373
   193
       get back to the heap again.
jacint@373
   194
    */
jacint@387
   195
    state_enum state(const Item &item) const {
jacint@387
   196
      int i=iimap[item];
jacint@387
   197
      if( i>=0 ) {
jacint@387
   198
	if ( container[i].in ) i=0;
jacint@387
   199
	else i=-2; 
jacint@387
   200
      }
jacint@387
   201
      return state_enum(i);
jacint@387
   202
    }    
jacint@387
   203
    
jacint@387
   204
  private:
jacint@387
   205
    
jacint@387
   206
    void balance();
jacint@387
   207
    void makeroot(int c);
jacint@387
   208
    void cut(int a, int b);
jacint@387
   209
    void cascade(int a);
jacint@387
   210
    void fuse(int a, int b);
jacint@387
   211
    void unlace(int a);
jacint@373
   212
jacint@373
   213
jacint@387
   214
    class store {
jacint@387
   215
      friend class FibHeap;
jacint@387
   216
      
jacint@387
   217
      Item name;
jacint@387
   218
      int parent;
jacint@387
   219
      int left_neighbor;
jacint@387
   220
      int right_neighbor;
jacint@387
   221
      int child;
jacint@387
   222
      int degree;  
jacint@387
   223
      bool marked;
jacint@387
   224
      bool in;
jacint@387
   225
      PrioType prio;
jacint@387
   226
      
jacint@387
   227
      store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
jacint@387
   228
    };
jacint@387
   229
  };    
jacint@387
   230
 
jacint@387
   231
jacint@373
   232
jacint@373
   233
    // **********************************************************************
jacint@373
   234
    //  IMPLEMENTATIONS
jacint@373
   235
    // **********************************************************************
jacint@373
   236
    
jacint@387
   237
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   238
    typename Compare>
jacint@387
   239
  void FibHeap<Item, Prio, ItemIntMap, Compare>::set 
jacint@387
   240
  (Item const item, PrioType const value) 
jacint@387
   241
  {
jacint@387
   242
    int i=iimap[item];
jacint@387
   243
    if ( i >= 0 && container[i].in ) {
jacint@387
   244
      if ( comp(value, container[i].prio) ) decrease(item, value); 
jacint@387
   245
      if ( comp(container[i].prio, value) ) increase(item, value); 
jacint@387
   246
    } else push(item, value);
jacint@387
   247
  }
alpar@255
   248
    
jacint@387
   249
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   250
    typename Compare>
jacint@387
   251
  void FibHeap<Item, Prio, ItemIntMap, Compare>::push 
jacint@387
   252
  (Item const item, PrioType const value) {
jacint@387
   253
      int i=iimap[item];      
alpar@255
   254
      if ( i < 0 ) {
alpar@255
   255
	int s=container.size();
jacint@387
   256
	iimap.set( item, s );	
alpar@255
   257
	store st;
jacint@387
   258
	st.name=item;
alpar@255
   259
	container.push_back(st);
alpar@255
   260
	i=s;
alpar@255
   261
      } else {
alpar@255
   262
	container[i].parent=container[i].child=-1;
alpar@255
   263
	container[i].degree=0;
alpar@255
   264
	container[i].in=true;
alpar@255
   265
	container[i].marked=false;
alpar@255
   266
      }
alpar@255
   267
alpar@255
   268
      if ( num_items ) {
alpar@255
   269
	container[container[minimum].right_neighbor].left_neighbor=i;
alpar@255
   270
	container[i].right_neighbor=container[minimum].right_neighbor;
alpar@255
   271
	container[minimum].right_neighbor=i;
alpar@255
   272
	container[i].left_neighbor=minimum;
alpar@255
   273
	if ( comp( value, container[minimum].prio) ) minimum=i; 
alpar@255
   274
      } else {
alpar@255
   275
	container[i].right_neighbor=container[i].left_neighbor=i;
alpar@255
   276
	minimum=i;	
alpar@255
   277
      }
alpar@255
   278
      container[i].prio=value;
alpar@255
   279
      ++num_items;
alpar@255
   280
    }
alpar@255
   281
    
jacint@387
   282
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   283
    typename Compare>
jacint@387
   284
  void FibHeap<Item, Prio, ItemIntMap, Compare>::pop() {
alpar@255
   285
      /*The first case is that there are only one root.*/
alpar@255
   286
      if ( container[minimum].left_neighbor==minimum ) {
alpar@255
   287
	container[minimum].in=false;
alpar@255
   288
	if ( container[minimum].degree!=0 ) { 
alpar@255
   289
	  makeroot(container[minimum].child);
alpar@255
   290
	  minimum=container[minimum].child;
alpar@255
   291
	  balance();
alpar@255
   292
	}
alpar@255
   293
      } else {
alpar@255
   294
	int right=container[minimum].right_neighbor;
alpar@255
   295
	unlace(minimum);
alpar@255
   296
	container[minimum].in=false;
alpar@255
   297
	if ( container[minimum].degree > 0 ) {
alpar@255
   298
	  int left=container[minimum].left_neighbor;
alpar@255
   299
	  int child=container[minimum].child;
alpar@255
   300
	  int last_child=container[child].left_neighbor;
alpar@255
   301
	
alpar@255
   302
	  makeroot(child);
alpar@255
   303
	  
alpar@255
   304
	  container[left].right_neighbor=child;
alpar@255
   305
	  container[child].left_neighbor=left;
alpar@255
   306
	  container[right].left_neighbor=last_child;
alpar@255
   307
	  container[last_child].right_neighbor=right;
alpar@255
   308
	}
alpar@255
   309
	minimum=right;
alpar@255
   310
	balance();
alpar@255
   311
      } // the case where there are more roots
alpar@255
   312
      --num_items;   
alpar@255
   313
    }
alpar@255
   314
jacint@387
   315
jacint@387
   316
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   317
    typename Compare>
jacint@387
   318
  void FibHeap<Item, Prio, ItemIntMap, Compare>::erase 
jacint@387
   319
  (const Item& item) {
jacint@387
   320
      int i=iimap[item];
alpar@255
   321
      
alpar@255
   322
      if ( i >= 0 && container[i].in ) { 	
alpar@255
   323
	if ( container[i].parent!=-1 ) {
alpar@255
   324
	  int p=container[i].parent;
alpar@255
   325
	  cut(i,p);	    
alpar@255
   326
	  cascade(p);
alpar@255
   327
	}
alpar@255
   328
	minimum=i;     //As if its prio would be -infinity
alpar@255
   329
	pop();
alpar@255
   330
      }
jacint@387
   331
  }
alpar@255
   332
    
jacint@387
   333
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   334
    typename Compare>
jacint@387
   335
  void FibHeap<Item, Prio, ItemIntMap, Compare>::decrease 
jacint@387
   336
  (Item item, PrioType const value) {
jacint@387
   337
      int i=iimap[item];
alpar@255
   338
      container[i].prio=value;
alpar@255
   339
      int p=container[i].parent;
alpar@255
   340
      
alpar@255
   341
      if ( p!=-1 && comp(value, container[p].prio) ) {
alpar@255
   342
	cut(i,p);	    
alpar@255
   343
	cascade(p);
alpar@255
   344
      }      
alpar@255
   345
      if ( comp(value, container[minimum].prio) ) minimum=i; 
jacint@387
   346
  }
jacint@387
   347
 
alpar@255
   348
jacint@387
   349
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   350
    typename Compare>
jacint@387
   351
  void FibHeap<Item, Prio, ItemIntMap, Compare>::balance() {      
alpar@255
   352
alpar@255
   353
    int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
alpar@255
   354
  
alpar@255
   355
    std::vector<int> A(maxdeg,-1); 
alpar@255
   356
    
alpar@255
   357
    /*
alpar@255
   358
     *Recall that now minimum does not point to the minimum prio element.
alpar@255
   359
     *We set minimum to this during balance().
alpar@255
   360
     */
alpar@255
   361
    int anchor=container[minimum].left_neighbor; 
alpar@255
   362
    int next=minimum; 
alpar@255
   363
    bool end=false; 
alpar@255
   364
    	
alpar@255
   365
       do {
alpar@255
   366
	int active=next;
alpar@255
   367
	if ( anchor==active ) end=true;
alpar@255
   368
	int d=container[active].degree;
alpar@255
   369
	next=container[active].right_neighbor;
alpar@255
   370
alpar@255
   371
	while (A[d]!=-1) {	  
alpar@255
   372
	  if( comp(container[active].prio, container[A[d]].prio) ) {
alpar@255
   373
	    fuse(active,A[d]); 
alpar@255
   374
	  } else { 
alpar@255
   375
	    fuse(A[d],active);
alpar@255
   376
	    active=A[d];
alpar@255
   377
	  } 
alpar@255
   378
	  A[d]=-1;
alpar@255
   379
	  ++d;
alpar@255
   380
	}	
alpar@255
   381
	A[d]=active;
alpar@255
   382
       } while ( !end );
alpar@255
   383
alpar@255
   384
alpar@255
   385
       while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
alpar@255
   386
       int s=minimum;
alpar@255
   387
       int m=minimum;
alpar@255
   388
       do {  
alpar@255
   389
	 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
alpar@255
   390
	 s=container[s].right_neighbor;
alpar@255
   391
       } while ( s != m );
alpar@255
   392
    }
alpar@255
   393
jacint@387
   394
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   395
    typename Compare>
jacint@387
   396
  void FibHeap<Item, Prio, ItemIntMap, Compare>::makeroot 
jacint@387
   397
  (int c) {
alpar@255
   398
      int s=c;
alpar@255
   399
      do {  
alpar@255
   400
	container[s].parent=-1;
alpar@255
   401
	s=container[s].right_neighbor;
alpar@255
   402
      } while ( s != c );
alpar@255
   403
    }
jacint@387
   404
  
jacint@387
   405
  
jacint@387
   406
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   407
    typename Compare>
jacint@387
   408
  void FibHeap<Item, Prio, ItemIntMap, Compare>::cut 
jacint@387
   409
  (int a, int b) {    
jacint@387
   410
    /*
jacint@387
   411
     *Replacing a from the children of b.
jacint@387
   412
     */
jacint@387
   413
    --container[b].degree;
alpar@255
   414
    
jacint@387
   415
    if ( container[b].degree !=0 ) {
jacint@387
   416
      int child=container[b].child;
jacint@387
   417
      if ( child==a ) 
jacint@387
   418
	container[b].child=container[child].right_neighbor;
jacint@387
   419
      unlace(a);
jacint@387
   420
    }
jacint@387
   421
    
jacint@387
   422
    
jacint@387
   423
    /*Lacing a to the roots.*/
jacint@387
   424
    int right=container[minimum].right_neighbor;
jacint@387
   425
    container[minimum].right_neighbor=a;
jacint@387
   426
    container[a].left_neighbor=minimum;
jacint@387
   427
    container[a].right_neighbor=right;
jacint@387
   428
    container[right].left_neighbor=a;
jacint@387
   429
    
jacint@387
   430
    container[a].parent=-1;
jacint@387
   431
    container[a].marked=false;
jacint@387
   432
  }
jacint@387
   433
  
alpar@255
   434
jacint@387
   435
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   436
    typename Compare>
jacint@387
   437
  void FibHeap<Item, Prio, ItemIntMap, Compare>::cascade 
jacint@387
   438
  (int a) 
alpar@255
   439
    {
alpar@255
   440
      if ( container[a].parent!=-1 ) {
alpar@255
   441
	int p=container[a].parent;
alpar@255
   442
	
alpar@255
   443
	if ( container[a].marked==false ) container[a].marked=true;
alpar@255
   444
	else {
alpar@255
   445
	  cut(a,p);
alpar@255
   446
	  cascade(p);
alpar@255
   447
	}
alpar@255
   448
      }
alpar@255
   449
    }
alpar@255
   450
alpar@255
   451
jacint@387
   452
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   453
    typename Compare>
jacint@387
   454
  void FibHeap<Item, Prio, ItemIntMap, Compare>::fuse 
jacint@387
   455
  (int a, int b) {
alpar@255
   456
      unlace(b);
alpar@255
   457
      
alpar@255
   458
      /*Lacing b under a.*/
alpar@255
   459
      container[b].parent=a;
alpar@255
   460
alpar@255
   461
      if (container[a].degree==0) {
alpar@255
   462
	container[b].left_neighbor=b;
alpar@255
   463
	container[b].right_neighbor=b;
alpar@255
   464
	container[a].child=b;	
alpar@255
   465
      } else {
alpar@255
   466
	int child=container[a].child;
alpar@255
   467
	int last_child=container[child].left_neighbor;
alpar@255
   468
	container[child].left_neighbor=b;
alpar@255
   469
	container[b].right_neighbor=child;
alpar@255
   470
	container[last_child].right_neighbor=b;
alpar@255
   471
	container[b].left_neighbor=last_child;
alpar@255
   472
      }
alpar@255
   473
alpar@255
   474
      ++container[a].degree;
alpar@255
   475
      
alpar@255
   476
      container[b].marked=false;
alpar@255
   477
    }
alpar@255
   478
jacint@387
   479
  
jacint@387
   480
  /*
jacint@387
   481
   *It is invoked only if a has siblings.
jacint@387
   482
   */
jacint@387
   483
  template <typename Item, typename Prio, typename ItemIntMap, 
jacint@387
   484
    typename Compare>
jacint@387
   485
  void FibHeap<Item, Prio, ItemIntMap, Compare>::unlace 
jacint@387
   486
  (int a) {      
alpar@255
   487
      int leftn=container[a].left_neighbor;
alpar@255
   488
      int rightn=container[a].right_neighbor;
alpar@255
   489
      container[leftn].right_neighbor=rightn;
alpar@255
   490
      container[rightn].left_neighbor=leftn;
jacint@387
   491
  }
alpar@255
   492
  
alpar@255
   493
} //namespace hugo
alpar@255
   494
#endif