1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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.
28 #include <lemon/math.h>
34 ///\brief Fibonacci Heap.
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 CMP 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
44 ///\ref BinHeap "binary heap".
46 ///\param PRIO Type of the priority of the items.
47 ///\param IM A read and writable Item int map, used internally
48 ///to handle the cross references.
49 ///\param CMP A class for the ordering of the priorities. The
50 ///default is \c std::less<PRIO>.
55 template <typename PRIO, typename IM, typename CMP>
57 template <typename PRIO, typename IM, typename CMP = std::less<PRIO> >
62 typedef IM ItemIntMap;
66 typedef typename ItemIntMap::Key Item;
68 typedef std::pair<Item,Prio> Pair;
75 std::vector<Store> _data;
83 /// \brief Type to represent the items states.
85 /// Each Item element have a state associated to it. It may be "in heap",
86 /// "pre heap" or "post heap". The latter two are indifferent from the
87 /// heap's point of view, but may be useful to the user.
89 /// The item-int map must be initialized in such way that it assigns
90 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
92 IN_HEAP = 0, ///< = 0.
93 PRE_HEAP = -1, ///< = -1.
94 POST_HEAP = -2 ///< = -2.
97 /// \brief The constructor
99 /// \c map should be given to the constructor, since it is
100 /// used internally to handle the cross references.
101 explicit FibHeap(ItemIntMap &map)
102 : _minimum(0), _iim(map), _num() {}
104 /// \brief The constructor
106 /// \c map should be given to the constructor, since it is used
107 /// internally to handle the cross references. \c comp is an
108 /// object for ordering of the priorities.
109 FibHeap(ItemIntMap &map, const Compare &comp)
110 : _minimum(0), _iim(map), _comp(comp), _num() {}
112 /// \brief The number of items stored in the heap.
114 /// Returns the number of items stored in the heap.
115 int size() const { return _num; }
117 /// \brief Checks if the heap stores no items.
119 /// Returns \c true if and only if the heap stores no items.
120 bool empty() const { return _num==0; }
122 /// \brief Make empty this heap.
124 /// Make empty this heap. It does not change the cross reference
125 /// map. If you want to reuse a heap what is not surely empty you
126 /// should first clear the heap and after that you should set the
127 /// cross reference map for each item to \c PRE_HEAP.
129 _data.clear(); _minimum = 0; _num = 0;
132 /// \brief \c item gets to the heap with priority \c value independently
133 /// if \c item was already there.
135 /// This method calls \ref push(\c item, \c value) if \c item is not
136 /// stored in the heap and it calls \ref decrease(\c item, \c value) or
137 /// \ref increase(\c item, \c value) otherwise.
138 void set (const Item& item, const Prio& value) {
140 if ( i >= 0 && _data[i].in ) {
141 if ( _comp(value, _data[i].prio) ) decrease(item, value);
142 if ( _comp(_data[i].prio, value) ) increase(item, value);
143 } else push(item, value);
146 /// \brief Adds \c item to the heap with priority \c value.
148 /// Adds \c item to the heap with priority \c value.
149 /// \pre \c item must not be stored in the heap.
150 void push (const Item& item, const Prio& value) {
160 _data[i].parent=_data[i].child=-1;
163 _data[i].marked=false;
167 _data[_data[_minimum].right_neighbor].left_neighbor=i;
168 _data[i].right_neighbor=_data[_minimum].right_neighbor;
169 _data[_minimum].right_neighbor=i;
170 _data[i].left_neighbor=_minimum;
171 if ( _comp( value, _data[_minimum].prio) ) _minimum=i;
173 _data[i].right_neighbor=_data[i].left_neighbor=i;
180 /// \brief Returns the item with minimum priority relative to \c Compare.
182 /// This method returns the item with minimum priority relative to \c
184 /// \pre The heap must be nonempty.
185 Item top() const { return _data[_minimum].name; }
187 /// \brief Returns the minimum priority relative to \c Compare.
189 /// It returns the minimum priority relative to \c Compare.
190 /// \pre The heap must be nonempty.
191 const Prio& prio() const { return _data[_minimum].prio; }
193 /// \brief Returns the priority of \c item.
195 /// It returns the priority of \c item.
196 /// \pre \c item must be in the heap.
197 const Prio& operator[](const Item& item) const {
198 return _data[_iim[item]].prio;
201 /// \brief Deletes the item with minimum priority relative to \c Compare.
203 /// This method deletes the item with minimum priority relative to \c
204 /// Compare from the heap.
205 /// \pre The heap must be non-empty.
207 /*The first case is that there are only one root.*/
208 if ( _data[_minimum].left_neighbor==_minimum ) {
209 _data[_minimum].in=false;
210 if ( _data[_minimum].degree!=0 ) {
211 makeroot(_data[_minimum].child);
212 _minimum=_data[_minimum].child;
216 int right=_data[_minimum].right_neighbor;
218 _data[_minimum].in=false;
219 if ( _data[_minimum].degree > 0 ) {
220 int left=_data[_minimum].left_neighbor;
221 int child=_data[_minimum].child;
222 int last_child=_data[child].left_neighbor;
226 _data[left].right_neighbor=child;
227 _data[child].left_neighbor=left;
228 _data[right].left_neighbor=last_child;
229 _data[last_child].right_neighbor=right;
233 } // the case where there are more roots
237 /// \brief Deletes \c item from the heap.
239 /// This method deletes \c item from the heap, if \c item was already
240 /// stored in the heap. It is quite inefficient in Fibonacci heaps.
241 void erase (const Item& item) {
244 if ( i >= 0 && _data[i].in ) {
245 if ( _data[i].parent!=-1 ) {
246 int p=_data[i].parent;
250 _minimum=i; //As if its prio would be -infinity
255 /// \brief Decreases the priority of \c item to \c value.
257 /// This method decreases the priority of \c item to \c value.
258 /// \pre \c item must be stored in the heap with priority at least \c
259 /// value relative to \c Compare.
260 void decrease (Item item, const Prio& value) {
263 int p=_data[i].parent;
265 if ( p!=-1 && _comp(value, _data[p].prio) ) {
269 if ( _comp(value, _data[_minimum].prio) ) _minimum=i;
272 /// \brief Increases the priority of \c item to \c value.
274 /// This method sets the priority of \c item to \c value. Though
275 /// there is no precondition on the priority of \c item, this
276 /// method should be used only if it is indeed necessary to increase
277 /// (relative to \c Compare) the priority of \c item, because this
278 /// method is inefficient.
279 void increase (Item item, const Prio& value) {
285 /// \brief Returns if \c item is in, has already been in, or has never
286 /// been in the heap.
288 /// This method returns PRE_HEAP if \c item has never been in the
289 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
290 /// otherwise. In the latter case it is possible that \c item will
291 /// get back to the heap again.
292 State state(const Item &item) const {
295 if ( _data[i].in ) i=0;
301 /// \brief Sets the state of the \c item in the heap.
303 /// Sets the state of the \c item in the heap. It can be used to
304 /// manually clear the heap when it is important to achive the
305 /// better time _complexity.
306 /// \param i The item.
307 /// \param st The state. It should not be \c IN_HEAP.
308 void state(const Item& i, State st) {
312 if (state(i) == IN_HEAP) {
326 int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1;
328 std::vector<int> A(maxdeg,-1);
331 *Recall that now minimum does not point to the minimum prio element.
332 *We set minimum to this during balance().
334 int anchor=_data[_minimum].left_neighbor;
340 if ( anchor==active ) end=true;
341 int d=_data[active].degree;
342 next=_data[active].right_neighbor;
345 if( _comp(_data[active].prio, _data[A[d]].prio) ) {
358 while ( _data[_minimum].parent >=0 )
359 _minimum=_data[_minimum].parent;
363 if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s;
364 s=_data[s].right_neighbor;
368 void makeroot(int c) {
372 s=_data[s].right_neighbor;
376 void cut(int a, int b) {
378 *Replacing a from the children of b.
382 if ( _data[b].degree !=0 ) {
383 int child=_data[b].child;
385 _data[b].child=_data[child].right_neighbor;
390 /*Lacing a to the roots.*/
391 int right=_data[_minimum].right_neighbor;
392 _data[_minimum].right_neighbor=a;
393 _data[a].left_neighbor=_minimum;
394 _data[a].right_neighbor=right;
395 _data[right].left_neighbor=a;
398 _data[a].marked=false;
401 void cascade(int a) {
402 if ( _data[a].parent!=-1 ) {
403 int p=_data[a].parent;
405 if ( _data[a].marked==false ) _data[a].marked=true;
413 void fuse(int a, int b) {
416 /*Lacing b under a.*/
419 if (_data[a].degree==0) {
420 _data[b].left_neighbor=b;
421 _data[b].right_neighbor=b;
424 int child=_data[a].child;
425 int last_child=_data[child].left_neighbor;
426 _data[child].left_neighbor=b;
427 _data[b].right_neighbor=child;
428 _data[last_child].right_neighbor=b;
429 _data[b].left_neighbor=last_child;
434 _data[b].marked=false;
438 *It is invoked only if a has siblings.
441 int leftn=_data[a].left_neighbor;
442 int rightn=_data[a].right_neighbor;
443 _data[leftn].right_neighbor=rightn;
444 _data[rightn].left_neighbor=leftn;
449 friend class FibHeap;
461 Store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
467 #endif //LEMON_FIB_HEAP_H