1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BUCKET_HEAP_H
20 #define LEMON_BUCKET_HEAP_H
24 ///\brief Bucket heap implementation.
32 namespace _bucket_heap_bits {
35 struct DirectionTraits {
36 static bool less(int left, int right) {
39 static void increase(int& value) {
45 struct DirectionTraits<false> {
46 static bool less(int left, int right) {
49 static void increase(int& value) {
58 /// \brief Bucket heap data structure.
60 /// This class implements the \e bucket \e heap data structure.
61 /// It practically conforms to the \ref concepts::Heap "heap concept",
62 /// but it has some limitations.
64 /// The bucket heap is a very simple structure. It can store only
65 /// \c int priorities and it maintains a list of items for each priority
66 /// in the range <tt>[0..C)</tt>. So it should only be used when the
67 /// priorities are small. It is not intended to use as a Dijkstra heap.
69 /// \tparam IM A read-writable item map with \c int values, used
70 /// internally to handle the cross references.
71 /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
72 /// The default is \e min-heap. If this parameter is set to \c false,
73 /// then the comparison is reversed, so the top(), prio() and pop()
74 /// functions deal with the item having maximum priority instead of the
77 /// \sa SimpleBucketHeap
78 template <typename IM, bool MIN = true>
83 /// Type of the item-int map.
84 typedef IM ItemIntMap;
85 /// Type of the priorities.
87 /// Type of the items stored in the heap.
88 typedef typename ItemIntMap::Key Item;
89 /// Type of the item-priority pairs.
90 typedef std::pair<Item,Prio> Pair;
94 typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
98 /// \brief Type to represent the states of the items.
100 /// Each item has a state associated to it. It can be "in heap",
101 /// "pre-heap" or "post-heap". The latter two are indifferent from the
102 /// heap's point of view, but may be useful to the user.
104 /// The item-int map must be initialized in such way that it assigns
105 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
107 IN_HEAP = 0, ///< = 0.
108 PRE_HEAP = -1, ///< = -1.
109 POST_HEAP = -2 ///< = -2.
114 /// \brief Constructor.
117 /// \param map A map that assigns \c int values to the items.
118 /// It is used internally to handle the cross references.
119 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
120 explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
122 /// \brief The number of items stored in the heap.
124 /// This function returns the number of items stored in the heap.
125 int size() const { return _data.size(); }
127 /// \brief Check if the heap is empty.
129 /// This function returns \c true if the heap is empty.
130 bool empty() const { return _data.empty(); }
132 /// \brief Make the heap empty.
134 /// This functon makes the heap empty.
135 /// It does not change the cross reference map. If you want to reuse
136 /// a heap that is not surely empty, you should first clear it and
137 /// then you should set the cross reference map to \c PRE_HEAP
140 _data.clear(); _first.clear(); _minimum = 0;
145 void relocateLast(int idx) {
146 if (idx + 1 < int(_data.size())) {
147 _data[idx] = _data.back();
148 if (_data[idx].prev != -1) {
149 _data[_data[idx].prev].next = idx;
151 _first[_data[idx].value] = idx;
153 if (_data[idx].next != -1) {
154 _data[_data[idx].next].prev = idx;
156 _iim[_data[idx].item] = idx;
161 void unlace(int idx) {
162 if (_data[idx].prev != -1) {
163 _data[_data[idx].prev].next = _data[idx].next;
165 _first[_data[idx].value] = _data[idx].next;
167 if (_data[idx].next != -1) {
168 _data[_data[idx].next].prev = _data[idx].prev;
173 if (int(_first.size()) <= _data[idx].value) {
174 _first.resize(_data[idx].value + 1, -1);
176 _data[idx].next = _first[_data[idx].value];
177 if (_data[idx].next != -1) {
178 _data[_data[idx].next].prev = idx;
180 _first[_data[idx].value] = idx;
181 _data[idx].prev = -1;
186 /// \brief Insert a pair of item and priority into the heap.
188 /// This function inserts \c p.first to the heap with priority
190 /// \param p The pair to insert.
191 /// \pre \c p.first must not be stored in the heap.
192 void push(const Pair& p) {
193 push(p.first, p.second);
196 /// \brief Insert an item into the heap with the given priority.
198 /// This function inserts the given item into the heap with the
200 /// \param i The item to insert.
201 /// \param p The priority of the item.
202 /// \pre \e i must not be stored in the heap.
203 void push(const Item &i, const Prio &p) {
204 int idx = _data.size();
206 _data.push_back(BucketItem(i, p));
208 if (Direction::less(p, _minimum)) {
213 /// \brief Return the item having minimum priority.
215 /// This function returns the item having minimum priority.
216 /// \pre The heap must be non-empty.
218 while (_first[_minimum] == -1) {
219 Direction::increase(_minimum);
221 return _data[_first[_minimum]].item;
224 /// \brief The minimum priority.
226 /// This function returns the minimum priority.
227 /// \pre The heap must be non-empty.
229 while (_first[_minimum] == -1) {
230 Direction::increase(_minimum);
235 /// \brief Remove the item having minimum priority.
237 /// This function removes the item having minimum priority.
238 /// \pre The heap must be non-empty.
240 while (_first[_minimum] == -1) {
241 Direction::increase(_minimum);
243 int idx = _first[_minimum];
244 _iim[_data[idx].item] = -2;
249 /// \brief Remove the given item from the heap.
251 /// This function removes the given item from the heap if it is
253 /// \param i The item to delete.
254 /// \pre \e i must be in the heap.
255 void erase(const Item &i) {
257 _iim[_data[idx].item] = -2;
262 /// \brief The priority of the given item.
264 /// This function returns the priority of the given item.
265 /// \param i The item.
266 /// \pre \e i must be in the heap.
267 Prio operator[](const Item &i) const {
269 return _data[idx].value;
272 /// \brief Set the priority of an item or insert it, if it is
273 /// not stored in the heap.
275 /// This method sets the priority of the given item if it is
276 /// already stored in the heap. Otherwise it inserts the given
277 /// item into the heap with the given priority.
278 /// \param i The item.
279 /// \param p The priority.
280 void set(const Item &i, const Prio &p) {
284 } else if (Direction::less(p, _data[idx].value)) {
291 /// \brief Decrease the priority of an item to the given value.
293 /// This function decreases the priority of an item to the given value.
294 /// \param i The item.
295 /// \param p The priority.
296 /// \pre \e i must be stored in the heap with priority at least \e p.
297 void decrease(const Item &i, const Prio &p) {
300 _data[idx].value = p;
301 if (Direction::less(p, _minimum)) {
307 /// \brief Increase the priority of an item to the given value.
309 /// This function increases the priority of an item to the given value.
310 /// \param i The item.
311 /// \param p The priority.
312 /// \pre \e i must be stored in the heap with priority at most \e p.
313 void increase(const Item &i, const Prio &p) {
316 _data[idx].value = p;
320 /// \brief Return the state of an item.
322 /// This method returns \c PRE_HEAP if the given item has never
323 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
324 /// and \c POST_HEAP otherwise.
325 /// In the latter case it is possible that the item will get back
326 /// to the heap again.
327 /// \param i The item.
328 State state(const Item &i) const {
330 if (idx >= 0) idx = 0;
334 /// \brief Set the state of an item in the heap.
336 /// This function sets the state of the given item in the heap.
337 /// It can be used to manually clear the heap when it is important
338 /// to achive better time complexity.
339 /// \param i The item.
340 /// \param st The state. It should not be \c IN_HEAP.
341 void state(const Item& i, State st) {
345 if (state(i) == IN_HEAP) {
358 BucketItem(const Item& _item, int _value)
359 : item(_item), value(_value) {}
368 std::vector<int> _first;
369 std::vector<BucketItem> _data;
370 mutable int _minimum;
372 }; // class BucketHeap
376 /// \brief Simplified bucket heap data structure.
378 /// This class implements a simplified \e bucket \e heap data
379 /// structure. It does not provide some functionality, but it is
380 /// faster and simpler than BucketHeap. The main difference is
381 /// that BucketHeap stores a doubly-linked list for each key while
382 /// this class stores only simply-linked lists. It supports erasing
383 /// only for the item having minimum priority and it does not support
384 /// key increasing and decreasing.
386 /// Note that this implementation does not conform to the
387 /// \ref concepts::Heap "heap concept" due to the lack of some
390 /// \tparam IM A read-writable item map with \c int values, used
391 /// internally to handle the cross references.
392 /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
393 /// The default is \e min-heap. If this parameter is set to \c false,
394 /// then the comparison is reversed, so the top(), prio() and pop()
395 /// functions deal with the item having maximum priority instead of the
399 template <typename IM, bool MIN = true >
400 class SimpleBucketHeap {
404 /// Type of the item-int map.
405 typedef IM ItemIntMap;
406 /// Type of the priorities.
408 /// Type of the items stored in the heap.
409 typedef typename ItemIntMap::Key Item;
410 /// Type of the item-priority pairs.
411 typedef std::pair<Item,Prio> Pair;
415 typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
419 /// \brief Type to represent the states of the items.
421 /// Each item has a state associated to it. It can be "in heap",
422 /// "pre-heap" or "post-heap". The latter two are indifferent from the
423 /// heap's point of view, but may be useful to the user.
425 /// The item-int map must be initialized in such way that it assigns
426 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
428 IN_HEAP = 0, ///< = 0.
429 PRE_HEAP = -1, ///< = -1.
430 POST_HEAP = -2 ///< = -2.
435 /// \brief Constructor.
438 /// \param map A map that assigns \c int values to the items.
439 /// It is used internally to handle the cross references.
440 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
441 explicit SimpleBucketHeap(ItemIntMap &map)
442 : _iim(map), _free(-1), _num(0), _minimum(0) {}
444 /// \brief The number of items stored in the heap.
446 /// This function returns the number of items stored in the heap.
447 int size() const { return _num; }
449 /// \brief Check if the heap is empty.
451 /// This function returns \c true if the heap is empty.
452 bool empty() const { return _num == 0; }
454 /// \brief Make the heap empty.
456 /// This functon makes the heap empty.
457 /// It does not change the cross reference map. If you want to reuse
458 /// a heap that is not surely empty, you should first clear it and
459 /// then you should set the cross reference map to \c PRE_HEAP
462 _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
465 /// \brief Insert a pair of item and priority into the heap.
467 /// This function inserts \c p.first to the heap with priority
469 /// \param p The pair to insert.
470 /// \pre \c p.first must not be stored in the heap.
471 void push(const Pair& p) {
472 push(p.first, p.second);
475 /// \brief Insert an item into the heap with the given priority.
477 /// This function inserts the given item into the heap with the
479 /// \param i The item to insert.
480 /// \param p The priority of the item.
481 /// \pre \e i must not be stored in the heap.
482 void push(const Item &i, const Prio &p) {
486 _data.push_back(BucketItem(i));
489 _free = _data[idx].next;
493 if (p >= int(_first.size())) _first.resize(p + 1, -1);
494 _data[idx].next = _first[p];
496 if (Direction::less(p, _minimum)) {
502 /// \brief Return the item having minimum priority.
504 /// This function returns the item having minimum priority.
505 /// \pre The heap must be non-empty.
507 while (_first[_minimum] == -1) {
508 Direction::increase(_minimum);
510 return _data[_first[_minimum]].item;
513 /// \brief The minimum priority.
515 /// This function returns the minimum priority.
516 /// \pre The heap must be non-empty.
518 while (_first[_minimum] == -1) {
519 Direction::increase(_minimum);
524 /// \brief Remove the item having minimum priority.
526 /// This function removes the item having minimum priority.
527 /// \pre The heap must be non-empty.
529 while (_first[_minimum] == -1) {
530 Direction::increase(_minimum);
532 int idx = _first[_minimum];
533 _iim[_data[idx].item] = -2;
534 _first[_minimum] = _data[idx].next;
535 _data[idx].next = _free;
540 /// \brief The priority of the given item.
542 /// This function returns the priority of the given item.
543 /// \param i The item.
544 /// \pre \e i must be in the heap.
545 /// \warning This operator is not a constant time function because
546 /// it scans the whole data structure to find the proper value.
547 Prio operator[](const Item &i) const {
548 for (int k = 0; k < int(_first.size()); ++k) {
551 if (_data[idx].item == i) {
554 idx = _data[idx].next;
560 /// \brief Return the state of an item.
562 /// This method returns \c PRE_HEAP if the given item has never
563 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
564 /// and \c POST_HEAP otherwise.
565 /// In the latter case it is possible that the item will get back
566 /// to the heap again.
567 /// \param i The item.
568 State state(const Item &i) const {
570 if (idx >= 0) idx = 0;
577 BucketItem(const Item& _item)
585 std::vector<int> _first;
586 std::vector<BucketItem> _data;
588 mutable int _minimum;
590 }; // class SimpleBucketHeap