1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/kary_heap.h Thu Jul 09 02:38:01 2009 +0200
1.3 @@ -0,0 +1,342 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2008
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_KARY_HEAP_H
1.23 +#define LEMON_KARY_HEAP_H
1.24 +
1.25 +///\ingroup auxdat
1.26 +///\file
1.27 +///\brief Kary Heap implementation.
1.28 +
1.29 +#include <iostream>
1.30 +#include <vector>
1.31 +#include <utility>
1.32 +#include <functional>
1.33 +
1.34 +namespace lemon {
1.35 +
1.36 + ///\ingroup auxdat
1.37 + ///
1.38 + ///\brief A Kary Heap implementation.
1.39 + ///
1.40 + ///This class implements the \e Kary \e heap data structure. A \e heap
1.41 + ///is a data structure for storing items with specified values called \e
1.42 + ///priorities in such a way that finding the item with minimum priority is
1.43 + ///efficient. \c Compare specifies the ordering of the priorities. In a heap
1.44 + ///one can change the priority of an item, add or erase an item, etc.
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 + ///
1.52 + ///\sa FibHeap
1.53 + ///\sa Dijkstra
1.54 + ///\author Dorian Batha
1.55 +
1.56 + template <typename _Prio, typename _ItemIntMap,
1.57 + typename _Compare = std::less<_Prio> >
1.58 +
1.59 + class KaryHeap {
1.60 +
1.61 + public:
1.62 + ///\e
1.63 + typedef _ItemIntMap ItemIntMap;
1.64 + ///\e
1.65 + typedef _Prio Prio;
1.66 + ///\e
1.67 + typedef typename ItemIntMap::Key Item;
1.68 + ///\e
1.69 + typedef std::pair<Item,Prio> Pair;
1.70 + ///\e
1.71 + typedef _Compare Compare;
1.72 + ///\e
1.73 +
1.74 + /// \brief Type to represent the items states.
1.75 + ///
1.76 + /// Each Item element have a state associated to it. It may be "in heap",
1.77 + /// "pre heap" or "post heap". The latter two are indifferent from the
1.78 + /// heap's point of view, but may be useful to the user.
1.79 + ///
1.80 + /// The ItemIntMap \e should be initialized in such way that it maps
1.81 + /// PRE_HEAP (-1) to any element to be put in the heap...
1.82 + enum State {
1.83 + IN_HEAP = 0,
1.84 + PRE_HEAP = -1,
1.85 + POST_HEAP = -2
1.86 + };
1.87 +
1.88 + private:
1.89 + std::vector<Pair> data;
1.90 + Compare comp;
1.91 + ItemIntMap &iim;
1.92 + int K;
1.93 +
1.94 + public:
1.95 + /// \brief The constructor.
1.96 + ///
1.97 + /// The constructor.
1.98 + /// \param _iim should be given to the constructor, since it is used
1.99 + /// internally to handle the cross references. The value of the map
1.100 + /// should be PRE_HEAP (-1) for each element.
1.101 + explicit KaryHeap(ItemIntMap &_iim, const int &_K=32) : iim(_iim), K(_K) {}
1.102 +
1.103 + /// \brief The constructor.
1.104 + ///
1.105 + /// The constructor.
1.106 + /// \param _iim should be given to the constructor, since it is used
1.107 + /// internally to handle the cross references. The value of the map
1.108 + /// should be PRE_HEAP (-1) for each element.
1.109 + ///
1.110 + /// \param _comp The comparator function object.
1.111 + KaryHeap(ItemIntMap &_iim, const Compare &_comp, const int &_K=32)
1.112 + : iim(_iim), comp(_comp), K(_K) {}
1.113 +
1.114 +
1.115 + /// The number of items stored in the heap.
1.116 + ///
1.117 + /// \brief Returns the number of items stored in the heap.
1.118 + int size() const { return data.size(); }
1.119 +
1.120 + /// \brief Checks if the heap stores no items.
1.121 + ///
1.122 + /// Returns \c true if and only if the heap stores no items.
1.123 + bool empty() const { return data.empty(); }
1.124 +
1.125 + /// \brief Make empty this heap.
1.126 + ///
1.127 + /// Make empty this heap. It does not change the cross reference map.
1.128 + /// If you want to reuse what is not surely empty you should first clear
1.129 + /// the heap and after that you should set the cross reference map for
1.130 + /// each item to \c PRE_HEAP.
1.131 + void clear() { data.clear(); }
1.132 +
1.133 + private:
1.134 + int parent(int i) { return (i-1)/K; }
1.135 + int first_child(int i) { return K*i+1; }
1.136 +
1.137 + bool less(const Pair &p1, const Pair &p2) const {
1.138 + return comp(p1.second, p2.second);
1.139 + }
1.140 +
1.141 + int find_min(const int child, const int length) {
1.142 + int min=child, i=1;
1.143 + while( i<K && child+i<length ) {
1.144 + if( less(data[child+i], data[min]) )
1.145 + min=child+i;
1.146 + ++i;
1.147 + }
1.148 + return min;
1.149 + }
1.150 +
1.151 + void bubble_up(int hole, Pair p) {
1.152 + int par = parent(hole);
1.153 + while( hole>0 && less(p,data[par]) ) {
1.154 + move(data[par],hole);
1.155 + hole = par;
1.156 + par = parent(hole);
1.157 + }
1.158 + move(p, hole);
1.159 + }
1.160 +
1.161 + void bubble_down(int hole, Pair p, int length) {
1.162 + if( length>1 ) {
1.163 + int child = first_child(hole);
1.164 + while( child<length ) {
1.165 + child = find_min(child, length);
1.166 + if( !less(data[child], p) )
1.167 + goto ok;
1.168 + move(data[child], hole);
1.169 + hole = child;
1.170 + child = first_child(hole);
1.171 + }
1.172 + }
1.173 + ok:
1.174 + move(p, hole);
1.175 + }
1.176 +
1.177 + void move(const Pair &p, int i) {
1.178 + data[i] = p;
1.179 + iim.set(p.first, i);
1.180 + }
1.181 +
1.182 + public:
1.183 + /// \brief Insert a pair of item and priority into the heap.
1.184 + ///
1.185 + /// Adds \c p.first to the heap with priority \c p.second.
1.186 + /// \param p The pair to insert.
1.187 + void push(const Pair &p) {
1.188 + int n = data.size();
1.189 + data.resize(n+1);
1.190 + bubble_up(n, p);
1.191 + }
1.192 +
1.193 + /// \brief Insert an item into the heap with the given heap.
1.194 + ///
1.195 + /// Adds \c i to the heap with priority \c p.
1.196 + /// \param i The item to insert.
1.197 + /// \param p The priority of the item.
1.198 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
1.199 +
1.200 + /// \brief Returns the item with minimum priority relative to \c Compare.
1.201 + ///
1.202 + /// This method returns the item with minimum priority relative to \c
1.203 + /// Compare.
1.204 + /// \pre The heap must be nonempty.
1.205 + Item top() const { return data[0].first; }
1.206 +
1.207 + /// \brief Returns the minimum priority relative to \c Compare.
1.208 + ///
1.209 + /// It returns the minimum priority relative to \c Compare.
1.210 + /// \pre The heap must be nonempty.
1.211 + Prio prio() const { return data[0].second; }
1.212 +
1.213 + /// \brief Deletes the item with minimum priority relative to \c Compare.
1.214 + ///
1.215 + /// This method deletes the item with minimum priority relative to \c
1.216 + /// Compare from the heap.
1.217 + /// \pre The heap must be non-empty.
1.218 + void pop() {
1.219 + int n = data.size()-1;
1.220 + iim.set(data[0].first, POST_HEAP);
1.221 + if (n>0) bubble_down(0, data[n], n);
1.222 + data.pop_back();
1.223 + }
1.224 +
1.225 + /// \brief Deletes \c i from the heap.
1.226 + ///
1.227 + /// This method deletes item \c i from the heap.
1.228 + /// \param i The item to erase.
1.229 + /// \pre The item should be in the heap.
1.230 + void erase(const Item &i) {
1.231 + int h = iim[i];
1.232 + int n = data.size()-1;
1.233 + iim.set(data[h].first, POST_HEAP);
1.234 + if( h<n ) {
1.235 + if( less(data[parent(h)], data[n]) )
1.236 + bubble_down(h, data[n], n);
1.237 + else
1.238 + bubble_up(h, data[n]);
1.239 + }
1.240 + data.pop_back();
1.241 + }
1.242 +
1.243 +
1.244 + /// \brief Returns the priority of \c i.
1.245 + ///
1.246 + /// This function returns the priority of item \c i.
1.247 + /// \pre \c i must be in the heap.
1.248 + /// \param i The item.
1.249 + Prio operator[](const Item &i) const {
1.250 + int idx = iim[i];
1.251 + return data[idx].second;
1.252 + }
1.253 +
1.254 + /// \brief \c i gets to the heap with priority \c p independently
1.255 + /// if \c i was already there.
1.256 + ///
1.257 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.258 + /// in the heap and sets the priority of \c i to \c p otherwise.
1.259 + /// \param i The item.
1.260 + /// \param p The priority.
1.261 + void set(const Item &i, const Prio &p) {
1.262 + int idx = iim[i];
1.263 + if( idx<0 )
1.264 + push(i,p);
1.265 + else if( comp(p, data[idx].second) )
1.266 + bubble_up(idx, Pair(i,p));
1.267 + else
1.268 + bubble_down(idx, Pair(i,p), data.size());
1.269 + }
1.270 +
1.271 + /// \brief Decreases the priority of \c i to \c p.
1.272 + ///
1.273 + /// This method decreases the priority of item \c i to \c p.
1.274 + /// \pre \c i must be stored in the heap with priority at least \c
1.275 + /// p relative to \c Compare.
1.276 + /// \param i The item.
1.277 + /// \param p The priority.
1.278 + void decrease(const Item &i, const Prio &p) {
1.279 + int idx = iim[i];
1.280 + bubble_up(idx, Pair(i,p));
1.281 + }
1.282 +
1.283 + /// \brief Increases the priority of \c i to \c p.
1.284 + ///
1.285 + /// This method sets the priority of item \c i to \c p.
1.286 + /// \pre \c i must be stored in the heap with priority at most \c
1.287 + /// p relative to \c Compare.
1.288 + /// \param i The item.
1.289 + /// \param p The priority.
1.290 + void increase(const Item &i, const Prio &p) {
1.291 + int idx = iim[i];
1.292 + bubble_down(idx, Pair(i,p), data.size());
1.293 + }
1.294 +
1.295 + /// \brief Returns if \c item is in, has already been in, or has
1.296 + /// never been in the heap.
1.297 + ///
1.298 + /// This method returns PRE_HEAP if \c item has never been in the
1.299 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.300 + /// otherwise. In the latter case it is possible that \c item will
1.301 + /// get back to the heap again.
1.302 + /// \param i The item.
1.303 + State state(const Item &i) const {
1.304 + int s = iim[i];
1.305 + if (s>=0) s=0;
1.306 + return State(s);
1.307 + }
1.308 +
1.309 + /// \brief Sets the state of the \c item in the heap.
1.310 + ///
1.311 + /// Sets the state of the \c item in the heap. It can be used to
1.312 + /// manually clear the heap when it is important to achive the
1.313 + /// better time complexity.
1.314 + /// \param i The item.
1.315 + /// \param st The state. It should not be \c IN_HEAP.
1.316 + void state(const Item& i, State st) {
1.317 + switch (st) {
1.318 + case POST_HEAP:
1.319 + case PRE_HEAP:
1.320 + if (state(i) == IN_HEAP) erase(i);
1.321 + iim[i] = st;
1.322 + break;
1.323 + case IN_HEAP:
1.324 + break;
1.325 + }
1.326 + }
1.327 +
1.328 + /// \brief Replaces an item in the heap.
1.329 + ///
1.330 + /// The \c i item is replaced with \c j item. The \c i item should
1.331 + /// be in the heap, while the \c j should be out of the heap. The
1.332 + /// \c i item will out of the heap and \c j will be in the heap
1.333 + /// with the same prioriority as prevoiusly the \c i item.
1.334 + void replace(const Item& i, const Item& j) {
1.335 + int idx=iim[i];
1.336 + iim.set(i, iim[j]);
1.337 + iim.set(j, idx);
1.338 + data[idx].first=j;
1.339 + }
1.340 +
1.341 + }; // class KaryHeap
1.342 +
1.343 +} // namespace lemon
1.344 +
1.345 +#endif // LEMON_KARY_HEAP_H