2 * lemon/radix_heap.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_RADIX_HEAP_H
18 #define LEMON_RADIX_HEAP_H
22 ///\brief Radix Heap implementation.
25 #include <lemon/error.h>
29 /// \addtogroup auxdat
32 /// \brief Exception thrown by RadixHeap.
34 /// This Exception is thrown when a smaller priority
35 /// is inserted into the \e RadixHeap then the last time erased.
37 /// \author Balazs Dezso
39 class UnderFlowPriorityError : public RuntimeError {
41 virtual const char* exceptionName() const {
42 return "lemon::UnderFlowPriorityError";
46 /// \brief A Radix Heap implementation.
48 /// This class implements the \e radix \e heap data structure. A \e heap
49 /// is a data structure for storing items with specified values called \e
50 /// priorities in such a way that finding the item with minimum priority is
51 /// efficient. This heap type can store only items with \e int priority.
52 /// In a heap one can change the priority of an item, add or erase an
53 /// item, but the priority cannot be decreased under the last removed
56 /// \param _Item Type of the items to be stored.
57 /// \param _ItemIntMap A read and writable Item int map, used internally
58 /// to handle the cross references.
62 /// \author Balazs Dezso
64 template <typename _Item, typename _ItemIntMap>
70 typedef _ItemIntMap ItemIntMap;
72 /// \brief Type to represent the items states.
74 /// Each Item element have a state associated to it. It may be "in heap",
75 /// "pre heap" or "post heap". The latter two are indifferent from the
76 /// heap's point of view, but may be useful to the user.
78 /// The ItemIntMap \e should be initialized in such way that it maps
79 /// PRE_HEAP (-1) to any element to be put in the heap...
92 RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {}
98 RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
101 std::vector<RadixItem> data;
102 std::vector<RadixBox> boxes;
108 /// \brief The constructor.
111 /// \param _iim should be given to the constructor, since it is used
112 /// internally to handle the cross references. The value of the map
113 /// should be PRE_HEAP (-1) for each element.
114 explicit RadixHeap(ItemIntMap &_iim) : iim(_iim) {
115 boxes.push_back(RadixBox(0, 1));
116 boxes.push_back(RadixBox(1, 1));
119 /// \brief The constructor.
123 /// \param _iim It should be given to the constructor, since it is used
124 /// internally to handle the cross references. The value of the map
125 /// should be PRE_HEAP (-1) for each element.
127 /// \param capacity It determines the initial capacity of the heap.
128 RadixHeap(ItemIntMap &_iim, int capacity) : iim(_iim) {
129 boxes.push_back(RadixBox(0, 1));
130 boxes.push_back(RadixBox(1, 1));
131 while (upper(boxes.back(), capacity)) {
136 /// The number of items stored in the heap.
138 /// \brief Returns the number of items stored in the heap.
139 int size() const { return data.size(); }
140 /// \brief Checks if the heap stores no items.
142 /// Returns \c true if and only if the heap stores no items.
143 bool empty() const { return data.empty(); }
147 bool upper(int box, Prio prio) {
148 return prio < boxes[box].min;
151 bool lower(int box, Prio prio) {
152 return prio >= boxes[box].min + boxes[box].size;
155 /// \brief Remove item from the box list.
156 void remove(int index) {
157 if (data[index].prev >= 0) {
158 data[data[index].prev].next = data[index].next;
160 boxes[data[index].box].first = data[index].next;
162 if (data[index].next >= 0) {
163 data[data[index].next].prev = data[index].prev;
167 /// \brief Insert item into the box list.
168 void insert(int box, int index) {
169 if (boxes[box].first == -1) {
170 boxes[box].first = index;
171 data[index].next = data[index].prev = -1;
173 data[index].next = boxes[box].first;
174 data[boxes[box].first].prev = index;
175 data[index].prev = -1;
176 boxes[box].first = index;
178 data[index].box = box;
181 /// \brief Add a new box to the box list.
183 int min = boxes.back().min + boxes.back().size;
184 int size = 2 * boxes.back().size;
185 boxes.push_back(RadixBox(min, size));
188 /// \brief Move an item up into the proper box.
189 void bubble_up(int index) {
190 if (!lower(data[index].box, data[index].prio)) return;
192 int box = findUp(data[index].box, data[index].prio);
196 /// \brief Find up the proper box for the item with the given prio.
197 int findUp(int start, int prio) {
198 while (lower(start, prio)) {
199 if (++start == (int)boxes.size()) {
206 /// \brief Move an item down into the proper box.
207 void bubble_down(int index) {
208 if (!upper(data[index].box, data[index].prio)) return;
210 int box = findDown(data[index].box, data[index].prio);
214 /// \brief Find up the proper box for the item with the given prio.
215 int findDown(int start, int prio) {
216 while (upper(start, prio)) {
217 if (--start < 0) throw UnderFlowPriorityError();
222 /// \brief Find the first not empty box.
225 while (boxes[first].first == -1) ++first;
229 /// \brief Gives back the minimal prio of the box.
230 int minValue(int box) {
231 int min = data[boxes[box].first].prio;
232 for (int k = boxes[box].first; k != -1; k = data[k].next) {
233 if (data[k].prio < min) min = data[k].prio;
238 /// \brief Rearrange the items of the heap and makes the
239 /// first box not empty.
241 int box = findFirst();
242 if (box == 0) return;
243 int min = minValue(box);
244 for (int i = 0; i <= box; ++i) {
246 min += boxes[i].size;
248 int curr = boxes[box].first, next;
250 next = data[curr].next;
256 void relocate_last(int index) {
257 if (index != (int)data.size() - 1) {
258 data[index] = data.back();
259 if (data[index].prev != -1) {
260 data[data[index].prev].next = index;
262 boxes[data[index].box].first = index;
264 if (data[index].next != -1) {
265 data[data[index].next].prev = index;
267 iim[data[index].item] = index;
274 /// \brief Insert an item into the heap with the given heap.
276 /// Adds \c i to the heap with priority \c p.
277 /// \param i The item to insert.
278 /// \param p The priority of the item.
279 void push(const Item &i, const Prio &p) {
282 data.push_back(RadixItem(i, p));
283 while (lower(boxes.size() - 1, p)) {
286 int box = findDown(boxes.size() - 1, p);
290 /// \brief Returns the item with minimum priority.
292 /// This method returns the item with minimum priority.
293 /// \pre The heap must be nonempty.
295 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
296 return data[boxes[0].first].item;
299 /// \brief Returns the minimum priority.
301 /// It returns the minimum priority.
302 /// \pre The heap must be nonempty.
304 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
305 return data[boxes[0].first].prio;
308 /// \brief Deletes the item with minimum priority.
310 /// This method deletes the item with minimum priority.
311 /// \pre The heap must be non-empty.
314 int index = boxes[0].first;
315 iim[data[index].item] = POST_HEAP;
317 relocate_last(index);
320 /// \brief Deletes \c i from the heap.
322 /// This method deletes item \c i from the heap, if \c i was
323 /// already stored in the heap.
324 /// \param i The item to erase.
325 void erase(const Item &i) {
329 relocate_last(index);
332 /// \brief Returns the priority of \c i.
334 /// This function returns the priority of item \c i.
335 /// \pre \c i must be in the heap.
336 /// \param i The item.
337 Prio operator[](const Item &i) const {
339 return data[idx].prio;
342 /// \brief \c i gets to the heap with priority \c p independently
343 /// if \c i was already there.
345 /// This method calls \ref push(\c i, \c p) if \c i is not stored
346 /// in the heap and sets the priority of \c i to \c p otherwise.
347 /// It may throw an \e UnderFlowPriorityException.
348 /// \param i The item.
349 /// \param p The priority.
350 void set(const Item &i, const Prio &p) {
355 else if( p >= data[idx].prio ) {
365 /// \brief Decreases the priority of \c i to \c p.
367 /// This method decreases the priority of item \c i to \c p.
368 /// \pre \c i must be stored in the heap with priority at least \c p, and
369 /// \c should be greater then the last removed item's priority.
370 /// \param i The item.
371 /// \param p The priority.
372 void decrease(const Item &i, const Prio &p) {
378 /// \brief Increases the priority of \c i to \c p.
380 /// This method sets the priority of item \c i to \c p.
381 /// \pre \c i must be stored in the heap with priority at most \c
382 /// p relative to \c Compare.
383 /// \param i The item.
384 /// \param p The priority.
385 void increase(const Item &i, const Prio &p) {
391 /// \brief Returns if \c item is in, has already been in, or has
392 /// never been in the heap.
394 /// This method returns PRE_HEAP if \c item has never been in the
395 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
396 /// otherwise. In the latter case it is possible that \c item will
397 /// get back to the heap again.
398 /// \param i The item.
399 state_enum state(const Item &i) const {
402 return state_enum(s);
405 }; // class RadixHeap
412 #endif // LEMON_RADIX_HEAP_H