lemon/bellman_ford.h
changeset 1871 3905d347112c
parent 1858 a5b6d941ed52
child 1875 98698b69a902
equal deleted inserted replaced
12:0cf01fe691ad 0:ae1949538876
     1 /* -*- C++ -*-
     1 /* -*- C++ -*-
     2  * lemon/belmann_ford.h - Part of LEMON, a generic C++ optimization library
     2  * lemon/bellman_ford.h - Part of LEMON, a generic C++ optimization library
     3  *
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     6  *
     7  * Permission to use, modify and distribute this software is granted
     7  * Permission to use, modify and distribute this software is granted
    17 #ifndef LEMON_BELMANN_FORD_H
    17 #ifndef LEMON_BELMANN_FORD_H
    18 #define LEMON_BELMANN_FORD_H
    18 #define LEMON_BELMANN_FORD_H
    19 
    19 
    20 /// \ingroup flowalgs
    20 /// \ingroup flowalgs
    21 /// \file
    21 /// \file
    22 /// \brief BelmannFord algorithm.
    22 /// \brief BellmanFord algorithm.
    23 ///
    23 ///
    24 
    24 
    25 #include <lemon/list_graph.h>
    25 #include <lemon/list_graph.h>
    26 #include <lemon/invalid.h>
    26 #include <lemon/invalid.h>
    27 #include <lemon/error.h>
    27 #include <lemon/error.h>
    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.
    59       return left < right;
    59       return left < right;
    60     }
    60     }
    61   };
    61   };
    62 
    62 
    63   template <typename Value>
    63   template <typename Value>
    64   struct BelmannFordDefaultOperationTraits<Value, false> {
    64   struct BellmanFordDefaultOperationTraits<Value, false> {
    65     static Value zero() {
    65     static Value zero() {
    66       return static_cast<Value>(0);
    66       return static_cast<Value>(0);
    67     }
    67     }
    68     static Value infinity() {
    68     static Value infinity() {
    69       return std::numeric_limits<Value>::max();
    69       return std::numeric_limits<Value>::max();
    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.
   247       _mask = new MaskMap(*graph, false);
   247       _mask = new MaskMap(*graph, false);
   248     }
   248     }
   249     
   249     
   250   public :
   250   public :
   251  
   251  
   252     typedef BelmannFord Create;
   252     typedef BellmanFord Create;
   253 
   253 
   254     /// \name Named template parameters
   254     /// \name Named template parameters
   255 
   255 
   256     ///@{
   256     ///@{
   257 
   257 
   267     /// type
   267     /// type
   268     /// \ref named-templ-param "Named parameter" for setting PredMap type
   268     /// \ref named-templ-param "Named parameter" for setting PredMap type
   269     ///
   269     ///
   270     template <class T>
   270     template <class T>
   271     struct DefPredMap 
   271     struct DefPredMap 
   272       : public BelmannFord< Graph, LengthMap, DefPredMapTraits<T> > {
   272       : public BellmanFord< Graph, LengthMap, DefPredMapTraits<T> > {
   273       typedef BelmannFord< Graph, LengthMap, DefPredMapTraits<T> > Create;
   273       typedef BellmanFord< Graph, LengthMap, DefPredMapTraits<T> > Create;
   274     };
   274     };
   275     
   275     
   276     template <class T>
   276     template <class T>
   277     struct DefDistMapTraits : public Traits {
   277     struct DefDistMapTraits : public Traits {
   278       typedef T DistMap;
   278       typedef T DistMap;
   286     ///
   286     ///
   287     /// \ref named-templ-param "Named parameter" for setting DistMap type
   287     /// \ref named-templ-param "Named parameter" for setting DistMap type
   288     ///
   288     ///
   289     template <class T>
   289     template <class T>
   290     struct DefDistMap 
   290     struct DefDistMap 
   291       : public BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > {
   291       : public BellmanFord< Graph, LengthMap, DefDistMapTraits<T> > {
   292       typedef BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > Create;
   292       typedef BellmanFord< Graph, LengthMap, DefDistMapTraits<T> > Create;
   293     };
   293     };
   294     
   294     
   295     template <class T>
   295     template <class T>
   296     struct DefOperationTraitsTraits : public Traits {
   296     struct DefOperationTraitsTraits : public Traits {
   297       typedef T OperationTraits;
   297       typedef T OperationTraits;
   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.
   347     /// Sets the map storing the predecessor edges.
   347     /// Sets the map storing the predecessor edges.
   348     /// If you don't use this function before calling \ref run(),
   348     /// If you don't use this function before calling \ref run(),
   349     /// it will allocate one. The destuctor deallocates this
   349     /// it will allocate one. The destuctor deallocates this
   350     /// automatically allocated map, of course.
   350     /// automatically allocated map, of course.
   351     /// \return \c (*this)
   351     /// \return \c (*this)
   352     BelmannFord &predMap(PredMap &m) {
   352     BellmanFord &predMap(PredMap &m) {
   353       if(local_pred) {
   353       if(local_pred) {
   354 	delete _pred;
   354 	delete _pred;
   355 	local_pred=false;
   355 	local_pred=false;
   356       }
   356       }
   357       _pred = &m;
   357       _pred = &m;
   363     /// Sets the map storing the distances calculated by the algorithm.
   363     /// Sets the map storing the distances calculated by the algorithm.
   364     /// If you don't use this function before calling \ref run(),
   364     /// If you don't use this function before calling \ref run(),
   365     /// it will allocate one. The destuctor deallocates this
   365     /// it will allocate one. The destuctor deallocates this
   366     /// automatically allocated map, of course.
   366     /// automatically allocated map, of course.
   367     /// \return \c (*this)
   367     /// \return \c (*this)
   368     BelmannFord &distMap(DistMap &m) {
   368     BellmanFord &distMap(DistMap &m) {
   369       if(local_dist) {
   369       if(local_dist) {
   370 	delete _dist;
   370 	delete _dist;
   371 	local_dist=false;
   371 	local_dist=false;
   372       }
   372       }
   373       _dist = &m;
   373       _dist = &m;
   414 	_process.push_back(source);
   414 	_process.push_back(source);
   415 	_mask->set(source, true);
   415 	_mask->set(source, true);
   416       }
   416       }
   417     }
   417     }
   418 
   418 
   419     /// \brief Executes one round from the belmann ford algorithm.
   419     /// \brief Executes one round from the bellman ford algorithm.
   420     ///
   420     ///
   421     /// If the algoritm calculated the distances in the previous round 
   421     /// If the algoritm calculated the distances in the previous round 
   422     /// strictly for all at most k length paths then it will calculate the 
   422     /// strictly for all at most k length paths then it will calculate the 
   423     /// distances strictly for all at most k + 1 length paths. With k
   423     /// distances strictly for all at most k + 1 length paths. With k
   424     /// iteration this function calculates the at most k length paths.
   424     /// iteration this function calculates the at most k length paths.
   448       }
   448       }
   449       _process.swap(nextProcess);
   449       _process.swap(nextProcess);
   450       return _process.empty();
   450       return _process.empty();
   451     }
   451     }
   452 
   452 
   453     /// \brief Executes one weak round from the belmann ford algorithm.
   453     /// \brief Executes one weak round from the bellman ford algorithm.
   454     ///
   454     ///
   455     /// If the algorithm calculated the distances in the
   455     /// If the algorithm calculated the distances in the
   456     /// previous round at least for all at most k length paths then it will
   456     /// previous round at least for all at most k length paths then it will
   457     /// calculate the distances at least for all at most k + 1 length paths.
   457     /// calculate the distances at least for all at most k + 1 length paths.
   458     /// This function does not make it possible to calculate strictly the
   458     /// This function does not make it possible to calculate strictly the
   486     /// \brief Executes the algorithm.
   486     /// \brief Executes the algorithm.
   487     ///
   487     ///
   488     /// \pre init() must be called and at least one node should be added
   488     /// \pre init() must be called and at least one node should be added
   489     /// with addSource() before using this function.
   489     /// with addSource() before using this function.
   490     ///
   490     ///
   491     /// This method runs the %BelmannFord algorithm from the root node(s)
   491     /// This method runs the %BellmanFord algorithm from the root node(s)
   492     /// in order to compute the shortest path to each node. The algorithm 
   492     /// in order to compute the shortest path to each node. The algorithm 
   493     /// computes 
   493     /// computes 
   494     /// - The shortest path tree.
   494     /// - The shortest path tree.
   495     /// - The distance of each node from the root(s).
   495     /// - The distance of each node from the root(s).
   496     void start() {
   496     void start() {
   504     ///
   504     ///
   505     /// \pre init() must be called and at least one node should be added
   505     /// \pre init() must be called and at least one node should be added
   506     /// with addSource() before using this function. If there is
   506     /// with addSource() before using this function. If there is
   507     /// a negative cycles in the graph it gives back false.
   507     /// a negative cycles in the graph it gives back false.
   508     ///
   508     ///
   509     /// This method runs the %BelmannFord algorithm from the root node(s)
   509     /// This method runs the %BellmanFord algorithm from the root node(s)
   510     /// in order to compute the shortest path to each node. The algorithm 
   510     /// in order to compute the shortest path to each node. The algorithm 
   511     /// computes 
   511     /// computes 
   512     /// - The shortest path tree.
   512     /// - The shortest path tree.
   513     /// - The distance of each node from the root(s).
   513     /// - The distance of each node from the root(s).
   514     bool checkedStart() {
   514     bool checkedStart() {
   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     ///
   553       init();
   553       init();
   554       addSource(s);
   554       addSource(s);
   555       start();
   555       start();
   556     }
   556     }
   557     
   557     
   558     /// \brief Runs %BelmannFord algorithm with limited path length 
   558     /// \brief Runs %BellmanFord algorithm with limited path length 
   559     /// from node \c s.
   559     /// from node \c s.
   560     ///    
   560     ///    
   561     /// This method runs the %BelmannFord algorithm from a root node \c s
   561     /// This method runs the %BellmanFord algorithm from a root node \c s
   562     /// in order to compute the shortest path with at most \c len edges 
   562     /// in order to compute the shortest path with at most \c len edges 
   563     /// to each node. The algorithm computes
   563     /// to each node. The algorithm computes
   564     /// - The shortest path tree.
   564     /// - The shortest path tree.
   565     /// - The distance of each node from the root.
   565     /// - The distance of each node from the root.
   566     ///
   566     ///
   577     }
   577     }
   578     
   578     
   579     ///@}
   579     ///@}
   580 
   580 
   581     /// \name Query Functions
   581     /// \name Query Functions
   582     /// The result of the %BelmannFord algorithm can be obtained using these
   582     /// The result of the %BellmanFord algorithm can be obtained using these
   583     /// functions.\n
   583     /// functions.\n
   584     /// Before the use of these functions,
   584     /// Before the use of these functions,
   585     /// either run() or start() must be called.
   585     /// either run() or start() must be called.
   586     
   586     
   587     ///@{
   587     ///@{
   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     }
   863     ///
   863     ///
   864     /// \ref named-templ-param "Named parameter"
   864     /// \ref named-templ-param "Named parameter"
   865     ///function for setting PredMap type
   865     ///function for setting PredMap type
   866     ///
   866     ///
   867     template<class T>
   867     template<class T>
   868     BelmannFordWizard<DefPredMapBase<T> > predMap(const T &t) 
   868     BellmanFordWizard<DefPredMapBase<T> > predMap(const T &t) 
   869     {
   869     {
   870       Base::_pred=(void *)&t;
   870       Base::_pred=(void *)&t;
   871       return BelmannFordWizard<DefPredMapBase<T> >(*this);
   871       return BellmanFordWizard<DefPredMapBase<T> >(*this);
   872     }
   872     }
   873     
   873     
   874     template<class T>
   874     template<class T>
   875     struct DefDistMapBase : public Base {
   875     struct DefDistMapBase : public Base {
   876       typedef T DistMap;
   876       typedef T DistMap;
   883     ///
   883     ///
   884     /// \ref named-templ-param "Named parameter"
   884     /// \ref named-templ-param "Named parameter"
   885     ///function for setting DistMap type
   885     ///function for setting DistMap type
   886     ///
   886     ///
   887     template<class T>
   887     template<class T>
   888     BelmannFordWizard<DefDistMapBase<T> > distMap(const T &t) {
   888     BellmanFordWizard<DefDistMapBase<T> > distMap(const T &t) {
   889       Base::_dist=(void *)&t;
   889       Base::_dist=(void *)&t;
   890       return BelmannFordWizard<DefDistMapBase<T> >(*this);
   890       return BellmanFordWizard<DefDistMapBase<T> >(*this);
   891     }
   891     }
   892 
   892 
   893     template<class T>
   893     template<class T>
   894     struct DefOperationTraitsBase : public Base {
   894     struct DefOperationTraitsBase : public Base {
   895       typedef T OperationTraits;
   895       typedef T OperationTraits;
   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