[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[100] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[100] | 4 | * |
---|
[463] | 5 | * Copyright (C) 2003-2009 |
---|
[100] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_BIN_HEAP_H |
---|
| 20 | #define LEMON_BIN_HEAP_H |
---|
| 21 | |
---|
[757] | 22 | ///\ingroup heaps |
---|
[100] | 23 | ///\file |
---|
[756] | 24 | ///\brief Binary heap implementation. |
---|
[100] | 25 | |
---|
| 26 | #include <vector> |
---|
| 27 | #include <utility> |
---|
| 28 | #include <functional> |
---|
| 29 | |
---|
| 30 | namespace lemon { |
---|
| 31 | |
---|
[757] | 32 | /// \ingroup heaps |
---|
[100] | 33 | /// |
---|
[756] | 34 | /// \brief Binary heap data structure. |
---|
[100] | 35 | /// |
---|
[756] | 36 | /// This class implements the \e binary \e heap data structure. |
---|
| 37 | /// It fully conforms to the \ref concepts::Heap "heap concept". |
---|
[730] | 38 | /// |
---|
[756] | 39 | /// \tparam PR Type of the priorities of the items. |
---|
| 40 | /// \tparam IM A read-writable item map with \c int values, used |
---|
| 41 | /// internally to handle the cross references. |
---|
| 42 | /// \tparam CMP A functor class for comparing the priorities. |
---|
| 43 | /// The default is \c std::less<PR>. |
---|
| 44 | #ifdef DOXYGEN |
---|
| 45 | template <typename PR, typename IM, typename CMP> |
---|
| 46 | #else |
---|
[730] | 47 | template <typename PR, typename IM, typename CMP = std::less<PR> > |
---|
[756] | 48 | #endif |
---|
[100] | 49 | class BinHeap { |
---|
[756] | 50 | public: |
---|
[100] | 51 | |
---|
[756] | 52 | /// Type of the item-int map. |
---|
[606] | 53 | typedef IM ItemIntMap; |
---|
[756] | 54 | /// Type of the priorities. |
---|
[606] | 55 | typedef PR Prio; |
---|
[756] | 56 | /// Type of the items stored in the heap. |
---|
[100] | 57 | typedef typename ItemIntMap::Key Item; |
---|
[756] | 58 | /// Type of the item-priority pairs. |
---|
[100] | 59 | typedef std::pair<Item,Prio> Pair; |
---|
[756] | 60 | /// Functor type for comparing the priorities. |
---|
[730] | 61 | typedef CMP Compare; |
---|
[100] | 62 | |
---|
[756] | 63 | /// \brief Type to represent the states of the items. |
---|
[100] | 64 | /// |
---|
[756] | 65 | /// Each item has a state associated to it. It can be "in heap", |
---|
| 66 | /// "pre-heap" or "post-heap". The latter two are indifferent from the |
---|
[100] | 67 | /// heap's point of view, but may be useful to the user. |
---|
| 68 | /// |
---|
[606] | 69 | /// The item-int map must be initialized in such way that it assigns |
---|
| 70 | /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
---|
[100] | 71 | enum State { |
---|
[631] | 72 | IN_HEAP = 0, ///< = 0. |
---|
| 73 | PRE_HEAP = -1, ///< = -1. |
---|
| 74 | POST_HEAP = -2 ///< = -2. |
---|
[100] | 75 | }; |
---|
| 76 | |
---|
| 77 | private: |
---|
[606] | 78 | std::vector<Pair> _data; |
---|
| 79 | Compare _comp; |
---|
| 80 | ItemIntMap &_iim; |
---|
[100] | 81 | |
---|
| 82 | public: |
---|
[756] | 83 | |
---|
| 84 | /// \brief Constructor. |
---|
[100] | 85 | /// |
---|
[756] | 86 | /// Constructor. |
---|
| 87 | /// \param map A map that assigns \c int values to the items. |
---|
| 88 | /// It is used internally to handle the cross references. |
---|
| 89 | /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
---|
[606] | 90 | explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
---|
[209] | 91 | |
---|
[756] | 92 | /// \brief Constructor. |
---|
[100] | 93 | /// |
---|
[756] | 94 | /// Constructor. |
---|
| 95 | /// \param map A map that assigns \c int values to the items. |
---|
| 96 | /// It is used internally to handle the cross references. |
---|
| 97 | /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
---|
| 98 | /// \param comp The function object used for comparing the priorities. |
---|
[606] | 99 | BinHeap(ItemIntMap &map, const Compare &comp) |
---|
| 100 | : _iim(map), _comp(comp) {} |
---|
[100] | 101 | |
---|
| 102 | |
---|
[756] | 103 | /// \brief The number of items stored in the heap. |
---|
[100] | 104 | /// |
---|
[756] | 105 | /// This function returns the number of items stored in the heap. |
---|
[606] | 106 | int size() const { return _data.size(); } |
---|
[209] | 107 | |
---|
[756] | 108 | /// \brief Check if the heap is empty. |
---|
[100] | 109 | /// |
---|
[756] | 110 | /// This function returns \c true if the heap is empty. |
---|
[606] | 111 | bool empty() const { return _data.empty(); } |
---|
[100] | 112 | |
---|
[756] | 113 | /// \brief Make the heap empty. |
---|
[209] | 114 | /// |
---|
[756] | 115 | /// This functon makes the heap empty. |
---|
| 116 | /// It does not change the cross reference map. If you want to reuse |
---|
| 117 | /// a heap that is not surely empty, you should first clear it and |
---|
| 118 | /// then you should set the cross reference map to \c PRE_HEAP |
---|
| 119 | /// for each item. |
---|
[209] | 120 | void clear() { |
---|
[606] | 121 | _data.clear(); |
---|
[100] | 122 | } |
---|
| 123 | |
---|
| 124 | private: |
---|
| 125 | static int parent(int i) { return (i-1)/2; } |
---|
| 126 | |
---|
[758] | 127 | static int secondChild(int i) { return 2*i+2; } |
---|
[100] | 128 | bool less(const Pair &p1, const Pair &p2) const { |
---|
[606] | 129 | return _comp(p1.second, p2.second); |
---|
[100] | 130 | } |
---|
| 131 | |
---|
[758] | 132 | int bubbleUp(int hole, Pair p) { |
---|
[100] | 133 | int par = parent(hole); |
---|
[606] | 134 | while( hole>0 && less(p,_data[par]) ) { |
---|
| 135 | move(_data[par],hole); |
---|
[209] | 136 | hole = par; |
---|
| 137 | par = parent(hole); |
---|
[100] | 138 | } |
---|
| 139 | move(p, hole); |
---|
| 140 | return hole; |
---|
| 141 | } |
---|
| 142 | |
---|
[758] | 143 | int bubbleDown(int hole, Pair p, int length) { |
---|
| 144 | int child = secondChild(hole); |
---|
[100] | 145 | while(child < length) { |
---|
[606] | 146 | if( less(_data[child-1], _data[child]) ) { |
---|
[209] | 147 | --child; |
---|
| 148 | } |
---|
[606] | 149 | if( !less(_data[child], p) ) |
---|
[209] | 150 | goto ok; |
---|
[606] | 151 | move(_data[child], hole); |
---|
[209] | 152 | hole = child; |
---|
[758] | 153 | child = secondChild(hole); |
---|
[100] | 154 | } |
---|
| 155 | child--; |
---|
[606] | 156 | if( child<length && less(_data[child], p) ) { |
---|
| 157 | move(_data[child], hole); |
---|
[209] | 158 | hole=child; |
---|
[100] | 159 | } |
---|
| 160 | ok: |
---|
| 161 | move(p, hole); |
---|
| 162 | return hole; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void move(const Pair &p, int i) { |
---|
[606] | 166 | _data[i] = p; |
---|
| 167 | _iim.set(p.first, i); |
---|
[100] | 168 | } |
---|
| 169 | |
---|
| 170 | public: |
---|
[756] | 171 | |
---|
[100] | 172 | /// \brief Insert a pair of item and priority into the heap. |
---|
| 173 | /// |
---|
[756] | 174 | /// This function inserts \c p.first to the heap with priority |
---|
| 175 | /// \c p.second. |
---|
[100] | 176 | /// \param p The pair to insert. |
---|
[756] | 177 | /// \pre \c p.first must not be stored in the heap. |
---|
[100] | 178 | void push(const Pair &p) { |
---|
[606] | 179 | int n = _data.size(); |
---|
| 180 | _data.resize(n+1); |
---|
[758] | 181 | bubbleUp(n, p); |
---|
[100] | 182 | } |
---|
| 183 | |
---|
[756] | 184 | /// \brief Insert an item into the heap with the given priority. |
---|
[209] | 185 | /// |
---|
[756] | 186 | /// This function inserts the given item into the heap with the |
---|
| 187 | /// given priority. |
---|
[100] | 188 | /// \param i The item to insert. |
---|
| 189 | /// \param p The priority of the item. |
---|
[756] | 190 | /// \pre \e i must not be stored in the heap. |
---|
[100] | 191 | void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
---|
| 192 | |
---|
[756] | 193 | /// \brief Return the item having minimum priority. |
---|
[100] | 194 | /// |
---|
[756] | 195 | /// This function returns the item having minimum priority. |
---|
| 196 | /// \pre The heap must be non-empty. |
---|
[100] | 197 | Item top() const { |
---|
[606] | 198 | return _data[0].first; |
---|
[100] | 199 | } |
---|
| 200 | |
---|
[756] | 201 | /// \brief The minimum priority. |
---|
[100] | 202 | /// |
---|
[756] | 203 | /// This function returns the minimum priority. |
---|
| 204 | /// \pre The heap must be non-empty. |
---|
[100] | 205 | Prio prio() const { |
---|
[606] | 206 | return _data[0].second; |
---|
[100] | 207 | } |
---|
| 208 | |
---|
[756] | 209 | /// \brief Remove the item having minimum priority. |
---|
[100] | 210 | /// |
---|
[756] | 211 | /// This function removes the item having minimum priority. |
---|
[209] | 212 | /// \pre The heap must be non-empty. |
---|
[100] | 213 | void pop() { |
---|
[606] | 214 | int n = _data.size()-1; |
---|
| 215 | _iim.set(_data[0].first, POST_HEAP); |
---|
[100] | 216 | if (n > 0) { |
---|
[758] | 217 | bubbleDown(0, _data[n], n); |
---|
[100] | 218 | } |
---|
[606] | 219 | _data.pop_back(); |
---|
[100] | 220 | } |
---|
| 221 | |
---|
[756] | 222 | /// \brief Remove the given item from the heap. |
---|
[100] | 223 | /// |
---|
[756] | 224 | /// This function removes the given item from the heap if it is |
---|
| 225 | /// already stored. |
---|
| 226 | /// \param i The item to delete. |
---|
| 227 | /// \pre \e i must be in the heap. |
---|
[100] | 228 | void erase(const Item &i) { |
---|
[606] | 229 | int h = _iim[i]; |
---|
| 230 | int n = _data.size()-1; |
---|
| 231 | _iim.set(_data[h].first, POST_HEAP); |
---|
[100] | 232 | if( h < n ) { |
---|
[758] | 233 | if ( bubbleUp(h, _data[n]) == h) { |
---|
| 234 | bubbleDown(h, _data[n], n); |
---|
[209] | 235 | } |
---|
[100] | 236 | } |
---|
[606] | 237 | _data.pop_back(); |
---|
[100] | 238 | } |
---|
| 239 | |
---|
[756] | 240 | /// \brief The priority of the given item. |
---|
[100] | 241 | /// |
---|
[756] | 242 | /// This function returns the priority of the given item. |
---|
[606] | 243 | /// \param i The item. |
---|
[756] | 244 | /// \pre \e i must be in the heap. |
---|
[100] | 245 | Prio operator[](const Item &i) const { |
---|
[606] | 246 | int idx = _iim[i]; |
---|
| 247 | return _data[idx].second; |
---|
[100] | 248 | } |
---|
| 249 | |
---|
[756] | 250 | /// \brief Set the priority of an item or insert it, if it is |
---|
| 251 | /// not stored in the heap. |
---|
[100] | 252 | /// |
---|
[756] | 253 | /// This method sets the priority of the given item if it is |
---|
| 254 | /// already stored in the heap. Otherwise it inserts the given |
---|
| 255 | /// item into the heap with the given priority. |
---|
[100] | 256 | /// \param i The item. |
---|
| 257 | /// \param p The priority. |
---|
| 258 | void set(const Item &i, const Prio &p) { |
---|
[606] | 259 | int idx = _iim[i]; |
---|
[100] | 260 | if( idx < 0 ) { |
---|
[209] | 261 | push(i,p); |
---|
[100] | 262 | } |
---|
[606] | 263 | else if( _comp(p, _data[idx].second) ) { |
---|
[758] | 264 | bubbleUp(idx, Pair(i,p)); |
---|
[100] | 265 | } |
---|
| 266 | else { |
---|
[758] | 267 | bubbleDown(idx, Pair(i,p), _data.size()); |
---|
[100] | 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
[756] | 271 | /// \brief Decrease the priority of an item to the given value. |
---|
[100] | 272 | /// |
---|
[756] | 273 | /// This function decreases the priority of an item to the given value. |
---|
[606] | 274 | /// \param i The item. |
---|
| 275 | /// \param p The priority. |
---|
[756] | 276 | /// \pre \e i must be stored in the heap with priority at least \e p. |
---|
[100] | 277 | void decrease(const Item &i, const Prio &p) { |
---|
[606] | 278 | int idx = _iim[i]; |
---|
[758] | 279 | bubbleUp(idx, Pair(i,p)); |
---|
[100] | 280 | } |
---|
[209] | 281 | |
---|
[756] | 282 | /// \brief Increase the priority of an item to the given value. |
---|
[100] | 283 | /// |
---|
[756] | 284 | /// This function increases the priority of an item to the given value. |
---|
[606] | 285 | /// \param i The item. |
---|
| 286 | /// \param p The priority. |
---|
[756] | 287 | /// \pre \e i must be stored in the heap with priority at most \e p. |
---|
[100] | 288 | void increase(const Item &i, const Prio &p) { |
---|
[606] | 289 | int idx = _iim[i]; |
---|
[758] | 290 | bubbleDown(idx, Pair(i,p), _data.size()); |
---|
[100] | 291 | } |
---|
| 292 | |
---|
[756] | 293 | /// \brief Return the state of an item. |
---|
[100] | 294 | /// |
---|
[756] | 295 | /// This method returns \c PRE_HEAP if the given item has never |
---|
| 296 | /// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
---|
| 297 | /// and \c POST_HEAP otherwise. |
---|
| 298 | /// In the latter case it is possible that the item will get back |
---|
| 299 | /// to the heap again. |
---|
[100] | 300 | /// \param i The item. |
---|
| 301 | State state(const Item &i) const { |
---|
[606] | 302 | int s = _iim[i]; |
---|
[100] | 303 | if( s>=0 ) |
---|
[209] | 304 | s=0; |
---|
[100] | 305 | return State(s); |
---|
| 306 | } |
---|
| 307 | |
---|
[756] | 308 | /// \brief Set the state of an item in the heap. |
---|
[100] | 309 | /// |
---|
[756] | 310 | /// This function sets the state of the given item in the heap. |
---|
| 311 | /// It can be used to manually clear the heap when it is important |
---|
| 312 | /// to achive better time complexity. |
---|
[100] | 313 | /// \param i The item. |
---|
[209] | 314 | /// \param st The state. It should not be \c IN_HEAP. |
---|
[100] | 315 | void state(const Item& i, State st) { |
---|
| 316 | switch (st) { |
---|
| 317 | case POST_HEAP: |
---|
| 318 | case PRE_HEAP: |
---|
| 319 | if (state(i) == IN_HEAP) { |
---|
| 320 | erase(i); |
---|
| 321 | } |
---|
[606] | 322 | _iim[i] = st; |
---|
[100] | 323 | break; |
---|
| 324 | case IN_HEAP: |
---|
| 325 | break; |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
[756] | 329 | /// \brief Replace an item in the heap. |
---|
[100] | 330 | /// |
---|
[756] | 331 | /// This function replaces item \c i with item \c j. |
---|
| 332 | /// Item \c i must be in the heap, while \c j must be out of the heap. |
---|
| 333 | /// After calling this method, item \c i will be out of the |
---|
| 334 | /// heap and \c j will be in the heap with the same prioriority |
---|
| 335 | /// as item \c i had before. |
---|
[100] | 336 | void replace(const Item& i, const Item& j) { |
---|
[606] | 337 | int idx = _iim[i]; |
---|
| 338 | _iim.set(i, _iim[j]); |
---|
| 339 | _iim.set(j, idx); |
---|
| 340 | _data[idx].first = j; |
---|
[100] | 341 | } |
---|
| 342 | |
---|
| 343 | }; // class BinHeap |
---|
[209] | 344 | |
---|
[100] | 345 | } // namespace lemon |
---|
| 346 | |
---|
| 347 | #endif // LEMON_BIN_HEAP_H |
---|