lemon/belmann_ford.h
author deba
Wed, 02 Nov 2005 15:25:13 +0000
changeset 1752 dce1f28ac595
parent 1723 fb4f801dd692
child 1754 4bf5ceb49023
permissions -rw-r--r--
IterableIntMap

todo: documentation need
     1 /* -*- C++ -*-
     2  * lemon/belmann_ford.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_BELMANN_FORD_H
    18 #define LEMON_BELMANN_FORD_H
    19 
    20 ///\ingroup flowalgs
    21 /// \file
    22 /// \brief BelmannFord algorithm.
    23 ///
    24 /// \todo getPath() should be implemented! (also for BFS and DFS)
    25 
    26 #include <lemon/list_graph.h>
    27 #include <lemon/invalid.h>
    28 #include <lemon/error.h>
    29 #include <lemon/maps.h>
    30 
    31 #include <limits>
    32 
    33 namespace lemon {
    34 
    35   /// \brief Default OperationTraits for the BelmannFord algorithm class.
    36   ///  
    37   /// It defines all computational operations and constants which are
    38   /// used in the belmann ford algorithm. The default implementation
    39   /// is based on the numeric_limits class. If the numeric type does not
    40   /// have infinity value then the maximum value is used as extremal
    41   /// infinity value.
    42   template <
    43     typename Value, 
    44     bool has_infinity = std::numeric_limits<Value>::has_infinity>
    45   struct BelmannFordDefaultOperationTraits {
    46     /// \brief Gives back the zero value of the type.
    47     static Value zero() {
    48       return static_cast<Value>(0);
    49     }
    50     /// \brief Gives back the positive infinity value of the type.
    51     static Value infinity() {
    52       return std::numeric_limits<Value>::infinity();
    53     }
    54     /// \brief Gives back the sum of the given two elements.
    55     static Value plus(const Value& left, const Value& right) {
    56       return left + right;
    57     }
    58     /// \brief Gives back true only if the first value less than the second.
    59     static bool less(const Value& left, const Value& right) {
    60       return left < right;
    61     }
    62   };
    63 
    64   template <typename Value>
    65   struct BelmannFordDefaultOperationTraits<Value, false> {
    66     static Value zero() {
    67       return static_cast<Value>(0);
    68     }
    69     static Value infinity() {
    70       return std::numeric_limits<Value>::max();
    71     }
    72     static Value plus(const Value& left, const Value& right) {
    73       if (left == infinity() || right == infinity()) return infinity();
    74       return left + right;
    75     }
    76     static bool less(const Value& left, const Value& right) {
    77       return left < right;
    78     }
    79   };
    80   
    81   /// \brief Default traits class of BelmannFord class.
    82   ///
    83   /// Default traits class of BelmannFord class.
    84   /// \param _Graph Graph type.
    85   /// \param _LegthMap Type of length map.
    86   template<class _Graph, class _LengthMap>
    87   struct BelmannFordDefaultTraits {
    88     /// The graph type the algorithm runs on. 
    89     typedef _Graph Graph;
    90 
    91     /// \brief The type of the map that stores the edge lengths.
    92     ///
    93     /// The type of the map that stores the edge lengths.
    94     /// It must meet the \ref concept::ReadMap "ReadMap" concept.
    95     typedef _LengthMap LengthMap;
    96 
    97     // The type of the length of the edges.
    98     typedef typename _LengthMap::Value Value;
    99 
   100     /// \brief Operation traits for belmann-ford algorithm.
   101     ///
   102     /// It defines the infinity type on the given Value type
   103     /// and the used operation.
   104     /// \see BelmannFordDefaultOperationTraits
   105     typedef BelmannFordDefaultOperationTraits<Value> OperationTraits;
   106  
   107     /// \brief The type of the map that stores the last edges of the 
   108     /// shortest paths.
   109     /// 
   110     /// The type of the map that stores the last
   111     /// edges of the shortest paths.
   112     /// It must meet the \ref concept::WriteMap "WriteMap" concept.
   113     ///
   114     typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
   115 
   116     /// \brief Instantiates a PredMap.
   117     /// 
   118     /// This function instantiates a \ref PredMap. 
   119     /// \param G is the graph, to which we would like to define the PredMap.
   120     /// \todo The graph alone may be insufficient for the initialization
   121     static PredMap *createPredMap(const _Graph& graph) {
   122       return new PredMap(graph);
   123     }
   124 
   125     /// \brief The type of the map that stores the dists of the nodes.
   126     ///
   127     /// The type of the map that stores the dists of the nodes.
   128     /// It must meet the \ref concept::WriteMap "WriteMap" concept.
   129     ///
   130     typedef typename Graph::template NodeMap<typename _LengthMap::Value> 
   131     DistMap;
   132 
   133     /// \brief Instantiates a DistMap.
   134     ///
   135     /// This function instantiates a \ref DistMap. 
   136     /// \param G is the graph, to which we would like to define the 
   137     /// \ref DistMap
   138     static DistMap *createDistMap(const _Graph& graph) {
   139       return new DistMap(graph);
   140     }
   141 
   142   };
   143   
   144   /// \brief BelmannFord algorithm class.
   145   ///
   146   /// \ingroup flowalgs
   147   /// This class provides an efficient implementation of \c Belmann-Ford 
   148   /// algorithm. The edge lengths are passed to the algorithm using a
   149   /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any 
   150   /// kind of length.
   151   ///
   152   /// The Belmann-Ford algorithm solves the shortest path from one node
   153   /// problem when the edges can have negative length but the graph should
   154   /// not contain circle with negative sum of length. If we can assume
   155   /// that all edge is non-negative in the graph then the dijkstra algorithm
   156   /// should be used rather.
   157   ///
   158   /// The complexity of the algorithm is O(n * e).
   159   ///
   160   /// The type of the length is determined by the
   161   /// \ref concept::ReadMap::Value "Value" of the length map.
   162   ///
   163   /// \param _Graph The graph type the algorithm runs on. The default value
   164   /// is \ref ListGraph. The value of _Graph is not used directly by
   165   /// BelmannFord, it is only passed to \ref BelmannFordDefaultTraits.
   166   /// \param _LengthMap This read-only EdgeMap determines the lengths of the
   167   /// edges. The default map type is \ref concept::StaticGraph::EdgeMap 
   168   /// "Graph::EdgeMap<int>".  The value of _LengthMap is not used directly 
   169   /// by BelmannFord, it is only passed to \ref BelmannFordDefaultTraits.  
   170   /// \param _Traits Traits class to set various data types used by the 
   171   /// algorithm.  The default traits class is \ref BelmannFordDefaultTraits
   172   /// "BelmannFordDefaultTraits<_Graph,_LengthMap>".  See \ref
   173   /// BelmannFordDefaultTraits for the documentation of a BelmannFord traits
   174   /// class.
   175   ///
   176   /// \author Balazs Dezso
   177 
   178 #ifdef DOXYGEN
   179   template <typename _Graph, typename _LengthMap, typename _Traits>
   180 #else
   181   template <typename _Graph=ListGraph,
   182 	    typename _LengthMap=typename _Graph::template EdgeMap<int>,
   183 	    typename _Traits=BelmannFordDefaultTraits<_Graph,_LengthMap> >
   184 #endif
   185   class BelmannFord {
   186   public:
   187     
   188     /// \brief \ref Exception for uninitialized parameters.
   189     ///
   190     /// This error represents problems in the initialization
   191     /// of the parameters of the algorithms.
   192 
   193     class UninitializedParameter : public lemon::UninitializedParameter {
   194     public:
   195       virtual const char* exceptionName() const {
   196 	return "lemon::BelmannFord::UninitializedParameter";
   197       }
   198     };
   199 
   200     typedef _Traits Traits;
   201     ///The type of the underlying graph.
   202     typedef typename _Traits::Graph Graph;
   203 
   204     typedef typename Graph::Node Node;
   205     typedef typename Graph::NodeIt NodeIt;
   206     typedef typename Graph::Edge Edge;
   207     typedef typename Graph::EdgeIt EdgeIt;
   208     
   209     /// \brief The type of the length of the edges.
   210     typedef typename _Traits::LengthMap::Value Value;
   211     /// \brief The type of the map that stores the edge lengths.
   212     typedef typename _Traits::LengthMap LengthMap;
   213     /// \brief The type of the map that stores the last
   214     /// edges of the shortest paths.
   215     typedef typename _Traits::PredMap PredMap;
   216     /// \brief The type of the map that stores the dists of the nodes.
   217     typedef typename _Traits::DistMap DistMap;
   218     /// \brief The operation traits.
   219     typedef typename _Traits::OperationTraits OperationTraits;
   220   private:
   221     /// Pointer to the underlying graph.
   222     const Graph *graph;
   223     /// Pointer to the length map
   224     const LengthMap *length;
   225     ///Pointer to the map of predecessors edges.
   226     PredMap *_pred;
   227     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   228     bool local_pred;
   229     ///Pointer to the map of distances.
   230     DistMap *_dist;
   231     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   232     bool local_dist;
   233 
   234     /// Creates the maps if necessary.
   235     void create_maps() {
   236       if(!_pred) {
   237 	local_pred = true;
   238 	_pred = Traits::createPredMap(*graph);
   239       }
   240       if(!_dist) {
   241 	local_dist = true;
   242 	_dist = Traits::createDistMap(*graph);
   243       }
   244     }
   245     
   246   public :
   247  
   248     typedef BelmannFord Create;
   249 
   250     /// \name Named template parameters
   251 
   252     ///@{
   253 
   254     template <class T>
   255     struct DefPredMapTraits : public Traits {
   256       typedef T PredMap;
   257       static PredMap *createPredMap(const Graph&) {
   258 	throw UninitializedParameter();
   259       }
   260     };
   261 
   262     /// \brief \ref named-templ-param "Named parameter" for setting PredMap 
   263     /// type
   264     /// \ref named-templ-param "Named parameter" for setting PredMap type
   265     ///
   266     template <class T>
   267     struct DefPredMap {
   268       typedef BelmannFord< Graph, LengthMap, DefPredMapTraits<T> > Create;
   269     };
   270     
   271     template <class T>
   272     struct DefDistMapTraits : public Traits {
   273       typedef T DistMap;
   274       static DistMap *createDistMap(const Graph& graph) {
   275 	throw UninitializedParameter();
   276       }
   277     };
   278 
   279     /// \brief \ref named-templ-param "Named parameter" for setting DistMap 
   280     /// type
   281     ///
   282     /// \ref named-templ-param "Named parameter" for setting DistMap type
   283     ///
   284     template <class T>
   285     struct DefDistMap 
   286       : public BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > {
   287       typedef BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > Create;
   288     };
   289     
   290     template <class T>
   291     struct DefOperationTraitsTraits : public Traits {
   292       typedef T OperationTraits;
   293     };
   294     
   295     /// \brief \ref named-templ-param "Named parameter" for setting 
   296     /// OperationTraits type
   297     ///
   298     /// \ref named-templ-param "Named parameter" for setting OperationTraits
   299     /// type
   300     template <class T>
   301     struct DefOperationTraits
   302       : public BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> > {
   303       typedef BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> >
   304       Create;
   305     };
   306     
   307     ///@}
   308 
   309   protected:
   310     
   311     BelmannFord() {}
   312 
   313   public:      
   314     
   315     /// \brief Constructor.
   316     ///
   317     /// \param _graph the graph the algorithm will run on.
   318     /// \param _length the length map used by the algorithm.
   319     BelmannFord(const Graph& _graph, const LengthMap& _length) :
   320       graph(&_graph), length(&_length),
   321       _pred(0), local_pred(false),
   322       _dist(0), local_dist(false) {}
   323     
   324     ///Destructor.
   325     ~BelmannFord() {
   326       if(local_pred) delete _pred;
   327       if(local_dist) delete _dist;
   328     }
   329 
   330     /// \brief Sets the length map.
   331     ///
   332     /// Sets the length map.
   333     /// \return \c (*this)
   334     BelmannFord &lengthMap(const LengthMap &m) {
   335       length = &m;
   336       return *this;
   337     }
   338 
   339     /// \brief Sets the map storing the predecessor edges.
   340     ///
   341     /// Sets the map storing the predecessor edges.
   342     /// If you don't use this function before calling \ref run(),
   343     /// it will allocate one. The destuctor deallocates this
   344     /// automatically allocated map, of course.
   345     /// \return \c (*this)
   346     BelmannFord &predMap(PredMap &m) {
   347       if(local_pred) {
   348 	delete _pred;
   349 	local_pred=false;
   350       }
   351       _pred = &m;
   352       return *this;
   353     }
   354 
   355     /// \brief Sets the map storing the distances calculated by the algorithm.
   356     ///
   357     /// Sets the map storing the distances calculated by the algorithm.
   358     /// If you don't use this function before calling \ref run(),
   359     /// it will allocate one. The destuctor deallocates this
   360     /// automatically allocated map, of course.
   361     /// \return \c (*this)
   362     BelmannFord &distMap(DistMap &m) {
   363       if(local_dist) {
   364 	delete _dist;
   365 	local_dist=false;
   366       }
   367       _dist = &m;
   368       return *this;
   369     }
   370 
   371     /// \name Execution control
   372     /// The simplest way to execute the algorithm is to use
   373     /// one of the member functions called \c run(...).
   374     /// \n
   375     /// If you need more control on the execution,
   376     /// first you must call \ref init(), then you can add several source nodes
   377     /// with \ref addSource().
   378     /// Finally \ref start() will perform the actual path
   379     /// computation.
   380 
   381     ///@{
   382 
   383     /// \brief Initializes the internal data structures.
   384     /// 
   385     /// Initializes the internal data structures.
   386     void init(const Value value = OperationTraits::infinity()) {
   387       create_maps();
   388       for (NodeIt it(*graph); it != INVALID; ++it) {
   389 	_pred->set(it, INVALID);
   390 	_dist->set(it, value);
   391       }
   392     }
   393     
   394     /// \brief Adds a new source node.
   395     ///
   396     /// The optional second parameter is the initial distance of the node.
   397     /// It just sets the distance of the node to the given value.
   398     void addSource(Node source, Value dst = OperationTraits::zero()) {
   399       _dist->set(source, dst);
   400     }
   401 
   402     /// \brief Executes the algorithm.
   403     ///
   404     /// \pre init() must be called and at least one node should be added
   405     /// with addSource() before using this function.
   406     ///
   407     /// This method runs the %BelmannFord algorithm from the root node(s)
   408     /// in order to compute the shortest path to each node. The algorithm 
   409     /// computes 
   410     /// - The shortest path tree.
   411     /// - The distance of each node from the root(s).
   412     void start() {
   413       int num = countNodes(*graph) - 1;
   414       for (int i = 0; i < num; ++i) {
   415 	bool done = true;
   416 	for (EdgeIt it(*graph); it != INVALID; ++it) {
   417 	  Node source = graph->source(it);
   418 	  Node target = graph->target(it);
   419 	  Value relaxed = 
   420 	    OperationTraits::plus((*_dist)[source], (*length)[it]);
   421 	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
   422 	    _pred->set(target, it);
   423 	    _dist->set(target, relaxed);
   424 	    done = false; 
   425 	  }
   426 	}
   427 	if (done) return;
   428       }
   429     }
   430 
   431     /// \brief Executes the algorithm and checks the negative circles.
   432     ///
   433     /// \pre init() must be called and at least one node should be added
   434     /// with addSource() before using this function. If there is
   435     /// a negative circle in the graph it gives back false.
   436     ///
   437     /// This method runs the %BelmannFord algorithm from the root node(s)
   438     /// in order to compute the shortest path to each node. The algorithm 
   439     /// computes 
   440     /// - The shortest path tree.
   441     /// - The distance of each node from the root(s).
   442     bool checkedStart() {
   443       int num = countNodes(*graph);
   444       for (int i = 0; i < num; ++i) {
   445 	bool done = true;
   446 	for (EdgeIt it(*graph); it != INVALID; ++it) {
   447 	  Node source = graph->source(it);
   448 	  Node target = graph->target(it);
   449 	  Value relaxed = 
   450 	    OperationTraits::plus((*_dist)[source], (*length)[it]);
   451 	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
   452 	    _pred->set(target, it);
   453 	    _dist->set(target, relaxed);
   454 	    done = false; 
   455 	  }
   456 	}
   457 	if (done) return true;
   458       }
   459       return false;
   460     }
   461     
   462     /// \brief Runs %BelmannFord algorithm from node \c s.
   463     ///    
   464     /// This method runs the %BelmannFord algorithm from a root node \c s
   465     /// in order to compute the shortest path to each node. The algorithm 
   466     /// computes
   467     /// - The shortest path tree.
   468     /// - The distance of each node from the root.
   469     ///
   470     /// \note d.run(s) is just a shortcut of the following code.
   471     /// \code
   472     ///  d.init();
   473     ///  d.addSource(s);
   474     ///  d.start();
   475     /// \endcode
   476     void run(Node s) {
   477       init();
   478       addSource(s);
   479       start();
   480     }
   481     
   482     ///@}
   483 
   484     /// \name Query Functions
   485     /// The result of the %BelmannFord algorithm can be obtained using these
   486     /// functions.\n
   487     /// Before the use of these functions,
   488     /// either run() or start() must be called.
   489     
   490     ///@{
   491 
   492     /// \brief Copies the shortest path to \c t into \c p
   493     ///    
   494     /// This function copies the shortest path to \c t into \c p.
   495     /// If it \c t is a source itself or unreachable, then it does not
   496     /// alter \c p.
   497     /// \todo Is it the right way to handle unreachable nodes?
   498     /// \return Returns \c true if a path to \c t was actually copied to \c p,
   499     /// \c false otherwise.
   500     /// \sa DirPath
   501     template <typename Path>
   502     bool getPath(Path &p, Node t) {
   503       if(reached(t)) {
   504 	p.clear();
   505 	typename Path::Builder b(p);
   506 	for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
   507 	  b.pushFront(pred(t));
   508 	b.commit();
   509 	return true;
   510       }
   511       return false;
   512     }
   513 	  
   514     /// \brief The distance of a node from the root.
   515     ///
   516     /// Returns the distance of a node from the root.
   517     /// \pre \ref run() must be called before using this function.
   518     /// \warning If node \c v in unreachable from the root the return value
   519     /// of this funcion is undefined.
   520     Value dist(Node v) const { return (*_dist)[v]; }
   521 
   522     /// \brief Returns the 'previous edge' of the shortest path tree.
   523     ///
   524     /// For a node \c v it returns the 'previous edge' of the shortest path 
   525     /// tree, i.e. it returns the last edge of a shortest path from the root 
   526     /// to \c v. It is \ref INVALID if \c v is unreachable from the root or 
   527     /// if \c v=s. The shortest path tree used here is equal to the shortest 
   528     /// path tree used in \ref predNode(). 
   529     /// \pre \ref run() must be called before using
   530     /// this function.
   531     /// \todo predEdge could be a better name.
   532     Edge pred(Node v) const { return (*_pred)[v]; }
   533 
   534     /// \brief Returns the 'previous node' of the shortest path tree.
   535     ///
   536     /// For a node \c v it returns the 'previous node' of the shortest path 
   537     /// tree, i.e. it returns the last but one node from a shortest path from 
   538     /// the root to \c /v. It is INVALID if \c v is unreachable from the root 
   539     /// or if \c v=s. The shortest path tree used here is equal to the 
   540     /// shortest path tree used in \ref pred().  \pre \ref run() must be 
   541     /// called before using this function.
   542     Node predNode(Node v) const { 
   543       return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]); 
   544     }
   545     
   546     /// \brief Returns a reference to the NodeMap of distances.
   547     ///
   548     /// Returns a reference to the NodeMap of distances. \pre \ref run() must
   549     /// be called before using this function.
   550     const DistMap &distMap() const { return *_dist;}
   551  
   552     /// \brief Returns a reference to the shortest path tree map.
   553     ///
   554     /// Returns a reference to the NodeMap of the edges of the
   555     /// shortest path tree.
   556     /// \pre \ref run() must be called before using this function.
   557     const PredMap &predMap() const { return *_pred; }
   558  
   559     /// \brief Checks if a node is reachable from the root.
   560     ///
   561     /// Returns \c true if \c v is reachable from the root.
   562     /// \pre \ref run() must be called before using this function.
   563     ///
   564     bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
   565     
   566     ///@}
   567   };
   568  
   569   /// \brief Default traits class of BelmannFord function.
   570   ///
   571   /// Default traits class of BelmannFord function.
   572   /// \param _Graph Graph type.
   573   /// \param _LengthMap Type of length map.
   574   template <typename _Graph, typename _LengthMap>
   575   struct BelmannFordWizardDefaultTraits {
   576     /// \brief The graph type the algorithm runs on. 
   577     typedef _Graph Graph;
   578 
   579     /// \brief The type of the map that stores the edge lengths.
   580     ///
   581     /// The type of the map that stores the edge lengths.
   582     /// It must meet the \ref concept::ReadMap "ReadMap" concept.
   583     typedef _LengthMap LengthMap;
   584 
   585     /// \brief The value type of the length map.
   586     typedef typename _LengthMap::Value Value;
   587 
   588     /// \brief Operation traits for belmann-ford algorithm.
   589     ///
   590     /// It defines the infinity type on the given Value type
   591     /// and the used operation.
   592     /// \see BelmannFordDefaultOperationTraits
   593     typedef BelmannFordDefaultOperationTraits<Value> OperationTraits;
   594 
   595     /// \brief The type of the map that stores the last
   596     /// edges of the shortest paths.
   597     /// 
   598     /// The type of the map that stores the last
   599     /// edges of the shortest paths.
   600     /// It must meet the \ref concept::WriteMap "WriteMap" concept.
   601     typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
   602 
   603     /// \brief Instantiates a PredMap.
   604     /// 
   605     /// This function instantiates a \ref PredMap. 
   606     static PredMap *createPredMap(const _Graph &) {
   607       return new PredMap();
   608     }
   609     /// \brief The type of the map that stores the dists of the nodes.
   610     ///
   611     /// The type of the map that stores the dists of the nodes.
   612     /// It must meet the \ref concept::WriteMap "WriteMap" concept.
   613     typedef NullMap<typename Graph::Node, Value> DistMap;
   614     /// \brief Instantiates a DistMap.
   615     ///
   616     /// This function instantiates a \ref DistMap. 
   617     static DistMap *createDistMap(const _Graph &) {
   618       return new DistMap();
   619     }
   620   };
   621   
   622   /// \brief Default traits used by \ref BelmannFordWizard
   623   ///
   624   /// To make it easier to use BelmannFord algorithm
   625   /// we have created a wizard class.
   626   /// This \ref BelmannFordWizard class needs default traits,
   627   /// as well as the \ref BelmannFord class.
   628   /// The \ref BelmannFordWizardBase is a class to be the default traits of the
   629   /// \ref BelmannFordWizard class.
   630   /// \todo More named parameters are required...
   631   template<class _Graph,class _LengthMap>
   632   class BelmannFordWizardBase 
   633     : public BelmannFordWizardDefaultTraits<_Graph,_LengthMap> {
   634 
   635     typedef BelmannFordWizardDefaultTraits<_Graph,_LengthMap> Base;
   636   protected:
   637     /// Type of the nodes in the graph.
   638     typedef typename Base::Graph::Node Node;
   639 
   640     /// Pointer to the underlying graph.
   641     void *_graph;
   642     /// Pointer to the length map
   643     void *_length;
   644     ///Pointer to the map of predecessors edges.
   645     void *_pred;
   646     ///Pointer to the map of distances.
   647     void *_dist;
   648     ///Pointer to the source node.
   649     Node _source;
   650 
   651     public:
   652     /// Constructor.
   653     
   654     /// This constructor does not require parameters, therefore it initiates
   655     /// all of the attributes to default values (0, INVALID).
   656     BelmannFordWizardBase() : _graph(0), _length(0), _pred(0),
   657 			   _dist(0), _source(INVALID) {}
   658 
   659     /// Constructor.
   660     
   661     /// This constructor requires some parameters,
   662     /// listed in the parameters list.
   663     /// Others are initiated to 0.
   664     /// \param graph is the initial value of  \ref _graph
   665     /// \param length is the initial value of  \ref _length
   666     /// \param source is the initial value of  \ref _source
   667     BelmannFordWizardBase(const _Graph& graph, 
   668 			  const _LengthMap& length, 
   669 			  Node source = INVALID) :
   670       _graph((void *)&graph), _length((void *)&length), _pred(0),
   671       _dist(0), _source(source) {}
   672 
   673   };
   674   
   675   /// A class to make the usage of BelmannFord algorithm easier
   676 
   677   /// This class is created to make it easier to use BelmannFord algorithm.
   678   /// It uses the functions and features of the plain \ref BelmannFord,
   679   /// but it is much simpler to use it.
   680   ///
   681   /// Simplicity means that the way to change the types defined
   682   /// in the traits class is based on functions that returns the new class
   683   /// and not on templatable built-in classes.
   684   /// When using the plain \ref BelmannFord
   685   /// the new class with the modified type comes from
   686   /// the original class by using the ::
   687   /// operator. In the case of \ref BelmannFordWizard only
   688   /// a function have to be called and it will
   689   /// return the needed class.
   690   ///
   691   /// It does not have own \ref run method. When its \ref run method is called
   692   /// it initiates a plain \ref BelmannFord class, and calls the \ref 
   693   /// BelmannFord::run method of it.
   694   template<class _Traits>
   695   class BelmannFordWizard : public _Traits {
   696     typedef _Traits Base;
   697 
   698     ///The type of the underlying graph.
   699     typedef typename _Traits::Graph Graph;
   700 
   701     typedef typename Graph::Node Node;
   702     typedef typename Graph::NodeIt NodeIt;
   703     typedef typename Graph::Edge Edge;
   704     typedef typename Graph::OutEdgeIt EdgeIt;
   705     
   706     ///The type of the map that stores the edge lengths.
   707     typedef typename _Traits::LengthMap LengthMap;
   708 
   709     ///The type of the length of the edges.
   710     typedef typename LengthMap::Value Value;
   711 
   712     ///\brief The type of the map that stores the last
   713     ///edges of the shortest paths.
   714     typedef typename _Traits::PredMap PredMap;
   715 
   716     ///The type of the map that stores the dists of the nodes.
   717     typedef typename _Traits::DistMap DistMap;
   718 
   719   public:
   720     /// Constructor.
   721     BelmannFordWizard() : _Traits() {}
   722 
   723     /// \brief Constructor that requires parameters.
   724     ///
   725     /// Constructor that requires parameters.
   726     /// These parameters will be the default values for the traits class.
   727     BelmannFordWizard(const Graph& graph, const LengthMap& length, 
   728 		      Node source = INVALID) 
   729       : _Traits(graph, length, source) {}
   730 
   731     /// \brief Copy constructor
   732     BelmannFordWizard(const _Traits &b) : _Traits(b) {}
   733 
   734     ~BelmannFordWizard() {}
   735 
   736     /// \brief Runs BelmannFord algorithm from a given node.
   737     ///    
   738     /// Runs BelmannFord algorithm from a given node.
   739     /// The node can be given by the \ref source function.
   740     void run() {
   741       if(Base::_source == INVALID) throw UninitializedParameter();
   742       BelmannFord<Graph,LengthMap,_Traits> 
   743 	bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
   744       if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
   745       if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
   746       bf.run(Base::_source);
   747     }
   748 
   749     /// \brief Runs BelmannFord algorithm from the given node.
   750     ///
   751     /// Runs BelmannFord algorithm from the given node.
   752     /// \param s is the given source.
   753     void run(Node source) {
   754       Base::_source = source;
   755       run();
   756     }
   757 
   758     template<class T>
   759     struct DefPredMapBase : public Base {
   760       typedef T PredMap;
   761       static PredMap *createPredMap(const Graph &) { return 0; };
   762       DefPredMapBase(const _Traits &b) : _Traits(b) {}
   763     };
   764     
   765     ///\brief \ref named-templ-param "Named parameter"
   766     ///function for setting PredMap type
   767     ///
   768     /// \ref named-templ-param "Named parameter"
   769     ///function for setting PredMap type
   770     ///
   771     template<class T>
   772     BelmannFordWizard<DefPredMapBase<T> > predMap(const T &t) 
   773     {
   774       Base::_pred=(void *)&t;
   775       return BelmannFordWizard<DefPredMapBase<T> >(*this);
   776     }
   777     
   778     template<class T>
   779     struct DefDistMapBase : public Base {
   780       typedef T DistMap;
   781       static DistMap *createDistMap(const Graph &) { return 0; };
   782       DefDistMapBase(const _Traits &b) : _Traits(b) {}
   783     };
   784     
   785     ///\brief \ref named-templ-param "Named parameter"
   786     ///function for setting DistMap type
   787     ///
   788     /// \ref named-templ-param "Named parameter"
   789     ///function for setting DistMap type
   790     ///
   791     template<class T>
   792     BelmannFordWizard<DefDistMapBase<T> > distMap(const T &t) {
   793       Base::_dist=(void *)&t;
   794       return BelmannFordWizard<DefDistMapBase<T> >(*this);
   795     }
   796 
   797     template<class T>
   798     struct DefOperationTraitsBase : public Base {
   799       typedef T OperationTraits;
   800       DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
   801     };
   802     
   803     ///\brief \ref named-templ-param "Named parameter"
   804     ///function for setting OperationTraits type
   805     ///
   806     /// \ref named-templ-param "Named parameter"
   807     ///function for setting OperationTraits type
   808     ///
   809     template<class T>
   810     BelmannFordWizard<DefOperationTraitsBase<T> > distMap() {
   811       return BelmannFordWizard<DefDistMapBase<T> >(*this);
   812     }
   813     
   814     /// \brief Sets the source node, from which the BelmannFord algorithm runs.
   815     ///
   816     /// Sets the source node, from which the BelmannFord algorithm runs.
   817     /// \param s is the source node.
   818     BelmannFordWizard<_Traits>& source(Node source) {
   819       Base::_source = source;
   820       return *this;
   821     }
   822     
   823   };
   824   
   825   /// \brief Function type interface for BelmannFord algorithm.
   826   ///
   827   /// \ingroup flowalgs
   828   /// Function type interface for BelmannFord algorithm.
   829   ///
   830   /// This function also has several \ref named-templ-func-param 
   831   /// "named parameters", they are declared as the members of class 
   832   /// \ref BelmannFordWizard.
   833   /// The following
   834   /// example shows how to use these parameters.
   835   /// \code
   836   /// belmannford(g,length,source).predMap(preds).run();
   837   /// \endcode
   838   /// \warning Don't forget to put the \ref BelmannFordWizard::run() "run()"
   839   /// to the end of the parameter list.
   840   /// \sa BelmannFordWizard
   841   /// \sa BelmannFord
   842   template<class _Graph, class _LengthMap>
   843   BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> >
   844   belmannFord(const _Graph& graph,
   845 	      const _LengthMap& length, 
   846 	      typename _Graph::Node source = INVALID) {
   847     return BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> >
   848       (graph, length, source);
   849   }
   850 
   851 } //END OF NAMESPACE LEMON
   852 
   853 #endif
   854