1.1 --- a/doc/groups.dox Wed Sep 30 08:36:43 2009 +0200
1.2 +++ b/doc/groups.dox Wed Sep 30 08:41:06 2009 +0200
1.3 @@ -226,14 +226,6 @@
1.4 */
1.5
1.6 /**
1.7 -@defgroup matrices Matrices
1.8 -@ingroup datas
1.9 -\brief Two dimensional data storages implemented in LEMON.
1.10 -
1.11 -This group contains two dimensional data storages implemented in LEMON.
1.12 -*/
1.13 -
1.14 -/**
1.15 @defgroup paths Path Structures
1.16 @ingroup datas
1.17 \brief %Path structures implemented in LEMON.
1.18 @@ -246,7 +238,36 @@
1.19 efficient to have e.g. the Dijkstra algorithm to store its result in
1.20 any kind of path structure.
1.21
1.22 -\sa lemon::concepts::Path
1.23 +\sa \ref concepts::Path "Path concept"
1.24 +*/
1.25 +
1.26 +/**
1.27 +@defgroup heaps Heap Structures
1.28 +@ingroup datas
1.29 +\brief %Heap structures implemented in LEMON.
1.30 +
1.31 +This group contains the heap structures implemented in LEMON.
1.32 +
1.33 +LEMON provides several heap classes. They are efficient implementations
1.34 +of the abstract data type \e priority \e queue. They store items with
1.35 +specified values called \e priorities in such a way that finding and
1.36 +removing the item with minimum priority are efficient.
1.37 +The basic operations are adding and erasing items, changing the priority
1.38 +of an item, etc.
1.39 +
1.40 +Heaps are crucial in several algorithms, such as Dijkstra and Prim.
1.41 +The heap implementations have the same interface, thus any of them can be
1.42 +used easily in such algorithms.
1.43 +
1.44 +\sa \ref concepts::Heap "Heap concept"
1.45 +*/
1.46 +
1.47 +/**
1.48 +@defgroup matrices Matrices
1.49 +@ingroup datas
1.50 +\brief Two dimensional data storages implemented in LEMON.
1.51 +
1.52 +This group contains two dimensional data storages implemented in LEMON.
1.53 */
1.54
1.55 /**
1.56 @@ -259,6 +280,28 @@
1.57 */
1.58
1.59 /**
1.60 +@defgroup geomdat Geometric Data Structures
1.61 +@ingroup auxdat
1.62 +\brief Geometric data structures implemented in LEMON.
1.63 +
1.64 +This group contains geometric data structures implemented in LEMON.
1.65 +
1.66 + - \ref lemon::dim2::Point "dim2::Point" implements a two dimensional
1.67 + vector with the usual operations.
1.68 + - \ref lemon::dim2::Box "dim2::Box" can be used to determine the
1.69 + rectangular bounding box of a set of \ref lemon::dim2::Point
1.70 + "dim2::Point"'s.
1.71 +*/
1.72 +
1.73 +/**
1.74 +@defgroup matrices Matrices
1.75 +@ingroup auxdat
1.76 +\brief Two dimensional data storages implemented in LEMON.
1.77 +
1.78 +This group contains two dimensional data storages implemented in LEMON.
1.79 +*/
1.80 +
1.81 +/**
1.82 @defgroup algs Algorithms
1.83 \brief This group contains the several algorithms
1.84 implemented in LEMON.
1.85 @@ -298,6 +341,15 @@
1.86 */
1.87
1.88 /**
1.89 +@defgroup spantree Minimum Spanning Tree Algorithms
1.90 +@ingroup algs
1.91 +\brief Algorithms for finding minimum cost spanning trees and arborescences.
1.92 +
1.93 +This group contains the algorithms for finding minimum cost spanning
1.94 +trees and arborescences.
1.95 +*/
1.96 +
1.97 +/**
1.98 @defgroup max_flow Maximum Flow Algorithms
1.99 @ingroup algs
1.100 \brief Algorithms for finding maximum flows.
1.101 @@ -375,7 +427,7 @@
1.102 cut is the \f$X\f$ solution of the next optimization problem:
1.103
1.104 \f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
1.105 - \sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f]
1.106 + \sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
1.107
1.108 LEMON contains several algorithms related to minimum cut problems:
1.109
1.110 @@ -391,30 +443,6 @@
1.111 */
1.112
1.113 /**
1.114 -@defgroup graph_properties Connectivity and Other Graph Properties
1.115 -@ingroup algs
1.116 -\brief Algorithms for discovering the graph properties
1.117 -
1.118 -This group contains the algorithms for discovering the graph properties
1.119 -like connectivity, bipartiteness, euler property, simplicity etc.
1.120 -
1.121 -\image html edge_biconnected_components.png
1.122 -\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
1.123 -*/
1.124 -
1.125 -/**
1.126 -@defgroup planar Planarity Embedding and Drawing
1.127 -@ingroup algs
1.128 -\brief Algorithms for planarity checking, embedding and drawing
1.129 -
1.130 -This group contains the algorithms for planarity checking,
1.131 -embedding and drawing.
1.132 -
1.133 -\image html planar.png
1.134 -\image latex planar.eps "Plane graph" width=\textwidth
1.135 -*/
1.136 -
1.137 -/**
1.138 @defgroup matching Matching Algorithms
1.139 @ingroup algs
1.140 \brief Algorithms for finding matchings in graphs and bipartite graphs.
1.141 @@ -455,12 +483,36 @@
1.142 */
1.143
1.144 /**
1.145 -@defgroup spantree Minimum Spanning Tree Algorithms
1.146 +@defgroup graph_properties Connectivity and Other Graph Properties
1.147 @ingroup algs
1.148 -\brief Algorithms for finding minimum cost spanning trees and arborescences.
1.149 +\brief Algorithms for discovering the graph properties
1.150
1.151 -This group contains the algorithms for finding minimum cost spanning
1.152 -trees and arborescences.
1.153 +This group contains the algorithms for discovering the graph properties
1.154 +like connectivity, bipartiteness, euler property, simplicity etc.
1.155 +
1.156 +\image html connected_components.png
1.157 +\image latex connected_components.eps "Connected components" width=\textwidth
1.158 +*/
1.159 +
1.160 +/**
1.161 +@defgroup planar Planarity Embedding and Drawing
1.162 +@ingroup algs
1.163 +\brief Algorithms for planarity checking, embedding and drawing
1.164 +
1.165 +This group contains the algorithms for planarity checking,
1.166 +embedding and drawing.
1.167 +
1.168 +\image html planar.png
1.169 +\image latex planar.eps "Plane graph" width=\textwidth
1.170 +*/
1.171 +
1.172 +/**
1.173 +@defgroup approx Approximation Algorithms
1.174 +@ingroup algs
1.175 +\brief Approximation algorithms.
1.176 +
1.177 +This group contains the approximation and heuristic algorithms
1.178 +implemented in LEMON.
1.179 */
1.180
1.181 /**
1.182 @@ -473,15 +525,6 @@
1.183 */
1.184
1.185 /**
1.186 -@defgroup approx Approximation Algorithms
1.187 -@ingroup algs
1.188 -\brief Approximation algorithms.
1.189 -
1.190 -This group contains the approximation and heuristic algorithms
1.191 -implemented in LEMON.
1.192 -*/
1.193 -
1.194 -/**
1.195 @defgroup gen_opt_group General Optimization Tools
1.196 \brief This group contains some general optimization frameworks
1.197 implemented in LEMON.
1.198 @@ -587,7 +630,7 @@
1.199 */
1.200
1.201 /**
1.202 -@defgroup dimacs_group DIMACS format
1.203 +@defgroup dimacs_group DIMACS Format
1.204 @ingroup io_group
1.205 \brief Read and write files in DIMACS format
1.206
1.207 @@ -649,6 +692,15 @@
1.208 */
1.209
1.210 /**
1.211 +@defgroup tools Standalone Utility Applications
1.212 +
1.213 +Some utility applications are listed here.
1.214 +
1.215 +The standard compilation procedure (<tt>./configure;make</tt>) will compile
1.216 +them, as well.
1.217 +*/
1.218 +
1.219 +/**
1.220 \anchor demoprograms
1.221
1.222 @defgroup demos Demo Programs
1.223 @@ -660,13 +712,4 @@
1.224 <tt>make check</tt> commands.
1.225 */
1.226
1.227 -/**
1.228 -@defgroup tools Standalone Utility Applications
1.229 -
1.230 -Some utility applications are listed here.
1.231 -
1.232 -The standard compilation procedure (<tt>./configure;make</tt>) will compile
1.233 -them, as well.
1.234 -*/
1.235 -
1.236 }
2.1 --- a/lemon/Makefile.am Wed Sep 30 08:36:43 2009 +0200
2.2 +++ b/lemon/Makefile.am Wed Sep 30 08:41:06 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 @@ -98,6 +102,7 @@
2.30 lemon/min_cost_arborescence.h \
2.31 lemon/nauty_reader.h \
2.32 lemon/network_simplex.h \
2.33 + lemon/pairing_heap.h \
2.34 lemon/path.h \
2.35 lemon/preflow.h \
2.36 lemon/radix_heap.h \
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/lemon/bellman_ford.h Wed Sep 30 08:41:06 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/bfs.h Wed Sep 30 08:36:43 2009 +0200
4.2 +++ b/lemon/bfs.h Wed Sep 30 08:41:06 2009 +0200
4.3 @@ -47,7 +47,7 @@
4.4 ///
4.5 ///The type of the map that stores the predecessor
4.6 ///arcs of the shortest paths.
4.7 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.8 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.9 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
4.10 ///Instantiates a \c PredMap.
4.11
4.12 @@ -62,7 +62,8 @@
4.13 ///The type of the map that indicates which nodes are processed.
4.14
4.15 ///The type of the map that indicates which nodes are processed.
4.16 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.17 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.18 + ///By default it is a NullMap.
4.19 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
4.20 ///Instantiates a \c ProcessedMap.
4.21
4.22 @@ -81,7 +82,7 @@
4.23 ///The type of the map that indicates which nodes are reached.
4.24
4.25 ///The type of the map that indicates which nodes are reached.
4.26 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.27 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.28 typedef typename Digraph::template NodeMap<bool> ReachedMap;
4.29 ///Instantiates a \c ReachedMap.
4.30
4.31 @@ -96,7 +97,7 @@
4.32 ///The type of the map that stores the distances of the nodes.
4.33
4.34 ///The type of the map that stores the distances of the nodes.
4.35 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.36 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.37 typedef typename Digraph::template NodeMap<int> DistMap;
4.38 ///Instantiates a \c DistMap.
4.39
4.40 @@ -225,7 +226,7 @@
4.41 ///
4.42 ///\ref named-templ-param "Named parameter" for setting
4.43 ///\c PredMap type.
4.44 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.45 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.46 template <class T>
4.47 struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
4.48 typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
4.49 @@ -245,7 +246,7 @@
4.50 ///
4.51 ///\ref named-templ-param "Named parameter" for setting
4.52 ///\c DistMap type.
4.53 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.54 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.55 template <class T>
4.56 struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
4.57 typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
4.58 @@ -265,7 +266,7 @@
4.59 ///
4.60 ///\ref named-templ-param "Named parameter" for setting
4.61 ///\c ReachedMap type.
4.62 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.63 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.64 template <class T>
4.65 struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
4.66 typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
4.67 @@ -285,7 +286,7 @@
4.68 ///
4.69 ///\ref named-templ-param "Named parameter" for setting
4.70 ///\c ProcessedMap type.
4.71 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.72 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.73 template <class T>
4.74 struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
4.75 typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
4.76 @@ -413,8 +414,8 @@
4.77 ///\name Execution Control
4.78 ///The simplest way to execute the BFS algorithm is to use one of the
4.79 ///member functions called \ref run(Node) "run()".\n
4.80 - ///If you need more control on the execution, first you have to call
4.81 - ///\ref init(), then you can add several source nodes with
4.82 + ///If you need better control on the execution, you have to call
4.83 + ///\ref init() first, then you can add several source nodes with
4.84 ///\ref addSource(). Finally the actual path computation can be
4.85 ///performed with one of the \ref start() functions.
4.86
4.87 @@ -737,9 +738,9 @@
4.88
4.89 ///@{
4.90
4.91 - ///The shortest path to a node.
4.92 + ///The shortest path to the given node.
4.93
4.94 - ///Returns the shortest path to a node.
4.95 + ///Returns the shortest path to the given node from the root(s).
4.96 ///
4.97 ///\warning \c t should be reached from the root(s).
4.98 ///
4.99 @@ -747,9 +748,9 @@
4.100 ///must be called before using this function.
4.101 Path path(Node t) const { return Path(*G, *_pred, t); }
4.102
4.103 - ///The distance of a node from the root(s).
4.104 + ///The distance of the given node from the root(s).
4.105
4.106 - ///Returns the distance of a node from the root(s).
4.107 + ///Returns the distance of the given node from the root(s).
4.108 ///
4.109 ///\warning If node \c v is not reached from the root(s), then
4.110 ///the return value of this function is undefined.
4.111 @@ -758,29 +759,31 @@
4.112 ///must be called before using this function.
4.113 int dist(Node v) const { return (*_dist)[v]; }
4.114
4.115 - ///Returns the 'previous arc' of the shortest path tree for a node.
4.116 -
4.117 + ///\brief Returns the 'previous arc' of the shortest path tree for
4.118 + ///the given node.
4.119 + ///
4.120 ///This function returns the 'previous arc' of the shortest path
4.121 ///tree for the node \c v, i.e. it returns the last arc of a
4.122 ///shortest path from a root to \c v. It is \c INVALID if \c v
4.123 ///is not reached from the root(s) or if \c v is a root.
4.124 ///
4.125 ///The shortest path tree used here is equal to the shortest path
4.126 - ///tree used in \ref predNode().
4.127 + ///tree used in \ref predNode() and \ref predMap().
4.128 ///
4.129 ///\pre Either \ref run(Node) "run()" or \ref init()
4.130 ///must be called before using this function.
4.131 Arc predArc(Node v) const { return (*_pred)[v];}
4.132
4.133 - ///Returns the 'previous node' of the shortest path tree for a node.
4.134 -
4.135 + ///\brief Returns the 'previous node' of the shortest path tree for
4.136 + ///the given node.
4.137 + ///
4.138 ///This function returns the 'previous node' of the shortest path
4.139 ///tree for the node \c v, i.e. it returns the last but one node
4.140 - ///from a shortest path from a root to \c v. It is \c INVALID
4.141 + ///of a shortest path from a root to \c v. It is \c INVALID
4.142 ///if \c v is not reached from the root(s) or if \c v is a root.
4.143 ///
4.144 ///The shortest path tree used here is equal to the shortest path
4.145 - ///tree used in \ref predArc().
4.146 + ///tree used in \ref predArc() and \ref predMap().
4.147 ///
4.148 ///\pre Either \ref run(Node) "run()" or \ref init()
4.149 ///must be called before using this function.
4.150 @@ -801,13 +804,13 @@
4.151 ///predecessor arcs.
4.152 ///
4.153 ///Returns a const reference to the node map that stores the predecessor
4.154 - ///arcs, which form the shortest path tree.
4.155 + ///arcs, which form the shortest path tree (forest).
4.156 ///
4.157 ///\pre Either \ref run(Node) "run()" or \ref init()
4.158 ///must be called before using this function.
4.159 const PredMap &predMap() const { return *_pred;}
4.160
4.161 - ///Checks if a node is reached from the root(s).
4.162 + ///Checks if the given node is reached from the root(s).
4.163
4.164 ///Returns \c true if \c v is reached from the root(s).
4.165 ///
4.166 @@ -833,7 +836,7 @@
4.167 ///
4.168 ///The type of the map that stores the predecessor
4.169 ///arcs of the shortest paths.
4.170 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.171 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.172 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
4.173 ///Instantiates a PredMap.
4.174
4.175 @@ -848,7 +851,7 @@
4.176 ///The type of the map that indicates which nodes are processed.
4.177
4.178 ///The type of the map that indicates which nodes are processed.
4.179 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.180 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.181 ///By default it is a NullMap.
4.182 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
4.183 ///Instantiates a ProcessedMap.
4.184 @@ -868,7 +871,7 @@
4.185 ///The type of the map that indicates which nodes are reached.
4.186
4.187 ///The type of the map that indicates which nodes are reached.
4.188 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.189 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.190 typedef typename Digraph::template NodeMap<bool> ReachedMap;
4.191 ///Instantiates a ReachedMap.
4.192
4.193 @@ -883,7 +886,7 @@
4.194 ///The type of the map that stores the distances of the nodes.
4.195
4.196 ///The type of the map that stores the distances of the nodes.
4.197 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
4.198 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
4.199 typedef typename Digraph::template NodeMap<int> DistMap;
4.200 ///Instantiates a DistMap.
4.201
4.202 @@ -898,18 +901,14 @@
4.203 ///The type of the shortest paths.
4.204
4.205 ///The type of the shortest paths.
4.206 - ///It must meet the \ref concepts::Path "Path" concept.
4.207 + ///It must conform to the \ref concepts::Path "Path" concept.
4.208 typedef lemon::Path<Digraph> Path;
4.209 };
4.210
4.211 /// Default traits class used by BfsWizard
4.212
4.213 - /// To make it easier to use Bfs algorithm
4.214 - /// we have created a wizard class.
4.215 - /// This \ref BfsWizard class needs default traits,
4.216 - /// as well as the \ref Bfs class.
4.217 - /// The \ref BfsWizardBase is a class to be the default traits of the
4.218 - /// \ref BfsWizard class.
4.219 + /// Default traits class used by BfsWizard.
4.220 + /// \tparam GR The type of the digraph.
4.221 template<class GR>
4.222 class BfsWizardBase : public BfsWizardDefaultTraits<GR>
4.223 {
4.224 @@ -937,7 +936,7 @@
4.225 public:
4.226 /// Constructor.
4.227
4.228 - /// This constructor does not require parameters, therefore it initiates
4.229 + /// This constructor does not require parameters, it initiates
4.230 /// all of the attributes to \c 0.
4.231 BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
4.232 _dist(0), _path(0), _di(0) {}
4.233 @@ -967,7 +966,6 @@
4.234 {
4.235 typedef TR Base;
4.236
4.237 - ///The type of the digraph the algorithm runs on.
4.238 typedef typename TR::Digraph Digraph;
4.239
4.240 typedef typename Digraph::Node Node;
4.241 @@ -975,16 +973,10 @@
4.242 typedef typename Digraph::Arc Arc;
4.243 typedef typename Digraph::OutArcIt OutArcIt;
4.244
4.245 - ///\brief The type of the map that stores the predecessor
4.246 - ///arcs of the shortest paths.
4.247 typedef typename TR::PredMap PredMap;
4.248 - ///\brief The type of the map that stores the distances of the nodes.
4.249 typedef typename TR::DistMap DistMap;
4.250 - ///\brief The type of the map that indicates which nodes are reached.
4.251 typedef typename TR::ReachedMap ReachedMap;
4.252 - ///\brief The type of the map that indicates which nodes are processed.
4.253 typedef typename TR::ProcessedMap ProcessedMap;
4.254 - ///The type of the shortest paths
4.255 typedef typename TR::Path Path;
4.256
4.257 public:
4.258 @@ -1067,11 +1059,12 @@
4.259 static PredMap *createPredMap(const Digraph &) { return 0; };
4.260 SetPredMapBase(const TR &b) : TR(b) {}
4.261 };
4.262 - ///\brief \ref named-func-param "Named parameter"
4.263 - ///for setting PredMap object.
4.264 +
4.265 + ///\brief \ref named-templ-param "Named parameter" for setting
4.266 + ///the predecessor map.
4.267 ///
4.268 - ///\ref named-func-param "Named parameter"
4.269 - ///for setting PredMap object.
4.270 + ///\ref named-templ-param "Named parameter" function for setting
4.271 + ///the map that stores the predecessor arcs of the nodes.
4.272 template<class T>
4.273 BfsWizard<SetPredMapBase<T> > predMap(const T &t)
4.274 {
4.275 @@ -1085,11 +1078,12 @@
4.276 static ReachedMap *createReachedMap(const Digraph &) { return 0; };
4.277 SetReachedMapBase(const TR &b) : TR(b) {}
4.278 };
4.279 - ///\brief \ref named-func-param "Named parameter"
4.280 - ///for setting ReachedMap object.
4.281 +
4.282 + ///\brief \ref named-templ-param "Named parameter" for setting
4.283 + ///the reached map.
4.284 ///
4.285 - /// \ref named-func-param "Named parameter"
4.286 - ///for setting ReachedMap object.
4.287 + ///\ref named-templ-param "Named parameter" function for setting
4.288 + ///the map that indicates which nodes are reached.
4.289 template<class T>
4.290 BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
4.291 {
4.292 @@ -1103,11 +1097,13 @@
4.293 static DistMap *createDistMap(const Digraph &) { return 0; };
4.294 SetDistMapBase(const TR &b) : TR(b) {}
4.295 };
4.296 - ///\brief \ref named-func-param "Named parameter"
4.297 - ///for setting DistMap object.
4.298 +
4.299 + ///\brief \ref named-templ-param "Named parameter" for setting
4.300 + ///the distance map.
4.301 ///
4.302 - /// \ref named-func-param "Named parameter"
4.303 - ///for setting DistMap object.
4.304 + ///\ref named-templ-param "Named parameter" function for setting
4.305 + ///the map that stores the distances of the nodes calculated
4.306 + ///by the algorithm.
4.307 template<class T>
4.308 BfsWizard<SetDistMapBase<T> > distMap(const T &t)
4.309 {
4.310 @@ -1121,11 +1117,12 @@
4.311 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
4.312 SetProcessedMapBase(const TR &b) : TR(b) {}
4.313 };
4.314 - ///\brief \ref named-func-param "Named parameter"
4.315 - ///for setting ProcessedMap object.
4.316 +
4.317 + ///\brief \ref named-func-param "Named parameter" for setting
4.318 + ///the processed map.
4.319 ///
4.320 - /// \ref named-func-param "Named parameter"
4.321 - ///for setting ProcessedMap object.
4.322 + ///\ref named-templ-param "Named parameter" function for setting
4.323 + ///the map that indicates which nodes are processed.
4.324 template<class T>
4.325 BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
4.326 {
4.327 @@ -1264,7 +1261,7 @@
4.328 /// \brief The type of the map that indicates which nodes are reached.
4.329 ///
4.330 /// The type of the map that indicates which nodes are reached.
4.331 - /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.332 + /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
4.333 typedef typename Digraph::template NodeMap<bool> ReachedMap;
4.334
4.335 /// \brief Instantiates a ReachedMap.
4.336 @@ -1425,8 +1422,8 @@
4.337 /// \name Execution Control
4.338 /// The simplest way to execute the BFS algorithm is to use one of the
4.339 /// member functions called \ref run(Node) "run()".\n
4.340 - /// If you need more control on the execution, first you have to call
4.341 - /// \ref init(), then you can add several source nodes with
4.342 + /// If you need better control on the execution, you have to call
4.343 + /// \ref init() first, then you can add several source nodes with
4.344 /// \ref addSource(). Finally the actual path computation can be
4.345 /// performed with one of the \ref start() functions.
4.346
4.347 @@ -1735,7 +1732,7 @@
4.348
4.349 ///@{
4.350
4.351 - /// \brief Checks if a node is reached from the root(s).
4.352 + /// \brief Checks if the given node is reached from the root(s).
4.353 ///
4.354 /// Returns \c true if \c v is reached from the root(s).
4.355 ///
5.1 --- a/lemon/bin_heap.h Wed Sep 30 08:36:43 2009 +0200
5.2 +++ b/lemon/bin_heap.h Wed Sep 30 08:41:06 2009 +0200
5.3 @@ -19,9 +19,9 @@
5.4 #ifndef LEMON_BIN_HEAP_H
5.5 #define LEMON_BIN_HEAP_H
5.6
5.7 -///\ingroup auxdat
5.8 +///\ingroup heaps
5.9 ///\file
5.10 -///\brief Binary Heap implementation.
5.11 +///\brief Binary heap implementation.
5.12
5.13 #include <vector>
5.14 #include <utility>
5.15 @@ -29,45 +29,41 @@
5.16
5.17 namespace lemon {
5.18
5.19 - ///\ingroup auxdat
5.20 + /// \ingroup heaps
5.21 ///
5.22 - ///\brief A Binary Heap implementation.
5.23 + /// \brief Binary heap data structure.
5.24 ///
5.25 - ///This class implements the \e binary \e heap data structure.
5.26 + /// This class implements the \e binary \e heap data structure.
5.27 + /// It fully conforms to the \ref concepts::Heap "heap concept".
5.28 ///
5.29 - ///A \e heap is a data structure for storing items with specified values
5.30 - ///called \e priorities in such a way that finding the item with minimum
5.31 - ///priority is efficient. \c CMP specifies the ordering of the priorities.
5.32 - ///In a heap one can change the priority of an item, add or erase an
5.33 - ///item, etc.
5.34 - ///
5.35 - ///\tparam PR Type of the priority of the items.
5.36 - ///\tparam IM A read and writable item map with int values, used internally
5.37 - ///to handle the cross references.
5.38 - ///\tparam CMP A functor class for the ordering of the priorities.
5.39 - ///The default is \c std::less<PR>.
5.40 - ///
5.41 - ///\sa FibHeap
5.42 - ///\sa Dijkstra
5.43 + /// \tparam PR Type of the priorities of the items.
5.44 + /// \tparam IM A read-writable item map with \c int values, used
5.45 + /// internally to handle the cross references.
5.46 + /// \tparam CMP A functor class for comparing the priorities.
5.47 + /// The default is \c std::less<PR>.
5.48 +#ifdef DOXYGEN
5.49 + template <typename PR, typename IM, typename CMP>
5.50 +#else
5.51 template <typename PR, typename IM, typename CMP = std::less<PR> >
5.52 +#endif
5.53 class BinHeap {
5.54 + public:
5.55
5.56 - public:
5.57 - ///\e
5.58 + /// Type of the item-int map.
5.59 typedef IM ItemIntMap;
5.60 - ///\e
5.61 + /// Type of the priorities.
5.62 typedef PR Prio;
5.63 - ///\e
5.64 + /// Type of the items stored in the heap.
5.65 typedef typename ItemIntMap::Key Item;
5.66 - ///\e
5.67 + /// Type of the item-priority pairs.
5.68 typedef std::pair<Item,Prio> Pair;
5.69 - ///\e
5.70 + /// Functor type for comparing the priorities.
5.71 typedef CMP Compare;
5.72
5.73 - /// \brief Type to represent the items states.
5.74 + /// \brief Type to represent the states of the items.
5.75 ///
5.76 - /// Each Item element have a state associated to it. It may be "in heap",
5.77 - /// "pre heap" or "post heap". The latter two are indifferent from the
5.78 + /// Each item has a state associated to it. It can be "in heap",
5.79 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
5.80 /// heap's point of view, but may be useful to the user.
5.81 ///
5.82 /// The item-int map must be initialized in such way that it assigns
5.83 @@ -84,42 +80,43 @@
5.84 ItemIntMap &_iim;
5.85
5.86 public:
5.87 - /// \brief The constructor.
5.88 +
5.89 + /// \brief Constructor.
5.90 ///
5.91 - /// The constructor.
5.92 - /// \param map should be given to the constructor, since it is used
5.93 - /// internally to handle the cross references. The value of the map
5.94 - /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
5.95 + /// Constructor.
5.96 + /// \param map A map that assigns \c int values to the items.
5.97 + /// It is used internally to handle the cross references.
5.98 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
5.99 explicit BinHeap(ItemIntMap &map) : _iim(map) {}
5.100
5.101 - /// \brief The constructor.
5.102 + /// \brief Constructor.
5.103 ///
5.104 - /// The constructor.
5.105 - /// \param map should be given to the constructor, since it is used
5.106 - /// internally to handle the cross references. The value of the map
5.107 - /// should be PRE_HEAP (-1) for each element.
5.108 - ///
5.109 - /// \param comp The comparator function object.
5.110 + /// Constructor.
5.111 + /// \param map A map that assigns \c int values to the items.
5.112 + /// It is used internally to handle the cross references.
5.113 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
5.114 + /// \param comp The function object used for comparing the priorities.
5.115 BinHeap(ItemIntMap &map, const Compare &comp)
5.116 : _iim(map), _comp(comp) {}
5.117
5.118
5.119 - /// The number of items stored in the heap.
5.120 + /// \brief The number of items stored in the heap.
5.121 ///
5.122 - /// \brief Returns the number of items stored in the heap.
5.123 + /// This function returns the number of items stored in the heap.
5.124 int size() const { return _data.size(); }
5.125
5.126 - /// \brief Checks if the heap stores no items.
5.127 + /// \brief Check if the heap is empty.
5.128 ///
5.129 - /// Returns \c true if and only if the heap stores no items.
5.130 + /// This function returns \c true if the heap is empty.
5.131 bool empty() const { return _data.empty(); }
5.132
5.133 - /// \brief Make empty this heap.
5.134 + /// \brief Make the heap empty.
5.135 ///
5.136 - /// Make empty this heap. It does not change the cross reference map.
5.137 - /// If you want to reuse what is not surely empty you should first clear
5.138 - /// the heap and after that you should set the cross reference map for
5.139 - /// each item to \c PRE_HEAP.
5.140 + /// This functon makes the heap empty.
5.141 + /// It does not change the cross reference map. If you want to reuse
5.142 + /// a heap that is not surely empty, you should first clear it and
5.143 + /// then you should set the cross reference map to \c PRE_HEAP
5.144 + /// for each item.
5.145 void clear() {
5.146 _data.clear();
5.147 }
5.148 @@ -127,12 +124,12 @@
5.149 private:
5.150 static int parent(int i) { return (i-1)/2; }
5.151
5.152 - static int second_child(int i) { return 2*i+2; }
5.153 + static int secondChild(int i) { return 2*i+2; }
5.154 bool less(const Pair &p1, const Pair &p2) const {
5.155 return _comp(p1.second, p2.second);
5.156 }
5.157
5.158 - int bubble_up(int hole, Pair p) {
5.159 + int bubbleUp(int hole, Pair p) {
5.160 int par = parent(hole);
5.161 while( hole>0 && less(p,_data[par]) ) {
5.162 move(_data[par],hole);
5.163 @@ -143,8 +140,8 @@
5.164 return hole;
5.165 }
5.166
5.167 - int bubble_down(int hole, Pair p, int length) {
5.168 - int child = second_child(hole);
5.169 + int bubbleDown(int hole, Pair p, int length) {
5.170 + int child = secondChild(hole);
5.171 while(child < length) {
5.172 if( less(_data[child-1], _data[child]) ) {
5.173 --child;
5.174 @@ -153,7 +150,7 @@
5.175 goto ok;
5.176 move(_data[child], hole);
5.177 hole = child;
5.178 - child = second_child(hole);
5.179 + child = secondChild(hole);
5.180 }
5.181 child--;
5.182 if( child<length && less(_data[child], p) ) {
5.183 @@ -171,87 +168,91 @@
5.184 }
5.185
5.186 public:
5.187 +
5.188 /// \brief Insert a pair of item and priority into the heap.
5.189 ///
5.190 - /// Adds \c p.first to the heap with priority \c p.second.
5.191 + /// This function inserts \c p.first to the heap with priority
5.192 + /// \c p.second.
5.193 /// \param p The pair to insert.
5.194 + /// \pre \c p.first must not be stored in the heap.
5.195 void push(const Pair &p) {
5.196 int n = _data.size();
5.197 _data.resize(n+1);
5.198 - bubble_up(n, p);
5.199 + bubbleUp(n, p);
5.200 }
5.201
5.202 - /// \brief Insert an item into the heap with the given heap.
5.203 + /// \brief Insert an item into the heap with the given priority.
5.204 ///
5.205 - /// Adds \c i to the heap with priority \c p.
5.206 + /// This function inserts the given item into the heap with the
5.207 + /// given priority.
5.208 /// \param i The item to insert.
5.209 /// \param p The priority of the item.
5.210 + /// \pre \e i must not be stored in the heap.
5.211 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
5.212
5.213 - /// \brief Returns the item with minimum priority relative to \c Compare.
5.214 + /// \brief Return the item having minimum priority.
5.215 ///
5.216 - /// This method returns the item with minimum priority relative to \c
5.217 - /// Compare.
5.218 - /// \pre The heap must be nonempty.
5.219 + /// This function returns the item having minimum priority.
5.220 + /// \pre The heap must be non-empty.
5.221 Item top() const {
5.222 return _data[0].first;
5.223 }
5.224
5.225 - /// \brief Returns the minimum priority relative to \c Compare.
5.226 + /// \brief The minimum priority.
5.227 ///
5.228 - /// It returns the minimum priority relative to \c Compare.
5.229 - /// \pre The heap must be nonempty.
5.230 + /// This function returns the minimum priority.
5.231 + /// \pre The heap must be non-empty.
5.232 Prio prio() const {
5.233 return _data[0].second;
5.234 }
5.235
5.236 - /// \brief Deletes the item with minimum priority relative to \c Compare.
5.237 + /// \brief Remove the item having minimum priority.
5.238 ///
5.239 - /// This method deletes the item with minimum priority relative to \c
5.240 - /// Compare from the heap.
5.241 + /// This function removes the item having minimum priority.
5.242 /// \pre The heap must be non-empty.
5.243 void pop() {
5.244 int n = _data.size()-1;
5.245 _iim.set(_data[0].first, POST_HEAP);
5.246 if (n > 0) {
5.247 - bubble_down(0, _data[n], n);
5.248 + bubbleDown(0, _data[n], n);
5.249 }
5.250 _data.pop_back();
5.251 }
5.252
5.253 - /// \brief Deletes \c i from the heap.
5.254 + /// \brief Remove the given item from the heap.
5.255 ///
5.256 - /// This method deletes item \c i from the heap.
5.257 - /// \param i The item to erase.
5.258 - /// \pre The item should be in the heap.
5.259 + /// This function removes the given item from the heap if it is
5.260 + /// already stored.
5.261 + /// \param i The item to delete.
5.262 + /// \pre \e i must be in the heap.
5.263 void erase(const Item &i) {
5.264 int h = _iim[i];
5.265 int n = _data.size()-1;
5.266 _iim.set(_data[h].first, POST_HEAP);
5.267 if( h < n ) {
5.268 - if ( bubble_up(h, _data[n]) == h) {
5.269 - bubble_down(h, _data[n], n);
5.270 + if ( bubbleUp(h, _data[n]) == h) {
5.271 + bubbleDown(h, _data[n], n);
5.272 }
5.273 }
5.274 _data.pop_back();
5.275 }
5.276
5.277 -
5.278 - /// \brief Returns the priority of \c i.
5.279 + /// \brief The priority of the given item.
5.280 ///
5.281 - /// This function returns the priority of item \c i.
5.282 + /// This function returns the priority of the given item.
5.283 /// \param i The item.
5.284 - /// \pre \c i must be in the heap.
5.285 + /// \pre \e i must be in the heap.
5.286 Prio operator[](const Item &i) const {
5.287 int idx = _iim[i];
5.288 return _data[idx].second;
5.289 }
5.290
5.291 - /// \brief \c i gets to the heap with priority \c p independently
5.292 - /// if \c i was already there.
5.293 + /// \brief Set the priority of an item or insert it, if it is
5.294 + /// not stored in the heap.
5.295 ///
5.296 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
5.297 - /// in the heap and sets the priority of \c i to \c p otherwise.
5.298 + /// This method sets the priority of the given item if it is
5.299 + /// already stored in the heap. Otherwise it inserts the given
5.300 + /// item into the heap with the given priority.
5.301 /// \param i The item.
5.302 /// \param p The priority.
5.303 void set(const Item &i, const Prio &p) {
5.304 @@ -260,44 +261,42 @@
5.305 push(i,p);
5.306 }
5.307 else if( _comp(p, _data[idx].second) ) {
5.308 - bubble_up(idx, Pair(i,p));
5.309 + bubbleUp(idx, Pair(i,p));
5.310 }
5.311 else {
5.312 - bubble_down(idx, Pair(i,p), _data.size());
5.313 + bubbleDown(idx, Pair(i,p), _data.size());
5.314 }
5.315 }
5.316
5.317 - /// \brief Decreases the priority of \c i to \c p.
5.318 + /// \brief Decrease the priority of an item to the given value.
5.319 ///
5.320 - /// This method decreases the priority of item \c i to \c p.
5.321 + /// This function decreases the priority of an item to the given value.
5.322 /// \param i The item.
5.323 /// \param p The priority.
5.324 - /// \pre \c i must be stored in the heap with priority at least \c
5.325 - /// p relative to \c Compare.
5.326 + /// \pre \e i must be stored in the heap with priority at least \e p.
5.327 void decrease(const Item &i, const Prio &p) {
5.328 int idx = _iim[i];
5.329 - bubble_up(idx, Pair(i,p));
5.330 + bubbleUp(idx, Pair(i,p));
5.331 }
5.332
5.333 - /// \brief Increases the priority of \c i to \c p.
5.334 + /// \brief Increase the priority of an item to the given value.
5.335 ///
5.336 - /// This method sets the priority of item \c i to \c p.
5.337 + /// This function increases the priority of an item to the given value.
5.338 /// \param i The item.
5.339 /// \param p The priority.
5.340 - /// \pre \c i must be stored in the heap with priority at most \c
5.341 - /// p relative to \c Compare.
5.342 + /// \pre \e i must be stored in the heap with priority at most \e p.
5.343 void increase(const Item &i, const Prio &p) {
5.344 int idx = _iim[i];
5.345 - bubble_down(idx, Pair(i,p), _data.size());
5.346 + bubbleDown(idx, Pair(i,p), _data.size());
5.347 }
5.348
5.349 - /// \brief Returns if \c item is in, has already been in, or has
5.350 - /// never been in the heap.
5.351 + /// \brief Return the state of an item.
5.352 ///
5.353 - /// This method returns PRE_HEAP if \c item has never been in the
5.354 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
5.355 - /// otherwise. In the latter case it is possible that \c item will
5.356 - /// get back to the heap again.
5.357 + /// This method returns \c PRE_HEAP if the given item has never
5.358 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
5.359 + /// and \c POST_HEAP otherwise.
5.360 + /// In the latter case it is possible that the item will get back
5.361 + /// to the heap again.
5.362 /// \param i The item.
5.363 State state(const Item &i) const {
5.364 int s = _iim[i];
5.365 @@ -306,11 +305,11 @@
5.366 return State(s);
5.367 }
5.368
5.369 - /// \brief Sets the state of the \c item in the heap.
5.370 + /// \brief Set the state of an item in the heap.
5.371 ///
5.372 - /// Sets the state of the \c item in the heap. It can be used to
5.373 - /// manually clear the heap when it is important to achive the
5.374 - /// better time complexity.
5.375 + /// This function sets the state of the given item in the heap.
5.376 + /// It can be used to manually clear the heap when it is important
5.377 + /// to achive better time complexity.
5.378 /// \param i The item.
5.379 /// \param st The state. It should not be \c IN_HEAP.
5.380 void state(const Item& i, State st) {
5.381 @@ -327,12 +326,13 @@
5.382 }
5.383 }
5.384
5.385 - /// \brief Replaces an item in the heap.
5.386 + /// \brief Replace an item in the heap.
5.387 ///
5.388 - /// The \c i item is replaced with \c j item. The \c i item should
5.389 - /// be in the heap, while the \c j should be out of the heap. The
5.390 - /// \c i item will out of the heap and \c j will be in the heap
5.391 - /// with the same prioriority as prevoiusly the \c i item.
5.392 + /// This function replaces item \c i with item \c j.
5.393 + /// Item \c i must be in the heap, while \c j must be out of the heap.
5.394 + /// After calling this method, item \c i will be out of the
5.395 + /// heap and \c j will be in the heap with the same prioriority
5.396 + /// as item \c i had before.
5.397 void replace(const Item& i, const Item& j) {
5.398 int idx = _iim[i];
5.399 _iim.set(i, _iim[j]);
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/lemon/binom_heap.h Wed Sep 30 08:41:06 2009 +0200
6.3 @@ -0,0 +1,445 @@
6.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
6.5 + *
6.6 + * This file is a part of LEMON, a generic C++ optimization library.
6.7 + *
6.8 + * Copyright (C) 2003-2009
6.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
6.11 + *
6.12 + * Permission to use, modify and distribute this software is granted
6.13 + * provided that this copyright notice appears in all copies. For
6.14 + * precise terms see the accompanying LICENSE file.
6.15 + *
6.16 + * This software is provided "AS IS" with no warranty of any kind,
6.17 + * express or implied, and with no claim as to its suitability for any
6.18 + * purpose.
6.19 + *
6.20 + */
6.21 +
6.22 +#ifndef LEMON_BINOM_HEAP_H
6.23 +#define LEMON_BINOM_HEAP_H
6.24 +
6.25 +///\file
6.26 +///\ingroup heaps
6.27 +///\brief Binomial Heap implementation.
6.28 +
6.29 +#include <vector>
6.30 +#include <utility>
6.31 +#include <functional>
6.32 +#include <lemon/math.h>
6.33 +#include <lemon/counter.h>
6.34 +
6.35 +namespace lemon {
6.36 +
6.37 + /// \ingroup heaps
6.38 + ///
6.39 + ///\brief Binomial heap data structure.
6.40 + ///
6.41 + /// This class implements the \e binomial \e heap data structure.
6.42 + /// It fully conforms to the \ref concepts::Heap "heap concept".
6.43 + ///
6.44 + /// The methods \ref increase() and \ref erase() are not efficient
6.45 + /// in a binomial heap. In case of many calls of these operations,
6.46 + /// it is better to use other heap structure, e.g. \ref BinHeap
6.47 + /// "binary heap".
6.48 + ///
6.49 + /// \tparam PR Type of the priorities of the items.
6.50 + /// \tparam IM A read-writable item map with \c int values, used
6.51 + /// internally to handle the cross references.
6.52 + /// \tparam CMP A functor class for comparing the priorities.
6.53 + /// The default is \c std::less<PR>.
6.54 +#ifdef DOXYGEN
6.55 + template <typename PR, typename IM, typename CMP>
6.56 +#else
6.57 + template <typename PR, typename IM, typename CMP = std::less<PR> >
6.58 +#endif
6.59 + class BinomHeap {
6.60 + public:
6.61 + /// Type of the item-int map.
6.62 + typedef IM ItemIntMap;
6.63 + /// Type of the priorities.
6.64 + typedef PR Prio;
6.65 + /// Type of the items stored in the heap.
6.66 + typedef typename ItemIntMap::Key Item;
6.67 + /// Functor type for comparing the priorities.
6.68 + typedef CMP Compare;
6.69 +
6.70 + /// \brief Type to represent the states of the items.
6.71 + ///
6.72 + /// Each item has a state associated to it. It can be "in heap",
6.73 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
6.74 + /// heap's point of view, but may be useful to the user.
6.75 + ///
6.76 + /// The item-int map must be initialized in such way that it assigns
6.77 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
6.78 + enum State {
6.79 + IN_HEAP = 0, ///< = 0.
6.80 + PRE_HEAP = -1, ///< = -1.
6.81 + POST_HEAP = -2 ///< = -2.
6.82 + };
6.83 +
6.84 + private:
6.85 + class Store;
6.86 +
6.87 + std::vector<Store> _data;
6.88 + int _min, _head;
6.89 + ItemIntMap &_iim;
6.90 + Compare _comp;
6.91 + int _num_items;
6.92 +
6.93 + public:
6.94 + /// \brief Constructor.
6.95 + ///
6.96 + /// Constructor.
6.97 + /// \param map A map that assigns \c int values to the items.
6.98 + /// It is used internally to handle the cross references.
6.99 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
6.100 + explicit BinomHeap(ItemIntMap &map)
6.101 + : _min(0), _head(-1), _iim(map), _num_items(0) {}
6.102 +
6.103 + /// \brief Constructor.
6.104 + ///
6.105 + /// Constructor.
6.106 + /// \param map A map that assigns \c int values to the items.
6.107 + /// It is used internally to handle the cross references.
6.108 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
6.109 + /// \param comp The function object used for comparing the priorities.
6.110 + BinomHeap(ItemIntMap &map, const Compare &comp)
6.111 + : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
6.112 +
6.113 + /// \brief The number of items stored in the heap.
6.114 + ///
6.115 + /// This function returns the number of items stored in the heap.
6.116 + int size() const { return _num_items; }
6.117 +
6.118 + /// \brief Check if the heap is empty.
6.119 + ///
6.120 + /// This function returns \c true if the heap is empty.
6.121 + bool empty() const { return _num_items==0; }
6.122 +
6.123 + /// \brief Make the heap empty.
6.124 + ///
6.125 + /// This functon makes the heap empty.
6.126 + /// It does not change the cross reference map. If you want to reuse
6.127 + /// a heap that is not surely empty, you should first clear it and
6.128 + /// then you should set the cross reference map to \c PRE_HEAP
6.129 + /// for each item.
6.130 + void clear() {
6.131 + _data.clear(); _min=0; _num_items=0; _head=-1;
6.132 + }
6.133 +
6.134 + /// \brief Set the priority of an item or insert it, if it is
6.135 + /// not stored in the heap.
6.136 + ///
6.137 + /// This method sets the priority of the given item if it is
6.138 + /// already stored in the heap. Otherwise it inserts the given
6.139 + /// item into the heap with the given priority.
6.140 + /// \param item The item.
6.141 + /// \param value The priority.
6.142 + void set (const Item& item, const Prio& value) {
6.143 + int i=_iim[item];
6.144 + if ( i >= 0 && _data[i].in ) {
6.145 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
6.146 + if ( _comp(_data[i].prio, value) ) increase(item, value);
6.147 + } else push(item, value);
6.148 + }
6.149 +
6.150 + /// \brief Insert an item into the heap with the given priority.
6.151 + ///
6.152 + /// This function inserts the given item into the heap with the
6.153 + /// given priority.
6.154 + /// \param item The item to insert.
6.155 + /// \param value The priority of the item.
6.156 + /// \pre \e item must not be stored in the heap.
6.157 + void push (const Item& item, const Prio& value) {
6.158 + int i=_iim[item];
6.159 + if ( i<0 ) {
6.160 + int s=_data.size();
6.161 + _iim.set( item,s );
6.162 + Store st;
6.163 + st.name=item;
6.164 + st.prio=value;
6.165 + _data.push_back(st);
6.166 + i=s;
6.167 + }
6.168 + else {
6.169 + _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
6.170 + _data[i].degree=0;
6.171 + _data[i].in=true;
6.172 + _data[i].prio=value;
6.173 + }
6.174 +
6.175 + if( 0==_num_items ) {
6.176 + _head=i;
6.177 + _min=i;
6.178 + } else {
6.179 + merge(i);
6.180 + if( _comp(_data[i].prio, _data[_min].prio) ) _min=i;
6.181 + }
6.182 + ++_num_items;
6.183 + }
6.184 +
6.185 + /// \brief Return the item having minimum priority.
6.186 + ///
6.187 + /// This function returns the item having minimum priority.
6.188 + /// \pre The heap must be non-empty.
6.189 + Item top() const { return _data[_min].name; }
6.190 +
6.191 + /// \brief The minimum priority.
6.192 + ///
6.193 + /// This function returns the minimum priority.
6.194 + /// \pre The heap must be non-empty.
6.195 + Prio prio() const { return _data[_min].prio; }
6.196 +
6.197 + /// \brief The priority of the given item.
6.198 + ///
6.199 + /// This function returns the priority of the given item.
6.200 + /// \param item The item.
6.201 + /// \pre \e item must be in the heap.
6.202 + const Prio& operator[](const Item& item) const {
6.203 + return _data[_iim[item]].prio;
6.204 + }
6.205 +
6.206 + /// \brief Remove the item having minimum priority.
6.207 + ///
6.208 + /// This function removes the item having minimum priority.
6.209 + /// \pre The heap must be non-empty.
6.210 + void pop() {
6.211 + _data[_min].in=false;
6.212 +
6.213 + int head_child=-1;
6.214 + if ( _data[_min].child!=-1 ) {
6.215 + int child=_data[_min].child;
6.216 + int neighb;
6.217 + while( child!=-1 ) {
6.218 + neighb=_data[child].right_neighbor;
6.219 + _data[child].parent=-1;
6.220 + _data[child].right_neighbor=head_child;
6.221 + head_child=child;
6.222 + child=neighb;
6.223 + }
6.224 + }
6.225 +
6.226 + if ( _data[_head].right_neighbor==-1 ) {
6.227 + // there was only one root
6.228 + _head=head_child;
6.229 + }
6.230 + else {
6.231 + // there were more roots
6.232 + if( _head!=_min ) { unlace(_min); }
6.233 + else { _head=_data[_head].right_neighbor; }
6.234 + merge(head_child);
6.235 + }
6.236 + _min=findMin();
6.237 + --_num_items;
6.238 + }
6.239 +
6.240 + /// \brief Remove the given item from the heap.
6.241 + ///
6.242 + /// This function removes the given item from the heap if it is
6.243 + /// already stored.
6.244 + /// \param item The item to delete.
6.245 + /// \pre \e item must be in the heap.
6.246 + void erase (const Item& item) {
6.247 + int i=_iim[item];
6.248 + if ( i >= 0 && _data[i].in ) {
6.249 + decrease( item, _data[_min].prio-1 );
6.250 + pop();
6.251 + }
6.252 + }
6.253 +
6.254 + /// \brief Decrease the priority of an item to the given value.
6.255 + ///
6.256 + /// This function decreases the priority of an item to the given value.
6.257 + /// \param item The item.
6.258 + /// \param value The priority.
6.259 + /// \pre \e item must be stored in the heap with priority at least \e value.
6.260 + void decrease (Item item, const Prio& value) {
6.261 + int i=_iim[item];
6.262 + int p=_data[i].parent;
6.263 + _data[i].prio=value;
6.264 +
6.265 + while( p!=-1 && _comp(value, _data[p].prio) ) {
6.266 + _data[i].name=_data[p].name;
6.267 + _data[i].prio=_data[p].prio;
6.268 + _data[p].name=item;
6.269 + _data[p].prio=value;
6.270 + _iim[_data[i].name]=i;
6.271 + i=p;
6.272 + p=_data[p].parent;
6.273 + }
6.274 + _iim[item]=i;
6.275 + if ( _comp(value, _data[_min].prio) ) _min=i;
6.276 + }
6.277 +
6.278 + /// \brief Increase the priority of an item to the given value.
6.279 + ///
6.280 + /// This function increases the priority of an item to the given value.
6.281 + /// \param item The item.
6.282 + /// \param value The priority.
6.283 + /// \pre \e item must be stored in the heap with priority at most \e value.
6.284 + void increase (Item item, const Prio& value) {
6.285 + erase(item);
6.286 + push(item, value);
6.287 + }
6.288 +
6.289 + /// \brief Return the state of an item.
6.290 + ///
6.291 + /// This method returns \c PRE_HEAP if the given item has never
6.292 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
6.293 + /// and \c POST_HEAP otherwise.
6.294 + /// In the latter case it is possible that the item will get back
6.295 + /// to the heap again.
6.296 + /// \param item The item.
6.297 + State state(const Item &item) const {
6.298 + int i=_iim[item];
6.299 + if( i>=0 ) {
6.300 + if ( _data[i].in ) i=0;
6.301 + else i=-2;
6.302 + }
6.303 + return State(i);
6.304 + }
6.305 +
6.306 + /// \brief Set the state of an item in the heap.
6.307 + ///
6.308 + /// This function sets the state of the given item in the heap.
6.309 + /// It can be used to manually clear the heap when it is important
6.310 + /// to achive better time complexity.
6.311 + /// \param i The item.
6.312 + /// \param st The state. It should not be \c IN_HEAP.
6.313 + void state(const Item& i, State st) {
6.314 + switch (st) {
6.315 + case POST_HEAP:
6.316 + case PRE_HEAP:
6.317 + if (state(i) == IN_HEAP) {
6.318 + erase(i);
6.319 + }
6.320 + _iim[i] = st;
6.321 + break;
6.322 + case IN_HEAP:
6.323 + break;
6.324 + }
6.325 + }
6.326 +
6.327 + private:
6.328 +
6.329 + // Find the minimum of the roots
6.330 + int findMin() {
6.331 + if( _head!=-1 ) {
6.332 + int min_loc=_head, min_val=_data[_head].prio;
6.333 + for( int x=_data[_head].right_neighbor; x!=-1;
6.334 + x=_data[x].right_neighbor ) {
6.335 + if( _comp( _data[x].prio,min_val ) ) {
6.336 + min_val=_data[x].prio;
6.337 + min_loc=x;
6.338 + }
6.339 + }
6.340 + return min_loc;
6.341 + }
6.342 + else return -1;
6.343 + }
6.344 +
6.345 + // Merge the heap with another heap starting at the given position
6.346 + void merge(int a) {
6.347 + if( _head==-1 || a==-1 ) return;
6.348 + if( _data[a].right_neighbor==-1 &&
6.349 + _data[a].degree<=_data[_head].degree ) {
6.350 + _data[a].right_neighbor=_head;
6.351 + _head=a;
6.352 + } else {
6.353 + interleave(a);
6.354 + }
6.355 + if( _data[_head].right_neighbor==-1 ) return;
6.356 +
6.357 + int x=_head;
6.358 + int x_prev=-1, x_next=_data[x].right_neighbor;
6.359 + while( x_next!=-1 ) {
6.360 + if( _data[x].degree!=_data[x_next].degree ||
6.361 + ( _data[x_next].right_neighbor!=-1 &&
6.362 + _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
6.363 + x_prev=x;
6.364 + x=x_next;
6.365 + }
6.366 + else {
6.367 + if( _comp(_data[x_next].prio,_data[x].prio) ) {
6.368 + if( x_prev==-1 ) {
6.369 + _head=x_next;
6.370 + } else {
6.371 + _data[x_prev].right_neighbor=x_next;
6.372 + }
6.373 + fuse(x,x_next);
6.374 + x=x_next;
6.375 + }
6.376 + else {
6.377 + _data[x].right_neighbor=_data[x_next].right_neighbor;
6.378 + fuse(x_next,x);
6.379 + }
6.380 + }
6.381 + x_next=_data[x].right_neighbor;
6.382 + }
6.383 + }
6.384 +
6.385 + // Interleave the elements of the given list into the list of the roots
6.386 + void interleave(int a) {
6.387 + int p=_head, q=a;
6.388 + int curr=_data.size();
6.389 + _data.push_back(Store());
6.390 +
6.391 + while( p!=-1 || q!=-1 ) {
6.392 + if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
6.393 + _data[curr].right_neighbor=p;
6.394 + curr=p;
6.395 + p=_data[p].right_neighbor;
6.396 + }
6.397 + else {
6.398 + _data[curr].right_neighbor=q;
6.399 + curr=q;
6.400 + q=_data[q].right_neighbor;
6.401 + }
6.402 + }
6.403 +
6.404 + _head=_data.back().right_neighbor;
6.405 + _data.pop_back();
6.406 + }
6.407 +
6.408 + // Lace node a under node b
6.409 + void fuse(int a, int b) {
6.410 + _data[a].parent=b;
6.411 + _data[a].right_neighbor=_data[b].child;
6.412 + _data[b].child=a;
6.413 +
6.414 + ++_data[b].degree;
6.415 + }
6.416 +
6.417 + // Unlace node a (if it has siblings)
6.418 + void unlace(int a) {
6.419 + int neighb=_data[a].right_neighbor;
6.420 + int other=_head;
6.421 +
6.422 + while( _data[other].right_neighbor!=a )
6.423 + other=_data[other].right_neighbor;
6.424 + _data[other].right_neighbor=neighb;
6.425 + }
6.426 +
6.427 + private:
6.428 +
6.429 + class Store {
6.430 + friend class BinomHeap;
6.431 +
6.432 + Item name;
6.433 + int parent;
6.434 + int right_neighbor;
6.435 + int child;
6.436 + int degree;
6.437 + bool in;
6.438 + Prio prio;
6.439 +
6.440 + Store() : parent(-1), right_neighbor(-1), child(-1), degree(0),
6.441 + in(true) {}
6.442 + };
6.443 + };
6.444 +
6.445 +} //namespace lemon
6.446 +
6.447 +#endif //LEMON_BINOM_HEAP_H
6.448 +
7.1 --- a/lemon/bits/map_extender.h Wed Sep 30 08:36:43 2009 +0200
7.2 +++ b/lemon/bits/map_extender.h Wed Sep 30 08:41:06 2009 +0200
7.3 @@ -49,6 +49,8 @@
7.4 typedef typename Parent::Reference Reference;
7.5 typedef typename Parent::ConstReference ConstReference;
7.6
7.7 + typedef typename Parent::ReferenceMapTag ReferenceMapTag;
7.8 +
7.9 class MapIt;
7.10 class ConstMapIt;
7.11
7.12 @@ -191,6 +193,8 @@
7.13 typedef typename Parent::Reference Reference;
7.14 typedef typename Parent::ConstReference ConstReference;
7.15
7.16 + typedef typename Parent::ReferenceMapTag ReferenceMapTag;
7.17 +
7.18 class MapIt;
7.19 class ConstMapIt;
7.20
8.1 --- a/lemon/bucket_heap.h Wed Sep 30 08:36:43 2009 +0200
8.2 +++ b/lemon/bucket_heap.h Wed Sep 30 08:41:06 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 Wed Sep 30 08:36:43 2009 +0200
9.2 +++ b/lemon/circulation.h Wed Sep 30 08:41:06 2009 +0200
9.3 @@ -72,7 +72,11 @@
9.4 /// The type of the map that stores the flow values.
9.5 /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
9.6 /// concept.
9.7 +#ifdef DOXYGEN
9.8 + typedef GR::ArcMap<Value> FlowMap;
9.9 +#else
9.10 typedef typename Digraph::template ArcMap<Value> FlowMap;
9.11 +#endif
9.12
9.13 /// \brief Instantiates a FlowMap.
9.14 ///
9.15 @@ -87,9 +91,12 @@
9.16 ///
9.17 /// The elevator type used by the algorithm.
9.18 ///
9.19 - /// \sa Elevator
9.20 - /// \sa LinkedElevator
9.21 + /// \sa Elevator, LinkedElevator
9.22 +#ifdef DOXYGEN
9.23 + typedef lemon::Elevator<GR, GR::Node> Elevator;
9.24 +#else
9.25 typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
9.26 +#endif
9.27
9.28 /// \brief Instantiates an Elevator.
9.29 ///
9.30 @@ -450,25 +457,27 @@
9.31 return *_level;
9.32 }
9.33
9.34 - /// \brief Sets the tolerance used by algorithm.
9.35 + /// \brief Sets the tolerance used by the algorithm.
9.36 ///
9.37 - /// Sets the tolerance used by algorithm.
9.38 - Circulation& tolerance(const Tolerance& tolerance) const {
9.39 + /// Sets the tolerance object used by the algorithm.
9.40 + /// \return <tt>(*this)</tt>
9.41 + Circulation& tolerance(const Tolerance& tolerance) {
9.42 _tol = tolerance;
9.43 return *this;
9.44 }
9.45
9.46 /// \brief Returns a const reference to the tolerance.
9.47 ///
9.48 - /// Returns a const reference to the tolerance.
9.49 + /// Returns a const reference to the tolerance object used by
9.50 + /// the algorithm.
9.51 const Tolerance& tolerance() const {
9.52 - return tolerance;
9.53 + return _tol;
9.54 }
9.55
9.56 /// \name Execution Control
9.57 /// The simplest way to execute the algorithm is to call \ref run().\n
9.58 - /// If you need more control on the initial solution or the execution,
9.59 - /// first you have to call one of the \ref init() functions, then
9.60 + /// If you need better control on the initial solution or the execution,
9.61 + /// you have to call one of the \ref init() functions first, then
9.62 /// the \ref start() function.
9.63
9.64 ///@{
10.1 --- a/lemon/concepts/heap.h Wed Sep 30 08:36:43 2009 +0200
10.2 +++ b/lemon/concepts/heap.h Wed Sep 30 08:41:06 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/concepts/maps.h Wed Sep 30 08:36:43 2009 +0200
11.2 +++ b/lemon/concepts/maps.h Wed Sep 30 08:41:06 2009 +0200
11.3 @@ -182,7 +182,8 @@
11.4
11.5 template<typename _ReferenceMap>
11.6 struct Constraints {
11.7 - void constraints() {
11.8 + typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
11.9 + constraints() {
11.10 checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
11.11 ref = m[key];
11.12 m[key] = val;
12.1 --- a/lemon/dfs.h Wed Sep 30 08:36:43 2009 +0200
12.2 +++ b/lemon/dfs.h Wed Sep 30 08:41:06 2009 +0200
12.3 @@ -47,7 +47,7 @@
12.4 ///
12.5 ///The type of the map that stores the predecessor
12.6 ///arcs of the %DFS paths.
12.7 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.8 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.9 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
12.10 ///Instantiates a \c PredMap.
12.11
12.12 @@ -62,7 +62,8 @@
12.13 ///The type of the map that indicates which nodes are processed.
12.14
12.15 ///The type of the map that indicates which nodes are processed.
12.16 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.17 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.18 + ///By default it is a NullMap.
12.19 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
12.20 ///Instantiates a \c ProcessedMap.
12.21
12.22 @@ -81,7 +82,7 @@
12.23 ///The type of the map that indicates which nodes are reached.
12.24
12.25 ///The type of the map that indicates which nodes are reached.
12.26 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.27 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.28 typedef typename Digraph::template NodeMap<bool> ReachedMap;
12.29 ///Instantiates a \c ReachedMap.
12.30
12.31 @@ -96,7 +97,7 @@
12.32 ///The type of the map that stores the distances of the nodes.
12.33
12.34 ///The type of the map that stores the distances of the nodes.
12.35 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.36 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.37 typedef typename Digraph::template NodeMap<int> DistMap;
12.38 ///Instantiates a \c DistMap.
12.39
12.40 @@ -224,7 +225,7 @@
12.41 ///
12.42 ///\ref named-templ-param "Named parameter" for setting
12.43 ///\c PredMap type.
12.44 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.45 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.46 template <class T>
12.47 struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
12.48 typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
12.49 @@ -244,7 +245,7 @@
12.50 ///
12.51 ///\ref named-templ-param "Named parameter" for setting
12.52 ///\c DistMap type.
12.53 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.54 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.55 template <class T>
12.56 struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
12.57 typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
12.58 @@ -264,7 +265,7 @@
12.59 ///
12.60 ///\ref named-templ-param "Named parameter" for setting
12.61 ///\c ReachedMap type.
12.62 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.63 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.64 template <class T>
12.65 struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
12.66 typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
12.67 @@ -284,7 +285,7 @@
12.68 ///
12.69 ///\ref named-templ-param "Named parameter" for setting
12.70 ///\c ProcessedMap type.
12.71 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.72 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.73 template <class T>
12.74 struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
12.75 typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
12.76 @@ -411,8 +412,8 @@
12.77 ///\name Execution Control
12.78 ///The simplest way to execute the DFS algorithm is to use one of the
12.79 ///member functions called \ref run(Node) "run()".\n
12.80 - ///If you need more control on the execution, first you have to call
12.81 - ///\ref init(), then you can add a source node with \ref addSource()
12.82 + ///If you need better control on the execution, you have to call
12.83 + ///\ref init() first, then you can add a source node with \ref addSource()
12.84 ///and perform the actual computation with \ref start().
12.85 ///This procedure can be repeated if there are nodes that have not
12.86 ///been reached.
12.87 @@ -669,9 +670,9 @@
12.88
12.89 ///@{
12.90
12.91 - ///The DFS path to a node.
12.92 + ///The DFS path to the given node.
12.93
12.94 - ///Returns the DFS path to a node.
12.95 + ///Returns the DFS path to the given node from the root(s).
12.96 ///
12.97 ///\warning \c t should be reached from the root(s).
12.98 ///
12.99 @@ -679,9 +680,9 @@
12.100 ///must be called before using this function.
12.101 Path path(Node t) const { return Path(*G, *_pred, t); }
12.102
12.103 - ///The distance of a node from the root(s).
12.104 + ///The distance of the given node from the root(s).
12.105
12.106 - ///Returns the distance of a node from the root(s).
12.107 + ///Returns the distance of the given node from the root(s).
12.108 ///
12.109 ///\warning If node \c v is not reached from the root(s), then
12.110 ///the return value of this function is undefined.
12.111 @@ -690,7 +691,7 @@
12.112 ///must be called before using this function.
12.113 int dist(Node v) const { return (*_dist)[v]; }
12.114
12.115 - ///Returns the 'previous arc' of the %DFS tree for a node.
12.116 + ///Returns the 'previous arc' of the %DFS tree for the given node.
12.117
12.118 ///This function returns the 'previous arc' of the %DFS tree for the
12.119 ///node \c v, i.e. it returns the last arc of a %DFS path from a
12.120 @@ -698,21 +699,21 @@
12.121 ///root(s) or if \c v is a root.
12.122 ///
12.123 ///The %DFS tree used here is equal to the %DFS tree used in
12.124 - ///\ref predNode().
12.125 + ///\ref predNode() and \ref predMap().
12.126 ///
12.127 ///\pre Either \ref run(Node) "run()" or \ref init()
12.128 ///must be called before using this function.
12.129 Arc predArc(Node v) const { return (*_pred)[v];}
12.130
12.131 - ///Returns the 'previous node' of the %DFS tree.
12.132 + ///Returns the 'previous node' of the %DFS tree for the given node.
12.133
12.134 ///This function returns the 'previous node' of the %DFS
12.135 ///tree for the node \c v, i.e. it returns the last but one node
12.136 - ///from a %DFS path from a root to \c v. It is \c INVALID
12.137 + ///of a %DFS path from a root to \c v. It is \c INVALID
12.138 ///if \c v is not reached from the root(s) or if \c v is a root.
12.139 ///
12.140 ///The %DFS tree used here is equal to the %DFS tree used in
12.141 - ///\ref predArc().
12.142 + ///\ref predArc() and \ref predMap().
12.143 ///
12.144 ///\pre Either \ref run(Node) "run()" or \ref init()
12.145 ///must be called before using this function.
12.146 @@ -733,13 +734,13 @@
12.147 ///predecessor arcs.
12.148 ///
12.149 ///Returns a const reference to the node map that stores the predecessor
12.150 - ///arcs, which form the DFS tree.
12.151 + ///arcs, which form the DFS tree (forest).
12.152 ///
12.153 ///\pre Either \ref run(Node) "run()" or \ref init()
12.154 ///must be called before using this function.
12.155 const PredMap &predMap() const { return *_pred;}
12.156
12.157 - ///Checks if a node is reached from the root(s).
12.158 + ///Checks if the given node. node is reached from the root(s).
12.159
12.160 ///Returns \c true if \c v is reached from the root(s).
12.161 ///
12.162 @@ -765,7 +766,7 @@
12.163 ///
12.164 ///The type of the map that stores the predecessor
12.165 ///arcs of the %DFS paths.
12.166 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.167 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.168 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
12.169 ///Instantiates a PredMap.
12.170
12.171 @@ -780,7 +781,7 @@
12.172 ///The type of the map that indicates which nodes are processed.
12.173
12.174 ///The type of the map that indicates which nodes are processed.
12.175 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.176 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.177 ///By default it is a NullMap.
12.178 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
12.179 ///Instantiates a ProcessedMap.
12.180 @@ -800,7 +801,7 @@
12.181 ///The type of the map that indicates which nodes are reached.
12.182
12.183 ///The type of the map that indicates which nodes are reached.
12.184 - ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.185 + ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.186 typedef typename Digraph::template NodeMap<bool> ReachedMap;
12.187 ///Instantiates a ReachedMap.
12.188
12.189 @@ -815,7 +816,7 @@
12.190 ///The type of the map that stores the distances of the nodes.
12.191
12.192 ///The type of the map that stores the distances of the nodes.
12.193 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
12.194 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
12.195 typedef typename Digraph::template NodeMap<int> DistMap;
12.196 ///Instantiates a DistMap.
12.197
12.198 @@ -830,18 +831,14 @@
12.199 ///The type of the DFS paths.
12.200
12.201 ///The type of the DFS paths.
12.202 - ///It must meet the \ref concepts::Path "Path" concept.
12.203 + ///It must conform to the \ref concepts::Path "Path" concept.
12.204 typedef lemon::Path<Digraph> Path;
12.205 };
12.206
12.207 /// Default traits class used by DfsWizard
12.208
12.209 - /// To make it easier to use Dfs algorithm
12.210 - /// we have created a wizard class.
12.211 - /// This \ref DfsWizard class needs default traits,
12.212 - /// as well as the \ref Dfs class.
12.213 - /// The \ref DfsWizardBase is a class to be the default traits of the
12.214 - /// \ref DfsWizard class.
12.215 + /// Default traits class used by DfsWizard.
12.216 + /// \tparam GR The type of the digraph.
12.217 template<class GR>
12.218 class DfsWizardBase : public DfsWizardDefaultTraits<GR>
12.219 {
12.220 @@ -869,7 +866,7 @@
12.221 public:
12.222 /// Constructor.
12.223
12.224 - /// This constructor does not require parameters, therefore it initiates
12.225 + /// This constructor does not require parameters, it initiates
12.226 /// all of the attributes to \c 0.
12.227 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
12.228 _dist(0), _path(0), _di(0) {}
12.229 @@ -899,7 +896,6 @@
12.230 {
12.231 typedef TR Base;
12.232
12.233 - ///The type of the digraph the algorithm runs on.
12.234 typedef typename TR::Digraph Digraph;
12.235
12.236 typedef typename Digraph::Node Node;
12.237 @@ -907,16 +903,10 @@
12.238 typedef typename Digraph::Arc Arc;
12.239 typedef typename Digraph::OutArcIt OutArcIt;
12.240
12.241 - ///\brief The type of the map that stores the predecessor
12.242 - ///arcs of the DFS paths.
12.243 typedef typename TR::PredMap PredMap;
12.244 - ///\brief The type of the map that stores the distances of the nodes.
12.245 typedef typename TR::DistMap DistMap;
12.246 - ///\brief The type of the map that indicates which nodes are reached.
12.247 typedef typename TR::ReachedMap ReachedMap;
12.248 - ///\brief The type of the map that indicates which nodes are processed.
12.249 typedef typename TR::ProcessedMap ProcessedMap;
12.250 - ///The type of the DFS paths
12.251 typedef typename TR::Path Path;
12.252
12.253 public:
12.254 @@ -999,11 +989,12 @@
12.255 static PredMap *createPredMap(const Digraph &) { return 0; };
12.256 SetPredMapBase(const TR &b) : TR(b) {}
12.257 };
12.258 - ///\brief \ref named-func-param "Named parameter"
12.259 - ///for setting PredMap object.
12.260 +
12.261 + ///\brief \ref named-templ-param "Named parameter" for setting
12.262 + ///the predecessor map.
12.263 ///
12.264 - ///\ref named-func-param "Named parameter"
12.265 - ///for setting PredMap object.
12.266 + ///\ref named-templ-param "Named parameter" function for setting
12.267 + ///the map that stores the predecessor arcs of the nodes.
12.268 template<class T>
12.269 DfsWizard<SetPredMapBase<T> > predMap(const T &t)
12.270 {
12.271 @@ -1017,11 +1008,12 @@
12.272 static ReachedMap *createReachedMap(const Digraph &) { return 0; };
12.273 SetReachedMapBase(const TR &b) : TR(b) {}
12.274 };
12.275 - ///\brief \ref named-func-param "Named parameter"
12.276 - ///for setting ReachedMap object.
12.277 +
12.278 + ///\brief \ref named-templ-param "Named parameter" for setting
12.279 + ///the reached map.
12.280 ///
12.281 - /// \ref named-func-param "Named parameter"
12.282 - ///for setting ReachedMap object.
12.283 + ///\ref named-templ-param "Named parameter" function for setting
12.284 + ///the map that indicates which nodes are reached.
12.285 template<class T>
12.286 DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
12.287 {
12.288 @@ -1035,11 +1027,13 @@
12.289 static DistMap *createDistMap(const Digraph &) { return 0; };
12.290 SetDistMapBase(const TR &b) : TR(b) {}
12.291 };
12.292 - ///\brief \ref named-func-param "Named parameter"
12.293 - ///for setting DistMap object.
12.294 +
12.295 + ///\brief \ref named-templ-param "Named parameter" for setting
12.296 + ///the distance map.
12.297 ///
12.298 - /// \ref named-func-param "Named parameter"
12.299 - ///for setting DistMap object.
12.300 + ///\ref named-templ-param "Named parameter" function for setting
12.301 + ///the map that stores the distances of the nodes calculated
12.302 + ///by the algorithm.
12.303 template<class T>
12.304 DfsWizard<SetDistMapBase<T> > distMap(const T &t)
12.305 {
12.306 @@ -1053,11 +1047,12 @@
12.307 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
12.308 SetProcessedMapBase(const TR &b) : TR(b) {}
12.309 };
12.310 - ///\brief \ref named-func-param "Named parameter"
12.311 - ///for setting ProcessedMap object.
12.312 +
12.313 + ///\brief \ref named-func-param "Named parameter" for setting
12.314 + ///the processed map.
12.315 ///
12.316 - /// \ref named-func-param "Named parameter"
12.317 - ///for setting ProcessedMap object.
12.318 + ///\ref named-templ-param "Named parameter" function for setting
12.319 + ///the map that indicates which nodes are processed.
12.320 template<class T>
12.321 DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
12.322 {
12.323 @@ -1208,7 +1203,7 @@
12.324 /// \brief The type of the map that indicates which nodes are reached.
12.325 ///
12.326 /// The type of the map that indicates which nodes are reached.
12.327 - /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.328 + /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
12.329 typedef typename Digraph::template NodeMap<bool> ReachedMap;
12.330
12.331 /// \brief Instantiates a ReachedMap.
12.332 @@ -1369,8 +1364,8 @@
12.333 /// \name Execution Control
12.334 /// The simplest way to execute the DFS algorithm is to use one of the
12.335 /// member functions called \ref run(Node) "run()".\n
12.336 - /// If you need more control on the execution, first you have to call
12.337 - /// \ref init(), then you can add a source node with \ref addSource()
12.338 + /// If you need better control on the execution, you have to call
12.339 + /// \ref init() first, then you can add a source node with \ref addSource()
12.340 /// and perform the actual computation with \ref start().
12.341 /// This procedure can be repeated if there are nodes that have not
12.342 /// been reached.
12.343 @@ -1620,7 +1615,7 @@
12.344
12.345 ///@{
12.346
12.347 - /// \brief Checks if a node is reached from the root(s).
12.348 + /// \brief Checks if the given node is reached from the root(s).
12.349 ///
12.350 /// Returns \c true if \c v is reached from the root(s).
12.351 ///
13.1 --- a/lemon/dijkstra.h Wed Sep 30 08:36:43 2009 +0200
13.2 +++ b/lemon/dijkstra.h Wed Sep 30 08:41:06 2009 +0200
13.3 @@ -70,9 +70,9 @@
13.4 ///The type of the map that stores the arc lengths.
13.5
13.6 ///The type of the map that stores the arc lengths.
13.7 - ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
13.8 + ///It must conform to the \ref concepts::ReadMap "ReadMap" concept.
13.9 typedef LEN LengthMap;
13.10 - ///The type of the length of the arcs.
13.11 + ///The type of the arc lengths.
13.12 typedef typename LEN::Value Value;
13.13
13.14 /// Operation traits for %Dijkstra algorithm.
13.15 @@ -116,7 +116,7 @@
13.16 ///
13.17 ///The type of the map that stores the predecessor
13.18 ///arcs of the shortest paths.
13.19 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.20 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.21 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
13.22 ///Instantiates a \c PredMap.
13.23
13.24 @@ -131,7 +131,7 @@
13.25 ///The type of the map that indicates which nodes are processed.
13.26
13.27 ///The type of the map that indicates which nodes are processed.
13.28 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.29 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.30 ///By default it is a NullMap.
13.31 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
13.32 ///Instantiates a \c ProcessedMap.
13.33 @@ -151,7 +151,7 @@
13.34 ///The type of the map that stores the distances of the nodes.
13.35
13.36 ///The type of the map that stores the distances of the nodes.
13.37 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.38 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.39 typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap;
13.40 ///Instantiates a \c DistMap.
13.41
13.42 @@ -169,6 +169,10 @@
13.43 /// \ingroup shortest_path
13.44 ///This class provides an efficient implementation of the %Dijkstra algorithm.
13.45 ///
13.46 + ///The %Dijkstra algorithm solves the single-source shortest path problem
13.47 + ///when all arc lengths are non-negative. If there are negative lengths,
13.48 + ///the BellmanFord algorithm should be used instead.
13.49 + ///
13.50 ///The arc lengths are passed to the algorithm using a
13.51 ///\ref concepts::ReadMap "ReadMap",
13.52 ///so it is easy to change it to any kind of length.
13.53 @@ -201,7 +205,7 @@
13.54 ///The type of the digraph the algorithm runs on.
13.55 typedef typename TR::Digraph Digraph;
13.56
13.57 - ///The type of the length of the arcs.
13.58 + ///The type of the arc lengths.
13.59 typedef typename TR::LengthMap::Value Value;
13.60 ///The type of the map that stores the arc lengths.
13.61 typedef typename TR::LengthMap LengthMap;
13.62 @@ -304,7 +308,7 @@
13.63 ///
13.64 ///\ref named-templ-param "Named parameter" for setting
13.65 ///\c PredMap type.
13.66 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.67 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.68 template <class T>
13.69 struct SetPredMap
13.70 : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
13.71 @@ -325,7 +329,7 @@
13.72 ///
13.73 ///\ref named-templ-param "Named parameter" for setting
13.74 ///\c DistMap type.
13.75 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.76 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.77 template <class T>
13.78 struct SetDistMap
13.79 : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
13.80 @@ -346,7 +350,7 @@
13.81 ///
13.82 ///\ref named-templ-param "Named parameter" for setting
13.83 ///\c ProcessedMap type.
13.84 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.85 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.86 template <class T>
13.87 struct SetProcessedMap
13.88 : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
13.89 @@ -443,6 +447,7 @@
13.90 ///
13.91 ///\ref named-templ-param "Named parameter" for setting
13.92 ///\c OperationTraits type.
13.93 + /// For more information see \ref DijkstraDefaultOperationTraits.
13.94 template <class T>
13.95 struct SetOperationTraits
13.96 : public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
13.97 @@ -584,8 +589,8 @@
13.98 ///\name Execution Control
13.99 ///The simplest way to execute the %Dijkstra algorithm is to use
13.100 ///one of the member functions called \ref run(Node) "run()".\n
13.101 - ///If you need more control on the execution, first you have to call
13.102 - ///\ref init(), then you can add several source nodes with
13.103 + ///If you need better control on the execution, you have to call
13.104 + ///\ref init() first, then you can add several source nodes with
13.105 ///\ref addSource(). Finally the actual path computation can be
13.106 ///performed with one of the \ref start() functions.
13.107
13.108 @@ -801,14 +806,14 @@
13.109 ///\name Query Functions
13.110 ///The results of the %Dijkstra algorithm can be obtained using these
13.111 ///functions.\n
13.112 - ///Either \ref run(Node) "run()" or \ref start() should be called
13.113 + ///Either \ref run(Node) "run()" or \ref init() should be called
13.114 ///before using them.
13.115
13.116 ///@{
13.117
13.118 - ///The shortest path to a node.
13.119 + ///The shortest path to the given node.
13.120
13.121 - ///Returns the shortest path to a node.
13.122 + ///Returns the shortest path to the given node from the root(s).
13.123 ///
13.124 ///\warning \c t should be reached from the root(s).
13.125 ///
13.126 @@ -816,9 +821,9 @@
13.127 ///must be called before using this function.
13.128 Path path(Node t) const { return Path(*G, *_pred, t); }
13.129
13.130 - ///The distance of a node from the root(s).
13.131 + ///The distance of the given node from the root(s).
13.132
13.133 - ///Returns the distance of a node from the root(s).
13.134 + ///Returns the distance of the given node from the root(s).
13.135 ///
13.136 ///\warning If node \c v is not reached from the root(s), then
13.137 ///the return value of this function is undefined.
13.138 @@ -827,29 +832,31 @@
13.139 ///must be called before using this function.
13.140 Value dist(Node v) const { return (*_dist)[v]; }
13.141
13.142 - ///Returns the 'previous arc' of the shortest path tree for a node.
13.143 -
13.144 + ///\brief Returns the 'previous arc' of the shortest path tree for
13.145 + ///the given node.
13.146 + ///
13.147 ///This function returns the 'previous arc' of the shortest path
13.148 ///tree for the node \c v, i.e. it returns the last arc of a
13.149 ///shortest path from a root to \c v. It is \c INVALID if \c v
13.150 ///is not reached from the root(s) or if \c v is a root.
13.151 ///
13.152 ///The shortest path tree used here is equal to the shortest path
13.153 - ///tree used in \ref predNode().
13.154 + ///tree used in \ref predNode() and \ref predMap().
13.155 ///
13.156 ///\pre Either \ref run(Node) "run()" or \ref init()
13.157 ///must be called before using this function.
13.158 Arc predArc(Node v) const { return (*_pred)[v]; }
13.159
13.160 - ///Returns the 'previous node' of the shortest path tree for a node.
13.161 -
13.162 + ///\brief Returns the 'previous node' of the shortest path tree for
13.163 + ///the given node.
13.164 + ///
13.165 ///This function returns the 'previous node' of the shortest path
13.166 ///tree for the node \c v, i.e. it returns the last but one node
13.167 - ///from a shortest path from a root to \c v. It is \c INVALID
13.168 + ///of a shortest path from a root to \c v. It is \c INVALID
13.169 ///if \c v is not reached from the root(s) or if \c v is a root.
13.170 ///
13.171 ///The shortest path tree used here is equal to the shortest path
13.172 - ///tree used in \ref predArc().
13.173 + ///tree used in \ref predArc() and \ref predMap().
13.174 ///
13.175 ///\pre Either \ref run(Node) "run()" or \ref init()
13.176 ///must be called before using this function.
13.177 @@ -870,13 +877,13 @@
13.178 ///predecessor arcs.
13.179 ///
13.180 ///Returns a const reference to the node map that stores the predecessor
13.181 - ///arcs, which form the shortest path tree.
13.182 + ///arcs, which form the shortest path tree (forest).
13.183 ///
13.184 ///\pre Either \ref run(Node) "run()" or \ref init()
13.185 ///must be called before using this function.
13.186 const PredMap &predMap() const { return *_pred;}
13.187
13.188 - ///Checks if a node is reached from the root(s).
13.189 + ///Checks if the given node is reached from the root(s).
13.190
13.191 ///Returns \c true if \c v is reached from the root(s).
13.192 ///
13.193 @@ -895,9 +902,9 @@
13.194 bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
13.195 Heap::POST_HEAP; }
13.196
13.197 - ///The current distance of a node from the root(s).
13.198 + ///The current distance of the given node from the root(s).
13.199
13.200 - ///Returns the current distance of a node from the root(s).
13.201 + ///Returns the current distance of the given node from the root(s).
13.202 ///It may be decreased in the following processes.
13.203 ///
13.204 ///\pre Either \ref run(Node) "run()" or \ref init()
13.205 @@ -924,9 +931,9 @@
13.206 ///The type of the map that stores the arc lengths.
13.207
13.208 ///The type of the map that stores the arc lengths.
13.209 - ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
13.210 + ///It must conform to the \ref concepts::ReadMap "ReadMap" concept.
13.211 typedef LEN LengthMap;
13.212 - ///The type of the length of the arcs.
13.213 + ///The type of the arc lengths.
13.214 typedef typename LEN::Value Value;
13.215
13.216 /// Operation traits for Dijkstra algorithm.
13.217 @@ -973,7 +980,7 @@
13.218 ///
13.219 ///The type of the map that stores the predecessor
13.220 ///arcs of the shortest paths.
13.221 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.222 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.223 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
13.224 ///Instantiates a PredMap.
13.225
13.226 @@ -988,7 +995,7 @@
13.227 ///The type of the map that indicates which nodes are processed.
13.228
13.229 ///The type of the map that indicates which nodes are processed.
13.230 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.231 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.232 ///By default it is a NullMap.
13.233 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
13.234 ///Instantiates a ProcessedMap.
13.235 @@ -1008,7 +1015,7 @@
13.236 ///The type of the map that stores the distances of the nodes.
13.237
13.238 ///The type of the map that stores the distances of the nodes.
13.239 - ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
13.240 + ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
13.241 typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap;
13.242 ///Instantiates a DistMap.
13.243
13.244 @@ -1023,18 +1030,15 @@
13.245 ///The type of the shortest paths.
13.246
13.247 ///The type of the shortest paths.
13.248 - ///It must meet the \ref concepts::Path "Path" concept.
13.249 + ///It must conform to the \ref concepts::Path "Path" concept.
13.250 typedef lemon::Path<Digraph> Path;
13.251 };
13.252
13.253 /// Default traits class used by DijkstraWizard
13.254
13.255 - /// To make it easier to use Dijkstra algorithm
13.256 - /// we have created a wizard class.
13.257 - /// This \ref DijkstraWizard class needs default traits,
13.258 - /// as well as the \ref Dijkstra class.
13.259 - /// The \ref DijkstraWizardBase is a class to be the default traits of the
13.260 - /// \ref DijkstraWizard class.
13.261 + /// Default traits class used by DijkstraWizard.
13.262 + /// \tparam GR The type of the digraph.
13.263 + /// \tparam LEN The type of the length map.
13.264 template<typename GR, typename LEN>
13.265 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN>
13.266 {
13.267 @@ -1093,7 +1097,6 @@
13.268 {
13.269 typedef TR Base;
13.270
13.271 - ///The type of the digraph the algorithm runs on.
13.272 typedef typename TR::Digraph Digraph;
13.273
13.274 typedef typename Digraph::Node Node;
13.275 @@ -1101,20 +1104,12 @@
13.276 typedef typename Digraph::Arc Arc;
13.277 typedef typename Digraph::OutArcIt OutArcIt;
13.278
13.279 - ///The type of the map that stores the arc lengths.
13.280 typedef typename TR::LengthMap LengthMap;
13.281 - ///The type of the length of the arcs.
13.282 typedef typename LengthMap::Value Value;
13.283 - ///\brief The type of the map that stores the predecessor
13.284 - ///arcs of the shortest paths.
13.285 typedef typename TR::PredMap PredMap;
13.286 - ///The type of the map that stores the distances of the nodes.
13.287 typedef typename TR::DistMap DistMap;
13.288 - ///The type of the map that indicates which nodes are processed.
13.289 typedef typename TR::ProcessedMap ProcessedMap;
13.290 - ///The type of the shortest paths
13.291 typedef typename TR::Path Path;
13.292 - ///The heap type used by the dijkstra algorithm.
13.293 typedef typename TR::Heap Heap;
13.294
13.295 public:
13.296 @@ -1186,11 +1181,12 @@
13.297 static PredMap *createPredMap(const Digraph &) { return 0; };
13.298 SetPredMapBase(const TR &b) : TR(b) {}
13.299 };
13.300 - ///\brief \ref named-func-param "Named parameter"
13.301 - ///for setting PredMap object.
13.302 +
13.303 + ///\brief \ref named-templ-param "Named parameter" for setting
13.304 + ///the predecessor map.
13.305 ///
13.306 - ///\ref named-func-param "Named parameter"
13.307 - ///for setting PredMap object.
13.308 + ///\ref named-templ-param "Named parameter" function for setting
13.309 + ///the map that stores the predecessor arcs of the nodes.
13.310 template<class T>
13.311 DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
13.312 {
13.313 @@ -1204,11 +1200,13 @@
13.314 static DistMap *createDistMap(const Digraph &) { return 0; };
13.315 SetDistMapBase(const TR &b) : TR(b) {}
13.316 };
13.317 - ///\brief \ref named-func-param "Named parameter"
13.318 - ///for setting DistMap object.
13.319 +
13.320 + ///\brief \ref named-templ-param "Named parameter" for setting
13.321 + ///the distance map.
13.322 ///
13.323 - ///\ref named-func-param "Named parameter"
13.324 - ///for setting DistMap object.
13.325 + ///\ref named-templ-param "Named parameter" function for setting
13.326 + ///the map that stores the distances of the nodes calculated
13.327 + ///by the algorithm.
13.328 template<class T>
13.329 DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
13.330 {
13.331 @@ -1222,11 +1220,12 @@
13.332 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
13.333 SetProcessedMapBase(const TR &b) : TR(b) {}
13.334 };
13.335 - ///\brief \ref named-func-param "Named parameter"
13.336 - ///for setting ProcessedMap object.
13.337 +
13.338 + ///\brief \ref named-func-param "Named parameter" for setting
13.339 + ///the processed map.
13.340 ///
13.341 - /// \ref named-func-param "Named parameter"
13.342 - ///for setting ProcessedMap object.
13.343 + ///\ref named-templ-param "Named parameter" function for setting
13.344 + ///the map that indicates which nodes are processed.
13.345 template<class T>
13.346 DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
13.347 {
13.348 @@ -1239,6 +1238,7 @@
13.349 typedef T Path;
13.350 SetPathBase(const TR &b) : TR(b) {}
13.351 };
13.352 +
13.353 ///\brief \ref named-func-param "Named parameter"
13.354 ///for getting the shortest path to the target node.
13.355 ///
14.1 --- a/lemon/dim2.h Wed Sep 30 08:36:43 2009 +0200
14.2 +++ b/lemon/dim2.h Wed Sep 30 08:41:06 2009 +0200
14.3 @@ -21,16 +21,9 @@
14.4
14.5 #include <iostream>
14.6
14.7 -///\ingroup misc
14.8 +///\ingroup geomdat
14.9 ///\file
14.10 ///\brief A simple two dimensional vector and a bounding box implementation
14.11 -///
14.12 -/// The class \ref lemon::dim2::Point "dim2::Point" implements
14.13 -/// a two dimensional vector with the usual operations.
14.14 -///
14.15 -/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
14.16 -/// the rectangular bounding box of a set of
14.17 -/// \ref lemon::dim2::Point "dim2::Point"'s.
14.18
14.19 namespace lemon {
14.20
14.21 @@ -40,7 +33,7 @@
14.22 ///tools for handling two dimensional coordinates
14.23 namespace dim2 {
14.24
14.25 - /// \addtogroup misc
14.26 + /// \addtogroup geomdat
14.27 /// @{
14.28
14.29 /// Two dimensional vector (plain vector)
15.1 --- a/lemon/fib_heap.h Wed Sep 30 08:36:43 2009 +0200
15.2 +++ b/lemon/fib_heap.h Wed Sep 30 08:41:06 2009 +0200
15.3 @@ -20,53 +20,49 @@
15.4 #define LEMON_FIB_HEAP_H
15.5
15.6 ///\file
15.7 -///\ingroup auxdat
15.8 -///\brief Fibonacci Heap implementation.
15.9 +///\ingroup heaps
15.10 +///\brief Fibonacci heap implementation.
15.11
15.12 #include <vector>
15.13 +#include <utility>
15.14 #include <functional>
15.15 #include <lemon/math.h>
15.16
15.17 namespace lemon {
15.18
15.19 - /// \ingroup auxdat
15.20 + /// \ingroup heaps
15.21 ///
15.22 - ///\brief Fibonacci Heap.
15.23 + /// \brief Fibonacci heap data structure.
15.24 ///
15.25 - ///This class implements the \e Fibonacci \e heap data structure. A \e heap
15.26 - ///is a data structure for storing items with specified values called \e
15.27 - ///priorities in such a way that finding the item with minimum priority is
15.28 - ///efficient. \c CMP specifies the ordering of the priorities. In a heap
15.29 - ///one can change the priority of an item, add or erase an item, etc.
15.30 + /// This class implements the \e Fibonacci \e heap data structure.
15.31 + /// It fully conforms to the \ref concepts::Heap "heap concept".
15.32 ///
15.33 - ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
15.34 - ///heap. In case of many calls to these operations, it is better to use a
15.35 - ///\ref BinHeap "binary heap".
15.36 + /// The methods \ref increase() and \ref erase() are not efficient in a
15.37 + /// Fibonacci heap. In case of many calls of these operations, it is
15.38 + /// better to use other heap structure, e.g. \ref BinHeap "binary heap".
15.39 ///
15.40 - ///\param PRIO Type of the priority of the items.
15.41 - ///\param IM A read and writable Item int map, used internally
15.42 - ///to handle the cross references.
15.43 - ///\param CMP A class for the ordering of the priorities. The
15.44 - ///default is \c std::less<PRIO>.
15.45 - ///
15.46 - ///\sa BinHeap
15.47 - ///\sa Dijkstra
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 PRIO, typename IM, typename CMP>
15.55 + template <typename PR, typename IM, typename CMP>
15.56 #else
15.57 - template <typename PRIO, typename IM, typename CMP = std::less<PRIO> >
15.58 + template <typename PR, typename IM, typename CMP = std::less<PR> >
15.59 #endif
15.60 class FibHeap {
15.61 public:
15.62 - ///\e
15.63 +
15.64 + /// Type of the item-int map.
15.65 typedef IM ItemIntMap;
15.66 - ///\e
15.67 - typedef PRIO Prio;
15.68 - ///\e
15.69 + /// Type of the priorities.
15.70 + typedef PR Prio;
15.71 + /// Type of the items stored in the heap.
15.72 typedef typename ItemIntMap::Key Item;
15.73 - ///\e
15.74 + /// Type of the item-priority pairs.
15.75 typedef std::pair<Item,Prio> Pair;
15.76 - ///\e
15.77 + /// Functor type for comparing the priorities.
15.78 typedef CMP Compare;
15.79
15.80 private:
15.81 @@ -80,10 +76,10 @@
15.82
15.83 public:
15.84
15.85 - /// \brief Type to represent the items states.
15.86 + /// \brief Type to represent the states of the items.
15.87 ///
15.88 - /// Each Item element have a state associated to it. It may be "in heap",
15.89 - /// "pre heap" or "post heap". The latter two are indifferent from the
15.90 + /// Each item has a state associated to it. It can be "in heap",
15.91 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
15.92 /// heap's point of view, but may be useful to the user.
15.93 ///
15.94 /// The item-int map must be initialized in such way that it assigns
15.95 @@ -94,60 +90,54 @@
15.96 POST_HEAP = -2 ///< = -2.
15.97 };
15.98
15.99 - /// \brief The constructor
15.100 + /// \brief Constructor.
15.101 ///
15.102 - /// \c map should be given to the constructor, since it is
15.103 - /// used internally to handle the cross references.
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 explicit FibHeap(ItemIntMap &map)
15.109 : _minimum(0), _iim(map), _num() {}
15.110
15.111 - /// \brief The constructor
15.112 + /// \brief Constructor.
15.113 ///
15.114 - /// \c map should be given to the constructor, since it is used
15.115 - /// internally to handle the cross references. \c comp is an
15.116 - /// object for ordering of the priorities.
15.117 + /// Constructor.
15.118 + /// \param map A map that assigns \c int values to the items.
15.119 + /// It is used internally to handle the cross references.
15.120 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
15.121 + /// \param comp The function object used for comparing the priorities.
15.122 FibHeap(ItemIntMap &map, const Compare &comp)
15.123 : _minimum(0), _iim(map), _comp(comp), _num() {}
15.124
15.125 /// \brief The number of items stored in the heap.
15.126 ///
15.127 - /// Returns the number of items stored in the heap.
15.128 + /// This function returns the number of items stored in the heap.
15.129 int size() const { return _num; }
15.130
15.131 - /// \brief Checks if the heap stores no items.
15.132 + /// \brief Check if the heap is empty.
15.133 ///
15.134 - /// Returns \c true if and only if the heap stores no items.
15.135 + /// This function returns \c true if the heap is empty.
15.136 bool empty() const { return _num==0; }
15.137
15.138 - /// \brief Make empty this heap.
15.139 + /// \brief Make the heap empty.
15.140 ///
15.141 - /// Make empty this heap. It does not change the cross reference
15.142 - /// map. If you want to reuse a heap what is not surely empty you
15.143 - /// should first clear the heap and after that you should set the
15.144 - /// cross reference map for each item to \c PRE_HEAP.
15.145 + /// This functon makes the heap empty.
15.146 + /// It does not change the cross reference map. If you want to reuse
15.147 + /// a heap that is not surely empty, you should first clear it and
15.148 + /// then you should set the cross reference map to \c PRE_HEAP
15.149 + /// for each item.
15.150 void clear() {
15.151 _data.clear(); _minimum = 0; _num = 0;
15.152 }
15.153
15.154 - /// \brief \c item gets to the heap with priority \c value independently
15.155 - /// if \c item was already there.
15.156 + /// \brief Insert an item into the heap with the given priority.
15.157 ///
15.158 - /// This method calls \ref push(\c item, \c value) if \c item is not
15.159 - /// stored in the heap and it calls \ref decrease(\c item, \c value) or
15.160 - /// \ref increase(\c item, \c value) otherwise.
15.161 - void set (const Item& item, const Prio& value) {
15.162 - int i=_iim[item];
15.163 - if ( i >= 0 && _data[i].in ) {
15.164 - if ( _comp(value, _data[i].prio) ) decrease(item, value);
15.165 - if ( _comp(_data[i].prio, value) ) increase(item, value);
15.166 - } else push(item, value);
15.167 - }
15.168 -
15.169 - /// \brief Adds \c item to the heap with priority \c value.
15.170 - ///
15.171 - /// Adds \c item to the heap with priority \c value.
15.172 - /// \pre \c item must not be stored in the heap.
15.173 - void push (const Item& item, const Prio& value) {
15.174 + /// This function inserts the given item into the heap with the
15.175 + /// given priority.
15.176 + /// \param item The item to insert.
15.177 + /// \param prio The priority of the item.
15.178 + /// \pre \e item must not be stored in the heap.
15.179 + void push (const Item& item, const Prio& prio) {
15.180 int i=_iim[item];
15.181 if ( i < 0 ) {
15.182 int s=_data.size();
15.183 @@ -168,47 +158,37 @@
15.184 _data[i].right_neighbor=_data[_minimum].right_neighbor;
15.185 _data[_minimum].right_neighbor=i;
15.186 _data[i].left_neighbor=_minimum;
15.187 - if ( _comp( value, _data[_minimum].prio) ) _minimum=i;
15.188 + if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
15.189 } else {
15.190 _data[i].right_neighbor=_data[i].left_neighbor=i;
15.191 _minimum=i;
15.192 }
15.193 - _data[i].prio=value;
15.194 + _data[i].prio=prio;
15.195 ++_num;
15.196 }
15.197
15.198 - /// \brief Returns the item with minimum priority relative to \c Compare.
15.199 + /// \brief Return the item having minimum priority.
15.200 ///
15.201 - /// This method returns the item with minimum priority relative to \c
15.202 - /// Compare.
15.203 - /// \pre The heap must be nonempty.
15.204 + /// This function returns the item having minimum priority.
15.205 + /// \pre The heap must be non-empty.
15.206 Item top() const { return _data[_minimum].name; }
15.207
15.208 - /// \brief Returns the minimum priority relative to \c Compare.
15.209 + /// \brief The minimum priority.
15.210 ///
15.211 - /// It returns the minimum priority relative to \c Compare.
15.212 - /// \pre The heap must be nonempty.
15.213 - const Prio& prio() const { return _data[_minimum].prio; }
15.214 + /// This function returns the minimum priority.
15.215 + /// \pre The heap must be non-empty.
15.216 + Prio prio() const { return _data[_minimum].prio; }
15.217
15.218 - /// \brief Returns the priority of \c item.
15.219 + /// \brief Remove the item having minimum priority.
15.220 ///
15.221 - /// It returns the priority of \c item.
15.222 - /// \pre \c item must be in the heap.
15.223 - const Prio& operator[](const Item& item) const {
15.224 - return _data[_iim[item]].prio;
15.225 - }
15.226 -
15.227 - /// \brief Deletes the item with minimum priority relative to \c Compare.
15.228 - ///
15.229 - /// This method deletes the item with minimum priority relative to \c
15.230 - /// Compare from the heap.
15.231 + /// This function removes the item having minimum priority.
15.232 /// \pre The heap must be non-empty.
15.233 void pop() {
15.234 /*The first case is that there are only one root.*/
15.235 if ( _data[_minimum].left_neighbor==_minimum ) {
15.236 _data[_minimum].in=false;
15.237 if ( _data[_minimum].degree!=0 ) {
15.238 - makeroot(_data[_minimum].child);
15.239 + makeRoot(_data[_minimum].child);
15.240 _minimum=_data[_minimum].child;
15.241 balance();
15.242 }
15.243 @@ -221,7 +201,7 @@
15.244 int child=_data[_minimum].child;
15.245 int last_child=_data[child].left_neighbor;
15.246
15.247 - makeroot(child);
15.248 + makeRoot(child);
15.249
15.250 _data[left].right_neighbor=child;
15.251 _data[child].left_neighbor=left;
15.252 @@ -234,10 +214,12 @@
15.253 --_num;
15.254 }
15.255
15.256 - /// \brief Deletes \c item from the heap.
15.257 + /// \brief Remove the given item from the heap.
15.258 ///
15.259 - /// This method deletes \c item from the heap, if \c item was already
15.260 - /// stored in the heap. It is quite inefficient in Fibonacci heaps.
15.261 + /// This function removes the given item from the heap if it is
15.262 + /// already stored.
15.263 + /// \param item The item to delete.
15.264 + /// \pre \e item must be in the heap.
15.265 void erase (const Item& item) {
15.266 int i=_iim[item];
15.267
15.268 @@ -252,43 +234,68 @@
15.269 }
15.270 }
15.271
15.272 - /// \brief Decreases the priority of \c item to \c value.
15.273 + /// \brief The priority of the given item.
15.274 ///
15.275 - /// This method decreases the priority of \c item to \c value.
15.276 - /// \pre \c item must be stored in the heap with priority at least \c
15.277 - /// value relative to \c Compare.
15.278 - void decrease (Item item, const Prio& value) {
15.279 + /// This function returns the priority of the given item.
15.280 + /// \param item The item.
15.281 + /// \pre \e item must be in the heap.
15.282 + Prio operator[](const Item& item) const {
15.283 + return _data[_iim[item]].prio;
15.284 + }
15.285 +
15.286 + /// \brief Set the priority of an item or insert it, if it is
15.287 + /// not stored in the heap.
15.288 + ///
15.289 + /// This method sets the priority of the given item if it is
15.290 + /// already stored in the heap. Otherwise it inserts the given
15.291 + /// item into the heap with the given priority.
15.292 + /// \param item The item.
15.293 + /// \param prio The priority.
15.294 + void set (const Item& item, const Prio& prio) {
15.295 int i=_iim[item];
15.296 - _data[i].prio=value;
15.297 + if ( i >= 0 && _data[i].in ) {
15.298 + if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
15.299 + if ( _comp(_data[i].prio, prio) ) increase(item, prio);
15.300 + } else push(item, prio);
15.301 + }
15.302 +
15.303 + /// \brief Decrease the priority of an item to the given value.
15.304 + ///
15.305 + /// This function decreases the priority of an item to the given value.
15.306 + /// \param item The item.
15.307 + /// \param prio The priority.
15.308 + /// \pre \e item must be stored in the heap with priority at least \e prio.
15.309 + void decrease (const Item& item, const Prio& prio) {
15.310 + int i=_iim[item];
15.311 + _data[i].prio=prio;
15.312 int p=_data[i].parent;
15.313
15.314 - if ( p!=-1 && _comp(value, _data[p].prio) ) {
15.315 + if ( p!=-1 && _comp(prio, _data[p].prio) ) {
15.316 cut(i,p);
15.317 cascade(p);
15.318 }
15.319 - if ( _comp(value, _data[_minimum].prio) ) _minimum=i;
15.320 + if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
15.321 }
15.322
15.323 - /// \brief Increases the priority of \c item to \c value.
15.324 + /// \brief Increase the priority of an item to the given value.
15.325 ///
15.326 - /// This method sets the priority of \c item to \c value. Though
15.327 - /// there is no precondition on the priority of \c item, this
15.328 - /// method should be used only if it is indeed necessary to increase
15.329 - /// (relative to \c Compare) the priority of \c item, because this
15.330 - /// method is inefficient.
15.331 - void increase (Item item, const Prio& value) {
15.332 + /// This function increases the priority of an item to the given value.
15.333 + /// \param item The item.
15.334 + /// \param prio The priority.
15.335 + /// \pre \e item must be stored in the heap with priority at most \e prio.
15.336 + void increase (const Item& item, const Prio& prio) {
15.337 erase(item);
15.338 - push(item, value);
15.339 + push(item, prio);
15.340 }
15.341
15.342 -
15.343 - /// \brief Returns if \c item is in, has already been in, or has never
15.344 - /// been in the heap.
15.345 + /// \brief Return the state of an item.
15.346 ///
15.347 - /// This method returns PRE_HEAP if \c item has never been in the
15.348 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
15.349 - /// otherwise. In the latter case it is possible that \c item will
15.350 - /// get back to the heap again.
15.351 + /// This method returns \c PRE_HEAP if the given item has never
15.352 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
15.353 + /// and \c POST_HEAP otherwise.
15.354 + /// In the latter case it is possible that the item will get back
15.355 + /// to the heap again.
15.356 + /// \param item The item.
15.357 State state(const Item &item) const {
15.358 int i=_iim[item];
15.359 if( i>=0 ) {
15.360 @@ -298,11 +305,11 @@
15.361 return State(i);
15.362 }
15.363
15.364 - /// \brief Sets the state of the \c item in the heap.
15.365 + /// \brief Set the state of an item in the heap.
15.366 ///
15.367 - /// Sets the state of the \c item in the heap. It can be used to
15.368 - /// manually clear the heap when it is important to achive the
15.369 - /// better time _complexity.
15.370 + /// This function sets the state of the given item in the heap.
15.371 + /// It can be used to manually clear the heap when it is important
15.372 + /// to achive better time complexity.
15.373 /// \param i The item.
15.374 /// \param st The state. It should not be \c IN_HEAP.
15.375 void state(const Item& i, State st) {
15.376 @@ -365,7 +372,7 @@
15.377 } while ( s != m );
15.378 }
15.379
15.380 - void makeroot(int c) {
15.381 + void makeRoot(int c) {
15.382 int s=c;
15.383 do {
15.384 _data[s].parent=-1;
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/lemon/fourary_heap.h Wed Sep 30 08:41:06 2009 +0200
16.3 @@ -0,0 +1,342 @@
16.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
16.5 + *
16.6 + * This file is a part of LEMON, a generic C++ optimization library.
16.7 + *
16.8 + * Copyright (C) 2003-2009
16.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
16.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
16.11 + *
16.12 + * Permission to use, modify and distribute this software is granted
16.13 + * provided that this copyright notice appears in all copies. For
16.14 + * precise terms see the accompanying LICENSE file.
16.15 + *
16.16 + * This software is provided "AS IS" with no warranty of any kind,
16.17 + * express or implied, and with no claim as to its suitability for any
16.18 + * purpose.
16.19 + *
16.20 + */
16.21 +
16.22 +#ifndef LEMON_FOURARY_HEAP_H
16.23 +#define LEMON_FOURARY_HEAP_H
16.24 +
16.25 +///\ingroup heaps
16.26 +///\file
16.27 +///\brief Fourary heap implementation.
16.28 +
16.29 +#include <vector>
16.30 +#include <utility>
16.31 +#include <functional>
16.32 +
16.33 +namespace lemon {
16.34 +
16.35 + /// \ingroup heaps
16.36 + ///
16.37 + ///\brief Fourary heap data structure.
16.38 + ///
16.39 + /// This class implements the \e fourary \e heap data structure.
16.40 + /// It fully conforms to the \ref concepts::Heap "heap concept".
16.41 + ///
16.42 + /// The fourary heap is a specialization of the \ref KaryHeap "K-ary heap"
16.43 + /// for <tt>K=4</tt>. It is similar to the \ref BinHeap "binary heap",
16.44 + /// but its nodes have at most four children, instead of two.
16.45 + ///
16.46 + /// \tparam PR Type of the priorities of the items.
16.47 + /// \tparam IM A read-writable item map with \c int values, used
16.48 + /// internally to handle the cross references.
16.49 + /// \tparam CMP A functor class for comparing the priorities.
16.50 + /// The default is \c std::less<PR>.
16.51 + ///
16.52 + ///\sa BinHeap
16.53 + ///\sa KaryHeap
16.54 +#ifdef DOXYGEN
16.55 + template <typename PR, typename IM, typename CMP>
16.56 +#else
16.57 + template <typename PR, typename IM, typename CMP = std::less<PR> >
16.58 +#endif
16.59 + class FouraryHeap {
16.60 + public:
16.61 + /// Type of the item-int map.
16.62 + typedef IM ItemIntMap;
16.63 + /// Type of the priorities.
16.64 + typedef PR Prio;
16.65 + /// Type of the items stored in the heap.
16.66 + typedef typename ItemIntMap::Key Item;
16.67 + /// Type of the item-priority pairs.
16.68 + typedef std::pair<Item,Prio> Pair;
16.69 + /// Functor type for comparing the priorities.
16.70 + typedef CMP Compare;
16.71 +
16.72 + /// \brief Type to represent the states of the items.
16.73 + ///
16.74 + /// Each item has a state associated to it. It can be "in heap",
16.75 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
16.76 + /// heap's point of view, but may be useful to the user.
16.77 + ///
16.78 + /// The item-int map must be initialized in such way that it assigns
16.79 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
16.80 + enum State {
16.81 + IN_HEAP = 0, ///< = 0.
16.82 + PRE_HEAP = -1, ///< = -1.
16.83 + POST_HEAP = -2 ///< = -2.
16.84 + };
16.85 +
16.86 + private:
16.87 + std::vector<Pair> _data;
16.88 + Compare _comp;
16.89 + ItemIntMap &_iim;
16.90 +
16.91 + public:
16.92 + /// \brief Constructor.
16.93 + ///
16.94 + /// Constructor.
16.95 + /// \param map A map that assigns \c int values to the items.
16.96 + /// It is used internally to handle the cross references.
16.97 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
16.98 + explicit FouraryHeap(ItemIntMap &map) : _iim(map) {}
16.99 +
16.100 + /// \brief Constructor.
16.101 + ///
16.102 + /// Constructor.
16.103 + /// \param map A map that assigns \c int values to the items.
16.104 + /// It is used internally to handle the cross references.
16.105 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
16.106 + /// \param comp The function object used for comparing the priorities.
16.107 + FouraryHeap(ItemIntMap &map, const Compare &comp)
16.108 + : _iim(map), _comp(comp) {}
16.109 +
16.110 + /// \brief The number of items stored in the heap.
16.111 + ///
16.112 + /// This function returns the number of items stored in the heap.
16.113 + int size() const { return _data.size(); }
16.114 +
16.115 + /// \brief Check if the heap is empty.
16.116 + ///
16.117 + /// This function returns \c true if the heap is empty.
16.118 + bool empty() const { return _data.empty(); }
16.119 +
16.120 + /// \brief Make the heap empty.
16.121 + ///
16.122 + /// This functon makes the heap empty.
16.123 + /// It does not change the cross reference map. If you want to reuse
16.124 + /// a heap that is not surely empty, you should first clear it and
16.125 + /// then you should set the cross reference map to \c PRE_HEAP
16.126 + /// for each item.
16.127 + void clear() { _data.clear(); }
16.128 +
16.129 + private:
16.130 + static int parent(int i) { return (i-1)/4; }
16.131 + static int firstChild(int i) { return 4*i+1; }
16.132 +
16.133 + bool less(const Pair &p1, const Pair &p2) const {
16.134 + return _comp(p1.second, p2.second);
16.135 + }
16.136 +
16.137 + void bubbleUp(int hole, Pair p) {
16.138 + int par = parent(hole);
16.139 + while( hole>0 && less(p,_data[par]) ) {
16.140 + move(_data[par],hole);
16.141 + hole = par;
16.142 + par = parent(hole);
16.143 + }
16.144 + move(p, hole);
16.145 + }
16.146 +
16.147 + void bubbleDown(int hole, Pair p, int length) {
16.148 + if( length>1 ) {
16.149 + int child = firstChild(hole);
16.150 + while( child+3<length ) {
16.151 + int min=child;
16.152 + if( less(_data[++child], _data[min]) ) min=child;
16.153 + if( less(_data[++child], _data[min]) ) min=child;
16.154 + if( less(_data[++child], _data[min]) ) min=child;
16.155 + if( !less(_data[min], p) )
16.156 + goto ok;
16.157 + move(_data[min], hole);
16.158 + hole = min;
16.159 + child = firstChild(hole);
16.160 + }
16.161 + if ( child<length ) {
16.162 + int min = child;
16.163 + if( ++child<length && less(_data[child], _data[min]) ) min=child;
16.164 + if( ++child<length && less(_data[child], _data[min]) ) min=child;
16.165 + if( less(_data[min], p) ) {
16.166 + move(_data[min], hole);
16.167 + hole = min;
16.168 + }
16.169 + }
16.170 + }
16.171 + ok:
16.172 + move(p, hole);
16.173 + }
16.174 +
16.175 + void move(const Pair &p, int i) {
16.176 + _data[i] = p;
16.177 + _iim.set(p.first, i);
16.178 + }
16.179 +
16.180 + public:
16.181 + /// \brief Insert a pair of item and priority into the heap.
16.182 + ///
16.183 + /// This function inserts \c p.first to the heap with priority
16.184 + /// \c p.second.
16.185 + /// \param p The pair to insert.
16.186 + /// \pre \c p.first must not be stored in the heap.
16.187 + void push(const Pair &p) {
16.188 + int n = _data.size();
16.189 + _data.resize(n+1);
16.190 + bubbleUp(n, p);
16.191 + }
16.192 +
16.193 + /// \brief Insert an item into the heap with the given priority.
16.194 + ///
16.195 + /// This function inserts the given item into the heap with the
16.196 + /// given priority.
16.197 + /// \param i The item to insert.
16.198 + /// \param p The priority of the item.
16.199 + /// \pre \e i must not be stored in the heap.
16.200 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
16.201 +
16.202 + /// \brief Return the item having minimum priority.
16.203 + ///
16.204 + /// This function returns the item having minimum priority.
16.205 + /// \pre The heap must be non-empty.
16.206 + Item top() const { return _data[0].first; }
16.207 +
16.208 + /// \brief The minimum priority.
16.209 + ///
16.210 + /// This function returns the minimum priority.
16.211 + /// \pre The heap must be non-empty.
16.212 + Prio prio() const { return _data[0].second; }
16.213 +
16.214 + /// \brief Remove the item having minimum priority.
16.215 + ///
16.216 + /// This function removes the item having minimum priority.
16.217 + /// \pre The heap must be non-empty.
16.218 + void pop() {
16.219 + int n = _data.size()-1;
16.220 + _iim.set(_data[0].first, POST_HEAP);
16.221 + if (n>0) bubbleDown(0, _data[n], n);
16.222 + _data.pop_back();
16.223 + }
16.224 +
16.225 + /// \brief Remove the given item from the heap.
16.226 + ///
16.227 + /// This function removes the given item from the heap if it is
16.228 + /// already stored.
16.229 + /// \param i The item to delete.
16.230 + /// \pre \e i must be in the heap.
16.231 + void erase(const Item &i) {
16.232 + int h = _iim[i];
16.233 + int n = _data.size()-1;
16.234 + _iim.set(_data[h].first, POST_HEAP);
16.235 + if( h<n ) {
16.236 + if( less(_data[parent(h)], _data[n]) )
16.237 + bubbleDown(h, _data[n], n);
16.238 + else
16.239 + bubbleUp(h, _data[n]);
16.240 + }
16.241 + _data.pop_back();
16.242 + }
16.243 +
16.244 + /// \brief The priority of the given item.
16.245 + ///
16.246 + /// This function returns the priority of the given item.
16.247 + /// \param i The item.
16.248 + /// \pre \e i must be in the heap.
16.249 + Prio operator[](const Item &i) const {
16.250 + int idx = _iim[i];
16.251 + return _data[idx].second;
16.252 + }
16.253 +
16.254 + /// \brief Set the priority of an item or insert it, if it is
16.255 + /// not stored in the heap.
16.256 + ///
16.257 + /// This method sets the priority of the given item if it is
16.258 + /// already stored in the heap. Otherwise it inserts the given
16.259 + /// item into the heap with the given priority.
16.260 + /// \param i The item.
16.261 + /// \param p The priority.
16.262 + void set(const Item &i, const Prio &p) {
16.263 + int idx = _iim[i];
16.264 + if( idx < 0 )
16.265 + push(i,p);
16.266 + else if( _comp(p, _data[idx].second) )
16.267 + bubbleUp(idx, Pair(i,p));
16.268 + else
16.269 + bubbleDown(idx, Pair(i,p), _data.size());
16.270 + }
16.271 +
16.272 + /// \brief Decrease the priority of an item to the given value.
16.273 + ///
16.274 + /// This function decreases the priority of an item to the given value.
16.275 + /// \param i The item.
16.276 + /// \param p The priority.
16.277 + /// \pre \e i must be stored in the heap with priority at least \e p.
16.278 + void decrease(const Item &i, const Prio &p) {
16.279 + int idx = _iim[i];
16.280 + bubbleUp(idx, Pair(i,p));
16.281 + }
16.282 +
16.283 + /// \brief Increase the priority of an item to the given value.
16.284 + ///
16.285 + /// This function increases the priority of an item to the given value.
16.286 + /// \param i The item.
16.287 + /// \param p The priority.
16.288 + /// \pre \e i must be stored in the heap with priority at most \e p.
16.289 + void increase(const Item &i, const Prio &p) {
16.290 + int idx = _iim[i];
16.291 + bubbleDown(idx, Pair(i,p), _data.size());
16.292 + }
16.293 +
16.294 + /// \brief Return the state of an item.
16.295 + ///
16.296 + /// This method returns \c PRE_HEAP if the given item has never
16.297 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
16.298 + /// and \c POST_HEAP otherwise.
16.299 + /// In the latter case it is possible that the item will get back
16.300 + /// to the heap again.
16.301 + /// \param i The item.
16.302 + State state(const Item &i) const {
16.303 + int s = _iim[i];
16.304 + if (s>=0) s=0;
16.305 + return State(s);
16.306 + }
16.307 +
16.308 + /// \brief Set the state of an item in the heap.
16.309 + ///
16.310 + /// This function sets the state of the given item in the heap.
16.311 + /// It can be used to manually clear the heap when it is important
16.312 + /// to achive better time complexity.
16.313 + /// \param i The item.
16.314 + /// \param st The state. It should not be \c IN_HEAP.
16.315 + void state(const Item& i, State st) {
16.316 + switch (st) {
16.317 + case POST_HEAP:
16.318 + case PRE_HEAP:
16.319 + if (state(i) == IN_HEAP) erase(i);
16.320 + _iim[i] = st;
16.321 + break;
16.322 + case IN_HEAP:
16.323 + break;
16.324 + }
16.325 + }
16.326 +
16.327 + /// \brief Replace an item in the heap.
16.328 + ///
16.329 + /// This function replaces item \c i with item \c j.
16.330 + /// Item \c i must be in the heap, while \c j must be out of the heap.
16.331 + /// After calling this method, item \c i will be out of the
16.332 + /// heap and \c j will be in the heap with the same prioriority
16.333 + /// as item \c i had before.
16.334 + void replace(const Item& i, const Item& j) {
16.335 + int idx = _iim[i];
16.336 + _iim.set(i, _iim[j]);
16.337 + _iim.set(j, idx);
16.338 + _data[idx].first = j;
16.339 + }
16.340 +
16.341 + }; // class FouraryHeap
16.342 +
16.343 +} // namespace lemon
16.344 +
16.345 +#endif // LEMON_FOURARY_HEAP_H
17.1 --- a/lemon/gomory_hu.h Wed Sep 30 08:36:43 2009 +0200
17.2 +++ b/lemon/gomory_hu.h Wed Sep 30 08:41:06 2009 +0200
17.3 @@ -359,10 +359,10 @@
17.4 /// This example counts the nodes in the minimum cut separating \c s from
17.5 /// \c t.
17.6 /// \code
17.7 - /// GomoruHu<Graph> gom(g, capacities);
17.8 + /// GomoryHu<Graph> gom(g, capacities);
17.9 /// gom.run();
17.10 /// int cnt=0;
17.11 - /// for(GomoruHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
17.12 + /// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
17.13 /// \endcode
17.14 class MinCutNodeIt
17.15 {
17.16 @@ -456,10 +456,10 @@
17.17 /// This example computes the value of the minimum cut separating \c s from
17.18 /// \c t.
17.19 /// \code
17.20 - /// GomoruHu<Graph> gom(g, capacities);
17.21 + /// GomoryHu<Graph> gom(g, capacities);
17.22 /// gom.run();
17.23 /// int value=0;
17.24 - /// for(GomoruHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
17.25 + /// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
17.26 /// value+=capacities[e];
17.27 /// \endcode
17.28 /// The result will be the same as the value returned by
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/lemon/kary_heap.h Wed Sep 30 08:41:06 2009 +0200
18.3 @@ -0,0 +1,352 @@
18.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
18.5 + *
18.6 + * This file is a part of LEMON, a generic C++ optimization library.
18.7 + *
18.8 + * Copyright (C) 2003-2009
18.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
18.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
18.11 + *
18.12 + * Permission to use, modify and distribute this software is granted
18.13 + * provided that this copyright notice appears in all copies. For
18.14 + * precise terms see the accompanying LICENSE file.
18.15 + *
18.16 + * This software is provided "AS IS" with no warranty of any kind,
18.17 + * express or implied, and with no claim as to its suitability for any
18.18 + * purpose.
18.19 + *
18.20 + */
18.21 +
18.22 +#ifndef LEMON_KARY_HEAP_H
18.23 +#define LEMON_KARY_HEAP_H
18.24 +
18.25 +///\ingroup heaps
18.26 +///\file
18.27 +///\brief Fourary heap implementation.
18.28 +
18.29 +#include <vector>
18.30 +#include <utility>
18.31 +#include <functional>
18.32 +
18.33 +namespace lemon {
18.34 +
18.35 + /// \ingroup heaps
18.36 + ///
18.37 + ///\brief K-ary heap data structure.
18.38 + ///
18.39 + /// This class implements the \e K-ary \e heap data structure.
18.40 + /// It fully conforms to the \ref concepts::Heap "heap concept".
18.41 + ///
18.42 + /// The \ref KaryHeap "K-ary heap" is a generalization of the
18.43 + /// \ref BinHeap "binary heap" structure, its nodes have at most
18.44 + /// \c K children, instead of two.
18.45 + /// \ref BinHeap and \ref FouraryHeap are specialized implementations
18.46 + /// of this structure for <tt>K=2</tt> and <tt>K=4</tt>, respectively.
18.47 + ///
18.48 + /// \tparam PR Type of the priorities of the items.
18.49 + /// \tparam IM A read-writable item map with \c int values, used
18.50 + /// internally to handle the cross references.
18.51 + /// \tparam K The degree of the heap, each node have at most \e K
18.52 + /// children. The default is 16. Powers of two are suggested to use
18.53 + /// so that the multiplications and divisions needed to traverse the
18.54 + /// nodes of the heap could be performed faster.
18.55 + /// \tparam CMP A functor class for comparing the priorities.
18.56 + /// The default is \c std::less<PR>.
18.57 + ///
18.58 + ///\sa BinHeap
18.59 + ///\sa FouraryHeap
18.60 +#ifdef DOXYGEN
18.61 + template <typename PR, typename IM, int K, typename CMP>
18.62 +#else
18.63 + template <typename PR, typename IM, int K = 16,
18.64 + typename CMP = std::less<PR> >
18.65 +#endif
18.66 + class KaryHeap {
18.67 + public:
18.68 + /// Type of the item-int map.
18.69 + typedef IM ItemIntMap;
18.70 + /// Type of the priorities.
18.71 + typedef PR Prio;
18.72 + /// Type of the items stored in the heap.
18.73 + typedef typename ItemIntMap::Key Item;
18.74 + /// Type of the item-priority pairs.
18.75 + typedef std::pair<Item,Prio> Pair;
18.76 + /// Functor type for comparing the priorities.
18.77 + typedef CMP Compare;
18.78 +
18.79 + /// \brief Type to represent the states of the items.
18.80 + ///
18.81 + /// Each item has a state associated to it. It can be "in heap",
18.82 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
18.83 + /// heap's point of view, but may be useful to the user.
18.84 + ///
18.85 + /// The item-int map must be initialized in such way that it assigns
18.86 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
18.87 + enum State {
18.88 + IN_HEAP = 0, ///< = 0.
18.89 + PRE_HEAP = -1, ///< = -1.
18.90 + POST_HEAP = -2 ///< = -2.
18.91 + };
18.92 +
18.93 + private:
18.94 + std::vector<Pair> _data;
18.95 + Compare _comp;
18.96 + ItemIntMap &_iim;
18.97 +
18.98 + public:
18.99 + /// \brief Constructor.
18.100 + ///
18.101 + /// Constructor.
18.102 + /// \param map A map that assigns \c int values to the items.
18.103 + /// It is used internally to handle the cross references.
18.104 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
18.105 + explicit KaryHeap(ItemIntMap &map) : _iim(map) {}
18.106 +
18.107 + /// \brief Constructor.
18.108 + ///
18.109 + /// Constructor.
18.110 + /// \param map A map that assigns \c int values to the items.
18.111 + /// It is used internally to handle the cross references.
18.112 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
18.113 + /// \param comp The function object used for comparing the priorities.
18.114 + KaryHeap(ItemIntMap &map, const Compare &comp)
18.115 + : _iim(map), _comp(comp) {}
18.116 +
18.117 + /// \brief The number of items stored in the heap.
18.118 + ///
18.119 + /// This function returns the number of items stored in the heap.
18.120 + int size() const { return _data.size(); }
18.121 +
18.122 + /// \brief Check if the heap is empty.
18.123 + ///
18.124 + /// This function returns \c true if the heap is empty.
18.125 + bool empty() const { return _data.empty(); }
18.126 +
18.127 + /// \brief Make the heap empty.
18.128 + ///
18.129 + /// This functon makes the heap empty.
18.130 + /// It does not change the cross reference map. If you want to reuse
18.131 + /// a heap that is not surely empty, you should first clear it and
18.132 + /// then you should set the cross reference map to \c PRE_HEAP
18.133 + /// for each item.
18.134 + void clear() { _data.clear(); }
18.135 +
18.136 + private:
18.137 + int parent(int i) { return (i-1)/K; }
18.138 + int firstChild(int i) { return K*i+1; }
18.139 +
18.140 + bool less(const Pair &p1, const Pair &p2) const {
18.141 + return _comp(p1.second, p2.second);
18.142 + }
18.143 +
18.144 + void bubbleUp(int hole, Pair p) {
18.145 + int par = parent(hole);
18.146 + while( hole>0 && less(p,_data[par]) ) {
18.147 + move(_data[par],hole);
18.148 + hole = par;
18.149 + par = parent(hole);
18.150 + }
18.151 + move(p, hole);
18.152 + }
18.153 +
18.154 + void bubbleDown(int hole, Pair p, int length) {
18.155 + if( length>1 ) {
18.156 + int child = firstChild(hole);
18.157 + while( child+K<=length ) {
18.158 + int min=child;
18.159 + for (int i=1; i<K; ++i) {
18.160 + if( less(_data[child+i], _data[min]) )
18.161 + min=child+i;
18.162 + }
18.163 + if( !less(_data[min], p) )
18.164 + goto ok;
18.165 + move(_data[min], hole);
18.166 + hole = min;
18.167 + child = firstChild(hole);
18.168 + }
18.169 + if ( child<length ) {
18.170 + int min = child;
18.171 + while (++child < length) {
18.172 + if( less(_data[child], _data[min]) )
18.173 + min=child;
18.174 + }
18.175 + if( less(_data[min], p) ) {
18.176 + move(_data[min], hole);
18.177 + hole = min;
18.178 + }
18.179 + }
18.180 + }
18.181 + ok:
18.182 + move(p, hole);
18.183 + }
18.184 +
18.185 + void move(const Pair &p, int i) {
18.186 + _data[i] = p;
18.187 + _iim.set(p.first, i);
18.188 + }
18.189 +
18.190 + public:
18.191 + /// \brief Insert a pair of item and priority into the heap.
18.192 + ///
18.193 + /// This function inserts \c p.first to the heap with priority
18.194 + /// \c p.second.
18.195 + /// \param p The pair to insert.
18.196 + /// \pre \c p.first must not be stored in the heap.
18.197 + void push(const Pair &p) {
18.198 + int n = _data.size();
18.199 + _data.resize(n+1);
18.200 + bubbleUp(n, p);
18.201 + }
18.202 +
18.203 + /// \brief Insert an item into the heap with the given priority.
18.204 + ///
18.205 + /// This function inserts the given item into the heap with the
18.206 + /// given priority.
18.207 + /// \param i The item to insert.
18.208 + /// \param p The priority of the item.
18.209 + /// \pre \e i must not be stored in the heap.
18.210 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
18.211 +
18.212 + /// \brief Return the item having minimum priority.
18.213 + ///
18.214 + /// This function returns the item having minimum priority.
18.215 + /// \pre The heap must be non-empty.
18.216 + Item top() const { return _data[0].first; }
18.217 +
18.218 + /// \brief The minimum priority.
18.219 + ///
18.220 + /// This function returns the minimum priority.
18.221 + /// \pre The heap must be non-empty.
18.222 + Prio prio() const { return _data[0].second; }
18.223 +
18.224 + /// \brief Remove the item having minimum priority.
18.225 + ///
18.226 + /// This function removes the item having minimum priority.
18.227 + /// \pre The heap must be non-empty.
18.228 + void pop() {
18.229 + int n = _data.size()-1;
18.230 + _iim.set(_data[0].first, POST_HEAP);
18.231 + if (n>0) bubbleDown(0, _data[n], n);
18.232 + _data.pop_back();
18.233 + }
18.234 +
18.235 + /// \brief Remove the given item from the heap.
18.236 + ///
18.237 + /// This function removes the given item from the heap if it is
18.238 + /// already stored.
18.239 + /// \param i The item to delete.
18.240 + /// \pre \e i must be in the heap.
18.241 + void erase(const Item &i) {
18.242 + int h = _iim[i];
18.243 + int n = _data.size()-1;
18.244 + _iim.set(_data[h].first, POST_HEAP);
18.245 + if( h<n ) {
18.246 + if( less(_data[parent(h)], _data[n]) )
18.247 + bubbleDown(h, _data[n], n);
18.248 + else
18.249 + bubbleUp(h, _data[n]);
18.250 + }
18.251 + _data.pop_back();
18.252 + }
18.253 +
18.254 + /// \brief The priority of the given item.
18.255 + ///
18.256 + /// This function returns the priority of the given item.
18.257 + /// \param i The item.
18.258 + /// \pre \e i must be in the heap.
18.259 + Prio operator[](const Item &i) const {
18.260 + int idx = _iim[i];
18.261 + return _data[idx].second;
18.262 + }
18.263 +
18.264 + /// \brief Set the priority of an item or insert it, if it is
18.265 + /// not stored in the heap.
18.266 + ///
18.267 + /// This method sets the priority of the given item if it is
18.268 + /// already stored in the heap. Otherwise it inserts the given
18.269 + /// item into the heap with the given priority.
18.270 + /// \param i The item.
18.271 + /// \param p The priority.
18.272 + void set(const Item &i, const Prio &p) {
18.273 + int idx = _iim[i];
18.274 + if( idx<0 )
18.275 + push(i,p);
18.276 + else if( _comp(p, _data[idx].second) )
18.277 + bubbleUp(idx, Pair(i,p));
18.278 + else
18.279 + bubbleDown(idx, Pair(i,p), _data.size());
18.280 + }
18.281 +
18.282 + /// \brief Decrease the priority of an item to the given value.
18.283 + ///
18.284 + /// This function decreases the priority of an item to the given value.
18.285 + /// \param i The item.
18.286 + /// \param p The priority.
18.287 + /// \pre \e i must be stored in the heap with priority at least \e p.
18.288 + void decrease(const Item &i, const Prio &p) {
18.289 + int idx = _iim[i];
18.290 + bubbleUp(idx, Pair(i,p));
18.291 + }
18.292 +
18.293 + /// \brief Increase the priority of an item to the given value.
18.294 + ///
18.295 + /// This function increases the priority of an item to the given value.
18.296 + /// \param i The item.
18.297 + /// \param p The priority.
18.298 + /// \pre \e i must be stored in the heap with priority at most \e p.
18.299 + void increase(const Item &i, const Prio &p) {
18.300 + int idx = _iim[i];
18.301 + bubbleDown(idx, Pair(i,p), _data.size());
18.302 + }
18.303 +
18.304 + /// \brief Return the state of an item.
18.305 + ///
18.306 + /// This method returns \c PRE_HEAP if the given item has never
18.307 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
18.308 + /// and \c POST_HEAP otherwise.
18.309 + /// In the latter case it is possible that the item will get back
18.310 + /// to the heap again.
18.311 + /// \param i The item.
18.312 + State state(const Item &i) const {
18.313 + int s = _iim[i];
18.314 + if (s>=0) s=0;
18.315 + return State(s);
18.316 + }
18.317 +
18.318 + /// \brief Set the state of an item in the heap.
18.319 + ///
18.320 + /// This function sets the state of the given item in the heap.
18.321 + /// It can be used to manually clear the heap when it is important
18.322 + /// to achive better time complexity.
18.323 + /// \param i The item.
18.324 + /// \param st The state. It should not be \c IN_HEAP.
18.325 + void state(const Item& i, State st) {
18.326 + switch (st) {
18.327 + case POST_HEAP:
18.328 + case PRE_HEAP:
18.329 + if (state(i) == IN_HEAP) erase(i);
18.330 + _iim[i] = st;
18.331 + break;
18.332 + case IN_HEAP:
18.333 + break;
18.334 + }
18.335 + }
18.336 +
18.337 + /// \brief Replace an item in the heap.
18.338 + ///
18.339 + /// This function replaces item \c i with item \c j.
18.340 + /// Item \c i must be in the heap, while \c j must be out of the heap.
18.341 + /// After calling this method, item \c i will be out of the
18.342 + /// heap and \c j will be in the heap with the same prioriority
18.343 + /// as item \c i had before.
18.344 + void replace(const Item& i, const Item& j) {
18.345 + int idx=_iim[i];
18.346 + _iim.set(i, _iim[j]);
18.347 + _iim.set(j, idx);
18.348 + _data[idx].first=j;
18.349 + }
18.350 +
18.351 + }; // class KaryHeap
18.352 +
18.353 +} // namespace lemon
18.354 +
18.355 +#endif // LEMON_KARY_HEAP_H
19.1 --- a/lemon/maps.h Wed Sep 30 08:36:43 2009 +0200
19.2 +++ b/lemon/maps.h Wed Sep 30 08:41:06 2009 +0200
19.3 @@ -22,6 +22,7 @@
19.4 #include <iterator>
19.5 #include <functional>
19.6 #include <vector>
19.7 +#include <map>
19.8
19.9 #include <lemon/core.h>
19.10
19.11 @@ -29,8 +30,6 @@
19.12 ///\ingroup maps
19.13 ///\brief Miscellaneous property maps
19.14
19.15 -#include <map>
19.16 -
19.17 namespace lemon {
19.18
19.19 /// \addtogroup maps
19.20 @@ -57,7 +56,7 @@
19.21 /// its type definitions, or if you have to provide a writable map,
19.22 /// but data written to it is not required (i.e. it will be sent to
19.23 /// <tt>/dev/null</tt>).
19.24 - /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
19.25 + /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
19.26 ///
19.27 /// \sa ConstMap
19.28 template<typename K, typename V>
19.29 @@ -90,7 +89,7 @@
19.30 /// value to each key.
19.31 ///
19.32 /// In other aspects it is equivalent to \c NullMap.
19.33 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
19.34 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
19.35 /// concept, but it absorbs the data written to it.
19.36 ///
19.37 /// The simplest way of using this map is through the constMap()
19.38 @@ -159,7 +158,7 @@
19.39 /// value to each key.
19.40 ///
19.41 /// In other aspects it is equivalent to \c NullMap.
19.42 - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
19.43 + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
19.44 /// concept, but it absorbs the data written to it.
19.45 ///
19.46 /// The simplest way of using this map is through the constMap()
19.47 @@ -233,7 +232,7 @@
19.48 /// values to integer keys from the range <tt>[0..size-1]</tt>.
19.49 /// It can be used with some data structures, for example
19.50 /// \c UnionFind, \c BinHeap, when the used items are small
19.51 - /// integers. This map conforms the \ref concepts::ReferenceMap
19.52 + /// integers. This map conforms to the \ref concepts::ReferenceMap
19.53 /// "ReferenceMap" concept.
19.54 ///
19.55 /// The simplest way of using this map is through the rangeMap()
19.56 @@ -341,7 +340,7 @@
19.57 /// that you can specify a default value for the keys that are not
19.58 /// stored actually. This value can be different from the default
19.59 /// contructed value (i.e. \c %Value()).
19.60 - /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
19.61 + /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
19.62 /// concept.
19.63 ///
19.64 /// This map is useful if a default value should be assigned to most of
19.65 @@ -707,7 +706,7 @@
19.66 /// "readable map" to another type using the default conversion.
19.67 /// The \c Key type of it is inherited from \c M and the \c Value
19.68 /// type is \c V.
19.69 - /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
19.70 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
19.71 ///
19.72 /// The simplest way of using this map is through the convertMap()
19.73 /// function.
19.74 @@ -1790,11 +1789,11 @@
19.75 /// order of Dfs algorithm, as the following examples show.
19.76 /// \code
19.77 /// std::vector<Node> v;
19.78 - /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
19.79 + /// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
19.80 /// \endcode
19.81 /// \code
19.82 /// std::vector<Node> v(countNodes(g));
19.83 - /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
19.84 + /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
19.85 /// \endcode
19.86 ///
19.87 /// \note The container of the iterator must contain enough space
19.88 @@ -1818,7 +1817,7 @@
19.89 /// \brief Provides an immutable and unique id for each item in a graph.
19.90 ///
19.91 /// IdMap provides a unique and immutable id for each item of the
19.92 - /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
19.93 + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
19.94 /// - \b unique: different items get different ids,
19.95 /// - \b immutable: the id of an item does not change (even if you
19.96 /// delete other nodes).
19.97 @@ -1826,7 +1825,7 @@
19.98 /// Using this map you get access (i.e. can read) the inner id values of
19.99 /// the items stored in the graph, which is returned by the \c id()
19.100 /// function of the graph. This map can be inverted with its member
19.101 - /// class \c InverseMap or with the \c operator() member.
19.102 + /// class \c InverseMap or with the \c operator()() member.
19.103 ///
19.104 /// \tparam GR The graph type.
19.105 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
19.106 @@ -1866,9 +1865,11 @@
19.107
19.108 public:
19.109
19.110 - /// \brief This class represents the inverse of its owner (IdMap).
19.111 + /// \brief The inverse map type of IdMap.
19.112 ///
19.113 - /// This class represents the inverse of its owner (IdMap).
19.114 + /// The inverse map type of IdMap. The subscript operator gives back
19.115 + /// an item by its id.
19.116 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
19.117 /// \see inverse()
19.118 class InverseMap {
19.119 public:
19.120 @@ -1883,9 +1884,9 @@
19.121 /// Constructor for creating an id-to-item map.
19.122 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
19.123
19.124 - /// \brief Gives back the given item from its id.
19.125 + /// \brief Gives back an item by its id.
19.126 ///
19.127 - /// Gives back the given item from its id.
19.128 + /// Gives back an item by its id.
19.129 Item operator[](int id) const { return _graph->fromId(id, Item());}
19.130
19.131 private:
19.132 @@ -1898,14 +1899,31 @@
19.133 InverseMap inverse() const { return InverseMap(*_graph);}
19.134 };
19.135
19.136 + /// \brief Returns an \c IdMap class.
19.137 + ///
19.138 + /// This function just returns an \c IdMap class.
19.139 + /// \relates IdMap
19.140 + template <typename K, typename GR>
19.141 + inline IdMap<GR, K> idMap(const GR& graph) {
19.142 + return IdMap<GR, K>(graph);
19.143 + }
19.144
19.145 /// \brief General cross reference graph map type.
19.146
19.147 /// This class provides simple invertable graph maps.
19.148 /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
19.149 /// and if a key is set to a new value, then stores it in the inverse map.
19.150 - /// The values of the map can be accessed
19.151 - /// with stl compatible forward iterator.
19.152 + /// The graph items can be accessed by their values either using
19.153 + /// \c InverseMap or \c operator()(), and the values of the map can be
19.154 + /// accessed with an STL compatible forward iterator (\c ValueIt).
19.155 + ///
19.156 + /// This map is intended to be used when all associated values are
19.157 + /// different (the map is actually invertable) or there are only a few
19.158 + /// items with the same value.
19.159 + /// Otherwise consider to use \c IterableValueMap, which is more
19.160 + /// suitable and more efficient for such cases. It provides iterators
19.161 + /// to traverse the items with the same associated value, however
19.162 + /// it does not have \c InverseMap.
19.163 ///
19.164 /// This type is not reference map, so it cannot be modified with
19.165 /// the subscript operator.
19.166 @@ -1946,56 +1964,66 @@
19.167
19.168 /// \brief Forward iterator for values.
19.169 ///
19.170 - /// This iterator is an stl compatible forward
19.171 + /// This iterator is an STL compatible forward
19.172 /// iterator on the values of the map. The values can
19.173 /// be accessed in the <tt>[beginValue, endValue)</tt> range.
19.174 /// They are considered with multiplicity, so each value is
19.175 /// traversed for each item it is assigned to.
19.176 - class ValueIterator
19.177 + class ValueIt
19.178 : public std::iterator<std::forward_iterator_tag, Value> {
19.179 friend class CrossRefMap;
19.180 private:
19.181 - ValueIterator(typename Container::const_iterator _it)
19.182 + ValueIt(typename Container::const_iterator _it)
19.183 : it(_it) {}
19.184 public:
19.185
19.186 - ValueIterator() {}
19.187 -
19.188 - ValueIterator& operator++() { ++it; return *this; }
19.189 - ValueIterator operator++(int) {
19.190 - ValueIterator tmp(*this);
19.191 + /// Constructor
19.192 + ValueIt() {}
19.193 +
19.194 + /// \e
19.195 + ValueIt& operator++() { ++it; return *this; }
19.196 + /// \e
19.197 + ValueIt operator++(int) {
19.198 + ValueIt tmp(*this);
19.199 operator++();
19.200 return tmp;
19.201 }
19.202
19.203 + /// \e
19.204 const Value& operator*() const { return it->first; }
19.205 + /// \e
19.206 const Value* operator->() const { return &(it->first); }
19.207
19.208 - bool operator==(ValueIterator jt) const { return it == jt.it; }
19.209 - bool operator!=(ValueIterator jt) const { return it != jt.it; }
19.210 + /// \e
19.211 + bool operator==(ValueIt jt) const { return it == jt.it; }
19.212 + /// \e
19.213 + bool operator!=(ValueIt jt) const { return it != jt.it; }
19.214
19.215 private:
19.216 typename Container::const_iterator it;
19.217 };
19.218 +
19.219 + /// Alias for \c ValueIt
19.220 + typedef ValueIt ValueIterator;
19.221
19.222 /// \brief Returns an iterator to the first value.
19.223 ///
19.224 - /// Returns an stl compatible iterator to the
19.225 + /// Returns an STL compatible iterator to the
19.226 /// first value of the map. The values of the
19.227 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
19.228 /// range.
19.229 - ValueIterator beginValue() const {
19.230 - return ValueIterator(_inv_map.begin());
19.231 + ValueIt beginValue() const {
19.232 + return ValueIt(_inv_map.begin());
19.233 }
19.234
19.235 /// \brief Returns an iterator after the last value.
19.236 ///
19.237 - /// Returns an stl compatible iterator after the
19.238 + /// Returns an STL compatible iterator after the
19.239 /// last value of the map. The values of the
19.240 /// map can be accessed in the <tt>[beginValue, endValue)</tt>
19.241 /// range.
19.242 - ValueIterator endValue() const {
19.243 - return ValueIterator(_inv_map.end());
19.244 + ValueIt endValue() const {
19.245 + return ValueIt(_inv_map.end());
19.246 }
19.247
19.248 /// \brief Sets the value associated with the given key.
19.249 @@ -2033,6 +2061,14 @@
19.250 typename Container::const_iterator it = _inv_map.find(val);
19.251 return it != _inv_map.end() ? it->second : INVALID;
19.252 }
19.253 +
19.254 + /// \brief Returns the number of items with the given value.
19.255 + ///
19.256 + /// This function returns the number of items with the given value
19.257 + /// associated with it.
19.258 + int count(const Value &val) const {
19.259 + return _inv_map.count(val);
19.260 + }
19.261
19.262 protected:
19.263
19.264 @@ -2083,10 +2119,12 @@
19.265
19.266 public:
19.267
19.268 - /// \brief The inverse map type.
19.269 + /// \brief The inverse map type of CrossRefMap.
19.270 ///
19.271 - /// The inverse of this map. The subscript operator of the map
19.272 - /// gives back the item that was last assigned to the value.
19.273 + /// The inverse map type of CrossRefMap. The subscript operator gives
19.274 + /// back an item by its value.
19.275 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
19.276 + /// \see inverse()
19.277 class InverseMap {
19.278 public:
19.279 /// \brief Constructor
19.280 @@ -2113,20 +2151,20 @@
19.281 const CrossRefMap& _inverted;
19.282 };
19.283
19.284 - /// \brief It gives back the read-only inverse map.
19.285 + /// \brief Gives back the inverse of the map.
19.286 ///
19.287 - /// It gives back the read-only inverse map.
19.288 + /// Gives back the inverse of the CrossRefMap.
19.289 InverseMap inverse() const {
19.290 return InverseMap(*this);
19.291 }
19.292
19.293 };
19.294
19.295 - /// \brief Provides continuous and unique ID for the
19.296 + /// \brief Provides continuous and unique id for the
19.297 /// items of a graph.
19.298 ///
19.299 /// RangeIdMap provides a unique and continuous
19.300 - /// ID for each item of a given type (\c Node, \c Arc or
19.301 + /// id for each item of a given type (\c Node, \c Arc or
19.302 /// \c Edge) in a graph. This id is
19.303 /// - \b unique: different items get different ids,
19.304 /// - \b continuous: the range of the ids is the set of integers
19.305 @@ -2137,7 +2175,7 @@
19.306 /// Thus this id is not (necessarily) the same as what can get using
19.307 /// the \c id() function of the graph or \ref IdMap.
19.308 /// This map can be inverted with its member class \c InverseMap,
19.309 - /// or with the \c operator() member.
19.310 + /// or with the \c operator()() member.
19.311 ///
19.312 /// \tparam GR The graph type.
19.313 /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
19.314 @@ -2265,16 +2303,16 @@
19.315 _inv_map[pi] = q;
19.316 }
19.317
19.318 - /// \brief Gives back the \e RangeId of the item
19.319 + /// \brief Gives back the \e range \e id of the item
19.320 ///
19.321 - /// Gives back the \e RangeId of the item.
19.322 + /// Gives back the \e range \e id of the item.
19.323 int operator[](const Item& item) const {
19.324 return Map::operator[](item);
19.325 }
19.326
19.327 - /// \brief Gives back the item belonging to a \e RangeId
19.328 - ///
19.329 - /// Gives back the item belonging to a \e RangeId.
19.330 + /// \brief Gives back the item belonging to a \e range \e id
19.331 + ///
19.332 + /// Gives back the item belonging to the given \e range \e id.
19.333 Item operator()(int id) const {
19.334 return _inv_map[id];
19.335 }
19.336 @@ -2288,7 +2326,9 @@
19.337
19.338 /// \brief The inverse map type of RangeIdMap.
19.339 ///
19.340 - /// The inverse map type of RangeIdMap.
19.341 + /// The inverse map type of RangeIdMap. The subscript operator gives
19.342 + /// back an item by its \e range \e id.
19.343 + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
19.344 class InverseMap {
19.345 public:
19.346 /// \brief Constructor
19.347 @@ -2306,7 +2346,7 @@
19.348 /// \brief Subscript operator.
19.349 ///
19.350 /// Subscript operator. It gives back the item
19.351 - /// that the descriptor currently belongs to.
19.352 + /// that the given \e range \e id currently belongs to.
19.353 Value operator[](const Key& key) const {
19.354 return _inverted(key);
19.355 }
19.356 @@ -2324,12 +2364,932 @@
19.357
19.358 /// \brief Gives back the inverse of the map.
19.359 ///
19.360 - /// Gives back the inverse of the map.
19.361 + /// Gives back the inverse of the RangeIdMap.
19.362 const InverseMap inverse() const {
19.363 return InverseMap(*this);
19.364 }
19.365 };
19.366
19.367 + /// \brief Returns a \c RangeIdMap class.
19.368 + ///
19.369 + /// This function just returns an \c RangeIdMap class.
19.370 + /// \relates RangeIdMap
19.371 + template <typename K, typename GR>
19.372 + inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
19.373 + return RangeIdMap<GR, K>(graph);
19.374 + }
19.375 +
19.376 + /// \brief Dynamic iterable \c bool map.
19.377 + ///
19.378 + /// This class provides a special graph map type which can store a
19.379 + /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
19.380 + /// For both \c true and \c false values it is possible to iterate on
19.381 + /// the keys mapped to the value.
19.382 + ///
19.383 + /// This type is a reference map, so it can be modified with the
19.384 + /// subscript operator.
19.385 + ///
19.386 + /// \tparam GR The graph type.
19.387 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
19.388 + /// \c GR::Edge).
19.389 + ///
19.390 + /// \see IterableIntMap, IterableValueMap
19.391 + /// \see CrossRefMap
19.392 + template <typename GR, typename K>
19.393 + class IterableBoolMap
19.394 + : protected ItemSetTraits<GR, K>::template Map<int>::Type {
19.395 + private:
19.396 + typedef GR Graph;
19.397 +
19.398 + typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
19.399 + typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
19.400 +
19.401 + std::vector<K> _array;
19.402 + int _sep;
19.403 +
19.404 + public:
19.405 +
19.406 + /// Indicates that the map is reference map.
19.407 + typedef True ReferenceMapTag;
19.408 +
19.409 + /// The key type
19.410 + typedef K Key;
19.411 + /// The value type
19.412 + typedef bool Value;
19.413 + /// The const reference type.
19.414 + typedef const Value& ConstReference;
19.415 +
19.416 + private:
19.417 +
19.418 + int position(const Key& key) const {
19.419 + return Parent::operator[](key);
19.420 + }
19.421 +
19.422 + public:
19.423 +
19.424 + /// \brief Reference to the value of the map.
19.425 + ///
19.426 + /// This class is similar to the \c bool type. It can be converted to
19.427 + /// \c bool and it provides the same operators.
19.428 + class Reference {
19.429 + friend class IterableBoolMap;
19.430 + private:
19.431 + Reference(IterableBoolMap& map, const Key& key)
19.432 + : _key(key), _map(map) {}
19.433 + public:
19.434 +
19.435 + Reference& operator=(const Reference& value) {
19.436 + _map.set(_key, static_cast<bool>(value));
19.437 + return *this;
19.438 + }
19.439 +
19.440 + operator bool() const {
19.441 + return static_cast<const IterableBoolMap&>(_map)[_key];
19.442 + }
19.443 +
19.444 + Reference& operator=(bool value) {
19.445 + _map.set(_key, value);
19.446 + return *this;
19.447 + }
19.448 + Reference& operator&=(bool value) {
19.449 + _map.set(_key, _map[_key] & value);
19.450 + return *this;
19.451 + }
19.452 + Reference& operator|=(bool value) {
19.453 + _map.set(_key, _map[_key] | value);
19.454 + return *this;
19.455 + }
19.456 + Reference& operator^=(bool value) {
19.457 + _map.set(_key, _map[_key] ^ value);
19.458 + return *this;
19.459 + }
19.460 + private:
19.461 + Key _key;
19.462 + IterableBoolMap& _map;
19.463 + };
19.464 +
19.465 + /// \brief Constructor of the map with a default value.
19.466 + ///
19.467 + /// Constructor of the map with a default value.
19.468 + explicit IterableBoolMap(const Graph& graph, bool def = false)
19.469 + : Parent(graph) {
19.470 + typename Parent::Notifier* nf = Parent::notifier();
19.471 + Key it;
19.472 + for (nf->first(it); it != INVALID; nf->next(it)) {
19.473 + Parent::set(it, _array.size());
19.474 + _array.push_back(it);
19.475 + }
19.476 + _sep = (def ? _array.size() : 0);
19.477 + }
19.478 +
19.479 + /// \brief Const subscript operator of the map.
19.480 + ///
19.481 + /// Const subscript operator of the map.
19.482 + bool operator[](const Key& key) const {
19.483 + return position(key) < _sep;
19.484 + }
19.485 +
19.486 + /// \brief Subscript operator of the map.
19.487 + ///
19.488 + /// Subscript operator of the map.
19.489 + Reference operator[](const Key& key) {
19.490 + return Reference(*this, key);
19.491 + }
19.492 +
19.493 + /// \brief Set operation of the map.
19.494 + ///
19.495 + /// Set operation of the map.
19.496 + void set(const Key& key, bool value) {
19.497 + int pos = position(key);
19.498 + if (value) {
19.499 + if (pos < _sep) return;
19.500 + Key tmp = _array[_sep];
19.501 + _array[_sep] = key;
19.502 + Parent::set(key, _sep);
19.503 + _array[pos] = tmp;
19.504 + Parent::set(tmp, pos);
19.505 + ++_sep;
19.506 + } else {
19.507 + if (pos >= _sep) return;
19.508 + --_sep;
19.509 + Key tmp = _array[_sep];
19.510 + _array[_sep] = key;
19.511 + Parent::set(key, _sep);
19.512 + _array[pos] = tmp;
19.513 + Parent::set(tmp, pos);
19.514 + }
19.515 + }
19.516 +
19.517 + /// \brief Set all items.
19.518 + ///
19.519 + /// Set all items in the map.
19.520 + /// \note Constant time operation.
19.521 + void setAll(bool value) {
19.522 + _sep = (value ? _array.size() : 0);
19.523 + }
19.524 +
19.525 + /// \brief Returns the number of the keys mapped to \c true.
19.526 + ///
19.527 + /// Returns the number of the keys mapped to \c true.
19.528 + int trueNum() const {
19.529 + return _sep;
19.530 + }
19.531 +
19.532 + /// \brief Returns the number of the keys mapped to \c false.
19.533 + ///
19.534 + /// Returns the number of the keys mapped to \c false.
19.535 + int falseNum() const {
19.536 + return _array.size() - _sep;
19.537 + }
19.538 +
19.539 + /// \brief Iterator for the keys mapped to \c true.
19.540 + ///
19.541 + /// Iterator for the keys mapped to \c true. It works
19.542 + /// like a graph item iterator, it can be converted to
19.543 + /// the key type of the map, incremented with \c ++ operator, and
19.544 + /// if the iterator leaves the last valid key, it will be equal to
19.545 + /// \c INVALID.
19.546 + class TrueIt : public Key {
19.547 + public:
19.548 + typedef Key Parent;
19.549 +
19.550 + /// \brief Creates an iterator.
19.551 + ///
19.552 + /// Creates an iterator. It iterates on the
19.553 + /// keys mapped to \c true.
19.554 + /// \param map The IterableBoolMap.
19.555 + explicit TrueIt(const IterableBoolMap& map)
19.556 + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
19.557 + _map(&map) {}
19.558 +
19.559 + /// \brief Invalid constructor \& conversion.
19.560 + ///
19.561 + /// This constructor initializes the iterator to be invalid.
19.562 + /// \sa Invalid for more details.
19.563 + TrueIt(Invalid) : Parent(INVALID), _map(0) {}
19.564 +
19.565 + /// \brief Increment operator.
19.566 + ///
19.567 + /// Increment operator.
19.568 + TrueIt& operator++() {
19.569 + int pos = _map->position(*this);
19.570 + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
19.571 + return *this;
19.572 + }
19.573 +
19.574 + private:
19.575 + const IterableBoolMap* _map;
19.576 + };
19.577 +
19.578 + /// \brief Iterator for the keys mapped to \c false.
19.579 + ///
19.580 + /// Iterator for the keys mapped to \c false. It works
19.581 + /// like a graph item iterator, it can be converted to
19.582 + /// the key type of the map, incremented with \c ++ operator, and
19.583 + /// if the iterator leaves the last valid key, it will be equal to
19.584 + /// \c INVALID.
19.585 + class FalseIt : public Key {
19.586 + public:
19.587 + typedef Key Parent;
19.588 +
19.589 + /// \brief Creates an iterator.
19.590 + ///
19.591 + /// Creates an iterator. It iterates on the
19.592 + /// keys mapped to \c false.
19.593 + /// \param map The IterableBoolMap.
19.594 + explicit FalseIt(const IterableBoolMap& map)
19.595 + : Parent(map._sep < int(map._array.size()) ?
19.596 + map._array.back() : INVALID), _map(&map) {}
19.597 +
19.598 + /// \brief Invalid constructor \& conversion.
19.599 + ///
19.600 + /// This constructor initializes the iterator to be invalid.
19.601 + /// \sa Invalid for more details.
19.602 + FalseIt(Invalid) : Parent(INVALID), _map(0) {}
19.603 +
19.604 + /// \brief Increment operator.
19.605 + ///
19.606 + /// Increment operator.
19.607 + FalseIt& operator++() {
19.608 + int pos = _map->position(*this);
19.609 + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
19.610 + return *this;
19.611 + }
19.612 +
19.613 + private:
19.614 + const IterableBoolMap* _map;
19.615 + };
19.616 +
19.617 + /// \brief Iterator for the keys mapped to a given value.
19.618 + ///
19.619 + /// Iterator for the keys mapped to a given value. It works
19.620 + /// like a graph item iterator, it can be converted to
19.621 + /// the key type of the map, incremented with \c ++ operator, and
19.622 + /// if the iterator leaves the last valid key, it will be equal to
19.623 + /// \c INVALID.
19.624 + class ItemIt : public Key {
19.625 + public:
19.626 + typedef Key Parent;
19.627 +
19.628 + /// \brief Creates an iterator with a value.
19.629 + ///
19.630 + /// Creates an iterator with a value. It iterates on the
19.631 + /// keys mapped to the given value.
19.632 + /// \param map The IterableBoolMap.
19.633 + /// \param value The value.
19.634 + ItemIt(const IterableBoolMap& map, bool value)
19.635 + : Parent(value ?
19.636 + (map._sep > 0 ?
19.637 + map._array[map._sep - 1] : INVALID) :
19.638 + (map._sep < int(map._array.size()) ?
19.639 + map._array.back() : INVALID)), _map(&map) {}
19.640 +
19.641 + /// \brief Invalid constructor \& conversion.
19.642 + ///
19.643 + /// This constructor initializes the iterator to be invalid.
19.644 + /// \sa Invalid for more details.
19.645 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
19.646 +
19.647 + /// \brief Increment operator.
19.648 + ///
19.649 + /// Increment operator.
19.650 + ItemIt& operator++() {
19.651 + int pos = _map->position(*this);
19.652 + int _sep = pos >= _map->_sep ? _map->_sep : 0;
19.653 + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
19.654 + return *this;
19.655 + }
19.656 +
19.657 + private:
19.658 + const IterableBoolMap* _map;
19.659 + };
19.660 +
19.661 + protected:
19.662 +
19.663 + virtual void add(const Key& key) {
19.664 + Parent::add(key);
19.665 + Parent::set(key, _array.size());
19.666 + _array.push_back(key);
19.667 + }
19.668 +
19.669 + virtual void add(const std::vector<Key>& keys) {
19.670 + Parent::add(keys);
19.671 + for (int i = 0; i < int(keys.size()); ++i) {
19.672 + Parent::set(keys[i], _array.size());
19.673 + _array.push_back(keys[i]);
19.674 + }
19.675 + }
19.676 +
19.677 + virtual void erase(const Key& key) {
19.678 + int pos = position(key);
19.679 + if (pos < _sep) {
19.680 + --_sep;
19.681 + Parent::set(_array[_sep], pos);
19.682 + _array[pos] = _array[_sep];
19.683 + Parent::set(_array.back(), _sep);
19.684 + _array[_sep] = _array.back();
19.685 + _array.pop_back();
19.686 + } else {
19.687 + Parent::set(_array.back(), pos);
19.688 + _array[pos] = _array.back();
19.689 + _array.pop_back();
19.690 + }
19.691 + Parent::erase(key);
19.692 + }
19.693 +
19.694 + virtual void erase(const std::vector<Key>& keys) {
19.695 + for (int i = 0; i < int(keys.size()); ++i) {
19.696 + int pos = position(keys[i]);
19.697 + if (pos < _sep) {
19.698 + --_sep;
19.699 + Parent::set(_array[_sep], pos);
19.700 + _array[pos] = _array[_sep];
19.701 + Parent::set(_array.back(), _sep);
19.702 + _array[_sep] = _array.back();
19.703 + _array.pop_back();
19.704 + } else {
19.705 + Parent::set(_array.back(), pos);
19.706 + _array[pos] = _array.back();
19.707 + _array.pop_back();
19.708 + }
19.709 + }
19.710 + Parent::erase(keys);
19.711 + }
19.712 +
19.713 + virtual void build() {
19.714 + Parent::build();
19.715 + typename Parent::Notifier* nf = Parent::notifier();
19.716 + Key it;
19.717 + for (nf->first(it); it != INVALID; nf->next(it)) {
19.718 + Parent::set(it, _array.size());
19.719 + _array.push_back(it);
19.720 + }
19.721 + _sep = 0;
19.722 + }
19.723 +
19.724 + virtual void clear() {
19.725 + _array.clear();
19.726 + _sep = 0;
19.727 + Parent::clear();
19.728 + }
19.729 +
19.730 + };
19.731 +
19.732 +
19.733 + namespace _maps_bits {
19.734 + template <typename Item>
19.735 + struct IterableIntMapNode {
19.736 + IterableIntMapNode() : value(-1) {}
19.737 + IterableIntMapNode(int _value) : value(_value) {}
19.738 + Item prev, next;
19.739 + int value;
19.740 + };
19.741 + }
19.742 +
19.743 + /// \brief Dynamic iterable integer map.
19.744 + ///
19.745 + /// This class provides a special graph map type which can store an
19.746 + /// integer value for graph items (\c Node, \c Arc or \c Edge).
19.747 + /// For each non-negative value it is possible to iterate on the keys
19.748 + /// mapped to the value.
19.749 + ///
19.750 + /// This map is intended to be used with small integer values, for which
19.751 + /// it is efficient, and supports iteration only for non-negative values.
19.752 + /// If you need large values and/or iteration for negative integers,
19.753 + /// consider to use \ref IterableValueMap instead.
19.754 + ///
19.755 + /// This type is a reference map, so it can be modified with the
19.756 + /// subscript operator.
19.757 + ///
19.758 + /// \note The size of the data structure depends on the largest
19.759 + /// value in the map.
19.760 + ///
19.761 + /// \tparam GR The graph type.
19.762 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
19.763 + /// \c GR::Edge).
19.764 + ///
19.765 + /// \see IterableBoolMap, IterableValueMap
19.766 + /// \see CrossRefMap
19.767 + template <typename GR, typename K>
19.768 + class IterableIntMap
19.769 + : protected ItemSetTraits<GR, K>::
19.770 + template Map<_maps_bits::IterableIntMapNode<K> >::Type {
19.771 + public:
19.772 + typedef typename ItemSetTraits<GR, K>::
19.773 + template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
19.774 +
19.775 + /// The key type
19.776 + typedef K Key;
19.777 + /// The value type
19.778 + typedef int Value;
19.779 + /// The graph type
19.780 + typedef GR Graph;
19.781 +
19.782 + /// \brief Constructor of the map.
19.783 + ///
19.784 + /// Constructor of the map. It sets all values to -1.
19.785 + explicit IterableIntMap(const Graph& graph)
19.786 + : Parent(graph) {}
19.787 +
19.788 + /// \brief Constructor of the map with a given value.
19.789 + ///
19.790 + /// Constructor of the map with a given value.
19.791 + explicit IterableIntMap(const Graph& graph, int value)
19.792 + : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
19.793 + if (value >= 0) {
19.794 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
19.795 + lace(it);
19.796 + }
19.797 + }
19.798 + }
19.799 +
19.800 + private:
19.801 +
19.802 + void unlace(const Key& key) {
19.803 + typename Parent::Value& node = Parent::operator[](key);
19.804 + if (node.value < 0) return;
19.805 + if (node.prev != INVALID) {
19.806 + Parent::operator[](node.prev).next = node.next;
19.807 + } else {
19.808 + _first[node.value] = node.next;
19.809 + }
19.810 + if (node.next != INVALID) {
19.811 + Parent::operator[](node.next).prev = node.prev;
19.812 + }
19.813 + while (!_first.empty() && _first.back() == INVALID) {
19.814 + _first.pop_back();
19.815 + }
19.816 + }
19.817 +
19.818 + void lace(const Key& key) {
19.819 + typename Parent::Value& node = Parent::operator[](key);
19.820 + if (node.value < 0) return;
19.821 + if (node.value >= int(_first.size())) {
19.822 + _first.resize(node.value + 1, INVALID);
19.823 + }
19.824 + node.prev = INVALID;
19.825 + node.next = _first[node.value];
19.826 + if (node.next != INVALID) {
19.827 + Parent::operator[](node.next).prev = key;
19.828 + }
19.829 + _first[node.value] = key;
19.830 + }
19.831 +
19.832 + public:
19.833 +
19.834 + /// Indicates that the map is reference map.
19.835 + typedef True ReferenceMapTag;
19.836 +
19.837 + /// \brief Reference to the value of the map.
19.838 + ///
19.839 + /// This class is similar to the \c int type. It can
19.840 + /// be converted to \c int and it has the same operators.
19.841 + class Reference {
19.842 + friend class IterableIntMap;
19.843 + private:
19.844 + Reference(IterableIntMap& map, const Key& key)
19.845 + : _key(key), _map(map) {}
19.846 + public:
19.847 +
19.848 + Reference& operator=(const Reference& value) {
19.849 + _map.set(_key, static_cast<const int&>(value));
19.850 + return *this;
19.851 + }
19.852 +
19.853 + operator const int&() const {
19.854 + return static_cast<const IterableIntMap&>(_map)[_key];
19.855 + }
19.856 +
19.857 + Reference& operator=(int value) {
19.858 + _map.set(_key, value);
19.859 + return *this;
19.860 + }
19.861 + Reference& operator++() {
19.862 + _map.set(_key, _map[_key] + 1);
19.863 + return *this;
19.864 + }
19.865 + int operator++(int) {
19.866 + int value = _map[_key];
19.867 + _map.set(_key, value + 1);
19.868 + return value;
19.869 + }
19.870 + Reference& operator--() {
19.871 + _map.set(_key, _map[_key] - 1);
19.872 + return *this;
19.873 + }
19.874 + int operator--(int) {
19.875 + int value = _map[_key];
19.876 + _map.set(_key, value - 1);
19.877 + return value;
19.878 + }
19.879 + Reference& operator+=(int value) {
19.880 + _map.set(_key, _map[_key] + value);
19.881 + return *this;
19.882 + }
19.883 + Reference& operator-=(int value) {
19.884 + _map.set(_key, _map[_key] - value);
19.885 + return *this;
19.886 + }
19.887 + Reference& operator*=(int value) {
19.888 + _map.set(_key, _map[_key] * value);
19.889 + return *this;
19.890 + }
19.891 + Reference& operator/=(int value) {
19.892 + _map.set(_key, _map[_key] / value);
19.893 + return *this;
19.894 + }
19.895 + Reference& operator%=(int value) {
19.896 + _map.set(_key, _map[_key] % value);
19.897 + return *this;
19.898 + }
19.899 + Reference& operator&=(int value) {
19.900 + _map.set(_key, _map[_key] & value);
19.901 + return *this;
19.902 + }
19.903 + Reference& operator|=(int value) {
19.904 + _map.set(_key, _map[_key] | value);
19.905 + return *this;
19.906 + }
19.907 + Reference& operator^=(int value) {
19.908 + _map.set(_key, _map[_key] ^ value);
19.909 + return *this;
19.910 + }
19.911 + Reference& operator<<=(int value) {
19.912 + _map.set(_key, _map[_key] << value);
19.913 + return *this;
19.914 + }
19.915 + Reference& operator>>=(int value) {
19.916 + _map.set(_key, _map[_key] >> value);
19.917 + return *this;
19.918 + }
19.919 +
19.920 + private:
19.921 + Key _key;
19.922 + IterableIntMap& _map;
19.923 + };
19.924 +
19.925 + /// The const reference type.
19.926 + typedef const Value& ConstReference;
19.927 +
19.928 + /// \brief Gives back the maximal value plus one.
19.929 + ///
19.930 + /// Gives back the maximal value plus one.
19.931 + int size() const {
19.932 + return _first.size();
19.933 + }
19.934 +
19.935 + /// \brief Set operation of the map.
19.936 + ///
19.937 + /// Set operation of the map.
19.938 + void set(const Key& key, const Value& value) {
19.939 + unlace(key);
19.940 + Parent::operator[](key).value = value;
19.941 + lace(key);
19.942 + }
19.943 +
19.944 + /// \brief Const subscript operator of the map.
19.945 + ///
19.946 + /// Const subscript operator of the map.
19.947 + const Value& operator[](const Key& key) const {
19.948 + return Parent::operator[](key).value;
19.949 + }
19.950 +
19.951 + /// \brief Subscript operator of the map.
19.952 + ///
19.953 + /// Subscript operator of the map.
19.954 + Reference operator[](const Key& key) {
19.955 + return Reference(*this, key);
19.956 + }
19.957 +
19.958 + /// \brief Iterator for the keys with the same value.
19.959 + ///
19.960 + /// Iterator for the keys with the same value. It works
19.961 + /// like a graph item iterator, it can be converted to
19.962 + /// the item type of the map, incremented with \c ++ operator, and
19.963 + /// if the iterator leaves the last valid item, it will be equal to
19.964 + /// \c INVALID.
19.965 + class ItemIt : public Key {
19.966 + public:
19.967 + typedef Key Parent;
19.968 +
19.969 + /// \brief Invalid constructor \& conversion.
19.970 + ///
19.971 + /// This constructor initializes the iterator to be invalid.
19.972 + /// \sa Invalid for more details.
19.973 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
19.974 +
19.975 + /// \brief Creates an iterator with a value.
19.976 + ///
19.977 + /// Creates an iterator with a value. It iterates on the
19.978 + /// keys mapped to the given value.
19.979 + /// \param map The IterableIntMap.
19.980 + /// \param value The value.
19.981 + ItemIt(const IterableIntMap& map, int value) : _map(&map) {
19.982 + if (value < 0 || value >= int(_map->_first.size())) {
19.983 + Parent::operator=(INVALID);
19.984 + } else {
19.985 + Parent::operator=(_map->_first[value]);
19.986 + }
19.987 + }
19.988 +
19.989 + /// \brief Increment operator.
19.990 + ///
19.991 + /// Increment operator.
19.992 + ItemIt& operator++() {
19.993 + Parent::operator=(_map->IterableIntMap::Parent::
19.994 + operator[](static_cast<Parent&>(*this)).next);
19.995 + return *this;
19.996 + }
19.997 +
19.998 + private:
19.999 + const IterableIntMap* _map;
19.1000 + };
19.1001 +
19.1002 + protected:
19.1003 +
19.1004 + virtual void erase(const Key& key) {
19.1005 + unlace(key);
19.1006 + Parent::erase(key);
19.1007 + }
19.1008 +
19.1009 + virtual void erase(const std::vector<Key>& keys) {
19.1010 + for (int i = 0; i < int(keys.size()); ++i) {
19.1011 + unlace(keys[i]);
19.1012 + }
19.1013 + Parent::erase(keys);
19.1014 + }
19.1015 +
19.1016 + virtual void clear() {
19.1017 + _first.clear();
19.1018 + Parent::clear();
19.1019 + }
19.1020 +
19.1021 + private:
19.1022 + std::vector<Key> _first;
19.1023 + };
19.1024 +
19.1025 + namespace _maps_bits {
19.1026 + template <typename Item, typename Value>
19.1027 + struct IterableValueMapNode {
19.1028 + IterableValueMapNode(Value _value = Value()) : value(_value) {}
19.1029 + Item prev, next;
19.1030 + Value value;
19.1031 + };
19.1032 + }
19.1033 +
19.1034 + /// \brief Dynamic iterable map for comparable values.
19.1035 + ///
19.1036 + /// This class provides a special graph map type which can store a
19.1037 + /// comparable value for graph items (\c Node, \c Arc or \c Edge).
19.1038 + /// For each value it is possible to iterate on the keys mapped to
19.1039 + /// the value (\c ItemIt), and the values of the map can be accessed
19.1040 + /// with an STL compatible forward iterator (\c ValueIt).
19.1041 + /// The map stores a linked list for each value, which contains
19.1042 + /// the items mapped to the value, and the used values are stored
19.1043 + /// in balanced binary tree (\c std::map).
19.1044 + ///
19.1045 + /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
19.1046 + /// specialized for \c bool and \c int values, respectively.
19.1047 + ///
19.1048 + /// This type is not reference map, so it cannot be modified with
19.1049 + /// the subscript operator.
19.1050 + ///
19.1051 + /// \tparam GR The graph type.
19.1052 + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
19.1053 + /// \c GR::Edge).
19.1054 + /// \tparam V The value type of the map. It can be any comparable
19.1055 + /// value type.
19.1056 + ///
19.1057 + /// \see IterableBoolMap, IterableIntMap
19.1058 + /// \see CrossRefMap
19.1059 + template <typename GR, typename K, typename V>
19.1060 + class IterableValueMap
19.1061 + : protected ItemSetTraits<GR, K>::
19.1062 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
19.1063 + public:
19.1064 + typedef typename ItemSetTraits<GR, K>::
19.1065 + template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
19.1066 +
19.1067 + /// The key type
19.1068 + typedef K Key;
19.1069 + /// The value type
19.1070 + typedef V Value;
19.1071 + /// The graph type
19.1072 + typedef GR Graph;
19.1073 +
19.1074 + public:
19.1075 +
19.1076 + /// \brief Constructor of the map with a given value.
19.1077 + ///
19.1078 + /// Constructor of the map with a given value.
19.1079 + explicit IterableValueMap(const Graph& graph,
19.1080 + const Value& value = Value())
19.1081 + : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
19.1082 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
19.1083 + lace(it);
19.1084 + }
19.1085 + }
19.1086 +
19.1087 + protected:
19.1088 +
19.1089 + void unlace(const Key& key) {
19.1090 + typename Parent::Value& node = Parent::operator[](key);
19.1091 + if (node.prev != INVALID) {
19.1092 + Parent::operator[](node.prev).next = node.next;
19.1093 + } else {
19.1094 + if (node.next != INVALID) {
19.1095 + _first[node.value] = node.next;
19.1096 + } else {
19.1097 + _first.erase(node.value);
19.1098 + }
19.1099 + }
19.1100 + if (node.next != INVALID) {
19.1101 + Parent::operator[](node.next).prev = node.prev;
19.1102 + }
19.1103 + }
19.1104 +
19.1105 + void lace(const Key& key) {
19.1106 + typename Parent::Value& node = Parent::operator[](key);
19.1107 + typename std::map<Value, Key>::iterator it = _first.find(node.value);
19.1108 + if (it == _first.end()) {
19.1109 + node.prev = node.next = INVALID;
19.1110 + _first.insert(std::make_pair(node.value, key));
19.1111 + } else {
19.1112 + node.prev = INVALID;
19.1113 + node.next = it->second;
19.1114 + if (node.next != INVALID) {
19.1115 + Parent::operator[](node.next).prev = key;
19.1116 + }
19.1117 + it->second = key;
19.1118 + }
19.1119 + }
19.1120 +
19.1121 + public:
19.1122 +
19.1123 + /// \brief Forward iterator for values.
19.1124 + ///
19.1125 + /// This iterator is an STL compatible forward
19.1126 + /// iterator on the values of the map. The values can
19.1127 + /// be accessed in the <tt>[beginValue, endValue)</tt> range.
19.1128 + class ValueIt
19.1129 + : public std::iterator<std::forward_iterator_tag, Value> {
19.1130 + friend class IterableValueMap;
19.1131 + private:
19.1132 + ValueIt(typename std::map<Value, Key>::const_iterator _it)
19.1133 + : it(_it) {}
19.1134 + public:
19.1135 +
19.1136 + /// Constructor
19.1137 + ValueIt() {}
19.1138 +
19.1139 + /// \e
19.1140 + ValueIt& operator++() { ++it; return *this; }
19.1141 + /// \e
19.1142 + ValueIt operator++(int) {
19.1143 + ValueIt tmp(*this);
19.1144 + operator++();
19.1145 + return tmp;
19.1146 + }
19.1147 +
19.1148 + /// \e
19.1149 + const Value& operator*() const { return it->first; }
19.1150 + /// \e
19.1151 + const Value* operator->() const { return &(it->first); }
19.1152 +
19.1153 + /// \e
19.1154 + bool operator==(ValueIt jt) const { return it == jt.it; }
19.1155 + /// \e
19.1156 + bool operator!=(ValueIt jt) const { return it != jt.it; }
19.1157 +
19.1158 + private:
19.1159 + typename std::map<Value, Key>::const_iterator it;
19.1160 + };
19.1161 +
19.1162 + /// \brief Returns an iterator to the first value.
19.1163 + ///
19.1164 + /// Returns an STL compatible iterator to the
19.1165 + /// first value of the map. The values of the
19.1166 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
19.1167 + /// range.
19.1168 + ValueIt beginValue() const {
19.1169 + return ValueIt(_first.begin());
19.1170 + }
19.1171 +
19.1172 + /// \brief Returns an iterator after the last value.
19.1173 + ///
19.1174 + /// Returns an STL compatible iterator after the
19.1175 + /// last value of the map. The values of the
19.1176 + /// map can be accessed in the <tt>[beginValue, endValue)</tt>
19.1177 + /// range.
19.1178 + ValueIt endValue() const {
19.1179 + return ValueIt(_first.end());
19.1180 + }
19.1181 +
19.1182 + /// \brief Set operation of the map.
19.1183 + ///
19.1184 + /// Set operation of the map.
19.1185 + void set(const Key& key, const Value& value) {
19.1186 + unlace(key);
19.1187 + Parent::operator[](key).value = value;
19.1188 + lace(key);
19.1189 + }
19.1190 +
19.1191 + /// \brief Const subscript operator of the map.
19.1192 + ///
19.1193 + /// Const subscript operator of the map.
19.1194 + const Value& operator[](const Key& key) const {
19.1195 + return Parent::operator[](key).value;
19.1196 + }
19.1197 +
19.1198 + /// \brief Iterator for the keys with the same value.
19.1199 + ///
19.1200 + /// Iterator for the keys with the same value. It works
19.1201 + /// like a graph item iterator, it can be converted to
19.1202 + /// the item type of the map, incremented with \c ++ operator, and
19.1203 + /// if the iterator leaves the last valid item, it will be equal to
19.1204 + /// \c INVALID.
19.1205 + class ItemIt : public Key {
19.1206 + public:
19.1207 + typedef Key Parent;
19.1208 +
19.1209 + /// \brief Invalid constructor \& conversion.
19.1210 + ///
19.1211 + /// This constructor initializes the iterator to be invalid.
19.1212 + /// \sa Invalid for more details.
19.1213 + ItemIt(Invalid) : Parent(INVALID), _map(0) {}
19.1214 +
19.1215 + /// \brief Creates an iterator with a value.
19.1216 + ///
19.1217 + /// Creates an iterator with a value. It iterates on the
19.1218 + /// keys which have the given value.
19.1219 + /// \param map The IterableValueMap
19.1220 + /// \param value The value
19.1221 + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
19.1222 + typename std::map<Value, Key>::const_iterator it =
19.1223 + map._first.find(value);
19.1224 + if (it == map._first.end()) {
19.1225 + Parent::operator=(INVALID);
19.1226 + } else {
19.1227 + Parent::operator=(it->second);
19.1228 + }
19.1229 + }
19.1230 +
19.1231 + /// \brief Increment operator.
19.1232 + ///
19.1233 + /// Increment Operator.
19.1234 + ItemIt& operator++() {
19.1235 + Parent::operator=(_map->IterableValueMap::Parent::
19.1236 + operator[](static_cast<Parent&>(*this)).next);
19.1237 + return *this;
19.1238 + }
19.1239 +
19.1240 +
19.1241 + private:
19.1242 + const IterableValueMap* _map;
19.1243 + };
19.1244 +
19.1245 + protected:
19.1246 +
19.1247 + virtual void add(const Key& key) {
19.1248 + Parent::add(key);
19.1249 + unlace(key);
19.1250 + }
19.1251 +
19.1252 + virtual void add(const std::vector<Key>& keys) {
19.1253 + Parent::add(keys);
19.1254 + for (int i = 0; i < int(keys.size()); ++i) {
19.1255 + lace(keys[i]);
19.1256 + }
19.1257 + }
19.1258 +
19.1259 + virtual void erase(const Key& key) {
19.1260 + unlace(key);
19.1261 + Parent::erase(key);
19.1262 + }
19.1263 +
19.1264 + virtual void erase(const std::vector<Key>& keys) {
19.1265 + for (int i = 0; i < int(keys.size()); ++i) {
19.1266 + unlace(keys[i]);
19.1267 + }
19.1268 + Parent::erase(keys);
19.1269 + }
19.1270 +
19.1271 + virtual void build() {
19.1272 + Parent::build();
19.1273 + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
19.1274 + lace(it);
19.1275 + }
19.1276 + }
19.1277 +
19.1278 + virtual void clear() {
19.1279 + _first.clear();
19.1280 + Parent::clear();
19.1281 + }
19.1282 +
19.1283 + private:
19.1284 + std::map<Value, Key> _first;
19.1285 + };
19.1286 +
19.1287 /// \brief Map of the source nodes of arcs in a digraph.
19.1288 ///
19.1289 /// SourceMap provides access for the source node of each arc in a digraph,
19.1290 @@ -2340,9 +3300,9 @@
19.1291 class SourceMap {
19.1292 public:
19.1293
19.1294 - ///\e
19.1295 + /// The key type (the \c Arc type of the digraph).
19.1296 typedef typename GR::Arc Key;
19.1297 - ///\e
19.1298 + /// The value type (the \c Node type of the digraph).
19.1299 typedef typename GR::Node Value;
19.1300
19.1301 /// \brief Constructor
19.1302 @@ -2381,9 +3341,9 @@
19.1303 class TargetMap {
19.1304 public:
19.1305
19.1306 - ///\e
19.1307 + /// The key type (the \c Arc type of the digraph).
19.1308 typedef typename GR::Arc Key;
19.1309 - ///\e
19.1310 + /// The value type (the \c Node type of the digraph).
19.1311 typedef typename GR::Node Value;
19.1312
19.1313 /// \brief Constructor
19.1314 @@ -2423,8 +3383,10 @@
19.1315 class ForwardMap {
19.1316 public:
19.1317
19.1318 + /// The key type (the \c Edge type of the digraph).
19.1319 + typedef typename GR::Edge Key;
19.1320 + /// The value type (the \c Arc type of the digraph).
19.1321 typedef typename GR::Arc Value;
19.1322 - typedef typename GR::Edge Key;
19.1323
19.1324 /// \brief Constructor
19.1325 ///
19.1326 @@ -2463,8 +3425,10 @@
19.1327 class BackwardMap {
19.1328 public:
19.1329
19.1330 + /// The key type (the \c Edge type of the digraph).
19.1331 + typedef typename GR::Edge Key;
19.1332 + /// The value type (the \c Arc type of the digraph).
19.1333 typedef typename GR::Arc Value;
19.1334 - typedef typename GR::Edge Key;
19.1335
19.1336 /// \brief Constructor
19.1337 ///
19.1338 @@ -2499,7 +3463,7 @@
19.1339 /// in constant time. On the other hand, the values are updated automatically
19.1340 /// whenever the digraph changes.
19.1341 ///
19.1342 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
19.1343 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
19.1344 /// may provide alternative ways to modify the digraph.
19.1345 /// The correct behavior of InDegMap is not guarantied if these additional
19.1346 /// features are used. For example the functions
19.1347 @@ -2515,7 +3479,7 @@
19.1348 ::ItemNotifier::ObserverBase {
19.1349
19.1350 public:
19.1351 -
19.1352 +
19.1353 /// The graph type of InDegMap
19.1354 typedef GR Graph;
19.1355 typedef GR Digraph;
19.1356 @@ -2629,7 +3593,7 @@
19.1357 /// in constant time. On the other hand, the values are updated automatically
19.1358 /// whenever the digraph changes.
19.1359 ///
19.1360 - /// \warning Besides \c addNode() and \c addArc(), a digraph structure
19.1361 + /// \warning Besides \c addNode() and \c addArc(), a digraph structure
19.1362 /// may provide alternative ways to modify the digraph.
19.1363 /// The correct behavior of OutDegMap is not guarantied if these additional
19.1364 /// features are used. For example the functions
20.1 --- a/lemon/min_cost_arborescence.h Wed Sep 30 08:36:43 2009 +0200
20.2 +++ b/lemon/min_cost_arborescence.h Wed Sep 30 08:41:06 2009 +0200
20.3 @@ -488,8 +488,8 @@
20.4 /// \name Execution Control
20.5 /// The simplest way to execute the algorithm is to use
20.6 /// one of the member functions called \c run(...). \n
20.7 - /// If you need more control on the execution,
20.8 - /// first you must call \ref init(), then you can add several
20.9 + /// If you need better control on the execution,
20.10 + /// you have to call \ref init() first, then you can add several
20.11 /// source nodes with \ref addSource().
20.12 /// Finally \ref start() will perform the arborescence
20.13 /// computation.
21.1 --- a/lemon/network_simplex.h Wed Sep 30 08:36:43 2009 +0200
21.2 +++ b/lemon/network_simplex.h Wed Sep 30 08:41:06 2009 +0200
21.3 @@ -161,8 +161,6 @@
21.4
21.5 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
21.6
21.7 - typedef std::vector<Arc> ArcVector;
21.8 - typedef std::vector<Node> NodeVector;
21.9 typedef std::vector<int> IntVector;
21.10 typedef std::vector<bool> BoolVector;
21.11 typedef std::vector<Value> ValueVector;
21.12 @@ -364,33 +362,32 @@
21.13 bool findEnteringArc() {
21.14 Cost c, min = 0;
21.15 int cnt = _block_size;
21.16 - int e, min_arc = _next_arc;
21.17 + int e;
21.18 for (e = _next_arc; e < _search_arc_num; ++e) {
21.19 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.20 if (c < min) {
21.21 min = c;
21.22 - min_arc = e;
21.23 + _in_arc = e;
21.24 }
21.25 if (--cnt == 0) {
21.26 - if (min < 0) break;
21.27 + if (min < 0) goto search_end;
21.28 cnt = _block_size;
21.29 }
21.30 }
21.31 - if (min == 0 || cnt > 0) {
21.32 - for (e = 0; e < _next_arc; ++e) {
21.33 - c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.34 - if (c < min) {
21.35 - min = c;
21.36 - min_arc = e;
21.37 - }
21.38 - if (--cnt == 0) {
21.39 - if (min < 0) break;
21.40 - cnt = _block_size;
21.41 - }
21.42 + for (e = 0; e < _next_arc; ++e) {
21.43 + c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.44 + if (c < min) {
21.45 + min = c;
21.46 + _in_arc = e;
21.47 + }
21.48 + if (--cnt == 0) {
21.49 + if (min < 0) goto search_end;
21.50 + cnt = _block_size;
21.51 }
21.52 }
21.53 if (min >= 0) return false;
21.54 - _in_arc = min_arc;
21.55 +
21.56 + search_end:
21.57 _next_arc = e;
21.58 return true;
21.59 }
21.60 @@ -428,7 +425,7 @@
21.61 _next_arc(0)
21.62 {
21.63 // The main parameters of the pivot rule
21.64 - const double LIST_LENGTH_FACTOR = 1.0;
21.65 + const double LIST_LENGTH_FACTOR = 0.25;
21.66 const int MIN_LIST_LENGTH = 10;
21.67 const double MINOR_LIMIT_FACTOR = 0.1;
21.68 const int MIN_MINOR_LIMIT = 3;
21.69 @@ -445,7 +442,7 @@
21.70 /// Find next entering arc
21.71 bool findEnteringArc() {
21.72 Cost min, c;
21.73 - int e, min_arc = _next_arc;
21.74 + int e;
21.75 if (_curr_length > 0 && _minor_count < _minor_limit) {
21.76 // Minor iteration: select the best eligible arc from the
21.77 // current candidate list
21.78 @@ -456,16 +453,13 @@
21.79 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.80 if (c < min) {
21.81 min = c;
21.82 - min_arc = e;
21.83 + _in_arc = e;
21.84 }
21.85 - if (c >= 0) {
21.86 + else if (c >= 0) {
21.87 _candidates[i--] = _candidates[--_curr_length];
21.88 }
21.89 }
21.90 - if (min < 0) {
21.91 - _in_arc = min_arc;
21.92 - return true;
21.93 - }
21.94 + if (min < 0) return true;
21.95 }
21.96
21.97 // Major iteration: build a new candidate list
21.98 @@ -477,27 +471,26 @@
21.99 _candidates[_curr_length++] = e;
21.100 if (c < min) {
21.101 min = c;
21.102 - min_arc = e;
21.103 + _in_arc = e;
21.104 }
21.105 - if (_curr_length == _list_length) break;
21.106 + if (_curr_length == _list_length) goto search_end;
21.107 }
21.108 }
21.109 - if (_curr_length < _list_length) {
21.110 - for (e = 0; e < _next_arc; ++e) {
21.111 - c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.112 - if (c < 0) {
21.113 - _candidates[_curr_length++] = e;
21.114 - if (c < min) {
21.115 - min = c;
21.116 - min_arc = e;
21.117 - }
21.118 - if (_curr_length == _list_length) break;
21.119 + for (e = 0; e < _next_arc; ++e) {
21.120 + c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.121 + if (c < 0) {
21.122 + _candidates[_curr_length++] = e;
21.123 + if (c < min) {
21.124 + min = c;
21.125 + _in_arc = e;
21.126 }
21.127 + if (_curr_length == _list_length) goto search_end;
21.128 }
21.129 }
21.130 if (_curr_length == 0) return false;
21.131 +
21.132 + search_end:
21.133 _minor_count = 1;
21.134 - _in_arc = min_arc;
21.135 _next_arc = e;
21.136 return true;
21.137 }
21.138 @@ -549,7 +542,7 @@
21.139 _next_arc(0), _cand_cost(ns._search_arc_num), _sort_func(_cand_cost)
21.140 {
21.141 // The main parameters of the pivot rule
21.142 - const double BLOCK_SIZE_FACTOR = 1.5;
21.143 + const double BLOCK_SIZE_FACTOR = 1.0;
21.144 const int MIN_BLOCK_SIZE = 10;
21.145 const double HEAD_LENGTH_FACTOR = 0.1;
21.146 const int MIN_HEAD_LENGTH = 3;
21.147 @@ -578,39 +571,35 @@
21.148
21.149 // Extend the list
21.150 int cnt = _block_size;
21.151 - int last_arc = 0;
21.152 int limit = _head_length;
21.153
21.154 - for (int e = _next_arc; e < _search_arc_num; ++e) {
21.155 + for (e = _next_arc; e < _search_arc_num; ++e) {
21.156 _cand_cost[e] = _state[e] *
21.157 (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.158 if (_cand_cost[e] < 0) {
21.159 _candidates[_curr_length++] = e;
21.160 - last_arc = e;
21.161 }
21.162 if (--cnt == 0) {
21.163 - if (_curr_length > limit) break;
21.164 + if (_curr_length > limit) goto search_end;
21.165 limit = 0;
21.166 cnt = _block_size;
21.167 }
21.168 }
21.169 - if (_curr_length <= limit) {
21.170 - for (int e = 0; e < _next_arc; ++e) {
21.171 - _cand_cost[e] = _state[e] *
21.172 - (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.173 - if (_cand_cost[e] < 0) {
21.174 - _candidates[_curr_length++] = e;
21.175 - last_arc = e;
21.176 - }
21.177 - if (--cnt == 0) {
21.178 - if (_curr_length > limit) break;
21.179 - limit = 0;
21.180 - cnt = _block_size;
21.181 - }
21.182 + for (e = 0; e < _next_arc; ++e) {
21.183 + _cand_cost[e] = _state[e] *
21.184 + (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
21.185 + if (_cand_cost[e] < 0) {
21.186 + _candidates[_curr_length++] = e;
21.187 + }
21.188 + if (--cnt == 0) {
21.189 + if (_curr_length > limit) goto search_end;
21.190 + limit = 0;
21.191 + cnt = _block_size;
21.192 }
21.193 }
21.194 if (_curr_length == 0) return false;
21.195 - _next_arc = last_arc + 1;
21.196 +
21.197 + search_end:
21.198
21.199 // Make heap of the candidate list (approximating a partial sort)
21.200 make_heap( _candidates.begin(), _candidates.begin() + _curr_length,
21.201 @@ -618,6 +607,7 @@
21.202
21.203 // Pop the first element of the heap
21.204 _in_arc = _candidates[0];
21.205 + _next_arc = e;
21.206 pop_heap( _candidates.begin(), _candidates.begin() + _curr_length,
21.207 _sort_func );
21.208 _curr_length = std::min(_head_length, _curr_length - 1);
21.209 @@ -633,7 +623,11 @@
21.210 /// The constructor of the class.
21.211 ///
21.212 /// \param graph The digraph the algorithm runs on.
21.213 - NetworkSimplex(const GR& graph) :
21.214 + /// \param arc_mixing Indicate if the arcs have to be stored in a
21.215 + /// mixed order in the internal data structure.
21.216 + /// In special cases, it could lead to better overall performance,
21.217 + /// but it is usually slower. Therefore it is disabled by default.
21.218 + NetworkSimplex(const GR& graph, bool arc_mixing = false) :
21.219 _graph(graph), _node_id(graph), _arc_id(graph),
21.220 INF(std::numeric_limits<Value>::has_infinity ?
21.221 std::numeric_limits<Value>::infinity() :
21.222 @@ -671,31 +665,33 @@
21.223 _last_succ.resize(all_node_num);
21.224 _state.resize(max_arc_num);
21.225
21.226 - // Copy the graph (store the arcs in a mixed order)
21.227 + // Copy the graph
21.228 int i = 0;
21.229 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
21.230 _node_id[n] = i;
21.231 }
21.232 - int k = std::max(int(std::sqrt(double(_arc_num))), 10);
21.233 - i = 0;
21.234 - for (ArcIt a(_graph); a != INVALID; ++a) {
21.235 - _arc_id[a] = i;
21.236 - _source[i] = _node_id[_graph.source(a)];
21.237 - _target[i] = _node_id[_graph.target(a)];
21.238 - if ((i += k) >= _arc_num) i = (i % k) + 1;
21.239 + if (arc_mixing) {
21.240 + // Store the arcs in a mixed order
21.241 + int k = std::max(int(std::sqrt(double(_arc_num))), 10);
21.242 + int i = 0, j = 0;
21.243 + for (ArcIt a(_graph); a != INVALID; ++a) {
21.244 + _arc_id[a] = i;
21.245 + _source[i] = _node_id[_graph.source(a)];
21.246 + _target[i] = _node_id[_graph.target(a)];
21.247 + if ((i += k) >= _arc_num) i = ++j;
21.248 + }
21.249 + } else {
21.250 + // Store the arcs in the original order
21.251 + int i = 0;
21.252 + for (ArcIt a(_graph); a != INVALID; ++a, ++i) {
21.253 + _arc_id[a] = i;
21.254 + _source[i] = _node_id[_graph.source(a)];
21.255 + _target[i] = _node_id[_graph.target(a)];
21.256 + }
21.257 }
21.258
21.259 - // Initialize maps
21.260 - for (int i = 0; i != _node_num; ++i) {
21.261 - _supply[i] = 0;
21.262 - }
21.263 - for (int i = 0; i != _arc_num; ++i) {
21.264 - _lower[i] = 0;
21.265 - _upper[i] = INF;
21.266 - _cost[i] = 1;
21.267 - }
21.268 - _have_lower = false;
21.269 - _stype = GEQ;
21.270 + // Reset parameters
21.271 + reset();
21.272 }
21.273
21.274 /// \name Parameters
21.275 @@ -768,7 +764,6 @@
21.276 /// This function sets the supply values of the nodes.
21.277 /// If neither this function nor \ref stSupply() is used before
21.278 /// calling \ref run(), the supply of each node will be set to zero.
21.279 - /// (It makes sense only if non-zero lower bounds are given.)
21.280 ///
21.281 /// \param map A node map storing the supply values.
21.282 /// Its \c Value type must be convertible to the \c Value type
21.283 @@ -789,7 +784,6 @@
21.284 /// and the required flow value.
21.285 /// If neither this function nor \ref supplyMap() is used before
21.286 /// calling \ref run(), the supply of each node will be set to zero.
21.287 - /// (It makes sense only if non-zero lower bounds are given.)
21.288 ///
21.289 /// Using this function has the same effect as using \ref supplyMap()
21.290 /// with such a map in which \c k is assigned to \c s, \c -k is
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
22.2 +++ b/lemon/pairing_heap.h Wed Sep 30 08:41:06 2009 +0200
22.3 @@ -0,0 +1,474 @@
22.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
22.5 + *
22.6 + * This file is a part of LEMON, a generic C++ optimization library.
22.7 + *
22.8 + * Copyright (C) 2003-2009
22.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
22.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
22.11 + *
22.12 + * Permission to use, modify and distribute this software is granted
22.13 + * provided that this copyright notice appears in all copies. For
22.14 + * precise terms see the accompanying LICENSE file.
22.15 + *
22.16 + * This software is provided "AS IS" with no warranty of any kind,
22.17 + * express or implied, and with no claim as to its suitability for any
22.18 + * purpose.
22.19 + *
22.20 + */
22.21 +
22.22 +#ifndef LEMON_PAIRING_HEAP_H
22.23 +#define LEMON_PAIRING_HEAP_H
22.24 +
22.25 +///\file
22.26 +///\ingroup heaps
22.27 +///\brief Pairing heap implementation.
22.28 +
22.29 +#include <vector>
22.30 +#include <utility>
22.31 +#include <functional>
22.32 +#include <lemon/math.h>
22.33 +
22.34 +namespace lemon {
22.35 +
22.36 + /// \ingroup heaps
22.37 + ///
22.38 + ///\brief Pairing Heap.
22.39 + ///
22.40 + /// This class implements the \e pairing \e heap data structure.
22.41 + /// It fully conforms to the \ref concepts::Heap "heap concept".
22.42 + ///
22.43 + /// The methods \ref increase() and \ref erase() are not efficient
22.44 + /// in a pairing heap. In case of many calls of these operations,
22.45 + /// it is better to use other heap structure, e.g. \ref BinHeap
22.46 + /// "binary heap".
22.47 + ///
22.48 + /// \tparam PR Type of the priorities of the items.
22.49 + /// \tparam IM A read-writable item map with \c int values, used
22.50 + /// internally to handle the cross references.
22.51 + /// \tparam CMP A functor class for comparing the priorities.
22.52 + /// The default is \c std::less<PR>.
22.53 +#ifdef DOXYGEN
22.54 + template <typename PR, typename IM, typename CMP>
22.55 +#else
22.56 + template <typename PR, typename IM, typename CMP = std::less<PR> >
22.57 +#endif
22.58 + class PairingHeap {
22.59 + public:
22.60 + /// Type of the item-int map.
22.61 + typedef IM ItemIntMap;
22.62 + /// Type of the priorities.
22.63 + typedef PR Prio;
22.64 + /// Type of the items stored in the heap.
22.65 + typedef typename ItemIntMap::Key Item;
22.66 + /// Functor type for comparing the priorities.
22.67 + typedef CMP Compare;
22.68 +
22.69 + /// \brief Type to represent the states of the items.
22.70 + ///
22.71 + /// Each item has a state associated to it. It can be "in heap",
22.72 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
22.73 + /// heap's point of view, but may be useful to the user.
22.74 + ///
22.75 + /// The item-int map must be initialized in such way that it assigns
22.76 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
22.77 + enum State {
22.78 + IN_HEAP = 0, ///< = 0.
22.79 + PRE_HEAP = -1, ///< = -1.
22.80 + POST_HEAP = -2 ///< = -2.
22.81 + };
22.82 +
22.83 + private:
22.84 + class store;
22.85 +
22.86 + std::vector<store> _data;
22.87 + int _min;
22.88 + ItemIntMap &_iim;
22.89 + Compare _comp;
22.90 + int _num_items;
22.91 +
22.92 + public:
22.93 + /// \brief Constructor.
22.94 + ///
22.95 + /// Constructor.
22.96 + /// \param map A map that assigns \c int values to the items.
22.97 + /// It is used internally to handle the cross references.
22.98 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
22.99 + explicit PairingHeap(ItemIntMap &map)
22.100 + : _min(0), _iim(map), _num_items(0) {}
22.101 +
22.102 + /// \brief Constructor.
22.103 + ///
22.104 + /// Constructor.
22.105 + /// \param map A map that assigns \c int values to the items.
22.106 + /// It is used internally to handle the cross references.
22.107 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
22.108 + /// \param comp The function object used for comparing the priorities.
22.109 + PairingHeap(ItemIntMap &map, const Compare &comp)
22.110 + : _min(0), _iim(map), _comp(comp), _num_items(0) {}
22.111 +
22.112 + /// \brief The number of items stored in the heap.
22.113 + ///
22.114 + /// This function returns the number of items stored in the heap.
22.115 + int size() const { return _num_items; }
22.116 +
22.117 + /// \brief Check if the heap is empty.
22.118 + ///
22.119 + /// This function returns \c true if the heap is empty.
22.120 + bool empty() const { return _num_items==0; }
22.121 +
22.122 + /// \brief Make the heap empty.
22.123 + ///
22.124 + /// This functon makes the heap empty.
22.125 + /// It does not change the cross reference map. If you want to reuse
22.126 + /// a heap that is not surely empty, you should first clear it and
22.127 + /// then you should set the cross reference map to \c PRE_HEAP
22.128 + /// for each item.
22.129 + void clear() {
22.130 + _data.clear();
22.131 + _min = 0;
22.132 + _num_items = 0;
22.133 + }
22.134 +
22.135 + /// \brief Set the priority of an item or insert it, if it is
22.136 + /// not stored in the heap.
22.137 + ///
22.138 + /// This method sets the priority of the given item if it is
22.139 + /// already stored in the heap. Otherwise it inserts the given
22.140 + /// item into the heap with the given priority.
22.141 + /// \param item The item.
22.142 + /// \param value The priority.
22.143 + void set (const Item& item, const Prio& value) {
22.144 + int i=_iim[item];
22.145 + if ( i>=0 && _data[i].in ) {
22.146 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
22.147 + if ( _comp(_data[i].prio, value) ) increase(item, value);
22.148 + } else push(item, value);
22.149 + }
22.150 +
22.151 + /// \brief Insert an item into the heap with the given priority.
22.152 + ///
22.153 + /// This function inserts the given item into the heap with the
22.154 + /// given priority.
22.155 + /// \param item The item to insert.
22.156 + /// \param value The priority of the item.
22.157 + /// \pre \e item must not be stored in the heap.
22.158 + void push (const Item& item, const Prio& value) {
22.159 + int i=_iim[item];
22.160 + if( i<0 ) {
22.161 + int s=_data.size();
22.162 + _iim.set(item, s);
22.163 + store st;
22.164 + st.name=item;
22.165 + _data.push_back(st);
22.166 + i=s;
22.167 + } else {
22.168 + _data[i].parent=_data[i].child=-1;
22.169 + _data[i].left_child=false;
22.170 + _data[i].degree=0;
22.171 + _data[i].in=true;
22.172 + }
22.173 +
22.174 + _data[i].prio=value;
22.175 +
22.176 + if ( _num_items!=0 ) {
22.177 + if ( _comp( value, _data[_min].prio) ) {
22.178 + fuse(i,_min);
22.179 + _min=i;
22.180 + }
22.181 + else fuse(_min,i);
22.182 + }
22.183 + else _min=i;
22.184 +
22.185 + ++_num_items;
22.186 + }
22.187 +
22.188 + /// \brief Return the item having minimum priority.
22.189 + ///
22.190 + /// This function returns the item having minimum priority.
22.191 + /// \pre The heap must be non-empty.
22.192 + Item top() const { return _data[_min].name; }
22.193 +
22.194 + /// \brief The minimum priority.
22.195 + ///
22.196 + /// This function returns the minimum priority.
22.197 + /// \pre The heap must be non-empty.
22.198 + const Prio& prio() const { return _data[_min].prio; }
22.199 +
22.200 + /// \brief The priority of the given item.
22.201 + ///
22.202 + /// This function returns the priority of the given item.
22.203 + /// \param item The item.
22.204 + /// \pre \e item must be in the heap.
22.205 + const Prio& operator[](const Item& item) const {
22.206 + return _data[_iim[item]].prio;
22.207 + }
22.208 +
22.209 + /// \brief Remove the item having minimum priority.
22.210 + ///
22.211 + /// This function removes the item having minimum priority.
22.212 + /// \pre The heap must be non-empty.
22.213 + void pop() {
22.214 + std::vector<int> trees;
22.215 + int i=0, child_right = 0;
22.216 + _data[_min].in=false;
22.217 +
22.218 + if( -1!=_data[_min].child ) {
22.219 + i=_data[_min].child;
22.220 + trees.push_back(i);
22.221 + _data[i].parent = -1;
22.222 + _data[_min].child = -1;
22.223 +
22.224 + int ch=-1;
22.225 + while( _data[i].child!=-1 ) {
22.226 + ch=_data[i].child;
22.227 + if( _data[ch].left_child && i==_data[ch].parent ) {
22.228 + break;
22.229 + } else {
22.230 + if( _data[ch].left_child ) {
22.231 + child_right=_data[ch].parent;
22.232 + _data[ch].parent = i;
22.233 + --_data[i].degree;
22.234 + }
22.235 + else {
22.236 + child_right=ch;
22.237 + _data[i].child=-1;
22.238 + _data[i].degree=0;
22.239 + }
22.240 + _data[child_right].parent = -1;
22.241 + trees.push_back(child_right);
22.242 + i = child_right;
22.243 + }
22.244 + }
22.245 +
22.246 + int num_child = trees.size();
22.247 + int other;
22.248 + for( i=0; i<num_child-1; i+=2 ) {
22.249 + if ( !_comp(_data[trees[i]].prio, _data[trees[i+1]].prio) ) {
22.250 + other=trees[i];
22.251 + trees[i]=trees[i+1];
22.252 + trees[i+1]=other;
22.253 + }
22.254 + fuse( trees[i], trees[i+1] );
22.255 + }
22.256 +
22.257 + i = (0==(num_child % 2)) ? num_child-2 : num_child-1;
22.258 + while(i>=2) {
22.259 + if ( _comp(_data[trees[i]].prio, _data[trees[i-2]].prio) ) {
22.260 + other=trees[i];
22.261 + trees[i]=trees[i-2];
22.262 + trees[i-2]=other;
22.263 + }
22.264 + fuse( trees[i-2], trees[i] );
22.265 + i-=2;
22.266 + }
22.267 + _min = trees[0];
22.268 + }
22.269 + else {
22.270 + _min = _data[_min].child;
22.271 + }
22.272 +
22.273 + if (_min >= 0) _data[_min].left_child = false;
22.274 + --_num_items;
22.275 + }
22.276 +
22.277 + /// \brief Remove the given item from the heap.
22.278 + ///
22.279 + /// This function removes the given item from the heap if it is
22.280 + /// already stored.
22.281 + /// \param item The item to delete.
22.282 + /// \pre \e item must be in the heap.
22.283 + void erase (const Item& item) {
22.284 + int i=_iim[item];
22.285 + if ( i>=0 && _data[i].in ) {
22.286 + decrease( item, _data[_min].prio-1 );
22.287 + pop();
22.288 + }
22.289 + }
22.290 +
22.291 + /// \brief Decrease the priority of an item to the given value.
22.292 + ///
22.293 + /// This function decreases the priority of an item to the given value.
22.294 + /// \param item The item.
22.295 + /// \param value The priority.
22.296 + /// \pre \e item must be stored in the heap with priority at least \e value.
22.297 + void decrease (Item item, const Prio& value) {
22.298 + int i=_iim[item];
22.299 + _data[i].prio=value;
22.300 + int p=_data[i].parent;
22.301 +
22.302 + if( _data[i].left_child && i!=_data[p].child ) {
22.303 + p=_data[p].parent;
22.304 + }
22.305 +
22.306 + if ( p!=-1 && _comp(value,_data[p].prio) ) {
22.307 + cut(i,p);
22.308 + if ( _comp(_data[_min].prio,value) ) {
22.309 + fuse(_min,i);
22.310 + } else {
22.311 + fuse(i,_min);
22.312 + _min=i;
22.313 + }
22.314 + }
22.315 + }
22.316 +
22.317 + /// \brief Increase the priority of an item to the given value.
22.318 + ///
22.319 + /// This function increases the priority of an item to the given value.
22.320 + /// \param item The item.
22.321 + /// \param value The priority.
22.322 + /// \pre \e item must be stored in the heap with priority at most \e value.
22.323 + void increase (Item item, const Prio& value) {
22.324 + erase(item);
22.325 + push(item,value);
22.326 + }
22.327 +
22.328 + /// \brief Return the state of an item.
22.329 + ///
22.330 + /// This method returns \c PRE_HEAP if the given item has never
22.331 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
22.332 + /// and \c POST_HEAP otherwise.
22.333 + /// In the latter case it is possible that the item will get back
22.334 + /// to the heap again.
22.335 + /// \param item The item.
22.336 + State state(const Item &item) const {
22.337 + int i=_iim[item];
22.338 + if( i>=0 ) {
22.339 + if( _data[i].in ) i=0;
22.340 + else i=-2;
22.341 + }
22.342 + return State(i);
22.343 + }
22.344 +
22.345 + /// \brief Set the state of an item in the heap.
22.346 + ///
22.347 + /// This function sets the state of the given item in the heap.
22.348 + /// It can be used to manually clear the heap when it is important
22.349 + /// to achive better time complexity.
22.350 + /// \param i The item.
22.351 + /// \param st The state. It should not be \c IN_HEAP.
22.352 + void state(const Item& i, State st) {
22.353 + switch (st) {
22.354 + case POST_HEAP:
22.355 + case PRE_HEAP:
22.356 + if (state(i) == IN_HEAP) erase(i);
22.357 + _iim[i]=st;
22.358 + break;
22.359 + case IN_HEAP:
22.360 + break;
22.361 + }
22.362 + }
22.363 +
22.364 + private:
22.365 +
22.366 + void cut(int a, int b) {
22.367 + int child_a;
22.368 + switch (_data[a].degree) {
22.369 + case 2:
22.370 + child_a = _data[_data[a].child].parent;
22.371 + if( _data[a].left_child ) {
22.372 + _data[child_a].left_child=true;
22.373 + _data[b].child=child_a;
22.374 + _data[child_a].parent=_data[a].parent;
22.375 + }
22.376 + else {
22.377 + _data[child_a].left_child=false;
22.378 + _data[child_a].parent=b;
22.379 + if( a!=_data[b].child )
22.380 + _data[_data[b].child].parent=child_a;
22.381 + else
22.382 + _data[b].child=child_a;
22.383 + }
22.384 + --_data[a].degree;
22.385 + _data[_data[a].child].parent=a;
22.386 + break;
22.387 +
22.388 + case 1:
22.389 + child_a = _data[a].child;
22.390 + if( !_data[child_a].left_child ) {
22.391 + --_data[a].degree;
22.392 + if( _data[a].left_child ) {
22.393 + _data[child_a].left_child=true;
22.394 + _data[child_a].parent=_data[a].parent;
22.395 + _data[b].child=child_a;
22.396 + }
22.397 + else {
22.398 + _data[child_a].left_child=false;
22.399 + _data[child_a].parent=b;
22.400 + if( a!=_data[b].child )
22.401 + _data[_data[b].child].parent=child_a;
22.402 + else
22.403 + _data[b].child=child_a;
22.404 + }
22.405 + _data[a].child=-1;
22.406 + }
22.407 + else {
22.408 + --_data[b].degree;
22.409 + if( _data[a].left_child ) {
22.410 + _data[b].child =
22.411 + (1==_data[b].degree) ? _data[a].parent : -1;
22.412 + } else {
22.413 + if (1==_data[b].degree)
22.414 + _data[_data[b].child].parent=b;
22.415 + else
22.416 + _data[b].child=-1;
22.417 + }
22.418 + }
22.419 + break;
22.420 +
22.421 + case 0:
22.422 + --_data[b].degree;
22.423 + if( _data[a].left_child ) {
22.424 + _data[b].child =
22.425 + (0!=_data[b].degree) ? _data[a].parent : -1;
22.426 + } else {
22.427 + if( 0!=_data[b].degree )
22.428 + _data[_data[b].child].parent=b;
22.429 + else
22.430 + _data[b].child=-1;
22.431 + }
22.432 + break;
22.433 + }
22.434 + _data[a].parent=-1;
22.435 + _data[a].left_child=false;
22.436 + }
22.437 +
22.438 + void fuse(int a, int b) {
22.439 + int child_a = _data[a].child;
22.440 + int child_b = _data[b].child;
22.441 + _data[a].child=b;
22.442 + _data[b].parent=a;
22.443 + _data[b].left_child=true;
22.444 +
22.445 + if( -1!=child_a ) {
22.446 + _data[b].child=child_a;
22.447 + _data[child_a].parent=b;
22.448 + _data[child_a].left_child=false;
22.449 + ++_data[b].degree;
22.450 +
22.451 + if( -1!=child_b ) {
22.452 + _data[b].child=child_b;
22.453 + _data[child_b].parent=child_a;
22.454 + }
22.455 + }
22.456 + else { ++_data[a].degree; }
22.457 + }
22.458 +
22.459 + class store {
22.460 + friend class PairingHeap;
22.461 +
22.462 + Item name;
22.463 + int parent;
22.464 + int child;
22.465 + bool left_child;
22.466 + int degree;
22.467 + bool in;
22.468 + Prio prio;
22.469 +
22.470 + store() : parent(-1), child(-1), left_child(false), degree(0), in(true) {}
22.471 + };
22.472 + };
22.473 +
22.474 +} //namespace lemon
22.475 +
22.476 +#endif //LEMON_PAIRING_HEAP_H
22.477 +
23.1 --- a/lemon/preflow.h Wed Sep 30 08:36:43 2009 +0200
23.2 +++ b/lemon/preflow.h Wed Sep 30 08:41:06 2009 +0200
23.3 @@ -52,7 +52,11 @@
23.4 ///
23.5 /// The type of the map that stores the flow values.
23.6 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
23.7 +#ifdef DOXYGEN
23.8 + typedef GR::ArcMap<Value> FlowMap;
23.9 +#else
23.10 typedef typename Digraph::template ArcMap<Value> FlowMap;
23.11 +#endif
23.12
23.13 /// \brief Instantiates a FlowMap.
23.14 ///
23.15 @@ -67,9 +71,12 @@
23.16 ///
23.17 /// The elevator type used by Preflow algorithm.
23.18 ///
23.19 - /// \sa Elevator
23.20 - /// \sa LinkedElevator
23.21 - typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator;
23.22 + /// \sa Elevator, LinkedElevator
23.23 +#ifdef DOXYGEN
23.24 + typedef lemon::Elevator<GR, GR::Node> Elevator;
23.25 +#else
23.26 + typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
23.27 +#endif
23.28
23.29 /// \brief Instantiates an Elevator.
23.30 ///
23.31 @@ -97,7 +104,7 @@
23.32 /// \e push-relabel algorithm producing a \ref max_flow
23.33 /// "flow of maximum value" in a digraph.
23.34 /// The preflow algorithms are the fastest known maximum
23.35 - /// flow algorithms. The current implementation use a mixture of the
23.36 + /// flow algorithms. The current implementation uses a mixture of the
23.37 /// \e "highest label" and the \e "bound decrease" heuristics.
23.38 /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
23.39 ///
23.40 @@ -371,26 +378,28 @@
23.41 return *_level;
23.42 }
23.43
23.44 - /// \brief Sets the tolerance used by algorithm.
23.45 + /// \brief Sets the tolerance used by the algorithm.
23.46 ///
23.47 - /// Sets the tolerance used by algorithm.
23.48 - Preflow& tolerance(const Tolerance& tolerance) const {
23.49 + /// Sets the tolerance object used by the algorithm.
23.50 + /// \return <tt>(*this)</tt>
23.51 + Preflow& tolerance(const Tolerance& tolerance) {
23.52 _tolerance = tolerance;
23.53 return *this;
23.54 }
23.55
23.56 /// \brief Returns a const reference to the tolerance.
23.57 ///
23.58 - /// Returns a const reference to the tolerance.
23.59 + /// Returns a const reference to the tolerance object used by
23.60 + /// the algorithm.
23.61 const Tolerance& tolerance() const {
23.62 - return tolerance;
23.63 + return _tolerance;
23.64 }
23.65
23.66 /// \name Execution Control
23.67 /// The simplest way to execute the preflow algorithm is to use
23.68 /// \ref run() or \ref runMinCut().\n
23.69 - /// If you need more control on the initial solution or the execution,
23.70 - /// first you have to call one of the \ref init() functions, then
23.71 + /// If you need better control on the initial solution or the execution,
23.72 + /// you have to call one of the \ref init() functions first, then
23.73 /// \ref startFirstPhase() and if you need it \ref startSecondPhase().
23.74
23.75 ///@{
24.1 --- a/lemon/radix_heap.h Wed Sep 30 08:36:43 2009 +0200
24.2 +++ b/lemon/radix_heap.h Wed Sep 30 08:41:06 2009 +0200
24.3 @@ -19,9 +19,9 @@
24.4 #ifndef LEMON_RADIX_HEAP_H
24.5 #define LEMON_RADIX_HEAP_H
24.6
24.7 -///\ingroup auxdat
24.8 +///\ingroup heaps
24.9 ///\file
24.10 -///\brief Radix Heap implementation.
24.11 +///\brief Radix heap implementation.
24.12
24.13 #include <vector>
24.14 #include <lemon/error.h>
24.15 @@ -29,56 +29,54 @@
24.16 namespace lemon {
24.17
24.18
24.19 - /// \ingroup auxdata
24.20 + /// \ingroup heaps
24.21 ///
24.22 - /// \brief A Radix Heap implementation.
24.23 + /// \brief Radix heap data structure.
24.24 ///
24.25 - /// This class implements the \e radix \e heap data structure. A \e heap
24.26 - /// is a data structure for storing items with specified values called \e
24.27 - /// priorities in such a way that finding the item with minimum priority is
24.28 - /// efficient. This heap type can store only items with \e int priority.
24.29 - /// In a heap one can change the priority of an item, add or erase an
24.30 - /// item, but the priority cannot be decreased under the last removed
24.31 - /// item's priority.
24.32 + /// This class implements the \e radix \e heap data structure.
24.33 + /// It practically conforms to the \ref concepts::Heap "heap concept",
24.34 + /// but it has some limitations due its special implementation.
24.35 + /// The type of the priorities must be \c int and the priority of an
24.36 + /// item cannot be decreased under the priority of the last removed item.
24.37 ///
24.38 - /// \param IM A read and writable Item int map, used internally
24.39 - /// to handle the cross references.
24.40 - ///
24.41 - /// \see BinHeap
24.42 - /// \see Dijkstra
24.43 + /// \tparam IM A read-writable item map with \c int values, used
24.44 + /// internally to handle the cross references.
24.45 template <typename IM>
24.46 class RadixHeap {
24.47
24.48 public:
24.49 - typedef typename IM::Key Item;
24.50 +
24.51 + /// Type of the item-int map.
24.52 + typedef IM ItemIntMap;
24.53 + /// Type of the priorities.
24.54 typedef int Prio;
24.55 - typedef IM ItemIntMap;
24.56 + /// Type of the items stored in the heap.
24.57 + typedef typename ItemIntMap::Key Item;
24.58
24.59 /// \brief Exception thrown by RadixHeap.
24.60 ///
24.61 - /// This Exception is thrown when a smaller priority
24.62 - /// is inserted into the \e RadixHeap then the last time erased.
24.63 + /// This exception is thrown when an item is inserted into a
24.64 + /// RadixHeap with a priority smaller than the last erased one.
24.65 /// \see RadixHeap
24.66 -
24.67 - class UnderFlowPriorityError : public Exception {
24.68 + class PriorityUnderflowError : public Exception {
24.69 public:
24.70 virtual const char* what() const throw() {
24.71 - return "lemon::RadixHeap::UnderFlowPriorityError";
24.72 + return "lemon::RadixHeap::PriorityUnderflowError";
24.73 }
24.74 };
24.75
24.76 - /// \brief Type to represent the items states.
24.77 + /// \brief Type to represent the states of the items.
24.78 ///
24.79 - /// Each Item element have a state associated to it. It may be "in heap",
24.80 - /// "pre heap" or "post heap". The latter two are indifferent from the
24.81 + /// Each item has a state associated to it. It can be "in heap",
24.82 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
24.83 /// heap's point of view, but may be useful to the user.
24.84 ///
24.85 - /// The ItemIntMap \e should be initialized in such way that it maps
24.86 - /// PRE_HEAP (-1) to any element to be put in the heap...
24.87 + /// The item-int map must be initialized in such way that it assigns
24.88 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
24.89 enum State {
24.90 - IN_HEAP = 0,
24.91 - PRE_HEAP = -1,
24.92 - POST_HEAP = -2
24.93 + IN_HEAP = 0, ///< = 0.
24.94 + PRE_HEAP = -1, ///< = -1.
24.95 + POST_HEAP = -2 ///< = -2.
24.96 };
24.97
24.98 private:
24.99 @@ -96,52 +94,55 @@
24.100 RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
24.101 };
24.102
24.103 - std::vector<RadixItem> data;
24.104 - std::vector<RadixBox> boxes;
24.105 + std::vector<RadixItem> _data;
24.106 + std::vector<RadixBox> _boxes;
24.107
24.108 ItemIntMap &_iim;
24.109
24.110 + public:
24.111
24.112 - public:
24.113 - /// \brief The constructor.
24.114 + /// \brief Constructor.
24.115 ///
24.116 - /// The constructor.
24.117 - ///
24.118 - /// \param map It should be given to the constructor, since it is used
24.119 - /// internally to handle the cross references. The value of the map
24.120 - /// should be PRE_HEAP (-1) for each element.
24.121 - ///
24.122 - /// \param minimal The initial minimal value of the heap.
24.123 - /// \param capacity It determines the initial capacity of the heap.
24.124 - RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0)
24.125 - : _iim(map) {
24.126 - boxes.push_back(RadixBox(minimal, 1));
24.127 - boxes.push_back(RadixBox(minimal + 1, 1));
24.128 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
24.129 + /// Constructor.
24.130 + /// \param map A map that assigns \c int values to the items.
24.131 + /// It is used internally to handle the cross references.
24.132 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
24.133 + /// \param minimum The initial minimum value of the heap.
24.134 + /// \param capacity The initial capacity of the heap.
24.135 + RadixHeap(ItemIntMap &map, int minimum = 0, int capacity = 0)
24.136 + : _iim(map)
24.137 + {
24.138 + _boxes.push_back(RadixBox(minimum, 1));
24.139 + _boxes.push_back(RadixBox(minimum + 1, 1));
24.140 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
24.141 extend();
24.142 }
24.143 }
24.144
24.145 - /// The number of items stored in the heap.
24.146 + /// \brief The number of items stored in the heap.
24.147 ///
24.148 - /// \brief Returns the number of items stored in the heap.
24.149 - int size() const { return data.size(); }
24.150 - /// \brief Checks if the heap stores no items.
24.151 + /// This function returns the number of items stored in the heap.
24.152 + int size() const { return _data.size(); }
24.153 +
24.154 + /// \brief Check if the heap is empty.
24.155 ///
24.156 - /// Returns \c true if and only if the heap stores no items.
24.157 - bool empty() const { return data.empty(); }
24.158 + /// This function returns \c true if the heap is empty.
24.159 + bool empty() const { return _data.empty(); }
24.160
24.161 - /// \brief Make empty this heap.
24.162 + /// \brief Make the heap empty.
24.163 ///
24.164 - /// Make empty this heap. It does not change the cross reference
24.165 - /// map. If you want to reuse a heap what is not surely empty you
24.166 - /// should first clear the heap and after that you should set the
24.167 - /// cross reference map for each item to \c PRE_HEAP.
24.168 - void clear(int minimal = 0, int capacity = 0) {
24.169 - data.clear(); boxes.clear();
24.170 - boxes.push_back(RadixBox(minimal, 1));
24.171 - boxes.push_back(RadixBox(minimal + 1, 1));
24.172 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
24.173 + /// This functon makes the heap empty.
24.174 + /// It does not change the cross reference map. If you want to reuse
24.175 + /// a heap that is not surely empty, you should first clear it and
24.176 + /// then you should set the cross reference map to \c PRE_HEAP
24.177 + /// for each item.
24.178 + /// \param minimum The minimum value of the heap.
24.179 + /// \param capacity The capacity of the heap.
24.180 + void clear(int minimum = 0, int capacity = 0) {
24.181 + _data.clear(); _boxes.clear();
24.182 + _boxes.push_back(RadixBox(minimum, 1));
24.183 + _boxes.push_back(RadixBox(minimum + 1, 1));
24.184 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
24.185 extend();
24.186 }
24.187 }
24.188 @@ -149,255 +150,259 @@
24.189 private:
24.190
24.191 bool upper(int box, Prio pr) {
24.192 - return pr < boxes[box].min;
24.193 + return pr < _boxes[box].min;
24.194 }
24.195
24.196 bool lower(int box, Prio pr) {
24.197 - return pr >= boxes[box].min + boxes[box].size;
24.198 + return pr >= _boxes[box].min + _boxes[box].size;
24.199 }
24.200
24.201 - /// \brief Remove item from the box list.
24.202 + // Remove item from the box list
24.203 void remove(int index) {
24.204 - if (data[index].prev >= 0) {
24.205 - data[data[index].prev].next = data[index].next;
24.206 + if (_data[index].prev >= 0) {
24.207 + _data[_data[index].prev].next = _data[index].next;
24.208 } else {
24.209 - boxes[data[index].box].first = data[index].next;
24.210 + _boxes[_data[index].box].first = _data[index].next;
24.211 }
24.212 - if (data[index].next >= 0) {
24.213 - data[data[index].next].prev = data[index].prev;
24.214 + if (_data[index].next >= 0) {
24.215 + _data[_data[index].next].prev = _data[index].prev;
24.216 }
24.217 }
24.218
24.219 - /// \brief Insert item into the box list.
24.220 + // Insert item into the box list
24.221 void insert(int box, int index) {
24.222 - if (boxes[box].first == -1) {
24.223 - boxes[box].first = index;
24.224 - data[index].next = data[index].prev = -1;
24.225 + if (_boxes[box].first == -1) {
24.226 + _boxes[box].first = index;
24.227 + _data[index].next = _data[index].prev = -1;
24.228 } else {
24.229 - data[index].next = boxes[box].first;
24.230 - data[boxes[box].first].prev = index;
24.231 - data[index].prev = -1;
24.232 - boxes[box].first = index;
24.233 + _data[index].next = _boxes[box].first;
24.234 + _data[_boxes[box].first].prev = index;
24.235 + _data[index].prev = -1;
24.236 + _boxes[box].first = index;
24.237 }
24.238 - data[index].box = box;
24.239 + _data[index].box = box;
24.240 }
24.241
24.242 - /// \brief Add a new box to the box list.
24.243 + // Add a new box to the box list
24.244 void extend() {
24.245 - int min = boxes.back().min + boxes.back().size;
24.246 - int bs = 2 * boxes.back().size;
24.247 - boxes.push_back(RadixBox(min, bs));
24.248 + int min = _boxes.back().min + _boxes.back().size;
24.249 + int bs = 2 * _boxes.back().size;
24.250 + _boxes.push_back(RadixBox(min, bs));
24.251 }
24.252
24.253 - /// \brief Move an item up into the proper box.
24.254 - void bubble_up(int index) {
24.255 - if (!lower(data[index].box, data[index].prio)) return;
24.256 + // Move an item up into the proper box.
24.257 + void bubbleUp(int index) {
24.258 + if (!lower(_data[index].box, _data[index].prio)) return;
24.259 remove(index);
24.260 - int box = findUp(data[index].box, data[index].prio);
24.261 + int box = findUp(_data[index].box, _data[index].prio);
24.262 insert(box, index);
24.263 }
24.264
24.265 - /// \brief Find up the proper box for the item with the given prio.
24.266 + // Find up the proper box for the item with the given priority
24.267 int findUp(int start, int pr) {
24.268 while (lower(start, pr)) {
24.269 - if (++start == int(boxes.size())) {
24.270 + if (++start == int(_boxes.size())) {
24.271 extend();
24.272 }
24.273 }
24.274 return start;
24.275 }
24.276
24.277 - /// \brief Move an item down into the proper box.
24.278 - void bubble_down(int index) {
24.279 - if (!upper(data[index].box, data[index].prio)) return;
24.280 + // Move an item down into the proper box
24.281 + void bubbleDown(int index) {
24.282 + if (!upper(_data[index].box, _data[index].prio)) return;
24.283 remove(index);
24.284 - int box = findDown(data[index].box, data[index].prio);
24.285 + int box = findDown(_data[index].box, _data[index].prio);
24.286 insert(box, index);
24.287 }
24.288
24.289 - /// \brief Find up the proper box for the item with the given prio.
24.290 + // Find down the proper box for the item with the given priority
24.291 int findDown(int start, int pr) {
24.292 while (upper(start, pr)) {
24.293 - if (--start < 0) throw UnderFlowPriorityError();
24.294 + if (--start < 0) throw PriorityUnderflowError();
24.295 }
24.296 return start;
24.297 }
24.298
24.299 - /// \brief Find the first not empty box.
24.300 + // Find the first non-empty box
24.301 int findFirst() {
24.302 int first = 0;
24.303 - while (boxes[first].first == -1) ++first;
24.304 + while (_boxes[first].first == -1) ++first;
24.305 return first;
24.306 }
24.307
24.308 - /// \brief Gives back the minimal prio of the box.
24.309 + // Gives back the minimum priority of the given box
24.310 int minValue(int box) {
24.311 - int min = data[boxes[box].first].prio;
24.312 - for (int k = boxes[box].first; k != -1; k = data[k].next) {
24.313 - if (data[k].prio < min) min = data[k].prio;
24.314 + int min = _data[_boxes[box].first].prio;
24.315 + for (int k = _boxes[box].first; k != -1; k = _data[k].next) {
24.316 + if (_data[k].prio < min) min = _data[k].prio;
24.317 }
24.318 return min;
24.319 }
24.320
24.321 - /// \brief Rearrange the items of the heap and makes the
24.322 - /// first box not empty.
24.323 + // Rearrange the items of the heap and make the first box non-empty
24.324 void moveDown() {
24.325 int box = findFirst();
24.326 if (box == 0) return;
24.327 int min = minValue(box);
24.328 for (int i = 0; i <= box; ++i) {
24.329 - boxes[i].min = min;
24.330 - min += boxes[i].size;
24.331 + _boxes[i].min = min;
24.332 + min += _boxes[i].size;
24.333 }
24.334 - int curr = boxes[box].first, next;
24.335 + int curr = _boxes[box].first, next;
24.336 while (curr != -1) {
24.337 - next = data[curr].next;
24.338 - bubble_down(curr);
24.339 + next = _data[curr].next;
24.340 + bubbleDown(curr);
24.341 curr = next;
24.342 }
24.343 }
24.344
24.345 - void relocate_last(int index) {
24.346 - if (index != int(data.size()) - 1) {
24.347 - data[index] = data.back();
24.348 - if (data[index].prev != -1) {
24.349 - data[data[index].prev].next = index;
24.350 + void relocateLast(int index) {
24.351 + if (index != int(_data.size()) - 1) {
24.352 + _data[index] = _data.back();
24.353 + if (_data[index].prev != -1) {
24.354 + _data[_data[index].prev].next = index;
24.355 } else {
24.356 - boxes[data[index].box].first = index;
24.357 + _boxes[_data[index].box].first = index;
24.358 }
24.359 - if (data[index].next != -1) {
24.360 - data[data[index].next].prev = index;
24.361 + if (_data[index].next != -1) {
24.362 + _data[_data[index].next].prev = index;
24.363 }
24.364 - _iim[data[index].item] = index;
24.365 + _iim[_data[index].item] = index;
24.366 }
24.367 - data.pop_back();
24.368 + _data.pop_back();
24.369 }
24.370
24.371 public:
24.372
24.373 /// \brief Insert an item into the heap with the given priority.
24.374 ///
24.375 - /// Adds \c i to the heap with priority \c p.
24.376 + /// This function inserts the given item into the heap with the
24.377 + /// given priority.
24.378 /// \param i The item to insert.
24.379 /// \param p The priority of the item.
24.380 + /// \pre \e i must not be stored in the heap.
24.381 + /// \warning This method may throw an \c UnderFlowPriorityException.
24.382 void push(const Item &i, const Prio &p) {
24.383 - int n = data.size();
24.384 + int n = _data.size();
24.385 _iim.set(i, n);
24.386 - data.push_back(RadixItem(i, p));
24.387 - while (lower(boxes.size() - 1, p)) {
24.388 + _data.push_back(RadixItem(i, p));
24.389 + while (lower(_boxes.size() - 1, p)) {
24.390 extend();
24.391 }
24.392 - int box = findDown(boxes.size() - 1, p);
24.393 + int box = findDown(_boxes.size() - 1, p);
24.394 insert(box, n);
24.395 }
24.396
24.397 - /// \brief Returns the item with minimum priority.
24.398 + /// \brief Return the item having minimum priority.
24.399 ///
24.400 - /// This method returns the item with minimum priority.
24.401 - /// \pre The heap must be nonempty.
24.402 + /// This function returns the item having minimum priority.
24.403 + /// \pre The heap must be non-empty.
24.404 Item top() const {
24.405 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
24.406 - return data[boxes[0].first].item;
24.407 + return _data[_boxes[0].first].item;
24.408 }
24.409
24.410 - /// \brief Returns the minimum priority.
24.411 + /// \brief The minimum priority.
24.412 ///
24.413 - /// It returns the minimum priority.
24.414 - /// \pre The heap must be nonempty.
24.415 + /// This function returns the minimum priority.
24.416 + /// \pre The heap must be non-empty.
24.417 Prio prio() const {
24.418 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
24.419 - return data[boxes[0].first].prio;
24.420 + return _data[_boxes[0].first].prio;
24.421 }
24.422
24.423 - /// \brief Deletes the item with minimum priority.
24.424 + /// \brief Remove the item having minimum priority.
24.425 ///
24.426 - /// This method deletes the item with minimum priority.
24.427 + /// This function removes the item having minimum priority.
24.428 /// \pre The heap must be non-empty.
24.429 void pop() {
24.430 moveDown();
24.431 - int index = boxes[0].first;
24.432 - _iim[data[index].item] = POST_HEAP;
24.433 + int index = _boxes[0].first;
24.434 + _iim[_data[index].item] = POST_HEAP;
24.435 remove(index);
24.436 - relocate_last(index);
24.437 + relocateLast(index);
24.438 }
24.439
24.440 - /// \brief Deletes \c i from the heap.
24.441 + /// \brief Remove the given item from the heap.
24.442 ///
24.443 - /// This method deletes item \c i from the heap, if \c i was
24.444 - /// already stored in the heap.
24.445 - /// \param i The item to erase.
24.446 + /// This function removes the given item from the heap if it is
24.447 + /// already stored.
24.448 + /// \param i The item to delete.
24.449 + /// \pre \e i must be in the heap.
24.450 void erase(const Item &i) {
24.451 int index = _iim[i];
24.452 _iim[i] = POST_HEAP;
24.453 remove(index);
24.454 - relocate_last(index);
24.455 + relocateLast(index);
24.456 }
24.457
24.458 - /// \brief Returns the priority of \c i.
24.459 + /// \brief The priority of the given item.
24.460 ///
24.461 - /// This function returns the priority of item \c i.
24.462 - /// \pre \c i must be in the heap.
24.463 + /// This function returns the priority of the given item.
24.464 /// \param i The item.
24.465 + /// \pre \e i must be in the heap.
24.466 Prio operator[](const Item &i) const {
24.467 int idx = _iim[i];
24.468 - return data[idx].prio;
24.469 + return _data[idx].prio;
24.470 }
24.471
24.472 - /// \brief \c i gets to the heap with priority \c p independently
24.473 - /// if \c i was already there.
24.474 + /// \brief Set the priority of an item or insert it, if it is
24.475 + /// not stored in the heap.
24.476 ///
24.477 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
24.478 - /// in the heap and sets the priority of \c i to \c p otherwise.
24.479 - /// It may throw an \e UnderFlowPriorityException.
24.480 + /// This method sets the priority of the given item if it is
24.481 + /// already stored in the heap. Otherwise it inserts the given
24.482 + /// item into the heap with the given priority.
24.483 /// \param i The item.
24.484 /// \param p The priority.
24.485 + /// \pre \e i must be in the heap.
24.486 + /// \warning This method may throw an \c UnderFlowPriorityException.
24.487 void set(const Item &i, const Prio &p) {
24.488 int idx = _iim[i];
24.489 if( idx < 0 ) {
24.490 push(i, p);
24.491 }
24.492 - else if( p >= data[idx].prio ) {
24.493 - data[idx].prio = p;
24.494 - bubble_up(idx);
24.495 + else if( p >= _data[idx].prio ) {
24.496 + _data[idx].prio = p;
24.497 + bubbleUp(idx);
24.498 } else {
24.499 - data[idx].prio = p;
24.500 - bubble_down(idx);
24.501 + _data[idx].prio = p;
24.502 + bubbleDown(idx);
24.503 }
24.504 }
24.505
24.506 -
24.507 - /// \brief Decreases the priority of \c i to \c p.
24.508 + /// \brief Decrease the priority of an item to the given value.
24.509 ///
24.510 - /// This method decreases the priority of item \c i to \c p.
24.511 - /// \pre \c i must be stored in the heap with priority at least \c p, and
24.512 - /// \c should be greater or equal to the last removed item's priority.
24.513 + /// This function decreases the priority of an item to the given value.
24.514 /// \param i The item.
24.515 /// \param p The priority.
24.516 + /// \pre \e i must be stored in the heap with priority at least \e p.
24.517 + /// \warning This method may throw an \c UnderFlowPriorityException.
24.518 void decrease(const Item &i, const Prio &p) {
24.519 int idx = _iim[i];
24.520 - data[idx].prio = p;
24.521 - bubble_down(idx);
24.522 + _data[idx].prio = p;
24.523 + bubbleDown(idx);
24.524 }
24.525
24.526 - /// \brief Increases the priority of \c i to \c p.
24.527 + /// \brief Increase the priority of an item to the given value.
24.528 ///
24.529 - /// This method sets the priority of item \c i to \c p.
24.530 - /// \pre \c i must be stored in the heap with priority at most \c p
24.531 + /// This function increases the priority of an item to the given value.
24.532 /// \param i The item.
24.533 /// \param p The priority.
24.534 + /// \pre \e i must be stored in the heap with priority at most \e p.
24.535 void increase(const Item &i, const Prio &p) {
24.536 int idx = _iim[i];
24.537 - data[idx].prio = p;
24.538 - bubble_up(idx);
24.539 + _data[idx].prio = p;
24.540 + bubbleUp(idx);
24.541 }
24.542
24.543 - /// \brief Returns if \c item is in, has already been in, or has
24.544 - /// never been in the heap.
24.545 + /// \brief Return the state of an item.
24.546 ///
24.547 - /// This method returns PRE_HEAP if \c item has never been in the
24.548 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
24.549 - /// otherwise. In the latter case it is possible that \c item will
24.550 - /// get back to the heap again.
24.551 + /// This method returns \c PRE_HEAP if the given item has never
24.552 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
24.553 + /// and \c POST_HEAP otherwise.
24.554 + /// In the latter case it is possible that the item will get back
24.555 + /// to the heap again.
24.556 /// \param i The item.
24.557 State state(const Item &i) const {
24.558 int s = _iim[i];
24.559 @@ -405,11 +410,11 @@
24.560 return State(s);
24.561 }
24.562
24.563 - /// \brief Sets the state of the \c item in the heap.
24.564 + /// \brief Set the state of an item in the heap.
24.565 ///
24.566 - /// Sets the state of the \c item in the heap. It can be used to
24.567 - /// manually clear the heap when it is important to achive the
24.568 - /// better time complexity.
24.569 + /// This function sets the state of the given item in the heap.
24.570 + /// It can be used to manually clear the heap when it is important
24.571 + /// to achive better time complexity.
24.572 /// \param i The item.
24.573 /// \param st The state. It should not be \c IN_HEAP.
24.574 void state(const Item& i, State st) {
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
25.2 +++ b/scripts/bootstrap.sh Wed Sep 30 08:41:06 2009 +0200
25.3 @@ -0,0 +1,116 @@
25.4 +#!/bin/bash
25.5 +#
25.6 +# This file is a part of LEMON, a generic C++ optimization library.
25.7 +#
25.8 +# Copyright (C) 2003-2009
25.9 +# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
25.10 +# (Egervary Research Group on Combinatorial Optimization, EGRES).
25.11 +#
25.12 +# Permission to use, modify and distribute this software is granted
25.13 +# provided that this copyright notice appears in all copies. For
25.14 +# precise terms see the accompanying LICENSE file.
25.15 +#
25.16 +# This software is provided "AS IS" with no warranty of any kind,
25.17 +# express or implied, and with no claim as to its suitability for any
25.18 +# purpose.
25.19 +
25.20 +
25.21 +if [ ! -f ~/.lemon-bootstrap ]; then
25.22 + echo 'Create ~/.lemon-bootstrap'.
25.23 + cat >~/.lemon-bootstrap <<EOF
25.24 +#
25.25 +# Default settings for bootstraping the LEMON source code repository
25.26 +#
25.27 +EOF
25.28 +fi
25.29 +
25.30 +source ~/.lemon-bootstrap
25.31 +if [ -f ../../../.lemon-bootstrap ]; then source ../../../.lemon-bootstrap; fi
25.32 +if [ -f ../../.lemon-bootstrap ]; then source ../../.lemon-bootstrap; fi
25.33 +if [ -f ../.lemon-bootstrap ]; then source ../.lemon-bootstrap; fi
25.34 +if [ -f ./.lemon-bootstrap ]; then source ./.lemon-bootstrap; fi
25.35 +
25.36 +
25.37 +function augment_config() {
25.38 + if [ "x${!1}" == "x" ]; then
25.39 + eval $1=$2
25.40 + echo Add "'$1'" to '~/.lemon-bootstrap'.
25.41 + echo >>~/.lemon-bootstrap
25.42 + echo $3 >>~/.lemon-bootstrap
25.43 + echo $1=$2 >>~/.lemon-bootstrap
25.44 + fi
25.45 +}
25.46 +
25.47 +augment_config LEMON_INSTALL_PREFIX /usr/local \
25.48 + "# LEMON installation prefix"
25.49 +
25.50 +augment_config COIN_OR_PREFIX /usr/local/coin-or \
25.51 + "# COIN-OR installation root prefix (used for CLP/CBC)"
25.52 +
25.53 +
25.54 +function ask() {
25.55 +echo -n "$1 [$2]? "
25.56 +read _an
25.57 +if [ "x$_an" == "x" ]; then
25.58 + ret="$2"
25.59 +else
25.60 + ret=$_an
25.61 +fi
25.62 +}
25.63 +
25.64 +function yesorno() {
25.65 + ret='rossz'
25.66 + while [ "$ret" != "y" -a "$ret" != "n" -a "$ret" != "yes" -a "$ret" != "no" ]; do
25.67 + ask "$1" "$2"
25.68 + done
25.69 + if [ "$ret" != "y" -a "$ret" != "yes" ]; then
25.70 + return 1
25.71 + else
25.72 + return 0
25.73 + fi
25.74 +}
25.75 +
25.76 +if yesorno "External build" "n"
25.77 +then
25.78 + CONFIGURE_PATH=".."
25.79 +else
25.80 + CONFIGURE_PATH="."
25.81 + if yesorno "Autoreconf" "y"
25.82 + then
25.83 + AUTORE=yes
25.84 + else
25.85 + AUTORE=no
25.86 + fi
25.87 +fi
25.88 +
25.89 +if yesorno "Optimize" "n"
25.90 +then
25.91 + opt_flags=' -O2'
25.92 +else
25.93 + opt_flags=''
25.94 +fi
25.95 +
25.96 +if yesorno "Stop on warning" "y"
25.97 +then
25.98 + werror_flags=' -Werror'
25.99 +else
25.100 + werror_flags=''
25.101 +fi
25.102 +
25.103 +cxx_flags="CXXFLAGS=-ggdb$opt_flags$werror_flags"
25.104 +
25.105 +if yesorno "Use COIN" "n"
25.106 +then
25.107 + coin_flag="--with-coin=$COIN_OR_PREFIX"
25.108 +else
25.109 + coin_flag=""
25.110 +fi
25.111 +
25.112 +
25.113 +if [ "x$AUTORE" == "xyes" ]; then
25.114 + autoreconf -vif;
25.115 +fi
25.116 +${CONFIGURE_PATH}/configure --prefix=$LEMON_INSTALL_PREFIX \
25.117 +"$cxx_flags" \
25.118 +$coin_flag \
25.119 +$*
26.1 --- a/scripts/chg-len.py Wed Sep 30 08:36:43 2009 +0200
26.2 +++ b/scripts/chg-len.py Wed Sep 30 08:41:06 2009 +0200
26.3 @@ -1,4 +1,18 @@
26.4 #! /usr/bin/env python
26.5 +#
26.6 +# This file is a part of LEMON, a generic C++ optimization library.
26.7 +#
26.8 +# Copyright (C) 2003-2009
26.9 +# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
26.10 +# (Egervary Research Group on Combinatorial Optimization, EGRES).
26.11 +#
26.12 +# Permission to use, modify and distribute this software is granted
26.13 +# provided that this copyright notice appears in all copies. For
26.14 +# precise terms see the accompanying LICENSE file.
26.15 +#
26.16 +# This software is provided "AS IS" with no warranty of any kind,
26.17 +# express or implied, and with no claim as to its suitability for any
26.18 +# purpose.
26.19
26.20 import sys
26.21
27.1 --- a/scripts/mk-release.sh Wed Sep 30 08:36:43 2009 +0200
27.2 +++ b/scripts/mk-release.sh Wed Sep 30 08:41:06 2009 +0200
27.3 @@ -1,4 +1,18 @@
27.4 #!/bin/bash
27.5 +#
27.6 +# This file is a part of LEMON, a generic C++ optimization library.
27.7 +#
27.8 +# Copyright (C) 2003-2009
27.9 +# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
27.10 +# (Egervary Research Group on Combinatorial Optimization, EGRES).
27.11 +#
27.12 +# Permission to use, modify and distribute this software is granted
27.13 +# provided that this copyright notice appears in all copies. For
27.14 +# precise terms see the accompanying LICENSE file.
27.15 +#
27.16 +# This software is provided "AS IS" with no warranty of any kind,
27.17 +# express or implied, and with no claim as to its suitability for any
27.18 +# purpose.
27.19
27.20 set -e
27.21
28.1 --- a/scripts/unify-sources.sh Wed Sep 30 08:36:43 2009 +0200
28.2 +++ b/scripts/unify-sources.sh Wed Sep 30 08:41:06 2009 +0200
28.3 @@ -1,4 +1,18 @@
28.4 #!/bin/bash
28.5 +#
28.6 +# This file is a part of LEMON, a generic C++ optimization library.
28.7 +#
28.8 +# Copyright (C) 2003-2009
28.9 +# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
28.10 +# (Egervary Research Group on Combinatorial Optimization, EGRES).
28.11 +#
28.12 +# Permission to use, modify and distribute this software is granted
28.13 +# provided that this copyright notice appears in all copies. For
28.14 +# precise terms see the accompanying LICENSE file.
28.15 +#
28.16 +# This software is provided "AS IS" with no warranty of any kind,
28.17 +# express or implied, and with no claim as to its suitability for any
28.18 +# purpose.
28.19
28.20 YEAR=`date +%Y`
28.21 HGROOT=`hg root`
29.1 --- a/test/CMakeLists.txt Wed Sep 30 08:36:43 2009 +0200
29.2 +++ b/test/CMakeLists.txt Wed Sep 30 08:41:06 2009 +0200
29.3 @@ -9,6 +9,7 @@
29.4
29.5 SET(TESTS
29.6 adaptors_test
29.7 + bellman_ford_test
29.8 bfs_test
29.9 circulation_test
29.10 connectivity_test
30.1 --- a/test/Makefile.am Wed Sep 30 08:36:43 2009 +0200
30.2 +++ b/test/Makefile.am Wed Sep 30 08:41:06 2009 +0200
30.3 @@ -7,6 +7,7 @@
30.4
30.5 check_PROGRAMS += \
30.6 test/adaptors_test \
30.7 + test/bellman_ford_test \
30.8 test/bfs_test \
30.9 test/circulation_test \
30.10 test/connectivity_test \
30.11 @@ -52,6 +53,7 @@
30.12 XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
30.13
30.14 test_adaptors_test_SOURCES = test/adaptors_test.cc
30.15 +test_bellman_ford_test_SOURCES = test/bellman_ford_test.cc
30.16 test_bfs_test_SOURCES = test/bfs_test.cc
30.17 test_circulation_test_SOURCES = test/circulation_test.cc
30.18 test_counter_test_SOURCES = test/counter_test.cc
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
31.2 +++ b/test/bellman_ford_test.cc Wed Sep 30 08:41:06 2009 +0200
31.3 @@ -0,0 +1,283 @@
31.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
31.5 + *
31.6 + * This file is a part of LEMON, a generic C++ optimization library.
31.7 + *
31.8 + * Copyright (C) 2003-2009
31.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
31.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
31.11 + *
31.12 + * Permission to use, modify and distribute this software is granted
31.13 + * provided that this copyright notice appears in all copies. For
31.14 + * precise terms see the accompanying LICENSE file.
31.15 + *
31.16 + * This software is provided "AS IS" with no warranty of any kind,
31.17 + * express or implied, and with no claim as to its suitability for any
31.18 + * purpose.
31.19 + *
31.20 + */
31.21 +
31.22 +#include <lemon/concepts/digraph.h>
31.23 +#include <lemon/smart_graph.h>
31.24 +#include <lemon/list_graph.h>
31.25 +#include <lemon/lgf_reader.h>
31.26 +#include <lemon/bellman_ford.h>
31.27 +#include <lemon/path.h>
31.28 +
31.29 +#include "graph_test.h"
31.30 +#include "test_tools.h"
31.31 +
31.32 +using namespace lemon;
31.33 +
31.34 +char test_lgf[] =
31.35 + "@nodes\n"
31.36 + "label\n"
31.37 + "0\n"
31.38 + "1\n"
31.39 + "2\n"
31.40 + "3\n"
31.41 + "4\n"
31.42 + "@arcs\n"
31.43 + " length\n"
31.44 + "0 1 3\n"
31.45 + "1 2 -3\n"
31.46 + "1 2 -5\n"
31.47 + "1 3 -2\n"
31.48 + "0 2 -1\n"
31.49 + "1 2 -4\n"
31.50 + "0 3 2\n"
31.51 + "4 2 -5\n"
31.52 + "2 3 1\n"
31.53 + "@attributes\n"
31.54 + "source 0\n"
31.55 + "target 3\n";
31.56 +
31.57 +
31.58 +void checkBellmanFordCompile()
31.59 +{
31.60 + typedef int Value;
31.61 + typedef concepts::Digraph Digraph;
31.62 + typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
31.63 + typedef BellmanFord<Digraph, LengthMap> BF;
31.64 + typedef Digraph::Node Node;
31.65 + typedef Digraph::Arc Arc;
31.66 +
31.67 + Digraph gr;
31.68 + Node s, t, n;
31.69 + Arc e;
31.70 + Value l;
31.71 + int k;
31.72 + bool b;
31.73 + BF::DistMap d(gr);
31.74 + BF::PredMap p(gr);
31.75 + LengthMap length;
31.76 + concepts::Path<Digraph> pp;
31.77 +
31.78 + {
31.79 + BF bf_test(gr,length);
31.80 + const BF& const_bf_test = bf_test;
31.81 +
31.82 + bf_test.run(s);
31.83 + bf_test.run(s,k);
31.84 +
31.85 + bf_test.init();
31.86 + bf_test.addSource(s);
31.87 + bf_test.addSource(s, 1);
31.88 + b = bf_test.processNextRound();
31.89 + b = bf_test.processNextWeakRound();
31.90 +
31.91 + bf_test.start();
31.92 + bf_test.checkedStart();
31.93 + bf_test.limitedStart(k);
31.94 +
31.95 + l = const_bf_test.dist(t);
31.96 + e = const_bf_test.predArc(t);
31.97 + s = const_bf_test.predNode(t);
31.98 + b = const_bf_test.reached(t);
31.99 + d = const_bf_test.distMap();
31.100 + p = const_bf_test.predMap();
31.101 + pp = const_bf_test.path(t);
31.102 +
31.103 + for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
31.104 + }
31.105 + {
31.106 + BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
31.107 + ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
31.108 + ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
31.109 + ::Create bf_test(gr,length);
31.110 +
31.111 + LengthMap length_map;
31.112 + concepts::ReadWriteMap<Node,Arc> pred_map;
31.113 + concepts::ReadWriteMap<Node,Value> dist_map;
31.114 +
31.115 + bf_test
31.116 + .lengthMap(length_map)
31.117 + .predMap(pred_map)
31.118 + .distMap(dist_map);
31.119 +
31.120 + bf_test.run(s);
31.121 + bf_test.run(s,k);
31.122 +
31.123 + bf_test.init();
31.124 + bf_test.addSource(s);
31.125 + bf_test.addSource(s, 1);
31.126 + b = bf_test.processNextRound();
31.127 + b = bf_test.processNextWeakRound();
31.128 +
31.129 + bf_test.start();
31.130 + bf_test.checkedStart();
31.131 + bf_test.limitedStart(k);
31.132 +
31.133 + l = bf_test.dist(t);
31.134 + e = bf_test.predArc(t);
31.135 + s = bf_test.predNode(t);
31.136 + b = bf_test.reached(t);
31.137 + pp = bf_test.path(t);
31.138 + }
31.139 +}
31.140 +
31.141 +void checkBellmanFordFunctionCompile()
31.142 +{
31.143 + typedef int Value;
31.144 + typedef concepts::Digraph Digraph;
31.145 + typedef Digraph::Arc Arc;
31.146 + typedef Digraph::Node Node;
31.147 + typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
31.148 +
31.149 + Digraph g;
31.150 + bool b;
31.151 + bellmanFord(g,LengthMap()).run(Node());
31.152 + b = bellmanFord(g,LengthMap()).run(Node(),Node());
31.153 + bellmanFord(g,LengthMap())
31.154 + .predMap(concepts::ReadWriteMap<Node,Arc>())
31.155 + .distMap(concepts::ReadWriteMap<Node,Value>())
31.156 + .run(Node());
31.157 + b=bellmanFord(g,LengthMap())
31.158 + .predMap(concepts::ReadWriteMap<Node,Arc>())
31.159 + .distMap(concepts::ReadWriteMap<Node,Value>())
31.160 + .path(concepts::Path<Digraph>())
31.161 + .dist(Value())
31.162 + .run(Node(),Node());
31.163 +}
31.164 +
31.165 +
31.166 +template <typename Digraph, typename Value>
31.167 +void checkBellmanFord() {
31.168 + TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
31.169 + typedef typename Digraph::template ArcMap<Value> LengthMap;
31.170 +
31.171 + Digraph gr;
31.172 + Node s, t;
31.173 + LengthMap length(gr);
31.174 +
31.175 + std::istringstream input(test_lgf);
31.176 + digraphReader(gr, input).
31.177 + arcMap("length", length).
31.178 + node("source", s).
31.179 + node("target", t).
31.180 + run();
31.181 +
31.182 + BellmanFord<Digraph, LengthMap>
31.183 + bf(gr, length);
31.184 + bf.run(s);
31.185 + Path<Digraph> p = bf.path(t);
31.186 +
31.187 + check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
31.188 + check(p.length() == 3, "path() found a wrong path.");
31.189 + check(checkPath(gr, p), "path() found a wrong path.");
31.190 + check(pathSource(gr, p) == s, "path() found a wrong path.");
31.191 + check(pathTarget(gr, p) == t, "path() found a wrong path.");
31.192 +
31.193 + ListPath<Digraph> path;
31.194 + Value dist;
31.195 + bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
31.196 +
31.197 + check(reached && dist == -1, "Bellman-Ford found a wrong path.");
31.198 + check(path.length() == 3, "path() found a wrong path.");
31.199 + check(checkPath(gr, path), "path() found a wrong path.");
31.200 + check(pathSource(gr, path) == s, "path() found a wrong path.");
31.201 + check(pathTarget(gr, path) == t, "path() found a wrong path.");
31.202 +
31.203 + for(ArcIt e(gr); e!=INVALID; ++e) {
31.204 + Node u=gr.source(e);
31.205 + Node v=gr.target(e);
31.206 + check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
31.207 + "Wrong output. dist(target)-dist(source)-arc_length=" <<
31.208 + bf.dist(v) - bf.dist(u) - length[e]);
31.209 + }
31.210 +
31.211 + for(NodeIt v(gr); v!=INVALID; ++v) {
31.212 + if (bf.reached(v)) {
31.213 + check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
31.214 + if (bf.predArc(v)!=INVALID ) {
31.215 + Arc e=bf.predArc(v);
31.216 + Node u=gr.source(e);
31.217 + check(u==bf.predNode(v),"Wrong tree.");
31.218 + check(bf.dist(v) - bf.dist(u) == length[e],
31.219 + "Wrong distance! Difference: " <<
31.220 + bf.dist(v) - bf.dist(u) - length[e]);
31.221 + }
31.222 + }
31.223 + }
31.224 +}
31.225 +
31.226 +void checkBellmanFordNegativeCycle() {
31.227 + DIGRAPH_TYPEDEFS(SmartDigraph);
31.228 +
31.229 + SmartDigraph gr;
31.230 + IntArcMap length(gr);
31.231 +
31.232 + Node n1 = gr.addNode();
31.233 + Node n2 = gr.addNode();
31.234 + Node n3 = gr.addNode();
31.235 + Node n4 = gr.addNode();
31.236 +
31.237 + Arc a1 = gr.addArc(n1, n2);
31.238 + Arc a2 = gr.addArc(n2, n2);
31.239 +
31.240 + length[a1] = 2;
31.241 + length[a2] = -1;
31.242 +
31.243 + {
31.244 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
31.245 + bf.run(n1);
31.246 + StaticPath<SmartDigraph> p = bf.negativeCycle();
31.247 + check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
31.248 + "Wrong negative cycle.");
31.249 + }
31.250 +
31.251 + length[a2] = 0;
31.252 +
31.253 + {
31.254 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
31.255 + bf.run(n1);
31.256 + check(bf.negativeCycle().empty(),
31.257 + "Negative cycle should not be found.");
31.258 + }
31.259 +
31.260 + length[gr.addArc(n1, n3)] = 5;
31.261 + length[gr.addArc(n4, n3)] = 1;
31.262 + length[gr.addArc(n2, n4)] = 2;
31.263 + length[gr.addArc(n3, n2)] = -4;
31.264 +
31.265 + {
31.266 + BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
31.267 + bf.init();
31.268 + bf.addSource(n1);
31.269 + for (int i = 0; i < 4; ++i) {
31.270 + check(bf.negativeCycle().empty(),
31.271 + "Negative cycle should not be found.");
31.272 + bf.processNextRound();
31.273 + }
31.274 + StaticPath<SmartDigraph> p = bf.negativeCycle();
31.275 + check(p.length() == 3, "Wrong negative cycle.");
31.276 + check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
31.277 + "Wrong negative cycle.");
31.278 + }
31.279 +}
31.280 +
31.281 +int main() {
31.282 + checkBellmanFord<ListDigraph, int>();
31.283 + checkBellmanFord<SmartDigraph, double>();
31.284 + checkBellmanFordNegativeCycle();
31.285 + return 0;
31.286 +}
32.1 --- a/test/circulation_test.cc Wed Sep 30 08:36:43 2009 +0200
32.2 +++ b/test/circulation_test.cc Wed Sep 30 08:41:06 2009 +0200
32.3 @@ -87,6 +87,11 @@
32.4 .upperMap(ucap)
32.5 .supplyMap(supply)
32.6 .flowMap(flow);
32.7 +
32.8 + const CirculationType::Elevator& elev = const_circ_test.elevator();
32.9 + circ_test.elevator(const_cast<CirculationType::Elevator&>(elev));
32.10 + CirculationType::Tolerance tol = const_circ_test.tolerance();
32.11 + circ_test.tolerance(tol);
32.12
32.13 circ_test.init();
32.14 circ_test.greedyInit();
33.1 --- a/test/heap_test.cc Wed Sep 30 08:36:43 2009 +0200
33.2 +++ b/test/heap_test.cc Wed Sep 30 08:41:06 2009 +0200
33.3 @@ -25,14 +25,17 @@
33.4 #include <lemon/concepts/heap.h>
33.5
33.6 #include <lemon/smart_graph.h>
33.7 -
33.8 #include <lemon/lgf_reader.h>
33.9 #include <lemon/dijkstra.h>
33.10 #include <lemon/maps.h>
33.11
33.12 #include <lemon/bin_heap.h>
33.13 +#include <lemon/fourary_heap.h>
33.14 +#include <lemon/kary_heap.h>
33.15 #include <lemon/fib_heap.h>
33.16 +#include <lemon/pairing_heap.h>
33.17 #include <lemon/radix_heap.h>
33.18 +#include <lemon/binom_heap.h>
33.19 #include <lemon/bucket_heap.h>
33.20
33.21 #include "test_tools.h"
33.22 @@ -89,18 +92,16 @@
33.23 template <typename Heap>
33.24 void heapSortTest() {
33.25 RangeMap<int> map(test_len, -1);
33.26 -
33.27 Heap heap(map);
33.28
33.29 std::vector<int> v(test_len);
33.30 -
33.31 for (int i = 0; i < test_len; ++i) {
33.32 v[i] = test_seq[i];
33.33 heap.push(i, v[i]);
33.34 }
33.35 std::sort(v.begin(), v.end());
33.36 for (int i = 0; i < test_len; ++i) {
33.37 - check(v[i] == heap.prio() ,"Wrong order in heap sort.");
33.38 + check(v[i] == heap.prio(), "Wrong order in heap sort.");
33.39 heap.pop();
33.40 }
33.41 }
33.42 @@ -112,7 +113,6 @@
33.43 Heap heap(map);
33.44
33.45 std::vector<int> v(test_len);
33.46 -
33.47 for (int i = 0; i < test_len; ++i) {
33.48 v[i] = test_seq[i];
33.49 heap.push(i, v[i]);
33.50 @@ -123,13 +123,11 @@
33.51 }
33.52 std::sort(v.begin(), v.end());
33.53 for (int i = 0; i < test_len; ++i) {
33.54 - check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
33.55 + check(v[i] == heap.prio(), "Wrong order in heap increase test.");
33.56 heap.pop();
33.57 }
33.58 }
33.59
33.60 -
33.61 -
33.62 template <typename Heap>
33.63 void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
33.64 Node source) {
33.65 @@ -144,7 +142,7 @@
33.66 Node t = digraph.target(a);
33.67 if (dijkstra.reached(s)) {
33.68 check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
33.69 - "Error in a shortest path tree!");
33.70 + "Error in shortest path tree.");
33.71 }
33.72 }
33.73
33.74 @@ -153,7 +151,7 @@
33.75 Arc a = dijkstra.predArc(n);
33.76 Node s = digraph.source(a);
33.77 check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
33.78 - "Error in a shortest path tree!");
33.79 + "Error in shortest path tree.");
33.80 }
33.81 }
33.82
33.83 @@ -175,6 +173,7 @@
33.84 node("source", source).
33.85 run();
33.86
33.87 + // BinHeap
33.88 {
33.89 typedef BinHeap<Prio, ItemIntMap> IntHeap;
33.90 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.91 @@ -186,6 +185,31 @@
33.92 dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.93 }
33.94
33.95 + // FouraryHeap
33.96 + {
33.97 + typedef FouraryHeap<Prio, ItemIntMap> IntHeap;
33.98 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.99 + heapSortTest<IntHeap>();
33.100 + heapIncreaseTest<IntHeap>();
33.101 +
33.102 + typedef FouraryHeap<Prio, IntNodeMap > NodeHeap;
33.103 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
33.104 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.105 + }
33.106 +
33.107 + // KaryHeap
33.108 + {
33.109 + typedef KaryHeap<Prio, ItemIntMap> IntHeap;
33.110 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.111 + heapSortTest<IntHeap>();
33.112 + heapIncreaseTest<IntHeap>();
33.113 +
33.114 + typedef KaryHeap<Prio, IntNodeMap > NodeHeap;
33.115 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
33.116 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.117 + }
33.118 +
33.119 + // FibHeap
33.120 {
33.121 typedef FibHeap<Prio, ItemIntMap> IntHeap;
33.122 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.123 @@ -197,6 +221,19 @@
33.124 dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.125 }
33.126
33.127 + // PairingHeap
33.128 + {
33.129 + typedef PairingHeap<Prio, ItemIntMap> IntHeap;
33.130 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.131 + heapSortTest<IntHeap>();
33.132 + heapIncreaseTest<IntHeap>();
33.133 +
33.134 + typedef PairingHeap<Prio, IntNodeMap > NodeHeap;
33.135 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
33.136 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.137 + }
33.138 +
33.139 + // RadixHeap
33.140 {
33.141 typedef RadixHeap<ItemIntMap> IntHeap;
33.142 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.143 @@ -208,6 +245,19 @@
33.144 dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.145 }
33.146
33.147 + // BinomHeap
33.148 + {
33.149 + typedef BinomHeap<Prio, ItemIntMap> IntHeap;
33.150 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.151 + heapSortTest<IntHeap>();
33.152 + heapIncreaseTest<IntHeap>();
33.153 +
33.154 + typedef BinomHeap<Prio, IntNodeMap > NodeHeap;
33.155 + checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
33.156 + dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.157 + }
33.158 +
33.159 + // BucketHeap, SimpleBucketHeap
33.160 {
33.161 typedef BucketHeap<ItemIntMap> IntHeap;
33.162 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
33.163 @@ -217,8 +267,10 @@
33.164 typedef BucketHeap<IntNodeMap > NodeHeap;
33.165 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
33.166 dijkstraHeapTest<NodeHeap>(digraph, length, source);
33.167 +
33.168 + typedef SimpleBucketHeap<ItemIntMap> SimpleIntHeap;
33.169 + heapSortTest<SimpleIntHeap>();
33.170 }
33.171
33.172 -
33.173 return 0;
33.174 }
34.1 --- a/test/maps_test.cc Wed Sep 30 08:36:43 2009 +0200
34.2 +++ b/test/maps_test.cc Wed Sep 30 08:41:06 2009 +0200
34.3 @@ -23,6 +23,9 @@
34.4 #include <lemon/concepts/maps.h>
34.5 #include <lemon/maps.h>
34.6 #include <lemon/list_graph.h>
34.7 +#include <lemon/smart_graph.h>
34.8 +#include <lemon/adaptors.h>
34.9 +#include <lemon/dfs.h>
34.10
34.11 #include "test_tools.h"
34.12
34.13 @@ -61,6 +64,12 @@
34.14 typedef ReadWriteMap<A, bool> BoolWriteMap;
34.15 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
34.16
34.17 +template<typename Map1, typename Map2, typename ItemIt>
34.18 +void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
34.19 + for (; it != INVALID; ++it)
34.20 + check(map1[it] == map2[it], "The maps are not equal");
34.21 +}
34.22 +
34.23 int main()
34.24 {
34.25 // Map concepts
34.26 @@ -329,6 +338,10 @@
34.27 // LoggerBoolMap
34.28 {
34.29 typedef std::vector<int> vec;
34.30 + checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
34.31 + checkConcept<WriteMap<int, bool>,
34.32 + LoggerBoolMap<std::back_insert_iterator<vec> > >();
34.33 +
34.34 vec v1;
34.35 vec v2(10);
34.36 LoggerBoolMap<std::back_insert_iterator<vec> >
34.37 @@ -348,6 +361,155 @@
34.38 for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
34.39 it != map2.end(); ++it )
34.40 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
34.41 +
34.42 + typedef ListDigraph Graph;
34.43 + DIGRAPH_TYPEDEFS(Graph);
34.44 + Graph gr;
34.45 +
34.46 + Node n0 = gr.addNode();
34.47 + Node n1 = gr.addNode();
34.48 + Node n2 = gr.addNode();
34.49 + Node n3 = gr.addNode();
34.50 +
34.51 + gr.addArc(n3, n0);
34.52 + gr.addArc(n3, n2);
34.53 + gr.addArc(n0, n2);
34.54 + gr.addArc(n2, n1);
34.55 + gr.addArc(n0, n1);
34.56 +
34.57 + {
34.58 + std::vector<Node> v;
34.59 + dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
34.60 +
34.61 + check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
34.62 + "Something is wrong with LoggerBoolMap");
34.63 + }
34.64 + {
34.65 + std::vector<Node> v(countNodes(gr));
34.66 + dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
34.67 +
34.68 + check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
34.69 + "Something is wrong with LoggerBoolMap");
34.70 + }
34.71 + }
34.72 +
34.73 + // IdMap, RangeIdMap
34.74 + {
34.75 + typedef ListDigraph Graph;
34.76 + DIGRAPH_TYPEDEFS(Graph);
34.77 +
34.78 + checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
34.79 + checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
34.80 + checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
34.81 + checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
34.82 +
34.83 + Graph gr;
34.84 + IdMap<Graph, Node> nmap(gr);
34.85 + IdMap<Graph, Arc> amap(gr);
34.86 + RangeIdMap<Graph, Node> nrmap(gr);
34.87 + RangeIdMap<Graph, Arc> armap(gr);
34.88 +
34.89 + Node n0 = gr.addNode();
34.90 + Node n1 = gr.addNode();
34.91 + Node n2 = gr.addNode();
34.92 +
34.93 + Arc a0 = gr.addArc(n0, n1);
34.94 + Arc a1 = gr.addArc(n0, n2);
34.95 + Arc a2 = gr.addArc(n2, n1);
34.96 + Arc a3 = gr.addArc(n2, n0);
34.97 +
34.98 + check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
34.99 + check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
34.100 + check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
34.101 +
34.102 + check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
34.103 + check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
34.104 + check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
34.105 + check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
34.106 +
34.107 + check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
34.108 + check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
34.109 +
34.110 + check(nrmap.size() == 3 && armap.size() == 4,
34.111 + "Wrong RangeIdMap::size()");
34.112 +
34.113 + check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
34.114 + check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
34.115 + check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
34.116 +
34.117 + check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
34.118 + check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
34.119 + check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
34.120 + check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
34.121 +
34.122 + check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
34.123 + check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
34.124 +
34.125 + gr.erase(n1);
34.126 +
34.127 + if (nrmap[n0] == 1) nrmap.swap(n0, n2);
34.128 + nrmap.swap(n2, n0);
34.129 + if (armap[a1] == 1) armap.swap(a1, a3);
34.130 + armap.swap(a3, a1);
34.131 +
34.132 + check(nrmap.size() == 2 && armap.size() == 2,
34.133 + "Wrong RangeIdMap::size()");
34.134 +
34.135 + check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
34.136 + check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
34.137 +
34.138 + check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
34.139 + check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
34.140 +
34.141 + check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
34.142 + check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
34.143 + }
34.144 +
34.145 + // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
34.146 + {
34.147 + typedef ListGraph Graph;
34.148 + GRAPH_TYPEDEFS(Graph);
34.149 +
34.150 + checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
34.151 + checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
34.152 + checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
34.153 + checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
34.154 + checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
34.155 + checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
34.156 +
34.157 + Graph gr;
34.158 + Node n0 = gr.addNode();
34.159 + Node n1 = gr.addNode();
34.160 + Node n2 = gr.addNode();
34.161 +
34.162 + gr.addEdge(n0,n1);
34.163 + gr.addEdge(n1,n2);
34.164 + gr.addEdge(n0,n2);
34.165 + gr.addEdge(n2,n1);
34.166 + gr.addEdge(n1,n2);
34.167 + gr.addEdge(n0,n1);
34.168 +
34.169 + for (EdgeIt e(gr); e != INVALID; ++e) {
34.170 + check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
34.171 + check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
34.172 + }
34.173 +
34.174 + compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
34.175 + targetMap(orienter(gr, constMap<Edge, bool>(false))),
34.176 + EdgeIt(gr));
34.177 +
34.178 + typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
34.179 + Digraph dgr(gr, constMap<Edge, bool>(true));
34.180 + OutDegMap<Digraph> odm(dgr);
34.181 + InDegMap<Digraph> idm(dgr);
34.182 +
34.183 + check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
34.184 + check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
34.185 +
34.186 + gr.addEdge(n2, n0);
34.187 +
34.188 + check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
34.189 + check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
34.190 }
34.191
34.192 // CrossRefMap
34.193 @@ -357,6 +519,73 @@
34.194
34.195 checkConcept<ReadWriteMap<Node, int>,
34.196 CrossRefMap<Graph, Node, int> >();
34.197 + checkConcept<ReadWriteMap<Node, bool>,
34.198 + CrossRefMap<Graph, Node, bool> >();
34.199 + checkConcept<ReadWriteMap<Node, double>,
34.200 + CrossRefMap<Graph, Node, double> >();
34.201 +
34.202 + Graph gr;
34.203 + typedef CrossRefMap<Graph, Node, char> CRMap;
34.204 + CRMap map(gr);
34.205 +
34.206 + Node n0 = gr.addNode();
34.207 + Node n1 = gr.addNode();
34.208 + Node n2 = gr.addNode();
34.209 +
34.210 + map.set(n0, 'A');
34.211 + map.set(n1, 'B');
34.212 + map.set(n2, 'C');
34.213 +
34.214 + check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
34.215 + "Wrong CrossRefMap");
34.216 + check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
34.217 + "Wrong CrossRefMap");
34.218 + check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
34.219 + "Wrong CrossRefMap");
34.220 + check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
34.221 + "Wrong CrossRefMap::count()");
34.222 +
34.223 + CRMap::ValueIt it = map.beginValue();
34.224 + check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
34.225 + it == map.endValue(), "Wrong value iterator");
34.226 +
34.227 + map.set(n2, 'A');
34.228 +
34.229 + check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
34.230 + "Wrong CrossRefMap");
34.231 + check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
34.232 + check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
34.233 + check(map('C') == INVALID && map.inverse()['C'] == INVALID,
34.234 + "Wrong CrossRefMap");
34.235 + check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
34.236 + "Wrong CrossRefMap::count()");
34.237 +
34.238 + it = map.beginValue();
34.239 + check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
34.240 + it == map.endValue(), "Wrong value iterator");
34.241 +
34.242 + map.set(n0, 'C');
34.243 +
34.244 + check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
34.245 + "Wrong CrossRefMap");
34.246 + check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
34.247 + check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
34.248 + check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
34.249 + check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
34.250 + "Wrong CrossRefMap::count()");
34.251 +
34.252 + it = map.beginValue();
34.253 + check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
34.254 + it == map.endValue(), "Wrong value iterator");
34.255 + }
34.256 +
34.257 + // CrossRefMap
34.258 + {
34.259 + typedef SmartDigraph Graph;
34.260 + DIGRAPH_TYPEDEFS(Graph);
34.261 +
34.262 + checkConcept<ReadWriteMap<Node, int>,
34.263 + CrossRefMap<Graph, Node, int> >();
34.264
34.265 Graph gr;
34.266 typedef CrossRefMap<Graph, Node, char> CRMap;
34.267 @@ -383,6 +612,193 @@
34.268 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
34.269 it == map.endValue(), "Wrong value iterator");
34.270 }
34.271 +
34.272 + // Iterable bool map
34.273 + {
34.274 + typedef SmartGraph Graph;
34.275 + typedef SmartGraph::Node Item;
34.276
34.277 + typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
34.278 + checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
34.279 +
34.280 + const int num = 10;
34.281 + Graph g;
34.282 + std::vector<Item> items;
34.283 + for (int i = 0; i < num; ++i) {
34.284 + items.push_back(g.addNode());
34.285 + }
34.286 +
34.287 + Ibm map1(g, true);
34.288 + int n = 0;
34.289 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
34.290 + check(map1[static_cast<Item>(it)], "Wrong TrueIt");
34.291 + ++n;
34.292 + }
34.293 + check(n == num, "Wrong number");
34.294 +
34.295 + n = 0;
34.296 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
34.297 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
34.298 + ++n;
34.299 + }
34.300 + check(n == num, "Wrong number");
34.301 + check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
34.302 + check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
34.303 +
34.304 + map1[items[5]] = true;
34.305 +
34.306 + n = 0;
34.307 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
34.308 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
34.309 + ++n;
34.310 + }
34.311 + check(n == num, "Wrong number");
34.312 +
34.313 + map1[items[num / 2]] = false;
34.314 + check(map1[items[num / 2]] == false, "Wrong map value");
34.315 +
34.316 + n = 0;
34.317 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
34.318 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
34.319 + ++n;
34.320 + }
34.321 + check(n == num - 1, "Wrong number");
34.322 +
34.323 + n = 0;
34.324 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
34.325 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
34.326 + ++n;
34.327 + }
34.328 + check(n == 1, "Wrong number");
34.329 +
34.330 + map1[items[0]] = false;
34.331 + check(map1[items[0]] == false, "Wrong map value");
34.332 +
34.333 + map1[items[num - 1]] = false;
34.334 + check(map1[items[num - 1]] == false, "Wrong map value");
34.335 +
34.336 + n = 0;
34.337 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
34.338 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
34.339 + ++n;
34.340 + }
34.341 + check(n == num - 3, "Wrong number");
34.342 + check(map1.trueNum() == num - 3, "Wrong number");
34.343 +
34.344 + n = 0;
34.345 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
34.346 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
34.347 + ++n;
34.348 + }
34.349 + check(n == 3, "Wrong number");
34.350 + check(map1.falseNum() == 3, "Wrong number");
34.351 + }
34.352 +
34.353 + // Iterable int map
34.354 + {
34.355 + typedef SmartGraph Graph;
34.356 + typedef SmartGraph::Node Item;
34.357 + typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
34.358 +
34.359 + checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
34.360 +
34.361 + const int num = 10;
34.362 + Graph g;
34.363 + std::vector<Item> items;
34.364 + for (int i = 0; i < num; ++i) {
34.365 + items.push_back(g.addNode());
34.366 + }
34.367 +
34.368 + Iim map1(g);
34.369 + check(map1.size() == 0, "Wrong size");
34.370 +
34.371 + for (int i = 0; i < num; ++i) {
34.372 + map1[items[i]] = i;
34.373 + }
34.374 + check(map1.size() == num, "Wrong size");
34.375 +
34.376 + for (int i = 0; i < num; ++i) {
34.377 + Iim::ItemIt it(map1, i);
34.378 + check(static_cast<Item>(it) == items[i], "Wrong value");
34.379 + ++it;
34.380 + check(static_cast<Item>(it) == INVALID, "Wrong value");
34.381 + }
34.382 +
34.383 + for (int i = 0; i < num; ++i) {
34.384 + map1[items[i]] = i % 2;
34.385 + }
34.386 + check(map1.size() == 2, "Wrong size");
34.387 +
34.388 + int n = 0;
34.389 + for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
34.390 + check(map1[static_cast<Item>(it)] == 0, "Wrong value");
34.391 + ++n;
34.392 + }
34.393 + check(n == (num + 1) / 2, "Wrong number");
34.394 +
34.395 + for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
34.396 + check(map1[static_cast<Item>(it)] == 1, "Wrong value");
34.397 + ++n;
34.398 + }
34.399 + check(n == num, "Wrong number");
34.400 +
34.401 + }
34.402 +
34.403 + // Iterable value map
34.404 + {
34.405 + typedef SmartGraph Graph;
34.406 + typedef SmartGraph::Node Item;
34.407 + typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
34.408 +
34.409 + checkConcept<ReadWriteMap<Item, double>, Ivm>();
34.410 +
34.411 + const int num = 10;
34.412 + Graph g;
34.413 + std::vector<Item> items;
34.414 + for (int i = 0; i < num; ++i) {
34.415 + items.push_back(g.addNode());
34.416 + }
34.417 +
34.418 + Ivm map1(g, 0.0);
34.419 + check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
34.420 + check(*map1.beginValue() == 0.0, "Wrong value");
34.421 +
34.422 + for (int i = 0; i < num; ++i) {
34.423 + map1.set(items[i], static_cast<double>(i));
34.424 + }
34.425 + check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
34.426 +
34.427 + for (int i = 0; i < num; ++i) {
34.428 + Ivm::ItemIt it(map1, static_cast<double>(i));
34.429 + check(static_cast<Item>(it) == items[i], "Wrong value");
34.430 + ++it;
34.431 + check(static_cast<Item>(it) == INVALID, "Wrong value");
34.432 + }
34.433 +
34.434 + for (Ivm::ValueIt vit = map1.beginValue();
34.435 + vit != map1.endValue(); ++vit) {
34.436 + check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
34.437 + "Wrong ValueIt");
34.438 + }
34.439 +
34.440 + for (int i = 0; i < num; ++i) {
34.441 + map1.set(items[i], static_cast<double>(i % 2));
34.442 + }
34.443 + check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
34.444 +
34.445 + int n = 0;
34.446 + for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
34.447 + check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
34.448 + ++n;
34.449 + }
34.450 + check(n == (num + 1) / 2, "Wrong number");
34.451 +
34.452 + for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
34.453 + check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
34.454 + ++n;
34.455 + }
34.456 + check(n == num, "Wrong number");
34.457 +
34.458 + }
34.459 return 0;
34.460 }
35.1 --- a/test/preflow_test.cc Wed Sep 30 08:36:43 2009 +0200
35.2 +++ b/test/preflow_test.cc Wed Sep 30 08:41:06 2009 +0200
35.3 @@ -94,6 +94,11 @@
35.4 ::Create PreflowType;
35.5 PreflowType preflow_test(g, cap, n, n);
35.6 const PreflowType& const_preflow_test = preflow_test;
35.7 +
35.8 + const PreflowType::Elevator& elev = const_preflow_test.elevator();
35.9 + preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
35.10 + PreflowType::Tolerance tol = const_preflow_test.tolerance();
35.11 + preflow_test.tolerance(tol);
35.12
35.13 preflow_test
35.14 .capacityMap(cap)
36.1 --- a/tools/lemon-0.x-to-1.x.sh Wed Sep 30 08:36:43 2009 +0200
36.2 +++ b/tools/lemon-0.x-to-1.x.sh Wed Sep 30 08:41:06 2009 +0200
36.3 @@ -35,10 +35,10 @@
36.4 -e "s/IncEdgeIt/_In_cEd_geIt_label_/g"\
36.5 -e "s/Edge\>/_Ar_c_label_/g"\
36.6 -e "s/\<edge\>/_ar_c_label_/g"\
36.7 - -e "s/_edge\>/_ar_c_label_/g"\
36.8 + -e "s/_edge\>/__ar_c_label_/g"\
36.9 -e "s/Edges\>/_Ar_c_label_s/g"\
36.10 -e "s/\<edges\>/_ar_c_label_s/g"\
36.11 - -e "s/_edges\>/_ar_c_label_s/g"\
36.12 + -e "s/_edges\>/__ar_c_label_s/g"\
36.13 -e "s/\([Ee]\)dge\([a-z]\)/_\1d_ge_label_\2/g"\
36.14 -e "s/\([a-z]\)edge/\1_ed_ge_label_/g"\
36.15 -e "s/Edge/_Ar_c_label_/g"\
36.16 @@ -68,6 +68,11 @@
36.17 -e "s/_blu_e_label_/blue/g"\
36.18 -e "s/_GR_APH_TY_PEDE_FS_label_/GRAPH_TYPEDEFS/g"\
36.19 -e "s/_DIGR_APH_TY_PEDE_FS_label_/DIGRAPH_TYPEDEFS/g"\
36.20 + -e "s/\<digraph_adaptor\.h\>/adaptors.h/g"\
36.21 + -e "s/\<digraph_utils\.h\>/core.h/g"\
36.22 + -e "s/\<digraph_reader\.h\>/lgf_reader.h/g"\
36.23 + -e "s/\<digraph_writer\.h\>/lgf_writer.h/g"\
36.24 + -e "s/\<topology\.h\>/connectivity.h/g"\
36.25 -e "s/DigraphToEps/GraphToEps/g"\
36.26 -e "s/digraphToEps/graphToEps/g"\
36.27 -e "s/\<DefPredMap\>/SetPredMap/g"\