lemon/fib_heap.h
author alpar
Tue, 31 Oct 2006 08:39:01 +0000
changeset 2279 2c3fee01d3ed
parent 2050 d9a221218ea4
child 2391 14a343be7a5a
permissions -rw-r--r--
Update scripts
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@1956
     5
 * Copyright (C) 2003-2006
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>
deba@1332
    28
#include <cmath>
alpar@255
    29
alpar@921
    30
namespace lemon {
alpar@255
    31
  
deba@1834
    32
  /// \ingroup auxdat
alpar@430
    33
jacint@857
    34
  /// Fibonacci Heap.
jacint@373
    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
jacint@857
    44
  ///\e binary \e heap.
jacint@857
    45
  ///
jacint@857
    46
  ///\param Prio Type of the priority of the items.
alpar@1204
    47
  ///\param ItemIntMap A read and writable Item int map, used internally
alpar@1204
    48
  ///to handle the cross references.
jacint@857
    49
  ///\param Compare A class for the ordering of the priorities. The
jacint@857
    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
mqrelly@2263
    57
  template <typename Prio, 
jacint@373
    58
	    typename ItemIntMap, 
jacint@373
    59
	    typename Compare>
jacint@373
    60
#else
mqrelly@2263
    61
  template <typename Prio, 
jacint@373
    62
	    typename ItemIntMap, 
alpar@255
    63
	    typename Compare = std::less<Prio> >
jacint@373
    64
#endif
alpar@255
    65
  class FibHeap {
mqrelly@2263
    66
  public:
mqrelly@2263
    67
    typedef typename ItemIntMap::Key Item;
alpar@255
    68
    typedef Prio PrioType;
alpar@255
    69
    
jacint@373
    70
  private:
alpar@255
    71
    class store;
alpar@255
    72
    
alpar@255
    73
    std::vector<store> container;
alpar@255
    74
    int minimum;
alpar@255
    75
    ItemIntMap &iimap;
alpar@255
    76
    Compare comp;
alpar@255
    77
    int num_items;
jacint@373
    78
    
alpar@255
    79
  public:
alpar@1127
    80
    ///Status of the nodes
alpar@255
    81
    enum state_enum {
alpar@1127
    82
      ///The node is in the heap
alpar@255
    83
      IN_HEAP = 0,
alpar@1127
    84
      ///The node has never been in the heap
alpar@255
    85
      PRE_HEAP = -1,
alpar@1127
    86
      ///The node was in the heap but it got out of it
alpar@255
    87
      POST_HEAP = -2
alpar@255
    88
    };
alpar@255
    89
    
deba@1717
    90
    /// \brief The constructor
deba@1717
    91
    ///
deba@1717
    92
    /// \c _iimap should be given to the constructor, since it is
deba@1717
    93
    ///   used internally to handle the cross references.
deba@1185
    94
    explicit FibHeap(ItemIntMap &_iimap) 
deba@1185
    95
      : minimum(0), iimap(_iimap), num_items() {} 
jacint@1270
    96
 
deba@1717
    97
    /// \brief The constructor
deba@1717
    98
    ///
deba@1717
    99
    /// \c _iimap should be given to the constructor, since it is used
deba@1717
   100
    /// internally to handle the cross references. \c _comp is an
deba@1717
   101
    /// object for ordering of the priorities. 
jacint@373
   102
    FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(0), 
jacint@1270
   103
		  iimap(_iimap), comp(_comp), num_items() {}
alpar@255
   104
    
deba@1717
   105
    /// \brief The number of items stored in the heap.
deba@1717
   106
    ///
deba@1717
   107
    /// Returns the number of items stored in the heap.
jacint@373
   108
    int size() const { return num_items; }
jacint@373
   109
deba@1717
   110
    /// \brief Checks if the heap stores no items.
deba@1717
   111
    ///
deba@1717
   112
    ///   Returns \c true if and only if the heap stores no items.
jacint@373
   113
    bool empty() const { return num_items==0; }
jacint@373
   114
deba@1717
   115
    /// \brief Make empty this heap.
deba@1717
   116
    /// 
deba@2050
   117
    /// Make empty this heap. It does not change the cross reference
deba@2050
   118
    /// map.  If you want to reuse a heap what is not surely empty you
deba@2050
   119
    /// should first clear the heap and after that you should set the
deba@2050
   120
    /// cross reference map for each item to \c PRE_HEAP.
deba@1753
   121
    void clear() {
deba@1717
   122
      container.clear(); minimum = 0; num_items = 0;
deba@1717
   123
    }
jacint@373
   124
deba@1717
   125
    /// \brief \c item gets to the heap with priority \c value independently 
deba@1717
   126
    /// if \c item was already there.
deba@1717
   127
    ///
deba@1717
   128
    /// This method calls \ref push(\c item, \c value) if \c item is not
deba@1717
   129
    /// stored in the heap and it calls \ref decrease(\c item, \c value) or
deba@1717
   130
    /// \ref increase(\c item, \c value) otherwise.
jacint@387
   131
    void set (Item const item, PrioType const value); 
jacint@373
   132
    
deba@1717
   133
    /// \brief Adds \c item to the heap with priority \c value. 
deba@1717
   134
    ///    
deba@1717
   135
    /// Adds \c item to the heap with priority \c value. 
deba@1717
   136
    /// \pre \c item must not be stored in the heap. 
jacint@387
   137
    void push (Item const item, PrioType const value);
jacint@373
   138
    
deba@1717
   139
    /// \brief Returns the item with minimum priority relative to \c Compare.
deba@1717
   140
    ///
deba@1717
   141
    /// This method returns the item with minimum priority relative to \c
deba@1717
   142
    /// Compare.  
deba@1717
   143
    /// \pre The heap must be nonempty.  
jacint@373
   144
    Item top() const { return container[minimum].name; }
jacint@373
   145
deba@1717
   146
    /// \brief Returns the minimum priority relative to \c Compare.
deba@1717
   147
    ///
deba@1717
   148
    /// It returns the minimum priority relative to \c Compare.
deba@1717
   149
    /// \pre The heap must be nonempty.
jacint@373
   150
    PrioType prio() const { return container[minimum].prio; }
jacint@373
   151
    
deba@1717
   152
    /// \brief Returns the priority of \c item.
deba@1717
   153
    ///
deba@1717
   154
    /// This function returns the priority of \c item.
deba@1717
   155
    /// \pre \c item must be in the heap.
jacint@387
   156
    PrioType& operator[](const Item& item) { 
jacint@387
   157
      return container[iimap[item]].prio; 
jacint@387
   158
    }
jacint@373
   159
    
deba@1717
   160
    /// \brief Returns the priority of \c item.
deba@1717
   161
    ///
deba@1717
   162
    /// It returns the priority of \c item.
deba@1717
   163
    /// \pre \c item must be in the heap.
jacint@387
   164
    const PrioType& operator[](const Item& item) const { 
jacint@387
   165
      return container[iimap[item]].prio; 
alpar@255
   166
    }
alpar@255
   167
alpar@255
   168
deba@1717
   169
    /// \brief Deletes the item with minimum priority relative to \c Compare.
deba@1717
   170
    ///
deba@1717
   171
    /// This method deletes the item with minimum priority relative to \c
deba@1717
   172
    /// Compare from the heap.  
deba@1717
   173
    /// \pre The heap must be non-empty.  
jacint@373
   174
    void pop();
jacint@373
   175
deba@1717
   176
    /// \brief Deletes \c item from the heap.
deba@1717
   177
    ///
deba@1717
   178
    /// This method deletes \c item from the heap, if \c item was already
deba@1717
   179
    /// stored in the heap. It is quite inefficient in Fibonacci heaps.
jacint@387
   180
    void erase (const Item& item); 
jacint@373
   181
deba@1717
   182
    /// \brief Decreases the priority of \c item to \c value.
deba@1717
   183
    ///
deba@1717
   184
    /// This method decreases the priority of \c item to \c value.
deba@1717
   185
    /// \pre \c item must be stored in the heap with priority at least \c
deba@1717
   186
    ///   value relative to \c Compare.
jacint@387
   187
    void decrease (Item item, PrioType const value); 
jacint@373
   188
deba@1717
   189
    /// \brief Increases the priority of \c item to \c value.
deba@1717
   190
    ///
deba@1717
   191
    /// This method sets the priority of \c item to \c value. Though
deba@1717
   192
    /// there is no precondition on the priority of \c item, this
deba@1717
   193
    /// method should be used only if it is indeed necessary to increase
deba@1717
   194
    /// (relative to \c Compare) the priority of \c item, because this
deba@1717
   195
    /// method is inefficient.
jacint@387
   196
    void increase (Item item, PrioType const value) {
jacint@387
   197
      erase(item);
jacint@387
   198
      push(item, value);
jacint@373
   199
    }
jacint@373
   200
jacint@373
   201
deba@1717
   202
    /// \brief Returns if \c item is in, has already been in, or has never 
deba@1717
   203
    /// been in the heap.
deba@1717
   204
    ///
deba@1717
   205
    /// This method returns PRE_HEAP if \c item has never been in the
deba@1717
   206
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1717
   207
    /// otherwise. In the latter case it is possible that \c item will
deba@1717
   208
    /// get back to the heap again.
jacint@387
   209
    state_enum state(const Item &item) const {
jacint@387
   210
      int i=iimap[item];
jacint@387
   211
      if( i>=0 ) {
jacint@387
   212
	if ( container[i].in ) i=0;
jacint@387
   213
	else i=-2; 
jacint@387
   214
      }
jacint@387
   215
      return state_enum(i);
jacint@387
   216
    }    
deba@1902
   217
deba@1902
   218
    /// \brief Sets the state of the \c item in the heap.
deba@1902
   219
    ///
deba@1902
   220
    /// Sets the state of the \c item in the heap. It can be used to
deba@1902
   221
    /// manually clear the heap when it is important to achive the
deba@1902
   222
    /// better time complexity.
deba@1902
   223
    /// \param i The item.
deba@1902
   224
    /// \param st The state. It should not be \c IN_HEAP. 
deba@1902
   225
    void state(const Item& i, state_enum st) {
deba@1902
   226
      switch (st) {
deba@1902
   227
      case POST_HEAP:
deba@1902
   228
      case PRE_HEAP:
deba@1902
   229
        if (state(i) == IN_HEAP) {
deba@1902
   230
          erase(i);
deba@1902
   231
        }
deba@1903
   232
        iimap[i] = st;
deba@1902
   233
        break;
deba@1906
   234
      case IN_HEAP:
deba@1906
   235
        break;
deba@1902
   236
      }
deba@1902
   237
    }
jacint@387
   238
    
jacint@387
   239
  private:
jacint@387
   240
    
jacint@387
   241
    void balance();
jacint@387
   242
    void makeroot(int c);
jacint@387
   243
    void cut(int a, int b);
jacint@387
   244
    void cascade(int a);
jacint@387
   245
    void fuse(int a, int b);
jacint@387
   246
    void unlace(int a);
jacint@373
   247
jacint@373
   248
jacint@387
   249
    class store {
jacint@387
   250
      friend class FibHeap;
jacint@387
   251
      
jacint@387
   252
      Item name;
jacint@387
   253
      int parent;
jacint@387
   254
      int left_neighbor;
jacint@387
   255
      int right_neighbor;
jacint@387
   256
      int child;
jacint@387
   257
      int degree;  
jacint@387
   258
      bool marked;
jacint@387
   259
      bool in;
jacint@387
   260
      PrioType prio;
jacint@387
   261
      
jacint@387
   262
      store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
jacint@387
   263
    };
jacint@387
   264
  };    
jacint@387
   265
 
jacint@387
   266
jacint@373
   267
jacint@373
   268
    // **********************************************************************
jacint@373
   269
    //  IMPLEMENTATIONS
jacint@373
   270
    // **********************************************************************
jacint@373
   271
    
mqrelly@2263
   272
  template <typename Prio, typename ItemIntMap, 
jacint@387
   273
    typename Compare>
mqrelly@2263
   274
  void FibHeap<Prio, ItemIntMap, Compare>::set 
jacint@387
   275
  (Item const item, PrioType const value) 
jacint@387
   276
  {
jacint@387
   277
    int i=iimap[item];
jacint@387
   278
    if ( i >= 0 && container[i].in ) {
jacint@387
   279
      if ( comp(value, container[i].prio) ) decrease(item, value); 
jacint@387
   280
      if ( comp(container[i].prio, value) ) increase(item, value); 
jacint@387
   281
    } else push(item, value);
jacint@387
   282
  }
alpar@255
   283
    
mqrelly@2263
   284
  template <typename Prio, typename ItemIntMap, 
jacint@387
   285
    typename Compare>
mqrelly@2263
   286
  void FibHeap<Prio, ItemIntMap, Compare>::push 
jacint@387
   287
  (Item const item, PrioType const value) {
jacint@387
   288
      int i=iimap[item];      
alpar@255
   289
      if ( i < 0 ) {
alpar@255
   290
	int s=container.size();
jacint@387
   291
	iimap.set( item, s );	
alpar@255
   292
	store st;
jacint@387
   293
	st.name=item;
alpar@255
   294
	container.push_back(st);
alpar@255
   295
	i=s;
alpar@255
   296
      } else {
alpar@255
   297
	container[i].parent=container[i].child=-1;
alpar@255
   298
	container[i].degree=0;
alpar@255
   299
	container[i].in=true;
alpar@255
   300
	container[i].marked=false;
alpar@255
   301
      }
alpar@255
   302
alpar@255
   303
      if ( num_items ) {
alpar@255
   304
	container[container[minimum].right_neighbor].left_neighbor=i;
alpar@255
   305
	container[i].right_neighbor=container[minimum].right_neighbor;
alpar@255
   306
	container[minimum].right_neighbor=i;
alpar@255
   307
	container[i].left_neighbor=minimum;
alpar@255
   308
	if ( comp( value, container[minimum].prio) ) minimum=i; 
alpar@255
   309
      } else {
alpar@255
   310
	container[i].right_neighbor=container[i].left_neighbor=i;
alpar@255
   311
	minimum=i;	
alpar@255
   312
      }
alpar@255
   313
      container[i].prio=value;
alpar@255
   314
      ++num_items;
alpar@255
   315
    }
alpar@255
   316
    
mqrelly@2263
   317
  template <typename Prio, typename ItemIntMap, 
jacint@387
   318
    typename Compare>
mqrelly@2263
   319
  void FibHeap<Prio, ItemIntMap, Compare>::pop() {
alpar@255
   320
      /*The first case is that there are only one root.*/
alpar@255
   321
      if ( container[minimum].left_neighbor==minimum ) {
alpar@255
   322
	container[minimum].in=false;
alpar@255
   323
	if ( container[minimum].degree!=0 ) { 
alpar@255
   324
	  makeroot(container[minimum].child);
alpar@255
   325
	  minimum=container[minimum].child;
alpar@255
   326
	  balance();
alpar@255
   327
	}
alpar@255
   328
      } else {
alpar@255
   329
	int right=container[minimum].right_neighbor;
alpar@255
   330
	unlace(minimum);
alpar@255
   331
	container[minimum].in=false;
alpar@255
   332
	if ( container[minimum].degree > 0 ) {
alpar@255
   333
	  int left=container[minimum].left_neighbor;
alpar@255
   334
	  int child=container[minimum].child;
alpar@255
   335
	  int last_child=container[child].left_neighbor;
alpar@255
   336
	
alpar@255
   337
	  makeroot(child);
alpar@255
   338
	  
alpar@255
   339
	  container[left].right_neighbor=child;
alpar@255
   340
	  container[child].left_neighbor=left;
alpar@255
   341
	  container[right].left_neighbor=last_child;
alpar@255
   342
	  container[last_child].right_neighbor=right;
alpar@255
   343
	}
alpar@255
   344
	minimum=right;
alpar@255
   345
	balance();
alpar@255
   346
      } // the case where there are more roots
alpar@255
   347
      --num_items;   
alpar@255
   348
    }
alpar@255
   349
jacint@387
   350
mqrelly@2263
   351
  template <typename Prio, typename ItemIntMap, 
jacint@387
   352
    typename Compare>
mqrelly@2263
   353
  void FibHeap<Prio, ItemIntMap, Compare>::erase 
jacint@387
   354
  (const Item& item) {
jacint@387
   355
      int i=iimap[item];
alpar@255
   356
      
alpar@255
   357
      if ( i >= 0 && container[i].in ) { 	
alpar@255
   358
	if ( container[i].parent!=-1 ) {
alpar@255
   359
	  int p=container[i].parent;
alpar@255
   360
	  cut(i,p);	    
alpar@255
   361
	  cascade(p);
alpar@255
   362
	}
alpar@255
   363
	minimum=i;     //As if its prio would be -infinity
alpar@255
   364
	pop();
alpar@255
   365
      }
jacint@387
   366
  }
alpar@255
   367
    
mqrelly@2263
   368
  template <typename Prio, typename ItemIntMap, 
jacint@387
   369
    typename Compare>
mqrelly@2263
   370
  void FibHeap<Prio, ItemIntMap, Compare>::decrease 
jacint@387
   371
  (Item item, PrioType const value) {
jacint@387
   372
      int i=iimap[item];
alpar@255
   373
      container[i].prio=value;
alpar@255
   374
      int p=container[i].parent;
alpar@255
   375
      
alpar@255
   376
      if ( p!=-1 && comp(value, container[p].prio) ) {
alpar@255
   377
	cut(i,p);	    
alpar@255
   378
	cascade(p);
alpar@255
   379
      }      
alpar@255
   380
      if ( comp(value, container[minimum].prio) ) minimum=i; 
jacint@387
   381
  }
jacint@387
   382
 
alpar@255
   383
mqrelly@2263
   384
  template <typename Prio, typename ItemIntMap, 
jacint@387
   385
    typename Compare>
mqrelly@2263
   386
  void FibHeap<Prio, ItemIntMap, Compare>::balance() {      
alpar@255
   387
deba@1332
   388
    int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1;
alpar@255
   389
  
alpar@255
   390
    std::vector<int> A(maxdeg,-1); 
alpar@255
   391
    
alpar@255
   392
    /*
alpar@255
   393
     *Recall that now minimum does not point to the minimum prio element.
alpar@255
   394
     *We set minimum to this during balance().
alpar@255
   395
     */
alpar@255
   396
    int anchor=container[minimum].left_neighbor; 
alpar@255
   397
    int next=minimum; 
alpar@255
   398
    bool end=false; 
alpar@255
   399
    	
alpar@255
   400
       do {
alpar@255
   401
	int active=next;
alpar@255
   402
	if ( anchor==active ) end=true;
alpar@255
   403
	int d=container[active].degree;
alpar@255
   404
	next=container[active].right_neighbor;
alpar@255
   405
alpar@255
   406
	while (A[d]!=-1) {	  
alpar@255
   407
	  if( comp(container[active].prio, container[A[d]].prio) ) {
alpar@255
   408
	    fuse(active,A[d]); 
alpar@255
   409
	  } else { 
alpar@255
   410
	    fuse(A[d],active);
alpar@255
   411
	    active=A[d];
alpar@255
   412
	  } 
alpar@255
   413
	  A[d]=-1;
alpar@255
   414
	  ++d;
alpar@255
   415
	}	
alpar@255
   416
	A[d]=active;
alpar@255
   417
       } while ( !end );
alpar@255
   418
alpar@255
   419
alpar@255
   420
       while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
alpar@255
   421
       int s=minimum;
alpar@255
   422
       int m=minimum;
alpar@255
   423
       do {  
alpar@255
   424
	 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
alpar@255
   425
	 s=container[s].right_neighbor;
alpar@255
   426
       } while ( s != m );
alpar@255
   427
    }
alpar@255
   428
mqrelly@2263
   429
  template <typename Prio, typename ItemIntMap, 
jacint@387
   430
    typename Compare>
mqrelly@2263
   431
  void FibHeap<Prio, ItemIntMap, Compare>::makeroot 
jacint@387
   432
  (int c) {
alpar@255
   433
      int s=c;
alpar@255
   434
      do {  
alpar@255
   435
	container[s].parent=-1;
alpar@255
   436
	s=container[s].right_neighbor;
alpar@255
   437
      } while ( s != c );
alpar@255
   438
    }
jacint@387
   439
  
jacint@387
   440
  
mqrelly@2263
   441
  template <typename Prio, typename ItemIntMap, 
jacint@387
   442
    typename Compare>
mqrelly@2263
   443
  void FibHeap<Prio, ItemIntMap, Compare>::cut 
jacint@387
   444
  (int a, int b) {    
jacint@387
   445
    /*
jacint@387
   446
     *Replacing a from the children of b.
jacint@387
   447
     */
jacint@387
   448
    --container[b].degree;
alpar@255
   449
    
jacint@387
   450
    if ( container[b].degree !=0 ) {
jacint@387
   451
      int child=container[b].child;
jacint@387
   452
      if ( child==a ) 
jacint@387
   453
	container[b].child=container[child].right_neighbor;
jacint@387
   454
      unlace(a);
jacint@387
   455
    }
jacint@387
   456
    
jacint@387
   457
    
jacint@387
   458
    /*Lacing a to the roots.*/
jacint@387
   459
    int right=container[minimum].right_neighbor;
jacint@387
   460
    container[minimum].right_neighbor=a;
jacint@387
   461
    container[a].left_neighbor=minimum;
jacint@387
   462
    container[a].right_neighbor=right;
jacint@387
   463
    container[right].left_neighbor=a;
jacint@387
   464
    
jacint@387
   465
    container[a].parent=-1;
jacint@387
   466
    container[a].marked=false;
jacint@387
   467
  }
jacint@387
   468
  
alpar@255
   469
mqrelly@2263
   470
  template <typename Prio, typename ItemIntMap, 
jacint@387
   471
    typename Compare>
mqrelly@2263
   472
  void FibHeap<Prio, ItemIntMap, Compare>::cascade 
jacint@387
   473
  (int a) 
alpar@255
   474
    {
alpar@255
   475
      if ( container[a].parent!=-1 ) {
alpar@255
   476
	int p=container[a].parent;
alpar@255
   477
	
alpar@255
   478
	if ( container[a].marked==false ) container[a].marked=true;
alpar@255
   479
	else {
alpar@255
   480
	  cut(a,p);
alpar@255
   481
	  cascade(p);
alpar@255
   482
	}
alpar@255
   483
      }
alpar@255
   484
    }
alpar@255
   485
alpar@255
   486
mqrelly@2263
   487
  template <typename Prio, typename ItemIntMap, 
jacint@387
   488
    typename Compare>
mqrelly@2263
   489
  void FibHeap<Prio, ItemIntMap, Compare>::fuse 
jacint@387
   490
  (int a, int b) {
alpar@255
   491
      unlace(b);
alpar@255
   492
      
alpar@255
   493
      /*Lacing b under a.*/
alpar@255
   494
      container[b].parent=a;
alpar@255
   495
alpar@255
   496
      if (container[a].degree==0) {
alpar@255
   497
	container[b].left_neighbor=b;
alpar@255
   498
	container[b].right_neighbor=b;
alpar@255
   499
	container[a].child=b;	
alpar@255
   500
      } else {
alpar@255
   501
	int child=container[a].child;
alpar@255
   502
	int last_child=container[child].left_neighbor;
alpar@255
   503
	container[child].left_neighbor=b;
alpar@255
   504
	container[b].right_neighbor=child;
alpar@255
   505
	container[last_child].right_neighbor=b;
alpar@255
   506
	container[b].left_neighbor=last_child;
alpar@255
   507
      }
alpar@255
   508
alpar@255
   509
      ++container[a].degree;
alpar@255
   510
      
alpar@255
   511
      container[b].marked=false;
alpar@255
   512
    }
alpar@255
   513
jacint@387
   514
  
jacint@387
   515
  /*
jacint@387
   516
   *It is invoked only if a has siblings.
jacint@387
   517
   */
mqrelly@2263
   518
  template <typename Prio, typename ItemIntMap, 
jacint@387
   519
    typename Compare>
mqrelly@2263
   520
  void FibHeap<Prio, ItemIntMap, Compare>::unlace 
jacint@387
   521
  (int a) {      
alpar@255
   522
      int leftn=container[a].left_neighbor;
alpar@255
   523
      int rightn=container[a].right_neighbor;
alpar@255
   524
      container[leftn].right_neighbor=rightn;
alpar@255
   525
      container[rightn].left_neighbor=leftn;
jacint@387
   526
  }
alpar@255
   527
  
alpar@430
   528
alpar@921
   529
} //namespace lemon
alpar@477
   530
alpar@921
   531
#endif //LEMON_FIB_HEAP_H
alpar@477
   532