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