lemon/dijkstra.h
changeset 1705 3f63d9db307b
parent 1665 fdeb961110ac
child 1709 a323456bf7c8
equal deleted inserted replaced
4:1503ca16978b 5:ea2d7d19af93
    76     ///\todo The graph alone may be insufficient for the initialization
    76     ///\todo The graph alone may be insufficient for the initialization
    77     static PredMap *createPredMap(const GR &G) 
    77     static PredMap *createPredMap(const GR &G) 
    78     {
    78     {
    79       return new PredMap(G);
    79       return new PredMap(G);
    80     }
    80     }
    81 //     ///\brief The type of the map that stores the last but one
       
    82 //     ///nodes of the shortest paths.
       
    83 //     ///
       
    84 //     ///The type of the map that stores the last but one
       
    85 //     ///nodes of the shortest paths.
       
    86 //     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
       
    87 //     ///
       
    88 //     typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
       
    89 //     ///Instantiates a PredNodeMap.
       
    90     
       
    91 //     ///This function instantiates a \ref PredNodeMap. 
       
    92 //     ///\param G is the graph, to which
       
    93 //     ///we would like to define the \ref PredNodeMap
       
    94 //     static PredNodeMap *createPredNodeMap(const GR &G)
       
    95 //     {
       
    96 //       return new PredNodeMap();
       
    97 //     }
       
    98 
    81 
    99     ///The type of the map that stores whether a nodes is processed.
    82     ///The type of the map that stores whether a nodes is processed.
   100  
    83  
   101     ///The type of the map that stores whether a nodes is processed.
    84     ///The type of the map that stores whether a nodes is processed.
   102     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
    85     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
   207     ///The type of the map that stores the edge lengths.
   190     ///The type of the map that stores the edge lengths.
   208     typedef typename TR::LengthMap LengthMap;
   191     typedef typename TR::LengthMap LengthMap;
   209     ///\brief The type of the map that stores the last
   192     ///\brief The type of the map that stores the last
   210     ///edges of the shortest paths.
   193     ///edges of the shortest paths.
   211     typedef typename TR::PredMap PredMap;
   194     typedef typename TR::PredMap PredMap;
   212 //     ///\brief The type of the map that stores the last but one
       
   213 //     ///nodes of the shortest paths.
       
   214 //     typedef typename TR::PredNodeMap PredNodeMap;
       
   215     ///The type of the map indicating if a node is processed.
   195     ///The type of the map indicating if a node is processed.
   216     typedef typename TR::ProcessedMap ProcessedMap;
   196     typedef typename TR::ProcessedMap ProcessedMap;
   217     ///The type of the map that stores the dists of the nodes.
   197     ///The type of the map that stores the dists of the nodes.
   218     typedef typename TR::DistMap DistMap;
   198     typedef typename TR::DistMap DistMap;
   219     ///The heap type used by the dijkstra algorithm.
   199     ///The heap type used by the dijkstra algorithm.
   225     const LengthMap *length;
   205     const LengthMap *length;
   226     ///Pointer to the map of predecessors edges.
   206     ///Pointer to the map of predecessors edges.
   227     PredMap *_pred;
   207     PredMap *_pred;
   228     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   208     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   229     bool local_pred;
   209     bool local_pred;
   230 //     ///Pointer to the map of predecessors nodes.
       
   231 //     PredNodeMap *_predNode;
       
   232 //     ///Indicates if \ref _predNode is locally allocated (\c true) or not.
       
   233 //     bool local_predNode;
       
   234     ///Pointer to the map of distances.
   210     ///Pointer to the map of distances.
   235     DistMap *_dist;
   211     DistMap *_dist;
   236     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   212     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   237     bool local_dist;
   213     bool local_dist;
   238     ///Pointer to the map of processed status of the nodes.
   214     ///Pointer to the map of processed status of the nodes.
   239     ProcessedMap *_processed;
   215     ProcessedMap *_processed;
   240     ///Indicates if \ref _processed is locally allocated (\c true) or not.
   216     ///Indicates if \ref _processed is locally allocated (\c true) or not.
   241     bool local_processed;
   217     bool local_processed;
   242 
   218 
   243 //     ///The source node of the last execution.
       
   244 //     Node source;
       
   245 
       
   246     ///Creates the maps if necessary.
   219     ///Creates the maps if necessary.
   247     
   220     
   248     ///\todo Error if \c G or are \c NULL. What about \c length?
   221     ///\todo Error if \c G or are \c NULL. What about \c length?
   249     ///\todo Better memory allocation (instead of new).
   222     ///\todo Better memory allocation (instead of new).
   250     void create_maps() 
   223     void create_maps() 
   251     {
   224     {
   252       if(!_pred) {
   225       if(!_pred) {
   253 	local_pred = true;
   226 	local_pred = true;
   254 	_pred = Traits::createPredMap(*G);
   227 	_pred = Traits::createPredMap(*G);
   255       }
   228       }
   256 //       if(!_predNode) {
       
   257 // 	local_predNode = true;
       
   258 // 	_predNode = Traits::createPredNodeMap(*G);
       
   259 //       }
       
   260       if(!_dist) {
   229       if(!_dist) {
   261 	local_dist = true;
   230 	local_dist = true;
   262 	_dist = Traits::createDistMap(*G);
   231 	_dist = Traits::createDistMap(*G);
   263       }
   232       }
   264       if(!_processed) {
   233       if(!_processed) {
   287     ///
   256     ///
   288     template <class T>
   257     template <class T>
   289     class DefPredMap : public Dijkstra< Graph,
   258     class DefPredMap : public Dijkstra< Graph,
   290 					LengthMap,
   259 					LengthMap,
   291 					DefPredMapTraits<T> > { };
   260 					DefPredMapTraits<T> > { };
   292     
       
   293 //     template <class T>
       
   294 //     struct DefPredNodeMapTraits : public Traits {
       
   295 //       typedef T PredNodeMap;
       
   296 //       static PredNodeMap *createPredNodeMap(const Graph &G) 
       
   297 //       {
       
   298 // 	throw UninitializedParameter();
       
   299 //       }
       
   300 //     };
       
   301 //     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
       
   302 
       
   303 //     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
       
   304 //     ///
       
   305 //     template <class T>
       
   306 //     class DefPredNodeMap : public Dijkstra< Graph,
       
   307 // 					    LengthMap,
       
   308 // 					    DefPredNodeMapTraits<T> > { };
       
   309     
   261     
   310     template <class T>
   262     template <class T>
   311     struct DefDistMapTraits : public Traits {
   263     struct DefDistMapTraits : public Traits {
   312       typedef T DistMap;
   264       typedef T DistMap;
   313       static DistMap *createDistMap(const Graph &G) 
   265       static DistMap *createDistMap(const Graph &G) 
   373     ///\param _G the graph the algorithm will run on.
   325     ///\param _G the graph the algorithm will run on.
   374     ///\param _length the length map used by the algorithm.
   326     ///\param _length the length map used by the algorithm.
   375     Dijkstra(const Graph& _G, const LengthMap& _length) :
   327     Dijkstra(const Graph& _G, const LengthMap& _length) :
   376       G(&_G), length(&_length),
   328       G(&_G), length(&_length),
   377       _pred(NULL), local_pred(false),
   329       _pred(NULL), local_pred(false),
   378 //       _predNode(NULL), local_predNode(false),
       
   379       _dist(NULL), local_dist(false),
   330       _dist(NULL), local_dist(false),
   380       _processed(NULL), local_processed(false),
   331       _processed(NULL), local_processed(false),
   381       _heap_map(*G,-1),_heap(_heap_map)
   332       _heap_map(*G,-1),_heap(_heap_map)
   382     { }
   333     { }
   383     
   334     
   384     ///Destructor.
   335     ///Destructor.
   385     ~Dijkstra() 
   336     ~Dijkstra() 
   386     {
   337     {
   387       if(local_pred) delete _pred;
   338       if(local_pred) delete _pred;
   388 //       if(local_predNode) delete _predNode;
       
   389       if(local_dist) delete _dist;
   339       if(local_dist) delete _dist;
   390       if(local_processed) delete _processed;
   340       if(local_processed) delete _processed;
   391     }
   341     }
   392 
   342 
   393     ///Sets the length map.
   343     ///Sets the length map.
   415       }
   365       }
   416       _pred = &m;
   366       _pred = &m;
   417       return *this;
   367       return *this;
   418     }
   368     }
   419 
   369 
   420 //     ///Sets the map storing the predecessor nodes.
       
   421 
       
   422 //     ///Sets the map storing the predecessor nodes.
       
   423 //     ///If you don't use this function before calling \ref run(),
       
   424 //     ///it will allocate one. The destuctor deallocates this
       
   425 //     ///automatically allocated map, of course.
       
   426 //     ///\return <tt> (*this) </tt>
       
   427 //     Dijkstra &predNodeMap(PredNodeMap &m) 
       
   428 //     {
       
   429 //       if(local_predNode) {
       
   430 // 	delete _predNode;
       
   431 // 	local_predNode=false;
       
   432 //       }
       
   433 //       _predNode = &m;
       
   434 //       return *this;
       
   435 //     }
       
   436 
       
   437     ///Sets the map storing the distances calculated by the algorithm.
   370     ///Sets the map storing the distances calculated by the algorithm.
   438 
   371 
   439     ///Sets the map storing the distances calculated by the algorithm.
   372     ///Sets the map storing the distances calculated by the algorithm.
   440     ///If you don't use this function before calling \ref run(),
   373     ///If you don't use this function before calling \ref run(),
   441     ///it will allocate one. The destuctor deallocates this
   374     ///it will allocate one. The destuctor deallocates this
   454   private:
   387   private:
   455     void finalizeNodeData(Node v,Value dst)
   388     void finalizeNodeData(Node v,Value dst)
   456     {
   389     {
   457       _processed->set(v,true);
   390       _processed->set(v,true);
   458       _dist->set(v, dst);
   391       _dist->set(v, dst);
   459 //       if((*_pred)[v]!=INVALID)
       
   460 //       _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do?
       
   461     }
   392     }
   462 
   393 
   463   public:
   394   public:
   464     ///\name Execution control
   395     ///\name Execution control
   465     ///The simplest way to execute the algorithm is to use
   396     ///The simplest way to execute the algorithm is to use
   483     {
   414     {
   484       create_maps();
   415       create_maps();
   485       while(!_heap.empty()) _heap.pop();
   416       while(!_heap.empty()) _heap.pop();
   486       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   417       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   487 	_pred->set(u,INVALID);
   418 	_pred->set(u,INVALID);
   488 // 	_predNode->set(u,INVALID);
       
   489 	_processed->set(u,false);
   419 	_processed->set(u,false);
   490 	_heap_map.set(u,Heap::PRE_HEAP);
   420 	_heap_map.set(u,Heap::PRE_HEAP);
   491       }
   421       }
   492     }
   422     }
   493     
   423     
   500     ///It checks if the node has already been added to the heap and
   430     ///It checks if the node has already been added to the heap and
   501     ///It is pushed to the heap only if either it was not in the heap
   431     ///It is pushed to the heap only if either it was not in the heap
   502     ///or the shortest path found till then is longer then \c dst.
   432     ///or the shortest path found till then is longer then \c dst.
   503     void addSource(Node s,Value dst=0)
   433     void addSource(Node s,Value dst=0)
   504     {
   434     {
   505 //       source = s;
       
   506       if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
   435       if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
   507       else if(_heap[s]<dst) {
   436       else if(_heap[s]<dst) {
   508 	_heap.push(s,dst);
   437 	_heap.push(s,dst);
   509 	_pred->set(s,INVALID);
   438 	_pred->set(s,INVALID);
   510       }
   439       }
   528 	Node w=G->target(e); 
   457 	Node w=G->target(e); 
   529 	switch(_heap.state(w)) {
   458 	switch(_heap.state(w)) {
   530 	case Heap::PRE_HEAP:
   459 	case Heap::PRE_HEAP:
   531 	  _heap.push(w,oldvalue+(*length)[e]); 
   460 	  _heap.push(w,oldvalue+(*length)[e]); 
   532 	  _pred->set(w,e);
   461 	  _pred->set(w,e);
   533 //  	  _predNode->set(w,v);
       
   534 	  break;
   462 	  break;
   535 	case Heap::IN_HEAP:
   463 	case Heap::IN_HEAP:
   536 	  if ( oldvalue+(*length)[e] < _heap[w] ) {
   464 	  if ( oldvalue+(*length)[e] < _heap[w] ) {
   537 	    _heap.decrease(w, oldvalue+(*length)[e]); 
   465 	    _heap.decrease(w, oldvalue+(*length)[e]); 
   538 	    _pred->set(w,e);
   466 	    _pred->set(w,e);
   539 // 	    _predNode->set(w,v);
       
   540 	  }
   467 	  }
   541 	  break;
   468 	  break;
   542 	case Heap::POST_HEAP:
   469 	case Heap::POST_HEAP:
   543 	  break;
   470 	  break;
   544 	}
   471 	}
   550     
   477     
   551     ///Next node to be processed.
   478     ///Next node to be processed.
   552     ///
   479     ///
   553     ///\return The next node to be processed or INVALID if the priority heap
   480     ///\return The next node to be processed or INVALID if the priority heap
   554     /// is empty.
   481     /// is empty.
   555     Node NextNode()
   482     Node nextNode()
   556     { 
   483     { 
   557       return _heap.empty()?_heap.top():INVALID;
   484       return _heap.empty()?_heap.top():INVALID;
   558     }
   485     }
   559  
   486  
   560     ///\brief Returns \c false if there are nodes
   487     ///\brief Returns \c false if there are nodes
   740     ///Returns a reference to the NodeMap of the edges of the
   667     ///Returns a reference to the NodeMap of the edges of the
   741     ///shortest path tree.
   668     ///shortest path tree.
   742     ///\pre \ref run() must be called before using this function.
   669     ///\pre \ref run() must be called before using this function.
   743     const PredMap &predMap() const { return *_pred;}
   670     const PredMap &predMap() const { return *_pred;}
   744  
   671  
   745 //     ///Returns a reference to the map of nodes of shortest paths.
       
   746 
       
   747 //     ///Returns a reference to the NodeMap of the last but one nodes of the
       
   748 //     ///shortest path tree.
       
   749 //     ///\pre \ref run() must be called before using this function.
       
   750 //     const PredNodeMap &predNodeMap() const { return *_predNode;}
       
   751 
       
   752     ///Checks if a node is reachable from the root.
   672     ///Checks if a node is reachable from the root.
   753 
   673 
   754     ///Returns \c true if \c v is reachable from the root.
   674     ///Returns \c true if \c v is reachable from the root.
   755     ///\warning The source nodes are inditated as unreached.
   675     ///\warning The source nodes are inditated as unreached.
   756     ///\pre \ref run() must be called before using this function.
   676     ///\pre \ref run() must be called before using this function.
  1099   }
  1019   }
  1100 
  1020 
  1101 } //END OF NAMESPACE LEMON
  1021 } //END OF NAMESPACE LEMON
  1102 
  1022 
  1103 #endif
  1023 #endif
  1104