COIN-OR::LEMON - Graph Library

Ticket #96: dijk_4a3bfa4577a5.patch

File dijk_4a3bfa4577a5.patch, 37.9 KB (added by Peter Kovacs, 16 years ago)
  • lemon/bfs.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1221731815 -7200
    # Node ID 4a3bfa4577a506eae6bdafa3017458a2a6a328ce
    # Parent  c691064dfd4f2e3d4e230a1fd9845a1d96de2a5d
    Modify the function interface of bfs, dfs, and dijkstra (ticket #96)
     - Make the source node a mandatory argument of bfs()/dfs()/dijkstra().
     - Support s-t searches with function interface.
     - Set NodeMap<T> instead of NullMap as PredMap and DistMap in the default
       traits classes.
     - Modify the related test files.
     - Doc improvements.
     - Bug fix in concepts/path.h.
    
    diff --git a/lemon/bfs.h b/lemon/bfs.h
    a b  
    2828#include <lemon/core.h>
    2929#include <lemon/error.h>
    3030#include <lemon/maps.h>
     31#include <lemon/path.h>
    3132
    3233namespace lemon {
    3334
     
    841842    ///The type of the map that stores the predecessor
    842843    ///arcs of the shortest paths.
    843844    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    844     typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
     845    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    845846    ///Instantiates a \ref PredMap.
    846847
    847848    ///This function instantiates a \ref PredMap.
    848849    ///\param g is the digraph, to which we would like to define the
    849850    ///\ref PredMap.
    850851    ///\todo The digraph alone may be insufficient to initialize
    851 #ifdef DOXYGEN
    852852    static PredMap *createPredMap(const Digraph &g)
    853 #else
    854     static PredMap *createPredMap(const Digraph &)
    855 #endif
    856853    {
    857       return new PredMap();
     854      return new PredMap(g);
    858855    }
    859856
    860857    ///The type of the map that indicates which nodes are processed.
    861858
    862859    ///The type of the map that indicates which nodes are processed.
    863860    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
     861    ///By default it is a NullMap.
    864862    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    865863    ///Instantiates a \ref ProcessedMap.
    866864
     
    895893
    896894    ///The type of the map that stores the distances of the nodes.
    897895    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    898     ///
    899     typedef NullMap<typename Digraph::Node,int> DistMap;
     896    typedef typename Digraph::template NodeMap<int> DistMap;
    900897    ///Instantiates a \ref DistMap.
    901898
    902899    ///This function instantiates a \ref DistMap.
    903900    ///\param g is the digraph, to which we would like to define
    904901    ///the \ref DistMap
    905 #ifdef DOXYGEN
    906902    static DistMap *createDistMap(const Digraph &g)
    907 #else
    908     static DistMap *createDistMap(const Digraph &)
    909 #endif
    910903    {
    911       return new DistMap();
     904      return new DistMap(g);
    912905    }
     906
     907    ///The type of the shortest paths.
     908
     909    ///The type of the shortest paths.
     910    ///It must meet the \ref concepts::Path "Path" concept.
     911    typedef lemon::Path<Digraph> Path;
    913912  };
    914913
    915914  /// Default traits class used by \ref BfsWizard
     
    939938    void *_pred;
    940939    //Pointer to the map of distances.
    941940    void *_dist;
    942     //Pointer to the source node.
     941    //Pointer to the shortest path to the target node.
     942    void *_path;
     943    //Pointer to the distance of the target node.
     944    int *_di;
     945    //The source node.
    943946    Node _source;
     947    //The the target node.
     948    Node _target;
    944949
    945950    public:
    946951    /// Constructor.
     
    948953    /// This constructor does not require parameters, therefore it initiates
    949954    /// all of the attributes to default values (0, INVALID).
    950955    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
    951                       _dist(0), _source(INVALID) {}
     956                      _dist(0), _path(0), _di(0),
     957                      _source(INVALID), _target(INVALID) {}
    952958
    953959    /// Constructor.
    954960
     
    957963    /// Others are initiated to 0.
    958964    /// \param g The digraph the algorithm runs on.
    959965    /// \param s The source node.
    960     BfsWizardBase(const GR &g, Node s=INVALID) :
     966    /// \param t The optional target node.
     967    BfsWizardBase(const GR &g, Node s, Node t=INVALID) :
    961968      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
    962       _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
     969      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0),
     970      _source(s), _target(t) {}
    963971
    964972  };
    965973
     
    10061014    typedef typename TR::ReachedMap ReachedMap;
    10071015    ///\brief The type of the map that indicates which nodes are processed.
    10081016    typedef typename TR::ProcessedMap ProcessedMap;
     1017    ///The type of the shortest paths
     1018    typedef typename TR::Path Path;
    10091019
    10101020  public:
    10111021
     
    10161026
    10171027    /// Constructor that requires parameters.
    10181028    /// These parameters will be the default values for the traits class.
    1019     BfsWizard(const Digraph &g, Node s=INVALID) :
    1020       TR(g,s) {}
     1029    /// \param g The digraph the algorithm runs on.
     1030    /// \param s The source node.
     1031    /// \param t The optional target node.
     1032    BfsWizard(const Digraph &g, Node s, Node t=INVALID) :
     1033      TR(g,s,t) {}
    10211034
    10221035    ///Copy constructor
    10231036    BfsWizard(const TR &b) : TR(b) {}
    10241037
    10251038    ~BfsWizard() {}
    10261039
    1027     ///Runs BFS algorithm from a source node.
     1040    ///Runs BFS algorithm from the source node.
    10281041
    1029     ///Runs BFS algorithm from a source node.
    1030     ///The node can be given with the \ref source() function.
    1031     void run()
     1042    ///This function runs BFS algorithm from the source node that
     1043    ///was given in the constructor.
     1044    ///If a target node is given, the function computes the shortest
     1045    ///path to that node (it stops the search when it reaches that
     1046    ///node), otherwise it computes the shortest path to each node.
     1047    ///\param t The optional target node.
     1048    ///\return \c true if no target node was given (so the function
     1049    ///computed the shortest path to each reachable node) or if the
     1050    ///given target node was reached.
     1051    bool run(Node t=INVALID)
    10321052    {
     1053      if(t!=INVALID) Base::_target=t;
    10331054      if(Base::_source==INVALID) throw UninitializedParameter();
    10341055      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
    10351056      if(Base::_reached)
     
    10401061        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    10411062      if(Base::_dist)
    10421063        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
    1043       alg.run(Base::_source);
    1044     }
    1045 
    1046     ///Runs BFS algorithm from the given node.
    1047 
    1048     ///Runs BFS algorithm from the given node.
    1049     ///\param s is the given source.
    1050     void run(Node s)
    1051     {
    1052       Base::_source=s;
    1053       run();
    1054     }
    1055 
    1056     /// Sets the source node, from which the Bfs algorithm runs.
    1057 
    1058     /// Sets the source node, from which the Bfs algorithm runs.
    1059     /// \param s is the source node.
    1060     BfsWizard<TR> &source(Node s)
    1061     {
    1062       Base::_source=s;
    1063       return *this;
     1064      if(Base::_target!=INVALID) {
     1065        alg.run(Base::_source, Base::_target);
     1066        if (Base::_path)
     1067          *reinterpret_cast<Path*>(Base::_path) = alg.path(Base::_target);
     1068        if (Base::_di)
     1069          *Base::_di = alg.dist(Base::_target);
     1070        return alg.reached(Base::_target);
     1071      } else {
     1072        alg.run(Base::_source);
     1073        return true;
     1074      }
    10641075    }
    10651076
    10661077    template<class T>
     
    11001111    }
    11011112
    11021113    template<class T>
     1114    struct SetDistMapBase : public Base {
     1115      typedef T DistMap;
     1116      static DistMap *createDistMap(const Digraph &) { return 0; };
     1117      SetDistMapBase(const TR &b) : TR(b) {}
     1118    };
     1119    ///\brief \ref named-templ-param "Named parameter"
     1120    ///for setting \ref DistMap object.
     1121    ///
     1122    /// \ref named-templ-param "Named parameter"
     1123    ///for setting \ref DistMap object.
     1124    template<class T>
     1125    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
     1126    {
     1127      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
     1128      return BfsWizard<SetDistMapBase<T> >(*this);
     1129    }
     1130
     1131    template<class T>
    11031132    struct SetProcessedMapBase : public Base {
    11041133      typedef T ProcessedMap;
    11051134      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
     
    11181147    }
    11191148
    11201149    template<class T>
    1121     struct SetDistMapBase : public Base {
    1122       typedef T DistMap;
    1123       static DistMap *createDistMap(const Digraph &) { return 0; };
    1124       SetDistMapBase(const TR &b) : TR(b) {}
     1150    struct SetPathBase : public Base {
     1151      typedef T Path;
     1152      SetPathBase(const TR &b) : TR(b) {}
    11251153    };
    11261154    ///\brief \ref named-templ-param "Named parameter"
    1127     ///for setting \ref DistMap object.
     1155    ///for getting the shortest path to the target node.
    11281156    ///
    1129     /// \ref named-templ-param "Named parameter"
    1130     ///for setting \ref DistMap object.
     1157    ///\ref named-templ-param "Named parameter"
     1158    ///for getting the shortest path to the target node.
    11311159    template<class T>
    1132     BfsWizard<SetDistMapBase<T> > distMap(const T &t)
     1160    BfsWizard<SetPathBase<T> > path(const T &t)
    11331161    {
    1134       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
    1135       return BfsWizard<SetDistMapBase<T> >(*this);
     1162      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
     1163      return BfsWizard<SetPathBase<T> >(*this);
     1164    }
     1165
     1166    ///\brief \ref named-templ-param "Named parameter"
     1167    ///for getting the distance of the target node.
     1168    ///
     1169    ///\ref named-templ-param "Named parameter"
     1170    ///for getting the distance of the target node.
     1171    BfsWizard dist(const int &d)
     1172    {
     1173      Base::_di=const_cast<int*>(&d);
     1174      return *this;
    11361175    }
    11371176
    11381177  };
    11391178
    1140   ///Function type interface for Bfs algorithm.
     1179  ///Function type interface for BFS algorithm.
    11411180
    11421181  /// \ingroup search
    1143   ///Function type interface for Bfs algorithm.
     1182  ///Function type interface for BFS algorithm.
    11441183  ///
    11451184  ///This function also has several
    11461185  ///\ref named-templ-func-param "named parameters",
    11471186  ///they are declared as the members of class \ref BfsWizard.
    1148   ///The following
    1149   ///example shows how to use these parameters.
     1187  ///The following examples show how to use these parameters.
    11501188  ///\code
    1151   ///  bfs(g,source).predMap(preds).run();
     1189  ///  // Compute shortest path to each node
     1190  ///  bfs(g,source).predMap(preds).distMap(dists).run();
     1191  ///
     1192  ///  // Compute shortest path to one node
     1193  ///  bool reached = bfs(g,source,target).path(p).dist(d).run();
    11521194  ///\endcode
    11531195  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
    11541196  ///to the end of the parameter list.
     
    11561198  ///\sa Bfs
    11571199  template<class GR>
    11581200  BfsWizard<BfsWizardBase<GR> >
    1159   bfs(const GR &g,typename GR::Node s=INVALID)
     1201  bfs(const GR &digraph,
     1202      typename GR::Node source, typename GR::Node target=INVALID)
    11601203  {
    1161     return BfsWizard<BfsWizardBase<GR> >(g,s);
     1204    return BfsWizard<BfsWizardBase<GR> >(digraph,source,target);
    11621205  }
    11631206
    11641207#ifdef DOXYGEN
  • lemon/concepts/path.h

    diff --git a/lemon/concepts/path.h b/lemon/concepts/path.h
    a b  
    6666
    6767      /// \brief Template assigment
    6868      template <typename CPath>
    69       Path& operator=(const CPath& cpath) {}
     69      Path& operator=(const CPath& cpath) {
     70        ignore_unused_variable_warning(cpath);
     71        return *this;
     72      }
    7073
    7174      /// Length of the path ie. the number of arcs in the path.
    7275      int length() const { return 0;}
  • lemon/dfs.h

    diff --git a/lemon/dfs.h b/lemon/dfs.h
    a b  
    2929#include <lemon/error.h>
    3030#include <lemon/assert.h>
    3131#include <lemon/maps.h>
     32#include <lemon/path.h>
    3233
    3334namespace lemon {
    3435
     
    775776    ///The type of the map that stores the predecessor
    776777    ///arcs of the %DFS paths.
    777778    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    778     ///
    779     typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
     779    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    780780    ///Instantiates a \ref PredMap.
    781781
    782782    ///This function instantiates a \ref PredMap.
    783783    ///\param g is the digraph, to which we would like to define the
    784784    ///\ref PredMap.
    785785    ///\todo The digraph alone may be insufficient to initialize
    786 #ifdef DOXYGEN
    787786    static PredMap *createPredMap(const Digraph &g)
    788 #else
    789     static PredMap *createPredMap(const Digraph &)
    790 #endif
    791787    {
    792       return new PredMap();
     788      return new PredMap(g);
    793789    }
    794790
    795791    ///The type of the map that indicates which nodes are processed.
    796792
    797793    ///The type of the map that indicates which nodes are processed.
    798794    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
     795    ///By default it is a NullMap.
    799796    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    800797    ///Instantiates a \ref ProcessedMap.
    801798
     
    830827
    831828    ///The type of the map that stores the distances of the nodes.
    832829    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    833     ///
    834     typedef NullMap<typename Digraph::Node,int> DistMap;
     830    typedef typename Digraph::template NodeMap<int> DistMap;
    835831    ///Instantiates a \ref DistMap.
    836832
    837833    ///This function instantiates a \ref DistMap.
    838834    ///\param g is the digraph, to which we would like to define
    839835    ///the \ref DistMap
    840 #ifdef DOXYGEN
    841836    static DistMap *createDistMap(const Digraph &g)
    842 #else
    843     static DistMap *createDistMap(const Digraph &)
    844 #endif
    845837    {
    846       return new DistMap();
     838      return new DistMap(g);
    847839    }
     840
     841    ///The type of the DFS paths.
     842
     843    ///The type of the DFS paths.
     844    ///It must meet the \ref concepts::Path "Path" concept.
     845    typedef lemon::Path<Digraph> Path;
    848846  };
    849847
    850848  /// Default traits class used by \ref DfsWizard
     
    874872    void *_pred;
    875873    //Pointer to the map of distances.
    876874    void *_dist;
    877     //Pointer to the source node.
     875    //Pointer to the DFS path to the target node.
     876    void *_path;
     877    //Pointer to the distance of the target node.
     878    int *_di;
     879    //The source node.
    878880    Node _source;
     881    //The the target node.
     882    Node _target;
    879883
    880884    public:
    881885    /// Constructor.
     
    883887    /// This constructor does not require parameters, therefore it initiates
    884888    /// all of the attributes to default values (0, INVALID).
    885889    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
    886                       _dist(0), _source(INVALID) {}
     890                      _dist(0), _path(0), _di(0),
     891                      _source(INVALID), _target(INVALID) {}
    887892
    888893    /// Constructor.
    889894
     
    892897    /// Others are initiated to 0.
    893898    /// \param g The digraph the algorithm runs on.
    894899    /// \param s The source node.
    895     DfsWizardBase(const GR &g, Node s=INVALID) :
     900    /// \param t The optional target node.
     901    DfsWizardBase(const GR &g, Node s, Node t=INVALID) :
    896902      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
    897       _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
     903      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0),
     904      _source(s), _target(t) {}
    898905
    899906  };
    900907
     
    933940    typedef typename Digraph::OutArcIt OutArcIt;
    934941
    935942    ///\brief The type of the map that stores the predecessor
    936     ///arcs of the shortest paths.
     943    ///arcs of the DFS paths.
    937944    typedef typename TR::PredMap PredMap;
    938945    ///\brief The type of the map that stores the distances of the nodes.
    939946    typedef typename TR::DistMap DistMap;
     
    941948    typedef typename TR::ReachedMap ReachedMap;
    942949    ///\brief The type of the map that indicates which nodes are processed.
    943950    typedef typename TR::ProcessedMap ProcessedMap;
     951    ///The type of the DFS paths
     952    typedef typename TR::Path Path;
    944953
    945954  public:
    946955
     
    951960
    952961    /// Constructor that requires parameters.
    953962    /// These parameters will be the default values for the traits class.
    954     DfsWizard(const Digraph &g, Node s=INVALID) :
    955       TR(g,s) {}
     963    /// \param g The digraph the algorithm runs on.
     964    /// \param s The source node.
     965    /// \param t The optional target node.
     966    DfsWizard(const Digraph &g, Node s, Node t=INVALID) :
     967      TR(g,s,t) {}
    956968
    957969    ///Copy constructor
    958970    DfsWizard(const TR &b) : TR(b) {}
    959971
    960972    ~DfsWizard() {}
    961973
    962     ///Runs DFS algorithm from a source node.
     974    ///Runs DFS algorithm from the source node.
    963975
    964     ///Runs DFS algorithm from a source node.
    965     ///The node can be given with the \ref source() function.
    966     void run()
     976    ///This function runs DFS algorithm from the source node that
     977    ///was given in the constructor.
     978    ///If a target node is given, the function computes the DFS
     979    ///path to that node (it stops the search when it reaches that
     980    ///node), otherwise it computes the DFS path to each node.
     981    ///\param t The optional target node.
     982    ///\return \c true if no target node was given (so the function
     983    ///computed the DFS path to each reachable node) or if the
     984    ///given target node was reached.
     985    bool run(Node t=INVALID)
    967986    {
     987      if(t!=INVALID) Base::_target=t;
    968988      if(Base::_source==INVALID) throw UninitializedParameter();
    969989      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
    970990      if(Base::_reached)
     
    975995        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    976996      if(Base::_dist)
    977997        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
    978       alg.run(Base::_source);
    979     }
    980 
    981     ///Runs DFS algorithm from the given node.
    982 
    983     ///Runs DFS algorithm from the given node.
    984     ///\param s is the given source.
    985     void run(Node s)
    986     {
    987       Base::_source=s;
    988       run();
    989     }
    990 
    991     /// Sets the source node, from which the Dfs algorithm runs.
    992 
    993     /// Sets the source node, from which the Dfs algorithm runs.
    994     /// \param s is the source node.
    995     DfsWizard<TR> &source(Node s)
    996     {
    997       Base::_source=s;
    998       return *this;
     998      if(Base::_target!=INVALID) {
     999        alg.run(Base::_source, Base::_target);
     1000        if (Base::_path)
     1001          *reinterpret_cast<Path*>(Base::_path) = alg.path(Base::_target);
     1002        if (Base::_di)
     1003          *Base::_di = alg.dist(Base::_target);
     1004        return alg.reached(Base::_target);
     1005      } else {
     1006        alg.run(Base::_source);
     1007        return true;
     1008      }
    9991009    }
    10001010
    10011011    template<class T>
     
    10351045    }
    10361046
    10371047    template<class T>
     1048    struct SetDistMapBase : public Base {
     1049      typedef T DistMap;
     1050      static DistMap *createDistMap(const Digraph &) { return 0; };
     1051      SetDistMapBase(const TR &b) : TR(b) {}
     1052    };
     1053    ///\brief \ref named-templ-param "Named parameter"
     1054    ///for setting \ref DistMap object.
     1055    ///
     1056    ///\ref named-templ-param "Named parameter"
     1057    ///for setting \ref DistMap object.
     1058    template<class T>
     1059    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
     1060    {
     1061      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
     1062      return DfsWizard<SetDistMapBase<T> >(*this);
     1063    }
     1064
     1065    template<class T>
    10381066    struct SetProcessedMapBase : public Base {
    10391067      typedef T ProcessedMap;
    10401068      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
     
    10531081    }
    10541082
    10551083    template<class T>
    1056     struct SetDistMapBase : public Base {
    1057       typedef T DistMap;
    1058       static DistMap *createDistMap(const Digraph &) { return 0; };
    1059       SetDistMapBase(const TR &b) : TR(b) {}
     1084    struct SetPathBase : public Base {
     1085      typedef T Path;
     1086      SetPathBase(const TR &b) : TR(b) {}
    10601087    };
    10611088    ///\brief \ref named-templ-param "Named parameter"
    1062     ///for setting \ref DistMap object.
     1089    ///for getting the DFS path to the target node.
    10631090    ///
    10641091    ///\ref named-templ-param "Named parameter"
    1065     ///for setting \ref DistMap object.
     1092    ///for getting the DFS path to the target node.
    10661093    template<class T>
    1067     DfsWizard<SetDistMapBase<T> > distMap(const T &t)
     1094    DfsWizard<SetPathBase<T> > path(const T &t)
    10681095    {
    1069       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
    1070       return DfsWizard<SetDistMapBase<T> >(*this);
     1096      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
     1097      return DfsWizard<SetPathBase<T> >(*this);
     1098    }
     1099
     1100    ///\brief \ref named-templ-param "Named parameter"
     1101    ///for getting the distance of the target node.
     1102    ///
     1103    ///\ref named-templ-param "Named parameter"
     1104    ///for getting the distance of the target node.
     1105    DfsWizard dist(const int &d)
     1106    {
     1107      Base::_di=const_cast<int*>(&d);
     1108      return *this;
    10711109    }
    10721110
    10731111  };
    10741112
    1075   ///Function type interface for Dfs algorithm.
     1113  ///Function type interface for DFS algorithm.
    10761114
    10771115  ///\ingroup search
    1078   ///Function type interface for Dfs algorithm.
     1116  ///Function type interface for DFS algorithm.
    10791117  ///
    10801118  ///This function also has several
    10811119  ///\ref named-templ-func-param "named parameters",
    10821120  ///they are declared as the members of class \ref DfsWizard.
    1083   ///The following
    1084   ///example shows how to use these parameters.
     1121  ///The following examples show how to use these parameters.
    10851122  ///\code
    1086   ///  dfs(g,source).predMap(preds).run();
     1123  ///  // Compute the DFS tree
     1124  ///  dfs(g,source).predMap(preds).distMap(dists).run();
     1125  ///
     1126  ///  // Compute the DFS path to one node
     1127  ///  bool reached = dfs(g,source,target).path(p).dist(d).run();
    10871128  ///\endcode
     1129
    10881130  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
    10891131  ///to the end of the parameter list.
    10901132  ///\sa DfsWizard
    10911133  ///\sa Dfs
    10921134  template<class GR>
    10931135  DfsWizard<DfsWizardBase<GR> >
    1094   dfs(const GR &g,typename GR::Node s=INVALID)
     1136  dfs(const GR &digraph,
     1137      typename GR::Node source, typename GR::Node target=INVALID)
    10951138  {
    1096     return DfsWizard<DfsWizardBase<GR> >(g,s);
     1139    return DfsWizard<DfsWizardBase<GR> >(digraph,source,target);
    10971140  }
    10981141
    10991142#ifdef DOXYGEN
  • lemon/dijkstra.h

    diff --git a/lemon/dijkstra.h b/lemon/dijkstra.h
    a b  
    3030#include <lemon/core.h>
    3131#include <lemon/error.h>
    3232#include <lemon/maps.h>
     33#include <lemon/path.h>
    3334
    3435namespace lemon {
    3536
     
    987988    ///The type of the map that stores the predecessor
    988989    ///arcs of the shortest paths.
    989990    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    990     typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
     991    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    991992    ///Instantiates a \ref PredMap.
    992993
    993994    ///This function instantiates a \ref PredMap.
    994995    ///\param g is the digraph, to which we would like to define the
    995996    ///\ref PredMap.
    996997    ///\todo The digraph alone may be insufficient to initialize
    997 #ifdef DOXYGEN
    998998    static PredMap *createPredMap(const Digraph &g)
    999 #else
    1000     static PredMap *createPredMap(const Digraph &)
    1001 #endif
    1002999    {
    1003       return new PredMap();
     1000      return new PredMap(g);
    10041001    }
    10051002
    10061003    ///The type of the map that indicates which nodes are processed.
     
    10301027
    10311028    ///The type of the map that stores the distances of the nodes.
    10321029    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    1033     typedef NullMap<typename Digraph::Node,Value> DistMap;
     1030    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
    10341031    ///Instantiates a \ref DistMap.
    10351032
    10361033    ///This function instantiates a \ref DistMap.
    10371034    ///\param g is the digraph, to which we would like to define
    10381035    ///the \ref DistMap
    1039 #ifdef DOXYGEN
    10401036    static DistMap *createDistMap(const Digraph &g)
    1041 #else
    1042     static DistMap *createDistMap(const Digraph &)
    1043 #endif
    10441037    {
    1045       return new DistMap();
     1038      return new DistMap(g);
    10461039    }
     1040
     1041    ///The type of the shortest paths.
     1042
     1043    ///The type of the shortest paths.
     1044    ///It must meet the \ref concepts::Path "Path" concept.
     1045    typedef lemon::Path<Digraph> Path;
    10471046  };
    10481047
    10491048  /// Default traits class used by \ref DijkstraWizard
     
    10651064
    10661065    //Pointer to the digraph the algorithm runs on.
    10671066    void *_g;
    1068     //Pointer to the length map
     1067    //Pointer to the length map.
    10691068    void *_length;
    10701069    //Pointer to the map of processed nodes.
    10711070    void *_processed;
     
    10731072    void *_pred;
    10741073    //Pointer to the map of distances.
    10751074    void *_dist;
    1076     //Pointer to the source node.
     1075    //Pointer to the shortest path to the target node.
     1076    void *_path;
     1077    //Pointer to the distance of the target node.
     1078    void *_di;
     1079    //The source node.
    10771080    Node _source;
     1081    //The target node.
     1082    Node _target;
    10781083
    10791084  public:
    10801085    /// Constructor.
     
    10821087    /// This constructor does not require parameters, therefore it initiates
    10831088    /// all of the attributes to default values (0, INVALID).
    10841089    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
    1085                            _dist(0), _source(INVALID) {}
     1090                           _dist(0), _path(0), _di(0),
     1091                           _source(INVALID), _target(INVALID) {}
    10861092
    10871093    /// Constructor.
    10881094
     
    10921098    /// \param g The digraph the algorithm runs on.
    10931099    /// \param l The length map.
    10941100    /// \param s The source node.
    1095     DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
     1101    /// \param t The optional target node.
     1102    DijkstraWizardBase(const GR &g,const LM &l, Node s, Node t=INVALID) :
    10961103      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
    10971104      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
    1098       _processed(0), _pred(0), _dist(0), _source(s) {}
     1105      _processed(0), _pred(0), _dist(0), _path(0), _di(0),
     1106      _source(s), _target(t) {}
    10991107
    11001108  };
    11011109
     
    11441152    typedef typename TR::DistMap DistMap;
    11451153    ///The type of the map that indicates which nodes are processed.
    11461154    typedef typename TR::ProcessedMap ProcessedMap;
     1155    ///The type of the shortest paths
     1156    typedef typename TR::Path Path;
    11471157    ///The heap type used by the dijkstra algorithm.
    11481158    typedef typename TR::Heap Heap;
    11491159
     
    11561166
    11571167    /// Constructor that requires parameters.
    11581168    /// These parameters will be the default values for the traits class.
    1159     DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
    1160       TR(g,l,s) {}
     1169    /// \param g The digraph the algorithm runs on.
     1170    /// \param l The length map.
     1171    /// \param s The source node.
     1172    /// \param t The optional target node.
     1173    DijkstraWizard(const Digraph &g, const LengthMap &l,
     1174                   Node s, Node t=INVALID) :
     1175      TR(g,l,s,t) {}
    11611176
    11621177    ///Copy constructor
    11631178    DijkstraWizard(const TR &b) : TR(b) {}
    11641179
    11651180    ~DijkstraWizard() {}
    11661181
    1167     ///Runs Dijkstra algorithm from a source node.
     1182    ///Runs Dijkstra algorithm from the source node.
    11681183
    1169     ///Runs Dijkstra algorithm from a source node.
    1170     ///The node can be given with the \ref source() function.
    1171     void run()
     1184    ///This function runs Dijkstra algorithm from the source node that
     1185    ///was given in the constructor.
     1186    ///If a target node is given, the function computes the shortest
     1187    ///path to that node (it stops the search when it processes that
     1188    ///node), otherwise it computes the shortest path to each node.
     1189    ///\param t The optional target node.
     1190    ///\return \c true if no target node was given (so the function
     1191    ///computed the shortest path to each reachable node) or if the
     1192    ///given target node was reached.
     1193    bool run(Node t=INVALID)
    11721194    {
     1195      if(t!=INVALID) Base::_target=t;
    11731196      if(Base::_source==INVALID) throw UninitializedParameter();
    11741197      Dijkstra<Digraph,LengthMap,TR>
    1175         dij(*reinterpret_cast<const Digraph*>(Base::_g),
    1176             *reinterpret_cast<const LengthMap*>(Base::_length));
     1198        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
     1199             *reinterpret_cast<const LengthMap*>(Base::_length));
    11771200      if(Base::_processed)
    1178         dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
     1201        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
    11791202      if(Base::_pred)
    1180         dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
     1203        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    11811204      if(Base::_dist)
    1182         dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
    1183       dij.run(Base::_source);
    1184     }
    1185 
    1186     ///Runs Dijkstra algorithm from the given node.
    1187 
    1188     ///Runs Dijkstra algorithm from the given node.
    1189     ///\param s is the given source.
    1190     void run(Node s)
    1191     {
    1192       Base::_source=s;
    1193       run();
    1194     }
    1195 
    1196     /// Sets the source node, from which the Dijkstra algorithm runs.
    1197 
    1198     /// Sets the source node, from which the Dijkstra algorithm runs.
    1199     /// \param s is the source node.
    1200     DijkstraWizard<TR> &source(Node s)
    1201     {
    1202       Base::_source=s;
    1203       return *this;
     1205        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
     1206      if(Base::_target!=INVALID) {
     1207        dijk.run(Base::_source, Base::_target);
     1208        if (Base::_path)
     1209          *reinterpret_cast<Path*>(Base::_path) = dijk.path(Base::_target);
     1210        if (Base::_di)
     1211          *reinterpret_cast<Value*>(Base::_di) = dijk.dist(Base::_target);
     1212        return dijk.reached(Base::_target);
     1213      } else {
     1214        dijk.run(Base::_source);
     1215        return true;
     1216      }
    12041217    }
    12051218
    12061219    template<class T>
     
    12221235    }
    12231236
    12241237    template<class T>
     1238    struct SetDistMapBase : public Base {
     1239      typedef T DistMap;
     1240      static DistMap *createDistMap(const Digraph &) { return 0; };
     1241      SetDistMapBase(const TR &b) : TR(b) {}
     1242    };
     1243    ///\brief \ref named-templ-param "Named parameter"
     1244    ///for setting \ref DistMap object.
     1245    ///
     1246    ///\ref named-templ-param "Named parameter"
     1247    ///for setting \ref DistMap object.
     1248    template<class T>
     1249    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
     1250    {
     1251      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
     1252      return DijkstraWizard<SetDistMapBase<T> >(*this);
     1253    }
     1254
     1255    template<class T>
    12251256    struct SetProcessedMapBase : public Base {
    12261257      typedef T ProcessedMap;
    12271258      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
     
    12401271    }
    12411272
    12421273    template<class T>
    1243     struct SetDistMapBase : public Base {
    1244       typedef T DistMap;
    1245       static DistMap *createDistMap(const Digraph &) { return 0; };
    1246       SetDistMapBase(const TR &b) : TR(b) {}
     1274    struct SetPathBase : public Base {
     1275      typedef T Path;
     1276      SetPathBase(const TR &b) : TR(b) {}
    12471277    };
    12481278    ///\brief \ref named-templ-param "Named parameter"
    1249     ///for setting \ref DistMap object.
     1279    ///for getting the shortest path to the target node.
    12501280    ///
    12511281    ///\ref named-templ-param "Named parameter"
    1252     ///for setting \ref DistMap object.
     1282    ///for getting the shortest path to the target node.
    12531283    template<class T>
    1254     DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
     1284    DijkstraWizard<SetPathBase<T> > path(const T &t)
    12551285    {
    1256       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
    1257       return DijkstraWizard<SetDistMapBase<T> >(*this);
     1286      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
     1287      return DijkstraWizard<SetPathBase<T> >(*this);
     1288    }
     1289
     1290    ///\brief \ref named-templ-param "Named parameter"
     1291    ///for getting the distance of the target node.
     1292    ///
     1293    ///\ref named-templ-param "Named parameter"
     1294    ///for getting the distance of the target node.
     1295    DijkstraWizard dist(const Value &d)
     1296    {
     1297      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
     1298      return *this;
    12581299    }
    12591300
    12601301  };
     
    12671308  ///This function also has several
    12681309  ///\ref named-templ-func-param "named parameters",
    12691310  ///they are declared as the members of class \ref DijkstraWizard.
    1270   ///The following
    1271   ///example shows how to use these parameters.
     1311  ///The following examples show how to use these parameters.
    12721312  ///\code
    1273   ///  dijkstra(g,length,source).predMap(preds).run();
     1313  ///  // Compute shortest path to each node
     1314  ///  dijkstra(g,length,source).predMap(preds).distMap(dists).run();
     1315  ///
     1316  ///  // Compute shortest path to one node
     1317  ///  bool reached = dijkstra(g,length,source,target).path(p).dist(d).run();
    12741318  ///\endcode
    12751319  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
    12761320  ///to the end of the parameter list.
     
    12781322  ///\sa Dijkstra
    12791323  template<class GR, class LM>
    12801324  DijkstraWizard<DijkstraWizardBase<GR,LM> >
    1281   dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
     1325  dijkstra(const GR &digraph, const LM &length,
     1326           typename GR::Node source, typename GR::Node target=INVALID)
    12821327  {
    1283     return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
     1328    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length,
     1329                                                      source,target);
    12841330  }
    12851331
    12861332} //END OF NAMESPACE LEMON
  • test/bfs_test.cc

    diff --git a/test/bfs_test.cc b/test/bfs_test.cc
    a b  
    6262  bool b;
    6363  BType::DistMap d(G);
    6464  BType::PredMap p(G);
    65   //  BType::PredNodeMap pn(G);
    6665
    6766  BType bfs_test(G);
    6867
     
    7271  e  = bfs_test.predArc(n);
    7372  n  = bfs_test.predNode(n);
    7473  d  = bfs_test.distMap();
    75 
    7674  p  = bfs_test.predMap();
    77   //  pn = bfs_test.predNodeMap();
    7875  b  = bfs_test.reached(n);
    7976
    8077  Path<Digraph> pp = bfs_test.path(n);
     
    8986
    9087  Digraph g;
    9188  bfs(g,Node()).run();
    92   bfs(g).source(Node()).run();
    93   bfs(g)
    94     .predMap(concepts::WriteMap<Node,Arc>())
    95     .distMap(concepts::WriteMap<Node,VType>())
    96     .reachedMap(concepts::ReadWriteMap<Node,bool>())
     89  bfs(g,Node())
     90    .predMap(concepts::ReadWriteMap<Node,Arc>())
     91    .distMap(concepts::ReadWriteMap<Node,VType>())
    9792    .processedMap(concepts::WriteMap<Node,bool>())
     93    .run();
     94  bfs(g,Node(),Node())
     95    .predMap(concepts::ReadWriteMap<Node,Arc>())
     96    .distMap(concepts::ReadWriteMap<Node,VType>())
     97    .processedMap(concepts::WriteMap<Node,bool>())
     98    .path(concepts::Path<Digraph>()).dist(VType())
     99    .run();
     100  bfs(g,Node())
     101    .predMap(concepts::ReadWriteMap<Node,Arc>())
     102    .distMap(concepts::ReadWriteMap<Node,VType>())
     103    .processedMap(concepts::WriteMap<Node,bool>())
     104    .path(concepts::Path<Digraph>()).dist(VType())
    98105    .run(Node());
    99106}
    100107
     
    114121  Bfs<Digraph> bfs_test(G);
    115122  bfs_test.run(s);
    116123
    117   check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));
     124  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
    118125
    119126  Path<Digraph> p = bfs_test.path(t);
    120127  check(p.length()==2,"path() found a wrong path.");
     
    128135    Node v=G.target(a);
    129136    check( !bfs_test.reached(u) ||
    130137           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
    131            "Wrong output." << G.id(v) << ' ' << G.id(u));
     138           "Wrong output. " << G.id(u) << "->" << G.id(v));
    132139  }
    133140
    134141  for(NodeIt v(G); v!=INVALID; ++v) {
     
    140147        check(u==bfs_test.predNode(v),"Wrong tree.");
    141148        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
    142149              "Wrong distance. Difference: "
    143               << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
    144                           - 1));
     150              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
    145151      }
    146152    }
     153  }
     154
     155  {
     156    NullMap<Node,Arc> myPredMap;
     157    bfs(G,s).predMap(myPredMap).run();
    147158  }
    148159}
    149160
  • test/dfs_test.cc

    diff --git a/test/dfs_test.cc b/test/dfs_test.cc
    a b  
    8989
    9090  Digraph g;
    9191  dfs(g,Node()).run();
    92   dfs(g).source(Node()).run();
    93   dfs(g)
    94     .predMap(concepts::WriteMap<Node,Arc>())
    95     .distMap(concepts::WriteMap<Node,VType>())
    96     .reachedMap(concepts::ReadWriteMap<Node,bool>())
     92  dfs(g,Node())
     93    .predMap(concepts::ReadWriteMap<Node,Arc>())
     94    .distMap(concepts::ReadWriteMap<Node,VType>())
    9795    .processedMap(concepts::WriteMap<Node,bool>())
     96    .run();
     97  dfs(g,Node(),Node())
     98    .predMap(concepts::ReadWriteMap<Node,Arc>())
     99    .distMap(concepts::ReadWriteMap<Node,VType>())
     100    .processedMap(concepts::WriteMap<Node,bool>())
     101    .path(concepts::Path<Digraph>()).dist(VType())
     102    .run();
     103  dfs(g,Node())
     104    .predMap(concepts::ReadWriteMap<Node,Arc>())
     105    .distMap(concepts::ReadWriteMap<Node,VType>())
     106    .processedMap(concepts::WriteMap<Node,bool>())
     107    .path(concepts::Path<Digraph>()).dist(VType())
    98108    .run(Node());
    99109}
    100110
     
    129139        check(u==dfs_test.predNode(v),"Wrong tree.");
    130140        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
    131141              "Wrong distance. (" << dfs_test.dist(u) << "->"
    132               <<dfs_test.dist(v) << ')');
     142              << dfs_test.dist(v) << ")");
    133143      }
    134144    }
     145  }
     146
     147  {
     148    NullMap<Node,Arc> myPredMap;
     149    dfs(G,s).predMap(myPredMap).run();
    135150  }
    136151}
    137152
  • test/dijkstra_test.cc

    diff --git a/test/dijkstra_test.cc b/test/dijkstra_test.cc
    a b  
    6464  bool b;
    6565  DType::DistMap d(G);
    6666  DType::PredMap p(G);
    67   //  DType::PredNodeMap pn(G);
    6867  LengthMap length;
    6968
    7069  DType dijkstra_test(G,length);
     
    7675  n  = dijkstra_test.predNode(n);
    7776  d  = dijkstra_test.distMap();
    7877  p  = dijkstra_test.predMap();
    79   //  pn = dijkstra_test.predNodeMap();
    8078  b  = dijkstra_test.reached(n);
    8179
    8280  Path<Digraph> pp = dijkstra_test.path(n);
     
    9290
    9391  Digraph g;
    9492  dijkstra(g,LengthMap(),Node()).run();
    95   dijkstra(g,LengthMap()).source(Node()).run();
    96   dijkstra(g,LengthMap())
    97     .predMap(concepts::WriteMap<Node,Arc>())
    98     .distMap(concepts::WriteMap<Node,VType>())
     93  dijkstra(g,LengthMap(),Node())
     94    .predMap(concepts::ReadWriteMap<Node,Arc>())
     95    .distMap(concepts::ReadWriteMap<Node,VType>())
     96    .processedMap(concepts::WriteMap<Node,bool>())
     97    .run();
     98  dijkstra(g,LengthMap(),Node(),Node())
     99    .predMap(concepts::ReadWriteMap<Node,Arc>())
     100    .distMap(concepts::ReadWriteMap<Node,VType>())
     101    .processedMap(concepts::WriteMap<Node,bool>())
     102    .path(concepts::Path<Digraph>()).dist(VType())
     103    .run();
     104  dijkstra(g,LengthMap(),Node())
     105    .predMap(concepts::ReadWriteMap<Node,Arc>())
     106    .distMap(concepts::ReadWriteMap<Node,VType>())
     107    .processedMap(concepts::WriteMap<Node,bool>())
     108    .path(concepts::Path<Digraph>()).dist(VType())
    99109    .run(Node());
    100110}
    101111
     
    122132  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
    123133
    124134  Path<Digraph> p = dijkstra_test.path(t);
    125   check(p.length()==3,"getPath() found a wrong path.");
     135  check(p.length()==3,"path() found a wrong path.");
    126136  check(checkPath(G, p),"path() found a wrong path.");
    127137  check(pathSource(G, p) == s,"path() found a wrong path.");
    128138  check(pathTarget(G, p) == t,"path() found a wrong path.");
     
    132142    Node v=G.target(e);
    133143    check( !dijkstra_test.reached(u) ||
    134144           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
    135            "dist(target)-dist(source)-arc_length= " <<
     145           "Wrong output. dist(target)-dist(source)-arc_length=" <<
    136146           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
    137147  }
    138148
     
    152162
    153163  {
    154164    NullMap<Node,Arc> myPredMap;
    155     dijkstra(G,length).predMap(myPredMap).run(s);
     165    dijkstra(G,length,s).predMap(myPredMap).run();
    156166  }
    157167}
    158168