29 |
29 |
30 #include <limits> |
30 #include <limits> |
31 |
31 |
32 namespace lemon { |
32 namespace lemon { |
33 |
33 |
34 /// \brief Default OperationTraits for the BelmannFord algorithm class. |
34 /// \brief Default OperationTraits for the BellmanFord algorithm class. |
35 /// |
35 /// |
36 /// It defines all computational operations and constants which are |
36 /// It defines all computational operations and constants which are |
37 /// used in the belmann ford algorithm. The default implementation |
37 /// used in the bellman ford algorithm. The default implementation |
38 /// is based on the numeric_limits class. If the numeric type does not |
38 /// is based on the numeric_limits class. If the numeric type does not |
39 /// have infinity value then the maximum value is used as extremal |
39 /// have infinity value then the maximum value is used as extremal |
40 /// infinity value. |
40 /// infinity value. |
41 template < |
41 template < |
42 typename Value, |
42 typename Value, |
43 bool has_infinity = std::numeric_limits<Value>::has_infinity> |
43 bool has_infinity = std::numeric_limits<Value>::has_infinity> |
44 struct BelmannFordDefaultOperationTraits { |
44 struct BellmanFordDefaultOperationTraits { |
45 /// \brief Gives back the zero value of the type. |
45 /// \brief Gives back the zero value of the type. |
46 static Value zero() { |
46 static Value zero() { |
47 return static_cast<Value>(0); |
47 return static_cast<Value>(0); |
48 } |
48 } |
49 /// \brief Gives back the positive infinity value of the type. |
49 /// \brief Gives back the positive infinity value of the type. |
75 static bool less(const Value& left, const Value& right) { |
75 static bool less(const Value& left, const Value& right) { |
76 return left < right; |
76 return left < right; |
77 } |
77 } |
78 }; |
78 }; |
79 |
79 |
80 /// \brief Default traits class of BelmannFord class. |
80 /// \brief Default traits class of BellmanFord class. |
81 /// |
81 /// |
82 /// Default traits class of BelmannFord class. |
82 /// Default traits class of BellmanFord class. |
83 /// \param _Graph Graph type. |
83 /// \param _Graph Graph type. |
84 /// \param _LegthMap Type of length map. |
84 /// \param _LegthMap Type of length map. |
85 template<class _Graph, class _LengthMap> |
85 template<class _Graph, class _LengthMap> |
86 struct BelmannFordDefaultTraits { |
86 struct BellmanFordDefaultTraits { |
87 /// The graph type the algorithm runs on. |
87 /// The graph type the algorithm runs on. |
88 typedef _Graph Graph; |
88 typedef _Graph Graph; |
89 |
89 |
90 /// \brief The type of the map that stores the edge lengths. |
90 /// \brief The type of the map that stores the edge lengths. |
91 /// |
91 /// |
94 typedef _LengthMap LengthMap; |
94 typedef _LengthMap LengthMap; |
95 |
95 |
96 // The type of the length of the edges. |
96 // The type of the length of the edges. |
97 typedef typename _LengthMap::Value Value; |
97 typedef typename _LengthMap::Value Value; |
98 |
98 |
99 /// \brief Operation traits for belmann-ford algorithm. |
99 /// \brief Operation traits for bellman-ford algorithm. |
100 /// |
100 /// |
101 /// It defines the infinity type on the given Value type |
101 /// It defines the infinity type on the given Value type |
102 /// and the used operation. |
102 /// and the used operation. |
103 /// \see BelmannFordDefaultOperationTraits |
103 /// \see BellmanFordDefaultOperationTraits |
104 typedef BelmannFordDefaultOperationTraits<Value> OperationTraits; |
104 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
105 |
105 |
106 /// \brief The type of the map that stores the last edges of the |
106 /// \brief The type of the map that stores the last edges of the |
107 /// shortest paths. |
107 /// shortest paths. |
108 /// |
108 /// |
109 /// The type of the map that stores the last |
109 /// The type of the map that stores the last |
137 return new DistMap(graph); |
137 return new DistMap(graph); |
138 } |
138 } |
139 |
139 |
140 }; |
140 }; |
141 |
141 |
142 /// \brief %BelmannFord algorithm class. |
142 /// \brief %BellmanFord algorithm class. |
143 /// |
143 /// |
144 /// \ingroup flowalgs |
144 /// \ingroup flowalgs |
145 /// This class provides an efficient implementation of \c Belmann-Ford |
145 /// This class provides an efficient implementation of \c Bellman-Ford |
146 /// algorithm. The edge lengths are passed to the algorithm using a |
146 /// algorithm. The edge lengths are passed to the algorithm using a |
147 /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any |
147 /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any |
148 /// kind of length. |
148 /// kind of length. |
149 /// |
149 /// |
150 /// The Belmann-Ford algorithm solves the shortest path from one node |
150 /// The Bellman-Ford algorithm solves the shortest path from one node |
151 /// problem when the edges can have negative length but the graph should |
151 /// problem when the edges can have negative length but the graph should |
152 /// not contain cycles with negative sum of length. If we can assume |
152 /// not contain cycles with negative sum of length. If we can assume |
153 /// that all edge is non-negative in the graph then the dijkstra algorithm |
153 /// that all edge is non-negative in the graph then the dijkstra algorithm |
154 /// should be used rather. |
154 /// should be used rather. |
155 /// |
155 /// |
158 /// The type of the length is determined by the |
158 /// The type of the length is determined by the |
159 /// \ref concept::ReadMap::Value "Value" of the length map. |
159 /// \ref concept::ReadMap::Value "Value" of the length map. |
160 /// |
160 /// |
161 /// \param _Graph The graph type the algorithm runs on. The default value |
161 /// \param _Graph The graph type the algorithm runs on. The default value |
162 /// is \ref ListGraph. The value of _Graph is not used directly by |
162 /// is \ref ListGraph. The value of _Graph is not used directly by |
163 /// BelmannFord, it is only passed to \ref BelmannFordDefaultTraits. |
163 /// BellmanFord, it is only passed to \ref BellmanFordDefaultTraits. |
164 /// \param _LengthMap This read-only EdgeMap determines the lengths of the |
164 /// \param _LengthMap This read-only EdgeMap determines the lengths of the |
165 /// edges. The default map type is \ref concept::StaticGraph::EdgeMap |
165 /// edges. The default map type is \ref concept::StaticGraph::EdgeMap |
166 /// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly |
166 /// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly |
167 /// by BelmannFord, it is only passed to \ref BelmannFordDefaultTraits. |
167 /// by BellmanFord, it is only passed to \ref BellmanFordDefaultTraits. |
168 /// \param _Traits Traits class to set various data types used by the |
168 /// \param _Traits Traits class to set various data types used by the |
169 /// algorithm. The default traits class is \ref BelmannFordDefaultTraits |
169 /// algorithm. The default traits class is \ref BellmanFordDefaultTraits |
170 /// "BelmannFordDefaultTraits<_Graph,_LengthMap>". See \ref |
170 /// "BellmanFordDefaultTraits<_Graph,_LengthMap>". See \ref |
171 /// BelmannFordDefaultTraits for the documentation of a BelmannFord traits |
171 /// BellmanFordDefaultTraits for the documentation of a BellmanFord traits |
172 /// class. |
172 /// class. |
173 /// |
173 /// |
174 /// \author Balazs Dezso |
174 /// \author Balazs Dezso |
175 |
175 |
176 #ifdef DOXYGEN |
176 #ifdef DOXYGEN |
177 template <typename _Graph, typename _LengthMap, typename _Traits> |
177 template <typename _Graph, typename _LengthMap, typename _Traits> |
178 #else |
178 #else |
179 template <typename _Graph=ListGraph, |
179 template <typename _Graph=ListGraph, |
180 typename _LengthMap=typename _Graph::template EdgeMap<int>, |
180 typename _LengthMap=typename _Graph::template EdgeMap<int>, |
181 typename _Traits=BelmannFordDefaultTraits<_Graph,_LengthMap> > |
181 typename _Traits=BellmanFordDefaultTraits<_Graph,_LengthMap> > |
182 #endif |
182 #endif |
183 class BelmannFord { |
183 class BellmanFord { |
184 public: |
184 public: |
185 |
185 |
186 /// \brief \ref Exception for uninitialized parameters. |
186 /// \brief \ref Exception for uninitialized parameters. |
187 /// |
187 /// |
188 /// This error represents problems in the initialization |
188 /// This error represents problems in the initialization |
189 /// of the parameters of the algorithms. |
189 /// of the parameters of the algorithms. |
190 |
190 |
191 class UninitializedParameter : public lemon::UninitializedParameter { |
191 class UninitializedParameter : public lemon::UninitializedParameter { |
192 public: |
192 public: |
193 virtual const char* exceptionName() const { |
193 virtual const char* exceptionName() const { |
194 return "lemon::BelmannFord::UninitializedParameter"; |
194 return "lemon::BellmanFord::UninitializedParameter"; |
195 } |
195 } |
196 }; |
196 }; |
197 |
197 |
198 typedef _Traits Traits; |
198 typedef _Traits Traits; |
199 ///The type of the underlying graph. |
199 ///The type of the underlying graph. |
302 /// |
302 /// |
303 /// \ref named-templ-param "Named parameter" for setting OperationTraits |
303 /// \ref named-templ-param "Named parameter" for setting OperationTraits |
304 /// type |
304 /// type |
305 template <class T> |
305 template <class T> |
306 struct DefOperationTraits |
306 struct DefOperationTraits |
307 : public BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> > { |
307 : public BellmanFord< Graph, LengthMap, DefOperationTraitsTraits<T> > { |
308 typedef BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> > |
308 typedef BellmanFord< Graph, LengthMap, DefOperationTraitsTraits<T> > |
309 Create; |
309 Create; |
310 }; |
310 }; |
311 |
311 |
312 ///@} |
312 ///@} |
313 |
313 |
314 protected: |
314 protected: |
315 |
315 |
316 BelmannFord() {} |
316 BellmanFord() {} |
317 |
317 |
318 public: |
318 public: |
319 |
319 |
320 /// \brief Constructor. |
320 /// \brief Constructor. |
321 /// |
321 /// |
322 /// \param _graph the graph the algorithm will run on. |
322 /// \param _graph the graph the algorithm will run on. |
323 /// \param _length the length map used by the algorithm. |
323 /// \param _length the length map used by the algorithm. |
324 BelmannFord(const Graph& _graph, const LengthMap& _length) : |
324 BellmanFord(const Graph& _graph, const LengthMap& _length) : |
325 graph(&_graph), length(&_length), |
325 graph(&_graph), length(&_length), |
326 _pred(0), local_pred(false), |
326 _pred(0), local_pred(false), |
327 _dist(0), local_dist(false) {} |
327 _dist(0), local_dist(false) {} |
328 |
328 |
329 ///Destructor. |
329 ///Destructor. |
330 ~BelmannFord() { |
330 ~BellmanFord() { |
331 if(local_pred) delete _pred; |
331 if(local_pred) delete _pred; |
332 if(local_dist) delete _dist; |
332 if(local_dist) delete _dist; |
333 delete _mask; |
333 delete _mask; |
334 } |
334 } |
335 |
335 |
336 /// \brief Sets the length map. |
336 /// \brief Sets the length map. |
337 /// |
337 /// |
338 /// Sets the length map. |
338 /// Sets the length map. |
339 /// \return \c (*this) |
339 /// \return \c (*this) |
340 BelmannFord &lengthMap(const LengthMap &m) { |
340 BellmanFord &lengthMap(const LengthMap &m) { |
341 length = &m; |
341 length = &m; |
342 return *this; |
342 return *this; |
343 } |
343 } |
344 |
344 |
345 /// \brief Sets the map storing the predecessor edges. |
345 /// \brief Sets the map storing the predecessor edges. |
522 /// \brief Executes the algorithm with path length limit. |
522 /// \brief Executes the algorithm with path length limit. |
523 /// |
523 /// |
524 /// \pre init() must be called and at least one node should be added |
524 /// \pre init() must be called and at least one node should be added |
525 /// with addSource() before using this function. |
525 /// with addSource() before using this function. |
526 /// |
526 /// |
527 /// This method runs the %BelmannFord algorithm from the root node(s) |
527 /// This method runs the %BellmanFord algorithm from the root node(s) |
528 /// in order to compute the shortest path with at most \c length edge |
528 /// in order to compute the shortest path with at most \c length edge |
529 /// long paths to each node. The algorithm computes |
529 /// long paths to each node. The algorithm computes |
530 /// - The shortest path tree. |
530 /// - The shortest path tree. |
531 /// - The limited distance of each node from the root(s). |
531 /// - The limited distance of each node from the root(s). |
532 void limitedStart(int length) { |
532 void limitedStart(int length) { |
533 for (int i = 0; i < length; ++i) { |
533 for (int i = 0; i < length; ++i) { |
534 if (processNextRound()) break; |
534 if (processNextRound()) break; |
535 } |
535 } |
536 } |
536 } |
537 |
537 |
538 /// \brief Runs %BelmannFord algorithm from node \c s. |
538 /// \brief Runs %BellmanFord algorithm from node \c s. |
539 /// |
539 /// |
540 /// This method runs the %BelmannFord algorithm from a root node \c s |
540 /// This method runs the %BellmanFord algorithm from a root node \c s |
541 /// in order to compute the shortest path to each node. The algorithm |
541 /// in order to compute the shortest path to each node. The algorithm |
542 /// computes |
542 /// computes |
543 /// - The shortest path tree. |
543 /// - The shortest path tree. |
544 /// - The distance of each node from the root. |
544 /// - The distance of each node from the root. |
545 /// |
545 /// |
660 bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); } |
660 bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); } |
661 |
661 |
662 ///@} |
662 ///@} |
663 }; |
663 }; |
664 |
664 |
665 /// \brief Default traits class of BelmannFord function. |
665 /// \brief Default traits class of BellmanFord function. |
666 /// |
666 /// |
667 /// Default traits class of BelmannFord function. |
667 /// Default traits class of BellmanFord function. |
668 /// \param _Graph Graph type. |
668 /// \param _Graph Graph type. |
669 /// \param _LengthMap Type of length map. |
669 /// \param _LengthMap Type of length map. |
670 template <typename _Graph, typename _LengthMap> |
670 template <typename _Graph, typename _LengthMap> |
671 struct BelmannFordWizardDefaultTraits { |
671 struct BellmanFordWizardDefaultTraits { |
672 /// \brief The graph type the algorithm runs on. |
672 /// \brief The graph type the algorithm runs on. |
673 typedef _Graph Graph; |
673 typedef _Graph Graph; |
674 |
674 |
675 /// \brief The type of the map that stores the edge lengths. |
675 /// \brief The type of the map that stores the edge lengths. |
676 /// |
676 /// |
679 typedef _LengthMap LengthMap; |
679 typedef _LengthMap LengthMap; |
680 |
680 |
681 /// \brief The value type of the length map. |
681 /// \brief The value type of the length map. |
682 typedef typename _LengthMap::Value Value; |
682 typedef typename _LengthMap::Value Value; |
683 |
683 |
684 /// \brief Operation traits for belmann-ford algorithm. |
684 /// \brief Operation traits for bellman-ford algorithm. |
685 /// |
685 /// |
686 /// It defines the infinity type on the given Value type |
686 /// It defines the infinity type on the given Value type |
687 /// and the used operation. |
687 /// and the used operation. |
688 /// \see BelmannFordDefaultOperationTraits |
688 /// \see BellmanFordDefaultOperationTraits |
689 typedef BelmannFordDefaultOperationTraits<Value> OperationTraits; |
689 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
690 |
690 |
691 /// \brief The type of the map that stores the last |
691 /// \brief The type of the map that stores the last |
692 /// edges of the shortest paths. |
692 /// edges of the shortest paths. |
693 /// |
693 /// |
694 /// The type of the map that stores the last |
694 /// The type of the map that stores the last |
713 static DistMap *createDistMap(const _Graph &) { |
713 static DistMap *createDistMap(const _Graph &) { |
714 return new DistMap(); |
714 return new DistMap(); |
715 } |
715 } |
716 }; |
716 }; |
717 |
717 |
718 /// \brief Default traits used by \ref BelmannFordWizard |
718 /// \brief Default traits used by \ref BellmanFordWizard |
719 /// |
719 /// |
720 /// To make it easier to use BelmannFord algorithm |
720 /// To make it easier to use BellmanFord algorithm |
721 /// we have created a wizard class. |
721 /// we have created a wizard class. |
722 /// This \ref BelmannFordWizard class needs default traits, |
722 /// This \ref BellmanFordWizard class needs default traits, |
723 /// as well as the \ref BelmannFord class. |
723 /// as well as the \ref BellmanFord class. |
724 /// The \ref BelmannFordWizardBase is a class to be the default traits of the |
724 /// The \ref BellmanFordWizardBase is a class to be the default traits of the |
725 /// \ref BelmannFordWizard class. |
725 /// \ref BellmanFordWizard class. |
726 /// \todo More named parameters are required... |
726 /// \todo More named parameters are required... |
727 template<class _Graph,class _LengthMap> |
727 template<class _Graph,class _LengthMap> |
728 class BelmannFordWizardBase |
728 class BellmanFordWizardBase |
729 : public BelmannFordWizardDefaultTraits<_Graph,_LengthMap> { |
729 : public BellmanFordWizardDefaultTraits<_Graph,_LengthMap> { |
730 |
730 |
731 typedef BelmannFordWizardDefaultTraits<_Graph,_LengthMap> Base; |
731 typedef BellmanFordWizardDefaultTraits<_Graph,_LengthMap> Base; |
732 protected: |
732 protected: |
733 /// Type of the nodes in the graph. |
733 /// Type of the nodes in the graph. |
734 typedef typename Base::Graph::Node Node; |
734 typedef typename Base::Graph::Node Node; |
735 |
735 |
736 /// Pointer to the underlying graph. |
736 /// Pointer to the underlying graph. |
747 public: |
747 public: |
748 /// Constructor. |
748 /// Constructor. |
749 |
749 |
750 /// This constructor does not require parameters, therefore it initiates |
750 /// This constructor does not require parameters, therefore it initiates |
751 /// all of the attributes to default values (0, INVALID). |
751 /// all of the attributes to default values (0, INVALID). |
752 BelmannFordWizardBase() : _graph(0), _length(0), _pred(0), |
752 BellmanFordWizardBase() : _graph(0), _length(0), _pred(0), |
753 _dist(0), _source(INVALID) {} |
753 _dist(0), _source(INVALID) {} |
754 |
754 |
755 /// Constructor. |
755 /// Constructor. |
756 |
756 |
757 /// This constructor requires some parameters, |
757 /// This constructor requires some parameters, |
758 /// listed in the parameters list. |
758 /// listed in the parameters list. |
759 /// Others are initiated to 0. |
759 /// Others are initiated to 0. |
760 /// \param graph is the initial value of \ref _graph |
760 /// \param graph is the initial value of \ref _graph |
761 /// \param length is the initial value of \ref _length |
761 /// \param length is the initial value of \ref _length |
762 /// \param source is the initial value of \ref _source |
762 /// \param source is the initial value of \ref _source |
763 BelmannFordWizardBase(const _Graph& graph, |
763 BellmanFordWizardBase(const _Graph& graph, |
764 const _LengthMap& length, |
764 const _LengthMap& length, |
765 Node source = INVALID) : |
765 Node source = INVALID) : |
766 _graph((void *)&graph), _length((void *)&length), _pred(0), |
766 _graph((void *)&graph), _length((void *)&length), _pred(0), |
767 _dist(0), _source(source) {} |
767 _dist(0), _source(source) {} |
768 |
768 |
769 }; |
769 }; |
770 |
770 |
771 /// A class to make the usage of BelmannFord algorithm easier |
771 /// A class to make the usage of BellmanFord algorithm easier |
772 |
772 |
773 /// This class is created to make it easier to use BelmannFord algorithm. |
773 /// This class is created to make it easier to use BellmanFord algorithm. |
774 /// It uses the functions and features of the plain \ref BelmannFord, |
774 /// It uses the functions and features of the plain \ref BellmanFord, |
775 /// but it is much simpler to use it. |
775 /// but it is much simpler to use it. |
776 /// |
776 /// |
777 /// Simplicity means that the way to change the types defined |
777 /// Simplicity means that the way to change the types defined |
778 /// in the traits class is based on functions that returns the new class |
778 /// in the traits class is based on functions that returns the new class |
779 /// and not on templatable built-in classes. |
779 /// and not on templatable built-in classes. |
780 /// When using the plain \ref BelmannFord |
780 /// When using the plain \ref BellmanFord |
781 /// the new class with the modified type comes from |
781 /// the new class with the modified type comes from |
782 /// the original class by using the :: |
782 /// the original class by using the :: |
783 /// operator. In the case of \ref BelmannFordWizard only |
783 /// operator. In the case of \ref BellmanFordWizard only |
784 /// a function have to be called and it will |
784 /// a function have to be called and it will |
785 /// return the needed class. |
785 /// return the needed class. |
786 /// |
786 /// |
787 /// It does not have own \ref run method. When its \ref run method is called |
787 /// It does not have own \ref run method. When its \ref run method is called |
788 /// it initiates a plain \ref BelmannFord class, and calls the \ref |
788 /// it initiates a plain \ref BellmanFord class, and calls the \ref |
789 /// BelmannFord::run method of it. |
789 /// BellmanFord::run method of it. |
790 template<class _Traits> |
790 template<class _Traits> |
791 class BelmannFordWizard : public _Traits { |
791 class BellmanFordWizard : public _Traits { |
792 typedef _Traits Base; |
792 typedef _Traits Base; |
793 |
793 |
794 ///The type of the underlying graph. |
794 ///The type of the underlying graph. |
795 typedef typename _Traits::Graph Graph; |
795 typedef typename _Traits::Graph Graph; |
796 |
796 |
812 ///The type of the map that stores the dists of the nodes. |
812 ///The type of the map that stores the dists of the nodes. |
813 typedef typename _Traits::DistMap DistMap; |
813 typedef typename _Traits::DistMap DistMap; |
814 |
814 |
815 public: |
815 public: |
816 /// Constructor. |
816 /// Constructor. |
817 BelmannFordWizard() : _Traits() {} |
817 BellmanFordWizard() : _Traits() {} |
818 |
818 |
819 /// \brief Constructor that requires parameters. |
819 /// \brief Constructor that requires parameters. |
820 /// |
820 /// |
821 /// Constructor that requires parameters. |
821 /// Constructor that requires parameters. |
822 /// These parameters will be the default values for the traits class. |
822 /// These parameters will be the default values for the traits class. |
823 BelmannFordWizard(const Graph& graph, const LengthMap& length, |
823 BellmanFordWizard(const Graph& graph, const LengthMap& length, |
824 Node source = INVALID) |
824 Node source = INVALID) |
825 : _Traits(graph, length, source) {} |
825 : _Traits(graph, length, source) {} |
826 |
826 |
827 /// \brief Copy constructor |
827 /// \brief Copy constructor |
828 BelmannFordWizard(const _Traits &b) : _Traits(b) {} |
828 BellmanFordWizard(const _Traits &b) : _Traits(b) {} |
829 |
829 |
830 ~BelmannFordWizard() {} |
830 ~BellmanFordWizard() {} |
831 |
831 |
832 /// \brief Runs BelmannFord algorithm from a given node. |
832 /// \brief Runs BellmanFord algorithm from a given node. |
833 /// |
833 /// |
834 /// Runs BelmannFord algorithm from a given node. |
834 /// Runs BellmanFord algorithm from a given node. |
835 /// The node can be given by the \ref source function. |
835 /// The node can be given by the \ref source function. |
836 void run() { |
836 void run() { |
837 if(Base::_source == INVALID) throw UninitializedParameter(); |
837 if(Base::_source == INVALID) throw UninitializedParameter(); |
838 BelmannFord<Graph,LengthMap,_Traits> |
838 BellmanFord<Graph,LengthMap,_Traits> |
839 bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length); |
839 bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length); |
840 if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred); |
840 if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred); |
841 if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist); |
841 if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist); |
842 bf.run(Base::_source); |
842 bf.run(Base::_source); |
843 } |
843 } |
844 |
844 |
845 /// \brief Runs BelmannFord algorithm from the given node. |
845 /// \brief Runs BellmanFord algorithm from the given node. |
846 /// |
846 /// |
847 /// Runs BelmannFord algorithm from the given node. |
847 /// Runs BellmanFord algorithm from the given node. |
848 /// \param source is the given source. |
848 /// \param source is the given source. |
849 void run(Node source) { |
849 void run(Node source) { |
850 Base::_source = source; |
850 Base::_source = source; |
851 run(); |
851 run(); |
852 } |
852 } |
901 /// |
901 /// |
902 /// \ref named-templ-param "Named parameter" |
902 /// \ref named-templ-param "Named parameter" |
903 ///function for setting OperationTraits type |
903 ///function for setting OperationTraits type |
904 /// |
904 /// |
905 template<class T> |
905 template<class T> |
906 BelmannFordWizard<DefOperationTraitsBase<T> > distMap() { |
906 BellmanFordWizard<DefOperationTraitsBase<T> > distMap() { |
907 return BelmannFordWizard<DefDistMapBase<T> >(*this); |
907 return BellmanFordWizard<DefDistMapBase<T> >(*this); |
908 } |
908 } |
909 |
909 |
910 /// \brief Sets the source node, from which the BelmannFord algorithm runs. |
910 /// \brief Sets the source node, from which the BellmanFord algorithm runs. |
911 /// |
911 /// |
912 /// Sets the source node, from which the BelmannFord algorithm runs. |
912 /// Sets the source node, from which the BellmanFord algorithm runs. |
913 /// \param source is the source node. |
913 /// \param source is the source node. |
914 BelmannFordWizard<_Traits>& source(Node source) { |
914 BellmanFordWizard<_Traits>& source(Node source) { |
915 Base::_source = source; |
915 Base::_source = source; |
916 return *this; |
916 return *this; |
917 } |
917 } |
918 |
918 |
919 }; |
919 }; |
920 |
920 |
921 /// \brief Function type interface for BelmannFord algorithm. |
921 /// \brief Function type interface for BellmanFord algorithm. |
922 /// |
922 /// |
923 /// \ingroup flowalgs |
923 /// \ingroup flowalgs |
924 /// Function type interface for BelmannFord algorithm. |
924 /// Function type interface for BellmanFord algorithm. |
925 /// |
925 /// |
926 /// This function also has several \ref named-templ-func-param |
926 /// This function also has several \ref named-templ-func-param |
927 /// "named parameters", they are declared as the members of class |
927 /// "named parameters", they are declared as the members of class |
928 /// \ref BelmannFordWizard. |
928 /// \ref BellmanFordWizard. |
929 /// The following |
929 /// The following |
930 /// example shows how to use these parameters. |
930 /// example shows how to use these parameters. |
931 /// \code |
931 /// \code |
932 /// belmannford(g,length,source).predMap(preds).run(); |
932 /// bellmanford(g,length,source).predMap(preds).run(); |
933 /// \endcode |
933 /// \endcode |
934 /// \warning Don't forget to put the \ref BelmannFordWizard::run() "run()" |
934 /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
935 /// to the end of the parameter list. |
935 /// to the end of the parameter list. |
936 /// \sa BelmannFordWizard |
936 /// \sa BellmanFordWizard |
937 /// \sa BelmannFord |
937 /// \sa BellmanFord |
938 template<class _Graph, class _LengthMap> |
938 template<class _Graph, class _LengthMap> |
939 BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> > |
939 BellmanFordWizard<BellmanFordWizardBase<_Graph,_LengthMap> > |
940 belmannFord(const _Graph& graph, |
940 bellmanFord(const _Graph& graph, |
941 const _LengthMap& length, |
941 const _LengthMap& length, |
942 typename _Graph::Node source = INVALID) { |
942 typename _Graph::Node source = INVALID) { |
943 return BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> > |
943 return BellmanFordWizard<BellmanFordWizardBase<_Graph,_LengthMap> > |
944 (graph, length, source); |
944 (graph, length, source); |
945 } |
945 } |
946 |
946 |
947 } //END OF NAMESPACE LEMON |
947 } //END OF NAMESPACE LEMON |
948 |
948 |