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.
29 #include <lemon/math.h>
35 /// \brief Fibonacci heap data structure.
37 /// This class implements the \e Fibonacci \e heap data structure.
38 /// It fully conforms to the \ref concepts::Heap "heap concept".
40 /// The methods \ref increase() and \ref erase() are not efficient in a
41 /// Fibonacci heap. In case of many calls of these operations, it is
42 /// better to use other heap structure, e.g. \ref BinHeap "binary heap".
44 /// \tparam PR Type of the priorities of the items.
45 /// \tparam IM A read-writable item map with \c int values, used
46 /// internally to handle the cross references.
47 /// \tparam CMP A functor class for comparing the priorities.
48 /// The default is \c std::less<PR>.
50 template <typename PR, typename IM, typename CMP>
52 template <typename PR, typename IM, typename CMP = std::less<PR> >
57 /// Type of the item-int map.
58 typedef IM ItemIntMap;
59 /// Type of the priorities.
61 /// Type of the items stored in the heap.
62 typedef typename ItemIntMap::Key Item;
63 /// Type of the item-priority pairs.
64 typedef std::pair<Item,Prio> Pair;
65 /// Functor type for comparing the priorities.
71 std::vector<Store> _data;
79 /// \brief Type to represent the states of the items.
81 /// Each item has a state associated to it. It can be "in heap",
82 /// "pre-heap" or "post-heap". The latter two are indifferent from the
83 /// heap's point of view, but may be useful to the user.
85 /// The item-int map must be initialized in such way that it assigns
86 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
88 IN_HEAP = 0, ///< = 0.
89 PRE_HEAP = -1, ///< = -1.
90 POST_HEAP = -2 ///< = -2.
93 /// \brief Constructor.
96 /// \param map A map that assigns \c int values to the items.
97 /// It is used internally to handle the cross references.
98 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
99 explicit FibHeap(ItemIntMap &map)
100 : _minimum(0), _iim(map), _num() {}
102 /// \brief Constructor.
105 /// \param map A map that assigns \c int values to the items.
106 /// It is used internally to handle the cross references.
107 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
108 /// \param comp The function object used for comparing 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 /// This function returns the number of items stored in the heap.
115 int size() const { return _num; }
117 /// \brief Check if the heap is empty.
119 /// This function returns \c true if the heap is empty.
120 bool empty() const { return _num==0; }
122 /// \brief Make the heap empty.
124 /// This functon makes the heap empty.
125 /// It does not change the cross reference map. If you want to reuse
126 /// a heap that is not surely empty, you should first clear it and
127 /// then you should set the cross reference map to \c PRE_HEAP
130 _data.clear(); _minimum = 0; _num = 0;
133 /// \brief Insert an item into the heap with the given priority.
135 /// This function inserts the given item into the heap with the
137 /// \param item The item to insert.
138 /// \param prio The priority of the item.
139 /// \pre \e item must not be stored in the heap.
140 void push (const Item& item, const Prio& prio) {
150 _data[i].parent=_data[i].child=-1;
153 _data[i].marked=false;
157 _data[_data[_minimum].right_neighbor].left_neighbor=i;
158 _data[i].right_neighbor=_data[_minimum].right_neighbor;
159 _data[_minimum].right_neighbor=i;
160 _data[i].left_neighbor=_minimum;
161 if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
163 _data[i].right_neighbor=_data[i].left_neighbor=i;
170 /// \brief Return the item having minimum priority.
172 /// This function returns the item having minimum priority.
173 /// \pre The heap must be non-empty.
174 Item top() const { return _data[_minimum].name; }
176 /// \brief The minimum priority.
178 /// This function returns the minimum priority.
179 /// \pre The heap must be non-empty.
180 Prio prio() const { return _data[_minimum].prio; }
182 /// \brief Remove the item having minimum priority.
184 /// This function removes the item having minimum priority.
185 /// \pre The heap must be non-empty.
187 /*The first case is that there are only one root.*/
188 if ( _data[_minimum].left_neighbor==_minimum ) {
189 _data[_minimum].in=false;
190 if ( _data[_minimum].degree!=0 ) {
191 makeRoot(_data[_minimum].child);
192 _minimum=_data[_minimum].child;
196 int right=_data[_minimum].right_neighbor;
198 _data[_minimum].in=false;
199 if ( _data[_minimum].degree > 0 ) {
200 int left=_data[_minimum].left_neighbor;
201 int child=_data[_minimum].child;
202 int last_child=_data[child].left_neighbor;
206 _data[left].right_neighbor=child;
207 _data[child].left_neighbor=left;
208 _data[right].left_neighbor=last_child;
209 _data[last_child].right_neighbor=right;
213 } // the case where there are more roots
217 /// \brief Remove the given item from the heap.
219 /// This function removes the given item from the heap if it is
221 /// \param item The item to delete.
222 /// \pre \e item must be in the heap.
223 void erase (const Item& item) {
226 if ( i >= 0 && _data[i].in ) {
227 if ( _data[i].parent!=-1 ) {
228 int p=_data[i].parent;
232 _minimum=i; //As if its prio would be -infinity
237 /// \brief The priority of the given item.
239 /// This function returns the priority of the given item.
240 /// \param item The item.
241 /// \pre \e item must be in the heap.
242 Prio operator[](const Item& item) const {
243 return _data[_iim[item]].prio;
246 /// \brief Set the priority of an item or insert it, if it is
247 /// not stored in the heap.
249 /// This method sets the priority of the given item if it is
250 /// already stored in the heap. Otherwise it inserts the given
251 /// item into the heap with the given priority.
252 /// \param item The item.
253 /// \param prio The priority.
254 void set (const Item& item, const Prio& prio) {
256 if ( i >= 0 && _data[i].in ) {
257 if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
258 if ( _comp(_data[i].prio, prio) ) increase(item, prio);
259 } else push(item, prio);
262 /// \brief Decrease the priority of an item to the given value.
264 /// This function decreases the priority of an item to the given value.
265 /// \param item The item.
266 /// \param prio The priority.
267 /// \pre \e item must be stored in the heap with priority at least \e prio.
268 void decrease (const Item& item, const Prio& prio) {
271 int p=_data[i].parent;
273 if ( p!=-1 && _comp(prio, _data[p].prio) ) {
277 if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
280 /// \brief Increase the priority of an item to the given value.
282 /// This function increases the priority of an item to the given value.
283 /// \param item The item.
284 /// \param prio The priority.
285 /// \pre \e item must be stored in the heap with priority at most \e prio.
286 void increase (const Item& item, const Prio& prio) {
291 /// \brief Return the state of an item.
293 /// This method returns \c PRE_HEAP if the given item has never
294 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
295 /// and \c POST_HEAP otherwise.
296 /// In the latter case it is possible that the item will get back
297 /// to the heap again.
298 /// \param item The item.
299 State state(const Item &item) const {
302 if ( _data[i].in ) i=0;
308 /// \brief Set the state of an item in the heap.
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.
313 /// \param i The item.
314 /// \param st The state. It should not be \c IN_HEAP.
315 void state(const Item& i, State st) {
319 if (state(i) == IN_HEAP) {
333 int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1;
335 std::vector<int> A(maxdeg,-1);
338 *Recall that now minimum does not point to the minimum prio element.
339 *We set minimum to this during balance().
341 int anchor=_data[_minimum].left_neighbor;
347 if ( anchor==active ) end=true;
348 int d=_data[active].degree;
349 next=_data[active].right_neighbor;
352 if( _comp(_data[active].prio, _data[A[d]].prio) ) {
365 while ( _data[_minimum].parent >=0 )
366 _minimum=_data[_minimum].parent;
370 if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s;
371 s=_data[s].right_neighbor;
375 void makeRoot(int c) {
379 s=_data[s].right_neighbor;
383 void cut(int a, int b) {
385 *Replacing a from the children of b.
389 if ( _data[b].degree !=0 ) {
390 int child=_data[b].child;
392 _data[b].child=_data[child].right_neighbor;
397 /*Lacing a to the roots.*/
398 int right=_data[_minimum].right_neighbor;
399 _data[_minimum].right_neighbor=a;
400 _data[a].left_neighbor=_minimum;
401 _data[a].right_neighbor=right;
402 _data[right].left_neighbor=a;
405 _data[a].marked=false;
408 void cascade(int a) {
409 if ( _data[a].parent!=-1 ) {
410 int p=_data[a].parent;
412 if ( _data[a].marked==false ) _data[a].marked=true;
420 void fuse(int a, int b) {
423 /*Lacing b under a.*/
426 if (_data[a].degree==0) {
427 _data[b].left_neighbor=b;
428 _data[b].right_neighbor=b;
431 int child=_data[a].child;
432 int last_child=_data[child].left_neighbor;
433 _data[child].left_neighbor=b;
434 _data[b].right_neighbor=child;
435 _data[last_child].right_neighbor=b;
436 _data[b].left_neighbor=last_child;
441 _data[b].marked=false;
445 *It is invoked only if a has siblings.
448 int leftn=_data[a].left_neighbor;
449 int rightn=_data[a].right_neighbor;
450 _data[leftn].right_neighbor=rightn;
451 _data[rightn].left_neighbor=leftn;
456 friend class FibHeap;
468 Store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
474 #endif //LEMON_FIB_HEAP_H