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