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.
25 * Must be called only if heap is nonempty.
27 *Prio prio() : returns the least Prio
28 * Must be called only if heap is nonempty.
30 *Prio get(Item) : returns Prio of Item
31 * Must be called only if Item is in heap.
33 *void pop() : deletes the Item with least Prio
35 *void erase(Item) : deletes Item from the heap if it was already there
37 *void decrease(Item, P) : decreases prio of Item to P.
38 * Item must be in the heap with prio at least P.
40 *void increase(Item, P) : sets prio of Item to P.
42 *state_enum state(Item) : returns PRE_HEAP if Item has not been in the
43 * heap until now, IN_HEAP if it is in the heap at the moment, and
44 * POST_HEAP otherwise. In the latter case it is possible that Item
45 * will get back to the heap again.
47 *In Fibonacci heaps, increase and erase are not efficient, in case of
48 *many calls to these operations, it is better to use bin_heap.
60 /// A Fibonacci Heap implementation.
61 template <typename Item, typename Prio, typename ItemIntMap,
62 typename Compare = std::less<Prio> >
65 typedef Prio PrioType;
69 std::vector<store> container;
75 ///\todo It is use nowhere
76 ///\todo It doesn't conforms to the naming conventions.
86 FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {}
87 FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(),
88 iimap(_iimap), comp(_comp), num_items() {}
96 bool empty() const { return num_items==0; }
99 void set (Item const it, PrioType const value) {
101 if ( i >= 0 && container[i].in ) {
102 if ( comp(value, container[i].prio) ) decrease(it, value);
103 if ( comp(container[i].prio, value) ) increase(it, value);
104 } else push(it, value);
108 void push (Item const it, PrioType const value) {
111 int s=container.size();
115 container.push_back(st);
118 container[i].parent=container[i].child=-1;
119 container[i].degree=0;
120 container[i].in=true;
121 container[i].marked=false;
125 container[container[minimum].right_neighbor].left_neighbor=i;
126 container[i].right_neighbor=container[minimum].right_neighbor;
127 container[minimum].right_neighbor=i;
128 container[i].left_neighbor=minimum;
129 if ( comp( value, container[minimum].prio) ) minimum=i;
131 container[i].right_neighbor=container[i].left_neighbor=i;
134 container[i].prio=value;
140 return container[minimum].name;
144 PrioType prio() const {
145 return container[minimum].prio;
151 PrioType& operator[](const Item& it) {
152 return container[iimap[it]].prio;
155 const PrioType& operator[](const Item& it) const {
156 return container[iimap[it]].prio;
159 // const PrioType get(const Item& it) const {
160 // return container[iimap[it]].prio;
164 /*The first case is that there are only one root.*/
165 if ( container[minimum].left_neighbor==minimum ) {
166 container[minimum].in=false;
167 if ( container[minimum].degree!=0 ) {
168 makeroot(container[minimum].child);
169 minimum=container[minimum].child;
173 int right=container[minimum].right_neighbor;
175 container[minimum].in=false;
176 if ( container[minimum].degree > 0 ) {
177 int left=container[minimum].left_neighbor;
178 int child=container[minimum].child;
179 int last_child=container[child].left_neighbor;
183 container[left].right_neighbor=child;
184 container[child].left_neighbor=left;
185 container[right].left_neighbor=last_child;
186 container[last_child].right_neighbor=right;
190 } // the case where there are more roots
195 void erase (const Item& it) {
198 if ( i >= 0 && container[i].in ) {
199 if ( container[i].parent!=-1 ) {
200 int p=container[i].parent;
204 minimum=i; //As if its prio would be -infinity
210 void decrease (Item it, PrioType const value) {
212 container[i].prio=value;
213 int p=container[i].parent;
215 if ( p!=-1 && comp(value, container[p].prio) ) {
219 if ( comp(value, container[minimum].prio) ) minimum=i;
223 void increase (Item it, PrioType const value) {
229 state_enum state(const Item &it) const {
232 if ( container[i].in ) i=0;
235 return state_enum(i);
243 int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
245 std::vector<int> A(maxdeg,-1);
248 *Recall that now minimum does not point to the minimum prio element.
249 *We set minimum to this during balance().
251 int anchor=container[minimum].left_neighbor;
257 if ( anchor==active ) end=true;
258 int d=container[active].degree;
259 next=container[active].right_neighbor;
262 if( comp(container[active].prio, container[A[d]].prio) ) {
275 while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
279 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
280 s=container[s].right_neighbor;
285 void makeroot (int c) {
288 container[s].parent=-1;
289 s=container[s].right_neighbor;
294 void cut (int a, int b) {
296 *Replacing a from the children of b.
298 --container[b].degree;
300 if ( container[b].degree !=0 ) {
301 int child=container[b].child;
303 container[b].child=container[child].right_neighbor;
308 /*Lacing a 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) {
337 /*Lacing b under a.*/
338 container[b].parent=a;
340 if (container[a].degree==0) {
341 container[b].left_neighbor=b;
342 container[b].right_neighbor=b;
343 container[a].child=b;
345 int child=container[a].child;
346 int last_child=container[child].left_neighbor;
347 container[child].left_neighbor=b;
348 container[b].right_neighbor=child;
349 container[last_child].right_neighbor=b;
350 container[b].left_neighbor=last_child;
353 ++container[a].degree;
355 container[b].marked=false;
360 *It is invoked only if a has siblings.
362 void unlace (int a) {
363 int leftn=container[a].left_neighbor;
364 int rightn=container[a].right_neighbor;
365 container[leftn].right_neighbor=rightn;
366 container[rightn].left_neighbor=leftn;
371 friend class FibHeap;
383 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}