src/lemon/fib_heap.h
changeset 921 818510fa3d99
parent 911 89a4fbb99cad
child 967 6563019430ba
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/fib_heap.h	Wed Sep 29 15:30:04 2004 +0000
     1.3 @@ -0,0 +1,510 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/fib_heap.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, 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 <math.h>
    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, for the usage of
    1.51 +  ///the heap.
    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 +  ///\author Jacint Szabo 
    1.56 + 
    1.57 +#ifdef DOXYGEN
    1.58 +  template <typename Item, 
    1.59 +	    typename Prio, 
    1.60 +	    typename ItemIntMap, 
    1.61 +	    typename Compare>
    1.62 +#else
    1.63 +  template <typename Item, 
    1.64 +	    typename Prio, 
    1.65 +	    typename ItemIntMap, 
    1.66 +	    typename Compare = std::less<Prio> >
    1.67 +#endif
    1.68 +  class FibHeap {
    1.69 +  public:     
    1.70 +    typedef Prio PrioType;
    1.71 +    
    1.72 +  private:
    1.73 +    class store;
    1.74 +    
    1.75 +    std::vector<store> container;
    1.76 +    int minimum;
    1.77 +    ItemIntMap &iimap;
    1.78 +    Compare comp;
    1.79 +    int num_items;
    1.80 +    
    1.81 +  public:
    1.82 +    enum state_enum {
    1.83 +      IN_HEAP = 0,
    1.84 +      PRE_HEAP = -1,
    1.85 +      POST_HEAP = -2
    1.86 +    };
    1.87 +    
    1.88 +    FibHeap(ItemIntMap &_iimap) : minimum(0), iimap(_iimap), num_items() {} 
    1.89 +    FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(0), 
    1.90 +      iimap(_iimap), comp(_comp), num_items() {}
    1.91 +    
    1.92 +    ///The number of items stored in the heap.
    1.93 +
    1.94 +    /**
    1.95 +       Returns the number of items stored in the heap.
    1.96 +    */
    1.97 +    int size() const { return num_items; }
    1.98 +
    1.99 +    ///Checks if the heap stores no items.
   1.100 +    
   1.101 +    /**
   1.102 +       Returns \c true if and only if the heap stores no items.
   1.103 +    */
   1.104 +    bool empty() const { return num_items==0; }
   1.105 +
   1.106 +    ///\c item gets to the heap with priority \c value independently if \c item was already there.
   1.107 +
   1.108 +    /**
   1.109 +       This method calls \ref push(\c item, \c value) if \c item is not
   1.110 +       stored in the heap and it calls \ref decrease(\c item, \c value) or
   1.111 +       \ref increase(\c item, \c value) otherwise.
   1.112 +    */
   1.113 +    void set (Item const item, PrioType const value); 
   1.114 +    
   1.115 +    ///Adds \c item to the heap with priority \c value. 
   1.116 +    
   1.117 +    /**
   1.118 +       Adds \c item to the heap with priority \c value. 
   1.119 +       \pre \c item must not be stored in the heap. 
   1.120 +    */
   1.121 +    void push (Item const item, PrioType const value);
   1.122 +    
   1.123 +    ///Returns the item with minimum priority relative to \c Compare.
   1.124 +    
   1.125 +    /**
   1.126 +       This method returns the item with minimum priority relative to \c
   1.127 +       Compare.  
   1.128 +       \pre The heap must be nonempty.  
   1.129 +    */
   1.130 +    Item top() const { return container[minimum].name; }
   1.131 +
   1.132 +    ///Returns the minimum priority relative to \c Compare.
   1.133 +
   1.134 +    /**
   1.135 +       It returns the minimum priority relative to \c Compare.
   1.136 +       \pre The heap must be nonempty.
   1.137 +    */
   1.138 +    PrioType prio() const { return container[minimum].prio; }
   1.139 +    
   1.140 +    ///Returns the priority of \c item.
   1.141 +
   1.142 +    /**
   1.143 +       This function returns the priority of \c item.
   1.144 +       \pre \c item must be in the heap.
   1.145 +    */
   1.146 +    PrioType& operator[](const Item& item) { 
   1.147 +      return container[iimap[item]].prio; 
   1.148 +    }
   1.149 +    
   1.150 +    ///Returns the priority of \c item.
   1.151 +    
   1.152 +    /**
   1.153 +       It returns the priority of \c item.
   1.154 +       \pre \c item must be in the heap.
   1.155 +    */
   1.156 +    const PrioType& operator[](const Item& item) const { 
   1.157 +      return container[iimap[item]].prio; 
   1.158 +    }
   1.159 +
   1.160 +
   1.161 +    ///Deletes the item with minimum priority relative to \c Compare.
   1.162 +
   1.163 +    /**
   1.164 +    This method deletes the item with minimum priority relative to \c
   1.165 +    Compare from the heap.  
   1.166 +    \pre The heap must be non-empty.  
   1.167 +    */
   1.168 +    void pop();
   1.169 +
   1.170 +    ///Deletes \c item from the heap.
   1.171 +
   1.172 +    /**
   1.173 +       This method deletes \c item from the heap, if \c item was already
   1.174 +       stored in the heap. It is quite inefficient in Fibonacci heaps.
   1.175 +    */
   1.176 +    void erase (const Item& item); 
   1.177 +
   1.178 +    ///Decreases the priority of \c item to \c value.
   1.179 +
   1.180 +    /**
   1.181 +       This method decreases the priority of \c item to \c value.
   1.182 +       \pre \c item must be stored in the heap with priority at least \c
   1.183 +       value relative to \c Compare.
   1.184 +    */
   1.185 +    void decrease (Item item, PrioType const value); 
   1.186 +
   1.187 +    ///Increases the priority of \c item to \c value.
   1.188 +
   1.189 +    /**
   1.190 +       This method sets the priority of \c item to \c value. Though
   1.191 +       there is no precondition on the priority of \c item, this
   1.192 +       method should be used only if it is indeed necessary to increase
   1.193 +       (relative to \c Compare) the priority of \c item, because this
   1.194 +       method is inefficient.
   1.195 +    */
   1.196 +    void increase (Item item, PrioType const value) {
   1.197 +      erase(item);
   1.198 +      push(item, value);
   1.199 +    }
   1.200 +
   1.201 +
   1.202 +    ///Returns if \c item is in, has already been in, or has never been in the heap.
   1.203 +
   1.204 +    /**
   1.205 +       This method returns PRE_HEAP if \c item has never been in the
   1.206 +       heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.207 +       otherwise. In the latter case it is possible that \c item will
   1.208 +       get back to the heap again.
   1.209 +    */
   1.210 +    state_enum state(const Item &item) const {
   1.211 +      int i=iimap[item];
   1.212 +      if( i>=0 ) {
   1.213 +	if ( container[i].in ) i=0;
   1.214 +	else i=-2; 
   1.215 +      }
   1.216 +      return state_enum(i);
   1.217 +    }    
   1.218 +    
   1.219 +  private:
   1.220 +    
   1.221 +    void balance();
   1.222 +    void makeroot(int c);
   1.223 +    void cut(int a, int b);
   1.224 +    void cascade(int a);
   1.225 +    void fuse(int a, int b);
   1.226 +    void unlace(int a);
   1.227 +
   1.228 +
   1.229 +    class store {
   1.230 +      friend class FibHeap;
   1.231 +      
   1.232 +      Item name;
   1.233 +      int parent;
   1.234 +      int left_neighbor;
   1.235 +      int right_neighbor;
   1.236 +      int child;
   1.237 +      int degree;  
   1.238 +      bool marked;
   1.239 +      bool in;
   1.240 +      PrioType prio;
   1.241 +      
   1.242 +      store() : parent(-1), child(-1), degree(), marked(false), in(true) {} 
   1.243 +    };
   1.244 +  };    
   1.245 + 
   1.246 +
   1.247 +
   1.248 +    // **********************************************************************
   1.249 +    //  IMPLEMENTATIONS
   1.250 +    // **********************************************************************
   1.251 +    
   1.252 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.253 +    typename Compare>
   1.254 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::set 
   1.255 +  (Item const item, PrioType const value) 
   1.256 +  {
   1.257 +    int i=iimap[item];
   1.258 +    if ( i >= 0 && container[i].in ) {
   1.259 +      if ( comp(value, container[i].prio) ) decrease(item, value); 
   1.260 +      if ( comp(container[i].prio, value) ) increase(item, value); 
   1.261 +    } else push(item, value);
   1.262 +  }
   1.263 +    
   1.264 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.265 +    typename Compare>
   1.266 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::push 
   1.267 +  (Item const item, PrioType const value) {
   1.268 +      int i=iimap[item];      
   1.269 +      if ( i < 0 ) {
   1.270 +	int s=container.size();
   1.271 +	iimap.set( item, s );	
   1.272 +	store st;
   1.273 +	st.name=item;
   1.274 +	container.push_back(st);
   1.275 +	i=s;
   1.276 +      } else {
   1.277 +	container[i].parent=container[i].child=-1;
   1.278 +	container[i].degree=0;
   1.279 +	container[i].in=true;
   1.280 +	container[i].marked=false;
   1.281 +      }
   1.282 +
   1.283 +      if ( num_items ) {
   1.284 +	container[container[minimum].right_neighbor].left_neighbor=i;
   1.285 +	container[i].right_neighbor=container[minimum].right_neighbor;
   1.286 +	container[minimum].right_neighbor=i;
   1.287 +	container[i].left_neighbor=minimum;
   1.288 +	if ( comp( value, container[minimum].prio) ) minimum=i; 
   1.289 +      } else {
   1.290 +	container[i].right_neighbor=container[i].left_neighbor=i;
   1.291 +	minimum=i;	
   1.292 +      }
   1.293 +      container[i].prio=value;
   1.294 +      ++num_items;
   1.295 +    }
   1.296 +    
   1.297 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.298 +    typename Compare>
   1.299 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::pop() {
   1.300 +      /*The first case is that there are only one root.*/
   1.301 +      if ( container[minimum].left_neighbor==minimum ) {
   1.302 +	container[minimum].in=false;
   1.303 +	if ( container[minimum].degree!=0 ) { 
   1.304 +	  makeroot(container[minimum].child);
   1.305 +	  minimum=container[minimum].child;
   1.306 +	  balance();
   1.307 +	}
   1.308 +      } else {
   1.309 +	int right=container[minimum].right_neighbor;
   1.310 +	unlace(minimum);
   1.311 +	container[minimum].in=false;
   1.312 +	if ( container[minimum].degree > 0 ) {
   1.313 +	  int left=container[minimum].left_neighbor;
   1.314 +	  int child=container[minimum].child;
   1.315 +	  int last_child=container[child].left_neighbor;
   1.316 +	
   1.317 +	  makeroot(child);
   1.318 +	  
   1.319 +	  container[left].right_neighbor=child;
   1.320 +	  container[child].left_neighbor=left;
   1.321 +	  container[right].left_neighbor=last_child;
   1.322 +	  container[last_child].right_neighbor=right;
   1.323 +	}
   1.324 +	minimum=right;
   1.325 +	balance();
   1.326 +      } // the case where there are more roots
   1.327 +      --num_items;   
   1.328 +    }
   1.329 +
   1.330 +
   1.331 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.332 +    typename Compare>
   1.333 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::erase 
   1.334 +  (const Item& item) {
   1.335 +      int i=iimap[item];
   1.336 +      
   1.337 +      if ( i >= 0 && container[i].in ) { 	
   1.338 +	if ( container[i].parent!=-1 ) {
   1.339 +	  int p=container[i].parent;
   1.340 +	  cut(i,p);	    
   1.341 +	  cascade(p);
   1.342 +	}
   1.343 +	minimum=i;     //As if its prio would be -infinity
   1.344 +	pop();
   1.345 +      }
   1.346 +  }
   1.347 +    
   1.348 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.349 +    typename Compare>
   1.350 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::decrease 
   1.351 +  (Item item, PrioType const value) {
   1.352 +      int i=iimap[item];
   1.353 +      container[i].prio=value;
   1.354 +      int p=container[i].parent;
   1.355 +      
   1.356 +      if ( p!=-1 && comp(value, container[p].prio) ) {
   1.357 +	cut(i,p);	    
   1.358 +	cascade(p);
   1.359 +      }      
   1.360 +      if ( comp(value, container[minimum].prio) ) minimum=i; 
   1.361 +  }
   1.362 + 
   1.363 +
   1.364 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.365 +    typename Compare>
   1.366 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::balance() {      
   1.367 +
   1.368 +    int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
   1.369 +  
   1.370 +    std::vector<int> A(maxdeg,-1); 
   1.371 +    
   1.372 +    /*
   1.373 +     *Recall that now minimum does not point to the minimum prio element.
   1.374 +     *We set minimum to this during balance().
   1.375 +     */
   1.376 +    int anchor=container[minimum].left_neighbor; 
   1.377 +    int next=minimum; 
   1.378 +    bool end=false; 
   1.379 +    	
   1.380 +       do {
   1.381 +	int active=next;
   1.382 +	if ( anchor==active ) end=true;
   1.383 +	int d=container[active].degree;
   1.384 +	next=container[active].right_neighbor;
   1.385 +
   1.386 +	while (A[d]!=-1) {	  
   1.387 +	  if( comp(container[active].prio, container[A[d]].prio) ) {
   1.388 +	    fuse(active,A[d]); 
   1.389 +	  } else { 
   1.390 +	    fuse(A[d],active);
   1.391 +	    active=A[d];
   1.392 +	  } 
   1.393 +	  A[d]=-1;
   1.394 +	  ++d;
   1.395 +	}	
   1.396 +	A[d]=active;
   1.397 +       } while ( !end );
   1.398 +
   1.399 +
   1.400 +       while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
   1.401 +       int s=minimum;
   1.402 +       int m=minimum;
   1.403 +       do {  
   1.404 +	 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
   1.405 +	 s=container[s].right_neighbor;
   1.406 +       } while ( s != m );
   1.407 +    }
   1.408 +
   1.409 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.410 +    typename Compare>
   1.411 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::makeroot 
   1.412 +  (int c) {
   1.413 +      int s=c;
   1.414 +      do {  
   1.415 +	container[s].parent=-1;
   1.416 +	s=container[s].right_neighbor;
   1.417 +      } while ( s != c );
   1.418 +    }
   1.419 +  
   1.420 +  
   1.421 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.422 +    typename Compare>
   1.423 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::cut 
   1.424 +  (int a, int b) {    
   1.425 +    /*
   1.426 +     *Replacing a from the children of b.
   1.427 +     */
   1.428 +    --container[b].degree;
   1.429 +    
   1.430 +    if ( container[b].degree !=0 ) {
   1.431 +      int child=container[b].child;
   1.432 +      if ( child==a ) 
   1.433 +	container[b].child=container[child].right_neighbor;
   1.434 +      unlace(a);
   1.435 +    }
   1.436 +    
   1.437 +    
   1.438 +    /*Lacing a to the roots.*/
   1.439 +    int right=container[minimum].right_neighbor;
   1.440 +    container[minimum].right_neighbor=a;
   1.441 +    container[a].left_neighbor=minimum;
   1.442 +    container[a].right_neighbor=right;
   1.443 +    container[right].left_neighbor=a;
   1.444 +    
   1.445 +    container[a].parent=-1;
   1.446 +    container[a].marked=false;
   1.447 +  }
   1.448 +  
   1.449 +
   1.450 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.451 +    typename Compare>
   1.452 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::cascade 
   1.453 +  (int a) 
   1.454 +    {
   1.455 +      if ( container[a].parent!=-1 ) {
   1.456 +	int p=container[a].parent;
   1.457 +	
   1.458 +	if ( container[a].marked==false ) container[a].marked=true;
   1.459 +	else {
   1.460 +	  cut(a,p);
   1.461 +	  cascade(p);
   1.462 +	}
   1.463 +      }
   1.464 +    }
   1.465 +
   1.466 +
   1.467 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.468 +    typename Compare>
   1.469 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::fuse 
   1.470 +  (int a, int b) {
   1.471 +      unlace(b);
   1.472 +      
   1.473 +      /*Lacing b under a.*/
   1.474 +      container[b].parent=a;
   1.475 +
   1.476 +      if (container[a].degree==0) {
   1.477 +	container[b].left_neighbor=b;
   1.478 +	container[b].right_neighbor=b;
   1.479 +	container[a].child=b;	
   1.480 +      } else {
   1.481 +	int child=container[a].child;
   1.482 +	int last_child=container[child].left_neighbor;
   1.483 +	container[child].left_neighbor=b;
   1.484 +	container[b].right_neighbor=child;
   1.485 +	container[last_child].right_neighbor=b;
   1.486 +	container[b].left_neighbor=last_child;
   1.487 +      }
   1.488 +
   1.489 +      ++container[a].degree;
   1.490 +      
   1.491 +      container[b].marked=false;
   1.492 +    }
   1.493 +
   1.494 +  
   1.495 +  /*
   1.496 +   *It is invoked only if a has siblings.
   1.497 +   */
   1.498 +  template <typename Item, typename Prio, typename ItemIntMap, 
   1.499 +    typename Compare>
   1.500 +  void FibHeap<Item, Prio, ItemIntMap, Compare>::unlace 
   1.501 +  (int a) {      
   1.502 +      int leftn=container[a].left_neighbor;
   1.503 +      int rightn=container[a].right_neighbor;
   1.504 +      container[leftn].right_neighbor=rightn;
   1.505 +      container[rightn].left_neighbor=leftn;
   1.506 +  }
   1.507 +  
   1.508 +  ///@}
   1.509 +
   1.510 +} //namespace lemon
   1.511 +
   1.512 +#endif //LEMON_FIB_HEAP_H
   1.513 +