kpeter@701
|
1 |
/* -*- C++ -*-
|
kpeter@701
|
2 |
*
|
kpeter@701
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
kpeter@701
|
4 |
*
|
kpeter@701
|
5 |
* Copyright (C) 2003-2008
|
kpeter@701
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@701
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@701
|
8 |
*
|
kpeter@701
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@701
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@701
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@701
|
12 |
*
|
kpeter@701
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@701
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@701
|
15 |
* purpose.
|
kpeter@701
|
16 |
*
|
kpeter@701
|
17 |
*/
|
kpeter@701
|
18 |
|
kpeter@701
|
19 |
#ifndef LEMON_KARY_HEAP_H
|
kpeter@701
|
20 |
#define LEMON_KARY_HEAP_H
|
kpeter@701
|
21 |
|
kpeter@701
|
22 |
///\ingroup auxdat
|
kpeter@701
|
23 |
///\file
|
kpeter@701
|
24 |
///\brief Kary Heap implementation.
|
kpeter@701
|
25 |
|
kpeter@701
|
26 |
#include <iostream>
|
kpeter@701
|
27 |
#include <vector>
|
kpeter@701
|
28 |
#include <utility>
|
kpeter@701
|
29 |
#include <functional>
|
kpeter@701
|
30 |
|
kpeter@701
|
31 |
namespace lemon {
|
kpeter@701
|
32 |
|
kpeter@701
|
33 |
///\ingroup auxdat
|
kpeter@701
|
34 |
///
|
kpeter@701
|
35 |
///\brief A Kary Heap implementation.
|
kpeter@701
|
36 |
///
|
kpeter@701
|
37 |
///This class implements the \e Kary \e heap data structure. A \e heap
|
kpeter@701
|
38 |
///is a data structure for storing items with specified values called \e
|
kpeter@701
|
39 |
///priorities in such a way that finding the item with minimum priority is
|
kpeter@701
|
40 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap
|
kpeter@701
|
41 |
///one can change the priority of an item, add or erase an item, etc.
|
kpeter@701
|
42 |
///
|
kpeter@701
|
43 |
///\param _Prio Type of the priority of the items.
|
kpeter@701
|
44 |
///\param _ItemIntMap A read and writable Item int map, used internally
|
kpeter@701
|
45 |
///to handle the cross references.
|
kpeter@701
|
46 |
///\param _Compare A class for the ordering of the priorities. The
|
kpeter@701
|
47 |
///default is \c std::less<_Prio>.
|
kpeter@701
|
48 |
///
|
kpeter@701
|
49 |
///\sa FibHeap
|
kpeter@701
|
50 |
///\sa Dijkstra
|
kpeter@701
|
51 |
///\author Dorian Batha
|
kpeter@701
|
52 |
|
kpeter@701
|
53 |
template <typename _Prio, typename _ItemIntMap,
|
kpeter@701
|
54 |
typename _Compare = std::less<_Prio> >
|
kpeter@701
|
55 |
|
kpeter@701
|
56 |
class KaryHeap {
|
kpeter@701
|
57 |
|
kpeter@701
|
58 |
public:
|
kpeter@701
|
59 |
///\e
|
kpeter@701
|
60 |
typedef _ItemIntMap ItemIntMap;
|
kpeter@701
|
61 |
///\e
|
kpeter@701
|
62 |
typedef _Prio Prio;
|
kpeter@701
|
63 |
///\e
|
kpeter@701
|
64 |
typedef typename ItemIntMap::Key Item;
|
kpeter@701
|
65 |
///\e
|
kpeter@701
|
66 |
typedef std::pair<Item,Prio> Pair;
|
kpeter@701
|
67 |
///\e
|
kpeter@701
|
68 |
typedef _Compare Compare;
|
kpeter@701
|
69 |
///\e
|
kpeter@701
|
70 |
|
kpeter@701
|
71 |
/// \brief Type to represent the items states.
|
kpeter@701
|
72 |
///
|
kpeter@701
|
73 |
/// Each Item element have a state associated to it. It may be "in heap",
|
kpeter@701
|
74 |
/// "pre heap" or "post heap". The latter two are indifferent from the
|
kpeter@701
|
75 |
/// heap's point of view, but may be useful to the user.
|
kpeter@701
|
76 |
///
|
kpeter@701
|
77 |
/// The ItemIntMap \e should be initialized in such way that it maps
|
kpeter@701
|
78 |
/// PRE_HEAP (-1) to any element to be put in the heap...
|
kpeter@701
|
79 |
enum State {
|
kpeter@701
|
80 |
IN_HEAP = 0,
|
kpeter@701
|
81 |
PRE_HEAP = -1,
|
kpeter@701
|
82 |
POST_HEAP = -2
|
kpeter@701
|
83 |
};
|
kpeter@701
|
84 |
|
kpeter@701
|
85 |
private:
|
kpeter@701
|
86 |
std::vector<Pair> data;
|
kpeter@701
|
87 |
Compare comp;
|
kpeter@701
|
88 |
ItemIntMap &iim;
|
kpeter@701
|
89 |
int K;
|
kpeter@701
|
90 |
|
kpeter@701
|
91 |
public:
|
kpeter@701
|
92 |
/// \brief The constructor.
|
kpeter@701
|
93 |
///
|
kpeter@701
|
94 |
/// The constructor.
|
kpeter@701
|
95 |
/// \param _iim should be given to the constructor, since it is used
|
kpeter@701
|
96 |
/// internally to handle the cross references. The value of the map
|
kpeter@701
|
97 |
/// should be PRE_HEAP (-1) for each element.
|
kpeter@701
|
98 |
explicit KaryHeap(ItemIntMap &_iim, const int &_K=32) : iim(_iim), K(_K) {}
|
kpeter@701
|
99 |
|
kpeter@701
|
100 |
/// \brief The constructor.
|
kpeter@701
|
101 |
///
|
kpeter@701
|
102 |
/// The constructor.
|
kpeter@701
|
103 |
/// \param _iim should be given to the constructor, since it is used
|
kpeter@701
|
104 |
/// internally to handle the cross references. The value of the map
|
kpeter@701
|
105 |
/// should be PRE_HEAP (-1) for each element.
|
kpeter@701
|
106 |
///
|
kpeter@701
|
107 |
/// \param _comp The comparator function object.
|
kpeter@701
|
108 |
KaryHeap(ItemIntMap &_iim, const Compare &_comp, const int &_K=32)
|
kpeter@701
|
109 |
: iim(_iim), comp(_comp), K(_K) {}
|
kpeter@701
|
110 |
|
kpeter@701
|
111 |
|
kpeter@701
|
112 |
/// The number of items stored in the heap.
|
kpeter@701
|
113 |
///
|
kpeter@701
|
114 |
/// \brief Returns the number of items stored in the heap.
|
kpeter@701
|
115 |
int size() const { return data.size(); }
|
kpeter@701
|
116 |
|
kpeter@701
|
117 |
/// \brief Checks if the heap stores no items.
|
kpeter@701
|
118 |
///
|
kpeter@701
|
119 |
/// Returns \c true if and only if the heap stores no items.
|
kpeter@701
|
120 |
bool empty() const { return data.empty(); }
|
kpeter@701
|
121 |
|
kpeter@701
|
122 |
/// \brief Make empty this heap.
|
kpeter@701
|
123 |
///
|
kpeter@701
|
124 |
/// Make empty this heap. It does not change the cross reference map.
|
kpeter@701
|
125 |
/// If you want to reuse what is not surely empty you should first clear
|
kpeter@701
|
126 |
/// the heap and after that you should set the cross reference map for
|
kpeter@701
|
127 |
/// each item to \c PRE_HEAP.
|
kpeter@701
|
128 |
void clear() { data.clear(); }
|
kpeter@701
|
129 |
|
kpeter@701
|
130 |
private:
|
kpeter@701
|
131 |
int parent(int i) { return (i-1)/K; }
|
kpeter@701
|
132 |
int first_child(int i) { return K*i+1; }
|
kpeter@701
|
133 |
|
kpeter@701
|
134 |
bool less(const Pair &p1, const Pair &p2) const {
|
kpeter@701
|
135 |
return comp(p1.second, p2.second);
|
kpeter@701
|
136 |
}
|
kpeter@701
|
137 |
|
kpeter@701
|
138 |
int find_min(const int child, const int length) {
|
kpeter@701
|
139 |
int min=child, i=1;
|
kpeter@701
|
140 |
while( i<K && child+i<length ) {
|
kpeter@701
|
141 |
if( less(data[child+i], data[min]) )
|
kpeter@701
|
142 |
min=child+i;
|
kpeter@701
|
143 |
++i;
|
kpeter@701
|
144 |
}
|
kpeter@701
|
145 |
return min;
|
kpeter@701
|
146 |
}
|
kpeter@701
|
147 |
|
kpeter@701
|
148 |
void bubble_up(int hole, Pair p) {
|
kpeter@701
|
149 |
int par = parent(hole);
|
kpeter@701
|
150 |
while( hole>0 && less(p,data[par]) ) {
|
kpeter@701
|
151 |
move(data[par],hole);
|
kpeter@701
|
152 |
hole = par;
|
kpeter@701
|
153 |
par = parent(hole);
|
kpeter@701
|
154 |
}
|
kpeter@701
|
155 |
move(p, hole);
|
kpeter@701
|
156 |
}
|
kpeter@701
|
157 |
|
kpeter@701
|
158 |
void bubble_down(int hole, Pair p, int length) {
|
kpeter@701
|
159 |
if( length>1 ) {
|
kpeter@701
|
160 |
int child = first_child(hole);
|
kpeter@701
|
161 |
while( child<length ) {
|
kpeter@701
|
162 |
child = find_min(child, length);
|
kpeter@701
|
163 |
if( !less(data[child], p) )
|
kpeter@701
|
164 |
goto ok;
|
kpeter@701
|
165 |
move(data[child], hole);
|
kpeter@701
|
166 |
hole = child;
|
kpeter@701
|
167 |
child = first_child(hole);
|
kpeter@701
|
168 |
}
|
kpeter@701
|
169 |
}
|
kpeter@701
|
170 |
ok:
|
kpeter@701
|
171 |
move(p, hole);
|
kpeter@701
|
172 |
}
|
kpeter@701
|
173 |
|
kpeter@701
|
174 |
void move(const Pair &p, int i) {
|
kpeter@701
|
175 |
data[i] = p;
|
kpeter@701
|
176 |
iim.set(p.first, i);
|
kpeter@701
|
177 |
}
|
kpeter@701
|
178 |
|
kpeter@701
|
179 |
public:
|
kpeter@701
|
180 |
/// \brief Insert a pair of item and priority into the heap.
|
kpeter@701
|
181 |
///
|
kpeter@701
|
182 |
/// Adds \c p.first to the heap with priority \c p.second.
|
kpeter@701
|
183 |
/// \param p The pair to insert.
|
kpeter@701
|
184 |
void push(const Pair &p) {
|
kpeter@701
|
185 |
int n = data.size();
|
kpeter@701
|
186 |
data.resize(n+1);
|
kpeter@701
|
187 |
bubble_up(n, p);
|
kpeter@701
|
188 |
}
|
kpeter@701
|
189 |
|
kpeter@701
|
190 |
/// \brief Insert an item into the heap with the given heap.
|
kpeter@701
|
191 |
///
|
kpeter@701
|
192 |
/// Adds \c i to the heap with priority \c p.
|
kpeter@701
|
193 |
/// \param i The item to insert.
|
kpeter@701
|
194 |
/// \param p The priority of the item.
|
kpeter@701
|
195 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
|
kpeter@701
|
196 |
|
kpeter@701
|
197 |
/// \brief Returns the item with minimum priority relative to \c Compare.
|
kpeter@701
|
198 |
///
|
kpeter@701
|
199 |
/// This method returns the item with minimum priority relative to \c
|
kpeter@701
|
200 |
/// Compare.
|
kpeter@701
|
201 |
/// \pre The heap must be nonempty.
|
kpeter@701
|
202 |
Item top() const { return data[0].first; }
|
kpeter@701
|
203 |
|
kpeter@701
|
204 |
/// \brief Returns the minimum priority relative to \c Compare.
|
kpeter@701
|
205 |
///
|
kpeter@701
|
206 |
/// It returns the minimum priority relative to \c Compare.
|
kpeter@701
|
207 |
/// \pre The heap must be nonempty.
|
kpeter@701
|
208 |
Prio prio() const { return data[0].second; }
|
kpeter@701
|
209 |
|
kpeter@701
|
210 |
/// \brief Deletes the item with minimum priority relative to \c Compare.
|
kpeter@701
|
211 |
///
|
kpeter@701
|
212 |
/// This method deletes the item with minimum priority relative to \c
|
kpeter@701
|
213 |
/// Compare from the heap.
|
kpeter@701
|
214 |
/// \pre The heap must be non-empty.
|
kpeter@701
|
215 |
void pop() {
|
kpeter@701
|
216 |
int n = data.size()-1;
|
kpeter@701
|
217 |
iim.set(data[0].first, POST_HEAP);
|
kpeter@701
|
218 |
if (n>0) bubble_down(0, data[n], n);
|
kpeter@701
|
219 |
data.pop_back();
|
kpeter@701
|
220 |
}
|
kpeter@701
|
221 |
|
kpeter@701
|
222 |
/// \brief Deletes \c i from the heap.
|
kpeter@701
|
223 |
///
|
kpeter@701
|
224 |
/// This method deletes item \c i from the heap.
|
kpeter@701
|
225 |
/// \param i The item to erase.
|
kpeter@701
|
226 |
/// \pre The item should be in the heap.
|
kpeter@701
|
227 |
void erase(const Item &i) {
|
kpeter@701
|
228 |
int h = iim[i];
|
kpeter@701
|
229 |
int n = data.size()-1;
|
kpeter@701
|
230 |
iim.set(data[h].first, POST_HEAP);
|
kpeter@701
|
231 |
if( h<n ) {
|
kpeter@701
|
232 |
if( less(data[parent(h)], data[n]) )
|
kpeter@701
|
233 |
bubble_down(h, data[n], n);
|
kpeter@701
|
234 |
else
|
kpeter@701
|
235 |
bubble_up(h, data[n]);
|
kpeter@701
|
236 |
}
|
kpeter@701
|
237 |
data.pop_back();
|
kpeter@701
|
238 |
}
|
kpeter@701
|
239 |
|
kpeter@701
|
240 |
|
kpeter@701
|
241 |
/// \brief Returns the priority of \c i.
|
kpeter@701
|
242 |
///
|
kpeter@701
|
243 |
/// This function returns the priority of item \c i.
|
kpeter@701
|
244 |
/// \pre \c i must be in the heap.
|
kpeter@701
|
245 |
/// \param i The item.
|
kpeter@701
|
246 |
Prio operator[](const Item &i) const {
|
kpeter@701
|
247 |
int idx = iim[i];
|
kpeter@701
|
248 |
return data[idx].second;
|
kpeter@701
|
249 |
}
|
kpeter@701
|
250 |
|
kpeter@701
|
251 |
/// \brief \c i gets to the heap with priority \c p independently
|
kpeter@701
|
252 |
/// if \c i was already there.
|
kpeter@701
|
253 |
///
|
kpeter@701
|
254 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored
|
kpeter@701
|
255 |
/// in the heap and sets the priority of \c i to \c p otherwise.
|
kpeter@701
|
256 |
/// \param i The item.
|
kpeter@701
|
257 |
/// \param p The priority.
|
kpeter@701
|
258 |
void set(const Item &i, const Prio &p) {
|
kpeter@701
|
259 |
int idx = iim[i];
|
kpeter@701
|
260 |
if( idx<0 )
|
kpeter@701
|
261 |
push(i,p);
|
kpeter@701
|
262 |
else if( comp(p, data[idx].second) )
|
kpeter@701
|
263 |
bubble_up(idx, Pair(i,p));
|
kpeter@701
|
264 |
else
|
kpeter@701
|
265 |
bubble_down(idx, Pair(i,p), data.size());
|
kpeter@701
|
266 |
}
|
kpeter@701
|
267 |
|
kpeter@701
|
268 |
/// \brief Decreases the priority of \c i to \c p.
|
kpeter@701
|
269 |
///
|
kpeter@701
|
270 |
/// This method decreases the priority of item \c i to \c p.
|
kpeter@701
|
271 |
/// \pre \c i must be stored in the heap with priority at least \c
|
kpeter@701
|
272 |
/// p relative to \c Compare.
|
kpeter@701
|
273 |
/// \param i The item.
|
kpeter@701
|
274 |
/// \param p The priority.
|
kpeter@701
|
275 |
void decrease(const Item &i, const Prio &p) {
|
kpeter@701
|
276 |
int idx = iim[i];
|
kpeter@701
|
277 |
bubble_up(idx, Pair(i,p));
|
kpeter@701
|
278 |
}
|
kpeter@701
|
279 |
|
kpeter@701
|
280 |
/// \brief Increases the priority of \c i to \c p.
|
kpeter@701
|
281 |
///
|
kpeter@701
|
282 |
/// This method sets the priority of item \c i to \c p.
|
kpeter@701
|
283 |
/// \pre \c i must be stored in the heap with priority at most \c
|
kpeter@701
|
284 |
/// p relative to \c Compare.
|
kpeter@701
|
285 |
/// \param i The item.
|
kpeter@701
|
286 |
/// \param p The priority.
|
kpeter@701
|
287 |
void increase(const Item &i, const Prio &p) {
|
kpeter@701
|
288 |
int idx = iim[i];
|
kpeter@701
|
289 |
bubble_down(idx, Pair(i,p), data.size());
|
kpeter@701
|
290 |
}
|
kpeter@701
|
291 |
|
kpeter@701
|
292 |
/// \brief Returns if \c item is in, has already been in, or has
|
kpeter@701
|
293 |
/// never been in the heap.
|
kpeter@701
|
294 |
///
|
kpeter@701
|
295 |
/// This method returns PRE_HEAP if \c item has never been in the
|
kpeter@701
|
296 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
|
kpeter@701
|
297 |
/// otherwise. In the latter case it is possible that \c item will
|
kpeter@701
|
298 |
/// get back to the heap again.
|
kpeter@701
|
299 |
/// \param i The item.
|
kpeter@701
|
300 |
State state(const Item &i) const {
|
kpeter@701
|
301 |
int s = iim[i];
|
kpeter@701
|
302 |
if (s>=0) s=0;
|
kpeter@701
|
303 |
return State(s);
|
kpeter@701
|
304 |
}
|
kpeter@701
|
305 |
|
kpeter@701
|
306 |
/// \brief Sets the state of the \c item in the heap.
|
kpeter@701
|
307 |
///
|
kpeter@701
|
308 |
/// Sets the state of the \c item in the heap. It can be used to
|
kpeter@701
|
309 |
/// manually clear the heap when it is important to achive the
|
kpeter@701
|
310 |
/// better time complexity.
|
kpeter@701
|
311 |
/// \param i The item.
|
kpeter@701
|
312 |
/// \param st The state. It should not be \c IN_HEAP.
|
kpeter@701
|
313 |
void state(const Item& i, State st) {
|
kpeter@701
|
314 |
switch (st) {
|
kpeter@701
|
315 |
case POST_HEAP:
|
kpeter@701
|
316 |
case PRE_HEAP:
|
kpeter@701
|
317 |
if (state(i) == IN_HEAP) erase(i);
|
kpeter@701
|
318 |
iim[i] = st;
|
kpeter@701
|
319 |
break;
|
kpeter@701
|
320 |
case IN_HEAP:
|
kpeter@701
|
321 |
break;
|
kpeter@701
|
322 |
}
|
kpeter@701
|
323 |
}
|
kpeter@701
|
324 |
|
kpeter@701
|
325 |
/// \brief Replaces an item in the heap.
|
kpeter@701
|
326 |
///
|
kpeter@701
|
327 |
/// The \c i item is replaced with \c j item. The \c i item should
|
kpeter@701
|
328 |
/// be in the heap, while the \c j should be out of the heap. The
|
kpeter@701
|
329 |
/// \c i item will out of the heap and \c j will be in the heap
|
kpeter@701
|
330 |
/// with the same prioriority as prevoiusly the \c i item.
|
kpeter@701
|
331 |
void replace(const Item& i, const Item& j) {
|
kpeter@701
|
332 |
int idx=iim[i];
|
kpeter@701
|
333 |
iim.set(i, iim[j]);
|
kpeter@701
|
334 |
iim.set(j, idx);
|
kpeter@701
|
335 |
data[idx].first=j;
|
kpeter@701
|
336 |
}
|
kpeter@701
|
337 |
|
kpeter@701
|
338 |
}; // class KaryHeap
|
kpeter@701
|
339 |
|
kpeter@701
|
340 |
} // namespace lemon
|
kpeter@701
|
341 |
|
kpeter@701
|
342 |
#endif // LEMON_KARY_HEAP_H
|