1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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 A Bucket Heap implementation.
60 /// This class implements the \e bucket \e heap data structure. A \e heap
61 /// is a data structure for storing items with specified values called \e
62 /// priorities in such a way that finding the item with minimum priority is
63 /// efficient. The bucket heap is very simple implementation, it can store
64 /// only integer priorities and it stores for each priority in the
65 /// \f$ [0..C) \f$ range a list of items. So it should be used only when
66 /// the priorities are small. It is not intended to use as dijkstra heap.
68 /// \param IM A read and write Item int map, used internally
69 /// to handle the cross references.
70 /// \param MIN If the given parameter is false then instead of the
71 /// minimum value the maximum can be retrivied with the top() and
72 /// prio() member functions.
73 template <typename IM, bool MIN = true>
78 typedef typename IM::Key Item;
82 typedef std::pair<Item, Prio> Pair;
84 typedef IM ItemIntMap;
88 typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
92 /// \brief Type to represent the items states.
94 /// Each Item element have a state associated to it. It may be "in heap",
95 /// "pre heap" or "post heap". The latter two are indifferent from the
96 /// heap's point of view, but may be useful to the user.
98 /// The item-int map must be initialized in such way that it assigns
99 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
101 IN_HEAP = 0, ///< = 0.
102 PRE_HEAP = -1, ///< = -1.
103 POST_HEAP = -2 ///< = -2.
107 /// \brief The constructor.
110 /// \param map should be given to the constructor, since it is used
111 /// internally to handle the cross references. The value of the map
112 /// should be PRE_HEAP (-1) for each element.
113 explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
115 /// The number of items stored in the heap.
117 /// \brief Returns the number of items stored in the heap.
118 int size() const { return _data.size(); }
120 /// \brief Checks if the heap stores no items.
122 /// Returns \c true if and only if the heap stores no items.
123 bool empty() const { return _data.empty(); }
125 /// \brief Make empty this heap.
127 /// Make empty this heap. It does not change the cross reference
128 /// map. If you want to reuse a heap what is not surely empty you
129 /// should first clear the heap and after that you should set the
130 /// cross reference map for each item to \c PRE_HEAP.
132 _data.clear(); _first.clear(); _minimum = 0;
137 void relocate_last(int idx) {
138 if (idx + 1 < int(_data.size())) {
139 _data[idx] = _data.back();
140 if (_data[idx].prev != -1) {
141 _data[_data[idx].prev].next = idx;
143 _first[_data[idx].value] = idx;
145 if (_data[idx].next != -1) {
146 _data[_data[idx].next].prev = idx;
148 _iim[_data[idx].item] = idx;
153 void unlace(int idx) {
154 if (_data[idx].prev != -1) {
155 _data[_data[idx].prev].next = _data[idx].next;
157 _first[_data[idx].value] = _data[idx].next;
159 if (_data[idx].next != -1) {
160 _data[_data[idx].next].prev = _data[idx].prev;
165 if (int(_first.size()) <= _data[idx].value) {
166 _first.resize(_data[idx].value + 1, -1);
168 _data[idx].next = _first[_data[idx].value];
169 if (_data[idx].next != -1) {
170 _data[_data[idx].next].prev = idx;
172 _first[_data[idx].value] = idx;
173 _data[idx].prev = -1;
177 /// \brief Insert a pair of item and priority into the heap.
179 /// Adds \c p.first to the heap with priority \c p.second.
180 /// \param p The pair to insert.
181 void push(const Pair& p) {
182 push(p.first, p.second);
185 /// \brief Insert an item into the heap with the given priority.
187 /// Adds \c i to the heap with priority \c p.
188 /// \param i The item to insert.
189 /// \param p The priority of the item.
190 void push(const Item &i, const Prio &p) {
191 int idx = _data.size();
193 _data.push_back(BucketItem(i, p));
195 if (Direction::less(p, _minimum)) {
200 /// \brief Returns the item with minimum priority.
202 /// This method returns the item with minimum priority.
203 /// \pre The heap must be nonempty.
205 while (_first[_minimum] == -1) {
206 Direction::increase(_minimum);
208 return _data[_first[_minimum]].item;
211 /// \brief Returns the minimum priority.
213 /// It returns the minimum priority.
214 /// \pre The heap must be nonempty.
216 while (_first[_minimum] == -1) {
217 Direction::increase(_minimum);
222 /// \brief Deletes the item with minimum priority.
224 /// This method deletes the item with minimum priority from the heap.
225 /// \pre The heap must be non-empty.
227 while (_first[_minimum] == -1) {
228 Direction::increase(_minimum);
230 int idx = _first[_minimum];
231 _iim[_data[idx].item] = -2;
236 /// \brief Deletes \c i from the heap.
238 /// This method deletes item \c i from the heap, if \c i was
239 /// already stored in the heap.
240 /// \param i The item to erase.
241 void erase(const Item &i) {
243 _iim[_data[idx].item] = -2;
249 /// \brief Returns the priority of \c i.
251 /// This function returns the priority of item \c i.
252 /// \pre \c i must be in the heap.
253 /// \param i The item.
254 Prio operator[](const Item &i) const {
256 return _data[idx].value;
259 /// \brief \c i gets to the heap with priority \c p independently
260 /// if \c i was already there.
262 /// This method calls \ref push(\c i, \c p) if \c i is not stored
263 /// in the heap and sets the priority of \c i to \c p otherwise.
264 /// \param i The item.
265 /// \param p The priority.
266 void set(const Item &i, const Prio &p) {
270 } else if (Direction::less(p, _data[idx].value)) {
277 /// \brief Decreases the priority of \c i to \c p.
279 /// This method decreases the priority of item \c i to \c p.
280 /// \pre \c i must be stored in the heap with priority at least \c
281 /// p relative to \c Compare.
282 /// \param i The item.
283 /// \param p The priority.
284 void decrease(const Item &i, const Prio &p) {
287 _data[idx].value = p;
288 if (Direction::less(p, _minimum)) {
294 /// \brief Increases the priority of \c i to \c p.
296 /// This method sets the priority of item \c i to \c p.
297 /// \pre \c i must be stored in the heap with priority at most \c
298 /// p relative to \c Compare.
299 /// \param i The item.
300 /// \param p The priority.
301 void increase(const Item &i, const Prio &p) {
304 _data[idx].value = p;
308 /// \brief Returns if \c item is in, has already been in, or has
309 /// never been in the heap.
311 /// This method returns PRE_HEAP if \c item has never been in the
312 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
313 /// otherwise. In the latter case it is possible that \c item will
314 /// get back to the heap again.
315 /// \param i The item.
316 State state(const Item &i) const {
318 if (idx >= 0) idx = 0;
322 /// \brief Sets the state of the \c item in the heap.
324 /// Sets the state of the \c item in the heap. It can be used to
325 /// manually clear the heap when it is important to achive the
326 /// better time complexity.
327 /// \param i The item.
328 /// \param st The state. It should not be \c IN_HEAP.
329 void state(const Item& i, State st) {
333 if (state(i) == IN_HEAP) {
346 BucketItem(const Item& _item, int _value)
347 : item(_item), value(_value) {}
356 std::vector<int> _first;
357 std::vector<BucketItem> _data;
358 mutable int _minimum;
360 }; // class BucketHeap
364 /// \brief A Simplified Bucket Heap implementation.
366 /// This class implements a simplified \e bucket \e heap data
367 /// structure. It does not provide some functionality but it faster
368 /// and simplier data structure than the BucketHeap. The main
369 /// difference is that the BucketHeap stores for every key a double
370 /// linked list while this class stores just simple lists. In the
371 /// other way it does not support erasing each elements just the
372 /// minimal and it does not supports key increasing, decreasing.
374 /// \param IM A read and write Item int map, used internally
375 /// to handle the cross references.
376 /// \param MIN If the given parameter is false then instead of the
377 /// minimum value the maximum can be retrivied with the top() and
378 /// prio() member functions.
381 template <typename IM, bool MIN = true >
382 class SimpleBucketHeap {
385 typedef typename IM::Key Item;
387 typedef std::pair<Item, Prio> Pair;
388 typedef IM ItemIntMap;
392 typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
396 /// \brief Type to represent the items states.
398 /// Each Item element have a state associated to it. It may be "in heap",
399 /// "pre heap" or "post heap". The latter two are indifferent from the
400 /// heap's point of view, but may be useful to the user.
402 /// The item-int map must be initialized in such way that it assigns
403 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
405 IN_HEAP = 0, ///< = 0.
406 PRE_HEAP = -1, ///< = -1.
407 POST_HEAP = -2 ///< = -2.
412 /// \brief The constructor.
415 /// \param map should be given to the constructor, since it is used
416 /// internally to handle the cross references. The value of the map
417 /// should be PRE_HEAP (-1) for each element.
418 explicit SimpleBucketHeap(ItemIntMap &map)
419 : _iim(map), _free(-1), _num(0), _minimum(0) {}
421 /// \brief Returns the number of items stored in the heap.
423 /// The number of items stored in the heap.
424 int size() const { return _num; }
426 /// \brief Checks if the heap stores no items.
428 /// Returns \c true if and only if the heap stores no items.
429 bool empty() const { return _num == 0; }
431 /// \brief Make empty this heap.
433 /// Make empty this heap. It does not change the cross reference
434 /// map. If you want to reuse a heap what is not surely empty you
435 /// should first clear the heap and after that you should set the
436 /// cross reference map for each item to \c PRE_HEAP.
438 _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
441 /// \brief Insert a pair of item and priority into the heap.
443 /// Adds \c p.first to the heap with priority \c p.second.
444 /// \param p The pair to insert.
445 void push(const Pair& p) {
446 push(p.first, p.second);
449 /// \brief Insert an item into the heap with the given priority.
451 /// Adds \c i to the heap with priority \c p.
452 /// \param i The item to insert.
453 /// \param p The priority of the item.
454 void push(const Item &i, const Prio &p) {
458 _data.push_back(BucketItem(i));
461 _free = _data[idx].next;
465 if (p >= int(_first.size())) _first.resize(p + 1, -1);
466 _data[idx].next = _first[p];
468 if (Direction::less(p, _minimum)) {
474 /// \brief Returns the item with minimum priority.
476 /// This method returns the item with minimum priority.
477 /// \pre The heap must be nonempty.
479 while (_first[_minimum] == -1) {
480 Direction::increase(_minimum);
482 return _data[_first[_minimum]].item;
485 /// \brief Returns the minimum priority.
487 /// It returns the minimum priority.
488 /// \pre The heap must be nonempty.
490 while (_first[_minimum] == -1) {
491 Direction::increase(_minimum);
496 /// \brief Deletes the item with minimum priority.
498 /// This method deletes the item with minimum priority from the heap.
499 /// \pre The heap must be non-empty.
501 while (_first[_minimum] == -1) {
502 Direction::increase(_minimum);
504 int idx = _first[_minimum];
505 _iim[_data[idx].item] = -2;
506 _first[_minimum] = _data[idx].next;
507 _data[idx].next = _free;
512 /// \brief Returns the priority of \c i.
514 /// This function returns the priority of item \c i.
515 /// \warning This operator is not a constant time function
516 /// because it scans the whole data structure to find the proper
518 /// \pre \c i must be in the heap.
519 /// \param i The item.
520 Prio operator[](const Item &i) const {
521 for (int k = 0; k < _first.size(); ++k) {
524 if (_data[idx].item == i) {
527 idx = _data[idx].next;
533 /// \brief Returns if \c item is in, has already been in, or has
534 /// never been in the heap.
536 /// This method returns PRE_HEAP if \c item has never been in the
537 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
538 /// otherwise. In the latter case it is possible that \c item will
539 /// get back to the heap again.
540 /// \param i The item.
541 State state(const Item &i) const {
543 if (idx >= 0) idx = 0;
550 BucketItem(const Item& _item)
558 std::vector<int> _first;
559 std::vector<BucketItem> _data;
561 mutable int _minimum;
563 }; // class SimpleBucketHeap