[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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 | */ |
---|
[255] | 18 | |
---|
[921] | 19 | #ifndef LEMON_FIB_HEAP_H |
---|
| 20 | #define LEMON_FIB_HEAP_H |
---|
[255] | 21 | |
---|
[857] | 22 | ///\file |
---|
[491] | 23 | ///\ingroup auxdat |
---|
[255] | 24 | ///\brief Fibonacci Heap implementation. |
---|
| 25 | |
---|
| 26 | #include <vector> |
---|
| 27 | #include <functional> |
---|
[1332] | 28 | #include <cmath> |
---|
[255] | 29 | |
---|
[921] | 30 | namespace lemon { |
---|
[255] | 31 | |
---|
[1834] | 32 | /// \ingroup auxdat |
---|
[430] | 33 | |
---|
[857] | 34 | /// Fibonacci Heap. |
---|
[373] | 35 | |
---|
[857] | 36 | ///This class implements the \e Fibonacci \e heap data structure. A \e heap |
---|
| 37 | ///is a data structure for storing items with specified values called \e |
---|
| 38 | ///priorities in such a way that finding the item with minimum priority is |
---|
[911] | 39 | ///efficient. \c Compare specifies the ordering of the priorities. In a heap |
---|
[857] | 40 | ///one can change the priority of an item, add or erase an item, etc. |
---|
| 41 | /// |
---|
| 42 | ///The methods \ref increase and \ref erase are not efficient in a Fibonacci |
---|
| 43 | ///heap. In case of many calls to these operations, it is better to use a |
---|
| 44 | ///\e binary \e heap. |
---|
| 45 | /// |
---|
| 46 | ///\param Prio Type of the priority of the items. |
---|
[1204] | 47 | ///\param ItemIntMap A read and writable Item int map, used internally |
---|
| 48 | ///to handle the cross references. |
---|
[857] | 49 | ///\param Compare A class for the ordering of the priorities. The |
---|
| 50 | ///default is \c std::less<Prio>. |
---|
| 51 | /// |
---|
[967] | 52 | ///\sa BinHeap |
---|
| 53 | ///\sa Dijkstra |
---|
[857] | 54 | ///\author Jacint Szabo |
---|
| 55 | |
---|
[373] | 56 | #ifdef DOXYGEN |
---|
[2263] | 57 | template <typename Prio, |
---|
[373] | 58 | typename ItemIntMap, |
---|
| 59 | typename Compare> |
---|
| 60 | #else |
---|
[2263] | 61 | template <typename Prio, |
---|
[373] | 62 | typename ItemIntMap, |
---|
[255] | 63 | typename Compare = std::less<Prio> > |
---|
[373] | 64 | #endif |
---|
[255] | 65 | class FibHeap { |
---|
[2263] | 66 | public: |
---|
| 67 | typedef typename ItemIntMap::Key Item; |
---|
[255] | 68 | typedef Prio PrioType; |
---|
| 69 | |
---|
[373] | 70 | private: |
---|
[255] | 71 | class store; |
---|
| 72 | |
---|
| 73 | std::vector<store> container; |
---|
| 74 | int minimum; |
---|
| 75 | ItemIntMap &iimap; |
---|
| 76 | Compare comp; |
---|
| 77 | int num_items; |
---|
[373] | 78 | |
---|
[255] | 79 | public: |
---|
[1127] | 80 | ///Status of the nodes |
---|
[255] | 81 | enum state_enum { |
---|
[1127] | 82 | ///The node is in the heap |
---|
[255] | 83 | IN_HEAP = 0, |
---|
[1127] | 84 | ///The node has never been in the heap |
---|
[255] | 85 | PRE_HEAP = -1, |
---|
[1127] | 86 | ///The node was in the heap but it got out of it |
---|
[255] | 87 | POST_HEAP = -2 |
---|
| 88 | }; |
---|
| 89 | |
---|
[1717] | 90 | /// \brief The constructor |
---|
| 91 | /// |
---|
| 92 | /// \c _iimap should be given to the constructor, since it is |
---|
| 93 | /// used internally to handle the cross references. |
---|
[1185] | 94 | explicit FibHeap(ItemIntMap &_iimap) |
---|
| 95 | : minimum(0), iimap(_iimap), num_items() {} |
---|
[1270] | 96 | |
---|
[1717] | 97 | /// \brief The constructor |
---|
| 98 | /// |
---|
| 99 | /// \c _iimap should be given to the constructor, since it is used |
---|
| 100 | /// internally to handle the cross references. \c _comp is an |
---|
| 101 | /// object for ordering of the priorities. |
---|
[373] | 102 | FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(0), |
---|
[1270] | 103 | iimap(_iimap), comp(_comp), num_items() {} |
---|
[255] | 104 | |
---|
[1717] | 105 | /// \brief The number of items stored in the heap. |
---|
| 106 | /// |
---|
| 107 | /// Returns the number of items stored in the heap. |
---|
[373] | 108 | int size() const { return num_items; } |
---|
| 109 | |
---|
[1717] | 110 | /// \brief Checks if the heap stores no items. |
---|
| 111 | /// |
---|
| 112 | /// Returns \c true if and only if the heap stores no items. |
---|
[373] | 113 | bool empty() const { return num_items==0; } |
---|
| 114 | |
---|
[1717] | 115 | /// \brief Make empty this heap. |
---|
| 116 | /// |
---|
[2050] | 117 | /// Make empty this heap. It does not change the cross reference |
---|
| 118 | /// map. If you want to reuse a heap what is not surely empty you |
---|
| 119 | /// should first clear the heap and after that you should set the |
---|
| 120 | /// cross reference map for each item to \c PRE_HEAP. |
---|
[1753] | 121 | void clear() { |
---|
[1717] | 122 | container.clear(); minimum = 0; num_items = 0; |
---|
| 123 | } |
---|
[373] | 124 | |
---|
[1717] | 125 | /// \brief \c item gets to the heap with priority \c value independently |
---|
| 126 | /// if \c item was already there. |
---|
| 127 | /// |
---|
| 128 | /// This method calls \ref push(\c item, \c value) if \c item is not |
---|
| 129 | /// stored in the heap and it calls \ref decrease(\c item, \c value) or |
---|
| 130 | /// \ref increase(\c item, \c value) otherwise. |
---|
[387] | 131 | void set (Item const item, PrioType const value); |
---|
[373] | 132 | |
---|
[1717] | 133 | /// \brief Adds \c item to the heap with priority \c value. |
---|
| 134 | /// |
---|
| 135 | /// Adds \c item to the heap with priority \c value. |
---|
| 136 | /// \pre \c item must not be stored in the heap. |
---|
[387] | 137 | void push (Item const item, PrioType const value); |
---|
[373] | 138 | |
---|
[1717] | 139 | /// \brief Returns the item with minimum priority relative to \c Compare. |
---|
| 140 | /// |
---|
| 141 | /// This method returns the item with minimum priority relative to \c |
---|
| 142 | /// Compare. |
---|
| 143 | /// \pre The heap must be nonempty. |
---|
[373] | 144 | Item top() const { return container[minimum].name; } |
---|
| 145 | |
---|
[1717] | 146 | /// \brief Returns the minimum priority relative to \c Compare. |
---|
| 147 | /// |
---|
| 148 | /// It returns the minimum priority relative to \c Compare. |
---|
| 149 | /// \pre The heap must be nonempty. |
---|
[373] | 150 | PrioType prio() const { return container[minimum].prio; } |
---|
| 151 | |
---|
[1717] | 152 | /// \brief Returns the priority of \c item. |
---|
| 153 | /// |
---|
| 154 | /// This function returns the priority of \c item. |
---|
| 155 | /// \pre \c item must be in the heap. |
---|
[387] | 156 | PrioType& operator[](const Item& item) { |
---|
| 157 | return container[iimap[item]].prio; |
---|
| 158 | } |
---|
[373] | 159 | |
---|
[1717] | 160 | /// \brief Returns the priority of \c item. |
---|
| 161 | /// |
---|
| 162 | /// It returns the priority of \c item. |
---|
| 163 | /// \pre \c item must be in the heap. |
---|
[387] | 164 | const PrioType& operator[](const Item& item) const { |
---|
| 165 | return container[iimap[item]].prio; |
---|
[255] | 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[1717] | 169 | /// \brief Deletes the item with minimum priority relative to \c Compare. |
---|
| 170 | /// |
---|
| 171 | /// This method deletes the item with minimum priority relative to \c |
---|
| 172 | /// Compare from the heap. |
---|
| 173 | /// \pre The heap must be non-empty. |
---|
[373] | 174 | void pop(); |
---|
| 175 | |
---|
[1717] | 176 | /// \brief Deletes \c item from the heap. |
---|
| 177 | /// |
---|
| 178 | /// This method deletes \c item from the heap, if \c item was already |
---|
| 179 | /// stored in the heap. It is quite inefficient in Fibonacci heaps. |
---|
[387] | 180 | void erase (const Item& item); |
---|
[373] | 181 | |
---|
[1717] | 182 | /// \brief Decreases the priority of \c item to \c value. |
---|
| 183 | /// |
---|
| 184 | /// This method decreases the priority of \c item to \c value. |
---|
| 185 | /// \pre \c item must be stored in the heap with priority at least \c |
---|
| 186 | /// value relative to \c Compare. |
---|
[387] | 187 | void decrease (Item item, PrioType const value); |
---|
[373] | 188 | |
---|
[1717] | 189 | /// \brief Increases the priority of \c item to \c value. |
---|
| 190 | /// |
---|
| 191 | /// This method sets the priority of \c item to \c value. Though |
---|
| 192 | /// there is no precondition on the priority of \c item, this |
---|
| 193 | /// method should be used only if it is indeed necessary to increase |
---|
| 194 | /// (relative to \c Compare) the priority of \c item, because this |
---|
| 195 | /// method is inefficient. |
---|
[387] | 196 | void increase (Item item, PrioType const value) { |
---|
| 197 | erase(item); |
---|
| 198 | push(item, value); |
---|
[373] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
[1717] | 202 | /// \brief Returns if \c item is in, has already been in, or has never |
---|
| 203 | /// been in the heap. |
---|
| 204 | /// |
---|
| 205 | /// This method returns PRE_HEAP if \c item has never been in the |
---|
| 206 | /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
---|
| 207 | /// otherwise. In the latter case it is possible that \c item will |
---|
| 208 | /// get back to the heap again. |
---|
[387] | 209 | state_enum state(const Item &item) const { |
---|
| 210 | int i=iimap[item]; |
---|
| 211 | if( i>=0 ) { |
---|
| 212 | if ( container[i].in ) i=0; |
---|
| 213 | else i=-2; |
---|
| 214 | } |
---|
| 215 | return state_enum(i); |
---|
| 216 | } |
---|
[1902] | 217 | |
---|
| 218 | /// \brief Sets the state of the \c item in the heap. |
---|
| 219 | /// |
---|
| 220 | /// Sets the state of the \c item in the heap. It can be used to |
---|
| 221 | /// manually clear the heap when it is important to achive the |
---|
| 222 | /// better time complexity. |
---|
| 223 | /// \param i The item. |
---|
| 224 | /// \param st The state. It should not be \c IN_HEAP. |
---|
| 225 | void state(const Item& i, state_enum st) { |
---|
| 226 | switch (st) { |
---|
| 227 | case POST_HEAP: |
---|
| 228 | case PRE_HEAP: |
---|
| 229 | if (state(i) == IN_HEAP) { |
---|
| 230 | erase(i); |
---|
| 231 | } |
---|
[1903] | 232 | iimap[i] = st; |
---|
[1902] | 233 | break; |
---|
[1906] | 234 | case IN_HEAP: |
---|
| 235 | break; |
---|
[1902] | 236 | } |
---|
| 237 | } |
---|
[387] | 238 | |
---|
| 239 | private: |
---|
| 240 | |
---|
| 241 | void balance(); |
---|
| 242 | void makeroot(int c); |
---|
| 243 | void cut(int a, int b); |
---|
| 244 | void cascade(int a); |
---|
| 245 | void fuse(int a, int b); |
---|
| 246 | void unlace(int a); |
---|
[373] | 247 | |
---|
| 248 | |
---|
[387] | 249 | class store { |
---|
| 250 | friend class FibHeap; |
---|
| 251 | |
---|
| 252 | Item name; |
---|
| 253 | int parent; |
---|
| 254 | int left_neighbor; |
---|
| 255 | int right_neighbor; |
---|
| 256 | int child; |
---|
| 257 | int degree; |
---|
| 258 | bool marked; |
---|
| 259 | bool in; |
---|
| 260 | PrioType prio; |
---|
| 261 | |
---|
| 262 | store() : parent(-1), child(-1), degree(), marked(false), in(true) {} |
---|
| 263 | }; |
---|
| 264 | }; |
---|
| 265 | |
---|
| 266 | |
---|
[373] | 267 | |
---|
| 268 | // ********************************************************************** |
---|
| 269 | // IMPLEMENTATIONS |
---|
| 270 | // ********************************************************************** |
---|
| 271 | |
---|
[2263] | 272 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 273 | typename Compare> |
---|
[2263] | 274 | void FibHeap<Prio, ItemIntMap, Compare>::set |
---|
[387] | 275 | (Item const item, PrioType const value) |
---|
| 276 | { |
---|
| 277 | int i=iimap[item]; |
---|
| 278 | if ( i >= 0 && container[i].in ) { |
---|
| 279 | if ( comp(value, container[i].prio) ) decrease(item, value); |
---|
| 280 | if ( comp(container[i].prio, value) ) increase(item, value); |
---|
| 281 | } else push(item, value); |
---|
| 282 | } |
---|
[255] | 283 | |
---|
[2263] | 284 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 285 | typename Compare> |
---|
[2263] | 286 | void FibHeap<Prio, ItemIntMap, Compare>::push |
---|
[387] | 287 | (Item const item, PrioType const value) { |
---|
| 288 | int i=iimap[item]; |
---|
[255] | 289 | if ( i < 0 ) { |
---|
| 290 | int s=container.size(); |
---|
[387] | 291 | iimap.set( item, s ); |
---|
[255] | 292 | store st; |
---|
[387] | 293 | st.name=item; |
---|
[255] | 294 | container.push_back(st); |
---|
| 295 | i=s; |
---|
| 296 | } else { |
---|
| 297 | container[i].parent=container[i].child=-1; |
---|
| 298 | container[i].degree=0; |
---|
| 299 | container[i].in=true; |
---|
| 300 | container[i].marked=false; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | if ( num_items ) { |
---|
| 304 | container[container[minimum].right_neighbor].left_neighbor=i; |
---|
| 305 | container[i].right_neighbor=container[minimum].right_neighbor; |
---|
| 306 | container[minimum].right_neighbor=i; |
---|
| 307 | container[i].left_neighbor=minimum; |
---|
| 308 | if ( comp( value, container[minimum].prio) ) minimum=i; |
---|
| 309 | } else { |
---|
| 310 | container[i].right_neighbor=container[i].left_neighbor=i; |
---|
| 311 | minimum=i; |
---|
| 312 | } |
---|
| 313 | container[i].prio=value; |
---|
| 314 | ++num_items; |
---|
| 315 | } |
---|
| 316 | |
---|
[2263] | 317 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 318 | typename Compare> |
---|
[2263] | 319 | void FibHeap<Prio, ItemIntMap, Compare>::pop() { |
---|
[255] | 320 | /*The first case is that there are only one root.*/ |
---|
| 321 | if ( container[minimum].left_neighbor==minimum ) { |
---|
| 322 | container[minimum].in=false; |
---|
| 323 | if ( container[minimum].degree!=0 ) { |
---|
| 324 | makeroot(container[minimum].child); |
---|
| 325 | minimum=container[minimum].child; |
---|
| 326 | balance(); |
---|
| 327 | } |
---|
| 328 | } else { |
---|
| 329 | int right=container[minimum].right_neighbor; |
---|
| 330 | unlace(minimum); |
---|
| 331 | container[minimum].in=false; |
---|
| 332 | if ( container[minimum].degree > 0 ) { |
---|
| 333 | int left=container[minimum].left_neighbor; |
---|
| 334 | int child=container[minimum].child; |
---|
| 335 | int last_child=container[child].left_neighbor; |
---|
| 336 | |
---|
| 337 | makeroot(child); |
---|
| 338 | |
---|
| 339 | container[left].right_neighbor=child; |
---|
| 340 | container[child].left_neighbor=left; |
---|
| 341 | container[right].left_neighbor=last_child; |
---|
| 342 | container[last_child].right_neighbor=right; |
---|
| 343 | } |
---|
| 344 | minimum=right; |
---|
| 345 | balance(); |
---|
| 346 | } // the case where there are more roots |
---|
| 347 | --num_items; |
---|
| 348 | } |
---|
| 349 | |
---|
[387] | 350 | |
---|
[2263] | 351 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 352 | typename Compare> |
---|
[2263] | 353 | void FibHeap<Prio, ItemIntMap, Compare>::erase |
---|
[387] | 354 | (const Item& item) { |
---|
| 355 | int i=iimap[item]; |
---|
[255] | 356 | |
---|
| 357 | if ( i >= 0 && container[i].in ) { |
---|
| 358 | if ( container[i].parent!=-1 ) { |
---|
| 359 | int p=container[i].parent; |
---|
| 360 | cut(i,p); |
---|
| 361 | cascade(p); |
---|
| 362 | } |
---|
| 363 | minimum=i; //As if its prio would be -infinity |
---|
| 364 | pop(); |
---|
| 365 | } |
---|
[387] | 366 | } |
---|
[255] | 367 | |
---|
[2263] | 368 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 369 | typename Compare> |
---|
[2263] | 370 | void FibHeap<Prio, ItemIntMap, Compare>::decrease |
---|
[387] | 371 | (Item item, PrioType const value) { |
---|
| 372 | int i=iimap[item]; |
---|
[255] | 373 | container[i].prio=value; |
---|
| 374 | int p=container[i].parent; |
---|
| 375 | |
---|
| 376 | if ( p!=-1 && comp(value, container[p].prio) ) { |
---|
| 377 | cut(i,p); |
---|
| 378 | cascade(p); |
---|
| 379 | } |
---|
| 380 | if ( comp(value, container[minimum].prio) ) minimum=i; |
---|
[387] | 381 | } |
---|
| 382 | |
---|
[255] | 383 | |
---|
[2263] | 384 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 385 | typename Compare> |
---|
[2263] | 386 | void FibHeap<Prio, ItemIntMap, Compare>::balance() { |
---|
[255] | 387 | |
---|
[1332] | 388 | int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1; |
---|
[255] | 389 | |
---|
| 390 | std::vector<int> A(maxdeg,-1); |
---|
| 391 | |
---|
| 392 | /* |
---|
| 393 | *Recall that now minimum does not point to the minimum prio element. |
---|
| 394 | *We set minimum to this during balance(). |
---|
| 395 | */ |
---|
| 396 | int anchor=container[minimum].left_neighbor; |
---|
| 397 | int next=minimum; |
---|
| 398 | bool end=false; |
---|
| 399 | |
---|
| 400 | do { |
---|
| 401 | int active=next; |
---|
| 402 | if ( anchor==active ) end=true; |
---|
| 403 | int d=container[active].degree; |
---|
| 404 | next=container[active].right_neighbor; |
---|
| 405 | |
---|
| 406 | while (A[d]!=-1) { |
---|
| 407 | if( comp(container[active].prio, container[A[d]].prio) ) { |
---|
| 408 | fuse(active,A[d]); |
---|
| 409 | } else { |
---|
| 410 | fuse(A[d],active); |
---|
| 411 | active=A[d]; |
---|
| 412 | } |
---|
| 413 | A[d]=-1; |
---|
| 414 | ++d; |
---|
| 415 | } |
---|
| 416 | A[d]=active; |
---|
| 417 | } while ( !end ); |
---|
| 418 | |
---|
| 419 | |
---|
| 420 | while ( container[minimum].parent >=0 ) minimum=container[minimum].parent; |
---|
| 421 | int s=minimum; |
---|
| 422 | int m=minimum; |
---|
| 423 | do { |
---|
| 424 | if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; |
---|
| 425 | s=container[s].right_neighbor; |
---|
| 426 | } while ( s != m ); |
---|
| 427 | } |
---|
| 428 | |
---|
[2263] | 429 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 430 | typename Compare> |
---|
[2263] | 431 | void FibHeap<Prio, ItemIntMap, Compare>::makeroot |
---|
[387] | 432 | (int c) { |
---|
[255] | 433 | int s=c; |
---|
| 434 | do { |
---|
| 435 | container[s].parent=-1; |
---|
| 436 | s=container[s].right_neighbor; |
---|
| 437 | } while ( s != c ); |
---|
| 438 | } |
---|
[387] | 439 | |
---|
| 440 | |
---|
[2263] | 441 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 442 | typename Compare> |
---|
[2263] | 443 | void FibHeap<Prio, ItemIntMap, Compare>::cut |
---|
[387] | 444 | (int a, int b) { |
---|
| 445 | /* |
---|
| 446 | *Replacing a from the children of b. |
---|
| 447 | */ |
---|
| 448 | --container[b].degree; |
---|
[255] | 449 | |
---|
[387] | 450 | if ( container[b].degree !=0 ) { |
---|
| 451 | int child=container[b].child; |
---|
| 452 | if ( child==a ) |
---|
| 453 | container[b].child=container[child].right_neighbor; |
---|
| 454 | unlace(a); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | |
---|
| 458 | /*Lacing a to the roots.*/ |
---|
| 459 | int right=container[minimum].right_neighbor; |
---|
| 460 | container[minimum].right_neighbor=a; |
---|
| 461 | container[a].left_neighbor=minimum; |
---|
| 462 | container[a].right_neighbor=right; |
---|
| 463 | container[right].left_neighbor=a; |
---|
| 464 | |
---|
| 465 | container[a].parent=-1; |
---|
| 466 | container[a].marked=false; |
---|
| 467 | } |
---|
| 468 | |
---|
[255] | 469 | |
---|
[2263] | 470 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 471 | typename Compare> |
---|
[2263] | 472 | void FibHeap<Prio, ItemIntMap, Compare>::cascade |
---|
[387] | 473 | (int a) |
---|
[255] | 474 | { |
---|
| 475 | if ( container[a].parent!=-1 ) { |
---|
| 476 | int p=container[a].parent; |
---|
| 477 | |
---|
| 478 | if ( container[a].marked==false ) container[a].marked=true; |
---|
| 479 | else { |
---|
| 480 | cut(a,p); |
---|
| 481 | cascade(p); |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | |
---|
[2263] | 487 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 488 | typename Compare> |
---|
[2263] | 489 | void FibHeap<Prio, ItemIntMap, Compare>::fuse |
---|
[387] | 490 | (int a, int b) { |
---|
[255] | 491 | unlace(b); |
---|
| 492 | |
---|
| 493 | /*Lacing b under a.*/ |
---|
| 494 | container[b].parent=a; |
---|
| 495 | |
---|
| 496 | if (container[a].degree==0) { |
---|
| 497 | container[b].left_neighbor=b; |
---|
| 498 | container[b].right_neighbor=b; |
---|
| 499 | container[a].child=b; |
---|
| 500 | } else { |
---|
| 501 | int child=container[a].child; |
---|
| 502 | int last_child=container[child].left_neighbor; |
---|
| 503 | container[child].left_neighbor=b; |
---|
| 504 | container[b].right_neighbor=child; |
---|
| 505 | container[last_child].right_neighbor=b; |
---|
| 506 | container[b].left_neighbor=last_child; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | ++container[a].degree; |
---|
| 510 | |
---|
| 511 | container[b].marked=false; |
---|
| 512 | } |
---|
| 513 | |
---|
[387] | 514 | |
---|
| 515 | /* |
---|
| 516 | *It is invoked only if a has siblings. |
---|
| 517 | */ |
---|
[2263] | 518 | template <typename Prio, typename ItemIntMap, |
---|
[387] | 519 | typename Compare> |
---|
[2263] | 520 | void FibHeap<Prio, ItemIntMap, Compare>::unlace |
---|
[387] | 521 | (int a) { |
---|
[255] | 522 | int leftn=container[a].left_neighbor; |
---|
| 523 | int rightn=container[a].right_neighbor; |
---|
| 524 | container[leftn].right_neighbor=rightn; |
---|
| 525 | container[rightn].left_neighbor=leftn; |
---|
[387] | 526 | } |
---|
[255] | 527 | |
---|
[430] | 528 | |
---|
[921] | 529 | } //namespace lemon |
---|
[477] | 530 | |
---|
[921] | 531 | #endif //LEMON_FIB_HEAP_H |
---|
[477] | 532 | |
---|