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