3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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_KARY_HEAP_H
20 #define LEMON_KARY_HEAP_H
24 ///\brief Kary Heap implementation.
35 ///\brief A Kary Heap implementation.
37 ///This class implements the \e Kary \e heap data structure. A \e heap
38 ///is a data structure for storing items with specified values called \e
39 ///priorities in such a way that finding the item with minimum priority is
40 ///efficient. \c Compare specifies the ordering of the priorities. In a heap
41 ///one can change the priority of an item, add or erase an item, etc.
43 ///\param _Prio Type of the priority of the items.
44 ///\param _ItemIntMap A read and writable Item int map, used internally
45 ///to handle the cross references.
46 ///\param _Compare A class for the ordering of the priorities. The
47 ///default is \c std::less<_Prio>.
51 ///\author Dorian Batha
53 template <typename _Prio, typename _ItemIntMap,
54 typename _Compare = std::less<_Prio> >
60 typedef _ItemIntMap ItemIntMap;
64 typedef typename ItemIntMap::Key Item;
66 typedef std::pair<Item,Prio> Pair;
68 typedef _Compare Compare;
71 /// \brief Type to represent the items states.
73 /// Each Item element have a state associated to it. It may be "in heap",
74 /// "pre heap" or "post heap". The latter two are indifferent from the
75 /// heap's point of view, but may be useful to the user.
77 /// The ItemIntMap \e should be initialized in such way that it maps
78 /// PRE_HEAP (-1) to any element to be put in the heap...
86 std::vector<Pair> data;
92 /// \brief The constructor.
95 /// \param _iim should be given to the constructor, since it is used
96 /// internally to handle the cross references. The value of the map
97 /// should be PRE_HEAP (-1) for each element.
98 explicit KaryHeap(ItemIntMap &_iim, const int &_K=32) : iim(_iim), K(_K) {}
100 /// \brief The constructor.
103 /// \param _iim should be given to the constructor, since it is used
104 /// internally to handle the cross references. The value of the map
105 /// should be PRE_HEAP (-1) for each element.
107 /// \param _comp The comparator function object.
108 KaryHeap(ItemIntMap &_iim, const Compare &_comp, const int &_K=32)
109 : iim(_iim), comp(_comp), K(_K) {}
112 /// The number of items stored in the heap.
114 /// \brief Returns the number of items stored in the heap.
115 int size() const { return data.size(); }
117 /// \brief Checks if the heap stores no items.
119 /// Returns \c true if and only if the heap stores no items.
120 bool empty() const { return data.empty(); }
122 /// \brief Make empty this heap.
124 /// Make empty this heap. It does not change the cross reference map.
125 /// If you want to reuse what is not surely empty you should first clear
126 /// the heap and after that you should set the cross reference map for
127 /// each item to \c PRE_HEAP.
128 void clear() { data.clear(); }
131 int parent(int i) { return (i-1)/K; }
132 int first_child(int i) { return K*i+1; }
134 bool less(const Pair &p1, const Pair &p2) const {
135 return comp(p1.second, p2.second);
138 int find_min(const int child, const int length) {
140 while( i<K && child+i<length ) {
141 if( less(data[child+i], data[min]) )
148 void bubble_up(int hole, Pair p) {
149 int par = parent(hole);
150 while( hole>0 && less(p,data[par]) ) {
151 move(data[par],hole);
158 void bubble_down(int hole, Pair p, int length) {
160 int child = first_child(hole);
161 while( child<length ) {
162 child = find_min(child, length);
163 if( !less(data[child], p) )
165 move(data[child], hole);
167 child = first_child(hole);
174 void move(const Pair &p, int i) {
180 /// \brief Insert a pair of item and priority into the heap.
182 /// Adds \c p.first to the heap with priority \c p.second.
183 /// \param p The pair to insert.
184 void push(const Pair &p) {
190 /// \brief Insert an item into the heap with the given heap.
192 /// Adds \c i to the heap with priority \c p.
193 /// \param i The item to insert.
194 /// \param p The priority of the item.
195 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
197 /// \brief Returns the item with minimum priority relative to \c Compare.
199 /// This method returns the item with minimum priority relative to \c
201 /// \pre The heap must be nonempty.
202 Item top() const { return data[0].first; }
204 /// \brief Returns the minimum priority relative to \c Compare.
206 /// It returns the minimum priority relative to \c Compare.
207 /// \pre The heap must be nonempty.
208 Prio prio() const { return data[0].second; }
210 /// \brief Deletes the item with minimum priority relative to \c Compare.
212 /// This method deletes the item with minimum priority relative to \c
213 /// Compare from the heap.
214 /// \pre The heap must be non-empty.
216 int n = data.size()-1;
217 iim.set(data[0].first, POST_HEAP);
218 if (n>0) bubble_down(0, data[n], n);
222 /// \brief Deletes \c i from the heap.
224 /// This method deletes item \c i from the heap.
225 /// \param i The item to erase.
226 /// \pre The item should be in the heap.
227 void erase(const Item &i) {
229 int n = data.size()-1;
230 iim.set(data[h].first, POST_HEAP);
232 if( less(data[parent(h)], data[n]) )
233 bubble_down(h, data[n], n);
235 bubble_up(h, data[n]);
241 /// \brief Returns the priority of \c i.
243 /// This function returns the priority of item \c i.
244 /// \pre \c i must be in the heap.
245 /// \param i The item.
246 Prio operator[](const Item &i) const {
248 return data[idx].second;
251 /// \brief \c i gets to the heap with priority \c p independently
252 /// if \c i was already there.
254 /// This method calls \ref push(\c i, \c p) if \c i is not stored
255 /// in the heap and sets the priority of \c i to \c p otherwise.
256 /// \param i The item.
257 /// \param p The priority.
258 void set(const Item &i, const Prio &p) {
262 else if( comp(p, data[idx].second) )
263 bubble_up(idx, Pair(i,p));
265 bubble_down(idx, Pair(i,p), data.size());
268 /// \brief Decreases the priority of \c i to \c p.
270 /// This method decreases the priority of item \c i to \c p.
271 /// \pre \c i must be stored in the heap with priority at least \c
272 /// p relative to \c Compare.
273 /// \param i The item.
274 /// \param p The priority.
275 void decrease(const Item &i, const Prio &p) {
277 bubble_up(idx, Pair(i,p));
280 /// \brief Increases the priority of \c i to \c p.
282 /// This method sets the priority of item \c i to \c p.
283 /// \pre \c i must be stored in the heap with priority at most \c
284 /// p relative to \c Compare.
285 /// \param i The item.
286 /// \param p The priority.
287 void increase(const Item &i, const Prio &p) {
289 bubble_down(idx, Pair(i,p), data.size());
292 /// \brief Returns if \c item is in, has already been in, or has
293 /// never been in the heap.
295 /// This method returns PRE_HEAP if \c item has never been in the
296 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
297 /// otherwise. In the latter case it is possible that \c item will
298 /// get back to the heap again.
299 /// \param i The item.
300 State state(const Item &i) const {
306 /// \brief Sets the state of the \c item in the heap.
308 /// Sets the state of the \c item in the heap. It can be used to
309 /// manually clear the heap when it is important to achive the
310 /// better time complexity.
311 /// \param i The item.
312 /// \param st The state. It should not be \c IN_HEAP.
313 void state(const Item& i, State st) {
317 if (state(i) == IN_HEAP) erase(i);
325 /// \brief Replaces an item in the heap.
327 /// The \c i item is replaced with \c j item. The \c i item should
328 /// be in the heap, while the \c j should be out of the heap. The
329 /// \c i item will out of the heap and \c j will be in the heap
330 /// with the same prioriority as prevoiusly the \c i item.
331 void replace(const Item& i, const Item& j) {
342 #endif // LEMON_KARY_HEAP_H