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