lemon/bin_heap.h
author ladanyi
Mon, 23 May 2005 04:48:14 +0000
changeset 1435 8e85e6bbefdf
parent 1359 src/lemon/bin_heap.h@1581f961cfaa
child 1717 75fe24093ded
permissions -rw-r--r--
trunk/src/* move to trunk/
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
alpar@430
    30
  /// \addtogroup auxdat
alpar@430
    31
  /// @{
alpar@430
    32
jacint@1270
    33
  /// A Binary Heap implementation.
alpar@967
    34
  
jacint@1270
    35
  ///This class implements the \e binary \e heap data structure. A \e heap
jacint@1270
    36
  ///is a data structure for storing items with specified values called \e
jacint@1270
    37
  ///priorities in such a way that finding the item with minimum priority is
jacint@1270
    38
  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
jacint@1270
    39
  ///one can change the priority of an item, add or erase an item, etc.
jacint@1270
    40
  ///
jacint@1270
    41
  ///\param Item Type of the items to be stored.  
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
klao@172
    50
  template <typename Item, typename Prio, typename ItemIntMap,
klao@172
    51
	    typename Compare = std::less<Prio> >
klao@37
    52
  class BinHeap {
klao@37
    53
klao@37
    54
  public:
klao@172
    55
    typedef Item                             ItemType;
klao@37
    56
    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
klao@172
    57
    typedef Prio                             PrioType;
klao@172
    58
    typedef std::pair<ItemType,PrioType>     PairType;
klao@172
    59
    typedef ItemIntMap                       ItemIntMapType;
klao@172
    60
    typedef Compare                          PrioCompare;
klao@37
    61
deba@1331
    62
    /// \brief Type to represent the items states.
klao@274
    63
    ///
deba@1331
    64
    /// Each Item element have a state associated to it. It may be "in heap",
alpar@1336
    65
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@1331
    66
    /// heap's point of view, but may be useful to the user.
deba@1331
    67
    ///
alpar@1336
    68
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@1331
    69
    /// PRE_HEAP (-1) to any element to be put in the heap...
klao@39
    70
    enum state_enum {
klao@37
    71
      IN_HEAP = 0,
klao@37
    72
      PRE_HEAP = -1,
klao@37
    73
      POST_HEAP = -2
klao@37
    74
    };
klao@37
    75
klao@37
    76
  private:
klao@37
    77
    std::vector<PairType> data;
klao@37
    78
    Compare comp;
klao@172
    79
    ItemIntMap &iim;
klao@37
    80
klao@37
    81
  public:
deba@1331
    82
    /// \brief The constructor.
deba@1331
    83
    ///
deba@1331
    84
    /// The constructor.
deba@1331
    85
    /// \param _iim should be given to the constructor, since it is used
deba@1331
    86
    /// internally to handle the cross references. The value of the map
deba@1331
    87
    /// should be PRE_HEAP (-1) for each element.
deba@1185
    88
    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
jacint@1270
    89
    
deba@1331
    90
    /// \brief The constructor.
deba@1331
    91
    ///
deba@1331
    92
    /// The constructor.
deba@1331
    93
    /// \param _iim should be given to the constructor, since it is used
deba@1331
    94
    /// internally to handle the cross references. The value of the map
deba@1331
    95
    /// should be PRE_HEAP (-1) for each element.
deba@1331
    96
    ///
deba@1331
    97
    /// \param _comp The comparator function object.
deba@1191
    98
    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
deba@1185
    99
      : iim(_iim), comp(_comp) {}
klao@37
   100
klao@37
   101
deba@1331
   102
    /// The number of items stored in the heap.
deba@1331
   103
    ///
deba@1331
   104
    /// \brief Returns the number of items stored in the heap.
klao@37
   105
    int size() const { return data.size(); }
jacint@1270
   106
    
deba@1331
   107
    /// \brief Checks if the heap stores no items.
deba@1331
   108
    ///
deba@1331
   109
    /// Returns \c true if and only if the heap stores no items.
klao@41
   110
    bool empty() const { return data.empty(); }
klao@37
   111
klao@37
   112
  private:
klao@37
   113
    static int parent(int i) { return (i-1)/2; }
klao@37
   114
    static int second_child(int i) { return 2*i+2; }
klao@214
   115
    bool less(const PairType &p1, const PairType &p2) const {
klao@37
   116
      return comp(p1.second, p2.second);
klao@37
   117
    }
klao@37
   118
klao@37
   119
    int bubble_up(int hole, PairType p);
klao@37
   120
    int bubble_down(int hole, PairType p, int length);
klao@37
   121
klao@37
   122
    void move(const PairType &p, int i) {
klao@37
   123
      data[i] = p;
klao@172
   124
      iim.set(p.first, i);
klao@37
   125
    }
klao@37
   126
klao@41
   127
    void rmidx(int h) {
klao@41
   128
      int n = data.size()-1;
klao@41
   129
      if( h>=0 && h<=n ) {
klao@172
   130
	iim.set(data[h].first, POST_HEAP);
klao@41
   131
	if ( h<n ) {
klao@41
   132
	  bubble_down(h, data[n], n);
klao@41
   133
	}
klao@41
   134
	data.pop_back();
klao@41
   135
      }
klao@41
   136
    }
klao@41
   137
klao@37
   138
  public:
deba@1331
   139
    /// \brief Insert a pair of item and priority into the heap.
deba@1331
   140
    ///
deba@1331
   141
    /// Adds \c p.first to the heap with priority \c p.second.
deba@1331
   142
    /// \param p The pair to insert.
klao@37
   143
    void push(const PairType &p) {
klao@37
   144
      int n = data.size();
klao@37
   145
      data.resize(n+1);
klao@37
   146
      bubble_up(n, p);
klao@37
   147
    }
jacint@1270
   148
deba@1331
   149
    /// \brief Insert an item into the heap with the given heap.
deba@1331
   150
    ///    
deba@1331
   151
    /// Adds \c i to the heap with priority \c p. 
deba@1331
   152
    /// \param i The item to insert.
deba@1331
   153
    /// \param p The priority of the item.
klao@172
   154
    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
klao@37
   155
deba@1331
   156
    /// \brief Returns the item with minimum priority relative to \c Compare.
deba@1331
   157
    ///
deba@1331
   158
    /// This method returns the item with minimum priority relative to \c
deba@1331
   159
    /// Compare.  
deba@1331
   160
    /// \pre The heap must be nonempty.  
klao@172
   161
    Item top() const {
klao@37
   162
      return data[0].first;
klao@37
   163
    }
jacint@1270
   164
deba@1331
   165
    /// \brief Returns the minimum priority relative to \c Compare.
deba@1331
   166
    ///
deba@1331
   167
    /// It returns the minimum priority relative to \c Compare.
deba@1331
   168
    /// \pre The heap must be nonempty.
klao@274
   169
    Prio prio() const {
klao@37
   170
      return data[0].second;
klao@37
   171
    }
klao@37
   172
deba@1331
   173
    /// \brief Deletes the item with minimum priority relative to \c Compare.
deba@1331
   174
    ///
deba@1331
   175
    /// This method deletes the item with minimum priority relative to \c
deba@1331
   176
    /// Compare from the heap.  
deba@1331
   177
    /// \pre The heap must be non-empty.  
klao@37
   178
    void pop() {
klao@41
   179
      rmidx(0);
klao@41
   180
    }
klao@41
   181
deba@1331
   182
    /// \brief Deletes \c i from the heap.
deba@1331
   183
    ///
deba@1331
   184
    /// This method deletes item \c i from the heap, if \c i was
deba@1331
   185
    /// already stored in the heap.
deba@1331
   186
    /// \param i The item to erase. 
klao@172
   187
    void erase(const Item &i) {
jacint@221
   188
      rmidx(iim[i]);
klao@37
   189
    }
klao@37
   190
jacint@1270
   191
    
deba@1331
   192
    /// \brief Returns the priority of \c i.
deba@1331
   193
    ///
deba@1331
   194
    /// This function returns the priority of item \c i.  
deba@1331
   195
    /// \pre \c i must be in the heap.
deba@1331
   196
    /// \param i The item.
klao@274
   197
    Prio operator[](const Item &i) const {
jacint@221
   198
      int idx = iim[i];
klao@37
   199
      return data[idx].second;
klao@37
   200
    }
klao@274
   201
deba@1331
   202
    /// \brief \c i gets to the heap with priority \c p independently 
deba@1331
   203
    /// if \c i was already there.
deba@1331
   204
    ///
deba@1331
   205
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1331
   206
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1331
   207
    /// \param i The item.
deba@1331
   208
    /// \param p The priority.
klao@172
   209
    void set(const Item &i, const Prio &p) {
jacint@221
   210
      int idx = iim[i];
klao@37
   211
      if( idx < 0 ) {
klao@172
   212
	push(i,p);
klao@37
   213
      }
klao@172
   214
      else if( comp(p, data[idx].second) ) {
klao@172
   215
	bubble_up(idx, PairType(i,p));
klao@37
   216
      }
klao@37
   217
      else {
klao@172
   218
	bubble_down(idx, PairType(i,p), data.size());
klao@37
   219
      }
klao@37
   220
    }
klao@37
   221
deba@1331
   222
    /// \brief Decreases the priority of \c i to \c p.
jacint@1270
   223
deba@1331
   224
    /// This method decreases the priority of item \c i to \c p.
deba@1331
   225
    /// \pre \c i must be stored in the heap with priority at least \c
deba@1331
   226
    /// p relative to \c Compare.
deba@1331
   227
    /// \param i The item.
deba@1331
   228
    /// \param p The priority.
klao@172
   229
    void decrease(const Item &i, const Prio &p) {
jacint@221
   230
      int idx = iim[i];
klao@172
   231
      bubble_up(idx, PairType(i,p));
klao@37
   232
    }
jacint@1270
   233
    
deba@1331
   234
    /// \brief Increases the priority of \c i to \c p.
deba@1331
   235
    ///
deba@1331
   236
    /// This method sets the priority of item \c i to \c p. 
deba@1331
   237
    /// \pre \c i must be stored in the heap with priority at most \c
deba@1331
   238
    /// p relative to \c Compare.
deba@1331
   239
    /// \param i The item.
deba@1331
   240
    /// \param p The priority.
klao@172
   241
    void increase(const Item &i, const Prio &p) {
jacint@221
   242
      int idx = iim[i];
klao@172
   243
      bubble_down(idx, PairType(i,p), data.size());
klao@37
   244
    }
klao@37
   245
deba@1331
   246
    /// \brief Returns if \c item is in, has already been in, or has 
deba@1331
   247
    /// never been in the heap.
deba@1331
   248
    ///
deba@1331
   249
    /// This method returns PRE_HEAP if \c item has never been in the
deba@1331
   250
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1331
   251
    /// otherwise. In the latter case it is possible that \c item will
deba@1331
   252
    /// get back to the heap again.
deba@1331
   253
    /// \param i The item.
klao@172
   254
    state_enum state(const Item &i) const {
jacint@221
   255
      int s = iim[i];
klao@39
   256
      if( s>=0 )
klao@39
   257
	s=0;
klao@39
   258
      return state_enum(s);
klao@39
   259
    }
klao@39
   260
klao@37
   261
  }; // class BinHeap
klao@37
   262
klao@37
   263
  
klao@37
   264
  template <typename K, typename V, typename M, typename C>
klao@37
   265
  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
klao@37
   266
    int par = parent(hole);
klao@37
   267
    while( hole>0 && less(p,data[par]) ) {
klao@37
   268
      move(data[par],hole);
klao@37
   269
      hole = par;
klao@37
   270
      par = parent(hole);
klao@37
   271
    }
klao@37
   272
    move(p, hole);
klao@37
   273
    return hole;
klao@37
   274
  }
klao@37
   275
klao@37
   276
  template <typename K, typename V, typename M, typename C>
klao@37
   277
  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
klao@37
   278
    int child = second_child(hole);
klao@37
   279
    while(child < length) {
klao@37
   280
      if( less(data[child-1], data[child]) ) {
klao@37
   281
	--child;
klao@37
   282
      }
klao@37
   283
      if( !less(data[child], p) )
klao@37
   284
	goto ok;
klao@37
   285
      move(data[child], hole);
klao@37
   286
      hole = child;
klao@37
   287
      child = second_child(hole);
klao@37
   288
    }
klao@37
   289
    child--;
klao@37
   290
    if( child<length && less(data[child], p) ) {
klao@37
   291
      move(data[child], hole);
klao@37
   292
      hole=child;
klao@37
   293
    }
klao@37
   294
  ok:
klao@37
   295
    move(p, hole);
klao@37
   296
    return hole;
klao@37
   297
  }
klao@37
   298
alpar@430
   299
  ///@}
alpar@430
   300
alpar@921
   301
} // namespace lemon
klao@37
   302
alpar@921
   303
#endif // LEMON_BIN_HEAP_H