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_BINOM_HEAP_H
20 #define LEMON_BINOM_HEAP_H
24 ///\brief Binomial Heap implementation.
28 #include <lemon/math.h>
29 #include <lemon/counter.h>
35 ///\brief Binomial Heap.
37 ///This class implements the \e Binomial \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 ///The methods \ref increase and \ref erase are not efficient in a Binomial
44 ///heap. In case of many calls to these operations, it is better to use a
45 ///\ref BinHeap "binary heap".
47 ///\param _Prio Type of the priority of the items.
48 ///\param _ItemIntMap A read and writable Item int map, used internally
49 ///to handle the cross references.
50 ///\param _Compare A class for the ordering of the priorities. The
51 ///default is \c std::less<_Prio>.
55 ///\author Dorian Batha
58 template <typename _Prio,
62 template <typename _Prio,
64 typename _Compare = std::less<_Prio> >
68 typedef _ItemIntMap ItemIntMap;
70 typedef typename ItemIntMap::Key Item;
71 typedef std::pair<Item,Prio> Pair;
72 typedef _Compare Compare;
77 std::vector<store> container;
84 ///Status of the nodes
86 ///The node is in the heap
88 ///The node has never been in the heap
90 ///The node was in the heap but it got out of it
94 /// \brief The constructor
96 /// \c _iimap should be given to the constructor, since it is
97 /// used internally to handle the cross references.
98 explicit BinomHeap(ItemIntMap &_iimap)
99 : minimum(0), head(-1), iimap(_iimap), num_items() {}
101 /// \brief The constructor
103 /// \c _iimap should be given to the constructor, since it is used
104 /// internally to handle the cross references. \c _comp is an
105 /// object for ordering of the priorities.
106 BinomHeap(ItemIntMap &_iimap, const Compare &_comp)
107 : minimum(0), head(-1), iimap(_iimap), comp(_comp), num_items() {}
109 /// \brief The number of items stored in the heap.
111 /// Returns the number of items stored in the heap.
112 int size() const { return num_items; }
114 /// \brief Checks if the heap stores no items.
116 /// Returns \c true if and only if the heap stores no items.
117 bool empty() const { return num_items==0; }
119 /// \brief Make empty this heap.
121 /// Make empty this heap. It does not change the cross reference
122 /// map. If you want to reuse a heap what is not surely empty you
123 /// should first clear the heap and after that you should set the
124 /// cross reference map for each item to \c PRE_HEAP.
126 container.clear(); minimum=0; num_items=0; head=-1;
129 /// \brief \c item gets to the heap with priority \c value independently
130 /// if \c item was already there.
132 /// This method calls \ref push(\c item, \c value) if \c item is not
133 /// stored in the heap and it calls \ref decrease(\c item, \c value) or
134 /// \ref increase(\c item, \c value) otherwise.
135 void set (const Item& item, const Prio& value) {
137 if ( i >= 0 && container[i].in ) {
138 if ( comp(value, container[i].prio) ) decrease(item, value);
139 if ( comp(container[i].prio, value) ) increase(item, value);
140 } else push(item, value);
143 /// \brief Adds \c item to the heap with priority \c value.
145 /// Adds \c item to the heap with priority \c value.
146 /// \pre \c item must not be stored in the heap.
147 void push (const Item& item, const Prio& value) {
150 int s=container.size();
154 container.push_back(st);
158 container[i].parent=container[i].right_neighbor=container[i].child=-1;
159 container[i].degree=0;
160 container[i].in=true;
162 container[i].prio=value;
164 if( 0==num_items ) { head=i; minimum=i; }
167 minimum = find_min();
172 /// \brief Returns the item with minimum priority relative to \c Compare.
174 /// This method returns the item with minimum priority relative to \c
176 /// \pre The heap must be nonempty.
177 Item top() const { return container[minimum].name; }
179 /// \brief Returns the minimum priority relative to \c Compare.
181 /// It returns the minimum priority relative to \c Compare.
182 /// \pre The heap must be nonempty.
183 const Prio& prio() const { return container[minimum].prio; }
185 /// \brief Returns the priority of \c item.
187 /// It returns the priority of \c item.
188 /// \pre \c item must be in the heap.
189 const Prio& operator[](const Item& item) const {
190 return container[iimap[item]].prio;
193 /// \brief Deletes the item with minimum priority relative to \c Compare.
195 /// This method deletes the item with minimum priority relative to \c
196 /// Compare from the heap.
197 /// \pre The heap must be non-empty.
199 container[minimum].in=false;
202 if ( container[minimum].child!=-1 ) {
203 int child=container[minimum].child;
207 neighb=container[child].right_neighbor;
208 container[child].parent=-1;
209 container[child].right_neighbor=prev;
216 // The first case is that there are only one root.
217 if ( -1==container[head].right_neighbor ) {
220 // The case where there are more roots.
222 if( head!=minimum ) { unlace(minimum); }
223 else { head=container[head].right_neighbor; }
231 /// \brief Deletes \c item from the heap.
233 /// This method deletes \c item from the heap, if \c item was already
234 /// stored in the heap. It is quite inefficient in Binomial heaps.
235 void erase (const Item& item) {
237 if ( i >= 0 && container[i].in ) {
238 decrease( item, container[minimum].prio-1 );
243 /// \brief Decreases the priority of \c item to \c value.
245 /// This method decreases the priority of \c item to \c value.
246 /// \pre \c item must be stored in the heap with priority at least \c
247 /// value relative to \c Compare.
248 void decrease (Item item, const Prio& value) {
251 if( comp( value,container[i].prio ) ) {
252 container[i].prio=value;
254 int p_loc=container[i].parent, loc=i;
255 int parent, child, neighb;
257 while( -1!=p_loc && comp(container[loc].prio,container[p_loc].prio) ) {
259 // parent set for other loc_child
260 child=container[loc].child;
262 container[child].parent=p_loc;
263 child=container[child].right_neighbor;
266 // parent set for other p_loc_child
267 child=container[p_loc].child;
269 container[child].parent=loc;
270 child=container[child].right_neighbor;
273 child=container[p_loc].child;
274 container[p_loc].child=container[loc].child;
277 container[loc].child=child;
279 // left_neighb set for p_loc
280 if( container[loc].child!=p_loc ) {
281 while( container[child].right_neighbor!=loc )
282 child=container[child].right_neighbor;
283 container[child].right_neighbor=p_loc;
286 // left_neighb set for loc
287 parent=container[p_loc].parent;
288 if( -1!=parent ) child=container[parent].child;
292 while( container[child].right_neighbor!=p_loc )
293 child=container[child].right_neighbor;
294 container[child].right_neighbor=loc;
297 neighb=container[p_loc].right_neighbor;
298 container[p_loc].right_neighbor=container[loc].right_neighbor;
299 container[loc].right_neighbor=neighb;
301 container[p_loc].parent=loc;
302 container[loc].parent=parent;
304 if( -1!=parent && container[parent].child==p_loc )
305 container[parent].child=loc;
307 /*if new parent will be the first root*/
311 p_loc=container[loc].parent;
314 if( comp(value,container[minimum].prio) ) {
319 /// \brief Increases the priority of \c item to \c value.
321 /// This method sets the priority of \c item to \c value. Though
322 /// there is no precondition on the priority of \c item, this
323 /// method should be used only if it is indeed necessary to increase
324 /// (relative to \c Compare) the priority of \c item, because this
325 /// method is inefficient.
326 void increase (Item item, const Prio& value) {
332 /// \brief Returns if \c item is in, has already been in, or has never
333 /// been in the heap.
335 /// This method returns PRE_HEAP if \c item has never been in the
336 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
337 /// otherwise. In the latter case it is possible that \c item will
338 /// get back to the heap again.
339 State state(const Item &item) const {
342 if ( container[i].in ) i=0;
348 /// \brief Sets the state of the \c item in the heap.
350 /// Sets the state of the \c item in the heap. It can be used to
351 /// manually clear the heap when it is important to achive the
352 /// better time complexity.
353 /// \param i The item.
354 /// \param st The state. It should not be \c IN_HEAP.
355 void state(const Item& i, State st) {
359 if (state(i) == IN_HEAP) {
371 int min_loc=-1, min_val;
374 min_val=container[x].prio;
376 x=container[x].right_neighbor;
379 if( comp( container[x].prio,min_val ) ) {
380 min_val=container[x].prio;
383 x=container[x].right_neighbor;
394 int x_prev=-1, x_next=container[x].right_neighbor;
395 while( -1!=x_next ) {
396 if( container[x].degree!=container[x_next].degree || ( -1!=container[x_next].right_neighbor && container[container[x_next].right_neighbor].degree==container[x].degree ) ) {
401 if( comp(container[x].prio,container[x_next].prio) ) {
402 container[x].right_neighbor=container[x_next].right_neighbor;
406 if( -1==x_prev ) { head=x_next; }
408 container[x_prev].right_neighbor=x_next;
414 x_next=container[x].right_neighbor;
419 void interleave(int a) {
420 int other=-1, head_other=-1;
422 while( -1!=a || -1!=head ) {
424 if( -1==head_other ) {
428 container[other].right_neighbor=head;
432 else if( -1==head ) {
433 if( -1==head_other ) {
437 container[other].right_neighbor=a;
442 if( container[a].degree<container[head].degree ) {
443 if( -1==head_other ) {
447 container[other].right_neighbor=a;
450 a=container[a].right_neighbor;
453 if( -1==head_other ) {
457 container[other].right_neighbor=head;
460 head=container[head].right_neighbor;
468 void fuse(int a, int b) {
469 container[a].parent=b;
470 container[a].right_neighbor=container[b].child;
471 container[b].child=a;
473 ++container[b].degree;
476 // It is invoked only if a has siblings.
478 int neighb=container[a].right_neighbor;
481 while( container[other].right_neighbor!=a )
482 other=container[other].right_neighbor;
483 container[other].right_neighbor=neighb;
489 friend class BinomHeap;
499 store() : parent(-1), right_neighbor(-1), child(-1), degree(0), in(true) {}
505 #endif //LEMON_BINOM_HEAP_H