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