1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/pairing_heap.h Thu Nov 05 15:50:01 2009 +0100
1.3 @@ -0,0 +1,474 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2009
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_PAIRING_HEAP_H
1.23 +#define LEMON_PAIRING_HEAP_H
1.24 +
1.25 +///\file
1.26 +///\ingroup heaps
1.27 +///\brief Pairing heap implementation.
1.28 +
1.29 +#include <vector>
1.30 +#include <utility>
1.31 +#include <functional>
1.32 +#include <lemon/math.h>
1.33 +
1.34 +namespace lemon {
1.35 +
1.36 + /// \ingroup heaps
1.37 + ///
1.38 + ///\brief Pairing Heap.
1.39 + ///
1.40 + /// This class implements the \e pairing \e heap data structure.
1.41 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.42 + ///
1.43 + /// The methods \ref increase() and \ref erase() are not efficient
1.44 + /// in a pairing heap. In case of many calls of these operations,
1.45 + /// it is better to use other heap structure, e.g. \ref BinHeap
1.46 + /// "binary heap".
1.47 + ///
1.48 + /// \tparam PR Type of the priorities of the items.
1.49 + /// \tparam IM A read-writable item map with \c int values, used
1.50 + /// internally to handle the cross references.
1.51 + /// \tparam CMP A functor class for comparing the priorities.
1.52 + /// The default is \c std::less<PR>.
1.53 +#ifdef DOXYGEN
1.54 + template <typename PR, typename IM, typename CMP>
1.55 +#else
1.56 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.57 +#endif
1.58 + class PairingHeap {
1.59 + public:
1.60 + /// Type of the item-int map.
1.61 + typedef IM ItemIntMap;
1.62 + /// Type of the priorities.
1.63 + typedef PR Prio;
1.64 + /// Type of the items stored in the heap.
1.65 + typedef typename ItemIntMap::Key Item;
1.66 + /// Functor type for comparing the priorities.
1.67 + typedef CMP Compare;
1.68 +
1.69 + /// \brief Type to represent the states of the items.
1.70 + ///
1.71 + /// Each item has a state associated to it. It can be "in heap",
1.72 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.73 + /// heap's point of view, but may be useful to the user.
1.74 + ///
1.75 + /// The item-int map must be initialized in such way that it assigns
1.76 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.77 + enum State {
1.78 + IN_HEAP = 0, ///< = 0.
1.79 + PRE_HEAP = -1, ///< = -1.
1.80 + POST_HEAP = -2 ///< = -2.
1.81 + };
1.82 +
1.83 + private:
1.84 + class store;
1.85 +
1.86 + std::vector<store> _data;
1.87 + int _min;
1.88 + ItemIntMap &_iim;
1.89 + Compare _comp;
1.90 + int _num_items;
1.91 +
1.92 + public:
1.93 + /// \brief Constructor.
1.94 + ///
1.95 + /// Constructor.
1.96 + /// \param map A map that assigns \c int values to the items.
1.97 + /// It is used internally to handle the cross references.
1.98 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.99 + explicit PairingHeap(ItemIntMap &map)
1.100 + : _min(0), _iim(map), _num_items(0) {}
1.101 +
1.102 + /// \brief Constructor.
1.103 + ///
1.104 + /// Constructor.
1.105 + /// \param map A map that assigns \c int values to the items.
1.106 + /// It is used internally to handle the cross references.
1.107 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.108 + /// \param comp The function object used for comparing the priorities.
1.109 + PairingHeap(ItemIntMap &map, const Compare &comp)
1.110 + : _min(0), _iim(map), _comp(comp), _num_items(0) {}
1.111 +
1.112 + /// \brief The number of items stored in the heap.
1.113 + ///
1.114 + /// This function returns the number of items stored in the heap.
1.115 + int size() const { return _num_items; }
1.116 +
1.117 + /// \brief Check if the heap is empty.
1.118 + ///
1.119 + /// This function returns \c true if the heap is empty.
1.120 + bool empty() const { return _num_items==0; }
1.121 +
1.122 + /// \brief Make the heap empty.
1.123 + ///
1.124 + /// This functon makes the heap empty.
1.125 + /// It does not change the cross reference map. If you want to reuse
1.126 + /// a heap that is not surely empty, you should first clear it and
1.127 + /// then you should set the cross reference map to \c PRE_HEAP
1.128 + /// for each item.
1.129 + void clear() {
1.130 + _data.clear();
1.131 + _min = 0;
1.132 + _num_items = 0;
1.133 + }
1.134 +
1.135 + /// \brief Set the priority of an item or insert it, if it is
1.136 + /// not stored in the heap.
1.137 + ///
1.138 + /// This method sets the priority of the given item if it is
1.139 + /// already stored in the heap. Otherwise it inserts the given
1.140 + /// item into the heap with the given priority.
1.141 + /// \param item The item.
1.142 + /// \param value The priority.
1.143 + void set (const Item& item, const Prio& value) {
1.144 + int i=_iim[item];
1.145 + if ( i>=0 && _data[i].in ) {
1.146 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
1.147 + if ( _comp(_data[i].prio, value) ) increase(item, value);
1.148 + } else push(item, value);
1.149 + }
1.150 +
1.151 + /// \brief Insert an item into the heap with the given priority.
1.152 + ///
1.153 + /// This function inserts the given item into the heap with the
1.154 + /// given priority.
1.155 + /// \param item The item to insert.
1.156 + /// \param value The priority of the item.
1.157 + /// \pre \e item must not be stored in the heap.
1.158 + void push (const Item& item, const Prio& value) {
1.159 + int i=_iim[item];
1.160 + if( i<0 ) {
1.161 + int s=_data.size();
1.162 + _iim.set(item, s);
1.163 + store st;
1.164 + st.name=item;
1.165 + _data.push_back(st);
1.166 + i=s;
1.167 + } else {
1.168 + _data[i].parent=_data[i].child=-1;
1.169 + _data[i].left_child=false;
1.170 + _data[i].degree=0;
1.171 + _data[i].in=true;
1.172 + }
1.173 +
1.174 + _data[i].prio=value;
1.175 +
1.176 + if ( _num_items!=0 ) {
1.177 + if ( _comp( value, _data[_min].prio) ) {
1.178 + fuse(i,_min);
1.179 + _min=i;
1.180 + }
1.181 + else fuse(_min,i);
1.182 + }
1.183 + else _min=i;
1.184 +
1.185 + ++_num_items;
1.186 + }
1.187 +
1.188 + /// \brief Return the item having minimum priority.
1.189 + ///
1.190 + /// This function returns the item having minimum priority.
1.191 + /// \pre The heap must be non-empty.
1.192 + Item top() const { return _data[_min].name; }
1.193 +
1.194 + /// \brief The minimum priority.
1.195 + ///
1.196 + /// This function returns the minimum priority.
1.197 + /// \pre The heap must be non-empty.
1.198 + const Prio& prio() const { return _data[_min].prio; }
1.199 +
1.200 + /// \brief The priority of the given item.
1.201 + ///
1.202 + /// This function returns the priority of the given item.
1.203 + /// \param item The item.
1.204 + /// \pre \e item must be in the heap.
1.205 + const Prio& operator[](const Item& item) const {
1.206 + return _data[_iim[item]].prio;
1.207 + }
1.208 +
1.209 + /// \brief Remove the item having minimum priority.
1.210 + ///
1.211 + /// This function removes the item having minimum priority.
1.212 + /// \pre The heap must be non-empty.
1.213 + void pop() {
1.214 + std::vector<int> trees;
1.215 + int i=0, child_right = 0;
1.216 + _data[_min].in=false;
1.217 +
1.218 + if( -1!=_data[_min].child ) {
1.219 + i=_data[_min].child;
1.220 + trees.push_back(i);
1.221 + _data[i].parent = -1;
1.222 + _data[_min].child = -1;
1.223 +
1.224 + int ch=-1;
1.225 + while( _data[i].child!=-1 ) {
1.226 + ch=_data[i].child;
1.227 + if( _data[ch].left_child && i==_data[ch].parent ) {
1.228 + break;
1.229 + } else {
1.230 + if( _data[ch].left_child ) {
1.231 + child_right=_data[ch].parent;
1.232 + _data[ch].parent = i;
1.233 + --_data[i].degree;
1.234 + }
1.235 + else {
1.236 + child_right=ch;
1.237 + _data[i].child=-1;
1.238 + _data[i].degree=0;
1.239 + }
1.240 + _data[child_right].parent = -1;
1.241 + trees.push_back(child_right);
1.242 + i = child_right;
1.243 + }
1.244 + }
1.245 +
1.246 + int num_child = trees.size();
1.247 + int other;
1.248 + for( i=0; i<num_child-1; i+=2 ) {
1.249 + if ( !_comp(_data[trees[i]].prio, _data[trees[i+1]].prio) ) {
1.250 + other=trees[i];
1.251 + trees[i]=trees[i+1];
1.252 + trees[i+1]=other;
1.253 + }
1.254 + fuse( trees[i], trees[i+1] );
1.255 + }
1.256 +
1.257 + i = (0==(num_child % 2)) ? num_child-2 : num_child-1;
1.258 + while(i>=2) {
1.259 + if ( _comp(_data[trees[i]].prio, _data[trees[i-2]].prio) ) {
1.260 + other=trees[i];
1.261 + trees[i]=trees[i-2];
1.262 + trees[i-2]=other;
1.263 + }
1.264 + fuse( trees[i-2], trees[i] );
1.265 + i-=2;
1.266 + }
1.267 + _min = trees[0];
1.268 + }
1.269 + else {
1.270 + _min = _data[_min].child;
1.271 + }
1.272 +
1.273 + if (_min >= 0) _data[_min].left_child = false;
1.274 + --_num_items;
1.275 + }
1.276 +
1.277 + /// \brief Remove the given item from the heap.
1.278 + ///
1.279 + /// This function removes the given item from the heap if it is
1.280 + /// already stored.
1.281 + /// \param item The item to delete.
1.282 + /// \pre \e item must be in the heap.
1.283 + void erase (const Item& item) {
1.284 + int i=_iim[item];
1.285 + if ( i>=0 && _data[i].in ) {
1.286 + decrease( item, _data[_min].prio-1 );
1.287 + pop();
1.288 + }
1.289 + }
1.290 +
1.291 + /// \brief Decrease the priority of an item to the given value.
1.292 + ///
1.293 + /// This function decreases the priority of an item to the given value.
1.294 + /// \param item The item.
1.295 + /// \param value The priority.
1.296 + /// \pre \e item must be stored in the heap with priority at least \e value.
1.297 + void decrease (Item item, const Prio& value) {
1.298 + int i=_iim[item];
1.299 + _data[i].prio=value;
1.300 + int p=_data[i].parent;
1.301 +
1.302 + if( _data[i].left_child && i!=_data[p].child ) {
1.303 + p=_data[p].parent;
1.304 + }
1.305 +
1.306 + if ( p!=-1 && _comp(value,_data[p].prio) ) {
1.307 + cut(i,p);
1.308 + if ( _comp(_data[_min].prio,value) ) {
1.309 + fuse(_min,i);
1.310 + } else {
1.311 + fuse(i,_min);
1.312 + _min=i;
1.313 + }
1.314 + }
1.315 + }
1.316 +
1.317 + /// \brief Increase the priority of an item to the given value.
1.318 + ///
1.319 + /// This function increases the priority of an item to the given value.
1.320 + /// \param item The item.
1.321 + /// \param value The priority.
1.322 + /// \pre \e item must be stored in the heap with priority at most \e value.
1.323 + void increase (Item item, const Prio& value) {
1.324 + erase(item);
1.325 + push(item,value);
1.326 + }
1.327 +
1.328 + /// \brief Return the state of an item.
1.329 + ///
1.330 + /// This method returns \c PRE_HEAP if the given item has never
1.331 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.332 + /// and \c POST_HEAP otherwise.
1.333 + /// In the latter case it is possible that the item will get back
1.334 + /// to the heap again.
1.335 + /// \param item The item.
1.336 + State state(const Item &item) const {
1.337 + int i=_iim[item];
1.338 + if( i>=0 ) {
1.339 + if( _data[i].in ) i=0;
1.340 + else i=-2;
1.341 + }
1.342 + return State(i);
1.343 + }
1.344 +
1.345 + /// \brief Set the state of an item in the heap.
1.346 + ///
1.347 + /// This function sets the state of the given item in the heap.
1.348 + /// It can be used to manually clear the heap when it is important
1.349 + /// to achive better time complexity.
1.350 + /// \param i The item.
1.351 + /// \param st The state. It should not be \c IN_HEAP.
1.352 + void state(const Item& i, State st) {
1.353 + switch (st) {
1.354 + case POST_HEAP:
1.355 + case PRE_HEAP:
1.356 + if (state(i) == IN_HEAP) erase(i);
1.357 + _iim[i]=st;
1.358 + break;
1.359 + case IN_HEAP:
1.360 + break;
1.361 + }
1.362 + }
1.363 +
1.364 + private:
1.365 +
1.366 + void cut(int a, int b) {
1.367 + int child_a;
1.368 + switch (_data[a].degree) {
1.369 + case 2:
1.370 + child_a = _data[_data[a].child].parent;
1.371 + if( _data[a].left_child ) {
1.372 + _data[child_a].left_child=true;
1.373 + _data[b].child=child_a;
1.374 + _data[child_a].parent=_data[a].parent;
1.375 + }
1.376 + else {
1.377 + _data[child_a].left_child=false;
1.378 + _data[child_a].parent=b;
1.379 + if( a!=_data[b].child )
1.380 + _data[_data[b].child].parent=child_a;
1.381 + else
1.382 + _data[b].child=child_a;
1.383 + }
1.384 + --_data[a].degree;
1.385 + _data[_data[a].child].parent=a;
1.386 + break;
1.387 +
1.388 + case 1:
1.389 + child_a = _data[a].child;
1.390 + if( !_data[child_a].left_child ) {
1.391 + --_data[a].degree;
1.392 + if( _data[a].left_child ) {
1.393 + _data[child_a].left_child=true;
1.394 + _data[child_a].parent=_data[a].parent;
1.395 + _data[b].child=child_a;
1.396 + }
1.397 + else {
1.398 + _data[child_a].left_child=false;
1.399 + _data[child_a].parent=b;
1.400 + if( a!=_data[b].child )
1.401 + _data[_data[b].child].parent=child_a;
1.402 + else
1.403 + _data[b].child=child_a;
1.404 + }
1.405 + _data[a].child=-1;
1.406 + }
1.407 + else {
1.408 + --_data[b].degree;
1.409 + if( _data[a].left_child ) {
1.410 + _data[b].child =
1.411 + (1==_data[b].degree) ? _data[a].parent : -1;
1.412 + } else {
1.413 + if (1==_data[b].degree)
1.414 + _data[_data[b].child].parent=b;
1.415 + else
1.416 + _data[b].child=-1;
1.417 + }
1.418 + }
1.419 + break;
1.420 +
1.421 + case 0:
1.422 + --_data[b].degree;
1.423 + if( _data[a].left_child ) {
1.424 + _data[b].child =
1.425 + (0!=_data[b].degree) ? _data[a].parent : -1;
1.426 + } else {
1.427 + if( 0!=_data[b].degree )
1.428 + _data[_data[b].child].parent=b;
1.429 + else
1.430 + _data[b].child=-1;
1.431 + }
1.432 + break;
1.433 + }
1.434 + _data[a].parent=-1;
1.435 + _data[a].left_child=false;
1.436 + }
1.437 +
1.438 + void fuse(int a, int b) {
1.439 + int child_a = _data[a].child;
1.440 + int child_b = _data[b].child;
1.441 + _data[a].child=b;
1.442 + _data[b].parent=a;
1.443 + _data[b].left_child=true;
1.444 +
1.445 + if( -1!=child_a ) {
1.446 + _data[b].child=child_a;
1.447 + _data[child_a].parent=b;
1.448 + _data[child_a].left_child=false;
1.449 + ++_data[b].degree;
1.450 +
1.451 + if( -1!=child_b ) {
1.452 + _data[b].child=child_b;
1.453 + _data[child_b].parent=child_a;
1.454 + }
1.455 + }
1.456 + else { ++_data[a].degree; }
1.457 + }
1.458 +
1.459 + class store {
1.460 + friend class PairingHeap;
1.461 +
1.462 + Item name;
1.463 + int parent;
1.464 + int child;
1.465 + bool left_child;
1.466 + int degree;
1.467 + bool in;
1.468 + Prio prio;
1.469 +
1.470 + store() : parent(-1), child(-1), left_child(false), degree(0), in(true) {}
1.471 + };
1.472 + };
1.473 +
1.474 +} //namespace lemon
1.475 +
1.476 +#endif //LEMON_PAIRING_HEAP_H
1.477 +