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