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