alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@100
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@100
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
alpar@100
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@100
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@100
|
8 |
*
|
alpar@100
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@100
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@100
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@100
|
12 |
*
|
alpar@100
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@100
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@100
|
15 |
* purpose.
|
alpar@100
|
16 |
*
|
alpar@100
|
17 |
*/
|
alpar@100
|
18 |
|
kpeter@756
|
19 |
#ifndef LEMON_CONCEPTS_HEAP_H
|
kpeter@756
|
20 |
#define LEMON_CONCEPTS_HEAP_H
|
kpeter@756
|
21 |
|
alpar@100
|
22 |
///\ingroup concept
|
alpar@100
|
23 |
///\file
|
kpeter@113
|
24 |
///\brief The concept of heaps.
|
alpar@100
|
25 |
|
deba@220
|
26 |
#include <lemon/core.h>
|
deba@566
|
27 |
#include <lemon/concept_check.h>
|
alpar@100
|
28 |
|
alpar@100
|
29 |
namespace lemon {
|
kpeter@113
|
30 |
|
alpar@100
|
31 |
namespace concepts {
|
kpeter@113
|
32 |
|
alpar@100
|
33 |
/// \addtogroup concept
|
alpar@100
|
34 |
/// @{
|
alpar@100
|
35 |
|
kpeter@113
|
36 |
/// \brief The heap concept.
|
alpar@100
|
37 |
///
|
kpeter@756
|
38 |
/// This concept class describes the main interface of heaps.
|
kpeter@757
|
39 |
/// The various \ref heaps "heap structures" are efficient
|
kpeter@756
|
40 |
/// implementations of the abstract data type \e priority \e queue.
|
kpeter@756
|
41 |
/// They store items with specified values called \e priorities
|
kpeter@756
|
42 |
/// in such a way that finding and removing the item with minimum
|
kpeter@756
|
43 |
/// priority are efficient. The basic operations are adding and
|
kpeter@756
|
44 |
/// erasing items, changing the priority of an item, etc.
|
kpeter@606
|
45 |
///
|
kpeter@756
|
46 |
/// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
|
kpeter@756
|
47 |
/// Any class that conforms to this concept can be used easily in such
|
kpeter@756
|
48 |
/// algorithms.
|
kpeter@756
|
49 |
///
|
kpeter@756
|
50 |
/// \tparam PR Type of the priorities of the items.
|
kpeter@756
|
51 |
/// \tparam IM A read-writable item map with \c int values, used
|
kpeter@606
|
52 |
/// internally to handle the cross references.
|
kpeter@756
|
53 |
/// \tparam CMP A functor class for comparing the priorities.
|
kpeter@606
|
54 |
/// The default is \c std::less<PR>.
|
kpeter@606
|
55 |
#ifdef DOXYGEN
|
kpeter@756
|
56 |
template <typename PR, typename IM, typename CMP>
|
kpeter@606
|
57 |
#else
|
kpeter@756
|
58 |
template <typename PR, typename IM, typename CMP = std::less<PR> >
|
kpeter@606
|
59 |
#endif
|
alpar@100
|
60 |
class Heap {
|
alpar@100
|
61 |
public:
|
alpar@100
|
62 |
|
kpeter@606
|
63 |
/// Type of the item-int map.
|
kpeter@606
|
64 |
typedef IM ItemIntMap;
|
kpeter@606
|
65 |
/// Type of the priorities.
|
kpeter@606
|
66 |
typedef PR Prio;
|
kpeter@113
|
67 |
/// Type of the items stored in the heap.
|
kpeter@113
|
68 |
typedef typename ItemIntMap::Key Item;
|
alpar@100
|
69 |
|
kpeter@113
|
70 |
/// \brief Type to represent the states of the items.
|
alpar@100
|
71 |
///
|
kpeter@113
|
72 |
/// Each item has a state associated to it. It can be "in heap",
|
kpeter@756
|
73 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the
|
kpeter@756
|
74 |
/// heap's point of view, but may be useful to the user.
|
alpar@100
|
75 |
///
|
kpeter@606
|
76 |
/// The item-int map must be initialized in such way that it assigns
|
kpeter@606
|
77 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
|
alpar@100
|
78 |
enum State {
|
kpeter@631
|
79 |
IN_HEAP = 0, ///< = 0. The "in heap" state constant.
|
kpeter@756
|
80 |
PRE_HEAP = -1, ///< = -1. The "pre-heap" state constant.
|
kpeter@756
|
81 |
POST_HEAP = -2 ///< = -2. The "post-heap" state constant.
|
alpar@100
|
82 |
};
|
alpar@209
|
83 |
|
kpeter@756
|
84 |
/// \brief Constructor.
|
alpar@100
|
85 |
///
|
kpeter@756
|
86 |
/// Constructor.
|
kpeter@113
|
87 |
/// \param map A map that assigns \c int values to keys of type
|
kpeter@113
|
88 |
/// \c Item. It is used internally by the heap implementations to
|
kpeter@113
|
89 |
/// handle the cross references. The assigned value must be
|
kpeter@756
|
90 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item.
|
kpeter@883
|
91 |
#ifdef DOXYGEN
|
kpeter@113
|
92 |
explicit Heap(ItemIntMap &map) {}
|
kpeter@883
|
93 |
#else
|
kpeter@883
|
94 |
explicit Heap(ItemIntMap&) {}
|
alpar@956
|
95 |
#endif
|
kpeter@631
|
96 |
|
kpeter@756
|
97 |
/// \brief Constructor.
|
kpeter@756
|
98 |
///
|
kpeter@756
|
99 |
/// Constructor.
|
kpeter@756
|
100 |
/// \param map A map that assigns \c int values to keys of type
|
kpeter@756
|
101 |
/// \c Item. It is used internally by the heap implementations to
|
kpeter@756
|
102 |
/// handle the cross references. The assigned value must be
|
kpeter@756
|
103 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item.
|
kpeter@756
|
104 |
/// \param comp The function object used for comparing the priorities.
|
kpeter@883
|
105 |
#ifdef DOXYGEN
|
kpeter@756
|
106 |
explicit Heap(ItemIntMap &map, const CMP &comp) {}
|
kpeter@883
|
107 |
#else
|
kpeter@883
|
108 |
explicit Heap(ItemIntMap&, const CMP&) {}
|
alpar@956
|
109 |
#endif
|
alpar@100
|
110 |
|
alpar@100
|
111 |
/// \brief The number of items stored in the heap.
|
alpar@100
|
112 |
///
|
kpeter@756
|
113 |
/// This function returns the number of items stored in the heap.
|
alpar@100
|
114 |
int size() const { return 0; }
|
alpar@100
|
115 |
|
kpeter@756
|
116 |
/// \brief Check if the heap is empty.
|
alpar@100
|
117 |
///
|
kpeter@756
|
118 |
/// This function returns \c true if the heap is empty.
|
alpar@100
|
119 |
bool empty() const { return false; }
|
alpar@100
|
120 |
|
kpeter@756
|
121 |
/// \brief Make the heap empty.
|
alpar@100
|
122 |
///
|
kpeter@756
|
123 |
/// This functon makes the heap empty.
|
kpeter@756
|
124 |
/// It does not change the cross reference map. If you want to reuse
|
kpeter@756
|
125 |
/// a heap that is not surely empty, you should first clear it and
|
kpeter@756
|
126 |
/// then you should set the cross reference map to \c PRE_HEAP
|
kpeter@756
|
127 |
/// for each item.
|
kpeter@756
|
128 |
void clear() {}
|
alpar@100
|
129 |
|
kpeter@756
|
130 |
/// \brief Insert an item into the heap with the given priority.
|
alpar@209
|
131 |
///
|
kpeter@756
|
132 |
/// This function inserts the given item into the heap with the
|
kpeter@756
|
133 |
/// given priority.
|
alpar@100
|
134 |
/// \param i The item to insert.
|
alpar@100
|
135 |
/// \param p The priority of the item.
|
kpeter@756
|
136 |
/// \pre \e i must not be stored in the heap.
|
kpeter@883
|
137 |
#ifdef DOXYGEN
|
alpar@100
|
138 |
void push(const Item &i, const Prio &p) {}
|
kpeter@883
|
139 |
#else
|
kpeter@883
|
140 |
void push(const Item&, const Prio&) {}
|
alpar@956
|
141 |
#endif
|
alpar@100
|
142 |
|
kpeter@756
|
143 |
/// \brief Return the item having minimum priority.
|
alpar@100
|
144 |
///
|
kpeter@756
|
145 |
/// This function returns the item having minimum priority.
|
kpeter@113
|
146 |
/// \pre The heap must be non-empty.
|
kpeter@883
|
147 |
Item top() const { return Item(); }
|
alpar@100
|
148 |
|
kpeter@113
|
149 |
/// \brief The minimum priority.
|
alpar@100
|
150 |
///
|
kpeter@756
|
151 |
/// This function returns the minimum priority.
|
kpeter@113
|
152 |
/// \pre The heap must be non-empty.
|
kpeter@883
|
153 |
Prio prio() const { return Prio(); }
|
alpar@100
|
154 |
|
kpeter@756
|
155 |
/// \brief Remove the item having minimum priority.
|
alpar@100
|
156 |
///
|
kpeter@756
|
157 |
/// This function removes the item having minimum priority.
|
kpeter@113
|
158 |
/// \pre The heap must be non-empty.
|
alpar@100
|
159 |
void pop() {}
|
alpar@100
|
160 |
|
kpeter@756
|
161 |
/// \brief Remove the given item from the heap.
|
alpar@100
|
162 |
///
|
kpeter@756
|
163 |
/// This function removes the given item from the heap if it is
|
kpeter@756
|
164 |
/// already stored.
|
alpar@209
|
165 |
/// \param i The item to delete.
|
kpeter@756
|
166 |
/// \pre \e i must be in the heap.
|
kpeter@883
|
167 |
#ifdef DOXYGEN
|
alpar@100
|
168 |
void erase(const Item &i) {}
|
kpeter@883
|
169 |
#else
|
kpeter@883
|
170 |
void erase(const Item&) {}
|
alpar@956
|
171 |
#endif
|
alpar@100
|
172 |
|
kpeter@756
|
173 |
/// \brief The priority of the given item.
|
alpar@100
|
174 |
///
|
kpeter@756
|
175 |
/// This function returns the priority of the given item.
|
kpeter@606
|
176 |
/// \param i The item.
|
kpeter@756
|
177 |
/// \pre \e i must be in the heap.
|
kpeter@883
|
178 |
#ifdef DOXYGEN
|
alpar@100
|
179 |
Prio operator[](const Item &i) const {}
|
kpeter@883
|
180 |
#else
|
kpeter@883
|
181 |
Prio operator[](const Item&) const { return Prio(); }
|
alpar@956
|
182 |
#endif
|
alpar@100
|
183 |
|
kpeter@756
|
184 |
/// \brief Set the priority of an item or insert it, if it is
|
kpeter@113
|
185 |
/// not stored in the heap.
|
alpar@100
|
186 |
///
|
kpeter@113
|
187 |
/// This method sets the priority of the given item if it is
|
kpeter@756
|
188 |
/// already stored in the heap. Otherwise it inserts the given
|
kpeter@756
|
189 |
/// item into the heap with the given priority.
|
kpeter@113
|
190 |
///
|
alpar@100
|
191 |
/// \param i The item.
|
alpar@100
|
192 |
/// \param p The priority.
|
kpeter@883
|
193 |
#ifdef DOXYGEN
|
alpar@100
|
194 |
void set(const Item &i, const Prio &p) {}
|
kpeter@883
|
195 |
#else
|
kpeter@883
|
196 |
void set(const Item&, const Prio&) {}
|
alpar@956
|
197 |
#endif
|
alpar@209
|
198 |
|
kpeter@756
|
199 |
/// \brief Decrease the priority of an item to the given value.
|
alpar@100
|
200 |
///
|
kpeter@756
|
201 |
/// This function decreases the priority of an item to the given value.
|
alpar@100
|
202 |
/// \param i The item.
|
alpar@100
|
203 |
/// \param p The priority.
|
kpeter@756
|
204 |
/// \pre \e i must be stored in the heap with priority at least \e p.
|
kpeter@883
|
205 |
#ifdef DOXYGEN
|
alpar@100
|
206 |
void decrease(const Item &i, const Prio &p) {}
|
kpeter@883
|
207 |
#else
|
kpeter@883
|
208 |
void decrease(const Item&, const Prio&) {}
|
alpar@956
|
209 |
#endif
|
alpar@100
|
210 |
|
kpeter@756
|
211 |
/// \brief Increase the priority of an item to the given value.
|
alpar@100
|
212 |
///
|
kpeter@756
|
213 |
/// This function increases the priority of an item to the given value.
|
alpar@100
|
214 |
/// \param i The item.
|
alpar@100
|
215 |
/// \param p The priority.
|
kpeter@756
|
216 |
/// \pre \e i must be stored in the heap with priority at most \e p.
|
kpeter@883
|
217 |
#ifdef DOXYGEN
|
alpar@100
|
218 |
void increase(const Item &i, const Prio &p) {}
|
kpeter@883
|
219 |
#else
|
kpeter@883
|
220 |
void increase(const Item&, const Prio&) {}
|
alpar@956
|
221 |
#endif
|
alpar@100
|
222 |
|
kpeter@756
|
223 |
/// \brief Return the state of an item.
|
alpar@100
|
224 |
///
|
kpeter@113
|
225 |
/// This method returns \c PRE_HEAP if the given item has never
|
kpeter@113
|
226 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment,
|
kpeter@113
|
227 |
/// and \c POST_HEAP otherwise.
|
kpeter@113
|
228 |
/// In the latter case it is possible that the item will get back
|
kpeter@113
|
229 |
/// to the heap again.
|
alpar@100
|
230 |
/// \param i The item.
|
kpeter@883
|
231 |
#ifdef DOXYGEN
|
alpar@100
|
232 |
State state(const Item &i) const {}
|
kpeter@883
|
233 |
#else
|
kpeter@883
|
234 |
State state(const Item&) const { return PRE_HEAP; }
|
alpar@956
|
235 |
#endif
|
alpar@100
|
236 |
|
kpeter@756
|
237 |
/// \brief Set the state of an item in the heap.
|
alpar@100
|
238 |
///
|
kpeter@756
|
239 |
/// This function sets the state of the given item in the heap.
|
kpeter@756
|
240 |
/// It can be used to manually clear the heap when it is important
|
kpeter@756
|
241 |
/// to achive better time complexity.
|
alpar@100
|
242 |
/// \param i The item.
|
kpeter@113
|
243 |
/// \param st The state. It should not be \c IN_HEAP.
|
kpeter@883
|
244 |
#ifdef DOXYGEN
|
alpar@100
|
245 |
void state(const Item& i, State st) {}
|
kpeter@883
|
246 |
#else
|
kpeter@883
|
247 |
void state(const Item&, State) {}
|
alpar@956
|
248 |
#endif
|
alpar@100
|
249 |
|
alpar@100
|
250 |
|
alpar@100
|
251 |
template <typename _Heap>
|
alpar@100
|
252 |
struct Constraints {
|
alpar@100
|
253 |
public:
|
alpar@209
|
254 |
void constraints() {
|
alpar@209
|
255 |
typedef typename _Heap::Item OwnItem;
|
alpar@209
|
256 |
typedef typename _Heap::Prio OwnPrio;
|
alpar@209
|
257 |
typedef typename _Heap::State OwnState;
|
kpeter@113
|
258 |
|
alpar@209
|
259 |
Item item;
|
alpar@209
|
260 |
Prio prio;
|
alpar@209
|
261 |
item=Item();
|
alpar@209
|
262 |
prio=Prio();
|
alpar@209
|
263 |
ignore_unused_variable_warning(item);
|
alpar@209
|
264 |
ignore_unused_variable_warning(prio);
|
alpar@100
|
265 |
|
alpar@209
|
266 |
OwnItem own_item;
|
alpar@209
|
267 |
OwnPrio own_prio;
|
alpar@209
|
268 |
OwnState own_state;
|
alpar@209
|
269 |
own_item=Item();
|
alpar@209
|
270 |
own_prio=Prio();
|
alpar@209
|
271 |
ignore_unused_variable_warning(own_item);
|
alpar@209
|
272 |
ignore_unused_variable_warning(own_prio);
|
alpar@209
|
273 |
ignore_unused_variable_warning(own_state);
|
alpar@100
|
274 |
|
alpar@209
|
275 |
_Heap heap1(map);
|
alpar@209
|
276 |
_Heap heap2 = heap1;
|
alpar@209
|
277 |
ignore_unused_variable_warning(heap1);
|
alpar@209
|
278 |
ignore_unused_variable_warning(heap2);
|
alpar@100
|
279 |
|
alpar@209
|
280 |
int s = heap.size();
|
alpar@209
|
281 |
ignore_unused_variable_warning(s);
|
alpar@209
|
282 |
bool e = heap.empty();
|
alpar@209
|
283 |
ignore_unused_variable_warning(e);
|
alpar@100
|
284 |
|
alpar@209
|
285 |
prio = heap.prio();
|
alpar@209
|
286 |
item = heap.top();
|
alpar@209
|
287 |
prio = heap[item];
|
alpar@209
|
288 |
own_prio = heap.prio();
|
alpar@209
|
289 |
own_item = heap.top();
|
alpar@209
|
290 |
own_prio = heap[own_item];
|
alpar@100
|
291 |
|
alpar@209
|
292 |
heap.push(item, prio);
|
alpar@209
|
293 |
heap.push(own_item, own_prio);
|
alpar@209
|
294 |
heap.pop();
|
alpar@100
|
295 |
|
alpar@209
|
296 |
heap.set(item, prio);
|
alpar@209
|
297 |
heap.decrease(item, prio);
|
alpar@209
|
298 |
heap.increase(item, prio);
|
alpar@209
|
299 |
heap.set(own_item, own_prio);
|
alpar@209
|
300 |
heap.decrease(own_item, own_prio);
|
alpar@209
|
301 |
heap.increase(own_item, own_prio);
|
alpar@100
|
302 |
|
alpar@209
|
303 |
heap.erase(item);
|
alpar@209
|
304 |
heap.erase(own_item);
|
alpar@209
|
305 |
heap.clear();
|
alpar@100
|
306 |
|
alpar@209
|
307 |
own_state = heap.state(own_item);
|
alpar@209
|
308 |
heap.state(own_item, own_state);
|
alpar@100
|
309 |
|
alpar@209
|
310 |
own_state = _Heap::PRE_HEAP;
|
alpar@209
|
311 |
own_state = _Heap::IN_HEAP;
|
alpar@209
|
312 |
own_state = _Heap::POST_HEAP;
|
alpar@209
|
313 |
}
|
alpar@209
|
314 |
|
alpar@209
|
315 |
_Heap& heap;
|
alpar@209
|
316 |
ItemIntMap& map;
|
alpar@1125
|
317 |
Constraints() {}
|
alpar@100
|
318 |
};
|
alpar@100
|
319 |
};
|
alpar@100
|
320 |
|
alpar@100
|
321 |
/// @}
|
alpar@100
|
322 |
} // namespace lemon
|
alpar@100
|
323 |
}
|
deba@576
|
324 |
#endif
|