2 * lemon/dag_shortest_path.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_DAG_SHORTEST_PATH_H
18 #define LEMON_DAG_SHORTEST_PATH_H
22 /// \brief DagShortestPath algorithm.
25 #include <lemon/list_graph.h>
26 #include <lemon/invalid.h>
27 #include <lemon/error.h>
28 #include <lemon/maps.h>
29 #include <lemon/topology.h>
35 /// \brief Default OperationTraits for the DagShortestPath algorithm class.
37 /// It defines all computational operations and constants which are
38 /// used in the dag shortest path algorithm. The default implementation
39 /// is based on the numeric_limits class. If the numeric type does not
40 /// have infinity value then the maximum value is used as extremal
44 bool has_infinity = std::numeric_limits<Value>::has_infinity>
45 struct DagShortestPathDefaultOperationTraits {
46 /// \brief Gives back the zero value of the type.
48 return static_cast<Value>(0);
50 /// \brief Gives back the positive infinity value of the type.
51 static Value infinity() {
52 return std::numeric_limits<Value>::infinity();
54 /// \brief Gives back the sum of the given two elements.
55 static Value plus(const Value& left, const Value& right) {
58 /// \brief Gives back true only if the first value less than the second.
59 static bool less(const Value& left, const Value& right) {
64 template <typename Value>
65 struct DagShortestPathDefaultOperationTraits<Value, false> {
67 return static_cast<Value>(0);
69 static Value infinity() {
70 return std::numeric_limits<Value>::max();
72 static Value plus(const Value& left, const Value& right) {
73 if (left == infinity() || right == infinity()) return infinity();
76 static bool less(const Value& left, const Value& right) {
81 /// \brief Default traits class of DagShortestPath class.
83 /// Default traits class of DagShortestPath class.
84 /// \param _Graph Graph type.
85 /// \param _LegthMap Type of length map.
86 template<class _Graph, class _LengthMap>
87 struct DagShortestPathDefaultTraits {
88 /// The graph type the algorithm runs on.
91 /// \brief The type of the map that stores the edge lengths.
93 /// The type of the map that stores the edge lengths.
94 /// It must meet the \ref concept::ReadMap "ReadMap" concept.
95 typedef _LengthMap LengthMap;
97 // The type of the length of the edges.
98 typedef typename _LengthMap::Value Value;
100 /// \brief Operation traits for dag shortest path algorithm.
102 /// It defines the infinity type on the given Value type
103 /// and the used operation.
104 /// \see DagShortestPathDefaultOperationTraits
105 typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
107 /// \brief The type of the map that stores the last edges of the
110 /// The type of the map that stores the last
111 /// edges of the shortest paths.
112 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
114 typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
116 /// \brief Instantiates a PredMap.
118 /// This function instantiates a \ref PredMap.
119 /// \param G is the graph, to which we would like to define the PredMap.
120 /// \todo The graph alone may be insufficient for the initialization
121 static PredMap *createPredMap(const _Graph& graph) {
122 return new PredMap(graph);
125 /// \brief The type of the map that stores the dists of the nodes.
127 /// The type of the map that stores the dists of the nodes.
128 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
130 typedef typename Graph::template NodeMap<typename _LengthMap::Value>
133 /// \brief Instantiates a DistMap.
135 /// This function instantiates a \ref DistMap.
136 /// \param G is the graph, to which we would like to define the
138 static DistMap *createDistMap(const _Graph& graph) {
139 return new DistMap(graph);
144 /// \brief Inverse OperationTraits for the DagShortestPath algorithm class.
146 /// It defines all computational operations and constants which are
147 /// used in the dag shortest path algorithm. It is the inverse of
148 /// \ref DagShortestPathDefaultOperationTraits, so it can be used to
149 /// calculate the longest path. The default implementation
150 /// is based on the numeric_limits class. If the numeric type does not
151 /// have infinity value then the minimum value is used as extremal
155 bool has_infinity = std::numeric_limits<Value>::has_infinity>
156 struct DagLongestPathOperationTraits {
157 /// \brief Gives back the zero value of the type.
158 static Value zero() {
159 return static_cast<Value>(0);
161 /// \brief Gives back the negative infinity value of the type.
162 static Value infinity() {
163 return -(std::numeric_limits<Value>::infinity());
165 /// \brief Gives back the sum of the given two elements.
166 static Value plus(const Value& left, const Value& right) {
169 /// \brief Gives back true only if the first value less than the second.
170 static bool less(const Value& left, const Value& right) {
175 template <typename Value>
176 struct DagLongestPathOperationTraits<Value, false> {
177 static Value zero() {
178 return static_cast<Value>(0);
180 static Value infinity() {
181 return std::numeric_limits<Value>::min();
183 static Value plus(const Value& left, const Value& right) {
184 if (left == infinity() || right == infinity()) return infinity();
187 static bool less(const Value& left, const Value& right) {
192 /// \brief Inverse traits class of DagShortestPath class.
194 /// Inverse traits class of DagShortestPath class.
195 /// \param _Graph Graph type.
196 /// \param _LegthMap Type of length map.
197 template<class _Graph, class _LengthMap>
198 struct DagLongestPathTraits {
199 /// The graph type the algorithm runs on.
200 typedef _Graph Graph;
202 /// \brief The type of the map that stores the edge lengths.
204 /// The type of the map that stores the edge lengths.
205 /// It must meet the \ref concept::ReadMap "ReadMap" concept.
206 typedef _LengthMap LengthMap;
208 // The type of the length of the edges.
209 typedef typename _LengthMap::Value Value;
211 /// \brief Inverse operation traits for dag shortest path algorithm.
213 /// It defines the infinity type on the given Value type
214 /// and the used operation.
215 /// \see DagLongestPathOperationTraits
216 typedef DagLongestPathOperationTraits<Value> OperationTraits;
218 /// \brief The type of the map that stores the last edges of the
221 /// The type of the map that stores the last
222 /// edges of the longest paths.
223 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
225 typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
227 /// \brief Instantiates a PredMap.
229 /// This function instantiates a \ref PredMap.
230 /// \param G is the graph, to which we would like to define the PredMap.
231 /// \todo The graph alone may be insufficient for the initialization
232 static PredMap *createPredMap(const _Graph& graph) {
233 return new PredMap(graph);
236 /// \brief The type of the map that stores the dists of the nodes.
238 /// The type of the map that stores the dists of the nodes.
239 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
241 typedef typename Graph::template NodeMap<typename _LengthMap::Value>
244 /// \brief Instantiates a DistMap.
246 /// This function instantiates a \ref DistMap.
247 /// \param G is the graph, to which we would like to define the
249 static DistMap *createDistMap(const _Graph& graph) {
250 return new DistMap(graph);
256 /// \brief %DagShortestPath algorithm class.
258 /// \ingroup flowalgs
259 /// This class provides an efficient implementation of a Dag sortest path
260 /// searching algorithm. The edge lengths are passed to the algorithm
261 /// using a \ref concept::ReadMap "ReadMap", so it is easy to change it
262 /// to any kind of length.
264 /// The complexity of the algorithm is O(n + e).
266 /// The type of the length is determined by the
267 /// \ref concept::ReadMap::Value "Value" of the length map.
269 /// \param _Graph The graph type the algorithm runs on. The default value
270 /// is \ref ListGraph. The value of _Graph is not used directly by
271 /// DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
272 /// \param _LengthMap This read-only EdgeMap determines the lengths of the
273 /// edges. The default map type is \ref concept::StaticGraph::EdgeMap
274 /// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly
275 /// by DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
276 /// \param _Traits Traits class to set various data types used by the
277 /// algorithm. The default traits class is \ref DagShortestPathDefaultTraits
278 /// "DagShortestPathDefaultTraits<_Graph,_LengthMap>". See \ref
279 /// DagShortestPathDefaultTraits for the documentation of a DagShortestPath traits
282 /// \author Balazs Attila Mihaly (based on Balazs Dezso's work)
285 template <typename _Graph, typename _LengthMap, typename _Traits>
287 template <typename _Graph=ListGraph,
288 typename _LengthMap=typename _Graph::template EdgeMap<int>,
289 typename _Traits=DagShortestPathDefaultTraits<_Graph,_LengthMap> >
291 class DagShortestPath {
294 /// \brief \ref Exception for uninitialized parameters.
296 /// This error represents problems in the initialization
297 /// of the parameters of the algorithms.
299 class UninitializedParameter : public lemon::UninitializedParameter {
301 virtual const char* exceptionName() const {
302 return "lemon::DagShortestPath::UninitializedParameter";
306 typedef _Traits Traits;
307 ///The type of the underlying graph.
308 typedef typename _Traits::Graph Graph;
310 typedef typename Graph::Node Node;
311 typedef typename Graph::NodeIt NodeIt;
312 typedef typename Graph::Edge Edge;
313 typedef typename Graph::EdgeIt EdgeIt;
314 typedef typename Graph::OutEdgeIt OutEdgeIt;
316 /// \brief The type of the length of the edges.
317 typedef typename _Traits::LengthMap::Value Value;
318 /// \brief The type of the map that stores the edge lengths.
319 typedef typename _Traits::LengthMap LengthMap;
320 /// \brief The type of the map that stores the last
321 /// edges of the shortest paths.
322 typedef typename _Traits::PredMap PredMap;
323 /// \brief The type of the map that stores the dists of the nodes.
324 typedef typename _Traits::DistMap DistMap;
325 /// \brief The operation traits.
326 typedef typename _Traits::OperationTraits OperationTraits;
327 /// \brief The Node weight map.
328 typedef typename Graph::NodeMap<Value> WeightMap;
330 /// Pointer to the underlying graph
332 /// Pointer to the length map
333 const LengthMap *length;
334 ///Pointer to the map of predecessors edges
336 ///Indicates if \ref _pred is locally allocated (\c true) or not
338 ///Pointer to the map of distances
340 ///Indicates if \ref _dist is locally allocated (\c true) or not
342 ///Process step counter
343 unsigned int _process_step;
345 std::vector<Node> _node_order;
347 /// Creates the maps if necessary.
351 _pred = Traits::createPredMap(*graph);
355 _dist = Traits::createDistMap(*graph);
361 typedef DagShortestPath Create;
363 /// \name Named template parameters
368 struct DefPredMapTraits : public Traits {
370 static PredMap *createPredMap(const Graph&) {
371 throw UninitializedParameter();
375 /// \brief \ref named-templ-param "Named parameter" for setting PredMap
377 /// \ref named-templ-param "Named parameter" for setting PredMap type
381 typedef DagShortestPath< Graph, LengthMap, DefPredMapTraits<T> > Create;
385 struct DefDistMapTraits : public Traits {
387 static DistMap *createDistMap(const Graph& graph) {
388 throw UninitializedParameter();
392 /// \brief \ref named-templ-param "Named parameter" for setting DistMap
395 /// \ref named-templ-param "Named parameter" for setting DistMap type
399 : public DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > {
400 typedef DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > Create;
404 struct DefOperationTraitsTraits : public Traits {
405 typedef T OperationTraits;
408 /// \brief \ref named-templ-param "Named parameter" for setting
409 /// OperationTraits type
411 /// \ref named-templ-param "Named parameter" for setting OperationTraits
414 struct DefOperationTraits
415 : public DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> > {
416 typedef DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> >
428 /// \brief Constructor.
430 /// \param _graph the graph the algorithm will run on.
431 /// \param _length the length map used by the algorithm.
432 DagShortestPath(const Graph& _graph, const LengthMap& _length) :
433 graph(&_graph), length(&_length),
434 _pred(0), local_pred(false),
435 _dist(0), local_dist(false){}
437 /// \brief Constructor with node weight map.
439 /// \param _graph the graph the algorithm will run on.
440 /// \param _length the length map used by the algorithm.
441 /// The NodeMap _length will be used as the weight map.
442 /// Each edge will have the weight of its target node.
443 DagShortestPath(const Graph& _graph, const WeightMap& _length) :
445 _pred(0), local_pred(false),
446 _dist(0), local_dist(false){
447 length=new LengthMap(_graph);
448 _dist=new DistMap(_graph);
449 for(EdgeIt eit(_graph);eit!=INVALID;++eit)
450 (const_cast<LengthMap*>(length))->set(eit,_length[_graph.target(eit)]);
455 if(local_pred) delete _pred;
456 if(local_dist) delete _dist;
459 /// \brief Sets the length map.
461 /// Sets the length map.
462 /// \return \c (*this)
463 DagShortestPath &lengthMap(const LengthMap &m) {
468 /// \brief Sets the map storing the predecessor edges.
470 /// Sets the map storing the predecessor edges.
471 /// If you don't use this function before calling \ref run(),
472 /// it will allocate one. The destuctor deallocates this
473 /// automatically allocated map, of course.
474 /// \return \c (*this)
475 DagShortestPath &predMap(PredMap &m) {
484 /// \brief Sets the map storing the distances calculated by the algorithm.
486 /// Sets the map storing the distances calculated by the algorithm.
487 /// If you don't use this function before calling \ref run(),
488 /// it will allocate one. The destuctor deallocates this
489 /// automatically allocated map, of course.
490 /// \return \c (*this)
491 DagShortestPath &distMap(DistMap &m) {
500 /// \name Execution control
501 /// The simplest way to execute the algorithm is to use
502 /// one of the member functions called \c run(...)
504 /// If you need more control on the execution,
505 /// first you must call \ref init(...), then you can add several source
506 /// nodes with \ref addSource().
507 /// Finally \ref start() will perform the actual path computation.
508 /// Some functions have an alternative form (\ref checkedInit(...),
509 /// \ref checkedRun(...)) which also verifies if the graph given in the
510 /// constructor is a dag.
514 /// \brief Initializes the internal data structures.
516 /// Initializes the internal data structures.
517 void init(const Value value = OperationTraits::infinity()) {
518 typedef typename Graph::template NodeMap<int> NodeOrderMap;
520 NodeOrderMap node_order(*graph);
521 topologicalSort(*graph,node_order);
522 _node_order.resize(countNodes(*graph),INVALID);
524 for (NodeIt it(*graph); it != INVALID; ++it) {
525 _node_order[node_order[it]]=it;
526 _pred->set(it, INVALID);
527 _dist->set(it, value);
531 /// \brief Initializes the internal data structures
532 /// with a given topological sort (NodeMap).
534 /// Initializes the internal data structures
535 /// with a given topological sort (NodeMap).
536 void init(const typename Graph::template NodeMap<int>& node_order,
537 const Value value = OperationTraits::infinity()) {
539 _node_order.resize(countNodes(*graph),INVALID);
541 for (NodeIt it(*graph); it != INVALID; ++it) {
542 _node_order[node_order[it]]=it;
543 _pred->set(it, INVALID);
544 _dist->set(it, value);
548 /// \brief Initializes the internal data structures
549 /// with a given topological sort (std::vector).
551 /// Initializes the internal data structures
552 /// with a given topological sort (std::vector).
553 void init(const std::vector<Node>& node_order,
554 const Value value = OperationTraits::infinity()) {
556 _node_order=node_order;
558 for (NodeIt it(*graph); it != INVALID; ++it) {
559 _pred->set(it, INVALID);
560 _dist->set(it, value);
564 /// \brief Initializes the internal data structures. It also checks if the graph is dag.
566 /// Initializes the internal data structures. It also checks if the graph is dag.
567 /// \return true if the graph (given in the constructor) is dag, false otherwise.
568 bool checkedInit(const Value value = OperationTraits::infinity()) {
569 typedef typename Graph::template NodeMap<int> NodeOrderMap;
570 NodeOrderMap node_order(*graph);
571 if(!checkedTopologicalSort(*graph,node_order))return false;
572 init(node_order,value);
576 /// \brief Initializes the internal data structures with a given
577 /// topological sort (NodeMap). It also checks if the graph is dag.
579 /// Initializes the internal data structures with a given
580 /// topological sort (NodeMap). It also checks if the graph is dag.
581 /// \return true if the graph (given in the constructor) is dag, false otherwise.
582 bool checkedInit(const typename Graph::template NodeMap<int>& node_order,
583 const Value value = OperationTraits::infinity()) {
584 for(NodeIt it(*graph);it!=INVALID;++it){
585 for(OutEdgeIt oeit(*graph,it);oeit!=INVALID;++oeit){
586 if(node_order[graph->target(oeit)]<node_order[it])return false;
589 init(node_order,value);
593 /// \brief Initializes the internal data structures with a given
594 /// topological sort (std::vector). It also checks if the graph is dag.
596 /// Initializes the internal data structures with a given
597 /// topological sort (std::vector). It also checks if the graph is dag.
598 /// \return true if the graph (given in the constructor) is dag, false otherwise.
599 bool checkedInit(const std::vector<Node>& node_order,
600 const Value value = OperationTraits::infinity()) {
601 typedef typename Graph::template NodeMap<bool> BoolNodeMap;
602 BoolNodeMap _processed(*graph,false);
603 for(unsigned int i=0;i<_node_order.size();++i){
604 _processed[node_order[i]]=true;
605 for(OutEdgeIt oeit(*graph,node_order[i]);oeit!=INVALID;++oeit){
606 if(_processed[graph->target(oeit)])return false;
609 init(node_order,value);
613 /// \brief Adds a new source node.
615 /// The optional second parameter is the initial distance of the node.
616 /// It just sets the distance of the node to the given value.
617 void addSource(Node source, Value dst = OperationTraits::zero()) {
618 if((*_dist)[source] != dst){
619 _dist->set(source, dst);
623 /// \brief Executes one step from the dag shortest path algorithm.
625 /// If the algoritm calculated the distances in the previous step
626 /// strictly for all at most k length paths then it will calculate the
627 /// distances strictly for all at most k + 1 length paths. With k
628 /// iteration this function calculates the at most k length paths.
629 ///\pre the queue is not empty
630 ///\return the currently processed node
631 Node processNextNode() {
632 if(reached(_node_order[_process_step])){
633 for (OutEdgeIt it(*graph, _node_order[_process_step]); it != INVALID; ++it) {
634 Node target = graph->target(it);
636 OperationTraits::plus((*_dist)[_node_order[_process_step]], (*length)[it]);
637 if (OperationTraits::less(relaxed, (*_dist)[target])) {
638 _pred->set(target, it);
639 _dist->set(target, relaxed);
644 return _node_order[_process_step-1];
647 ///\brief Returns \c false if there are nodes
648 ///to be processed in the queue
650 ///Returns \c false if there are nodes
651 ///to be processed in the queue
652 bool emptyQueue() { return _node_order.size()-1==_process_step; }
654 ///\brief Returns the number of the nodes to be processed.
656 ///Returns the number of the nodes to be processed in the queue.
657 int queueSize() { return _node_order.size()-1-_process_step; }
659 /// \brief Executes the algorithm.
661 /// \pre init() must be called and at least one node should be added
662 /// with addSource() before using this function.
664 /// This method runs the %DagShortestPath algorithm from the root node(s)
665 /// in order to compute the shortest path to each node. The algorithm
667 /// - The shortest path tree.
668 /// - The distance of each node from the root(s).
670 while(!emptyQueue()) {
675 /// \brief Runs %DagShortestPath algorithm from node \c s.
677 /// This method runs the %DagShortestPath algorithm from a root node \c s
678 /// in order to compute the shortest path to each node. The algorithm
680 /// - The shortest path tree.
681 /// - The distance of each node from the root.
683 /// \note d.run(s) is just a shortcut of the following code.
695 /// \brief Runs %DagShortestPath algorithm from node \c s.
696 /// It also checks if the graph is a dag.
698 /// This method runs the %DagShortestPath algorithm from a root node \c s
699 /// in order to compute the shortest path to each node. The algorithm
701 /// - The shortest path tree.
702 /// - The distance of each node from the root.
703 /// The algorithm checks if the graph given int the constructor is a dag.
704 bool checkedRun(Node s) {
705 if(!checkedInit())return false;
713 /// \name Query Functions
714 /// The result of the %DagShortestPath algorithm can be obtained using these
716 /// Before the use of these functions,
717 /// either run() or start() must be called.
721 /// \brief Copies the shortest path to \c t into \c p
723 /// This function copies the shortest path to \c t into \c p.
724 /// If it \c t is a source itself or unreachable, then it does not
727 /// \return Returns \c true if a path to \c t was actually copied to \c p,
728 /// \c false otherwise.
730 template <typename Path>
731 bool getPath(Path &p, Node t) {
734 typename Path::Builder b(p);
735 for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
736 b.pushFront(predEdge(t));
743 /// \brief The distance of a node from the root.
745 /// Returns the distance of a node from the root.
746 /// \pre \ref run() must be called before using this function.
747 /// \warning If node \c v in unreachable from the root the return value
748 /// of this funcion is undefined.
749 Value dist(Node v) const { return (*_dist)[v]; }
751 /// \brief Returns the 'previous edge' of the shortest path tree.
753 /// For a node \c v it returns the 'previous edge' of the shortest path
754 /// tree, i.e. it returns the last edge of a shortest path from the root
755 /// to \c v. It is \ref INVALID if \c v is unreachable from the root or
756 /// if \c v=s. The shortest path tree used here is equal to the shortest
757 /// path tree used in \ref predNode().
758 /// \pre \ref run() must be called before using
760 Edge predEdge(Node v) const { return (*_pred)[v]; }
762 /// \brief Returns the 'previous node' of the shortest path tree.
764 /// For a node \c v it returns the 'previous node' of the shortest path
765 /// tree, i.e. it returns the last but one node from a shortest path from
766 /// the root to \c /v. It is INVALID if \c v is unreachable from the root
767 /// or if \c v=s. The shortest path tree used here is equal to the
768 /// shortest path tree used in \ref predEdge(). \pre \ref run() must be
769 /// called before using this function.
770 Node predNode(Node v) const {
771 return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]);
774 /// \brief Returns a reference to the NodeMap of distances.
776 /// Returns a reference to the NodeMap of distances. \pre \ref run() must
777 /// be called before using this function.
778 const DistMap &distMap() const { return *_dist;}
780 /// \brief Returns a reference to the shortest path tree map.
782 /// Returns a reference to the NodeMap of the edges of the
783 /// shortest path tree.
784 /// \pre \ref run() must be called before using this function.
785 const PredMap &predMap() const { return *_pred; }
787 /// \brief Checks if a node is reachable from the root.
789 /// Returns \c true if \c v is reachable from the root.
790 /// \pre \ref run() must be called before using this function.
792 bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
797 /// \brief Default traits class of DagShortestPath function.
799 /// Default traits class of DagShortestPath function.
800 /// \param _Graph Graph type.
801 /// \param _LengthMap Type of length map.
802 template <typename _Graph, typename _LengthMap>
803 struct DagShortestPathWizardDefaultTraits {
804 /// \brief The graph type the algorithm runs on.
805 typedef _Graph Graph;
807 /// \brief The type of the map that stores the edge lengths.
809 /// The type of the map that stores the edge lengths.
810 /// It must meet the \ref concept::ReadMap "ReadMap" concept.
811 typedef _LengthMap LengthMap;
813 /// \brief The value type of the length map.
814 typedef typename _LengthMap::Value Value;
816 /// \brief Operation traits for dag shortest path algorithm.
818 /// It defines the infinity type on the given Value type
819 /// and the used operation.
820 /// \see DagShortestPathDefaultOperationTraits
821 typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
823 /// \brief The type of the map that stores the last
824 /// edges of the shortest paths.
826 /// The type of the map that stores the last
827 /// edges of the shortest paths.
828 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
829 typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
831 /// \brief Instantiates a PredMap.
833 /// This function instantiates a \ref PredMap.
834 static PredMap *createPredMap(const _Graph &) {
835 return new PredMap();
837 /// \brief The type of the map that stores the dists of the nodes.
839 /// The type of the map that stores the dists of the nodes.
840 /// It must meet the \ref concept::WriteMap "WriteMap" concept.
841 typedef NullMap<typename Graph::Node, Value> DistMap;
842 /// \brief Instantiates a DistMap.
844 /// This function instantiates a \ref DistMap.
845 static DistMap *createDistMap(const _Graph &) {
846 return new DistMap();
850 /// \brief Default traits used by \ref DagShortestPathWizard
852 /// To make it easier to use DagShortestPath algorithm
853 /// we have created a wizard class.
854 /// This \ref DagShortestPathWizard class needs default traits,
855 /// as well as the \ref DagShortestPath class.
856 /// The \ref DagShortestPathWizardBase is a class to be the default traits of the
857 /// \ref DagShortestPathWizard class.
858 /// \todo More named parameters are required...
859 template<class _Graph,class _LengthMap>
860 class DagShortestPathWizardBase
861 : public DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> {
863 typedef DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> Base;
865 /// Type of the nodes in the graph.
866 typedef typename Base::Graph::Node Node;
868 /// Pointer to the underlying graph.
870 /// Pointer to the length map
872 ///Pointer to the map of predecessors edges.
874 ///Pointer to the map of distances.
876 ///Pointer to the source node.
882 /// This constructor does not require parameters, therefore it initiates
883 /// all of the attributes to default values (0, INVALID).
884 DagShortestPathWizardBase() : _graph(0), _length(0), _pred(0),
885 _dist(0), _source(INVALID) {}
889 /// This constructor requires some parameters,
890 /// listed in the parameters list.
891 /// Others are initiated to 0.
892 /// \param graph is the initial value of \ref _graph
893 /// \param length is the initial value of \ref _length
894 /// \param source is the initial value of \ref _source
895 DagShortestPathWizardBase(const _Graph& graph,
896 const _LengthMap& length,
897 Node source = INVALID) :
898 _graph((void *)&graph), _length((void *)&length), _pred(0),
899 _dist(0), _source(source) {}
903 /// A class to make the usage of DagShortestPath algorithm easier
905 /// This class is created to make it easier to use DagShortestPath algorithm.
906 /// It uses the functions and features of the plain \ref DagShortestPath,
907 /// but it is much simpler to use it.
909 /// Simplicity means that the way to change the types defined
910 /// in the traits class is based on functions that returns the new class
911 /// and not on templatable built-in classes.
912 /// When using the plain \ref DagShortestPath
913 /// the new class with the modified type comes from
914 /// the original class by using the ::
915 /// operator. In the case of \ref DagShortestPathWizard only
916 /// a function have to be called and it will
917 /// return the needed class.
919 /// It does not have own \ref run method. When its \ref run method is called
920 /// it initiates a plain \ref DagShortestPath class, and calls the \ref
921 /// DagShortestPath::run() method of it.
922 template<class _Traits>
923 class DagShortestPathWizard : public _Traits {
924 typedef _Traits Base;
926 ///The type of the underlying graph.
927 typedef typename _Traits::Graph Graph;
929 typedef typename Graph::Node Node;
930 typedef typename Graph::NodeIt NodeIt;
931 typedef typename Graph::Edge Edge;
932 typedef typename Graph::OutEdgeIt EdgeIt;
934 ///The type of the map that stores the edge lengths.
935 typedef typename _Traits::LengthMap LengthMap;
937 ///The type of the length of the edges.
938 typedef typename LengthMap::Value Value;
940 ///\brief The type of the map that stores the last
941 ///edges of the shortest paths.
942 typedef typename _Traits::PredMap PredMap;
944 ///The type of the map that stores the dists of the nodes.
945 typedef typename _Traits::DistMap DistMap;
949 DagShortestPathWizard() : _Traits() {}
951 /// \brief Constructor that requires parameters.
953 /// Constructor that requires parameters.
954 /// These parameters will be the default values for the traits class.
955 DagShortestPathWizard(const Graph& graph, const LengthMap& length,
956 Node source = INVALID)
957 : _Traits(graph, length, source) {}
959 /// \brief Copy constructor
960 DagShortestPathWizard(const _Traits &b) : _Traits(b) {}
962 ~DagShortestPathWizard() {}
964 /// \brief Runs DagShortestPath algorithm from a given node.
966 /// Runs DagShortestPath algorithm from a given node.
967 /// The node can be given by the \ref source function.
969 if(Base::_source == INVALID) throw UninitializedParameter();
970 DagShortestPath<Graph,LengthMap,_Traits>
971 bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
972 if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
973 if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
974 bf.run(Base::_source);
977 /// \brief Runs DagShortestPath algorithm from the given node.
979 /// Runs DagShortestPath algorithm from the given node.
980 /// \param s is the given source.
981 void run(Node source) {
982 Base::_source = source;
987 struct DefPredMapBase : public Base {
989 static PredMap *createPredMap(const Graph &) { return 0; };
990 DefPredMapBase(const _Traits &b) : _Traits(b) {}
993 ///\brief \ref named-templ-param "Named parameter"
994 ///function for setting PredMap type
996 /// \ref named-templ-param "Named parameter"
997 ///function for setting PredMap type
1000 DagShortestPathWizard<DefPredMapBase<T> > predMap(const T &t)
1002 Base::_pred=(void *)&t;
1003 return DagShortestPathWizard<DefPredMapBase<T> >(*this);
1007 struct DefDistMapBase : public Base {
1009 static DistMap *createDistMap(const Graph &) { return 0; };
1010 DefDistMapBase(const _Traits &b) : _Traits(b) {}
1013 ///\brief \ref named-templ-param "Named parameter"
1014 ///function for setting DistMap type
1016 /// \ref named-templ-param "Named parameter"
1017 ///function for setting DistMap type
1020 DagShortestPathWizard<DefDistMapBase<T> > distMap(const T &t) {
1021 Base::_dist=(void *)&t;
1022 return DagShortestPathWizard<DefDistMapBase<T> >(*this);
1026 struct DefOperationTraitsBase : public Base {
1027 typedef T OperationTraits;
1028 DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
1031 ///\brief \ref named-templ-param "Named parameter"
1032 ///function for setting OperationTraits type
1034 /// \ref named-templ-param "Named parameter"
1035 ///function for setting OperationTraits type
1038 DagShortestPathWizard<DefOperationTraitsBase<T> > distMap() {
1039 return DagShortestPathWizard<DefDistMapBase<T> >(*this);
1042 /// \brief Sets the source node, from which the DagShortestPath algorithm runs.
1044 /// Sets the source node, from which the DagShortestPath algorithm runs.
1045 /// \param s is the source node.
1046 DagShortestPathWizard<_Traits>& source(Node source) {
1047 Base::_source = source;
1053 /// \brief Function type interface for DagShortestPath algorithm.
1055 /// \ingroup flowalgs
1056 /// Function type interface for DagShortestPath algorithm.
1058 /// This function also has several \ref named-templ-func-param
1059 /// "named parameters", they are declared as the members of class
1060 /// \ref DagShortestPathWizard.
1062 /// example shows how to use these parameters.
1064 /// dagShortestPath(g,length,source).predMap(preds).run();
1066 /// \warning Don't forget to put the \ref DagShortestPathWizard::run() "run()"
1067 /// to the end of the parameter list.
1068 /// \sa DagShortestPathWizard
1069 /// \sa DagShortestPath
1070 template<class _Graph, class _LengthMap>
1071 DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
1072 dagShortestPath(const _Graph& graph,
1073 const _LengthMap& length,
1074 typename _Graph::Node source = INVALID) {
1075 return DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
1076 (graph, length, source);
1079 } //END OF NAMESPACE LEMON