lemon/bin_heap.h
author deba
Wed, 14 Dec 2005 18:07:28 +0000
changeset 1858 a5b6d941ed52
parent 1717 75fe24093ded
child 1875 98698b69a902
permissions -rw-r--r--
Bug fix in def pred map
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
klao@39
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@39
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
klao@39
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
klao@39
    14
 *
klao@39
    15
 */
klao@39
    16
alpar@921
    17
#ifndef LEMON_BIN_HEAP_H
alpar@921
    18
#define LEMON_BIN_HEAP_H
klao@37
    19
klao@491
    20
///\ingroup auxdat
klao@274
    21
///\file
klao@274
    22
///\brief Binary Heap implementation.
klao@274
    23
klao@37
    24
#include <vector>
klao@37
    25
#include <utility>
klao@37
    26
#include <functional>
klao@37
    27
alpar@921
    28
namespace lemon {
klao@37
    29
deba@1834
    30
  /// \ingroup auxdat
alpar@430
    31
jacint@1270
    32
  /// A Binary Heap implementation.
alpar@967
    33
  
jacint@1270
    34
  ///This class implements the \e binary \e heap data structure. A \e heap
jacint@1270
    35
  ///is a data structure for storing items with specified values called \e
jacint@1270
    36
  ///priorities in such a way that finding the item with minimum priority is
jacint@1270
    37
  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
jacint@1270
    38
  ///one can change the priority of an item, add or erase an item, etc.
jacint@1270
    39
  ///
jacint@1270
    40
  ///\param Item Type of the items to be stored.  
jacint@1270
    41
  ///\param Prio Type of the priority of the items.
jacint@1270
    42
  ///\param ItemIntMap A read and writable Item int map, used internally
jacint@1270
    43
  ///to handle the cross references.
jacint@1270
    44
  ///\param Compare A class for the ordering of the priorities. The
jacint@1270
    45
  ///default is \c std::less<Prio>.
alpar@967
    46
  ///
alpar@967
    47
  ///\sa FibHeap
alpar@967
    48
  ///\sa Dijkstra
klao@172
    49
  template <typename Item, typename Prio, typename ItemIntMap,
klao@172
    50
	    typename Compare = std::less<Prio> >
klao@37
    51
  class BinHeap {
klao@37
    52
klao@37
    53
  public:
klao@172
    54
    typedef Item                             ItemType;
klao@37
    55
    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
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@1717
   113
    /// Make empty this heap.
deba@1717
   114
    void clear() { 
deba@1717
   115
      for (int i = 0; i < (int)data.size(); ++i) {
deba@1717
   116
	iim.set(data[i].first, POST_HEAP);
deba@1717
   117
      }
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.
klao@172
   163
    void push(const Item &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.  
klao@172
   170
    Item 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. 
klao@172
   196
    void erase(const Item &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.
klao@274
   206
    Prio operator[](const Item &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.
klao@172
   218
    void set(const Item &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.
klao@172
   238
    void decrease(const Item &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.
klao@172
   250
    void increase(const Item &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.
klao@172
   263
    state_enum state(const Item &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
klao@37
   270
  }; // class BinHeap
klao@37
   271
klao@37
   272
  
klao@37
   273
  template <typename K, typename V, typename M, typename C>
klao@37
   274
  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
klao@37
   275
    int par = parent(hole);
klao@37
   276
    while( hole>0 && less(p,data[par]) ) {
klao@37
   277
      move(data[par],hole);
klao@37
   278
      hole = par;
klao@37
   279
      par = parent(hole);
klao@37
   280
    }
klao@37
   281
    move(p, hole);
klao@37
   282
    return hole;
klao@37
   283
  }
klao@37
   284
klao@37
   285
  template <typename K, typename V, typename M, typename C>
klao@37
   286
  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
klao@37
   287
    int child = second_child(hole);
klao@37
   288
    while(child < length) {
klao@37
   289
      if( less(data[child-1], data[child]) ) {
klao@37
   290
	--child;
klao@37
   291
      }
klao@37
   292
      if( !less(data[child], p) )
klao@37
   293
	goto ok;
klao@37
   294
      move(data[child], hole);
klao@37
   295
      hole = child;
klao@37
   296
      child = second_child(hole);
klao@37
   297
    }
klao@37
   298
    child--;
klao@37
   299
    if( child<length && less(data[child], p) ) {
klao@37
   300
      move(data[child], hole);
klao@37
   301
      hole=child;
klao@37
   302
    }
klao@37
   303
  ok:
klao@37
   304
    move(p, hole);
klao@37
   305
    return hole;
klao@37
   306
  }
klao@37
   307
alpar@430
   308
alpar@921
   309
} // namespace lemon
klao@37
   310
alpar@921
   311
#endif // LEMON_BIN_HEAP_H