deba@1912: /* -*- C++ -*- deba@1912: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1912: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1912: * deba@1912: * Permission to use, modify and distribute this software is granted deba@1912: * provided that this copyright notice appears in all copies. For deba@1912: * precise terms see the accompanying LICENSE file. deba@1912: * deba@1912: * This software is provided "AS IS" with no warranty of any kind, deba@1912: * express or implied, and with no claim as to its suitability for any deba@1912: * purpose. deba@1912: * deba@1912: */ deba@1912: deba@1912: #ifndef LEMON_DAG_SHORTEST_PATH_H deba@1912: #define LEMON_DAG_SHORTEST_PATH_H deba@1912: deba@1912: ///\ingroup flowalgs deba@1912: /// \file deba@1912: /// \brief DagShortestPath algorithm. deba@1912: /// deba@1912: deba@1912: #include deba@1993: #include deba@1912: #include deba@1912: #include deba@1912: #include deba@1912: deba@1912: #include deba@1912: deba@1912: namespace lemon { deba@1912: deba@1912: /// \brief Default OperationTraits for the DagShortestPath algorithm class. deba@1912: /// deba@1912: /// It defines all computational operations and constants which are deba@1912: /// used in the dag shortest path algorithm. The default implementation deba@1912: /// is based on the numeric_limits class. If the numeric type does not deba@1912: /// have infinity value then the maximum value is used as extremal deba@1912: /// infinity value. deba@1912: template < deba@1912: typename Value, deba@1912: bool has_infinity = std::numeric_limits::has_infinity> deba@1912: struct DagShortestPathDefaultOperationTraits { deba@1912: /// \brief Gives back the zero value of the type. deba@1912: static Value zero() { deba@1912: return static_cast(0); deba@1912: } deba@1912: /// \brief Gives back the positive infinity value of the type. deba@1912: static Value infinity() { deba@1912: return std::numeric_limits::infinity(); deba@1912: } deba@1912: /// \brief Gives back the sum of the given two elements. deba@1912: static Value plus(const Value& left, const Value& right) { deba@1912: return left + right; deba@1912: } deba@1912: /// \brief Gives back true only if the first value less than the second. deba@1912: static bool less(const Value& left, const Value& right) { deba@1912: return left < right; deba@1912: } deba@1912: }; deba@1912: deba@1912: template deba@1912: struct DagShortestPathDefaultOperationTraits { deba@1912: static Value zero() { deba@1912: return static_cast(0); deba@1912: } deba@1912: static Value infinity() { deba@1912: return std::numeric_limits::max(); deba@1912: } deba@1912: static Value plus(const Value& left, const Value& right) { deba@1912: if (left == infinity() || right == infinity()) return infinity(); deba@1912: return left + right; deba@1912: } deba@1912: static bool less(const Value& left, const Value& right) { deba@1912: return left < right; deba@1912: } deba@1912: }; deba@1912: deba@1912: /// \brief Default traits class of DagShortestPath class. deba@1912: /// deba@1912: /// Default traits class of DagShortestPath class. deba@1912: /// \param _Graph Graph type. deba@1912: /// \param _LegthMap Type of length map. deba@1912: template deba@1912: struct DagShortestPathDefaultTraits { deba@1912: /// The graph type the algorithm runs on. deba@1912: typedef _Graph Graph; deba@1912: deba@1912: /// \brief The type of the map that stores the edge lengths. deba@1912: /// deba@1912: /// The type of the map that stores the edge lengths. alpar@2260: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. deba@1912: typedef _LengthMap LengthMap; deba@1912: deba@1912: // The type of the length of the edges. deba@1912: typedef typename _LengthMap::Value Value; deba@1912: deba@1912: /// \brief Operation traits for dag shortest path algorithm. deba@1912: /// deba@1912: /// It defines the infinity type on the given Value type deba@1912: /// and the used operation. deba@1912: /// \see DagShortestPathDefaultOperationTraits deba@1912: typedef DagShortestPathDefaultOperationTraits OperationTraits; deba@1912: deba@1912: /// \brief The type of the map that stores the last edges of the deba@1912: /// shortest paths. deba@1912: /// deba@1912: /// The type of the map that stores the last deba@1912: /// edges of the shortest paths. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: /// deba@1912: typedef typename Graph::template NodeMap PredMap; deba@1912: deba@1912: /// \brief Instantiates a PredMap. deba@1912: /// deba@1912: /// This function instantiates a \ref PredMap. alpar@1946: /// \param graph is the graph, to which we would alpar@1946: /// like to define the PredMap. deba@1912: /// \todo The graph alone may be insufficient for the initialization deba@1912: static PredMap *createPredMap(const _Graph& graph) { deba@1912: return new PredMap(graph); deba@1912: } deba@1912: deba@1912: /// \brief The type of the map that stores the dists of the nodes. deba@1912: /// deba@1912: /// The type of the map that stores the dists of the nodes. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: /// deba@1912: typedef typename Graph::template NodeMap deba@1912: DistMap; deba@1912: deba@1912: /// \brief Instantiates a DistMap. deba@1912: /// deba@1912: /// This function instantiates a \ref DistMap. alpar@1946: /// \param graph is the graph, to which we would like to define the deba@1912: /// \ref DistMap deba@1912: static DistMap *createDistMap(const _Graph& graph) { deba@1912: return new DistMap(graph); deba@1912: } deba@1912: deba@1912: }; deba@1912: deba@1912: /// \brief Inverse OperationTraits for the DagShortestPath algorithm class. deba@1912: /// deba@1912: /// It defines all computational operations and constants which are deba@1912: /// used in the dag shortest path algorithm. It is the inverse of deba@1912: /// \ref DagShortestPathDefaultOperationTraits, so it can be used to deba@1912: /// calculate the longest path. The default implementation deba@1912: /// is based on the numeric_limits class. If the numeric type does not deba@1912: /// have infinity value then the minimum value is used as extremal deba@1912: /// infinity value. deba@1912: template < deba@1912: typename Value, deba@1912: bool has_infinity = std::numeric_limits::has_infinity> deba@1912: struct DagLongestPathOperationTraits { deba@1912: /// \brief Gives back the zero value of the type. deba@1912: static Value zero() { deba@1912: return static_cast(0); deba@1912: } deba@1912: /// \brief Gives back the negative infinity value of the type. deba@1912: static Value infinity() { deba@1912: return -(std::numeric_limits::infinity()); deba@1912: } deba@1912: /// \brief Gives back the sum of the given two elements. deba@1912: static Value plus(const Value& left, const Value& right) { deba@1912: return left + right; deba@1912: } deba@1912: /// \brief Gives back true only if the first value less than the second. deba@1912: static bool less(const Value& left, const Value& right) { deba@1912: return left > right; deba@1912: } deba@1912: }; deba@1912: deba@1912: template deba@1912: struct DagLongestPathOperationTraits { deba@1912: static Value zero() { deba@1912: return static_cast(0); deba@1912: } deba@1912: static Value infinity() { deba@1912: return std::numeric_limits::min(); deba@1912: } deba@1912: static Value plus(const Value& left, const Value& right) { deba@1912: if (left == infinity() || right == infinity()) return infinity(); deba@1912: return left + right; deba@1912: } deba@1912: static bool less(const Value& left, const Value& right) { deba@1912: return left > right; deba@1912: } deba@1912: }; deba@1912: deba@1912: /// \brief Inverse traits class of DagShortestPath class. deba@1912: /// deba@1912: /// Inverse traits class of DagShortestPath class. deba@1912: /// \param _Graph Graph type. deba@1912: /// \param _LegthMap Type of length map. deba@1912: template deba@1912: struct DagLongestPathTraits { deba@1912: /// The graph type the algorithm runs on. deba@1912: typedef _Graph Graph; deba@1912: deba@1912: /// \brief The type of the map that stores the edge lengths. deba@1912: /// deba@1912: /// The type of the map that stores the edge lengths. alpar@2260: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. deba@1912: typedef _LengthMap LengthMap; deba@1912: deba@1912: // The type of the length of the edges. deba@1912: typedef typename _LengthMap::Value Value; deba@1912: deba@1912: /// \brief Inverse operation traits for dag shortest path algorithm. deba@1912: /// deba@1912: /// It defines the infinity type on the given Value type deba@1912: /// and the used operation. deba@1912: /// \see DagLongestPathOperationTraits deba@1912: typedef DagLongestPathOperationTraits OperationTraits; deba@1912: deba@1912: /// \brief The type of the map that stores the last edges of the deba@1912: /// longest paths. deba@1912: /// deba@1912: /// The type of the map that stores the last deba@1912: /// edges of the longest paths. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: /// deba@1912: typedef typename Graph::template NodeMap PredMap; deba@1912: deba@1912: /// \brief Instantiates a PredMap. deba@1912: /// deba@1912: /// This function instantiates a \ref PredMap. alpar@1946: /// \param graph is the graph, alpar@1946: /// to which we would like to define the PredMap. deba@1912: /// \todo The graph alone may be insufficient for the initialization deba@1912: static PredMap *createPredMap(const _Graph& graph) { deba@1912: return new PredMap(graph); deba@1912: } deba@1912: deba@1912: /// \brief The type of the map that stores the dists of the nodes. deba@1912: /// deba@1912: /// The type of the map that stores the dists of the nodes. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: /// deba@1912: typedef typename Graph::template NodeMap deba@1912: DistMap; deba@1912: deba@1912: /// \brief Instantiates a DistMap. deba@1912: /// deba@1912: /// This function instantiates a \ref DistMap. alpar@1946: /// \param graph is the graph, to which we would like to define the deba@1912: /// \ref DistMap deba@1912: static DistMap *createDistMap(const _Graph& graph) { deba@1912: return new DistMap(graph); deba@1912: } deba@1912: deba@1912: }; deba@1912: deba@1912: deba@1912: /// \brief %DagShortestPath algorithm class. deba@1912: /// deba@1912: /// \ingroup flowalgs deba@1912: /// This class provides an efficient implementation of a Dag sortest path deba@1912: /// searching algorithm. The edge lengths are passed to the algorithm alpar@2260: /// using a \ref concepts::ReadMap "ReadMap", so it is easy to change it deba@1912: /// to any kind of length. deba@1912: /// deba@1912: /// The complexity of the algorithm is O(n + e). deba@1912: /// deba@1912: /// The type of the length is determined by the alpar@2260: /// \ref concepts::ReadMap::Value "Value" of the length map. deba@1912: /// deba@1912: /// \param _Graph The graph type the algorithm runs on. The default value deba@1912: /// is \ref ListGraph. The value of _Graph is not used directly by deba@1912: /// DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits. deba@1912: /// \param _LengthMap This read-only EdgeMap determines the lengths of the alpar@2260: /// edges. The default map type is \ref concepts::Graph::EdgeMap deba@1912: /// "Graph::EdgeMap". The value of _LengthMap is not used directly deba@1912: /// by DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits. deba@1912: /// \param _Traits Traits class to set various data types used by the deba@1912: /// algorithm. The default traits class is \ref DagShortestPathDefaultTraits deba@1912: /// "DagShortestPathDefaultTraits<_Graph,_LengthMap>". See \ref deba@1912: /// DagShortestPathDefaultTraits for the documentation of a DagShortestPath traits deba@1912: /// class. deba@1912: /// deba@1912: /// \author Balazs Attila Mihaly (based on Balazs Dezso's work) deba@1912: deba@1912: #ifdef DOXYGEN deba@1912: template deba@1912: #else deba@1912: template , deba@1912: typename _Traits=DagShortestPathDefaultTraits<_Graph,_LengthMap> > deba@1912: #endif deba@1912: class DagShortestPath { deba@1912: public: deba@1912: deba@1912: /// \brief \ref Exception for uninitialized parameters. deba@1912: /// deba@1912: /// This error represents problems in the initialization deba@1912: /// of the parameters of the algorithms. deba@1912: deba@1912: class UninitializedParameter : public lemon::UninitializedParameter { deba@1912: public: alpar@2151: virtual const char* what() const throw() { deba@1912: return "lemon::DagShortestPath::UninitializedParameter"; deba@1912: } deba@1912: }; deba@1912: deba@1912: typedef _Traits Traits; deba@1912: ///The type of the underlying graph. deba@1912: typedef typename _Traits::Graph Graph; deba@1912: deba@1912: typedef typename Graph::Node Node; deba@1912: typedef typename Graph::NodeIt NodeIt; deba@1912: typedef typename Graph::Edge Edge; deba@1912: typedef typename Graph::EdgeIt EdgeIt; deba@1912: typedef typename Graph::OutEdgeIt OutEdgeIt; deba@1912: deba@1912: /// \brief The type of the length of the edges. deba@1912: typedef typename _Traits::LengthMap::Value Value; deba@1912: /// \brief The type of the map that stores the edge lengths. deba@1912: typedef typename _Traits::LengthMap LengthMap; deba@1912: /// \brief The type of the map that stores the last deba@1912: /// edges of the shortest paths. deba@1912: typedef typename _Traits::PredMap PredMap; deba@1912: /// \brief The type of the map that stores the dists of the nodes. deba@1912: typedef typename _Traits::DistMap DistMap; deba@1912: /// \brief The operation traits. deba@1912: typedef typename _Traits::OperationTraits OperationTraits; deba@1912: /// \brief The Node weight map. deba@1999: typedef typename Graph::template NodeMap WeightMap; deba@1912: private: deba@1912: /// Pointer to the underlying graph deba@1912: const Graph *graph; deba@1912: /// Pointer to the length map deba@1912: const LengthMap *length; deba@1912: ///Pointer to the map of predecessors edges deba@1912: PredMap *_pred; deba@1912: ///Indicates if \ref _pred is locally allocated (\c true) or not deba@1912: bool local_pred; deba@1912: ///Pointer to the map of distances deba@1912: DistMap *_dist; deba@1912: ///Indicates if \ref _dist is locally allocated (\c true) or not deba@1912: bool local_dist; deba@1912: ///Process step counter deba@1912: unsigned int _process_step; deba@1912: deba@1912: std::vector _node_order; deba@1912: deba@1912: /// Creates the maps if necessary. deba@1912: void create_maps() { deba@1912: if(!_pred) { deba@1912: local_pred = true; deba@1912: _pred = Traits::createPredMap(*graph); deba@1912: } deba@1912: if(!_dist) { deba@1912: local_dist = true; deba@1912: _dist = Traits::createDistMap(*graph); deba@1912: } deba@1912: } deba@1912: deba@1912: public : deba@1912: deba@1912: typedef DagShortestPath Create; deba@1912: deba@1912: /// \name Named template parameters deba@1912: deba@1912: ///@{ deba@1912: deba@1912: template deba@1912: struct DefPredMapTraits : public Traits { deba@1912: typedef T PredMap; deba@1912: static PredMap *createPredMap(const Graph&) { deba@1912: throw UninitializedParameter(); deba@1912: } deba@1912: }; deba@1912: deba@1912: /// \brief \ref named-templ-param "Named parameter" for setting PredMap deba@1912: /// type deba@1912: /// \ref named-templ-param "Named parameter" for setting PredMap type deba@1912: /// deba@1912: template deba@1912: struct DefPredMap { deba@1912: typedef DagShortestPath< Graph, LengthMap, DefPredMapTraits > Create; deba@1912: }; deba@1912: deba@1912: template deba@1912: struct DefDistMapTraits : public Traits { deba@1912: typedef T DistMap; deba@1912: static DistMap *createDistMap(const Graph& graph) { deba@1912: throw UninitializedParameter(); deba@1912: } deba@1912: }; deba@1912: deba@1912: /// \brief \ref named-templ-param "Named parameter" for setting DistMap deba@1912: /// type deba@1912: /// deba@1912: /// \ref named-templ-param "Named parameter" for setting DistMap type deba@1912: /// deba@1912: template deba@1912: struct DefDistMap deba@1912: : public DagShortestPath< Graph, LengthMap, DefDistMapTraits > { deba@1912: typedef DagShortestPath< Graph, LengthMap, DefDistMapTraits > Create; deba@1912: }; deba@1912: deba@1912: template deba@1912: struct DefOperationTraitsTraits : public Traits { deba@1912: typedef T OperationTraits; deba@1912: }; deba@1912: deba@1912: /// \brief \ref named-templ-param "Named parameter" for setting deba@1912: /// OperationTraits type deba@1912: /// deba@1912: /// \ref named-templ-param "Named parameter" for setting OperationTraits deba@1912: /// type deba@1912: template deba@1912: struct DefOperationTraits deba@1912: : public DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits > { deba@1912: typedef DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits > deba@1912: Create; deba@1912: }; deba@1912: deba@1912: ///@} deba@1912: deba@1912: protected: deba@1912: deba@1912: DagShortestPath() {} deba@1912: deba@1912: public: deba@1912: deba@1912: /// \brief Constructor. deba@1912: /// deba@1912: /// \param _graph the graph the algorithm will run on. deba@1912: /// \param _length the length map used by the algorithm. deba@1912: DagShortestPath(const Graph& _graph, const LengthMap& _length) : deba@1912: graph(&_graph), length(&_length), deba@1912: _pred(0), local_pred(false), deba@1912: _dist(0), local_dist(false){} deba@1912: deba@1912: /// \brief Constructor with node weight map. deba@1912: /// deba@1912: /// \param _graph the graph the algorithm will run on. deba@1912: /// \param _length the length map used by the algorithm. deba@1912: /// The NodeMap _length will be used as the weight map. deba@1912: /// Each edge will have the weight of its target node. deba@1912: DagShortestPath(const Graph& _graph, const WeightMap& _length) : deba@1912: graph(&_graph), deba@1912: _pred(0), local_pred(false), deba@1912: _dist(0), local_dist(false){ deba@1912: length=new LengthMap(_graph); deba@1912: _dist=new DistMap(_graph); deba@1912: for(EdgeIt eit(_graph);eit!=INVALID;++eit) deba@1912: (const_cast(length))->set(eit,_length[_graph.target(eit)]); deba@1912: } deba@1912: deba@1912: ///Destructor. deba@1912: ~DagShortestPath() { deba@1912: if(local_pred) delete _pred; deba@1912: if(local_dist) delete _dist; deba@1912: } deba@1912: deba@1912: /// \brief Sets the length map. deba@1912: /// deba@1912: /// Sets the length map. deba@1912: /// \return \c (*this) deba@1912: DagShortestPath &lengthMap(const LengthMap &m) { deba@1912: length = &m; deba@1912: return *this; deba@1912: } deba@1912: deba@1912: /// \brief Sets the map storing the predecessor edges. deba@1912: /// deba@1912: /// Sets the map storing the predecessor edges. deba@1912: /// If you don't use this function before calling \ref run(), deba@1912: /// it will allocate one. The destuctor deallocates this deba@1912: /// automatically allocated map, of course. deba@1912: /// \return \c (*this) deba@1912: DagShortestPath &predMap(PredMap &m) { deba@1912: if(local_pred) { deba@1912: delete _pred; deba@1912: local_pred=false; deba@1912: } deba@1912: _pred = &m; deba@1912: return *this; deba@1912: } deba@1912: deba@1912: /// \brief Sets the map storing the distances calculated by the algorithm. deba@1912: /// deba@1912: /// Sets the map storing the distances calculated by the algorithm. deba@1912: /// If you don't use this function before calling \ref run(), deba@1912: /// it will allocate one. The destuctor deallocates this deba@1912: /// automatically allocated map, of course. deba@1912: /// \return \c (*this) deba@1912: DagShortestPath &distMap(DistMap &m) { deba@1912: if(local_dist) { deba@1912: delete _dist; deba@1912: local_dist=false; deba@1912: } deba@1912: _dist = &m; deba@1912: return *this; deba@1912: } deba@1912: deba@1912: /// \name Execution control deba@1912: /// The simplest way to execute the algorithm is to use deba@1912: /// one of the member functions called \c run(...) deba@1912: /// \n deba@1912: /// If you need more control on the execution, deba@1912: /// first you must call \ref init(...), then you can add several source deba@1912: /// nodes with \ref addSource(). deba@1912: /// Finally \ref start() will perform the actual path computation. deba@1912: /// Some functions have an alternative form (\ref checkedInit(...), deba@1912: /// \ref checkedRun(...)) which also verifies if the graph given in the deba@1912: /// constructor is a dag. deba@1912: deba@1912: ///@{ deba@1912: deba@1912: /// \brief Initializes the internal data structures. deba@1912: /// deba@1912: /// Initializes the internal data structures. deba@1912: void init(const Value value = OperationTraits::infinity()) { deba@1912: typedef typename Graph::template NodeMap NodeOrderMap; deba@1912: _process_step=0; deba@1912: NodeOrderMap node_order(*graph); deba@1912: topologicalSort(*graph,node_order); deba@1912: _node_order.resize(countNodes(*graph),INVALID); deba@1912: create_maps(); deba@1912: for (NodeIt it(*graph); it != INVALID; ++it) { deba@1912: _node_order[node_order[it]]=it; deba@1912: _pred->set(it, INVALID); deba@1912: _dist->set(it, value); deba@1912: } deba@1912: } deba@1912: deba@1912: /// \brief Initializes the internal data structures deba@1912: /// with a given topological sort (NodeMap). deba@1912: /// deba@1912: /// Initializes the internal data structures deba@1912: /// with a given topological sort (NodeMap). deba@1912: void init(const typename Graph::template NodeMap& node_order, deba@1912: const Value value = OperationTraits::infinity()) { deba@1912: _process_step=0; deba@1912: _node_order.resize(countNodes(*graph),INVALID); deba@1912: create_maps(); deba@1912: for (NodeIt it(*graph); it != INVALID; ++it) { deba@1912: _node_order[node_order[it]]=it; deba@1912: _pred->set(it, INVALID); deba@1912: _dist->set(it, value); deba@1912: } deba@1912: } deba@1912: deba@1912: /// \brief Initializes the internal data structures deba@1912: /// with a given topological sort (std::vector). deba@1912: /// deba@1912: /// Initializes the internal data structures deba@1912: /// with a given topological sort (std::vector). deba@1912: void init(const std::vector& node_order, deba@1912: const Value value = OperationTraits::infinity()) { deba@1912: _process_step=0; deba@1912: _node_order=node_order; deba@1912: create_maps(); deba@1912: for (NodeIt it(*graph); it != INVALID; ++it) { deba@1912: _pred->set(it, INVALID); deba@1912: _dist->set(it, value); deba@1912: } deba@1912: } deba@1912: deba@1912: /// \brief Initializes the internal data structures. It also checks if the graph is dag. deba@1912: /// deba@1912: /// Initializes the internal data structures. It also checks if the graph is dag. deba@1912: /// \return true if the graph (given in the constructor) is dag, false otherwise. deba@1912: bool checkedInit(const Value value = OperationTraits::infinity()) { deba@1912: typedef typename Graph::template NodeMap NodeOrderMap; deba@1912: NodeOrderMap node_order(*graph); deba@1912: if(!checkedTopologicalSort(*graph,node_order))return false; deba@1912: init(node_order,value); deba@1912: return true; deba@1912: } deba@1912: deba@1912: /// \brief Initializes the internal data structures with a given deba@1912: /// topological sort (NodeMap). It also checks if the graph is dag. deba@1912: /// deba@1912: /// Initializes the internal data structures with a given deba@1912: /// topological sort (NodeMap). It also checks if the graph is dag. deba@1912: /// \return true if the graph (given in the constructor) is dag, false otherwise. deba@1912: bool checkedInit(const typename Graph::template NodeMap& node_order, deba@1912: const Value value = OperationTraits::infinity()) { deba@1912: for(NodeIt it(*graph);it!=INVALID;++it){ deba@1912: for(OutEdgeIt oeit(*graph,it);oeit!=INVALID;++oeit){ deba@1912: if(node_order[graph->target(oeit)]& node_order, deba@1912: const Value value = OperationTraits::infinity()) { deba@1912: typedef typename Graph::template NodeMap BoolNodeMap; deba@1912: BoolNodeMap _processed(*graph,false); deba@1912: for(unsigned int i=0;i<_node_order.size();++i){ deba@1912: _processed[node_order[i]]=true; deba@1912: for(OutEdgeIt oeit(*graph,node_order[i]);oeit!=INVALID;++oeit){ deba@1912: if(_processed[graph->target(oeit)])return false; deba@1912: } deba@1912: } deba@1912: init(node_order,value); deba@1912: return true; deba@1912: } deba@1912: deba@1912: /// \brief Adds a new source node. deba@1912: /// deba@1912: /// The optional second parameter is the initial distance of the node. deba@1912: /// It just sets the distance of the node to the given value. deba@1912: void addSource(Node source, Value dst = OperationTraits::zero()) { deba@1912: if((*_dist)[source] != dst){ deba@1912: _dist->set(source, dst); deba@1912: } deba@1912: } deba@1912: deba@1912: /// \brief Executes one step from the dag shortest path algorithm. deba@1912: /// deba@1912: /// If the algoritm calculated the distances in the previous step deba@1912: /// strictly for all at most k length paths then it will calculate the deba@1912: /// distances strictly for all at most k + 1 length paths. With k deba@1912: /// iteration this function calculates the at most k length paths. deba@1912: ///\pre the queue is not empty deba@1912: ///\return the currently processed node deba@1912: Node processNextNode() { deba@1912: if(reached(_node_order[_process_step])){ deba@1912: for (OutEdgeIt it(*graph, _node_order[_process_step]); it != INVALID; ++it) { deba@1912: Node target = graph->target(it); deba@1912: Value relaxed = deba@1912: OperationTraits::plus((*_dist)[_node_order[_process_step]], (*length)[it]); deba@1912: if (OperationTraits::less(relaxed, (*_dist)[target])) { deba@1912: _pred->set(target, it); deba@1912: _dist->set(target, relaxed); deba@1912: } deba@1912: } deba@1912: } deba@1912: ++_process_step; deba@1912: return _node_order[_process_step-1]; deba@1912: } deba@1912: deba@1912: ///\brief Returns \c false if there are nodes deba@1912: ///to be processed in the queue deba@1912: /// deba@1912: ///Returns \c false if there are nodes deba@1912: ///to be processed in the queue deba@1912: bool emptyQueue() { return _node_order.size()-1==_process_step; } deba@1912: deba@1912: ///\brief Returns the number of the nodes to be processed. deba@1912: /// deba@1912: ///Returns the number of the nodes to be processed in the queue. deba@1912: int queueSize() { return _node_order.size()-1-_process_step; } deba@1912: deba@1912: /// \brief Executes the algorithm. deba@1912: /// deba@1912: /// \pre init() must be called and at least one node should be added deba@1912: /// with addSource() before using this function. deba@1912: /// deba@1912: /// This method runs the %DagShortestPath algorithm from the root node(s) deba@1912: /// in order to compute the shortest path to each node. The algorithm deba@1912: /// computes deba@1912: /// - The shortest path tree. deba@1912: /// - The distance of each node from the root(s). deba@1912: void start() { deba@1912: while(!emptyQueue()) { deba@1912: processNextNode(); deba@1912: } deba@1912: } deba@1912: deba@1912: /// \brief Runs %DagShortestPath algorithm from node \c s. deba@1912: /// deba@1912: /// This method runs the %DagShortestPath algorithm from a root node \c s deba@1912: /// in order to compute the shortest path to each node. The algorithm deba@1912: /// computes deba@1912: /// - The shortest path tree. deba@1912: /// - The distance of each node from the root. deba@1912: /// deba@1912: /// \note d.run(s) is just a shortcut of the following code. alpar@1946: ///\code deba@1912: /// d.init(); deba@1912: /// d.addSource(s); deba@1912: /// d.start(); alpar@1946: ///\endcode deba@1912: void run(Node s) { deba@1912: init(); deba@1912: addSource(s); deba@1912: start(); deba@1912: } deba@1912: deba@1912: /// \brief Runs %DagShortestPath algorithm from node \c s. deba@1912: /// It also checks if the graph is a dag. deba@1912: /// deba@1912: /// This method runs the %DagShortestPath algorithm from a root node \c s deba@1912: /// in order to compute the shortest path to each node. The algorithm deba@1912: /// computes deba@1912: /// - The shortest path tree. deba@1912: /// - The distance of each node from the root. deba@1912: /// The algorithm checks if the graph given int the constructor is a dag. deba@1912: bool checkedRun(Node s) { deba@1912: if(!checkedInit())return false; deba@1912: addSource(s); deba@1912: start(); deba@1912: return true; deba@1912: } deba@1912: deba@1912: ///@} deba@1912: deba@1912: /// \name Query Functions deba@1912: /// The result of the %DagShortestPath algorithm can be obtained using these deba@1912: /// functions.\n deba@1912: /// Before the use of these functions, deba@1912: /// either run() or start() must be called. deba@1912: deba@1912: ///@{ deba@1912: deba@2335: typedef PredMapPath Path; deba@2335: deba@2335: ///Gives back the shortest path. deba@2335: deba@2335: ///Gives back the shortest path. deba@2335: ///\pre The \c t should be reachable from the source. deba@2335: Path path(Node t) deba@2335: { deba@2335: return Path(*graph, *_pred, t); deba@1912: } deba@1912: deba@1912: /// \brief The distance of a node from the root. deba@1912: /// deba@1912: /// Returns the distance of a node from the root. deba@1912: /// \pre \ref run() must be called before using this function. deba@1912: /// \warning If node \c v in unreachable from the root the return value deba@1912: /// of this funcion is undefined. deba@1912: Value dist(Node v) const { return (*_dist)[v]; } deba@1912: deba@1912: /// \brief Returns the 'previous edge' of the shortest path tree. deba@1912: /// deba@1912: /// For a node \c v it returns the 'previous edge' of the shortest path deba@1912: /// tree, i.e. it returns the last edge of a shortest path from the root deba@1912: /// to \c v. It is \ref INVALID if \c v is unreachable from the root or deba@1912: /// if \c v=s. The shortest path tree used here is equal to the shortest deba@1912: /// path tree used in \ref predNode(). deba@1912: /// \pre \ref run() must be called before using deba@1912: /// this function. deba@1912: Edge predEdge(Node v) const { return (*_pred)[v]; } deba@1912: deba@1912: /// \brief Returns the 'previous node' of the shortest path tree. deba@1912: /// deba@1912: /// For a node \c v it returns the 'previous node' of the shortest path deba@1912: /// tree, i.e. it returns the last but one node from a shortest path from deba@1912: /// the root to \c /v. It is INVALID if \c v is unreachable from the root deba@1912: /// or if \c v=s. The shortest path tree used here is equal to the deba@1912: /// shortest path tree used in \ref predEdge(). \pre \ref run() must be deba@1912: /// called before using this function. deba@1912: Node predNode(Node v) const { deba@1912: return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]); deba@1912: } deba@1912: deba@1912: /// \brief Returns a reference to the NodeMap of distances. deba@1912: /// deba@1912: /// Returns a reference to the NodeMap of distances. \pre \ref run() must deba@1912: /// be called before using this function. deba@1912: const DistMap &distMap() const { return *_dist;} deba@1912: deba@1912: /// \brief Returns a reference to the shortest path tree map. deba@1912: /// deba@1912: /// Returns a reference to the NodeMap of the edges of the deba@1912: /// shortest path tree. deba@1912: /// \pre \ref run() must be called before using this function. deba@1912: const PredMap &predMap() const { return *_pred; } deba@1912: deba@1912: /// \brief Checks if a node is reachable from the root. deba@1912: /// deba@1912: /// Returns \c true if \c v is reachable from the root. deba@1912: /// \pre \ref run() must be called before using this function. deba@1912: /// deba@1912: bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); } deba@1912: deba@1912: ///@} deba@1912: }; deba@1912: deba@1912: /// \brief Default traits class of DagShortestPath function. deba@1912: /// deba@1912: /// Default traits class of DagShortestPath function. deba@1912: /// \param _Graph Graph type. deba@1912: /// \param _LengthMap Type of length map. deba@1912: template deba@1912: struct DagShortestPathWizardDefaultTraits { deba@1912: /// \brief The graph type the algorithm runs on. deba@1912: typedef _Graph Graph; deba@1912: deba@1912: /// \brief The type of the map that stores the edge lengths. deba@1912: /// deba@1912: /// The type of the map that stores the edge lengths. alpar@2260: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. deba@1912: typedef _LengthMap LengthMap; deba@1912: deba@1912: /// \brief The value type of the length map. deba@1912: typedef typename _LengthMap::Value Value; deba@1912: deba@1912: /// \brief Operation traits for dag shortest path algorithm. deba@1912: /// deba@1912: /// It defines the infinity type on the given Value type deba@1912: /// and the used operation. deba@1912: /// \see DagShortestPathDefaultOperationTraits deba@1912: typedef DagShortestPathDefaultOperationTraits OperationTraits; deba@1912: deba@1912: /// \brief The type of the map that stores the last deba@1912: /// edges of the shortest paths. deba@1912: /// deba@1912: /// The type of the map that stores the last deba@1912: /// edges of the shortest paths. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: typedef NullMap PredMap; deba@1912: deba@1912: /// \brief Instantiates a PredMap. deba@1912: /// deba@1912: /// This function instantiates a \ref PredMap. deba@1912: static PredMap *createPredMap(const _Graph &) { deba@1912: return new PredMap(); deba@1912: } deba@1912: /// \brief The type of the map that stores the dists of the nodes. deba@1912: /// deba@1912: /// The type of the map that stores the dists of the nodes. alpar@2260: /// It must meet the \ref concepts::WriteMap "WriteMap" concept. deba@1912: typedef NullMap DistMap; deba@1912: /// \brief Instantiates a DistMap. deba@1912: /// deba@1912: /// This function instantiates a \ref DistMap. deba@1912: static DistMap *createDistMap(const _Graph &) { deba@1912: return new DistMap(); deba@1912: } deba@1912: }; deba@1912: deba@1912: /// \brief Default traits used by \ref DagShortestPathWizard deba@1912: /// deba@1912: /// To make it easier to use DagShortestPath algorithm deba@1912: /// we have created a wizard class. deba@1912: /// This \ref DagShortestPathWizard class needs default traits, deba@1912: /// as well as the \ref DagShortestPath class. deba@1912: /// The \ref DagShortestPathWizardBase is a class to be the default traits of the deba@1912: /// \ref DagShortestPathWizard class. deba@1912: /// \todo More named parameters are required... deba@1912: template deba@1912: class DagShortestPathWizardBase deba@1912: : public DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> { deba@1912: deba@1912: typedef DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> Base; deba@1912: protected: deba@1912: /// Type of the nodes in the graph. deba@1912: typedef typename Base::Graph::Node Node; deba@1912: deba@1912: /// Pointer to the underlying graph. deba@1912: void *_graph; deba@1912: /// Pointer to the length map deba@1912: void *_length; deba@1912: ///Pointer to the map of predecessors edges. deba@1912: void *_pred; deba@1912: ///Pointer to the map of distances. deba@1912: void *_dist; deba@1912: ///Pointer to the source node. deba@1912: Node _source; deba@1912: deba@1912: public: deba@1912: /// Constructor. deba@1912: deba@1912: /// This constructor does not require parameters, therefore it initiates deba@1912: /// all of the attributes to default values (0, INVALID). deba@1912: DagShortestPathWizardBase() : _graph(0), _length(0), _pred(0), deba@1912: _dist(0), _source(INVALID) {} deba@1912: deba@1912: /// Constructor. deba@1912: deba@1912: /// This constructor requires some parameters, deba@1912: /// listed in the parameters list. deba@1912: /// Others are initiated to 0. deba@1912: /// \param graph is the initial value of \ref _graph deba@1912: /// \param length is the initial value of \ref _length deba@1912: /// \param source is the initial value of \ref _source deba@1912: DagShortestPathWizardBase(const _Graph& graph, deba@1912: const _LengthMap& length, deba@1912: Node source = INVALID) : deba@1912: _graph((void *)&graph), _length((void *)&length), _pred(0), deba@1912: _dist(0), _source(source) {} deba@1912: deba@1912: }; deba@1912: deba@1912: /// A class to make the usage of DagShortestPath algorithm easier deba@1912: deba@1912: /// This class is created to make it easier to use DagShortestPath algorithm. deba@1912: /// It uses the functions and features of the plain \ref DagShortestPath, deba@1912: /// but it is much simpler to use it. deba@1912: /// deba@1912: /// Simplicity means that the way to change the types defined deba@1912: /// in the traits class is based on functions that returns the new class deba@1912: /// and not on templatable built-in classes. deba@1912: /// When using the plain \ref DagShortestPath deba@1912: /// the new class with the modified type comes from deba@1912: /// the original class by using the :: deba@1912: /// operator. In the case of \ref DagShortestPathWizard only deba@1912: /// a function have to be called and it will deba@1912: /// return the needed class. deba@1912: /// deba@1912: /// It does not have own \ref run method. When its \ref run method is called deba@1912: /// it initiates a plain \ref DagShortestPath class, and calls the \ref deba@1912: /// DagShortestPath::run() method of it. deba@1912: template deba@1912: class DagShortestPathWizard : public _Traits { deba@1912: typedef _Traits Base; deba@1912: deba@1912: ///The type of the underlying graph. deba@1912: typedef typename _Traits::Graph Graph; deba@1912: deba@1912: typedef typename Graph::Node Node; deba@1912: typedef typename Graph::NodeIt NodeIt; deba@1912: typedef typename Graph::Edge Edge; deba@1912: typedef typename Graph::OutEdgeIt EdgeIt; deba@1912: deba@1912: ///The type of the map that stores the edge lengths. deba@1912: typedef typename _Traits::LengthMap LengthMap; deba@1912: deba@1912: ///The type of the length of the edges. deba@1912: typedef typename LengthMap::Value Value; deba@1912: deba@1912: ///\brief The type of the map that stores the last deba@1912: ///edges of the shortest paths. deba@1912: typedef typename _Traits::PredMap PredMap; deba@1912: deba@1912: ///The type of the map that stores the dists of the nodes. deba@1912: typedef typename _Traits::DistMap DistMap; deba@1912: deba@1912: public: deba@1912: /// Constructor. deba@1912: DagShortestPathWizard() : _Traits() {} deba@1912: deba@1912: /// \brief Constructor that requires parameters. deba@1912: /// deba@1912: /// Constructor that requires parameters. deba@1912: /// These parameters will be the default values for the traits class. deba@1912: DagShortestPathWizard(const Graph& graph, const LengthMap& length, deba@1912: Node source = INVALID) deba@1912: : _Traits(graph, length, source) {} deba@1912: deba@1912: /// \brief Copy constructor deba@1912: DagShortestPathWizard(const _Traits &b) : _Traits(b) {} deba@1912: deba@1912: ~DagShortestPathWizard() {} deba@1912: deba@1912: /// \brief Runs DagShortestPath algorithm from a given node. deba@1912: /// deba@1912: /// Runs DagShortestPath algorithm from a given node. deba@1912: /// The node can be given by the \ref source function. deba@1912: void run() { deba@1912: if(Base::_source == INVALID) throw UninitializedParameter(); deba@1912: DagShortestPath deba@1912: bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length); deba@1912: if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred); deba@1912: if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist); deba@1912: bf.run(Base::_source); deba@1912: } deba@1912: deba@1912: /// \brief Runs DagShortestPath algorithm from the given node. deba@1912: /// deba@1912: /// Runs DagShortestPath algorithm from the given node. alpar@1946: /// \param source is the given source. deba@1912: void run(Node source) { deba@1912: Base::_source = source; deba@1912: run(); deba@1912: } deba@1912: deba@1912: template deba@1912: struct DefPredMapBase : public Base { deba@1912: typedef T PredMap; deba@1912: static PredMap *createPredMap(const Graph &) { return 0; }; deba@1912: DefPredMapBase(const _Traits &b) : _Traits(b) {} deba@1912: }; deba@1912: deba@1912: ///\brief \ref named-templ-param "Named parameter" deba@1912: ///function for setting PredMap type deba@1912: /// deba@1912: /// \ref named-templ-param "Named parameter" deba@1912: ///function for setting PredMap type deba@1912: /// deba@1912: template deba@1912: DagShortestPathWizard > predMap(const T &t) deba@1912: { deba@1912: Base::_pred=(void *)&t; deba@1912: return DagShortestPathWizard >(*this); deba@1912: } deba@1912: deba@1912: template deba@1912: struct DefDistMapBase : public Base { deba@1912: typedef T DistMap; deba@1912: static DistMap *createDistMap(const Graph &) { return 0; }; deba@1912: DefDistMapBase(const _Traits &b) : _Traits(b) {} deba@1912: }; deba@1912: deba@1912: ///\brief \ref named-templ-param "Named parameter" deba@1912: ///function for setting DistMap type deba@1912: /// deba@1912: /// \ref named-templ-param "Named parameter" deba@1912: ///function for setting DistMap type deba@1912: /// deba@1912: template deba@1912: DagShortestPathWizard > distMap(const T &t) { deba@1912: Base::_dist=(void *)&t; deba@1912: return DagShortestPathWizard >(*this); deba@1912: } deba@1912: deba@1912: template deba@1912: struct DefOperationTraitsBase : public Base { deba@1912: typedef T OperationTraits; deba@1912: DefOperationTraitsBase(const _Traits &b) : _Traits(b) {} deba@1912: }; deba@1912: deba@1912: ///\brief \ref named-templ-param "Named parameter" deba@1912: ///function for setting OperationTraits type deba@1912: /// deba@1912: /// \ref named-templ-param "Named parameter" deba@1912: ///function for setting OperationTraits type deba@1912: /// deba@1912: template deba@1912: DagShortestPathWizard > distMap() { deba@1912: return DagShortestPathWizard >(*this); deba@1912: } deba@1912: deba@1912: /// \brief Sets the source node, from which the DagShortestPath algorithm runs. deba@1912: /// deba@1912: /// Sets the source node, from which the DagShortestPath algorithm runs. alpar@1946: /// \param source is the source node. deba@1912: DagShortestPathWizard<_Traits>& source(Node source) { deba@1912: Base::_source = source; deba@1912: return *this; deba@1912: } deba@1912: deba@1912: }; deba@1912: deba@1912: /// \brief Function type interface for DagShortestPath algorithm. deba@1912: /// deba@1912: /// \ingroup flowalgs deba@1912: /// Function type interface for DagShortestPath algorithm. deba@1912: /// deba@1912: /// This function also has several \ref named-templ-func-param deba@1912: /// "named parameters", they are declared as the members of class deba@1912: /// \ref DagShortestPathWizard. deba@1912: /// The following deba@1912: /// example shows how to use these parameters. alpar@1946: ///\code deba@1912: /// dagShortestPath(g,length,source).predMap(preds).run(); alpar@1946: ///\endcode deba@1912: /// \warning Don't forget to put the \ref DagShortestPathWizard::run() "run()" deba@1912: /// to the end of the parameter list. deba@1912: /// \sa DagShortestPathWizard deba@1912: /// \sa DagShortestPath deba@1912: template deba@1912: DagShortestPathWizard > deba@1912: dagShortestPath(const _Graph& graph, deba@1912: const _LengthMap& length, deba@1912: typename _Graph::Node source = INVALID) { deba@1912: return DagShortestPathWizard > deba@1912: (graph, length, source); deba@1912: } deba@1912: deba@1912: } //END OF NAMESPACE LEMON deba@1912: deba@1912: #endif deba@1912: