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