3 *template <typename Item,
6 * typename Compare = std::less<Prio> >
10 *FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare)
14 *int size() : returns the number of elements in the heap
16 *bool empty() : true iff size()=0
18 *void set(Item, Prio) : calls push(Item, Prio) if Item is not
19 * in the heap, and calls decrease/increase(Item, Prio) otherwise
21 *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item
22 * mustn't be in the heap.
24 *Item top() : returns the Item with least Prio
26 *Prio prio() : returns the least Prio
28 *Prio get(Item) : returns Prio of Item
30 *void pop() : deletes the Item with least Prio
32 *void erase(Item) : deletes Item from the heap if it was already there
34 *void decrease(Item, P) : decreases prio of Item to P.
35 * Item must be in the heap with prio at least P.
37 *void increase(Item, P) : sets prio of Item to P.
39 *state_enum state(Item) : returns PRE_HEAP if Item has not been in the
40 * heap until now, IN_HEAP if it is in the heap at the moment, and
41 * POST_HEAP otherwise. In the latter case it is possible that Item
42 * will get back to the heap again.
44 *In Fibonacci heaps, increase and erase are not efficient, in case of
45 *many calls to these operations, it is better to use bin_heap.
57 template <typename Item, typename Prio, typename ItemIntMap,
58 typename Compare = std::less<Prio> >
62 typedef Prio PrioType;
66 std::vector<store> container;
80 FibHeap(ItemIntMap &_iimap) : minimum(), blank(true), iimap(_iimap) {}
81 FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(),
82 blank(true), iimap(_iimap), comp(_comp) {}
87 for ( unsigned int i=0; i!=container.size(); ++i )
88 if ( container[i].in ) ++s;
93 bool empty() const { return blank; }
96 void set (Item const it, PrioType const value) {
98 if ( i >= 0 && container[i].in ) {
99 if ( !comp(container[i].prio, value) ) decrease(it, value);
100 if ( comp(container[i].prio, value) ) increase(it, value);
101 } else push(it, value);
105 void push (Item const it, PrioType const value) {
108 int s=container.size();
112 container.push_back(st);
115 container[i].parent=container[i].child=-1;
116 container[i].degree=0;
117 container[i].in=true;
118 container[i].marked=false;
122 container[container[minimum].right_neighbor].left_neighbor=i;
123 container[i].right_neighbor=container[minimum].right_neighbor;
124 container[minimum].right_neighbor=i;
125 container[i].left_neighbor=minimum;
126 if ( !comp( container[minimum].prio, value) ) minimum=i;
128 container[i].right_neighbor=container[i].left_neighbor=i;
132 container[i].prio=value;
138 return container[minimum].name;
145 PrioType prio() const {
147 return container[minimum].prio;
154 const PrioType get(const Item& it) const {
157 if ( i >= 0 && container[i].in ) {
158 return container[i].prio;
168 /*The first case is that there are only one root.*/
169 if ( container[minimum].left_neighbor==minimum ) {
170 container[minimum].in=false;
171 if ( container[minimum].degree==0 ) blank=true;
173 makeroot(container[minimum].child);
174 minimum=container[minimum].child;
178 int right=container[minimum].right_neighbor;
180 container[minimum].in=false;
181 if ( container[minimum].degree > 0 ) {
182 int left=container[minimum].left_neighbor;
183 int child=container[minimum].child;
184 int last_child=container[child].left_neighbor;
188 container[left].right_neighbor=child;
189 container[child].left_neighbor=left;
190 container[right].left_neighbor=last_child;
191 container[last_child].right_neighbor=right;
195 } // the case where there are more roots
199 void erase (const Item& it) {
202 if ( i >= 0 && container[i].in ) {
204 if ( container[i].parent!=-1 ) {
205 int p=container[i].parent;
208 minimum=i; //As if its prio would be -infinity
215 void decrease (Item it, PrioType const value) {
217 container[i].prio=value;
218 int p=container[i].parent;
220 if ( p!=-1 && comp(value, container[p].prio) ) {
223 if ( comp(value, container[minimum].prio) ) minimum=i;
228 void increase (Item it, PrioType const value) {
234 state_enum state(const Item &it) const {
237 if ( container[i].in ) i=0;
240 return state_enum(i);
248 int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
250 std::vector<int> A(maxdeg,-1);
253 *Recall that now minimum does not point to the minimum prio element.
254 *We set minimum to this during balance().
256 int anchor=container[minimum].left_neighbor;
262 if ( anchor==active ) end=true;
263 int d=container[active].degree;
264 next=container[active].right_neighbor;
267 if( comp(container[active].prio, container[A[d]].prio) ) {
280 while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
284 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
285 s=container[s].right_neighbor;
290 void makeroot (int c) {
293 container[s].parent=-1;
294 s=container[s].right_neighbor;
299 void cut (int a, int b) {
301 *Replacing a from the children of b.
303 --container[b].degree;
305 if ( container[b].degree !=0 ) {
306 int child=container[b].child;
308 container[b].child=container[child].right_neighbor;
313 /*Lacing i to the roots.*/
314 int right=container[minimum].right_neighbor;
315 container[minimum].right_neighbor=a;
316 container[a].left_neighbor=minimum;
317 container[a].right_neighbor=right;
318 container[right].left_neighbor=a;
320 container[a].parent=-1;
321 container[a].marked=false;
327 if ( container[a].parent!=-1 ) {
328 int p=container[a].parent;
330 if ( container[a].marked==false ) container[a].marked=true;
339 void fuse (int a, int b) {
342 /*Lacing b under a.*/
343 container[b].parent=a;
345 if (container[a].degree==0) {
346 container[b].left_neighbor=b;
347 container[b].right_neighbor=b;
348 container[a].child=b;
350 int child=container[a].child;
351 int last_child=container[child].left_neighbor;
352 container[child].left_neighbor=b;
353 container[b].right_neighbor=child;
354 container[last_child].right_neighbor=b;
355 container[b].left_neighbor=last_child;
358 ++container[a].degree;
360 container[b].marked=false;
365 *It is invoked only if a has siblings.
367 void unlace (int a) {
368 int leftn=container[a].left_neighbor;
369 int rightn=container[a].right_neighbor;
370 container[leftn].right_neighbor=rightn;
371 container[rightn].left_neighbor=leftn;
376 friend class FibHeap;
388 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}