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