COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/bfs.h

    r278 r258  
    2929#include <lemon/error.h>
    3030#include <lemon/maps.h>
    31 #include <lemon/path.h>
    3231
    3332namespace lemon {
     
    117116  ///This class provides an efficient implementation of the %BFS algorithm.
    118117  ///
    119   ///There is also a \ref bfs() "function-type interface" for the BFS
     118  ///There is also a \ref bfs() "function type interface" for the BFS
    120119  ///algorithm, which is convenient in the simplier cases and it can be
    121120  ///used easier.
     
    843842    ///arcs of the shortest paths.
    844843    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    845     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
     844    typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
    846845    ///Instantiates a \ref PredMap.
    847846
     
    850849    ///\ref PredMap.
    851850    ///\todo The digraph alone may be insufficient to initialize
     851#ifdef DOXYGEN
    852852    static PredMap *createPredMap(const Digraph &g)
    853     {
    854       return new PredMap(g);
     853#else
     854    static PredMap *createPredMap(const Digraph &)
     855#endif
     856    {
     857      return new PredMap();
    855858    }
    856859
     
    859862    ///The type of the map that indicates which nodes are processed.
    860863    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    861     ///By default it is a NullMap.
    862864    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    863865    ///Instantiates a \ref ProcessedMap.
     
    894896    ///The type of the map that stores the distances of the nodes.
    895897    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    896     typedef typename Digraph::template NodeMap<int> DistMap;
     898    ///
     899    typedef NullMap<typename Digraph::Node,int> DistMap;
    897900    ///Instantiates a \ref DistMap.
    898901
     
    900903    ///\param g is the digraph, to which we would like to define
    901904    ///the \ref DistMap
     905#ifdef DOXYGEN
    902906    static DistMap *createDistMap(const Digraph &g)
    903     {
    904       return new DistMap(g);
    905     }
    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;
     907#else
     908    static DistMap *createDistMap(const Digraph &)
     909#endif
     910    {
     911      return new DistMap();
     912    }
    912913  };
    913914
     
    939940    //Pointer to the map of distances.
    940941    void *_dist;
    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;
     942    //Pointer to the source node.
     943    Node _source;
    945944
    946945    public:
     
    948947
    949948    /// This constructor does not require parameters, therefore it initiates
    950     /// all of the attributes to \c 0.
     949    /// all of the attributes to default values (0, INVALID).
    951950    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
    952                       _dist(0), _path(0), _di(0) {}
     951                      _dist(0), _source(INVALID) {}
    953952
    954953    /// Constructor.
    955954
    956     /// This constructor requires one parameter,
    957     /// others are initiated to \c 0.
     955    /// This constructor requires some parameters,
     956    /// listed in the parameters list.
     957    /// Others are initiated to 0.
    958958    /// \param g The digraph the algorithm runs on.
    959     BfsWizardBase(const GR &g) :
     959    /// \param s The source node.
     960    BfsWizardBase(const GR &g, Node s=INVALID) :
    960961      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
    961       _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
     962      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
    962963
    963964  };
    964965
    965   /// Auxiliary class for the function-type interface of BFS algorithm.
    966 
    967   /// This auxiliary class is created to implement the
    968   /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
    969   /// It does not have own \ref run() method, it uses the functions
    970   /// and features of the plain \ref Bfs.
     966  /// Auxiliary class for the function type interface of BFS algorithm.
     967
     968  /// This auxiliary class is created to implement the function type
     969  /// interface of \ref Bfs algorithm. It uses the functions and features
     970  /// of the plain \ref Bfs, but it is much simpler to use it.
     971  /// It should only be used through the \ref bfs() function, which makes
     972  /// it easier to use the algorithm.
    971973  ///
    972   /// This class should only be used through the \ref bfs() function,
    973   /// which makes it easier to use the algorithm.
     974  /// Simplicity means that the way to change the types defined
     975  /// in the traits class is based on functions that returns the new class
     976  /// and not on templatable built-in classes.
     977  /// When using the plain \ref Bfs
     978  /// the new class with the modified type comes from
     979  /// the original class by using the ::
     980  /// operator. In the case of \ref BfsWizard only
     981  /// a function have to be called, and it will
     982  /// return the needed class.
     983  ///
     984  /// It does not have own \ref run() method. When its \ref run() method
     985  /// is called, it initiates a plain \ref Bfs object, and calls the
     986  /// \ref Bfs::run() method of it.
    974987  template<class TR>
    975988  class BfsWizard : public TR
     
    9941007    ///\brief The type of the map that indicates which nodes are processed.
    9951008    typedef typename TR::ProcessedMap ProcessedMap;
    996     ///The type of the shortest paths
    997     typedef typename TR::Path Path;
    9981009
    9991010  public:
     
    10061017    /// Constructor that requires parameters.
    10071018    /// These parameters will be the default values for the traits class.
    1008     /// \param g The digraph the algorithm runs on.
    1009     BfsWizard(const Digraph &g) :
    1010       TR(g) {}
     1019    BfsWizard(const Digraph &g, Node s=INVALID) :
     1020      TR(g,s) {}
    10111021
    10121022    ///Copy constructor
     
    10151025    ~BfsWizard() {}
    10161026
    1017     ///Runs BFS algorithm from the given source node.
    1018 
    1019     ///This method runs BFS algorithm from node \c s
    1020     ///in order to compute the shortest path to each node.
     1027    ///Runs BFS algorithm from a source node.
     1028
     1029    ///Runs BFS algorithm from a source node.
     1030    ///The node can be given with the \ref source() function.
     1031    void run()
     1032    {
     1033      if(Base::_source==INVALID) throw UninitializedParameter();
     1034      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
     1035      if(Base::_reached)
     1036        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
     1037      if(Base::_processed)
     1038        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
     1039      if(Base::_pred)
     1040        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
     1041      if(Base::_dist)
     1042        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.
    10211050    void run(Node s)
    10221051    {
    1023       Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
    1024       if (Base::_pred)
    1025         alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    1026       if (Base::_dist)
    1027         alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
    1028       if (Base::_reached)
    1029         alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
    1030       if (Base::_processed)
    1031         alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
    1032       if (s!=INVALID)
    1033         alg.run(s);
    1034       else
    1035         alg.run();
    1036     }
    1037 
    1038     ///Finds the shortest path between \c s and \c t.
    1039 
    1040     ///This method runs BFS algorithm from node \c s
    1041     ///in order to compute the shortest path to node \c t
    1042     ///(it stops searching when \c t is processed).
    1043     ///
    1044     ///\return \c true if \c t is reachable form \c s.
    1045     bool run(Node s, Node t)
    1046     {
    1047       if (s==INVALID || t==INVALID) throw UninitializedParameter();
    1048       Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
    1049       if (Base::_pred)
    1050         alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    1051       if (Base::_dist)
    1052         alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
    1053       if (Base::_reached)
    1054         alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
    1055       if (Base::_processed)
    1056         alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
    1057       alg.run(s,t);
    1058       if (Base::_path)
    1059         *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
    1060       if (Base::_di)
    1061         *Base::_di = alg.dist(t);
    1062       return alg.reached(t);
    1063     }
    1064 
    1065     ///Runs BFS algorithm to visit all nodes in the digraph.
    1066 
    1067     ///This method runs BFS algorithm in order to compute
    1068     ///the shortest path to each node.
    1069     void run()
    1070     {
    1071       run(INVALID);
     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;
    10721064    }
    10731065
     
    10781070      SetPredMapBase(const TR &b) : TR(b) {}
    10791071    };
    1080     ///\brief \ref named-func-param "Named parameter"
     1072    ///\brief \ref named-templ-param "Named parameter"
    10811073    ///for setting \ref PredMap object.
    10821074    ///
    1083     ///\ref named-func-param "Named parameter"
     1075    /// \ref named-templ-param "Named parameter"
    10841076    ///for setting \ref PredMap object.
    10851077    template<class T>
     
    10961088      SetReachedMapBase(const TR &b) : TR(b) {}
    10971089    };
    1098     ///\brief \ref named-func-param "Named parameter"
     1090    ///\brief \ref named-templ-param "Named parameter"
    10991091    ///for setting \ref ReachedMap object.
    11001092    ///
    1101     /// \ref named-func-param "Named parameter"
     1093    /// \ref named-templ-param "Named parameter"
    11021094    ///for setting \ref ReachedMap object.
    11031095    template<class T>
     
    11061098      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
    11071099      return BfsWizard<SetReachedMapBase<T> >(*this);
     1100    }
     1101
     1102    template<class T>
     1103    struct SetProcessedMapBase : public Base {
     1104      typedef T ProcessedMap;
     1105      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
     1106      SetProcessedMapBase(const TR &b) : TR(b) {}
     1107    };
     1108    ///\brief \ref named-templ-param "Named parameter"
     1109    ///for setting \ref ProcessedMap object.
     1110    ///
     1111    /// \ref named-templ-param "Named parameter"
     1112    ///for setting \ref ProcessedMap object.
     1113    template<class T>
     1114    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
     1115    {
     1116      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
     1117      return BfsWizard<SetProcessedMapBase<T> >(*this);
    11081118    }
    11091119
     
    11141124      SetDistMapBase(const TR &b) : TR(b) {}
    11151125    };
    1116     ///\brief \ref named-func-param "Named parameter"
     1126    ///\brief \ref named-templ-param "Named parameter"
    11171127    ///for setting \ref DistMap object.
    11181128    ///
    1119     /// \ref named-func-param "Named parameter"
     1129    /// \ref named-templ-param "Named parameter"
    11201130    ///for setting \ref DistMap object.
    11211131    template<class T>
     
    11261136    }
    11271137
    1128     template<class T>
    1129     struct SetProcessedMapBase : public Base {
    1130       typedef T ProcessedMap;
    1131       static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
    1132       SetProcessedMapBase(const TR &b) : TR(b) {}
    1133     };
    1134     ///\brief \ref named-func-param "Named parameter"
    1135     ///for setting \ref ProcessedMap object.
    1136     ///
    1137     /// \ref named-func-param "Named parameter"
    1138     ///for setting \ref ProcessedMap object.
    1139     template<class T>
    1140     BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
    1141     {
    1142       Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
    1143       return BfsWizard<SetProcessedMapBase<T> >(*this);
    1144     }
    1145 
    1146     template<class T>
    1147     struct SetPathBase : public Base {
    1148       typedef T Path;
    1149       SetPathBase(const TR &b) : TR(b) {}
    1150     };
    1151     ///\brief \ref named-func-param "Named parameter"
    1152     ///for getting the shortest path to the target node.
    1153     ///
    1154     ///\ref named-func-param "Named parameter"
    1155     ///for getting the shortest path to the target node.
    1156     template<class T>
    1157     BfsWizard<SetPathBase<T> > path(const T &t)
    1158     {
    1159       Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
    1160       return BfsWizard<SetPathBase<T> >(*this);
    1161     }
    1162 
    1163     ///\brief \ref named-func-param "Named parameter"
    1164     ///for getting the distance of the target node.
    1165     ///
    1166     ///\ref named-func-param "Named parameter"
    1167     ///for getting the distance of the target node.
    1168     BfsWizard dist(const int &d)
    1169     {
    1170       Base::_di=const_cast<int*>(&d);
    1171       return *this;
    1172     }
    1173 
    11741138  };
    11751139
    1176   ///Function-type interface for BFS algorithm.
     1140  ///Function type interface for Bfs algorithm.
    11771141
    11781142  /// \ingroup search
    1179   ///Function-type interface for BFS algorithm.
     1143  ///Function type interface for Bfs algorithm.
    11801144  ///
    1181   ///This function also has several \ref named-func-param "named parameters",
     1145  ///This function also has several
     1146  ///\ref named-templ-func-param "named parameters",
    11821147  ///they are declared as the members of class \ref BfsWizard.
    1183   ///The following examples show how to use these parameters.
     1148  ///The following
     1149  ///example shows how to use these parameters.
    11841150  ///\code
    1185   ///  // Compute shortest path from node s to each node
    1186   ///  bfs(g).predMap(preds).distMap(dists).run(s);
    1187   ///
    1188   ///  // Compute shortest path from s to t
    1189   ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
     1151  ///  bfs(g,source).predMap(preds).run();
    11901152  ///\endcode
    11911153  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
     
    11951157  template<class GR>
    11961158  BfsWizard<BfsWizardBase<GR> >
    1197   bfs(const GR &digraph)
     1159  bfs(const GR &g,typename GR::Node s=INVALID)
    11981160  {
    1199     return BfsWizard<BfsWizardBase<GR> >(digraph);
     1161    return BfsWizard<BfsWizardBase<GR> >(g,s);
    12001162  }
    12011163
Note: See TracChangeset for help on using the changeset viewer.