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