3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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_BIN_HEAP_H
20 #define LEMON_BIN_HEAP_H
24 ///\brief Binary Heap implementation.
34 ///\brief A Binary Heap implementation.
36 ///This class implements the \e binary \e heap data structure. A \e heap
37 ///is a data structure for storing items with specified values called \e
38 ///priorities in such a way that finding the item with minimum priority is
39 ///efficient. \c Compare specifies the ordering of the priorities. In a heap
40 ///one can change the priority of an item, add or erase an item, etc.
42 ///\param _Prio Type of the priority of the items.
43 ///\param _ItemIntMap A read and writable Item int map, used internally
44 ///to handle the cross references.
45 ///\param _Compare A class for the ordering of the priorities. The
46 ///default is \c std::less<_Prio>.
50 template <typename _Prio, typename _ItemIntMap,
51 typename _Compare = std::less<_Prio> >
55 typedef _ItemIntMap ItemIntMap;
57 typedef typename ItemIntMap::Key Item;
58 typedef std::pair<Item,Prio> Pair;
59 typedef _Compare Compare;
61 /// \brief Type to represent the items states.
63 /// Each Item element have a state associated to it. It may be "in heap",
64 /// "pre heap" or "post heap". The latter two are indifferent from the
65 /// heap's point of view, but may be useful to the user.
67 /// The ItemIntMap \e should be initialized in such way that it maps
68 /// PRE_HEAP (-1) to any element to be put in the heap...
76 std::vector<Pair> data;
81 /// \brief The constructor.
84 /// \param _iim should be given to the constructor, since it is used
85 /// internally to handle the cross references. The value of the map
86 /// should be PRE_HEAP (-1) for each element.
87 explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
89 /// \brief The constructor.
92 /// \param _iim should be given to the constructor, since it is used
93 /// internally to handle the cross references. The value of the map
94 /// should be PRE_HEAP (-1) for each element.
96 /// \param _comp The comparator function object.
97 BinHeap(ItemIntMap &_iim, const Compare &_comp)
98 : iim(_iim), comp(_comp) {}
101 /// The number of items stored in the heap.
103 /// \brief Returns the number of items stored in the heap.
104 int size() const { return data.size(); }
106 /// \brief Checks if the heap stores no items.
108 /// Returns \c true if and only if the heap stores no items.
109 bool empty() const { return data.empty(); }
111 /// \brief Make empty this heap.
113 /// Make empty this heap. It does not change the cross reference map.
114 /// If you want to reuse what is not surely empty you should first clear
115 /// the heap and after that you should set the cross reference map for
116 /// each item to \c PRE_HEAP.
122 static int parent(int i) { return (i-1)/2; }
124 static int second_child(int i) { return 2*i+2; }
125 bool less(const Pair &p1, const Pair &p2) const {
126 return comp(p1.second, p2.second);
129 int bubble_up(int hole, Pair p) {
130 int par = parent(hole);
131 while( hole>0 && less(p,data[par]) ) {
132 move(data[par],hole);
140 int bubble_down(int hole, Pair p, int length) {
141 int child = second_child(hole);
142 while(child < length) {
143 if( less(data[child-1], data[child]) ) {
146 if( !less(data[child], p) )
148 move(data[child], hole);
150 child = second_child(hole);
153 if( child<length && less(data[child], p) ) {
154 move(data[child], hole);
162 void move(const Pair &p, int i) {
168 /// \brief Insert a pair of item and priority into the heap.
170 /// Adds \c p.first to the heap with priority \c p.second.
171 /// \param p The pair to insert.
172 void push(const Pair &p) {
178 /// \brief Insert an item into the heap with the given heap.
180 /// Adds \c i to the heap with priority \c p.
181 /// \param i The item to insert.
182 /// \param p The priority of the item.
183 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
185 /// \brief Returns the item with minimum priority relative to \c Compare.
187 /// This method returns the item with minimum priority relative to \c
189 /// \pre The heap must be nonempty.
191 return data[0].first;
194 /// \brief Returns the minimum priority relative to \c Compare.
196 /// It returns the minimum priority relative to \c Compare.
197 /// \pre The heap must be nonempty.
199 return data[0].second;
202 /// \brief Deletes the item with minimum priority relative to \c Compare.
204 /// This method deletes the item with minimum priority relative to \c
205 /// Compare from the heap.
206 /// \pre The heap must be non-empty.
208 int n = data.size()-1;
209 iim.set(data[0].first, POST_HEAP);
211 bubble_down(0, data[n], n);
216 /// \brief Deletes \c i from the heap.
218 /// This method deletes item \c i from the heap.
219 /// \param i The item to erase.
220 /// \pre The item should be in the heap.
221 void erase(const Item &i) {
223 int n = data.size()-1;
224 iim.set(data[h].first, POST_HEAP);
226 if ( bubble_up(h, data[n]) == h) {
227 bubble_down(h, data[n], n);
234 /// \brief Returns the priority of \c i.
236 /// This function returns the priority of item \c i.
237 /// \pre \c i must be in the heap.
238 /// \param i The item.
239 Prio operator[](const Item &i) const {
241 return data[idx].second;
244 /// \brief \c i gets to the heap with priority \c p independently
245 /// if \c i was already there.
247 /// This method calls \ref push(\c i, \c p) if \c i is not stored
248 /// in the heap and sets the priority of \c i to \c p otherwise.
249 /// \param i The item.
250 /// \param p The priority.
251 void set(const Item &i, const Prio &p) {
256 else if( comp(p, data[idx].second) ) {
257 bubble_up(idx, Pair(i,p));
260 bubble_down(idx, Pair(i,p), data.size());
264 /// \brief Decreases the priority of \c i to \c p.
266 /// This method decreases the priority of item \c i to \c p.
267 /// \pre \c i must be stored in the heap with priority at least \c
268 /// p relative to \c Compare.
269 /// \param i The item.
270 /// \param p The priority.
271 void decrease(const Item &i, const Prio &p) {
273 bubble_up(idx, Pair(i,p));
276 /// \brief Increases the priority of \c i to \c p.
278 /// This method sets the priority of item \c i to \c p.
279 /// \pre \c i must be stored in the heap with priority at most \c
280 /// p relative to \c Compare.
281 /// \param i The item.
282 /// \param p The priority.
283 void increase(const Item &i, const Prio &p) {
285 bubble_down(idx, Pair(i,p), data.size());
288 /// \brief Returns if \c item is in, has already been in, or has
289 /// never been in the heap.
291 /// This method returns PRE_HEAP if \c item has never been in the
292 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
293 /// otherwise. In the latter case it is possible that \c item will
294 /// get back to the heap again.
295 /// \param i The item.
296 State state(const Item &i) const {
303 /// \brief Sets the state of the \c item in the heap.
305 /// Sets the state of the \c item in the heap. It can be used to
306 /// manually clear the heap when it is important to achive the
307 /// better time complexity.
308 /// \param i The item.
309 /// \param st The state. It should not be \c IN_HEAP.
310 void state(const Item& i, State st) {
314 if (state(i) == IN_HEAP) {
328 #endif // LEMON_BIN_HEAP_H