lemon/radix_heap.h
changeset 1435 8e85e6bbefdf
parent 1359 1581f961cfaa
child 1717 75fe24093ded
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/radix_heap.h	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,412 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/radix_heap.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_RADIX_HEAP_H
    1.21 +#define LEMON_RADIX_HEAP_H
    1.22 +
    1.23 +///\ingroup auxdat
    1.24 +///\file
    1.25 +///\brief Radix Heap implementation.
    1.26 +
    1.27 +#include <vector>
    1.28 +#include <lemon/error.h>
    1.29 +
    1.30 +namespace lemon {
    1.31 +
    1.32 +  /// \addtogroup auxdat
    1.33 +  /// @{
    1.34 +
    1.35 +  /// \brief Exception thrown by RadixHeap.
    1.36 +  ///  
    1.37 +  /// This Exception is thrown when a smaller priority
    1.38 +  /// is inserted into the \e RadixHeap then the last time erased.
    1.39 +  /// \see RadixHeap
    1.40 +  /// \author Balazs Dezso
    1.41 +
    1.42 +  class UnderFlowPriorityError : public RuntimeError {
    1.43 +  public:
    1.44 +    virtual const char* exceptionName() const {
    1.45 +      return "lemon::UnderFlowPriorityError";
    1.46 +    }  
    1.47 +  };
    1.48 +
    1.49 +  /// \brief A Radix Heap implementation.
    1.50 +  ///
    1.51 +  /// This class implements the \e radix \e heap data structure. A \e heap
    1.52 +  /// is a data structure for storing items with specified values called \e
    1.53 +  /// priorities in such a way that finding the item with minimum priority is
    1.54 +  /// efficient. This heap type can store only items with \e int priority.
    1.55 +  /// In a heap one can change the priority of an item, add or erase an 
    1.56 +  /// item, but the priority cannot be decreased under the last removed 
    1.57 +  /// item's priority.
    1.58 +  ///
    1.59 +  /// \param _Item Type of the items to be stored.  
    1.60 +  /// \param _ItemIntMap A read and writable Item int map, used internally
    1.61 +  /// to handle the cross references.
    1.62 +  ///
    1.63 +  /// \see BinHeap
    1.64 +  /// \see Dijkstra
    1.65 +  /// \author Balazs Dezso
    1.66 +
    1.67 +  template <typename _Item, typename _ItemIntMap>
    1.68 +  class RadixHeap {
    1.69 +
    1.70 +  public:
    1.71 +    typedef _Item Item;
    1.72 +    typedef int Prio;
    1.73 +    typedef _ItemIntMap ItemIntMap;
    1.74 +
    1.75 +    /// \brief Type to represent the items states.
    1.76 +    ///
    1.77 +    /// Each Item element have a state associated to it. It may be "in heap",
    1.78 +    /// "pre heap" or "post heap". The latter two are indifferent from the
    1.79 +    /// heap's point of view, but may be useful to the user.
    1.80 +    ///
    1.81 +    /// The ItemIntMap \e should be initialized in such way that it maps
    1.82 +    /// PRE_HEAP (-1) to any element to be put in the heap...
    1.83 +    enum state_enum {
    1.84 +      IN_HEAP = 0,
    1.85 +      PRE_HEAP = -1,
    1.86 +      POST_HEAP = -2
    1.87 +    };
    1.88 +
    1.89 +  private:
    1.90 +    
    1.91 +    struct RadixItem {
    1.92 +      int prev, next, box;
    1.93 +      Item item;
    1.94 +      int prio;
    1.95 +      RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {}
    1.96 +    };
    1.97 +
    1.98 +    struct RadixBox {
    1.99 +      int first;
   1.100 +      int min, size;
   1.101 +      RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
   1.102 +    };
   1.103 +
   1.104 +    std::vector<RadixItem> data;
   1.105 +    std::vector<RadixBox> boxes;
   1.106 +
   1.107 +    ItemIntMap &iim;
   1.108 +
   1.109 +
   1.110 +  public:
   1.111 +    /// \brief The constructor.
   1.112 +    ///
   1.113 +    /// The constructor.
   1.114 +    /// \param _iim should be given to the constructor, since it is used
   1.115 +    /// internally to handle the cross references. The value of the map
   1.116 +    /// should be PRE_HEAP (-1) for each element.
   1.117 +    explicit RadixHeap(ItemIntMap &_iim) : iim(_iim) {
   1.118 +      boxes.push_back(RadixBox(0, 1));
   1.119 +      boxes.push_back(RadixBox(1, 1));
   1.120 +    }
   1.121 +
   1.122 +    /// \brief The constructor.
   1.123 +    ///
   1.124 +    /// The constructor.
   1.125 +    ///
   1.126 +    /// \param _iim It should be given to the constructor, since it is used
   1.127 +    /// internally to handle the cross references. The value of the map
   1.128 +    /// should be PRE_HEAP (-1) for each element.
   1.129 +    ///
   1.130 +    /// \param capacity It determines the initial capacity of the heap. 
   1.131 +    RadixHeap(ItemIntMap &_iim, int capacity) : iim(_iim) {
   1.132 +      boxes.push_back(RadixBox(0, 1));
   1.133 +      boxes.push_back(RadixBox(1, 1));
   1.134 +      while (upper(boxes.back(), capacity)) {
   1.135 +	extend();
   1.136 +      }
   1.137 +    }
   1.138 +
   1.139 +    /// The number of items stored in the heap.
   1.140 +    ///
   1.141 +    /// \brief Returns the number of items stored in the heap.
   1.142 +    int size() const { return data.size(); }
   1.143 +    /// \brief Checks if the heap stores no items.
   1.144 +    ///
   1.145 +    /// Returns \c true if and only if the heap stores no items.
   1.146 +    bool empty() const { return data.empty(); }
   1.147 +
   1.148 +  private:
   1.149 +
   1.150 +    bool upper(int box, Prio prio) {
   1.151 +      return prio < boxes[box].min;
   1.152 +    }
   1.153 +
   1.154 +    bool lower(int box, Prio prio) {
   1.155 +      return prio >= boxes[box].min + boxes[box].size;
   1.156 +    }
   1.157 +
   1.158 +    /// \brief Remove item from the box list.
   1.159 +    void remove(int index) {
   1.160 +      if (data[index].prev >= 0) {
   1.161 +	data[data[index].prev].next = data[index].next;
   1.162 +      } else {
   1.163 +	boxes[data[index].box].first = data[index].next;
   1.164 +      }
   1.165 +      if (data[index].next >= 0) {
   1.166 +	data[data[index].next].prev = data[index].prev;
   1.167 +      }
   1.168 +    }
   1.169 +
   1.170 +    /// \brief Insert item into the box list.
   1.171 +    void insert(int box, int index) {
   1.172 +      if (boxes[box].first == -1) {
   1.173 +	boxes[box].first = index;
   1.174 +	data[index].next = data[index].prev = -1;
   1.175 +      } else {
   1.176 +	data[index].next = boxes[box].first;
   1.177 +	data[boxes[box].first].prev = index;
   1.178 +	data[index].prev = -1;
   1.179 +	boxes[box].first = index;
   1.180 +      }
   1.181 +      data[index].box = box;
   1.182 +    }
   1.183 +
   1.184 +    /// \brief Add a new box to the box list.
   1.185 +    void extend() {
   1.186 +      int min = boxes.back().min + boxes.back().size;
   1.187 +      int size = 2 * boxes.back().size;
   1.188 +      boxes.push_back(RadixBox(min, size));
   1.189 +    }
   1.190 +
   1.191 +    /// \brief Move an item up into the proper box.
   1.192 +    void bubble_up(int index) {
   1.193 +      if (!lower(data[index].box, data[index].prio)) return;
   1.194 +      remove(index);
   1.195 +      int box = findUp(data[index].box, data[index].prio);
   1.196 +      insert(box, index);      
   1.197 +    }
   1.198 +
   1.199 +    /// \brief Find up the proper box for the item with the given prio.
   1.200 +    int findUp(int start, int prio) {
   1.201 +      while (lower(start, prio)) {
   1.202 +	if (++start == (int)boxes.size()) {
   1.203 +	  extend();
   1.204 +	}
   1.205 +      }
   1.206 +      return start;
   1.207 +    }
   1.208 +
   1.209 +    /// \brief Move an item down into the proper box.
   1.210 +    void bubble_down(int index) {
   1.211 +      if (!upper(data[index].box, data[index].prio)) return;
   1.212 +      remove(index);
   1.213 +      int box = findDown(data[index].box, data[index].prio);
   1.214 +      insert(box, index);
   1.215 +    }
   1.216 +
   1.217 +    /// \brief Find up the proper box for the item with the given prio.
   1.218 +    int findDown(int start, int prio) {
   1.219 +      while (upper(start, prio)) {
   1.220 +	if (--start < 0) throw UnderFlowPriorityError();
   1.221 +      }
   1.222 +      return start;
   1.223 +    }
   1.224 +
   1.225 +    /// \brief Find the first not empty box.
   1.226 +    int findFirst() {
   1.227 +      int first = 0;
   1.228 +      while (boxes[first].first == -1) ++first;
   1.229 +      return first;
   1.230 +    }
   1.231 +
   1.232 +    /// \brief Gives back the minimal prio of the box.
   1.233 +    int minValue(int box) {
   1.234 +      int min = data[boxes[box].first].prio;
   1.235 +      for (int k = boxes[box].first; k != -1; k = data[k].next) {
   1.236 +	if (data[k].prio < min) min = data[k].prio;
   1.237 +      }
   1.238 +      return min;
   1.239 +    }
   1.240 +
   1.241 +    /// \brief Rearrange the items of the heap and makes the 
   1.242 +    /// first box not empty.
   1.243 +    void moveDown() {
   1.244 +      int box = findFirst();
   1.245 +      if (box == 0) return;
   1.246 +      int min = minValue(box);
   1.247 +      for (int i = 0; i <= box; ++i) {
   1.248 +	boxes[i].min = min;
   1.249 +	min += boxes[i].size;
   1.250 +      }
   1.251 +      int curr = boxes[box].first, next;
   1.252 +      while (curr != -1) {
   1.253 +	next = data[curr].next;
   1.254 +	bubble_down(curr);
   1.255 +	curr = next;
   1.256 +      }      
   1.257 +    }
   1.258 +
   1.259 +    void relocate_last(int index) {
   1.260 +      if (index != (int)data.size() - 1) {
   1.261 +	data[index] = data.back();
   1.262 +	if (data[index].prev != -1) {
   1.263 +	  data[data[index].prev].next = index;
   1.264 +	} else {
   1.265 +	  boxes[data[index].box].first = index;
   1.266 +	}
   1.267 +	if (data[index].next != -1) {
   1.268 +	  data[data[index].next].prev = index;
   1.269 +	}
   1.270 +	iim[data[index].item] = index;
   1.271 +      }
   1.272 +      data.pop_back();
   1.273 +    }
   1.274 +
   1.275 +  public:
   1.276 +
   1.277 +    /// \brief Insert an item into the heap with the given heap.
   1.278 +    ///    
   1.279 +    /// Adds \c i to the heap with priority \c p. 
   1.280 +    /// \param i The item to insert.
   1.281 +    /// \param p The priority of the item.
   1.282 +    void push(const Item &i, const Prio &p) {
   1.283 +      int n = data.size();
   1.284 +      iim.set(i, n);
   1.285 +      data.push_back(RadixItem(i, p));
   1.286 +      while (lower(boxes.size() - 1, p)) {
   1.287 +	extend();
   1.288 +      }
   1.289 +      int box = findDown(boxes.size() - 1, p);
   1.290 +      insert(box, n);
   1.291 +    }
   1.292 +
   1.293 +    /// \brief Returns the item with minimum priority.
   1.294 +    ///
   1.295 +    /// This method returns the item with minimum priority.  
   1.296 +    /// \pre The heap must be nonempty.  
   1.297 +    Item top() const {
   1.298 +      const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
   1.299 +      return data[boxes[0].first].item;
   1.300 +    }
   1.301 +
   1.302 +    /// \brief Returns the minimum priority.
   1.303 +    ///
   1.304 +    /// It returns the minimum priority.
   1.305 +    /// \pre The heap must be nonempty.
   1.306 +    Prio prio() const {
   1.307 +      const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
   1.308 +      return data[boxes[0].first].prio;
   1.309 +     }
   1.310 +
   1.311 +    /// \brief Deletes the item with minimum priority.
   1.312 +    ///
   1.313 +    /// This method deletes the item with minimum priority.
   1.314 +    /// \pre The heap must be non-empty.  
   1.315 +    void pop() {
   1.316 +      moveDown();
   1.317 +      int index = boxes[0].first;
   1.318 +      iim[data[index].item] = POST_HEAP;
   1.319 +      remove(index);
   1.320 +      relocate_last(index);
   1.321 +    }
   1.322 +
   1.323 +    /// \brief Deletes \c i from the heap.
   1.324 +    ///
   1.325 +    /// This method deletes item \c i from the heap, if \c i was
   1.326 +    /// already stored in the heap.
   1.327 +    /// \param i The item to erase. 
   1.328 +    void erase(const Item &i) {
   1.329 +      int index = iim[i];
   1.330 +      iim[i] = POST_HEAP;
   1.331 +      remove(index);
   1.332 +      relocate_last(index);
   1.333 +   }
   1.334 +
   1.335 +    /// \brief Returns the priority of \c i.
   1.336 +    ///
   1.337 +    /// This function returns the priority of item \c i.  
   1.338 +    /// \pre \c i must be in the heap.
   1.339 +    /// \param i The item.
   1.340 +    Prio operator[](const Item &i) const {
   1.341 +      int idx = iim[i];
   1.342 +      return data[idx].prio;
   1.343 +    }
   1.344 +
   1.345 +    /// \brief \c i gets to the heap with priority \c p independently 
   1.346 +    /// if \c i was already there.
   1.347 +    ///
   1.348 +    /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.349 +    /// in the heap and sets the priority of \c i to \c p otherwise.
   1.350 +    /// It may throw an \e UnderFlowPriorityException. 
   1.351 +    /// \param i The item.
   1.352 +    /// \param p The priority.
   1.353 +    void set(const Item &i, const Prio &p) {
   1.354 +      int idx = iim[i];
   1.355 +      if( idx < 0 ) {
   1.356 +	push(i, p);
   1.357 +      }
   1.358 +      else if( p >= data[idx].prio ) {
   1.359 +	data[idx].prio = p;
   1.360 +	bubble_up(idx);
   1.361 +      } else {
   1.362 +	data[idx].prio = p;
   1.363 +	bubble_down(idx);
   1.364 +      }
   1.365 +    }
   1.366 +
   1.367 +
   1.368 +    /// \brief Decreases the priority of \c i to \c p.
   1.369 +    ///
   1.370 +    /// This method decreases the priority of item \c i to \c p.
   1.371 +    /// \pre \c i must be stored in the heap with priority at least \c p, and
   1.372 +    /// \c should be greater then the last removed item's priority.
   1.373 +    /// \param i The item.
   1.374 +    /// \param p The priority.
   1.375 +    void decrease(const Item &i, const Prio &p) {
   1.376 +      int idx = iim[i];
   1.377 +      data[idx].prio = p;
   1.378 +      bubble_down(idx);
   1.379 +    }
   1.380 +
   1.381 +    /// \brief Increases the priority of \c i to \c p.
   1.382 +    ///
   1.383 +    /// This method sets the priority of item \c i to \c p. 
   1.384 +    /// \pre \c i must be stored in the heap with priority at most \c
   1.385 +    /// p relative to \c Compare.
   1.386 +    /// \param i The item.
   1.387 +    /// \param p The priority.
   1.388 +    void increase(const Item &i, const Prio &p) {
   1.389 +      int idx = iim[i];
   1.390 +      data[idx].prio = p;
   1.391 +      bubble_up(idx);
   1.392 +    }
   1.393 +
   1.394 +    /// \brief Returns if \c item is in, has already been in, or has 
   1.395 +    /// never been in the heap.
   1.396 +    ///
   1.397 +    /// This method returns PRE_HEAP if \c item has never been in the
   1.398 +    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.399 +    /// otherwise. In the latter case it is possible that \c item will
   1.400 +    /// get back to the heap again.
   1.401 +    /// \param i The item.
   1.402 +    state_enum state(const Item &i) const {
   1.403 +      int s = iim[i];
   1.404 +      if( s >= 0 ) s = 0;
   1.405 +      return state_enum(s);
   1.406 +    }
   1.407 +
   1.408 +  }; // class RadixHeap
   1.409 +
   1.410 +
   1.411 +  ///@}
   1.412 +
   1.413 +} // namespace lemon
   1.414 +
   1.415 +#endif // LEMON_RADIX_HEAP_H