Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

dijkstra.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_DIJKSTRA_H
00018 #define LEMON_DIJKSTRA_H
00019 
00025 
00026 #include <lemon/list_graph.h>
00027 #include <lemon/bin_heap.h>
00028 #include <lemon/invalid.h>
00029 #include <lemon/error.h>
00030 #include <lemon/maps.h>
00031 
00032 namespace lemon {
00033 
00034 
00035   
00037 
00041   template<class GR, class LM>
00042   struct DijkstraDefaultTraits
00043   {
00045     typedef GR Graph;
00047 
00050     typedef LM LengthMap;
00051     //The type of the length of the edges.
00052     typedef typename LM::Value Value;
00054 
00059     typedef BinHeap<typename Graph::Node,
00060                     typename LM::Value,
00061                     typename GR::template NodeMap<int>,
00062                     std::less<Value> > Heap;
00063 
00071     typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
00073  
00077     static PredMap *createPredMap(const GR &G) 
00078     {
00079       return new PredMap(G);
00080     }
00081 //     ///\brief The type of the map that stores the last but one
00082 //     ///nodes of the shortest paths.
00083 //     ///
00084 //     ///The type of the map that stores the last but one
00085 //     ///nodes of the shortest paths.
00086 //     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
00087 //     ///
00088 //     typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
00089 //     ///Instantiates a PredNodeMap.
00090     
00091 //     ///This function instantiates a \ref PredNodeMap. 
00092 //     ///\param G is the graph, to which
00093 //     ///we would like to define the \ref PredNodeMap
00094 //     static PredNodeMap *createPredNodeMap(const GR &G)
00095 //     {
00096 //       return new PredNodeMap();
00097 //     }
00098 
00100  
00107     typedef NullMap<typename Graph::Node,bool> ProcessedMap;
00109  
00113 #ifdef DOXYGEN
00114     static ProcessedMap *createProcessedMap(const GR &g)
00115 #else
00116     static ProcessedMap *createProcessedMap(const GR &)
00117 #endif
00118     {
00119       return new ProcessedMap();
00120     }
00122  
00126     typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
00128  
00131     static DistMap *createDistMap(const GR &G)
00132     {
00133       return new DistMap(G);
00134     }
00135   };
00136   
00138   
00168 
00169 #ifdef DOXYGEN
00170   template <typename GR,
00171             typename LM,
00172             typename TR>
00173 #else
00174   template <typename GR=ListGraph,
00175             typename LM=typename GR::template EdgeMap<int>,
00176             typename TR=DijkstraDefaultTraits<GR,LM> >
00177 #endif
00178   class Dijkstra {
00179   public:
00186     class UninitializedParameter : public lemon::UninitializedParameter {
00187     public:
00188       virtual const char* exceptionName() const {
00189         return "lemon::Dijkstra::UninitializedParameter";
00190       }
00191     };
00192 
00193     typedef TR Traits;
00195     typedef typename TR::Graph Graph;
00197     typedef typename Graph::Node Node;
00199     typedef typename Graph::NodeIt NodeIt;
00201     typedef typename Graph::Edge Edge;
00203     typedef typename Graph::OutEdgeIt OutEdgeIt;
00204     
00206     typedef typename TR::LengthMap::Value Value;
00208     typedef typename TR::LengthMap LengthMap;
00211     typedef typename TR::PredMap PredMap;
00212 //     ///\brief The type of the map that stores the last but one
00213 //     ///nodes of the shortest paths.
00214 //     typedef typename TR::PredNodeMap PredNodeMap;
00216     typedef typename TR::ProcessedMap ProcessedMap;
00218     typedef typename TR::DistMap DistMap;
00220     typedef typename TR::Heap Heap;
00221   private:
00223     const Graph *G;
00225     const LengthMap *length;
00227     PredMap *_pred;
00229     bool local_pred;
00230 //     ///Pointer to the map of predecessors nodes.
00231 //     PredNodeMap *_predNode;
00232 //     ///Indicates if \ref _predNode is locally allocated (\c true) or not.
00233 //     bool local_predNode;
00235     DistMap *_dist;
00237     bool local_dist;
00239     ProcessedMap *_processed;
00241     bool local_processed;
00242 
00243 //     ///The source node of the last execution.
00244 //     Node source;
00245 
00247     
00250     void create_maps() 
00251     {
00252       if(!_pred) {
00253         local_pred = true;
00254         _pred = Traits::createPredMap(*G);
00255       }
00256 //       if(!_predNode) {
00257 //      local_predNode = true;
00258 //      _predNode = Traits::createPredNodeMap(*G);
00259 //       }
00260       if(!_dist) {
00261         local_dist = true;
00262         _dist = Traits::createDistMap(*G);
00263       }
00264       if(!_processed) {
00265         local_processed = true;
00266         _processed = Traits::createProcessedMap(*G);
00267       }
00268     }
00269     
00270   public :
00271  
00273 
00275 
00276     template <class T>
00277     struct DefPredMapTraits : public Traits {
00278       typedef T PredMap;
00279       static PredMap *createPredMap(const Graph &G) 
00280       {
00281         throw UninitializedParameter();
00282       }
00283     };
00285 
00288     template <class T>
00289     class DefPredMap : public Dijkstra< Graph,
00290                                         LengthMap,
00291                                         DefPredMapTraits<T> > { };
00292     
00293 //     template <class T>
00294 //     struct DefPredNodeMapTraits : public Traits {
00295 //       typedef T PredNodeMap;
00296 //       static PredNodeMap *createPredNodeMap(const Graph &G) 
00297 //       {
00298 //      throw UninitializedParameter();
00299 //       }
00300 //     };
00301 //     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
00302 
00303 //     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
00304 //     ///
00305 //     template <class T>
00306 //     class DefPredNodeMap : public Dijkstra< Graph,
00307 //                                          LengthMap,
00308 //                                          DefPredNodeMapTraits<T> > { };
00309     
00310     template <class T>
00311     struct DefDistMapTraits : public Traits {
00312       typedef T DistMap;
00313       static DistMap *createDistMap(const Graph &G) 
00314       {
00315         throw UninitializedParameter();
00316       }
00317     };
00319 
00322     template <class T>
00323     class DefDistMap : public Dijkstra< Graph,
00324                                         LengthMap,
00325                                         DefDistMapTraits<T> > { };
00326     
00327     template <class T>
00328     struct DefProcessedMapTraits : public Traits {
00329       typedef T ProcessedMap;
00330       static ProcessedMap *createProcessedMap(const Graph &G) 
00331       {
00332         throw UninitializedParameter();
00333       }
00334     };
00336 
00339     template <class T>
00340     class DefProcessedMap : public Dijkstra< Graph,
00341                                         LengthMap,
00342                                         DefProcessedMapTraits<T> > { };
00343     
00344     struct DefGraphProcessedMapTraits : public Traits {
00345       typedef typename Graph::template NodeMap<bool> ProcessedMap;
00346       static ProcessedMap *createProcessedMap(const Graph &G) 
00347       {
00348         return new ProcessedMap(G);
00349       }
00350     };
00357     template <class T>
00358     class DefProcessedMapToBeDefaultMap :
00359       public Dijkstra< Graph,
00360                        LengthMap,
00361                        DefGraphProcessedMapTraits> { };
00362     
00364 
00365 
00366   private:
00367     typename Graph::template NodeMap<int> _heap_map;
00368     Heap _heap;
00369   public:      
00370     
00372     
00375     Dijkstra(const Graph& _G, const LengthMap& _length) :
00376       G(&_G), length(&_length),
00377       _pred(NULL), local_pred(false),
00378 //       _predNode(NULL), local_predNode(false),
00379       _dist(NULL), local_dist(false),
00380       _processed(NULL), local_processed(false),
00381       _heap_map(*G,-1),_heap(_heap_map)
00382     { }
00383     
00385     ~Dijkstra() 
00386     {
00387       if(local_pred) delete _pred;
00388 //       if(local_predNode) delete _predNode;
00389       if(local_dist) delete _dist;
00390       if(local_processed) delete _processed;
00391     }
00392 
00394 
00397     Dijkstra &lengthMap(const LengthMap &m) 
00398     {
00399       length = &m;
00400       return *this;
00401     }
00402 
00404 
00410     Dijkstra &predMap(PredMap &m) 
00411     {
00412       if(local_pred) {
00413         delete _pred;
00414         local_pred=false;
00415       }
00416       _pred = &m;
00417       return *this;
00418     }
00419 
00420 //     ///Sets the map storing the predecessor nodes.
00421 
00422 //     ///Sets the map storing the predecessor nodes.
00423 //     ///If you don't use this function before calling \ref run(),
00424 //     ///it will allocate one. The destuctor deallocates this
00425 //     ///automatically allocated map, of course.
00426 //     ///\return <tt> (*this) </tt>
00427 //     Dijkstra &predNodeMap(PredNodeMap &m) 
00428 //     {
00429 //       if(local_predNode) {
00430 //      delete _predNode;
00431 //      local_predNode=false;
00432 //       }
00433 //       _predNode = &m;
00434 //       return *this;
00435 //     }
00436 
00438 
00444     Dijkstra &distMap(DistMap &m) 
00445     {
00446       if(local_dist) {
00447         delete _dist;
00448         local_dist=false;
00449       }
00450       _dist = &m;
00451       return *this;
00452     }
00453 
00454   private:
00455     void finalizeNodeData(Node v,Value dst)
00456     {
00457       _processed->set(v,true);
00458       _dist->set(v, dst);
00459 //       if((*_pred)[v]!=INVALID)
00460 //       _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do?
00461     }
00462 
00463   public:
00473 
00475 
00477 
00482     void init()
00483     {
00484       create_maps();
00485       while(!_heap.empty()) _heap.pop();
00486       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
00487         _pred->set(u,INVALID);
00488 //      _predNode->set(u,INVALID);
00489         _processed->set(u,false);
00490         _heap_map.set(u,Heap::PRE_HEAP);
00491       }
00492     }
00493     
00495 
00503     void addSource(Node s,Value dst=0)
00504     {
00505 //       source = s;
00506       if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
00507       else if(_heap[s]<dst) {
00508         _heap.push(s,dst);
00509         _pred->set(s,INVALID);
00510       }
00511     }
00512     
00514 
00520     Node processNextNode()
00521     {
00522       Node v=_heap.top(); 
00523       Value oldvalue=_heap[v];
00524       _heap.pop();
00525       finalizeNodeData(v,oldvalue);
00526       
00527       for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
00528         Node w=G->target(e); 
00529         switch(_heap.state(w)) {
00530         case Heap::PRE_HEAP:
00531           _heap.push(w,oldvalue+(*length)[e]); 
00532           _pred->set(w,e);
00533 //        _predNode->set(w,v);
00534           break;
00535         case Heap::IN_HEAP:
00536           if ( oldvalue+(*length)[e] < _heap[w] ) {
00537             _heap.decrease(w, oldvalue+(*length)[e]); 
00538             _pred->set(w,e);
00539 //          _predNode->set(w,v);
00540           }
00541           break;
00542         case Heap::POST_HEAP:
00543           break;
00544         }
00545       }
00546       return v;
00547     }
00548 
00554     bool emptyQueue() { return _heap.empty(); }
00556 
00559     int queueSize() { return _heap.size(); }
00560     
00562 
00575     void start()
00576     {
00577       while ( !_heap.empty() ) processNextNode();
00578     }
00579     
00581 
00594     void start(Node dest)
00595     {
00596       while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
00597       if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
00598     }
00599     
00601 
00609     template<class NodeBoolMap>
00610     void start(const NodeBoolMap &nm)
00611     {
00612       while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
00613       if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
00614     }
00615     
00617     
00631     void run(Node s) {
00632       init();
00633       addSource(s);
00634       start();
00635     }
00636     
00638     
00650     Value run(Node s,Node t) {
00651       init();
00652       addSource(s);
00653       start(t);
00654       return (*_pred)[t]==INVALID?0:(*_dist)[t];
00655     }
00656     
00658 
00664     
00666 
00668     
00676     template<class P>
00677     bool getPath(P &p,Node t) 
00678     {
00679       if(reached(t)) {
00680         p.clear();
00681         typename P::Builder b(p);
00682         for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
00683           b.pushFront(pred(t));
00684         b.commit();
00685         return true;
00686       }
00687       return false;
00688     }
00689           
00691 
00696     Value dist(Node v) const { return (*_dist)[v]; }
00697 
00699 
00708     Edge pred(Node v) const { return (*_pred)[v]; }
00709 
00711 
00718     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
00719                                   G->source((*_pred)[v]); }
00720     
00722 
00725     const DistMap &distMap() const { return *_dist;}
00726  
00728 
00732     const PredMap &predMap() const { return *_pred;}
00733  
00734 //     ///Returns a reference to the map of nodes of shortest paths.
00735 
00736 //     ///Returns a reference to the NodeMap of the last but one nodes of the
00737 //     ///shortest path tree.
00738 //     ///\pre \ref run() must be called before using this function.
00739 //     const PredNodeMap &predNodeMap() const { return *_predNode;}
00740 
00742 
00747     bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
00748     
00750   };
00751 
00752 
00753 
00754 
00755  
00757 
00761   template<class GR, class LM>
00762   struct DijkstraWizardDefaultTraits
00763   {
00765     typedef GR Graph;
00767 
00770     typedef LM LengthMap;
00771     //The type of the length of the edges.
00772     typedef typename LM::Value Value;
00774 
00779     typedef BinHeap<typename Graph::Node,
00780                     typename LM::Value,
00781                     typename GR::template NodeMap<int>,
00782                     std::less<Value> > Heap;
00783 
00791     typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
00793  
00797 #ifdef DOXYGEN
00798     static PredMap *createPredMap(const GR &g) 
00799 #else
00800     static PredMap *createPredMap(const GR &) 
00801 #endif
00802     {
00803       return new PredMap();
00804     }
00806  
00813     typedef NullMap<typename Graph::Node,bool> ProcessedMap;
00815  
00819 #ifdef DOXYGEN
00820     static ProcessedMap *createProcessedMap(const GR &g)
00821 #else
00822     static ProcessedMap *createProcessedMap(const GR &)
00823 #endif
00824     {
00825       return new ProcessedMap();
00826     }
00828  
00832     typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
00834  
00837 #ifdef DOXYGEN
00838     static DistMap *createDistMap(const GR &g)
00839 #else
00840     static DistMap *createDistMap(const GR &)
00841 #endif
00842     {
00843       return new DistMap();
00844     }
00845   };
00846   
00848 
00856   template<class GR,class LM>
00857   class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
00858   {
00859 
00860     typedef DijkstraWizardDefaultTraits<GR,LM> Base;
00861   protected:
00863     typedef typename Base::Graph::Node Node;
00864 
00866     void *_g;
00868     void *_length;
00870     void *_pred;
00871 //     ///Pointer to the map of predecessors nodes.
00872 //     void *_predNode;
00874     void *_dist;
00876     Node _source;
00877 
00878     public:
00880     
00883     DijkstraWizardBase() : _g(0), _length(0), _pred(0),
00884 //                         _predNode(0),
00885                            _dist(0), _source(INVALID) {}
00886 
00888     
00895     DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
00896       _g((void *)&g), _length((void *)&l), _pred(0),
00897 //       _predNode(0),
00898       _dist(0), _source(s) {}
00899 
00900   };
00901   
00903 
00921   template<class TR>
00922   class DijkstraWizard : public TR
00923   {
00924     typedef TR Base;
00925 
00927     typedef typename TR::Graph Graph;
00928     //\e
00929     typedef typename Graph::Node Node;
00930     //\e
00931     typedef typename Graph::NodeIt NodeIt;
00932     //\e
00933     typedef typename Graph::Edge Edge;
00934     //\e
00935     typedef typename Graph::OutEdgeIt OutEdgeIt;
00936     
00938     typedef typename TR::LengthMap LengthMap;
00940     typedef typename LengthMap::Value Value;
00943     typedef typename TR::PredMap PredMap;
00944 //     ///\brief The type of the map that stores the last but one
00945 //     ///nodes of the shortest paths.
00946 //     typedef typename TR::PredNodeMap PredNodeMap;
00948     typedef typename TR::DistMap DistMap;
00949 
00951     typedef typename TR::Heap Heap;
00952 public:
00954     DijkstraWizard() : TR() {}
00955 
00957 
00960     DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
00961       TR(g,l,s) {}
00962 
00964     DijkstraWizard(const TR &b) : TR(b) {}
00965 
00966     ~DijkstraWizard() {}
00967 
00969     
00972     void run()
00973     {
00974       if(Base::_source==INVALID) throw UninitializedParameter();
00975       Dijkstra<Graph,LengthMap,TR> 
00976         dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
00977       if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
00978 //       if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
00979       if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
00980       dij.run(Base::_source);
00981     }
00982 
00984 
00987     void run(Node s)
00988     {
00989       Base::_source=s;
00990       run();
00991     }
00992 
00993     template<class T>
00994     struct DefPredMapBase : public Base {
00995       typedef T PredMap;
00996       static PredMap *createPredMap(const Graph &) { return 0; };
00997       DefPredMapBase(const TR &b) : TR(b) {}
00998     };
00999     
01006     template<class T>
01007     DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
01008     {
01009       Base::_pred=(void *)&t;
01010       return DijkstraWizard<DefPredMapBase<T> >(*this);
01011     }
01012     
01013 
01014 //     template<class T>
01015 //     struct DefPredNodeMapBase : public Base {
01016 //       typedef T PredNodeMap;
01017 //       static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
01018 //       DefPredNodeMapBase(const TR &b) : TR(b) {}
01019 //     };
01020     
01021 //     ///\brief \ref named-templ-param "Named parameter"
01022 //     ///function for setting PredNodeMap type
01023 //     ///
01024 //     /// \ref named-templ-param "Named parameter"
01025 //     ///function for setting PredNodeMap type
01026 //     ///
01027 //     template<class T>
01028 //     DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
01029 //     {
01030 //       Base::_predNode=(void *)&t;
01031 //       return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
01032 //     }
01033    
01034     template<class T>
01035     struct DefDistMapBase : public Base {
01036       typedef T DistMap;
01037       static DistMap *createDistMap(const Graph &) { return 0; };
01038       DefDistMapBase(const TR &b) : TR(b) {}
01039     };
01040     
01047     template<class T>
01048     DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
01049     {
01050       Base::_dist=(void *)&t;
01051       return DijkstraWizard<DefDistMapBase<T> >(*this);
01052     }
01053     
01055 
01058     DijkstraWizard<TR> &source(Node s) 
01059     {
01060       Base::_source=s;
01061       return *this;
01062     }
01063     
01064   };
01065   
01067 
01083   template<class GR, class LM>
01084   DijkstraWizard<DijkstraWizardBase<GR,LM> >
01085   dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
01086   {
01087     return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
01088   }
01089 
01090 } //END OF NAMESPACE LEMON
01091 
01092 #endif
01093 

Generated on Sat Aug 27 14:14:51 2005 for LEMON by  doxygen 1.4.4