Added the function isFinite(), and replaced the calls to finite() with it.
This was necessary because finite() is not a standard function. Neither can
we use its standard counterpart isfinite(), because it was introduced only
in C99, and therefore it is not supplied by all C++ implementations.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_DAG_SHORTEST_PATH_H
20 #define LEMON_DAG_SHORTEST_PATH_H
24 /// \brief DagShortestPath algorithm.
27 #include <lemon/list_graph.h>
28 #include <lemon/bits/invalid.h>
29 #include <lemon/error.h>
30 #include <lemon/maps.h>
31 #include <lemon/topology.h>
37 /// \brief Default OperationTraits for the DagShortestPath algorithm class.
39 /// It defines all computational operations and constants which are
40 /// used in the dag shortest path algorithm. The default implementation
41 /// is based on the numeric_limits class. If the numeric type does not
42 /// have infinity value then the maximum value is used as extremal
46 bool has_infinity = std::numeric_limits<Value>::has_infinity>
47 struct DagShortestPathDefaultOperationTraits {
48 /// \brief Gives back the zero value of the type.
50 return static_cast<Value>(0);
52 /// \brief Gives back the positive infinity value of the type.
53 static Value infinity() {
54 return std::numeric_limits<Value>::infinity();
56 /// \brief Gives back the sum of the given two elements.
57 static Value plus(const Value& left, const Value& right) {
60 /// \brief Gives back true only if the first value less than the second.
61 static bool less(const Value& left, const Value& right) {
66 template <typename Value>
67 struct DagShortestPathDefaultOperationTraits<Value, false> {
69 return static_cast<Value>(0);
71 static Value infinity() {
72 return std::numeric_limits<Value>::max();
74 static Value plus(const Value& left, const Value& right) {
75 if (left == infinity() || right == infinity()) return infinity();
78 static bool less(const Value& left, const Value& right) {
83 /// \brief Default traits class of DagShortestPath class.
85 /// Default traits class of DagShortestPath class.
86 /// \param _Graph Graph type.
87 /// \param _LegthMap Type of length map.
88 template<class _Graph, class _LengthMap>
89 struct DagShortestPathDefaultTraits {
90 /// The graph type the algorithm runs on.
93 /// \brief The type of the map that stores the edge lengths.
95 /// The type of the map that stores the edge lengths.
96 /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
97 typedef _LengthMap LengthMap;
99 // The type of the length of the edges.
100 typedef typename _LengthMap::Value Value;
102 /// \brief Operation traits for dag shortest path algorithm.
104 /// It defines the infinity type on the given Value type
105 /// and the used operation.
106 /// \see DagShortestPathDefaultOperationTraits
107 typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
109 /// \brief The type of the map that stores the last edges of the
112 /// The type of the map that stores the last
113 /// edges of the shortest paths.
114 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
116 typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
118 /// \brief Instantiates a PredMap.
120 /// This function instantiates a \ref PredMap.
121 /// \param graph is the graph, to which we would
122 /// like to define the PredMap.
123 /// \todo The graph alone may be insufficient for the initialization
124 static PredMap *createPredMap(const _Graph& graph) {
125 return new PredMap(graph);
128 /// \brief The type of the map that stores the dists of the nodes.
130 /// The type of the map that stores the dists of the nodes.
131 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
133 typedef typename Graph::template NodeMap<typename _LengthMap::Value>
136 /// \brief Instantiates a DistMap.
138 /// This function instantiates a \ref DistMap.
139 /// \param graph is the graph, to which we would like to define the
141 static DistMap *createDistMap(const _Graph& graph) {
142 return new DistMap(graph);
147 /// \brief Inverse OperationTraits for the DagShortestPath algorithm class.
149 /// It defines all computational operations and constants which are
150 /// used in the dag shortest path algorithm. It is the inverse of
151 /// \ref DagShortestPathDefaultOperationTraits, so it can be used to
152 /// calculate the longest path. The default implementation
153 /// is based on the numeric_limits class. If the numeric type does not
154 /// have infinity value then the minimum value is used as extremal
158 bool has_infinity = std::numeric_limits<Value>::has_infinity>
159 struct DagLongestPathOperationTraits {
160 /// \brief Gives back the zero value of the type.
161 static Value zero() {
162 return static_cast<Value>(0);
164 /// \brief Gives back the negative infinity value of the type.
165 static Value infinity() {
166 return -(std::numeric_limits<Value>::infinity());
168 /// \brief Gives back the sum of the given two elements.
169 static Value plus(const Value& left, const Value& right) {
172 /// \brief Gives back true only if the first value less than the second.
173 static bool less(const Value& left, const Value& right) {
178 template <typename Value>
179 struct DagLongestPathOperationTraits<Value, false> {
180 static Value zero() {
181 return static_cast<Value>(0);
183 static Value infinity() {
184 return std::numeric_limits<Value>::min();
186 static Value plus(const Value& left, const Value& right) {
187 if (left == infinity() || right == infinity()) return infinity();
190 static bool less(const Value& left, const Value& right) {
195 /// \brief Inverse traits class of DagShortestPath class.
197 /// Inverse traits class of DagShortestPath class.
198 /// \param _Graph Graph type.
199 /// \param _LegthMap Type of length map.
200 template<class _Graph, class _LengthMap>
201 struct DagLongestPathTraits {
202 /// The graph type the algorithm runs on.
203 typedef _Graph Graph;
205 /// \brief The type of the map that stores the edge lengths.
207 /// The type of the map that stores the edge lengths.
208 /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
209 typedef _LengthMap LengthMap;
211 // The type of the length of the edges.
212 typedef typename _LengthMap::Value Value;
214 /// \brief Inverse operation traits for dag shortest path algorithm.
216 /// It defines the infinity type on the given Value type
217 /// and the used operation.
218 /// \see DagLongestPathOperationTraits
219 typedef DagLongestPathOperationTraits<Value> OperationTraits;
221 /// \brief The type of the map that stores the last edges of the
224 /// The type of the map that stores the last
225 /// edges of the longest paths.
226 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
228 typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
230 /// \brief Instantiates a PredMap.
232 /// This function instantiates a \ref PredMap.
233 /// \param graph is the graph,
234 /// to which we would like to define the PredMap.
235 /// \todo The graph alone may be insufficient for the initialization
236 static PredMap *createPredMap(const _Graph& graph) {
237 return new PredMap(graph);
240 /// \brief The type of the map that stores the dists of the nodes.
242 /// The type of the map that stores the dists of the nodes.
243 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
245 typedef typename Graph::template NodeMap<typename _LengthMap::Value>
248 /// \brief Instantiates a DistMap.
250 /// This function instantiates a \ref DistMap.
251 /// \param graph is the graph, to which we would like to define the
253 static DistMap *createDistMap(const _Graph& graph) {
254 return new DistMap(graph);
260 /// \brief %DagShortestPath algorithm class.
262 /// \ingroup flowalgs
263 /// This class provides an efficient implementation of a Dag sortest path
264 /// searching algorithm. The edge lengths are passed to the algorithm
265 /// using a \ref concepts::ReadMap "ReadMap", so it is easy to change it
266 /// to any kind of length.
268 /// The complexity of the algorithm is O(n + e).
270 /// The type of the length is determined by the
271 /// \ref concepts::ReadMap::Value "Value" of the length map.
273 /// \param _Graph The graph type the algorithm runs on. The default value
274 /// is \ref ListGraph. The value of _Graph is not used directly by
275 /// DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
276 /// \param _LengthMap This read-only EdgeMap determines the lengths of the
277 /// edges. The default map type is \ref concepts::Graph::EdgeMap
278 /// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly
279 /// by DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
280 /// \param _Traits Traits class to set various data types used by the
281 /// algorithm. The default traits class is \ref DagShortestPathDefaultTraits
282 /// "DagShortestPathDefaultTraits<_Graph,_LengthMap>". See \ref
283 /// DagShortestPathDefaultTraits for the documentation of a DagShortestPath traits
286 /// \author Balazs Attila Mihaly (based on Balazs Dezso's work)
289 template <typename _Graph, typename _LengthMap, typename _Traits>
291 template <typename _Graph=ListGraph,
292 typename _LengthMap=typename _Graph::template EdgeMap<int>,
293 typename _Traits=DagShortestPathDefaultTraits<_Graph,_LengthMap> >
295 class DagShortestPath {
298 /// \brief \ref Exception for uninitialized parameters.
300 /// This error represents problems in the initialization
301 /// of the parameters of the algorithms.
303 class UninitializedParameter : public lemon::UninitializedParameter {
305 virtual const char* what() const throw() {
306 return "lemon::DagShortestPath::UninitializedParameter";
310 typedef _Traits Traits;
311 ///The type of the underlying graph.
312 typedef typename _Traits::Graph Graph;
314 typedef typename Graph::Node Node;
315 typedef typename Graph::NodeIt NodeIt;
316 typedef typename Graph::Edge Edge;
317 typedef typename Graph::EdgeIt EdgeIt;
318 typedef typename Graph::OutEdgeIt OutEdgeIt;
320 /// \brief The type of the length of the edges.
321 typedef typename _Traits::LengthMap::Value Value;
322 /// \brief The type of the map that stores the edge lengths.
323 typedef typename _Traits::LengthMap LengthMap;
324 /// \brief The type of the map that stores the last
325 /// edges of the shortest paths.
326 typedef typename _Traits::PredMap PredMap;
327 /// \brief The type of the map that stores the dists of the nodes.
328 typedef typename _Traits::DistMap DistMap;
329 /// \brief The operation traits.
330 typedef typename _Traits::OperationTraits OperationTraits;
331 /// \brief The Node weight map.
332 typedef typename Graph::template NodeMap<Value> WeightMap;
334 /// Pointer to the underlying graph
336 /// Pointer to the length map
337 const LengthMap *length;
338 ///Pointer to the map of predecessors edges
340 ///Indicates if \ref _pred is locally allocated (\c true) or not
342 ///Pointer to the map of distances
344 ///Indicates if \ref _dist is locally allocated (\c true) or not
346 ///Process step counter
347 unsigned int _process_step;
349 std::vector<Node> _node_order;
351 /// Creates the maps if necessary.
355 _pred = Traits::createPredMap(*graph);
359 _dist = Traits::createDistMap(*graph);
365 typedef DagShortestPath Create;
367 /// \name Named template parameters
372 struct DefPredMapTraits : public Traits {
374 static PredMap *createPredMap(const Graph&) {
375 throw UninitializedParameter();
379 /// \brief \ref named-templ-param "Named parameter" for setting PredMap
381 /// \ref named-templ-param "Named parameter" for setting PredMap type
385 typedef DagShortestPath< Graph, LengthMap, DefPredMapTraits<T> > Create;
389 struct DefDistMapTraits : public Traits {
391 static DistMap *createDistMap(const Graph& graph) {
392 throw UninitializedParameter();
396 /// \brief \ref named-templ-param "Named parameter" for setting DistMap
399 /// \ref named-templ-param "Named parameter" for setting DistMap type
403 : public DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > {
404 typedef DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > Create;
408 struct DefOperationTraitsTraits : public Traits {
409 typedef T OperationTraits;
412 /// \brief \ref named-templ-param "Named parameter" for setting
413 /// OperationTraits type
415 /// \ref named-templ-param "Named parameter" for setting OperationTraits
418 struct DefOperationTraits
419 : public DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> > {
420 typedef DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> >
432 /// \brief Constructor.
434 /// \param _graph the graph the algorithm will run on.
435 /// \param _length the length map used by the algorithm.
436 DagShortestPath(const Graph& _graph, const LengthMap& _length) :
437 graph(&_graph), length(&_length),
438 _pred(0), local_pred(false),
439 _dist(0), local_dist(false){}
441 /// \brief Constructor with node weight map.
443 /// \param _graph the graph the algorithm will run on.
444 /// \param _length the length map used by the algorithm.
445 /// The NodeMap _length will be used as the weight map.
446 /// Each edge will have the weight of its target node.
447 DagShortestPath(const Graph& _graph, const WeightMap& _length) :
449 _pred(0), local_pred(false),
450 _dist(0), local_dist(false){
451 length=new LengthMap(_graph);
452 _dist=new DistMap(_graph);
453 for(EdgeIt eit(_graph);eit!=INVALID;++eit)
454 (const_cast<LengthMap*>(length))->set(eit,_length[_graph.target(eit)]);
459 if(local_pred) delete _pred;
460 if(local_dist) delete _dist;
463 /// \brief Sets the length map.
465 /// Sets the length map.
466 /// \return \c (*this)
467 DagShortestPath &lengthMap(const LengthMap &m) {
472 /// \brief Sets the map storing the predecessor edges.
474 /// Sets the map storing the predecessor edges.
475 /// If you don't use this function before calling \ref run(),
476 /// it will allocate one. The destuctor deallocates this
477 /// automatically allocated map, of course.
478 /// \return \c (*this)
479 DagShortestPath &predMap(PredMap &m) {
488 /// \brief Sets the map storing the distances calculated by the algorithm.
490 /// Sets the map storing the distances calculated by the algorithm.
491 /// If you don't use this function before calling \ref run(),
492 /// it will allocate one. The destuctor deallocates this
493 /// automatically allocated map, of course.
494 /// \return \c (*this)
495 DagShortestPath &distMap(DistMap &m) {
504 /// \name Execution control
505 /// The simplest way to execute the algorithm is to use
506 /// one of the member functions called \c run(...)
508 /// If you need more control on the execution,
509 /// first you must call \ref init(...), then you can add several source
510 /// nodes with \ref addSource().
511 /// Finally \ref start() will perform the actual path computation.
512 /// Some functions have an alternative form (\ref checkedInit(...),
513 /// \ref checkedRun(...)) which also verifies if the graph given in the
514 /// constructor is a dag.
518 /// \brief Initializes the internal data structures.
520 /// Initializes the internal data structures.
521 void init(const Value value = OperationTraits::infinity()) {
522 typedef typename Graph::template NodeMap<int> NodeOrderMap;
524 NodeOrderMap node_order(*graph);
525 topologicalSort(*graph,node_order);
526 _node_order.resize(countNodes(*graph),INVALID);
528 for (NodeIt it(*graph); it != INVALID; ++it) {
529 _node_order[node_order[it]]=it;
530 _pred->set(it, INVALID);
531 _dist->set(it, value);
535 /// \brief Initializes the internal data structures
536 /// with a given topological sort (NodeMap).
538 /// Initializes the internal data structures
539 /// with a given topological sort (NodeMap).
540 void init(const typename Graph::template NodeMap<int>& node_order,
541 const Value value = OperationTraits::infinity()) {
543 _node_order.resize(countNodes(*graph),INVALID);
545 for (NodeIt it(*graph); it != INVALID; ++it) {
546 _node_order[node_order[it]]=it;
547 _pred->set(it, INVALID);
548 _dist->set(it, value);
552 /// \brief Initializes the internal data structures
553 /// with a given topological sort (std::vector).
555 /// Initializes the internal data structures
556 /// with a given topological sort (std::vector).
557 void init(const std::vector<Node>& node_order,
558 const Value value = OperationTraits::infinity()) {
560 _node_order=node_order;
562 for (NodeIt it(*graph); it != INVALID; ++it) {
563 _pred->set(it, INVALID);
564 _dist->set(it, value);
568 /// \brief Initializes the internal data structures. It also checks if the graph is dag.
570 /// Initializes the internal data structures. It also checks if the graph is dag.
571 /// \return true if the graph (given in the constructor) is dag, false otherwise.
572 bool checkedInit(const Value value = OperationTraits::infinity()) {
573 typedef typename Graph::template NodeMap<int> NodeOrderMap;
574 NodeOrderMap node_order(*graph);
575 if(!checkedTopologicalSort(*graph,node_order))return false;
576 init(node_order,value);
580 /// \brief Initializes the internal data structures with a given
581 /// topological sort (NodeMap). It also checks if the graph is dag.
583 /// Initializes the internal data structures with a given
584 /// topological sort (NodeMap). It also checks if the graph is dag.
585 /// \return true if the graph (given in the constructor) is dag, false otherwise.
586 bool checkedInit(const typename Graph::template NodeMap<int>& node_order,
587 const Value value = OperationTraits::infinity()) {
588 for(NodeIt it(*graph);it!=INVALID;++it){
589 for(OutEdgeIt oeit(*graph,it);oeit!=INVALID;++oeit){
590 if(node_order[graph->target(oeit)]<node_order[it])return false;
593 init(node_order,value);
597 /// \brief Initializes the internal data structures with a given
598 /// topological sort (std::vector). It also checks if the graph is dag.
600 /// Initializes the internal data structures with a given
601 /// topological sort (std::vector). It also checks if the graph is dag.
602 /// \return true if the graph (given in the constructor) is dag, false otherwise.
603 bool checkedInit(const std::vector<Node>& node_order,
604 const Value value = OperationTraits::infinity()) {
605 typedef typename Graph::template NodeMap<bool> BoolNodeMap;
606 BoolNodeMap _processed(*graph,false);
607 for(unsigned int i=0;i<_node_order.size();++i){
608 _processed[node_order[i]]=true;
609 for(OutEdgeIt oeit(*graph,node_order[i]);oeit!=INVALID;++oeit){
610 if(_processed[graph->target(oeit)])return false;
613 init(node_order,value);
617 /// \brief Adds a new source node.
619 /// The optional second parameter is the initial distance of the node.
620 /// It just sets the distance of the node to the given value.
621 void addSource(Node source, Value dst = OperationTraits::zero()) {
622 if((*_dist)[source] != dst){
623 _dist->set(source, dst);
627 /// \brief Executes one step from the dag shortest path algorithm.
629 /// If the algoritm calculated the distances in the previous step
630 /// strictly for all at most k length paths then it will calculate the
631 /// distances strictly for all at most k + 1 length paths. With k
632 /// iteration this function calculates the at most k length paths.
633 ///\pre the queue is not empty
634 ///\return the currently processed node
635 Node processNextNode() {
636 if(reached(_node_order[_process_step])){
637 for (OutEdgeIt it(*graph, _node_order[_process_step]); it != INVALID; ++it) {
638 Node target = graph->target(it);
640 OperationTraits::plus((*_dist)[_node_order[_process_step]], (*length)[it]);
641 if (OperationTraits::less(relaxed, (*_dist)[target])) {
642 _pred->set(target, it);
643 _dist->set(target, relaxed);
648 return _node_order[_process_step-1];
651 ///\brief Returns \c false if there are nodes
652 ///to be processed in the queue
654 ///Returns \c false if there are nodes
655 ///to be processed in the queue
656 bool emptyQueue() { return _node_order.size()-1==_process_step; }
658 ///\brief Returns the number of the nodes to be processed.
660 ///Returns the number of the nodes to be processed in the queue.
661 int queueSize() { return _node_order.size()-1-_process_step; }
663 /// \brief Executes the algorithm.
665 /// \pre init() must be called and at least one node should be added
666 /// with addSource() before using this function.
668 /// This method runs the %DagShortestPath algorithm from the root node(s)
669 /// in order to compute the shortest path to each node. The algorithm
671 /// - The shortest path tree.
672 /// - The distance of each node from the root(s).
674 while(!emptyQueue()) {
679 /// \brief Runs %DagShortestPath algorithm from node \c s.
681 /// This method runs the %DagShortestPath algorithm from a root node \c s
682 /// in order to compute the shortest path to each node. The algorithm
684 /// - The shortest path tree.
685 /// - The distance of each node from the root.
687 /// \note d.run(s) is just a shortcut of the following code.
699 /// \brief Runs %DagShortestPath algorithm from node \c s.
700 /// It also checks if the graph is a dag.
702 /// This method runs the %DagShortestPath algorithm from a root node \c s
703 /// in order to compute the shortest path to each node. The algorithm
705 /// - The shortest path tree.
706 /// - The distance of each node from the root.
707 /// The algorithm checks if the graph given int the constructor is a dag.
708 bool checkedRun(Node s) {
709 if(!checkedInit())return false;
717 /// \name Query Functions
718 /// The result of the %DagShortestPath algorithm can be obtained using these
720 /// Before the use of these functions,
721 /// either run() or start() must be called.
725 typedef PredMapPath<Graph, PredMap> Path;
727 ///Gives back the shortest path.
729 ///Gives back the shortest path.
730 ///\pre The \c t should be reachable from the source.
733 return Path(*graph, *_pred, t);
736 /// \brief The distance of a node from the root.
738 /// Returns the distance of a node from the root.
739 /// \pre \ref run() must be called before using this function.
740 /// \warning If node \c v in unreachable from the root the return value
741 /// of this funcion is undefined.
742 Value dist(Node v) const { return (*_dist)[v]; }
744 /// \brief Returns the 'previous edge' of the shortest path tree.
746 /// For a node \c v it returns the 'previous edge' of the shortest path
747 /// tree, i.e. it returns the last edge of a shortest path from the root
748 /// to \c v. It is \ref INVALID if \c v is unreachable from the root or
749 /// if \c v=s. The shortest path tree used here is equal to the shortest
750 /// path tree used in \ref predNode().
751 /// \pre \ref run() must be called before using
753 Edge predEdge(Node v) const { return (*_pred)[v]; }
755 /// \brief Returns the 'previous node' of the shortest path tree.
757 /// For a node \c v it returns the 'previous node' of the shortest path
758 /// tree, i.e. it returns the last but one node from a shortest path from
759 /// the root to \c /v. It is INVALID if \c v is unreachable from the root
760 /// or if \c v=s. The shortest path tree used here is equal to the
761 /// shortest path tree used in \ref predEdge(). \pre \ref run() must be
762 /// called before using this function.
763 Node predNode(Node v) const {
764 return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]);
767 /// \brief Returns a reference to the NodeMap of distances.
769 /// Returns a reference to the NodeMap of distances. \pre \ref run() must
770 /// be called before using this function.
771 const DistMap &distMap() const { return *_dist;}
773 /// \brief Returns a reference to the shortest path tree map.
775 /// Returns a reference to the NodeMap of the edges of the
776 /// shortest path tree.
777 /// \pre \ref run() must be called before using this function.
778 const PredMap &predMap() const { return *_pred; }
780 /// \brief Checks if a node is reachable from the root.
782 /// Returns \c true if \c v is reachable from the root.
783 /// \pre \ref run() must be called before using this function.
785 bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
790 /// \brief Default traits class of DagShortestPath function.
792 /// Default traits class of DagShortestPath function.
793 /// \param _Graph Graph type.
794 /// \param _LengthMap Type of length map.
795 template <typename _Graph, typename _LengthMap>
796 struct DagShortestPathWizardDefaultTraits {
797 /// \brief The graph type the algorithm runs on.
798 typedef _Graph Graph;
800 /// \brief The type of the map that stores the edge lengths.
802 /// The type of the map that stores the edge lengths.
803 /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
804 typedef _LengthMap LengthMap;
806 /// \brief The value type of the length map.
807 typedef typename _LengthMap::Value Value;
809 /// \brief Operation traits for dag shortest path algorithm.
811 /// It defines the infinity type on the given Value type
812 /// and the used operation.
813 /// \see DagShortestPathDefaultOperationTraits
814 typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
816 /// \brief The type of the map that stores the last
817 /// edges of the shortest paths.
819 /// The type of the map that stores the last
820 /// edges of the shortest paths.
821 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
822 typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
824 /// \brief Instantiates a PredMap.
826 /// This function instantiates a \ref PredMap.
827 static PredMap *createPredMap(const _Graph &) {
828 return new PredMap();
830 /// \brief The type of the map that stores the dists of the nodes.
832 /// The type of the map that stores the dists of the nodes.
833 /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
834 typedef NullMap<typename Graph::Node, Value> DistMap;
835 /// \brief Instantiates a DistMap.
837 /// This function instantiates a \ref DistMap.
838 static DistMap *createDistMap(const _Graph &) {
839 return new DistMap();
843 /// \brief Default traits used by \ref DagShortestPathWizard
845 /// To make it easier to use DagShortestPath algorithm
846 /// we have created a wizard class.
847 /// This \ref DagShortestPathWizard class needs default traits,
848 /// as well as the \ref DagShortestPath class.
849 /// The \ref DagShortestPathWizardBase is a class to be the default traits of the
850 /// \ref DagShortestPathWizard class.
851 /// \todo More named parameters are required...
852 template<class _Graph,class _LengthMap>
853 class DagShortestPathWizardBase
854 : public DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> {
856 typedef DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> Base;
858 /// Type of the nodes in the graph.
859 typedef typename Base::Graph::Node Node;
861 /// Pointer to the underlying graph.
863 /// Pointer to the length map
865 ///Pointer to the map of predecessors edges.
867 ///Pointer to the map of distances.
869 ///Pointer to the source node.
875 /// This constructor does not require parameters, therefore it initiates
876 /// all of the attributes to default values (0, INVALID).
877 DagShortestPathWizardBase() : _graph(0), _length(0), _pred(0),
878 _dist(0), _source(INVALID) {}
882 /// This constructor requires some parameters,
883 /// listed in the parameters list.
884 /// Others are initiated to 0.
885 /// \param graph is the initial value of \ref _graph
886 /// \param length is the initial value of \ref _length
887 /// \param source is the initial value of \ref _source
888 DagShortestPathWizardBase(const _Graph& graph,
889 const _LengthMap& length,
890 Node source = INVALID) :
891 _graph((void *)&graph), _length((void *)&length), _pred(0),
892 _dist(0), _source(source) {}
896 /// A class to make the usage of DagShortestPath algorithm easier
898 /// This class is created to make it easier to use DagShortestPath algorithm.
899 /// It uses the functions and features of the plain \ref DagShortestPath,
900 /// but it is much simpler to use it.
902 /// Simplicity means that the way to change the types defined
903 /// in the traits class is based on functions that returns the new class
904 /// and not on templatable built-in classes.
905 /// When using the plain \ref DagShortestPath
906 /// the new class with the modified type comes from
907 /// the original class by using the ::
908 /// operator. In the case of \ref DagShortestPathWizard only
909 /// a function have to be called and it will
910 /// return the needed class.
912 /// It does not have own \ref run method. When its \ref run method is called
913 /// it initiates a plain \ref DagShortestPath class, and calls the \ref
914 /// DagShortestPath::run() method of it.
915 template<class _Traits>
916 class DagShortestPathWizard : public _Traits {
917 typedef _Traits Base;
919 ///The type of the underlying graph.
920 typedef typename _Traits::Graph Graph;
922 typedef typename Graph::Node Node;
923 typedef typename Graph::NodeIt NodeIt;
924 typedef typename Graph::Edge Edge;
925 typedef typename Graph::OutEdgeIt EdgeIt;
927 ///The type of the map that stores the edge lengths.
928 typedef typename _Traits::LengthMap LengthMap;
930 ///The type of the length of the edges.
931 typedef typename LengthMap::Value Value;
933 ///\brief The type of the map that stores the last
934 ///edges of the shortest paths.
935 typedef typename _Traits::PredMap PredMap;
937 ///The type of the map that stores the dists of the nodes.
938 typedef typename _Traits::DistMap DistMap;
942 DagShortestPathWizard() : _Traits() {}
944 /// \brief Constructor that requires parameters.
946 /// Constructor that requires parameters.
947 /// These parameters will be the default values for the traits class.
948 DagShortestPathWizard(const Graph& graph, const LengthMap& length,
949 Node source = INVALID)
950 : _Traits(graph, length, source) {}
952 /// \brief Copy constructor
953 DagShortestPathWizard(const _Traits &b) : _Traits(b) {}
955 ~DagShortestPathWizard() {}
957 /// \brief Runs DagShortestPath algorithm from a given node.
959 /// Runs DagShortestPath algorithm from a given node.
960 /// The node can be given by the \ref source function.
962 if(Base::_source == INVALID) throw UninitializedParameter();
963 DagShortestPath<Graph,LengthMap,_Traits>
964 bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
965 if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
966 if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
967 bf.run(Base::_source);
970 /// \brief Runs DagShortestPath algorithm from the given node.
972 /// Runs DagShortestPath algorithm from the given node.
973 /// \param source is the given source.
974 void run(Node source) {
975 Base::_source = source;
980 struct DefPredMapBase : public Base {
982 static PredMap *createPredMap(const Graph &) { return 0; };
983 DefPredMapBase(const _Traits &b) : _Traits(b) {}
986 ///\brief \ref named-templ-param "Named parameter"
987 ///function for setting PredMap type
989 /// \ref named-templ-param "Named parameter"
990 ///function for setting PredMap type
993 DagShortestPathWizard<DefPredMapBase<T> > predMap(const T &t)
995 Base::_pred=(void *)&t;
996 return DagShortestPathWizard<DefPredMapBase<T> >(*this);
1000 struct DefDistMapBase : public Base {
1002 static DistMap *createDistMap(const Graph &) { return 0; };
1003 DefDistMapBase(const _Traits &b) : _Traits(b) {}
1006 ///\brief \ref named-templ-param "Named parameter"
1007 ///function for setting DistMap type
1009 /// \ref named-templ-param "Named parameter"
1010 ///function for setting DistMap type
1013 DagShortestPathWizard<DefDistMapBase<T> > distMap(const T &t) {
1014 Base::_dist=(void *)&t;
1015 return DagShortestPathWizard<DefDistMapBase<T> >(*this);
1019 struct DefOperationTraitsBase : public Base {
1020 typedef T OperationTraits;
1021 DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
1024 ///\brief \ref named-templ-param "Named parameter"
1025 ///function for setting OperationTraits type
1027 /// \ref named-templ-param "Named parameter"
1028 ///function for setting OperationTraits type
1031 DagShortestPathWizard<DefOperationTraitsBase<T> > distMap() {
1032 return DagShortestPathWizard<DefDistMapBase<T> >(*this);
1035 /// \brief Sets the source node, from which the DagShortestPath algorithm runs.
1037 /// Sets the source node, from which the DagShortestPath algorithm runs.
1038 /// \param source is the source node.
1039 DagShortestPathWizard<_Traits>& source(Node source) {
1040 Base::_source = source;
1046 /// \brief Function type interface for DagShortestPath algorithm.
1048 /// \ingroup flowalgs
1049 /// Function type interface for DagShortestPath algorithm.
1051 /// This function also has several \ref named-templ-func-param
1052 /// "named parameters", they are declared as the members of class
1053 /// \ref DagShortestPathWizard.
1055 /// example shows how to use these parameters.
1057 /// dagShortestPath(g,length,source).predMap(preds).run();
1059 /// \warning Don't forget to put the \ref DagShortestPathWizard::run() "run()"
1060 /// to the end of the parameter list.
1061 /// \sa DagShortestPathWizard
1062 /// \sa DagShortestPath
1063 template<class _Graph, class _LengthMap>
1064 DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
1065 dagShortestPath(const _Graph& graph,
1066 const _LengthMap& length,
1067 typename _Graph::Node source = INVALID) {
1068 return DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
1069 (graph, length, source);
1072 } //END OF NAMESPACE LEMON