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