src/lemon/radix_heap.h
author deba
Sat, 09 Apr 2005 19:35:33 +0000
changeset 1333 2640cf6547ff
parent 1205 a9a3354b01d4
child 1336 fd5fd79123fd
permissions -rw-r--r--
Functionality changed:
The first map is the id map => The map named "id" is the id map

Documentation improvments
deba@1186
     1
/* -*- C++ -*-
deba@1331
     2
 * src/lemon/radix_heap.h - Part of LEMON, a generic C++ optimization library
deba@1186
     3
 *
deba@1186
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1186
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
deba@1186
     6
 *
deba@1186
     7
 * Permission to use, modify and distribute this software is granted
deba@1186
     8
 * provided that this copyright notice appears in all copies. For
deba@1186
     9
 * precise terms see the accompanying LICENSE file.
deba@1186
    10
 *
deba@1186
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1186
    12
 * express or implied, and with no claim as to its suitability for any
deba@1186
    13
 * purpose.
deba@1186
    14
 *
deba@1186
    15
 */
deba@1186
    16
deba@1186
    17
#ifndef LEMON_RADIX_HEAP_H
deba@1186
    18
#define LEMON_RADIX_HEAP_H
deba@1186
    19
deba@1186
    20
///\ingroup auxdat
deba@1186
    21
///\file
deba@1186
    22
///\brief Radix Heap implementation.
deba@1186
    23
///\todo It should be documented.
deba@1186
    24
deba@1186
    25
#include <vector>
deba@1186
    26
#include <lemon/error.h>
deba@1186
    27
deba@1186
    28
namespace lemon {
deba@1186
    29
deba@1186
    30
  /// \addtogroup auxdat
deba@1186
    31
  /// @{
deba@1186
    32
deba@1331
    33
  /// \brief Exception thrown by RadixHeap.
deba@1331
    34
  ///  
deba@1331
    35
  /// This Exception is thrown when a smaller priority
deba@1331
    36
  /// is inserted into the \e RadixHeap then the last time erased.
deba@1331
    37
  /// \see RadixHeap
deba@1331
    38
  /// \author Balazs Dezso
deba@1186
    39
deba@1331
    40
  class UnderFlowPriorityError : public RuntimeError {
deba@1186
    41
  public:
deba@1186
    42
    virtual const char* exceptionName() const {
deba@1331
    43
      return "lemon::UnderFlowPriorityError";
deba@1186
    44
    }  
deba@1186
    45
  };
deba@1186
    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",
deba@1331
    76
    /// "pre heap" or "post heap". The later two are indifferent from the
deba@1331
    77
    /// heap's point of view, but may be useful to the user.
deba@1331
    78
    ///
deba@1331
    79
    /// The ItemIntMap _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
    /// \param _iim should be given to the constructor, since it is used
deba@1331
   113
    /// internally to handle the cross references. The value of the map
deba@1331
   114
    /// should be PRE_HEAP (-1) for each element.
deba@1186
   115
    explicit RadixHeap(ItemIntMap &_iim) : iim(_iim) {
deba@1186
   116
      boxes.push_back(RadixBox(0, 1));
deba@1186
   117
      boxes.push_back(RadixBox(1, 1));
deba@1186
   118
    }
deba@1186
   119
deba@1331
   120
    /// \brief The constructor.
deba@1331
   121
    ///
deba@1331
   122
    /// The constructor.
deba@1331
   123
    ///
deba@1331
   124
    /// \param _iim It should be given to the constructor, since it is used
deba@1331
   125
    /// internally to handle the cross references. The value of the map
deba@1331
   126
    /// should be PRE_HEAP (-1) for each element.
deba@1331
   127
    ///
deba@1331
   128
    /// \param capacity It determines the initial capacity of the heap. 
deba@1186
   129
    RadixHeap(ItemIntMap &_iim, int capacity) : iim(_iim) {
deba@1186
   130
      boxes.push_back(RadixBox(0, 1));
deba@1186
   131
      boxes.push_back(RadixBox(1, 1));
deba@1186
   132
      while (upper(boxes.back(), capacity)) {
deba@1186
   133
	extend();
deba@1186
   134
      }
deba@1186
   135
    }
deba@1186
   136
deba@1331
   137
    /// The number of items stored in the heap.
deba@1331
   138
    ///
deba@1331
   139
    /// \brief Returns the number of items stored in the heap.
deba@1186
   140
    int size() const { return data.size(); }
deba@1331
   141
    /// \brief Checks if the heap stores no items.
deba@1331
   142
    ///
deba@1331
   143
    /// Returns \c true if and only if the heap stores no items.
deba@1186
   144
    bool empty() const { return data.empty(); }
deba@1186
   145
deba@1186
   146
  private:
deba@1186
   147
deba@1186
   148
    bool upper(int box, Prio prio) {
deba@1186
   149
      return prio < boxes[box].min;
deba@1186
   150
    }
deba@1186
   151
deba@1186
   152
    bool lower(int box, Prio prio) {
deba@1186
   153
      return prio >= boxes[box].min + boxes[box].size;
deba@1186
   154
    }
deba@1186
   155
deba@1186
   156
    /// \brief Remove item from the box list.
deba@1186
   157
    void remove(int index) {
deba@1186
   158
      if (data[index].prev >= 0) {
deba@1186
   159
	data[data[index].prev].next = data[index].next;
deba@1186
   160
      } else {
deba@1186
   161
	boxes[data[index].box].first = data[index].next;
deba@1186
   162
      }
deba@1186
   163
      if (data[index].next >= 0) {
deba@1186
   164
	data[data[index].next].prev = data[index].prev;
deba@1186
   165
      }
deba@1186
   166
    }
deba@1186
   167
deba@1186
   168
    /// \brief Insert item into the box list.
deba@1186
   169
    void insert(int box, int index) {
deba@1186
   170
      if (boxes[box].first == -1) {
deba@1186
   171
	boxes[box].first = index;
deba@1186
   172
	data[index].next = data[index].prev = -1;
deba@1186
   173
      } else {
deba@1186
   174
	data[index].next = boxes[box].first;
deba@1186
   175
	data[boxes[box].first].prev = index;
deba@1186
   176
	data[index].prev = -1;
deba@1186
   177
	boxes[box].first = index;
deba@1186
   178
      }
deba@1186
   179
      data[index].box = box;
deba@1186
   180
    }
deba@1186
   181
deba@1186
   182
    /// \brief Add a new box to the box list.
deba@1186
   183
    void extend() {
deba@1186
   184
      int min = boxes.back().min + boxes.back().size;
deba@1186
   185
      int size = 2 * boxes.back().size;
deba@1186
   186
      boxes.push_back(RadixBox(min, size));
deba@1186
   187
    }
deba@1186
   188
deba@1186
   189
    /// \brief Move an item up into the proper box.
deba@1186
   190
    void bubble_up(int index) {
deba@1205
   191
      if (!lower(data[index].box, data[index].prio)) return;
deba@1186
   192
      remove(index);
deba@1186
   193
      int box = findUp(data[index].box, data[index].prio);
deba@1186
   194
      insert(box, index);      
deba@1186
   195
    }
deba@1186
   196
deba@1186
   197
    /// \brief Find up the proper box for the item with the given prio.
deba@1186
   198
    int findUp(int start, int prio) {
deba@1186
   199
      while (lower(start, prio)) {
deba@1186
   200
	if (++start == (int)boxes.size()) {
deba@1186
   201
	  extend();
deba@1186
   202
	}
deba@1186
   203
      }
deba@1186
   204
      return start;
deba@1186
   205
    }
deba@1186
   206
deba@1186
   207
    /// \brief Move an item down into the proper box.
deba@1186
   208
    void bubble_down(int index) {
deba@1186
   209
      if (!upper(data[index].box, data[index].prio)) return;
deba@1186
   210
      remove(index);
deba@1186
   211
      int box = findDown(data[index].box, data[index].prio);
deba@1186
   212
      insert(box, index);
deba@1186
   213
    }
deba@1186
   214
deba@1186
   215
    /// \brief Find up the proper box for the item with the given prio.
deba@1186
   216
    int findDown(int start, int prio) {
deba@1186
   217
      while (upper(start, prio)) {
deba@1331
   218
	if (--start < 0) throw UnderFlowPriorityError();
deba@1186
   219
      }
deba@1186
   220
      return start;
deba@1186
   221
    }
deba@1186
   222
deba@1186
   223
    /// \brief Find the first not empty box.
deba@1186
   224
    int findFirst() {
deba@1186
   225
      int first = 0;
deba@1186
   226
      while (boxes[first].first == -1) ++first;
deba@1186
   227
      return first;
deba@1186
   228
    }
deba@1186
   229
deba@1186
   230
    /// \brief Gives back the minimal prio of the box.
deba@1186
   231
    int minValue(int box) {
deba@1186
   232
      int min = data[boxes[box].first].prio;
deba@1186
   233
      for (int k = boxes[box].first; k != -1; k = data[k].next) {
deba@1186
   234
	if (data[k].prio < min) min = data[k].prio;
deba@1186
   235
      }
deba@1186
   236
      return min;
deba@1186
   237
    }
deba@1186
   238
deba@1186
   239
    /// \brief Rearrange the items of the heap and makes the 
deba@1186
   240
    /// first box not empty.
deba@1186
   241
    void moveDown() {
deba@1186
   242
      int box = findFirst();
deba@1186
   243
      if (box == 0) return;
deba@1186
   244
      int min = minValue(box);
deba@1186
   245
      for (int i = 0; i <= box; ++i) {
deba@1186
   246
	boxes[i].min = min;
deba@1186
   247
	min += boxes[i].size;
deba@1186
   248
      }
deba@1186
   249
      int curr = boxes[box].first, next;
deba@1186
   250
      while (curr != -1) {
deba@1186
   251
	next = data[curr].next;
deba@1186
   252
	bubble_down(curr);
deba@1186
   253
	curr = next;
deba@1186
   254
      }      
deba@1186
   255
    }
deba@1186
   256
deba@1186
   257
    void relocate_last(int index) {
deba@1186
   258
      if (index != (int)data.size() - 1) {
deba@1186
   259
	data[index] = data.back();
deba@1186
   260
	if (data[index].prev != -1) {
deba@1186
   261
	  data[data[index].prev].next = index;
deba@1186
   262
	} else {
deba@1186
   263
	  boxes[data[index].box].first = index;
deba@1186
   264
	}
deba@1186
   265
	if (data[index].next != -1) {
deba@1186
   266
	  data[data[index].next].prev = index;
deba@1186
   267
	}
deba@1186
   268
	iim[data[index].item] = index;
deba@1186
   269
      }
deba@1186
   270
      data.pop_back();
deba@1186
   271
    }
deba@1186
   272
deba@1186
   273
  public:
deba@1186
   274
deba@1331
   275
    /// \brief Insert an item into the heap with the given heap.
deba@1331
   276
    ///    
deba@1331
   277
    /// Adds \c i to the heap with priority \c p. 
deba@1331
   278
    /// \param i The item to insert.
deba@1331
   279
    /// \param p The priority of the item.
deba@1186
   280
    void push(const Item &i, const Prio &p) {
deba@1186
   281
      int n = data.size();
deba@1186
   282
      iim.set(i, n);
deba@1186
   283
      data.push_back(RadixItem(i, p));
deba@1186
   284
      while (lower(boxes.size() - 1, p)) {
deba@1186
   285
	extend();
deba@1186
   286
      }
deba@1186
   287
      int box = findDown(boxes.size() - 1, p);
deba@1186
   288
      insert(box, n);
deba@1186
   289
    }
deba@1186
   290
deba@1331
   291
    /// \brief Returns the item with minimum priority.
deba@1331
   292
    ///
deba@1331
   293
    /// This method returns the item with minimum priority.  
deba@1331
   294
    /// \pre The heap must be nonempty.  
deba@1186
   295
    Item top() const {
deba@1186
   296
      const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
deba@1186
   297
      return data[boxes[0].first].item;
deba@1186
   298
    }
deba@1186
   299
deba@1331
   300
    /// \brief Returns the minimum priority.
deba@1331
   301
    ///
deba@1331
   302
    /// It returns the minimum priority.
deba@1331
   303
    /// \pre The heap must be nonempty.
deba@1186
   304
    Prio prio() const {
deba@1186
   305
      const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
deba@1186
   306
      return data[boxes[0].first].prio;
deba@1186
   307
     }
deba@1186
   308
deba@1331
   309
    /// \brief Deletes the item with minimum priority.
deba@1331
   310
    ///
deba@1331
   311
    /// This method deletes the item with minimum priority.
deba@1331
   312
    /// \pre The heap must be non-empty.  
deba@1186
   313
    void pop() {
deba@1186
   314
      moveDown();
deba@1186
   315
      int index = boxes[0].first;
deba@1186
   316
      iim[data[index].item] = POST_HEAP;
deba@1186
   317
      remove(index);
deba@1186
   318
      relocate_last(index);
deba@1186
   319
    }
deba@1186
   320
deba@1331
   321
    /// \brief Deletes \c i from the heap.
deba@1331
   322
    ///
deba@1331
   323
    /// This method deletes item \c i from the heap, if \c i was
deba@1331
   324
    /// already stored in the heap.
deba@1331
   325
    /// \param i The item to erase. 
deba@1186
   326
    void erase(const Item &i) {
deba@1186
   327
      int index = iim[i];
deba@1186
   328
      iim[i] = POST_HEAP;
deba@1186
   329
      remove(index);
deba@1186
   330
      relocate_last(index);
deba@1186
   331
   }
deba@1186
   332
deba@1331
   333
    /// \brief Returns the priority of \c i.
deba@1331
   334
    ///
deba@1331
   335
    /// This function returns the priority of item \c i.  
deba@1331
   336
    /// \pre \c i must be in the heap.
deba@1331
   337
    /// \param i The item.
deba@1186
   338
    Prio operator[](const Item &i) const {
deba@1186
   339
      int idx = iim[i];
deba@1186
   340
      return data[idx].prio;
deba@1186
   341
    }
deba@1186
   342
deba@1331
   343
    /// \brief \c i gets to the heap with priority \c p independently 
deba@1331
   344
    /// if \c i was already there.
deba@1331
   345
    ///
deba@1331
   346
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1331
   347
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1331
   348
    /// It may throw an \e UnderFlowPriorityException. 
deba@1331
   349
    /// \param i The item.
deba@1331
   350
    /// \param p The priority.
deba@1186
   351
    void set(const Item &i, const Prio &p) {
deba@1186
   352
      int idx = iim[i];
deba@1186
   353
      if( idx < 0 ) {
deba@1186
   354
	push(i, p);
deba@1186
   355
      }
deba@1186
   356
      else if( p >= data[idx].prio ) {
deba@1186
   357
	data[idx].prio = p;
deba@1186
   358
	bubble_up(idx);
deba@1186
   359
      } else {
deba@1186
   360
	data[idx].prio = p;
deba@1186
   361
	bubble_down(idx);
deba@1186
   362
      }
deba@1186
   363
    }
deba@1186
   364
deba@1331
   365
deba@1331
   366
    /// \brief Decreases the priority of \c i to \c p.
deba@1331
   367
    ///
deba@1331
   368
    /// This method decreases the priority of item \c i to \c p.
deba@1331
   369
    /// \pre \c i must be stored in the heap with priority at least \c p, and
deba@1331
   370
    /// \c should be greater then the last removed item's priority.
deba@1331
   371
    /// \param i The item.
deba@1331
   372
    /// \param p The priority.
deba@1186
   373
    void decrease(const Item &i, const Prio &p) {
deba@1186
   374
      int idx = iim[i];
deba@1186
   375
      data[idx].prio = p;
deba@1186
   376
      bubble_down(idx);
deba@1186
   377
    }
deba@1186
   378
deba@1331
   379
    /// \brief Increases the priority of \c i to \c p.
deba@1331
   380
    ///
deba@1331
   381
    /// This method sets the priority of item \c i to \c p. 
deba@1331
   382
    /// \pre \c i must be stored in the heap with priority at most \c
deba@1331
   383
    /// p relative to \c Compare.
deba@1331
   384
    /// \param i The item.
deba@1331
   385
    /// \param p The priority.
deba@1186
   386
    void increase(const Item &i, const Prio &p) {
deba@1186
   387
      int idx = iim[i];
deba@1186
   388
      data[idx].prio = p;
deba@1186
   389
      bubble_up(idx);
deba@1186
   390
    }
deba@1186
   391
deba@1331
   392
    /// \brief Returns if \c item is in, has already been in, or has 
deba@1331
   393
    /// never been in the heap.
deba@1331
   394
    ///
deba@1331
   395
    /// This method returns PRE_HEAP if \c item has never been in the
deba@1331
   396
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1331
   397
    /// otherwise. In the latter case it is possible that \c item will
deba@1331
   398
    /// get back to the heap again.
deba@1331
   399
    /// \param i The item.
deba@1186
   400
    state_enum state(const Item &i) const {
deba@1186
   401
      int s = iim[i];
deba@1186
   402
      if( s >= 0 ) s = 0;
deba@1186
   403
      return state_enum(s);
deba@1186
   404
    }
deba@1186
   405
deba@1186
   406
  }; // class RadixHeap
deba@1186
   407
deba@1186
   408
deba@1186
   409
  ///@}
deba@1186
   410
deba@1186
   411
} // namespace lemon
deba@1186
   412
deba@1186
   413
#endif // LEMON_RADIX_HEAP_H