1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/fib_heap.h Thu Jun 11 22:11:29 2009 +0200
1.3 @@ -0,0 +1,467 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2009
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_FIB_HEAP_H
1.23 +#define LEMON_FIB_HEAP_H
1.24 +
1.25 +///\file
1.26 +///\ingroup auxdat
1.27 +///\brief Fibonacci Heap implementation.
1.28 +
1.29 +#include <vector>
1.30 +#include <functional>
1.31 +#include <lemon/math.h>
1.32 +
1.33 +namespace lemon {
1.34 +
1.35 + /// \ingroup auxdat
1.36 + ///
1.37 + ///\brief Fibonacci Heap.
1.38 + ///
1.39 + ///This class implements the \e Fibonacci \e heap data structure. A \e heap
1.40 + ///is a data structure for storing items with specified values called \e
1.41 + ///priorities in such a way that finding the item with minimum priority is
1.42 + ///efficient. \c Compare specifies the ordering of the priorities. In a heap
1.43 + ///one can change the priority of an item, add or erase an item, etc.
1.44 + ///
1.45 + ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
1.46 + ///heap. In case of many calls to these operations, it is better to use a
1.47 + ///\ref BinHeap "binary heap".
1.48 + ///
1.49 + ///\param _Prio Type of the priority of the items.
1.50 + ///\param _ItemIntMap A read and writable Item int map, used internally
1.51 + ///to handle the cross references.
1.52 + ///\param _Compare A class for the ordering of the priorities. The
1.53 + ///default is \c std::less<_Prio>.
1.54 + ///
1.55 + ///\sa BinHeap
1.56 + ///\sa Dijkstra
1.57 +#ifdef DOXYGEN
1.58 + template <typename _Prio,
1.59 + typename _ItemIntMap,
1.60 + typename _Compare>
1.61 +#else
1.62 + template <typename _Prio,
1.63 + typename _ItemIntMap,
1.64 + typename _Compare = std::less<_Prio> >
1.65 +#endif
1.66 + class FibHeap {
1.67 + public:
1.68 + ///\e
1.69 + typedef _ItemIntMap ItemIntMap;
1.70 + ///\e
1.71 + typedef _Prio Prio;
1.72 + ///\e
1.73 + typedef typename ItemIntMap::Key Item;
1.74 + ///\e
1.75 + typedef std::pair<Item,Prio> Pair;
1.76 + ///\e
1.77 + typedef _Compare Compare;
1.78 +
1.79 + private:
1.80 + class store;
1.81 +
1.82 + std::vector<store> container;
1.83 + int minimum;
1.84 + ItemIntMap &iimap;
1.85 + Compare comp;
1.86 + int num_items;
1.87 +
1.88 + public:
1.89 + ///Status of the nodes
1.90 + enum State {
1.91 + ///The node is in the heap
1.92 + IN_HEAP = 0,
1.93 + ///The node has never been in the heap
1.94 + PRE_HEAP = -1,
1.95 + ///The node was in the heap but it got out of it
1.96 + POST_HEAP = -2
1.97 + };
1.98 +
1.99 + /// \brief The constructor
1.100 + ///
1.101 + /// \c _iimap should be given to the constructor, since it is
1.102 + /// used internally to handle the cross references.
1.103 + explicit FibHeap(ItemIntMap &_iimap)
1.104 + : minimum(0), iimap(_iimap), num_items() {}
1.105 +
1.106 + /// \brief The constructor
1.107 + ///
1.108 + /// \c _iimap should be given to the constructor, since it is used
1.109 + /// internally to handle the cross references. \c _comp is an
1.110 + /// object for ordering of the priorities.
1.111 + FibHeap(ItemIntMap &_iimap, const Compare &_comp)
1.112 + : minimum(0), iimap(_iimap), comp(_comp), num_items() {}
1.113 +
1.114 + /// \brief The number of items stored in the heap.
1.115 + ///
1.116 + /// Returns the number of items stored in the heap.
1.117 + int size() const { return num_items; }
1.118 +
1.119 + /// \brief Checks if the heap stores no items.
1.120 + ///
1.121 + /// Returns \c true if and only if the heap stores no items.
1.122 + bool empty() const { return num_items==0; }
1.123 +
1.124 + /// \brief Make empty this heap.
1.125 + ///
1.126 + /// Make empty this heap. It does not change the cross reference
1.127 + /// map. If you want to reuse a heap what is not surely empty you
1.128 + /// should first clear the heap and after that you should set the
1.129 + /// cross reference map for each item to \c PRE_HEAP.
1.130 + void clear() {
1.131 + container.clear(); minimum = 0; num_items = 0;
1.132 + }
1.133 +
1.134 + /// \brief \c item gets to the heap with priority \c value independently
1.135 + /// if \c item was already there.
1.136 + ///
1.137 + /// This method calls \ref push(\c item, \c value) if \c item is not
1.138 + /// stored in the heap and it calls \ref decrease(\c item, \c value) or
1.139 + /// \ref increase(\c item, \c value) otherwise.
1.140 + void set (const Item& item, const Prio& value) {
1.141 + int i=iimap[item];
1.142 + if ( i >= 0 && container[i].in ) {
1.143 + if ( comp(value, container[i].prio) ) decrease(item, value);
1.144 + if ( comp(container[i].prio, value) ) increase(item, value);
1.145 + } else push(item, value);
1.146 + }
1.147 +
1.148 + /// \brief Adds \c item to the heap with priority \c value.
1.149 + ///
1.150 + /// Adds \c item to the heap with priority \c value.
1.151 + /// \pre \c item must not be stored in the heap.
1.152 + void push (const Item& item, const Prio& value) {
1.153 + int i=iimap[item];
1.154 + if ( i < 0 ) {
1.155 + int s=container.size();
1.156 + iimap.set( item, s );
1.157 + store st;
1.158 + st.name=item;
1.159 + container.push_back(st);
1.160 + i=s;
1.161 + } else {
1.162 + container[i].parent=container[i].child=-1;
1.163 + container[i].degree=0;
1.164 + container[i].in=true;
1.165 + container[i].marked=false;
1.166 + }
1.167 +
1.168 + if ( num_items ) {
1.169 + container[container[minimum].right_neighbor].left_neighbor=i;
1.170 + container[i].right_neighbor=container[minimum].right_neighbor;
1.171 + container[minimum].right_neighbor=i;
1.172 + container[i].left_neighbor=minimum;
1.173 + if ( comp( value, container[minimum].prio) ) minimum=i;
1.174 + } else {
1.175 + container[i].right_neighbor=container[i].left_neighbor=i;
1.176 + minimum=i;
1.177 + }
1.178 + container[i].prio=value;
1.179 + ++num_items;
1.180 + }
1.181 +
1.182 + /// \brief Returns the item with minimum priority relative to \c Compare.
1.183 + ///
1.184 + /// This method returns the item with minimum priority relative to \c
1.185 + /// Compare.
1.186 + /// \pre The heap must be nonempty.
1.187 + Item top() const { return container[minimum].name; }
1.188 +
1.189 + /// \brief Returns the minimum priority relative to \c Compare.
1.190 + ///
1.191 + /// It returns the minimum priority relative to \c Compare.
1.192 + /// \pre The heap must be nonempty.
1.193 + const Prio& prio() const { return container[minimum].prio; }
1.194 +
1.195 + /// \brief Returns the priority of \c item.
1.196 + ///
1.197 + /// It returns the priority of \c item.
1.198 + /// \pre \c item must be in the heap.
1.199 + const Prio& operator[](const Item& item) const {
1.200 + return container[iimap[item]].prio;
1.201 + }
1.202 +
1.203 + /// \brief Deletes the item with minimum priority relative to \c Compare.
1.204 + ///
1.205 + /// This method deletes the item with minimum priority relative to \c
1.206 + /// Compare from the heap.
1.207 + /// \pre The heap must be non-empty.
1.208 + void pop() {
1.209 + /*The first case is that there are only one root.*/
1.210 + if ( container[minimum].left_neighbor==minimum ) {
1.211 + container[minimum].in=false;
1.212 + if ( container[minimum].degree!=0 ) {
1.213 + makeroot(container[minimum].child);
1.214 + minimum=container[minimum].child;
1.215 + balance();
1.216 + }
1.217 + } else {
1.218 + int right=container[minimum].right_neighbor;
1.219 + unlace(minimum);
1.220 + container[minimum].in=false;
1.221 + if ( container[minimum].degree > 0 ) {
1.222 + int left=container[minimum].left_neighbor;
1.223 + int child=container[minimum].child;
1.224 + int last_child=container[child].left_neighbor;
1.225 +
1.226 + makeroot(child);
1.227 +
1.228 + container[left].right_neighbor=child;
1.229 + container[child].left_neighbor=left;
1.230 + container[right].left_neighbor=last_child;
1.231 + container[last_child].right_neighbor=right;
1.232 + }
1.233 + minimum=right;
1.234 + balance();
1.235 + } // the case where there are more roots
1.236 + --num_items;
1.237 + }
1.238 +
1.239 + /// \brief Deletes \c item from the heap.
1.240 + ///
1.241 + /// This method deletes \c item from the heap, if \c item was already
1.242 + /// stored in the heap. It is quite inefficient in Fibonacci heaps.
1.243 + void erase (const Item& item) {
1.244 + int i=iimap[item];
1.245 +
1.246 + if ( i >= 0 && container[i].in ) {
1.247 + if ( container[i].parent!=-1 ) {
1.248 + int p=container[i].parent;
1.249 + cut(i,p);
1.250 + cascade(p);
1.251 + }
1.252 + minimum=i; //As if its prio would be -infinity
1.253 + pop();
1.254 + }
1.255 + }
1.256 +
1.257 + /// \brief Decreases the priority of \c item to \c value.
1.258 + ///
1.259 + /// This method decreases the priority of \c item to \c value.
1.260 + /// \pre \c item must be stored in the heap with priority at least \c
1.261 + /// value relative to \c Compare.
1.262 + void decrease (Item item, const Prio& value) {
1.263 + int i=iimap[item];
1.264 + container[i].prio=value;
1.265 + int p=container[i].parent;
1.266 +
1.267 + if ( p!=-1 && comp(value, container[p].prio) ) {
1.268 + cut(i,p);
1.269 + cascade(p);
1.270 + }
1.271 + if ( comp(value, container[minimum].prio) ) minimum=i;
1.272 + }
1.273 +
1.274 + /// \brief Increases the priority of \c item to \c value.
1.275 + ///
1.276 + /// This method sets the priority of \c item to \c value. Though
1.277 + /// there is no precondition on the priority of \c item, this
1.278 + /// method should be used only if it is indeed necessary to increase
1.279 + /// (relative to \c Compare) the priority of \c item, because this
1.280 + /// method is inefficient.
1.281 + void increase (Item item, const Prio& value) {
1.282 + erase(item);
1.283 + push(item, value);
1.284 + }
1.285 +
1.286 +
1.287 + /// \brief Returns if \c item is in, has already been in, or has never
1.288 + /// been in the heap.
1.289 + ///
1.290 + /// This method returns PRE_HEAP if \c item has never been in the
1.291 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.292 + /// otherwise. In the latter case it is possible that \c item will
1.293 + /// get back to the heap again.
1.294 + State state(const Item &item) const {
1.295 + int i=iimap[item];
1.296 + if( i>=0 ) {
1.297 + if ( container[i].in ) i=0;
1.298 + else i=-2;
1.299 + }
1.300 + return State(i);
1.301 + }
1.302 +
1.303 + /// \brief Sets the state of the \c item in the heap.
1.304 + ///
1.305 + /// Sets the state of the \c item in the heap. It can be used to
1.306 + /// manually clear the heap when it is important to achive the
1.307 + /// better time complexity.
1.308 + /// \param i The item.
1.309 + /// \param st The state. It should not be \c IN_HEAP.
1.310 + void state(const Item& i, State st) {
1.311 + switch (st) {
1.312 + case POST_HEAP:
1.313 + case PRE_HEAP:
1.314 + if (state(i) == IN_HEAP) {
1.315 + erase(i);
1.316 + }
1.317 + iimap[i] = st;
1.318 + break;
1.319 + case IN_HEAP:
1.320 + break;
1.321 + }
1.322 + }
1.323 +
1.324 + private:
1.325 +
1.326 + void balance() {
1.327 +
1.328 + int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1;
1.329 +
1.330 + std::vector<int> A(maxdeg,-1);
1.331 +
1.332 + /*
1.333 + *Recall that now minimum does not point to the minimum prio element.
1.334 + *We set minimum to this during balance().
1.335 + */
1.336 + int anchor=container[minimum].left_neighbor;
1.337 + int next=minimum;
1.338 + bool end=false;
1.339 +
1.340 + do {
1.341 + int active=next;
1.342 + if ( anchor==active ) end=true;
1.343 + int d=container[active].degree;
1.344 + next=container[active].right_neighbor;
1.345 +
1.346 + while (A[d]!=-1) {
1.347 + if( comp(container[active].prio, container[A[d]].prio) ) {
1.348 + fuse(active,A[d]);
1.349 + } else {
1.350 + fuse(A[d],active);
1.351 + active=A[d];
1.352 + }
1.353 + A[d]=-1;
1.354 + ++d;
1.355 + }
1.356 + A[d]=active;
1.357 + } while ( !end );
1.358 +
1.359 +
1.360 + while ( container[minimum].parent >=0 )
1.361 + minimum=container[minimum].parent;
1.362 + int s=minimum;
1.363 + int m=minimum;
1.364 + do {
1.365 + if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
1.366 + s=container[s].right_neighbor;
1.367 + } while ( s != m );
1.368 + }
1.369 +
1.370 + void makeroot(int c) {
1.371 + int s=c;
1.372 + do {
1.373 + container[s].parent=-1;
1.374 + s=container[s].right_neighbor;
1.375 + } while ( s != c );
1.376 + }
1.377 +
1.378 + void cut(int a, int b) {
1.379 + /*
1.380 + *Replacing a from the children of b.
1.381 + */
1.382 + --container[b].degree;
1.383 +
1.384 + if ( container[b].degree !=0 ) {
1.385 + int child=container[b].child;
1.386 + if ( child==a )
1.387 + container[b].child=container[child].right_neighbor;
1.388 + unlace(a);
1.389 + }
1.390 +
1.391 +
1.392 + /*Lacing a to the roots.*/
1.393 + int right=container[minimum].right_neighbor;
1.394 + container[minimum].right_neighbor=a;
1.395 + container[a].left_neighbor=minimum;
1.396 + container[a].right_neighbor=right;
1.397 + container[right].left_neighbor=a;
1.398 +
1.399 + container[a].parent=-1;
1.400 + container[a].marked=false;
1.401 + }
1.402 +
1.403 + void cascade(int a) {
1.404 + if ( container[a].parent!=-1 ) {
1.405 + int p=container[a].parent;
1.406 +
1.407 + if ( container[a].marked==false ) container[a].marked=true;
1.408 + else {
1.409 + cut(a,p);
1.410 + cascade(p);
1.411 + }
1.412 + }
1.413 + }
1.414 +
1.415 + void fuse(int a, int b) {
1.416 + unlace(b);
1.417 +
1.418 + /*Lacing b under a.*/
1.419 + container[b].parent=a;
1.420 +
1.421 + if (container[a].degree==0) {
1.422 + container[b].left_neighbor=b;
1.423 + container[b].right_neighbor=b;
1.424 + container[a].child=b;
1.425 + } else {
1.426 + int child=container[a].child;
1.427 + int last_child=container[child].left_neighbor;
1.428 + container[child].left_neighbor=b;
1.429 + container[b].right_neighbor=child;
1.430 + container[last_child].right_neighbor=b;
1.431 + container[b].left_neighbor=last_child;
1.432 + }
1.433 +
1.434 + ++container[a].degree;
1.435 +
1.436 + container[b].marked=false;
1.437 + }
1.438 +
1.439 + /*
1.440 + *It is invoked only if a has siblings.
1.441 + */
1.442 + void unlace(int a) {
1.443 + int leftn=container[a].left_neighbor;
1.444 + int rightn=container[a].right_neighbor;
1.445 + container[leftn].right_neighbor=rightn;
1.446 + container[rightn].left_neighbor=leftn;
1.447 + }
1.448 +
1.449 +
1.450 + class store {
1.451 + friend class FibHeap;
1.452 +
1.453 + Item name;
1.454 + int parent;
1.455 + int left_neighbor;
1.456 + int right_neighbor;
1.457 + int child;
1.458 + int degree;
1.459 + bool marked;
1.460 + bool in;
1.461 + Prio prio;
1.462 +
1.463 + store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
1.464 + };
1.465 + };
1.466 +
1.467 +} //namespace lemon
1.468 +
1.469 +#endif //LEMON_FIB_HEAP_H
1.470 +