1.1 --- a/lemon/kary_heap.h Thu Jul 09 02:39:47 2009 +0200
1.2 +++ b/lemon/kary_heap.h Thu Jul 09 04:07:08 2009 +0200
1.3 @@ -1,8 +1,8 @@
1.4 -/* -*- C++ -*-
1.5 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.6 *
1.7 - * This file is a part of LEMON, a generic C++ optimization library
1.8 + * This file is a part of LEMON, a generic C++ optimization library.
1.9 *
1.10 - * Copyright (C) 2003-2008
1.11 + * Copyright (C) 2003-2009
1.12 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.13 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.14 *
1.15 @@ -19,152 +19,151 @@
1.16 #ifndef LEMON_KARY_HEAP_H
1.17 #define LEMON_KARY_HEAP_H
1.18
1.19 -///\ingroup auxdat
1.20 +///\ingroup heaps
1.21 ///\file
1.22 -///\brief Kary Heap implementation.
1.23 +///\brief Fourary heap implementation.
1.24
1.25 -#include <iostream>
1.26 #include <vector>
1.27 #include <utility>
1.28 #include <functional>
1.29
1.30 namespace lemon {
1.31
1.32 - ///\ingroup auxdat
1.33 + /// \ingroup heaps
1.34 ///
1.35 - ///\brief A Kary Heap implementation.
1.36 + ///\brief K-ary heap data structure.
1.37 ///
1.38 - ///This class implements the \e Kary \e heap data structure. A \e heap
1.39 - ///is a data structure for storing items with specified values called \e
1.40 - ///priorities in such a way that finding the item with minimum priority is
1.41 - ///efficient. \c Compare specifies the ordering of the priorities. In a heap
1.42 - ///one can change the priority of an item, add or erase an item, etc.
1.43 + /// This class implements the \e K-ary \e heap data structure.
1.44 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.45 ///
1.46 - ///\param _Prio Type of the priority of the items.
1.47 - ///\param _ItemIntMap A read and writable Item int map, used internally
1.48 - ///to handle the cross references.
1.49 - ///\param _Compare A class for the ordering of the priorities. The
1.50 - ///default is \c std::less<_Prio>.
1.51 + /// The \ref KaryHeap "K-ary heap" is a generalization of the
1.52 + /// \ref BinHeap "binary heap" structure, its nodes have at most
1.53 + /// \c K children, instead of two.
1.54 + /// \ref BinHeap and \ref FouraryHeap are specialized implementations
1.55 + /// of this structure for <tt>K=2</tt> and <tt>K=4</tt>, respectively.
1.56 ///
1.57 - ///\sa FibHeap
1.58 - ///\sa Dijkstra
1.59 - ///\author Dorian Batha
1.60 + /// \tparam PR Type of the priorities of the items.
1.61 + /// \tparam IM A read-writable item map with \c int values, used
1.62 + /// internally to handle the cross references.
1.63 + /// \tparam CMP A functor class for comparing the priorities.
1.64 + /// The default is \c std::less<PR>.
1.65 + ///
1.66 + ///\sa BinHeap
1.67 + ///\sa FouraryHeap
1.68 +#ifdef DOXYGEN
1.69 + template <typename PR, typename IM, typename CMP>
1.70 +#else
1.71 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.72 +#endif
1.73 + class KaryHeap {
1.74 + public:
1.75 + /// Type of the item-int map.
1.76 + typedef IM ItemIntMap;
1.77 + /// Type of the priorities.
1.78 + typedef PR Prio;
1.79 + /// Type of the items stored in the heap.
1.80 + typedef typename ItemIntMap::Key Item;
1.81 + /// Type of the item-priority pairs.
1.82 + typedef std::pair<Item,Prio> Pair;
1.83 + /// Functor type for comparing the priorities.
1.84 + typedef CMP Compare;
1.85
1.86 - template <typename _Prio, typename _ItemIntMap,
1.87 - typename _Compare = std::less<_Prio> >
1.88 -
1.89 - class KaryHeap {
1.90 -
1.91 - public:
1.92 - ///\e
1.93 - typedef _ItemIntMap ItemIntMap;
1.94 - ///\e
1.95 - typedef _Prio Prio;
1.96 - ///\e
1.97 - typedef typename ItemIntMap::Key Item;
1.98 - ///\e
1.99 - typedef std::pair<Item,Prio> Pair;
1.100 - ///\e
1.101 - typedef _Compare Compare;
1.102 - ///\e
1.103 -
1.104 - /// \brief Type to represent the items states.
1.105 + /// \brief Type to represent the states of the items.
1.106 ///
1.107 - /// Each Item element have a state associated to it. It may be "in heap",
1.108 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.109 + /// Each item has a state associated to it. It can be "in heap",
1.110 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.111 /// heap's point of view, but may be useful to the user.
1.112 ///
1.113 - /// The ItemIntMap \e should be initialized in such way that it maps
1.114 - /// PRE_HEAP (-1) to any element to be put in the heap...
1.115 + /// The item-int map must be initialized in such way that it assigns
1.116 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.117 enum State {
1.118 - IN_HEAP = 0,
1.119 - PRE_HEAP = -1,
1.120 - POST_HEAP = -2
1.121 + IN_HEAP = 0, ///< = 0.
1.122 + PRE_HEAP = -1, ///< = -1.
1.123 + POST_HEAP = -2 ///< = -2.
1.124 };
1.125
1.126 private:
1.127 - std::vector<Pair> data;
1.128 - Compare comp;
1.129 - ItemIntMap &iim;
1.130 - int K;
1.131 + std::vector<Pair> _data;
1.132 + Compare _comp;
1.133 + ItemIntMap &_iim;
1.134 + int _K;
1.135
1.136 public:
1.137 - /// \brief The constructor.
1.138 + /// \brief Constructor.
1.139 ///
1.140 - /// The constructor.
1.141 - /// \param _iim should be given to the constructor, since it is used
1.142 - /// internally to handle the cross references. The value of the map
1.143 - /// should be PRE_HEAP (-1) for each element.
1.144 - explicit KaryHeap(ItemIntMap &_iim, const int &_K=32) : iim(_iim), K(_K) {}
1.145 + /// Constructor.
1.146 + /// \param map A map that assigns \c int values to the items.
1.147 + /// It is used internally to handle the cross references.
1.148 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.149 + explicit KaryHeap(ItemIntMap &map, int K=32) : _iim(map), _K(K) {}
1.150
1.151 - /// \brief The constructor.
1.152 + /// \brief Constructor.
1.153 ///
1.154 - /// The constructor.
1.155 - /// \param _iim should be given to the constructor, since it is used
1.156 - /// internally to handle the cross references. The value of the map
1.157 - /// should be PRE_HEAP (-1) for each element.
1.158 + /// Constructor.
1.159 + /// \param map A map that assigns \c int values to the items.
1.160 + /// It is used internally to handle the cross references.
1.161 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.162 + /// \param comp The function object used for comparing the priorities.
1.163 + KaryHeap(ItemIntMap &map, const Compare &comp, int K=32)
1.164 + : _iim(map), _comp(comp), _K(K) {}
1.165 +
1.166 + /// \brief The number of items stored in the heap.
1.167 ///
1.168 - /// \param _comp The comparator function object.
1.169 - KaryHeap(ItemIntMap &_iim, const Compare &_comp, const int &_K=32)
1.170 - : iim(_iim), comp(_comp), K(_K) {}
1.171 + /// This function returns the number of items stored in the heap.
1.172 + int size() const { return _data.size(); }
1.173
1.174 + /// \brief Check if the heap is empty.
1.175 + ///
1.176 + /// This function returns \c true if the heap is empty.
1.177 + bool empty() const { return _data.empty(); }
1.178
1.179 - /// The number of items stored in the heap.
1.180 + /// \brief Make the heap empty.
1.181 ///
1.182 - /// \brief Returns the number of items stored in the heap.
1.183 - int size() const { return data.size(); }
1.184 -
1.185 - /// \brief Checks if the heap stores no items.
1.186 - ///
1.187 - /// Returns \c true if and only if the heap stores no items.
1.188 - bool empty() const { return data.empty(); }
1.189 -
1.190 - /// \brief Make empty this heap.
1.191 - ///
1.192 - /// Make empty this heap. It does not change the cross reference map.
1.193 - /// If you want to reuse what is not surely empty you should first clear
1.194 - /// the heap and after that you should set the cross reference map for
1.195 - /// each item to \c PRE_HEAP.
1.196 - void clear() { data.clear(); }
1.197 + /// This functon makes the heap empty.
1.198 + /// It does not change the cross reference map. If you want to reuse
1.199 + /// a heap that is not surely empty, you should first clear it and
1.200 + /// then you should set the cross reference map to \c PRE_HEAP
1.201 + /// for each item.
1.202 + void clear() { _data.clear(); }
1.203
1.204 private:
1.205 - int parent(int i) { return (i-1)/K; }
1.206 - int first_child(int i) { return K*i+1; }
1.207 + int parent(int i) { return (i-1)/_K; }
1.208 + int firstChild(int i) { return _K*i+1; }
1.209
1.210 bool less(const Pair &p1, const Pair &p2) const {
1.211 - return comp(p1.second, p2.second);
1.212 + return _comp(p1.second, p2.second);
1.213 }
1.214
1.215 - int find_min(const int child, const int length) {
1.216 + int findMin(const int child, const int length) {
1.217 int min=child, i=1;
1.218 - while( i<K && child+i<length ) {
1.219 - if( less(data[child+i], data[min]) )
1.220 + while( i<_K && child+i<length ) {
1.221 + if( less(_data[child+i], _data[min]) )
1.222 min=child+i;
1.223 ++i;
1.224 }
1.225 return min;
1.226 }
1.227
1.228 - void bubble_up(int hole, Pair p) {
1.229 + void bubbleUp(int hole, Pair p) {
1.230 int par = parent(hole);
1.231 - while( hole>0 && less(p,data[par]) ) {
1.232 - move(data[par],hole);
1.233 + while( hole>0 && less(p,_data[par]) ) {
1.234 + move(_data[par],hole);
1.235 hole = par;
1.236 par = parent(hole);
1.237 }
1.238 move(p, hole);
1.239 }
1.240
1.241 - void bubble_down(int hole, Pair p, int length) {
1.242 + void bubbleDown(int hole, Pair p, int length) {
1.243 if( length>1 ) {
1.244 - int child = first_child(hole);
1.245 + int child = firstChild(hole);
1.246 while( child<length ) {
1.247 - child = find_min(child, length);
1.248 - if( !less(data[child], p) )
1.249 + child = findMin(child, length);
1.250 + if( !less(_data[child], p) )
1.251 goto ok;
1.252 - move(data[child], hole);
1.253 + move(_data[child], hole);
1.254 hole = child;
1.255 - child = first_child(hole);
1.256 + child = firstChild(hole);
1.257 }
1.258 }
1.259 ok:
1.260 @@ -172,167 +171,169 @@
1.261 }
1.262
1.263 void move(const Pair &p, int i) {
1.264 - data[i] = p;
1.265 - iim.set(p.first, i);
1.266 + _data[i] = p;
1.267 + _iim.set(p.first, i);
1.268 }
1.269
1.270 public:
1.271 /// \brief Insert a pair of item and priority into the heap.
1.272 ///
1.273 - /// Adds \c p.first to the heap with priority \c p.second.
1.274 + /// This function inserts \c p.first to the heap with priority
1.275 + /// \c p.second.
1.276 /// \param p The pair to insert.
1.277 + /// \pre \c p.first must not be stored in the heap.
1.278 void push(const Pair &p) {
1.279 - int n = data.size();
1.280 - data.resize(n+1);
1.281 - bubble_up(n, p);
1.282 + int n = _data.size();
1.283 + _data.resize(n+1);
1.284 + bubbleUp(n, p);
1.285 }
1.286
1.287 - /// \brief Insert an item into the heap with the given heap.
1.288 + /// \brief Insert an item into the heap with the given priority.
1.289 ///
1.290 - /// Adds \c i to the heap with priority \c p.
1.291 + /// This function inserts the given item into the heap with the
1.292 + /// given priority.
1.293 /// \param i The item to insert.
1.294 /// \param p The priority of the item.
1.295 + /// \pre \e i must not be stored in the heap.
1.296 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
1.297
1.298 - /// \brief Returns the item with minimum priority relative to \c Compare.
1.299 + /// \brief Return the item having minimum priority.
1.300 ///
1.301 - /// This method returns the item with minimum priority relative to \c
1.302 - /// Compare.
1.303 - /// \pre The heap must be nonempty.
1.304 - Item top() const { return data[0].first; }
1.305 + /// This function returns the item having minimum priority.
1.306 + /// \pre The heap must be non-empty.
1.307 + Item top() const { return _data[0].first; }
1.308
1.309 - /// \brief Returns the minimum priority relative to \c Compare.
1.310 + /// \brief The minimum priority.
1.311 ///
1.312 - /// It returns the minimum priority relative to \c Compare.
1.313 - /// \pre The heap must be nonempty.
1.314 - Prio prio() const { return data[0].second; }
1.315 + /// This function returns the minimum priority.
1.316 + /// \pre The heap must be non-empty.
1.317 + Prio prio() const { return _data[0].second; }
1.318
1.319 - /// \brief Deletes the item with minimum priority relative to \c Compare.
1.320 + /// \brief Remove the item having minimum priority.
1.321 ///
1.322 - /// This method deletes the item with minimum priority relative to \c
1.323 - /// Compare from the heap.
1.324 + /// This function removes the item having minimum priority.
1.325 /// \pre The heap must be non-empty.
1.326 void pop() {
1.327 - int n = data.size()-1;
1.328 - iim.set(data[0].first, POST_HEAP);
1.329 - if (n>0) bubble_down(0, data[n], n);
1.330 - data.pop_back();
1.331 + int n = _data.size()-1;
1.332 + _iim.set(_data[0].first, POST_HEAP);
1.333 + if (n>0) bubbleDown(0, _data[n], n);
1.334 + _data.pop_back();
1.335 }
1.336
1.337 - /// \brief Deletes \c i from the heap.
1.338 + /// \brief Remove the given item from the heap.
1.339 ///
1.340 - /// This method deletes item \c i from the heap.
1.341 - /// \param i The item to erase.
1.342 - /// \pre The item should be in the heap.
1.343 + /// This function removes the given item from the heap if it is
1.344 + /// already stored.
1.345 + /// \param i The item to delete.
1.346 + /// \pre \e i must be in the heap.
1.347 void erase(const Item &i) {
1.348 - int h = iim[i];
1.349 - int n = data.size()-1;
1.350 - iim.set(data[h].first, POST_HEAP);
1.351 + int h = _iim[i];
1.352 + int n = _data.size()-1;
1.353 + _iim.set(_data[h].first, POST_HEAP);
1.354 if( h<n ) {
1.355 - if( less(data[parent(h)], data[n]) )
1.356 - bubble_down(h, data[n], n);
1.357 + if( less(_data[parent(h)], _data[n]) )
1.358 + bubbleDown(h, _data[n], n);
1.359 else
1.360 - bubble_up(h, data[n]);
1.361 + bubbleUp(h, _data[n]);
1.362 }
1.363 - data.pop_back();
1.364 + _data.pop_back();
1.365 }
1.366
1.367 -
1.368 - /// \brief Returns the priority of \c i.
1.369 + /// \brief The priority of the given item.
1.370 ///
1.371 - /// This function returns the priority of item \c i.
1.372 - /// \pre \c i must be in the heap.
1.373 + /// This function returns the priority of the given item.
1.374 /// \param i The item.
1.375 + /// \pre \e i must be in the heap.
1.376 Prio operator[](const Item &i) const {
1.377 - int idx = iim[i];
1.378 - return data[idx].second;
1.379 + int idx = _iim[i];
1.380 + return _data[idx].second;
1.381 }
1.382
1.383 - /// \brief \c i gets to the heap with priority \c p independently
1.384 - /// if \c i was already there.
1.385 + /// \brief Set the priority of an item or insert it, if it is
1.386 + /// not stored in the heap.
1.387 ///
1.388 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.389 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.390 + /// This method sets the priority of the given item if it is
1.391 + /// already stored in the heap. Otherwise it inserts the given
1.392 + /// item into the heap with the given priority.
1.393 /// \param i The item.
1.394 /// \param p The priority.
1.395 void set(const Item &i, const Prio &p) {
1.396 - int idx = iim[i];
1.397 + int idx = _iim[i];
1.398 if( idx<0 )
1.399 push(i,p);
1.400 - else if( comp(p, data[idx].second) )
1.401 - bubble_up(idx, Pair(i,p));
1.402 + else if( _comp(p, _data[idx].second) )
1.403 + bubbleUp(idx, Pair(i,p));
1.404 else
1.405 - bubble_down(idx, Pair(i,p), data.size());
1.406 + bubbleDown(idx, Pair(i,p), _data.size());
1.407 }
1.408
1.409 - /// \brief Decreases the priority of \c i to \c p.
1.410 + /// \brief Decrease the priority of an item to the given value.
1.411 ///
1.412 - /// This method decreases the priority of item \c i to \c p.
1.413 - /// \pre \c i must be stored in the heap with priority at least \c
1.414 - /// p relative to \c Compare.
1.415 + /// This function decreases the priority of an item to the given value.
1.416 /// \param i The item.
1.417 /// \param p The priority.
1.418 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.419 void decrease(const Item &i, const Prio &p) {
1.420 - int idx = iim[i];
1.421 - bubble_up(idx, Pair(i,p));
1.422 + int idx = _iim[i];
1.423 + bubbleUp(idx, Pair(i,p));
1.424 }
1.425
1.426 - /// \brief Increases the priority of \c i to \c p.
1.427 + /// \brief Increase the priority of an item to the given value.
1.428 ///
1.429 - /// This method sets the priority of item \c i to \c p.
1.430 - /// \pre \c i must be stored in the heap with priority at most \c
1.431 - /// p relative to \c Compare.
1.432 + /// This function increases the priority of an item to the given value.
1.433 /// \param i The item.
1.434 /// \param p The priority.
1.435 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.436 void increase(const Item &i, const Prio &p) {
1.437 - int idx = iim[i];
1.438 - bubble_down(idx, Pair(i,p), data.size());
1.439 + int idx = _iim[i];
1.440 + bubbleDown(idx, Pair(i,p), _data.size());
1.441 }
1.442
1.443 - /// \brief Returns if \c item is in, has already been in, or has
1.444 - /// never been in the heap.
1.445 + /// \brief Return the state of an item.
1.446 ///
1.447 - /// This method returns PRE_HEAP if \c item has never been in the
1.448 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.449 - /// otherwise. In the latter case it is possible that \c item will
1.450 - /// get back to the heap again.
1.451 + /// This method returns \c PRE_HEAP if the given item has never
1.452 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.453 + /// and \c POST_HEAP otherwise.
1.454 + /// In the latter case it is possible that the item will get back
1.455 + /// to the heap again.
1.456 /// \param i The item.
1.457 State state(const Item &i) const {
1.458 - int s = iim[i];
1.459 + int s = _iim[i];
1.460 if (s>=0) s=0;
1.461 return State(s);
1.462 }
1.463
1.464 - /// \brief Sets the state of the \c item in the heap.
1.465 + /// \brief Set the state of an item in the heap.
1.466 ///
1.467 - /// Sets the state of the \c item in the heap. It can be used to
1.468 - /// manually clear the heap when it is important to achive the
1.469 - /// better time complexity.
1.470 + /// This function sets the state of the given item in the heap.
1.471 + /// It can be used to manually clear the heap when it is important
1.472 + /// to achive better time complexity.
1.473 /// \param i The item.
1.474 /// \param st The state. It should not be \c IN_HEAP.
1.475 void state(const Item& i, State st) {
1.476 switch (st) {
1.477 - case POST_HEAP:
1.478 - case PRE_HEAP:
1.479 - if (state(i) == IN_HEAP) erase(i);
1.480 - iim[i] = st;
1.481 - break;
1.482 - case IN_HEAP:
1.483 - break;
1.484 + case POST_HEAP:
1.485 + case PRE_HEAP:
1.486 + if (state(i) == IN_HEAP) erase(i);
1.487 + _iim[i] = st;
1.488 + break;
1.489 + case IN_HEAP:
1.490 + break;
1.491 }
1.492 }
1.493
1.494 - /// \brief Replaces an item in the heap.
1.495 + /// \brief Replace an item in the heap.
1.496 ///
1.497 - /// The \c i item is replaced with \c j item. The \c i item should
1.498 - /// be in the heap, while the \c j should be out of the heap. The
1.499 - /// \c i item will out of the heap and \c j will be in the heap
1.500 - /// with the same prioriority as prevoiusly the \c i item.
1.501 + /// This function replaces item \c i with item \c j.
1.502 + /// Item \c i must be in the heap, while \c j must be out of the heap.
1.503 + /// After calling this method, item \c i will be out of the
1.504 + /// heap and \c j will be in the heap with the same prioriority
1.505 + /// as item \c i had before.
1.506 void replace(const Item& i, const Item& j) {
1.507 - int idx=iim[i];
1.508 - iim.set(i, iim[j]);
1.509 - iim.set(j, idx);
1.510 - data[idx].first=j;
1.511 + int idx=_iim[i];
1.512 + _iim.set(i, _iim[j]);
1.513 + _iim.set(j, idx);
1.514 + _data[idx].first=j;
1.515 }
1.516
1.517 }; // class KaryHeap