lemon/dijkstra.h
changeset 281 e9b4fbe163f5
parent 280 e7f8647ce760
parent 278 931190050520
child 287 bb40b6db0a58
     1.1 --- a/lemon/dijkstra.h	Mon Jul 14 15:23:11 2008 +0100
     1.2 +++ b/lemon/dijkstra.h	Fri Sep 26 09:52:28 2008 +0200
     1.3 @@ -30,6 +30,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 @@ -196,7 +197,7 @@
    1.12    ///\ref concepts::ReadMap::Value "Value" of the length map.
    1.13    ///It is also possible to change the underlying priority heap.
    1.14    ///
    1.15 -  ///There is also a \ref dijkstra() "function type interface" for the
    1.16 +  ///There is also a \ref dijkstra() "function-type interface" for the
    1.17    ///%Dijkstra algorithm, which is convenient in the simplier cases and
    1.18    ///it can be used easier.
    1.19    ///
    1.20 @@ -982,19 +983,15 @@
    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 @@ -1021,20 +1018,22 @@
    1.43  
    1.44      ///The type of the map that stores the distances of the nodes.
    1.45      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.46 -    typedef NullMap<typename Digraph::Node,Value> DistMap;
    1.47 +    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
    1.48      ///Instantiates a \ref DistMap.
    1.49  
    1.50      ///This function instantiates a \ref DistMap.
    1.51      ///\param g is the digraph, to which we would like to define
    1.52      ///the \ref DistMap
    1.53 -#ifdef DOXYGEN
    1.54      static DistMap *createDistMap(const Digraph &g)
    1.55 -#else
    1.56 -    static DistMap *createDistMap(const Digraph &)
    1.57 -#endif
    1.58      {
    1.59 -      return new DistMap();
    1.60 +      return new DistMap(g);
    1.61      }
    1.62 +
    1.63 +    ///The type of the shortest paths.
    1.64 +
    1.65 +    ///The type of the shortest paths.
    1.66 +    ///It must meet the \ref concepts::Path "Path" concept.
    1.67 +    typedef lemon::Path<Digraph> Path;
    1.68    };
    1.69  
    1.70    /// Default traits class used by \ref DijkstraWizard
    1.71 @@ -1055,7 +1054,7 @@
    1.72  
    1.73      //Pointer to the digraph the algorithm runs on.
    1.74      void *_g;
    1.75 -    //Pointer to the length map
    1.76 +    //Pointer to the length map.
    1.77      void *_length;
    1.78      //Pointer to the map of processed nodes.
    1.79      void *_processed;
    1.80 @@ -1063,53 +1062,41 @@
    1.81      void *_pred;
    1.82      //Pointer to the map of distances.
    1.83      void *_dist;
    1.84 -    //Pointer to the source node.
    1.85 -    Node _source;
    1.86 +    //Pointer to the shortest path to the target node.
    1.87 +    void *_path;
    1.88 +    //Pointer to the distance of the target node.
    1.89 +    void *_di;
    1.90  
    1.91    public:
    1.92      /// Constructor.
    1.93  
    1.94      /// This constructor does not require parameters, therefore it initiates
    1.95 -    /// all of the attributes to default values (0, INVALID).
    1.96 +    /// all of the attributes to \c 0.
    1.97      DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
    1.98 -                           _dist(0), _source(INVALID) {}
    1.99 +                           _dist(0), _path(0), _di(0) {}
   1.100  
   1.101      /// Constructor.
   1.102  
   1.103 -    /// This constructor requires some parameters,
   1.104 -    /// listed in the parameters list.
   1.105 -    /// Others are initiated to 0.
   1.106 +    /// This constructor requires two parameters,
   1.107 +    /// others are initiated to \c 0.
   1.108      /// \param g The digraph the algorithm runs on.
   1.109      /// \param l The length map.
   1.110 -    /// \param s The source node.
   1.111 -    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
   1.112 +    DijkstraWizardBase(const GR &g,const LM &l) :
   1.113        _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   1.114        _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
   1.115 -      _processed(0), _pred(0), _dist(0), _source(s) {}
   1.116 +      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
   1.117  
   1.118    };
   1.119  
   1.120 -  /// Auxiliary class for the function type interface of Dijkstra algorithm.
   1.121 +  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
   1.122  
   1.123 -  /// This auxiliary class is created to implement the function type
   1.124 -  /// interface of \ref Dijkstra algorithm. It uses the functions and features
   1.125 -  /// of the plain \ref Dijkstra, but it is much simpler to use it.
   1.126 -  /// It should only be used through the \ref dijkstra() function, which makes
   1.127 -  /// it easier to use the algorithm.
   1.128 +  /// This auxiliary class is created to implement the
   1.129 +  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
   1.130 +  /// It does not have own \ref run() method, it uses the functions
   1.131 +  /// and features of the plain \ref Dijkstra.
   1.132    ///
   1.133 -  /// Simplicity means that the way to change the types defined
   1.134 -  /// in the traits class is based on functions that returns the new class
   1.135 -  /// and not on templatable built-in classes.
   1.136 -  /// When using the plain \ref Dijkstra
   1.137 -  /// the new class with the modified type comes from
   1.138 -  /// the original class by using the ::
   1.139 -  /// operator. In the case of \ref DijkstraWizard only
   1.140 -  /// a function have to be called, and it will
   1.141 -  /// return the needed class.
   1.142 -  ///
   1.143 -  /// It does not have own \ref run() method. When its \ref run() method
   1.144 -  /// is called, it initiates a plain \ref Dijkstra object, and calls the
   1.145 -  /// \ref Dijkstra::run() method of it.
   1.146 +  /// This class should only be used through the \ref dijkstra() function,
   1.147 +  /// which makes it easier to use the algorithm.
   1.148    template<class TR>
   1.149    class DijkstraWizard : public TR
   1.150    {
   1.151 @@ -1134,6 +1121,8 @@
   1.152      typedef typename TR::DistMap DistMap;
   1.153      ///The type of the map that indicates which nodes are processed.
   1.154      typedef typename TR::ProcessedMap ProcessedMap;
   1.155 +    ///The type of the shortest paths
   1.156 +    typedef typename TR::Path Path;
   1.157      ///The heap type used by the dijkstra algorithm.
   1.158      typedef typename TR::Heap Heap;
   1.159  
   1.160 @@ -1146,51 +1135,60 @@
   1.161  
   1.162      /// Constructor that requires parameters.
   1.163      /// These parameters will be the default values for the traits class.
   1.164 -    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
   1.165 -      TR(g,l,s) {}
   1.166 +    /// \param g The digraph the algorithm runs on.
   1.167 +    /// \param l The length map.
   1.168 +    DijkstraWizard(const Digraph &g, const LengthMap &l) :
   1.169 +      TR(g,l) {}
   1.170  
   1.171      ///Copy constructor
   1.172      DijkstraWizard(const TR &b) : TR(b) {}
   1.173  
   1.174      ~DijkstraWizard() {}
   1.175  
   1.176 -    ///Runs Dijkstra algorithm from a source node.
   1.177 +    ///Runs Dijkstra algorithm from the given source node.
   1.178  
   1.179 -    ///Runs Dijkstra algorithm from a source node.
   1.180 -    ///The node can be given with the \ref source() function.
   1.181 -    void run()
   1.182 +    ///This method runs %Dijkstra algorithm from the given source node
   1.183 +    ///in order to compute the shortest path to each node.
   1.184 +    void run(Node s)
   1.185      {
   1.186 -      if(Base::_source==INVALID) throw UninitializedParameter();
   1.187 +      if (s==INVALID) throw UninitializedParameter();
   1.188        Dijkstra<Digraph,LengthMap,TR>
   1.189 -        dij(*reinterpret_cast<const Digraph*>(Base::_g),
   1.190 -            *reinterpret_cast<const LengthMap*>(Base::_length));
   1.191 -      if(Base::_processed)
   1.192 -        dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.193 -      if(Base::_pred)
   1.194 -        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.195 -      if(Base::_dist)
   1.196 -        dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.197 -      dij.run(Base::_source);
   1.198 +        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
   1.199 +             *reinterpret_cast<const LengthMap*>(Base::_length));
   1.200 +      if (Base::_pred)
   1.201 +        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.202 +      if (Base::_dist)
   1.203 +        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.204 +      if (Base::_processed)
   1.205 +        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.206 +      dijk.run(s);
   1.207      }
   1.208  
   1.209 -    ///Runs Dijkstra algorithm from the given node.
   1.210 +    ///Finds the shortest path between \c s and \c t.
   1.211  
   1.212 -    ///Runs Dijkstra algorithm from the given node.
   1.213 -    ///\param s is the given source.
   1.214 -    void run(Node s)
   1.215 +    ///This method runs the %Dijkstra algorithm from node \c s
   1.216 +    ///in order to compute the shortest path to node \c t
   1.217 +    ///(it stops searching when \c t is processed).
   1.218 +    ///
   1.219 +    ///\return \c true if \c t is reachable form \c s.
   1.220 +    bool run(Node s, Node t)
   1.221      {
   1.222 -      Base::_source=s;
   1.223 -      run();
   1.224 -    }
   1.225 -
   1.226 -    /// Sets the source node, from which the Dijkstra algorithm runs.
   1.227 -
   1.228 -    /// Sets the source node, from which the Dijkstra algorithm runs.
   1.229 -    /// \param s is the source node.
   1.230 -    DijkstraWizard<TR> &source(Node s)
   1.231 -    {
   1.232 -      Base::_source=s;
   1.233 -      return *this;
   1.234 +      if (s==INVALID || t==INVALID) throw UninitializedParameter();
   1.235 +      Dijkstra<Digraph,LengthMap,TR>
   1.236 +        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
   1.237 +             *reinterpret_cast<const LengthMap*>(Base::_length));
   1.238 +      if (Base::_pred)
   1.239 +        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.240 +      if (Base::_dist)
   1.241 +        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.242 +      if (Base::_processed)
   1.243 +        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.244 +      dijk.run(s,t);
   1.245 +      if (Base::_path)
   1.246 +        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
   1.247 +      if (Base::_di)
   1.248 +        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
   1.249 +      return dijk.reached(t);
   1.250      }
   1.251  
   1.252      template<class T>
   1.253 @@ -1199,10 +1197,10 @@
   1.254        static PredMap *createPredMap(const Digraph &) { return 0; };
   1.255        SetPredMapBase(const TR &b) : TR(b) {}
   1.256      };
   1.257 -    ///\brief \ref named-templ-param "Named parameter"
   1.258 +    ///\brief \ref named-func-param "Named parameter"
   1.259      ///for setting \ref PredMap object.
   1.260      ///
   1.261 -    ///\ref named-templ-param "Named parameter"
   1.262 +    ///\ref named-func-param "Named parameter"
   1.263      ///for setting \ref PredMap object.
   1.264      template<class T>
   1.265      DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
   1.266 @@ -1212,15 +1210,33 @@
   1.267      }
   1.268  
   1.269      template<class T>
   1.270 +    struct SetDistMapBase : public Base {
   1.271 +      typedef T DistMap;
   1.272 +      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.273 +      SetDistMapBase(const TR &b) : TR(b) {}
   1.274 +    };
   1.275 +    ///\brief \ref named-func-param "Named parameter"
   1.276 +    ///for setting \ref DistMap object.
   1.277 +    ///
   1.278 +    ///\ref named-func-param "Named parameter"
   1.279 +    ///for setting \ref DistMap object.
   1.280 +    template<class T>
   1.281 +    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
   1.282 +    {
   1.283 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.284 +      return DijkstraWizard<SetDistMapBase<T> >(*this);
   1.285 +    }
   1.286 +
   1.287 +    template<class T>
   1.288      struct SetProcessedMapBase : public Base {
   1.289        typedef T ProcessedMap;
   1.290        static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
   1.291        SetProcessedMapBase(const TR &b) : TR(b) {}
   1.292      };
   1.293 -    ///\brief \ref named-templ-param "Named parameter"
   1.294 +    ///\brief \ref named-func-param "Named parameter"
   1.295      ///for setting \ref ProcessedMap object.
   1.296      ///
   1.297 -    /// \ref named-templ-param "Named parameter"
   1.298 +    /// \ref named-func-param "Named parameter"
   1.299      ///for setting \ref ProcessedMap object.
   1.300      template<class T>
   1.301      DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
   1.302 @@ -1230,37 +1246,49 @@
   1.303      }
   1.304  
   1.305      template<class T>
   1.306 -    struct SetDistMapBase : public Base {
   1.307 -      typedef T DistMap;
   1.308 -      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.309 -      SetDistMapBase(const TR &b) : TR(b) {}
   1.310 +    struct SetPathBase : public Base {
   1.311 +      typedef T Path;
   1.312 +      SetPathBase(const TR &b) : TR(b) {}
   1.313      };
   1.314 -    ///\brief \ref named-templ-param "Named parameter"
   1.315 -    ///for setting \ref DistMap object.
   1.316 +    ///\brief \ref named-func-param "Named parameter"
   1.317 +    ///for getting the shortest path to the target node.
   1.318      ///
   1.319 -    ///\ref named-templ-param "Named parameter"
   1.320 -    ///for setting \ref DistMap object.
   1.321 +    ///\ref named-func-param "Named parameter"
   1.322 +    ///for getting the shortest path to the target node.
   1.323      template<class T>
   1.324 -    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
   1.325 +    DijkstraWizard<SetPathBase<T> > path(const T &t)
   1.326      {
   1.327 -      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.328 -      return DijkstraWizard<SetDistMapBase<T> >(*this);
   1.329 +      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.330 +      return DijkstraWizard<SetPathBase<T> >(*this);
   1.331 +    }
   1.332 +
   1.333 +    ///\brief \ref named-func-param "Named parameter"
   1.334 +    ///for getting the distance of the target node.
   1.335 +    ///
   1.336 +    ///\ref named-func-param "Named parameter"
   1.337 +    ///for getting the distance of the target node.
   1.338 +    DijkstraWizard dist(const Value &d)
   1.339 +    {
   1.340 +      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
   1.341 +      return *this;
   1.342      }
   1.343  
   1.344    };
   1.345  
   1.346 -  ///Function type interface for Dijkstra algorithm.
   1.347 +  ///Function-type interface for Dijkstra algorithm.
   1.348  
   1.349    /// \ingroup shortest_path
   1.350 -  ///Function type interface for Dijkstra algorithm.
   1.351 +  ///Function-type interface for Dijkstra algorithm.
   1.352    ///
   1.353 -  ///This function also has several
   1.354 -  ///\ref named-templ-func-param "named parameters",
   1.355 +  ///This function also has several \ref named-func-param "named parameters",
   1.356    ///they are declared as the members of class \ref DijkstraWizard.
   1.357 -  ///The following
   1.358 -  ///example shows how to use these parameters.
   1.359 +  ///The following examples show how to use these parameters.
   1.360    ///\code
   1.361 -  ///  dijkstra(g,length,source).predMap(preds).run();
   1.362 +  ///  // Compute shortest path from node s to each node
   1.363 +  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
   1.364 +  ///
   1.365 +  ///  // Compute shortest path from s to t
   1.366 +  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
   1.367    ///\endcode
   1.368    ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
   1.369    ///to the end of the parameter list.
   1.370 @@ -1268,9 +1296,9 @@
   1.371    ///\sa Dijkstra
   1.372    template<class GR, class LM>
   1.373    DijkstraWizard<DijkstraWizardBase<GR,LM> >
   1.374 -  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
   1.375 +  dijkstra(const GR &digraph, const LM &length)
   1.376    {
   1.377 -    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
   1.378 +    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
   1.379    }
   1.380  
   1.381  } //END OF NAMESPACE LEMON