lemon/radix_heap.h
author alpar
Thu, 30 Mar 2006 09:42:05 +0000
changeset 2022 0f3367da6104
parent 1906 7fa90b66ca9e
child 2050 d9a221218ea4
permissions -rw-r--r--
docfix
deba@1186
     1
/* -*- C++ -*-
deba@1186
     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).
deba@1186
     8
 *
deba@1186
     9
 * Permission to use, modify and distribute this software is granted
deba@1186
    10
 * provided that this copyright notice appears in all copies. For
deba@1186
    11
 * precise terms see the accompanying LICENSE file.
deba@1186
    12
 *
deba@1186
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1186
    14
 * express or implied, and with no claim as to its suitability for any
deba@1186
    15
 * purpose.
deba@1186
    16
 *
deba@1186
    17
 */
deba@1186
    18
deba@1186
    19
#ifndef LEMON_RADIX_HEAP_H
deba@1186
    20
#define LEMON_RADIX_HEAP_H
deba@1186
    21
deba@1186
    22
///\ingroup auxdat
deba@1186
    23
///\file
deba@1186
    24
///\brief Radix Heap implementation.
deba@1186
    25
deba@1186
    26
#include <vector>
deba@1186
    27
#include <lemon/error.h>
deba@1186
    28
deba@1186
    29
namespace lemon {
deba@1186
    30
deba@1331
    31
  /// \brief Exception thrown by RadixHeap.
deba@1331
    32
  ///  
deba@1331
    33
  /// This Exception is thrown when a smaller priority
deba@1331
    34
  /// is inserted into the \e RadixHeap then the last time erased.
deba@1331
    35
  /// \see RadixHeap
deba@1331
    36
  /// \author Balazs Dezso
deba@1186
    37
deba@1331
    38
  class UnderFlowPriorityError : public RuntimeError {
deba@1186
    39
  public:
deba@1186
    40
    virtual const char* exceptionName() const {
deba@1331
    41
      return "lemon::UnderFlowPriorityError";
deba@1186
    42
    }  
deba@1186
    43
  };
deba@1186
    44
deba@1717
    45
  /// \ingroup auxdata
deba@1717
    46
  ///
deba@1331
    47
  /// \brief A Radix Heap implementation.
deba@1331
    48
  ///
deba@1331
    49
  /// This class implements the \e radix \e heap data structure. A \e heap
deba@1331
    50
  /// is a data structure for storing items with specified values called \e
deba@1331
    51
  /// priorities in such a way that finding the item with minimum priority is
deba@1331
    52
  /// efficient. This heap type can store only items with \e int priority.
deba@1331
    53
  /// In a heap one can change the priority of an item, add or erase an 
deba@1331
    54
  /// item, but the priority cannot be decreased under the last removed 
deba@1331
    55
  /// item's priority.
deba@1331
    56
  ///
deba@1331
    57
  /// \param _Item Type of the items to be stored.  
deba@1331
    58
  /// \param _ItemIntMap A read and writable Item int map, used internally
deba@1331
    59
  /// to handle the cross references.
deba@1331
    60
  ///
deba@1331
    61
  /// \see BinHeap
deba@1331
    62
  /// \see Dijkstra
deba@1331
    63
  /// \author Balazs Dezso
deba@1331
    64
deba@1186
    65
  template <typename _Item, typename _ItemIntMap>
deba@1186
    66
  class RadixHeap {
deba@1186
    67
deba@1186
    68
  public:
deba@1186
    69
    typedef _Item Item;
deba@1186
    70
    typedef int Prio;
deba@1186
    71
    typedef _ItemIntMap ItemIntMap;
deba@1186
    72
deba@1331
    73
    /// \brief Type to represent the items states.
deba@1186
    74
    ///
deba@1331
    75
    /// Each Item element have a state associated to it. It may be "in heap",
alpar@1336
    76
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@1331
    77
    /// heap's point of view, but may be useful to the user.
deba@1331
    78
    ///
alpar@1336
    79
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@1331
    80
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@1186
    81
    enum state_enum {
deba@1186
    82
      IN_HEAP = 0,
deba@1186
    83
      PRE_HEAP = -1,
deba@1186
    84
      POST_HEAP = -2
deba@1186
    85
    };
deba@1186
    86
deba@1186
    87
  private:
deba@1186
    88
    
deba@1186
    89
    struct RadixItem {
deba@1186
    90
      int prev, next, box;
deba@1186
    91
      Item item;
deba@1186
    92
      int prio;
deba@1186
    93
      RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {}
deba@1186
    94
    };
deba@1186
    95
deba@1186
    96
    struct RadixBox {
deba@1186
    97
      int first;
deba@1186
    98
      int min, size;
deba@1186
    99
      RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
deba@1186
   100
    };
deba@1186
   101
deba@1186
   102
    std::vector<RadixItem> data;
deba@1186
   103
    std::vector<RadixBox> boxes;
deba@1186
   104
deba@1186
   105
    ItemIntMap &iim;
deba@1186
   106
deba@1186
   107
deba@1186
   108
  public:
deba@1331
   109
    /// \brief The constructor.
deba@1331
   110
    ///
deba@1331
   111
    /// The constructor.
deba@1331
   112
    ///
deba@1331
   113
    /// \param _iim It should be given to the constructor, since it is used
deba@1331
   114
    /// internally to handle the cross references. The value of the map
deba@1331
   115
    /// should be PRE_HEAP (-1) for each element.
deba@1331
   116
    ///
deba@1717
   117
    /// \param minimal The initial minimal value of the heap.
deba@1331
   118
    /// \param capacity It determines the initial capacity of the heap. 
deba@1717
   119
    RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) 
deba@1717
   120
      : iim(_iim) {
deba@1717
   121
      boxes.push_back(RadixBox(minimal, 1));
deba@1717
   122
      boxes.push_back(RadixBox(minimal + 1, 1));
deba@1717
   123
      while (lower(boxes.size() - 1, capacity + minimal - 1)) {
deba@1186
   124
	extend();
deba@1186
   125
      }
deba@1186
   126
    }
deba@1186
   127
deba@1331
   128
    /// The number of items stored in the heap.
deba@1331
   129
    ///
deba@1331
   130
    /// \brief Returns the number of items stored in the heap.
deba@1186
   131
    int size() const { return data.size(); }
deba@1331
   132
    /// \brief Checks if the heap stores no items.
deba@1331
   133
    ///
deba@1331
   134
    /// Returns \c true if and only if the heap stores no items.
deba@1186
   135
    bool empty() const { return data.empty(); }
deba@1186
   136
deba@1717
   137
    /// \brief Make empty this heap.
deba@1717
   138
    /// 
deba@1717
   139
    /// Make empty this heap.
deba@1717
   140
    void clear(int minimal = 0, int capacity = 0) { 
deba@1717
   141
      for (int i = 0; i < (int)data.size(); ++i) {
deba@1717
   142
	iim[data[i].item] = -2;
deba@1717
   143
      }
deba@1717
   144
      data.clear(); boxes.clear(); 
deba@1717
   145
      boxes.push_back(RadixBox(minimal, 1));
deba@1717
   146
      boxes.push_back(RadixBox(minimal + 1, 1));
deba@1717
   147
      while (lower(boxes.size() - 1, capacity + minimal - 1)) {
deba@1717
   148
	extend();
deba@1717
   149
      }
deba@1717
   150
    }
deba@1717
   151
deba@1186
   152
  private:
deba@1186
   153
deba@1186
   154
    bool upper(int box, Prio prio) {
deba@1186
   155
      return prio < boxes[box].min;
deba@1186
   156
    }
deba@1186
   157
deba@1186
   158
    bool lower(int box, Prio prio) {
deba@1186
   159
      return prio >= boxes[box].min + boxes[box].size;
deba@1186
   160
    }
deba@1186
   161
deba@1186
   162
    /// \brief Remove item from the box list.
deba@1186
   163
    void remove(int index) {
deba@1186
   164
      if (data[index].prev >= 0) {
deba@1186
   165
	data[data[index].prev].next = data[index].next;
deba@1186
   166
      } else {
deba@1186
   167
	boxes[data[index].box].first = data[index].next;
deba@1186
   168
      }
deba@1186
   169
      if (data[index].next >= 0) {
deba@1186
   170
	data[data[index].next].prev = data[index].prev;
deba@1186
   171
      }
deba@1186
   172
    }
deba@1186
   173
deba@1186
   174
    /// \brief Insert item into the box list.
deba@1186
   175
    void insert(int box, int index) {
deba@1186
   176
      if (boxes[box].first == -1) {
deba@1186
   177
	boxes[box].first = index;
deba@1186
   178
	data[index].next = data[index].prev = -1;
deba@1186
   179
      } else {
deba@1186
   180
	data[index].next = boxes[box].first;
deba@1186
   181
	data[boxes[box].first].prev = index;
deba@1186
   182
	data[index].prev = -1;
deba@1186
   183
	boxes[box].first = index;
deba@1186
   184
      }
deba@1186
   185
      data[index].box = box;
deba@1186
   186
    }
deba@1186
   187
deba@1186
   188
    /// \brief Add a new box to the box list.
deba@1186
   189
    void extend() {
deba@1186
   190
      int min = boxes.back().min + boxes.back().size;
deba@1186
   191
      int size = 2 * boxes.back().size;
deba@1186
   192
      boxes.push_back(RadixBox(min, size));
deba@1186
   193
    }
deba@1186
   194
deba@1186
   195
    /// \brief Move an item up into the proper box.
deba@1186
   196
    void bubble_up(int index) {
deba@1205
   197
      if (!lower(data[index].box, data[index].prio)) return;
deba@1186
   198
      remove(index);
deba@1186
   199
      int box = findUp(data[index].box, data[index].prio);
deba@1186
   200
      insert(box, index);      
deba@1186
   201
    }
deba@1186
   202
deba@1186
   203
    /// \brief Find up the proper box for the item with the given prio.
deba@1186
   204
    int findUp(int start, int prio) {
deba@1186
   205
      while (lower(start, prio)) {
deba@1186
   206
	if (++start == (int)boxes.size()) {
deba@1186
   207
	  extend();
deba@1186
   208
	}
deba@1186
   209
      }
deba@1186
   210
      return start;
deba@1186
   211
    }
deba@1186
   212
deba@1186
   213
    /// \brief Move an item down into the proper box.
deba@1186
   214
    void bubble_down(int index) {
deba@1186
   215
      if (!upper(data[index].box, data[index].prio)) return;
deba@1186
   216
      remove(index);
deba@1186
   217
      int box = findDown(data[index].box, data[index].prio);
deba@1186
   218
      insert(box, index);
deba@1186
   219
    }
deba@1186
   220
deba@1186
   221
    /// \brief Find up the proper box for the item with the given prio.
deba@1186
   222
    int findDown(int start, int prio) {
deba@1186
   223
      while (upper(start, prio)) {
deba@1331
   224
	if (--start < 0) throw UnderFlowPriorityError();
deba@1186
   225
      }
deba@1186
   226
      return start;
deba@1186
   227
    }
deba@1186
   228
deba@1186
   229
    /// \brief Find the first not empty box.
deba@1186
   230
    int findFirst() {
deba@1186
   231
      int first = 0;
deba@1186
   232
      while (boxes[first].first == -1) ++first;
deba@1186
   233
      return first;
deba@1186
   234
    }
deba@1186
   235
deba@1186
   236
    /// \brief Gives back the minimal prio of the box.
deba@1186
   237
    int minValue(int box) {
deba@1186
   238
      int min = data[boxes[box].first].prio;
deba@1186
   239
      for (int k = boxes[box].first; k != -1; k = data[k].next) {
deba@1186
   240
	if (data[k].prio < min) min = data[k].prio;
deba@1186
   241
      }
deba@1186
   242
      return min;
deba@1186
   243
    }
deba@1186
   244
deba@1186
   245
    /// \brief Rearrange the items of the heap and makes the 
deba@1186
   246
    /// first box not empty.
deba@1186
   247
    void moveDown() {
deba@1186
   248
      int box = findFirst();
deba@1186
   249
      if (box == 0) return;
deba@1186
   250
      int min = minValue(box);
deba@1186
   251
      for (int i = 0; i <= box; ++i) {
deba@1186
   252
	boxes[i].min = min;
deba@1186
   253
	min += boxes[i].size;
deba@1186
   254
      }
deba@1186
   255
      int curr = boxes[box].first, next;
deba@1186
   256
      while (curr != -1) {
deba@1186
   257
	next = data[curr].next;
deba@1186
   258
	bubble_down(curr);
deba@1186
   259
	curr = next;
deba@1186
   260
      }      
deba@1186
   261
    }
deba@1186
   262
deba@1186
   263
    void relocate_last(int index) {
deba@1186
   264
      if (index != (int)data.size() - 1) {
deba@1186
   265
	data[index] = data.back();
deba@1186
   266
	if (data[index].prev != -1) {
deba@1186
   267
	  data[data[index].prev].next = index;
deba@1186
   268
	} else {
deba@1186
   269
	  boxes[data[index].box].first = index;
deba@1186
   270
	}
deba@1186
   271
	if (data[index].next != -1) {
deba@1186
   272
	  data[data[index].next].prev = index;
deba@1186
   273
	}
deba@1186
   274
	iim[data[index].item] = index;
deba@1186
   275
      }
deba@1186
   276
      data.pop_back();
deba@1186
   277
    }
deba@1186
   278
deba@1186
   279
  public:
deba@1186
   280
deba@1717
   281
    /// \brief Insert an item into the heap with the given priority.
deba@1331
   282
    ///    
deba@1331
   283
    /// Adds \c i to the heap with priority \c p. 
deba@1331
   284
    /// \param i The item to insert.
deba@1331
   285
    /// \param p The priority of the item.
deba@1186
   286
    void push(const Item &i, const Prio &p) {
deba@1186
   287
      int n = data.size();
deba@1186
   288
      iim.set(i, n);
deba@1186
   289
      data.push_back(RadixItem(i, p));
deba@1186
   290
      while (lower(boxes.size() - 1, p)) {
deba@1186
   291
	extend();
deba@1186
   292
      }
deba@1186
   293
      int box = findDown(boxes.size() - 1, p);
deba@1186
   294
      insert(box, n);
deba@1186
   295
    }
deba@1186
   296
deba@1331
   297
    /// \brief Returns the item with minimum priority.
deba@1331
   298
    ///
deba@1331
   299
    /// This method returns the item with minimum priority.  
deba@1331
   300
    /// \pre The heap must be nonempty.  
deba@1186
   301
    Item top() const {
deba@1717
   302
      const_cast<RadixHeap<Item, ItemIntMap>&>(*this).moveDown();
deba@1186
   303
      return data[boxes[0].first].item;
deba@1186
   304
    }
deba@1186
   305
deba@1331
   306
    /// \brief Returns the minimum priority.
deba@1331
   307
    ///
deba@1331
   308
    /// It returns the minimum priority.
deba@1331
   309
    /// \pre The heap must be nonempty.
deba@1186
   310
    Prio prio() const {
deba@1717
   311
      const_cast<RadixHeap<Item, ItemIntMap>&>(*this).moveDown();
deba@1186
   312
      return data[boxes[0].first].prio;
deba@1186
   313
     }
deba@1186
   314
deba@1331
   315
    /// \brief Deletes the item with minimum priority.
deba@1331
   316
    ///
deba@1331
   317
    /// This method deletes the item with minimum priority.
deba@1331
   318
    /// \pre The heap must be non-empty.  
deba@1186
   319
    void pop() {
deba@1186
   320
      moveDown();
deba@1186
   321
      int index = boxes[0].first;
deba@1186
   322
      iim[data[index].item] = POST_HEAP;
deba@1186
   323
      remove(index);
deba@1186
   324
      relocate_last(index);
deba@1186
   325
    }
deba@1186
   326
deba@1331
   327
    /// \brief Deletes \c i from the heap.
deba@1331
   328
    ///
deba@1331
   329
    /// This method deletes item \c i from the heap, if \c i was
deba@1331
   330
    /// already stored in the heap.
deba@1331
   331
    /// \param i The item to erase. 
deba@1186
   332
    void erase(const Item &i) {
deba@1186
   333
      int index = iim[i];
deba@1186
   334
      iim[i] = POST_HEAP;
deba@1186
   335
      remove(index);
deba@1186
   336
      relocate_last(index);
deba@1186
   337
   }
deba@1186
   338
deba@1331
   339
    /// \brief Returns the priority of \c i.
deba@1331
   340
    ///
deba@1331
   341
    /// This function returns the priority of item \c i.  
deba@1331
   342
    /// \pre \c i must be in the heap.
deba@1331
   343
    /// \param i The item.
deba@1186
   344
    Prio operator[](const Item &i) const {
deba@1186
   345
      int idx = iim[i];
deba@1186
   346
      return data[idx].prio;
deba@1186
   347
    }
deba@1186
   348
deba@1331
   349
    /// \brief \c i gets to the heap with priority \c p independently 
deba@1331
   350
    /// if \c i was already there.
deba@1331
   351
    ///
deba@1331
   352
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1331
   353
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1331
   354
    /// It may throw an \e UnderFlowPriorityException. 
deba@1331
   355
    /// \param i The item.
deba@1331
   356
    /// \param p The priority.
deba@1186
   357
    void set(const Item &i, const Prio &p) {
deba@1186
   358
      int idx = iim[i];
deba@1186
   359
      if( idx < 0 ) {
deba@1186
   360
	push(i, p);
deba@1186
   361
      }
deba@1186
   362
      else if( p >= data[idx].prio ) {
deba@1186
   363
	data[idx].prio = p;
deba@1186
   364
	bubble_up(idx);
deba@1186
   365
      } else {
deba@1186
   366
	data[idx].prio = p;
deba@1186
   367
	bubble_down(idx);
deba@1186
   368
      }
deba@1186
   369
    }
deba@1186
   370
deba@1331
   371
deba@1331
   372
    /// \brief Decreases the priority of \c i to \c p.
deba@1331
   373
    ///
deba@1331
   374
    /// This method decreases the priority of item \c i to \c p.
deba@1331
   375
    /// \pre \c i must be stored in the heap with priority at least \c p, and
deba@1758
   376
    /// \c should be greater or equal to the last removed item's priority.
deba@1331
   377
    /// \param i The item.
deba@1331
   378
    /// \param p The priority.
deba@1186
   379
    void decrease(const Item &i, const Prio &p) {
deba@1186
   380
      int idx = iim[i];
deba@1186
   381
      data[idx].prio = p;
deba@1186
   382
      bubble_down(idx);
deba@1186
   383
    }
deba@1186
   384
deba@1331
   385
    /// \brief Increases the priority of \c i to \c p.
deba@1331
   386
    ///
deba@1331
   387
    /// This method sets the priority of item \c i to \c p. 
deba@1758
   388
    /// \pre \c i must be stored in the heap with priority at most \c p
deba@1331
   389
    /// \param i The item.
deba@1331
   390
    /// \param p The priority.
deba@1186
   391
    void increase(const Item &i, const Prio &p) {
deba@1186
   392
      int idx = iim[i];
deba@1186
   393
      data[idx].prio = p;
deba@1186
   394
      bubble_up(idx);
deba@1186
   395
    }
deba@1186
   396
deba@1331
   397
    /// \brief Returns if \c item is in, has already been in, or has 
deba@1331
   398
    /// never been in the heap.
deba@1331
   399
    ///
deba@1331
   400
    /// This method returns PRE_HEAP if \c item has never been in the
deba@1331
   401
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1331
   402
    /// otherwise. In the latter case it is possible that \c item will
deba@1331
   403
    /// get back to the heap again.
deba@1331
   404
    /// \param i The item.
deba@1186
   405
    state_enum state(const Item &i) const {
deba@1186
   406
      int s = iim[i];
deba@1186
   407
      if( s >= 0 ) s = 0;
deba@1186
   408
      return state_enum(s);
deba@1186
   409
    }
deba@1186
   410
deba@1902
   411
    /// \brief Sets the state of the \c item in the heap.
deba@1902
   412
    ///
deba@1902
   413
    /// Sets the state of the \c item in the heap. It can be used to
deba@1902
   414
    /// manually clear the heap when it is important to achive the
deba@1902
   415
    /// better time complexity.
deba@1902
   416
    /// \param i The item.
deba@1902
   417
    /// \param st The state. It should not be \c IN_HEAP. 
deba@1902
   418
    void state(const Item& i, state_enum st) {
deba@1902
   419
      switch (st) {
deba@1902
   420
      case POST_HEAP:
deba@1902
   421
      case PRE_HEAP:
deba@1902
   422
        if (state(i) == IN_HEAP) {
deba@1902
   423
          erase(i);
deba@1902
   424
        }
deba@1903
   425
        iim[i] = st;
deba@1902
   426
        break;
deba@1906
   427
      case IN_HEAP:
deba@1906
   428
        break;
deba@1902
   429
      }
deba@1902
   430
    }
deba@1902
   431
deba@1186
   432
  }; // class RadixHeap
deba@1186
   433
deba@1186
   434
} // namespace lemon
deba@1186
   435
deba@1186
   436
#endif // LEMON_RADIX_HEAP_H