deba@728
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@728
|
2 |
*
|
deba@728
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@728
|
4 |
*
|
deba@728
|
5 |
* Copyright (C) 2003-2009
|
deba@728
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@728
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@728
|
8 |
*
|
deba@728
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@728
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@728
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@728
|
12 |
*
|
deba@728
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@728
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@728
|
15 |
* purpose.
|
deba@728
|
16 |
*
|
deba@728
|
17 |
*/
|
deba@728
|
18 |
|
deba@728
|
19 |
#ifndef LEMON_FIB_HEAP_H
|
deba@728
|
20 |
#define LEMON_FIB_HEAP_H
|
deba@728
|
21 |
|
deba@728
|
22 |
///\file
|
kpeter@757
|
23 |
///\ingroup heaps
|
kpeter@756
|
24 |
///\brief Fibonacci heap implementation.
|
deba@728
|
25 |
|
deba@728
|
26 |
#include <vector>
|
kpeter@756
|
27 |
#include <utility>
|
deba@728
|
28 |
#include <functional>
|
deba@728
|
29 |
#include <lemon/math.h>
|
deba@728
|
30 |
|
deba@728
|
31 |
namespace lemon {
|
deba@728
|
32 |
|
kpeter@757
|
33 |
/// \ingroup heaps
|
deba@728
|
34 |
///
|
kpeter@756
|
35 |
/// \brief Fibonacci heap data structure.
|
deba@728
|
36 |
///
|
kpeter@756
|
37 |
/// This class implements the \e Fibonacci \e heap data structure.
|
kpeter@756
|
38 |
/// It fully conforms to the \ref concepts::Heap "heap concept".
|
deba@728
|
39 |
///
|
kpeter@756
|
40 |
/// The methods \ref increase() and \ref erase() are not efficient in a
|
kpeter@756
|
41 |
/// Fibonacci heap. In case of many calls of these operations, it is
|
kpeter@756
|
42 |
/// better to use other heap structure, e.g. \ref BinHeap "binary heap".
|
deba@728
|
43 |
///
|
kpeter@756
|
44 |
/// \tparam PR Type of the priorities of the items.
|
kpeter@756
|
45 |
/// \tparam IM A read-writable item map with \c int values, used
|
kpeter@756
|
46 |
/// internally to handle the cross references.
|
kpeter@756
|
47 |
/// \tparam CMP A functor class for comparing the priorities.
|
kpeter@756
|
48 |
/// The default is \c std::less<PR>.
|
deba@728
|
49 |
#ifdef DOXYGEN
|
kpeter@756
|
50 |
template <typename PR, typename IM, typename CMP>
|
deba@728
|
51 |
#else
|
kpeter@756
|
52 |
template <typename PR, typename IM, typename CMP = std::less<PR> >
|
deba@728
|
53 |
#endif
|
deba@728
|
54 |
class FibHeap {
|
deba@728
|
55 |
public:
|
kpeter@756
|
56 |
|
kpeter@756
|
57 |
/// Type of the item-int map.
|
deba@730
|
58 |
typedef IM ItemIntMap;
|
kpeter@756
|
59 |
/// Type of the priorities.
|
kpeter@756
|
60 |
typedef PR Prio;
|
kpeter@756
|
61 |
/// Type of the items stored in the heap.
|
deba@728
|
62 |
typedef typename ItemIntMap::Key Item;
|
kpeter@756
|
63 |
/// Type of the item-priority pairs.
|
deba@728
|
64 |
typedef std::pair<Item,Prio> Pair;
|
kpeter@756
|
65 |
/// Functor type for comparing the priorities.
|
deba@730
|
66 |
typedef CMP Compare;
|
deba@728
|
67 |
|
deba@728
|
68 |
private:
|
deba@730
|
69 |
class Store;
|
deba@728
|
70 |
|
deba@730
|
71 |
std::vector<Store> _data;
|
deba@730
|
72 |
int _minimum;
|
deba@730
|
73 |
ItemIntMap &_iim;
|
deba@730
|
74 |
Compare _comp;
|
deba@730
|
75 |
int _num;
|
deba@728
|
76 |
|
deba@728
|
77 |
public:
|
deba@730
|
78 |
|
kpeter@756
|
79 |
/// \brief Type to represent the states of the items.
|
deba@730
|
80 |
///
|
kpeter@756
|
81 |
/// Each item has a state associated to it. It can be "in heap",
|
kpeter@756
|
82 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the
|
deba@730
|
83 |
/// heap's point of view, but may be useful to the user.
|
deba@730
|
84 |
///
|
deba@730
|
85 |
/// The item-int map must be initialized in such way that it assigns
|
deba@730
|
86 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
|
deba@728
|
87 |
enum State {
|
deba@730
|
88 |
IN_HEAP = 0, ///< = 0.
|
deba@730
|
89 |
PRE_HEAP = -1, ///< = -1.
|
deba@730
|
90 |
POST_HEAP = -2 ///< = -2.
|
deba@728
|
91 |
};
|
deba@728
|
92 |
|
kpeter@756
|
93 |
/// \brief Constructor.
|
deba@728
|
94 |
///
|
kpeter@756
|
95 |
/// Constructor.
|
kpeter@756
|
96 |
/// \param map A map that assigns \c int values to the items.
|
kpeter@756
|
97 |
/// It is used internally to handle the cross references.
|
kpeter@756
|
98 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
|
deba@730
|
99 |
explicit FibHeap(ItemIntMap &map)
|
deba@730
|
100 |
: _minimum(0), _iim(map), _num() {}
|
deba@728
|
101 |
|
kpeter@756
|
102 |
/// \brief Constructor.
|
deba@728
|
103 |
///
|
kpeter@756
|
104 |
/// Constructor.
|
kpeter@756
|
105 |
/// \param map A map that assigns \c int values to the items.
|
kpeter@756
|
106 |
/// It is used internally to handle the cross references.
|
kpeter@756
|
107 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
|
kpeter@756
|
108 |
/// \param comp The function object used for comparing the priorities.
|
deba@730
|
109 |
FibHeap(ItemIntMap &map, const Compare &comp)
|
deba@730
|
110 |
: _minimum(0), _iim(map), _comp(comp), _num() {}
|
deba@728
|
111 |
|
deba@728
|
112 |
/// \brief The number of items stored in the heap.
|
deba@728
|
113 |
///
|
kpeter@756
|
114 |
/// This function returns the number of items stored in the heap.
|
deba@730
|
115 |
int size() const { return _num; }
|
deba@728
|
116 |
|
kpeter@756
|
117 |
/// \brief Check if the heap is empty.
|
deba@728
|
118 |
///
|
kpeter@756
|
119 |
/// This function returns \c true if the heap is empty.
|
deba@730
|
120 |
bool empty() const { return _num==0; }
|
deba@728
|
121 |
|
kpeter@756
|
122 |
/// \brief Make the heap empty.
|
deba@728
|
123 |
///
|
kpeter@756
|
124 |
/// This functon makes the heap empty.
|
kpeter@756
|
125 |
/// It does not change the cross reference map. If you want to reuse
|
kpeter@756
|
126 |
/// a heap that is not surely empty, you should first clear it and
|
kpeter@756
|
127 |
/// then you should set the cross reference map to \c PRE_HEAP
|
kpeter@756
|
128 |
/// for each item.
|
deba@728
|
129 |
void clear() {
|
deba@730
|
130 |
_data.clear(); _minimum = 0; _num = 0;
|
deba@728
|
131 |
}
|
deba@728
|
132 |
|
kpeter@756
|
133 |
/// \brief Insert an item into the heap with the given priority.
|
deba@728
|
134 |
///
|
kpeter@756
|
135 |
/// This function inserts the given item into the heap with the
|
kpeter@756
|
136 |
/// given priority.
|
kpeter@756
|
137 |
/// \param item The item to insert.
|
kpeter@756
|
138 |
/// \param prio The priority of the item.
|
kpeter@756
|
139 |
/// \pre \e item must not be stored in the heap.
|
kpeter@756
|
140 |
void push (const Item& item, const Prio& prio) {
|
deba@730
|
141 |
int i=_iim[item];
|
deba@728
|
142 |
if ( i < 0 ) {
|
deba@730
|
143 |
int s=_data.size();
|
deba@730
|
144 |
_iim.set( item, s );
|
deba@730
|
145 |
Store st;
|
deba@728
|
146 |
st.name=item;
|
deba@730
|
147 |
_data.push_back(st);
|
deba@728
|
148 |
i=s;
|
deba@728
|
149 |
} else {
|
deba@730
|
150 |
_data[i].parent=_data[i].child=-1;
|
deba@730
|
151 |
_data[i].degree=0;
|
deba@730
|
152 |
_data[i].in=true;
|
deba@730
|
153 |
_data[i].marked=false;
|
deba@728
|
154 |
}
|
deba@728
|
155 |
|
deba@730
|
156 |
if ( _num ) {
|
deba@730
|
157 |
_data[_data[_minimum].right_neighbor].left_neighbor=i;
|
deba@730
|
158 |
_data[i].right_neighbor=_data[_minimum].right_neighbor;
|
deba@730
|
159 |
_data[_minimum].right_neighbor=i;
|
deba@730
|
160 |
_data[i].left_neighbor=_minimum;
|
kpeter@756
|
161 |
if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
|
deba@728
|
162 |
} else {
|
deba@730
|
163 |
_data[i].right_neighbor=_data[i].left_neighbor=i;
|
deba@730
|
164 |
_minimum=i;
|
deba@728
|
165 |
}
|
kpeter@756
|
166 |
_data[i].prio=prio;
|
deba@730
|
167 |
++_num;
|
deba@728
|
168 |
}
|
deba@728
|
169 |
|
kpeter@756
|
170 |
/// \brief Return the item having minimum priority.
|
deba@728
|
171 |
///
|
kpeter@756
|
172 |
/// This function returns the item having minimum priority.
|
kpeter@756
|
173 |
/// \pre The heap must be non-empty.
|
deba@730
|
174 |
Item top() const { return _data[_minimum].name; }
|
deba@728
|
175 |
|
kpeter@756
|
176 |
/// \brief The minimum priority.
|
deba@728
|
177 |
///
|
kpeter@756
|
178 |
/// This function returns the minimum priority.
|
kpeter@756
|
179 |
/// \pre The heap must be non-empty.
|
kpeter@756
|
180 |
Prio prio() const { return _data[_minimum].prio; }
|
deba@728
|
181 |
|
kpeter@756
|
182 |
/// \brief Remove the item having minimum priority.
|
deba@728
|
183 |
///
|
kpeter@756
|
184 |
/// This function removes the item having minimum priority.
|
deba@728
|
185 |
/// \pre The heap must be non-empty.
|
deba@728
|
186 |
void pop() {
|
deba@728
|
187 |
/*The first case is that there are only one root.*/
|
deba@730
|
188 |
if ( _data[_minimum].left_neighbor==_minimum ) {
|
deba@730
|
189 |
_data[_minimum].in=false;
|
deba@730
|
190 |
if ( _data[_minimum].degree!=0 ) {
|
kpeter@758
|
191 |
makeRoot(_data[_minimum].child);
|
deba@730
|
192 |
_minimum=_data[_minimum].child;
|
deba@728
|
193 |
balance();
|
deba@728
|
194 |
}
|
deba@728
|
195 |
} else {
|
deba@730
|
196 |
int right=_data[_minimum].right_neighbor;
|
deba@730
|
197 |
unlace(_minimum);
|
deba@730
|
198 |
_data[_minimum].in=false;
|
deba@730
|
199 |
if ( _data[_minimum].degree > 0 ) {
|
deba@730
|
200 |
int left=_data[_minimum].left_neighbor;
|
deba@730
|
201 |
int child=_data[_minimum].child;
|
deba@730
|
202 |
int last_child=_data[child].left_neighbor;
|
deba@728
|
203 |
|
kpeter@758
|
204 |
makeRoot(child);
|
deba@728
|
205 |
|
deba@730
|
206 |
_data[left].right_neighbor=child;
|
deba@730
|
207 |
_data[child].left_neighbor=left;
|
deba@730
|
208 |
_data[right].left_neighbor=last_child;
|
deba@730
|
209 |
_data[last_child].right_neighbor=right;
|
deba@728
|
210 |
}
|
deba@730
|
211 |
_minimum=right;
|
deba@728
|
212 |
balance();
|
deba@728
|
213 |
} // the case where there are more roots
|
deba@730
|
214 |
--_num;
|
deba@728
|
215 |
}
|
deba@728
|
216 |
|
kpeter@756
|
217 |
/// \brief Remove the given item from the heap.
|
deba@728
|
218 |
///
|
kpeter@756
|
219 |
/// This function removes the given item from the heap if it is
|
kpeter@756
|
220 |
/// already stored.
|
kpeter@756
|
221 |
/// \param item The item to delete.
|
kpeter@756
|
222 |
/// \pre \e item must be in the heap.
|
deba@728
|
223 |
void erase (const Item& item) {
|
deba@730
|
224 |
int i=_iim[item];
|
deba@728
|
225 |
|
deba@730
|
226 |
if ( i >= 0 && _data[i].in ) {
|
deba@730
|
227 |
if ( _data[i].parent!=-1 ) {
|
deba@730
|
228 |
int p=_data[i].parent;
|
deba@728
|
229 |
cut(i,p);
|
deba@728
|
230 |
cascade(p);
|
deba@728
|
231 |
}
|
deba@730
|
232 |
_minimum=i; //As if its prio would be -infinity
|
deba@728
|
233 |
pop();
|
deba@728
|
234 |
}
|
deba@728
|
235 |
}
|
deba@728
|
236 |
|
kpeter@756
|
237 |
/// \brief The priority of the given item.
|
deba@728
|
238 |
///
|
kpeter@756
|
239 |
/// This function returns the priority of the given item.
|
kpeter@756
|
240 |
/// \param item The item.
|
kpeter@756
|
241 |
/// \pre \e item must be in the heap.
|
kpeter@756
|
242 |
Prio operator[](const Item& item) const {
|
kpeter@756
|
243 |
return _data[_iim[item]].prio;
|
kpeter@756
|
244 |
}
|
kpeter@756
|
245 |
|
kpeter@756
|
246 |
/// \brief Set the priority of an item or insert it, if it is
|
kpeter@756
|
247 |
/// not stored in the heap.
|
kpeter@756
|
248 |
///
|
kpeter@756
|
249 |
/// This method sets the priority of the given item if it is
|
kpeter@756
|
250 |
/// already stored in the heap. Otherwise it inserts the given
|
kpeter@756
|
251 |
/// item into the heap with the given priority.
|
kpeter@756
|
252 |
/// \param item The item.
|
kpeter@756
|
253 |
/// \param prio The priority.
|
kpeter@756
|
254 |
void set (const Item& item, const Prio& prio) {
|
deba@730
|
255 |
int i=_iim[item];
|
kpeter@756
|
256 |
if ( i >= 0 && _data[i].in ) {
|
kpeter@756
|
257 |
if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
|
kpeter@756
|
258 |
if ( _comp(_data[i].prio, prio) ) increase(item, prio);
|
kpeter@756
|
259 |
} else push(item, prio);
|
kpeter@756
|
260 |
}
|
kpeter@756
|
261 |
|
kpeter@756
|
262 |
/// \brief Decrease the priority of an item to the given value.
|
kpeter@756
|
263 |
///
|
kpeter@756
|
264 |
/// This function decreases the priority of an item to the given value.
|
kpeter@756
|
265 |
/// \param item The item.
|
kpeter@756
|
266 |
/// \param prio The priority.
|
kpeter@756
|
267 |
/// \pre \e item must be stored in the heap with priority at least \e prio.
|
kpeter@756
|
268 |
void decrease (const Item& item, const Prio& prio) {
|
kpeter@756
|
269 |
int i=_iim[item];
|
kpeter@756
|
270 |
_data[i].prio=prio;
|
deba@730
|
271 |
int p=_data[i].parent;
|
deba@728
|
272 |
|
kpeter@756
|
273 |
if ( p!=-1 && _comp(prio, _data[p].prio) ) {
|
deba@728
|
274 |
cut(i,p);
|
deba@728
|
275 |
cascade(p);
|
deba@728
|
276 |
}
|
kpeter@756
|
277 |
if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
|
deba@728
|
278 |
}
|
deba@728
|
279 |
|
kpeter@756
|
280 |
/// \brief Increase the priority of an item to the given value.
|
deba@728
|
281 |
///
|
kpeter@756
|
282 |
/// This function increases the priority of an item to the given value.
|
kpeter@756
|
283 |
/// \param item The item.
|
kpeter@756
|
284 |
/// \param prio The priority.
|
kpeter@756
|
285 |
/// \pre \e item must be stored in the heap with priority at most \e prio.
|
kpeter@756
|
286 |
void increase (const Item& item, const Prio& prio) {
|
deba@728
|
287 |
erase(item);
|
kpeter@756
|
288 |
push(item, prio);
|
deba@728
|
289 |
}
|
deba@728
|
290 |
|
kpeter@756
|
291 |
/// \brief Return the state of an item.
|
deba@728
|
292 |
///
|
kpeter@756
|
293 |
/// This method returns \c PRE_HEAP if the given item has never
|
kpeter@756
|
294 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment,
|
kpeter@756
|
295 |
/// and \c POST_HEAP otherwise.
|
kpeter@756
|
296 |
/// In the latter case it is possible that the item will get back
|
kpeter@756
|
297 |
/// to the heap again.
|
kpeter@756
|
298 |
/// \param item The item.
|
deba@728
|
299 |
State state(const Item &item) const {
|
deba@730
|
300 |
int i=_iim[item];
|
deba@728
|
301 |
if( i>=0 ) {
|
deba@730
|
302 |
if ( _data[i].in ) i=0;
|
deba@728
|
303 |
else i=-2;
|
deba@728
|
304 |
}
|
deba@728
|
305 |
return State(i);
|
deba@728
|
306 |
}
|
deba@728
|
307 |
|
kpeter@756
|
308 |
/// \brief Set the state of an item in the heap.
|
deba@728
|
309 |
///
|
kpeter@756
|
310 |
/// This function sets the state of the given item in the heap.
|
kpeter@756
|
311 |
/// It can be used to manually clear the heap when it is important
|
kpeter@756
|
312 |
/// to achive better time complexity.
|
deba@728
|
313 |
/// \param i The item.
|
deba@728
|
314 |
/// \param st The state. It should not be \c IN_HEAP.
|
deba@728
|
315 |
void state(const Item& i, State st) {
|
deba@728
|
316 |
switch (st) {
|
deba@728
|
317 |
case POST_HEAP:
|
deba@728
|
318 |
case PRE_HEAP:
|
deba@728
|
319 |
if (state(i) == IN_HEAP) {
|
deba@728
|
320 |
erase(i);
|
deba@728
|
321 |
}
|
deba@730
|
322 |
_iim[i] = st;
|
deba@728
|
323 |
break;
|
deba@728
|
324 |
case IN_HEAP:
|
deba@728
|
325 |
break;
|
deba@728
|
326 |
}
|
deba@728
|
327 |
}
|
deba@728
|
328 |
|
deba@728
|
329 |
private:
|
deba@728
|
330 |
|
deba@728
|
331 |
void balance() {
|
deba@728
|
332 |
|
deba@730
|
333 |
int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1;
|
deba@728
|
334 |
|
deba@728
|
335 |
std::vector<int> A(maxdeg,-1);
|
deba@728
|
336 |
|
deba@728
|
337 |
/*
|
deba@728
|
338 |
*Recall that now minimum does not point to the minimum prio element.
|
deba@728
|
339 |
*We set minimum to this during balance().
|
deba@728
|
340 |
*/
|
deba@730
|
341 |
int anchor=_data[_minimum].left_neighbor;
|
deba@730
|
342 |
int next=_minimum;
|
deba@728
|
343 |
bool end=false;
|
deba@728
|
344 |
|
deba@728
|
345 |
do {
|
deba@728
|
346 |
int active=next;
|
deba@728
|
347 |
if ( anchor==active ) end=true;
|
deba@730
|
348 |
int d=_data[active].degree;
|
deba@730
|
349 |
next=_data[active].right_neighbor;
|
deba@728
|
350 |
|
deba@728
|
351 |
while (A[d]!=-1) {
|
deba@730
|
352 |
if( _comp(_data[active].prio, _data[A[d]].prio) ) {
|
deba@728
|
353 |
fuse(active,A[d]);
|
deba@728
|
354 |
} else {
|
deba@728
|
355 |
fuse(A[d],active);
|
deba@728
|
356 |
active=A[d];
|
deba@728
|
357 |
}
|
deba@728
|
358 |
A[d]=-1;
|
deba@728
|
359 |
++d;
|
deba@728
|
360 |
}
|
deba@728
|
361 |
A[d]=active;
|
deba@728
|
362 |
} while ( !end );
|
deba@728
|
363 |
|
deba@728
|
364 |
|
deba@730
|
365 |
while ( _data[_minimum].parent >=0 )
|
deba@730
|
366 |
_minimum=_data[_minimum].parent;
|
deba@730
|
367 |
int s=_minimum;
|
deba@730
|
368 |
int m=_minimum;
|
deba@728
|
369 |
do {
|
deba@730
|
370 |
if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s;
|
deba@730
|
371 |
s=_data[s].right_neighbor;
|
deba@728
|
372 |
} while ( s != m );
|
deba@728
|
373 |
}
|
deba@728
|
374 |
|
kpeter@758
|
375 |
void makeRoot(int c) {
|
deba@728
|
376 |
int s=c;
|
deba@728
|
377 |
do {
|
deba@730
|
378 |
_data[s].parent=-1;
|
deba@730
|
379 |
s=_data[s].right_neighbor;
|
deba@728
|
380 |
} while ( s != c );
|
deba@728
|
381 |
}
|
deba@728
|
382 |
|
deba@728
|
383 |
void cut(int a, int b) {
|
deba@728
|
384 |
/*
|
deba@728
|
385 |
*Replacing a from the children of b.
|
deba@728
|
386 |
*/
|
deba@730
|
387 |
--_data[b].degree;
|
deba@728
|
388 |
|
deba@730
|
389 |
if ( _data[b].degree !=0 ) {
|
deba@730
|
390 |
int child=_data[b].child;
|
deba@728
|
391 |
if ( child==a )
|
deba@730
|
392 |
_data[b].child=_data[child].right_neighbor;
|
deba@728
|
393 |
unlace(a);
|
deba@728
|
394 |
}
|
deba@728
|
395 |
|
deba@728
|
396 |
|
deba@728
|
397 |
/*Lacing a to the roots.*/
|
deba@730
|
398 |
int right=_data[_minimum].right_neighbor;
|
deba@730
|
399 |
_data[_minimum].right_neighbor=a;
|
deba@730
|
400 |
_data[a].left_neighbor=_minimum;
|
deba@730
|
401 |
_data[a].right_neighbor=right;
|
deba@730
|
402 |
_data[right].left_neighbor=a;
|
deba@728
|
403 |
|
deba@730
|
404 |
_data[a].parent=-1;
|
deba@730
|
405 |
_data[a].marked=false;
|
deba@728
|
406 |
}
|
deba@728
|
407 |
|
deba@728
|
408 |
void cascade(int a) {
|
deba@730
|
409 |
if ( _data[a].parent!=-1 ) {
|
deba@730
|
410 |
int p=_data[a].parent;
|
deba@728
|
411 |
|
deba@730
|
412 |
if ( _data[a].marked==false ) _data[a].marked=true;
|
deba@728
|
413 |
else {
|
deba@728
|
414 |
cut(a,p);
|
deba@728
|
415 |
cascade(p);
|
deba@728
|
416 |
}
|
deba@728
|
417 |
}
|
deba@728
|
418 |
}
|
deba@728
|
419 |
|
deba@728
|
420 |
void fuse(int a, int b) {
|
deba@728
|
421 |
unlace(b);
|
deba@728
|
422 |
|
deba@728
|
423 |
/*Lacing b under a.*/
|
deba@730
|
424 |
_data[b].parent=a;
|
deba@728
|
425 |
|
deba@730
|
426 |
if (_data[a].degree==0) {
|
deba@730
|
427 |
_data[b].left_neighbor=b;
|
deba@730
|
428 |
_data[b].right_neighbor=b;
|
deba@730
|
429 |
_data[a].child=b;
|
deba@728
|
430 |
} else {
|
deba@730
|
431 |
int child=_data[a].child;
|
deba@730
|
432 |
int last_child=_data[child].left_neighbor;
|
deba@730
|
433 |
_data[child].left_neighbor=b;
|
deba@730
|
434 |
_data[b].right_neighbor=child;
|
deba@730
|
435 |
_data[last_child].right_neighbor=b;
|
deba@730
|
436 |
_data[b].left_neighbor=last_child;
|
deba@728
|
437 |
}
|
deba@728
|
438 |
|
deba@730
|
439 |
++_data[a].degree;
|
deba@728
|
440 |
|
deba@730
|
441 |
_data[b].marked=false;
|
deba@728
|
442 |
}
|
deba@728
|
443 |
|
deba@728
|
444 |
/*
|
deba@728
|
445 |
*It is invoked only if a has siblings.
|
deba@728
|
446 |
*/
|
deba@728
|
447 |
void unlace(int a) {
|
deba@730
|
448 |
int leftn=_data[a].left_neighbor;
|
deba@730
|
449 |
int rightn=_data[a].right_neighbor;
|
deba@730
|
450 |
_data[leftn].right_neighbor=rightn;
|
deba@730
|
451 |
_data[rightn].left_neighbor=leftn;
|
deba@728
|
452 |
}
|
deba@728
|
453 |
|
deba@728
|
454 |
|
deba@730
|
455 |
class Store {
|
deba@728
|
456 |
friend class FibHeap;
|
deba@728
|
457 |
|
deba@728
|
458 |
Item name;
|
deba@728
|
459 |
int parent;
|
deba@728
|
460 |
int left_neighbor;
|
deba@728
|
461 |
int right_neighbor;
|
deba@728
|
462 |
int child;
|
deba@728
|
463 |
int degree;
|
deba@728
|
464 |
bool marked;
|
deba@728
|
465 |
bool in;
|
deba@728
|
466 |
Prio prio;
|
deba@728
|
467 |
|
deba@730
|
468 |
Store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
|
deba@728
|
469 |
};
|
deba@728
|
470 |
};
|
deba@728
|
471 |
|
deba@728
|
472 |
} //namespace lemon
|
deba@728
|
473 |
|
deba@728
|
474 |
#endif //LEMON_FIB_HEAP_H
|
deba@728
|
475 |
|