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