lemon/bfs.h
changeset 278 931190050520
parent 258 0310c8984732
child 281 e9b4fbe163f5
child 286 da414906fe21
     1.1 --- a/lemon/bfs.h	Thu Sep 11 11:10:44 2008 +0100
     1.2 +++ b/lemon/bfs.h	Mon Sep 22 15:33:23 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 @@ -115,7 +116,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 @@ -841,26 +842,23 @@
    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      ///\todo The digraph alone may be insufficient to initialize
    1.32 -#ifdef DOXYGEN
    1.33      static PredMap *createPredMap(const Digraph &g)
    1.34 -#else
    1.35 -    static PredMap *createPredMap(const Digraph &)
    1.36 -#endif
    1.37      {
    1.38 -      return new PredMap();
    1.39 +      return new PredMap(g);
    1.40      }
    1.41  
    1.42      ///The type of the map that indicates which nodes are processed.
    1.43  
    1.44      ///The type of the map that indicates which nodes are processed.
    1.45      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.46 +    ///By default it is a NullMap.
    1.47      typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    1.48      ///Instantiates a \ref ProcessedMap.
    1.49  
    1.50 @@ -895,21 +893,22 @@
    1.51  
    1.52      ///The type of the map that stores the distances of the nodes.
    1.53      ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1.54 -    ///
    1.55 -    typedef NullMap<typename Digraph::Node,int> DistMap;
    1.56 +    typedef typename Digraph::template NodeMap<int> DistMap;
    1.57      ///Instantiates a \ref DistMap.
    1.58  
    1.59      ///This function instantiates a \ref DistMap.
    1.60      ///\param g is the digraph, to which we would like to define
    1.61      ///the \ref DistMap
    1.62 -#ifdef DOXYGEN
    1.63      static DistMap *createDistMap(const Digraph &g)
    1.64 -#else
    1.65 -    static DistMap *createDistMap(const Digraph &)
    1.66 -#endif
    1.67      {
    1.68 -      return new DistMap();
    1.69 +      return new DistMap(g);
    1.70      }
    1.71 +
    1.72 +    ///The type of the shortest paths.
    1.73 +
    1.74 +    ///The type of the shortest paths.
    1.75 +    ///It must meet the \ref concepts::Path "Path" concept.
    1.76 +    typedef lemon::Path<Digraph> Path;
    1.77    };
    1.78  
    1.79    /// Default traits class used by \ref BfsWizard
    1.80 @@ -939,51 +938,39 @@
    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 +    int *_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      BfsWizardBase() : _g(0), _reached(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 one parameter,
   1.107 +    /// others are initiated to \c 0.
   1.108      /// \param g The digraph the algorithm runs on.
   1.109 -    /// \param s The source node.
   1.110 -    BfsWizardBase(const GR &g, Node s=INVALID) :
   1.111 +    BfsWizardBase(const GR &g) :
   1.112        _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   1.113 -      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   1.114 +      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
   1.115  
   1.116    };
   1.117  
   1.118 -  /// Auxiliary class for the function type interface of BFS algorithm.
   1.119 +  /// Auxiliary class for the function-type interface of BFS algorithm.
   1.120  
   1.121 -  /// This auxiliary class is created to implement the function type
   1.122 -  /// interface of \ref Bfs algorithm. It uses the functions and features
   1.123 -  /// of the plain \ref Bfs, but it is much simpler to use it.
   1.124 -  /// It should only be used through the \ref bfs() function, which makes
   1.125 -  /// it easier to use the algorithm.
   1.126 +  /// This auxiliary class is created to implement the
   1.127 +  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
   1.128 +  /// It does not have own \ref run() method, it uses the functions
   1.129 +  /// and features of the plain \ref Bfs.
   1.130    ///
   1.131 -  /// Simplicity means that the way to change the types defined
   1.132 -  /// in the traits class is based on functions that returns the new class
   1.133 -  /// and not on templatable built-in classes.
   1.134 -  /// When using the plain \ref Bfs
   1.135 -  /// the new class with the modified type comes from
   1.136 -  /// the original class by using the ::
   1.137 -  /// operator. In the case of \ref BfsWizard only
   1.138 -  /// a function have to be called, and it will
   1.139 -  /// return the needed class.
   1.140 -  ///
   1.141 -  /// It does not have own \ref run() method. When its \ref run() method
   1.142 -  /// is called, it initiates a plain \ref Bfs object, and calls the
   1.143 -  /// \ref Bfs::run() method of it.
   1.144 +  /// This class should only be used through the \ref bfs() function,
   1.145 +  /// which makes it easier to use the algorithm.
   1.146    template<class TR>
   1.147    class BfsWizard : public TR
   1.148    {
   1.149 @@ -1006,6 +993,8 @@
   1.150      typedef typename TR::ReachedMap ReachedMap;
   1.151      ///\brief The type of the map that indicates which nodes are processed.
   1.152      typedef typename TR::ProcessedMap ProcessedMap;
   1.153 +    ///The type of the shortest paths
   1.154 +    typedef typename TR::Path Path;
   1.155  
   1.156    public:
   1.157  
   1.158 @@ -1016,51 +1005,70 @@
   1.159  
   1.160      /// Constructor that requires parameters.
   1.161      /// These parameters will be the default values for the traits class.
   1.162 -    BfsWizard(const Digraph &g, Node s=INVALID) :
   1.163 -      TR(g,s) {}
   1.164 +    /// \param g The digraph the algorithm runs on.
   1.165 +    BfsWizard(const Digraph &g) :
   1.166 +      TR(g) {}
   1.167  
   1.168      ///Copy constructor
   1.169      BfsWizard(const TR &b) : TR(b) {}
   1.170  
   1.171      ~BfsWizard() {}
   1.172  
   1.173 -    ///Runs BFS algorithm from a source node.
   1.174 +    ///Runs BFS algorithm from the given source node.
   1.175  
   1.176 -    ///Runs BFS algorithm from a source node.
   1.177 -    ///The node can be given with the \ref source() function.
   1.178 +    ///This method runs BFS algorithm from node \c s
   1.179 +    ///in order to compute the shortest path to each node.
   1.180 +    void run(Node s)
   1.181 +    {
   1.182 +      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.183 +      if (Base::_pred)
   1.184 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.185 +      if (Base::_dist)
   1.186 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.187 +      if (Base::_reached)
   1.188 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.189 +      if (Base::_processed)
   1.190 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.191 +      if (s!=INVALID)
   1.192 +        alg.run(s);
   1.193 +      else
   1.194 +        alg.run();
   1.195 +    }
   1.196 +
   1.197 +    ///Finds the shortest path between \c s and \c t.
   1.198 +
   1.199 +    ///This method runs BFS algorithm from node \c s
   1.200 +    ///in order to compute the shortest path to node \c t
   1.201 +    ///(it stops searching when \c t is processed).
   1.202 +    ///
   1.203 +    ///\return \c true if \c t is reachable form \c s.
   1.204 +    bool run(Node s, Node t)
   1.205 +    {
   1.206 +      if (s==INVALID || t==INVALID) throw UninitializedParameter();
   1.207 +      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.208 +      if (Base::_pred)
   1.209 +        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.210 +      if (Base::_dist)
   1.211 +        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.212 +      if (Base::_reached)
   1.213 +        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.214 +      if (Base::_processed)
   1.215 +        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.216 +      alg.run(s,t);
   1.217 +      if (Base::_path)
   1.218 +        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
   1.219 +      if (Base::_di)
   1.220 +        *Base::_di = alg.dist(t);
   1.221 +      return alg.reached(t);
   1.222 +    }
   1.223 +
   1.224 +    ///Runs BFS algorithm to visit all nodes in the digraph.
   1.225 +
   1.226 +    ///This method runs BFS algorithm in order to compute
   1.227 +    ///the shortest path to each node.
   1.228      void run()
   1.229      {
   1.230 -      if(Base::_source==INVALID) throw UninitializedParameter();
   1.231 -      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   1.232 -      if(Base::_reached)
   1.233 -        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
   1.234 -      if(Base::_processed)
   1.235 -        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
   1.236 -      if(Base::_pred)
   1.237 -        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
   1.238 -      if(Base::_dist)
   1.239 -        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   1.240 -      alg.run(Base::_source);
   1.241 -    }
   1.242 -
   1.243 -    ///Runs BFS algorithm from the given node.
   1.244 -
   1.245 -    ///Runs BFS algorithm from the given node.
   1.246 -    ///\param s is the given source.
   1.247 -    void run(Node s)
   1.248 -    {
   1.249 -      Base::_source=s;
   1.250 -      run();
   1.251 -    }
   1.252 -
   1.253 -    /// Sets the source node, from which the Bfs algorithm runs.
   1.254 -
   1.255 -    /// Sets the source node, from which the Bfs algorithm runs.
   1.256 -    /// \param s is the source node.
   1.257 -    BfsWizard<TR> &source(Node s)
   1.258 -    {
   1.259 -      Base::_source=s;
   1.260 -      return *this;
   1.261 +      run(INVALID);
   1.262      }
   1.263  
   1.264      template<class T>
   1.265 @@ -1069,10 +1077,10 @@
   1.266        static PredMap *createPredMap(const Digraph &) { return 0; };
   1.267        SetPredMapBase(const TR &b) : TR(b) {}
   1.268      };
   1.269 -    ///\brief \ref named-templ-param "Named parameter"
   1.270 +    ///\brief \ref named-func-param "Named parameter"
   1.271      ///for setting \ref PredMap object.
   1.272      ///
   1.273 -    /// \ref named-templ-param "Named parameter"
   1.274 +    ///\ref named-func-param "Named parameter"
   1.275      ///for setting \ref PredMap object.
   1.276      template<class T>
   1.277      BfsWizard<SetPredMapBase<T> > predMap(const T &t)
   1.278 @@ -1087,10 +1095,10 @@
   1.279        static ReachedMap *createReachedMap(const Digraph &) { return 0; };
   1.280        SetReachedMapBase(const TR &b) : TR(b) {}
   1.281      };
   1.282 -    ///\brief \ref named-templ-param "Named parameter"
   1.283 +    ///\brief \ref named-func-param "Named parameter"
   1.284      ///for setting \ref ReachedMap object.
   1.285      ///
   1.286 -    /// \ref named-templ-param "Named parameter"
   1.287 +    /// \ref named-func-param "Named parameter"
   1.288      ///for setting \ref ReachedMap object.
   1.289      template<class T>
   1.290      BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
   1.291 @@ -1100,15 +1108,33 @@
   1.292      }
   1.293  
   1.294      template<class T>
   1.295 +    struct SetDistMapBase : public Base {
   1.296 +      typedef T DistMap;
   1.297 +      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.298 +      SetDistMapBase(const TR &b) : TR(b) {}
   1.299 +    };
   1.300 +    ///\brief \ref named-func-param "Named parameter"
   1.301 +    ///for setting \ref DistMap object.
   1.302 +    ///
   1.303 +    /// \ref named-func-param "Named parameter"
   1.304 +    ///for setting \ref DistMap object.
   1.305 +    template<class T>
   1.306 +    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
   1.307 +    {
   1.308 +      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.309 +      return BfsWizard<SetDistMapBase<T> >(*this);
   1.310 +    }
   1.311 +
   1.312 +    template<class T>
   1.313      struct SetProcessedMapBase : public Base {
   1.314        typedef T ProcessedMap;
   1.315        static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
   1.316        SetProcessedMapBase(const TR &b) : TR(b) {}
   1.317      };
   1.318 -    ///\brief \ref named-templ-param "Named parameter"
   1.319 +    ///\brief \ref named-func-param "Named parameter"
   1.320      ///for setting \ref ProcessedMap object.
   1.321      ///
   1.322 -    /// \ref named-templ-param "Named parameter"
   1.323 +    /// \ref named-func-param "Named parameter"
   1.324      ///for setting \ref ProcessedMap object.
   1.325      template<class T>
   1.326      BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
   1.327 @@ -1118,37 +1144,49 @@
   1.328      }
   1.329  
   1.330      template<class T>
   1.331 -    struct SetDistMapBase : public Base {
   1.332 -      typedef T DistMap;
   1.333 -      static DistMap *createDistMap(const Digraph &) { return 0; };
   1.334 -      SetDistMapBase(const TR &b) : TR(b) {}
   1.335 +    struct SetPathBase : public Base {
   1.336 +      typedef T Path;
   1.337 +      SetPathBase(const TR &b) : TR(b) {}
   1.338      };
   1.339 -    ///\brief \ref named-templ-param "Named parameter"
   1.340 -    ///for setting \ref DistMap object.
   1.341 +    ///\brief \ref named-func-param "Named parameter"
   1.342 +    ///for getting the shortest path to the target node.
   1.343      ///
   1.344 -    /// \ref named-templ-param "Named parameter"
   1.345 -    ///for setting \ref DistMap object.
   1.346 +    ///\ref named-func-param "Named parameter"
   1.347 +    ///for getting the shortest path to the target node.
   1.348      template<class T>
   1.349 -    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
   1.350 +    BfsWizard<SetPathBase<T> > path(const T &t)
   1.351      {
   1.352 -      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.353 -      return BfsWizard<SetDistMapBase<T> >(*this);
   1.354 +      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
   1.355 +      return BfsWizard<SetPathBase<T> >(*this);
   1.356 +    }
   1.357 +
   1.358 +    ///\brief \ref named-func-param "Named parameter"
   1.359 +    ///for getting the distance of the target node.
   1.360 +    ///
   1.361 +    ///\ref named-func-param "Named parameter"
   1.362 +    ///for getting the distance of the target node.
   1.363 +    BfsWizard dist(const int &d)
   1.364 +    {
   1.365 +      Base::_di=const_cast<int*>(&d);
   1.366 +      return *this;
   1.367      }
   1.368  
   1.369    };
   1.370  
   1.371 -  ///Function type interface for Bfs algorithm.
   1.372 +  ///Function-type interface for BFS algorithm.
   1.373  
   1.374    /// \ingroup search
   1.375 -  ///Function type interface for Bfs algorithm.
   1.376 +  ///Function-type interface for BFS algorithm.
   1.377    ///
   1.378 -  ///This function also has several
   1.379 -  ///\ref named-templ-func-param "named parameters",
   1.380 +  ///This function also has several \ref named-func-param "named parameters",
   1.381    ///they are declared as the members of class \ref BfsWizard.
   1.382 -  ///The following
   1.383 -  ///example shows how to use these parameters.
   1.384 +  ///The following examples show how to use these parameters.
   1.385    ///\code
   1.386 -  ///  bfs(g,source).predMap(preds).run();
   1.387 +  ///  // Compute shortest path from node s to each node
   1.388 +  ///  bfs(g).predMap(preds).distMap(dists).run(s);
   1.389 +  ///
   1.390 +  ///  // Compute shortest path from s to t
   1.391 +  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
   1.392    ///\endcode
   1.393    ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
   1.394    ///to the end of the parameter list.
   1.395 @@ -1156,9 +1194,9 @@
   1.396    ///\sa Bfs
   1.397    template<class GR>
   1.398    BfsWizard<BfsWizardBase<GR> >
   1.399 -  bfs(const GR &g,typename GR::Node s=INVALID)
   1.400 +  bfs(const GR &digraph)
   1.401    {
   1.402 -    return BfsWizard<BfsWizardBase<GR> >(g,s);
   1.403 +    return BfsWizard<BfsWizardBase<GR> >(digraph);
   1.404    }
   1.405  
   1.406  #ifdef DOXYGEN