COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/alpar/dijkstra.h @ 1116:f97e1cbbd453

Last change on this file since 1116:f97e1cbbd453 was 1116:f97e1cbbd453, checked in by Alpar Juttner, 20 years ago
  • More or less follows the new naming convetions
  • New implementation for dijkstra();
File size: 18.4 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
[921]17#ifndef LEMON_DIJKSTRA_H
18#define LEMON_DIJKSTRA_H
[255]19
[758]20///\ingroup flowalgs
[255]21///\file
22///\brief Dijkstra algorithm.
23
[953]24#include <lemon/list_graph.h>
[921]25#include <lemon/bin_heap.h>
26#include <lemon/invalid.h>
[255]27
[921]28namespace lemon {
[385]29
[758]30/// \addtogroup flowalgs
[430]31/// @{
32
[954]33  ///Default traits class of Dijkstra class.
34
35  ///Default traits class of Dijkstra class.
36  ///\param GR Graph type.
37  ///\param LM Type of length map.
[953]38  template<class GR, class LM>
39  struct DijkstraDefaultTraits
40  {
[954]41    ///The graph type the algorithm runs on.
[953]42    typedef GR Graph;
43    ///The type of the map that stores the edge lengths.
44
[967]45    ///It must meet the \ref concept::ReadMap "ReadMap" concept.
[953]46    ///
47    typedef LM LengthMap;
[954]48    //The type of the length of the edges.
[987]49    typedef typename LM::Value Value;
[954]50    ///The heap type used by Dijkstra algorithm.
[967]51
52    ///The heap type used by Dijkstra algorithm.
53    ///
54    ///\sa BinHeap
55    ///\sa Dijkstra
[953]56    typedef BinHeap<typename Graph::Node,
[987]57                    typename LM::Value,
[953]58                    typename GR::template NodeMap<int>,
[987]59                    std::less<Value> > Heap;
[953]60
61    ///\brief The type of the map that stores the last
62    ///edges of the shortest paths.
63    ///
[967]64    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
[953]65    ///
[954]66    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
67    ///Instantiates a PredMap.
[953]68 
69    ///\todo Please document...
70    ///
[954]71    static PredMap *createPredMap(const GR &G)
[953]72    {
73      return new PredMap(G);
74    }
75    ///\brief The type of the map that stores the last but one
76    ///nodes of the shortest paths.
77    ///
[967]78    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
[953]79    ///
[954]80    typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
81    ///Instantiates a PredNodeMap.
[953]82 
83    ///\todo Please document...
[967]84    ///
[954]85    static PredNodeMap *createPredNodeMap(const GR &G)
[953]86    {
87      return new PredNodeMap(G);
88    }
89    ///The type of the map that stores the dists of the nodes.
90 
[967]91    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
[953]92    ///
[987]93    typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
[954]94    ///Instantiates a DistMap.
[953]95 
96    ///\todo Please document...
97    ///
[954]98    static DistMap *createDistMap(const GR &G)
[953]99    {
100      return new DistMap(G);
101    }
102  };
103 
[255]104  ///%Dijkstra algorithm class.
105
106  ///This class provides an efficient implementation of %Dijkstra algorithm.
107  ///The edge lengths are passed to the algorithm using a
[959]108  ///\ref concept::ReadMap "ReadMap",
[255]109  ///so it is easy to change it to any kind of length.
110  ///
[880]111  ///The type of the length is determined by the
[987]112  ///\ref concept::ReadMap::Value "Value" of the length map.
[255]113  ///
114  ///It is also possible to change the underlying priority heap.
115  ///
[953]116  ///\param GR The graph type the algorithm runs on. The default value is
[955]117  ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
[954]118  ///is only passed to \ref DijkstraDefaultTraits.
[584]119  ///\param LM This read-only
[385]120  ///EdgeMap
121  ///determines the
122  ///lengths of the edges. It is read once for each edge, so the map
123  ///may involve in relatively time consuming process to compute the edge
124  ///length if it is necessary. The default map type is
[959]125  ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
[955]126  ///The value of LM is not used directly by Dijkstra, it
[954]127  ///is only passed to \ref DijkstraDefaultTraits.
128  ///\param TR Traits class to set various data types used by the algorithm.
129  ///The default traits class is
[955]130  ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
[954]131  ///See \ref DijkstraDefaultTraits for the documentation of
132  ///a Dijkstra traits class.
[456]133  ///
[689]134  ///\author Jacint Szabo and Alpar Juttner
[693]135  ///\todo We need a typedef-names should be standardized. (-:
[584]136
[255]137#ifdef DOXYGEN
[584]138  template <typename GR,
139            typename LM,
[953]140            typename TR>
[255]141#else
[953]142  template <typename GR=ListGraph,
[584]143            typename LM=typename GR::template EdgeMap<int>,
[953]144            typename TR=DijkstraDefaultTraits<GR,LM> >
[255]145#endif
[1116]146  class Dijkstra {
[255]147  public:
[953]148    typedef TR Traits;
[584]149    ///The type of the underlying graph.
[954]150    typedef typename TR::Graph Graph;
[911]151    ///\e
[255]152    typedef typename Graph::Node Node;
[911]153    ///\e
[255]154    typedef typename Graph::NodeIt NodeIt;
[911]155    ///\e
[255]156    typedef typename Graph::Edge Edge;
[911]157    ///\e
[255]158    typedef typename Graph::OutEdgeIt OutEdgeIt;
159   
[584]160    ///The type of the length of the edges.
[987]161    typedef typename TR::LengthMap::Value Value;
[693]162    ///The type of the map that stores the edge lengths.
[954]163    typedef typename TR::LengthMap LengthMap;
[693]164    ///\brief The type of the map that stores the last
[584]165    ///edges of the shortest paths.
[953]166    typedef typename TR::PredMap PredMap;
[693]167    ///\brief The type of the map that stores the last but one
[584]168    ///nodes of the shortest paths.
[953]169    typedef typename TR::PredNodeMap PredNodeMap;
[693]170    ///The type of the map that stores the dists of the nodes.
[953]171    typedef typename TR::DistMap DistMap;
172    ///The heap type used by the dijkstra algorithm.
173    typedef typename TR::Heap Heap;
[255]174  private:
[802]175    /// Pointer to the underlying graph.
[688]176    const Graph *G;
[802]177    /// Pointer to the length map
[954]178    const LengthMap *length;
[802]179    ///Pointer to the map of predecessors edges.
[688]180    PredMap *predecessor;
[802]181    ///Indicates if \ref predecessor is locally allocated (\c true) or not.
[688]182    bool local_predecessor;
[802]183    ///Pointer to the map of predecessors nodes.
[688]184    PredNodeMap *pred_node;
[802]185    ///Indicates if \ref pred_node is locally allocated (\c true) or not.
[688]186    bool local_pred_node;
[802]187    ///Pointer to the map of distances.
[688]188    DistMap *distance;
[802]189    ///Indicates if \ref distance is locally allocated (\c true) or not.
[688]190    bool local_distance;
191
[802]192    ///The source node of the last execution.
[774]193    Node source;
194
[785]195    ///Initializes the maps.
[688]196   
[694]197    ///\todo Error if \c G or are \c NULL. What about \c length?
[688]198    ///\todo Better memory allocation (instead of new).
199    void init_maps()
200    {
201      if(!predecessor) {
202        local_predecessor = true;
[953]203        predecessor = Traits::createPredMap(*G);
[688]204      }
205      if(!pred_node) {
206        local_pred_node = true;
[953]207        pred_node = Traits::createPredNodeMap(*G);
[688]208      }
209      if(!distance) {
210        local_distance = true;
[953]211        distance = Traits::createDistMap(*G);
[688]212      }
213    }
[255]214   
215  public :
[1116]216 
[953]217    template <class T>
[1116]218    struct DefPredMapTraits : public Traits {
[953]219      typedef T PredMap;
220      ///\todo An exception should be thrown.
221      ///
222      static PredMap *createPredMap(const Graph &G)
223      {
224        std::cerr << __FILE__ ":" << __LINE__ <<
225          ": error: Special maps should be manually created" << std::endl;
226        exit(1);
227      }
228    };
[954]229    ///\ref named-templ-param "Named parameter" for setting PredMap type
230
231    ///\ref named-templ-param "Named parameter" for setting PredMap type
[1043]232    ///
[953]233    template <class T>
[1116]234    class DefPredMap : public Dijkstra< Graph,
[953]235                                        LengthMap,
[1116]236                                        DefPredMapTraits<T> > { };
[953]237   
238    template <class T>
[1116]239    struct DefPredNodeMapTraits : public Traits {
[953]240      typedef T PredNodeMap;
241      ///\todo An exception should be thrown.
242      ///
243      static PredNodeMap *createPredNodeMap(const Graph &G)
244      {
245        std::cerr << __FILE__ ":" << __LINE__ <<
246          ": error: Special maps should be manually created" << std::endl;
247        exit(1);
248      }
249    };
[954]250    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
251
252    ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
[1043]253    ///
[953]254    template <class T>
[1116]255    class DefPredNodeMap : public Dijkstra< Graph,
[953]256                                            LengthMap,
[1116]257                                            DefPredNodeMapTraits<T> > { };
[953]258   
259    template <class T>
[1116]260    struct DefDistMapTraits : public Traits {
[953]261      typedef T DistMap;
262      ///\todo An exception should be thrown.
263      ///
264      static DistMap *createDistMap(const Graph &G)
265      {
266        std::cerr << __FILE__ ":" << __LINE__ <<
267          ": error: Special maps should be manually created" << std::endl;
268        exit(1);
269      }
270    };
[954]271    ///\ref named-templ-param "Named parameter" for setting DistMap type
272
273    ///\ref named-templ-param "Named parameter" for setting DistMap type
[1043]274    ///
[953]275    template <class T>
[1116]276    class DefDistMap : public Dijkstra< Graph,
[953]277                                        LengthMap,
[1116]278                                        DefDistMapTraits<T> > { };
[953]279   
[802]280    ///Constructor.
[255]281   
[802]282    ///\param _G the graph the algorithm will run on.
283    ///\param _length the length map used by the algorithm.
[954]284    Dijkstra(const Graph& _G, const LengthMap& _length) :
[688]285      G(&_G), length(&_length),
[707]286      predecessor(NULL), local_predecessor(false),
287      pred_node(NULL), local_pred_node(false),
288      distance(NULL), local_distance(false)
[688]289    { }
290   
[802]291    ///Destructor.
[688]292    ~Dijkstra()
293    {
294      if(local_predecessor) delete predecessor;
295      if(local_pred_node) delete pred_node;
296      if(local_distance) delete distance;
297    }
298
299    ///Sets the length map.
300
301    ///Sets the length map.
302    ///\return <tt> (*this) </tt>
[1116]303    Dijkstra &lengthMap(const LengthMap &m)
[688]304    {
305      length = &m;
306      return *this;
307    }
308
309    ///Sets the map storing the predecessor edges.
310
311    ///Sets the map storing the predecessor edges.
312    ///If you don't use this function before calling \ref run(),
313    ///it will allocate one. The destuctor deallocates this
314    ///automatically allocated map, of course.
315    ///\return <tt> (*this) </tt>
[1116]316    Dijkstra &predMap(PredMap &m)
[688]317    {
318      if(local_predecessor) {
319        delete predecessor;
320        local_predecessor=false;
321      }
322      predecessor = &m;
323      return *this;
324    }
325
326    ///Sets the map storing the predecessor nodes.
327
328    ///Sets the map storing the predecessor nodes.
329    ///If you don't use this function before calling \ref run(),
330    ///it will allocate one. The destuctor deallocates this
331    ///automatically allocated map, of course.
332    ///\return <tt> (*this) </tt>
[1116]333    Dijkstra &predNodeMap(PredNodeMap &m)
[688]334    {
335      if(local_pred_node) {
336        delete pred_node;
337        local_pred_node=false;
338      }
339      pred_node = &m;
340      return *this;
341    }
342
343    ///Sets the map storing the distances calculated by the algorithm.
344
345    ///Sets the map storing the distances calculated by the algorithm.
346    ///If you don't use this function before calling \ref run(),
347    ///it will allocate one. The destuctor deallocates this
348    ///automatically allocated map, of course.
349    ///\return <tt> (*this) </tt>
[1116]350    Dijkstra &distMap(DistMap &m)
[688]351    {
352      if(local_distance) {
353        delete distance;
354        local_distance=false;
355      }
356      distance = &m;
357      return *this;
358    }
[255]359   
[694]360  ///Runs %Dijkstra algorithm from node \c s.
361
362  ///This method runs the %Dijkstra algorithm from a root node \c s
363  ///in order to
364  ///compute the
365  ///shortest path to each node. The algorithm computes
366  ///- The shortest path tree.
367  ///- The distance of each node from the root.
[954]368  ///\todo heap_map's type could also be in the traits class.
[694]369    void run(Node s) {
370     
371      init_maps();
372     
[774]373      source = s;
374     
375      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
[694]376        predecessor->set(u,INVALID);
377        pred_node->set(u,INVALID);
378      }
379     
[954]380      typename Graph::template NodeMap<int> heap_map(*G,-1);
[694]381     
[953]382      Heap heap(heap_map);
[694]383     
384      heap.push(s,0);
385     
386      while ( !heap.empty() ) {
387       
388        Node v=heap.top();
[987]389        Value oldvalue=heap[v];
[694]390        heap.pop();
391        distance->set(v, oldvalue);
392       
393       
[774]394        for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
[986]395          Node w=G->target(e);
[694]396          switch(heap.state(w)) {
[953]397          case Heap::PRE_HEAP:
[694]398            heap.push(w,oldvalue+(*length)[e]);
399            predecessor->set(w,e);
400            pred_node->set(w,v);
401            break;
[953]402          case Heap::IN_HEAP:
[694]403            if ( oldvalue+(*length)[e] < heap[w] ) {
404              heap.decrease(w, oldvalue+(*length)[e]);
405              predecessor->set(w,e);
406              pred_node->set(w,v);
407            }
408            break;
[953]409          case Heap::POST_HEAP:
[694]410            break;
411          }
412        }
413      }
414    }
[255]415   
[385]416    ///The distance of a node from the root.
[255]417
[385]418    ///Returns the distance of a node from the root.
[255]419    ///\pre \ref run() must be called before using this function.
[385]420    ///\warning If node \c v in unreachable from the root the return value
[255]421    ///of this funcion is undefined.
[987]422    Value dist(Node v) const { return (*distance)[v]; }
[373]423
[584]424    ///Returns the 'previous edge' of the shortest path tree.
[255]425
[584]426    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
[785]427    ///i.e. it returns the last edge of a shortest path from the root to \c
[688]428    ///v. It is \ref INVALID
429    ///if \c v is unreachable from the root or if \c v=s. The
[385]430    ///shortest path tree used here is equal to the shortest path tree used in
431    ///\ref predNode(Node v).  \pre \ref run() must be called before using
432    ///this function.
[780]433    ///\todo predEdge could be a better name.
[688]434    Edge pred(Node v) const { return (*predecessor)[v]; }
[373]435
[584]436    ///Returns the 'previous node' of the shortest path tree.
[255]437
[584]438    ///For a node \c v it returns the 'previous node' of the shortest path tree,
[385]439    ///i.e. it returns the last but one node from a shortest path from the
440    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
441    ///\c v=s. The shortest path tree used here is equal to the shortest path
442    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
443    ///using this function.
[688]444    Node predNode(Node v) const { return (*pred_node)[v]; }
[255]445   
446    ///Returns a reference to the NodeMap of distances.
447
[385]448    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
449    ///be called before using this function.
[688]450    const DistMap &distMap() const { return *distance;}
[385]451 
[255]452    ///Returns a reference to the shortest path tree map.
453
454    ///Returns a reference to the NodeMap of the edges of the
455    ///shortest path tree.
456    ///\pre \ref run() must be called before using this function.
[688]457    const PredMap &predMap() const { return *predecessor;}
[385]458 
459    ///Returns a reference to the map of nodes of shortest paths.
[255]460
461    ///Returns a reference to the NodeMap of the last but one nodes of the
[385]462    ///shortest path tree.
[255]463    ///\pre \ref run() must be called before using this function.
[688]464    const PredNodeMap &predNodeMap() const { return *pred_node;}
[255]465
[385]466    ///Checks if a node is reachable from the root.
[255]467
[385]468    ///Returns \c true if \c v is reachable from the root.
[802]469    ///\note The root node is reported to be reached!
[255]470    ///\pre \ref run() must be called before using this function.
[385]471    ///
[780]472    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
[255]473   
474  };
[953]475
[1116]476  template<class GR,class LM>
477  class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
478  {
479
480    typedef DijkstraDefaultTraits<GR,LM> Base;
481  protected:
482    /// Pointer to the underlying graph.
483    void *_g;
484    /// Pointer to the length map
485    void *_length;
486    ///Pointer to the map of predecessors edges.
487    void *_pred;
488    ///Pointer to the map of predecessors nodes.
489    void *_predNode;
490    ///Pointer to the map of distances.
491    void *_dist;
492    ///Pointer to the source node.
493    void *_source;
494
495    typedef typename Base::Graph::Node Node;
496
497    public:
498    DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
499                       _dist(0), _source(INVALID) {}
500
501    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
502      _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
503                  _dist(0), _source((void *)&s) {}
504
505  };
506 
[953]507  ///\e
508
509  ///\e
510  ///
511  template<class TR>
[1116]512  class DijkstraWizard : public TR
[953]513  {
[1116]514    typedef TR Base;
[953]515
516    ///The type of the underlying graph.
517    typedef typename TR::Graph Graph;
518    ///\e
519    typedef typename Graph::Node Node;
520    ///\e
521    typedef typename Graph::NodeIt NodeIt;
522    ///\e
523    typedef typename Graph::Edge Edge;
524    ///\e
525    typedef typename Graph::OutEdgeIt OutEdgeIt;
526   
527    ///The type of the map that stores the edge lengths.
528    typedef typename TR::LengthMap LengthMap;
529    ///The type of the length of the edges.
[987]530    typedef typename LengthMap::Value Value;
[953]531    ///\brief The type of the map that stores the last
532    ///edges of the shortest paths.
533    typedef typename TR::PredMap PredMap;
534    ///\brief The type of the map that stores the last but one
535    ///nodes of the shortest paths.
536    typedef typename TR::PredNodeMap PredNodeMap;
537    ///The type of the map that stores the dists of the nodes.
538    typedef typename TR::DistMap DistMap;
539
540    ///The heap type used by the dijkstra algorithm.
541    typedef typename TR::Heap Heap;
[1116]542public:
543    DijkstraWizard() : TR() {}
[953]544
[1116]545    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
546      TR(g,l,s) {}
[953]547
[1116]548    DijkstraWizard(const TR &b) : TR(b) {}
[953]549
[1116]550    ~DijkstraWizard() {}
551
552    ///\e
553    void run()
[953]554    {
[1116]555      Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
556      if(_pred) Dij.predMap(*(PredMap*)_pred);
557      if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
558      if(_dist) Dij.distMap(*(DistMap*)_dist);
559      Dij.run(*(Node*)_source);
560    }
561
562    ///\e
563    void run(Node s)
564    {
565      _source=(void *)&s;
566      run();
[953]567    }
568
569    template<class T>
[1116]570    struct DefPredMapBase : public Base {
571      typedef T PredMap;
572      static PredMap *createPredMap(const Graph &G) {};
573    };
[953]574   
575    ///\e
576    template<class T>
[1116]577    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
[953]578    {
[1116]579      _pred=(void *)&t;
580      return DijkstraWizard<DefPredMapBase<T> >(*this);
[953]581    }
582   
[1116]583
[953]584    template<class T>
[1116]585    struct DefPredNodeMapBase : public Base {
586      typedef T PredNodeMap;
587      static PredNodeMap *createPredNodeMap(const Graph &G) {};
588    };
589   
[953]590    ///\e
591    template<class T>
[1116]592    DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
[953]593    {
[1116]594      _predNode=(void *)&t;
595      return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
[953]596    }
[1116]597   
598    template<class T>
599    struct DefDistMapBase : public Base {
600      typedef T DistMap;
601      static DistMap *createDistMap(const Graph &G) {};
602    };
[953]603   
604    ///\e
605    template<class T>
[1116]606    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
[953]607    {
[1116]608      _dist=(void *)&t;
609      return DijkstraWizard<DefDistMapBase<T> >(*this);
[953]610    }
[1116]611
[953]612    ///\e
[1116]613    DijkstraWizard<TR> &setSource(Node s)
[953]614    {
[1116]615      source=(void *)&s;
[953]616      return *this;
617    }
618   
619  };
[255]620 
[953]621  ///\e
622
[954]623  ///\todo Please document...
[953]624  ///
625  template<class GR, class LM>
[1116]626  DijkstraWizard<DijkstraWizardBase<GR,LM> >
627  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
[953]628  {
[1116]629    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
[953]630  }
631
[430]632/// @}
[255]633 
[921]634} //END OF NAMESPACE LEMON
[255]635
636#endif
637
638
Note: See TracBrowser for help on using the repository browser.