lemon/bin_heap.h
author convert-repo
Thu, 04 Mar 2010 19:34:55 +0000
changeset 2659 611ced85018b
parent 2548 a3ba22ebccc6
permissions -rw-r--r--
update tags
alpar@906
     1
/* -*- C++ -*-
klao@39
     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).
klao@39
     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.
klao@39
    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.
klao@39
    16
 *
klao@39
    17
 */
klao@39
    18
alpar@921
    19
#ifndef LEMON_BIN_HEAP_H
alpar@921
    20
#define LEMON_BIN_HEAP_H
klao@37
    21
klao@491
    22
///\ingroup auxdat
klao@274
    23
///\file
klao@274
    24
///\brief Binary Heap implementation.
klao@274
    25
klao@37
    26
#include <vector>
klao@37
    27
#include <utility>
klao@37
    28
#include <functional>
klao@37
    29
alpar@921
    30
namespace lemon {
klao@37
    31
deba@2529
    32
  ///\ingroup auxdat
deba@2529
    33
  ///
deba@2529
    34
  ///\brief A Binary Heap implementation.
deba@2529
    35
  ///
jacint@1270
    36
  ///This class implements the \e binary \e heap data structure. A \e heap
jacint@1270
    37
  ///is a data structure for storing items with specified values called \e
jacint@1270
    38
  ///priorities in such a way that finding the item with minimum priority is
jacint@1270
    39
  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
jacint@1270
    40
  ///one can change the priority of an item, add or erase an item, etc.
jacint@1270
    41
  ///
deba@2547
    42
  ///\param _Prio Type of the priority of the items.
deba@2547
    43
  ///\param _ItemIntMap A read and writable Item int map, used internally
jacint@1270
    44
  ///to handle the cross references.
deba@2547
    45
  ///\param _Compare A class for the ordering of the priorities. The
deba@2547
    46
  ///default is \c std::less<_Prio>.
alpar@967
    47
  ///
alpar@967
    48
  ///\sa FibHeap
alpar@967
    49
  ///\sa Dijkstra
deba@2547
    50
  template <typename _Prio, typename _ItemIntMap,
deba@2547
    51
	    typename _Compare = std::less<_Prio> >
klao@37
    52
  class BinHeap {
klao@37
    53
klao@37
    54
  public:
deba@2548
    55
    ///\e
deba@2547
    56
    typedef _ItemIntMap ItemIntMap;
deba@2548
    57
    ///\e
deba@2547
    58
    typedef _Prio Prio;
deba@2548
    59
    ///\e
deba@2547
    60
    typedef typename ItemIntMap::Key Item;
deba@2548
    61
    ///\e
deba@2547
    62
    typedef std::pair<Item,Prio> Pair;
deba@2548
    63
    ///\e
deba@2547
    64
    typedef _Compare Compare;
klao@37
    65
deba@1331
    66
    /// \brief Type to represent the items states.
klao@274
    67
    ///
deba@1331
    68
    /// Each Item element have a state associated to it. It may be "in heap",
alpar@1336
    69
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@1331
    70
    /// heap's point of view, but may be useful to the user.
deba@1331
    71
    ///
alpar@1336
    72
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@1331
    73
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@2547
    74
    enum State {
klao@37
    75
      IN_HEAP = 0,
klao@37
    76
      PRE_HEAP = -1,
klao@37
    77
      POST_HEAP = -2
klao@37
    78
    };
klao@37
    79
klao@37
    80
  private:
deba@2547
    81
    std::vector<Pair> data;
klao@37
    82
    Compare comp;
klao@172
    83
    ItemIntMap &iim;
klao@37
    84
klao@37
    85
  public:
deba@1331
    86
    /// \brief The constructor.
deba@1331
    87
    ///
deba@1331
    88
    /// The constructor.
deba@1331
    89
    /// \param _iim should be given to the constructor, since it is used
deba@1331
    90
    /// internally to handle the cross references. The value of the map
deba@1331
    91
    /// should be PRE_HEAP (-1) for each element.
deba@1185
    92
    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
jacint@1270
    93
    
deba@1331
    94
    /// \brief The constructor.
deba@1331
    95
    ///
deba@1331
    96
    /// The constructor.
deba@1331
    97
    /// \param _iim should be given to the constructor, since it is used
deba@1331
    98
    /// internally to handle the cross references. The value of the map
deba@1331
    99
    /// should be PRE_HEAP (-1) for each element.
deba@1331
   100
    ///
deba@1331
   101
    /// \param _comp The comparator function object.
deba@1191
   102
    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
deba@1185
   103
      : iim(_iim), comp(_comp) {}
klao@37
   104
klao@37
   105
deba@1331
   106
    /// The number of items stored in the heap.
deba@1331
   107
    ///
deba@1331
   108
    /// \brief Returns the number of items stored in the heap.
klao@37
   109
    int size() const { return data.size(); }
jacint@1270
   110
    
deba@1331
   111
    /// \brief Checks if the heap stores no items.
deba@1331
   112
    ///
deba@1331
   113
    /// Returns \c true if and only if the heap stores no items.
klao@41
   114
    bool empty() const { return data.empty(); }
klao@37
   115
deba@1717
   116
    /// \brief Make empty this heap.
deba@1717
   117
    /// 
deba@2050
   118
    /// Make empty this heap. It does not change the cross reference map.
deba@2050
   119
    /// If you want to reuse what is not surely empty you should first clear
deba@2050
   120
    /// the heap and after that you should set the cross reference map for
deba@2050
   121
    /// each item to \c PRE_HEAP.
deba@1717
   122
    void clear() { 
deba@1717
   123
      data.clear(); 
deba@1717
   124
    }
deba@1717
   125
klao@37
   126
  private:
klao@37
   127
    static int parent(int i) { return (i-1)/2; }
deba@2546
   128
klao@37
   129
    static int second_child(int i) { return 2*i+2; }
deba@2547
   130
    bool less(const Pair &p1, const Pair &p2) const {
klao@37
   131
      return comp(p1.second, p2.second);
klao@37
   132
    }
klao@37
   133
deba@2547
   134
    int bubble_up(int hole, Pair p) {
deba@2546
   135
      int par = parent(hole);
deba@2546
   136
      while( hole>0 && less(p,data[par]) ) {
deba@2546
   137
	move(data[par],hole);
deba@2546
   138
	hole = par;
deba@2546
   139
	par = parent(hole);
deba@2546
   140
      }
deba@2546
   141
      move(p, hole);
deba@2546
   142
      return hole;
deba@2546
   143
    }
deba@2546
   144
deba@2547
   145
    int bubble_down(int hole, Pair p, int length) {
deba@2546
   146
      int child = second_child(hole);
deba@2546
   147
      while(child < length) {
deba@2546
   148
	if( less(data[child-1], data[child]) ) {
deba@2546
   149
	  --child;
deba@2546
   150
	}
deba@2546
   151
	if( !less(data[child], p) )
deba@2546
   152
	  goto ok;
deba@2546
   153
	move(data[child], hole);
deba@2546
   154
	hole = child;
deba@2546
   155
	child = second_child(hole);
deba@2546
   156
      }
deba@2546
   157
      child--;
deba@2546
   158
      if( child<length && less(data[child], p) ) {
deba@2546
   159
	move(data[child], hole);
deba@2546
   160
	hole=child;
deba@2546
   161
      }
deba@2546
   162
    ok:
deba@2546
   163
      move(p, hole);
deba@2546
   164
      return hole;
deba@2546
   165
    }
klao@37
   166
deba@2547
   167
    void move(const Pair &p, int i) {
klao@37
   168
      data[i] = p;
klao@172
   169
      iim.set(p.first, i);
klao@37
   170
    }
klao@37
   171
klao@37
   172
  public:
deba@1331
   173
    /// \brief Insert a pair of item and priority into the heap.
deba@1331
   174
    ///
deba@1331
   175
    /// Adds \c p.first to the heap with priority \c p.second.
deba@1331
   176
    /// \param p The pair to insert.
deba@2547
   177
    void push(const Pair &p) {
klao@37
   178
      int n = data.size();
klao@37
   179
      data.resize(n+1);
klao@37
   180
      bubble_up(n, p);
klao@37
   181
    }
jacint@1270
   182
deba@1331
   183
    /// \brief Insert an item into the heap with the given heap.
deba@1331
   184
    ///    
deba@1331
   185
    /// Adds \c i to the heap with priority \c p. 
deba@1331
   186
    /// \param i The item to insert.
deba@1331
   187
    /// \param p The priority of the item.
deba@2547
   188
    void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
klao@37
   189
deba@1331
   190
    /// \brief Returns the item with minimum priority relative to \c Compare.
deba@1331
   191
    ///
deba@1331
   192
    /// This method returns the item with minimum priority relative to \c
deba@1331
   193
    /// Compare.  
deba@1331
   194
    /// \pre The heap must be nonempty.  
deba@2547
   195
    Item top() const {
klao@37
   196
      return data[0].first;
klao@37
   197
    }
jacint@1270
   198
deba@1331
   199
    /// \brief Returns the minimum priority relative to \c Compare.
deba@1331
   200
    ///
deba@1331
   201
    /// It returns the minimum priority relative to \c Compare.
deba@1331
   202
    /// \pre The heap must be nonempty.
klao@274
   203
    Prio prio() const {
klao@37
   204
      return data[0].second;
klao@37
   205
    }
klao@37
   206
deba@1331
   207
    /// \brief Deletes the item with minimum priority relative to \c Compare.
deba@1331
   208
    ///
deba@1331
   209
    /// This method deletes the item with minimum priority relative to \c
deba@1331
   210
    /// Compare from the heap.  
deba@1331
   211
    /// \pre The heap must be non-empty.  
klao@37
   212
    void pop() {
deba@2546
   213
      int n = data.size()-1;
deba@2546
   214
      iim.set(data[0].first, POST_HEAP);
deba@2546
   215
      if (n > 0) {
deba@2546
   216
	bubble_down(0, data[n], n);
deba@2546
   217
      }
deba@2546
   218
      data.pop_back();
klao@41
   219
    }
klao@41
   220
deba@1331
   221
    /// \brief Deletes \c i from the heap.
deba@1331
   222
    ///
deba@2546
   223
    /// This method deletes item \c i from the heap.
deba@2546
   224
    /// \param i The item to erase.
deba@2546
   225
    /// \pre The item should be in the heap.
deba@2547
   226
    void erase(const Item &i) {
deba@2546
   227
      int h = iim[i];
deba@2546
   228
      int n = data.size()-1;
deba@2546
   229
      iim.set(data[h].first, POST_HEAP);
deba@2546
   230
      if( h < n ) {
deba@2546
   231
	if ( bubble_up(h, data[n]) == h) {
deba@2546
   232
	  bubble_down(h, data[n], n);
deba@2546
   233
	}
deba@2546
   234
      }
deba@2546
   235
      data.pop_back();
klao@37
   236
    }
klao@37
   237
jacint@1270
   238
    
deba@1331
   239
    /// \brief Returns the priority of \c i.
deba@1331
   240
    ///
deba@1331
   241
    /// This function returns the priority of item \c i.  
deba@1331
   242
    /// \pre \c i must be in the heap.
deba@1331
   243
    /// \param i The item.
deba@2547
   244
    Prio operator[](const Item &i) const {
jacint@221
   245
      int idx = iim[i];
klao@37
   246
      return data[idx].second;
klao@37
   247
    }
klao@274
   248
deba@1331
   249
    /// \brief \c i gets to the heap with priority \c p independently 
deba@1331
   250
    /// if \c i was already there.
deba@1331
   251
    ///
deba@1331
   252
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1331
   253
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1331
   254
    /// \param i The item.
deba@1331
   255
    /// \param p The priority.
deba@2547
   256
    void set(const Item &i, const Prio &p) {
jacint@221
   257
      int idx = iim[i];
klao@37
   258
      if( idx < 0 ) {
klao@172
   259
	push(i,p);
klao@37
   260
      }
klao@172
   261
      else if( comp(p, data[idx].second) ) {
deba@2547
   262
	bubble_up(idx, Pair(i,p));
klao@37
   263
      }
klao@37
   264
      else {
deba@2547
   265
	bubble_down(idx, Pair(i,p), data.size());
klao@37
   266
      }
klao@37
   267
    }
klao@37
   268
deba@1331
   269
    /// \brief Decreases the priority of \c i to \c p.
deba@2529
   270
    ///
deba@1331
   271
    /// This method decreases the priority of item \c i to \c p.
deba@1331
   272
    /// \pre \c i must be stored in the heap with priority at least \c
deba@1331
   273
    /// p relative to \c Compare.
deba@1331
   274
    /// \param i The item.
deba@1331
   275
    /// \param p The priority.
deba@2547
   276
    void decrease(const Item &i, const Prio &p) {
jacint@221
   277
      int idx = iim[i];
deba@2547
   278
      bubble_up(idx, Pair(i,p));
klao@37
   279
    }
jacint@1270
   280
    
deba@1331
   281
    /// \brief Increases the priority of \c i to \c p.
deba@1331
   282
    ///
deba@1331
   283
    /// This method sets the priority of item \c i to \c p. 
deba@1331
   284
    /// \pre \c i must be stored in the heap with priority at most \c
deba@1331
   285
    /// p relative to \c Compare.
deba@1331
   286
    /// \param i The item.
deba@1331
   287
    /// \param p The priority.
deba@2547
   288
    void increase(const Item &i, const Prio &p) {
jacint@221
   289
      int idx = iim[i];
deba@2547
   290
      bubble_down(idx, Pair(i,p), data.size());
klao@37
   291
    }
klao@37
   292
deba@1331
   293
    /// \brief Returns if \c item is in, has already been in, or has 
deba@1331
   294
    /// never been in the heap.
deba@1331
   295
    ///
deba@1331
   296
    /// This method returns PRE_HEAP if \c item has never been in the
deba@1331
   297
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1331
   298
    /// otherwise. In the latter case it is possible that \c item will
deba@1331
   299
    /// get back to the heap again.
deba@1331
   300
    /// \param i The item.
deba@2547
   301
    State state(const Item &i) const {
jacint@221
   302
      int s = iim[i];
klao@39
   303
      if( s>=0 )
klao@39
   304
	s=0;
deba@2547
   305
      return State(s);
klao@39
   306
    }
klao@39
   307
deba@1902
   308
    /// \brief Sets the state of the \c item in the heap.
deba@1902
   309
    ///
deba@1902
   310
    /// Sets the state of the \c item in the heap. It can be used to
deba@1902
   311
    /// manually clear the heap when it is important to achive the
deba@1902
   312
    /// better time complexity.
deba@1902
   313
    /// \param i The item.
deba@1902
   314
    /// \param st The state. It should not be \c IN_HEAP. 
deba@2547
   315
    void state(const Item& i, State st) {
deba@1902
   316
      switch (st) {
deba@1902
   317
      case POST_HEAP:
deba@1902
   318
      case PRE_HEAP:
deba@1902
   319
        if (state(i) == IN_HEAP) {
deba@1902
   320
          erase(i);
deba@1902
   321
        }
deba@1903
   322
        iim[i] = st;
deba@1902
   323
        break;
deba@1906
   324
      case IN_HEAP:
deba@1906
   325
        break;
deba@1902
   326
      }
deba@1902
   327
    }
deba@1902
   328
deba@2548
   329
    /// \brief Replaces an item in the heap.
deba@2548
   330
    ///
deba@2548
   331
    /// The \c i item is replaced with \c j item. The \c i item should
deba@2548
   332
    /// be in the heap, while the \c j should be out of the heap. The
deba@2548
   333
    /// \c i item will out of the heap and \c j will be in the heap
deba@2548
   334
    /// with the same prioriority as prevoiusly the \c i item.
deba@2548
   335
    void replace(const Item& i, const Item& j) {
deba@2548
   336
      int idx = iim[i];
deba@2548
   337
      iim.set(i, iim[j]);
deba@2548
   338
      iim.set(j, idx);
deba@2548
   339
      data[idx].first = j;
deba@2548
   340
    }
deba@2548
   341
klao@37
   342
  }; // class BinHeap
klao@37
   343
  
alpar@921
   344
} // namespace lemon
klao@37
   345
alpar@921
   346
#endif // LEMON_BIN_HEAP_H