1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/fib_heap.h Thu Dec 10 17:05:35 2009 +0100
1.3 @@ -0,0 +1,468 @@
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 CMP 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 IM A read and writable Item int map, used internally
1.51 + ///to handle the cross references.
1.52 + ///\param CMP 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, typename IM, typename CMP>
1.59 +#else
1.60 + template <typename PRIO, typename IM, typename CMP = std::less<PRIO> >
1.61 +#endif
1.62 + class FibHeap {
1.63 + public:
1.64 + ///\e
1.65 + typedef IM ItemIntMap;
1.66 + ///\e
1.67 + typedef PRIO Prio;
1.68 + ///\e
1.69 + typedef typename ItemIntMap::Key Item;
1.70 + ///\e
1.71 + typedef std::pair<Item,Prio> Pair;
1.72 + ///\e
1.73 + typedef CMP Compare;
1.74 +
1.75 + private:
1.76 + class Store;
1.77 +
1.78 + std::vector<Store> _data;
1.79 + int _minimum;
1.80 + ItemIntMap &_iim;
1.81 + Compare _comp;
1.82 + int _num;
1.83 +
1.84 + public:
1.85 +
1.86 + /// \brief Type to represent the items states.
1.87 + ///
1.88 + /// Each Item element have a state associated to it. It may be "in heap",
1.89 + /// "pre heap" or "post heap". The latter two are indifferent from the
1.90 + /// heap's point of view, but may be useful to the user.
1.91 + ///
1.92 + /// The item-int map must be initialized in such way that it assigns
1.93 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.94 + enum State {
1.95 + IN_HEAP = 0, ///< = 0.
1.96 + PRE_HEAP = -1, ///< = -1.
1.97 + POST_HEAP = -2 ///< = -2.
1.98 + };
1.99 +
1.100 + /// \brief The constructor
1.101 + ///
1.102 + /// \c map should be given to the constructor, since it is
1.103 + /// used internally to handle the cross references.
1.104 + explicit FibHeap(ItemIntMap &map)
1.105 + : _minimum(0), _iim(map), _num() {}
1.106 +
1.107 + /// \brief The constructor
1.108 + ///
1.109 + /// \c map should be given to the constructor, since it is used
1.110 + /// internally to handle the cross references. \c comp is an
1.111 + /// object for ordering of the priorities.
1.112 + FibHeap(ItemIntMap &map, const Compare &comp)
1.113 + : _minimum(0), _iim(map), _comp(comp), _num() {}
1.114 +
1.115 + /// \brief The number of items stored in the heap.
1.116 + ///
1.117 + /// Returns the number of items stored in the heap.
1.118 + int size() const { return _num; }
1.119 +
1.120 + /// \brief Checks if the heap stores no items.
1.121 + ///
1.122 + /// Returns \c true if and only if the heap stores no items.
1.123 + bool empty() const { return _num==0; }
1.124 +
1.125 + /// \brief Make empty this heap.
1.126 + ///
1.127 + /// Make empty this heap. It does not change the cross reference
1.128 + /// map. If you want to reuse a heap what is not surely empty you
1.129 + /// should first clear the heap and after that you should set the
1.130 + /// cross reference map for each item to \c PRE_HEAP.
1.131 + void clear() {
1.132 + _data.clear(); _minimum = 0; _num = 0;
1.133 + }
1.134 +
1.135 + /// \brief \c item gets to the heap with priority \c value independently
1.136 + /// if \c item was already there.
1.137 + ///
1.138 + /// This method calls \ref push(\c item, \c value) if \c item is not
1.139 + /// stored in the heap and it calls \ref decrease(\c item, \c value) or
1.140 + /// \ref increase(\c item, \c value) otherwise.
1.141 + void set (const Item& item, const Prio& value) {
1.142 + int i=_iim[item];
1.143 + if ( i >= 0 && _data[i].in ) {
1.144 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
1.145 + if ( _comp(_data[i].prio, value) ) increase(item, value);
1.146 + } else push(item, value);
1.147 + }
1.148 +
1.149 + /// \brief Adds \c item to the heap with priority \c value.
1.150 + ///
1.151 + /// Adds \c item to the heap with priority \c value.
1.152 + /// \pre \c item must not be stored in the heap.
1.153 + void push (const Item& item, const Prio& value) {
1.154 + int i=_iim[item];
1.155 + if ( i < 0 ) {
1.156 + int s=_data.size();
1.157 + _iim.set( item, s );
1.158 + Store st;
1.159 + st.name=item;
1.160 + _data.push_back(st);
1.161 + i=s;
1.162 + } else {
1.163 + _data[i].parent=_data[i].child=-1;
1.164 + _data[i].degree=0;
1.165 + _data[i].in=true;
1.166 + _data[i].marked=false;
1.167 + }
1.168 +
1.169 + if ( _num ) {
1.170 + _data[_data[_minimum].right_neighbor].left_neighbor=i;
1.171 + _data[i].right_neighbor=_data[_minimum].right_neighbor;
1.172 + _data[_minimum].right_neighbor=i;
1.173 + _data[i].left_neighbor=_minimum;
1.174 + if ( _comp( value, _data[_minimum].prio) ) _minimum=i;
1.175 + } else {
1.176 + _data[i].right_neighbor=_data[i].left_neighbor=i;
1.177 + _minimum=i;
1.178 + }
1.179 + _data[i].prio=value;
1.180 + ++_num;
1.181 + }
1.182 +
1.183 + /// \brief Returns the item with minimum priority relative to \c Compare.
1.184 + ///
1.185 + /// This method returns the item with minimum priority relative to \c
1.186 + /// Compare.
1.187 + /// \pre The heap must be nonempty.
1.188 + Item top() const { return _data[_minimum].name; }
1.189 +
1.190 + /// \brief Returns the minimum priority relative to \c Compare.
1.191 + ///
1.192 + /// It returns the minimum priority relative to \c Compare.
1.193 + /// \pre The heap must be nonempty.
1.194 + const Prio& prio() const { return _data[_minimum].prio; }
1.195 +
1.196 + /// \brief Returns the priority of \c item.
1.197 + ///
1.198 + /// It returns the priority of \c item.
1.199 + /// \pre \c item must be in the heap.
1.200 + const Prio& operator[](const Item& item) const {
1.201 + return _data[_iim[item]].prio;
1.202 + }
1.203 +
1.204 + /// \brief Deletes the item with minimum priority relative to \c Compare.
1.205 + ///
1.206 + /// This method deletes the item with minimum priority relative to \c
1.207 + /// Compare from the heap.
1.208 + /// \pre The heap must be non-empty.
1.209 + void pop() {
1.210 + /*The first case is that there are only one root.*/
1.211 + if ( _data[_minimum].left_neighbor==_minimum ) {
1.212 + _data[_minimum].in=false;
1.213 + if ( _data[_minimum].degree!=0 ) {
1.214 + makeroot(_data[_minimum].child);
1.215 + _minimum=_data[_minimum].child;
1.216 + balance();
1.217 + }
1.218 + } else {
1.219 + int right=_data[_minimum].right_neighbor;
1.220 + unlace(_minimum);
1.221 + _data[_minimum].in=false;
1.222 + if ( _data[_minimum].degree > 0 ) {
1.223 + int left=_data[_minimum].left_neighbor;
1.224 + int child=_data[_minimum].child;
1.225 + int last_child=_data[child].left_neighbor;
1.226 +
1.227 + makeroot(child);
1.228 +
1.229 + _data[left].right_neighbor=child;
1.230 + _data[child].left_neighbor=left;
1.231 + _data[right].left_neighbor=last_child;
1.232 + _data[last_child].right_neighbor=right;
1.233 + }
1.234 + _minimum=right;
1.235 + balance();
1.236 + } // the case where there are more roots
1.237 + --_num;
1.238 + }
1.239 +
1.240 + /// \brief Deletes \c item from the heap.
1.241 + ///
1.242 + /// This method deletes \c item from the heap, if \c item was already
1.243 + /// stored in the heap. It is quite inefficient in Fibonacci heaps.
1.244 + void erase (const Item& item) {
1.245 + int i=_iim[item];
1.246 +
1.247 + if ( i >= 0 && _data[i].in ) {
1.248 + if ( _data[i].parent!=-1 ) {
1.249 + int p=_data[i].parent;
1.250 + cut(i,p);
1.251 + cascade(p);
1.252 + }
1.253 + _minimum=i; //As if its prio would be -infinity
1.254 + pop();
1.255 + }
1.256 + }
1.257 +
1.258 + /// \brief Decreases the priority of \c item to \c value.
1.259 + ///
1.260 + /// This method decreases the priority of \c item to \c value.
1.261 + /// \pre \c item must be stored in the heap with priority at least \c
1.262 + /// value relative to \c Compare.
1.263 + void decrease (Item item, const Prio& value) {
1.264 + int i=_iim[item];
1.265 + _data[i].prio=value;
1.266 + int p=_data[i].parent;
1.267 +
1.268 + if ( p!=-1 && _comp(value, _data[p].prio) ) {
1.269 + cut(i,p);
1.270 + cascade(p);
1.271 + }
1.272 + if ( _comp(value, _data[_minimum].prio) ) _minimum=i;
1.273 + }
1.274 +
1.275 + /// \brief Increases the priority of \c item to \c value.
1.276 + ///
1.277 + /// This method sets the priority of \c item to \c value. Though
1.278 + /// there is no precondition on the priority of \c item, this
1.279 + /// method should be used only if it is indeed necessary to increase
1.280 + /// (relative to \c Compare) the priority of \c item, because this
1.281 + /// method is inefficient.
1.282 + void increase (Item item, const Prio& value) {
1.283 + erase(item);
1.284 + push(item, value);
1.285 + }
1.286 +
1.287 +
1.288 + /// \brief Returns if \c item is in, has already been in, or has never
1.289 + /// been in the heap.
1.290 + ///
1.291 + /// This method returns PRE_HEAP if \c item has never been in the
1.292 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.293 + /// otherwise. In the latter case it is possible that \c item will
1.294 + /// get back to the heap again.
1.295 + State state(const Item &item) const {
1.296 + int i=_iim[item];
1.297 + if( i>=0 ) {
1.298 + if ( _data[i].in ) i=0;
1.299 + else i=-2;
1.300 + }
1.301 + return State(i);
1.302 + }
1.303 +
1.304 + /// \brief Sets the state of the \c item in the heap.
1.305 + ///
1.306 + /// Sets the state of the \c item in the heap. It can be used to
1.307 + /// manually clear the heap when it is important to achive the
1.308 + /// better time _complexity.
1.309 + /// \param i The item.
1.310 + /// \param st The state. It should not be \c IN_HEAP.
1.311 + void state(const Item& i, State st) {
1.312 + switch (st) {
1.313 + case POST_HEAP:
1.314 + case PRE_HEAP:
1.315 + if (state(i) == IN_HEAP) {
1.316 + erase(i);
1.317 + }
1.318 + _iim[i] = st;
1.319 + break;
1.320 + case IN_HEAP:
1.321 + break;
1.322 + }
1.323 + }
1.324 +
1.325 + private:
1.326 +
1.327 + void balance() {
1.328 +
1.329 + int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1;
1.330 +
1.331 + std::vector<int> A(maxdeg,-1);
1.332 +
1.333 + /*
1.334 + *Recall that now minimum does not point to the minimum prio element.
1.335 + *We set minimum to this during balance().
1.336 + */
1.337 + int anchor=_data[_minimum].left_neighbor;
1.338 + int next=_minimum;
1.339 + bool end=false;
1.340 +
1.341 + do {
1.342 + int active=next;
1.343 + if ( anchor==active ) end=true;
1.344 + int d=_data[active].degree;
1.345 + next=_data[active].right_neighbor;
1.346 +
1.347 + while (A[d]!=-1) {
1.348 + if( _comp(_data[active].prio, _data[A[d]].prio) ) {
1.349 + fuse(active,A[d]);
1.350 + } else {
1.351 + fuse(A[d],active);
1.352 + active=A[d];
1.353 + }
1.354 + A[d]=-1;
1.355 + ++d;
1.356 + }
1.357 + A[d]=active;
1.358 + } while ( !end );
1.359 +
1.360 +
1.361 + while ( _data[_minimum].parent >=0 )
1.362 + _minimum=_data[_minimum].parent;
1.363 + int s=_minimum;
1.364 + int m=_minimum;
1.365 + do {
1.366 + if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s;
1.367 + s=_data[s].right_neighbor;
1.368 + } while ( s != m );
1.369 + }
1.370 +
1.371 + void makeroot(int c) {
1.372 + int s=c;
1.373 + do {
1.374 + _data[s].parent=-1;
1.375 + s=_data[s].right_neighbor;
1.376 + } while ( s != c );
1.377 + }
1.378 +
1.379 + void cut(int a, int b) {
1.380 + /*
1.381 + *Replacing a from the children of b.
1.382 + */
1.383 + --_data[b].degree;
1.384 +
1.385 + if ( _data[b].degree !=0 ) {
1.386 + int child=_data[b].child;
1.387 + if ( child==a )
1.388 + _data[b].child=_data[child].right_neighbor;
1.389 + unlace(a);
1.390 + }
1.391 +
1.392 +
1.393 + /*Lacing a to the roots.*/
1.394 + int right=_data[_minimum].right_neighbor;
1.395 + _data[_minimum].right_neighbor=a;
1.396 + _data[a].left_neighbor=_minimum;
1.397 + _data[a].right_neighbor=right;
1.398 + _data[right].left_neighbor=a;
1.399 +
1.400 + _data[a].parent=-1;
1.401 + _data[a].marked=false;
1.402 + }
1.403 +
1.404 + void cascade(int a) {
1.405 + if ( _data[a].parent!=-1 ) {
1.406 + int p=_data[a].parent;
1.407 +
1.408 + if ( _data[a].marked==false ) _data[a].marked=true;
1.409 + else {
1.410 + cut(a,p);
1.411 + cascade(p);
1.412 + }
1.413 + }
1.414 + }
1.415 +
1.416 + void fuse(int a, int b) {
1.417 + unlace(b);
1.418 +
1.419 + /*Lacing b under a.*/
1.420 + _data[b].parent=a;
1.421 +
1.422 + if (_data[a].degree==0) {
1.423 + _data[b].left_neighbor=b;
1.424 + _data[b].right_neighbor=b;
1.425 + _data[a].child=b;
1.426 + } else {
1.427 + int child=_data[a].child;
1.428 + int last_child=_data[child].left_neighbor;
1.429 + _data[child].left_neighbor=b;
1.430 + _data[b].right_neighbor=child;
1.431 + _data[last_child].right_neighbor=b;
1.432 + _data[b].left_neighbor=last_child;
1.433 + }
1.434 +
1.435 + ++_data[a].degree;
1.436 +
1.437 + _data[b].marked=false;
1.438 + }
1.439 +
1.440 + /*
1.441 + *It is invoked only if a has siblings.
1.442 + */
1.443 + void unlace(int a) {
1.444 + int leftn=_data[a].left_neighbor;
1.445 + int rightn=_data[a].right_neighbor;
1.446 + _data[leftn].right_neighbor=rightn;
1.447 + _data[rightn].left_neighbor=leftn;
1.448 + }
1.449 +
1.450 +
1.451 + class Store {
1.452 + friend class FibHeap;
1.453 +
1.454 + Item name;
1.455 + int parent;
1.456 + int left_neighbor;
1.457 + int right_neighbor;
1.458 + int child;
1.459 + int degree;
1.460 + bool marked;
1.461 + bool in;
1.462 + Prio prio;
1.463 +
1.464 + Store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
1.465 + };
1.466 + };
1.467 +
1.468 +} //namespace lemon
1.469 +
1.470 +#endif //LEMON_FIB_HEAP_H
1.471 +