1.1 --- a/lemon/Makefile.am Sun Dec 18 03:01:53 2005 +0000
1.2 +++ b/lemon/Makefile.am Mon Dec 19 09:43:13 2005 +0000
1.3 @@ -21,7 +21,7 @@
1.4 endif
1.5
1.6 nobase_pkginclude_HEADERS = \
1.7 - belmann_ford.h \
1.8 + bellman_ford.h \
1.9 bezier.h \
1.10 bfs.h \
1.11 dfs.h \
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/lemon/bellman_ford.h Mon Dec 19 09:43:13 2005 +0000
2.3 @@ -0,0 +1,950 @@
2.4 +/* -*- C++ -*-
2.5 + * lemon/bellman_ford.h - Part of LEMON, a generic C++ optimization library
2.6 + *
2.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
2.9 + *
2.10 + * Permission to use, modify and distribute this software is granted
2.11 + * provided that this copyright notice appears in all copies. For
2.12 + * precise terms see the accompanying LICENSE file.
2.13 + *
2.14 + * This software is provided "AS IS" with no warranty of any kind,
2.15 + * express or implied, and with no claim as to its suitability for any
2.16 + * purpose.
2.17 + *
2.18 + */
2.19 +
2.20 +#ifndef LEMON_BELMANN_FORD_H
2.21 +#define LEMON_BELMANN_FORD_H
2.22 +
2.23 +/// \ingroup flowalgs
2.24 +/// \file
2.25 +/// \brief BellmanFord algorithm.
2.26 +///
2.27 +
2.28 +#include <lemon/list_graph.h>
2.29 +#include <lemon/invalid.h>
2.30 +#include <lemon/error.h>
2.31 +#include <lemon/maps.h>
2.32 +
2.33 +#include <limits>
2.34 +
2.35 +namespace lemon {
2.36 +
2.37 + /// \brief Default OperationTraits for the BellmanFord algorithm class.
2.38 + ///
2.39 + /// It defines all computational operations and constants which are
2.40 + /// used in the bellman ford algorithm. The default implementation
2.41 + /// is based on the numeric_limits class. If the numeric type does not
2.42 + /// have infinity value then the maximum value is used as extremal
2.43 + /// infinity value.
2.44 + template <
2.45 + typename Value,
2.46 + bool has_infinity = std::numeric_limits<Value>::has_infinity>
2.47 + struct BellmanFordDefaultOperationTraits {
2.48 + /// \brief Gives back the zero value of the type.
2.49 + static Value zero() {
2.50 + return static_cast<Value>(0);
2.51 + }
2.52 + /// \brief Gives back the positive infinity value of the type.
2.53 + static Value infinity() {
2.54 + return std::numeric_limits<Value>::infinity();
2.55 + }
2.56 + /// \brief Gives back the sum of the given two elements.
2.57 + static Value plus(const Value& left, const Value& right) {
2.58 + return left + right;
2.59 + }
2.60 + /// \brief Gives back true only if the first value less than the second.
2.61 + static bool less(const Value& left, const Value& right) {
2.62 + return left < right;
2.63 + }
2.64 + };
2.65 +
2.66 + template <typename Value>
2.67 + struct BellmanFordDefaultOperationTraits<Value, false> {
2.68 + static Value zero() {
2.69 + return static_cast<Value>(0);
2.70 + }
2.71 + static Value infinity() {
2.72 + return std::numeric_limits<Value>::max();
2.73 + }
2.74 + static Value plus(const Value& left, const Value& right) {
2.75 + if (left == infinity() || right == infinity()) return infinity();
2.76 + return left + right;
2.77 + }
2.78 + static bool less(const Value& left, const Value& right) {
2.79 + return left < right;
2.80 + }
2.81 + };
2.82 +
2.83 + /// \brief Default traits class of BellmanFord class.
2.84 + ///
2.85 + /// Default traits class of BellmanFord class.
2.86 + /// \param _Graph Graph type.
2.87 + /// \param _LegthMap Type of length map.
2.88 + template<class _Graph, class _LengthMap>
2.89 + struct BellmanFordDefaultTraits {
2.90 + /// The graph type the algorithm runs on.
2.91 + typedef _Graph Graph;
2.92 +
2.93 + /// \brief The type of the map that stores the edge lengths.
2.94 + ///
2.95 + /// The type of the map that stores the edge lengths.
2.96 + /// It must meet the \ref concept::ReadMap "ReadMap" concept.
2.97 + typedef _LengthMap LengthMap;
2.98 +
2.99 + // The type of the length of the edges.
2.100 + typedef typename _LengthMap::Value Value;
2.101 +
2.102 + /// \brief Operation traits for bellman-ford algorithm.
2.103 + ///
2.104 + /// It defines the infinity type on the given Value type
2.105 + /// and the used operation.
2.106 + /// \see BellmanFordDefaultOperationTraits
2.107 + typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
2.108 +
2.109 + /// \brief The type of the map that stores the last edges of the
2.110 + /// shortest paths.
2.111 + ///
2.112 + /// The type of the map that stores the last
2.113 + /// edges of the shortest paths.
2.114 + /// It must meet the \ref concept::WriteMap "WriteMap" concept.
2.115 + ///
2.116 + typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
2.117 +
2.118 + /// \brief Instantiates a PredMap.
2.119 + ///
2.120 + /// This function instantiates a \ref PredMap.
2.121 + /// \param graph is the graph, to which we would like to define the PredMap.
2.122 + static PredMap *createPredMap(const _Graph& graph) {
2.123 + return new PredMap(graph);
2.124 + }
2.125 +
2.126 + /// \brief The type of the map that stores the dists of the nodes.
2.127 + ///
2.128 + /// The type of the map that stores the dists of the nodes.
2.129 + /// It must meet the \ref concept::WriteMap "WriteMap" concept.
2.130 + ///
2.131 + typedef typename Graph::template NodeMap<typename _LengthMap::Value>
2.132 + DistMap;
2.133 +
2.134 + /// \brief Instantiates a DistMap.
2.135 + ///
2.136 + /// This function instantiates a \ref DistMap.
2.137 + /// \param graph is the graph, to which we would like to define the
2.138 + /// \ref DistMap
2.139 + static DistMap *createDistMap(const _Graph& graph) {
2.140 + return new DistMap(graph);
2.141 + }
2.142 +
2.143 + };
2.144 +
2.145 + /// \brief %BellmanFord algorithm class.
2.146 + ///
2.147 + /// \ingroup flowalgs
2.148 + /// This class provides an efficient implementation of \c Bellman-Ford
2.149 + /// algorithm. The edge lengths are passed to the algorithm using a
2.150 + /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any
2.151 + /// kind of length.
2.152 + ///
2.153 + /// The Bellman-Ford algorithm solves the shortest path from one node
2.154 + /// problem when the edges can have negative length but the graph should
2.155 + /// not contain cycles with negative sum of length. If we can assume
2.156 + /// that all edge is non-negative in the graph then the dijkstra algorithm
2.157 + /// should be used rather.
2.158 + ///
2.159 + /// The complexity of the algorithm is O(n * e).
2.160 + ///
2.161 + /// The type of the length is determined by the
2.162 + /// \ref concept::ReadMap::Value "Value" of the length map.
2.163 + ///
2.164 + /// \param _Graph The graph type the algorithm runs on. The default value
2.165 + /// is \ref ListGraph. The value of _Graph is not used directly by
2.166 + /// BellmanFord, it is only passed to \ref BellmanFordDefaultTraits.
2.167 + /// \param _LengthMap This read-only EdgeMap determines the lengths of the
2.168 + /// edges. The default map type is \ref concept::StaticGraph::EdgeMap
2.169 + /// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly
2.170 + /// by BellmanFord, it is only passed to \ref BellmanFordDefaultTraits.
2.171 + /// \param _Traits Traits class to set various data types used by the
2.172 + /// algorithm. The default traits class is \ref BellmanFordDefaultTraits
2.173 + /// "BellmanFordDefaultTraits<_Graph,_LengthMap>". See \ref
2.174 + /// BellmanFordDefaultTraits for the documentation of a BellmanFord traits
2.175 + /// class.
2.176 + ///
2.177 + /// \author Balazs Dezso
2.178 +
2.179 +#ifdef DOXYGEN
2.180 + template <typename _Graph, typename _LengthMap, typename _Traits>
2.181 +#else
2.182 + template <typename _Graph=ListGraph,
2.183 + typename _LengthMap=typename _Graph::template EdgeMap<int>,
2.184 + typename _Traits=BellmanFordDefaultTraits<_Graph,_LengthMap> >
2.185 +#endif
2.186 + class BellmanFord {
2.187 + public:
2.188 +
2.189 + /// \brief \ref Exception for uninitialized parameters.
2.190 + ///
2.191 + /// This error represents problems in the initialization
2.192 + /// of the parameters of the algorithms.
2.193 +
2.194 + class UninitializedParameter : public lemon::UninitializedParameter {
2.195 + public:
2.196 + virtual const char* exceptionName() const {
2.197 + return "lemon::BellmanFord::UninitializedParameter";
2.198 + }
2.199 + };
2.200 +
2.201 + typedef _Traits Traits;
2.202 + ///The type of the underlying graph.
2.203 + typedef typename _Traits::Graph Graph;
2.204 +
2.205 + typedef typename Graph::Node Node;
2.206 + typedef typename Graph::NodeIt NodeIt;
2.207 + typedef typename Graph::Edge Edge;
2.208 + typedef typename Graph::OutEdgeIt OutEdgeIt;
2.209 +
2.210 + /// \brief The type of the length of the edges.
2.211 + typedef typename _Traits::LengthMap::Value Value;
2.212 + /// \brief The type of the map that stores the edge lengths.
2.213 + typedef typename _Traits::LengthMap LengthMap;
2.214 + /// \brief The type of the map that stores the last
2.215 + /// edges of the shortest paths.
2.216 + typedef typename _Traits::PredMap PredMap;
2.217 + /// \brief The type of the map that stores the dists of the nodes.
2.218 + typedef typename _Traits::DistMap DistMap;
2.219 + /// \brief The operation traits.
2.220 + typedef typename _Traits::OperationTraits OperationTraits;
2.221 + private:
2.222 + /// Pointer to the underlying graph.
2.223 + const Graph *graph;
2.224 + /// Pointer to the length map
2.225 + const LengthMap *length;
2.226 + ///Pointer to the map of predecessors edges.
2.227 + PredMap *_pred;
2.228 + ///Indicates if \ref _pred is locally allocated (\c true) or not.
2.229 + bool local_pred;
2.230 + ///Pointer to the map of distances.
2.231 + DistMap *_dist;
2.232 + ///Indicates if \ref _dist is locally allocated (\c true) or not.
2.233 + bool local_dist;
2.234 +
2.235 + typedef typename Graph::template NodeMap<bool> MaskMap;
2.236 + MaskMap *_mask;
2.237 +
2.238 + std::vector<Node> _process;
2.239 +
2.240 + /// Creates the maps if necessary.
2.241 + void create_maps() {
2.242 + if(!_pred) {
2.243 + local_pred = true;
2.244 + _pred = Traits::createPredMap(*graph);
2.245 + }
2.246 + if(!_dist) {
2.247 + local_dist = true;
2.248 + _dist = Traits::createDistMap(*graph);
2.249 + }
2.250 + _mask = new MaskMap(*graph, false);
2.251 + }
2.252 +
2.253 + public :
2.254 +
2.255 + typedef BellmanFord Create;
2.256 +
2.257 + /// \name Named template parameters
2.258 +
2.259 + ///@{
2.260 +
2.261 + template <class T>
2.262 + struct DefPredMapTraits : public Traits {
2.263 + typedef T PredMap;
2.264 + static PredMap *createPredMap(const Graph&) {
2.265 + throw UninitializedParameter();
2.266 + }
2.267 + };
2.268 +
2.269 + /// \brief \ref named-templ-param "Named parameter" for setting PredMap
2.270 + /// type
2.271 + /// \ref named-templ-param "Named parameter" for setting PredMap type
2.272 + ///
2.273 + template <class T>
2.274 + struct DefPredMap
2.275 + : public BellmanFord< Graph, LengthMap, DefPredMapTraits<T> > {
2.276 + typedef BellmanFord< Graph, LengthMap, DefPredMapTraits<T> > Create;
2.277 + };
2.278 +
2.279 + template <class T>
2.280 + struct DefDistMapTraits : public Traits {
2.281 + typedef T DistMap;
2.282 + static DistMap *createDistMap(const Graph& graph) {
2.283 + throw UninitializedParameter();
2.284 + }
2.285 + };
2.286 +
2.287 + /// \brief \ref named-templ-param "Named parameter" for setting DistMap
2.288 + /// type
2.289 + ///
2.290 + /// \ref named-templ-param "Named parameter" for setting DistMap type
2.291 + ///
2.292 + template <class T>
2.293 + struct DefDistMap
2.294 + : public BellmanFord< Graph, LengthMap, DefDistMapTraits<T> > {
2.295 + typedef BellmanFord< Graph, LengthMap, DefDistMapTraits<T> > Create;
2.296 + };
2.297 +
2.298 + template <class T>
2.299 + struct DefOperationTraitsTraits : public Traits {
2.300 + typedef T OperationTraits;
2.301 + };
2.302 +
2.303 + /// \brief \ref named-templ-param "Named parameter" for setting
2.304 + /// OperationTraits type
2.305 + ///
2.306 + /// \ref named-templ-param "Named parameter" for setting OperationTraits
2.307 + /// type
2.308 + template <class T>
2.309 + struct DefOperationTraits
2.310 + : public BellmanFord< Graph, LengthMap, DefOperationTraitsTraits<T> > {
2.311 + typedef BellmanFord< Graph, LengthMap, DefOperationTraitsTraits<T> >
2.312 + Create;
2.313 + };
2.314 +
2.315 + ///@}
2.316 +
2.317 + protected:
2.318 +
2.319 + BellmanFord() {}
2.320 +
2.321 + public:
2.322 +
2.323 + /// \brief Constructor.
2.324 + ///
2.325 + /// \param _graph the graph the algorithm will run on.
2.326 + /// \param _length the length map used by the algorithm.
2.327 + BellmanFord(const Graph& _graph, const LengthMap& _length) :
2.328 + graph(&_graph), length(&_length),
2.329 + _pred(0), local_pred(false),
2.330 + _dist(0), local_dist(false) {}
2.331 +
2.332 + ///Destructor.
2.333 + ~BellmanFord() {
2.334 + if(local_pred) delete _pred;
2.335 + if(local_dist) delete _dist;
2.336 + delete _mask;
2.337 + }
2.338 +
2.339 + /// \brief Sets the length map.
2.340 + ///
2.341 + /// Sets the length map.
2.342 + /// \return \c (*this)
2.343 + BellmanFord &lengthMap(const LengthMap &m) {
2.344 + length = &m;
2.345 + return *this;
2.346 + }
2.347 +
2.348 + /// \brief Sets the map storing the predecessor edges.
2.349 + ///
2.350 + /// Sets the map storing the predecessor edges.
2.351 + /// If you don't use this function before calling \ref run(),
2.352 + /// it will allocate one. The destuctor deallocates this
2.353 + /// automatically allocated map, of course.
2.354 + /// \return \c (*this)
2.355 + BellmanFord &predMap(PredMap &m) {
2.356 + if(local_pred) {
2.357 + delete _pred;
2.358 + local_pred=false;
2.359 + }
2.360 + _pred = &m;
2.361 + return *this;
2.362 + }
2.363 +
2.364 + /// \brief Sets the map storing the distances calculated by the algorithm.
2.365 + ///
2.366 + /// Sets the map storing the distances calculated by the algorithm.
2.367 + /// If you don't use this function before calling \ref run(),
2.368 + /// it will allocate one. The destuctor deallocates this
2.369 + /// automatically allocated map, of course.
2.370 + /// \return \c (*this)
2.371 + BellmanFord &distMap(DistMap &m) {
2.372 + if(local_dist) {
2.373 + delete _dist;
2.374 + local_dist=false;
2.375 + }
2.376 + _dist = &m;
2.377 + return *this;
2.378 + }
2.379 +
2.380 + /// \name Execution control
2.381 + /// The simplest way to execute the algorithm is to use
2.382 + /// one of the member functions called \c run(...).
2.383 + /// \n
2.384 + /// If you need more control on the execution,
2.385 + /// first you must call \ref init(), then you can add several source nodes
2.386 + /// with \ref addSource().
2.387 + /// Finally \ref start() will perform the actual path
2.388 + /// computation.
2.389 +
2.390 + ///@{
2.391 +
2.392 + /// \brief Initializes the internal data structures.
2.393 + ///
2.394 + /// Initializes the internal data structures.
2.395 + void init(const Value value = OperationTraits::infinity()) {
2.396 + create_maps();
2.397 + for (NodeIt it(*graph); it != INVALID; ++it) {
2.398 + _pred->set(it, INVALID);
2.399 + _dist->set(it, value);
2.400 + }
2.401 + _process.clear();
2.402 + if (OperationTraits::less(value, OperationTraits::infinity())) {
2.403 + for (NodeIt it(*graph); it != INVALID; ++it) {
2.404 + _process.push_back(it);
2.405 + _mask->set(it, true);
2.406 + }
2.407 + }
2.408 + }
2.409 +
2.410 + /// \brief Adds a new source node.
2.411 + ///
2.412 + /// The optional second parameter is the initial distance of the node.
2.413 + /// It just sets the distance of the node to the given value.
2.414 + void addSource(Node source, Value dst = OperationTraits::zero()) {
2.415 + _dist->set(source, dst);
2.416 + if (!(*_mask)[source]) {
2.417 + _process.push_back(source);
2.418 + _mask->set(source, true);
2.419 + }
2.420 + }
2.421 +
2.422 + /// \brief Executes one round from the bellman ford algorithm.
2.423 + ///
2.424 + /// If the algoritm calculated the distances in the previous round
2.425 + /// strictly for all at most k length paths then it will calculate the
2.426 + /// distances strictly for all at most k + 1 length paths. With k
2.427 + /// iteration this function calculates the at most k length paths.
2.428 + /// \return %True when the algorithm have not found more shorter paths.
2.429 + bool processNextRound() {
2.430 + for (int i = 0; i < (int)_process.size(); ++i) {
2.431 + _mask->set(_process[i], false);
2.432 + }
2.433 + std::vector<Node> nextProcess;
2.434 + std::vector<Value> values(_process.size());
2.435 + for (int i = 0; i < (int)_process.size(); ++i) {
2.436 + values[i] = (*_dist)[_process[i]];
2.437 + }
2.438 + for (int i = 0; i < (int)_process.size(); ++i) {
2.439 + for (OutEdgeIt it(*graph, _process[i]); it != INVALID; ++it) {
2.440 + Node target = graph->target(it);
2.441 + Value relaxed = OperationTraits::plus(values[i], (*length)[it]);
2.442 + if (OperationTraits::less(relaxed, (*_dist)[target])) {
2.443 + _pred->set(target, it);
2.444 + _dist->set(target, relaxed);
2.445 + if (!(*_mask)[target]) {
2.446 + _mask->set(target, true);
2.447 + nextProcess.push_back(target);
2.448 + }
2.449 + }
2.450 + }
2.451 + }
2.452 + _process.swap(nextProcess);
2.453 + return _process.empty();
2.454 + }
2.455 +
2.456 + /// \brief Executes one weak round from the bellman ford algorithm.
2.457 + ///
2.458 + /// If the algorithm calculated the distances in the
2.459 + /// previous round at least for all at most k length paths then it will
2.460 + /// calculate the distances at least for all at most k + 1 length paths.
2.461 + /// This function does not make it possible to calculate strictly the
2.462 + /// at most k length minimal paths, this is why it is
2.463 + /// called just weak round.
2.464 + /// \return %True when the algorithm have not found more shorter paths.
2.465 + bool processNextWeakRound() {
2.466 + for (int i = 0; i < (int)_process.size(); ++i) {
2.467 + _mask->set(_process[i], false);
2.468 + }
2.469 + std::vector<Node> nextProcess;
2.470 + for (int i = 0; i < (int)_process.size(); ++i) {
2.471 + for (OutEdgeIt it(*graph, _process[i]); it != INVALID; ++it) {
2.472 + Node target = graph->target(it);
2.473 + Value relaxed =
2.474 + OperationTraits::plus((*_dist)[_process[i]], (*length)[it]);
2.475 + if (OperationTraits::less(relaxed, (*_dist)[target])) {
2.476 + _pred->set(target, it);
2.477 + _dist->set(target, relaxed);
2.478 + if (!(*_mask)[target]) {
2.479 + _mask->set(target, true);
2.480 + nextProcess.push_back(target);
2.481 + }
2.482 + }
2.483 + }
2.484 + }
2.485 + _process.swap(nextProcess);
2.486 + return _process.empty();
2.487 + }
2.488 +
2.489 + /// \brief Executes the algorithm.
2.490 + ///
2.491 + /// \pre init() must be called and at least one node should be added
2.492 + /// with addSource() before using this function.
2.493 + ///
2.494 + /// This method runs the %BellmanFord algorithm from the root node(s)
2.495 + /// in order to compute the shortest path to each node. The algorithm
2.496 + /// computes
2.497 + /// - The shortest path tree.
2.498 + /// - The distance of each node from the root(s).
2.499 + void start() {
2.500 + int num = countNodes(*graph) - 1;
2.501 + for (int i = 0; i < num; ++i) {
2.502 + if (processNextWeakRound()) break;
2.503 + }
2.504 + }
2.505 +
2.506 + /// \brief Executes the algorithm and checks the negative cycles.
2.507 + ///
2.508 + /// \pre init() must be called and at least one node should be added
2.509 + /// with addSource() before using this function. If there is
2.510 + /// a negative cycles in the graph it gives back false.
2.511 + ///
2.512 + /// This method runs the %BellmanFord algorithm from the root node(s)
2.513 + /// in order to compute the shortest path to each node. The algorithm
2.514 + /// computes
2.515 + /// - The shortest path tree.
2.516 + /// - The distance of each node from the root(s).
2.517 + bool checkedStart() {
2.518 + int num = countNodes(*graph);
2.519 + for (int i = 0; i < num; ++i) {
2.520 + if (processNextWeakRound()) return true;
2.521 + }
2.522 + return false;
2.523 + }
2.524 +
2.525 + /// \brief Executes the algorithm with path length limit.
2.526 + ///
2.527 + /// \pre init() must be called and at least one node should be added
2.528 + /// with addSource() before using this function.
2.529 + ///
2.530 + /// This method runs the %BellmanFord algorithm from the root node(s)
2.531 + /// in order to compute the shortest path with at most \c length edge
2.532 + /// long paths to each node. The algorithm computes
2.533 + /// - The shortest path tree.
2.534 + /// - The limited distance of each node from the root(s).
2.535 + void limitedStart(int length) {
2.536 + for (int i = 0; i < length; ++i) {
2.537 + if (processNextRound()) break;
2.538 + }
2.539 + }
2.540 +
2.541 + /// \brief Runs %BellmanFord algorithm from node \c s.
2.542 + ///
2.543 + /// This method runs the %BellmanFord algorithm from a root node \c s
2.544 + /// in order to compute the shortest path to each node. The algorithm
2.545 + /// computes
2.546 + /// - The shortest path tree.
2.547 + /// - The distance of each node from the root.
2.548 + ///
2.549 + /// \note d.run(s) is just a shortcut of the following code.
2.550 + /// \code
2.551 + /// d.init();
2.552 + /// d.addSource(s);
2.553 + /// d.start();
2.554 + /// \endcode
2.555 + void run(Node s) {
2.556 + init();
2.557 + addSource(s);
2.558 + start();
2.559 + }
2.560 +
2.561 + /// \brief Runs %BellmanFord algorithm with limited path length
2.562 + /// from node \c s.
2.563 + ///
2.564 + /// This method runs the %BellmanFord algorithm from a root node \c s
2.565 + /// in order to compute the shortest path with at most \c len edges
2.566 + /// to each node. The algorithm computes
2.567 + /// - The shortest path tree.
2.568 + /// - The distance of each node from the root.
2.569 + ///
2.570 + /// \note d.run(s, len) is just a shortcut of the following code.
2.571 + /// \code
2.572 + /// d.init();
2.573 + /// d.addSource(s);
2.574 + /// d.limitedStart(len);
2.575 + /// \endcode
2.576 + void run(Node s, int len) {
2.577 + init();
2.578 + addSource(s);
2.579 + limitedStart(len);
2.580 + }
2.581 +
2.582 + ///@}
2.583 +
2.584 + /// \name Query Functions
2.585 + /// The result of the %BellmanFord algorithm can be obtained using these
2.586 + /// functions.\n
2.587 + /// Before the use of these functions,
2.588 + /// either run() or start() must be called.
2.589 +
2.590 + ///@{
2.591 +
2.592 + /// \brief Copies the shortest path to \c t into \c p
2.593 + ///
2.594 + /// This function copies the shortest path to \c t into \c p.
2.595 + /// If it \c t is a source itself or unreachable, then it does not
2.596 + /// alter \c p.
2.597 + ///
2.598 + /// \return Returns \c true if a path to \c t was actually copied to \c p,
2.599 + /// \c false otherwise.
2.600 + /// \sa DirPath
2.601 + template <typename Path>
2.602 + bool getPath(Path &p, Node t) {
2.603 + if(reached(t)) {
2.604 + p.clear();
2.605 + typename Path::Builder b(p);
2.606 + for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
2.607 + b.pushFront(predEdge(t));
2.608 + b.commit();
2.609 + return true;
2.610 + }
2.611 + return false;
2.612 + }
2.613 +
2.614 + /// \brief The distance of a node from the root.
2.615 + ///
2.616 + /// Returns the distance of a node from the root.
2.617 + /// \pre \ref run() must be called before using this function.
2.618 + /// \warning If node \c v in unreachable from the root the return value
2.619 + /// of this funcion is undefined.
2.620 + Value dist(Node v) const { return (*_dist)[v]; }
2.621 +
2.622 + /// \brief Returns the 'previous edge' of the shortest path tree.
2.623 + ///
2.624 + /// For a node \c v it returns the 'previous edge' of the shortest path
2.625 + /// tree, i.e. it returns the last edge of a shortest path from the root
2.626 + /// to \c v. It is \ref INVALID if \c v is unreachable from the root or
2.627 + /// if \c v=s. The shortest path tree used here is equal to the shortest
2.628 + /// path tree used in \ref predNode().
2.629 + /// \pre \ref run() must be called before using
2.630 + /// this function.
2.631 + Edge predEdge(Node v) const { return (*_pred)[v]; }
2.632 +
2.633 + /// \brief Returns the 'previous node' of the shortest path tree.
2.634 + ///
2.635 + /// For a node \c v it returns the 'previous node' of the shortest path
2.636 + /// tree, i.e. it returns the last but one node from a shortest path from
2.637 + /// the root to \c /v. It is INVALID if \c v is unreachable from the root
2.638 + /// or if \c v=s. The shortest path tree used here is equal to the
2.639 + /// shortest path tree used in \ref predEdge(). \pre \ref run() must be
2.640 + /// called before using this function.
2.641 + Node predNode(Node v) const {
2.642 + return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]);
2.643 + }
2.644 +
2.645 + /// \brief Returns a reference to the NodeMap of distances.
2.646 + ///
2.647 + /// Returns a reference to the NodeMap of distances. \pre \ref run() must
2.648 + /// be called before using this function.
2.649 + const DistMap &distMap() const { return *_dist;}
2.650 +
2.651 + /// \brief Returns a reference to the shortest path tree map.
2.652 + ///
2.653 + /// Returns a reference to the NodeMap of the edges of the
2.654 + /// shortest path tree.
2.655 + /// \pre \ref run() must be called before using this function.
2.656 + const PredMap &predMap() const { return *_pred; }
2.657 +
2.658 + /// \brief Checks if a node is reachable from the root.
2.659 + ///
2.660 + /// Returns \c true if \c v is reachable from the root.
2.661 + /// \pre \ref run() must be called before using this function.
2.662 + ///
2.663 + bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
2.664 +
2.665 + ///@}
2.666 + };
2.667 +
2.668 + /// \brief Default traits class of BellmanFord function.
2.669 + ///
2.670 + /// Default traits class of BellmanFord function.
2.671 + /// \param _Graph Graph type.
2.672 + /// \param _LengthMap Type of length map.
2.673 + template <typename _Graph, typename _LengthMap>
2.674 + struct BellmanFordWizardDefaultTraits {
2.675 + /// \brief The graph type the algorithm runs on.
2.676 + typedef _Graph Graph;
2.677 +
2.678 + /// \brief The type of the map that stores the edge lengths.
2.679 + ///
2.680 + /// The type of the map that stores the edge lengths.
2.681 + /// It must meet the \ref concept::ReadMap "ReadMap" concept.
2.682 + typedef _LengthMap LengthMap;
2.683 +
2.684 + /// \brief The value type of the length map.
2.685 + typedef typename _LengthMap::Value Value;
2.686 +
2.687 + /// \brief Operation traits for bellman-ford algorithm.
2.688 + ///
2.689 + /// It defines the infinity type on the given Value type
2.690 + /// and the used operation.
2.691 + /// \see BellmanFordDefaultOperationTraits
2.692 + typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
2.693 +
2.694 + /// \brief The type of the map that stores the last
2.695 + /// edges of the shortest paths.
2.696 + ///
2.697 + /// The type of the map that stores the last
2.698 + /// edges of the shortest paths.
2.699 + /// It must meet the \ref concept::WriteMap "WriteMap" concept.
2.700 + typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
2.701 +
2.702 + /// \brief Instantiates a PredMap.
2.703 + ///
2.704 + /// This function instantiates a \ref PredMap.
2.705 + static PredMap *createPredMap(const _Graph &) {
2.706 + return new PredMap();
2.707 + }
2.708 + /// \brief The type of the map that stores the dists of the nodes.
2.709 + ///
2.710 + /// The type of the map that stores the dists of the nodes.
2.711 + /// It must meet the \ref concept::WriteMap "WriteMap" concept.
2.712 + typedef NullMap<typename Graph::Node, Value> DistMap;
2.713 + /// \brief Instantiates a DistMap.
2.714 + ///
2.715 + /// This function instantiates a \ref DistMap.
2.716 + static DistMap *createDistMap(const _Graph &) {
2.717 + return new DistMap();
2.718 + }
2.719 + };
2.720 +
2.721 + /// \brief Default traits used by \ref BellmanFordWizard
2.722 + ///
2.723 + /// To make it easier to use BellmanFord algorithm
2.724 + /// we have created a wizard class.
2.725 + /// This \ref BellmanFordWizard class needs default traits,
2.726 + /// as well as the \ref BellmanFord class.
2.727 + /// The \ref BellmanFordWizardBase is a class to be the default traits of the
2.728 + /// \ref BellmanFordWizard class.
2.729 + /// \todo More named parameters are required...
2.730 + template<class _Graph,class _LengthMap>
2.731 + class BellmanFordWizardBase
2.732 + : public BellmanFordWizardDefaultTraits<_Graph,_LengthMap> {
2.733 +
2.734 + typedef BellmanFordWizardDefaultTraits<_Graph,_LengthMap> Base;
2.735 + protected:
2.736 + /// Type of the nodes in the graph.
2.737 + typedef typename Base::Graph::Node Node;
2.738 +
2.739 + /// Pointer to the underlying graph.
2.740 + void *_graph;
2.741 + /// Pointer to the length map
2.742 + void *_length;
2.743 + ///Pointer to the map of predecessors edges.
2.744 + void *_pred;
2.745 + ///Pointer to the map of distances.
2.746 + void *_dist;
2.747 + ///Pointer to the source node.
2.748 + Node _source;
2.749 +
2.750 + public:
2.751 + /// Constructor.
2.752 +
2.753 + /// This constructor does not require parameters, therefore it initiates
2.754 + /// all of the attributes to default values (0, INVALID).
2.755 + BellmanFordWizardBase() : _graph(0), _length(0), _pred(0),
2.756 + _dist(0), _source(INVALID) {}
2.757 +
2.758 + /// Constructor.
2.759 +
2.760 + /// This constructor requires some parameters,
2.761 + /// listed in the parameters list.
2.762 + /// Others are initiated to 0.
2.763 + /// \param graph is the initial value of \ref _graph
2.764 + /// \param length is the initial value of \ref _length
2.765 + /// \param source is the initial value of \ref _source
2.766 + BellmanFordWizardBase(const _Graph& graph,
2.767 + const _LengthMap& length,
2.768 + Node source = INVALID) :
2.769 + _graph((void *)&graph), _length((void *)&length), _pred(0),
2.770 + _dist(0), _source(source) {}
2.771 +
2.772 + };
2.773 +
2.774 + /// A class to make the usage of BellmanFord algorithm easier
2.775 +
2.776 + /// This class is created to make it easier to use BellmanFord algorithm.
2.777 + /// It uses the functions and features of the plain \ref BellmanFord,
2.778 + /// but it is much simpler to use it.
2.779 + ///
2.780 + /// Simplicity means that the way to change the types defined
2.781 + /// in the traits class is based on functions that returns the new class
2.782 + /// and not on templatable built-in classes.
2.783 + /// When using the plain \ref BellmanFord
2.784 + /// the new class with the modified type comes from
2.785 + /// the original class by using the ::
2.786 + /// operator. In the case of \ref BellmanFordWizard only
2.787 + /// a function have to be called and it will
2.788 + /// return the needed class.
2.789 + ///
2.790 + /// It does not have own \ref run method. When its \ref run method is called
2.791 + /// it initiates a plain \ref BellmanFord class, and calls the \ref
2.792 + /// BellmanFord::run method of it.
2.793 + template<class _Traits>
2.794 + class BellmanFordWizard : public _Traits {
2.795 + typedef _Traits Base;
2.796 +
2.797 + ///The type of the underlying graph.
2.798 + typedef typename _Traits::Graph Graph;
2.799 +
2.800 + typedef typename Graph::Node Node;
2.801 + typedef typename Graph::NodeIt NodeIt;
2.802 + typedef typename Graph::Edge Edge;
2.803 + typedef typename Graph::OutEdgeIt EdgeIt;
2.804 +
2.805 + ///The type of the map that stores the edge lengths.
2.806 + typedef typename _Traits::LengthMap LengthMap;
2.807 +
2.808 + ///The type of the length of the edges.
2.809 + typedef typename LengthMap::Value Value;
2.810 +
2.811 + ///\brief The type of the map that stores the last
2.812 + ///edges of the shortest paths.
2.813 + typedef typename _Traits::PredMap PredMap;
2.814 +
2.815 + ///The type of the map that stores the dists of the nodes.
2.816 + typedef typename _Traits::DistMap DistMap;
2.817 +
2.818 + public:
2.819 + /// Constructor.
2.820 + BellmanFordWizard() : _Traits() {}
2.821 +
2.822 + /// \brief Constructor that requires parameters.
2.823 + ///
2.824 + /// Constructor that requires parameters.
2.825 + /// These parameters will be the default values for the traits class.
2.826 + BellmanFordWizard(const Graph& graph, const LengthMap& length,
2.827 + Node source = INVALID)
2.828 + : _Traits(graph, length, source) {}
2.829 +
2.830 + /// \brief Copy constructor
2.831 + BellmanFordWizard(const _Traits &b) : _Traits(b) {}
2.832 +
2.833 + ~BellmanFordWizard() {}
2.834 +
2.835 + /// \brief Runs BellmanFord algorithm from a given node.
2.836 + ///
2.837 + /// Runs BellmanFord algorithm from a given node.
2.838 + /// The node can be given by the \ref source function.
2.839 + void run() {
2.840 + if(Base::_source == INVALID) throw UninitializedParameter();
2.841 + BellmanFord<Graph,LengthMap,_Traits>
2.842 + bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
2.843 + if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
2.844 + if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
2.845 + bf.run(Base::_source);
2.846 + }
2.847 +
2.848 + /// \brief Runs BellmanFord algorithm from the given node.
2.849 + ///
2.850 + /// Runs BellmanFord algorithm from the given node.
2.851 + /// \param source is the given source.
2.852 + void run(Node source) {
2.853 + Base::_source = source;
2.854 + run();
2.855 + }
2.856 +
2.857 + template<class T>
2.858 + struct DefPredMapBase : public Base {
2.859 + typedef T PredMap;
2.860 + static PredMap *createPredMap(const Graph &) { return 0; };
2.861 + DefPredMapBase(const _Traits &b) : _Traits(b) {}
2.862 + };
2.863 +
2.864 + ///\brief \ref named-templ-param "Named parameter"
2.865 + ///function for setting PredMap type
2.866 + ///
2.867 + /// \ref named-templ-param "Named parameter"
2.868 + ///function for setting PredMap type
2.869 + ///
2.870 + template<class T>
2.871 + BellmanFordWizard<DefPredMapBase<T> > predMap(const T &t)
2.872 + {
2.873 + Base::_pred=(void *)&t;
2.874 + return BellmanFordWizard<DefPredMapBase<T> >(*this);
2.875 + }
2.876 +
2.877 + template<class T>
2.878 + struct DefDistMapBase : public Base {
2.879 + typedef T DistMap;
2.880 + static DistMap *createDistMap(const Graph &) { return 0; };
2.881 + DefDistMapBase(const _Traits &b) : _Traits(b) {}
2.882 + };
2.883 +
2.884 + ///\brief \ref named-templ-param "Named parameter"
2.885 + ///function for setting DistMap type
2.886 + ///
2.887 + /// \ref named-templ-param "Named parameter"
2.888 + ///function for setting DistMap type
2.889 + ///
2.890 + template<class T>
2.891 + BellmanFordWizard<DefDistMapBase<T> > distMap(const T &t) {
2.892 + Base::_dist=(void *)&t;
2.893 + return BellmanFordWizard<DefDistMapBase<T> >(*this);
2.894 + }
2.895 +
2.896 + template<class T>
2.897 + struct DefOperationTraitsBase : public Base {
2.898 + typedef T OperationTraits;
2.899 + DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
2.900 + };
2.901 +
2.902 + ///\brief \ref named-templ-param "Named parameter"
2.903 + ///function for setting OperationTraits type
2.904 + ///
2.905 + /// \ref named-templ-param "Named parameter"
2.906 + ///function for setting OperationTraits type
2.907 + ///
2.908 + template<class T>
2.909 + BellmanFordWizard<DefOperationTraitsBase<T> > distMap() {
2.910 + return BellmanFordWizard<DefDistMapBase<T> >(*this);
2.911 + }
2.912 +
2.913 + /// \brief Sets the source node, from which the BellmanFord algorithm runs.
2.914 + ///
2.915 + /// Sets the source node, from which the BellmanFord algorithm runs.
2.916 + /// \param source is the source node.
2.917 + BellmanFordWizard<_Traits>& source(Node source) {
2.918 + Base::_source = source;
2.919 + return *this;
2.920 + }
2.921 +
2.922 + };
2.923 +
2.924 + /// \brief Function type interface for BellmanFord algorithm.
2.925 + ///
2.926 + /// \ingroup flowalgs
2.927 + /// Function type interface for BellmanFord algorithm.
2.928 + ///
2.929 + /// This function also has several \ref named-templ-func-param
2.930 + /// "named parameters", they are declared as the members of class
2.931 + /// \ref BellmanFordWizard.
2.932 + /// The following
2.933 + /// example shows how to use these parameters.
2.934 + /// \code
2.935 + /// bellmanford(g,length,source).predMap(preds).run();
2.936 + /// \endcode
2.937 + /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()"
2.938 + /// to the end of the parameter list.
2.939 + /// \sa BellmanFordWizard
2.940 + /// \sa BellmanFord
2.941 + template<class _Graph, class _LengthMap>
2.942 + BellmanFordWizard<BellmanFordWizardBase<_Graph,_LengthMap> >
2.943 + bellmanFord(const _Graph& graph,
2.944 + const _LengthMap& length,
2.945 + typename _Graph::Node source = INVALID) {
2.946 + return BellmanFordWizard<BellmanFordWizardBase<_Graph,_LengthMap> >
2.947 + (graph, length, source);
2.948 + }
2.949 +
2.950 +} //END OF NAMESPACE LEMON
2.951 +
2.952 +#endif
2.953 +
3.1 --- a/lemon/johnson.h Sun Dec 18 03:01:53 2005 +0000
3.2 +++ b/lemon/johnson.h Mon Dec 19 09:43:13 2005 +0000
3.3 @@ -25,7 +25,7 @@
3.4 #include <lemon/list_graph.h>
3.5 #include <lemon/graph_utils.h>
3.6 #include <lemon/dijkstra.h>
3.7 -#include <lemon/belmann_ford.h>
3.8 +#include <lemon/bellman_ford.h>
3.9 #include <lemon/invalid.h>
3.10 #include <lemon/error.h>
3.11 #include <lemon/maps.h>
3.12 @@ -100,7 +100,7 @@
3.13 // The type of the length of the edges.
3.14 typedef typename _LengthMap::Value Value;
3.15
3.16 - /// \brief Operation traits for belmann-ford algorithm.
3.17 + /// \brief Operation traits for bellman-ford algorithm.
3.18 ///
3.19 /// It defines the infinity type on the given Value type
3.20 /// and the used operation.
3.21 @@ -544,21 +544,21 @@
3.22 /// - The distance between each node pairs.
3.23 void start() {
3.24
3.25 - typedef typename BelmannFord<Graph, LengthMap>::
3.26 + typedef typename BellmanFord<Graph, LengthMap>::
3.27 template DefOperationTraits<OperationTraits>::
3.28 template DefPredMap<NullMap<Node, Edge> >::
3.29 - Create BelmannFordType;
3.30 + Create BellmanFordType;
3.31
3.32 - BelmannFordType belmannford(*graph, *length);
3.33 + BellmanFordType bellmanford(*graph, *length);
3.34
3.35 NullMap<Node, Edge> predMap;
3.36
3.37 - belmannford.predMap(predMap);
3.38 + bellmanford.predMap(predMap);
3.39
3.40 - belmannford.init(OperationTraits::zero());
3.41 - belmannford.start();
3.42 + bellmanford.init(OperationTraits::zero());
3.43 + bellmanford.start();
3.44
3.45 - shiftedRun(belmannford.distMap());
3.46 + shiftedRun(bellmanford.distMap());
3.47 }
3.48
3.49 /// \brief Executes the algorithm and checks the negatvie cycles.
3.50 @@ -571,21 +571,21 @@
3.51 /// - The distance between each node pairs.
3.52 bool checkedStart() {
3.53
3.54 - typedef typename BelmannFord<Graph, LengthMap>::
3.55 + typedef typename BellmanFord<Graph, LengthMap>::
3.56 template DefOperationTraits<OperationTraits>::
3.57 template DefPredMap<NullMap<Node, Edge> >::
3.58 - Create BelmannFordType;
3.59 + Create BellmanFordType;
3.60
3.61 - BelmannFordType belmannford(*graph, *length);
3.62 + BellmanFordType bellmanford(*graph, *length);
3.63
3.64 NullMap<Node, Edge> predMap;
3.65
3.66 - belmannford.predMap(predMap);
3.67 + bellmanford.predMap(predMap);
3.68
3.69 - belmannford.init(OperationTraits::zero());
3.70 - if (!belmannford.checkedStart()) return false;
3.71 + bellmanford.init(OperationTraits::zero());
3.72 + if (!bellmanford.checkedStart()) return false;
3.73
3.74 - shiftedRun(belmannford.distMap());
3.75 + shiftedRun(bellmanford.distMap());
3.76 return true;
3.77 }
3.78