COIN-OR::LEMON - Graph Library

Ticket #96: dijk_work_e8e874ee792b.patch

File dijk_work_e8e874ee792b.patch, 9.8 KB (added by Peter Kovacs, 16 years ago)
  • lemon/concepts/path.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1221525397 -7200
    # Node ID e8e874ee792b40f2d967afb3e494f7e3f71848a9
    # Parent  c691064dfd4f2e3d4e230a1fd9845a1d96de2a5d
    Modify the function interface of dijkstra (ticket #96)
     - bug fix in concepts/path.h
     - make the source node a mandatory argument of dijkstra()
     - support s-t searches with dijkstra()
    
    diff -r c691064dfd4f -r e8e874ee792b 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/dijkstra.h

    diff -r c691064dfd4f -r e8e874ee792b 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;
     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;
    10761079    //Pointer to the source node.
    10771080    Node _source;
     1081    //Pointer to 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    DijkstraWizard(const Digraph &g,const LengthMap &l,
     1170                   Node s, Node t=INVALID) :
     1171      TR(g,l,s,t) {}
    11611172
    11621173    ///Copy constructor
    11631174    DijkstraWizard(const TR &b) : TR(b) {}
    11641175
    11651176    ~DijkstraWizard() {}
    11661177
    1167     ///Runs Dijkstra algorithm from a source node.
     1178    ///Runs Dijkstra algorithm from the given source node.
    11681179
    1169     ///Runs Dijkstra algorithm from a source node.
    1170     ///The node can be given with the \ref source() function.
    1171     void run()
     1180    ///Runs Dijkstra algorithm from the given source node.
     1181    ///\param t The optional target node.
     1182    void run(Node t=INVALID)
    11721183    {
     1184      if(t!=INVALID) Base::_target=t;
    11731185      if(Base::_source==INVALID) throw UninitializedParameter();
    11741186      Dijkstra<Digraph,LengthMap,TR>
    11751187        dij(*reinterpret_cast<const Digraph*>(Base::_g),
     
    11801192        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
    11811193      if(Base::_dist)
    11821194        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;
     1195      if(Base::_target!=INVALID) {
     1196        dij.run(Base::_source, Base::_target);
     1197        if (Base::_path)
     1198          *reinterpret_cast<Path*>(Base::_path) = dij.path(Base::_target);
     1199        if (Base::_di)
     1200          *reinterpret_cast<Value*>(Base::_di) = dij.dist(Base::_target);
     1201      } else {
     1202        dij.run(Base::_source);
     1203      }
    12041204    }
    12051205
    12061206    template<class T>
     
    12571257      return DijkstraWizard<SetDistMapBase<T> >(*this);
    12581258    }
    12591259
     1260    template<class T>
     1261    struct SetPathBase : public Base {
     1262      typedef T Path;
     1263      SetPathBase(const TR &b) : TR(b) {}
     1264    };
     1265    ///\brief \ref named-templ-param "Named parameter"
     1266    ///for getting the shortest path to the target node.
     1267    ///
     1268    ///\ref named-templ-param "Named parameter"
     1269    ///for getting the shortest path to the target node.
     1270    template<class T>
     1271    DijkstraWizard<SetPathBase<T> > path(const T &t)
     1272    {
     1273      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
     1274      return DijkstraWizard<SetPathBase<T> >(*this);
     1275    }
     1276
     1277    ///\brief \ref named-templ-param "Named parameter"
     1278    ///for getting the distance of the target node.
     1279    ///
     1280    ///\ref named-templ-param "Named parameter"
     1281    ///for getting the distance of the target node.
     1282    DijkstraWizard dist(const Value &d)
     1283    {
     1284      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
     1285      return *this;
     1286    }
     1287
    12601288  };
    12611289
    12621290  ///Function type interface for Dijkstra algorithm.
     
    12781306  ///\sa Dijkstra
    12791307  template<class GR, class LM>
    12801308  DijkstraWizard<DijkstraWizardBase<GR,LM> >
    1281   dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
     1309  dijkstra(const GR &g, const LM &l,
     1310           typename GR::Node s, typename GR::Node t=INVALID)
    12821311  {
    1283     return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
     1312    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s,t);
    12841313  }
    12851314
    12861315} //END OF NAMESPACE LEMON
  • test/dijkstra_test.cc

    diff -r c691064dfd4f -r e8e874ee792b test/dijkstra_test.cc
    a b  
    9292
    9393  Digraph g;
    9494  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>())
     95  dijkstra(g,LengthMap(),Node())
     96    .predMap(concepts::ReadWriteMap<Node,Arc>())
     97    .distMap(concepts::ReadWriteMap<Node,VType>())
     98    .processedMap(concepts::WriteMap<Node,bool>())
     99    .run();
     100  dijkstra(g,LengthMap(),Node(),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())
     105    .run();
     106  dijkstra(g,LengthMap(),Node())
     107    .predMap(concepts::ReadWriteMap<Node,Arc>())
     108    .distMap(concepts::ReadWriteMap<Node,VType>())
     109    .processedMap(concepts::WriteMap<Node,bool>())
     110    .path(concepts::Path<Digraph>()).dist(VType())
    99111    .run(Node());
    100112}
    101113
     
    152164
    153165  {
    154166    NullMap<Node,Arc> myPredMap;
    155     dijkstra(G,length).predMap(myPredMap).run(s);
     167    dijkstra(G,length,s).predMap(myPredMap).run();
    156168  }
    157169}
    158170