src/lemon/bin_heap.h
author athos
Thu, 07 Apr 2005 10:44:32 +0000
changeset 1316 daaf6b5c28d6
parent 1191 c988f12c6c0c
child 1331 7e93d3f0406d
permissions -rw-r--r--
Makefile has changed.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/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@906
     5
 * (Egervary Combinatorial Optimization Research Group, 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
klao@37
    62
    /**
klao@172
    63
     * Each Item element have a state associated to it. It may be "in heap",
klao@37
    64
     * "pre heap" or "post heap". The later two are indifferent from the
klao@37
    65
     * heap's point of view, but may be useful to the user.
klao@37
    66
     *
klao@172
    67
     * The ItemIntMap _should_ be initialized in such way, that it maps
klao@37
    68
     * PRE_HEAP (-1) to any element to be put in the heap...
klao@37
    69
     */
klao@274
    70
    ///\todo it is used nowhere
klao@274
    71
    ///
klao@39
    72
    enum state_enum {
klao@37
    73
      IN_HEAP = 0,
klao@37
    74
      PRE_HEAP = -1,
klao@37
    75
      POST_HEAP = -2
klao@37
    76
    };
klao@37
    77
klao@37
    78
  private:
klao@37
    79
    std::vector<PairType> data;
klao@37
    80
    Compare comp;
klao@37
    81
    // FIXME: jo ez igy???
klao@172
    82
    ItemIntMap &iim;
klao@37
    83
klao@37
    84
  public:
jacint@1270
    85
    ///The constructor
jacint@1270
    86
jacint@1270
    87
    /**
jacint@1270
    88
       \c _iim should be given to the constructor, since it is used
jacint@1270
    89
       internally to handle the cross references.
jacint@1270
    90
    */
deba@1185
    91
    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
jacint@1270
    92
    
jacint@1270
    93
    ///The constructor
jacint@1270
    94
jacint@1270
    95
    /**
jacint@1270
    96
       \c _iim should be given to the constructor, since it is used
jacint@1270
    97
       internally to handle the cross references. \c _comp is an
jacint@1270
    98
       object for ordering of the priorities.
jacint@1270
    99
    */
deba@1191
   100
    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
deba@1185
   101
      : iim(_iim), comp(_comp) {}
klao@37
   102
klao@37
   103
jacint@1270
   104
    ///The number of items stored in the heap.
jacint@1270
   105
jacint@1270
   106
    /**
jacint@1270
   107
       Returns the number of items stored in the heap.
jacint@1270
   108
    */
klao@37
   109
    int size() const { return data.size(); }
jacint@1270
   110
    
jacint@1270
   111
    ///Checks if the heap stores no items.
jacint@1270
   112
    
jacint@1270
   113
    /**
jacint@1270
   114
       Returns \c true if and only if the heap stores no items.
jacint@1270
   115
    */
klao@41
   116
    bool empty() const { return data.empty(); }
klao@37
   117
klao@37
   118
  private:
klao@37
   119
    static int parent(int i) { return (i-1)/2; }
klao@37
   120
    static int second_child(int i) { return 2*i+2; }
klao@214
   121
    bool less(const PairType &p1, const PairType &p2) const {
klao@37
   122
      return comp(p1.second, p2.second);
klao@37
   123
    }
klao@37
   124
klao@37
   125
    int bubble_up(int hole, PairType p);
klao@37
   126
    int bubble_down(int hole, PairType p, int length);
klao@37
   127
klao@37
   128
    void move(const PairType &p, int i) {
klao@37
   129
      data[i] = p;
klao@172
   130
      iim.set(p.first, i);
klao@37
   131
    }
klao@37
   132
klao@41
   133
    void rmidx(int h) {
klao@41
   134
      int n = data.size()-1;
klao@41
   135
      if( h>=0 && h<=n ) {
klao@172
   136
	iim.set(data[h].first, POST_HEAP);
klao@41
   137
	if ( h<n ) {
klao@41
   138
	  bubble_down(h, data[n], n);
klao@41
   139
	}
klao@41
   140
	data.pop_back();
klao@41
   141
      }
klao@41
   142
    }
klao@41
   143
klao@37
   144
  public:
jacint@1270
   145
    ///Adds \c p.first to the heap with priority \c p.second.
jacint@1270
   146
    
jacint@1270
   147
    /**
jacint@1270
   148
       Adds \c p.first to the heap with priority \c p.second.
jacint@1270
   149
       \c p.first must not be stored in the heap. 
jacint@1270
   150
    */
klao@37
   151
    void push(const PairType &p) {
klao@37
   152
      int n = data.size();
klao@37
   153
      data.resize(n+1);
klao@37
   154
      bubble_up(n, p);
klao@37
   155
    }
jacint@1270
   156
jacint@1270
   157
    ///Adds \c i to the heap with priority \c p. 
jacint@1270
   158
    
jacint@1270
   159
    /**
jacint@1270
   160
       Adds \c i to the heap with priority \c p. 
jacint@1270
   161
       \pre \c i must not be stored in the heap. 
jacint@1270
   162
    */
klao@172
   163
    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
klao@37
   164
jacint@1270
   165
    ///Returns the item with minimum priority relative to \c Compare.
jacint@1270
   166
    
jacint@1270
   167
    /**
jacint@1270
   168
       This method returns the item with minimum priority relative to \c
jacint@1270
   169
       Compare.  
jacint@1270
   170
       \pre The heap must be nonempty.  
jacint@1270
   171
    */
klao@172
   172
    Item top() const {
klao@37
   173
      return data[0].first;
klao@37
   174
    }
jacint@1270
   175
jacint@1270
   176
    ///Returns the minimum priority relative to \c Compare.
jacint@1270
   177
jacint@1270
   178
    /**
jacint@1270
   179
       It returns the minimum priority relative to \c Compare.
jacint@1270
   180
       \pre The heap must be nonempty.
jacint@1270
   181
    */
klao@274
   182
    Prio prio() const {
klao@37
   183
      return data[0].second;
klao@37
   184
    }
klao@37
   185
jacint@1270
   186
    ///Deletes the item with minimum priority relative to \c Compare.
jacint@1270
   187
jacint@1270
   188
    /**
jacint@1270
   189
    This method deletes the item with minimum priority relative to \c
jacint@1270
   190
    Compare from the heap.  
jacint@1270
   191
    \pre The heap must be non-empty.  
jacint@1270
   192
    */
klao@37
   193
    void pop() {
klao@41
   194
      rmidx(0);
klao@41
   195
    }
klao@41
   196
jacint@1270
   197
    ///Deletes \c i from the heap.
jacint@1270
   198
jacint@1270
   199
    /**
jacint@1270
   200
       This method deletes item \c i from the heap, if \c i was
jacint@1270
   201
       already stored in the heap. 
jacint@1270
   202
    */
klao@172
   203
    void erase(const Item &i) {
jacint@221
   204
      rmidx(iim[i]);
klao@37
   205
    }
klao@37
   206
jacint@1270
   207
    
jacint@1270
   208
    ///Returns the priority of \c i.
jacint@1270
   209
jacint@1270
   210
    /**
jacint@1270
   211
       This function returns the priority of item \c i.  
jacint@1270
   212
       \pre \c i must be in the heap.
jacint@1270
   213
    */
klao@274
   214
    Prio operator[](const Item &i) const {
jacint@221
   215
      int idx = iim[i];
klao@37
   216
      return data[idx].second;
klao@37
   217
    }
klao@274
   218
jacint@1270
   219
    ///\c i gets to the heap with priority \c p independently if \c i was already there.
jacint@1270
   220
jacint@1270
   221
    /**
jacint@1270
   222
       This method calls \ref push(\c i, \c p) if \c i is not stored
jacint@1270
   223
       in the heap and sets the priority of \c i to \c p otherwise.
jacint@1270
   224
    */
klao@172
   225
    void set(const Item &i, const Prio &p) {
jacint@221
   226
      int idx = iim[i];
klao@37
   227
      if( idx < 0 ) {
klao@172
   228
	push(i,p);
klao@37
   229
      }
klao@172
   230
      else if( comp(p, data[idx].second) ) {
klao@172
   231
	bubble_up(idx, PairType(i,p));
klao@37
   232
      }
klao@37
   233
      else {
klao@172
   234
	bubble_down(idx, PairType(i,p), data.size());
klao@37
   235
      }
klao@37
   236
    }
klao@37
   237
jacint@1270
   238
    ///Decreases the priority of \c i to \c p.
jacint@1270
   239
jacint@1270
   240
    /**
jacint@1270
   241
       This method decreases the priority of item \c i to \c p.
jacint@1270
   242
       \pre \c i must be stored in the heap with priority at least \c
jacint@1270
   243
       p relative to \c Compare.
jacint@1270
   244
    */
klao@172
   245
    void decrease(const Item &i, const Prio &p) {
jacint@221
   246
      int idx = iim[i];
klao@172
   247
      bubble_up(idx, PairType(i,p));
klao@37
   248
    }
jacint@1270
   249
    
jacint@1270
   250
    ///Increases the priority of \c i to \c p.
jacint@1270
   251
jacint@1270
   252
    /**
jacint@1270
   253
       This method sets the priority of item \c i to \c p. 
jacint@1270
   254
       \pre \c i must be stored in the heap with priority at most \c
jacint@1270
   255
       p relative to \c Compare.
jacint@1270
   256
    */
klao@172
   257
    void increase(const Item &i, const Prio &p) {
jacint@221
   258
      int idx = iim[i];
klao@172
   259
      bubble_down(idx, PairType(i,p), data.size());
klao@37
   260
    }
klao@37
   261
jacint@1270
   262
    ///Returns if \c item is in, has already been in, or has never been in the heap.
jacint@1270
   263
jacint@1270
   264
    /**
jacint@1270
   265
       This method returns PRE_HEAP if \c item has never been in the
jacint@1270
   266
       heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
jacint@1270
   267
       otherwise. In the latter case it is possible that \c item will
jacint@1270
   268
       get back to the heap again.
jacint@1270
   269
    */
klao@172
   270
    state_enum state(const Item &i) const {
jacint@221
   271
      int s = iim[i];
klao@39
   272
      if( s>=0 )
klao@39
   273
	s=0;
klao@39
   274
      return state_enum(s);
klao@39
   275
    }
klao@39
   276
klao@37
   277
  }; // class BinHeap
klao@37
   278
klao@37
   279
  
klao@37
   280
  template <typename K, typename V, typename M, typename C>
klao@37
   281
  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
klao@37
   282
    int par = parent(hole);
klao@37
   283
    while( hole>0 && less(p,data[par]) ) {
klao@37
   284
      move(data[par],hole);
klao@37
   285
      hole = par;
klao@37
   286
      par = parent(hole);
klao@37
   287
    }
klao@37
   288
    move(p, hole);
klao@37
   289
    return hole;
klao@37
   290
  }
klao@37
   291
klao@37
   292
  template <typename K, typename V, typename M, typename C>
klao@37
   293
  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
klao@37
   294
    int child = second_child(hole);
klao@37
   295
    while(child < length) {
klao@37
   296
      if( less(data[child-1], data[child]) ) {
klao@37
   297
	--child;
klao@37
   298
      }
klao@37
   299
      if( !less(data[child], p) )
klao@37
   300
	goto ok;
klao@37
   301
      move(data[child], hole);
klao@37
   302
      hole = child;
klao@37
   303
      child = second_child(hole);
klao@37
   304
    }
klao@37
   305
    child--;
klao@37
   306
    if( child<length && less(data[child], p) ) {
klao@37
   307
      move(data[child], hole);
klao@37
   308
      hole=child;
klao@37
   309
    }
klao@37
   310
  ok:
klao@37
   311
    move(p, hole);
klao@37
   312
    return hole;
klao@37
   313
  }
klao@37
   314
alpar@430
   315
  ///@}
alpar@430
   316
alpar@921
   317
} // namespace lemon
klao@37
   318
alpar@921
   319
#endif // LEMON_BIN_HEAP_H