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 push(Item, Prio) : pushes Item to the heap with priority Prio. If
19 * Item was already in the heap, it calls decrease(Item, Prio)
21 *Item top() : returns the Item with least Prio
23 *Prio prio() : returns the least Prio
25 *Prio get(Item) : returns Prio of Item
27 *void pop() : deletes the Item with least Prio
29 *void erase(Item) : deletes Item from the heap if it was already there
31 *void decrease(Item, P) : If Item was not in the heap, then it calls
32 * push(Item, P). If item is in the heap with Prio more than P
33 * then sets its Prio to P.
35 *void increase(Item, P) : If Item was not in the heap, then it calls
36 * push(Item, P). If item is in the heap with Prio less than P
37 * then sets its Prio to P.
40 *In Fibonacci heaps, increase and erase are not efficient, in case of
41 *many calls to these operations, it is better to use bin_heap.
53 template <typename Item, typename Prio, typename ItemIntMap,
54 typename Compare = std::less<Prio> >
58 typedef Prio PrioType;
62 std::vector<store> container;
70 FibHeap(ItemIntMap &_iimap) : minimum(), blank(true), iimap(_iimap) {}
71 FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(),
72 blank(true), iimap(_iimap), comp(_comp) {}
77 for ( unsigned int i=0; i!=container.size(); ++i )
78 if ( container[i].in ) ++s;
83 bool empty() const { return blank; }
86 void push (Item const it, PrioType const value)
91 if ( i >= 0 && container[i].in ) decrease(it, value);
94 int s=container.size();
98 container.push_back(st);
103 container[container[minimum].right_neighbor].left_neighbor=i;
104 container[i].right_neighbor=container[minimum].right_neighbor;
105 container[minimum].right_neighbor=i;
106 container[i].left_neighbor=minimum;
107 if ( !comp( container[minimum].prio, value) ) minimum=i;
112 container[i].right_neighbor=container[i].left_neighbor=i;
116 container[i].prio=value;
123 return container[minimum].name;
130 PrioType prio() const {
132 return container[minimum].prio;
139 const PrioType get(const Item& it) const
143 if ( i >= 0 && container[i].in ) {
144 return container[i].prio;
154 /*The first case is that there are only one root.*/
155 if ( container[minimum].left_neighbor==minimum ) {
156 container[minimum].in=false;
157 if ( container[minimum].degree==0 ) blank=true;
159 makeroot(container[minimum].child);
160 minimum=container[minimum].child;
164 int right=container[minimum].right_neighbor;
166 container[minimum].in=false;
167 if ( container[minimum].degree > 0 ) {
168 int left=container[minimum].left_neighbor;
169 int child=container[minimum].child;
170 int last_child=container[child].left_neighbor;
172 container[left].right_neighbor=child;
173 container[child].left_neighbor=left;
174 container[right].left_neighbor=last_child;
175 container[last_child].right_neighbor=right;
181 } // the case where there are more roots
186 void erase (const Item& it) {
189 if ( i >= 0 && container[i].in ) {
191 if ( container[i].parent!=-1 ) {
192 int p=container[i].parent;
195 minimum=i; //As if its prio would be -infinity
202 void decrease (Item it, PrioType const value) {
204 if ( i >= 0 && container[i].in ) {
206 if ( comp(value, container[i].prio) ) {
207 container[i].prio=value;
209 if ( container[i].parent!=-1 ) {
210 int p=container[i].parent;
212 if ( !comp(container[p].prio, value) ) {
215 if ( comp(value, container[minimum].prio) ) minimum=i;
219 } else push(it, value);
223 void increase (Item it, PrioType const value) {
226 if ( i >= 0 && container[i].in ) {
227 if ( comp(container[i].prio, value) ) {
231 } else push(it, value);
238 int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
240 std::vector<int> A(maxdeg,-1);
243 *Recall that now minimum does not point to the minimum prio element.
244 *We set minimum to this during balance().
246 int anchor=container[minimum].left_neighbor;
252 int d=container[active].degree;
253 if ( anchor==active ) end=true;
254 next = container[active].right_neighbor;
255 if ( !comp(container[minimum].prio, container[active].prio) )
260 if( comp(container[active].prio, container[A[d]].prio) ) {
278 *All the siblings of a are made roots.
280 void makeroot (int c)
284 container[s].parent=-1;
285 s=container[s].right_neighbor;
290 void cut (int a, int b)
294 *Replacing a from the children of b.
296 --container[b].degree;
298 if ( container[b].degree !=0 ) {
299 int child=container[b].child;
301 container[b].child=container[child].right_neighbor;
308 /*Lacing i to the roots.*/
309 int right=container[minimum].right_neighbor;
310 container[minimum].right_neighbor=a;
311 container[a].left_neighbor=minimum;
312 container[a].right_neighbor=right;
313 container[right].left_neighbor=a;
315 container[a].parent=-1;
316 container[a].marked=false;
322 if ( container[a].parent!=-1 ) {
323 int p=container[a].parent;
325 if ( container[a].marked==false ) container[a].marked=true;
334 void fuse (int a, int b)
340 /*Lacing b under a.*/
341 container[b].parent=a;
343 if (container[a].degree==0) {
344 container[b].left_neighbor=b;
345 container[b].right_neighbor=b;
346 container[a].child=b;
348 int child=container[a].child;
349 int last_child=container[child].left_neighbor;
350 container[child].left_neighbor=b;
351 container[b].right_neighbor=child;
352 container[last_child].right_neighbor=b;
353 container[b].left_neighbor=last_child;
356 ++container[a].degree;
358 container[b].marked=false;
363 *It is invoked only if a has siblings.
366 void unlace (int a) {
367 int leftn=container[a].left_neighbor;
368 int rightn=container[a].right_neighbor;
369 container[leftn].right_neighbor=rightn;
370 container[rightn].left_neighbor=leftn;
375 friend class FibHeap;
387 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}