kpeter@750
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@748
|
2 |
*
|
kpeter@750
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@748
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
kpeter@748
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@748
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@748
|
8 |
*
|
kpeter@748
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@748
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@748
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@748
|
12 |
*
|
kpeter@748
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@748
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@748
|
15 |
* purpose.
|
kpeter@748
|
16 |
*
|
kpeter@748
|
17 |
*/
|
kpeter@748
|
18 |
|
kpeter@929
|
19 |
#ifndef LEMON_BINOMIAL_HEAP_H
|
kpeter@929
|
20 |
#define LEMON_BINOMIAL_HEAP_H
|
kpeter@748
|
21 |
|
kpeter@748
|
22 |
///\file
|
kpeter@750
|
23 |
///\ingroup heaps
|
kpeter@748
|
24 |
///\brief Binomial Heap implementation.
|
kpeter@748
|
25 |
|
kpeter@748
|
26 |
#include <vector>
|
kpeter@750
|
27 |
#include <utility>
|
kpeter@748
|
28 |
#include <functional>
|
kpeter@748
|
29 |
#include <lemon/math.h>
|
kpeter@748
|
30 |
#include <lemon/counter.h>
|
kpeter@748
|
31 |
|
kpeter@748
|
32 |
namespace lemon {
|
kpeter@748
|
33 |
|
kpeter@750
|
34 |
/// \ingroup heaps
|
kpeter@748
|
35 |
///
|
kpeter@750
|
36 |
///\brief Binomial heap data structure.
|
kpeter@748
|
37 |
///
|
kpeter@750
|
38 |
/// This class implements the \e binomial \e heap data structure.
|
kpeter@750
|
39 |
/// It fully conforms to the \ref concepts::Heap "heap concept".
|
kpeter@748
|
40 |
///
|
kpeter@750
|
41 |
/// The methods \ref increase() and \ref erase() are not efficient
|
kpeter@750
|
42 |
/// in a binomial heap. In case of many calls of these operations,
|
kpeter@750
|
43 |
/// it is better to use other heap structure, e.g. \ref BinHeap
|
kpeter@750
|
44 |
/// "binary heap".
|
kpeter@748
|
45 |
///
|
kpeter@750
|
46 |
/// \tparam PR Type of the priorities of the items.
|
kpeter@750
|
47 |
/// \tparam IM A read-writable item map with \c int values, used
|
kpeter@750
|
48 |
/// internally to handle the cross references.
|
kpeter@750
|
49 |
/// \tparam CMP A functor class for comparing the priorities.
|
kpeter@750
|
50 |
/// The default is \c std::less<PR>.
|
kpeter@748
|
51 |
#ifdef DOXYGEN
|
kpeter@750
|
52 |
template <typename PR, typename IM, typename CMP>
|
kpeter@748
|
53 |
#else
|
kpeter@750
|
54 |
template <typename PR, typename IM, typename CMP = std::less<PR> >
|
kpeter@748
|
55 |
#endif
|
kpeter@929
|
56 |
class BinomialHeap {
|
kpeter@748
|
57 |
public:
|
kpeter@750
|
58 |
/// Type of the item-int map.
|
kpeter@750
|
59 |
typedef IM ItemIntMap;
|
kpeter@750
|
60 |
/// Type of the priorities.
|
kpeter@750
|
61 |
typedef PR Prio;
|
kpeter@750
|
62 |
/// Type of the items stored in the heap.
|
kpeter@748
|
63 |
typedef typename ItemIntMap::Key Item;
|
kpeter@750
|
64 |
/// Functor type for comparing the priorities.
|
kpeter@750
|
65 |
typedef CMP Compare;
|
kpeter@750
|
66 |
|
kpeter@750
|
67 |
/// \brief Type to represent the states of the items.
|
kpeter@750
|
68 |
///
|
kpeter@750
|
69 |
/// Each item has a state associated to it. It can be "in heap",
|
kpeter@750
|
70 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the
|
kpeter@750
|
71 |
/// heap's point of view, but may be useful to the user.
|
kpeter@750
|
72 |
///
|
kpeter@750
|
73 |
/// The item-int map must be initialized in such way that it assigns
|
kpeter@750
|
74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
|
kpeter@750
|
75 |
enum State {
|
kpeter@750
|
76 |
IN_HEAP = 0, ///< = 0.
|
kpeter@750
|
77 |
PRE_HEAP = -1, ///< = -1.
|
kpeter@750
|
78 |
POST_HEAP = -2 ///< = -2.
|
kpeter@750
|
79 |
};
|
kpeter@748
|
80 |
|
kpeter@748
|
81 |
private:
|
kpeter@754
|
82 |
class Store;
|
kpeter@748
|
83 |
|
kpeter@754
|
84 |
std::vector<Store> _data;
|
kpeter@750
|
85 |
int _min, _head;
|
kpeter@750
|
86 |
ItemIntMap &_iim;
|
kpeter@750
|
87 |
Compare _comp;
|
kpeter@750
|
88 |
int _num_items;
|
kpeter@748
|
89 |
|
kpeter@748
|
90 |
public:
|
kpeter@750
|
91 |
/// \brief Constructor.
|
kpeter@750
|
92 |
///
|
kpeter@750
|
93 |
/// Constructor.
|
kpeter@750
|
94 |
/// \param map A map that assigns \c int values to the items.
|
kpeter@750
|
95 |
/// It is used internally to handle the cross references.
|
kpeter@750
|
96 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
|
kpeter@929
|
97 |
explicit BinomialHeap(ItemIntMap &map)
|
kpeter@750
|
98 |
: _min(0), _head(-1), _iim(map), _num_items(0) {}
|
kpeter@748
|
99 |
|
kpeter@750
|
100 |
/// \brief Constructor.
|
kpeter@748
|
101 |
///
|
kpeter@750
|
102 |
/// Constructor.
|
kpeter@750
|
103 |
/// \param map A map that assigns \c int values to the items.
|
kpeter@750
|
104 |
/// It is used internally to handle the cross references.
|
kpeter@750
|
105 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
|
kpeter@750
|
106 |
/// \param comp The function object used for comparing the priorities.
|
kpeter@929
|
107 |
BinomialHeap(ItemIntMap &map, const Compare &comp)
|
kpeter@750
|
108 |
: _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
|
kpeter@748
|
109 |
|
kpeter@748
|
110 |
/// \brief The number of items stored in the heap.
|
kpeter@748
|
111 |
///
|
kpeter@750
|
112 |
/// This function returns the number of items stored in the heap.
|
kpeter@750
|
113 |
int size() const { return _num_items; }
|
kpeter@748
|
114 |
|
kpeter@750
|
115 |
/// \brief Check if the heap is empty.
|
kpeter@748
|
116 |
///
|
kpeter@750
|
117 |
/// This function returns \c true if the heap is empty.
|
kpeter@750
|
118 |
bool empty() const { return _num_items==0; }
|
kpeter@748
|
119 |
|
kpeter@750
|
120 |
/// \brief Make the heap empty.
|
kpeter@748
|
121 |
///
|
kpeter@750
|
122 |
/// This functon makes the heap empty.
|
kpeter@750
|
123 |
/// It does not change the cross reference map. If you want to reuse
|
kpeter@750
|
124 |
/// a heap that is not surely empty, you should first clear it and
|
kpeter@750
|
125 |
/// then you should set the cross reference map to \c PRE_HEAP
|
kpeter@750
|
126 |
/// for each item.
|
kpeter@748
|
127 |
void clear() {
|
kpeter@750
|
128 |
_data.clear(); _min=0; _num_items=0; _head=-1;
|
kpeter@748
|
129 |
}
|
kpeter@748
|
130 |
|
kpeter@750
|
131 |
/// \brief Set the priority of an item or insert it, if it is
|
kpeter@750
|
132 |
/// not stored in the heap.
|
kpeter@748
|
133 |
///
|
kpeter@750
|
134 |
/// This method sets the priority of the given item if it is
|
kpeter@750
|
135 |
/// already stored in the heap. Otherwise it inserts the given
|
kpeter@750
|
136 |
/// item into the heap with the given priority.
|
kpeter@750
|
137 |
/// \param item The item.
|
kpeter@750
|
138 |
/// \param value The priority.
|
kpeter@748
|
139 |
void set (const Item& item, const Prio& value) {
|
kpeter@750
|
140 |
int i=_iim[item];
|
kpeter@750
|
141 |
if ( i >= 0 && _data[i].in ) {
|
kpeter@750
|
142 |
if ( _comp(value, _data[i].prio) ) decrease(item, value);
|
kpeter@750
|
143 |
if ( _comp(_data[i].prio, value) ) increase(item, value);
|
kpeter@748
|
144 |
} else push(item, value);
|
kpeter@748
|
145 |
}
|
kpeter@748
|
146 |
|
kpeter@750
|
147 |
/// \brief Insert an item into the heap with the given priority.
|
kpeter@748
|
148 |
///
|
kpeter@750
|
149 |
/// This function inserts the given item into the heap with the
|
kpeter@750
|
150 |
/// given priority.
|
kpeter@750
|
151 |
/// \param item The item to insert.
|
kpeter@750
|
152 |
/// \param value The priority of the item.
|
kpeter@750
|
153 |
/// \pre \e item must not be stored in the heap.
|
kpeter@748
|
154 |
void push (const Item& item, const Prio& value) {
|
kpeter@750
|
155 |
int i=_iim[item];
|
kpeter@748
|
156 |
if ( i<0 ) {
|
kpeter@750
|
157 |
int s=_data.size();
|
kpeter@750
|
158 |
_iim.set( item,s );
|
kpeter@754
|
159 |
Store st;
|
kpeter@748
|
160 |
st.name=item;
|
kpeter@754
|
161 |
st.prio=value;
|
kpeter@750
|
162 |
_data.push_back(st);
|
kpeter@748
|
163 |
i=s;
|
kpeter@748
|
164 |
}
|
kpeter@748
|
165 |
else {
|
kpeter@750
|
166 |
_data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
|
kpeter@750
|
167 |
_data[i].degree=0;
|
kpeter@750
|
168 |
_data[i].in=true;
|
kpeter@754
|
169 |
_data[i].prio=value;
|
kpeter@748
|
170 |
}
|
kpeter@748
|
171 |
|
kpeter@754
|
172 |
if( 0==_num_items ) {
|
kpeter@754
|
173 |
_head=i;
|
kpeter@754
|
174 |
_min=i;
|
kpeter@754
|
175 |
} else {
|
kpeter@754
|
176 |
merge(i);
|
kpeter@754
|
177 |
if( _comp(_data[i].prio, _data[_min].prio) ) _min=i;
|
kpeter@754
|
178 |
}
|
kpeter@750
|
179 |
++_num_items;
|
kpeter@748
|
180 |
}
|
kpeter@748
|
181 |
|
kpeter@750
|
182 |
/// \brief Return the item having minimum priority.
|
kpeter@748
|
183 |
///
|
kpeter@750
|
184 |
/// This function returns the item having minimum priority.
|
kpeter@750
|
185 |
/// \pre The heap must be non-empty.
|
kpeter@750
|
186 |
Item top() const { return _data[_min].name; }
|
kpeter@748
|
187 |
|
kpeter@750
|
188 |
/// \brief The minimum priority.
|
kpeter@748
|
189 |
///
|
kpeter@750
|
190 |
/// This function returns the minimum priority.
|
kpeter@750
|
191 |
/// \pre The heap must be non-empty.
|
kpeter@750
|
192 |
Prio prio() const { return _data[_min].prio; }
|
kpeter@748
|
193 |
|
kpeter@750
|
194 |
/// \brief The priority of the given item.
|
kpeter@748
|
195 |
///
|
kpeter@750
|
196 |
/// This function returns the priority of the given item.
|
kpeter@750
|
197 |
/// \param item The item.
|
kpeter@750
|
198 |
/// \pre \e item must be in the heap.
|
kpeter@748
|
199 |
const Prio& operator[](const Item& item) const {
|
kpeter@750
|
200 |
return _data[_iim[item]].prio;
|
kpeter@748
|
201 |
}
|
kpeter@748
|
202 |
|
kpeter@750
|
203 |
/// \brief Remove the item having minimum priority.
|
kpeter@748
|
204 |
///
|
kpeter@750
|
205 |
/// This function removes the item having minimum priority.
|
kpeter@748
|
206 |
/// \pre The heap must be non-empty.
|
kpeter@748
|
207 |
void pop() {
|
kpeter@750
|
208 |
_data[_min].in=false;
|
kpeter@748
|
209 |
|
kpeter@748
|
210 |
int head_child=-1;
|
kpeter@750
|
211 |
if ( _data[_min].child!=-1 ) {
|
kpeter@750
|
212 |
int child=_data[_min].child;
|
kpeter@748
|
213 |
int neighb;
|
kpeter@748
|
214 |
while( child!=-1 ) {
|
kpeter@750
|
215 |
neighb=_data[child].right_neighbor;
|
kpeter@750
|
216 |
_data[child].parent=-1;
|
kpeter@754
|
217 |
_data[child].right_neighbor=head_child;
|
kpeter@748
|
218 |
head_child=child;
|
kpeter@748
|
219 |
child=neighb;
|
kpeter@748
|
220 |
}
|
kpeter@748
|
221 |
}
|
kpeter@748
|
222 |
|
kpeter@754
|
223 |
if ( _data[_head].right_neighbor==-1 ) {
|
kpeter@754
|
224 |
// there was only one root
|
kpeter@750
|
225 |
_head=head_child;
|
kpeter@748
|
226 |
}
|
kpeter@748
|
227 |
else {
|
kpeter@754
|
228 |
// there were more roots
|
kpeter@750
|
229 |
if( _head!=_min ) { unlace(_min); }
|
kpeter@750
|
230 |
else { _head=_data[_head].right_neighbor; }
|
kpeter@748
|
231 |
merge(head_child);
|
kpeter@748
|
232 |
}
|
kpeter@750
|
233 |
_min=findMin();
|
kpeter@750
|
234 |
--_num_items;
|
kpeter@748
|
235 |
}
|
kpeter@748
|
236 |
|
kpeter@750
|
237 |
/// \brief Remove the given item from the heap.
|
kpeter@748
|
238 |
///
|
kpeter@750
|
239 |
/// This function removes the given item from the heap if it is
|
kpeter@750
|
240 |
/// already stored.
|
kpeter@750
|
241 |
/// \param item The item to delete.
|
kpeter@750
|
242 |
/// \pre \e item must be in the heap.
|
kpeter@748
|
243 |
void erase (const Item& item) {
|
kpeter@750
|
244 |
int i=_iim[item];
|
kpeter@750
|
245 |
if ( i >= 0 && _data[i].in ) {
|
kpeter@750
|
246 |
decrease( item, _data[_min].prio-1 );
|
kpeter@748
|
247 |
pop();
|
kpeter@748
|
248 |
}
|
kpeter@748
|
249 |
}
|
kpeter@748
|
250 |
|
kpeter@750
|
251 |
/// \brief Decrease the priority of an item to the given value.
|
kpeter@748
|
252 |
///
|
kpeter@750
|
253 |
/// This function decreases the priority of an item to the given value.
|
kpeter@750
|
254 |
/// \param item The item.
|
kpeter@750
|
255 |
/// \param value The priority.
|
kpeter@750
|
256 |
/// \pre \e item must be stored in the heap with priority at least \e value.
|
kpeter@748
|
257 |
void decrease (Item item, const Prio& value) {
|
kpeter@750
|
258 |
int i=_iim[item];
|
kpeter@754
|
259 |
int p=_data[i].parent;
|
kpeter@754
|
260 |
_data[i].prio=value;
|
alpar@956
|
261 |
|
kpeter@754
|
262 |
while( p!=-1 && _comp(value, _data[p].prio) ) {
|
kpeter@754
|
263 |
_data[i].name=_data[p].name;
|
kpeter@754
|
264 |
_data[i].prio=_data[p].prio;
|
kpeter@754
|
265 |
_data[p].name=item;
|
kpeter@754
|
266 |
_data[p].prio=value;
|
kpeter@754
|
267 |
_iim[_data[i].name]=i;
|
kpeter@754
|
268 |
i=p;
|
kpeter@754
|
269 |
p=_data[p].parent;
|
kpeter@748
|
270 |
}
|
kpeter@754
|
271 |
_iim[item]=i;
|
kpeter@754
|
272 |
if ( _comp(value, _data[_min].prio) ) _min=i;
|
kpeter@748
|
273 |
}
|
kpeter@748
|
274 |
|
kpeter@750
|
275 |
/// \brief Increase the priority of an item to the given value.
|
kpeter@748
|
276 |
///
|
kpeter@750
|
277 |
/// This function increases the priority of an item to the given value.
|
kpeter@750
|
278 |
/// \param item The item.
|
kpeter@750
|
279 |
/// \param value The priority.
|
kpeter@750
|
280 |
/// \pre \e item must be stored in the heap with priority at most \e value.
|
kpeter@748
|
281 |
void increase (Item item, const Prio& value) {
|
kpeter@748
|
282 |
erase(item);
|
kpeter@748
|
283 |
push(item, value);
|
kpeter@748
|
284 |
}
|
kpeter@748
|
285 |
|
kpeter@750
|
286 |
/// \brief Return the state of an item.
|
kpeter@748
|
287 |
///
|
kpeter@750
|
288 |
/// This method returns \c PRE_HEAP if the given item has never
|
kpeter@750
|
289 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment,
|
kpeter@750
|
290 |
/// and \c POST_HEAP otherwise.
|
kpeter@750
|
291 |
/// In the latter case it is possible that the item will get back
|
kpeter@750
|
292 |
/// to the heap again.
|
kpeter@750
|
293 |
/// \param item The item.
|
kpeter@748
|
294 |
State state(const Item &item) const {
|
kpeter@750
|
295 |
int i=_iim[item];
|
kpeter@748
|
296 |
if( i>=0 ) {
|
kpeter@750
|
297 |
if ( _data[i].in ) i=0;
|
kpeter@748
|
298 |
else i=-2;
|
kpeter@748
|
299 |
}
|
kpeter@748
|
300 |
return State(i);
|
kpeter@748
|
301 |
}
|
kpeter@748
|
302 |
|
kpeter@750
|
303 |
/// \brief Set the state of an item in the heap.
|
kpeter@748
|
304 |
///
|
kpeter@750
|
305 |
/// This function sets the state of the given item in the heap.
|
kpeter@750
|
306 |
/// It can be used to manually clear the heap when it is important
|
kpeter@750
|
307 |
/// to achive better time complexity.
|
kpeter@748
|
308 |
/// \param i The item.
|
kpeter@748
|
309 |
/// \param st The state. It should not be \c IN_HEAP.
|
kpeter@748
|
310 |
void state(const Item& i, State st) {
|
kpeter@748
|
311 |
switch (st) {
|
kpeter@748
|
312 |
case POST_HEAP:
|
kpeter@748
|
313 |
case PRE_HEAP:
|
kpeter@748
|
314 |
if (state(i) == IN_HEAP) {
|
kpeter@748
|
315 |
erase(i);
|
kpeter@748
|
316 |
}
|
kpeter@750
|
317 |
_iim[i] = st;
|
kpeter@748
|
318 |
break;
|
kpeter@748
|
319 |
case IN_HEAP:
|
kpeter@748
|
320 |
break;
|
kpeter@748
|
321 |
}
|
kpeter@748
|
322 |
}
|
kpeter@748
|
323 |
|
kpeter@748
|
324 |
private:
|
alpar@956
|
325 |
|
kpeter@754
|
326 |
// Find the minimum of the roots
|
kpeter@750
|
327 |
int findMin() {
|
kpeter@754
|
328 |
if( _head!=-1 ) {
|
kpeter@754
|
329 |
int min_loc=_head, min_val=_data[_head].prio;
|
kpeter@754
|
330 |
for( int x=_data[_head].right_neighbor; x!=-1;
|
kpeter@754
|
331 |
x=_data[x].right_neighbor ) {
|
kpeter@750
|
332 |
if( _comp( _data[x].prio,min_val ) ) {
|
kpeter@750
|
333 |
min_val=_data[x].prio;
|
kpeter@748
|
334 |
min_loc=x;
|
kpeter@748
|
335 |
}
|
kpeter@748
|
336 |
}
|
kpeter@754
|
337 |
return min_loc;
|
kpeter@748
|
338 |
}
|
kpeter@754
|
339 |
else return -1;
|
kpeter@748
|
340 |
}
|
kpeter@748
|
341 |
|
kpeter@754
|
342 |
// Merge the heap with another heap starting at the given position
|
kpeter@748
|
343 |
void merge(int a) {
|
kpeter@754
|
344 |
if( _head==-1 || a==-1 ) return;
|
kpeter@754
|
345 |
if( _data[a].right_neighbor==-1 &&
|
kpeter@754
|
346 |
_data[a].degree<=_data[_head].degree ) {
|
kpeter@754
|
347 |
_data[a].right_neighbor=_head;
|
kpeter@754
|
348 |
_head=a;
|
kpeter@754
|
349 |
} else {
|
kpeter@754
|
350 |
interleave(a);
|
kpeter@754
|
351 |
}
|
kpeter@754
|
352 |
if( _data[_head].right_neighbor==-1 ) return;
|
alpar@956
|
353 |
|
kpeter@750
|
354 |
int x=_head;
|
kpeter@754
|
355 |
int x_prev=-1, x_next=_data[x].right_neighbor;
|
kpeter@754
|
356 |
while( x_next!=-1 ) {
|
kpeter@754
|
357 |
if( _data[x].degree!=_data[x_next].degree ||
|
kpeter@754
|
358 |
( _data[x_next].right_neighbor!=-1 &&
|
kpeter@754
|
359 |
_data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
|
kpeter@754
|
360 |
x_prev=x;
|
kpeter@754
|
361 |
x=x_next;
|
kpeter@754
|
362 |
}
|
kpeter@754
|
363 |
else {
|
kpeter@754
|
364 |
if( _comp(_data[x_next].prio,_data[x].prio) ) {
|
kpeter@754
|
365 |
if( x_prev==-1 ) {
|
kpeter@754
|
366 |
_head=x_next;
|
kpeter@754
|
367 |
} else {
|
kpeter@754
|
368 |
_data[x_prev].right_neighbor=x_next;
|
kpeter@754
|
369 |
}
|
kpeter@754
|
370 |
fuse(x,x_next);
|
kpeter@748
|
371 |
x=x_next;
|
kpeter@748
|
372 |
}
|
kpeter@748
|
373 |
else {
|
kpeter@754
|
374 |
_data[x].right_neighbor=_data[x_next].right_neighbor;
|
kpeter@754
|
375 |
fuse(x_next,x);
|
kpeter@748
|
376 |
}
|
kpeter@748
|
377 |
}
|
kpeter@754
|
378 |
x_next=_data[x].right_neighbor;
|
kpeter@748
|
379 |
}
|
kpeter@748
|
380 |
}
|
kpeter@748
|
381 |
|
kpeter@754
|
382 |
// Interleave the elements of the given list into the list of the roots
|
kpeter@748
|
383 |
void interleave(int a) {
|
kpeter@754
|
384 |
int p=_head, q=a;
|
kpeter@754
|
385 |
int curr=_data.size();
|
kpeter@754
|
386 |
_data.push_back(Store());
|
alpar@956
|
387 |
|
kpeter@754
|
388 |
while( p!=-1 || q!=-1 ) {
|
kpeter@754
|
389 |
if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
|
kpeter@754
|
390 |
_data[curr].right_neighbor=p;
|
kpeter@754
|
391 |
curr=p;
|
kpeter@754
|
392 |
p=_data[p].right_neighbor;
|
kpeter@748
|
393 |
}
|
kpeter@748
|
394 |
else {
|
kpeter@754
|
395 |
_data[curr].right_neighbor=q;
|
kpeter@754
|
396 |
curr=q;
|
kpeter@754
|
397 |
q=_data[q].right_neighbor;
|
kpeter@748
|
398 |
}
|
kpeter@748
|
399 |
}
|
alpar@956
|
400 |
|
kpeter@754
|
401 |
_head=_data.back().right_neighbor;
|
kpeter@754
|
402 |
_data.pop_back();
|
kpeter@748
|
403 |
}
|
kpeter@748
|
404 |
|
kpeter@754
|
405 |
// Lace node a under node b
|
kpeter@748
|
406 |
void fuse(int a, int b) {
|
kpeter@750
|
407 |
_data[a].parent=b;
|
kpeter@750
|
408 |
_data[a].right_neighbor=_data[b].child;
|
kpeter@750
|
409 |
_data[b].child=a;
|
kpeter@748
|
410 |
|
kpeter@750
|
411 |
++_data[b].degree;
|
kpeter@748
|
412 |
}
|
kpeter@748
|
413 |
|
kpeter@754
|
414 |
// Unlace node a (if it has siblings)
|
kpeter@748
|
415 |
void unlace(int a) {
|
kpeter@750
|
416 |
int neighb=_data[a].right_neighbor;
|
kpeter@750
|
417 |
int other=_head;
|
kpeter@748
|
418 |
|
kpeter@750
|
419 |
while( _data[other].right_neighbor!=a )
|
kpeter@750
|
420 |
other=_data[other].right_neighbor;
|
kpeter@750
|
421 |
_data[other].right_neighbor=neighb;
|
kpeter@748
|
422 |
}
|
kpeter@748
|
423 |
|
kpeter@748
|
424 |
private:
|
kpeter@748
|
425 |
|
kpeter@754
|
426 |
class Store {
|
kpeter@929
|
427 |
friend class BinomialHeap;
|
kpeter@748
|
428 |
|
kpeter@748
|
429 |
Item name;
|
kpeter@748
|
430 |
int parent;
|
kpeter@748
|
431 |
int right_neighbor;
|
kpeter@748
|
432 |
int child;
|
kpeter@748
|
433 |
int degree;
|
kpeter@748
|
434 |
bool in;
|
kpeter@748
|
435 |
Prio prio;
|
kpeter@748
|
436 |
|
kpeter@754
|
437 |
Store() : parent(-1), right_neighbor(-1), child(-1), degree(0),
|
kpeter@754
|
438 |
in(true) {}
|
kpeter@748
|
439 |
};
|
kpeter@748
|
440 |
};
|
kpeter@748
|
441 |
|
kpeter@748
|
442 |
} //namespace lemon
|
kpeter@748
|
443 |
|
kpeter@929
|
444 |
#endif //LEMON_BINOMIAL_HEAP_H
|
kpeter@748
|
445 |
|