deba@1699: /* -*- C++ -*- deba@1699: * lemon/belmann_ford.h - Part of LEMON, a generic C++ optimization library deba@1699: * deba@1699: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1699: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1699: * deba@1699: * Permission to use, modify and distribute this software is granted deba@1699: * provided that this copyright notice appears in all copies. For deba@1699: * precise terms see the accompanying LICENSE file. deba@1699: * deba@1699: * This software is provided "AS IS" with no warranty of any kind, deba@1699: * express or implied, and with no claim as to its suitability for any deba@1699: * purpose. deba@1699: * deba@1699: */ deba@1699: deba@1699: #ifndef LEMON_BELMANN_FORD_H deba@1699: #define LEMON_BELMANN_FORD_H deba@1699: deba@1699: ///\ingroup flowalgs deba@1699: /// \file deba@1699: /// \brief BelmannFord algorithm. deba@1699: /// deba@1699: /// \todo getPath() should be implemented! (also for BFS and DFS) deba@1699: deba@1699: #include deba@1699: #include deba@1699: #include deba@1699: #include deba@1699: deba@1699: #include deba@1699: deba@1699: namespace lemon { deba@1699: deba@1699: /// \brief Default OperationTraits for the BelmannFord algorithm class. deba@1699: /// deba@1699: /// It defines all computational operations and constants which are deba@1699: /// used in the belmann ford algorithm. The default implementation deba@1699: /// is based on the numeric_limits class. If the numeric type does not deba@1699: /// have infinity value then the maximum value is used as extremal deba@1699: /// infinity value. deba@1699: template < deba@1699: typename Value, deba@1699: bool has_infinity = std::numeric_limits::has_infinity> deba@1699: struct BelmannFordDefaultOperationTraits { deba@1699: /// \brief Gives back the zero value of the type. deba@1699: static Value zero() { deba@1699: return static_cast(0); deba@1699: } deba@1699: /// \brief Gives back the positive infinity value of the type. deba@1699: static Value infinity() { deba@1699: return std::numeric_limits::infinity(); deba@1699: } deba@1699: /// \brief Gives back the sum of the given two elements. deba@1699: static Value plus(const Value& left, const Value& right) { deba@1699: return left + right; deba@1699: } deba@1699: /// \brief Gives back true only if the first value less than the second. deba@1699: static bool less(const Value& left, const Value& right) { deba@1699: return left < right; deba@1699: } deba@1699: }; deba@1699: deba@1699: template deba@1699: struct BelmannFordDefaultOperationTraits { deba@1699: static Value zero() { deba@1699: return static_cast(0); deba@1699: } deba@1699: static Value infinity() { deba@1699: return std::numeric_limits::max(); deba@1699: } deba@1699: static Value plus(const Value& left, const Value& right) { deba@1699: if (left == infinity() || right == infinity()) return infinity(); deba@1699: return left + right; deba@1699: } deba@1699: static bool less(const Value& left, const Value& right) { deba@1699: return left < right; deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief Default traits class of BelmannFord class. deba@1699: /// deba@1699: /// Default traits class of BelmannFord class. deba@1699: /// \param _Graph Graph type. deba@1699: /// \param _LegthMap Type of length map. deba@1699: template deba@1699: struct BelmannFordDefaultTraits { deba@1699: /// The graph type the algorithm runs on. deba@1699: typedef _Graph Graph; deba@1699: deba@1699: /// \brief The type of the map that stores the edge lengths. deba@1699: /// deba@1699: /// The type of the map that stores the edge lengths. deba@1699: /// It must meet the \ref concept::ReadMap "ReadMap" concept. deba@1699: typedef _LengthMap LengthMap; deba@1699: deba@1699: // The type of the length of the edges. deba@1699: typedef typename _LengthMap::Value Value; deba@1699: deba@1699: /// \brief Operation traits for belmann-ford algorithm. deba@1699: /// deba@1699: /// It defines the infinity type on the given Value type deba@1699: /// and the used operation. deba@1699: /// \see BelmannFordDefaultOperationTraits deba@1699: typedef BelmannFordDefaultOperationTraits OperationTraits; deba@1699: deba@1699: /// \brief The type of the map that stores the last edges of the deba@1699: /// shortest paths. deba@1699: /// deba@1699: /// The type of the map that stores the last deba@1699: /// edges of the shortest paths. deba@1699: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1699: /// deba@1699: typedef typename Graph::template NodeMap PredMap; deba@1699: deba@1699: /// \brief Instantiates a PredMap. deba@1699: /// deba@1699: /// This function instantiates a \ref PredMap. deba@1699: /// \param G is the graph, to which we would like to define the PredMap. deba@1699: /// \todo The graph alone may be insufficient for the initialization deba@1699: static PredMap *createPredMap(const _Graph& graph) { deba@1699: return new PredMap(graph); deba@1699: } deba@1699: deba@1699: /// \brief The type of the map that stores the dists of the nodes. deba@1699: /// deba@1699: /// The type of the map that stores the dists of the nodes. deba@1699: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1699: /// deba@1699: typedef typename Graph::template NodeMap deba@1699: DistMap; deba@1699: deba@1699: /// \brief Instantiates a DistMap. deba@1699: /// deba@1699: /// This function instantiates a \ref DistMap. deba@1699: /// \param G is the graph, to which we would like to define the deba@1699: /// \ref DistMap deba@1699: static DistMap *createDistMap(const _Graph& graph) { deba@1699: return new DistMap(graph); deba@1699: } deba@1699: deba@1699: }; deba@1699: deba@1699: /// \brief BelmannFord algorithm class. deba@1699: /// deba@1699: /// \ingroup flowalgs deba@1723: /// This class provides an efficient implementation of \c Belmann-Ford deba@1699: /// algorithm. The edge lengths are passed to the algorithm using a deba@1699: /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any deba@1699: /// kind of length. deba@1699: /// deba@1723: /// The Belmann-Ford algorithm solves the shortest path from one node deba@1723: /// problem when the edges can have negative length but the graph should deba@1723: /// not contain circle with negative sum of length. If we can assume deba@1723: /// that all edge is non-negative in the graph then the dijkstra algorithm deba@1723: /// should be used rather. deba@1723: /// deba@1723: /// The complexity of the algorithm is O(n * e). deba@1723: /// deba@1699: /// The type of the length is determined by the deba@1699: /// \ref concept::ReadMap::Value "Value" of the length map. deba@1699: /// deba@1699: /// \param _Graph The graph type the algorithm runs on. The default value deba@1699: /// is \ref ListGraph. The value of _Graph is not used directly by deba@1699: /// BelmannFord, it is only passed to \ref BelmannFordDefaultTraits. deba@1699: /// \param _LengthMap This read-only EdgeMap determines the lengths of the deba@1699: /// edges. The default map type is \ref concept::StaticGraph::EdgeMap deba@1699: /// "Graph::EdgeMap". The value of _LengthMap is not used directly deba@1699: /// by BelmannFord, it is only passed to \ref BelmannFordDefaultTraits. deba@1699: /// \param _Traits Traits class to set various data types used by the deba@1699: /// algorithm. The default traits class is \ref BelmannFordDefaultTraits deba@1699: /// "BelmannFordDefaultTraits<_Graph,_LengthMap>". See \ref deba@1699: /// BelmannFordDefaultTraits for the documentation of a BelmannFord traits deba@1699: /// class. deba@1699: /// deba@1699: /// \author Balazs Dezso deba@1699: deba@1710: #ifdef DOXYGEN deba@1710: template deba@1710: #else deba@1699: template , deba@1699: typename _Traits=BelmannFordDefaultTraits<_Graph,_LengthMap> > deba@1710: #endif deba@1699: class BelmannFord { deba@1699: public: deba@1699: deba@1699: /// \brief \ref Exception for uninitialized parameters. deba@1699: /// deba@1699: /// This error represents problems in the initialization deba@1699: /// of the parameters of the algorithms. deba@1699: deba@1699: class UninitializedParameter : public lemon::UninitializedParameter { deba@1699: public: deba@1699: virtual const char* exceptionName() const { deba@1699: return "lemon::BelmannFord::UninitializedParameter"; deba@1699: } deba@1699: }; deba@1699: deba@1699: typedef _Traits Traits; deba@1699: ///The type of the underlying graph. deba@1699: typedef typename _Traits::Graph Graph; deba@1699: deba@1699: typedef typename Graph::Node Node; deba@1699: typedef typename Graph::NodeIt NodeIt; deba@1699: typedef typename Graph::Edge Edge; deba@1699: typedef typename Graph::EdgeIt EdgeIt; deba@1699: deba@1699: /// \brief The type of the length of the edges. deba@1699: typedef typename _Traits::LengthMap::Value Value; deba@1699: /// \brief The type of the map that stores the edge lengths. deba@1699: typedef typename _Traits::LengthMap LengthMap; deba@1699: /// \brief The type of the map that stores the last deba@1699: /// edges of the shortest paths. deba@1699: typedef typename _Traits::PredMap PredMap; deba@1699: /// \brief The type of the map that stores the dists of the nodes. deba@1699: typedef typename _Traits::DistMap DistMap; deba@1699: /// \brief The operation traits. deba@1699: typedef typename _Traits::OperationTraits OperationTraits; deba@1699: private: deba@1699: /// Pointer to the underlying graph. deba@1699: const Graph *graph; deba@1699: /// Pointer to the length map deba@1699: const LengthMap *length; deba@1699: ///Pointer to the map of predecessors edges. deba@1699: PredMap *_pred; deba@1699: ///Indicates if \ref _pred is locally allocated (\c true) or not. deba@1699: bool local_pred; deba@1699: ///Pointer to the map of distances. deba@1699: DistMap *_dist; deba@1699: ///Indicates if \ref _dist is locally allocated (\c true) or not. deba@1699: bool local_dist; deba@1699: deba@1699: /// Creates the maps if necessary. deba@1699: void create_maps() { deba@1699: if(!_pred) { deba@1699: local_pred = true; deba@1699: _pred = Traits::createPredMap(*graph); deba@1699: } deba@1699: if(!_dist) { deba@1699: local_dist = true; deba@1699: _dist = Traits::createDistMap(*graph); deba@1699: } deba@1699: } deba@1699: deba@1699: public : deba@1699: deba@1710: typedef BelmannFord Create; deba@1710: deba@1699: /// \name Named template parameters deba@1699: deba@1699: ///@{ deba@1699: deba@1699: template deba@1699: struct DefPredMapTraits : public Traits { deba@1699: typedef T PredMap; deba@1710: static PredMap *createPredMap(const Graph&) { deba@1699: throw UninitializedParameter(); deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting PredMap deba@1699: /// type deba@1699: /// \ref named-templ-param "Named parameter" for setting PredMap type deba@1699: /// deba@1699: template deba@1710: struct DefPredMap { deba@1710: typedef BelmannFord< Graph, LengthMap, DefPredMapTraits > Create; deba@1710: }; deba@1699: deba@1699: template deba@1699: struct DefDistMapTraits : public Traits { deba@1699: typedef T DistMap; deba@1699: static DistMap *createDistMap(const Graph& graph) { deba@1699: throw UninitializedParameter(); deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting DistMap deba@1699: /// type deba@1699: /// deba@1699: /// \ref named-templ-param "Named parameter" for setting DistMap type deba@1699: /// deba@1699: template deba@1710: struct DefDistMap deba@1710: : public BelmannFord< Graph, LengthMap, DefDistMapTraits > { deba@1710: typedef BelmannFord< Graph, LengthMap, DefDistMapTraits > Create; deba@1710: }; deba@1699: deba@1699: template deba@1699: struct DefOperationTraitsTraits : public Traits { deba@1699: typedef T OperationTraits; deba@1699: }; deba@1699: deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting deba@1699: /// OperationTraits type deba@1699: /// deba@1710: /// \ref named-templ-param "Named parameter" for setting OperationTraits deba@1710: /// type deba@1699: template deba@1710: struct DefOperationTraits deba@1699: : public BelmannFord< Graph, LengthMap, DefOperationTraitsTraits > { deba@1699: typedef BelmannFord< Graph, LengthMap, DefOperationTraitsTraits > deba@1710: Create; deba@1699: }; deba@1699: deba@1699: ///@} deba@1699: deba@1710: protected: deba@1710: deba@1710: BelmannFord() {} deba@1710: deba@1699: public: deba@1699: deba@1699: /// \brief Constructor. deba@1699: /// deba@1699: /// \param _graph the graph the algorithm will run on. deba@1699: /// \param _length the length map used by the algorithm. deba@1699: BelmannFord(const Graph& _graph, const LengthMap& _length) : deba@1699: graph(&_graph), length(&_length), deba@1699: _pred(0), local_pred(false), deba@1699: _dist(0), local_dist(false) {} deba@1699: deba@1699: ///Destructor. deba@1699: ~BelmannFord() { deba@1699: if(local_pred) delete _pred; deba@1699: if(local_dist) delete _dist; deba@1699: } deba@1699: deba@1699: /// \brief Sets the length map. deba@1699: /// deba@1699: /// Sets the length map. deba@1699: /// \return \c (*this) deba@1699: BelmannFord &lengthMap(const LengthMap &m) { deba@1699: length = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: /// \brief Sets the map storing the predecessor edges. deba@1699: /// deba@1699: /// Sets the map storing the predecessor edges. deba@1699: /// If you don't use this function before calling \ref run(), deba@1699: /// it will allocate one. The destuctor deallocates this deba@1699: /// automatically allocated map, of course. deba@1699: /// \return \c (*this) deba@1699: BelmannFord &predMap(PredMap &m) { deba@1699: if(local_pred) { deba@1699: delete _pred; deba@1699: local_pred=false; deba@1699: } deba@1699: _pred = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: /// \brief Sets the map storing the distances calculated by the algorithm. deba@1699: /// deba@1699: /// Sets the map storing the distances calculated by the algorithm. deba@1699: /// If you don't use this function before calling \ref run(), deba@1699: /// it will allocate one. The destuctor deallocates this deba@1699: /// automatically allocated map, of course. deba@1699: /// \return \c (*this) deba@1699: BelmannFord &distMap(DistMap &m) { deba@1699: if(local_dist) { deba@1699: delete _dist; deba@1699: local_dist=false; deba@1699: } deba@1699: _dist = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: /// \name Execution control deba@1699: /// The simplest way to execute the algorithm is to use deba@1699: /// one of the member functions called \c run(...). deba@1699: /// \n deba@1699: /// If you need more control on the execution, deba@1699: /// first you must call \ref init(), then you can add several source nodes deba@1699: /// with \ref addSource(). deba@1699: /// Finally \ref start() will perform the actual path deba@1699: /// computation. deba@1699: deba@1699: ///@{ deba@1699: deba@1699: /// \brief Initializes the internal data structures. deba@1699: /// deba@1699: /// Initializes the internal data structures. deba@1710: void init(const Value value = OperationTraits::infinity()) { deba@1699: create_maps(); deba@1699: for (NodeIt it(*graph); it != INVALID; ++it) { deba@1699: _pred->set(it, INVALID); deba@1710: _dist->set(it, value); deba@1699: } deba@1699: } deba@1699: deba@1699: /// \brief Adds a new source node. deba@1699: /// deba@1699: /// The optional second parameter is the initial distance of the node. deba@1699: /// It just sets the distance of the node to the given value. deba@1699: void addSource(Node source, Value dst = OperationTraits::zero()) { deba@1699: _dist->set(source, dst); deba@1699: } deba@1699: deba@1699: /// \brief Executes the algorithm. deba@1699: /// deba@1699: /// \pre init() must be called and at least one node should be added deba@1699: /// with addSource() before using this function. deba@1699: /// deba@1699: /// This method runs the %BelmannFord algorithm from the root node(s) deba@1699: /// in order to compute the shortest path to each node. The algorithm deba@1699: /// computes deba@1699: /// - The shortest path tree. deba@1699: /// - The distance of each node from the root(s). deba@1699: void start() { deba@1723: int num = countNodes(*graph) - 1; deba@1723: for (int i = 0; i < num; ++i) { deba@1723: bool ready = true; deba@1699: for (EdgeIt it(*graph); it != INVALID; ++it) { deba@1699: Node source = graph->source(it); deba@1699: Node target = graph->target(it); deba@1699: Value relaxed = deba@1699: OperationTraits::plus((*_dist)[source], (*length)[it]); deba@1699: if (OperationTraits::less(relaxed, (*_dist)[target])) { deba@1699: _pred->set(target, it); deba@1699: _dist->set(target, relaxed); deba@1699: ready = false; deba@1699: } deba@1699: } deba@1723: if (ready) return; deba@1699: } deba@1699: } deba@1723: deba@1723: /// \brief Executes the algorithm and check the negative circles. deba@1723: /// deba@1723: /// \pre init() must be called and at least one node should be added deba@1723: /// with addSource() before using this function. If there is deba@1723: /// a negative circle in the graph it gives back false. deba@1723: /// deba@1723: /// This method runs the %BelmannFord algorithm from the root node(s) deba@1723: /// in order to compute the shortest path to each node. The algorithm deba@1723: /// computes deba@1723: /// - The shortest path tree. deba@1723: /// - The distance of each node from the root(s). deba@1723: bool checkedStart() { deba@1723: int num = countNodes(*graph); deba@1723: for (int i = 0; i < num; ++i) { deba@1723: bool ready = true; deba@1723: for (EdgeIt it(*graph); it != INVALID; ++it) { deba@1723: Node source = graph->source(it); deba@1723: Node target = graph->target(it); deba@1723: Value relaxed = deba@1723: OperationTraits::plus((*_dist)[source], (*length)[it]); deba@1723: if (OperationTraits::less(relaxed, (*_dist)[target])) { deba@1723: _pred->set(target, it); deba@1723: _dist->set(target, relaxed); deba@1723: ready = false; deba@1723: } deba@1723: } deba@1723: if (ready) return true; deba@1723: } deba@1723: return false; deba@1723: } deba@1699: deba@1699: /// \brief Runs %BelmannFord algorithm from node \c s. deba@1699: /// deba@1699: /// This method runs the %BelmannFord algorithm from a root node \c s deba@1699: /// in order to compute the shortest path to each node. The algorithm deba@1699: /// computes deba@1699: /// - The shortest path tree. deba@1699: /// - The distance of each node from the root. deba@1699: /// deba@1699: /// \note d.run(s) is just a shortcut of the following code. deba@1699: /// \code deba@1699: /// d.init(); deba@1699: /// d.addSource(s); deba@1699: /// d.start(); deba@1699: /// \endcode deba@1699: void run(Node s) { deba@1699: init(); deba@1699: addSource(s); deba@1699: start(); deba@1699: } deba@1699: deba@1699: ///@} deba@1699: deba@1699: /// \name Query Functions deba@1699: /// The result of the %BelmannFord algorithm can be obtained using these deba@1699: /// functions.\n deba@1699: /// Before the use of these functions, deba@1699: /// either run() or start() must be called. deba@1699: deba@1699: ///@{ deba@1699: deba@1699: /// \brief Copies the shortest path to \c t into \c p deba@1699: /// deba@1699: /// This function copies the shortest path to \c t into \c p. deba@1699: /// If it \c t is a source itself or unreachable, then it does not deba@1699: /// alter \c p. deba@1699: /// \todo Is it the right way to handle unreachable nodes? deba@1699: /// \return Returns \c true if a path to \c t was actually copied to \c p, deba@1699: /// \c false otherwise. deba@1699: /// \sa DirPath deba@1699: template deba@1699: bool getPath(Path &p, Node t) { deba@1699: if(reached(t)) { deba@1699: p.clear(); deba@1699: typename Path::Builder b(p); deba@1699: for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t)) deba@1699: b.pushFront(pred(t)); deba@1699: b.commit(); deba@1699: return true; deba@1699: } deba@1699: return false; deba@1699: } deba@1699: deba@1699: /// \brief The distance of a node from the root. deba@1699: /// deba@1699: /// Returns the distance of a node from the root. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: /// \warning If node \c v in unreachable from the root the return value deba@1699: /// of this funcion is undefined. deba@1699: Value dist(Node v) const { return (*_dist)[v]; } deba@1699: deba@1699: /// \brief Returns the 'previous edge' of the shortest path tree. deba@1699: /// deba@1699: /// For a node \c v it returns the 'previous edge' of the shortest path deba@1699: /// tree, i.e. it returns the last edge of a shortest path from the root deba@1699: /// to \c v. It is \ref INVALID if \c v is unreachable from the root or deba@1699: /// if \c v=s. The shortest path tree used here is equal to the shortest deba@1699: /// path tree used in \ref predNode(). deba@1699: /// \pre \ref run() must be called before using deba@1699: /// this function. deba@1699: /// \todo predEdge could be a better name. deba@1699: Edge pred(Node v) const { return (*_pred)[v]; } deba@1699: deba@1699: /// \brief Returns the 'previous node' of the shortest path tree. deba@1699: /// deba@1699: /// For a node \c v it returns the 'previous node' of the shortest path deba@1699: /// tree, i.e. it returns the last but one node from a shortest path from deba@1699: /// the root to \c /v. It is INVALID if \c v is unreachable from the root deba@1699: /// or if \c v=s. The shortest path tree used here is equal to the deba@1699: /// shortest path tree used in \ref pred(). \pre \ref run() must be deba@1699: /// called before using this function. deba@1699: Node predNode(Node v) const { deba@1699: return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]); deba@1699: } deba@1699: deba@1699: /// \brief Returns a reference to the NodeMap of distances. deba@1699: /// deba@1699: /// Returns a reference to the NodeMap of distances. \pre \ref run() must deba@1699: /// be called before using this function. deba@1699: const DistMap &distMap() const { return *_dist;} deba@1699: deba@1699: /// \brief Returns a reference to the shortest path tree map. deba@1699: /// deba@1699: /// Returns a reference to the NodeMap of the edges of the deba@1699: /// shortest path tree. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: const PredMap &predMap() const { return *_pred; } deba@1699: deba@1699: /// \brief Checks if a node is reachable from the root. deba@1699: /// deba@1699: /// Returns \c true if \c v is reachable from the root. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: /// deba@1699: bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); } deba@1699: deba@1699: ///@} deba@1699: }; deba@1699: deba@1699: /// \brief Default traits class of BelmannFord function. deba@1699: /// deba@1699: /// Default traits class of BelmannFord function. deba@1699: /// \param _Graph Graph type. deba@1699: /// \param _LengthMap Type of length map. deba@1699: template deba@1699: struct BelmannFordWizardDefaultTraits { deba@1699: /// \brief The graph type the algorithm runs on. deba@1699: typedef _Graph Graph; deba@1699: deba@1699: /// \brief The type of the map that stores the edge lengths. deba@1699: /// deba@1699: /// The type of the map that stores the edge lengths. deba@1699: /// It must meet the \ref concept::ReadMap "ReadMap" concept. deba@1699: typedef _LengthMap LengthMap; deba@1699: deba@1699: /// \brief The value type of the length map. deba@1699: typedef typename _LengthMap::Value Value; deba@1699: deba@1699: /// \brief Operation traits for belmann-ford algorithm. deba@1699: /// deba@1699: /// It defines the infinity type on the given Value type deba@1699: /// and the used operation. deba@1699: /// \see BelmannFordDefaultOperationTraits deba@1699: typedef BelmannFordDefaultOperationTraits OperationTraits; deba@1699: deba@1699: /// \brief The type of the map that stores the last deba@1699: /// edges of the shortest paths. deba@1699: /// deba@1699: /// The type of the map that stores the last deba@1699: /// edges of the shortest paths. deba@1699: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1699: typedef NullMap PredMap; deba@1699: deba@1699: /// \brief Instantiates a PredMap. deba@1699: /// deba@1699: /// This function instantiates a \ref PredMap. deba@1699: static PredMap *createPredMap(const _Graph &) { deba@1699: return new PredMap(); deba@1699: } deba@1699: /// \brief The type of the map that stores the dists of the nodes. deba@1699: /// deba@1699: /// The type of the map that stores the dists of the nodes. deba@1699: /// It must meet the \ref concept::WriteMap "WriteMap" concept. deba@1699: typedef NullMap DistMap; deba@1699: /// \brief Instantiates a DistMap. deba@1699: /// deba@1699: /// This function instantiates a \ref DistMap. deba@1699: static DistMap *createDistMap(const _Graph &) { deba@1699: return new DistMap(); deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief Default traits used by \ref BelmannFordWizard deba@1699: /// deba@1699: /// To make it easier to use BelmannFord algorithm deba@1699: /// we have created a wizard class. deba@1699: /// This \ref BelmannFordWizard class needs default traits, deba@1699: /// as well as the \ref BelmannFord class. deba@1699: /// The \ref BelmannFordWizardBase is a class to be the default traits of the deba@1699: /// \ref BelmannFordWizard class. deba@1699: /// \todo More named parameters are required... deba@1699: template deba@1699: class BelmannFordWizardBase deba@1699: : public BelmannFordWizardDefaultTraits<_Graph,_LengthMap> { deba@1699: deba@1699: typedef BelmannFordWizardDefaultTraits<_Graph,_LengthMap> Base; deba@1699: protected: deba@1699: /// Type of the nodes in the graph. deba@1699: typedef typename Base::Graph::Node Node; deba@1699: deba@1699: /// Pointer to the underlying graph. deba@1699: void *_graph; deba@1699: /// Pointer to the length map deba@1699: void *_length; deba@1699: ///Pointer to the map of predecessors edges. deba@1699: void *_pred; deba@1699: ///Pointer to the map of distances. deba@1699: void *_dist; deba@1699: ///Pointer to the source node. deba@1699: Node _source; deba@1699: deba@1699: public: deba@1699: /// Constructor. deba@1699: deba@1699: /// This constructor does not require parameters, therefore it initiates deba@1699: /// all of the attributes to default values (0, INVALID). deba@1699: BelmannFordWizardBase() : _graph(0), _length(0), _pred(0), deba@1699: _dist(0), _source(INVALID) {} deba@1699: deba@1699: /// Constructor. deba@1699: deba@1699: /// This constructor requires some parameters, deba@1699: /// listed in the parameters list. deba@1699: /// Others are initiated to 0. deba@1699: /// \param graph is the initial value of \ref _graph deba@1699: /// \param length is the initial value of \ref _length deba@1699: /// \param source is the initial value of \ref _source deba@1699: BelmannFordWizardBase(const _Graph& graph, deba@1699: const _LengthMap& length, deba@1699: Node source = INVALID) : deba@1699: _graph((void *)&graph), _length((void *)&length), _pred(0), deba@1699: _dist(0), _source(source) {} deba@1699: deba@1699: }; deba@1699: deba@1699: /// A class to make the usage of BelmannFord algorithm easier deba@1699: deba@1699: /// This class is created to make it easier to use BelmannFord algorithm. deba@1699: /// It uses the functions and features of the plain \ref BelmannFord, deba@1699: /// but it is much simpler to use it. deba@1699: /// deba@1699: /// Simplicity means that the way to change the types defined deba@1699: /// in the traits class is based on functions that returns the new class deba@1699: /// and not on templatable built-in classes. deba@1699: /// When using the plain \ref BelmannFord deba@1699: /// the new class with the modified type comes from deba@1699: /// the original class by using the :: deba@1699: /// operator. In the case of \ref BelmannFordWizard only deba@1699: /// a function have to be called and it will deba@1699: /// return the needed class. deba@1699: /// deba@1699: /// It does not have own \ref run method. When its \ref run method is called deba@1699: /// it initiates a plain \ref BelmannFord class, and calls the \ref deba@1699: /// BelmannFord::run method of it. deba@1699: template deba@1699: class BelmannFordWizard : public _Traits { deba@1699: typedef _Traits Base; deba@1699: deba@1699: ///The type of the underlying graph. deba@1699: typedef typename _Traits::Graph Graph; deba@1699: deba@1699: typedef typename Graph::Node Node; deba@1699: typedef typename Graph::NodeIt NodeIt; deba@1699: typedef typename Graph::Edge Edge; deba@1699: typedef typename Graph::OutEdgeIt EdgeIt; deba@1699: deba@1699: ///The type of the map that stores the edge lengths. deba@1699: typedef typename _Traits::LengthMap LengthMap; deba@1699: deba@1699: ///The type of the length of the edges. deba@1699: typedef typename LengthMap::Value Value; deba@1699: deba@1699: ///\brief The type of the map that stores the last deba@1699: ///edges of the shortest paths. deba@1699: typedef typename _Traits::PredMap PredMap; deba@1699: deba@1699: ///The type of the map that stores the dists of the nodes. deba@1699: typedef typename _Traits::DistMap DistMap; deba@1699: deba@1699: public: deba@1699: /// Constructor. deba@1699: BelmannFordWizard() : _Traits() {} deba@1699: deba@1699: /// \brief Constructor that requires parameters. deba@1699: /// deba@1699: /// Constructor that requires parameters. deba@1699: /// These parameters will be the default values for the traits class. deba@1699: BelmannFordWizard(const Graph& graph, const LengthMap& length, deba@1699: Node source = INVALID) deba@1699: : _Traits(graph, length, source) {} deba@1699: deba@1699: /// \brief Copy constructor deba@1699: BelmannFordWizard(const _Traits &b) : _Traits(b) {} deba@1699: deba@1699: ~BelmannFordWizard() {} deba@1699: deba@1699: /// \brief Runs BelmannFord algorithm from a given node. deba@1699: /// deba@1699: /// Runs BelmannFord algorithm from a given node. deba@1699: /// The node can be given by the \ref source function. deba@1699: void run() { deba@1699: if(Base::_source == INVALID) throw UninitializedParameter(); deba@1699: BelmannFord deba@1699: bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length); deba@1699: if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred); deba@1699: if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist); deba@1699: bf.run(Base::_source); deba@1699: } deba@1699: deba@1699: /// \brief Runs BelmannFord algorithm from the given node. deba@1699: /// deba@1699: /// Runs BelmannFord algorithm from the given node. deba@1699: /// \param s is the given source. deba@1699: void run(Node source) { deba@1699: Base::_source = source; deba@1699: run(); deba@1699: } deba@1699: deba@1699: template deba@1699: struct DefPredMapBase : public Base { deba@1699: typedef T PredMap; deba@1699: static PredMap *createPredMap(const Graph &) { return 0; }; deba@1699: DefPredMapBase(const _Traits &b) : _Traits(b) {} deba@1699: }; deba@1699: deba@1699: ///\brief \ref named-templ-param "Named parameter" deba@1699: ///function for setting PredMap type deba@1699: /// deba@1699: /// \ref named-templ-param "Named parameter" deba@1699: ///function for setting PredMap type deba@1699: /// deba@1699: template deba@1699: BelmannFordWizard > predMap(const T &t) deba@1699: { deba@1699: Base::_pred=(void *)&t; deba@1699: return BelmannFordWizard >(*this); deba@1699: } deba@1699: deba@1699: template deba@1699: struct DefDistMapBase : public Base { deba@1699: typedef T DistMap; deba@1699: static DistMap *createDistMap(const Graph &) { return 0; }; deba@1699: DefDistMapBase(const _Traits &b) : _Traits(b) {} deba@1699: }; deba@1699: deba@1699: ///\brief \ref named-templ-param "Named parameter" deba@1699: ///function for setting DistMap type deba@1699: /// deba@1699: /// \ref named-templ-param "Named parameter" deba@1699: ///function for setting DistMap type deba@1699: /// deba@1699: template deba@1699: BelmannFordWizard > distMap(const T &t) { deba@1699: Base::_dist=(void *)&t; deba@1699: return BelmannFordWizard >(*this); deba@1699: } deba@1710: deba@1710: template deba@1710: struct DefOperationTraitsBase : public Base { deba@1710: typedef T OperationTraits; deba@1710: DefOperationTraitsBase(const _Traits &b) : _Traits(b) {} deba@1710: }; deba@1710: deba@1710: ///\brief \ref named-templ-param "Named parameter" deba@1710: ///function for setting OperationTraits type deba@1710: /// deba@1710: /// \ref named-templ-param "Named parameter" deba@1710: ///function for setting OperationTraits type deba@1710: /// deba@1710: template deba@1710: BelmannFordWizard > distMap() { deba@1710: return BelmannFordWizard >(*this); deba@1710: } deba@1699: deba@1699: /// \brief Sets the source node, from which the BelmannFord algorithm runs. deba@1699: /// deba@1699: /// Sets the source node, from which the BelmannFord algorithm runs. deba@1699: /// \param s is the source node. deba@1699: BelmannFordWizard<_Traits>& source(Node source) { deba@1699: Base::_source = source; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: }; deba@1699: deba@1699: /// \brief Function type interface for BelmannFord algorithm. deba@1699: /// deba@1699: /// \ingroup flowalgs deba@1699: /// Function type interface for BelmannFord algorithm. deba@1699: /// deba@1699: /// This function also has several \ref named-templ-func-param deba@1699: /// "named parameters", they are declared as the members of class deba@1699: /// \ref BelmannFordWizard. deba@1699: /// The following deba@1699: /// example shows how to use these parameters. deba@1699: /// \code deba@1699: /// belmannford(g,length,source).predMap(preds).run(); deba@1699: /// \endcode deba@1699: /// \warning Don't forget to put the \ref BelmannFordWizard::run() "run()" deba@1699: /// to the end of the parameter list. deba@1699: /// \sa BelmannFordWizard deba@1699: /// \sa BelmannFord deba@1699: template deba@1699: BelmannFordWizard > deba@1699: belmannFord(const _Graph& graph, deba@1699: const _LengthMap& length, deba@1699: typename _Graph::Node source = INVALID) { deba@1699: return BelmannFordWizard > deba@1699: (graph, length, source); deba@1699: } deba@1699: deba@1699: } //END OF NAMESPACE LEMON deba@1699: deba@1699: #endif deba@1699: