Merge
authorAlpar Juttner <alpar@cs.elte.hu>
Fri, 26 Sep 2008 09:52:28 +0200
changeset 281e9b4fbe163f5
parent 280 e7f8647ce760
parent 279 6307bbbf285b
child 283 66bb22401834
Merge
lemon/bfs.h
lemon/concepts/path.h
lemon/dfs.h
lemon/dijkstra.h
     1.1 --- a/lemon/bfs.h	Mon Jul 14 15:23:11 2008 +0100
     1.2 +++ b/lemon/bfs.h	Fri Sep 26 09:52:28 2008 +0200
     1.3 @@ -28,6 +28,7 @@
     1.4  #include <lemon/core.h>
     1.5  #include <lemon/error.h>
     1.6  #include <lemon/maps.h>
     1.7 +#include <lemon/path.h>
     1.8  
     1.9  namespace lemon {
    1.10  
    1.11 @@ -113,7 +114,7 @@
    1.12    ///\ingroup search
    1.13    ///This class provides an efficient implementation of the %BFS algorithm.
    1.14    ///
    1.15 -  ///There is also a \ref bfs() "function type interface" for the BFS
    1.16 +  ///There is also a \ref bfs() "function-type interface" for the BFS
    1.17    ///algorithm, which is convenient in the simplier cases and it can be
    1.18    ///used easier.
    1.19    ///
    1.20 @@ -838,25 +839,22 @@
    1.21      ///The type of the map that stores the predecessor
    1.22      ///arcs of the shortest paths.
    1.23      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.24 -    typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
    1.25 +    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    1.26      ///Instantiates a \ref PredMap.
    1.27  
    1.28      ///This function instantiates a \ref PredMap.
    1.29      ///\param g is the digraph, to which we would like to define the
    1.30      ///\ref PredMap.
    1.31 -#ifdef DOXYGEN
    1.32      static PredMap *createPredMap(const Digraph &g)
    1.33 -#else
    1.34 -    static PredMap *createPredMap(const Digraph &)
    1.35 -#endif
    1.36      {
    1.37 -      return new PredMap();
    1.38 +      return new PredMap(g);
    1.39      }
    1.40  
    1.41      ///The type of the map that indicates which nodes are processed.
    1.42  
    1.43      ///The type of the map that indicates which nodes are processed.
    1.44      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.45 +    ///By default it is a NullMap.
    1.46      typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    1.47      ///Instantiates a \ref ProcessedMap.
    1.48  
    1.49 @@ -891,21 +889,22 @@
    1.50  
    1.51      ///The type of the map that stores the distances of the nodes.
    1.52      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.53 -    ///
    1.54 -    typedef NullMap<typename Digraph::Node,int> DistMap;
    1.55 +    typedef typename Digraph::template NodeMap<int> DistMap;
    1.56      ///Instantiates a \ref DistMap.
    1.57  
    1.58      ///This function instantiates a \ref DistMap.
    1.59      ///\param g is the digraph, to which we would like to define
    1.60      ///the \ref DistMap
    1.61 -#ifdef DOXYGEN
    1.62      static DistMap *createDistMap(const Digraph &g)
    1.63 -#else
    1.64 -    static DistMap *createDistMap(const Digraph &)
    1.65 -#endif
    1.66      {
    1.67 -      return new DistMap();
    1.68 +      return new DistMap(g);
    1.69      }
    1.70 +
    1.71 +    ///The type of the shortest paths.
    1.72 +
    1.73 +    ///The type of the shortest paths.
    1.74 +    ///It must meet the \ref concepts::Path "Path" concept.
    1.75 +    typedef lemon::Path<Digraph> Path;
    1.76    };
    1.77  
    1.78    /// Default traits class used by \ref BfsWizard
    1.79 @@ -935,51 +934,39 @@
    1.80      void *_pred;
    1.81      //Pointer to the map of distances.
    1.82      void *_dist;
    1.83 -    //Pointer to the source node.
    1.84 -    Node _source;
    1.85 +    //Pointer to the shortest path to the target node.
    1.86 +    void *_path;
    1.87 +    //Pointer to the distance of the target node.
    1.88 +    int *_di;
    1.89  
    1.90      public:
    1.91      /// Constructor.
    1.92  
    1.93      /// This constructor does not require parameters, therefore it initiates
    1.94 -    /// all of the attributes to default values (0, INVALID).
    1.95 +    /// all of the attributes to \c 0.
    1.96      BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
    1.97 -                      _dist(0), _source(INVALID) {}
    1.98 +                      _dist(0), _path(0), _di(0) {}
    1.99  
   1.100      /// Constructor.
   1.101  
   1.102 -    /// This constructor requires some parameters,
   1.103 -    /// listed in the parameters list.
   1.104 -    /// Others are initiated to 0.
   1.105 +    /// This constructor requires one parameter,
   1.106 +    /// others are initiated to \c 0.
   1.107      /// \param g The digraph the algorithm runs on.
   1.108 -    /// \param s The source node.
   1.109 -    BfsWizardBase(const GR &g, Node s=INVALID) :
   1.110 +    BfsWizardBase(const GR &g) :
   1.111        _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   1.112 -      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   1.113 +      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
   1.114  
   1.115    };
   1.116  
   1.117 -  /// Auxiliary class for the function type interface of BFS algorithm.
   1.118 +  /// Auxiliary class for the function-type interface of BFS algorithm.
   1.119  
   1.120 -  /// This auxiliary class is created to implement the function type
   1.121 -  /// interface of \ref Bfs algorithm. It uses the functions and features
   1.122 -  /// of the plain \ref Bfs, but it is much simpler to use it.
   1.123 -  /// It should only be used through the \ref bfs() function, which makes
   1.124 -  /// it easier to use the algorithm.
   1.125 +  /// This auxiliary class is created to implement the
   1.126 +  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
   1.127 +  /// It does not have own \ref run() method, it uses the functions
   1.128 +  /// and features of the plain \ref Bfs.
   1.129    ///
   1.130 -  /// Simplicity means that the way to change the types defined
   1.131 -  /// in the traits class is based on functions that returns the new class
   1.132 -  /// and not on templatable built-in classes.
   1.133 -  /// When using the plain \ref Bfs
   1.134 -  /// the new class with the modified type comes from
   1.135 -  /// the original class by using the ::
   1.136 -  /// operator. In the case of \ref BfsWizard only
   1.137 -  /// a function have to be called, and it will
   1.138 -  /// return the needed class.
   1.139 -  ///
   1.140 -  /// It does not have own \ref run() method. When its \ref run() method
   1.141 -  /// is called, it initiates a plain \ref Bfs object, and calls the
   1.142 -  /// \ref Bfs::run() method of it.
   1.143 +  /// This class should only be used through the \ref bfs() function,
   1.144 +  /// which makes it easier to use the algorithm.
   1.145    template<class TR>
   1.146    class BfsWizard : public TR
   1.147    {
   1.148 @@ -1002,6 +989,8 @@
   1.149      typedef typename TR::ReachedMap ReachedMap;
   1.150      ///\brief The type of the map that indicates which nodes are processed.
   1.151      typedef typename TR::ProcessedMap ProcessedMap;
   1.152 +    ///The type of the shortest paths
   1.153 +    typedef typename TR::Path Path;
   1.154  
   1.155    public:
   1.156  
   1.157 @@ -1012,51 +1001,70 @@
   1.158  
   1.159      /// Constructor that requires parameters.
   1.160      /// These parameters will be the default values for the traits class.
   1.161 -    BfsWizard(const Digraph &g, Node s=INVALID) :
   1.162 -      TR(g,s) {}
   1.163 +    /// \param g The digraph the algorithm runs on.
   1.164 +    BfsWizard(const Digraph &g) :
   1.165 +      TR(g) {}
   1.166  
   1.167      ///Copy constructor
   1.168      BfsWizard(const TR &b) : TR(b) {}
   1.169  
   1.170      ~BfsWizard() {}
   1.171  
   1.172 -    ///Runs BFS algorithm from a source node.
   1.173 +    ///Runs BFS algorithm from the given source node.
   1.174  
   1.175 -    ///Runs BFS algorithm from a source node.
   1.176 -    ///The node can be given with the \ref source() function.
   1.177 +    ///This method runs BFS algorithm from node \c s
   1.178 +    ///in order to compute the shortest path to each node.
   1.179 +    void run(Node s)
   1.180 +    {
   1.181 +      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.182 +      if (Base::_pred)
   1.183 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.184 +      if (Base::_dist)
   1.185 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.186 +      if (Base::_reached)
   1.187 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.188 +      if (Base::_processed)
   1.189 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.190 +      if (s!=INVALID)
   1.191 +        alg.run(s);
   1.192 +      else
   1.193 +        alg.run();
   1.194 +    }
   1.195 +
   1.196 +    ///Finds the shortest path between \c s and \c t.
   1.197 +
   1.198 +    ///This method runs BFS algorithm from node \c s
   1.199 +    ///in order to compute the shortest path to node \c t
   1.200 +    ///(it stops searching when \c t is processed).
   1.201 +    ///
   1.202 +    ///\return \c true if \c t is reachable form \c s.
   1.203 +    bool run(Node s, Node t)
   1.204 +    {
   1.205 +      if (s==INVALID || t==INVALID) throw UninitializedParameter();
   1.206 +      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.207 +      if (Base::_pred)
   1.208 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.209 +      if (Base::_dist)
   1.210 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.211 +      if (Base::_reached)
   1.212 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.213 +      if (Base::_processed)
   1.214 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.215 +      alg.run(s,t);
   1.216 +      if (Base::_path)
   1.217 +        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
   1.218 +      if (Base::_di)
   1.219 +        *Base::_di = alg.dist(t);
   1.220 +      return alg.reached(t);
   1.221 +    }
   1.222 +
   1.223 +    ///Runs BFS algorithm to visit all nodes in the digraph.
   1.224 +
   1.225 +    ///This method runs BFS algorithm in order to compute
   1.226 +    ///the shortest path to each node.
   1.227      void run()
   1.228      {
   1.229 -      if(Base::_source==INVALID) throw UninitializedParameter();
   1.230 -      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.231 -      if(Base::_reached)
   1.232 -        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.233 -      if(Base::_processed)
   1.234 -        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.235 -      if(Base::_pred)
   1.236 -        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.237 -      if(Base::_dist)
   1.238 -        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.239 -      alg.run(Base::_source);
   1.240 -    }
   1.241 -
   1.242 -    ///Runs BFS algorithm from the given node.
   1.243 -
   1.244 -    ///Runs BFS algorithm from the given node.
   1.245 -    ///\param s is the given source.
   1.246 -    void run(Node s)
   1.247 -    {
   1.248 -      Base::_source=s;
   1.249 -      run();
   1.250 -    }
   1.251 -
   1.252 -    /// Sets the source node, from which the Bfs algorithm runs.
   1.253 -
   1.254 -    /// Sets the source node, from which the Bfs algorithm runs.
   1.255 -    /// \param s is the source node.
   1.256 -    BfsWizard<TR> &source(Node s)
   1.257 -    {
   1.258 -      Base::_source=s;
   1.259 -      return *this;
   1.260 +      run(INVALID);
   1.261      }
   1.262  
   1.263      template<class T>
   1.264 @@ -1065,10 +1073,10 @@
   1.265        static PredMap *createPredMap(const Digraph &) { return 0; };
   1.266        SetPredMapBase(const TR &b) : TR(b) {}
   1.267      };
   1.268 -    ///\brief \ref named-templ-param "Named parameter"
   1.269 +    ///\brief \ref named-func-param "Named parameter"
   1.270      ///for setting \ref PredMap object.
   1.271      ///
   1.272 -    /// \ref named-templ-param "Named parameter"
   1.273 +    ///\ref named-func-param "Named parameter"
   1.274      ///for setting \ref PredMap object.
   1.275      template<class T>
   1.276      BfsWizard<SetPredMapBase<T> > predMap(const T &t)
   1.277 @@ -1083,10 +1091,10 @@
   1.278        static ReachedMap *createReachedMap(const Digraph &) { return 0; };
   1.279        SetReachedMapBase(const TR &b) : TR(b) {}
   1.280      };
   1.281 -    ///\brief \ref named-templ-param "Named parameter"
   1.282 +    ///\brief \ref named-func-param "Named parameter"
   1.283      ///for setting \ref ReachedMap object.
   1.284      ///
   1.285 -    /// \ref named-templ-param "Named parameter"
   1.286 +    /// \ref named-func-param "Named parameter"
   1.287      ///for setting \ref ReachedMap object.
   1.288      template<class T>
   1.289      BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
   1.290 @@ -1096,15 +1104,33 @@
   1.291      }
   1.292  
   1.293      template<class T>
   1.294 +    struct SetDistMapBase : public Base {
   1.295 +      typedef T DistMap;
   1.296 +      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.297 +      SetDistMapBase(const TR &b) : TR(b) {}
   1.298 +    };
   1.299 +    ///\brief \ref named-func-param "Named parameter"
   1.300 +    ///for setting \ref DistMap object.
   1.301 +    ///
   1.302 +    /// \ref named-func-param "Named parameter"
   1.303 +    ///for setting \ref DistMap object.
   1.304 +    template<class T>
   1.305 +    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
   1.306 +    {
   1.307 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.308 +      return BfsWizard<SetDistMapBase<T> >(*this);
   1.309 +    }
   1.310 +
   1.311 +    template<class T>
   1.312      struct SetProcessedMapBase : public Base {
   1.313        typedef T ProcessedMap;
   1.314        static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
   1.315        SetProcessedMapBase(const TR &b) : TR(b) {}
   1.316      };
   1.317 -    ///\brief \ref named-templ-param "Named parameter"
   1.318 +    ///\brief \ref named-func-param "Named parameter"
   1.319      ///for setting \ref ProcessedMap object.
   1.320      ///
   1.321 -    /// \ref named-templ-param "Named parameter"
   1.322 +    /// \ref named-func-param "Named parameter"
   1.323      ///for setting \ref ProcessedMap object.
   1.324      template<class T>
   1.325      BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
   1.326 @@ -1114,37 +1140,49 @@
   1.327      }
   1.328  
   1.329      template<class T>
   1.330 -    struct SetDistMapBase : public Base {
   1.331 -      typedef T DistMap;
   1.332 -      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.333 -      SetDistMapBase(const TR &b) : TR(b) {}
   1.334 +    struct SetPathBase : public Base {
   1.335 +      typedef T Path;
   1.336 +      SetPathBase(const TR &b) : TR(b) {}
   1.337      };
   1.338 -    ///\brief \ref named-templ-param "Named parameter"
   1.339 -    ///for setting \ref DistMap object.
   1.340 +    ///\brief \ref named-func-param "Named parameter"
   1.341 +    ///for getting the shortest path to the target node.
   1.342      ///
   1.343 -    /// \ref named-templ-param "Named parameter"
   1.344 -    ///for setting \ref DistMap object.
   1.345 +    ///\ref named-func-param "Named parameter"
   1.346 +    ///for getting the shortest path to the target node.
   1.347      template<class T>
   1.348 -    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
   1.349 +    BfsWizard<SetPathBase<T> > path(const T &t)
   1.350      {
   1.351 -      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.352 -      return BfsWizard<SetDistMapBase<T> >(*this);
   1.353 +      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.354 +      return BfsWizard<SetPathBase<T> >(*this);
   1.355 +    }
   1.356 +
   1.357 +    ///\brief \ref named-func-param "Named parameter"
   1.358 +    ///for getting the distance of the target node.
   1.359 +    ///
   1.360 +    ///\ref named-func-param "Named parameter"
   1.361 +    ///for getting the distance of the target node.
   1.362 +    BfsWizard dist(const int &d)
   1.363 +    {
   1.364 +      Base::_di=const_cast<int*>(&d);
   1.365 +      return *this;
   1.366      }
   1.367  
   1.368    };
   1.369  
   1.370 -  ///Function type interface for Bfs algorithm.
   1.371 +  ///Function-type interface for BFS algorithm.
   1.372  
   1.373    /// \ingroup search
   1.374 -  ///Function type interface for Bfs algorithm.
   1.375 +  ///Function-type interface for BFS algorithm.
   1.376    ///
   1.377 -  ///This function also has several
   1.378 -  ///\ref named-templ-func-param "named parameters",
   1.379 +  ///This function also has several \ref named-func-param "named parameters",
   1.380    ///they are declared as the members of class \ref BfsWizard.
   1.381 -  ///The following
   1.382 -  ///example shows how to use these parameters.
   1.383 +  ///The following examples show how to use these parameters.
   1.384    ///\code
   1.385 -  ///  bfs(g,source).predMap(preds).run();
   1.386 +  ///  // Compute shortest path from node s to each node
   1.387 +  ///  bfs(g).predMap(preds).distMap(dists).run(s);
   1.388 +  ///
   1.389 +  ///  // Compute shortest path from s to t
   1.390 +  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
   1.391    ///\endcode
   1.392    ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
   1.393    ///to the end of the parameter list.
   1.394 @@ -1152,9 +1190,9 @@
   1.395    ///\sa Bfs
   1.396    template<class GR>
   1.397    BfsWizard<BfsWizardBase<GR> >
   1.398 -  bfs(const GR &g,typename GR::Node s=INVALID)
   1.399 +  bfs(const GR &digraph)
   1.400    {
   1.401 -    return BfsWizard<BfsWizardBase<GR> >(g,s);
   1.402 +    return BfsWizard<BfsWizardBase<GR> >(digraph);
   1.403    }
   1.404  
   1.405  #ifdef DOXYGEN
     2.1 --- a/lemon/concepts/path.h	Mon Jul 14 15:23:11 2008 +0100
     2.2 +++ b/lemon/concepts/path.h	Fri Sep 26 09:52:28 2008 +0200
     2.3 @@ -65,7 +65,10 @@
     2.4  
     2.5        /// \brief Template assigment
     2.6        template <typename CPath>
     2.7 -      Path& operator=(const CPath& cpath) {}
     2.8 +      Path& operator=(const CPath& cpath) {
     2.9 +        ignore_unused_variable_warning(cpath);
    2.10 +        return *this;
    2.11 +      }
    2.12  
    2.13        /// Length of the path ie. the number of arcs in the path.
    2.14        int length() const { return 0;}
     3.1 --- a/lemon/dfs.h	Mon Jul 14 15:23:11 2008 +0100
     3.2 +++ b/lemon/dfs.h	Fri Sep 26 09:52:28 2008 +0200
     3.3 @@ -29,6 +29,7 @@
     3.4  #include <lemon/error.h>
     3.5  #include <lemon/assert.h>
     3.6  #include <lemon/maps.h>
     3.7 +#include <lemon/path.h>
     3.8  
     3.9  namespace lemon {
    3.10  
    3.11 @@ -114,7 +115,7 @@
    3.12    ///\ingroup search
    3.13    ///This class provides an efficient implementation of the %DFS algorithm.
    3.14    ///
    3.15 -  ///There is also a \ref dfs() "function type interface" for the DFS
    3.16 +  ///There is also a \ref dfs() "function-type interface" for the DFS
    3.17    ///algorithm, which is convenient in the simplier cases and it can be
    3.18    ///used easier.
    3.19    ///
    3.20 @@ -772,26 +773,22 @@
    3.21      ///The type of the map that stores the predecessor
    3.22      ///arcs of the %DFS paths.
    3.23      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    3.24 -    ///
    3.25 -    typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
    3.26 +    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    3.27      ///Instantiates a \ref PredMap.
    3.28  
    3.29      ///This function instantiates a \ref PredMap.
    3.30      ///\param g is the digraph, to which we would like to define the
    3.31      ///\ref PredMap.
    3.32 -#ifdef DOXYGEN
    3.33      static PredMap *createPredMap(const Digraph &g)
    3.34 -#else
    3.35 -    static PredMap *createPredMap(const Digraph &)
    3.36 -#endif
    3.37      {
    3.38 -      return new PredMap();
    3.39 +      return new PredMap(g);
    3.40      }
    3.41  
    3.42      ///The type of the map that indicates which nodes are processed.
    3.43  
    3.44      ///The type of the map that indicates which nodes are processed.
    3.45      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    3.46 +    ///By default it is a NullMap.
    3.47      typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    3.48      ///Instantiates a \ref ProcessedMap.
    3.49  
    3.50 @@ -826,21 +823,22 @@
    3.51  
    3.52      ///The type of the map that stores the distances of the nodes.
    3.53      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    3.54 -    ///
    3.55 -    typedef NullMap<typename Digraph::Node,int> DistMap;
    3.56 +    typedef typename Digraph::template NodeMap<int> DistMap;
    3.57      ///Instantiates a \ref DistMap.
    3.58  
    3.59      ///This function instantiates a \ref DistMap.
    3.60      ///\param g is the digraph, to which we would like to define
    3.61      ///the \ref DistMap
    3.62 -#ifdef DOXYGEN
    3.63      static DistMap *createDistMap(const Digraph &g)
    3.64 -#else
    3.65 -    static DistMap *createDistMap(const Digraph &)
    3.66 -#endif
    3.67      {
    3.68 -      return new DistMap();
    3.69 +      return new DistMap(g);
    3.70      }
    3.71 +
    3.72 +    ///The type of the DFS paths.
    3.73 +
    3.74 +    ///The type of the DFS paths.
    3.75 +    ///It must meet the \ref concepts::Path "Path" concept.
    3.76 +    typedef lemon::Path<Digraph> Path;
    3.77    };
    3.78  
    3.79    /// Default traits class used by \ref DfsWizard
    3.80 @@ -870,51 +868,39 @@
    3.81      void *_pred;
    3.82      //Pointer to the map of distances.
    3.83      void *_dist;
    3.84 -    //Pointer to the source node.
    3.85 -    Node _source;
    3.86 +    //Pointer to the DFS path to the target node.
    3.87 +    void *_path;
    3.88 +    //Pointer to the distance of the target node.
    3.89 +    int *_di;
    3.90  
    3.91      public:
    3.92      /// Constructor.
    3.93  
    3.94      /// This constructor does not require parameters, therefore it initiates
    3.95 -    /// all of the attributes to default values (0, INVALID).
    3.96 +    /// all of the attributes to \c 0.
    3.97      DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
    3.98 -                      _dist(0), _source(INVALID) {}
    3.99 +                      _dist(0), _path(0), _di(0) {}
   3.100  
   3.101      /// Constructor.
   3.102  
   3.103 -    /// This constructor requires some parameters,
   3.104 -    /// listed in the parameters list.
   3.105 -    /// Others are initiated to 0.
   3.106 +    /// This constructor requires one parameter,
   3.107 +    /// others are initiated to \c 0.
   3.108      /// \param g The digraph the algorithm runs on.
   3.109 -    /// \param s The source node.
   3.110 -    DfsWizardBase(const GR &g, Node s=INVALID) :
   3.111 +    DfsWizardBase(const GR &g) :
   3.112        _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   3.113 -      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   3.114 +      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
   3.115  
   3.116    };
   3.117  
   3.118 -  /// Auxiliary class for the function type interface of DFS algorithm.
   3.119 +  /// Auxiliary class for the function-type interface of DFS algorithm.
   3.120  
   3.121 -  /// This auxiliary class is created to implement the function type
   3.122 -  /// interface of \ref Dfs algorithm. It uses the functions and features
   3.123 -  /// of the plain \ref Dfs, but it is much simpler to use it.
   3.124 -  /// It should only be used through the \ref dfs() function, which makes
   3.125 -  /// it easier to use the algorithm.
   3.126 +  /// This auxiliary class is created to implement the
   3.127 +  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
   3.128 +  /// It does not have own \ref run() method, it uses the functions
   3.129 +  /// and features of the plain \ref Dfs.
   3.130    ///
   3.131 -  /// Simplicity means that the way to change the types defined
   3.132 -  /// in the traits class is based on functions that returns the new class
   3.133 -  /// and not on templatable built-in classes.
   3.134 -  /// When using the plain \ref Dfs
   3.135 -  /// the new class with the modified type comes from
   3.136 -  /// the original class by using the ::
   3.137 -  /// operator. In the case of \ref DfsWizard only
   3.138 -  /// a function have to be called, and it will
   3.139 -  /// return the needed class.
   3.140 -  ///
   3.141 -  /// It does not have own \ref run() method. When its \ref run() method
   3.142 -  /// is called, it initiates a plain \ref Dfs object, and calls the
   3.143 -  /// \ref Dfs::run() method of it.
   3.144 +  /// This class should only be used through the \ref dfs() function,
   3.145 +  /// which makes it easier to use the algorithm.
   3.146    template<class TR>
   3.147    class DfsWizard : public TR
   3.148    {
   3.149 @@ -929,7 +915,7 @@
   3.150      typedef typename Digraph::OutArcIt OutArcIt;
   3.151  
   3.152      ///\brief The type of the map that stores the predecessor
   3.153 -    ///arcs of the shortest paths.
   3.154 +    ///arcs of the DFS paths.
   3.155      typedef typename TR::PredMap PredMap;
   3.156      ///\brief The type of the map that stores the distances of the nodes.
   3.157      typedef typename TR::DistMap DistMap;
   3.158 @@ -937,6 +923,8 @@
   3.159      typedef typename TR::ReachedMap ReachedMap;
   3.160      ///\brief The type of the map that indicates which nodes are processed.
   3.161      typedef typename TR::ProcessedMap ProcessedMap;
   3.162 +    ///The type of the DFS paths
   3.163 +    typedef typename TR::Path Path;
   3.164  
   3.165    public:
   3.166  
   3.167 @@ -947,51 +935,70 @@
   3.168  
   3.169      /// Constructor that requires parameters.
   3.170      /// These parameters will be the default values for the traits class.
   3.171 -    DfsWizard(const Digraph &g, Node s=INVALID) :
   3.172 -      TR(g,s) {}
   3.173 +    /// \param g The digraph the algorithm runs on.
   3.174 +    DfsWizard(const Digraph &g) :
   3.175 +      TR(g) {}
   3.176  
   3.177      ///Copy constructor
   3.178      DfsWizard(const TR &b) : TR(b) {}
   3.179  
   3.180      ~DfsWizard() {}
   3.181  
   3.182 -    ///Runs DFS algorithm from a source node.
   3.183 +    ///Runs DFS algorithm from the given source node.
   3.184  
   3.185 -    ///Runs DFS algorithm from a source node.
   3.186 -    ///The node can be given with the \ref source() function.
   3.187 +    ///This method runs DFS algorithm from node \c s
   3.188 +    ///in order to compute the DFS path to each node.
   3.189 +    void run(Node s)
   3.190 +    {
   3.191 +      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   3.192 +      if (Base::_pred)
   3.193 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   3.194 +      if (Base::_dist)
   3.195 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   3.196 +      if (Base::_reached)
   3.197 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   3.198 +      if (Base::_processed)
   3.199 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   3.200 +      if (s!=INVALID)
   3.201 +        alg.run(s);
   3.202 +      else
   3.203 +        alg.run();
   3.204 +    }
   3.205 +
   3.206 +    ///Finds the DFS path between \c s and \c t.
   3.207 +
   3.208 +    ///This method runs DFS algorithm from node \c s
   3.209 +    ///in order to compute the DFS path to node \c t
   3.210 +    ///(it stops searching when \c t is processed).
   3.211 +    ///
   3.212 +    ///\return \c true if \c t is reachable form \c s.
   3.213 +    bool run(Node s, Node t)
   3.214 +    {
   3.215 +      if (s==INVALID || t==INVALID) throw UninitializedParameter();
   3.216 +      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   3.217 +      if (Base::_pred)
   3.218 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   3.219 +      if (Base::_dist)
   3.220 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   3.221 +      if (Base::_reached)
   3.222 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   3.223 +      if (Base::_processed)
   3.224 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   3.225 +      alg.run(s,t);
   3.226 +      if (Base::_path)
   3.227 +        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
   3.228 +      if (Base::_di)
   3.229 +        *Base::_di = alg.dist(t);
   3.230 +      return alg.reached(t);
   3.231 +      }
   3.232 +
   3.233 +    ///Runs DFS algorithm to visit all nodes in the digraph.
   3.234 +
   3.235 +    ///This method runs DFS algorithm in order to compute
   3.236 +    ///the DFS path to each node.
   3.237      void run()
   3.238      {
   3.239 -      if(Base::_source==INVALID) throw UninitializedParameter();
   3.240 -      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   3.241 -      if(Base::_reached)
   3.242 -        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   3.243 -      if(Base::_processed)
   3.244 -        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   3.245 -      if(Base::_pred)
   3.246 -        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   3.247 -      if(Base::_dist)
   3.248 -        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   3.249 -      alg.run(Base::_source);
   3.250 -    }
   3.251 -
   3.252 -    ///Runs DFS algorithm from the given node.
   3.253 -
   3.254 -    ///Runs DFS algorithm from the given node.
   3.255 -    ///\param s is the given source.
   3.256 -    void run(Node s)
   3.257 -    {
   3.258 -      Base::_source=s;
   3.259 -      run();
   3.260 -    }
   3.261 -
   3.262 -    /// Sets the source node, from which the Dfs algorithm runs.
   3.263 -
   3.264 -    /// Sets the source node, from which the Dfs algorithm runs.
   3.265 -    /// \param s is the source node.
   3.266 -    DfsWizard<TR> &source(Node s)
   3.267 -    {
   3.268 -      Base::_source=s;
   3.269 -      return *this;
   3.270 +      run(INVALID);
   3.271      }
   3.272  
   3.273      template<class T>
   3.274 @@ -1000,10 +1007,10 @@
   3.275        static PredMap *createPredMap(const Digraph &) { return 0; };
   3.276        SetPredMapBase(const TR &b) : TR(b) {}
   3.277      };
   3.278 -    ///\brief \ref named-templ-param "Named parameter"
   3.279 +    ///\brief \ref named-func-param "Named parameter"
   3.280      ///for setting \ref PredMap object.
   3.281      ///
   3.282 -    ///\ref named-templ-param "Named parameter"
   3.283 +    ///\ref named-func-param "Named parameter"
   3.284      ///for setting \ref PredMap object.
   3.285      template<class T>
   3.286      DfsWizard<SetPredMapBase<T> > predMap(const T &t)
   3.287 @@ -1018,10 +1025,10 @@
   3.288        static ReachedMap *createReachedMap(const Digraph &) { return 0; };
   3.289        SetReachedMapBase(const TR &b) : TR(b) {}
   3.290      };
   3.291 -    ///\brief \ref named-templ-param "Named parameter"
   3.292 +    ///\brief \ref named-func-param "Named parameter"
   3.293      ///for setting \ref ReachedMap object.
   3.294      ///
   3.295 -    /// \ref named-templ-param "Named parameter"
   3.296 +    /// \ref named-func-param "Named parameter"
   3.297      ///for setting \ref ReachedMap object.
   3.298      template<class T>
   3.299      DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
   3.300 @@ -1031,15 +1038,33 @@
   3.301      }
   3.302  
   3.303      template<class T>
   3.304 +    struct SetDistMapBase : public Base {
   3.305 +      typedef T DistMap;
   3.306 +      static DistMap *createDistMap(const Digraph &) { return 0; };
   3.307 +      SetDistMapBase(const TR &b) : TR(b) {}
   3.308 +    };
   3.309 +    ///\brief \ref named-func-param "Named parameter"
   3.310 +    ///for setting \ref DistMap object.
   3.311 +    ///
   3.312 +    /// \ref named-func-param "Named parameter"
   3.313 +    ///for setting \ref DistMap object.
   3.314 +    template<class T>
   3.315 +    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
   3.316 +    {
   3.317 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   3.318 +      return DfsWizard<SetDistMapBase<T> >(*this);
   3.319 +    }
   3.320 +
   3.321 +    template<class T>
   3.322      struct SetProcessedMapBase : public Base {
   3.323        typedef T ProcessedMap;
   3.324        static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
   3.325        SetProcessedMapBase(const TR &b) : TR(b) {}
   3.326      };
   3.327 -    ///\brief \ref named-templ-param "Named parameter"
   3.328 +    ///\brief \ref named-func-param "Named parameter"
   3.329      ///for setting \ref ProcessedMap object.
   3.330      ///
   3.331 -    /// \ref named-templ-param "Named parameter"
   3.332 +    /// \ref named-func-param "Named parameter"
   3.333      ///for setting \ref ProcessedMap object.
   3.334      template<class T>
   3.335      DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
   3.336 @@ -1049,47 +1074,60 @@
   3.337      }
   3.338  
   3.339      template<class T>
   3.340 -    struct SetDistMapBase : public Base {
   3.341 -      typedef T DistMap;
   3.342 -      static DistMap *createDistMap(const Digraph &) { return 0; };
   3.343 -      SetDistMapBase(const TR &b) : TR(b) {}
   3.344 +    struct SetPathBase : public Base {
   3.345 +      typedef T Path;
   3.346 +      SetPathBase(const TR &b) : TR(b) {}
   3.347      };
   3.348 -    ///\brief \ref named-templ-param "Named parameter"
   3.349 -    ///for setting \ref DistMap object.
   3.350 +    ///\brief \ref named-func-param "Named parameter"
   3.351 +    ///for getting the DFS path to the target node.
   3.352      ///
   3.353 -    ///\ref named-templ-param "Named parameter"
   3.354 -    ///for setting \ref DistMap object.
   3.355 +    ///\ref named-func-param "Named parameter"
   3.356 +    ///for getting the DFS path to the target node.
   3.357      template<class T>
   3.358 -    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
   3.359 +    DfsWizard<SetPathBase<T> > path(const T &t)
   3.360      {
   3.361 -      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   3.362 -      return DfsWizard<SetDistMapBase<T> >(*this);
   3.363 +      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
   3.364 +      return DfsWizard<SetPathBase<T> >(*this);
   3.365 +    }
   3.366 +
   3.367 +    ///\brief \ref named-func-param "Named parameter"
   3.368 +    ///for getting the distance of the target node.
   3.369 +    ///
   3.370 +    ///\ref named-func-param "Named parameter"
   3.371 +    ///for getting the distance of the target node.
   3.372 +    DfsWizard dist(const int &d)
   3.373 +    {
   3.374 +      Base::_di=const_cast<int*>(&d);
   3.375 +      return *this;
   3.376      }
   3.377  
   3.378    };
   3.379  
   3.380 -  ///Function type interface for Dfs algorithm.
   3.381 +  ///Function-type interface for DFS algorithm.
   3.382  
   3.383    ///\ingroup search
   3.384 -  ///Function type interface for Dfs algorithm.
   3.385 +  ///Function-type interface for DFS algorithm.
   3.386    ///
   3.387 -  ///This function also has several
   3.388 -  ///\ref named-templ-func-param "named parameters",
   3.389 +  ///This function also has several \ref named-func-param "named parameters",
   3.390    ///they are declared as the members of class \ref DfsWizard.
   3.391 -  ///The following
   3.392 -  ///example shows how to use these parameters.
   3.393 +  ///The following examples show how to use these parameters.
   3.394    ///\code
   3.395 -  ///  dfs(g,source).predMap(preds).run();
   3.396 +  ///  // Compute the DFS tree
   3.397 +  ///  dfs(g).predMap(preds).distMap(dists).run(s);
   3.398 +  ///
   3.399 +  ///  // Compute the DFS path from s to t
   3.400 +  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
   3.401    ///\endcode
   3.402 +
   3.403    ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
   3.404    ///to the end of the parameter list.
   3.405    ///\sa DfsWizard
   3.406    ///\sa Dfs
   3.407    template<class GR>
   3.408    DfsWizard<DfsWizardBase<GR> >
   3.409 -  dfs(const GR &g,typename GR::Node s=INVALID)
   3.410 +  dfs(const GR &digraph)
   3.411    {
   3.412 -    return DfsWizard<DfsWizardBase<GR> >(g,s);
   3.413 +    return DfsWizard<DfsWizardBase<GR> >(digraph);
   3.414    }
   3.415  
   3.416  #ifdef DOXYGEN
     4.1 --- a/lemon/dijkstra.h	Mon Jul 14 15:23:11 2008 +0100
     4.2 +++ b/lemon/dijkstra.h	Fri Sep 26 09:52:28 2008 +0200
     4.3 @@ -30,6 +30,7 @@
     4.4  #include <lemon/core.h>
     4.5  #include <lemon/error.h>
     4.6  #include <lemon/maps.h>
     4.7 +#include <lemon/path.h>
     4.8  
     4.9  namespace lemon {
    4.10  
    4.11 @@ -196,7 +197,7 @@
    4.12    ///\ref concepts::ReadMap::Value "Value" of the length map.
    4.13    ///It is also possible to change the underlying priority heap.
    4.14    ///
    4.15 -  ///There is also a \ref dijkstra() "function type interface" for the
    4.16 +  ///There is also a \ref dijkstra() "function-type interface" for the
    4.17    ///%Dijkstra algorithm, which is convenient in the simplier cases and
    4.18    ///it can be used easier.
    4.19    ///
    4.20 @@ -982,19 +983,15 @@
    4.21      ///The type of the map that stores the predecessor
    4.22      ///arcs of the shortest paths.
    4.23      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    4.24 -    typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
    4.25 +    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    4.26      ///Instantiates a \ref PredMap.
    4.27  
    4.28      ///This function instantiates a \ref PredMap.
    4.29      ///\param g is the digraph, to which we would like to define the
    4.30      ///\ref PredMap.
    4.31 -#ifdef DOXYGEN
    4.32      static PredMap *createPredMap(const Digraph &g)
    4.33 -#else
    4.34 -    static PredMap *createPredMap(const Digraph &)
    4.35 -#endif
    4.36      {
    4.37 -      return new PredMap();
    4.38 +      return new PredMap(g);
    4.39      }
    4.40  
    4.41      ///The type of the map that indicates which nodes are processed.
    4.42 @@ -1021,20 +1018,22 @@
    4.43  
    4.44      ///The type of the map that stores the distances of the nodes.
    4.45      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    4.46 -    typedef NullMap<typename Digraph::Node,Value> DistMap;
    4.47 +    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
    4.48      ///Instantiates a \ref DistMap.
    4.49  
    4.50      ///This function instantiates a \ref DistMap.
    4.51      ///\param g is the digraph, to which we would like to define
    4.52      ///the \ref DistMap
    4.53 -#ifdef DOXYGEN
    4.54      static DistMap *createDistMap(const Digraph &g)
    4.55 -#else
    4.56 -    static DistMap *createDistMap(const Digraph &)
    4.57 -#endif
    4.58      {
    4.59 -      return new DistMap();
    4.60 +      return new DistMap(g);
    4.61      }
    4.62 +
    4.63 +    ///The type of the shortest paths.
    4.64 +
    4.65 +    ///The type of the shortest paths.
    4.66 +    ///It must meet the \ref concepts::Path "Path" concept.
    4.67 +    typedef lemon::Path<Digraph> Path;
    4.68    };
    4.69  
    4.70    /// Default traits class used by \ref DijkstraWizard
    4.71 @@ -1055,7 +1054,7 @@
    4.72  
    4.73      //Pointer to the digraph the algorithm runs on.
    4.74      void *_g;
    4.75 -    //Pointer to the length map
    4.76 +    //Pointer to the length map.
    4.77      void *_length;
    4.78      //Pointer to the map of processed nodes.
    4.79      void *_processed;
    4.80 @@ -1063,53 +1062,41 @@
    4.81      void *_pred;
    4.82      //Pointer to the map of distances.
    4.83      void *_dist;
    4.84 -    //Pointer to the source node.
    4.85 -    Node _source;
    4.86 +    //Pointer to the shortest path to the target node.
    4.87 +    void *_path;
    4.88 +    //Pointer to the distance of the target node.
    4.89 +    void *_di;
    4.90  
    4.91    public:
    4.92      /// Constructor.
    4.93  
    4.94      /// This constructor does not require parameters, therefore it initiates
    4.95 -    /// all of the attributes to default values (0, INVALID).
    4.96 +    /// all of the attributes to \c 0.
    4.97      DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
    4.98 -                           _dist(0), _source(INVALID) {}
    4.99 +                           _dist(0), _path(0), _di(0) {}
   4.100  
   4.101      /// Constructor.
   4.102  
   4.103 -    /// This constructor requires some parameters,
   4.104 -    /// listed in the parameters list.
   4.105 -    /// Others are initiated to 0.
   4.106 +    /// This constructor requires two parameters,
   4.107 +    /// others are initiated to \c 0.
   4.108      /// \param g The digraph the algorithm runs on.
   4.109      /// \param l The length map.
   4.110 -    /// \param s The source node.
   4.111 -    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
   4.112 +    DijkstraWizardBase(const GR &g,const LM &l) :
   4.113        _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   4.114        _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
   4.115 -      _processed(0), _pred(0), _dist(0), _source(s) {}
   4.116 +      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
   4.117  
   4.118    };
   4.119  
   4.120 -  /// Auxiliary class for the function type interface of Dijkstra algorithm.
   4.121 +  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
   4.122  
   4.123 -  /// This auxiliary class is created to implement the function type
   4.124 -  /// interface of \ref Dijkstra algorithm. It uses the functions and features
   4.125 -  /// of the plain \ref Dijkstra, but it is much simpler to use it.
   4.126 -  /// It should only be used through the \ref dijkstra() function, which makes
   4.127 -  /// it easier to use the algorithm.
   4.128 +  /// This auxiliary class is created to implement the
   4.129 +  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
   4.130 +  /// It does not have own \ref run() method, it uses the functions
   4.131 +  /// and features of the plain \ref Dijkstra.
   4.132    ///
   4.133 -  /// Simplicity means that the way to change the types defined
   4.134 -  /// in the traits class is based on functions that returns the new class
   4.135 -  /// and not on templatable built-in classes.
   4.136 -  /// When using the plain \ref Dijkstra
   4.137 -  /// the new class with the modified type comes from
   4.138 -  /// the original class by using the ::
   4.139 -  /// operator. In the case of \ref DijkstraWizard only
   4.140 -  /// a function have to be called, and it will
   4.141 -  /// return the needed class.
   4.142 -  ///
   4.143 -  /// It does not have own \ref run() method. When its \ref run() method
   4.144 -  /// is called, it initiates a plain \ref Dijkstra object, and calls the
   4.145 -  /// \ref Dijkstra::run() method of it.
   4.146 +  /// This class should only be used through the \ref dijkstra() function,
   4.147 +  /// which makes it easier to use the algorithm.
   4.148    template<class TR>
   4.149    class DijkstraWizard : public TR
   4.150    {
   4.151 @@ -1134,6 +1121,8 @@
   4.152      typedef typename TR::DistMap DistMap;
   4.153      ///The type of the map that indicates which nodes are processed.
   4.154      typedef typename TR::ProcessedMap ProcessedMap;
   4.155 +    ///The type of the shortest paths
   4.156 +    typedef typename TR::Path Path;
   4.157      ///The heap type used by the dijkstra algorithm.
   4.158      typedef typename TR::Heap Heap;
   4.159  
   4.160 @@ -1146,51 +1135,60 @@
   4.161  
   4.162      /// Constructor that requires parameters.
   4.163      /// These parameters will be the default values for the traits class.
   4.164 -    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
   4.165 -      TR(g,l,s) {}
   4.166 +    /// \param g The digraph the algorithm runs on.
   4.167 +    /// \param l The length map.
   4.168 +    DijkstraWizard(const Digraph &g, const LengthMap &l) :
   4.169 +      TR(g,l) {}
   4.170  
   4.171      ///Copy constructor
   4.172      DijkstraWizard(const TR &b) : TR(b) {}
   4.173  
   4.174      ~DijkstraWizard() {}
   4.175  
   4.176 -    ///Runs Dijkstra algorithm from a source node.
   4.177 +    ///Runs Dijkstra algorithm from the given source node.
   4.178  
   4.179 -    ///Runs Dijkstra algorithm from a source node.
   4.180 -    ///The node can be given with the \ref source() function.
   4.181 -    void run()
   4.182 +    ///This method runs %Dijkstra algorithm from the given source node
   4.183 +    ///in order to compute the shortest path to each node.
   4.184 +    void run(Node s)
   4.185      {
   4.186 -      if(Base::_source==INVALID) throw UninitializedParameter();
   4.187 +      if (s==INVALID) throw UninitializedParameter();
   4.188        Dijkstra<Digraph,LengthMap,TR>
   4.189 -        dij(*reinterpret_cast<const Digraph*>(Base::_g),
   4.190 -            *reinterpret_cast<const LengthMap*>(Base::_length));
   4.191 -      if(Base::_processed)
   4.192 -        dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   4.193 -      if(Base::_pred)
   4.194 -        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   4.195 -      if(Base::_dist)
   4.196 -        dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   4.197 -      dij.run(Base::_source);
   4.198 +        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
   4.199 +             *reinterpret_cast<const LengthMap*>(Base::_length));
   4.200 +      if (Base::_pred)
   4.201 +        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   4.202 +      if (Base::_dist)
   4.203 +        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   4.204 +      if (Base::_processed)
   4.205 +        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   4.206 +      dijk.run(s);
   4.207      }
   4.208  
   4.209 -    ///Runs Dijkstra algorithm from the given node.
   4.210 +    ///Finds the shortest path between \c s and \c t.
   4.211  
   4.212 -    ///Runs Dijkstra algorithm from the given node.
   4.213 -    ///\param s is the given source.
   4.214 -    void run(Node s)
   4.215 +    ///This method runs the %Dijkstra algorithm from node \c s
   4.216 +    ///in order to compute the shortest path to node \c t
   4.217 +    ///(it stops searching when \c t is processed).
   4.218 +    ///
   4.219 +    ///\return \c true if \c t is reachable form \c s.
   4.220 +    bool run(Node s, Node t)
   4.221      {
   4.222 -      Base::_source=s;
   4.223 -      run();
   4.224 -    }
   4.225 -
   4.226 -    /// Sets the source node, from which the Dijkstra algorithm runs.
   4.227 -
   4.228 -    /// Sets the source node, from which the Dijkstra algorithm runs.
   4.229 -    /// \param s is the source node.
   4.230 -    DijkstraWizard<TR> &source(Node s)
   4.231 -    {
   4.232 -      Base::_source=s;
   4.233 -      return *this;
   4.234 +      if (s==INVALID || t==INVALID) throw UninitializedParameter();
   4.235 +      Dijkstra<Digraph,LengthMap,TR>
   4.236 +        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
   4.237 +             *reinterpret_cast<const LengthMap*>(Base::_length));
   4.238 +      if (Base::_pred)
   4.239 +        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   4.240 +      if (Base::_dist)
   4.241 +        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   4.242 +      if (Base::_processed)
   4.243 +        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   4.244 +      dijk.run(s,t);
   4.245 +      if (Base::_path)
   4.246 +        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
   4.247 +      if (Base::_di)
   4.248 +        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
   4.249 +      return dijk.reached(t);
   4.250      }
   4.251  
   4.252      template<class T>
   4.253 @@ -1199,10 +1197,10 @@
   4.254        static PredMap *createPredMap(const Digraph &) { return 0; };
   4.255        SetPredMapBase(const TR &b) : TR(b) {}
   4.256      };
   4.257 -    ///\brief \ref named-templ-param "Named parameter"
   4.258 +    ///\brief \ref named-func-param "Named parameter"
   4.259      ///for setting \ref PredMap object.
   4.260      ///
   4.261 -    ///\ref named-templ-param "Named parameter"
   4.262 +    ///\ref named-func-param "Named parameter"
   4.263      ///for setting \ref PredMap object.
   4.264      template<class T>
   4.265      DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
   4.266 @@ -1212,15 +1210,33 @@
   4.267      }
   4.268  
   4.269      template<class T>
   4.270 +    struct SetDistMapBase : public Base {
   4.271 +      typedef T DistMap;
   4.272 +      static DistMap *createDistMap(const Digraph &) { return 0; };
   4.273 +      SetDistMapBase(const TR &b) : TR(b) {}
   4.274 +    };
   4.275 +    ///\brief \ref named-func-param "Named parameter"
   4.276 +    ///for setting \ref DistMap object.
   4.277 +    ///
   4.278 +    ///\ref named-func-param "Named parameter"
   4.279 +    ///for setting \ref DistMap object.
   4.280 +    template<class T>
   4.281 +    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
   4.282 +    {
   4.283 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   4.284 +      return DijkstraWizard<SetDistMapBase<T> >(*this);
   4.285 +    }
   4.286 +
   4.287 +    template<class T>
   4.288      struct SetProcessedMapBase : public Base {
   4.289        typedef T ProcessedMap;
   4.290        static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
   4.291        SetProcessedMapBase(const TR &b) : TR(b) {}
   4.292      };
   4.293 -    ///\brief \ref named-templ-param "Named parameter"
   4.294 +    ///\brief \ref named-func-param "Named parameter"
   4.295      ///for setting \ref ProcessedMap object.
   4.296      ///
   4.297 -    /// \ref named-templ-param "Named parameter"
   4.298 +    /// \ref named-func-param "Named parameter"
   4.299      ///for setting \ref ProcessedMap object.
   4.300      template<class T>
   4.301      DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
   4.302 @@ -1230,37 +1246,49 @@
   4.303      }
   4.304  
   4.305      template<class T>
   4.306 -    struct SetDistMapBase : public Base {
   4.307 -      typedef T DistMap;
   4.308 -      static DistMap *createDistMap(const Digraph &) { return 0; };
   4.309 -      SetDistMapBase(const TR &b) : TR(b) {}
   4.310 +    struct SetPathBase : public Base {
   4.311 +      typedef T Path;
   4.312 +      SetPathBase(const TR &b) : TR(b) {}
   4.313      };
   4.314 -    ///\brief \ref named-templ-param "Named parameter"
   4.315 -    ///for setting \ref DistMap object.
   4.316 +    ///\brief \ref named-func-param "Named parameter"
   4.317 +    ///for getting the shortest path to the target node.
   4.318      ///
   4.319 -    ///\ref named-templ-param "Named parameter"
   4.320 -    ///for setting \ref DistMap object.
   4.321 +    ///\ref named-func-param "Named parameter"
   4.322 +    ///for getting the shortest path to the target node.
   4.323      template<class T>
   4.324 -    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
   4.325 +    DijkstraWizard<SetPathBase<T> > path(const T &t)
   4.326      {
   4.327 -      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   4.328 -      return DijkstraWizard<SetDistMapBase<T> >(*this);
   4.329 +      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
   4.330 +      return DijkstraWizard<SetPathBase<T> >(*this);
   4.331 +    }
   4.332 +
   4.333 +    ///\brief \ref named-func-param "Named parameter"
   4.334 +    ///for getting the distance of the target node.
   4.335 +    ///
   4.336 +    ///\ref named-func-param "Named parameter"
   4.337 +    ///for getting the distance of the target node.
   4.338 +    DijkstraWizard dist(const Value &d)
   4.339 +    {
   4.340 +      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
   4.341 +      return *this;
   4.342      }
   4.343  
   4.344    };
   4.345  
   4.346 -  ///Function type interface for Dijkstra algorithm.
   4.347 +  ///Function-type interface for Dijkstra algorithm.
   4.348  
   4.349    /// \ingroup shortest_path
   4.350 -  ///Function type interface for Dijkstra algorithm.
   4.351 +  ///Function-type interface for Dijkstra algorithm.
   4.352    ///
   4.353 -  ///This function also has several
   4.354 -  ///\ref named-templ-func-param "named parameters",
   4.355 +  ///This function also has several \ref named-func-param "named parameters",
   4.356    ///they are declared as the members of class \ref DijkstraWizard.
   4.357 -  ///The following
   4.358 -  ///example shows how to use these parameters.
   4.359 +  ///The following examples show how to use these parameters.
   4.360    ///\code
   4.361 -  ///  dijkstra(g,length,source).predMap(preds).run();
   4.362 +  ///  // Compute shortest path from node s to each node
   4.363 +  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
   4.364 +  ///
   4.365 +  ///  // Compute shortest path from s to t
   4.366 +  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
   4.367    ///\endcode
   4.368    ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
   4.369    ///to the end of the parameter list.
   4.370 @@ -1268,9 +1296,9 @@
   4.371    ///\sa Dijkstra
   4.372    template<class GR, class LM>
   4.373    DijkstraWizard<DijkstraWizardBase<GR,LM> >
   4.374 -  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
   4.375 +  dijkstra(const GR &digraph, const LM &length)
   4.376    {
   4.377 -    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
   4.378 +    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
   4.379    }
   4.380  
   4.381  } //END OF NAMESPACE LEMON
     5.1 --- a/test/bfs_test.cc	Mon Jul 14 15:23:11 2008 +0100
     5.2 +++ b/test/bfs_test.cc	Fri Sep 26 09:52:28 2008 +0200
     5.3 @@ -62,7 +62,6 @@
     5.4    bool b;
     5.5    BType::DistMap d(G);
     5.6    BType::PredMap p(G);
     5.7 -  //  BType::PredNodeMap pn(G);
     5.8  
     5.9    BType bfs_test(G);
    5.10  
    5.11 @@ -72,9 +71,7 @@
    5.12    e  = bfs_test.predArc(n);
    5.13    n  = bfs_test.predNode(n);
    5.14    d  = bfs_test.distMap();
    5.15 -
    5.16    p  = bfs_test.predMap();
    5.17 -  //  pn = bfs_test.predNodeMap();
    5.18    b  = bfs_test.reached(n);
    5.19  
    5.20    Path<Digraph> pp = bfs_test.path(n);
    5.21 @@ -88,14 +85,30 @@
    5.22    typedef Digraph::Node Node;
    5.23  
    5.24    Digraph g;
    5.25 -  bfs(g,Node()).run();
    5.26 -  bfs(g).source(Node()).run();
    5.27 +  bool b;
    5.28 +  bfs(g).run(Node());
    5.29 +  b=bfs(g).run(Node(),Node());
    5.30 +  bfs(g).run();
    5.31    bfs(g)
    5.32 -    .predMap(concepts::WriteMap<Node,Arc>())
    5.33 -    .distMap(concepts::WriteMap<Node,VType>())
    5.34 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    5.35 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    5.36      .reachedMap(concepts::ReadWriteMap<Node,bool>())
    5.37      .processedMap(concepts::WriteMap<Node,bool>())
    5.38      .run(Node());
    5.39 +  b=bfs(g)
    5.40 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    5.41 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    5.42 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
    5.43 +    .processedMap(concepts::WriteMap<Node,bool>())
    5.44 +    .path(concepts::Path<Digraph>())
    5.45 +    .dist(VType())
    5.46 +    .run(Node(),Node());
    5.47 +  bfs(g)
    5.48 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    5.49 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    5.50 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
    5.51 +    .processedMap(concepts::WriteMap<Node,bool>())
    5.52 +    .run();
    5.53  }
    5.54  
    5.55  template <class Digraph>
    5.56 @@ -114,7 +127,7 @@
    5.57    Bfs<Digraph> bfs_test(G);
    5.58    bfs_test.run(s);
    5.59  
    5.60 -  check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));
    5.61 +  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
    5.62  
    5.63    Path<Digraph> p = bfs_test.path(t);
    5.64    check(p.length()==2,"path() found a wrong path.");
    5.65 @@ -128,7 +141,7 @@
    5.66      Node v=G.target(a);
    5.67      check( !bfs_test.reached(u) ||
    5.68             (bfs_test.dist(v) <= bfs_test.dist(u)+1),
    5.69 -           "Wrong output." << G.id(v) << ' ' << G.id(u));
    5.70 +           "Wrong output. " << G.id(u) << "->" << G.id(v));
    5.71    }
    5.72  
    5.73    for(NodeIt v(G); v!=INVALID; ++v) {
    5.74 @@ -140,11 +153,15 @@
    5.75          check(u==bfs_test.predNode(v),"Wrong tree.");
    5.76          check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
    5.77                "Wrong distance. Difference: "
    5.78 -              << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
    5.79 -                          - 1));
    5.80 +              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
    5.81        }
    5.82      }
    5.83    }
    5.84 +
    5.85 +  {
    5.86 +    NullMap<Node,Arc> myPredMap;
    5.87 +    bfs(G).predMap(myPredMap).run(s);
    5.88 +  }
    5.89  }
    5.90  
    5.91  int main()
     6.1 --- a/test/dfs_test.cc	Mon Jul 14 15:23:11 2008 +0100
     6.2 +++ b/test/dfs_test.cc	Fri Sep 26 09:52:28 2008 +0200
     6.3 @@ -20,7 +20,6 @@
     6.4  #include <lemon/smart_graph.h>
     6.5  #include <lemon/list_graph.h>
     6.6  #include <lemon/lgf_reader.h>
     6.7 -
     6.8  #include <lemon/dfs.h>
     6.9  #include <lemon/path.h>
    6.10  
    6.11 @@ -88,14 +87,30 @@
    6.12    typedef Digraph::Node Node;
    6.13  
    6.14    Digraph g;
    6.15 -  dfs(g,Node()).run();
    6.16 -  dfs(g).source(Node()).run();
    6.17 +  bool b;
    6.18 +  dfs(g).run(Node());
    6.19 +  b=dfs(g).run(Node(),Node());
    6.20 +  dfs(g).run();
    6.21    dfs(g)
    6.22 -    .predMap(concepts::WriteMap<Node,Arc>())
    6.23 -    .distMap(concepts::WriteMap<Node,VType>())
    6.24 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    6.25 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    6.26      .reachedMap(concepts::ReadWriteMap<Node,bool>())
    6.27      .processedMap(concepts::WriteMap<Node,bool>())
    6.28      .run(Node());
    6.29 +  b=dfs(g)
    6.30 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    6.31 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    6.32 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
    6.33 +    .processedMap(concepts::WriteMap<Node,bool>())
    6.34 +    .path(concepts::Path<Digraph>())
    6.35 +    .dist(VType())
    6.36 +    .run(Node(),Node());
    6.37 +  dfs(g)
    6.38 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    6.39 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    6.40 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
    6.41 +    .processedMap(concepts::WriteMap<Node,bool>())
    6.42 +    .run();
    6.43  }
    6.44  
    6.45  template <class Digraph>
    6.46 @@ -129,10 +144,15 @@
    6.47          check(u==dfs_test.predNode(v),"Wrong tree.");
    6.48          check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
    6.49                "Wrong distance. (" << dfs_test.dist(u) << "->"
    6.50 -              <<dfs_test.dist(v) << ')');
    6.51 +              << dfs_test.dist(v) << ")");
    6.52        }
    6.53      }
    6.54    }
    6.55 +
    6.56 +  {
    6.57 +    NullMap<Node,Arc> myPredMap;
    6.58 +    dfs(G).predMap(myPredMap).run(s);
    6.59 +  }
    6.60  }
    6.61  
    6.62  int main()
     7.1 --- a/test/dijkstra_test.cc	Mon Jul 14 15:23:11 2008 +0100
     7.2 +++ b/test/dijkstra_test.cc	Fri Sep 26 09:52:28 2008 +0200
     7.3 @@ -20,7 +20,6 @@
     7.4  #include <lemon/smart_graph.h>
     7.5  #include <lemon/list_graph.h>
     7.6  #include <lemon/lgf_reader.h>
     7.7 -
     7.8  #include <lemon/dijkstra.h>
     7.9  #include <lemon/path.h>
    7.10  
    7.11 @@ -64,7 +63,6 @@
    7.12    bool b;
    7.13    DType::DistMap d(G);
    7.14    DType::PredMap p(G);
    7.15 -  //  DType::PredNodeMap pn(G);
    7.16    LengthMap length;
    7.17  
    7.18    DType dijkstra_test(G,length);
    7.19 @@ -76,7 +74,6 @@
    7.20    n  = dijkstra_test.predNode(n);
    7.21    d  = dijkstra_test.distMap();
    7.22    p  = dijkstra_test.predMap();
    7.23 -  //  pn = dijkstra_test.predNodeMap();
    7.24    b  = dijkstra_test.reached(n);
    7.25  
    7.26    Path<Digraph> pp = dijkstra_test.path(n);
    7.27 @@ -91,12 +88,21 @@
    7.28    typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
    7.29  
    7.30    Digraph g;
    7.31 -  dijkstra(g,LengthMap(),Node()).run();
    7.32 -  dijkstra(g,LengthMap()).source(Node()).run();
    7.33 +  bool b;
    7.34 +  dijkstra(g,LengthMap()).run(Node());
    7.35 +  b=dijkstra(g,LengthMap()).run(Node(),Node());
    7.36    dijkstra(g,LengthMap())
    7.37 -    .predMap(concepts::WriteMap<Node,Arc>())
    7.38 -    .distMap(concepts::WriteMap<Node,VType>())
    7.39 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    7.40 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    7.41 +    .processedMap(concepts::WriteMap<Node,bool>())
    7.42      .run(Node());
    7.43 +  b=dijkstra(g,LengthMap())
    7.44 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
    7.45 +    .distMap(concepts::ReadWriteMap<Node,VType>())
    7.46 +    .processedMap(concepts::WriteMap<Node,bool>())
    7.47 +    .path(concepts::Path<Digraph>())
    7.48 +    .dist(VType())
    7.49 +    .run(Node(),Node());
    7.50  }
    7.51  
    7.52  template <class Digraph>
    7.53 @@ -122,7 +128,7 @@
    7.54    check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
    7.55  
    7.56    Path<Digraph> p = dijkstra_test.path(t);
    7.57 -  check(p.length()==3,"getPath() found a wrong path.");
    7.58 +  check(p.length()==3,"path() found a wrong path.");
    7.59    check(checkPath(G, p),"path() found a wrong path.");
    7.60    check(pathSource(G, p) == s,"path() found a wrong path.");
    7.61    check(pathTarget(G, p) == t,"path() found a wrong path.");
    7.62 @@ -132,7 +138,7 @@
    7.63      Node v=G.target(e);
    7.64      check( !dijkstra_test.reached(u) ||
    7.65             (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
    7.66 -           "dist(target)-dist(source)-arc_length= " <<
    7.67 +           "Wrong output. dist(target)-dist(source)-arc_length=" <<
    7.68             dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
    7.69    }
    7.70