lemon/bin_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/bin_heap.h	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,303 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/bin_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_BIN_HEAP_H
    1.21 +#define LEMON_BIN_HEAP_H
    1.22 +
    1.23 +///\ingroup auxdat
    1.24 +///\file
    1.25 +///\brief Binary Heap implementation.
    1.26 +
    1.27 +#include <vector>
    1.28 +#include <utility>
    1.29 +#include <functional>
    1.30 +
    1.31 +namespace lemon {
    1.32 +
    1.33 +  /// \addtogroup auxdat
    1.34 +  /// @{
    1.35 +
    1.36 +  /// A Binary Heap implementation.
    1.37 +  
    1.38 +  ///This class implements the \e binary \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 +  ///\param Item Type of the items to be stored.  
    1.45 +  ///\param Prio Type of the priority of the items.
    1.46 +  ///\param ItemIntMap A read and writable Item int map, used internally
    1.47 +  ///to handle the cross references.
    1.48 +  ///\param Compare A class for the ordering of the priorities. The
    1.49 +  ///default is \c std::less<Prio>.
    1.50 +  ///
    1.51 +  ///\sa FibHeap
    1.52 +  ///\sa Dijkstra
    1.53 +  template <typename Item, typename Prio, typename ItemIntMap,
    1.54 +	    typename Compare = std::less<Prio> >
    1.55 +  class BinHeap {
    1.56 +
    1.57 +  public:
    1.58 +    typedef Item                             ItemType;
    1.59 +    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
    1.60 +    typedef Prio                             PrioType;
    1.61 +    typedef std::pair<ItemType,PrioType>     PairType;
    1.62 +    typedef ItemIntMap                       ItemIntMapType;
    1.63 +    typedef Compare                          PrioCompare;
    1.64 +
    1.65 +    /// \brief Type to represent the items states.
    1.66 +    ///
    1.67 +    /// Each Item element have a state associated to it. It may be "in heap",
    1.68 +    /// "pre heap" or "post heap". The latter two are indifferent from the
    1.69 +    /// heap's point of view, but may be useful to the user.
    1.70 +    ///
    1.71 +    /// The ItemIntMap \e should be initialized in such way that it maps
    1.72 +    /// PRE_HEAP (-1) to any element to be put in the heap...
    1.73 +    enum state_enum {
    1.74 +      IN_HEAP = 0,
    1.75 +      PRE_HEAP = -1,
    1.76 +      POST_HEAP = -2
    1.77 +    };
    1.78 +
    1.79 +  private:
    1.80 +    std::vector<PairType> data;
    1.81 +    Compare comp;
    1.82 +    ItemIntMap &iim;
    1.83 +
    1.84 +  public:
    1.85 +    /// \brief The constructor.
    1.86 +    ///
    1.87 +    /// The constructor.
    1.88 +    /// \param _iim should be given to the constructor, since it is used
    1.89 +    /// internally to handle the cross references. The value of the map
    1.90 +    /// should be PRE_HEAP (-1) for each element.
    1.91 +    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
    1.92 +    
    1.93 +    /// \brief The constructor.
    1.94 +    ///
    1.95 +    /// The constructor.
    1.96 +    /// \param _iim should be given to the constructor, since it is used
    1.97 +    /// internally to handle the cross references. The value of the map
    1.98 +    /// should be PRE_HEAP (-1) for each element.
    1.99 +    ///
   1.100 +    /// \param _comp The comparator function object.
   1.101 +    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
   1.102 +      : iim(_iim), comp(_comp) {}
   1.103 +
   1.104 +
   1.105 +    /// The number of items stored in the heap.
   1.106 +    ///
   1.107 +    /// \brief Returns the number of items stored in the heap.
   1.108 +    int size() const { return data.size(); }
   1.109 +    
   1.110 +    /// \brief Checks if the heap stores no items.
   1.111 +    ///
   1.112 +    /// Returns \c true if and only if the heap stores no items.
   1.113 +    bool empty() const { return data.empty(); }
   1.114 +
   1.115 +  private:
   1.116 +    static int parent(int i) { return (i-1)/2; }
   1.117 +    static int second_child(int i) { return 2*i+2; }
   1.118 +    bool less(const PairType &p1, const PairType &p2) const {
   1.119 +      return comp(p1.second, p2.second);
   1.120 +    }
   1.121 +
   1.122 +    int bubble_up(int hole, PairType p);
   1.123 +    int bubble_down(int hole, PairType p, int length);
   1.124 +
   1.125 +    void move(const PairType &p, int i) {
   1.126 +      data[i] = p;
   1.127 +      iim.set(p.first, i);
   1.128 +    }
   1.129 +
   1.130 +    void rmidx(int h) {
   1.131 +      int n = data.size()-1;
   1.132 +      if( h>=0 && h<=n ) {
   1.133 +	iim.set(data[h].first, POST_HEAP);
   1.134 +	if ( h<n ) {
   1.135 +	  bubble_down(h, data[n], n);
   1.136 +	}
   1.137 +	data.pop_back();
   1.138 +      }
   1.139 +    }
   1.140 +
   1.141 +  public:
   1.142 +    /// \brief Insert a pair of item and priority into the heap.
   1.143 +    ///
   1.144 +    /// Adds \c p.first to the heap with priority \c p.second.
   1.145 +    /// \param p The pair to insert.
   1.146 +    void push(const PairType &p) {
   1.147 +      int n = data.size();
   1.148 +      data.resize(n+1);
   1.149 +      bubble_up(n, p);
   1.150 +    }
   1.151 +
   1.152 +    /// \brief Insert an item into the heap with the given heap.
   1.153 +    ///    
   1.154 +    /// Adds \c i to the heap with priority \c p. 
   1.155 +    /// \param i The item to insert.
   1.156 +    /// \param p The priority of the item.
   1.157 +    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
   1.158 +
   1.159 +    /// \brief Returns the item with minimum priority relative to \c Compare.
   1.160 +    ///
   1.161 +    /// This method returns the item with minimum priority relative to \c
   1.162 +    /// Compare.  
   1.163 +    /// \pre The heap must be nonempty.  
   1.164 +    Item top() const {
   1.165 +      return data[0].first;
   1.166 +    }
   1.167 +
   1.168 +    /// \brief Returns the minimum priority relative to \c Compare.
   1.169 +    ///
   1.170 +    /// It returns the minimum priority relative to \c Compare.
   1.171 +    /// \pre The heap must be nonempty.
   1.172 +    Prio prio() const {
   1.173 +      return data[0].second;
   1.174 +    }
   1.175 +
   1.176 +    /// \brief Deletes the item with minimum priority relative to \c Compare.
   1.177 +    ///
   1.178 +    /// This method deletes the item with minimum priority relative to \c
   1.179 +    /// Compare from the heap.  
   1.180 +    /// \pre The heap must be non-empty.  
   1.181 +    void pop() {
   1.182 +      rmidx(0);
   1.183 +    }
   1.184 +
   1.185 +    /// \brief Deletes \c i from the heap.
   1.186 +    ///
   1.187 +    /// This method deletes item \c i from the heap, if \c i was
   1.188 +    /// already stored in the heap.
   1.189 +    /// \param i The item to erase. 
   1.190 +    void erase(const Item &i) {
   1.191 +      rmidx(iim[i]);
   1.192 +    }
   1.193 +
   1.194 +    
   1.195 +    /// \brief Returns the priority of \c i.
   1.196 +    ///
   1.197 +    /// This function returns the priority of item \c i.  
   1.198 +    /// \pre \c i must be in the heap.
   1.199 +    /// \param i The item.
   1.200 +    Prio operator[](const Item &i) const {
   1.201 +      int idx = iim[i];
   1.202 +      return data[idx].second;
   1.203 +    }
   1.204 +
   1.205 +    /// \brief \c i gets to the heap with priority \c p independently 
   1.206 +    /// if \c i was already there.
   1.207 +    ///
   1.208 +    /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.209 +    /// in the heap and sets the priority of \c i to \c p otherwise.
   1.210 +    /// \param i The item.
   1.211 +    /// \param p The priority.
   1.212 +    void set(const Item &i, const Prio &p) {
   1.213 +      int idx = iim[i];
   1.214 +      if( idx < 0 ) {
   1.215 +	push(i,p);
   1.216 +      }
   1.217 +      else if( comp(p, data[idx].second) ) {
   1.218 +	bubble_up(idx, PairType(i,p));
   1.219 +      }
   1.220 +      else {
   1.221 +	bubble_down(idx, PairType(i,p), data.size());
   1.222 +      }
   1.223 +    }
   1.224 +
   1.225 +    /// \brief Decreases the priority of \c i to \c p.
   1.226 +
   1.227 +    /// This method decreases the priority of item \c i to \c p.
   1.228 +    /// \pre \c i must be stored in the heap with priority at least \c
   1.229 +    /// p relative to \c Compare.
   1.230 +    /// \param i The item.
   1.231 +    /// \param p The priority.
   1.232 +    void decrease(const Item &i, const Prio &p) {
   1.233 +      int idx = iim[i];
   1.234 +      bubble_up(idx, PairType(i,p));
   1.235 +    }
   1.236 +    
   1.237 +    /// \brief Increases the priority of \c i to \c p.
   1.238 +    ///
   1.239 +    /// This method sets the priority of item \c i to \c p. 
   1.240 +    /// \pre \c i must be stored in the heap with priority at most \c
   1.241 +    /// p relative to \c Compare.
   1.242 +    /// \param i The item.
   1.243 +    /// \param p The priority.
   1.244 +    void increase(const Item &i, const Prio &p) {
   1.245 +      int idx = iim[i];
   1.246 +      bubble_down(idx, PairType(i,p), data.size());
   1.247 +    }
   1.248 +
   1.249 +    /// \brief Returns if \c item is in, has already been in, or has 
   1.250 +    /// never been in the heap.
   1.251 +    ///
   1.252 +    /// This method returns PRE_HEAP if \c item has never been in the
   1.253 +    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.254 +    /// otherwise. In the latter case it is possible that \c item will
   1.255 +    /// get back to the heap again.
   1.256 +    /// \param i The item.
   1.257 +    state_enum state(const Item &i) const {
   1.258 +      int s = iim[i];
   1.259 +      if( s>=0 )
   1.260 +	s=0;
   1.261 +      return state_enum(s);
   1.262 +    }
   1.263 +
   1.264 +  }; // class BinHeap
   1.265 +
   1.266 +  
   1.267 +  template <typename K, typename V, typename M, typename C>
   1.268 +  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
   1.269 +    int par = parent(hole);
   1.270 +    while( hole>0 && less(p,data[par]) ) {
   1.271 +      move(data[par],hole);
   1.272 +      hole = par;
   1.273 +      par = parent(hole);
   1.274 +    }
   1.275 +    move(p, hole);
   1.276 +    return hole;
   1.277 +  }
   1.278 +
   1.279 +  template <typename K, typename V, typename M, typename C>
   1.280 +  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
   1.281 +    int child = second_child(hole);
   1.282 +    while(child < length) {
   1.283 +      if( less(data[child-1], data[child]) ) {
   1.284 +	--child;
   1.285 +      }
   1.286 +      if( !less(data[child], p) )
   1.287 +	goto ok;
   1.288 +      move(data[child], hole);
   1.289 +      hole = child;
   1.290 +      child = second_child(hole);
   1.291 +    }
   1.292 +    child--;
   1.293 +    if( child<length && less(data[child], p) ) {
   1.294 +      move(data[child], hole);
   1.295 +      hole=child;
   1.296 +    }
   1.297 +  ok:
   1.298 +    move(p, hole);
   1.299 +    return hole;
   1.300 +  }
   1.301 +
   1.302 +  ///@}
   1.303 +
   1.304 +} // namespace lemon
   1.305 +
   1.306 +#endif // LEMON_BIN_HEAP_H