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. Item must be in the heap
35 * with prio at least P.
37 *void increase(Item, P) : sets prio of Item 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 set (Item const it, PrioType const value) {
88 if ( i >= 0 && container[i].in ) {
89 if ( !comp(container[i].prio, value) ) decrease(it, value);
90 if ( comp(container[i].prio, value) ) increase(it, value);
91 } else push(it, value);
95 void push (Item const it, PrioType const value) {
98 int s=container.size();
102 container.push_back(st);
107 container[container[minimum].right_neighbor].left_neighbor=i;
108 container[i].right_neighbor=container[minimum].right_neighbor;
109 container[minimum].right_neighbor=i;
110 container[i].left_neighbor=minimum;
111 if ( !comp( container[minimum].prio, value) ) minimum=i;
114 container[i].right_neighbor=container[i].left_neighbor=i;
118 container[i].prio=value;
125 return container[minimum].name;
132 PrioType prio() const {
134 return container[minimum].prio;
141 const PrioType get(const Item& it) const
145 if ( i >= 0 && container[i].in ) {
146 return container[i].prio;
156 /*The first case is that there are only one root.*/
157 if ( container[minimum].left_neighbor==minimum ) {
158 container[minimum].in=false;
159 if ( container[minimum].degree==0 ) blank=true;
161 makeroot(container[minimum].child);
162 minimum=container[minimum].child;
166 int right=container[minimum].right_neighbor;
168 container[minimum].in=false;
169 if ( container[minimum].degree > 0 ) {
170 int left=container[minimum].left_neighbor;
171 int child=container[minimum].child;
172 int last_child=container[child].left_neighbor;
174 container[left].right_neighbor=child;
175 container[child].left_neighbor=left;
176 container[right].left_neighbor=last_child;
177 container[last_child].right_neighbor=right;
183 } // the case where there are more roots
188 void erase (const Item& it) {
191 if ( i >= 0 && container[i].in ) {
193 if ( container[i].parent!=-1 ) {
194 int p=container[i].parent;
197 minimum=i; //As if its prio would be -infinity
204 void decrease (Item it, PrioType const value) {
206 container[i].prio=value;
207 int p=container[i].parent;
209 if ( p!=-1 && comp(value, container[p].prio) ) {
212 if ( comp(value, container[minimum].prio) ) minimum=i;
217 void increase (Item it, PrioType const value) {
226 int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
228 std::vector<int> A(maxdeg,-1);
231 *Recall that now minimum does not point to the minimum prio element.
232 *We set minimum to this during balance().
234 int anchor=container[minimum].left_neighbor;
240 int d=container[active].degree;
241 if ( anchor==active ) end=true;
242 next = container[active].right_neighbor;
243 if ( !comp(container[minimum].prio, container[active].prio) )
248 if( comp(container[active].prio, container[A[d]].prio) ) {
266 *All the siblings of a are made roots.
268 void makeroot (int c)
272 container[s].parent=-1;
273 s=container[s].right_neighbor;
278 void cut (int a, int b)
282 *Replacing a from the children of b.
284 --container[b].degree;
286 if ( container[b].degree !=0 ) {
287 int child=container[b].child;
289 container[b].child=container[child].right_neighbor;
296 /*Lacing i to the roots.*/
297 int right=container[minimum].right_neighbor;
298 container[minimum].right_neighbor=a;
299 container[a].left_neighbor=minimum;
300 container[a].right_neighbor=right;
301 container[right].left_neighbor=a;
303 container[a].parent=-1;
304 container[a].marked=false;
310 if ( container[a].parent!=-1 ) {
311 int p=container[a].parent;
313 if ( container[a].marked==false ) container[a].marked=true;
322 void fuse (int a, int b)
328 /*Lacing b under a.*/
329 container[b].parent=a;
331 if (container[a].degree==0) {
332 container[b].left_neighbor=b;
333 container[b].right_neighbor=b;
334 container[a].child=b;
336 int child=container[a].child;
337 int last_child=container[child].left_neighbor;
338 container[child].left_neighbor=b;
339 container[b].right_neighbor=child;
340 container[last_child].right_neighbor=b;
341 container[b].left_neighbor=last_child;
344 ++container[a].degree;
346 container[b].marked=false;
351 *It is invoked only if a has siblings.
354 void unlace (int a) {
355 int leftn=container[a].left_neighbor;
356 int rightn=container[a].right_neighbor;
357 container[leftn].right_neighbor=rightn;
358 container[rightn].left_neighbor=leftn;
363 friend class FibHeap;
375 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}