1.1 --- a/doc/groups.dox Fri Jul 24 11:07:52 2009 +0200
1.2 +++ b/doc/groups.dox Fri Sep 25 09:06:32 2009 +0200
1.3 @@ -238,7 +238,36 @@
1.4 efficient to have e.g. the Dijkstra algorithm to store its result in
1.5 any kind of path structure.
1.6
1.7 -\sa lemon::concepts::Path
1.8 +\sa \ref concepts::Path "Path concept"
1.9 +*/
1.10 +
1.11 +/**
1.12 +@defgroup heaps Heap Structures
1.13 +@ingroup datas
1.14 +\brief %Heap structures implemented in LEMON.
1.15 +
1.16 +This group contains the heap structures implemented in LEMON.
1.17 +
1.18 +LEMON provides several heap classes. They are efficient implementations
1.19 +of the abstract data type \e priority \e queue. They store items with
1.20 +specified values called \e priorities in such a way that finding and
1.21 +removing the item with minimum priority are efficient.
1.22 +The basic operations are adding and erasing items, changing the priority
1.23 +of an item, etc.
1.24 +
1.25 +Heaps are crucial in several algorithms, such as Dijkstra and Prim.
1.26 +The heap implementations have the same interface, thus any of them can be
1.27 +used easily in such algorithms.
1.28 +
1.29 +\sa \ref concepts::Heap "Heap concept"
1.30 +*/
1.31 +
1.32 +/**
1.33 +@defgroup matrices Matrices
1.34 +@ingroup datas
1.35 +\brief Two dimensional data storages implemented in LEMON.
1.36 +
1.37 +This group contains two dimensional data storages implemented in LEMON.
1.38 */
1.39
1.40 /**
2.1 --- a/lemon/Makefile.am Fri Jul 24 11:07:52 2009 +0200
2.2 +++ b/lemon/Makefile.am Fri Sep 25 09:06:32 2009 +0200
2.3 @@ -57,8 +57,10 @@
2.4 lemon/adaptors.h \
2.5 lemon/arg_parser.h \
2.6 lemon/assert.h \
2.7 + lemon/bellman_ford.h \
2.8 lemon/bfs.h \
2.9 lemon/bin_heap.h \
2.10 + lemon/binom_heap.h \
2.11 lemon/bucket_heap.h \
2.12 lemon/cbc.h \
2.13 lemon/circulation.h \
2.14 @@ -78,12 +80,14 @@
2.15 lemon/error.h \
2.16 lemon/euler.h \
2.17 lemon/fib_heap.h \
2.18 + lemon/fourary_heap.h \
2.19 lemon/full_graph.h \
2.20 lemon/glpk.h \
2.21 lemon/gomory_hu.h \
2.22 lemon/graph_to_eps.h \
2.23 lemon/grid_graph.h \
2.24 lemon/hypercube_graph.h \
2.25 + lemon/kary_heap.h \
2.26 lemon/kruskal.h \
2.27 lemon/hao_orlin.h \
2.28 lemon/lgf_reader.h \
2.29 @@ -92,13 +96,13 @@
2.30 lemon/lp.h \
2.31 lemon/lp_base.h \
2.32 lemon/lp_skeleton.h \
2.33 - lemon/list_graph.h \
2.34 lemon/maps.h \
2.35 lemon/matching.h \
2.36 lemon/math.h \
2.37 lemon/min_cost_arborescence.h \
2.38 lemon/nauty_reader.h \
2.39 lemon/network_simplex.h \
2.40 + lemon/pairing_heap.h \
2.41 lemon/path.h \
2.42 lemon/preflow.h \
2.43 lemon/radix_heap.h \
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/lemon/bellman_ford.h Fri Sep 25 09:06:32 2009 +0200
3.3 @@ -0,0 +1,1100 @@
3.4 +/* -*- C++ -*-
3.5 + *
3.6 + * This file is a part of LEMON, a generic C++ optimization library
3.7 + *
3.8 + * Copyright (C) 2003-2008
3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
3.11 + *
3.12 + * Permission to use, modify and distribute this software is granted
3.13 + * provided that this copyright notice appears in all copies. For
3.14 + * precise terms see the accompanying LICENSE file.
3.15 + *
3.16 + * This software is provided "AS IS" with no warranty of any kind,
3.17 + * express or implied, and with no claim as to its suitability for any
3.18 + * purpose.
3.19 + *
3.20 + */
3.21 +
3.22 +#ifndef LEMON_BELLMAN_FORD_H
3.23 +#define LEMON_BELLMAN_FORD_H
3.24 +
3.25 +/// \ingroup shortest_path
3.26 +/// \file
3.27 +/// \brief Bellman-Ford algorithm.
3.28 +
3.29 +#include <lemon/bits/path_dump.h>
3.30 +#include <lemon/core.h>
3.31 +#include <lemon/error.h>
3.32 +#include <lemon/maps.h>
3.33 +#include <lemon/path.h>
3.34 +
3.35 +#include <limits>
3.36 +
3.37 +namespace lemon {
3.38 +
3.39 + /// \brief Default OperationTraits for the BellmanFord algorithm class.
3.40 + ///
3.41 + /// This operation traits class defines all computational operations
3.42 + /// and constants that are used in the Bellman-Ford algorithm.
3.43 + /// The default implementation is based on the \c numeric_limits class.
3.44 + /// If the numeric type does not have infinity value, then the maximum
3.45 + /// value is used as extremal infinity value.
3.46 + template <
3.47 + typename V,
3.48 + bool has_inf = std::numeric_limits<V>::has_infinity>
3.49 + struct BellmanFordDefaultOperationTraits {
3.50 + /// \e
3.51 + typedef V Value;
3.52 + /// \brief Gives back the zero value of the type.
3.53 + static Value zero() {
3.54 + return static_cast<Value>(0);
3.55 + }
3.56 + /// \brief Gives back the positive infinity value of the type.
3.57 + static Value infinity() {
3.58 + return std::numeric_limits<Value>::infinity();
3.59 + }
3.60 + /// \brief Gives back the sum of the given two elements.
3.61 + static Value plus(const Value& left, const Value& right) {
3.62 + return left + right;
3.63 + }
3.64 + /// \brief Gives back \c true only if the first value is less than
3.65 + /// the second.
3.66 + static bool less(const Value& left, const Value& right) {
3.67 + return left < right;
3.68 + }
3.69 + };
3.70 +
3.71 + template <typename V>
3.72 + struct BellmanFordDefaultOperationTraits<V, false> {
3.73 + typedef V Value;
3.74 + static Value zero() {
3.75 + return static_cast<Value>(0);
3.76 + }
3.77 + static Value infinity() {
3.78 + return std::numeric_limits<Value>::max();
3.79 + }
3.80 + static Value plus(const Value& left, const Value& right) {
3.81 + if (left == infinity() || right == infinity()) return infinity();
3.82 + return left + right;
3.83 + }
3.84 + static bool less(const Value& left, const Value& right) {
3.85 + return left < right;
3.86 + }
3.87 + };
3.88 +
3.89 + /// \brief Default traits class of BellmanFord class.
3.90 + ///
3.91 + /// Default traits class of BellmanFord class.
3.92 + /// \param GR The type of the digraph.
3.93 + /// \param LEN The type of the length map.
3.94 + template<typename GR, typename LEN>
3.95 + struct BellmanFordDefaultTraits {
3.96 + /// The type of the digraph the algorithm runs on.
3.97 + typedef GR Digraph;
3.98 +
3.99 + /// \brief The type of the map that stores the arc lengths.
3.100 + ///
3.101 + /// The type of the map that stores the arc lengths.
3.102 + /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
3.103 + typedef LEN LengthMap;
3.104 +
3.105 + /// The type of the arc lengths.
3.106 + typedef typename LEN::Value Value;
3.107 +
3.108 + /// \brief Operation traits for Bellman-Ford algorithm.
3.109 + ///
3.110 + /// It defines the used operations and the infinity value for the
3.111 + /// given \c Value type.
3.112 + /// \see BellmanFordDefaultOperationTraits
3.113 + typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
3.114 +
3.115 + /// \brief The type of the map that stores the last arcs of the
3.116 + /// shortest paths.
3.117 + ///
3.118 + /// The type of the map that stores the last
3.119 + /// arcs of the shortest paths.
3.120 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.121 + typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
3.122 +
3.123 + /// \brief Instantiates a \c PredMap.
3.124 + ///
3.125 + /// This function instantiates a \ref PredMap.
3.126 + /// \param g is the digraph to which we would like to define the
3.127 + /// \ref PredMap.
3.128 + static PredMap *createPredMap(const GR& g) {
3.129 + return new PredMap(g);
3.130 + }
3.131 +
3.132 + /// \brief The type of the map that stores the distances of the nodes.
3.133 + ///
3.134 + /// The type of the map that stores the distances of the nodes.
3.135 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.136 + typedef typename GR::template NodeMap<typename LEN::Value> DistMap;
3.137 +
3.138 + /// \brief Instantiates a \c DistMap.
3.139 + ///
3.140 + /// This function instantiates a \ref DistMap.
3.141 + /// \param g is the digraph to which we would like to define the
3.142 + /// \ref DistMap.
3.143 + static DistMap *createDistMap(const GR& g) {
3.144 + return new DistMap(g);
3.145 + }
3.146 +
3.147 + };
3.148 +
3.149 + /// \brief %BellmanFord algorithm class.
3.150 + ///
3.151 + /// \ingroup shortest_path
3.152 + /// This class provides an efficient implementation of the Bellman-Ford
3.153 + /// algorithm. The maximum time complexity of the algorithm is
3.154 + /// <tt>O(ne)</tt>.
3.155 + ///
3.156 + /// The Bellman-Ford algorithm solves the single-source shortest path
3.157 + /// problem when the arcs can have negative lengths, but the digraph
3.158 + /// should not contain directed cycles with negative total length.
3.159 + /// If all arc costs are non-negative, consider to use the Dijkstra
3.160 + /// algorithm instead, since it is more efficient.
3.161 + ///
3.162 + /// The arc lengths are passed to the algorithm using a
3.163 + /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
3.164 + /// kind of length. The type of the length values is determined by the
3.165 + /// \ref concepts::ReadMap::Value "Value" type of the length map.
3.166 + ///
3.167 + /// There is also a \ref bellmanFord() "function-type interface" for the
3.168 + /// Bellman-Ford algorithm, which is convenient in the simplier cases and
3.169 + /// it can be used easier.
3.170 + ///
3.171 + /// \tparam GR The type of the digraph the algorithm runs on.
3.172 + /// The default type is \ref ListDigraph.
3.173 + /// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies
3.174 + /// the lengths of the arcs. The default map type is
3.175 + /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
3.176 +#ifdef DOXYGEN
3.177 + template <typename GR, typename LEN, typename TR>
3.178 +#else
3.179 + template <typename GR=ListDigraph,
3.180 + typename LEN=typename GR::template ArcMap<int>,
3.181 + typename TR=BellmanFordDefaultTraits<GR,LEN> >
3.182 +#endif
3.183 + class BellmanFord {
3.184 + public:
3.185 +
3.186 + ///The type of the underlying digraph.
3.187 + typedef typename TR::Digraph Digraph;
3.188 +
3.189 + /// \brief The type of the arc lengths.
3.190 + typedef typename TR::LengthMap::Value Value;
3.191 + /// \brief The type of the map that stores the arc lengths.
3.192 + typedef typename TR::LengthMap LengthMap;
3.193 + /// \brief The type of the map that stores the last
3.194 + /// arcs of the shortest paths.
3.195 + typedef typename TR::PredMap PredMap;
3.196 + /// \brief The type of the map that stores the distances of the nodes.
3.197 + typedef typename TR::DistMap DistMap;
3.198 + /// The type of the paths.
3.199 + typedef PredMapPath<Digraph, PredMap> Path;
3.200 + ///\brief The \ref BellmanFordDefaultOperationTraits
3.201 + /// "operation traits class" of the algorithm.
3.202 + typedef typename TR::OperationTraits OperationTraits;
3.203 +
3.204 + ///The \ref BellmanFordDefaultTraits "traits class" of the algorithm.
3.205 + typedef TR Traits;
3.206 +
3.207 + private:
3.208 +
3.209 + typedef typename Digraph::Node Node;
3.210 + typedef typename Digraph::NodeIt NodeIt;
3.211 + typedef typename Digraph::Arc Arc;
3.212 + typedef typename Digraph::OutArcIt OutArcIt;
3.213 +
3.214 + // Pointer to the underlying digraph.
3.215 + const Digraph *_gr;
3.216 + // Pointer to the length map
3.217 + const LengthMap *_length;
3.218 + // Pointer to the map of predecessors arcs.
3.219 + PredMap *_pred;
3.220 + // Indicates if _pred is locally allocated (true) or not.
3.221 + bool _local_pred;
3.222 + // Pointer to the map of distances.
3.223 + DistMap *_dist;
3.224 + // Indicates if _dist is locally allocated (true) or not.
3.225 + bool _local_dist;
3.226 +
3.227 + typedef typename Digraph::template NodeMap<bool> MaskMap;
3.228 + MaskMap *_mask;
3.229 +
3.230 + std::vector<Node> _process;
3.231 +
3.232 + // Creates the maps if necessary.
3.233 + void create_maps() {
3.234 + if(!_pred) {
3.235 + _local_pred = true;
3.236 + _pred = Traits::createPredMap(*_gr);
3.237 + }
3.238 + if(!_dist) {
3.239 + _local_dist = true;
3.240 + _dist = Traits::createDistMap(*_gr);
3.241 + }
3.242 + _mask = new MaskMap(*_gr, false);
3.243 + }
3.244 +
3.245 + public :
3.246 +
3.247 + typedef BellmanFord Create;
3.248 +
3.249 + /// \name Named Template Parameters
3.250 +
3.251 + ///@{
3.252 +
3.253 + template <class T>
3.254 + struct SetPredMapTraits : public Traits {
3.255 + typedef T PredMap;
3.256 + static PredMap *createPredMap(const Digraph&) {
3.257 + LEMON_ASSERT(false, "PredMap is not initialized");
3.258 + return 0; // ignore warnings
3.259 + }
3.260 + };
3.261 +
3.262 + /// \brief \ref named-templ-param "Named parameter" for setting
3.263 + /// \c PredMap type.
3.264 + ///
3.265 + /// \ref named-templ-param "Named parameter" for setting
3.266 + /// \c PredMap type.
3.267 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.268 + template <class T>
3.269 + struct SetPredMap
3.270 + : public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > {
3.271 + typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create;
3.272 + };
3.273 +
3.274 + template <class T>
3.275 + struct SetDistMapTraits : public Traits {
3.276 + typedef T DistMap;
3.277 + static DistMap *createDistMap(const Digraph&) {
3.278 + LEMON_ASSERT(false, "DistMap is not initialized");
3.279 + return 0; // ignore warnings
3.280 + }
3.281 + };
3.282 +
3.283 + /// \brief \ref named-templ-param "Named parameter" for setting
3.284 + /// \c DistMap type.
3.285 + ///
3.286 + /// \ref named-templ-param "Named parameter" for setting
3.287 + /// \c DistMap type.
3.288 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.289 + template <class T>
3.290 + struct SetDistMap
3.291 + : public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > {
3.292 + typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create;
3.293 + };
3.294 +
3.295 + template <class T>
3.296 + struct SetOperationTraitsTraits : public Traits {
3.297 + typedef T OperationTraits;
3.298 + };
3.299 +
3.300 + /// \brief \ref named-templ-param "Named parameter" for setting
3.301 + /// \c OperationTraits type.
3.302 + ///
3.303 + /// \ref named-templ-param "Named parameter" for setting
3.304 + /// \c OperationTraits type.
3.305 + /// For more information see \ref BellmanFordDefaultOperationTraits.
3.306 + template <class T>
3.307 + struct SetOperationTraits
3.308 + : public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > {
3.309 + typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> >
3.310 + Create;
3.311 + };
3.312 +
3.313 + ///@}
3.314 +
3.315 + protected:
3.316 +
3.317 + BellmanFord() {}
3.318 +
3.319 + public:
3.320 +
3.321 + /// \brief Constructor.
3.322 + ///
3.323 + /// Constructor.
3.324 + /// \param g The digraph the algorithm runs on.
3.325 + /// \param length The length map used by the algorithm.
3.326 + BellmanFord(const Digraph& g, const LengthMap& length) :
3.327 + _gr(&g), _length(&length),
3.328 + _pred(0), _local_pred(false),
3.329 + _dist(0), _local_dist(false), _mask(0) {}
3.330 +
3.331 + ///Destructor.
3.332 + ~BellmanFord() {
3.333 + if(_local_pred) delete _pred;
3.334 + if(_local_dist) delete _dist;
3.335 + if(_mask) delete _mask;
3.336 + }
3.337 +
3.338 + /// \brief Sets the length map.
3.339 + ///
3.340 + /// Sets the length map.
3.341 + /// \return <tt>(*this)</tt>
3.342 + BellmanFord &lengthMap(const LengthMap &map) {
3.343 + _length = ↦
3.344 + return *this;
3.345 + }
3.346 +
3.347 + /// \brief Sets the map that stores the predecessor arcs.
3.348 + ///
3.349 + /// Sets the map that stores the predecessor arcs.
3.350 + /// If you don't use this function before calling \ref run()
3.351 + /// or \ref init(), an instance will be allocated automatically.
3.352 + /// The destructor deallocates this automatically allocated map,
3.353 + /// of course.
3.354 + /// \return <tt>(*this)</tt>
3.355 + BellmanFord &predMap(PredMap &map) {
3.356 + if(_local_pred) {
3.357 + delete _pred;
3.358 + _local_pred=false;
3.359 + }
3.360 + _pred = ↦
3.361 + return *this;
3.362 + }
3.363 +
3.364 + /// \brief Sets the map that stores the distances of the nodes.
3.365 + ///
3.366 + /// Sets the map that stores the distances of the nodes calculated
3.367 + /// by the algorithm.
3.368 + /// If you don't use this function before calling \ref run()
3.369 + /// or \ref init(), an instance will be allocated automatically.
3.370 + /// The destructor deallocates this automatically allocated map,
3.371 + /// of course.
3.372 + /// \return <tt>(*this)</tt>
3.373 + BellmanFord &distMap(DistMap &map) {
3.374 + if(_local_dist) {
3.375 + delete _dist;
3.376 + _local_dist=false;
3.377 + }
3.378 + _dist = ↦
3.379 + return *this;
3.380 + }
3.381 +
3.382 + /// \name Execution Control
3.383 + /// The simplest way to execute the Bellman-Ford algorithm is to use
3.384 + /// one of the member functions called \ref run().\n
3.385 + /// If you need better control on the execution, you have to call
3.386 + /// \ref init() first, then you can add several source nodes
3.387 + /// with \ref addSource(). Finally the actual path computation can be
3.388 + /// performed with \ref start(), \ref checkedStart() or
3.389 + /// \ref limitedStart().
3.390 +
3.391 + ///@{
3.392 +
3.393 + /// \brief Initializes the internal data structures.
3.394 + ///
3.395 + /// Initializes the internal data structures. The optional parameter
3.396 + /// is the initial distance of each node.
3.397 + void init(const Value value = OperationTraits::infinity()) {
3.398 + create_maps();
3.399 + for (NodeIt it(*_gr); it != INVALID; ++it) {
3.400 + _pred->set(it, INVALID);
3.401 + _dist->set(it, value);
3.402 + }
3.403 + _process.clear();
3.404 + if (OperationTraits::less(value, OperationTraits::infinity())) {
3.405 + for (NodeIt it(*_gr); it != INVALID; ++it) {
3.406 + _process.push_back(it);
3.407 + _mask->set(it, true);
3.408 + }
3.409 + }
3.410 + }
3.411 +
3.412 + /// \brief Adds a new source node.
3.413 + ///
3.414 + /// This function adds a new source node. The optional second parameter
3.415 + /// is the initial distance of the node.
3.416 + void addSource(Node source, Value dst = OperationTraits::zero()) {
3.417 + _dist->set(source, dst);
3.418 + if (!(*_mask)[source]) {
3.419 + _process.push_back(source);
3.420 + _mask->set(source, true);
3.421 + }
3.422 + }
3.423 +
3.424 + /// \brief Executes one round from the Bellman-Ford algorithm.
3.425 + ///
3.426 + /// If the algoritm calculated the distances in the previous round
3.427 + /// exactly for the paths of at most \c k arcs, then this function
3.428 + /// will calculate the distances exactly for the paths of at most
3.429 + /// <tt>k+1</tt> arcs. Performing \c k iterations using this function
3.430 + /// calculates the shortest path distances exactly for the paths
3.431 + /// consisting of at most \c k arcs.
3.432 + ///
3.433 + /// \warning The paths with limited arc number cannot be retrieved
3.434 + /// easily with \ref path() or \ref predArc() functions. If you also
3.435 + /// need the shortest paths and not only the distances, you should
3.436 + /// store the \ref predMap() "predecessor map" after each iteration
3.437 + /// and build the path manually.
3.438 + ///
3.439 + /// \return \c true when the algorithm have not found more shorter
3.440 + /// paths.
3.441 + ///
3.442 + /// \see ActiveIt
3.443 + bool processNextRound() {
3.444 + for (int i = 0; i < int(_process.size()); ++i) {
3.445 + _mask->set(_process[i], false);
3.446 + }
3.447 + std::vector<Node> nextProcess;
3.448 + std::vector<Value> values(_process.size());
3.449 + for (int i = 0; i < int(_process.size()); ++i) {
3.450 + values[i] = (*_dist)[_process[i]];
3.451 + }
3.452 + for (int i = 0; i < int(_process.size()); ++i) {
3.453 + for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
3.454 + Node target = _gr->target(it);
3.455 + Value relaxed = OperationTraits::plus(values[i], (*_length)[it]);
3.456 + if (OperationTraits::less(relaxed, (*_dist)[target])) {
3.457 + _pred->set(target, it);
3.458 + _dist->set(target, relaxed);
3.459 + if (!(*_mask)[target]) {
3.460 + _mask->set(target, true);
3.461 + nextProcess.push_back(target);
3.462 + }
3.463 + }
3.464 + }
3.465 + }
3.466 + _process.swap(nextProcess);
3.467 + return _process.empty();
3.468 + }
3.469 +
3.470 + /// \brief Executes one weak round from the Bellman-Ford algorithm.
3.471 + ///
3.472 + /// If the algorithm calculated the distances in the previous round
3.473 + /// at least for the paths of at most \c k arcs, then this function
3.474 + /// will calculate the distances at least for the paths of at most
3.475 + /// <tt>k+1</tt> arcs.
3.476 + /// This function does not make it possible to calculate the shortest
3.477 + /// path distances exactly for paths consisting of at most \c k arcs,
3.478 + /// this is why it is called weak round.
3.479 + ///
3.480 + /// \return \c true when the algorithm have not found more shorter
3.481 + /// paths.
3.482 + ///
3.483 + /// \see ActiveIt
3.484 + bool processNextWeakRound() {
3.485 + for (int i = 0; i < int(_process.size()); ++i) {
3.486 + _mask->set(_process[i], false);
3.487 + }
3.488 + std::vector<Node> nextProcess;
3.489 + for (int i = 0; i < int(_process.size()); ++i) {
3.490 + for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
3.491 + Node target = _gr->target(it);
3.492 + Value relaxed =
3.493 + OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]);
3.494 + if (OperationTraits::less(relaxed, (*_dist)[target])) {
3.495 + _pred->set(target, it);
3.496 + _dist->set(target, relaxed);
3.497 + if (!(*_mask)[target]) {
3.498 + _mask->set(target, true);
3.499 + nextProcess.push_back(target);
3.500 + }
3.501 + }
3.502 + }
3.503 + }
3.504 + _process.swap(nextProcess);
3.505 + return _process.empty();
3.506 + }
3.507 +
3.508 + /// \brief Executes the algorithm.
3.509 + ///
3.510 + /// Executes the algorithm.
3.511 + ///
3.512 + /// This method runs the Bellman-Ford algorithm from the root node(s)
3.513 + /// in order to compute the shortest path to each node.
3.514 + ///
3.515 + /// The algorithm computes
3.516 + /// - the shortest path tree (forest),
3.517 + /// - the distance of each node from the root(s).
3.518 + ///
3.519 + /// \pre init() must be called and at least one root node should be
3.520 + /// added with addSource() before using this function.
3.521 + void start() {
3.522 + int num = countNodes(*_gr) - 1;
3.523 + for (int i = 0; i < num; ++i) {
3.524 + if (processNextWeakRound()) break;
3.525 + }
3.526 + }
3.527 +
3.528 + /// \brief Executes the algorithm and checks the negative cycles.
3.529 + ///
3.530 + /// Executes the algorithm and checks the negative cycles.
3.531 + ///
3.532 + /// This method runs the Bellman-Ford algorithm from the root node(s)
3.533 + /// in order to compute the shortest path to each node and also checks
3.534 + /// if the digraph contains cycles with negative total length.
3.535 + ///
3.536 + /// The algorithm computes
3.537 + /// - the shortest path tree (forest),
3.538 + /// - the distance of each node from the root(s).
3.539 + ///
3.540 + /// \return \c false if there is a negative cycle in the digraph.
3.541 + ///
3.542 + /// \pre init() must be called and at least one root node should be
3.543 + /// added with addSource() before using this function.
3.544 + bool checkedStart() {
3.545 + int num = countNodes(*_gr);
3.546 + for (int i = 0; i < num; ++i) {
3.547 + if (processNextWeakRound()) return true;
3.548 + }
3.549 + return _process.empty();
3.550 + }
3.551 +
3.552 + /// \brief Executes the algorithm with arc number limit.
3.553 + ///
3.554 + /// Executes the algorithm with arc number limit.
3.555 + ///
3.556 + /// This method runs the Bellman-Ford algorithm from the root node(s)
3.557 + /// in order to compute the shortest path distance for each node
3.558 + /// using only the paths consisting of at most \c num arcs.
3.559 + ///
3.560 + /// The algorithm computes
3.561 + /// - the limited distance of each node from the root(s),
3.562 + /// - the predecessor arc for each node.
3.563 + ///
3.564 + /// \warning The paths with limited arc number cannot be retrieved
3.565 + /// easily with \ref path() or \ref predArc() functions. If you also
3.566 + /// need the shortest paths and not only the distances, you should
3.567 + /// store the \ref predMap() "predecessor map" after each iteration
3.568 + /// and build the path manually.
3.569 + ///
3.570 + /// \pre init() must be called and at least one root node should be
3.571 + /// added with addSource() before using this function.
3.572 + void limitedStart(int num) {
3.573 + for (int i = 0; i < num; ++i) {
3.574 + if (processNextRound()) break;
3.575 + }
3.576 + }
3.577 +
3.578 + /// \brief Runs the algorithm from the given root node.
3.579 + ///
3.580 + /// This method runs the Bellman-Ford algorithm from the given root
3.581 + /// node \c s in order to compute the shortest path to each node.
3.582 + ///
3.583 + /// The algorithm computes
3.584 + /// - the shortest path tree (forest),
3.585 + /// - the distance of each node from the root(s).
3.586 + ///
3.587 + /// \note bf.run(s) is just a shortcut of the following code.
3.588 + /// \code
3.589 + /// bf.init();
3.590 + /// bf.addSource(s);
3.591 + /// bf.start();
3.592 + /// \endcode
3.593 + void run(Node s) {
3.594 + init();
3.595 + addSource(s);
3.596 + start();
3.597 + }
3.598 +
3.599 + /// \brief Runs the algorithm from the given root node with arc
3.600 + /// number limit.
3.601 + ///
3.602 + /// This method runs the Bellman-Ford algorithm from the given root
3.603 + /// node \c s in order to compute the shortest path distance for each
3.604 + /// node using only the paths consisting of at most \c num arcs.
3.605 + ///
3.606 + /// The algorithm computes
3.607 + /// - the limited distance of each node from the root(s),
3.608 + /// - the predecessor arc for each node.
3.609 + ///
3.610 + /// \warning The paths with limited arc number cannot be retrieved
3.611 + /// easily with \ref path() or \ref predArc() functions. If you also
3.612 + /// need the shortest paths and not only the distances, you should
3.613 + /// store the \ref predMap() "predecessor map" after each iteration
3.614 + /// and build the path manually.
3.615 + ///
3.616 + /// \note bf.run(s, num) is just a shortcut of the following code.
3.617 + /// \code
3.618 + /// bf.init();
3.619 + /// bf.addSource(s);
3.620 + /// bf.limitedStart(num);
3.621 + /// \endcode
3.622 + void run(Node s, int num) {
3.623 + init();
3.624 + addSource(s);
3.625 + limitedStart(num);
3.626 + }
3.627 +
3.628 + ///@}
3.629 +
3.630 + /// \brief LEMON iterator for getting the active nodes.
3.631 + ///
3.632 + /// This class provides a common style LEMON iterator that traverses
3.633 + /// the active nodes of the Bellman-Ford algorithm after the last
3.634 + /// phase. These nodes should be checked in the next phase to
3.635 + /// find augmenting arcs outgoing from them.
3.636 + class ActiveIt {
3.637 + public:
3.638 +
3.639 + /// \brief Constructor.
3.640 + ///
3.641 + /// Constructor for getting the active nodes of the given BellmanFord
3.642 + /// instance.
3.643 + ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm)
3.644 + {
3.645 + _index = _algorithm->_process.size() - 1;
3.646 + }
3.647 +
3.648 + /// \brief Invalid constructor.
3.649 + ///
3.650 + /// Invalid constructor.
3.651 + ActiveIt(Invalid) : _algorithm(0), _index(-1) {}
3.652 +
3.653 + /// \brief Conversion to \c Node.
3.654 + ///
3.655 + /// Conversion to \c Node.
3.656 + operator Node() const {
3.657 + return _index >= 0 ? _algorithm->_process[_index] : INVALID;
3.658 + }
3.659 +
3.660 + /// \brief Increment operator.
3.661 + ///
3.662 + /// Increment operator.
3.663 + ActiveIt& operator++() {
3.664 + --_index;
3.665 + return *this;
3.666 + }
3.667 +
3.668 + bool operator==(const ActiveIt& it) const {
3.669 + return static_cast<Node>(*this) == static_cast<Node>(it);
3.670 + }
3.671 + bool operator!=(const ActiveIt& it) const {
3.672 + return static_cast<Node>(*this) != static_cast<Node>(it);
3.673 + }
3.674 + bool operator<(const ActiveIt& it) const {
3.675 + return static_cast<Node>(*this) < static_cast<Node>(it);
3.676 + }
3.677 +
3.678 + private:
3.679 + const BellmanFord* _algorithm;
3.680 + int _index;
3.681 + };
3.682 +
3.683 + /// \name Query Functions
3.684 + /// The result of the Bellman-Ford algorithm can be obtained using these
3.685 + /// functions.\n
3.686 + /// Either \ref run() or \ref init() should be called before using them.
3.687 +
3.688 + ///@{
3.689 +
3.690 + /// \brief The shortest path to the given node.
3.691 + ///
3.692 + /// Gives back the shortest path to the given node from the root(s).
3.693 + ///
3.694 + /// \warning \c t should be reached from the root(s).
3.695 + ///
3.696 + /// \pre Either \ref run() or \ref init() must be called before
3.697 + /// using this function.
3.698 + Path path(Node t) const
3.699 + {
3.700 + return Path(*_gr, *_pred, t);
3.701 + }
3.702 +
3.703 + /// \brief The distance of the given node from the root(s).
3.704 + ///
3.705 + /// Returns the distance of the given node from the root(s).
3.706 + ///
3.707 + /// \warning If node \c v is not reached from the root(s), then
3.708 + /// the return value of this function is undefined.
3.709 + ///
3.710 + /// \pre Either \ref run() or \ref init() must be called before
3.711 + /// using this function.
3.712 + Value dist(Node v) const { return (*_dist)[v]; }
3.713 +
3.714 + /// \brief Returns the 'previous arc' of the shortest path tree for
3.715 + /// the given node.
3.716 + ///
3.717 + /// This function returns the 'previous arc' of the shortest path
3.718 + /// tree for node \c v, i.e. it returns the last arc of a
3.719 + /// shortest path from a root to \c v. It is \c INVALID if \c v
3.720 + /// is not reached from the root(s) or if \c v is a root.
3.721 + ///
3.722 + /// The shortest path tree used here is equal to the shortest path
3.723 + /// tree used in \ref predNode() and \predMap().
3.724 + ///
3.725 + /// \pre Either \ref run() or \ref init() must be called before
3.726 + /// using this function.
3.727 + Arc predArc(Node v) const { return (*_pred)[v]; }
3.728 +
3.729 + /// \brief Returns the 'previous node' of the shortest path tree for
3.730 + /// the given node.
3.731 + ///
3.732 + /// This function returns the 'previous node' of the shortest path
3.733 + /// tree for node \c v, i.e. it returns the last but one node of
3.734 + /// a shortest path from a root to \c v. It is \c INVALID if \c v
3.735 + /// is not reached from the root(s) or if \c v is a root.
3.736 + ///
3.737 + /// The shortest path tree used here is equal to the shortest path
3.738 + /// tree used in \ref predArc() and \predMap().
3.739 + ///
3.740 + /// \pre Either \ref run() or \ref init() must be called before
3.741 + /// using this function.
3.742 + Node predNode(Node v) const {
3.743 + return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]);
3.744 + }
3.745 +
3.746 + /// \brief Returns a const reference to the node map that stores the
3.747 + /// distances of the nodes.
3.748 + ///
3.749 + /// Returns a const reference to the node map that stores the distances
3.750 + /// of the nodes calculated by the algorithm.
3.751 + ///
3.752 + /// \pre Either \ref run() or \ref init() must be called before
3.753 + /// using this function.
3.754 + const DistMap &distMap() const { return *_dist;}
3.755 +
3.756 + /// \brief Returns a const reference to the node map that stores the
3.757 + /// predecessor arcs.
3.758 + ///
3.759 + /// Returns a const reference to the node map that stores the predecessor
3.760 + /// arcs, which form the shortest path tree (forest).
3.761 + ///
3.762 + /// \pre Either \ref run() or \ref init() must be called before
3.763 + /// using this function.
3.764 + const PredMap &predMap() const { return *_pred; }
3.765 +
3.766 + /// \brief Checks if a node is reached from the root(s).
3.767 + ///
3.768 + /// Returns \c true if \c v is reached from the root(s).
3.769 + ///
3.770 + /// \pre Either \ref run() or \ref init() must be called before
3.771 + /// using this function.
3.772 + bool reached(Node v) const {
3.773 + return (*_dist)[v] != OperationTraits::infinity();
3.774 + }
3.775 +
3.776 + /// \brief Gives back a negative cycle.
3.777 + ///
3.778 + /// This function gives back a directed cycle with negative total
3.779 + /// length if the algorithm has already found one.
3.780 + /// Otherwise it gives back an empty path.
3.781 + lemon::Path<Digraph> negativeCycle() {
3.782 + typename Digraph::template NodeMap<int> state(*_gr, -1);
3.783 + lemon::Path<Digraph> cycle;
3.784 + for (int i = 0; i < int(_process.size()); ++i) {
3.785 + if (state[_process[i]] != -1) continue;
3.786 + for (Node v = _process[i]; (*_pred)[v] != INVALID;
3.787 + v = _gr->source((*_pred)[v])) {
3.788 + if (state[v] == i) {
3.789 + cycle.addFront((*_pred)[v]);
3.790 + for (Node u = _gr->source((*_pred)[v]); u != v;
3.791 + u = _gr->source((*_pred)[u])) {
3.792 + cycle.addFront((*_pred)[u]);
3.793 + }
3.794 + return cycle;
3.795 + }
3.796 + else if (state[v] >= 0) {
3.797 + break;
3.798 + }
3.799 + state[v] = i;
3.800 + }
3.801 + }
3.802 + return cycle;
3.803 + }
3.804 +
3.805 + ///@}
3.806 + };
3.807 +
3.808 + /// \brief Default traits class of bellmanFord() function.
3.809 + ///
3.810 + /// Default traits class of bellmanFord() function.
3.811 + /// \tparam GR The type of the digraph.
3.812 + /// \tparam LEN The type of the length map.
3.813 + template <typename GR, typename LEN>
3.814 + struct BellmanFordWizardDefaultTraits {
3.815 + /// The type of the digraph the algorithm runs on.
3.816 + typedef GR Digraph;
3.817 +
3.818 + /// \brief The type of the map that stores the arc lengths.
3.819 + ///
3.820 + /// The type of the map that stores the arc lengths.
3.821 + /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
3.822 + typedef LEN LengthMap;
3.823 +
3.824 + /// The type of the arc lengths.
3.825 + typedef typename LEN::Value Value;
3.826 +
3.827 + /// \brief Operation traits for Bellman-Ford algorithm.
3.828 + ///
3.829 + /// It defines the used operations and the infinity value for the
3.830 + /// given \c Value type.
3.831 + /// \see BellmanFordDefaultOperationTraits
3.832 + typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
3.833 +
3.834 + /// \brief The type of the map that stores the last
3.835 + /// arcs of the shortest paths.
3.836 + ///
3.837 + /// The type of the map that stores the last arcs of the shortest paths.
3.838 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.839 + typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
3.840 +
3.841 + /// \brief Instantiates a \c PredMap.
3.842 + ///
3.843 + /// This function instantiates a \ref PredMap.
3.844 + /// \param g is the digraph to which we would like to define the
3.845 + /// \ref PredMap.
3.846 + static PredMap *createPredMap(const GR &g) {
3.847 + return new PredMap(g);
3.848 + }
3.849 +
3.850 + /// \brief The type of the map that stores the distances of the nodes.
3.851 + ///
3.852 + /// The type of the map that stores the distances of the nodes.
3.853 + /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3.854 + typedef typename GR::template NodeMap<Value> DistMap;
3.855 +
3.856 + /// \brief Instantiates a \c DistMap.
3.857 + ///
3.858 + /// This function instantiates a \ref DistMap.
3.859 + /// \param g is the digraph to which we would like to define the
3.860 + /// \ref DistMap.
3.861 + static DistMap *createDistMap(const GR &g) {
3.862 + return new DistMap(g);
3.863 + }
3.864 +
3.865 + ///The type of the shortest paths.
3.866 +
3.867 + ///The type of the shortest paths.
3.868 + ///It must meet the \ref concepts::Path "Path" concept.
3.869 + typedef lemon::Path<Digraph> Path;
3.870 + };
3.871 +
3.872 + /// \brief Default traits class used by BellmanFordWizard.
3.873 + ///
3.874 + /// Default traits class used by BellmanFordWizard.
3.875 + /// \tparam GR The type of the digraph.
3.876 + /// \tparam LEN The type of the length map.
3.877 + template <typename GR, typename LEN>
3.878 + class BellmanFordWizardBase
3.879 + : public BellmanFordWizardDefaultTraits<GR, LEN> {
3.880 +
3.881 + typedef BellmanFordWizardDefaultTraits<GR, LEN> Base;
3.882 + protected:
3.883 + // Type of the nodes in the digraph.
3.884 + typedef typename Base::Digraph::Node Node;
3.885 +
3.886 + // Pointer to the underlying digraph.
3.887 + void *_graph;
3.888 + // Pointer to the length map
3.889 + void *_length;
3.890 + // Pointer to the map of predecessors arcs.
3.891 + void *_pred;
3.892 + // Pointer to the map of distances.
3.893 + void *_dist;
3.894 + //Pointer to the shortest path to the target node.
3.895 + void *_path;
3.896 + //Pointer to the distance of the target node.
3.897 + void *_di;
3.898 +
3.899 + public:
3.900 + /// Constructor.
3.901 +
3.902 + /// This constructor does not require parameters, it initiates
3.903 + /// all of the attributes to default values \c 0.
3.904 + BellmanFordWizardBase() :
3.905 + _graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {}
3.906 +
3.907 + /// Constructor.
3.908 +
3.909 + /// This constructor requires two parameters,
3.910 + /// others are initiated to \c 0.
3.911 + /// \param gr The digraph the algorithm runs on.
3.912 + /// \param len The length map.
3.913 + BellmanFordWizardBase(const GR& gr,
3.914 + const LEN& len) :
3.915 + _graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))),
3.916 + _length(reinterpret_cast<void*>(const_cast<LEN*>(&len))),
3.917 + _pred(0), _dist(0), _path(0), _di(0) {}
3.918 +
3.919 + };
3.920 +
3.921 + /// \brief Auxiliary class for the function-type interface of the
3.922 + /// \ref BellmanFord "Bellman-Ford" algorithm.
3.923 + ///
3.924 + /// This auxiliary class is created to implement the
3.925 + /// \ref bellmanFord() "function-type interface" of the
3.926 + /// \ref BellmanFord "Bellman-Ford" algorithm.
3.927 + /// It does not have own \ref run() method, it uses the
3.928 + /// functions and features of the plain \ref BellmanFord.
3.929 + ///
3.930 + /// This class should only be used through the \ref bellmanFord()
3.931 + /// function, which makes it easier to use the algorithm.
3.932 + template<class TR>
3.933 + class BellmanFordWizard : public TR {
3.934 + typedef TR Base;
3.935 +
3.936 + typedef typename TR::Digraph Digraph;
3.937 +
3.938 + typedef typename Digraph::Node Node;
3.939 + typedef typename Digraph::NodeIt NodeIt;
3.940 + typedef typename Digraph::Arc Arc;
3.941 + typedef typename Digraph::OutArcIt ArcIt;
3.942 +
3.943 + typedef typename TR::LengthMap LengthMap;
3.944 + typedef typename LengthMap::Value Value;
3.945 + typedef typename TR::PredMap PredMap;
3.946 + typedef typename TR::DistMap DistMap;
3.947 + typedef typename TR::Path Path;
3.948 +
3.949 + public:
3.950 + /// Constructor.
3.951 + BellmanFordWizard() : TR() {}
3.952 +
3.953 + /// \brief Constructor that requires parameters.
3.954 + ///
3.955 + /// Constructor that requires parameters.
3.956 + /// These parameters will be the default values for the traits class.
3.957 + /// \param gr The digraph the algorithm runs on.
3.958 + /// \param len The length map.
3.959 + BellmanFordWizard(const Digraph& gr, const LengthMap& len)
3.960 + : TR(gr, len) {}
3.961 +
3.962 + /// \brief Copy constructor
3.963 + BellmanFordWizard(const TR &b) : TR(b) {}
3.964 +
3.965 + ~BellmanFordWizard() {}
3.966 +
3.967 + /// \brief Runs the Bellman-Ford algorithm from the given source node.
3.968 + ///
3.969 + /// This method runs the Bellman-Ford algorithm from the given source
3.970 + /// node in order to compute the shortest path to each node.
3.971 + void run(Node s) {
3.972 + BellmanFord<Digraph,LengthMap,TR>
3.973 + bf(*reinterpret_cast<const Digraph*>(Base::_graph),
3.974 + *reinterpret_cast<const LengthMap*>(Base::_length));
3.975 + if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
3.976 + if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
3.977 + bf.run(s);
3.978 + }
3.979 +
3.980 + /// \brief Runs the Bellman-Ford algorithm to find the shortest path
3.981 + /// between \c s and \c t.
3.982 + ///
3.983 + /// This method runs the Bellman-Ford algorithm from node \c s
3.984 + /// in order to compute the shortest path to node \c t.
3.985 + /// Actually, it computes the shortest path to each node, but using
3.986 + /// this function you can retrieve the distance and the shortest path
3.987 + /// for a single target node easier.
3.988 + ///
3.989 + /// \return \c true if \c t is reachable form \c s.
3.990 + bool run(Node s, Node t) {
3.991 + BellmanFord<Digraph,LengthMap,TR>
3.992 + bf(*reinterpret_cast<const Digraph*>(Base::_graph),
3.993 + *reinterpret_cast<const LengthMap*>(Base::_length));
3.994 + if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
3.995 + if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
3.996 + bf.run(s);
3.997 + if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t);
3.998 + if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t);
3.999 + return bf.reached(t);
3.1000 + }
3.1001 +
3.1002 + template<class T>
3.1003 + struct SetPredMapBase : public Base {
3.1004 + typedef T PredMap;
3.1005 + static PredMap *createPredMap(const Digraph &) { return 0; };
3.1006 + SetPredMapBase(const TR &b) : TR(b) {}
3.1007 + };
3.1008 +
3.1009 + /// \brief \ref named-templ-param "Named parameter" for setting
3.1010 + /// the predecessor map.
3.1011 + ///
3.1012 + /// \ref named-templ-param "Named parameter" for setting
3.1013 + /// the map that stores the predecessor arcs of the nodes.
3.1014 + template<class T>
3.1015 + BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) {
3.1016 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
3.1017 + return BellmanFordWizard<SetPredMapBase<T> >(*this);
3.1018 + }
3.1019 +
3.1020 + template<class T>
3.1021 + struct SetDistMapBase : public Base {
3.1022 + typedef T DistMap;
3.1023 + static DistMap *createDistMap(const Digraph &) { return 0; };
3.1024 + SetDistMapBase(const TR &b) : TR(b) {}
3.1025 + };
3.1026 +
3.1027 + /// \brief \ref named-templ-param "Named parameter" for setting
3.1028 + /// the distance map.
3.1029 + ///
3.1030 + /// \ref named-templ-param "Named parameter" for setting
3.1031 + /// the map that stores the distances of the nodes calculated
3.1032 + /// by the algorithm.
3.1033 + template<class T>
3.1034 + BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) {
3.1035 + Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
3.1036 + return BellmanFordWizard<SetDistMapBase<T> >(*this);
3.1037 + }
3.1038 +
3.1039 + template<class T>
3.1040 + struct SetPathBase : public Base {
3.1041 + typedef T Path;
3.1042 + SetPathBase(const TR &b) : TR(b) {}
3.1043 + };
3.1044 +
3.1045 + /// \brief \ref named-func-param "Named parameter" for getting
3.1046 + /// the shortest path to the target node.
3.1047 + ///
3.1048 + /// \ref named-func-param "Named parameter" for getting
3.1049 + /// the shortest path to the target node.
3.1050 + template<class T>
3.1051 + BellmanFordWizard<SetPathBase<T> > path(const T &t)
3.1052 + {
3.1053 + Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
3.1054 + return BellmanFordWizard<SetPathBase<T> >(*this);
3.1055 + }
3.1056 +
3.1057 + /// \brief \ref named-func-param "Named parameter" for getting
3.1058 + /// the distance of the target node.
3.1059 + ///
3.1060 + /// \ref named-func-param "Named parameter" for getting
3.1061 + /// the distance of the target node.
3.1062 + BellmanFordWizard dist(const Value &d)
3.1063 + {
3.1064 + Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
3.1065 + return *this;
3.1066 + }
3.1067 +
3.1068 + };
3.1069 +
3.1070 + /// \brief Function type interface for the \ref BellmanFord "Bellman-Ford"
3.1071 + /// algorithm.
3.1072 + ///
3.1073 + /// \ingroup shortest_path
3.1074 + /// Function type interface for the \ref BellmanFord "Bellman-Ford"
3.1075 + /// algorithm.
3.1076 + ///
3.1077 + /// This function also has several \ref named-templ-func-param
3.1078 + /// "named parameters", they are declared as the members of class
3.1079 + /// \ref BellmanFordWizard.
3.1080 + /// The following examples show how to use these parameters.
3.1081 + /// \code
3.1082 + /// // Compute shortest path from node s to each node
3.1083 + /// bellmanFord(g,length).predMap(preds).distMap(dists).run(s);
3.1084 + ///
3.1085 + /// // Compute shortest path from s to t
3.1086 + /// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t);
3.1087 + /// \endcode
3.1088 + /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()"
3.1089 + /// to the end of the parameter list.
3.1090 + /// \sa BellmanFordWizard
3.1091 + /// \sa BellmanFord
3.1092 + template<typename GR, typename LEN>
3.1093 + BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >
3.1094 + bellmanFord(const GR& digraph,
3.1095 + const LEN& length)
3.1096 + {
3.1097 + return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length);
3.1098 + }
3.1099 +
3.1100 +} //END OF NAMESPACE LEMON
3.1101 +
3.1102 +#endif
3.1103 +
4.1 --- a/lemon/bin_heap.h Fri Jul 24 11:07:52 2009 +0200
4.2 +++ b/lemon/bin_heap.h Fri Sep 25 09:06:32 2009 +0200
4.3 @@ -19,9 +19,9 @@
4.4 #ifndef LEMON_BIN_HEAP_H
4.5 #define LEMON_BIN_HEAP_H
4.6
4.7 -///\ingroup auxdat
4.8 +///\ingroup heaps
4.9 ///\file
4.10 -///\brief Binary Heap implementation.
4.11 +///\brief Binary heap implementation.
4.12
4.13 #include <vector>
4.14 #include <utility>
4.15 @@ -29,45 +29,41 @@
4.16
4.17 namespace lemon {
4.18
4.19 - ///\ingroup auxdat
4.20 + /// \ingroup heaps
4.21 ///
4.22 - ///\brief A Binary Heap implementation.
4.23 + /// \brief Binary heap data structure.
4.24 ///
4.25 - ///This class implements the \e binary \e heap data structure.
4.26 + /// This class implements the \e binary \e heap data structure.
4.27 + /// It fully conforms to the \ref concepts::Heap "heap concept".
4.28 ///
4.29 - ///A \e heap is a data structure for storing items with specified values
4.30 - ///called \e priorities in such a way that finding the item with minimum
4.31 - ///priority is efficient. \c CMP specifies the ordering of the priorities.
4.32 - ///In a heap one can change the priority of an item, add or erase an
4.33 - ///item, etc.
4.34 - ///
4.35 - ///\tparam PR Type of the priority of the items.
4.36 - ///\tparam IM A read and writable item map with int values, used internally
4.37 - ///to handle the cross references.
4.38 - ///\tparam CMP A functor class for the ordering of the priorities.
4.39 - ///The default is \c std::less<PR>.
4.40 - ///
4.41 - ///\sa FibHeap
4.42 - ///\sa Dijkstra
4.43 + /// \tparam PR Type of the priorities of the items.
4.44 + /// \tparam IM A read-writable item map with \c int values, used
4.45 + /// internally to handle the cross references.
4.46 + /// \tparam CMP A functor class for comparing the priorities.
4.47 + /// The default is \c std::less<PR>.
4.48 +#ifdef DOXYGEN
4.49 + template <typename PR, typename IM, typename CMP>
4.50 +#else
4.51 template <typename PR, typename IM, typename CMP = std::less<PR> >
4.52 +#endif
4.53 class BinHeap {
4.54 + public:
4.55
4.56 - public:
4.57 - ///\e
4.58 + /// Type of the item-int map.
4.59 typedef IM ItemIntMap;
4.60 - ///\e
4.61 + /// Type of the priorities.
4.62 typedef PR Prio;
4.63 - ///\e
4.64 + /// Type of the items stored in the heap.
4.65 typedef typename ItemIntMap::Key Item;
4.66 - ///\e
4.67 + /// Type of the item-priority pairs.
4.68 typedef std::pair<Item,Prio> Pair;
4.69 - ///\e
4.70 + /// Functor type for comparing the priorities.
4.71 typedef CMP Compare;
4.72
4.73 - /// \brief Type to represent the items states.
4.74 + /// \brief Type to represent the states of the items.
4.75 ///
4.76 - /// Each Item element have a state associated to it. It may be "in heap",
4.77 - /// "pre heap" or "post heap". The latter two are indifferent from the
4.78 + /// Each item has a state associated to it. It can be "in heap",
4.79 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
4.80 /// heap's point of view, but may be useful to the user.
4.81 ///
4.82 /// The item-int map must be initialized in such way that it assigns
4.83 @@ -84,42 +80,43 @@
4.84 ItemIntMap &_iim;
4.85
4.86 public:
4.87 - /// \brief The constructor.
4.88 +
4.89 + /// \brief Constructor.
4.90 ///
4.91 - /// The constructor.
4.92 - /// \param map should be given to the constructor, since it is used
4.93 - /// internally to handle the cross references. The value of the map
4.94 - /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
4.95 + /// Constructor.
4.96 + /// \param map A map that assigns \c int values to the items.
4.97 + /// It is used internally to handle the cross references.
4.98 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
4.99 explicit BinHeap(ItemIntMap &map) : _iim(map) {}
4.100
4.101 - /// \brief The constructor.
4.102 + /// \brief Constructor.
4.103 ///
4.104 - /// The constructor.
4.105 - /// \param map should be given to the constructor, since it is used
4.106 - /// internally to handle the cross references. The value of the map
4.107 - /// should be PRE_HEAP (-1) for each element.
4.108 - ///
4.109 - /// \param comp The comparator function object.
4.110 + /// Constructor.
4.111 + /// \param map A map that assigns \c int values to the items.
4.112 + /// It is used internally to handle the cross references.
4.113 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
4.114 + /// \param comp The function object used for comparing the priorities.
4.115 BinHeap(ItemIntMap &map, const Compare &comp)
4.116 : _iim(map), _comp(comp) {}
4.117
4.118
4.119 - /// The number of items stored in the heap.
4.120 + /// \brief The number of items stored in the heap.
4.121 ///
4.122 - /// \brief Returns the number of items stored in the heap.
4.123 + /// This function returns the number of items stored in the heap.
4.124 int size() const { return _data.size(); }
4.125
4.126 - /// \brief Checks if the heap stores no items.
4.127 + /// \brief Check if the heap is empty.
4.128 ///
4.129 - /// Returns \c true if and only if the heap stores no items.
4.130 + /// This function returns \c true if the heap is empty.
4.131 bool empty() const { return _data.empty(); }
4.132
4.133 - /// \brief Make empty this heap.
4.134 + /// \brief Make the heap empty.
4.135 ///
4.136 - /// Make empty this heap. It does not change the cross reference map.
4.137 - /// If you want to reuse what is not surely empty you should first clear
4.138 - /// the heap and after that you should set the cross reference map for
4.139 - /// each item to \c PRE_HEAP.
4.140 + /// This functon makes the heap empty.
4.141 + /// It does not change the cross reference map. If you want to reuse
4.142 + /// a heap that is not surely empty, you should first clear it and
4.143 + /// then you should set the cross reference map to \c PRE_HEAP
4.144 + /// for each item.
4.145 void clear() {
4.146 _data.clear();
4.147 }
4.148 @@ -127,12 +124,12 @@
4.149 private:
4.150 static int parent(int i) { return (i-1)/2; }
4.151
4.152 - static int second_child(int i) { return 2*i+2; }
4.153 + static int secondChild(int i) { return 2*i+2; }
4.154 bool less(const Pair &p1, const Pair &p2) const {
4.155 return _comp(p1.second, p2.second);
4.156 }
4.157
4.158 - int bubble_up(int hole, Pair p) {
4.159 + int bubbleUp(int hole, Pair p) {
4.160 int par = parent(hole);
4.161 while( hole>0 && less(p,_data[par]) ) {
4.162 move(_data[par],hole);
4.163 @@ -143,8 +140,8 @@
4.164 return hole;
4.165 }
4.166
4.167 - int bubble_down(int hole, Pair p, int length) {
4.168 - int child = second_child(hole);
4.169 + int bubbleDown(int hole, Pair p, int length) {
4.170 + int child = secondChild(hole);
4.171 while(child < length) {
4.172 if( less(_data[child-1], _data[child]) ) {
4.173 --child;
4.174 @@ -153,7 +150,7 @@
4.175 goto ok;
4.176 move(_data[child], hole);
4.177 hole = child;
4.178 - child = second_child(hole);
4.179 + child = secondChild(hole);
4.180 }
4.181 child--;
4.182 if( child<length && less(_data[child], p) ) {
4.183 @@ -171,87 +168,91 @@
4.184 }
4.185
4.186 public:
4.187 +
4.188 /// \brief Insert a pair of item and priority into the heap.
4.189 ///
4.190 - /// Adds \c p.first to the heap with priority \c p.second.
4.191 + /// This function inserts \c p.first to the heap with priority
4.192 + /// \c p.second.
4.193 /// \param p The pair to insert.
4.194 + /// \pre \c p.first must not be stored in the heap.
4.195 void push(const Pair &p) {
4.196 int n = _data.size();
4.197 _data.resize(n+1);
4.198 - bubble_up(n, p);
4.199 + bubbleUp(n, p);
4.200 }
4.201
4.202 - /// \brief Insert an item into the heap with the given heap.
4.203 + /// \brief Insert an item into the heap with the given priority.
4.204 ///
4.205 - /// Adds \c i to the heap with priority \c p.
4.206 + /// This function inserts the given item into the heap with the
4.207 + /// given priority.
4.208 /// \param i The item to insert.
4.209 /// \param p The priority of the item.
4.210 + /// \pre \e i must not be stored in the heap.
4.211 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
4.212
4.213 - /// \brief Returns the item with minimum priority relative to \c Compare.
4.214 + /// \brief Return the item having minimum priority.
4.215 ///
4.216 - /// This method returns the item with minimum priority relative to \c
4.217 - /// Compare.
4.218 - /// \pre The heap must be nonempty.
4.219 + /// This function returns the item having minimum priority.
4.220 + /// \pre The heap must be non-empty.
4.221 Item top() const {
4.222 return _data[0].first;
4.223 }
4.224
4.225 - /// \brief Returns the minimum priority relative to \c Compare.
4.226 + /// \brief The minimum priority.
4.227 ///
4.228 - /// It returns the minimum priority relative to \c Compare.
4.229 - /// \pre The heap must be nonempty.
4.230 + /// This function returns the minimum priority.
4.231 + /// \pre The heap must be non-empty.
4.232 Prio prio() const {
4.233 return _data[0].second;
4.234 }
4.235
4.236 - /// \brief Deletes the item with minimum priority relative to \c Compare.
4.237 + /// \brief Remove the item having minimum priority.
4.238 ///
4.239 - /// This method deletes the item with minimum priority relative to \c
4.240 - /// Compare from the heap.
4.241 + /// This function removes the item having minimum priority.
4.242 /// \pre The heap must be non-empty.
4.243 void pop() {
4.244 int n = _data.size()-1;
4.245 _iim.set(_data[0].first, POST_HEAP);
4.246 if (n > 0) {
4.247 - bubble_down(0, _data[n], n);
4.248 + bubbleDown(0, _data[n], n);
4.249 }
4.250 _data.pop_back();
4.251 }
4.252
4.253 - /// \brief Deletes \c i from the heap.
4.254 + /// \brief Remove the given item from the heap.
4.255 ///
4.256 - /// This method deletes item \c i from the heap.
4.257 - /// \param i The item to erase.
4.258 - /// \pre The item should be in the heap.
4.259 + /// This function removes the given item from the heap if it is
4.260 + /// already stored.
4.261 + /// \param i The item to delete.
4.262 + /// \pre \e i must be in the heap.
4.263 void erase(const Item &i) {
4.264 int h = _iim[i];
4.265 int n = _data.size()-1;
4.266 _iim.set(_data[h].first, POST_HEAP);
4.267 if( h < n ) {
4.268 - if ( bubble_up(h, _data[n]) == h) {
4.269 - bubble_down(h, _data[n], n);
4.270 + if ( bubbleUp(h, _data[n]) == h) {
4.271 + bubbleDown(h, _data[n], n);
4.272 }
4.273 }
4.274 _data.pop_back();
4.275 }
4.276
4.277 -
4.278 - /// \brief Returns the priority of \c i.
4.279 + /// \brief The priority of the given item.
4.280 ///
4.281 - /// This function returns the priority of item \c i.
4.282 + /// This function returns the priority of the given item.
4.283 /// \param i The item.
4.284 - /// \pre \c i must be in the heap.
4.285 + /// \pre \e i must be in the heap.
4.286 Prio operator[](const Item &i) const {
4.287 int idx = _iim[i];
4.288 return _data[idx].second;
4.289 }
4.290
4.291 - /// \brief \c i gets to the heap with priority \c p independently
4.292 - /// if \c i was already there.
4.293 + /// \brief Set the priority of an item or insert it, if it is
4.294 + /// not stored in the heap.
4.295 ///
4.296 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
4.297 - /// in the heap and sets the priority of \c i to \c p otherwise.
4.298 + /// This method sets the priority of the given item if it is
4.299 + /// already stored in the heap. Otherwise it inserts the given
4.300 + /// item into the heap with the given priority.
4.301 /// \param i The item.
4.302 /// \param p The priority.
4.303 void set(const Item &i, const Prio &p) {
4.304 @@ -260,44 +261,42 @@
4.305 push(i,p);
4.306 }
4.307 else if( _comp(p, _data[idx].second) ) {
4.308 - bubble_up(idx, Pair(i,p));
4.309 + bubbleUp(idx, Pair(i,p));
4.310 }
4.311 else {
4.312 - bubble_down(idx, Pair(i,p), _data.size());
4.313 + bubbleDown(idx, Pair(i,p), _data.size());
4.314 }
4.315 }
4.316
4.317 - /// \brief Decreases the priority of \c i to \c p.
4.318 + /// \brief Decrease the priority of an item to the given value.
4.319 ///
4.320 - /// This method decreases the priority of item \c i to \c p.
4.321 + /// This function decreases the priority of an item to the given value.
4.322 /// \param i The item.
4.323 /// \param p The priority.
4.324 - /// \pre \c i must be stored in the heap with priority at least \c
4.325 - /// p relative to \c Compare.
4.326 + /// \pre \e i must be stored in the heap with priority at least \e p.
4.327 void decrease(const Item &i, const Prio &p) {
4.328 int idx = _iim[i];
4.329 - bubble_up(idx, Pair(i,p));
4.330 + bubbleUp(idx, Pair(i,p));
4.331 }
4.332
4.333 - /// \brief Increases the priority of \c i to \c p.
4.334 + /// \brief Increase the priority of an item to the given value.
4.335 ///
4.336 - /// This method sets the priority of item \c i to \c p.
4.337 + /// This function increases the priority of an item to the given value.
4.338 /// \param i The item.
4.339 /// \param p The priority.
4.340 - /// \pre \c i must be stored in the heap with priority at most \c
4.341 - /// p relative to \c Compare.
4.342 + /// \pre \e i must be stored in the heap with priority at most \e p.
4.343 void increase(const Item &i, const Prio &p) {
4.344 int idx = _iim[i];
4.345 - bubble_down(idx, Pair(i,p), _data.size());
4.346 + bubbleDown(idx, Pair(i,p), _data.size());
4.347 }
4.348
4.349 - /// \brief Returns if \c item is in, has already been in, or has
4.350 - /// never been in the heap.
4.351 + /// \brief Return the state of an item.
4.352 ///
4.353 - /// This method returns PRE_HEAP if \c item has never been in the
4.354 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
4.355 - /// otherwise. In the latter case it is possible that \c item will
4.356 - /// get back to the heap again.
4.357 + /// This method returns \c PRE_HEAP if the given item has never
4.358 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
4.359 + /// and \c POST_HEAP otherwise.
4.360 + /// In the latter case it is possible that the item will get back
4.361 + /// to the heap again.
4.362 /// \param i The item.
4.363 State state(const Item &i) const {
4.364 int s = _iim[i];
4.365 @@ -306,11 +305,11 @@
4.366 return State(s);
4.367 }
4.368
4.369 - /// \brief Sets the state of the \c item in the heap.
4.370 + /// \brief Set the state of an item in the heap.
4.371 ///
4.372 - /// Sets the state of the \c item in the heap. It can be used to
4.373 - /// manually clear the heap when it is important to achive the
4.374 - /// better time complexity.
4.375 + /// This function sets the state of the given item in the heap.
4.376 + /// It can be used to manually clear the heap when it is important
4.377 + /// to achive better time complexity.
4.378 /// \param i The item.
4.379 /// \param st The state. It should not be \c IN_HEAP.
4.380 void state(const Item& i, State st) {
4.381 @@ -327,12 +326,13 @@
4.382 }
4.383 }
4.384
4.385 - /// \brief Replaces an item in the heap.
4.386 + /// \brief Replace an item in the heap.
4.387 ///
4.388 - /// The \c i item is replaced with \c j item. The \c i item should
4.389 - /// be in the heap, while the \c j should be out of the heap. The
4.390 - /// \c i item will out of the heap and \c j will be in the heap
4.391 - /// with the same prioriority as prevoiusly the \c i item.
4.392 + /// This function replaces item \c i with item \c j.
4.393 + /// Item \c i must be in the heap, while \c j must be out of the heap.
4.394 + /// After calling this method, item \c i will be out of the
4.395 + /// heap and \c j will be in the heap with the same prioriority
4.396 + /// as item \c i had before.
4.397 void replace(const Item& i, const Item& j) {
4.398 int idx = _iim[i];
4.399 _iim.set(i, _iim[j]);
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/lemon/binom_heap.h Fri Sep 25 09:06:32 2009 +0200
5.3 @@ -0,0 +1,445 @@
5.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
5.5 + *
5.6 + * This file is a part of LEMON, a generic C++ optimization library.
5.7 + *
5.8 + * Copyright (C) 2003-2009
5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
5.11 + *
5.12 + * Permission to use, modify and distribute this software is granted
5.13 + * provided that this copyright notice appears in all copies. For
5.14 + * precise terms see the accompanying LICENSE file.
5.15 + *
5.16 + * This software is provided "AS IS" with no warranty of any kind,
5.17 + * express or implied, and with no claim as to its suitability for any
5.18 + * purpose.
5.19 + *
5.20 + */
5.21 +
5.22 +#ifndef LEMON_BINOM_HEAP_H
5.23 +#define LEMON_BINOM_HEAP_H
5.24 +
5.25 +///\file
5.26 +///\ingroup heaps
5.27 +///\brief Binomial Heap implementation.
5.28 +
5.29 +#include <vector>
5.30 +#include <utility>
5.31 +#include <functional>
5.32 +#include <lemon/math.h>
5.33 +#include <lemon/counter.h>
5.34 +
5.35 +namespace lemon {
5.36 +
5.37 + /// \ingroup heaps
5.38 + ///
5.39 + ///\brief Binomial heap data structure.
5.40 + ///
5.41 + /// This class implements the \e binomial \e heap data structure.
5.42 + /// It fully conforms to the \ref concepts::Heap "heap concept".
5.43 + ///
5.44 + /// The methods \ref increase() and \ref erase() are not efficient
5.45 + /// in a binomial heap. In case of many calls of these operations,
5.46 + /// it is better to use other heap structure, e.g. \ref BinHeap
5.47 + /// "binary heap".
5.48 + ///
5.49 + /// \tparam PR Type of the priorities of the items.
5.50 + /// \tparam IM A read-writable item map with \c int values, used
5.51 + /// internally to handle the cross references.
5.52 + /// \tparam CMP A functor class for comparing the priorities.
5.53 + /// The default is \c std::less<PR>.
5.54 +#ifdef DOXYGEN
5.55 + template <typename PR, typename IM, typename CMP>
5.56 +#else
5.57 + template <typename PR, typename IM, typename CMP = std::less<PR> >
5.58 +#endif
5.59 + class BinomHeap {
5.60 + public:
5.61 + /// Type of the item-int map.
5.62 + typedef IM ItemIntMap;
5.63 + /// Type of the priorities.
5.64 + typedef PR Prio;
5.65 + /// Type of the items stored in the heap.
5.66 + typedef typename ItemIntMap::Key Item;
5.67 + /// Functor type for comparing the priorities.
5.68 + typedef CMP Compare;
5.69 +
5.70 + /// \brief Type to represent the states of the items.
5.71 + ///
5.72 + /// Each item has a state associated to it. It can be "in heap",
5.73 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
5.74 + /// heap's point of view, but may be useful to the user.
5.75 + ///
5.76 + /// The item-int map must be initialized in such way that it assigns
5.77 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
5.78 + enum State {
5.79 + IN_HEAP = 0, ///< = 0.
5.80 + PRE_HEAP = -1, ///< = -1.
5.81 + POST_HEAP = -2 ///< = -2.
5.82 + };
5.83 +
5.84 + private:
5.85 + class Store;
5.86 +
5.87 + std::vector<Store> _data;
5.88 + int _min, _head;
5.89 + ItemIntMap &_iim;
5.90 + Compare _comp;
5.91 + int _num_items;
5.92 +
5.93 + public:
5.94 + /// \brief Constructor.
5.95 + ///
5.96 + /// Constructor.
5.97 + /// \param map A map that assigns \c int values to the items.
5.98 + /// It is used internally to handle the cross references.
5.99 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
5.100 + explicit BinomHeap(ItemIntMap &map)
5.101 + : _min(0), _head(-1), _iim(map), _num_items(0) {}
5.102 +
5.103 + /// \brief Constructor.
5.104 + ///
5.105 + /// Constructor.
5.106 + /// \param map A map that assigns \c int values to the items.
5.107 + /// It is used internally to handle the cross references.
5.108 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
5.109 + /// \param comp The function object used for comparing the priorities.
5.110 + BinomHeap(ItemIntMap &map, const Compare &comp)
5.111 + : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
5.112 +
5.113 + /// \brief The number of items stored in the heap.
5.114 + ///
5.115 + /// This function returns the number of items stored in the heap.
5.116 + int size() const { return _num_items; }
5.117 +
5.118 + /// \brief Check if the heap is empty.
5.119 + ///
5.120 + /// This function returns \c true if the heap is empty.
5.121 + bool empty() const { return _num_items==0; }
5.122 +
5.123 + /// \brief Make the heap empty.
5.124 + ///
5.125 + /// This functon makes the heap empty.
5.126 + /// It does not change the cross reference map. If you want to reuse
5.127 + /// a heap that is not surely empty, you should first clear it and
5.128 + /// then you should set the cross reference map to \c PRE_HEAP
5.129 + /// for each item.
5.130 + void clear() {
5.131 + _data.clear(); _min=0; _num_items=0; _head=-1;
5.132 + }
5.133 +
5.134 + /// \brief Set the priority of an item or insert it, if it is
5.135 + /// not stored in the heap.
5.136 + ///
5.137 + /// This method sets the priority of the given item if it is
5.138 + /// already stored in the heap. Otherwise it inserts the given
5.139 + /// item into the heap with the given priority.
5.140 + /// \param item The item.
5.141 + /// \param value The priority.
5.142 + void set (const Item& item, const Prio& value) {
5.143 + int i=_iim[item];
5.144 + if ( i >= 0 && _data[i].in ) {
5.145 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
5.146 + if ( _comp(_data[i].prio, value) ) increase(item, value);
5.147 + } else push(item, value);
5.148 + }
5.149 +
5.150 + /// \brief Insert an item into the heap with the given priority.
5.151 + ///
5.152 + /// This function inserts the given item into the heap with the
5.153 + /// given priority.
5.154 + /// \param item The item to insert.
5.155 + /// \param value The priority of the item.
5.156 + /// \pre \e item must not be stored in the heap.
5.157 + void push (const Item& item, const Prio& value) {
5.158 + int i=_iim[item];
5.159 + if ( i<0 ) {
5.160 + int s=_data.size();
5.161 + _iim.set( item,s );
5.162 + Store st;
5.163 + st.name=item;
5.164 + st.prio=value;
5.165 + _data.push_back(st);
5.166 + i=s;
5.167 + }
5.168 + else {
5.169 + _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
5.170 + _data[i].degree=0;
5.171 + _data[i].in=true;
5.172 + _data[i].prio=value;
5.173 + }
5.174 +
5.175 + if( 0==_num_items ) {
5.176 + _head=i;
5.177 + _min=i;
5.178 + } else {
5.179 + merge(i);
5.180 + if( _comp(_data[i].prio, _data[_min].prio) ) _min=i;
5.181 + }
5.182 + ++_num_items;
5.183 + }
5.184 +
5.185 + /// \brief Return the item having minimum priority.
5.186 + ///
5.187 + /// This function returns the item having minimum priority.
5.188 + /// \pre The heap must be non-empty.
5.189 + Item top() const { return _data[_min].name; }
5.190 +
5.191 + /// \brief The minimum priority.
5.192 + ///
5.193 + /// This function returns the minimum priority.
5.194 + /// \pre The heap must be non-empty.
5.195 + Prio prio() const { return _data[_min].prio; }
5.196 +
5.197 + /// \brief The priority of the given item.
5.198 + ///
5.199 + /// This function returns the priority of the given item.
5.200 + /// \param item The item.
5.201 + /// \pre \e item must be in the heap.
5.202 + const Prio& operator[](const Item& item) const {
5.203 + return _data[_iim[item]].prio;
5.204 + }
5.205 +
5.206 + /// \brief Remove the item having minimum priority.
5.207 + ///
5.208 + /// This function removes the item having minimum priority.
5.209 + /// \pre The heap must be non-empty.
5.210 + void pop() {
5.211 + _data[_min].in=false;
5.212 +
5.213 + int head_child=-1;
5.214 + if ( _data[_min].child!=-1 ) {
5.215 + int child=_data[_min].child;
5.216 + int neighb;
5.217 + while( child!=-1 ) {
5.218 + neighb=_data[child].right_neighbor;
5.219 + _data[child].parent=-1;
5.220 + _data[child].right_neighbor=head_child;
5.221 + head_child=child;
5.222 + child=neighb;
5.223 + }
5.224 + }
5.225 +
5.226 + if ( _data[_head].right_neighbor==-1 ) {
5.227 + // there was only one root
5.228 + _head=head_child;
5.229 + }
5.230 + else {
5.231 + // there were more roots
5.232 + if( _head!=_min ) { unlace(_min); }
5.233 + else { _head=_data[_head].right_neighbor; }
5.234 + merge(head_child);
5.235 + }
5.236 + _min=findMin();
5.237 + --_num_items;
5.238 + }
5.239 +
5.240 + /// \brief Remove the given item from the heap.
5.241 + ///
5.242 + /// This function removes the given item from the heap if it is
5.243 + /// already stored.
5.244 + /// \param item The item to delete.
5.245 + /// \pre \e item must be in the heap.
5.246 + void erase (const Item& item) {
5.247 + int i=_iim[item];
5.248 + if ( i >= 0 && _data[i].in ) {
5.249 + decrease( item, _data[_min].prio-1 );
5.250 + pop();
5.251 + }
5.252 + }
5.253 +
5.254 + /// \brief Decrease the priority of an item to the given value.
5.255 + ///
5.256 + /// This function decreases the priority of an item to the given value.
5.257 + /// \param item The item.
5.258 + /// \param value The priority.
5.259 + /// \pre \e item must be stored in the heap with priority at least \e value.
5.260 + void decrease (Item item, const Prio& value) {
5.261 + int i=_iim[item];
5.262 + int p=_data[i].parent;
5.263 + _data[i].prio=value;
5.264 +
5.265 + while( p!=-1 && _comp(value, _data[p].prio) ) {
5.266 + _data[i].name=_data[p].name;
5.267 + _data[i].prio=_data[p].prio;
5.268 + _data[p].name=item;
5.269 + _data[p].prio=value;
5.270 + _iim[_data[i].name]=i;
5.271 + i=p;
5.272 + p=_data[p].parent;
5.273 + }
5.274 + _iim[item]=i;
5.275 + if ( _comp(value, _data[_min].prio) ) _min=i;
5.276 + }
5.277 +
5.278 + /// \brief Increase the priority of an item to the given value.
5.279 + ///
5.280 + /// This function increases the priority of an item to the given value.
5.281 + /// \param item The item.
5.282 + /// \param value The priority.
5.283 + /// \pre \e item must be stored in the heap with priority at most \e value.
5.284 + void increase (Item item, const Prio& value) {
5.285 + erase(item);
5.286 + push(item, value);
5.287 + }
5.288 +
5.289 + /// \brief Return the state of an item.
5.290 + ///
5.291 + /// This method returns \c PRE_HEAP if the given item has never
5.292 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
5.293 + /// and \c POST_HEAP otherwise.
5.294 + /// In the latter case it is possible that the item will get back
5.295 + /// to the heap again.
5.296 + /// \param item The item.
5.297 + State state(const Item &item) const {
5.298 + int i=_iim[item];
5.299 + if( i>=0 ) {
5.300 + if ( _data[i].in ) i=0;
5.301 + else i=-2;
5.302 + }
5.303 + return State(i);
5.304 + }
5.305 +
5.306 + /// \brief Set the state of an item in the heap.
5.307 + ///
5.308 + /// This function sets the state of the given item in the heap.
5.309 + /// It can be used to manually clear the heap when it is important
5.310 + /// to achive better time complexity.
5.311 + /// \param i The item.
5.312 + /// \param st The state. It should not be \c IN_HEAP.
5.313 + void state(const Item& i, State st) {
5.314 + switch (st) {
5.315 + case POST_HEAP:
5.316 + case PRE_HEAP:
5.317 + if (state(i) == IN_HEAP) {
5.318 + erase(i);
5.319 + }
5.320 + _iim[i] = st;
5.321 + break;
5.322 + case IN_HEAP:
5.323 + break;
5.324 + }
5.325 + }
5.326 +
5.327 + private:
5.328 +
5.329 + // Find the minimum of the roots
5.330 + int findMin() {
5.331 + if( _head!=-1 ) {
5.332 + int min_loc=_head, min_val=_data[_head].prio;
5.333 + for( int x=_data[_head].right_neighbor; x!=-1;
5.334 + x=_data[x].right_neighbor ) {
5.335 + if( _comp( _data[x].prio,min_val ) ) {
5.336 + min_val=_data[x].prio;
5.337 + min_loc=x;
5.338 + }
5.339 + }
5.340 + return min_loc;
5.341 + }
5.342 + else return -1;
5.343 + }
5.344 +
5.345 + // Merge the heap with another heap starting at the given position
5.346 + void merge(int a) {
5.347 + if( _head==-1 || a==-1 ) return;
5.348 + if( _data[a].right_neighbor==-1 &&
5.349 + _data[a].degree<=_data[_head].degree ) {
5.350 + _data[a].right_neighbor=_head;
5.351 + _head=a;
5.352 + } else {
5.353 + interleave(a);
5.354 + }
5.355 + if( _data[_head].right_neighbor==-1 ) return;
5.356 +
5.357 + int x=_head;
5.358 + int x_prev=-1, x_next=_data[x].right_neighbor;
5.359 + while( x_next!=-1 ) {
5.360 + if( _data[x].degree!=_data[x_next].degree ||
5.361 + ( _data[x_next].right_neighbor!=-1 &&
5.362 + _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
5.363 + x_prev=x;
5.364 + x=x_next;
5.365 + }
5.366 + else {
5.367 + if( _comp(_data[x_next].prio,_data[x].prio) ) {
5.368 + if( x_prev==-1 ) {
5.369 + _head=x_next;
5.370 + } else {
5.371 + _data[x_prev].right_neighbor=x_next;
5.372 + }
5.373 + fuse(x,x_next);
5.374 + x=x_next;
5.375 + }
5.376 + else {
5.377 + _data[x].right_neighbor=_data[x_next].right_neighbor;
5.378 + fuse(x_next,x);
5.379 + }
5.380 + }
5.381 + x_next=_data[x].right_neighbor;
5.382 + }
5.383 + }
5.384 +
5.385 + // Interleave the elements of the given list into the list of the roots
5.386 + void interleave(int a) {
5.387 + int p=_head, q=a;
5.388 + int curr=_data.size();
5.389 + _data.push_back(Store());
5.390 +
5.391 + while( p!=-1 || q!=-1 ) {
5.392 + if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
5.393 + _data[curr].right_neighbor=p;
5.394 + curr=p;
5.395 + p=_data[p].right_neighbor;
5.396 + }
5.397 + else {
5.398 + _data[curr].right_neighbor=q;
5.399 + curr=q;
5.400 + q=_data[q].right_neighbor;
5.401 + }
5.402 + }
5.403 +
5.404 + _head=_data.back().right_neighbor;
5.405 + _data.pop_back();
5.406 + }
5.407 +
5.408 + // Lace node a under node b
5.409 + void fuse(int a, int b) {
5.410 + _data[a].parent=b;
5.411 + _data[a].right_neighbor=_data[b].child;
5.412 + _data[b].child=a;
5.413 +
5.414 + ++_data[b].degree;
5.415 + }
5.416 +
5.417 + // Unlace node a (if it has siblings)
5.418 + void unlace(int a) {
5.419 + int neighb=_data[a].right_neighbor;
5.420 + int other=_head;
5.421 +
5.422 + while( _data[other].right_neighbor!=a )
5.423 + other=_data[other].right_neighbor;
5.424 + _data[other].right_neighbor=neighb;
5.425 + }
5.426 +
5.427 + private:
5.428 +
5.429 + class Store {
5.430 + friend class BinomHeap;
5.431 +
5.432 + Item name;
5.433 + int parent;
5.434 + int right_neighbor;
5.435 + int child;
5.436 + int degree;
5.437 + bool in;
5.438 + Prio prio;
5.439 +
5.440 + Store() : parent(-1), right_neighbor(-1), child(-1), degree(0),
5.441 + in(true) {}
5.442 + };
5.443 + };
5.444 +
5.445 +} //namespace lemon
5.446 +
5.447 +#endif //LEMON_BINOM_HEAP_H
5.448 +
6.1 --- a/lemon/bits/edge_set_extender.h Fri Jul 24 11:07:52 2009 +0200
6.2 +++ b/lemon/bits/edge_set_extender.h Fri Sep 25 09:06:32 2009 +0200
6.3 @@ -537,7 +537,7 @@
6.4 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
6.5
6.6 public:
6.7 - ArcMap(const Graph& _g)
6.8 + explicit ArcMap(const Graph& _g)
6.9 : Parent(_g) {}
6.10 ArcMap(const Graph& _g, const _Value& _v)
6.11 : Parent(_g, _v) {}
6.12 @@ -561,7 +561,7 @@
6.13 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
6.14
6.15 public:
6.16 - EdgeMap(const Graph& _g)
6.17 + explicit EdgeMap(const Graph& _g)
6.18 : Parent(_g) {}
6.19
6.20 EdgeMap(const Graph& _g, const _Value& _v)
7.1 --- a/lemon/bits/graph_extender.h Fri Jul 24 11:07:52 2009 +0200
7.2 +++ b/lemon/bits/graph_extender.h Fri Sep 25 09:06:32 2009 +0200
7.3 @@ -604,7 +604,7 @@
7.4 typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
7.5
7.6 public:
7.7 - NodeMap(const Graph& graph)
7.8 + explicit NodeMap(const Graph& graph)
7.9 : Parent(graph) {}
7.10 NodeMap(const Graph& graph, const _Value& value)
7.11 : Parent(graph, value) {}
7.12 @@ -628,7 +628,7 @@
7.13 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
7.14
7.15 public:
7.16 - ArcMap(const Graph& graph)
7.17 + explicit ArcMap(const Graph& graph)
7.18 : Parent(graph) {}
7.19 ArcMap(const Graph& graph, const _Value& value)
7.20 : Parent(graph, value) {}
7.21 @@ -652,7 +652,7 @@
7.22 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
7.23
7.24 public:
7.25 - EdgeMap(const Graph& graph)
7.26 + explicit EdgeMap(const Graph& graph)
7.27 : Parent(graph) {}
7.28
7.29 EdgeMap(const Graph& graph, const _Value& value)
8.1 --- a/lemon/bucket_heap.h Fri Jul 24 11:07:52 2009 +0200
8.2 +++ b/lemon/bucket_heap.h Fri Sep 25 09:06:32 2009 +0200
8.3 @@ -19,9 +19,9 @@
8.4 #ifndef LEMON_BUCKET_HEAP_H
8.5 #define LEMON_BUCKET_HEAP_H
8.6
8.7 -///\ingroup auxdat
8.8 +///\ingroup heaps
8.9 ///\file
8.10 -///\brief Bucket Heap implementation.
8.11 +///\brief Bucket heap implementation.
8.12
8.13 #include <vector>
8.14 #include <utility>
8.15 @@ -53,35 +53,41 @@
8.16
8.17 }
8.18
8.19 - /// \ingroup auxdat
8.20 + /// \ingroup heaps
8.21 ///
8.22 - /// \brief A Bucket Heap implementation.
8.23 + /// \brief Bucket heap data structure.
8.24 ///
8.25 - /// This class implements the \e bucket \e heap data structure. A \e heap
8.26 - /// is a data structure for storing items with specified values called \e
8.27 - /// priorities in such a way that finding the item with minimum priority is
8.28 - /// efficient. The bucket heap is very simple implementation, it can store
8.29 - /// only integer priorities and it stores for each priority in the
8.30 - /// \f$ [0..C) \f$ range a list of items. So it should be used only when
8.31 - /// the priorities are small. It is not intended to use as dijkstra heap.
8.32 + /// This class implements the \e bucket \e heap data structure.
8.33 + /// It practically conforms to the \ref concepts::Heap "heap concept",
8.34 + /// but it has some limitations.
8.35 ///
8.36 - /// \param IM A read and write Item int map, used internally
8.37 - /// to handle the cross references.
8.38 - /// \param MIN If the given parameter is false then instead of the
8.39 - /// minimum value the maximum can be retrivied with the top() and
8.40 - /// prio() member functions.
8.41 + /// The bucket heap is a very simple structure. It can store only
8.42 + /// \c int priorities and it maintains a list of items for each priority
8.43 + /// in the range <tt>[0..C)</tt>. So it should only be used when the
8.44 + /// priorities are small. It is not intended to use as a Dijkstra heap.
8.45 + ///
8.46 + /// \tparam IM A read-writable item map with \c int values, used
8.47 + /// internally to handle the cross references.
8.48 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
8.49 + /// The default is \e min-heap. If this parameter is set to \c false,
8.50 + /// then the comparison is reversed, so the top(), prio() and pop()
8.51 + /// functions deal with the item having maximum priority instead of the
8.52 + /// minimum.
8.53 + ///
8.54 + /// \sa SimpleBucketHeap
8.55 template <typename IM, bool MIN = true>
8.56 class BucketHeap {
8.57
8.58 public:
8.59 - /// \e
8.60 - typedef typename IM::Key Item;
8.61 - /// \e
8.62 +
8.63 + /// Type of the item-int map.
8.64 + typedef IM ItemIntMap;
8.65 + /// Type of the priorities.
8.66 typedef int Prio;
8.67 - /// \e
8.68 - typedef std::pair<Item, Prio> Pair;
8.69 - /// \e
8.70 - typedef IM ItemIntMap;
8.71 + /// Type of the items stored in the heap.
8.72 + typedef typename ItemIntMap::Key Item;
8.73 + /// Type of the item-priority pairs.
8.74 + typedef std::pair<Item,Prio> Pair;
8.75
8.76 private:
8.77
8.78 @@ -89,10 +95,10 @@
8.79
8.80 public:
8.81
8.82 - /// \brief Type to represent the items states.
8.83 + /// \brief Type to represent the states of the items.
8.84 ///
8.85 - /// Each Item element have a state associated to it. It may be "in heap",
8.86 - /// "pre heap" or "post heap". The latter two are indifferent from the
8.87 + /// Each item has a state associated to it. It can be "in heap",
8.88 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
8.89 /// heap's point of view, but may be useful to the user.
8.90 ///
8.91 /// The item-int map must be initialized in such way that it assigns
8.92 @@ -104,37 +110,39 @@
8.93 };
8.94
8.95 public:
8.96 - /// \brief The constructor.
8.97 +
8.98 + /// \brief Constructor.
8.99 ///
8.100 - /// The constructor.
8.101 - /// \param map should be given to the constructor, since it is used
8.102 - /// internally to handle the cross references. The value of the map
8.103 - /// should be PRE_HEAP (-1) for each element.
8.104 + /// Constructor.
8.105 + /// \param map A map that assigns \c int values to the items.
8.106 + /// It is used internally to handle the cross references.
8.107 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
8.108 explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
8.109
8.110 - /// The number of items stored in the heap.
8.111 + /// \brief The number of items stored in the heap.
8.112 ///
8.113 - /// \brief Returns the number of items stored in the heap.
8.114 + /// This function returns the number of items stored in the heap.
8.115 int size() const { return _data.size(); }
8.116
8.117 - /// \brief Checks if the heap stores no items.
8.118 + /// \brief Check if the heap is empty.
8.119 ///
8.120 - /// Returns \c true if and only if the heap stores no items.
8.121 + /// This function returns \c true if the heap is empty.
8.122 bool empty() const { return _data.empty(); }
8.123
8.124 - /// \brief Make empty this heap.
8.125 + /// \brief Make the heap empty.
8.126 ///
8.127 - /// Make empty this heap. It does not change the cross reference
8.128 - /// map. If you want to reuse a heap what is not surely empty you
8.129 - /// should first clear the heap and after that you should set the
8.130 - /// cross reference map for each item to \c PRE_HEAP.
8.131 + /// This functon makes the heap empty.
8.132 + /// It does not change the cross reference map. If you want to reuse
8.133 + /// a heap that is not surely empty, you should first clear it and
8.134 + /// then you should set the cross reference map to \c PRE_HEAP
8.135 + /// for each item.
8.136 void clear() {
8.137 _data.clear(); _first.clear(); _minimum = 0;
8.138 }
8.139
8.140 private:
8.141
8.142 - void relocate_last(int idx) {
8.143 + void relocateLast(int idx) {
8.144 if (idx + 1 < int(_data.size())) {
8.145 _data[idx] = _data.back();
8.146 if (_data[idx].prev != -1) {
8.147 @@ -174,19 +182,24 @@
8.148 }
8.149
8.150 public:
8.151 +
8.152 /// \brief Insert a pair of item and priority into the heap.
8.153 ///
8.154 - /// Adds \c p.first to the heap with priority \c p.second.
8.155 + /// This function inserts \c p.first to the heap with priority
8.156 + /// \c p.second.
8.157 /// \param p The pair to insert.
8.158 + /// \pre \c p.first must not be stored in the heap.
8.159 void push(const Pair& p) {
8.160 push(p.first, p.second);
8.161 }
8.162
8.163 /// \brief Insert an item into the heap with the given priority.
8.164 ///
8.165 - /// Adds \c i to the heap with priority \c p.
8.166 + /// This function inserts the given item into the heap with the
8.167 + /// given priority.
8.168 /// \param i The item to insert.
8.169 /// \param p The priority of the item.
8.170 + /// \pre \e i must not be stored in the heap.
8.171 void push(const Item &i, const Prio &p) {
8.172 int idx = _data.size();
8.173 _iim[i] = idx;
8.174 @@ -197,10 +210,10 @@
8.175 }
8.176 }
8.177
8.178 - /// \brief Returns the item with minimum priority.
8.179 + /// \brief Return the item having minimum priority.
8.180 ///
8.181 - /// This method returns the item with minimum priority.
8.182 - /// \pre The heap must be nonempty.
8.183 + /// This function returns the item having minimum priority.
8.184 + /// \pre The heap must be non-empty.
8.185 Item top() const {
8.186 while (_first[_minimum] == -1) {
8.187 Direction::increase(_minimum);
8.188 @@ -208,10 +221,10 @@
8.189 return _data[_first[_minimum]].item;
8.190 }
8.191
8.192 - /// \brief Returns the minimum priority.
8.193 + /// \brief The minimum priority.
8.194 ///
8.195 - /// It returns the minimum priority.
8.196 - /// \pre The heap must be nonempty.
8.197 + /// This function returns the minimum priority.
8.198 + /// \pre The heap must be non-empty.
8.199 Prio prio() const {
8.200 while (_first[_minimum] == -1) {
8.201 Direction::increase(_minimum);
8.202 @@ -219,9 +232,9 @@
8.203 return _minimum;
8.204 }
8.205
8.206 - /// \brief Deletes the item with minimum priority.
8.207 + /// \brief Remove the item having minimum priority.
8.208 ///
8.209 - /// This method deletes the item with minimum priority from the heap.
8.210 + /// This function removes the item having minimum priority.
8.211 /// \pre The heap must be non-empty.
8.212 void pop() {
8.213 while (_first[_minimum] == -1) {
8.214 @@ -230,37 +243,38 @@
8.215 int idx = _first[_minimum];
8.216 _iim[_data[idx].item] = -2;
8.217 unlace(idx);
8.218 - relocate_last(idx);
8.219 + relocateLast(idx);
8.220 }
8.221
8.222 - /// \brief Deletes \c i from the heap.
8.223 + /// \brief Remove the given item from the heap.
8.224 ///
8.225 - /// This method deletes item \c i from the heap, if \c i was
8.226 - /// already stored in the heap.
8.227 - /// \param i The item to erase.
8.228 + /// This function removes the given item from the heap if it is
8.229 + /// already stored.
8.230 + /// \param i The item to delete.
8.231 + /// \pre \e i must be in the heap.
8.232 void erase(const Item &i) {
8.233 int idx = _iim[i];
8.234 _iim[_data[idx].item] = -2;
8.235 unlace(idx);
8.236 - relocate_last(idx);
8.237 + relocateLast(idx);
8.238 }
8.239
8.240 -
8.241 - /// \brief Returns the priority of \c i.
8.242 + /// \brief The priority of the given item.
8.243 ///
8.244 - /// This function returns the priority of item \c i.
8.245 - /// \pre \c i must be in the heap.
8.246 + /// This function returns the priority of the given item.
8.247 /// \param i The item.
8.248 + /// \pre \e i must be in the heap.
8.249 Prio operator[](const Item &i) const {
8.250 int idx = _iim[i];
8.251 return _data[idx].value;
8.252 }
8.253
8.254 - /// \brief \c i gets to the heap with priority \c p independently
8.255 - /// if \c i was already there.
8.256 + /// \brief Set the priority of an item or insert it, if it is
8.257 + /// not stored in the heap.
8.258 ///
8.259 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
8.260 - /// in the heap and sets the priority of \c i to \c p otherwise.
8.261 + /// This method sets the priority of the given item if it is
8.262 + /// already stored in the heap. Otherwise it inserts the given
8.263 + /// item into the heap with the given priority.
8.264 /// \param i The item.
8.265 /// \param p The priority.
8.266 void set(const Item &i, const Prio &p) {
8.267 @@ -274,13 +288,12 @@
8.268 }
8.269 }
8.270
8.271 - /// \brief Decreases the priority of \c i to \c p.
8.272 + /// \brief Decrease the priority of an item to the given value.
8.273 ///
8.274 - /// This method decreases the priority of item \c i to \c p.
8.275 - /// \pre \c i must be stored in the heap with priority at least \c
8.276 - /// p relative to \c Compare.
8.277 + /// This function decreases the priority of an item to the given value.
8.278 /// \param i The item.
8.279 /// \param p The priority.
8.280 + /// \pre \e i must be stored in the heap with priority at least \e p.
8.281 void decrease(const Item &i, const Prio &p) {
8.282 int idx = _iim[i];
8.283 unlace(idx);
8.284 @@ -291,13 +304,12 @@
8.285 lace(idx);
8.286 }
8.287
8.288 - /// \brief Increases the priority of \c i to \c p.
8.289 + /// \brief Increase the priority of an item to the given value.
8.290 ///
8.291 - /// This method sets the priority of item \c i to \c p.
8.292 - /// \pre \c i must be stored in the heap with priority at most \c
8.293 - /// p relative to \c Compare.
8.294 + /// This function increases the priority of an item to the given value.
8.295 /// \param i The item.
8.296 /// \param p The priority.
8.297 + /// \pre \e i must be stored in the heap with priority at most \e p.
8.298 void increase(const Item &i, const Prio &p) {
8.299 int idx = _iim[i];
8.300 unlace(idx);
8.301 @@ -305,13 +317,13 @@
8.302 lace(idx);
8.303 }
8.304
8.305 - /// \brief Returns if \c item is in, has already been in, or has
8.306 - /// never been in the heap.
8.307 + /// \brief Return the state of an item.
8.308 ///
8.309 - /// This method returns PRE_HEAP if \c item has never been in the
8.310 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
8.311 - /// otherwise. In the latter case it is possible that \c item will
8.312 - /// get back to the heap again.
8.313 + /// This method returns \c PRE_HEAP if the given item has never
8.314 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
8.315 + /// and \c POST_HEAP otherwise.
8.316 + /// In the latter case it is possible that the item will get back
8.317 + /// to the heap again.
8.318 /// \param i The item.
8.319 State state(const Item &i) const {
8.320 int idx = _iim[i];
8.321 @@ -319,11 +331,11 @@
8.322 return State(idx);
8.323 }
8.324
8.325 - /// \brief Sets the state of the \c item in the heap.
8.326 + /// \brief Set the state of an item in the heap.
8.327 ///
8.328 - /// Sets the state of the \c item in the heap. It can be used to
8.329 - /// manually clear the heap when it is important to achive the
8.330 - /// better time complexity.
8.331 + /// This function sets the state of the given item in the heap.
8.332 + /// It can be used to manually clear the heap when it is important
8.333 + /// to achive better time complexity.
8.334 /// \param i The item.
8.335 /// \param st The state. It should not be \c IN_HEAP.
8.336 void state(const Item& i, State st) {
8.337 @@ -359,33 +371,44 @@
8.338
8.339 }; // class BucketHeap
8.340
8.341 - /// \ingroup auxdat
8.342 + /// \ingroup heaps
8.343 ///
8.344 - /// \brief A Simplified Bucket Heap implementation.
8.345 + /// \brief Simplified bucket heap data structure.
8.346 ///
8.347 /// This class implements a simplified \e bucket \e heap data
8.348 - /// structure. It does not provide some functionality but it faster
8.349 - /// and simplier data structure than the BucketHeap. The main
8.350 - /// difference is that the BucketHeap stores for every key a double
8.351 - /// linked list while this class stores just simple lists. In the
8.352 - /// other way it does not support erasing each elements just the
8.353 - /// minimal and it does not supports key increasing, decreasing.
8.354 + /// structure. It does not provide some functionality, but it is
8.355 + /// faster and simpler than BucketHeap. The main difference is
8.356 + /// that BucketHeap stores a doubly-linked list for each key while
8.357 + /// this class stores only simply-linked lists. It supports erasing
8.358 + /// only for the item having minimum priority and it does not support
8.359 + /// key increasing and decreasing.
8.360 ///
8.361 - /// \param IM A read and write Item int map, used internally
8.362 - /// to handle the cross references.
8.363 - /// \param MIN If the given parameter is false then instead of the
8.364 - /// minimum value the maximum can be retrivied with the top() and
8.365 - /// prio() member functions.
8.366 + /// Note that this implementation does not conform to the
8.367 + /// \ref concepts::Heap "heap concept" due to the lack of some
8.368 + /// functionality.
8.369 + ///
8.370 + /// \tparam IM A read-writable item map with \c int values, used
8.371 + /// internally to handle the cross references.
8.372 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
8.373 + /// The default is \e min-heap. If this parameter is set to \c false,
8.374 + /// then the comparison is reversed, so the top(), prio() and pop()
8.375 + /// functions deal with the item having maximum priority instead of the
8.376 + /// minimum.
8.377 ///
8.378 /// \sa BucketHeap
8.379 template <typename IM, bool MIN = true >
8.380 class SimpleBucketHeap {
8.381
8.382 public:
8.383 - typedef typename IM::Key Item;
8.384 +
8.385 + /// Type of the item-int map.
8.386 + typedef IM ItemIntMap;
8.387 + /// Type of the priorities.
8.388 typedef int Prio;
8.389 - typedef std::pair<Item, Prio> Pair;
8.390 - typedef IM ItemIntMap;
8.391 + /// Type of the items stored in the heap.
8.392 + typedef typename ItemIntMap::Key Item;
8.393 + /// Type of the item-priority pairs.
8.394 + typedef std::pair<Item,Prio> Pair;
8.395
8.396 private:
8.397
8.398 @@ -393,10 +416,10 @@
8.399
8.400 public:
8.401
8.402 - /// \brief Type to represent the items states.
8.403 + /// \brief Type to represent the states of the items.
8.404 ///
8.405 - /// Each Item element have a state associated to it. It may be "in heap",
8.406 - /// "pre heap" or "post heap". The latter two are indifferent from the
8.407 + /// Each item has a state associated to it. It can be "in heap",
8.408 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
8.409 /// heap's point of view, but may be useful to the user.
8.410 ///
8.411 /// The item-int map must be initialized in such way that it assigns
8.412 @@ -409,48 +432,53 @@
8.413
8.414 public:
8.415
8.416 - /// \brief The constructor.
8.417 + /// \brief Constructor.
8.418 ///
8.419 - /// The constructor.
8.420 - /// \param map should be given to the constructor, since it is used
8.421 - /// internally to handle the cross references. The value of the map
8.422 - /// should be PRE_HEAP (-1) for each element.
8.423 + /// Constructor.
8.424 + /// \param map A map that assigns \c int values to the items.
8.425 + /// It is used internally to handle the cross references.
8.426 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
8.427 explicit SimpleBucketHeap(ItemIntMap &map)
8.428 : _iim(map), _free(-1), _num(0), _minimum(0) {}
8.429
8.430 - /// \brief Returns the number of items stored in the heap.
8.431 + /// \brief The number of items stored in the heap.
8.432 ///
8.433 - /// The number of items stored in the heap.
8.434 + /// This function returns the number of items stored in the heap.
8.435 int size() const { return _num; }
8.436
8.437 - /// \brief Checks if the heap stores no items.
8.438 + /// \brief Check if the heap is empty.
8.439 ///
8.440 - /// Returns \c true if and only if the heap stores no items.
8.441 + /// This function returns \c true if the heap is empty.
8.442 bool empty() const { return _num == 0; }
8.443
8.444 - /// \brief Make empty this heap.
8.445 + /// \brief Make the heap empty.
8.446 ///
8.447 - /// Make empty this heap. It does not change the cross reference
8.448 - /// map. If you want to reuse a heap what is not surely empty you
8.449 - /// should first clear the heap and after that you should set the
8.450 - /// cross reference map for each item to \c PRE_HEAP.
8.451 + /// This functon makes the heap empty.
8.452 + /// It does not change the cross reference map. If you want to reuse
8.453 + /// a heap that is not surely empty, you should first clear it and
8.454 + /// then you should set the cross reference map to \c PRE_HEAP
8.455 + /// for each item.
8.456 void clear() {
8.457 _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
8.458 }
8.459
8.460 /// \brief Insert a pair of item and priority into the heap.
8.461 ///
8.462 - /// Adds \c p.first to the heap with priority \c p.second.
8.463 + /// This function inserts \c p.first to the heap with priority
8.464 + /// \c p.second.
8.465 /// \param p The pair to insert.
8.466 + /// \pre \c p.first must not be stored in the heap.
8.467 void push(const Pair& p) {
8.468 push(p.first, p.second);
8.469 }
8.470
8.471 /// \brief Insert an item into the heap with the given priority.
8.472 ///
8.473 - /// Adds \c i to the heap with priority \c p.
8.474 + /// This function inserts the given item into the heap with the
8.475 + /// given priority.
8.476 /// \param i The item to insert.
8.477 /// \param p The priority of the item.
8.478 + /// \pre \e i must not be stored in the heap.
8.479 void push(const Item &i, const Prio &p) {
8.480 int idx;
8.481 if (_free == -1) {
8.482 @@ -471,10 +499,10 @@
8.483 ++_num;
8.484 }
8.485
8.486 - /// \brief Returns the item with minimum priority.
8.487 + /// \brief Return the item having minimum priority.
8.488 ///
8.489 - /// This method returns the item with minimum priority.
8.490 - /// \pre The heap must be nonempty.
8.491 + /// This function returns the item having minimum priority.
8.492 + /// \pre The heap must be non-empty.
8.493 Item top() const {
8.494 while (_first[_minimum] == -1) {
8.495 Direction::increase(_minimum);
8.496 @@ -482,10 +510,10 @@
8.497 return _data[_first[_minimum]].item;
8.498 }
8.499
8.500 - /// \brief Returns the minimum priority.
8.501 + /// \brief The minimum priority.
8.502 ///
8.503 - /// It returns the minimum priority.
8.504 - /// \pre The heap must be nonempty.
8.505 + /// This function returns the minimum priority.
8.506 + /// \pre The heap must be non-empty.
8.507 Prio prio() const {
8.508 while (_first[_minimum] == -1) {
8.509 Direction::increase(_minimum);
8.510 @@ -493,9 +521,9 @@
8.511 return _minimum;
8.512 }
8.513
8.514 - /// \brief Deletes the item with minimum priority.
8.515 + /// \brief Remove the item having minimum priority.
8.516 ///
8.517 - /// This method deletes the item with minimum priority from the heap.
8.518 + /// This function removes the item having minimum priority.
8.519 /// \pre The heap must be non-empty.
8.520 void pop() {
8.521 while (_first[_minimum] == -1) {
8.522 @@ -509,16 +537,15 @@
8.523 --_num;
8.524 }
8.525
8.526 - /// \brief Returns the priority of \c i.
8.527 + /// \brief The priority of the given item.
8.528 ///
8.529 - /// This function returns the priority of item \c i.
8.530 - /// \warning This operator is not a constant time function
8.531 - /// because it scans the whole data structure to find the proper
8.532 - /// value.
8.533 - /// \pre \c i must be in the heap.
8.534 + /// This function returns the priority of the given item.
8.535 /// \param i The item.
8.536 + /// \pre \e i must be in the heap.
8.537 + /// \warning This operator is not a constant time function because
8.538 + /// it scans the whole data structure to find the proper value.
8.539 Prio operator[](const Item &i) const {
8.540 - for (int k = 0; k < _first.size(); ++k) {
8.541 + for (int k = 0; k < int(_first.size()); ++k) {
8.542 int idx = _first[k];
8.543 while (idx != -1) {
8.544 if (_data[idx].item == i) {
8.545 @@ -530,13 +557,13 @@
8.546 return -1;
8.547 }
8.548
8.549 - /// \brief Returns if \c item is in, has already been in, or has
8.550 - /// never been in the heap.
8.551 + /// \brief Return the state of an item.
8.552 ///
8.553 - /// This method returns PRE_HEAP if \c item has never been in the
8.554 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
8.555 - /// otherwise. In the latter case it is possible that \c item will
8.556 - /// get back to the heap again.
8.557 + /// This method returns \c PRE_HEAP if the given item has never
8.558 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
8.559 + /// and \c POST_HEAP otherwise.
8.560 + /// In the latter case it is possible that the item will get back
8.561 + /// to the heap again.
8.562 /// \param i The item.
8.563 State state(const Item &i) const {
8.564 int idx = _iim[i];
9.1 --- a/lemon/circulation.h Fri Jul 24 11:07:52 2009 +0200
9.2 +++ b/lemon/circulation.h Fri Sep 25 09:06:32 2009 +0200
9.3 @@ -457,19 +457,21 @@
9.4 return *_level;
9.5 }
9.6
9.7 - /// \brief Sets the tolerance used by algorithm.
9.8 + /// \brief Sets the tolerance used by the algorithm.
9.9 ///
9.10 - /// Sets the tolerance used by algorithm.
9.11 - Circulation& tolerance(const Tolerance& tolerance) const {
9.12 + /// Sets the tolerance object used by the algorithm.
9.13 + /// \return <tt>(*this)</tt>
9.14 + Circulation& tolerance(const Tolerance& tolerance) {
9.15 _tol = tolerance;
9.16 return *this;
9.17 }
9.18
9.19 /// \brief Returns a const reference to the tolerance.
9.20 ///
9.21 - /// Returns a const reference to the tolerance.
9.22 + /// Returns a const reference to the tolerance object used by
9.23 + /// the algorithm.
9.24 const Tolerance& tolerance() const {
9.25 - return tolerance;
9.26 + return _tol;
9.27 }
9.28
9.29 /// \name Execution Control
10.1 --- a/lemon/concepts/heap.h Fri Jul 24 11:07:52 2009 +0200
10.2 +++ b/lemon/concepts/heap.h Fri Sep 25 09:06:32 2009 +0200
10.3 @@ -16,13 +16,13 @@
10.4 *
10.5 */
10.6
10.7 +#ifndef LEMON_CONCEPTS_HEAP_H
10.8 +#define LEMON_CONCEPTS_HEAP_H
10.9 +
10.10 ///\ingroup concept
10.11 ///\file
10.12 ///\brief The concept of heaps.
10.13
10.14 -#ifndef LEMON_CONCEPTS_HEAP_H
10.15 -#define LEMON_CONCEPTS_HEAP_H
10.16 -
10.17 #include <lemon/core.h>
10.18 #include <lemon/concept_check.h>
10.19
10.20 @@ -35,21 +35,27 @@
10.21
10.22 /// \brief The heap concept.
10.23 ///
10.24 - /// Concept class describing the main interface of heaps. A \e heap
10.25 - /// is a data structure for storing items with specified values called
10.26 - /// \e priorities in such a way that finding the item with minimum
10.27 - /// priority is efficient. In a heap one can change the priority of an
10.28 - /// item, add or erase an item, etc.
10.29 + /// This concept class describes the main interface of heaps.
10.30 + /// The various \ref heaps "heap structures" are efficient
10.31 + /// implementations of the abstract data type \e priority \e queue.
10.32 + /// They store items with specified values called \e priorities
10.33 + /// in such a way that finding and removing the item with minimum
10.34 + /// priority are efficient. The basic operations are adding and
10.35 + /// erasing items, changing the priority of an item, etc.
10.36 ///
10.37 - /// \tparam PR Type of the priority of the items.
10.38 - /// \tparam IM A read and writable item map with int values, used
10.39 + /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
10.40 + /// Any class that conforms to this concept can be used easily in such
10.41 + /// algorithms.
10.42 + ///
10.43 + /// \tparam PR Type of the priorities of the items.
10.44 + /// \tparam IM A read-writable item map with \c int values, used
10.45 /// internally to handle the cross references.
10.46 - /// \tparam Comp A functor class for the ordering of the priorities.
10.47 + /// \tparam CMP A functor class for comparing the priorities.
10.48 /// The default is \c std::less<PR>.
10.49 #ifdef DOXYGEN
10.50 - template <typename PR, typename IM, typename Comp = std::less<PR> >
10.51 + template <typename PR, typename IM, typename CMP>
10.52 #else
10.53 - template <typename PR, typename IM>
10.54 + template <typename PR, typename IM, typename CMP = std::less<PR> >
10.55 #endif
10.56 class Heap {
10.57 public:
10.58 @@ -64,109 +70,125 @@
10.59 /// \brief Type to represent the states of the items.
10.60 ///
10.61 /// Each item has a state associated to it. It can be "in heap",
10.62 - /// "pre heap" or "post heap". The later two are indifferent
10.63 - /// from the point of view of the heap, but may be useful for
10.64 - /// the user.
10.65 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
10.66 + /// heap's point of view, but may be useful to the user.
10.67 ///
10.68 /// The item-int map must be initialized in such way that it assigns
10.69 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
10.70 enum State {
10.71 IN_HEAP = 0, ///< = 0. The "in heap" state constant.
10.72 - PRE_HEAP = -1, ///< = -1. The "pre heap" state constant.
10.73 - POST_HEAP = -2 ///< = -2. The "post heap" state constant.
10.74 + PRE_HEAP = -1, ///< = -1. The "pre-heap" state constant.
10.75 + POST_HEAP = -2 ///< = -2. The "post-heap" state constant.
10.76 };
10.77
10.78 - /// \brief The constructor.
10.79 + /// \brief Constructor.
10.80 ///
10.81 - /// The constructor.
10.82 + /// Constructor.
10.83 /// \param map A map that assigns \c int values to keys of type
10.84 /// \c Item. It is used internally by the heap implementations to
10.85 /// handle the cross references. The assigned value must be
10.86 - /// \c PRE_HEAP (<tt>-1</tt>) for every item.
10.87 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
10.88 explicit Heap(ItemIntMap &map) {}
10.89
10.90 + /// \brief Constructor.
10.91 + ///
10.92 + /// Constructor.
10.93 + /// \param map A map that assigns \c int values to keys of type
10.94 + /// \c Item. It is used internally by the heap implementations to
10.95 + /// handle the cross references. The assigned value must be
10.96 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
10.97 + /// \param comp The function object used for comparing the priorities.
10.98 + explicit Heap(ItemIntMap &map, const CMP &comp) {}
10.99 +
10.100 /// \brief The number of items stored in the heap.
10.101 ///
10.102 - /// Returns the number of items stored in the heap.
10.103 + /// This function returns the number of items stored in the heap.
10.104 int size() const { return 0; }
10.105
10.106 - /// \brief Checks if the heap is empty.
10.107 + /// \brief Check if the heap is empty.
10.108 ///
10.109 - /// Returns \c true if the heap is empty.
10.110 + /// This function returns \c true if the heap is empty.
10.111 bool empty() const { return false; }
10.112
10.113 - /// \brief Makes the heap empty.
10.114 + /// \brief Make the heap empty.
10.115 ///
10.116 - /// Makes the heap empty.
10.117 - void clear();
10.118 + /// This functon makes the heap empty.
10.119 + /// It does not change the cross reference map. If you want to reuse
10.120 + /// a heap that is not surely empty, you should first clear it and
10.121 + /// then you should set the cross reference map to \c PRE_HEAP
10.122 + /// for each item.
10.123 + void clear() {}
10.124
10.125 - /// \brief Inserts an item into the heap with the given priority.
10.126 + /// \brief Insert an item into the heap with the given priority.
10.127 ///
10.128 - /// Inserts the given item into the heap with the given priority.
10.129 + /// This function inserts the given item into the heap with the
10.130 + /// given priority.
10.131 /// \param i The item to insert.
10.132 /// \param p The priority of the item.
10.133 + /// \pre \e i must not be stored in the heap.
10.134 void push(const Item &i, const Prio &p) {}
10.135
10.136 - /// \brief Returns the item having minimum priority.
10.137 + /// \brief Return the item having minimum priority.
10.138 ///
10.139 - /// Returns the item having minimum priority.
10.140 + /// This function returns the item having minimum priority.
10.141 /// \pre The heap must be non-empty.
10.142 Item top() const {}
10.143
10.144 /// \brief The minimum priority.
10.145 ///
10.146 - /// Returns the minimum priority.
10.147 + /// This function returns the minimum priority.
10.148 /// \pre The heap must be non-empty.
10.149 Prio prio() const {}
10.150
10.151 - /// \brief Removes the item having minimum priority.
10.152 + /// \brief Remove the item having minimum priority.
10.153 ///
10.154 - /// Removes the item having minimum priority.
10.155 + /// This function removes the item having minimum priority.
10.156 /// \pre The heap must be non-empty.
10.157 void pop() {}
10.158
10.159 - /// \brief Removes an item from the heap.
10.160 + /// \brief Remove the given item from the heap.
10.161 ///
10.162 - /// Removes the given item from the heap if it is already stored.
10.163 + /// This function removes the given item from the heap if it is
10.164 + /// already stored.
10.165 /// \param i The item to delete.
10.166 + /// \pre \e i must be in the heap.
10.167 void erase(const Item &i) {}
10.168
10.169 - /// \brief The priority of an item.
10.170 + /// \brief The priority of the given item.
10.171 ///
10.172 - /// Returns the priority of the given item.
10.173 + /// This function returns the priority of the given item.
10.174 /// \param i The item.
10.175 - /// \pre \c i must be in the heap.
10.176 + /// \pre \e i must be in the heap.
10.177 Prio operator[](const Item &i) const {}
10.178
10.179 - /// \brief Sets the priority of an item or inserts it, if it is
10.180 + /// \brief Set the priority of an item or insert it, if it is
10.181 /// not stored in the heap.
10.182 ///
10.183 /// This method sets the priority of the given item if it is
10.184 - /// already stored in the heap.
10.185 - /// Otherwise it inserts the given item with the given priority.
10.186 + /// already stored in the heap. Otherwise it inserts the given
10.187 + /// item into the heap with the given priority.
10.188 ///
10.189 /// \param i The item.
10.190 /// \param p The priority.
10.191 void set(const Item &i, const Prio &p) {}
10.192
10.193 - /// \brief Decreases the priority of an item to the given value.
10.194 + /// \brief Decrease the priority of an item to the given value.
10.195 ///
10.196 - /// Decreases the priority of an item to the given value.
10.197 + /// This function decreases the priority of an item to the given value.
10.198 /// \param i The item.
10.199 /// \param p The priority.
10.200 - /// \pre \c i must be stored in the heap with priority at least \c p.
10.201 + /// \pre \e i must be stored in the heap with priority at least \e p.
10.202 void decrease(const Item &i, const Prio &p) {}
10.203
10.204 - /// \brief Increases the priority of an item to the given value.
10.205 + /// \brief Increase the priority of an item to the given value.
10.206 ///
10.207 - /// Increases the priority of an item to the given value.
10.208 + /// This function increases the priority of an item to the given value.
10.209 /// \param i The item.
10.210 /// \param p The priority.
10.211 - /// \pre \c i must be stored in the heap with priority at most \c p.
10.212 + /// \pre \e i must be stored in the heap with priority at most \e p.
10.213 void increase(const Item &i, const Prio &p) {}
10.214
10.215 - /// \brief Returns if an item is in, has already been in, or has
10.216 - /// never been in the heap.
10.217 + /// \brief Return the state of an item.
10.218 ///
10.219 /// This method returns \c PRE_HEAP if the given item has never
10.220 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
10.221 @@ -176,11 +198,11 @@
10.222 /// \param i The item.
10.223 State state(const Item &i) const {}
10.224
10.225 - /// \brief Sets the state of an item in the heap.
10.226 + /// \brief Set the state of an item in the heap.
10.227 ///
10.228 - /// Sets the state of the given item in the heap. It can be used
10.229 - /// to manually clear the heap when it is important to achive the
10.230 - /// better time complexity.
10.231 + /// This function sets the state of the given item in the heap.
10.232 + /// It can be used to manually clear the heap when it is important
10.233 + /// to achive better time complexity.
10.234 /// \param i The item.
10.235 /// \param st The state. It should not be \c IN_HEAP.
10.236 void state(const Item& i, State st) {}
11.1 --- a/lemon/fib_heap.h Fri Jul 24 11:07:52 2009 +0200
11.2 +++ b/lemon/fib_heap.h Fri Sep 25 09:06:32 2009 +0200
11.3 @@ -20,53 +20,49 @@
11.4 #define LEMON_FIB_HEAP_H
11.5
11.6 ///\file
11.7 -///\ingroup auxdat
11.8 -///\brief Fibonacci Heap implementation.
11.9 +///\ingroup heaps
11.10 +///\brief Fibonacci heap implementation.
11.11
11.12 #include <vector>
11.13 +#include <utility>
11.14 #include <functional>
11.15 #include <lemon/math.h>
11.16
11.17 namespace lemon {
11.18
11.19 - /// \ingroup auxdat
11.20 + /// \ingroup heaps
11.21 ///
11.22 - ///\brief Fibonacci Heap.
11.23 + /// \brief Fibonacci heap data structure.
11.24 ///
11.25 - ///This class implements the \e Fibonacci \e heap data structure. A \e heap
11.26 - ///is a data structure for storing items with specified values called \e
11.27 - ///priorities in such a way that finding the item with minimum priority is
11.28 - ///efficient. \c CMP specifies the ordering of the priorities. In a heap
11.29 - ///one can change the priority of an item, add or erase an item, etc.
11.30 + /// This class implements the \e Fibonacci \e heap data structure.
11.31 + /// It fully conforms to the \ref concepts::Heap "heap concept".
11.32 ///
11.33 - ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
11.34 - ///heap. In case of many calls to these operations, it is better to use a
11.35 - ///\ref BinHeap "binary heap".
11.36 + /// The methods \ref increase() and \ref erase() are not efficient in a
11.37 + /// Fibonacci heap. In case of many calls of these operations, it is
11.38 + /// better to use other heap structure, e.g. \ref BinHeap "binary heap".
11.39 ///
11.40 - ///\param PRIO Type of the priority of the items.
11.41 - ///\param IM A read and writable Item int map, used internally
11.42 - ///to handle the cross references.
11.43 - ///\param CMP A class for the ordering of the priorities. The
11.44 - ///default is \c std::less<PRIO>.
11.45 - ///
11.46 - ///\sa BinHeap
11.47 - ///\sa Dijkstra
11.48 + /// \tparam PR Type of the priorities of the items.
11.49 + /// \tparam IM A read-writable item map with \c int values, used
11.50 + /// internally to handle the cross references.
11.51 + /// \tparam CMP A functor class for comparing the priorities.
11.52 + /// The default is \c std::less<PR>.
11.53 #ifdef DOXYGEN
11.54 - template <typename PRIO, typename IM, typename CMP>
11.55 + template <typename PR, typename IM, typename CMP>
11.56 #else
11.57 - template <typename PRIO, typename IM, typename CMP = std::less<PRIO> >
11.58 + template <typename PR, typename IM, typename CMP = std::less<PR> >
11.59 #endif
11.60 class FibHeap {
11.61 public:
11.62 - ///\e
11.63 +
11.64 + /// Type of the item-int map.
11.65 typedef IM ItemIntMap;
11.66 - ///\e
11.67 - typedef PRIO Prio;
11.68 - ///\e
11.69 + /// Type of the priorities.
11.70 + typedef PR Prio;
11.71 + /// Type of the items stored in the heap.
11.72 typedef typename ItemIntMap::Key Item;
11.73 - ///\e
11.74 + /// Type of the item-priority pairs.
11.75 typedef std::pair<Item,Prio> Pair;
11.76 - ///\e
11.77 + /// Functor type for comparing the priorities.
11.78 typedef CMP Compare;
11.79
11.80 private:
11.81 @@ -80,10 +76,10 @@
11.82
11.83 public:
11.84
11.85 - /// \brief Type to represent the items states.
11.86 + /// \brief Type to represent the states of the items.
11.87 ///
11.88 - /// Each Item element have a state associated to it. It may be "in heap",
11.89 - /// "pre heap" or "post heap". The latter two are indifferent from the
11.90 + /// Each item has a state associated to it. It can be "in heap",
11.91 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
11.92 /// heap's point of view, but may be useful to the user.
11.93 ///
11.94 /// The item-int map must be initialized in such way that it assigns
11.95 @@ -94,60 +90,54 @@
11.96 POST_HEAP = -2 ///< = -2.
11.97 };
11.98
11.99 - /// \brief The constructor
11.100 + /// \brief Constructor.
11.101 ///
11.102 - /// \c map should be given to the constructor, since it is
11.103 - /// used internally to handle the cross references.
11.104 + /// Constructor.
11.105 + /// \param map A map that assigns \c int values to the items.
11.106 + /// It is used internally to handle the cross references.
11.107 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
11.108 explicit FibHeap(ItemIntMap &map)
11.109 : _minimum(0), _iim(map), _num() {}
11.110
11.111 - /// \brief The constructor
11.112 + /// \brief Constructor.
11.113 ///
11.114 - /// \c map should be given to the constructor, since it is used
11.115 - /// internally to handle the cross references. \c comp is an
11.116 - /// object for ordering of the priorities.
11.117 + /// Constructor.
11.118 + /// \param map A map that assigns \c int values to the items.
11.119 + /// It is used internally to handle the cross references.
11.120 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
11.121 + /// \param comp The function object used for comparing the priorities.
11.122 FibHeap(ItemIntMap &map, const Compare &comp)
11.123 : _minimum(0), _iim(map), _comp(comp), _num() {}
11.124
11.125 /// \brief The number of items stored in the heap.
11.126 ///
11.127 - /// Returns the number of items stored in the heap.
11.128 + /// This function returns the number of items stored in the heap.
11.129 int size() const { return _num; }
11.130
11.131 - /// \brief Checks if the heap stores no items.
11.132 + /// \brief Check if the heap is empty.
11.133 ///
11.134 - /// Returns \c true if and only if the heap stores no items.
11.135 + /// This function returns \c true if the heap is empty.
11.136 bool empty() const { return _num==0; }
11.137
11.138 - /// \brief Make empty this heap.
11.139 + /// \brief Make the heap empty.
11.140 ///
11.141 - /// Make empty this heap. It does not change the cross reference
11.142 - /// map. If you want to reuse a heap what is not surely empty you
11.143 - /// should first clear the heap and after that you should set the
11.144 - /// cross reference map for each item to \c PRE_HEAP.
11.145 + /// This functon makes the heap empty.
11.146 + /// It does not change the cross reference map. If you want to reuse
11.147 + /// a heap that is not surely empty, you should first clear it and
11.148 + /// then you should set the cross reference map to \c PRE_HEAP
11.149 + /// for each item.
11.150 void clear() {
11.151 _data.clear(); _minimum = 0; _num = 0;
11.152 }
11.153
11.154 - /// \brief \c item gets to the heap with priority \c value independently
11.155 - /// if \c item was already there.
11.156 + /// \brief Insert an item into the heap with the given priority.
11.157 ///
11.158 - /// This method calls \ref push(\c item, \c value) if \c item is not
11.159 - /// stored in the heap and it calls \ref decrease(\c item, \c value) or
11.160 - /// \ref increase(\c item, \c value) otherwise.
11.161 - void set (const Item& item, const Prio& value) {
11.162 - int i=_iim[item];
11.163 - if ( i >= 0 && _data[i].in ) {
11.164 - if ( _comp(value, _data[i].prio) ) decrease(item, value);
11.165 - if ( _comp(_data[i].prio, value) ) increase(item, value);
11.166 - } else push(item, value);
11.167 - }
11.168 -
11.169 - /// \brief Adds \c item to the heap with priority \c value.
11.170 - ///
11.171 - /// Adds \c item to the heap with priority \c value.
11.172 - /// \pre \c item must not be stored in the heap.
11.173 - void push (const Item& item, const Prio& value) {
11.174 + /// This function inserts the given item into the heap with the
11.175 + /// given priority.
11.176 + /// \param item The item to insert.
11.177 + /// \param prio The priority of the item.
11.178 + /// \pre \e item must not be stored in the heap.
11.179 + void push (const Item& item, const Prio& prio) {
11.180 int i=_iim[item];
11.181 if ( i < 0 ) {
11.182 int s=_data.size();
11.183 @@ -168,47 +158,37 @@
11.184 _data[i].right_neighbor=_data[_minimum].right_neighbor;
11.185 _data[_minimum].right_neighbor=i;
11.186 _data[i].left_neighbor=_minimum;
11.187 - if ( _comp( value, _data[_minimum].prio) ) _minimum=i;
11.188 + if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
11.189 } else {
11.190 _data[i].right_neighbor=_data[i].left_neighbor=i;
11.191 _minimum=i;
11.192 }
11.193 - _data[i].prio=value;
11.194 + _data[i].prio=prio;
11.195 ++_num;
11.196 }
11.197
11.198 - /// \brief Returns the item with minimum priority relative to \c Compare.
11.199 + /// \brief Return the item having minimum priority.
11.200 ///
11.201 - /// This method returns the item with minimum priority relative to \c
11.202 - /// Compare.
11.203 - /// \pre The heap must be nonempty.
11.204 + /// This function returns the item having minimum priority.
11.205 + /// \pre The heap must be non-empty.
11.206 Item top() const { return _data[_minimum].name; }
11.207
11.208 - /// \brief Returns the minimum priority relative to \c Compare.
11.209 + /// \brief The minimum priority.
11.210 ///
11.211 - /// It returns the minimum priority relative to \c Compare.
11.212 - /// \pre The heap must be nonempty.
11.213 - const Prio& prio() const { return _data[_minimum].prio; }
11.214 + /// This function returns the minimum priority.
11.215 + /// \pre The heap must be non-empty.
11.216 + Prio prio() const { return _data[_minimum].prio; }
11.217
11.218 - /// \brief Returns the priority of \c item.
11.219 + /// \brief Remove the item having minimum priority.
11.220 ///
11.221 - /// It returns the priority of \c item.
11.222 - /// \pre \c item must be in the heap.
11.223 - const Prio& operator[](const Item& item) const {
11.224 - return _data[_iim[item]].prio;
11.225 - }
11.226 -
11.227 - /// \brief Deletes the item with minimum priority relative to \c Compare.
11.228 - ///
11.229 - /// This method deletes the item with minimum priority relative to \c
11.230 - /// Compare from the heap.
11.231 + /// This function removes the item having minimum priority.
11.232 /// \pre The heap must be non-empty.
11.233 void pop() {
11.234 /*The first case is that there are only one root.*/
11.235 if ( _data[_minimum].left_neighbor==_minimum ) {
11.236 _data[_minimum].in=false;
11.237 if ( _data[_minimum].degree!=0 ) {
11.238 - makeroot(_data[_minimum].child);
11.239 + makeRoot(_data[_minimum].child);
11.240 _minimum=_data[_minimum].child;
11.241 balance();
11.242 }
11.243 @@ -221,7 +201,7 @@
11.244 int child=_data[_minimum].child;
11.245 int last_child=_data[child].left_neighbor;
11.246
11.247 - makeroot(child);
11.248 + makeRoot(child);
11.249
11.250 _data[left].right_neighbor=child;
11.251 _data[child].left_neighbor=left;
11.252 @@ -234,10 +214,12 @@
11.253 --_num;
11.254 }
11.255
11.256 - /// \brief Deletes \c item from the heap.
11.257 + /// \brief Remove the given item from the heap.
11.258 ///
11.259 - /// This method deletes \c item from the heap, if \c item was already
11.260 - /// stored in the heap. It is quite inefficient in Fibonacci heaps.
11.261 + /// This function removes the given item from the heap if it is
11.262 + /// already stored.
11.263 + /// \param item The item to delete.
11.264 + /// \pre \e item must be in the heap.
11.265 void erase (const Item& item) {
11.266 int i=_iim[item];
11.267
11.268 @@ -252,43 +234,68 @@
11.269 }
11.270 }
11.271
11.272 - /// \brief Decreases the priority of \c item to \c value.
11.273 + /// \brief The priority of the given item.
11.274 ///
11.275 - /// This method decreases the priority of \c item to \c value.
11.276 - /// \pre \c item must be stored in the heap with priority at least \c
11.277 - /// value relative to \c Compare.
11.278 - void decrease (Item item, const Prio& value) {
11.279 + /// This function returns the priority of the given item.
11.280 + /// \param item The item.
11.281 + /// \pre \e item must be in the heap.
11.282 + Prio operator[](const Item& item) const {
11.283 + return _data[_iim[item]].prio;
11.284 + }
11.285 +
11.286 + /// \brief Set the priority of an item or insert it, if it is
11.287 + /// not stored in the heap.
11.288 + ///
11.289 + /// This method sets the priority of the given item if it is
11.290 + /// already stored in the heap. Otherwise it inserts the given
11.291 + /// item into the heap with the given priority.
11.292 + /// \param item The item.
11.293 + /// \param prio The priority.
11.294 + void set (const Item& item, const Prio& prio) {
11.295 int i=_iim[item];
11.296 - _data[i].prio=value;
11.297 + if ( i >= 0 && _data[i].in ) {
11.298 + if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
11.299 + if ( _comp(_data[i].prio, prio) ) increase(item, prio);
11.300 + } else push(item, prio);
11.301 + }
11.302 +
11.303 + /// \brief Decrease the priority of an item to the given value.
11.304 + ///
11.305 + /// This function decreases the priority of an item to the given value.
11.306 + /// \param item The item.
11.307 + /// \param prio The priority.
11.308 + /// \pre \e item must be stored in the heap with priority at least \e prio.
11.309 + void decrease (const Item& item, const Prio& prio) {
11.310 + int i=_iim[item];
11.311 + _data[i].prio=prio;
11.312 int p=_data[i].parent;
11.313
11.314 - if ( p!=-1 && _comp(value, _data[p].prio) ) {
11.315 + if ( p!=-1 && _comp(prio, _data[p].prio) ) {
11.316 cut(i,p);
11.317 cascade(p);
11.318 }
11.319 - if ( _comp(value, _data[_minimum].prio) ) _minimum=i;
11.320 + if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
11.321 }
11.322
11.323 - /// \brief Increases the priority of \c item to \c value.
11.324 + /// \brief Increase the priority of an item to the given value.
11.325 ///
11.326 - /// This method sets the priority of \c item to \c value. Though
11.327 - /// there is no precondition on the priority of \c item, this
11.328 - /// method should be used only if it is indeed necessary to increase
11.329 - /// (relative to \c Compare) the priority of \c item, because this
11.330 - /// method is inefficient.
11.331 - void increase (Item item, const Prio& value) {
11.332 + /// This function increases the priority of an item to the given value.
11.333 + /// \param item The item.
11.334 + /// \param prio The priority.
11.335 + /// \pre \e item must be stored in the heap with priority at most \e prio.
11.336 + void increase (const Item& item, const Prio& prio) {
11.337 erase(item);
11.338 - push(item, value);
11.339 + push(item, prio);
11.340 }
11.341
11.342 -
11.343 - /// \brief Returns if \c item is in, has already been in, or has never
11.344 - /// been in the heap.
11.345 + /// \brief Return the state of an item.
11.346 ///
11.347 - /// This method returns PRE_HEAP if \c item has never been in the
11.348 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
11.349 - /// otherwise. In the latter case it is possible that \c item will
11.350 - /// get back to the heap again.
11.351 + /// This method returns \c PRE_HEAP if the given item has never
11.352 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
11.353 + /// and \c POST_HEAP otherwise.
11.354 + /// In the latter case it is possible that the item will get back
11.355 + /// to the heap again.
11.356 + /// \param item The item.
11.357 State state(const Item &item) const {
11.358 int i=_iim[item];
11.359 if( i>=0 ) {
11.360 @@ -298,11 +305,11 @@
11.361 return State(i);
11.362 }
11.363
11.364 - /// \brief Sets the state of the \c item in the heap.
11.365 + /// \brief Set the state of an item in the heap.
11.366 ///
11.367 - /// Sets the state of the \c item in the heap. It can be used to
11.368 - /// manually clear the heap when it is important to achive the
11.369 - /// better time _complexity.
11.370 + /// This function sets the state of the given item in the heap.
11.371 + /// It can be used to manually clear the heap when it is important
11.372 + /// to achive better time complexity.
11.373 /// \param i The item.
11.374 /// \param st The state. It should not be \c IN_HEAP.
11.375 void state(const Item& i, State st) {
11.376 @@ -365,7 +372,7 @@
11.377 } while ( s != m );
11.378 }
11.379
11.380 - void makeroot(int c) {
11.381 + void makeRoot(int c) {
11.382 int s=c;
11.383 do {
11.384 _data[s].parent=-1;
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/lemon/fourary_heap.h Fri Sep 25 09:06:32 2009 +0200
12.3 @@ -0,0 +1,342 @@
12.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
12.5 + *
12.6 + * This file is a part of LEMON, a generic C++ optimization library.
12.7 + *
12.8 + * Copyright (C) 2003-2009
12.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
12.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
12.11 + *
12.12 + * Permission to use, modify and distribute this software is granted
12.13 + * provided that this copyright notice appears in all copies. For
12.14 + * precise terms see the accompanying LICENSE file.
12.15 + *
12.16 + * This software is provided "AS IS" with no warranty of any kind,
12.17 + * express or implied, and with no claim as to its suitability for any
12.18 + * purpose.
12.19 + *
12.20 + */
12.21 +
12.22 +#ifndef LEMON_FOURARY_HEAP_H
12.23 +#define LEMON_FOURARY_HEAP_H
12.24 +
12.25 +///\ingroup heaps
12.26 +///\file
12.27 +///\brief Fourary heap implementation.
12.28 +
12.29 +#include <vector>
12.30 +#include <utility>
12.31 +#include <functional>
12.32 +
12.33 +namespace lemon {
12.34 +
12.35 + /// \ingroup heaps
12.36 + ///
12.37 + ///\brief Fourary heap data structure.
12.38 + ///
12.39 + /// This class implements the \e fourary \e heap data structure.
12.40 + /// It fully conforms to the \ref concepts::Heap "heap concept".
12.41 + ///
12.42 + /// The fourary heap is a specialization of the \ref KaryHeap "K-ary heap"
12.43 + /// for <tt>K=4</tt>. It is similar to the \ref BinHeap "binary heap",
12.44 + /// but its nodes have at most four children, instead of two.
12.45 + ///
12.46 + /// \tparam PR Type of the priorities of the items.
12.47 + /// \tparam IM A read-writable item map with \c int values, used
12.48 + /// internally to handle the cross references.
12.49 + /// \tparam CMP A functor class for comparing the priorities.
12.50 + /// The default is \c std::less<PR>.
12.51 + ///
12.52 + ///\sa BinHeap
12.53 + ///\sa KaryHeap
12.54 +#ifdef DOXYGEN
12.55 + template <typename PR, typename IM, typename CMP>
12.56 +#else
12.57 + template <typename PR, typename IM, typename CMP = std::less<PR> >
12.58 +#endif
12.59 + class FouraryHeap {
12.60 + public:
12.61 + /// Type of the item-int map.
12.62 + typedef IM ItemIntMap;
12.63 + /// Type of the priorities.
12.64 + typedef PR Prio;
12.65 + /// Type of the items stored in the heap.
12.66 + typedef typename ItemIntMap::Key Item;
12.67 + /// Type of the item-priority pairs.
12.68 + typedef std::pair<Item,Prio> Pair;
12.69 + /// Functor type for comparing the priorities.
12.70 + typedef CMP Compare;
12.71 +
12.72 + /// \brief Type to represent the states of the items.
12.73 + ///
12.74 + /// Each item has a state associated to it. It can be "in heap",
12.75 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
12.76 + /// heap's point of view, but may be useful to the user.
12.77 + ///
12.78 + /// The item-int map must be initialized in such way that it assigns
12.79 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
12.80 + enum State {
12.81 + IN_HEAP = 0, ///< = 0.
12.82 + PRE_HEAP = -1, ///< = -1.
12.83 + POST_HEAP = -2 ///< = -2.
12.84 + };
12.85 +
12.86 + private:
12.87 + std::vector<Pair> _data;
12.88 + Compare _comp;
12.89 + ItemIntMap &_iim;
12.90 +
12.91 + public:
12.92 + /// \brief Constructor.
12.93 + ///
12.94 + /// Constructor.
12.95 + /// \param map A map that assigns \c int values to the items.
12.96 + /// It is used internally to handle the cross references.
12.97 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
12.98 + explicit FouraryHeap(ItemIntMap &map) : _iim(map) {}
12.99 +
12.100 + /// \brief Constructor.
12.101 + ///
12.102 + /// Constructor.
12.103 + /// \param map A map that assigns \c int values to the items.
12.104 + /// It is used internally to handle the cross references.
12.105 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
12.106 + /// \param comp The function object used for comparing the priorities.
12.107 + FouraryHeap(ItemIntMap &map, const Compare &comp)
12.108 + : _iim(map), _comp(comp) {}
12.109 +
12.110 + /// \brief The number of items stored in the heap.
12.111 + ///
12.112 + /// This function returns the number of items stored in the heap.
12.113 + int size() const { return _data.size(); }
12.114 +
12.115 + /// \brief Check if the heap is empty.
12.116 + ///
12.117 + /// This function returns \c true if the heap is empty.
12.118 + bool empty() const { return _data.empty(); }
12.119 +
12.120 + /// \brief Make the heap empty.
12.121 + ///
12.122 + /// This functon makes the heap empty.
12.123 + /// It does not change the cross reference map. If you want to reuse
12.124 + /// a heap that is not surely empty, you should first clear it and
12.125 + /// then you should set the cross reference map to \c PRE_HEAP
12.126 + /// for each item.
12.127 + void clear() { _data.clear(); }
12.128 +
12.129 + private:
12.130 + static int parent(int i) { return (i-1)/4; }
12.131 + static int firstChild(int i) { return 4*i+1; }
12.132 +
12.133 + bool less(const Pair &p1, const Pair &p2) const {
12.134 + return _comp(p1.second, p2.second);
12.135 + }
12.136 +
12.137 + void bubbleUp(int hole, Pair p) {
12.138 + int par = parent(hole);
12.139 + while( hole>0 && less(p,_data[par]) ) {
12.140 + move(_data[par],hole);
12.141 + hole = par;
12.142 + par = parent(hole);
12.143 + }
12.144 + move(p, hole);
12.145 + }
12.146 +
12.147 + void bubbleDown(int hole, Pair p, int length) {
12.148 + if( length>1 ) {
12.149 + int child = firstChild(hole);
12.150 + while( child+3<length ) {
12.151 + int min=child;
12.152 + if( less(_data[++child], _data[min]) ) min=child;
12.153 + if( less(_data[++child], _data[min]) ) min=child;
12.154 + if( less(_data[++child], _data[min]) ) min=child;
12.155 + if( !less(_data[min], p) )
12.156 + goto ok;
12.157 + move(_data[min], hole);
12.158 + hole = min;
12.159 + child = firstChild(hole);
12.160 + }
12.161 + if ( child<length ) {
12.162 + int min = child;
12.163 + if( ++child<length && less(_data[child], _data[min]) ) min=child;
12.164 + if( ++child<length && less(_data[child], _data[min]) ) min=child;
12.165 + if( less(_data[min], p) ) {
12.166 + move(_data[min], hole);
12.167 + hole = min;
12.168 + }
12.169 + }
12.170 + }
12.171 + ok:
12.172 + move(p, hole);
12.173 + }
12.174 +
12.175 + void move(const Pair &p, int i) {
12.176 + _data[i] = p;
12.177 + _iim.set(p.first, i);
12.178 + }
12.179 +
12.180 + public:
12.181 + /// \brief Insert a pair of item and priority into the heap.
12.182 + ///
12.183 + /// This function inserts \c p.first to the heap with priority
12.184 + /// \c p.second.
12.185 + /// \param p The pair to insert.
12.186 + /// \pre \c p.first must not be stored in the heap.
12.187 + void push(const Pair &p) {
12.188 + int n = _data.size();
12.189 + _data.resize(n+1);
12.190 + bubbleUp(n, p);
12.191 + }
12.192 +
12.193 + /// \brief Insert an item into the heap with the given priority.
12.194 + ///
12.195 + /// This function inserts the given item into the heap with the
12.196 + /// given priority.
12.197 + /// \param i The item to insert.
12.198 + /// \param p The priority of the item.
12.199 + /// \pre \e i must not be stored in the heap.
12.200 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
12.201 +
12.202 + /// \brief Return the item having minimum priority.
12.203 + ///
12.204 + /// This function returns the item having minimum priority.
12.205 + /// \pre The heap must be non-empty.
12.206 + Item top() const { return _data[0].first; }
12.207 +
12.208 + /// \brief The minimum priority.
12.209 + ///
12.210 + /// This function returns the minimum priority.
12.211 + /// \pre The heap must be non-empty.
12.212 + Prio prio() const { return _data[0].second; }
12.213 +
12.214 + /// \brief Remove the item having minimum priority.
12.215 + ///
12.216 + /// This function removes the item having minimum priority.
12.217 + /// \pre The heap must be non-empty.
12.218 + void pop() {
12.219 + int n = _data.size()-1;
12.220 + _iim.set(_data[0].first, POST_HEAP);
12.221 + if (n>0) bubbleDown(0, _data[n], n);
12.222 + _data.pop_back();
12.223 + }
12.224 +
12.225 + /// \brief Remove the given item from the heap.
12.226 + ///
12.227 + /// This function removes the given item from the heap if it is
12.228 + /// already stored.
12.229 + /// \param i The item to delete.
12.230 + /// \pre \e i must be in the heap.
12.231 + void erase(const Item &i) {
12.232 + int h = _iim[i];
12.233 + int n = _data.size()-1;
12.234 + _iim.set(_data[h].first, POST_HEAP);
12.235 + if( h<n ) {
12.236 + if( less(_data[parent(h)], _data[n]) )
12.237 + bubbleDown(h, _data[n], n);
12.238 + else
12.239 + bubbleUp(h, _data[n]);
12.240 + }
12.241 + _data.pop_back();
12.242 + }
12.243 +
12.244 + /// \brief The priority of the given item.
12.245 + ///
12.246 + /// This function returns the priority of the given item.
12.247 + /// \param i The item.
12.248 + /// \pre \e i must be in the heap.
12.249 + Prio operator[](const Item &i) const {
12.250 + int idx = _iim[i];
12.251 + return _data[idx].second;
12.252 + }
12.253 +
12.254 + /// \brief Set the priority of an item or insert it, if it is
12.255 + /// not stored in the heap.
12.256 + ///
12.257 + /// This method sets the priority of the given item if it is
12.258 + /// already stored in the heap. Otherwise it inserts the given
12.259 + /// item into the heap with the given priority.
12.260 + /// \param i The item.
12.261 + /// \param p The priority.
12.262 + void set(const Item &i, const Prio &p) {
12.263 + int idx = _iim[i];
12.264 + if( idx < 0 )
12.265 + push(i,p);
12.266 + else if( _comp(p, _data[idx].second) )
12.267 + bubbleUp(idx, Pair(i,p));
12.268 + else
12.269 + bubbleDown(idx, Pair(i,p), _data.size());
12.270 + }
12.271 +
12.272 + /// \brief Decrease the priority of an item to the given value.
12.273 + ///
12.274 + /// This function decreases the priority of an item to the given value.
12.275 + /// \param i The item.
12.276 + /// \param p The priority.
12.277 + /// \pre \e i must be stored in the heap with priority at least \e p.
12.278 + void decrease(const Item &i, const Prio &p) {
12.279 + int idx = _iim[i];
12.280 + bubbleUp(idx, Pair(i,p));
12.281 + }
12.282 +
12.283 + /// \brief Increase the priority of an item to the given value.
12.284 + ///
12.285 + /// This function increases the priority of an item to the given value.
12.286 + /// \param i The item.
12.287 + /// \param p The priority.
12.288 + /// \pre \e i must be stored in the heap with priority at most \e p.
12.289 + void increase(const Item &i, const Prio &p) {
12.290 + int idx = _iim[i];
12.291 + bubbleDown(idx, Pair(i,p), _data.size());
12.292 + }
12.293 +
12.294 + /// \brief Return the state of an item.
12.295 + ///
12.296 + /// This method returns \c PRE_HEAP if the given item has never
12.297 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
12.298 + /// and \c POST_HEAP otherwise.
12.299 + /// In the latter case it is possible that the item will get back
12.300 + /// to the heap again.
12.301 + /// \param i The item.
12.302 + State state(const Item &i) const {
12.303 + int s = _iim[i];
12.304 + if (s>=0) s=0;
12.305 + return State(s);
12.306 + }
12.307 +
12.308 + /// \brief Set the state of an item in the heap.
12.309 + ///
12.310 + /// This function sets the state of the given item in the heap.
12.311 + /// It can be used to manually clear the heap when it is important
12.312 + /// to achive better time complexity.
12.313 + /// \param i The item.
12.314 + /// \param st The state. It should not be \c IN_HEAP.
12.315 + void state(const Item& i, State st) {
12.316 + switch (st) {
12.317 + case POST_HEAP:
12.318 + case PRE_HEAP:
12.319 + if (state(i) == IN_HEAP) erase(i);
12.320 + _iim[i] = st;
12.321 + break;
12.322 + case IN_HEAP:
12.323 + break;
12.324 + }
12.325 + }
12.326 +
12.327 + /// \brief Replace an item in the heap.
12.328 + ///
12.329 + /// This function replaces item \c i with item \c j.
12.330 + /// Item \c i must be in the heap, while \c j must be out of the heap.
12.331 + /// After calling this method, item \c i will be out of the
12.332 + /// heap and \c j will be in the heap with the same prioriority
12.333 + /// as item \c i had before.
12.334 + void replace(const Item& i, const Item& j) {
12.335 + int idx = _iim[i];
12.336 + _iim.set(i, _iim[j]);
12.337 + _iim.set(j, idx);
12.338 + _data[idx].first = j;
12.339 + }
12.340 +
12.341 + }; // class FouraryHeap
12.342 +
12.343 +} // namespace lemon
12.344 +
12.345 +#endif // LEMON_FOURARY_HEAP_H
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/lemon/kary_heap.h Fri Sep 25 09:06:32 2009 +0200
13.3 @@ -0,0 +1,352 @@
13.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
13.5 + *
13.6 + * This file is a part of LEMON, a generic C++ optimization library.
13.7 + *
13.8 + * Copyright (C) 2003-2009
13.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
13.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
13.11 + *
13.12 + * Permission to use, modify and distribute this software is granted
13.13 + * provided that this copyright notice appears in all copies. For
13.14 + * precise terms see the accompanying LICENSE file.
13.15 + *
13.16 + * This software is provided "AS IS" with no warranty of any kind,
13.17 + * express or implied, and with no claim as to its suitability for any
13.18 + * purpose.
13.19 + *
13.20 + */
13.21 +
13.22 +#ifndef LEMON_KARY_HEAP_H
13.23 +#define LEMON_KARY_HEAP_H
13.24 +
13.25 +///\ingroup heaps
13.26 +///\file
13.27 +///\brief Fourary heap implementation.
13.28 +
13.29 +#include <vector>
13.30 +#include <utility>
13.31 +#include <functional>
13.32 +
13.33 +namespace lemon {
13.34 +
13.35 + /// \ingroup heaps
13.36 + ///
13.37 + ///\brief K-ary heap data structure.
13.38 + ///
13.39 + /// This class implements the \e K-ary \e heap data structure.
13.40 + /// It fully conforms to the \ref concepts::Heap "heap concept".
13.41 + ///
13.42 + /// The \ref KaryHeap "K-ary heap" is a generalization of the
13.43 + /// \ref BinHeap "binary heap" structure, its nodes have at most
13.44 + /// \c K children, instead of two.
13.45 + /// \ref BinHeap and \ref FouraryHeap are specialized implementations
13.46 + /// of this structure for <tt>K=2</tt> and <tt>K=4</tt>, respectively.
13.47 + ///
13.48 + /// \tparam PR Type of the priorities of the items.
13.49 + /// \tparam IM A read-writable item map with \c int values, used
13.50 + /// internally to handle the cross references.
13.51 + /// \tparam K The degree of the heap, each node have at most \e K
13.52 + /// children. The default is 16. Powers of two are suggested to use
13.53 + /// so that the multiplications and divisions needed to traverse the
13.54 + /// nodes of the heap could be performed faster.
13.55 + /// \tparam CMP A functor class for comparing the priorities.
13.56 + /// The default is \c std::less<PR>.
13.57 + ///
13.58 + ///\sa BinHeap
13.59 + ///\sa FouraryHeap
13.60 +#ifdef DOXYGEN
13.61 + template <typename PR, typename IM, int K, typename CMP>
13.62 +#else
13.63 + template <typename PR, typename IM, int K = 16,
13.64 + typename CMP = std::less<PR> >
13.65 +#endif
13.66 + class KaryHeap {
13.67 + public:
13.68 + /// Type of the item-int map.
13.69 + typedef IM ItemIntMap;
13.70 + /// Type of the priorities.
13.71 + typedef PR Prio;
13.72 + /// Type of the items stored in the heap.
13.73 + typedef typename ItemIntMap::Key Item;
13.74 + /// Type of the item-priority pairs.
13.75 + typedef std::pair<Item,Prio> Pair;
13.76 + /// Functor type for comparing the priorities.
13.77 + typedef CMP Compare;
13.78 +
13.79 + /// \brief Type to represent the states of the items.
13.80 + ///
13.81 + /// Each item has a state associated to it. It can be "in heap",
13.82 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
13.83 + /// heap's point of view, but may be useful to the user.
13.84 + ///
13.85 + /// The item-int map must be initialized in such way that it assigns
13.86 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
13.87 + enum State {
13.88 + IN_HEAP = 0, ///< = 0.
13.89 + PRE_HEAP = -1, ///< = -1.
13.90 + POST_HEAP = -2 ///< = -2.
13.91 + };
13.92 +
13.93 + private:
13.94 + std::vector<Pair> _data;
13.95 + Compare _comp;
13.96 + ItemIntMap &_iim;
13.97 +
13.98 + public:
13.99 + /// \brief Constructor.
13.100 + ///
13.101 + /// Constructor.
13.102 + /// \param map A map that assigns \c int values to the items.
13.103 + /// It is used internally to handle the cross references.
13.104 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
13.105 + explicit KaryHeap(ItemIntMap &map) : _iim(map) {}
13.106 +
13.107 + /// \brief Constructor.
13.108 + ///
13.109 + /// Constructor.
13.110 + /// \param map A map that assigns \c int values to the items.
13.111 + /// It is used internally to handle the cross references.
13.112 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
13.113 + /// \param comp The function object used for comparing the priorities.
13.114 + KaryHeap(ItemIntMap &map, const Compare &comp)
13.115 + : _iim(map), _comp(comp) {}
13.116 +
13.117 + /// \brief The number of items stored in the heap.
13.118 + ///
13.119 + /// This function returns the number of items stored in the heap.
13.120 + int size() const { return _data.size(); }
13.121 +
13.122 + /// \brief Check if the heap is empty.
13.123 + ///
13.124 + /// This function returns \c true if the heap is empty.
13.125 + bool empty() const { return _data.empty(); }
13.126 +
13.127 + /// \brief Make the heap empty.
13.128 + ///
13.129 + /// This functon makes the heap empty.
13.130 + /// It does not change the cross reference map. If you want to reuse
13.131 + /// a heap that is not surely empty, you should first clear it and
13.132 + /// then you should set the cross reference map to \c PRE_HEAP
13.133 + /// for each item.
13.134 + void clear() { _data.clear(); }
13.135 +
13.136 + private:
13.137 + int parent(int i) { return (i-1)/K; }
13.138 + int firstChild(int i) { return K*i+1; }
13.139 +
13.140 + bool less(const Pair &p1, const Pair &p2) const {
13.141 + return _comp(p1.second, p2.second);
13.142 + }
13.143 +
13.144 + void bubbleUp(int hole, Pair p) {
13.145 + int par = parent(hole);
13.146 + while( hole>0 && less(p,_data[par]) ) {
13.147 + move(_data[par],hole);
13.148 + hole = par;
13.149 + par = parent(hole);
13.150 + }
13.151 + move(p, hole);
13.152 + }
13.153 +
13.154 + void bubbleDown(int hole, Pair p, int length) {
13.155 + if( length>1 ) {
13.156 + int child = firstChild(hole);
13.157 + while( child+K<=length ) {
13.158 + int min=child;
13.159 + for (int i=1; i<K; ++i) {
13.160 + if( less(_data[child+i], _data[min]) )
13.161 + min=child+i;
13.162 + }
13.163 + if( !less(_data[min], p) )
13.164 + goto ok;
13.165 + move(_data[min], hole);
13.166 + hole = min;
13.167 + child = firstChild(hole);
13.168 + }
13.169 + if ( child<length ) {
13.170 + int min = child;
13.171 + while (++child < length) {
13.172 + if( less(_data[child], _data[min]) )
13.173 + min=child;
13.174 + }
13.175 + if( less(_data[min], p) ) {
13.176 + move(_data[min], hole);
13.177 + hole = min;
13.178 + }
13.179 + }
13.180 + }
13.181 + ok:
13.182 + move(p, hole);
13.183 + }
13.184 +
13.185 + void move(const Pair &p, int i) {
13.186 + _data[i] = p;
13.187 + _iim.set(p.first, i);
13.188 + }
13.189 +
13.190 + public:
13.191 + /// \brief Insert a pair of item and priority into the heap.
13.192 + ///
13.193 + /// This function inserts \c p.first to the heap with priority
13.194 + /// \c p.second.
13.195 + /// \param p The pair to insert.
13.196 + /// \pre \c p.first must not be stored in the heap.
13.197 + void push(const Pair &p) {
13.198 + int n = _data.size();
13.199 + _data.resize(n+1);
13.200 + bubbleUp(n, p);
13.201 + }
13.202 +
13.203 + /// \brief Insert an item into the heap with the given priority.
13.204 + ///
13.205 + /// This function inserts the given item into the heap with the
13.206 + /// given priority.
13.207 + /// \param i The item to insert.
13.208 + /// \param p The priority of the item.
13.209 + /// \pre \e i must not be stored in the heap.
13.210 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
13.211 +
13.212 + /// \brief Return the item having minimum priority.
13.213 + ///
13.214 + /// This function returns the item having minimum priority.
13.215 + /// \pre The heap must be non-empty.
13.216 + Item top() const { return _data[0].first; }
13.217 +
13.218 + /// \brief The minimum priority.
13.219 + ///
13.220 + /// This function returns the minimum priority.
13.221 + /// \pre The heap must be non-empty.
13.222 + Prio prio() const { return _data[0].second; }
13.223 +
13.224 + /// \brief Remove the item having minimum priority.
13.225 + ///
13.226 + /// This function removes the item having minimum priority.
13.227 + /// \pre The heap must be non-empty.
13.228 + void pop() {
13.229 + int n = _data.size()-1;
13.230 + _iim.set(_data[0].first, POST_HEAP);
13.231 + if (n>0) bubbleDown(0, _data[n], n);
13.232 + _data.pop_back();
13.233 + }
13.234 +
13.235 + /// \brief Remove the given item from the heap.
13.236 + ///
13.237 + /// This function removes the given item from the heap if it is
13.238 + /// already stored.
13.239 + /// \param i The item to delete.
13.240 + /// \pre \e i must be in the heap.
13.241 + void erase(const Item &i) {
13.242 + int h = _iim[i];
13.243 + int n = _data.size()-1;
13.244 + _iim.set(_data[h].first, POST_HEAP);
13.245 + if( h<n ) {
13.246 + if( less(_data[parent(h)], _data[n]) )
13.247 + bubbleDown(h, _data[n], n);
13.248 + else
13.249 + bubbleUp(h, _data[n]);
13.250 + }
13.251 + _data.pop_back();
13.252 + }
13.253 +
13.254 + /// \brief The priority of the given item.
13.255 + ///
13.256 + /// This function returns the priority of the given item.
13.257 + /// \param i The item.
13.258 + /// \pre \e i must be in the heap.
13.259 + Prio operator[](const Item &i) const {
13.260 + int idx = _iim[i];
13.261 + return _data[idx].second;
13.262 + }
13.263 +
13.264 + /// \brief Set the priority of an item or insert it, if it is
13.265 + /// not stored in the heap.
13.266 + ///
13.267 + /// This method sets the priority of the given item if it is
13.268 + /// already stored in the heap. Otherwise it inserts the given
13.269 + /// item into the heap with the given priority.
13.270 + /// \param i The item.
13.271 + /// \param p The priority.
13.272 + void set(const Item &i, const Prio &p) {
13.273 + int idx = _iim[i];
13.274 + if( idx<0 )
13.275 + push(i,p);
13.276 + else if( _comp(p, _data[idx].second) )
13.277 + bubbleUp(idx, Pair(i,p));
13.278 + else
13.279 + bubbleDown(idx, Pair(i,p), _data.size());
13.280 + }
13.281 +
13.282 + /// \brief Decrease the priority of an item to the given value.
13.283 + ///
13.284 + /// This function decreases the priority of an item to the given value.
13.285 + /// \param i The item.
13.286 + /// \param p The priority.
13.287 + /// \pre \e i must be stored in the heap with priority at least \e p.
13.288 + void decrease(const Item &i, const Prio &p) {
13.289 + int idx = _iim[i];
13.290 + bubbleUp(idx, Pair(i,p));
13.291 + }
13.292 +
13.293 + /// \brief Increase the priority of an item to the given value.
13.294 + ///
13.295 + /// This function increases the priority of an item to the given value.
13.296 + /// \param i The item.
13.297 + /// \param p The priority.
13.298 + /// \pre \e i must be stored in the heap with priority at most \e p.
13.299 + void increase(const Item &i, const Prio &p) {
13.300 + int idx = _iim[i];
13.301 + bubbleDown(idx, Pair(i,p), _data.size());
13.302 + }
13.303 +
13.304 + /// \brief Return the state of an item.
13.305 + ///
13.306 + /// This method returns \c PRE_HEAP if the given item has never
13.307 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
13.308 + /// and \c POST_HEAP otherwise.
13.309 + /// In the latter case it is possible that the item will get back
13.310 + /// to the heap again.
13.311 + /// \param i The item.
13.312 + State state(const Item &i) const {
13.313 + int s = _iim[i];
13.314 + if (s>=0) s=0;
13.315 + return State(s);
13.316 + }
13.317 +
13.318 + /// \brief Set the state of an item in the heap.
13.319 + ///
13.320 + /// This function sets the state of the given item in the heap.
13.321 + /// It can be used to manually clear the heap when it is important
13.322 + /// to achive better time complexity.
13.323 + /// \param i The item.
13.324 + /// \param st The state. It should not be \c IN_HEAP.
13.325 + void state(const Item& i, State st) {
13.326 + switch (st) {
13.327 + case POST_HEAP:
13.328 + case PRE_HEAP:
13.329 + if (state(i) == IN_HEAP) erase(i);
13.330 + _iim[i] = st;
13.331 + break;
13.332 + case IN_HEAP:
13.333 + break;
13.334 + }
13.335 + }
13.336 +
13.337 + /// \brief Replace an item in the heap.
13.338 + ///
13.339 + /// This function replaces item \c i with item \c j.
13.340 + /// Item \c i must be in the heap, while \c j must be out of the heap.
13.341 + /// After calling this method, item \c i will be out of the
13.342 + /// heap and \c j will be in the heap with the same prioriority
13.343 + /// as item \c i had before.
13.344 + void replace(const Item& i, const Item& j) {
13.345 + int idx=_iim[i];
13.346 + _iim.set(i, _iim[j]);
13.347 + _iim.set(j, idx);
13.348 + _data[idx].first=j;
13.349 + }
13.350 +
13.351 + }; // class KaryHeap
13.352 +
13.353 +} // namespace lemon
13.354 +
13.355 +#endif // LEMON_KARY_HEAP_H
14.1 --- a/lemon/maps.h Fri Jul 24 11:07:52 2009 +0200
14.2 +++ b/lemon/maps.h Fri Sep 25 09:06:32 2009 +0200
14.3 @@ -22,6 +22,7 @@
14.4 #include <iterator>
14.5 #include <functional>
14.6 #include <vector>
14.7 +#include <map>
14.8
14.9 #include <lemon/core.h>
14.10
14.11 @@ -29,8 +30,6 @@
14.12 ///\ingroup maps
14.13 ///\brief Miscellaneous property maps
14.14
14.15 -#include <map>
14.16 -
14.17 namespace lemon {
14.18
14.19 /// \addtogroup maps
14.20 @@ -1818,7 +1817,7 @@
14.21 /// \brief Provides an immutable and unique id for each item in a graph.
14.22 ///
14.23 /// IdMap provides a unique and immutable id for each item of the
14.24 - /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
14.25 + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
14.26 /// - \b unique: different items get different ids,
14.27 /// - \b immutable: the id of an item does not change (even if you
14.28 /// delete other nodes).
14.29 @@ -1902,13 +1901,14 @@
14.30 /// \brief General cross reference graph map type.
14.31
14.32 /// This class provides simple invertable graph maps.
14.33 - /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
14.34 - /// and if a key is set to a new value then store it
14.35 - /// in the inverse map.
14.36 - ///
14.37 + /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
14.38 + /// and if a key is set to a new value, then stores it in the inverse map.
14.39 /// The values of the map can be accessed
14.40 /// with stl compatible forward iterator.
14.41 ///
14.42 + /// This type is not reference map, so it cannot be modified with
14.43 + /// the subscript operator.
14.44 + ///
14.45 /// \tparam GR The graph type.
14.46 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
14.47 /// \c GR::Edge).
14.48 @@ -1923,7 +1923,7 @@
14.49 typedef typename ItemSetTraits<GR, K>::
14.50 template Map<V>::Type Map;
14.51
14.52 - typedef std::map<V, K> Container;
14.53 + typedef std::multimap<V, K> Container;
14.54 Container _inv_map;
14.55
14.56 public:
14.57 @@ -1948,6 +1948,8 @@
14.58 /// This iterator is an stl compatible forward
14.59 /// iterator on the values of the map. The values can
14.60 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
14.61 + /// They are considered with multiplicity, so each value is
14.62 + /// traversed for each item it is assigned to.
14.63 class ValueIterator
14.64 : public std::iterator<std::forward_iterator_tag, Value> {
14.65 friend class CrossRefMap;
14.66 @@ -2000,11 +2002,15 @@
14.67 /// Sets the value associated with the given key.
14.68 void set(const Key& key, const Value& val) {
14.69 Value oldval = Map::operator[](key);
14.70 - typename Container::iterator it = _inv_map.find(oldval);
14.71 - if (it != _inv_map.end() && it->second == key) {
14.72 - _inv_map.erase(it);
14.73 + typename Container::iterator it;
14.74 + for (it = _inv_map.equal_range(oldval).first;
14.75 + it != _inv_map.equal_range(oldval).second; ++it) {
14.76 + if (it->second == key) {
14.77 + _inv_map.erase(it);
14.78 + break;
14.79 + }
14.80 }
14.81 - _inv_map.insert(make_pair(val, key));
14.82 + _inv_map.insert(std::make_pair(val, key));
14.83 Map::set(key, val);
14.84 }
14.85
14.86 @@ -2016,11 +2022,14 @@
14.87 return Map::operator[](key);
14.88 }
14.89
14.90 - /// \brief Gives back the item by its value.
14.91 + /// \brief Gives back an item by its value.
14.92 ///
14.93 - /// Gives back the item by its value.
14.94 - Key operator()(const Value& key) const {
14.95 - typename Container::const_iterator it = _inv_map.find(key);
14.96 + /// This function gives back an item that is assigned to
14.97 + /// the given value or \c INVALID if no such item exists.
14.98 + /// If there are more items with the same associated value,
14.99 + /// only one of them is returned.
14.100 + Key operator()(const Value& val) const {
14.101 + typename Container::const_iterator it = _inv_map.find(val);
14.102 return it != _inv_map.end() ? it->second : INVALID;
14.103 }
14.104
14.105 @@ -2032,9 +2041,13 @@
14.106 /// \c AlterationNotifier.
14.107 virtual void erase(const Key& key) {
14.108 Value val = Map::operator[](key);
14.109 - typename Container::iterator it = _inv_map.find(val);
14.110 - if (it != _inv_map.end() && it->second == key) {
14.111 - _inv_map.erase(it);
14.112 + typename Container::iterator it;
14.113 + for (it = _inv_map.equal_range(val).first;
14.114 + it != _inv_map.equal_range(val).second; ++it) {
14.115 + if (it->second == key) {
14.116 + _inv_map.erase(it);
14.117 + break;
14.118 + }
14.119 }
14.120 Map::erase(key);
14.121 }
14.122 @@ -2046,9 +2059,13 @@
14.123 virtual void erase(const std::vector<Key>& keys) {
14.124 for (int i = 0; i < int(keys.size()); ++i) {
14.125 Value val = Map::operator[](keys[i]);
14.126 - typename Container::iterator it = _inv_map.find(val);
14.127 - if (it != _inv_map.end() && it->second == keys[i]) {
14.128 - _inv_map.erase(it);
14.129 + typename Container::iterator it;
14.130 + for (it = _inv_map.equal_range(val).first;
14.131 + it != _inv_map.equal_range(val).second; ++it) {
14.132 + if (it->second == keys[i]) {
14.133 + _inv_map.erase(it);
14.134 + break;
14.135 + }
14.136 }
14.137 }
14.138 Map::erase(keys);
14.139 @@ -2084,8 +2101,9 @@
14.140
14.141 /// \brief Subscript operator.
14.142 ///
14.143 - /// Subscript operator. It gives back the item
14.144 - /// that was last assigned to the given value.
14.145 + /// Subscript operator. It gives back an item
14.146 + /// that is assigned to the given value or \c INVALID
14.147 + /// if no such item exists.
14.148 Value operator[](const Key& key) const {
14.149 return _inverted(key);
14.150 }
14.151 @@ -2254,7 +2272,7 @@
14.152 }
14.153
14.154 /// \brief Gives back the item belonging to a \e RangeId
14.155 - ///
14.156 + ///
14.157 /// Gives back the item belonging to a \e RangeId.
14.158 Item operator()(int id) const {
14.159 return _inv_map[id];
14.160 @@ -2311,6 +2329,903 @@
14.161 }
14.162 };
14.163
14.164 + /// \brief Dynamic iterable \c bool map.
14.165 + ///
14.166 + /// This class provides a special graph map type which can store a
14.167 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
14.168 + /// For both \c true and \c false values it is possible to iterate on
14.169 + /// the keys.
14.170 + ///
14.171 + /// This type is a reference map, so it can be modified with the
14.172 + /// subscript operator.
14.173 + ///
14.174 + /// \tparam GR The graph type.
14.175 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
14.176 + /// \c GR::Edge).
14.177 + ///
14.178 + /// \see IterableIntMap, IterableValueMap
14.179 + /// \see CrossRefMap
14.180 + template <typename GR, typename K>
14.181 + class IterableBoolMap
14.182 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
14.183 + private:
14.184 + typedef GR Graph;
14.185 +
14.186 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
14.187 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
14.188 +
14.189 + std::vector<K> _array;
14.190 + int _sep;
14.191 +
14.192 + public:
14.193 +
14.194 + /// Indicates that the map is reference map.
14.195 + typedef True ReferenceMapTag;
14.196 +
14.197 + /// The key type
14.198 + typedef K Key;
14.199 + /// The value type
14.200 + typedef bool Value;
14.201 + /// The const reference type.
14.202 + typedef const Value& ConstReference;
14.203 +
14.204 + private:
14.205 +
14.206 + int position(const Key& key) const {
14.207 + return Parent::operator[](key);
14.208 + }
14.209 +
14.210 + public:
14.211 +
14.212 + /// \brief Reference to the value of the map.
14.213 + ///
14.214 + /// This class is similar to the \c bool type. It can be converted to
14.215 + /// \c bool and it provides the same operators.
14.216 + class Reference {
14.217 + friend class IterableBoolMap;
14.218 + private:
14.219 + Reference(IterableBoolMap& map, const Key& key)
14.220 + : _key(key), _map(map) {}
14.221 + public:
14.222 +
14.223 + Reference& operator=(const Reference& value) {
14.224 + _map.set(_key, static_cast<bool>(value));
14.225 + return *this;
14.226 + }
14.227 +
14.228 + operator bool() const {
14.229 + return static_cast<const IterableBoolMap&>(_map)[_key];
14.230 + }
14.231 +
14.232 + Reference& operator=(bool value) {
14.233 + _map.set(_key, value);
14.234 + return *this;
14.235 + }
14.236 + Reference& operator&=(bool value) {
14.237 + _map.set(_key, _map[_key] & value);
14.238 + return *this;
14.239 + }
14.240 + Reference& operator|=(bool value) {
14.241 + _map.set(_key, _map[_key] | value);
14.242 + return *this;
14.243 + }
14.244 + Reference& operator^=(bool value) {
14.245 + _map.set(_key, _map[_key] ^ value);
14.246 + return *this;
14.247 + }
14.248 + private:
14.249 + Key _key;
14.250 + IterableBoolMap& _map;
14.251 + };
14.252 +
14.253 + /// \brief Constructor of the map with a default value.
14.254 + ///
14.255 + /// Constructor of the map with a default value.
14.256 + explicit IterableBoolMap(const Graph& graph, bool def = false)
14.257 + : Parent(graph) {
14.258 + typename Parent::Notifier* nf = Parent::notifier();
14.259 + Key it;
14.260 + for (nf->first(it); it != INVALID; nf->next(it)) {
14.261 + Parent::set(it, _array.size());
14.262 + _array.push_back(it);
14.263 + }
14.264 + _sep = (def ? _array.size() : 0);
14.265 + }
14.266 +
14.267 + /// \brief Const subscript operator of the map.
14.268 + ///
14.269 + /// Const subscript operator of the map.
14.270 + bool operator[](const Key& key) const {
14.271 + return position(key) < _sep;
14.272 + }
14.273 +
14.274 + /// \brief Subscript operator of the map.
14.275 + ///
14.276 + /// Subscript operator of the map.
14.277 + Reference operator[](const Key& key) {
14.278 + return Reference(*this, key);
14.279 + }
14.280 +
14.281 + /// \brief Set operation of the map.
14.282 + ///
14.283 + /// Set operation of the map.
14.284 + void set(const Key& key, bool value) {
14.285 + int pos = position(key);
14.286 + if (value) {
14.287 + if (pos < _sep) return;
14.288 + Key tmp = _array[_sep];
14.289 + _array[_sep] = key;
14.290 + Parent::set(key, _sep);
14.291 + _array[pos] = tmp;
14.292 + Parent::set(tmp, pos);
14.293 + ++_sep;
14.294 + } else {
14.295 + if (pos >= _sep) return;
14.296 + --_sep;
14.297 + Key tmp = _array[_sep];
14.298 + _array[_sep] = key;
14.299 + Parent::set(key, _sep);
14.300 + _array[pos] = tmp;
14.301 + Parent::set(tmp, pos);
14.302 + }
14.303 + }
14.304 +
14.305 + /// \brief Set all items.
14.306 + ///
14.307 + /// Set all items in the map.
14.308 + /// \note Constant time operation.
14.309 + void setAll(bool value) {
14.310 + _sep = (value ? _array.size() : 0);
14.311 + }
14.312 +
14.313 + /// \brief Returns the number of the keys mapped to \c true.
14.314 + ///
14.315 + /// Returns the number of the keys mapped to \c true.
14.316 + int trueNum() const {
14.317 + return _sep;
14.318 + }
14.319 +
14.320 + /// \brief Returns the number of the keys mapped to \c false.
14.321 + ///
14.322 + /// Returns the number of the keys mapped to \c false.
14.323 + int falseNum() const {
14.324 + return _array.size() - _sep;
14.325 + }
14.326 +
14.327 + /// \brief Iterator for the keys mapped to \c true.
14.328 + ///
14.329 + /// Iterator for the keys mapped to \c true. It works
14.330 + /// like a graph item iterator, it can be converted to
14.331 + /// the key type of the map, incremented with \c ++ operator, and
14.332 + /// if the iterator leaves the last valid key, it will be equal to
14.333 + /// \c INVALID.
14.334 + class TrueIt : public Key {
14.335 + public:
14.336 + typedef Key Parent;
14.337 +
14.338 + /// \brief Creates an iterator.
14.339 + ///
14.340 + /// Creates an iterator. It iterates on the
14.341 + /// keys mapped to \c true.
14.342 + /// \param map The IterableBoolMap.
14.343 + explicit TrueIt(const IterableBoolMap& map)
14.344 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
14.345 + _map(&map) {}
14.346 +
14.347 + /// \brief Invalid constructor \& conversion.
14.348 + ///
14.349 + /// This constructor initializes the iterator to be invalid.
14.350 + /// \sa Invalid for more details.
14.351 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
14.352 +
14.353 + /// \brief Increment operator.
14.354 + ///
14.355 + /// Increment operator.
14.356 + TrueIt& operator++() {
14.357 + int pos = _map->position(*this);
14.358 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
14.359 + return *this;
14.360 + }
14.361 +
14.362 + private:
14.363 + const IterableBoolMap* _map;
14.364 + };
14.365 +
14.366 + /// \brief Iterator for the keys mapped to \c false.
14.367 + ///
14.368 + /// Iterator for the keys mapped to \c false. It works
14.369 + /// like a graph item iterator, it can be converted to
14.370 + /// the key type of the map, incremented with \c ++ operator, and
14.371 + /// if the iterator leaves the last valid key, it will be equal to
14.372 + /// \c INVALID.
14.373 + class FalseIt : public Key {
14.374 + public:
14.375 + typedef Key Parent;
14.376 +
14.377 + /// \brief Creates an iterator.
14.378 + ///
14.379 + /// Creates an iterator. It iterates on the
14.380 + /// keys mapped to \c false.
14.381 + /// \param map The IterableBoolMap.
14.382 + explicit FalseIt(const IterableBoolMap& map)
14.383 + : Parent(map._sep < int(map._array.size()) ?
14.384 + map._array.back() : INVALID), _map(&map) {}
14.385 +
14.386 + /// \brief Invalid constructor \& conversion.
14.387 + ///
14.388 + /// This constructor initializes the iterator to be invalid.
14.389 + /// \sa Invalid for more details.
14.390 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
14.391 +
14.392 + /// \brief Increment operator.
14.393 + ///
14.394 + /// Increment operator.
14.395 + FalseIt& operator++() {
14.396 + int pos = _map->position(*this);
14.397 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
14.398 + return *this;
14.399 + }
14.400 +
14.401 + private:
14.402 + const IterableBoolMap* _map;
14.403 + };
14.404 +
14.405 + /// \brief Iterator for the keys mapped to a given value.
14.406 + ///
14.407 + /// Iterator for the keys mapped to a given value. It works
14.408 + /// like a graph item iterator, it can be converted to
14.409 + /// the key type of the map, incremented with \c ++ operator, and
14.410 + /// if the iterator leaves the last valid key, it will be equal to
14.411 + /// \c INVALID.
14.412 + class ItemIt : public Key {
14.413 + public:
14.414 + typedef Key Parent;
14.415 +
14.416 + /// \brief Creates an iterator with a value.
14.417 + ///
14.418 + /// Creates an iterator with a value. It iterates on the
14.419 + /// keys mapped to the given value.
14.420 + /// \param map The IterableBoolMap.
14.421 + /// \param value The value.
14.422 + ItemIt(const IterableBoolMap& map, bool value)
14.423 + : Parent(value ?
14.424 + (map._sep > 0 ?
14.425 + map._array[map._sep - 1] : INVALID) :
14.426 + (map._sep < int(map._array.size()) ?
14.427 + map._array.back() : INVALID)), _map(&map) {}
14.428 +
14.429 + /// \brief Invalid constructor \& conversion.
14.430 + ///
14.431 + /// This constructor initializes the iterator to be invalid.
14.432 + /// \sa Invalid for more details.
14.433 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
14.434 +
14.435 + /// \brief Increment operator.
14.436 + ///
14.437 + /// Increment operator.
14.438 + ItemIt& operator++() {
14.439 + int pos = _map->position(*this);
14.440 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
14.441 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
14.442 + return *this;
14.443 + }
14.444 +
14.445 + private:
14.446 + const IterableBoolMap* _map;
14.447 + };
14.448 +
14.449 + protected:
14.450 +
14.451 + virtual void add(const Key& key) {
14.452 + Parent::add(key);
14.453 + Parent::set(key, _array.size());
14.454 + _array.push_back(key);
14.455 + }
14.456 +
14.457 + virtual void add(const std::vector<Key>& keys) {
14.458 + Parent::add(keys);
14.459 + for (int i = 0; i < int(keys.size()); ++i) {
14.460 + Parent::set(keys[i], _array.size());
14.461 + _array.push_back(keys[i]);
14.462 + }
14.463 + }
14.464 +
14.465 + virtual void erase(const Key& key) {
14.466 + int pos = position(key);
14.467 + if (pos < _sep) {
14.468 + --_sep;
14.469 + Parent::set(_array[_sep], pos);
14.470 + _array[pos] = _array[_sep];
14.471 + Parent::set(_array.back(), _sep);
14.472 + _array[_sep] = _array.back();
14.473 + _array.pop_back();
14.474 + } else {
14.475 + Parent::set(_array.back(), pos);
14.476 + _array[pos] = _array.back();
14.477 + _array.pop_back();
14.478 + }
14.479 + Parent::erase(key);
14.480 + }
14.481 +
14.482 + virtual void erase(const std::vector<Key>& keys) {
14.483 + for (int i = 0; i < int(keys.size()); ++i) {
14.484 + int pos = position(keys[i]);
14.485 + if (pos < _sep) {
14.486 + --_sep;
14.487 + Parent::set(_array[_sep], pos);
14.488 + _array[pos] = _array[_sep];
14.489 + Parent::set(_array.back(), _sep);
14.490 + _array[_sep] = _array.back();
14.491 + _array.pop_back();
14.492 + } else {
14.493 + Parent::set(_array.back(), pos);
14.494 + _array[pos] = _array.back();
14.495 + _array.pop_back();
14.496 + }
14.497 + }
14.498 + Parent::erase(keys);
14.499 + }
14.500 +
14.501 + virtual void build() {
14.502 + Parent::build();
14.503 + typename Parent::Notifier* nf = Parent::notifier();
14.504 + Key it;
14.505 + for (nf->first(it); it != INVALID; nf->next(it)) {
14.506 + Parent::set(it, _array.size());
14.507 + _array.push_back(it);
14.508 + }
14.509 + _sep = 0;
14.510 + }
14.511 +
14.512 + virtual void clear() {
14.513 + _array.clear();
14.514 + _sep = 0;
14.515 + Parent::clear();
14.516 + }
14.517 +
14.518 + };
14.519 +
14.520 +
14.521 + namespace _maps_bits {
14.522 + template <typename Item>
14.523 + struct IterableIntMapNode {
14.524 + IterableIntMapNode() : value(-1) {}
14.525 + IterableIntMapNode(int _value) : value(_value) {}
14.526 + Item prev, next;
14.527 + int value;
14.528 + };
14.529 + }
14.530 +
14.531 + /// \brief Dynamic iterable integer map.
14.532 + ///
14.533 + /// This class provides a special graph map type which can store an
14.534 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
14.535 + /// For each non-negative value it is possible to iterate on the keys
14.536 + /// mapped to the value.
14.537 + ///
14.538 + /// This type is a reference map, so it can be modified with the
14.539 + /// subscript operator.
14.540 + ///
14.541 + /// \note The size of the data structure depends on the largest
14.542 + /// value in the map.
14.543 + ///
14.544 + /// \tparam GR The graph type.
14.545 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
14.546 + /// \c GR::Edge).
14.547 + ///
14.548 + /// \see IterableBoolMap, IterableValueMap
14.549 + /// \see CrossRefMap
14.550 + template <typename GR, typename K>
14.551 + class IterableIntMap
14.552 + : protected ItemSetTraits<GR, K>::
14.553 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
14.554 + public:
14.555 + typedef typename ItemSetTraits<GR, K>::
14.556 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
14.557 +
14.558 + /// The key type
14.559 + typedef K Key;
14.560 + /// The value type
14.561 + typedef int Value;
14.562 + /// The graph type
14.563 + typedef GR Graph;
14.564 +
14.565 + /// \brief Constructor of the map.
14.566 + ///
14.567 + /// Constructor of the map. It sets all values to -1.
14.568 + explicit IterableIntMap(const Graph& graph)
14.569 + : Parent(graph) {}
14.570 +
14.571 + /// \brief Constructor of the map with a given value.
14.572 + ///
14.573 + /// Constructor of the map with a given value.
14.574 + explicit IterableIntMap(const Graph& graph, int value)
14.575 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
14.576 + if (value >= 0) {
14.577 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
14.578 + lace(it);
14.579 + }
14.580 + }
14.581 + }
14.582 +
14.583 + private:
14.584 +
14.585 + void unlace(const Key& key) {
14.586 + typename Parent::Value& node = Parent::operator[](key);
14.587 + if (node.value < 0) return;
14.588 + if (node.prev != INVALID) {
14.589 + Parent::operator[](node.prev).next = node.next;
14.590 + } else {
14.591 + _first[node.value] = node.next;
14.592 + }
14.593 + if (node.next != INVALID) {
14.594 + Parent::operator[](node.next).prev = node.prev;
14.595 + }
14.596 + while (!_first.empty() && _first.back() == INVALID) {
14.597 + _first.pop_back();
14.598 + }
14.599 + }
14.600 +
14.601 + void lace(const Key& key) {
14.602 + typename Parent::Value& node = Parent::operator[](key);
14.603 + if (node.value < 0) return;
14.604 + if (node.value >= int(_first.size())) {
14.605 + _first.resize(node.value + 1, INVALID);
14.606 + }
14.607 + node.prev = INVALID;
14.608 + node.next = _first[node.value];
14.609 + if (node.next != INVALID) {
14.610 + Parent::operator[](node.next).prev = key;
14.611 + }
14.612 + _first[node.value] = key;
14.613 + }
14.614 +
14.615 + public:
14.616 +
14.617 + /// Indicates that the map is reference map.
14.618 + typedef True ReferenceMapTag;
14.619 +
14.620 + /// \brief Reference to the value of the map.
14.621 + ///
14.622 + /// This class is similar to the \c int type. It can
14.623 + /// be converted to \c int and it has the same operators.
14.624 + class Reference {
14.625 + friend class IterableIntMap;
14.626 + private:
14.627 + Reference(IterableIntMap& map, const Key& key)
14.628 + : _key(key), _map(map) {}
14.629 + public:
14.630 +
14.631 + Reference& operator=(const Reference& value) {
14.632 + _map.set(_key, static_cast<const int&>(value));
14.633 + return *this;
14.634 + }
14.635 +
14.636 + operator const int&() const {
14.637 + return static_cast<const IterableIntMap&>(_map)[_key];
14.638 + }
14.639 +
14.640 + Reference& operator=(int value) {
14.641 + _map.set(_key, value);
14.642 + return *this;
14.643 + }
14.644 + Reference& operator++() {
14.645 + _map.set(_key, _map[_key] + 1);
14.646 + return *this;
14.647 + }
14.648 + int operator++(int) {
14.649 + int value = _map[_key];
14.650 + _map.set(_key, value + 1);
14.651 + return value;
14.652 + }
14.653 + Reference& operator--() {
14.654 + _map.set(_key, _map[_key] - 1);
14.655 + return *this;
14.656 + }
14.657 + int operator--(int) {
14.658 + int value = _map[_key];
14.659 + _map.set(_key, value - 1);
14.660 + return value;
14.661 + }
14.662 + Reference& operator+=(int value) {
14.663 + _map.set(_key, _map[_key] + value);
14.664 + return *this;
14.665 + }
14.666 + Reference& operator-=(int value) {
14.667 + _map.set(_key, _map[_key] - value);
14.668 + return *this;
14.669 + }
14.670 + Reference& operator*=(int value) {
14.671 + _map.set(_key, _map[_key] * value);
14.672 + return *this;
14.673 + }
14.674 + Reference& operator/=(int value) {
14.675 + _map.set(_key, _map[_key] / value);
14.676 + return *this;
14.677 + }
14.678 + Reference& operator%=(int value) {
14.679 + _map.set(_key, _map[_key] % value);
14.680 + return *this;
14.681 + }
14.682 + Reference& operator&=(int value) {
14.683 + _map.set(_key, _map[_key] & value);
14.684 + return *this;
14.685 + }
14.686 + Reference& operator|=(int value) {
14.687 + _map.set(_key, _map[_key] | value);
14.688 + return *this;
14.689 + }
14.690 + Reference& operator^=(int value) {
14.691 + _map.set(_key, _map[_key] ^ value);
14.692 + return *this;
14.693 + }
14.694 + Reference& operator<<=(int value) {
14.695 + _map.set(_key, _map[_key] << value);
14.696 + return *this;
14.697 + }
14.698 + Reference& operator>>=(int value) {
14.699 + _map.set(_key, _map[_key] >> value);
14.700 + return *this;
14.701 + }
14.702 +
14.703 + private:
14.704 + Key _key;
14.705 + IterableIntMap& _map;
14.706 + };
14.707 +
14.708 + /// The const reference type.
14.709 + typedef const Value& ConstReference;
14.710 +
14.711 + /// \brief Gives back the maximal value plus one.
14.712 + ///
14.713 + /// Gives back the maximal value plus one.
14.714 + int size() const {
14.715 + return _first.size();
14.716 + }
14.717 +
14.718 + /// \brief Set operation of the map.
14.719 + ///
14.720 + /// Set operation of the map.
14.721 + void set(const Key& key, const Value& value) {
14.722 + unlace(key);
14.723 + Parent::operator[](key).value = value;
14.724 + lace(key);
14.725 + }
14.726 +
14.727 + /// \brief Const subscript operator of the map.
14.728 + ///
14.729 + /// Const subscript operator of the map.
14.730 + const Value& operator[](const Key& key) const {
14.731 + return Parent::operator[](key).value;
14.732 + }
14.733 +
14.734 + /// \brief Subscript operator of the map.
14.735 + ///
14.736 + /// Subscript operator of the map.
14.737 + Reference operator[](const Key& key) {
14.738 + return Reference(*this, key);
14.739 + }
14.740 +
14.741 + /// \brief Iterator for the keys with the same value.
14.742 + ///
14.743 + /// Iterator for the keys with the same value. It works
14.744 + /// like a graph item iterator, it can be converted to
14.745 + /// the item type of the map, incremented with \c ++ operator, and
14.746 + /// if the iterator leaves the last valid item, it will be equal to
14.747 + /// \c INVALID.
14.748 + class ItemIt : public Key {
14.749 + public:
14.750 + typedef Key Parent;
14.751 +
14.752 + /// \brief Invalid constructor \& conversion.
14.753 + ///
14.754 + /// This constructor initializes the iterator to be invalid.
14.755 + /// \sa Invalid for more details.
14.756 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
14.757 +
14.758 + /// \brief Creates an iterator with a value.
14.759 + ///
14.760 + /// Creates an iterator with a value. It iterates on the
14.761 + /// keys mapped to the given value.
14.762 + /// \param map The IterableIntMap.
14.763 + /// \param value The value.
14.764 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
14.765 + if (value < 0 || value >= int(_map->_first.size())) {
14.766 + Parent::operator=(INVALID);
14.767 + } else {
14.768 + Parent::operator=(_map->_first[value]);
14.769 + }
14.770 + }
14.771 +
14.772 + /// \brief Increment operator.
14.773 + ///
14.774 + /// Increment operator.
14.775 + ItemIt& operator++() {
14.776 + Parent::operator=(_map->IterableIntMap::Parent::
14.777 + operator[](static_cast<Parent&>(*this)).next);
14.778 + return *this;
14.779 + }
14.780 +
14.781 + private:
14.782 + const IterableIntMap* _map;
14.783 + };
14.784 +
14.785 + protected:
14.786 +
14.787 + virtual void erase(const Key& key) {
14.788 + unlace(key);
14.789 + Parent::erase(key);
14.790 + }
14.791 +
14.792 + virtual void erase(const std::vector<Key>& keys) {
14.793 + for (int i = 0; i < int(keys.size()); ++i) {
14.794 + unlace(keys[i]);
14.795 + }
14.796 + Parent::erase(keys);
14.797 + }
14.798 +
14.799 + virtual void clear() {
14.800 + _first.clear();
14.801 + Parent::clear();
14.802 + }
14.803 +
14.804 + private:
14.805 + std::vector<Key> _first;
14.806 + };
14.807 +
14.808 + namespace _maps_bits {
14.809 + template <typename Item, typename Value>
14.810 + struct IterableValueMapNode {
14.811 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
14.812 + Item prev, next;
14.813 + Value value;
14.814 + };
14.815 + }
14.816 +
14.817 + /// \brief Dynamic iterable map for comparable values.
14.818 + ///
14.819 + /// This class provides a special graph map type which can store an
14.820 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
14.821 + /// For each value it is possible to iterate on the keys mapped to
14.822 + /// the value.
14.823 + ///
14.824 + /// The map stores for each value a linked list with
14.825 + /// the items which mapped to the value, and the values are stored
14.826 + /// in balanced binary tree. The values of the map can be accessed
14.827 + /// with stl compatible forward iterator.
14.828 + ///
14.829 + /// This type is not reference map, so it cannot be modified with
14.830 + /// the subscript operator.
14.831 + ///
14.832 + /// \tparam GR The graph type.
14.833 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
14.834 + /// \c GR::Edge).
14.835 + /// \tparam V The value type of the map. It can be any comparable
14.836 + /// value type.
14.837 + ///
14.838 + /// \see IterableBoolMap, IterableIntMap
14.839 + /// \see CrossRefMap
14.840 + template <typename GR, typename K, typename V>
14.841 + class IterableValueMap
14.842 + : protected ItemSetTraits<GR, K>::
14.843 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
14.844 + public:
14.845 + typedef typename ItemSetTraits<GR, K>::
14.846 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
14.847 +
14.848 + /// The key type
14.849 + typedef K Key;
14.850 + /// The value type
14.851 + typedef V Value;
14.852 + /// The graph type
14.853 + typedef GR Graph;
14.854 +
14.855 + public:
14.856 +
14.857 + /// \brief Constructor of the map with a given value.
14.858 + ///
14.859 + /// Constructor of the map with a given value.
14.860 + explicit IterableValueMap(const Graph& graph,
14.861 + const Value& value = Value())
14.862 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
14.863 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
14.864 + lace(it);
14.865 + }
14.866 + }
14.867 +
14.868 + protected:
14.869 +
14.870 + void unlace(const Key& key) {
14.871 + typename Parent::Value& node = Parent::operator[](key);
14.872 + if (node.prev != INVALID) {
14.873 + Parent::operator[](node.prev).next = node.next;
14.874 + } else {
14.875 + if (node.next != INVALID) {
14.876 + _first[node.value] = node.next;
14.877 + } else {
14.878 + _first.erase(node.value);
14.879 + }
14.880 + }
14.881 + if (node.next != INVALID) {
14.882 + Parent::operator[](node.next).prev = node.prev;
14.883 + }
14.884 + }
14.885 +
14.886 + void lace(const Key& key) {
14.887 + typename Parent::Value& node = Parent::operator[](key);
14.888 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
14.889 + if (it == _first.end()) {
14.890 + node.prev = node.next = INVALID;
14.891 + _first.insert(std::make_pair(node.value, key));
14.892 + } else {
14.893 + node.prev = INVALID;
14.894 + node.next = it->second;
14.895 + if (node.next != INVALID) {
14.896 + Parent::operator[](node.next).prev = key;
14.897 + }
14.898 + it->second = key;
14.899 + }
14.900 + }
14.901 +
14.902 + public:
14.903 +
14.904 + /// \brief Forward iterator for values.
14.905 + ///
14.906 + /// This iterator is an stl compatible forward
14.907 + /// iterator on the values of the map. The values can
14.908 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
14.909 + class ValueIterator
14.910 + : public std::iterator<std::forward_iterator_tag, Value> {
14.911 + friend class IterableValueMap;
14.912 + private:
14.913 + ValueIterator(typename std::map<Value, Key>::const_iterator _it)
14.914 + : it(_it) {}
14.915 + public:
14.916 +
14.917 + ValueIterator() {}
14.918 +
14.919 + ValueIterator& operator++() { ++it; return *this; }
14.920 + ValueIterator operator++(int) {
14.921 + ValueIterator tmp(*this);
14.922 + operator++();
14.923 + return tmp;
14.924 + }
14.925 +
14.926 + const Value& operator*() const { return it->first; }
14.927 + const Value* operator->() const { return &(it->first); }
14.928 +
14.929 + bool operator==(ValueIterator jt) const { return it == jt.it; }
14.930 + bool operator!=(ValueIterator jt) const { return it != jt.it; }
14.931 +
14.932 + private:
14.933 + typename std::map<Value, Key>::const_iterator it;
14.934 + };
14.935 +
14.936 + /// \brief Returns an iterator to the first value.
14.937 + ///
14.938 + /// Returns an stl compatible iterator to the
14.939 + /// first value of the map. The values of the
14.940 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
14.941 + /// range.
14.942 + ValueIterator beginValue() const {
14.943 + return ValueIterator(_first.begin());
14.944 + }
14.945 +
14.946 + /// \brief Returns an iterator after the last value.
14.947 + ///
14.948 + /// Returns an stl compatible iterator after the
14.949 + /// last value of the map. The values of the
14.950 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
14.951 + /// range.
14.952 + ValueIterator endValue() const {
14.953 + return ValueIterator(_first.end());
14.954 + }
14.955 +
14.956 + /// \brief Set operation of the map.
14.957 + ///
14.958 + /// Set operation of the map.
14.959 + void set(const Key& key, const Value& value) {
14.960 + unlace(key);
14.961 + Parent::operator[](key).value = value;
14.962 + lace(key);
14.963 + }
14.964 +
14.965 + /// \brief Const subscript operator of the map.
14.966 + ///
14.967 + /// Const subscript operator of the map.
14.968 + const Value& operator[](const Key& key) const {
14.969 + return Parent::operator[](key).value;
14.970 + }
14.971 +
14.972 + /// \brief Iterator for the keys with the same value.
14.973 + ///
14.974 + /// Iterator for the keys with the same value. It works
14.975 + /// like a graph item iterator, it can be converted to
14.976 + /// the item type of the map, incremented with \c ++ operator, and
14.977 + /// if the iterator leaves the last valid item, it will be equal to
14.978 + /// \c INVALID.
14.979 + class ItemIt : public Key {
14.980 + public:
14.981 + typedef Key Parent;
14.982 +
14.983 + /// \brief Invalid constructor \& conversion.
14.984 + ///
14.985 + /// This constructor initializes the iterator to be invalid.
14.986 + /// \sa Invalid for more details.
14.987 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
14.988 +
14.989 + /// \brief Creates an iterator with a value.
14.990 + ///
14.991 + /// Creates an iterator with a value. It iterates on the
14.992 + /// keys which have the given value.
14.993 + /// \param map The IterableValueMap
14.994 + /// \param value The value
14.995 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
14.996 + typename std::map<Value, Key>::const_iterator it =
14.997 + map._first.find(value);
14.998 + if (it == map._first.end()) {
14.999 + Parent::operator=(INVALID);
14.1000 + } else {
14.1001 + Parent::operator=(it->second);
14.1002 + }
14.1003 + }
14.1004 +
14.1005 + /// \brief Increment operator.
14.1006 + ///
14.1007 + /// Increment Operator.
14.1008 + ItemIt& operator++() {
14.1009 + Parent::operator=(_map->IterableValueMap::Parent::
14.1010 + operator[](static_cast<Parent&>(*this)).next);
14.1011 + return *this;
14.1012 + }
14.1013 +
14.1014 +
14.1015 + private:
14.1016 + const IterableValueMap* _map;
14.1017 + };
14.1018 +
14.1019 + protected:
14.1020 +
14.1021 + virtual void add(const Key& key) {
14.1022 + Parent::add(key);
14.1023 + unlace(key);
14.1024 + }
14.1025 +
14.1026 + virtual void add(const std::vector<Key>& keys) {
14.1027 + Parent::add(keys);
14.1028 + for (int i = 0; i < int(keys.size()); ++i) {
14.1029 + lace(keys[i]);
14.1030 + }
14.1031 + }
14.1032 +
14.1033 + virtual void erase(const Key& key) {
14.1034 + unlace(key);
14.1035 + Parent::erase(key);
14.1036 + }
14.1037 +
14.1038 + virtual void erase(const std::vector<Key>& keys) {
14.1039 + for (int i = 0; i < int(keys.size()); ++i) {
14.1040 + unlace(keys[i]);
14.1041 + }
14.1042 + Parent::erase(keys);
14.1043 + }
14.1044 +
14.1045 + virtual void build() {
14.1046 + Parent::build();
14.1047 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
14.1048 + lace(it);
14.1049 + }
14.1050 + }
14.1051 +
14.1052 + virtual void clear() {
14.1053 + _first.clear();
14.1054 + Parent::clear();
14.1055 + }
14.1056 +
14.1057 + private:
14.1058 + std::map<Value, Key> _first;
14.1059 + };
14.1060 +
14.1061 /// \brief Map of the source nodes of arcs in a digraph.
14.1062 ///
14.1063 /// SourceMap provides access for the source node of each arc in a digraph,
14.1064 @@ -2480,7 +3395,7 @@
14.1065 /// in constant time. On the other hand, the values are updated automatically
14.1066 /// whenever the digraph changes.
14.1067 ///
14.1068 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
14.1069 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
14.1070 /// may provide alternative ways to modify the digraph.
14.1071 /// The correct behavior of InDegMap is not guarantied if these additional
14.1072 /// features are used. For example the functions
14.1073 @@ -2496,7 +3411,7 @@
14.1074 ::ItemNotifier::ObserverBase {
14.1075
14.1076 public:
14.1077 -
14.1078 +
14.1079 /// The graph type of InDegMap
14.1080 typedef GR Graph;
14.1081 typedef GR Digraph;
14.1082 @@ -2610,7 +3525,7 @@
14.1083 /// in constant time. On the other hand, the values are updated automatically
14.1084 /// whenever the digraph changes.
14.1085 ///
14.1086 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
14.1087 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
14.1088 /// may provide alternative ways to modify the digraph.
14.1089 /// The correct behavior of OutDegMap is not guarantied if these additional
14.1090 /// features are used. For example the functions
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/lemon/pairing_heap.h Fri Sep 25 09:06:32 2009 +0200
15.3 @@ -0,0 +1,474 @@
15.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
15.5 + *
15.6 + * This file is a part of LEMON, a generic C++ optimization library.
15.7 + *
15.8 + * Copyright (C) 2003-2009
15.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
15.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
15.11 + *
15.12 + * Permission to use, modify and distribute this software is granted
15.13 + * provided that this copyright notice appears in all copies. For
15.14 + * precise terms see the accompanying LICENSE file.
15.15 + *
15.16 + * This software is provided "AS IS" with no warranty of any kind,
15.17 + * express or implied, and with no claim as to its suitability for any
15.18 + * purpose.
15.19 + *
15.20 + */
15.21 +
15.22 +#ifndef LEMON_PAIRING_HEAP_H
15.23 +#define LEMON_PAIRING_HEAP_H
15.24 +
15.25 +///\file
15.26 +///\ingroup heaps
15.27 +///\brief Pairing heap implementation.
15.28 +
15.29 +#include <vector>
15.30 +#include <utility>
15.31 +#include <functional>
15.32 +#include <lemon/math.h>
15.33 +
15.34 +namespace lemon {
15.35 +
15.36 + /// \ingroup heaps
15.37 + ///
15.38 + ///\brief Pairing Heap.
15.39 + ///
15.40 + /// This class implements the \e pairing \e heap data structure.
15.41 + /// It fully conforms to the \ref concepts::Heap "heap concept".
15.42 + ///
15.43 + /// The methods \ref increase() and \ref erase() are not efficient
15.44 + /// in a pairing heap. In case of many calls of these operations,
15.45 + /// it is better to use other heap structure, e.g. \ref BinHeap
15.46 + /// "binary heap".
15.47 + ///
15.48 + /// \tparam PR Type of the priorities of the items.
15.49 + /// \tparam IM A read-writable item map with \c int values, used
15.50 + /// internally to handle the cross references.
15.51 + /// \tparam CMP A functor class for comparing the priorities.
15.52 + /// The default is \c std::less<PR>.
15.53 +#ifdef DOXYGEN
15.54 + template <typename PR, typename IM, typename CMP>
15.55 +#else
15.56 + template <typename PR, typename IM, typename CMP = std::less<PR> >
15.57 +#endif
15.58 + class PairingHeap {
15.59 + public:
15.60 + /// Type of the item-int map.
15.61 + typedef IM ItemIntMap;
15.62 + /// Type of the priorities.
15.63 + typedef PR Prio;
15.64 + /// Type of the items stored in the heap.
15.65 + typedef typename ItemIntMap::Key Item;
15.66 + /// Functor type for comparing the priorities.
15.67 + typedef CMP Compare;
15.68 +
15.69 + /// \brief Type to represent the states of the items.
15.70 + ///
15.71 + /// Each item has a state associated to it. It can be "in heap",
15.72 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
15.73 + /// heap's point of view, but may be useful to the user.
15.74 + ///
15.75 + /// The item-int map must be initialized in such way that it assigns
15.76 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
15.77 + enum State {
15.78 + IN_HEAP = 0, ///< = 0.
15.79 + PRE_HEAP = -1, ///< = -1.
15.80 + POST_HEAP = -2 ///< = -2.
15.81 + };
15.82 +
15.83 + private:
15.84 + class store;
15.85 +
15.86 + std::vector<store> _data;
15.87 + int _min;
15.88 + ItemIntMap &_iim;
15.89 + Compare _comp;
15.90 + int _num_items;
15.91 +
15.92 + public:
15.93 + /// \brief Constructor.
15.94 + ///
15.95 + /// Constructor.
15.96 + /// \param map A map that assigns \c int values to the items.
15.97 + /// It is used internally to handle the cross references.
15.98 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
15.99 + explicit PairingHeap(ItemIntMap &map)
15.100 + : _min(0), _iim(map), _num_items(0) {}
15.101 +
15.102 + /// \brief Constructor.
15.103 + ///
15.104 + /// Constructor.
15.105 + /// \param map A map that assigns \c int values to the items.
15.106 + /// It is used internally to handle the cross references.
15.107 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
15.108 + /// \param comp The function object used for comparing the priorities.
15.109 + PairingHeap(ItemIntMap &map, const Compare &comp)
15.110 + : _min(0), _iim(map), _comp(comp), _num_items(0) {}
15.111 +
15.112 + /// \brief The number of items stored in the heap.
15.113 + ///
15.114 + /// This function returns the number of items stored in the heap.
15.115 + int size() const { return _num_items; }
15.116 +
15.117 + /// \brief Check if the heap is empty.
15.118 + ///
15.119 + /// This function returns \c true if the heap is empty.
15.120 + bool empty() const { return _num_items==0; }
15.121 +
15.122 + /// \brief Make the heap empty.
15.123 + ///
15.124 + /// This functon makes the heap empty.
15.125 + /// It does not change the cross reference map. If you want to reuse
15.126 + /// a heap that is not surely empty, you should first clear it and
15.127 + /// then you should set the cross reference map to \c PRE_HEAP
15.128 + /// for each item.
15.129 + void clear() {
15.130 + _data.clear();
15.131 + _min = 0;
15.132 + _num_items = 0;
15.133 + }
15.134 +
15.135 + /// \brief Set the priority of an item or insert it, if it is
15.136 + /// not stored in the heap.
15.137 + ///
15.138 + /// This method sets the priority of the given item if it is
15.139 + /// already stored in the heap. Otherwise it inserts the given
15.140 + /// item into the heap with the given priority.
15.141 + /// \param item The item.
15.142 + /// \param value The priority.
15.143 + void set (const Item& item, const Prio& value) {
15.144 + int i=_iim[item];
15.145 + if ( i>=0 && _data[i].in ) {
15.146 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
15.147 + if ( _comp(_data[i].prio, value) ) increase(item, value);
15.148 + } else push(item, value);
15.149 + }
15.150 +
15.151 + /// \brief Insert an item into the heap with the given priority.
15.152 + ///
15.153 + /// This function inserts the given item into the heap with the
15.154 + /// given priority.
15.155 + /// \param item The item to insert.
15.156 + /// \param value The priority of the item.
15.157 + /// \pre \e item must not be stored in the heap.
15.158 + void push (const Item& item, const Prio& value) {
15.159 + int i=_iim[item];
15.160 + if( i<0 ) {
15.161 + int s=_data.size();
15.162 + _iim.set(item, s);
15.163 + store st;
15.164 + st.name=item;
15.165 + _data.push_back(st);
15.166 + i=s;
15.167 + } else {
15.168 + _data[i].parent=_data[i].child=-1;
15.169 + _data[i].left_child=false;
15.170 + _data[i].degree=0;
15.171 + _data[i].in=true;
15.172 + }
15.173 +
15.174 + _data[i].prio=value;
15.175 +
15.176 + if ( _num_items!=0 ) {
15.177 + if ( _comp( value, _data[_min].prio) ) {
15.178 + fuse(i,_min);
15.179 + _min=i;
15.180 + }
15.181 + else fuse(_min,i);
15.182 + }
15.183 + else _min=i;
15.184 +
15.185 + ++_num_items;
15.186 + }
15.187 +
15.188 + /// \brief Return the item having minimum priority.
15.189 + ///
15.190 + /// This function returns the item having minimum priority.
15.191 + /// \pre The heap must be non-empty.
15.192 + Item top() const { return _data[_min].name; }
15.193 +
15.194 + /// \brief The minimum priority.
15.195 + ///
15.196 + /// This function returns the minimum priority.
15.197 + /// \pre The heap must be non-empty.
15.198 + const Prio& prio() const { return _data[_min].prio; }
15.199 +
15.200 + /// \brief The priority of the given item.
15.201 + ///
15.202 + /// This function returns the priority of the given item.
15.203 + /// \param item The item.
15.204 + /// \pre \e item must be in the heap.
15.205 + const Prio& operator[](const Item& item) const {
15.206 + return _data[_iim[item]].prio;
15.207 + }
15.208 +
15.209 + /// \brief Remove the item having minimum priority.
15.210 + ///
15.211 + /// This function removes the item having minimum priority.
15.212 + /// \pre The heap must be non-empty.
15.213 + void pop() {
15.214 + std::vector<int> trees;
15.215 + int i=0, child_right = 0;
15.216 + _data[_min].in=false;
15.217 +
15.218 + if( -1!=_data[_min].child ) {
15.219 + i=_data[_min].child;
15.220 + trees.push_back(i);
15.221 + _data[i].parent = -1;
15.222 + _data[_min].child = -1;
15.223 +
15.224 + int ch=-1;
15.225 + while( _data[i].child!=-1 ) {
15.226 + ch=_data[i].child;
15.227 + if( _data[ch].left_child && i==_data[ch].parent ) {
15.228 + break;
15.229 + } else {
15.230 + if( _data[ch].left_child ) {
15.231 + child_right=_data[ch].parent;
15.232 + _data[ch].parent = i;
15.233 + --_data[i].degree;
15.234 + }
15.235 + else {
15.236 + child_right=ch;
15.237 + _data[i].child=-1;
15.238 + _data[i].degree=0;
15.239 + }
15.240 + _data[child_right].parent = -1;
15.241 + trees.push_back(child_right);
15.242 + i = child_right;
15.243 + }
15.244 + }
15.245 +
15.246 + int num_child = trees.size();
15.247 + int other;
15.248 + for( i=0; i<num_child-1; i+=2 ) {
15.249 + if ( !_comp(_data[trees[i]].prio, _data[trees[i+1]].prio) ) {
15.250 + other=trees[i];
15.251 + trees[i]=trees[i+1];
15.252 + trees[i+1]=other;
15.253 + }
15.254 + fuse( trees[i], trees[i+1] );
15.255 + }
15.256 +
15.257 + i = (0==(num_child % 2)) ? num_child-2 : num_child-1;
15.258 + while(i>=2) {
15.259 + if ( _comp(_data[trees[i]].prio, _data[trees[i-2]].prio) ) {
15.260 + other=trees[i];
15.261 + trees[i]=trees[i-2];
15.262 + trees[i-2]=other;
15.263 + }
15.264 + fuse( trees[i-2], trees[i] );
15.265 + i-=2;
15.266 + }
15.267 + _min = trees[0];
15.268 + }
15.269 + else {
15.270 + _min = _data[_min].child;
15.271 + }
15.272 +
15.273 + if (_min >= 0) _data[_min].left_child = false;
15.274 + --_num_items;
15.275 + }
15.276 +
15.277 + /// \brief Remove the given item from the heap.
15.278 + ///
15.279 + /// This function removes the given item from the heap if it is
15.280 + /// already stored.
15.281 + /// \param item The item to delete.
15.282 + /// \pre \e item must be in the heap.
15.283 + void erase (const Item& item) {
15.284 + int i=_iim[item];
15.285 + if ( i>=0 && _data[i].in ) {
15.286 + decrease( item, _data[_min].prio-1 );
15.287 + pop();
15.288 + }
15.289 + }
15.290 +
15.291 + /// \brief Decrease the priority of an item to the given value.
15.292 + ///
15.293 + /// This function decreases the priority of an item to the given value.
15.294 + /// \param item The item.
15.295 + /// \param value The priority.
15.296 + /// \pre \e item must be stored in the heap with priority at least \e value.
15.297 + void decrease (Item item, const Prio& value) {
15.298 + int i=_iim[item];
15.299 + _data[i].prio=value;
15.300 + int p=_data[i].parent;
15.301 +
15.302 + if( _data[i].left_child && i!=_data[p].child ) {
15.303 + p=_data[p].parent;
15.304 + }
15.305 +
15.306 + if ( p!=-1 && _comp(value,_data[p].prio) ) {
15.307 + cut(i,p);
15.308 + if ( _comp(_data[_min].prio,value) ) {
15.309 + fuse(_min,i);
15.310 + } else {
15.311 + fuse(i,_min);
15.312 + _min=i;
15.313 + }
15.314 + }
15.315 + }
15.316 +
15.317 + /// \brief Increase the priority of an item to the given value.
15.318 + ///
15.319 + /// This function increases the priority of an item to the given value.
15.320 + /// \param item The item.
15.321 + /// \param value The priority.
15.322 + /// \pre \e item must be stored in the heap with priority at most \e value.
15.323 + void increase (Item item, const Prio& value) {
15.324 + erase(item);
15.325 + push(item,value);
15.326 + }
15.327 +
15.328 + /// \brief Return the state of an item.
15.329 + ///
15.330 + /// This method returns \c PRE_HEAP if the given item has never
15.331 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
15.332 + /// and \c POST_HEAP otherwise.
15.333 + /// In the latter case it is possible that the item will get back
15.334 + /// to the heap again.
15.335 + /// \param item The item.
15.336 + State state(const Item &item) const {
15.337 + int i=_iim[item];
15.338 + if( i>=0 ) {
15.339 + if( _data[i].in ) i=0;
15.340 + else i=-2;
15.341 + }
15.342 + return State(i);
15.343 + }
15.344 +
15.345 + /// \brief Set the state of an item in the heap.
15.346 + ///
15.347 + /// This function sets the state of the given item in the heap.
15.348 + /// It can be used to manually clear the heap when it is important
15.349 + /// to achive better time complexity.
15.350 + /// \param i The item.
15.351 + /// \param st The state. It should not be \c IN_HEAP.
15.352 + void state(const Item& i, State st) {
15.353 + switch (st) {
15.354 + case POST_HEAP:
15.355 + case PRE_HEAP:
15.356 + if (state(i) == IN_HEAP) erase(i);
15.357 + _iim[i]=st;
15.358 + break;
15.359 + case IN_HEAP:
15.360 + break;
15.361 + }
15.362 + }
15.363 +
15.364 + private:
15.365 +
15.366 + void cut(int a, int b) {
15.367 + int child_a;
15.368 + switch (_data[a].degree) {
15.369 + case 2:
15.370 + child_a = _data[_data[a].child].parent;
15.371 + if( _data[a].left_child ) {
15.372 + _data[child_a].left_child=true;
15.373 + _data[b].child=child_a;
15.374 + _data[child_a].parent=_data[a].parent;
15.375 + }
15.376 + else {
15.377 + _data[child_a].left_child=false;
15.378 + _data[child_a].parent=b;
15.379 + if( a!=_data[b].child )
15.380 + _data[_data[b].child].parent=child_a;
15.381 + else
15.382 + _data[b].child=child_a;
15.383 + }
15.384 + --_data[a].degree;
15.385 + _data[_data[a].child].parent=a;
15.386 + break;
15.387 +
15.388 + case 1:
15.389 + child_a = _data[a].child;
15.390 + if( !_data[child_a].left_child ) {
15.391 + --_data[a].degree;
15.392 + if( _data[a].left_child ) {
15.393 + _data[child_a].left_child=true;
15.394 + _data[child_a].parent=_data[a].parent;
15.395 + _data[b].child=child_a;
15.396 + }
15.397 + else {
15.398 + _data[child_a].left_child=false;
15.399 + _data[child_a].parent=b;
15.400 + if( a!=_data[b].child )
15.401 + _data[_data[b].child].parent=child_a;
15.402 + else
15.403 + _data[b].child=child_a;
15.404 + }
15.405 + _data[a].child=-1;
15.406 + }
15.407 + else {
15.408 + --_data[b].degree;
15.409 + if( _data[a].left_child ) {
15.410 + _data[b].child =
15.411 + (1==_data[b].degree) ? _data[a].parent : -1;
15.412 + } else {
15.413 + if (1==_data[b].degree)
15.414 + _data[_data[b].child].parent=b;
15.415 + else
15.416 + _data[b].child=-1;
15.417 + }
15.418 + }
15.419 + break;
15.420 +
15.421 + case 0:
15.422 + --_data[b].degree;
15.423 + if( _data[a].left_child ) {
15.424 + _data[b].child =
15.425 + (0!=_data[b].degree) ? _data[a].parent : -1;
15.426 + } else {
15.427 + if( 0!=_data[b].degree )
15.428 + _data[_data[b].child].parent=b;
15.429 + else
15.430 + _data[b].child=-1;
15.431 + }
15.432 + break;
15.433 + }
15.434 + _data[a].parent=-1;
15.435 + _data[a].left_child=false;
15.436 + }
15.437 +
15.438 + void fuse(int a, int b) {
15.439 + int child_a = _data[a].child;
15.440 + int child_b = _data[b].child;
15.441 + _data[a].child=b;
15.442 + _data[b].parent=a;
15.443 + _data[b].left_child=true;
15.444 +
15.445 + if( -1!=child_a ) {
15.446 + _data[b].child=child_a;
15.447 + _data[child_a].parent=b;
15.448 + _data[child_a].left_child=false;
15.449 + ++_data[b].degree;
15.450 +
15.451 + if( -1!=child_b ) {
15.452 + _data[b].child=child_b;
15.453 + _data[child_b].parent=child_a;
15.454 + }
15.455 + }
15.456 + else { ++_data[a].degree; }
15.457 + }
15.458 +
15.459 + class store {
15.460 + friend class PairingHeap;
15.461 +
15.462 + Item name;
15.463 + int parent;
15.464 + int child;
15.465 + bool left_child;
15.466 + int degree;
15.467 + bool in;
15.468 + Prio prio;
15.469 +
15.470 + store() : parent(-1), child(-1), left_child(false), degree(0), in(true) {}
15.471 + };
15.472 + };
15.473 +
15.474 +} //namespace lemon
15.475 +
15.476 +#endif //LEMON_PAIRING_HEAP_H
15.477 +
16.1 --- a/lemon/preflow.h Fri Jul 24 11:07:52 2009 +0200
16.2 +++ b/lemon/preflow.h Fri Sep 25 09:06:32 2009 +0200
16.3 @@ -104,7 +104,7 @@
16.4 /// \e push-relabel algorithm producing a \ref max_flow
16.5 /// "flow of maximum value" in a digraph.
16.6 /// The preflow algorithms are the fastest known maximum
16.7 - /// flow algorithms. The current implementation use a mixture of the
16.8 + /// flow algorithms. The current implementation uses a mixture of the
16.9 /// \e "highest label" and the \e "bound decrease" heuristics.
16.10 /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
16.11 ///
16.12 @@ -378,19 +378,21 @@
16.13 return *_level;
16.14 }
16.15
16.16 - /// \brief Sets the tolerance used by algorithm.
16.17 + /// \brief Sets the tolerance used by the algorithm.
16.18 ///
16.19 - /// Sets the tolerance used by algorithm.
16.20 - Preflow& tolerance(const Tolerance& tolerance) const {
16.21 + /// Sets the tolerance object used by the algorithm.
16.22 + /// \return <tt>(*this)</tt>
16.23 + Preflow& tolerance(const Tolerance& tolerance) {
16.24 _tolerance = tolerance;
16.25 return *this;
16.26 }
16.27
16.28 /// \brief Returns a const reference to the tolerance.
16.29 ///
16.30 - /// Returns a const reference to the tolerance.
16.31 + /// Returns a const reference to the tolerance object used by
16.32 + /// the algorithm.
16.33 const Tolerance& tolerance() const {
16.34 - return tolerance;
16.35 + return _tolerance;
16.36 }
16.37
16.38 /// \name Execution Control
17.1 --- a/lemon/radix_heap.h Fri Jul 24 11:07:52 2009 +0200
17.2 +++ b/lemon/radix_heap.h Fri Sep 25 09:06:32 2009 +0200
17.3 @@ -19,9 +19,9 @@
17.4 #ifndef LEMON_RADIX_HEAP_H
17.5 #define LEMON_RADIX_HEAP_H
17.6
17.7 -///\ingroup auxdat
17.8 +///\ingroup heaps
17.9 ///\file
17.10 -///\brief Radix Heap implementation.
17.11 +///\brief Radix heap implementation.
17.12
17.13 #include <vector>
17.14 #include <lemon/error.h>
17.15 @@ -29,56 +29,54 @@
17.16 namespace lemon {
17.17
17.18
17.19 - /// \ingroup auxdata
17.20 + /// \ingroup heaps
17.21 ///
17.22 - /// \brief A Radix Heap implementation.
17.23 + /// \brief Radix heap data structure.
17.24 ///
17.25 - /// This class implements the \e radix \e heap data structure. A \e heap
17.26 - /// is a data structure for storing items with specified values called \e
17.27 - /// priorities in such a way that finding the item with minimum priority is
17.28 - /// efficient. This heap type can store only items with \e int priority.
17.29 - /// In a heap one can change the priority of an item, add or erase an
17.30 - /// item, but the priority cannot be decreased under the last removed
17.31 - /// item's priority.
17.32 + /// This class implements the \e radix \e heap data structure.
17.33 + /// It practically conforms to the \ref concepts::Heap "heap concept",
17.34 + /// but it has some limitations due its special implementation.
17.35 + /// The type of the priorities must be \c int and the priority of an
17.36 + /// item cannot be decreased under the priority of the last removed item.
17.37 ///
17.38 - /// \param IM A read and writable Item int map, used internally
17.39 - /// to handle the cross references.
17.40 - ///
17.41 - /// \see BinHeap
17.42 - /// \see Dijkstra
17.43 + /// \tparam IM A read-writable item map with \c int values, used
17.44 + /// internally to handle the cross references.
17.45 template <typename IM>
17.46 class RadixHeap {
17.47
17.48 public:
17.49 - typedef typename IM::Key Item;
17.50 +
17.51 + /// Type of the item-int map.
17.52 + typedef IM ItemIntMap;
17.53 + /// Type of the priorities.
17.54 typedef int Prio;
17.55 - typedef IM ItemIntMap;
17.56 + /// Type of the items stored in the heap.
17.57 + typedef typename ItemIntMap::Key Item;
17.58
17.59 /// \brief Exception thrown by RadixHeap.
17.60 ///
17.61 - /// This Exception is thrown when a smaller priority
17.62 - /// is inserted into the \e RadixHeap then the last time erased.
17.63 + /// This exception is thrown when an item is inserted into a
17.64 + /// RadixHeap with a priority smaller than the last erased one.
17.65 /// \see RadixHeap
17.66 -
17.67 - class UnderFlowPriorityError : public Exception {
17.68 + class PriorityUnderflowError : public Exception {
17.69 public:
17.70 virtual const char* what() const throw() {
17.71 - return "lemon::RadixHeap::UnderFlowPriorityError";
17.72 + return "lemon::RadixHeap::PriorityUnderflowError";
17.73 }
17.74 };
17.75
17.76 - /// \brief Type to represent the items states.
17.77 + /// \brief Type to represent the states of the items.
17.78 ///
17.79 - /// Each Item element have a state associated to it. It may be "in heap",
17.80 - /// "pre heap" or "post heap". The latter two are indifferent from the
17.81 + /// Each item has a state associated to it. It can be "in heap",
17.82 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
17.83 /// heap's point of view, but may be useful to the user.
17.84 ///
17.85 - /// The ItemIntMap \e should be initialized in such way that it maps
17.86 - /// PRE_HEAP (-1) to any element to be put in the heap...
17.87 + /// The item-int map must be initialized in such way that it assigns
17.88 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
17.89 enum State {
17.90 - IN_HEAP = 0,
17.91 - PRE_HEAP = -1,
17.92 - POST_HEAP = -2
17.93 + IN_HEAP = 0, ///< = 0.
17.94 + PRE_HEAP = -1, ///< = -1.
17.95 + POST_HEAP = -2 ///< = -2.
17.96 };
17.97
17.98 private:
17.99 @@ -96,52 +94,55 @@
17.100 RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
17.101 };
17.102
17.103 - std::vector<RadixItem> data;
17.104 - std::vector<RadixBox> boxes;
17.105 + std::vector<RadixItem> _data;
17.106 + std::vector<RadixBox> _boxes;
17.107
17.108 ItemIntMap &_iim;
17.109
17.110 + public:
17.111
17.112 - public:
17.113 - /// \brief The constructor.
17.114 + /// \brief Constructor.
17.115 ///
17.116 - /// The constructor.
17.117 - ///
17.118 - /// \param map It should be given to the constructor, since it is used
17.119 - /// internally to handle the cross references. The value of the map
17.120 - /// should be PRE_HEAP (-1) for each element.
17.121 - ///
17.122 - /// \param minimal The initial minimal value of the heap.
17.123 - /// \param capacity It determines the initial capacity of the heap.
17.124 - RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0)
17.125 - : _iim(map) {
17.126 - boxes.push_back(RadixBox(minimal, 1));
17.127 - boxes.push_back(RadixBox(minimal + 1, 1));
17.128 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
17.129 + /// Constructor.
17.130 + /// \param map A map that assigns \c int values to the items.
17.131 + /// It is used internally to handle the cross references.
17.132 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
17.133 + /// \param minimum The initial minimum value of the heap.
17.134 + /// \param capacity The initial capacity of the heap.
17.135 + RadixHeap(ItemIntMap &map, int minimum = 0, int capacity = 0)
17.136 + : _iim(map)
17.137 + {
17.138 + _boxes.push_back(RadixBox(minimum, 1));
17.139 + _boxes.push_back(RadixBox(minimum + 1, 1));
17.140 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
17.141 extend();
17.142 }
17.143 }
17.144
17.145 - /// The number of items stored in the heap.
17.146 + /// \brief The number of items stored in the heap.
17.147 ///
17.148 - /// \brief Returns the number of items stored in the heap.
17.149 - int size() const { return data.size(); }
17.150 - /// \brief Checks if the heap stores no items.
17.151 + /// This function returns the number of items stored in the heap.
17.152 + int size() const { return _data.size(); }
17.153 +
17.154 + /// \brief Check if the heap is empty.
17.155 ///
17.156 - /// Returns \c true if and only if the heap stores no items.
17.157 - bool empty() const { return data.empty(); }
17.158 + /// This function returns \c true if the heap is empty.
17.159 + bool empty() const { return _data.empty(); }
17.160
17.161 - /// \brief Make empty this heap.
17.162 + /// \brief Make the heap empty.
17.163 ///
17.164 - /// Make empty this heap. It does not change the cross reference
17.165 - /// map. If you want to reuse a heap what is not surely empty you
17.166 - /// should first clear the heap and after that you should set the
17.167 - /// cross reference map for each item to \c PRE_HEAP.
17.168 - void clear(int minimal = 0, int capacity = 0) {
17.169 - data.clear(); boxes.clear();
17.170 - boxes.push_back(RadixBox(minimal, 1));
17.171 - boxes.push_back(RadixBox(minimal + 1, 1));
17.172 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
17.173 + /// This functon makes the heap empty.
17.174 + /// It does not change the cross reference map. If you want to reuse
17.175 + /// a heap that is not surely empty, you should first clear it and
17.176 + /// then you should set the cross reference map to \c PRE_HEAP
17.177 + /// for each item.
17.178 + /// \param minimum The minimum value of the heap.
17.179 + /// \param capacity The capacity of the heap.
17.180 + void clear(int minimum = 0, int capacity = 0) {
17.181 + _data.clear(); _boxes.clear();
17.182 + _boxes.push_back(RadixBox(minimum, 1));
17.183 + _boxes.push_back(RadixBox(minimum + 1, 1));
17.184 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
17.185 extend();
17.186 }
17.187 }
17.188 @@ -149,255 +150,259 @@
17.189 private:
17.190
17.191 bool upper(int box, Prio pr) {
17.192 - return pr < boxes[box].min;
17.193 + return pr < _boxes[box].min;
17.194 }
17.195
17.196 bool lower(int box, Prio pr) {
17.197 - return pr >= boxes[box].min + boxes[box].size;
17.198 + return pr >= _boxes[box].min + _boxes[box].size;
17.199 }
17.200
17.201 - /// \brief Remove item from the box list.
17.202 + // Remove item from the box list
17.203 void remove(int index) {
17.204 - if (data[index].prev >= 0) {
17.205 - data[data[index].prev].next = data[index].next;
17.206 + if (_data[index].prev >= 0) {
17.207 + _data[_data[index].prev].next = _data[index].next;
17.208 } else {
17.209 - boxes[data[index].box].first = data[index].next;
17.210 + _boxes[_data[index].box].first = _data[index].next;
17.211 }
17.212 - if (data[index].next >= 0) {
17.213 - data[data[index].next].prev = data[index].prev;
17.214 + if (_data[index].next >= 0) {
17.215 + _data[_data[index].next].prev = _data[index].prev;
17.216 }
17.217 }
17.218
17.219 - /// \brief Insert item into the box list.
17.220 + // Insert item into the box list
17.221 void insert(int box, int index) {
17.222 - if (boxes[box].first == -1) {
17.223 - boxes[box].first = index;
17.224 - data[index].next = data[index].prev = -1;
17.225 + if (_boxes[box].first == -1) {
17.226 + _boxes[box].first = index;
17.227 + _data[index].next = _data[index].prev = -1;
17.228 } else {
17.229 - data[index].next = boxes[box].first;
17.230 - data[boxes[box].first].prev = index;
17.231 - data[index].prev = -1;
17.232 - boxes[box].first = index;
17.233 + _data[index].next = _boxes[box].first;
17.234 + _data[_boxes[box].first].prev = index;
17.235 + _data[index].prev = -1;
17.236 + _boxes[box].first = index;
17.237 }
17.238 - data[index].box = box;
17.239 + _data[index].box = box;
17.240 }
17.241
17.242 - /// \brief Add a new box to the box list.
17.243 + // Add a new box to the box list
17.244 void extend() {
17.245 - int min = boxes.back().min + boxes.back().size;
17.246 - int bs = 2 * boxes.back().size;
17.247 - boxes.push_back(RadixBox(min, bs));
17.248 + int min = _boxes.back().min + _boxes.back().size;
17.249 + int bs = 2 * _boxes.back().size;
17.250 + _boxes.push_back(RadixBox(min, bs));
17.251 }
17.252
17.253 - /// \brief Move an item up into the proper box.
17.254 - void bubble_up(int index) {
17.255 - if (!lower(data[index].box, data[index].prio)) return;
17.256 + // Move an item up into the proper box.
17.257 + void bubbleUp(int index) {
17.258 + if (!lower(_data[index].box, _data[index].prio)) return;
17.259 remove(index);
17.260 - int box = findUp(data[index].box, data[index].prio);
17.261 + int box = findUp(_data[index].box, _data[index].prio);
17.262 insert(box, index);
17.263 }
17.264
17.265 - /// \brief Find up the proper box for the item with the given prio.
17.266 + // Find up the proper box for the item with the given priority
17.267 int findUp(int start, int pr) {
17.268 while (lower(start, pr)) {
17.269 - if (++start == int(boxes.size())) {
17.270 + if (++start == int(_boxes.size())) {
17.271 extend();
17.272 }
17.273 }
17.274 return start;
17.275 }
17.276
17.277 - /// \brief Move an item down into the proper box.
17.278 - void bubble_down(int index) {
17.279 - if (!upper(data[index].box, data[index].prio)) return;
17.280 + // Move an item down into the proper box
17.281 + void bubbleDown(int index) {
17.282 + if (!upper(_data[index].box, _data[index].prio)) return;
17.283 remove(index);
17.284 - int box = findDown(data[index].box, data[index].prio);
17.285 + int box = findDown(_data[index].box, _data[index].prio);
17.286 insert(box, index);
17.287 }
17.288
17.289 - /// \brief Find up the proper box for the item with the given prio.
17.290 + // Find down the proper box for the item with the given priority
17.291 int findDown(int start, int pr) {
17.292 while (upper(start, pr)) {
17.293 - if (--start < 0) throw UnderFlowPriorityError();
17.294 + if (--start < 0) throw PriorityUnderflowError();
17.295 }
17.296 return start;
17.297 }
17.298
17.299 - /// \brief Find the first not empty box.
17.300 + // Find the first non-empty box
17.301 int findFirst() {
17.302 int first = 0;
17.303 - while (boxes[first].first == -1) ++first;
17.304 + while (_boxes[first].first == -1) ++first;
17.305 return first;
17.306 }
17.307
17.308 - /// \brief Gives back the minimal prio of the box.
17.309 + // Gives back the minimum priority of the given box
17.310 int minValue(int box) {
17.311 - int min = data[boxes[box].first].prio;
17.312 - for (int k = boxes[box].first; k != -1; k = data[k].next) {
17.313 - if (data[k].prio < min) min = data[k].prio;
17.314 + int min = _data[_boxes[box].first].prio;
17.315 + for (int k = _boxes[box].first; k != -1; k = _data[k].next) {
17.316 + if (_data[k].prio < min) min = _data[k].prio;
17.317 }
17.318 return min;
17.319 }
17.320
17.321 - /// \brief Rearrange the items of the heap and makes the
17.322 - /// first box not empty.
17.323 + // Rearrange the items of the heap and make the first box non-empty
17.324 void moveDown() {
17.325 int box = findFirst();
17.326 if (box == 0) return;
17.327 int min = minValue(box);
17.328 for (int i = 0; i <= box; ++i) {
17.329 - boxes[i].min = min;
17.330 - min += boxes[i].size;
17.331 + _boxes[i].min = min;
17.332 + min += _boxes[i].size;
17.333 }
17.334 - int curr = boxes[box].first, next;
17.335 + int curr = _boxes[box].first, next;
17.336 while (curr != -1) {
17.337 - next = data[curr].next;
17.338 - bubble_down(curr);
17.339 + next = _data[curr].next;
17.340 + bubbleDown(curr);
17.341 curr = next;
17.342 }
17.343 }
17.344
17.345 - void relocate_last(int index) {
17.346 - if (index != int(data.size()) - 1) {
17.347 - data[index] = data.back();
17.348 - if (data[index].prev != -1) {
17.349 - data[data[index].prev].next = index;
17.350 + void relocateLast(int index) {
17.351 + if (index != int(_data.size()) - 1) {
17.352 + _data[index] = _data.back();
17.353 + if (_data[index].prev != -1) {
17.354 + _data[_data[index].prev].next = index;
17.355 } else {
17.356 - boxes[data[index].box].first = index;
17.357 + _boxes[_data[index].box].first = index;
17.358 }
17.359 - if (data[index].next != -1) {
17.360 - data[data[index].next].prev = index;
17.361 + if (_data[index].next != -1) {
17.362 + _data[_data[index].next].prev = index;
17.363 }
17.364 - _iim[data[index].item] = index;
17.365 + _iim[_data[index].item] = index;
17.366 }
17.367 - data.pop_back();
17.368 + _data.pop_back();
17.369 }
17.370
17.371 public:
17.372
17.373 /// \brief Insert an item into the heap with the given priority.
17.374 ///
17.375 - /// Adds \c i to the heap with priority \c p.
17.376 + /// This function inserts the given item into the heap with the
17.377 + /// given priority.
17.378 /// \param i The item to insert.
17.379 /// \param p The priority of the item.
17.380 + /// \pre \e i must not be stored in the heap.
17.381 + /// \warning This method may throw an \c UnderFlowPriorityException.
17.382 void push(const Item &i, const Prio &p) {
17.383 - int n = data.size();
17.384 + int n = _data.size();
17.385 _iim.set(i, n);
17.386 - data.push_back(RadixItem(i, p));
17.387 - while (lower(boxes.size() - 1, p)) {
17.388 + _data.push_back(RadixItem(i, p));
17.389 + while (lower(_boxes.size() - 1, p)) {
17.390 extend();
17.391 }
17.392 - int box = findDown(boxes.size() - 1, p);
17.393 + int box = findDown(_boxes.size() - 1, p);
17.394 insert(box, n);
17.395 }
17.396
17.397 - /// \brief Returns the item with minimum priority.
17.398 + /// \brief Return the item having minimum priority.
17.399 ///
17.400 - /// This method returns the item with minimum priority.
17.401 - /// \pre The heap must be nonempty.
17.402 + /// This function returns the item having minimum priority.
17.403 + /// \pre The heap must be non-empty.
17.404 Item top() const {
17.405 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
17.406 - return data[boxes[0].first].item;
17.407 + return _data[_boxes[0].first].item;
17.408 }
17.409
17.410 - /// \brief Returns the minimum priority.
17.411 + /// \brief The minimum priority.
17.412 ///
17.413 - /// It returns the minimum priority.
17.414 - /// \pre The heap must be nonempty.
17.415 + /// This function returns the minimum priority.
17.416 + /// \pre The heap must be non-empty.
17.417 Prio prio() const {
17.418 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
17.419 - return data[boxes[0].first].prio;
17.420 + return _data[_boxes[0].first].prio;
17.421 }
17.422
17.423 - /// \brief Deletes the item with minimum priority.
17.424 + /// \brief Remove the item having minimum priority.
17.425 ///
17.426 - /// This method deletes the item with minimum priority.
17.427 + /// This function removes the item having minimum priority.
17.428 /// \pre The heap must be non-empty.
17.429 void pop() {
17.430 moveDown();
17.431 - int index = boxes[0].first;
17.432 - _iim[data[index].item] = POST_HEAP;
17.433 + int index = _boxes[0].first;
17.434 + _iim[_data[index].item] = POST_HEAP;
17.435 remove(index);
17.436 - relocate_last(index);
17.437 + relocateLast(index);
17.438 }
17.439
17.440 - /// \brief Deletes \c i from the heap.
17.441 + /// \brief Remove the given item from the heap.
17.442 ///
17.443 - /// This method deletes item \c i from the heap, if \c i was
17.444 - /// already stored in the heap.
17.445 - /// \param i The item to erase.
17.446 + /// This function removes the given item from the heap if it is
17.447 + /// already stored.
17.448 + /// \param i The item to delete.
17.449 + /// \pre \e i must be in the heap.
17.450 void erase(const Item &i) {
17.451 int index = _iim[i];
17.452 _iim[i] = POST_HEAP;
17.453 remove(index);
17.454 - relocate_last(index);
17.455 + relocateLast(index);
17.456 }
17.457
17.458 - /// \brief Returns the priority of \c i.
17.459 + /// \brief The priority of the given item.
17.460 ///
17.461 - /// This function returns the priority of item \c i.
17.462 - /// \pre \c i must be in the heap.
17.463 + /// This function returns the priority of the given item.
17.464 /// \param i The item.
17.465 + /// \pre \e i must be in the heap.
17.466 Prio operator[](const Item &i) const {
17.467 int idx = _iim[i];
17.468 - return data[idx].prio;
17.469 + return _data[idx].prio;
17.470 }
17.471
17.472 - /// \brief \c i gets to the heap with priority \c p independently
17.473 - /// if \c i was already there.
17.474 + /// \brief Set the priority of an item or insert it, if it is
17.475 + /// not stored in the heap.
17.476 ///
17.477 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
17.478 - /// in the heap and sets the priority of \c i to \c p otherwise.
17.479 - /// It may throw an \e UnderFlowPriorityException.
17.480 + /// This method sets the priority of the given item if it is
17.481 + /// already stored in the heap. Otherwise it inserts the given
17.482 + /// item into the heap with the given priority.
17.483 /// \param i The item.
17.484 /// \param p The priority.
17.485 + /// \pre \e i must be in the heap.
17.486 + /// \warning This method may throw an \c UnderFlowPriorityException.
17.487 void set(const Item &i, const Prio &p) {
17.488 int idx = _iim[i];
17.489 if( idx < 0 ) {
17.490 push(i, p);
17.491 }
17.492 - else if( p >= data[idx].prio ) {
17.493 - data[idx].prio = p;
17.494 - bubble_up(idx);
17.495 + else if( p >= _data[idx].prio ) {
17.496 + _data[idx].prio = p;
17.497 + bubbleUp(idx);
17.498 } else {
17.499 - data[idx].prio = p;
17.500 - bubble_down(idx);
17.501 + _data[idx].prio = p;
17.502 + bubbleDown(idx);
17.503 }
17.504 }
17.505
17.506 -
17.507 - /// \brief Decreases the priority of \c i to \c p.
17.508 + /// \brief Decrease the priority of an item to the given value.
17.509 ///
17.510 - /// This method decreases the priority of item \c i to \c p.
17.511 - /// \pre \c i must be stored in the heap with priority at least \c p, and
17.512 - /// \c should be greater or equal to the last removed item's priority.
17.513 + /// This function decreases the priority of an item to the given value.
17.514 /// \param i The item.
17.515 /// \param p The priority.
17.516 + /// \pre \e i must be stored in the heap with priority at least \e p.
17.517 + /// \warning This method may throw an \c UnderFlowPriorityException.
17.518 void decrease(const Item &i, const Prio &p) {
17.519 int idx = _iim[i];
17.520 - data[idx].prio = p;
17.521 - bubble_down(idx);
17.522 + _data[idx].prio = p;
17.523 + bubbleDown(idx);
17.524 }
17.525
17.526 - /// \brief Increases the priority of \c i to \c p.
17.527 + /// \brief Increase the priority of an item to the given value.
17.528 ///
17.529 - /// This method sets the priority of item \c i to \c p.
17.530 - /// \pre \c i must be stored in the heap with priority at most \c p
17.531 + /// This function increases the priority of an item to the given value.
17.532 /// \param i The item.
17.533 /// \param p The priority.
17.534 + /// \pre \e i must be stored in the heap with priority at most \e p.
17.535 void increase(const Item &i, const Prio &p) {
17.536 int idx = _iim[i];
17.537 - data[idx].prio = p;
17.538 - bubble_up(idx);
17.539 + _data[idx].prio = p;
17.540 + bubbleUp(idx);
17.541 }
17.542
17.543 - /// \brief Returns if \c item is in, has already been in, or has
17.544 - /// never been in the heap.
17.545 + /// \brief Return the state of an item.
17.546 ///
17.547 - /// This method returns PRE_HEAP if \c item has never been in the
17.548 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
17.549 - /// otherwise. In the latter case it is possible that \c item will
17.550 - /// get back to the heap again.
17.551 + /// This method returns \c PRE_HEAP if the given item has never
17.552 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
17.553 + /// and \c POST_HEAP otherwise.
17.554 + /// In the latter case it is possible that the item will get back
17.555 + /// to the heap again.
17.556 /// \param i The item.
17.557 State state(const Item &i) const {
17.558 int s = _iim[i];
17.559 @@ -405,11 +410,11 @@
17.560 return State(s);
17.561 }
17.562
17.563 - /// \brief Sets the state of the \c item in the heap.
17.564 + /// \brief Set the state of an item in the heap.
17.565 ///
17.566 - /// Sets the state of the \c item in the heap. It can be used to
17.567 - /// manually clear the heap when it is important to achive the
17.568 - /// better time complexity.
17.569 + /// This function sets the state of the given item in the heap.
17.570 + /// It can be used to manually clear the heap when it is important
17.571 + /// to achive better time complexity.
17.572 /// \param i The item.
17.573 /// \param st The state. It should not be \c IN_HEAP.
17.574 void state(const Item& i, State st) {
18.1 --- a/test/CMakeLists.txt Fri Jul 24 11:07:52 2009 +0200
18.2 +++ b/test/CMakeLists.txt Fri Sep 25 09:06:32 2009 +0200
18.3 @@ -9,6 +9,7 @@
18.4
18.5 SET(TESTS
18.6 adaptors_test
18.7 + bellman_ford_test
18.8 bfs_test
18.9 circulation_test
18.10 connectivity_test
19.1 --- a/test/Makefile.am Fri Jul 24 11:07:52 2009 +0200
19.2 +++ b/test/Makefile.am Fri Sep 25 09:06:32 2009 +0200
19.3 @@ -7,6 +7,7 @@
19.4
19.5 check_PROGRAMS += \
19.6 test/adaptors_test \
19.7 + test/bellman_ford_test \
19.8 test/bfs_test \
19.9 test/circulation_test \
19.10 test/connectivity_test \
19.11 @@ -52,6 +53,7 @@
19.12 XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
19.13
19.14 test_adaptors_test_SOURCES = test/adaptors_test.cc
19.15 +test_bellman_ford_test_SOURCES = test/bellman_ford_test.cc
19.16 test_bfs_test_SOURCES = test/bfs_test.cc
19.17 test_circulation_test_SOURCES = test/circulation_test.cc
19.18 test_counter_test_SOURCES = test/counter_test.cc
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
20.2 +++ b/test/bellman_ford_test.cc Fri Sep 25 09:06:32 2009 +0200
20.3 @@ -0,0 +1,283 @@
20.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
20.5 + *
20.6 + * This file is a part of LEMON, a generic C++ optimization library.
20.7 + *
20.8 + * Copyright (C) 2003-2009
20.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
20.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
20.11 + *
20.12 + * Permission to use, modify and distribute this software is granted
20.13 + * provided that this copyright notice appears in all copies. For
20.14 + * precise terms see the accompanying LICENSE file.
20.15 + *
20.16 + * This software is provided "AS IS" with no warranty of any kind,
20.17 + * express or implied, and with no claim as to its suitability for any
20.18 + * purpose.
20.19 + *
20.20 + */
20.21 +
20.22 +#include <lemon/concepts/digraph.h>
20.23 +#include <lemon/smart_graph.h>
20.24 +#include <lemon/list_graph.h>
20.25 +#include <lemon/lgf_reader.h>
20.26 +#include <lemon/bellman_ford.h>
20.27 +#include <lemon/path.h>
20.28 +
20.29 +#include "graph_test.h"
20.30 +#include "test_tools.h"
20.31 +
20.32 +using namespace lemon;
20.33 +
20.34 +char test_lgf[] =
20.35 + "@nodes\n"
20.36 + "label\n"
20.37 + "0\n"
20.38 + "1\n"
20.39 + "2\n"
20.40 + "3\n"
20.41 + "4\n"
20.42 + "@arcs\n"
20.43 + " length\n"
20.44 + "0 1 3\n"
20.45 + "1 2 -3\n"
20.46 + "1 2 -5\n"
20.47 + "1 3 -2\n"
20.48 + "0 2 -1\n"
20.49 + "1 2 -4\n"
20.50 + "0 3 2\n"
20.51 + "4 2 -5\n"
20.52 + "2 3 1\n"
20.53 + "@attributes\n"
20.54 + "source 0\n"
20.55 + "target 3\n";
20.56 +
20.57 +
20.58 +void checkBellmanFordCompile()
20.59 +{
20.60 + typedef int Value;
20.61 + typedef concepts::Digraph Digraph;
20.62 + typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
20.63 + typedef BellmanFord<Digraph, LengthMap> BF;
20.64 + typedef Digraph::Node Node;
20.65 + typedef Digraph::Arc Arc;
20.66 +
20.67 + Digraph gr;
20.68 + Node s, t, n;
20.69 + Arc e;
20.70 + Value l;
20.71 + int k;
20.72 + bool b;
20.73 + BF::DistMap d(gr);
20.74 + BF::PredMap p(gr);
20.75 + LengthMap length;
20.76 + concepts::Path<Digraph> pp;
20.77 +
20.78 + {
20.79 + BF bf_test(gr,length);
20.80 + const BF& const_bf_test = bf_test;
20.81 +
20.82 + bf_test.run(s);
20.83 + bf_test.run(s,k);
20.84 +
20.85 + bf_test.init();
20.86 + bf_test.addSource(s);
20.87 + bf_test.addSource(s, 1);
20.88 + b = bf_test.processNextRound();
20.89 + b = bf_test.processNextWeakRound();
20.90 +
20.91 + bf_test.start();
20.92 + bf_test.checkedStart();
20.93 + bf_test.limitedStart(k);
20.94 +
20.95 + l = const_bf_test.dist(t);
20.96 + e = const_bf_test.predArc(t);
20.97 + s = const_bf_test.predNode(t);
20.98 + b = const_bf_test.reached(t);
20.99 + d = const_bf_test.distMap();
20.100 + p = const_bf_test.predMap();
20.101 + pp = const_bf_test.path(t);
20.102 +
20.103 + for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
20.104 + }
20.105 + {
20.106 + BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
20.107 + ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
20.108 + ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
20.109 + ::Create bf_test(gr,length);
20.110 +
20.111 + LengthMap length_map;
20.112 + concepts::ReadWriteMap<Node,Arc> pred_map;
20.113 + concepts::ReadWriteMap<Node,Value> dist_map;
20.114 +
20.115 + bf_test
20.116 + .lengthMap(length_map)
20.117 + .predMap(pred_map)
20.118 + .distMap(dist_map);
20.119 +
20.120 + bf_test.run(s);
20.121 + bf_test.run(s,k);
20.122 +
20.123 + bf_test.init();
20.124 + bf_test.addSource(s);
20.125 + bf_test.addSource(s, 1);
20.126 + b = bf_test.processNextRound();
20.127 + b = bf_test.processNextWeakRound();
20.128 +
20.129 + bf_test.start();
20.130 + bf_test.checkedStart();
20.131 + bf_test.limitedStart(k);
20.132 +
20.133 + l = bf_test.dist(t);
20.134 + e = bf_test.predArc(t);
20.135 + s = bf_test.predNode(t);
20.136 + b = bf_test.reached(t);
20.137 + pp = bf_test.path(t);
20.138 + }
20.139 +}
20.140 +
20.141 +void checkBellmanFordFunctionCompile()
20.142 +{
20.143 + typedef int Value;
20.144 + typedef concepts::Digraph Digraph;
20.145 + typedef Digraph::Arc Arc;
20.146 + typedef Digraph::Node Node;
20.147 + typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
20.148 +
20.149 + Digraph g;
20.150 + bool b;
20.151 + bellmanFord(g,LengthMap()).run(Node());
20.152 + b = bellmanFord(g,LengthMap()).run(Node(),Node());
20.153 + bellmanFord(g,LengthMap())
20.154 + .predMap(concepts::ReadWriteMap<Node,Arc>())
20.155 + .distMap(concepts::ReadWriteMap<Node,Value>())
20.156 + .run(Node());
20.157 + b=bellmanFord(g,LengthMap())
20.158 + .predMap(concepts::ReadWriteMap<Node,Arc>())
20.159 + .distMap(concepts::ReadWriteMap<Node,Value>())
20.160 + .path(concepts::Path<Digraph>())
20.161 + .dist(Value())
20.162 + .run(Node(),Node());
20.163 +}
20.164 +
20.165 +
20.166 +template <typename Digraph, typename Value>
20.167 +void checkBellmanFord() {
20.168 + TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
20.169 + typedef typename Digraph::template ArcMap<Value> LengthMap;
20.170 +
20.171 + Digraph gr;
20.172 + Node s, t;
20.173 + LengthMap length(gr);
20.174 +
20.175 + std::istringstream input(test_lgf);
20.176 + digraphReader(gr, input).
20.177 + arcMap("length", length).
20.178 + node("source", s).
20.179 + node("target", t).
20.180 + run();
20.181 +
20.182 + BellmanFord<Digraph, LengthMap>
20.183 + bf(gr, length);
20.184 + bf.run(s);
20.185 + Path<Digraph> p = bf.path(t);
20.186 +
20.187 + check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
20.188 + check(p.length() == 3, "path() found a wrong path.");
20.189 + check(checkPath(gr, p), "path() found a wrong path.");
20.190 + check(pathSource(gr, p) == s, "path() found a wrong path.");
20.191 + check(pathTarget(gr, p) == t, "path() found a wrong path.");
20.192 +
20.193 + ListPath<Digraph> path;
20.194 + Value dist;
20.195 + bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
20.196 +
20.197 + check(reached && dist == -1, "Bellman-Ford found a wrong path.");
20.198 + check(path.length() == 3, "path() found a wrong path.");
20.199 + check(checkPath(gr, path), "path() found a wrong path.");
20.200 + check(pathSource(gr, path) == s, "path() found a wrong path.");
20.201 + check(pathTarget(gr, path) == t, "path() found a wrong path.");
20.202 +
20.203 + for(ArcIt e(gr); e!=INVALID; ++e) {
20.204 + Node u=gr.source(e);
20.205 + Node v=gr.target(e);
20.206 + check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
20.207 + "Wrong output. dist(target)-dist(source)-arc_length=" <<
20.208 + bf.dist(v) - bf.dist(u) - length[e]);
20.209 + }
20.210 +
20.211 + for(NodeIt v(gr); v!=INVALID; ++v) {
20.212 + if (bf.reached(v)) {
20.213 + check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
20.214 + if (bf.predArc(v)!=INVALID ) {
20.215 + Arc e=bf.predArc(v);
20.216 + Node u=gr.source(e);
20.217 + check(u==bf.predNode(v),"Wrong tree.");
20.218 + check(bf.dist(v) - bf.dist(u) == length[e],
20.219 + "Wrong distance! Difference: " <<
20.220 + bf.dist(v) - bf.dist(u) - length[e]);
20.221 + }
20.222 + }
20.223 + }
20.224 +}
20.225 +
20.226 +void checkBellmanFordNegativeCycle() {
20.227 + DIGRAPH_TYPEDEFS(SmartDigraph);
20.228 +
20.229 + SmartDigraph gr;
20.230 + IntArcMap length(gr);
20.231 +
20.232 + Node n1 = gr.addNode();
20.233 + Node n2 = gr.addNode();
20.234 + Node n3 = gr.addNode();
20.235 + Node n4 = gr.addNode();
20.236 +
20.237 + Arc a1 = gr.addArc(n1, n2);
20.238 + Arc a2 = gr.addArc(n2, n2);
20.239 +
20.240 + length[a1] = 2;
20.241 + length[a2] = -1;
20.242 +
20.243 + {
20.244 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
20.245 + bf.run(n1);
20.246 + StaticPath<SmartDigraph> p = bf.negativeCycle();
20.247 + check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
20.248 + "Wrong negative cycle.");
20.249 + }
20.250 +
20.251 + length[a2] = 0;
20.252 +
20.253 + {
20.254 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
20.255 + bf.run(n1);
20.256 + check(bf.negativeCycle().empty(),
20.257 + "Negative cycle should not be found.");
20.258 + }
20.259 +
20.260 + length[gr.addArc(n1, n3)] = 5;
20.261 + length[gr.addArc(n4, n3)] = 1;
20.262 + length[gr.addArc(n2, n4)] = 2;
20.263 + length[gr.addArc(n3, n2)] = -4;
20.264 +
20.265 + {
20.266 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
20.267 + bf.init();
20.268 + bf.addSource(n1);
20.269 + for (int i = 0; i < 4; ++i) {
20.270 + check(bf.negativeCycle().empty(),
20.271 + "Negative cycle should not be found.");
20.272 + bf.processNextRound();
20.273 + }
20.274 + StaticPath<SmartDigraph> p = bf.negativeCycle();
20.275 + check(p.length() == 3, "Wrong negative cycle.");
20.276 + check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
20.277 + "Wrong negative cycle.");
20.278 + }
20.279 +}
20.280 +
20.281 +int main() {
20.282 + checkBellmanFord<ListDigraph, int>();
20.283 + checkBellmanFord<SmartDigraph, double>();
20.284 + checkBellmanFordNegativeCycle();
20.285 + return 0;
20.286 +}
21.1 --- a/test/circulation_test.cc Fri Jul 24 11:07:52 2009 +0200
21.2 +++ b/test/circulation_test.cc Fri Sep 25 09:06:32 2009 +0200
21.3 @@ -87,6 +87,11 @@
21.4 .upperMap(ucap)
21.5 .supplyMap(supply)
21.6 .flowMap(flow);
21.7 +
21.8 + const CirculationType::Elevator& elev = const_circ_test.elevator();
21.9 + circ_test.elevator(const_cast<CirculationType::Elevator&>(elev));
21.10 + CirculationType::Tolerance tol = const_circ_test.tolerance();
21.11 + circ_test.tolerance(tol);
21.12
21.13 circ_test.init();
21.14 circ_test.greedyInit();
22.1 --- a/test/heap_test.cc Fri Jul 24 11:07:52 2009 +0200
22.2 +++ b/test/heap_test.cc Fri Sep 25 09:06:32 2009 +0200
22.3 @@ -25,14 +25,17 @@
22.4 #include <lemon/concepts/heap.h>
22.5
22.6 #include <lemon/smart_graph.h>
22.7 -
22.8 #include <lemon/lgf_reader.h>
22.9 #include <lemon/dijkstra.h>
22.10 #include <lemon/maps.h>
22.11
22.12 #include <lemon/bin_heap.h>
22.13 +#include <lemon/fourary_heap.h>
22.14 +#include <lemon/kary_heap.h>
22.15 #include <lemon/fib_heap.h>
22.16 +#include <lemon/pairing_heap.h>
22.17 #include <lemon/radix_heap.h>
22.18 +#include <lemon/binom_heap.h>
22.19 #include <lemon/bucket_heap.h>
22.20
22.21 #include "test_tools.h"
22.22 @@ -89,18 +92,16 @@
22.23 template <typename Heap>
22.24 void heapSortTest() {
22.25 RangeMap<int> map(test_len, -1);
22.26 -
22.27 Heap heap(map);
22.28
22.29 std::vector<int> v(test_len);
22.30 -
22.31 for (int i = 0; i < test_len; ++i) {
22.32 v[i] = test_seq[i];
22.33 heap.push(i, v[i]);
22.34 }
22.35 std::sort(v.begin(), v.end());
22.36 for (int i = 0; i < test_len; ++i) {
22.37 - check(v[i] == heap.prio() ,"Wrong order in heap sort.");
22.38 + check(v[i] == heap.prio(), "Wrong order in heap sort.");
22.39 heap.pop();
22.40 }
22.41 }
22.42 @@ -112,7 +113,6 @@
22.43 Heap heap(map);
22.44
22.45 std::vector<int> v(test_len);
22.46 -
22.47 for (int i = 0; i < test_len; ++i) {
22.48 v[i] = test_seq[i];
22.49 heap.push(i, v[i]);
22.50 @@ -123,13 +123,11 @@
22.51 }
22.52 std::sort(v.begin(), v.end());
22.53 for (int i = 0; i < test_len; ++i) {
22.54 - check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
22.55 + check(v[i] == heap.prio(), "Wrong order in heap increase test.");
22.56 heap.pop();
22.57 }
22.58 }
22.59
22.60 -
22.61 -
22.62 template <typename Heap>
22.63 void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
22.64 Node source) {
22.65 @@ -144,7 +142,7 @@
22.66 Node t = digraph.target(a);
22.67 if (dijkstra.reached(s)) {
22.68 check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
22.69 - "Error in a shortest path tree!");
22.70 + "Error in shortest path tree.");
22.71 }
22.72 }
22.73
22.74 @@ -153,7 +151,7 @@
22.75 Arc a = dijkstra.predArc(n);
22.76 Node s = digraph.source(a);
22.77 check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
22.78 - "Error in a shortest path tree!");
22.79 + "Error in shortest path tree.");
22.80 }
22.81 }
22.82
22.83 @@ -175,6 +173,7 @@
22.84 node("source", source).
22.85 run();
22.86
22.87 + // BinHeap
22.88 {
22.89 typedef BinHeap<Prio, ItemIntMap> IntHeap;
22.90 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.91 @@ -186,6 +185,31 @@
22.92 dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.93 }
22.94
22.95 + // FouraryHeap
22.96 + {
22.97 + typedef FouraryHeap<Prio, ItemIntMap> IntHeap;
22.98 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.99 + heapSortTest<IntHeap>();
22.100 + heapIncreaseTest<IntHeap>();
22.101 +
22.102 + typedef FouraryHeap<Prio, IntNodeMap > NodeHeap;
22.103 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
22.104 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.105 + }
22.106 +
22.107 + // KaryHeap
22.108 + {
22.109 + typedef KaryHeap<Prio, ItemIntMap> IntHeap;
22.110 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.111 + heapSortTest<IntHeap>();
22.112 + heapIncreaseTest<IntHeap>();
22.113 +
22.114 + typedef KaryHeap<Prio, IntNodeMap > NodeHeap;
22.115 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
22.116 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.117 + }
22.118 +
22.119 + // FibHeap
22.120 {
22.121 typedef FibHeap<Prio, ItemIntMap> IntHeap;
22.122 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.123 @@ -197,6 +221,19 @@
22.124 dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.125 }
22.126
22.127 + // PairingHeap
22.128 + {
22.129 + typedef PairingHeap<Prio, ItemIntMap> IntHeap;
22.130 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.131 + heapSortTest<IntHeap>();
22.132 + heapIncreaseTest<IntHeap>();
22.133 +
22.134 + typedef PairingHeap<Prio, IntNodeMap > NodeHeap;
22.135 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
22.136 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.137 + }
22.138 +
22.139 + // RadixHeap
22.140 {
22.141 typedef RadixHeap<ItemIntMap> IntHeap;
22.142 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.143 @@ -208,6 +245,19 @@
22.144 dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.145 }
22.146
22.147 + // BinomHeap
22.148 + {
22.149 + typedef BinomHeap<Prio, ItemIntMap> IntHeap;
22.150 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.151 + heapSortTest<IntHeap>();
22.152 + heapIncreaseTest<IntHeap>();
22.153 +
22.154 + typedef BinomHeap<Prio, IntNodeMap > NodeHeap;
22.155 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
22.156 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.157 + }
22.158 +
22.159 + // BucketHeap, SimpleBucketHeap
22.160 {
22.161 typedef BucketHeap<ItemIntMap> IntHeap;
22.162 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
22.163 @@ -217,8 +267,10 @@
22.164 typedef BucketHeap<IntNodeMap > NodeHeap;
22.165 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
22.166 dijkstraHeapTest<NodeHeap>(digraph, length, source);
22.167 +
22.168 + typedef SimpleBucketHeap<ItemIntMap> SimpleIntHeap;
22.169 + heapSortTest<SimpleIntHeap>();
22.170 }
22.171
22.172 -
22.173 return 0;
22.174 }
23.1 --- a/test/maps_test.cc Fri Jul 24 11:07:52 2009 +0200
23.2 +++ b/test/maps_test.cc Fri Sep 25 09:06:32 2009 +0200
23.3 @@ -22,6 +22,7 @@
23.4 #include <lemon/concept_check.h>
23.5 #include <lemon/concepts/maps.h>
23.6 #include <lemon/maps.h>
23.7 +#include <lemon/smart_graph.h>
23.8
23.9 #include "test_tools.h"
23.10
23.11 @@ -349,5 +350,226 @@
23.12 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
23.13 }
23.14
23.15 + // CrossRefMap
23.16 + {
23.17 + typedef SmartDigraph Graph;
23.18 + DIGRAPH_TYPEDEFS(Graph);
23.19 +
23.20 + checkConcept<ReadWriteMap<Node, int>,
23.21 + CrossRefMap<Graph, Node, int> >();
23.22 +
23.23 + Graph gr;
23.24 + typedef CrossRefMap<Graph, Node, char> CRMap;
23.25 + typedef CRMap::ValueIterator ValueIt;
23.26 + CRMap map(gr);
23.27 +
23.28 + Node n0 = gr.addNode();
23.29 + Node n1 = gr.addNode();
23.30 + Node n2 = gr.addNode();
23.31 +
23.32 + map.set(n0, 'A');
23.33 + map.set(n1, 'B');
23.34 + map.set(n2, 'C');
23.35 + map.set(n2, 'A');
23.36 + map.set(n0, 'C');
23.37 +
23.38 + check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
23.39 + "Wrong CrossRefMap");
23.40 + check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
23.41 + check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
23.42 + check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
23.43 +
23.44 + ValueIt it = map.beginValue();
23.45 + check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
23.46 + it == map.endValue(), "Wrong value iterator");
23.47 + }
23.48 +
23.49 + // Iterable bool map
23.50 + {
23.51 + typedef SmartGraph Graph;
23.52 + typedef SmartGraph::Node Item;
23.53 +
23.54 + typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
23.55 + checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
23.56 +
23.57 + const int num = 10;
23.58 + Graph g;
23.59 + std::vector<Item> items;
23.60 + for (int i = 0; i < num; ++i) {
23.61 + items.push_back(g.addNode());
23.62 + }
23.63 +
23.64 + Ibm map1(g, true);
23.65 + int n = 0;
23.66 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
23.67 + check(map1[static_cast<Item>(it)], "Wrong TrueIt");
23.68 + ++n;
23.69 + }
23.70 + check(n == num, "Wrong number");
23.71 +
23.72 + n = 0;
23.73 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
23.74 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
23.75 + ++n;
23.76 + }
23.77 + check(n == num, "Wrong number");
23.78 + check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
23.79 + check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
23.80 +
23.81 + map1[items[5]] = true;
23.82 +
23.83 + n = 0;
23.84 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
23.85 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
23.86 + ++n;
23.87 + }
23.88 + check(n == num, "Wrong number");
23.89 +
23.90 + map1[items[num / 2]] = false;
23.91 + check(map1[items[num / 2]] == false, "Wrong map value");
23.92 +
23.93 + n = 0;
23.94 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
23.95 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
23.96 + ++n;
23.97 + }
23.98 + check(n == num - 1, "Wrong number");
23.99 +
23.100 + n = 0;
23.101 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
23.102 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
23.103 + ++n;
23.104 + }
23.105 + check(n == 1, "Wrong number");
23.106 +
23.107 + map1[items[0]] = false;
23.108 + check(map1[items[0]] == false, "Wrong map value");
23.109 +
23.110 + map1[items[num - 1]] = false;
23.111 + check(map1[items[num - 1]] == false, "Wrong map value");
23.112 +
23.113 + n = 0;
23.114 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
23.115 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
23.116 + ++n;
23.117 + }
23.118 + check(n == num - 3, "Wrong number");
23.119 + check(map1.trueNum() == num - 3, "Wrong number");
23.120 +
23.121 + n = 0;
23.122 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
23.123 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
23.124 + ++n;
23.125 + }
23.126 + check(n == 3, "Wrong number");
23.127 + check(map1.falseNum() == 3, "Wrong number");
23.128 + }
23.129 +
23.130 + // Iterable int map
23.131 + {
23.132 + typedef SmartGraph Graph;
23.133 + typedef SmartGraph::Node Item;
23.134 + typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
23.135 +
23.136 + checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
23.137 +
23.138 + const int num = 10;
23.139 + Graph g;
23.140 + std::vector<Item> items;
23.141 + for (int i = 0; i < num; ++i) {
23.142 + items.push_back(g.addNode());
23.143 + }
23.144 +
23.145 + Iim map1(g);
23.146 + check(map1.size() == 0, "Wrong size");
23.147 +
23.148 + for (int i = 0; i < num; ++i) {
23.149 + map1[items[i]] = i;
23.150 + }
23.151 + check(map1.size() == num, "Wrong size");
23.152 +
23.153 + for (int i = 0; i < num; ++i) {
23.154 + Iim::ItemIt it(map1, i);
23.155 + check(static_cast<Item>(it) == items[i], "Wrong value");
23.156 + ++it;
23.157 + check(static_cast<Item>(it) == INVALID, "Wrong value");
23.158 + }
23.159 +
23.160 + for (int i = 0; i < num; ++i) {
23.161 + map1[items[i]] = i % 2;
23.162 + }
23.163 + check(map1.size() == 2, "Wrong size");
23.164 +
23.165 + int n = 0;
23.166 + for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
23.167 + check(map1[static_cast<Item>(it)] == 0, "Wrong value");
23.168 + ++n;
23.169 + }
23.170 + check(n == (num + 1) / 2, "Wrong number");
23.171 +
23.172 + for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
23.173 + check(map1[static_cast<Item>(it)] == 1, "Wrong value");
23.174 + ++n;
23.175 + }
23.176 + check(n == num, "Wrong number");
23.177 +
23.178 + }
23.179 +
23.180 + // Iterable value map
23.181 + {
23.182 + typedef SmartGraph Graph;
23.183 + typedef SmartGraph::Node Item;
23.184 + typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
23.185 +
23.186 + checkConcept<ReadWriteMap<Item, double>, Ivm>();
23.187 +
23.188 + const int num = 10;
23.189 + Graph g;
23.190 + std::vector<Item> items;
23.191 + for (int i = 0; i < num; ++i) {
23.192 + items.push_back(g.addNode());
23.193 + }
23.194 +
23.195 + Ivm map1(g, 0.0);
23.196 + check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
23.197 + check(*map1.beginValue() == 0.0, "Wrong value");
23.198 +
23.199 + for (int i = 0; i < num; ++i) {
23.200 + map1.set(items[i], static_cast<double>(i));
23.201 + }
23.202 + check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
23.203 +
23.204 + for (int i = 0; i < num; ++i) {
23.205 + Ivm::ItemIt it(map1, static_cast<double>(i));
23.206 + check(static_cast<Item>(it) == items[i], "Wrong value");
23.207 + ++it;
23.208 + check(static_cast<Item>(it) == INVALID, "Wrong value");
23.209 + }
23.210 +
23.211 + for (Ivm::ValueIterator vit = map1.beginValue();
23.212 + vit != map1.endValue(); ++vit) {
23.213 + check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
23.214 + "Wrong ValueIterator");
23.215 + }
23.216 +
23.217 + for (int i = 0; i < num; ++i) {
23.218 + map1.set(items[i], static_cast<double>(i % 2));
23.219 + }
23.220 + check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
23.221 +
23.222 + int n = 0;
23.223 + for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
23.224 + check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
23.225 + ++n;
23.226 + }
23.227 + check(n == (num + 1) / 2, "Wrong number");
23.228 +
23.229 + for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
23.230 + check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
23.231 + ++n;
23.232 + }
23.233 + check(n == num, "Wrong number");
23.234 +
23.235 + }
23.236 return 0;
23.237 }
24.1 --- a/test/preflow_test.cc Fri Jul 24 11:07:52 2009 +0200
24.2 +++ b/test/preflow_test.cc Fri Sep 25 09:06:32 2009 +0200
24.3 @@ -94,6 +94,11 @@
24.4 ::Create PreflowType;
24.5 PreflowType preflow_test(g, cap, n, n);
24.6 const PreflowType& const_preflow_test = preflow_test;
24.7 +
24.8 + const PreflowType::Elevator& elev = const_preflow_test.elevator();
24.9 + preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
24.10 + PreflowType::Tolerance tol = const_preflow_test.tolerance();
24.11 + preflow_test.tolerance(tol);
24.12
24.13 preflow_test
24.14 .capacityMap(cap)
25.1 --- a/tools/lemon-0.x-to-1.x.sh Fri Jul 24 11:07:52 2009 +0200
25.2 +++ b/tools/lemon-0.x-to-1.x.sh Fri Sep 25 09:06:32 2009 +0200
25.3 @@ -35,10 +35,10 @@
25.4 -e "s/IncEdgeIt/_In_cEd_geIt_label_/g"\
25.5 -e "s/Edge\>/_Ar_c_label_/g"\
25.6 -e "s/\<edge\>/_ar_c_label_/g"\
25.7 - -e "s/_edge\>/_ar_c_label_/g"\
25.8 + -e "s/_edge\>/__ar_c_label_/g"\
25.9 -e "s/Edges\>/_Ar_c_label_s/g"\
25.10 -e "s/\<edges\>/_ar_c_label_s/g"\
25.11 - -e "s/_edges\>/_ar_c_label_s/g"\
25.12 + -e "s/_edges\>/__ar_c_label_s/g"\
25.13 -e "s/\([Ee]\)dge\([a-z]\)/_\1d_ge_label_\2/g"\
25.14 -e "s/\([a-z]\)edge/\1_ed_ge_label_/g"\
25.15 -e "s/Edge/_Ar_c_label_/g"\
25.16 @@ -68,6 +68,11 @@
25.17 -e "s/_blu_e_label_/blue/g"\
25.18 -e "s/_GR_APH_TY_PEDE_FS_label_/GRAPH_TYPEDEFS/g"\
25.19 -e "s/_DIGR_APH_TY_PEDE_FS_label_/DIGRAPH_TYPEDEFS/g"\
25.20 + -e "s/\<digraph_adaptor\.h\>/adaptors.h/g"\
25.21 + -e "s/\<digraph_utils\.h\>/core.h/g"\
25.22 + -e "s/\<digraph_reader\.h\>/lgf_reader.h/g"\
25.23 + -e "s/\<digraph_writer\.h\>/lgf_writer.h/g"\
25.24 + -e "s/\<topology\.h\>/connectivity.h/g"\
25.25 -e "s/DigraphToEps/GraphToEps/g"\
25.26 -e "s/digraphToEps/graphToEps/g"\
25.27 -e "s/\<DefPredMap\>/SetPredMap/g"\