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