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