lemon/fib_heap.h
changeset 756 0747f332c478
parent 730 9f529abcaebf
child 757 f1fe0ddad6f7
     1.1 --- a/lemon/fib_heap.h	Thu Jun 11 23:13:24 2009 +0200
     1.2 +++ b/lemon/fib_heap.h	Wed Jul 08 17:21:30 2009 +0200
     1.3 @@ -21,9 +21,10 @@
     1.4  
     1.5  ///\file
     1.6  ///\ingroup auxdat
     1.7 -///\brief Fibonacci Heap implementation.
     1.8 +///\brief Fibonacci heap implementation.
     1.9  
    1.10  #include <vector>
    1.11 +#include <utility>
    1.12  #include <functional>
    1.13  #include <lemon/math.h>
    1.14  
    1.15 @@ -31,42 +32,37 @@
    1.16  
    1.17    /// \ingroup auxdat
    1.18    ///
    1.19 -  ///\brief Fibonacci Heap.
    1.20 +  /// \brief Fibonacci heap data structure.
    1.21    ///
    1.22 -  ///This class implements the \e Fibonacci \e heap data structure. A \e heap
    1.23 -  ///is a data structure for storing items with specified values called \e
    1.24 -  ///priorities in such a way that finding the item with minimum priority is
    1.25 -  ///efficient. \c CMP specifies the ordering of the priorities. In a heap
    1.26 -  ///one can change the priority of an item, add or erase an item, etc.
    1.27 +  /// This class implements the \e Fibonacci \e heap data structure.
    1.28 +  /// It fully conforms to the \ref concepts::Heap "heap concept".
    1.29    ///
    1.30 -  ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
    1.31 -  ///heap. In case of many calls to these operations, it is better to use a
    1.32 -  ///\ref BinHeap "binary heap".
    1.33 +  /// The methods \ref increase() and \ref erase() are not efficient in a
    1.34 +  /// Fibonacci heap. In case of many calls of these operations, it is
    1.35 +  /// better to use other heap structure, e.g. \ref BinHeap "binary heap".
    1.36    ///
    1.37 -  ///\param PRIO Type of the priority of the items.
    1.38 -  ///\param IM A read and writable Item int map, used internally
    1.39 -  ///to handle the cross references.
    1.40 -  ///\param CMP A class for the ordering of the priorities. The
    1.41 -  ///default is \c std::less<PRIO>.
    1.42 -  ///
    1.43 -  ///\sa BinHeap
    1.44 -  ///\sa Dijkstra
    1.45 +  /// \tparam PR Type of the priorities of the items.
    1.46 +  /// \tparam IM A read-writable item map with \c int values, used
    1.47 +  /// internally to handle the cross references.
    1.48 +  /// \tparam CMP A functor class for comparing the priorities.
    1.49 +  /// The default is \c std::less<PR>.
    1.50  #ifdef DOXYGEN
    1.51 -  template <typename PRIO, typename IM, typename CMP>
    1.52 +  template <typename PR, typename IM, typename CMP>
    1.53  #else
    1.54 -  template <typename PRIO, typename IM, typename CMP = std::less<PRIO> >
    1.55 +  template <typename PR, typename IM, typename CMP = std::less<PR> >
    1.56  #endif
    1.57    class FibHeap {
    1.58    public:
    1.59 -    ///\e
    1.60 +
    1.61 +    /// Type of the item-int map.
    1.62      typedef IM ItemIntMap;
    1.63 -    ///\e
    1.64 -    typedef PRIO Prio;
    1.65 -    ///\e
    1.66 +    /// Type of the priorities.
    1.67 +    typedef PR Prio;
    1.68 +    /// Type of the items stored in the heap.
    1.69      typedef typename ItemIntMap::Key Item;
    1.70 -    ///\e
    1.71 +    /// Type of the item-priority pairs.
    1.72      typedef std::pair<Item,Prio> Pair;
    1.73 -    ///\e
    1.74 +    /// Functor type for comparing the priorities.
    1.75      typedef CMP Compare;
    1.76  
    1.77    private:
    1.78 @@ -80,10 +76,10 @@
    1.79  
    1.80    public:
    1.81  
    1.82 -    /// \brief Type to represent the items states.
    1.83 +    /// \brief Type to represent the states of the items.
    1.84      ///
    1.85 -    /// Each Item element have a state associated to it. It may be "in heap",
    1.86 -    /// "pre heap" or "post heap". The latter two are indifferent from the
    1.87 +    /// Each item has a state associated to it. It can be "in heap",
    1.88 +    /// "pre-heap" or "post-heap". The latter two are indifferent from the
    1.89      /// heap's point of view, but may be useful to the user.
    1.90      ///
    1.91      /// The item-int map must be initialized in such way that it assigns
    1.92 @@ -94,60 +90,54 @@
    1.93        POST_HEAP = -2  ///< = -2.
    1.94      };
    1.95  
    1.96 -    /// \brief The constructor
    1.97 +    /// \brief Constructor.
    1.98      ///
    1.99 -    /// \c map should be given to the constructor, since it is
   1.100 -    ///   used internally to handle the cross references.
   1.101 +    /// Constructor.
   1.102 +    /// \param map A map that assigns \c int values to the items.
   1.103 +    /// It is used internally to handle the cross references.
   1.104 +    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
   1.105      explicit FibHeap(ItemIntMap &map)
   1.106        : _minimum(0), _iim(map), _num() {}
   1.107  
   1.108 -    /// \brief The constructor
   1.109 +    /// \brief Constructor.
   1.110      ///
   1.111 -    /// \c map should be given to the constructor, since it is used
   1.112 -    /// internally to handle the cross references. \c comp is an
   1.113 -    /// object for ordering of the priorities.
   1.114 +    /// Constructor.
   1.115 +    /// \param map A map that assigns \c int values to the items.
   1.116 +    /// It is used internally to handle the cross references.
   1.117 +    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
   1.118 +    /// \param comp The function object used for comparing the priorities.
   1.119      FibHeap(ItemIntMap &map, const Compare &comp)
   1.120        : _minimum(0), _iim(map), _comp(comp), _num() {}
   1.121  
   1.122      /// \brief The number of items stored in the heap.
   1.123      ///
   1.124 -    /// Returns the number of items stored in the heap.
   1.125 +    /// This function returns the number of items stored in the heap.
   1.126      int size() const { return _num; }
   1.127  
   1.128 -    /// \brief Checks if the heap stores no items.
   1.129 +    /// \brief Check if the heap is empty.
   1.130      ///
   1.131 -    ///   Returns \c true if and only if the heap stores no items.
   1.132 +    /// This function returns \c true if the heap is empty.
   1.133      bool empty() const { return _num==0; }
   1.134  
   1.135 -    /// \brief Make empty this heap.
   1.136 +    /// \brief Make the heap empty.
   1.137      ///
   1.138 -    /// Make empty this heap. It does not change the cross reference
   1.139 -    /// map.  If you want to reuse a heap what is not surely empty you
   1.140 -    /// should first clear the heap and after that you should set the
   1.141 -    /// cross reference map for each item to \c PRE_HEAP.
   1.142 +    /// This functon makes the heap empty.
   1.143 +    /// It does not change the cross reference map. If you want to reuse
   1.144 +    /// a heap that is not surely empty, you should first clear it and
   1.145 +    /// then you should set the cross reference map to \c PRE_HEAP
   1.146 +    /// for each item.
   1.147      void clear() {
   1.148        _data.clear(); _minimum = 0; _num = 0;
   1.149      }
   1.150  
   1.151 -    /// \brief \c item gets to the heap with priority \c value independently
   1.152 -    /// if \c item was already there.
   1.153 +    /// \brief Insert an item into the heap with the given priority.
   1.154      ///
   1.155 -    /// This method calls \ref push(\c item, \c value) if \c item is not
   1.156 -    /// stored in the heap and it calls \ref decrease(\c item, \c value) or
   1.157 -    /// \ref increase(\c item, \c value) otherwise.
   1.158 -    void set (const Item& item, const Prio& value) {
   1.159 -      int i=_iim[item];
   1.160 -      if ( i >= 0 && _data[i].in ) {
   1.161 -        if ( _comp(value, _data[i].prio) ) decrease(item, value);
   1.162 -        if ( _comp(_data[i].prio, value) ) increase(item, value);
   1.163 -      } else push(item, value);
   1.164 -    }
   1.165 -
   1.166 -    /// \brief Adds \c item to the heap with priority \c value.
   1.167 -    ///
   1.168 -    /// Adds \c item to the heap with priority \c value.
   1.169 -    /// \pre \c item must not be stored in the heap.
   1.170 -    void push (const Item& item, const Prio& value) {
   1.171 +    /// This function inserts the given item into the heap with the
   1.172 +    /// given priority.
   1.173 +    /// \param item The item to insert.
   1.174 +    /// \param prio The priority of the item.
   1.175 +    /// \pre \e item must not be stored in the heap.
   1.176 +    void push (const Item& item, const Prio& prio) {
   1.177        int i=_iim[item];
   1.178        if ( i < 0 ) {
   1.179          int s=_data.size();
   1.180 @@ -168,40 +158,30 @@
   1.181          _data[i].right_neighbor=_data[_minimum].right_neighbor;
   1.182          _data[_minimum].right_neighbor=i;
   1.183          _data[i].left_neighbor=_minimum;
   1.184 -        if ( _comp( value, _data[_minimum].prio) ) _minimum=i;
   1.185 +        if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
   1.186        } else {
   1.187          _data[i].right_neighbor=_data[i].left_neighbor=i;
   1.188          _minimum=i;
   1.189        }
   1.190 -      _data[i].prio=value;
   1.191 +      _data[i].prio=prio;
   1.192        ++_num;
   1.193      }
   1.194  
   1.195 -    /// \brief Returns the item with minimum priority relative to \c Compare.
   1.196 +    /// \brief Return the item having minimum priority.
   1.197      ///
   1.198 -    /// This method returns the item with minimum priority relative to \c
   1.199 -    /// Compare.
   1.200 -    /// \pre The heap must be nonempty.
   1.201 +    /// This function returns the item having minimum priority.
   1.202 +    /// \pre The heap must be non-empty.
   1.203      Item top() const { return _data[_minimum].name; }
   1.204  
   1.205 -    /// \brief Returns the minimum priority relative to \c Compare.
   1.206 +    /// \brief The minimum priority.
   1.207      ///
   1.208 -    /// It returns the minimum priority relative to \c Compare.
   1.209 -    /// \pre The heap must be nonempty.
   1.210 -    const Prio& prio() const { return _data[_minimum].prio; }
   1.211 +    /// This function returns the minimum priority.
   1.212 +    /// \pre The heap must be non-empty.
   1.213 +    Prio prio() const { return _data[_minimum].prio; }
   1.214  
   1.215 -    /// \brief Returns the priority of \c item.
   1.216 +    /// \brief Remove the item having minimum priority.
   1.217      ///
   1.218 -    /// It returns the priority of \c item.
   1.219 -    /// \pre \c item must be in the heap.
   1.220 -    const Prio& operator[](const Item& item) const {
   1.221 -      return _data[_iim[item]].prio;
   1.222 -    }
   1.223 -
   1.224 -    /// \brief Deletes the item with minimum priority relative to \c Compare.
   1.225 -    ///
   1.226 -    /// This method deletes the item with minimum priority relative to \c
   1.227 -    /// Compare from the heap.
   1.228 +    /// This function removes the item having minimum priority.
   1.229      /// \pre The heap must be non-empty.
   1.230      void pop() {
   1.231        /*The first case is that there are only one root.*/
   1.232 @@ -234,10 +214,12 @@
   1.233        --_num;
   1.234      }
   1.235  
   1.236 -    /// \brief Deletes \c item from the heap.
   1.237 +    /// \brief Remove the given item from the heap.
   1.238      ///
   1.239 -    /// This method deletes \c item from the heap, if \c item was already
   1.240 -    /// stored in the heap. It is quite inefficient in Fibonacci heaps.
   1.241 +    /// This function removes the given item from the heap if it is
   1.242 +    /// already stored.
   1.243 +    /// \param item The item to delete.
   1.244 +    /// \pre \e item must be in the heap.
   1.245      void erase (const Item& item) {
   1.246        int i=_iim[item];
   1.247  
   1.248 @@ -252,43 +234,68 @@
   1.249        }
   1.250      }
   1.251  
   1.252 -    /// \brief Decreases the priority of \c item to \c value.
   1.253 +    /// \brief The priority of the given item.
   1.254      ///
   1.255 -    /// This method decreases the priority of \c item to \c value.
   1.256 -    /// \pre \c item must be stored in the heap with priority at least \c
   1.257 -    ///   value relative to \c Compare.
   1.258 -    void decrease (Item item, const Prio& value) {
   1.259 +    /// This function returns the priority of the given item.
   1.260 +    /// \param item The item.
   1.261 +    /// \pre \e item must be in the heap.
   1.262 +    Prio operator[](const Item& item) const {
   1.263 +      return _data[_iim[item]].prio;
   1.264 +    }
   1.265 +
   1.266 +    /// \brief Set the priority of an item or insert it, if it is
   1.267 +    /// not stored in the heap.
   1.268 +    ///
   1.269 +    /// This method sets the priority of the given item if it is
   1.270 +    /// already stored in the heap. Otherwise it inserts the given
   1.271 +    /// item into the heap with the given priority.
   1.272 +    /// \param item The item.
   1.273 +    /// \param prio The priority.
   1.274 +    void set (const Item& item, const Prio& prio) {
   1.275        int i=_iim[item];
   1.276 -      _data[i].prio=value;
   1.277 +      if ( i >= 0 && _data[i].in ) {
   1.278 +        if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
   1.279 +        if ( _comp(_data[i].prio, prio) ) increase(item, prio);
   1.280 +      } else push(item, prio);
   1.281 +    }
   1.282 +
   1.283 +    /// \brief Decrease the priority of an item to the given value.
   1.284 +    ///
   1.285 +    /// This function decreases the priority of an item to the given value.
   1.286 +    /// \param item The item.
   1.287 +    /// \param prio The priority.
   1.288 +    /// \pre \e item must be stored in the heap with priority at least \e prio.
   1.289 +    void decrease (const Item& item, const Prio& prio) {
   1.290 +      int i=_iim[item];
   1.291 +      _data[i].prio=prio;
   1.292        int p=_data[i].parent;
   1.293  
   1.294 -      if ( p!=-1 && _comp(value, _data[p].prio) ) {
   1.295 +      if ( p!=-1 && _comp(prio, _data[p].prio) ) {
   1.296          cut(i,p);
   1.297          cascade(p);
   1.298        }
   1.299 -      if ( _comp(value, _data[_minimum].prio) ) _minimum=i;
   1.300 +      if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
   1.301      }
   1.302  
   1.303 -    /// \brief Increases the priority of \c item to \c value.
   1.304 +    /// \brief Increase the priority of an item to the given value.
   1.305      ///
   1.306 -    /// This method sets the priority of \c item to \c value. Though
   1.307 -    /// there is no precondition on the priority of \c item, this
   1.308 -    /// method should be used only if it is indeed necessary to increase
   1.309 -    /// (relative to \c Compare) the priority of \c item, because this
   1.310 -    /// method is inefficient.
   1.311 -    void increase (Item item, const Prio& value) {
   1.312 +    /// This function increases the priority of an item to the given value.
   1.313 +    /// \param item The item.
   1.314 +    /// \param prio The priority.
   1.315 +    /// \pre \e item must be stored in the heap with priority at most \e prio.
   1.316 +    void increase (const Item& item, const Prio& prio) {
   1.317        erase(item);
   1.318 -      push(item, value);
   1.319 +      push(item, prio);
   1.320      }
   1.321  
   1.322 -
   1.323 -    /// \brief Returns if \c item is in, has already been in, or has never
   1.324 -    /// been in the heap.
   1.325 +    /// \brief Return the state of an item.
   1.326      ///
   1.327 -    /// This method returns PRE_HEAP if \c item has never been in the
   1.328 -    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.329 -    /// otherwise. In the latter case it is possible that \c item will
   1.330 -    /// get back to the heap again.
   1.331 +    /// This method returns \c PRE_HEAP if the given item has never
   1.332 +    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
   1.333 +    /// and \c POST_HEAP otherwise.
   1.334 +    /// In the latter case it is possible that the item will get back
   1.335 +    /// to the heap again.
   1.336 +    /// \param item The item.
   1.337      State state(const Item &item) const {
   1.338        int i=_iim[item];
   1.339        if( i>=0 ) {
   1.340 @@ -298,11 +305,11 @@
   1.341        return State(i);
   1.342      }
   1.343  
   1.344 -    /// \brief Sets the state of the \c item in the heap.
   1.345 +    /// \brief Set the state of an item in the heap.
   1.346      ///
   1.347 -    /// Sets the state of the \c item in the heap. It can be used to
   1.348 -    /// manually clear the heap when it is important to achive the
   1.349 -    /// better time _complexity.
   1.350 +    /// This function sets the state of the given item in the heap.
   1.351 +    /// It can be used to manually clear the heap when it is important
   1.352 +    /// to achive better time complexity.
   1.353      /// \param i The item.
   1.354      /// \param st The state. It should not be \c IN_HEAP.
   1.355      void state(const Item& i, State st) {