jacint@167
|
1 |
// -*- C++ -*-
|
jacint@167
|
2 |
/*
|
jacint@167
|
3 |
*template <typename Item,
|
jacint@167
|
4 |
* typename Prio,
|
jacint@167
|
5 |
* typename ItemIntMap,
|
jacint@167
|
6 |
* typename Compare = std::less<Prio> >
|
jacint@167
|
7 |
*
|
jacint@167
|
8 |
*constructors:
|
jacint@167
|
9 |
*
|
jacint@167
|
10 |
*FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare)
|
jacint@167
|
11 |
*
|
jacint@167
|
12 |
*Member functions:
|
jacint@167
|
13 |
*
|
jacint@167
|
14 |
*int size() : returns the number of elements in the heap
|
jacint@167
|
15 |
*
|
jacint@167
|
16 |
*bool empty() : true iff size()=0
|
jacint@167
|
17 |
*
|
jacint@167
|
18 |
*void set(Item, Prio) : calls push(Item, Prio) if Item is not
|
jacint@167
|
19 |
* in the heap, and calls decrease/increase(Item, Prio) otherwise
|
jacint@167
|
20 |
*
|
jacint@167
|
21 |
*void push(Item, Prio) : pushes Item to the heap with priority Prio. Item
|
jacint@167
|
22 |
* mustn't be in the heap.
|
jacint@167
|
23 |
*
|
jacint@167
|
24 |
*Item top() : returns the Item with least Prio
|
jacint@167
|
25 |
*
|
jacint@167
|
26 |
*Prio prio() : returns the least Prio
|
jacint@167
|
27 |
*
|
jacint@167
|
28 |
*Prio get(Item) : returns Prio of Item
|
jacint@167
|
29 |
*
|
jacint@167
|
30 |
*void pop() : deletes the Item with least Prio
|
jacint@167
|
31 |
*
|
jacint@167
|
32 |
*void erase(Item) : deletes Item from the heap if it was already there
|
jacint@167
|
33 |
*
|
jacint@167
|
34 |
*void decrease(Item, P) : decreases prio of Item to P.
|
jacint@167
|
35 |
* Item must be in the heap with prio at least P.
|
jacint@167
|
36 |
*
|
jacint@167
|
37 |
*void increase(Item, P) : sets prio of Item to P.
|
jacint@167
|
38 |
*
|
jacint@167
|
39 |
*state_enum state(Item) : returns PRE_HEAP if Item has not been in the
|
jacint@167
|
40 |
* heap until now, IN_HEAP if it is in the heap at the moment, and
|
jacint@167
|
41 |
* POST_HEAP otherwise. In the latter case it is possible that Item
|
jacint@167
|
42 |
* will get back to the heap again.
|
jacint@167
|
43 |
*
|
jacint@167
|
44 |
*In Fibonacci heaps, increase and erase are not efficient, in case of
|
jacint@167
|
45 |
*many calls to these operations, it is better to use bin_heap.
|
jacint@167
|
46 |
*/
|
jacint@167
|
47 |
|
jacint@167
|
48 |
#ifndef FIB_HEAP_H
|
jacint@167
|
49 |
#define FIB_HEAP_H
|
jacint@167
|
50 |
|
jacint@167
|
51 |
#include <vector>
|
jacint@167
|
52 |
#include <functional>
|
jacint@167
|
53 |
#include <math.h>
|
jacint@167
|
54 |
|
jacint@167
|
55 |
namespace hugo {
|
jacint@167
|
56 |
|
jacint@167
|
57 |
template <typename Item, typename Prio, typename ItemIntMap,
|
jacint@167
|
58 |
typename Compare = std::less<Prio> >
|
jacint@167
|
59 |
|
jacint@167
|
60 |
class FibHeap {
|
jacint@167
|
61 |
|
jacint@167
|
62 |
typedef Prio PrioType;
|
jacint@167
|
63 |
|
jacint@167
|
64 |
class store;
|
jacint@167
|
65 |
|
jacint@167
|
66 |
std::vector<store> container;
|
jacint@167
|
67 |
int minimum;
|
jacint@167
|
68 |
bool blank;
|
jacint@167
|
69 |
ItemIntMap &iimap;
|
jacint@167
|
70 |
Compare comp;
|
jacint@167
|
71 |
|
jacint@167
|
72 |
enum state_enum {
|
jacint@167
|
73 |
IN_HEAP = 0,
|
jacint@167
|
74 |
PRE_HEAP = -1,
|
jacint@167
|
75 |
POST_HEAP = -2
|
jacint@167
|
76 |
};
|
jacint@167
|
77 |
|
jacint@167
|
78 |
public :
|
jacint@167
|
79 |
|
jacint@167
|
80 |
FibHeap(ItemIntMap &_iimap) : minimum(), blank(true), iimap(_iimap) {}
|
jacint@167
|
81 |
FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(),
|
jacint@167
|
82 |
blank(true), iimap(_iimap), comp(_comp) {}
|
jacint@167
|
83 |
|
jacint@167
|
84 |
|
jacint@167
|
85 |
int size() const {
|
jacint@167
|
86 |
int s=0;
|
jacint@167
|
87 |
for ( unsigned int i=0; i!=container.size(); ++i )
|
jacint@167
|
88 |
if ( container[i].in ) ++s;
|
jacint@167
|
89 |
return s;
|
jacint@167
|
90 |
}
|
jacint@167
|
91 |
|
jacint@167
|
92 |
|
jacint@167
|
93 |
bool empty() const { return blank; }
|
jacint@167
|
94 |
|
jacint@167
|
95 |
|
jacint@167
|
96 |
void set (Item const it, PrioType const value) {
|
jacint@167
|
97 |
int i=iimap.get(it);
|
jacint@167
|
98 |
if ( i >= 0 && container[i].in ) {
|
jacint@167
|
99 |
if ( !comp(container[i].prio, value) ) decrease(it, value);
|
jacint@167
|
100 |
if ( comp(container[i].prio, value) ) increase(it, value);
|
jacint@167
|
101 |
} else push(it, value);
|
jacint@167
|
102 |
}
|
jacint@167
|
103 |
|
jacint@167
|
104 |
|
jacint@167
|
105 |
void push (Item const it, PrioType const value) {
|
jacint@167
|
106 |
int i=iimap.get(it);
|
jacint@167
|
107 |
if ( i < 0 ) {
|
jacint@167
|
108 |
int s=container.size();
|
jacint@167
|
109 |
iimap.set( it, s );
|
jacint@167
|
110 |
store st;
|
jacint@167
|
111 |
st.name=it;
|
jacint@167
|
112 |
container.push_back(st);
|
jacint@167
|
113 |
i=s;
|
jacint@167
|
114 |
} else {
|
jacint@167
|
115 |
container[i].parent=container[i].child=-1;
|
jacint@167
|
116 |
container[i].degree=0;
|
jacint@167
|
117 |
container[i].in=true;
|
jacint@167
|
118 |
container[i].marked=false;
|
jacint@167
|
119 |
}
|
jacint@167
|
120 |
|
jacint@167
|
121 |
if ( !blank ) {
|
jacint@167
|
122 |
container[container[minimum].right_neighbor].left_neighbor=i;
|
jacint@167
|
123 |
container[i].right_neighbor=container[minimum].right_neighbor;
|
jacint@167
|
124 |
container[minimum].right_neighbor=i;
|
jacint@167
|
125 |
container[i].left_neighbor=minimum;
|
jacint@167
|
126 |
if ( !comp( container[minimum].prio, value) ) minimum=i;
|
jacint@167
|
127 |
} else {
|
jacint@167
|
128 |
container[i].right_neighbor=container[i].left_neighbor=i;
|
jacint@167
|
129 |
minimum=i;
|
jacint@167
|
130 |
blank=false;
|
jacint@167
|
131 |
}
|
jacint@167
|
132 |
container[i].prio=value;
|
jacint@167
|
133 |
}
|
jacint@167
|
134 |
|
jacint@167
|
135 |
|
jacint@167
|
136 |
Item top() const {
|
jacint@167
|
137 |
if ( !blank ) {
|
jacint@167
|
138 |
return container[minimum].name;
|
jacint@167
|
139 |
} else {
|
jacint@167
|
140 |
return Item();
|
jacint@167
|
141 |
}
|
jacint@167
|
142 |
}
|
jacint@167
|
143 |
|
jacint@167
|
144 |
|
jacint@167
|
145 |
PrioType prio() const {
|
jacint@167
|
146 |
if ( !blank ) {
|
jacint@167
|
147 |
return container[minimum].prio;
|
jacint@167
|
148 |
} else {
|
jacint@167
|
149 |
return PrioType();
|
jacint@167
|
150 |
}
|
jacint@167
|
151 |
}
|
jacint@167
|
152 |
|
jacint@167
|
153 |
|
jacint@167
|
154 |
const PrioType get(const Item& it) const {
|
jacint@167
|
155 |
int i=iimap.get(it);
|
jacint@167
|
156 |
|
jacint@167
|
157 |
if ( i >= 0 && container[i].in ) {
|
jacint@167
|
158 |
return container[i].prio;
|
jacint@167
|
159 |
} else {
|
jacint@167
|
160 |
return PrioType();
|
jacint@167
|
161 |
}
|
jacint@167
|
162 |
}
|
jacint@167
|
163 |
|
jacint@167
|
164 |
|
jacint@167
|
165 |
|
jacint@167
|
166 |
|
jacint@167
|
167 |
void pop() {
|
jacint@167
|
168 |
/*The first case is that there are only one root.*/
|
jacint@167
|
169 |
if ( container[minimum].left_neighbor==minimum ) {
|
jacint@167
|
170 |
container[minimum].in=false;
|
jacint@167
|
171 |
if ( container[minimum].degree==0 ) blank=true;
|
jacint@167
|
172 |
else {
|
jacint@167
|
173 |
makeroot(container[minimum].child);
|
jacint@167
|
174 |
minimum=container[minimum].child;
|
jacint@167
|
175 |
balance();
|
jacint@167
|
176 |
}
|
jacint@167
|
177 |
} else {
|
jacint@167
|
178 |
int right=container[minimum].right_neighbor;
|
jacint@167
|
179 |
unlace(minimum);
|
jacint@167
|
180 |
container[minimum].in=false;
|
jacint@167
|
181 |
if ( container[minimum].degree > 0 ) {
|
jacint@167
|
182 |
int left=container[minimum].left_neighbor;
|
jacint@167
|
183 |
int child=container[minimum].child;
|
jacint@167
|
184 |
int last_child=container[child].left_neighbor;
|
jacint@167
|
185 |
|
jacint@167
|
186 |
makeroot(child);
|
jacint@167
|
187 |
|
jacint@167
|
188 |
container[left].right_neighbor=child;
|
jacint@167
|
189 |
container[child].left_neighbor=left;
|
jacint@167
|
190 |
container[right].left_neighbor=last_child;
|
jacint@167
|
191 |
container[last_child].right_neighbor=right;
|
jacint@167
|
192 |
}
|
jacint@167
|
193 |
minimum=right;
|
jacint@167
|
194 |
balance();
|
jacint@167
|
195 |
} // the case where there are more roots
|
jacint@167
|
196 |
}
|
jacint@167
|
197 |
|
jacint@167
|
198 |
|
jacint@167
|
199 |
void erase (const Item& it) {
|
jacint@167
|
200 |
int i=iimap.get(it);
|
jacint@167
|
201 |
|
jacint@167
|
202 |
if ( i >= 0 && container[i].in ) {
|
jacint@167
|
203 |
|
jacint@167
|
204 |
if ( container[i].parent!=-1 ) {
|
jacint@167
|
205 |
int p=container[i].parent;
|
jacint@167
|
206 |
cut(i,p);
|
jacint@167
|
207 |
cascade(p);
|
jacint@167
|
208 |
minimum=i; //As if its prio would be -infinity
|
jacint@167
|
209 |
}
|
jacint@167
|
210 |
pop();
|
jacint@167
|
211 |
}
|
jacint@167
|
212 |
}
|
jacint@167
|
213 |
|
jacint@167
|
214 |
|
jacint@167
|
215 |
void decrease (Item it, PrioType const value) {
|
jacint@167
|
216 |
int i=iimap.get(it);
|
jacint@167
|
217 |
container[i].prio=value;
|
jacint@167
|
218 |
int p=container[i].parent;
|
jacint@167
|
219 |
|
jacint@167
|
220 |
if ( p!=-1 && comp(value, container[p].prio) ) {
|
jacint@167
|
221 |
cut(i,p);
|
jacint@167
|
222 |
cascade(p);
|
jacint@167
|
223 |
if ( comp(value, container[minimum].prio) ) minimum=i;
|
jacint@167
|
224 |
}
|
jacint@167
|
225 |
}
|
jacint@167
|
226 |
|
jacint@167
|
227 |
|
jacint@167
|
228 |
void increase (Item it, PrioType const value) {
|
jacint@167
|
229 |
erase(it);
|
jacint@167
|
230 |
push(it, value);
|
jacint@167
|
231 |
}
|
jacint@167
|
232 |
|
jacint@167
|
233 |
|
jacint@167
|
234 |
state_enum state(const Item &it) const {
|
jacint@167
|
235 |
int i=iimap.get(it);
|
jacint@167
|
236 |
if( i>=0 ) {
|
jacint@167
|
237 |
if ( container[i].in ) i=0;
|
jacint@167
|
238 |
else i=-2;
|
jacint@167
|
239 |
}
|
jacint@167
|
240 |
return state_enum(i);
|
jacint@167
|
241 |
}
|
jacint@167
|
242 |
|
jacint@167
|
243 |
|
jacint@167
|
244 |
private:
|
jacint@167
|
245 |
|
jacint@167
|
246 |
void balance() {
|
jacint@167
|
247 |
|
jacint@167
|
248 |
int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
|
jacint@167
|
249 |
|
jacint@167
|
250 |
std::vector<int> A(maxdeg,-1);
|
jacint@167
|
251 |
|
jacint@167
|
252 |
/*
|
jacint@167
|
253 |
*Recall that now minimum does not point to the minimum prio element.
|
jacint@167
|
254 |
*We set minimum to this during balance().
|
jacint@167
|
255 |
*/
|
jacint@167
|
256 |
int anchor=container[minimum].left_neighbor;
|
jacint@167
|
257 |
int next=minimum;
|
jacint@167
|
258 |
bool end=false;
|
jacint@167
|
259 |
|
jacint@167
|
260 |
do {
|
jacint@167
|
261 |
int active=next;
|
jacint@167
|
262 |
if ( anchor==active ) end=true;
|
jacint@167
|
263 |
int d=container[active].degree;
|
jacint@167
|
264 |
next=container[active].right_neighbor;
|
jacint@167
|
265 |
|
jacint@167
|
266 |
while (A[d]!=-1) {
|
jacint@167
|
267 |
if( comp(container[active].prio, container[A[d]].prio) ) {
|
jacint@167
|
268 |
fuse(active,A[d]);
|
jacint@167
|
269 |
} else {
|
jacint@167
|
270 |
fuse(A[d],active);
|
jacint@167
|
271 |
active=A[d];
|
jacint@167
|
272 |
}
|
jacint@167
|
273 |
A[d]=-1;
|
jacint@167
|
274 |
++d;
|
jacint@167
|
275 |
}
|
jacint@167
|
276 |
A[d]=active;
|
jacint@167
|
277 |
} while ( !end );
|
jacint@167
|
278 |
|
jacint@167
|
279 |
|
jacint@167
|
280 |
while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
|
jacint@167
|
281 |
int s=minimum;
|
jacint@167
|
282 |
int m=minimum;
|
jacint@167
|
283 |
do {
|
jacint@167
|
284 |
if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
|
jacint@167
|
285 |
s=container[s].right_neighbor;
|
jacint@167
|
286 |
} while ( s != m );
|
jacint@167
|
287 |
}
|
jacint@167
|
288 |
|
jacint@167
|
289 |
|
jacint@167
|
290 |
void makeroot (int c) {
|
jacint@167
|
291 |
int s=c;
|
jacint@167
|
292 |
do {
|
jacint@167
|
293 |
container[s].parent=-1;
|
jacint@167
|
294 |
s=container[s].right_neighbor;
|
jacint@167
|
295 |
} while ( s != c );
|
jacint@167
|
296 |
}
|
jacint@167
|
297 |
|
jacint@167
|
298 |
|
jacint@167
|
299 |
void cut (int a, int b) {
|
jacint@167
|
300 |
/*
|
jacint@167
|
301 |
*Replacing a from the children of b.
|
jacint@167
|
302 |
*/
|
jacint@167
|
303 |
--container[b].degree;
|
jacint@167
|
304 |
|
jacint@167
|
305 |
if ( container[b].degree !=0 ) {
|
jacint@167
|
306 |
int child=container[b].child;
|
jacint@167
|
307 |
if ( child==a )
|
jacint@167
|
308 |
container[b].child=container[child].right_neighbor;
|
jacint@167
|
309 |
unlace(a);
|
jacint@167
|
310 |
}
|
jacint@167
|
311 |
|
jacint@167
|
312 |
|
jacint@167
|
313 |
/*Lacing i to the roots.*/
|
jacint@167
|
314 |
int right=container[minimum].right_neighbor;
|
jacint@167
|
315 |
container[minimum].right_neighbor=a;
|
jacint@167
|
316 |
container[a].left_neighbor=minimum;
|
jacint@167
|
317 |
container[a].right_neighbor=right;
|
jacint@167
|
318 |
container[right].left_neighbor=a;
|
jacint@167
|
319 |
|
jacint@167
|
320 |
container[a].parent=-1;
|
jacint@167
|
321 |
container[a].marked=false;
|
jacint@167
|
322 |
}
|
jacint@167
|
323 |
|
jacint@167
|
324 |
|
jacint@167
|
325 |
void cascade (int a)
|
jacint@167
|
326 |
{
|
jacint@167
|
327 |
if ( container[a].parent!=-1 ) {
|
jacint@167
|
328 |
int p=container[a].parent;
|
jacint@167
|
329 |
|
jacint@167
|
330 |
if ( container[a].marked==false ) container[a].marked=true;
|
jacint@167
|
331 |
else {
|
jacint@167
|
332 |
cut(a,p);
|
jacint@167
|
333 |
cascade(p);
|
jacint@167
|
334 |
}
|
jacint@167
|
335 |
}
|
jacint@167
|
336 |
}
|
jacint@167
|
337 |
|
jacint@167
|
338 |
|
jacint@167
|
339 |
void fuse (int a, int b) {
|
jacint@167
|
340 |
unlace(b);
|
jacint@167
|
341 |
|
jacint@167
|
342 |
/*Lacing b under a.*/
|
jacint@167
|
343 |
container[b].parent=a;
|
jacint@167
|
344 |
|
jacint@167
|
345 |
if (container[a].degree==0) {
|
jacint@167
|
346 |
container[b].left_neighbor=b;
|
jacint@167
|
347 |
container[b].right_neighbor=b;
|
jacint@167
|
348 |
container[a].child=b;
|
jacint@167
|
349 |
} else {
|
jacint@167
|
350 |
int child=container[a].child;
|
jacint@167
|
351 |
int last_child=container[child].left_neighbor;
|
jacint@167
|
352 |
container[child].left_neighbor=b;
|
jacint@167
|
353 |
container[b].right_neighbor=child;
|
jacint@167
|
354 |
container[last_child].right_neighbor=b;
|
jacint@167
|
355 |
container[b].left_neighbor=last_child;
|
jacint@167
|
356 |
}
|
jacint@167
|
357 |
|
jacint@167
|
358 |
++container[a].degree;
|
jacint@167
|
359 |
|
jacint@167
|
360 |
container[b].marked=false;
|
jacint@167
|
361 |
}
|
jacint@167
|
362 |
|
jacint@167
|
363 |
|
jacint@167
|
364 |
/*
|
jacint@167
|
365 |
*It is invoked only if a has siblings.
|
jacint@167
|
366 |
*/
|
jacint@167
|
367 |
void unlace (int a) {
|
jacint@167
|
368 |
int leftn=container[a].left_neighbor;
|
jacint@167
|
369 |
int rightn=container[a].right_neighbor;
|
jacint@167
|
370 |
container[leftn].right_neighbor=rightn;
|
jacint@167
|
371 |
container[rightn].left_neighbor=leftn;
|
jacint@167
|
372 |
}
|
jacint@167
|
373 |
|
jacint@167
|
374 |
|
jacint@167
|
375 |
class store {
|
jacint@167
|
376 |
friend class FibHeap;
|
jacint@167
|
377 |
|
jacint@167
|
378 |
Item name;
|
jacint@167
|
379 |
int parent;
|
jacint@167
|
380 |
int left_neighbor;
|
jacint@167
|
381 |
int right_neighbor;
|
jacint@167
|
382 |
int child;
|
jacint@167
|
383 |
int degree;
|
jacint@167
|
384 |
bool marked;
|
jacint@167
|
385 |
bool in;
|
jacint@167
|
386 |
PrioType prio;
|
jacint@167
|
387 |
|
jacint@167
|
388 |
store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
|
jacint@167
|
389 |
};
|
jacint@167
|
390 |
|
jacint@167
|
391 |
};
|
jacint@167
|
392 |
|
jacint@167
|
393 |
} //namespace hugo
|
jacint@167
|
394 |
#endif
|
jacint@167
|
395 |
|
jacint@167
|
396 |
|
jacint@167
|
397 |
|
jacint@167
|
398 |
|
jacint@167
|
399 |
|
jacint@167
|
400 |
|