3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_FIB_HEAP_H
20 #define LEMON_FIB_HEAP_H
24 ///\brief Fibonacci Heap implementation.
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
39 ///efficient. \c Compare specifies the ordering of the priorities. In a heap
40 ///one can change the priority of an item, add or erase an item, etc.
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
46 ///\param Item Type of the items to be stored.
47 ///\param Prio Type of the priority of the items.
48 ///\param ItemIntMap A read and writable Item int map, used internally
49 ///to handle the cross references.
50 ///\param Compare A class for the ordering of the priorities. The
51 ///default is \c std::less<Prio>.
55 ///\author Jacint Szabo
58 template <typename Item,
63 template <typename Item,
66 typename Compare = std::less<Prio> >
70 typedef Prio PrioType;
75 std::vector<store> container;
82 ///Status of the nodes
84 ///The node is in the heap
86 ///The node has never been in the heap
88 ///The node was in the heap but it got out of it
92 /// \brief The constructor
94 /// \c _iimap should be given to the constructor, since it is
95 /// used internally to handle the cross references.
96 explicit FibHeap(ItemIntMap &_iimap)
97 : minimum(0), iimap(_iimap), num_items() {}
99 /// \brief The constructor
101 /// \c _iimap should be given to the constructor, since it is used
102 /// internally to handle the cross references. \c _comp is an
103 /// object for ordering of the priorities.
104 FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(0),
105 iimap(_iimap), comp(_comp), num_items() {}
107 /// \brief The number of items stored in the heap.
109 /// Returns the number of items stored in the heap.
110 int size() const { return num_items; }
112 /// \brief Checks if the heap stores no items.
114 /// Returns \c true if and only if the heap stores no items.
115 bool empty() const { return num_items==0; }
117 /// \brief Make empty this heap.
119 /// Make empty this heap.
121 if (num_items != 0) {
122 for (int i = 0; i < (int)container.size(); ++i) {
123 iimap[container[i].name] = -2;
126 container.clear(); minimum = 0; num_items = 0;
129 /// \brief \c item gets to the heap with priority \c value independently
130 /// if \c item was already there.
132 /// This method calls \ref push(\c item, \c value) if \c item is not
133 /// stored in the heap and it calls \ref decrease(\c item, \c value) or
134 /// \ref increase(\c item, \c value) otherwise.
135 void set (Item const item, PrioType const value);
137 /// \brief Adds \c item to the heap with priority \c value.
139 /// Adds \c item to the heap with priority \c value.
140 /// \pre \c item must not be stored in the heap.
141 void push (Item const item, PrioType const value);
143 /// \brief Returns the item with minimum priority relative to \c Compare.
145 /// This method returns the item with minimum priority relative to \c
147 /// \pre The heap must be nonempty.
148 Item top() const { return container[minimum].name; }
150 /// \brief Returns the minimum priority relative to \c Compare.
152 /// It returns the minimum priority relative to \c Compare.
153 /// \pre The heap must be nonempty.
154 PrioType prio() const { return container[minimum].prio; }
156 /// \brief Returns the priority of \c item.
158 /// This function returns the priority of \c item.
159 /// \pre \c item must be in the heap.
160 PrioType& operator[](const Item& item) {
161 return container[iimap[item]].prio;
164 /// \brief Returns the priority of \c item.
166 /// It returns the priority of \c item.
167 /// \pre \c item must be in the heap.
168 const PrioType& operator[](const Item& item) const {
169 return container[iimap[item]].prio;
173 /// \brief Deletes the item with minimum priority relative to \c Compare.
175 /// This method deletes the item with minimum priority relative to \c
176 /// Compare from the heap.
177 /// \pre The heap must be non-empty.
180 /// \brief Deletes \c item from the heap.
182 /// This method deletes \c item from the heap, if \c item was already
183 /// stored in the heap. It is quite inefficient in Fibonacci heaps.
184 void erase (const Item& item);
186 /// \brief Decreases the priority of \c item to \c value.
188 /// This method decreases the priority of \c item to \c value.
189 /// \pre \c item must be stored in the heap with priority at least \c
190 /// value relative to \c Compare.
191 void decrease (Item item, PrioType const value);
193 /// \brief Increases the priority of \c item to \c value.
195 /// This method sets the priority of \c item to \c value. Though
196 /// there is no precondition on the priority of \c item, this
197 /// method should be used only if it is indeed necessary to increase
198 /// (relative to \c Compare) the priority of \c item, because this
199 /// method is inefficient.
200 void increase (Item item, PrioType const value) {
206 /// \brief Returns if \c item is in, has already been in, or has never
207 /// been in the heap.
209 /// This method returns PRE_HEAP if \c item has never been in the
210 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
211 /// otherwise. In the latter case it is possible that \c item will
212 /// get back to the heap again.
213 state_enum state(const Item &item) const {
216 if ( container[i].in ) i=0;
219 return state_enum(i);
222 /// \brief Sets the state of the \c item in the heap.
224 /// Sets the state of the \c item in the heap. It can be used to
225 /// manually clear the heap when it is important to achive the
226 /// better time complexity.
227 /// \param i The item.
228 /// \param st The state. It should not be \c IN_HEAP.
229 void state(const Item& i, state_enum st) {
233 if (state(i) == IN_HEAP) {
246 void makeroot(int c);
247 void cut(int a, int b);
249 void fuse(int a, int b);
254 friend class FibHeap;
266 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
272 // **********************************************************************
274 // **********************************************************************
276 template <typename Item, typename Prio, typename ItemIntMap,
278 void FibHeap<Item, Prio, ItemIntMap, Compare>::set
279 (Item const item, PrioType const value)
282 if ( i >= 0 && container[i].in ) {
283 if ( comp(value, container[i].prio) ) decrease(item, value);
284 if ( comp(container[i].prio, value) ) increase(item, value);
285 } else push(item, value);
288 template <typename Item, typename Prio, typename ItemIntMap,
290 void FibHeap<Item, Prio, ItemIntMap, Compare>::push
291 (Item const item, PrioType const value) {
294 int s=container.size();
295 iimap.set( item, s );
298 container.push_back(st);
301 container[i].parent=container[i].child=-1;
302 container[i].degree=0;
303 container[i].in=true;
304 container[i].marked=false;
308 container[container[minimum].right_neighbor].left_neighbor=i;
309 container[i].right_neighbor=container[minimum].right_neighbor;
310 container[minimum].right_neighbor=i;
311 container[i].left_neighbor=minimum;
312 if ( comp( value, container[minimum].prio) ) minimum=i;
314 container[i].right_neighbor=container[i].left_neighbor=i;
317 container[i].prio=value;
321 template <typename Item, typename Prio, typename ItemIntMap,
323 void FibHeap<Item, Prio, ItemIntMap, Compare>::pop() {
324 /*The first case is that there are only one root.*/
325 if ( container[minimum].left_neighbor==minimum ) {
326 container[minimum].in=false;
327 if ( container[minimum].degree!=0 ) {
328 makeroot(container[minimum].child);
329 minimum=container[minimum].child;
333 int right=container[minimum].right_neighbor;
335 container[minimum].in=false;
336 if ( container[minimum].degree > 0 ) {
337 int left=container[minimum].left_neighbor;
338 int child=container[minimum].child;
339 int last_child=container[child].left_neighbor;
343 container[left].right_neighbor=child;
344 container[child].left_neighbor=left;
345 container[right].left_neighbor=last_child;
346 container[last_child].right_neighbor=right;
350 } // the case where there are more roots
355 template <typename Item, typename Prio, typename ItemIntMap,
357 void FibHeap<Item, Prio, ItemIntMap, Compare>::erase
361 if ( i >= 0 && container[i].in ) {
362 if ( container[i].parent!=-1 ) {
363 int p=container[i].parent;
367 minimum=i; //As if its prio would be -infinity
372 template <typename Item, typename Prio, typename ItemIntMap,
374 void FibHeap<Item, Prio, ItemIntMap, Compare>::decrease
375 (Item item, PrioType const value) {
377 container[i].prio=value;
378 int p=container[i].parent;
380 if ( p!=-1 && comp(value, container[p].prio) ) {
384 if ( comp(value, container[minimum].prio) ) minimum=i;
388 template <typename Item, typename Prio, typename ItemIntMap,
390 void FibHeap<Item, Prio, ItemIntMap, Compare>::balance() {
392 int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1;
394 std::vector<int> A(maxdeg,-1);
397 *Recall that now minimum does not point to the minimum prio element.
398 *We set minimum to this during balance().
400 int anchor=container[minimum].left_neighbor;
406 if ( anchor==active ) end=true;
407 int d=container[active].degree;
408 next=container[active].right_neighbor;
411 if( comp(container[active].prio, container[A[d]].prio) ) {
424 while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
428 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
429 s=container[s].right_neighbor;
433 template <typename Item, typename Prio, typename ItemIntMap,
435 void FibHeap<Item, Prio, ItemIntMap, Compare>::makeroot
439 container[s].parent=-1;
440 s=container[s].right_neighbor;
445 template <typename Item, typename Prio, typename ItemIntMap,
447 void FibHeap<Item, Prio, ItemIntMap, Compare>::cut
450 *Replacing a from the children of b.
452 --container[b].degree;
454 if ( container[b].degree !=0 ) {
455 int child=container[b].child;
457 container[b].child=container[child].right_neighbor;
462 /*Lacing a to the roots.*/
463 int right=container[minimum].right_neighbor;
464 container[minimum].right_neighbor=a;
465 container[a].left_neighbor=minimum;
466 container[a].right_neighbor=right;
467 container[right].left_neighbor=a;
469 container[a].parent=-1;
470 container[a].marked=false;
474 template <typename Item, typename Prio, typename ItemIntMap,
476 void FibHeap<Item, Prio, ItemIntMap, Compare>::cascade
479 if ( container[a].parent!=-1 ) {
480 int p=container[a].parent;
482 if ( container[a].marked==false ) container[a].marked=true;
491 template <typename Item, typename Prio, typename ItemIntMap,
493 void FibHeap<Item, Prio, ItemIntMap, Compare>::fuse
497 /*Lacing b under a.*/
498 container[b].parent=a;
500 if (container[a].degree==0) {
501 container[b].left_neighbor=b;
502 container[b].right_neighbor=b;
503 container[a].child=b;
505 int child=container[a].child;
506 int last_child=container[child].left_neighbor;
507 container[child].left_neighbor=b;
508 container[b].right_neighbor=child;
509 container[last_child].right_neighbor=b;
510 container[b].left_neighbor=last_child;
513 ++container[a].degree;
515 container[b].marked=false;
520 *It is invoked only if a has siblings.
522 template <typename Item, typename Prio, typename ItemIntMap,
524 void FibHeap<Item, Prio, ItemIntMap, Compare>::unlace
526 int leftn=container[a].left_neighbor;
527 int rightn=container[a].right_neighbor;
528 container[leftn].right_neighbor=rightn;
529 container[rightn].left_neighbor=leftn;
535 #endif //LEMON_FIB_HEAP_H