lemon/fib_heap.h
author deba
Tue, 25 Mar 2008 16:28:06 +0000
changeset 2598 71f4bd3a9ae8
parent 2553 bfced05fa852
permissions -rw-r--r--
Minor bug fix
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@255
    18
alpar@921
    19
#ifndef LEMON_FIB_HEAP_H
alpar@921
    20
#define LEMON_FIB_HEAP_H
alpar@255
    21
jacint@857
    22
///\file
klao@491
    23
///\ingroup auxdat
alpar@255
    24
///\brief Fibonacci Heap implementation.
alpar@255
    25
alpar@255
    26
#include <vector>
alpar@255
    27
#include <functional>
alpar@2569
    28
#include <lemon/math.h>
alpar@255
    29
alpar@921
    30
namespace lemon {
alpar@255
    31
  
deba@1834
    32
  /// \ingroup auxdat
deba@2529
    33
  ///
deba@2529
    34
  ///\brief Fibonacci Heap.
deba@2529
    35
  ///
jacint@857
    36
  ///This class implements the \e Fibonacci \e heap data structure. A \e heap
jacint@857
    37
  ///is a data structure for storing items with specified values called \e
jacint@857
    38
  ///priorities in such a way that finding the item with minimum priority is
alpar@911
    39
  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
jacint@857
    40
  ///one can change the priority of an item, add or erase an item, etc.
jacint@857
    41
  ///
jacint@857
    42
  ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
jacint@857
    43
  ///heap. In case of many calls to these operations, it is better to use a
deba@2547
    44
  ///\ref BinHeap "binary heap".
jacint@857
    45
  ///
deba@2547
    46
  ///\param _Prio Type of the priority of the items.
deba@2547
    47
  ///\param _ItemIntMap A read and writable Item int map, used internally
alpar@1204
    48
  ///to handle the cross references.
deba@2547
    49
  ///\param _Compare A class for the ordering of the priorities. The
deba@2547
    50
  ///default is \c std::less<_Prio>.
jacint@857
    51
  ///
alpar@967
    52
  ///\sa BinHeap
alpar@967
    53
  ///\sa Dijkstra
jacint@857
    54
  ///\author Jacint Szabo 
jacint@857
    55
 
jacint@373
    56
#ifdef DOXYGEN
deba@2547
    57
  template <typename _Prio, 
deba@2547
    58
	    typename _ItemIntMap, 
deba@2547
    59
	    typename _Compare>
jacint@373
    60
#else
deba@2547
    61
  template <typename _Prio, 
deba@2547
    62
	    typename _ItemIntMap, 
deba@2547
    63
	    typename _Compare = std::less<_Prio> >
jacint@373
    64
#endif
alpar@255
    65
  class FibHeap {
mqrelly@2263
    66
  public:
deba@2547
    67
    typedef _ItemIntMap ItemIntMap;
deba@2547
    68
    typedef _Prio Prio;
mqrelly@2263
    69
    typedef typename ItemIntMap::Key Item;
deba@2547
    70
    typedef std::pair<Item,Prio> Pair;
deba@2547
    71
    typedef _Compare Compare;
alpar@255
    72
    
jacint@373
    73
  private:
alpar@255
    74
    class store;
alpar@255
    75
    
alpar@255
    76
    std::vector<store> container;
alpar@255
    77
    int minimum;
alpar@255
    78
    ItemIntMap &iimap;
alpar@255
    79
    Compare comp;
alpar@255
    80
    int num_items;
jacint@373
    81
    
alpar@255
    82
  public:
alpar@1127
    83
    ///Status of the nodes
deba@2547
    84
    enum State {
alpar@1127
    85
      ///The node is in the heap
alpar@255
    86
      IN_HEAP = 0,
alpar@1127
    87
      ///The node has never been in the heap
alpar@255
    88
      PRE_HEAP = -1,
alpar@1127
    89
      ///The node was in the heap but it got out of it
alpar@255
    90
      POST_HEAP = -2
alpar@255
    91
    };
alpar@255
    92
    
deba@1717
    93
    /// \brief The constructor
deba@1717
    94
    ///
deba@1717
    95
    /// \c _iimap should be given to the constructor, since it is
deba@1717
    96
    ///   used internally to handle the cross references.
deba@1185
    97
    explicit FibHeap(ItemIntMap &_iimap) 
deba@1185
    98
      : minimum(0), iimap(_iimap), num_items() {} 
jacint@1270
    99
 
deba@1717
   100
    /// \brief The constructor
deba@1717
   101
    ///
deba@1717
   102
    /// \c _iimap should be given to the constructor, since it is used
deba@1717
   103
    /// internally to handle the cross references. \c _comp is an
deba@1717
   104
    /// object for ordering of the priorities. 
deba@2547
   105
    FibHeap(ItemIntMap &_iimap, const Compare &_comp) 
deba@2547
   106
      : minimum(0), iimap(_iimap), comp(_comp), num_items() {}
alpar@255
   107
    
deba@1717
   108
    /// \brief The number of items stored in the heap.
deba@1717
   109
    ///
deba@1717
   110
    /// Returns the number of items stored in the heap.
jacint@373
   111
    int size() const { return num_items; }
jacint@373
   112
deba@1717
   113
    /// \brief Checks if the heap stores no items.
deba@1717
   114
    ///
deba@1717
   115
    ///   Returns \c true if and only if the heap stores no items.
jacint@373
   116
    bool empty() const { return num_items==0; }
jacint@373
   117
deba@1717
   118
    /// \brief Make empty this heap.
deba@1717
   119
    /// 
deba@2050
   120
    /// Make empty this heap. It does not change the cross reference
deba@2050
   121
    /// map.  If you want to reuse a heap what is not surely empty you
deba@2050
   122
    /// should first clear the heap and after that you should set the
deba@2050
   123
    /// cross reference map for each item to \c PRE_HEAP.
deba@1753
   124
    void clear() {
deba@1717
   125
      container.clear(); minimum = 0; num_items = 0;
deba@1717
   126
    }
jacint@373
   127
deba@1717
   128
    /// \brief \c item gets to the heap with priority \c value independently 
deba@1717
   129
    /// if \c item was already there.
deba@1717
   130
    ///
deba@1717
   131
    /// This method calls \ref push(\c item, \c value) if \c item is not
deba@1717
   132
    /// stored in the heap and it calls \ref decrease(\c item, \c value) or
deba@1717
   133
    /// \ref increase(\c item, \c value) otherwise.
deba@2547
   134
    void set (const Item& item, const Prio& value) {
deba@2547
   135
      int i=iimap[item];
deba@2547
   136
      if ( i >= 0 && container[i].in ) {
deba@2547
   137
	if ( comp(value, container[i].prio) ) decrease(item, value); 
deba@2547
   138
	if ( comp(container[i].prio, value) ) increase(item, value); 
deba@2547
   139
      } else push(item, value);
deba@2547
   140
    }
jacint@373
   141
    
deba@1717
   142
    /// \brief Adds \c item to the heap with priority \c value. 
deba@1717
   143
    ///    
deba@1717
   144
    /// Adds \c item to the heap with priority \c value. 
deba@1717
   145
    /// \pre \c item must not be stored in the heap. 
deba@2547
   146
    void push (const Item& item, const Prio& value) {
jacint@387
   147
      int i=iimap[item];      
alpar@255
   148
      if ( i < 0 ) {
alpar@255
   149
	int s=container.size();
jacint@387
   150
	iimap.set( item, s );	
alpar@255
   151
	store st;
jacint@387
   152
	st.name=item;
alpar@255
   153
	container.push_back(st);
alpar@255
   154
	i=s;
alpar@255
   155
      } else {
alpar@255
   156
	container[i].parent=container[i].child=-1;
alpar@255
   157
	container[i].degree=0;
alpar@255
   158
	container[i].in=true;
alpar@255
   159
	container[i].marked=false;
alpar@255
   160
      }
alpar@255
   161
alpar@255
   162
      if ( num_items ) {
alpar@255
   163
	container[container[minimum].right_neighbor].left_neighbor=i;
alpar@255
   164
	container[i].right_neighbor=container[minimum].right_neighbor;
alpar@255
   165
	container[minimum].right_neighbor=i;
alpar@255
   166
	container[i].left_neighbor=minimum;
alpar@255
   167
	if ( comp( value, container[minimum].prio) ) minimum=i; 
alpar@255
   168
      } else {
alpar@255
   169
	container[i].right_neighbor=container[i].left_neighbor=i;
alpar@255
   170
	minimum=i;	
alpar@255
   171
      }
alpar@255
   172
      container[i].prio=value;
alpar@255
   173
      ++num_items;
alpar@255
   174
    }
alpar@255
   175
    
deba@2547
   176
    /// \brief Returns the item with minimum priority relative to \c Compare.
deba@2547
   177
    ///
deba@2547
   178
    /// This method returns the item with minimum priority relative to \c
deba@2547
   179
    /// Compare.  
deba@2547
   180
    /// \pre The heap must be nonempty.  
deba@2547
   181
    Item top() const { return container[minimum].name; }
deba@2547
   182
deba@2547
   183
    /// \brief Returns the minimum priority relative to \c Compare.
deba@2547
   184
    ///
deba@2547
   185
    /// It returns the minimum priority relative to \c Compare.
deba@2547
   186
    /// \pre The heap must be nonempty.
deba@2547
   187
    const Prio& prio() const { return container[minimum].prio; }
deba@2547
   188
        
deba@2547
   189
    /// \brief Returns the priority of \c item.
deba@2547
   190
    ///
deba@2547
   191
    /// It returns the priority of \c item.
deba@2547
   192
    /// \pre \c item must be in the heap.
deba@2547
   193
    const Prio& operator[](const Item& item) const { 
deba@2547
   194
      return container[iimap[item]].prio; 
deba@2547
   195
    }
deba@2547
   196
deba@2547
   197
    /// \brief Deletes the item with minimum priority relative to \c Compare.
deba@2547
   198
    ///
deba@2547
   199
    /// This method deletes the item with minimum priority relative to \c
deba@2547
   200
    /// Compare from the heap.  
deba@2547
   201
    /// \pre The heap must be non-empty.  
deba@2547
   202
    void pop() {
alpar@255
   203
      /*The first case is that there are only one root.*/
alpar@255
   204
      if ( container[minimum].left_neighbor==minimum ) {
alpar@255
   205
	container[minimum].in=false;
alpar@255
   206
	if ( container[minimum].degree!=0 ) { 
alpar@255
   207
	  makeroot(container[minimum].child);
alpar@255
   208
	  minimum=container[minimum].child;
alpar@255
   209
	  balance();
alpar@255
   210
	}
alpar@255
   211
      } else {
alpar@255
   212
	int right=container[minimum].right_neighbor;
alpar@255
   213
	unlace(minimum);
alpar@255
   214
	container[minimum].in=false;
alpar@255
   215
	if ( container[minimum].degree > 0 ) {
alpar@255
   216
	  int left=container[minimum].left_neighbor;
alpar@255
   217
	  int child=container[minimum].child;
alpar@255
   218
	  int last_child=container[child].left_neighbor;
deba@2547
   219
	  
alpar@255
   220
	  makeroot(child);
alpar@255
   221
	  
alpar@255
   222
	  container[left].right_neighbor=child;
alpar@255
   223
	  container[child].left_neighbor=left;
alpar@255
   224
	  container[right].left_neighbor=last_child;
alpar@255
   225
	  container[last_child].right_neighbor=right;
alpar@255
   226
	}
alpar@255
   227
	minimum=right;
alpar@255
   228
	balance();
alpar@255
   229
      } // the case where there are more roots
alpar@255
   230
      --num_items;   
alpar@255
   231
    }
alpar@255
   232
deba@2547
   233
    /// \brief Deletes \c item from the heap.
deba@2547
   234
    ///
deba@2547
   235
    /// This method deletes \c item from the heap, if \c item was already
deba@2547
   236
    /// stored in the heap. It is quite inefficient in Fibonacci heaps.
deba@2547
   237
    void erase (const Item& item) {
jacint@387
   238
      int i=iimap[item];
alpar@255
   239
      
alpar@255
   240
      if ( i >= 0 && container[i].in ) { 	
alpar@255
   241
	if ( container[i].parent!=-1 ) {
alpar@255
   242
	  int p=container[i].parent;
alpar@255
   243
	  cut(i,p);	    
alpar@255
   244
	  cascade(p);
alpar@255
   245
	}
alpar@255
   246
	minimum=i;     //As if its prio would be -infinity
alpar@255
   247
	pop();
alpar@255
   248
      }
deba@2547
   249
    }
deba@2547
   250
deba@2547
   251
    /// \brief Decreases the priority of \c item to \c value.
deba@2547
   252
    ///
deba@2547
   253
    /// This method decreases the priority of \c item to \c value.
deba@2547
   254
    /// \pre \c item must be stored in the heap with priority at least \c
deba@2547
   255
    ///   value relative to \c Compare.
deba@2547
   256
    void decrease (Item item, const Prio& value) {
jacint@387
   257
      int i=iimap[item];
alpar@255
   258
      container[i].prio=value;
alpar@255
   259
      int p=container[i].parent;
alpar@255
   260
      
alpar@255
   261
      if ( p!=-1 && comp(value, container[p].prio) ) {
alpar@255
   262
	cut(i,p);	    
alpar@255
   263
	cascade(p);
alpar@255
   264
      }      
alpar@255
   265
      if ( comp(value, container[minimum].prio) ) minimum=i; 
deba@2547
   266
    }
alpar@255
   267
deba@2547
   268
    /// \brief Increases the priority of \c item to \c value.
deba@2547
   269
    ///
deba@2547
   270
    /// This method sets the priority of \c item to \c value. Though
deba@2547
   271
    /// there is no precondition on the priority of \c item, this
deba@2547
   272
    /// method should be used only if it is indeed necessary to increase
deba@2547
   273
    /// (relative to \c Compare) the priority of \c item, because this
deba@2547
   274
    /// method is inefficient.
deba@2547
   275
    void increase (Item item, const Prio& value) {
deba@2547
   276
      erase(item);
deba@2547
   277
      push(item, value);
deba@2547
   278
    }
alpar@255
   279
deba@2547
   280
deba@2547
   281
    /// \brief Returns if \c item is in, has already been in, or has never 
deba@2547
   282
    /// been in the heap.
deba@2547
   283
    ///
deba@2547
   284
    /// This method returns PRE_HEAP if \c item has never been in the
deba@2547
   285
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@2547
   286
    /// otherwise. In the latter case it is possible that \c item will
deba@2547
   287
    /// get back to the heap again.
deba@2547
   288
    State state(const Item &item) const {
deba@2547
   289
      int i=iimap[item];
deba@2547
   290
      if( i>=0 ) {
deba@2547
   291
	if ( container[i].in ) i=0;
deba@2547
   292
	else i=-2; 
deba@2547
   293
      }
deba@2547
   294
      return State(i);
deba@2547
   295
    }    
deba@2547
   296
deba@2547
   297
    /// \brief Sets the state of the \c item in the heap.
deba@2547
   298
    ///
deba@2547
   299
    /// Sets the state of the \c item in the heap. It can be used to
deba@2547
   300
    /// manually clear the heap when it is important to achive the
deba@2547
   301
    /// better time complexity.
deba@2547
   302
    /// \param i The item.
deba@2547
   303
    /// \param st The state. It should not be \c IN_HEAP. 
deba@2547
   304
    void state(const Item& i, State st) {
deba@2547
   305
      switch (st) {
deba@2547
   306
      case POST_HEAP:
deba@2547
   307
      case PRE_HEAP:
deba@2547
   308
        if (state(i) == IN_HEAP) {
deba@2547
   309
          erase(i);
deba@2547
   310
        }
deba@2547
   311
        iimap[i] = st;
deba@2547
   312
        break;
deba@2547
   313
      case IN_HEAP:
deba@2547
   314
        break;
deba@2547
   315
      }
deba@2547
   316
    }
deba@2547
   317
    
deba@2547
   318
  private:
deba@2547
   319
    
deba@2547
   320
    void balance() {
deba@2547
   321
deba@2547
   322
      int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1;
alpar@255
   323
  
deba@2547
   324
      std::vector<int> A(maxdeg,-1); 
alpar@255
   325
    
deba@2547
   326
      /*
deba@2547
   327
       *Recall that now minimum does not point to the minimum prio element.
deba@2547
   328
       *We set minimum to this during balance().
deba@2547
   329
       */
deba@2547
   330
      int anchor=container[minimum].left_neighbor; 
deba@2547
   331
      int next=minimum; 
deba@2547
   332
      bool end=false; 
alpar@255
   333
    	
deba@2547
   334
      do {
alpar@255
   335
	int active=next;
alpar@255
   336
	if ( anchor==active ) end=true;
alpar@255
   337
	int d=container[active].degree;
alpar@255
   338
	next=container[active].right_neighbor;
alpar@255
   339
alpar@255
   340
	while (A[d]!=-1) {	  
alpar@255
   341
	  if( comp(container[active].prio, container[A[d]].prio) ) {
alpar@255
   342
	    fuse(active,A[d]); 
alpar@255
   343
	  } else { 
alpar@255
   344
	    fuse(A[d],active);
alpar@255
   345
	    active=A[d];
alpar@255
   346
	  } 
alpar@255
   347
	  A[d]=-1;
alpar@255
   348
	  ++d;
alpar@255
   349
	}	
alpar@255
   350
	A[d]=active;
deba@2547
   351
      } while ( !end );
alpar@255
   352
alpar@255
   353
deba@2547
   354
      while ( container[minimum].parent >=0 ) 
deba@2547
   355
	minimum=container[minimum].parent;
deba@2547
   356
      int s=minimum;
deba@2547
   357
      int m=minimum;
deba@2547
   358
      do {  
deba@2547
   359
	if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
deba@2547
   360
	s=container[s].right_neighbor;
deba@2547
   361
      } while ( s != m );
alpar@255
   362
    }
alpar@255
   363
deba@2547
   364
    void makeroot(int c) {
alpar@255
   365
      int s=c;
alpar@255
   366
      do {  
alpar@255
   367
	container[s].parent=-1;
alpar@255
   368
	s=container[s].right_neighbor;
alpar@255
   369
      } while ( s != c );
alpar@255
   370
    }
deba@2547
   371
deba@2547
   372
    void cut(int a, int b) {
deba@2547
   373
      /*
deba@2547
   374
       *Replacing a from the children of b.
deba@2547
   375
       */
deba@2547
   376
      --container[b].degree;
alpar@255
   377
    
deba@2547
   378
      if ( container[b].degree !=0 ) {
deba@2547
   379
	int child=container[b].child;
deba@2547
   380
	if ( child==a ) 
deba@2547
   381
	  container[b].child=container[child].right_neighbor;
deba@2547
   382
	unlace(a);
deba@2547
   383
      }
deba@2547
   384
    
deba@2547
   385
    
deba@2547
   386
      /*Lacing a to the roots.*/
deba@2547
   387
      int right=container[minimum].right_neighbor;
deba@2547
   388
      container[minimum].right_neighbor=a;
deba@2547
   389
      container[a].left_neighbor=minimum;
deba@2547
   390
      container[a].right_neighbor=right;
deba@2547
   391
      container[right].left_neighbor=a;
deba@2547
   392
    
deba@2547
   393
      container[a].parent=-1;
deba@2547
   394
      container[a].marked=false;
jacint@387
   395
    }
alpar@255
   396
deba@2547
   397
    void cascade(int a) {
alpar@255
   398
      if ( container[a].parent!=-1 ) {
alpar@255
   399
	int p=container[a].parent;
alpar@255
   400
	
alpar@255
   401
	if ( container[a].marked==false ) container[a].marked=true;
alpar@255
   402
	else {
alpar@255
   403
	  cut(a,p);
alpar@255
   404
	  cascade(p);
alpar@255
   405
	}
alpar@255
   406
      }
alpar@255
   407
    }
alpar@255
   408
deba@2547
   409
    void fuse(int a, int b) {
alpar@255
   410
      unlace(b);
alpar@255
   411
      
alpar@255
   412
      /*Lacing b under a.*/
alpar@255
   413
      container[b].parent=a;
alpar@255
   414
alpar@255
   415
      if (container[a].degree==0) {
alpar@255
   416
	container[b].left_neighbor=b;
alpar@255
   417
	container[b].right_neighbor=b;
alpar@255
   418
	container[a].child=b;	
alpar@255
   419
      } else {
alpar@255
   420
	int child=container[a].child;
alpar@255
   421
	int last_child=container[child].left_neighbor;
alpar@255
   422
	container[child].left_neighbor=b;
alpar@255
   423
	container[b].right_neighbor=child;
alpar@255
   424
	container[last_child].right_neighbor=b;
alpar@255
   425
	container[b].left_neighbor=last_child;
alpar@255
   426
      }
alpar@255
   427
alpar@255
   428
      ++container[a].degree;
alpar@255
   429
      
alpar@255
   430
      container[b].marked=false;
alpar@255
   431
    }
alpar@255
   432
deba@2547
   433
    /*
deba@2547
   434
     *It is invoked only if a has siblings.
deba@2547
   435
     */
deba@2547
   436
    void unlace(int a) {
alpar@255
   437
      int leftn=container[a].left_neighbor;
alpar@255
   438
      int rightn=container[a].right_neighbor;
alpar@255
   439
      container[leftn].right_neighbor=rightn;
alpar@255
   440
      container[rightn].left_neighbor=leftn;
deba@2547
   441
    }
deba@2547
   442
deba@2547
   443
deba@2547
   444
    class store {
deba@2547
   445
      friend class FibHeap;
deba@2547
   446
      
deba@2547
   447
      Item name;
deba@2547
   448
      int parent;
deba@2547
   449
      int left_neighbor;
deba@2547
   450
      int right_neighbor;
deba@2547
   451
      int child;
deba@2547
   452
      int degree;  
deba@2547
   453
      bool marked;
deba@2547
   454
      bool in;
deba@2547
   455
      Prio prio;
deba@2547
   456
      
deba@2547
   457
      store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
deba@2547
   458
    };
deba@2547
   459
  };    
alpar@430
   460
alpar@921
   461
} //namespace lemon
alpar@477
   462
alpar@921
   463
#endif //LEMON_FIB_HEAP_H
alpar@477
   464