lemon/bin_heap.h
changeset 784 1a7fe3bef514
parent 710 f1fe0ddad6f7
child 1092 dceba191c00d
     1.1 --- a/lemon/bin_heap.h	Fri Oct 16 10:21:37 2009 +0200
     1.2 +++ b/lemon/bin_heap.h	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2008
     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 @@ -19,9 +19,9 @@
    1.13  #ifndef LEMON_BIN_HEAP_H
    1.14  #define LEMON_BIN_HEAP_H
    1.15  
    1.16 -///\ingroup auxdat
    1.17 +///\ingroup heaps
    1.18  ///\file
    1.19 -///\brief Binary Heap implementation.
    1.20 +///\brief Binary heap implementation.
    1.21  
    1.22  #include <vector>
    1.23  #include <utility>
    1.24 @@ -29,112 +29,110 @@
    1.25  
    1.26  namespace lemon {
    1.27  
    1.28 -  ///\ingroup auxdat
    1.29 +  /// \ingroup heaps
    1.30    ///
    1.31 -  ///\brief A Binary Heap implementation.
    1.32 +  /// \brief Binary heap data structure.
    1.33    ///
    1.34 -  ///This class implements the \e binary \e heap data structure. A \e heap
    1.35 -  ///is a data structure for storing items with specified values called \e
    1.36 -  ///priorities in such a way that finding the item with minimum priority is
    1.37 -  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
    1.38 -  ///one can change the priority of an item, add or erase an item, etc.
    1.39 +  /// This class implements the \e binary \e heap data structure.
    1.40 +  /// It fully conforms to the \ref concepts::Heap "heap concept".
    1.41    ///
    1.42 -  ///\tparam _Prio Type of the priority of the items.
    1.43 -  ///\tparam _ItemIntMap A read and writable Item int map, used internally
    1.44 -  ///to handle the cross references.
    1.45 -  ///\tparam _Compare A class for the ordering of the priorities. The
    1.46 -  ///default is \c std::less<_Prio>.
    1.47 -  ///
    1.48 -  ///\sa FibHeap
    1.49 -  ///\sa Dijkstra
    1.50 -  template <typename _Prio, typename _ItemIntMap,
    1.51 -            typename _Compare = std::less<_Prio> >
    1.52 +  /// \tparam PR Type of the priorities of the items.
    1.53 +  /// \tparam IM A read-writable item map with \c int values, used
    1.54 +  /// internally to handle the cross references.
    1.55 +  /// \tparam CMP A functor class for comparing the priorities.
    1.56 +  /// The default is \c std::less<PR>.
    1.57 +#ifdef DOXYGEN
    1.58 +  template <typename PR, typename IM, typename CMP>
    1.59 +#else
    1.60 +  template <typename PR, typename IM, typename CMP = std::less<PR> >
    1.61 +#endif
    1.62    class BinHeap {
    1.63 +  public:
    1.64  
    1.65 -  public:
    1.66 -    ///\e
    1.67 -    typedef _ItemIntMap ItemIntMap;
    1.68 -    ///\e
    1.69 -    typedef _Prio Prio;
    1.70 -    ///\e
    1.71 +    /// Type of the item-int map.
    1.72 +    typedef IM ItemIntMap;
    1.73 +    /// Type of the priorities.
    1.74 +    typedef PR Prio;
    1.75 +    /// Type of the items stored in the heap.
    1.76      typedef typename ItemIntMap::Key Item;
    1.77 -    ///\e
    1.78 +    /// Type of the item-priority pairs.
    1.79      typedef std::pair<Item,Prio> Pair;
    1.80 -    ///\e
    1.81 -    typedef _Compare Compare;
    1.82 +    /// Functor type for comparing the priorities.
    1.83 +    typedef CMP Compare;
    1.84  
    1.85 -    /// \brief Type to represent the items states.
    1.86 +    /// \brief Type to represent the states of the items.
    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 +    /// Each item has a state associated to it. It can be "in heap",
    1.91 +    /// "pre-heap" or "post-heap". The latter two are indifferent from the
    1.92      /// heap's point of view, but may be useful to the user.
    1.93      ///
    1.94 -    /// The ItemIntMap \e should be initialized in such way that it maps
    1.95 -    /// PRE_HEAP (-1) to any element to be put in the heap...
    1.96 +    /// The item-int map must be initialized in such way that it assigns
    1.97 +    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
    1.98      enum State {
    1.99 -      IN_HEAP = 0,
   1.100 -      PRE_HEAP = -1,
   1.101 -      POST_HEAP = -2
   1.102 +      IN_HEAP = 0,    ///< = 0.
   1.103 +      PRE_HEAP = -1,  ///< = -1.
   1.104 +      POST_HEAP = -2  ///< = -2.
   1.105      };
   1.106  
   1.107    private:
   1.108 -    std::vector<Pair> data;
   1.109 -    Compare comp;
   1.110 -    ItemIntMap &iim;
   1.111 +    std::vector<Pair> _data;
   1.112 +    Compare _comp;
   1.113 +    ItemIntMap &_iim;
   1.114  
   1.115    public:
   1.116 -    /// \brief The constructor.
   1.117 +
   1.118 +    /// \brief Constructor.
   1.119      ///
   1.120 -    /// The constructor.
   1.121 -    /// \param _iim should be given to the constructor, since it is used
   1.122 -    /// internally to handle the cross references. The value of the map
   1.123 -    /// should be PRE_HEAP (-1) for each element.
   1.124 -    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
   1.125 +    /// Constructor.
   1.126 +    /// \param map A map that assigns \c int values to the items.
   1.127 +    /// It is used internally to handle the cross references.
   1.128 +    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
   1.129 +    explicit BinHeap(ItemIntMap &map) : _iim(map) {}
   1.130  
   1.131 -    /// \brief The constructor.
   1.132 +    /// \brief Constructor.
   1.133      ///
   1.134 -    /// The constructor.
   1.135 -    /// \param _iim should be given to the constructor, since it is used
   1.136 -    /// internally to handle the cross references. The value of the map
   1.137 -    /// should be PRE_HEAP (-1) for each element.
   1.138 +    /// Constructor.
   1.139 +    /// \param map A map that assigns \c int values to the items.
   1.140 +    /// It is used internally to handle the cross references.
   1.141 +    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
   1.142 +    /// \param comp The function object used for comparing the priorities.
   1.143 +    BinHeap(ItemIntMap &map, const Compare &comp)
   1.144 +      : _iim(map), _comp(comp) {}
   1.145 +
   1.146 +
   1.147 +    /// \brief The number of items stored in the heap.
   1.148      ///
   1.149 -    /// \param _comp The comparator function object.
   1.150 -    BinHeap(ItemIntMap &_iim, const Compare &_comp)
   1.151 -      : iim(_iim), comp(_comp) {}
   1.152 +    /// This function returns the number of items stored in the heap.
   1.153 +    int size() const { return _data.size(); }
   1.154  
   1.155 +    /// \brief Check if the heap is empty.
   1.156 +    ///
   1.157 +    /// This function returns \c true if the heap is empty.
   1.158 +    bool empty() const { return _data.empty(); }
   1.159  
   1.160 -    /// The number of items stored in the heap.
   1.161 +    /// \brief Make the heap empty.
   1.162      ///
   1.163 -    /// \brief Returns the number of items stored in the heap.
   1.164 -    int size() const { return data.size(); }
   1.165 -
   1.166 -    /// \brief Checks if the heap stores no items.
   1.167 -    ///
   1.168 -    /// Returns \c true if and only if the heap stores no items.
   1.169 -    bool empty() const { return data.empty(); }
   1.170 -
   1.171 -    /// \brief Make empty this heap.
   1.172 -    ///
   1.173 -    /// Make empty this heap. It does not change the cross reference map.
   1.174 -    /// If you want to reuse what is not surely empty you should first clear
   1.175 -    /// the heap and after that you should set the cross reference map for
   1.176 -    /// each item to \c PRE_HEAP.
   1.177 +    /// This functon makes the heap empty.
   1.178 +    /// It does not change the cross reference map. If you want to reuse
   1.179 +    /// a heap that is not surely empty, you should first clear it and
   1.180 +    /// then you should set the cross reference map to \c PRE_HEAP
   1.181 +    /// for each item.
   1.182      void clear() {
   1.183 -      data.clear();
   1.184 +      _data.clear();
   1.185      }
   1.186  
   1.187    private:
   1.188      static int parent(int i) { return (i-1)/2; }
   1.189  
   1.190 -    static int second_child(int i) { return 2*i+2; }
   1.191 +    static int secondChild(int i) { return 2*i+2; }
   1.192      bool less(const Pair &p1, const Pair &p2) const {
   1.193 -      return comp(p1.second, p2.second);
   1.194 +      return _comp(p1.second, p2.second);
   1.195      }
   1.196  
   1.197 -    int bubble_up(int hole, Pair p) {
   1.198 +    int bubbleUp(int hole, Pair p) {
   1.199        int par = parent(hole);
   1.200 -      while( hole>0 && less(p,data[par]) ) {
   1.201 -        move(data[par],hole);
   1.202 +      while( hole>0 && less(p,_data[par]) ) {
   1.203 +        move(_data[par],hole);
   1.204          hole = par;
   1.205          par = parent(hole);
   1.206        }
   1.207 @@ -142,21 +140,21 @@
   1.208        return hole;
   1.209      }
   1.210  
   1.211 -    int bubble_down(int hole, Pair p, int length) {
   1.212 -      int child = second_child(hole);
   1.213 +    int bubbleDown(int hole, Pair p, int length) {
   1.214 +      int child = secondChild(hole);
   1.215        while(child < length) {
   1.216 -        if( less(data[child-1], data[child]) ) {
   1.217 +        if( less(_data[child-1], _data[child]) ) {
   1.218            --child;
   1.219          }
   1.220 -        if( !less(data[child], p) )
   1.221 +        if( !less(_data[child], p) )
   1.222            goto ok;
   1.223 -        move(data[child], hole);
   1.224 +        move(_data[child], hole);
   1.225          hole = child;
   1.226 -        child = second_child(hole);
   1.227 +        child = secondChild(hole);
   1.228        }
   1.229        child--;
   1.230 -      if( child<length && less(data[child], p) ) {
   1.231 -        move(data[child], hole);
   1.232 +      if( child<length && less(_data[child], p) ) {
   1.233 +        move(_data[child], hole);
   1.234          hole=child;
   1.235        }
   1.236      ok:
   1.237 @@ -165,151 +163,153 @@
   1.238      }
   1.239  
   1.240      void move(const Pair &p, int i) {
   1.241 -      data[i] = p;
   1.242 -      iim.set(p.first, i);
   1.243 +      _data[i] = p;
   1.244 +      _iim.set(p.first, i);
   1.245      }
   1.246  
   1.247    public:
   1.248 +
   1.249      /// \brief Insert a pair of item and priority into the heap.
   1.250      ///
   1.251 -    /// Adds \c p.first to the heap with priority \c p.second.
   1.252 +    /// This function inserts \c p.first to the heap with priority
   1.253 +    /// \c p.second.
   1.254      /// \param p The pair to insert.
   1.255 +    /// \pre \c p.first must not be stored in the heap.
   1.256      void push(const Pair &p) {
   1.257 -      int n = data.size();
   1.258 -      data.resize(n+1);
   1.259 -      bubble_up(n, p);
   1.260 +      int n = _data.size();
   1.261 +      _data.resize(n+1);
   1.262 +      bubbleUp(n, p);
   1.263      }
   1.264  
   1.265 -    /// \brief Insert an item into the heap with the given heap.
   1.266 +    /// \brief Insert an item into the heap with the given priority.
   1.267      ///
   1.268 -    /// Adds \c i to the heap with priority \c p.
   1.269 +    /// This function inserts the given item into the heap with the
   1.270 +    /// given priority.
   1.271      /// \param i The item to insert.
   1.272      /// \param p The priority of the item.
   1.273 +    /// \pre \e i must not be stored in the heap.
   1.274      void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
   1.275  
   1.276 -    /// \brief Returns the item with minimum priority relative to \c Compare.
   1.277 +    /// \brief Return the item having minimum priority.
   1.278      ///
   1.279 -    /// This method returns the item with minimum priority relative to \c
   1.280 -    /// Compare.
   1.281 -    /// \pre The heap must be nonempty.
   1.282 +    /// This function returns the item having minimum priority.
   1.283 +    /// \pre The heap must be non-empty.
   1.284      Item top() const {
   1.285 -      return data[0].first;
   1.286 +      return _data[0].first;
   1.287      }
   1.288  
   1.289 -    /// \brief Returns the minimum priority relative to \c Compare.
   1.290 +    /// \brief The minimum priority.
   1.291      ///
   1.292 -    /// It returns the minimum priority relative to \c Compare.
   1.293 -    /// \pre The heap must be nonempty.
   1.294 +    /// This function returns the minimum priority.
   1.295 +    /// \pre The heap must be non-empty.
   1.296      Prio prio() const {
   1.297 -      return data[0].second;
   1.298 +      return _data[0].second;
   1.299      }
   1.300  
   1.301 -    /// \brief Deletes the item with minimum priority relative to \c Compare.
   1.302 +    /// \brief Remove the item having minimum priority.
   1.303      ///
   1.304 -    /// This method deletes the item with minimum priority relative to \c
   1.305 -    /// Compare from the heap.
   1.306 +    /// This function removes the item having minimum priority.
   1.307      /// \pre The heap must be non-empty.
   1.308      void pop() {
   1.309 -      int n = data.size()-1;
   1.310 -      iim.set(data[0].first, POST_HEAP);
   1.311 +      int n = _data.size()-1;
   1.312 +      _iim.set(_data[0].first, POST_HEAP);
   1.313        if (n > 0) {
   1.314 -        bubble_down(0, data[n], n);
   1.315 +        bubbleDown(0, _data[n], n);
   1.316        }
   1.317 -      data.pop_back();
   1.318 +      _data.pop_back();
   1.319      }
   1.320  
   1.321 -    /// \brief Deletes \c i from the heap.
   1.322 +    /// \brief Remove the given item from the heap.
   1.323      ///
   1.324 -    /// This method deletes item \c i from the heap.
   1.325 -    /// \param i The item to erase.
   1.326 -    /// \pre The item should be in the heap.
   1.327 +    /// This function removes the given item from the heap if it is
   1.328 +    /// already stored.
   1.329 +    /// \param i The item to delete.
   1.330 +    /// \pre \e i must be in the heap.
   1.331      void erase(const Item &i) {
   1.332 -      int h = iim[i];
   1.333 -      int n = data.size()-1;
   1.334 -      iim.set(data[h].first, POST_HEAP);
   1.335 +      int h = _iim[i];
   1.336 +      int n = _data.size()-1;
   1.337 +      _iim.set(_data[h].first, POST_HEAP);
   1.338        if( h < n ) {
   1.339 -        if ( bubble_up(h, data[n]) == h) {
   1.340 -          bubble_down(h, data[n], n);
   1.341 +        if ( bubbleUp(h, _data[n]) == h) {
   1.342 +          bubbleDown(h, _data[n], n);
   1.343          }
   1.344        }
   1.345 -      data.pop_back();
   1.346 +      _data.pop_back();
   1.347      }
   1.348  
   1.349 -
   1.350 -    /// \brief Returns the priority of \c i.
   1.351 +    /// \brief The priority of the given item.
   1.352      ///
   1.353 -    /// This function returns the priority of item \c i.
   1.354 -    /// \pre \c i must be in the heap.
   1.355 +    /// This function returns the priority of the given item.
   1.356      /// \param i The item.
   1.357 +    /// \pre \e i must be in the heap.
   1.358      Prio operator[](const Item &i) const {
   1.359 -      int idx = iim[i];
   1.360 -      return data[idx].second;
   1.361 +      int idx = _iim[i];
   1.362 +      return _data[idx].second;
   1.363      }
   1.364  
   1.365 -    /// \brief \c i gets to the heap with priority \c p independently
   1.366 -    /// if \c i was already there.
   1.367 +    /// \brief Set the priority of an item or insert it, if it is
   1.368 +    /// not stored in the heap.
   1.369      ///
   1.370 -    /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.371 -    /// in the heap and sets the priority of \c i to \c p otherwise.
   1.372 +    /// This method sets the priority of the given item if it is
   1.373 +    /// already stored in the heap. Otherwise it inserts the given
   1.374 +    /// item into the heap with the given priority.
   1.375      /// \param i The item.
   1.376      /// \param p The priority.
   1.377      void set(const Item &i, const Prio &p) {
   1.378 -      int idx = iim[i];
   1.379 +      int idx = _iim[i];
   1.380        if( idx < 0 ) {
   1.381          push(i,p);
   1.382        }
   1.383 -      else if( comp(p, data[idx].second) ) {
   1.384 -        bubble_up(idx, Pair(i,p));
   1.385 +      else if( _comp(p, _data[idx].second) ) {
   1.386 +        bubbleUp(idx, Pair(i,p));
   1.387        }
   1.388        else {
   1.389 -        bubble_down(idx, Pair(i,p), data.size());
   1.390 +        bubbleDown(idx, Pair(i,p), _data.size());
   1.391        }
   1.392      }
   1.393  
   1.394 -    /// \brief Decreases the priority of \c i to \c p.
   1.395 +    /// \brief Decrease the priority of an item to the given value.
   1.396      ///
   1.397 -    /// This method decreases the priority of item \c i to \c p.
   1.398 -    /// \pre \c i must be stored in the heap with priority at least \c
   1.399 -    /// p relative to \c Compare.
   1.400 +    /// This function decreases the priority of an item to the given value.
   1.401      /// \param i The item.
   1.402      /// \param p The priority.
   1.403 +    /// \pre \e i must be stored in the heap with priority at least \e p.
   1.404      void decrease(const Item &i, const Prio &p) {
   1.405 -      int idx = iim[i];
   1.406 -      bubble_up(idx, Pair(i,p));
   1.407 +      int idx = _iim[i];
   1.408 +      bubbleUp(idx, Pair(i,p));
   1.409      }
   1.410  
   1.411 -    /// \brief Increases the priority of \c i to \c p.
   1.412 +    /// \brief Increase the priority of an item to the given value.
   1.413      ///
   1.414 -    /// This method sets the priority of item \c i to \c p.
   1.415 -    /// \pre \c i must be stored in the heap with priority at most \c
   1.416 -    /// p relative to \c Compare.
   1.417 +    /// This function increases the priority of an item to the given value.
   1.418      /// \param i The item.
   1.419      /// \param p The priority.
   1.420 +    /// \pre \e i must be stored in the heap with priority at most \e p.
   1.421      void increase(const Item &i, const Prio &p) {
   1.422 -      int idx = iim[i];
   1.423 -      bubble_down(idx, Pair(i,p), data.size());
   1.424 +      int idx = _iim[i];
   1.425 +      bubbleDown(idx, Pair(i,p), _data.size());
   1.426      }
   1.427  
   1.428 -    /// \brief Returns if \c item is in, has already been in, or has
   1.429 -    /// never been in the heap.
   1.430 +    /// \brief Return the state of an item.
   1.431      ///
   1.432 -    /// This method returns PRE_HEAP if \c item has never been in the
   1.433 -    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.434 -    /// otherwise. In the latter case it is possible that \c item will
   1.435 -    /// get back to the heap again.
   1.436 +    /// This method returns \c PRE_HEAP if the given item has never
   1.437 +    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
   1.438 +    /// and \c POST_HEAP otherwise.
   1.439 +    /// In the latter case it is possible that the item will get back
   1.440 +    /// to the heap again.
   1.441      /// \param i The item.
   1.442      State state(const Item &i) const {
   1.443 -      int s = iim[i];
   1.444 +      int s = _iim[i];
   1.445        if( s>=0 )
   1.446          s=0;
   1.447        return State(s);
   1.448      }
   1.449  
   1.450 -    /// \brief Sets the state of the \c item in the heap.
   1.451 +    /// \brief Set the state of an item in the heap.
   1.452      ///
   1.453 -    /// Sets the state of the \c item in the heap. It can be used to
   1.454 -    /// manually clear the heap when it is important to achive the
   1.455 -    /// better time complexity.
   1.456 +    /// This function sets the state of the given item in the heap.
   1.457 +    /// It can be used to manually clear the heap when it is important
   1.458 +    /// to achive better time complexity.
   1.459      /// \param i The item.
   1.460      /// \param st The state. It should not be \c IN_HEAP.
   1.461      void state(const Item& i, State st) {
   1.462 @@ -319,24 +319,25 @@
   1.463          if (state(i) == IN_HEAP) {
   1.464            erase(i);
   1.465          }
   1.466 -        iim[i] = st;
   1.467 +        _iim[i] = st;
   1.468          break;
   1.469        case IN_HEAP:
   1.470          break;
   1.471        }
   1.472      }
   1.473  
   1.474 -    /// \brief Replaces an item in the heap.
   1.475 +    /// \brief Replace an item in the heap.
   1.476      ///
   1.477 -    /// The \c i item is replaced with \c j item. The \c i item should
   1.478 -    /// be in the heap, while the \c j should be out of the heap. The
   1.479 -    /// \c i item will out of the heap and \c j will be in the heap
   1.480 -    /// with the same prioriority as prevoiusly the \c i item.
   1.481 +    /// This function replaces item \c i with item \c j.
   1.482 +    /// Item \c i must be in the heap, while \c j must be out of the heap.
   1.483 +    /// After calling this method, item \c i will be out of the
   1.484 +    /// heap and \c j will be in the heap with the same prioriority
   1.485 +    /// as item \c i had before.
   1.486      void replace(const Item& i, const Item& j) {
   1.487 -      int idx = iim[i];
   1.488 -      iim.set(i, iim[j]);
   1.489 -      iim.set(j, idx);
   1.490 -      data[idx].first = j;
   1.491 +      int idx = _iim[i];
   1.492 +      _iim.set(i, _iim[j]);
   1.493 +      _iim.set(j, idx);
   1.494 +      _data[idx].first = j;
   1.495      }
   1.496  
   1.497    }; // class BinHeap