Changeset 244:c30731a37f91 in lemon-1.2 for lemon/dfs.h
- Timestamp:
- 08/03/08 13:34:57 (15 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/dfs.h
r210 r244 22 22 ///\ingroup search 23 23 ///\file 24 ///\brief D fsalgorithm.24 ///\brief DFS algorithm. 25 25 26 26 #include <lemon/list_graph.h> … … 29 29 #include <lemon/bits/invalid.h> 30 30 #include <lemon/error.h> 31 #include <lemon/assert.h> 31 32 #include <lemon/maps.h> 32 33 33 #include <lemon/concept_check.h>34 35 34 namespace lemon { 36 37 35 38 36 ///Default traits class of Dfs class. … … 43 41 struct DfsDefaultTraits 44 42 { 45 ///The digraph typethe algorithm runs on.43 ///The type of the digraph the algorithm runs on. 46 44 typedef GR Digraph; 47 ///\brief The type of the map that stores the last 45 46 ///\brief The type of the map that stores the predecessor 48 47 ///arcs of the %DFS paths. 49 48 /// 50 ///The type of the map that stores the last49 ///The type of the map that stores the predecessor 51 50 ///arcs of the %DFS paths. 52 51 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 53 /// 54 typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; 55 ///Instantiates a PredMap. 52 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 53 ///Instantiates a \ref PredMap. 56 54 57 55 ///This function instantiates a \ref PredMap. 58 ///\param G is the digraph, to which we would like to define the PredMap. 56 ///\param g is the digraph, to which we would like to define the 57 ///\ref PredMap. 59 58 ///\todo The digraph alone may be insufficient to initialize 60 static PredMap *createPredMap(const GR &G)61 { 62 return new PredMap( G);59 static PredMap *createPredMap(const Digraph &g) 60 { 61 return new PredMap(g); 63 62 } 64 63 … … 67 66 ///The type of the map that indicates which nodes are processed. 68 67 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 69 /// \todo named parameter to set this type, function to read and write.68 ///By default it is a NullMap. 70 69 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 71 ///Instantiates a ProcessedMap.70 ///Instantiates a \ref ProcessedMap. 72 71 73 72 ///This function instantiates a \ref ProcessedMap. … … 75 74 ///we would like to define the \ref ProcessedMap 76 75 #ifdef DOXYGEN 77 static ProcessedMap *createProcessedMap(const GR&g)76 static ProcessedMap *createProcessedMap(const Digraph &g) 78 77 #else 79 static ProcessedMap *createProcessedMap(const GR&)78 static ProcessedMap *createProcessedMap(const Digraph &) 80 79 #endif 81 80 { 82 81 return new ProcessedMap(); 83 82 } 83 84 84 ///The type of the map that indicates which nodes are reached. 85 85 86 86 ///The type of the map that indicates which nodes are reached. 87 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 88 typedef typename Digraph::template NodeMap<bool> ReachedMap; 89 ///Instantiates a \ref ReachedMap. 90 91 ///This function instantiates a \ref ReachedMap. 92 ///\param g is the digraph, to which 93 ///we would like to define the \ref ReachedMap. 94 static ReachedMap *createReachedMap(const Digraph &g) 95 { 96 return new ReachedMap(g); 97 } 98 99 ///The type of the map that stores the distances of the nodes. 100 101 ///The type of the map that stores the distances of the nodes. 87 102 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 88 ///\todo named parameter to set this type, function to read and write.89 typedef typename Digraph::template NodeMap<bool> ReachedMap;90 ///Instantiates a ReachedMap.91 92 ///This function instantiates a \ref ReachedMap.93 ///\param G is the digraph, to which94 ///we would like to define the \ref ReachedMap.95 static ReachedMap *createReachedMap(const GR &G)96 {97 return new ReachedMap(G);98 }99 ///The type of the map that stores the dists of the nodes.100 101 ///The type of the map that stores the dists of the nodes.102 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.103 ///104 103 typedef typename Digraph::template NodeMap<int> DistMap; 105 ///Instantiates a DistMap.104 ///Instantiates a \ref DistMap. 106 105 107 106 ///This function instantiates a \ref DistMap. 108 ///\param G is the digraph, to which we would like to define109 /// the \ref DistMap110 static DistMap *createDistMap(const GR &G)111 { 112 return new DistMap( G);107 ///\param g is the digraph, to which we would like to define the 108 ///\ref DistMap. 109 static DistMap *createDistMap(const Digraph &g) 110 { 111 return new DistMap(g); 113 112 } 114 113 }; … … 119 118 ///This class provides an efficient implementation of the %DFS algorithm. 120 119 /// 121 ///\tparam GR The digraph type the algorithm runs on. The default value is 122 ///\ref ListDigraph. The value of GR is not used directly by Dfs, it 123 ///is only passed to \ref DfsDefaultTraits. 120 ///There is also a \ref dfs() "function type interface" for the DFS 121 ///algorithm, which is convenient in the simplier cases and it can be 122 ///used easier. 123 /// 124 ///\tparam GR The type of the digraph the algorithm runs on. 125 ///The default value is \ref ListDigraph. The value of GR is not used 126 ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. 124 127 ///\tparam TR Traits class to set various data types used by the algorithm. 125 128 ///The default traits class is … … 136 139 class Dfs { 137 140 public: 138 /** 139 * \brief \ref Exception for uninitialized parameters. 140 * 141 * This error represents problems in the initialization 142 * of the parameters of the algorithms. 143 */ 141 ///\ref Exception for uninitialized parameters. 142 143 ///This error represents problems in the initialization of the 144 ///parameters of the algorithm. 144 145 class UninitializedParameter : public lemon::UninitializedParameter { 145 146 public: … … 149 150 }; 150 151 152 ///The type of the digraph the algorithm runs on. 153 typedef typename TR::Digraph Digraph; 154 155 ///\brief The type of the map that stores the predecessor arcs of the 156 ///DFS paths. 157 typedef typename TR::PredMap PredMap; 158 ///The type of the map that stores the distances of the nodes. 159 typedef typename TR::DistMap DistMap; 160 ///The type of the map that indicates which nodes are reached. 161 typedef typename TR::ReachedMap ReachedMap; 162 ///The type of the map that indicates which nodes are processed. 163 typedef typename TR::ProcessedMap ProcessedMap; 164 ///The type of the paths. 165 typedef PredMapPath<Digraph, PredMap> Path; 166 167 ///The traits class. 151 168 typedef TR Traits; 152 ///The type of the underlying digraph. 153 typedef typename TR::Digraph Digraph;154 ///\e 169 170 private: 171 155 172 typedef typename Digraph::Node Node; 156 ///\e157 173 typedef typename Digraph::NodeIt NodeIt; 158 ///\e159 174 typedef typename Digraph::Arc Arc; 160 ///\e161 175 typedef typename Digraph::OutArcIt OutArcIt; 162 176 163 ///\brief The type of the map that stores the last 164 ///arcs of the %DFS paths. 165 typedef typename TR::PredMap PredMap; 166 ///The type of the map indicating which nodes are reached. 167 typedef typename TR::ReachedMap ReachedMap; 168 ///The type of the map indicating which nodes are processed. 169 typedef typename TR::ProcessedMap ProcessedMap; 170 ///The type of the map that stores the dists of the nodes. 171 typedef typename TR::DistMap DistMap; 172 private: 173 /// Pointer to the underlying digraph. 177 //Pointer to the underlying digraph. 174 178 const Digraph *G; 175 // /Pointer to the map of predecessorsarcs.179 //Pointer to the map of predecessor arcs. 176 180 PredMap *_pred; 177 // /Indicates if \ref _pred is locally allocated (\ctrue) or not.181 //Indicates if _pred is locally allocated (true) or not. 178 182 bool local_pred; 179 // /Pointer to the map of distances.183 //Pointer to the map of distances. 180 184 DistMap *_dist; 181 // /Indicates if \ref _dist is locally allocated (\ctrue) or not.185 //Indicates if _dist is locally allocated (true) or not. 182 186 bool local_dist; 183 // /Pointer to the map of reached status of the nodes.187 //Pointer to the map of reached status of the nodes. 184 188 ReachedMap *_reached; 185 // /Indicates if \ref _reached is locally allocated (\ctrue) or not.189 //Indicates if _reached is locally allocated (true) or not. 186 190 bool local_reached; 187 // /Pointer to the map of processed status of the nodes.191 //Pointer to the map of processed status of the nodes. 188 192 ProcessedMap *_processed; 189 // /Indicates if \ref _processed is locally allocated (\ctrue) or not.193 //Indicates if _processed is locally allocated (true) or not. 190 194 bool local_processed; 191 195 … … 194 198 195 199 ///Creates the maps if necessary. 196 197 200 ///\todo Better memory allocation (instead of new). 198 201 void create_maps() … … 231 234 struct DefPredMapTraits : public Traits { 232 235 typedef T PredMap; 233 static PredMap *createPredMap(const Digraph & G)236 static PredMap *createPredMap(const Digraph &) 234 237 { 235 238 throw UninitializedParameter(); … … 237 240 }; 238 241 ///\brief \ref named-templ-param "Named parameter" for setting 239 /// PredMap type240 /// 241 ///\ref named-templ-param "Named parameter" for setting PredMap type242 /// 242 ///\ref PredMap type. 243 /// 244 ///\ref named-templ-param "Named parameter" for setting 245 ///\ref PredMap type. 243 246 template <class T> 244 247 struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > { 245 248 typedef Dfs<Digraph, DefPredMapTraits<T> > Create; 246 249 }; 247 248 250 249 251 template <class T> … … 256 258 }; 257 259 ///\brief \ref named-templ-param "Named parameter" for setting 258 /// DistMap type259 /// 260 ///\ref named-templ-param "Named parameter" for setting DistMap261 /// type260 ///\ref DistMap type. 261 /// 262 ///\ref named-templ-param "Named parameter" for setting 263 ///\ref DistMap type. 262 264 template <class T> 263 struct DefDistMap {265 struct DefDistMap : public Dfs< Digraph, DefDistMapTraits<T> > { 264 266 typedef Dfs<Digraph, DefDistMapTraits<T> > Create; 265 267 }; … … 274 276 }; 275 277 ///\brief \ref named-templ-param "Named parameter" for setting 276 /// ReachedMap type277 /// 278 ///\ref named-templ-param "Named parameter" for setting ReachedMap type279 /// 278 ///\ref ReachedMap type. 279 /// 280 ///\ref named-templ-param "Named parameter" for setting 281 ///\ref ReachedMap type. 280 282 template <class T> 281 283 struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > { … … 292 294 }; 293 295 ///\brief \ref named-templ-param "Named parameter" for setting 294 /// ProcessedMap type295 /// 296 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type297 /// 296 ///\ref ProcessedMap type. 297 /// 298 ///\ref named-templ-param "Named parameter" for setting 299 ///\ref ProcessedMap type. 298 300 template <class T> 299 301 struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > { … … 303 305 struct DefDigraphProcessedMapTraits : public Traits { 304 306 typedef typename Digraph::template NodeMap<bool> ProcessedMap; 305 static ProcessedMap *createProcessedMap(const Digraph & G)307 static ProcessedMap *createProcessedMap(const Digraph &g) 306 308 { 307 return new ProcessedMap( G);308 } 309 }; 310 ///\brief \ref named-templ-param "Named parameter" 311 /// for setting the ProcessedMap type to be Digraph::NodeMap<bool>.312 /// 313 ///\ref named-templ-param "Named parameter" 314 /// for setting the ProcessedMap type to be Digraph::NodeMap<bool>.315 ///If you don't set it explicit ely, it will be automatically allocated.309 return new ProcessedMap(g); 310 } 311 }; 312 ///\brief \ref named-templ-param "Named parameter" for setting 313 ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 314 /// 315 ///\ref named-templ-param "Named parameter" for setting 316 ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 317 ///If you don't set it explicitly, it will be automatically allocated. 316 318 template <class T> 317 classDefProcessedMapToBeDefaultMap :319 struct DefProcessedMapToBeDefaultMap : 318 320 public Dfs< Digraph, DefDigraphProcessedMapTraits> { 319 321 typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create; … … 326 328 ///Constructor. 327 329 328 /// \param _G the digraph the algorithm will run on.329 /// 330 Dfs(const Digraph & _G) :331 G(& _G),330 ///Constructor. 331 ///\param g The digraph the algorithm runs on. 332 Dfs(const Digraph &g) : 333 G(&g), 332 334 _pred(NULL), local_pred(false), 333 335 _dist(NULL), local_dist(false), … … 345 347 } 346 348 347 ///Sets the map storingthe predecessor arcs.348 349 ///Sets the map storingthe predecessor arcs.349 ///Sets the map that stores the predecessor arcs. 350 351 ///Sets the map that stores the predecessor arcs. 350 352 ///If you don't use this function before calling \ref run(), 351 ///it will allocate one. The dest uctor deallocates this353 ///it will allocate one. The destructor deallocates this 352 354 ///automatically allocated map, of course. 353 355 ///\return <tt> (*this) </tt> … … 362 364 } 363 365 364 ///Sets the map storing the distances calculated by the algorithm.365 366 ///Sets the map storing the distances calculated by the algorithm.366 ///Sets the map that indicates which nodes are reached. 367 368 ///Sets the map that indicates which nodes are reached. 367 369 ///If you don't use this function before calling \ref run(), 368 ///it will allocate one. The destuctor deallocates this 370 ///it will allocate one. The destructor deallocates this 371 ///automatically allocated map, of course. 372 ///\return <tt> (*this) </tt> 373 Dfs &reachedMap(ReachedMap &m) 374 { 375 if(local_reached) { 376 delete _reached; 377 local_reached=false; 378 } 379 _reached = &m; 380 return *this; 381 } 382 383 ///Sets the map that indicates which nodes are processed. 384 385 ///Sets the map that indicates which nodes are processed. 386 ///If you don't use this function before calling \ref run(), 387 ///it will allocate one. The destructor deallocates this 388 ///automatically allocated map, of course. 389 ///\return <tt> (*this) </tt> 390 Dfs &processedMap(ProcessedMap &m) 391 { 392 if(local_processed) { 393 delete _processed; 394 local_processed=false; 395 } 396 _processed = &m; 397 return *this; 398 } 399 400 ///Sets the map that stores the distances of the nodes. 401 402 ///Sets the map that stores the distances of the nodes calculated by 403 ///the algorithm. 404 ///If you don't use this function before calling \ref run(), 405 ///it will allocate one. The destructor deallocates this 369 406 ///automatically allocated map, of course. 370 407 ///\return <tt> (*this) </tt> … … 379 416 } 380 417 381 ///Sets the map indicating if a node is reached.382 383 ///Sets the map indicating if a node is reached.384 ///If you don't use this function before calling \ref run(),385 ///it will allocate one. The destuctor deallocates this386 ///automatically allocated map, of course.387 ///\return <tt> (*this) </tt>388 Dfs &reachedMap(ReachedMap &m)389 {390 if(local_reached) {391 delete _reached;392 local_reached=false;393 }394 _reached = &m;395 return *this;396 }397 398 ///Sets the map indicating if a node is processed.399 400 ///Sets the map indicating if a node is processed.401 ///If you don't use this function before calling \ref run(),402 ///it will allocate one. The destuctor deallocates this403 ///automatically allocated map, of course.404 ///\return <tt> (*this) </tt>405 Dfs &processedMap(ProcessedMap &m)406 {407 if(local_processed) {408 delete _processed;409 local_processed=false;410 }411 _processed = &m;412 return *this;413 }414 415 418 public: 419 416 420 ///\name Execution control 417 421 ///The simplest way to execute the algorithm is to use 418 ///one of the member functions called \ c run(...).422 ///one of the member functions called \ref lemon::Dfs::run() "run()". 419 423 ///\n 420 ///If you need more control on the execution, 421 /// first you must call \ref init(), then you can add a source node422 ///with \ref addSource().423 ///Finally \ref start() will perform the actual path424 /// computation.424 ///If you need more control on the execution, first you must call 425 ///\ref lemon::Dfs::init() "init()", then you can add a source node 426 ///with \ref lemon::Dfs::addSource() "addSource()". 427 ///Finally \ref lemon::Dfs::start() "start()" will perform the 428 ///actual path computation. 425 429 426 430 ///@{ … … 437 441 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { 438 442 _pred->set(u,INVALID); 439 // _predNode->set(u,INVALID);440 443 _reached->set(u,false); 441 444 _processed->set(u,false); … … 447 450 ///Adds a new source node to the set of nodes to be processed. 448 451 /// 449 ///\warning dists are wrong (or at least strange) 450 ///in case of multiple sources. 452 ///\pre The stack must be empty. (Otherwise the algorithm gives 453 ///false results.) 454 /// 455 ///\warning Distances will be wrong (or at least strange) in case of 456 ///multiple sources. 451 457 void addSource(Node s) 452 458 { 459 LEMON_DEBUG(emptyQueue(), "The stack is not empty."); 453 460 if(!(*_reached)[s]) 454 461 { … … 473 480 ///\return The processed arc. 474 481 /// 475 ///\pre The stack must not be empty !482 ///\pre The stack must not be empty. 476 483 Arc processNextArc() 477 484 { … … 499 506 return e; 500 507 } 508 501 509 ///Next arc to be processed. 502 510 503 511 ///Next arc to be processed. 504 512 /// 505 ///\return The next arc to be processed or INVALID if the stack is506 /// empty.507 OutArcIt nextArc() 513 ///\return The next arc to be processed or \c INVALID if the stack 514 ///is empty. 515 OutArcIt nextArc() const 508 516 { 509 517 return _stack_head>=0?_stack[_stack_head]:INVALID; … … 511 519 512 520 ///\brief Returns \c false if there are nodes 513 ///to be processed in the queue521 ///to be processed. 514 522 /// 515 523 ///Returns \c false if there are nodes 516 ///to be processed in the queue 517 bool emptyQueue() { return _stack_head<0; } 524 ///to be processed in the queue (stack). 525 bool emptyQueue() const { return _stack_head<0; } 526 518 527 ///Returns the number of the nodes to be processed. 519 528 520 ///Returns the number of the nodes to be processed in the queue .521 int queueSize() { return _stack_head+1; }529 ///Returns the number of the nodes to be processed in the queue (stack). 530 int queueSize() const { return _stack_head+1; } 522 531 523 532 ///Executes the algorithm. … … 525 534 ///Executes the algorithm. 526 535 /// 527 ///\pre init() must be called and at least one node should be added 528 ///with addSource() before using this function. 529 /// 530 ///This method runs the %DFS algorithm from the root node(s) 531 ///in order to 532 ///compute the 533 ///%DFS path to each node. The algorithm computes 534 ///- The %DFS tree. 535 ///- The distance of each node from the root(s) in the %DFS tree. 536 /// 536 ///This method runs the %DFS algorithm from the root node 537 ///in order to compute the DFS path to each node. 538 /// 539 /// The algorithm computes 540 ///- the %DFS tree, 541 ///- the distance of each node from the root in the %DFS tree. 542 /// 543 ///\pre init() must be called and a root node should be 544 ///added with addSource() before using this function. 545 /// 546 ///\note <tt>d.start()</tt> is just a shortcut of the following code. 547 ///\code 548 /// while ( !d.emptyQueue() ) { 549 /// d.processNextArc(); 550 /// } 551 ///\endcode 537 552 void start() 538 553 { … … 540 555 } 541 556 542 ///Executes the algorithm until \c dest is reached. 543 544 ///Executes the algorithm until \c dest is reached. 545 /// 546 ///\pre init() must be called and at least one node should be added 547 ///with addSource() before using this function. 548 /// 549 ///This method runs the %DFS algorithm from the root node(s) 550 ///in order to 551 ///compute the 552 ///%DFS path to \c dest. The algorithm computes 553 ///- The %DFS path to \c dest. 554 ///- The distance of \c dest from the root(s) in the %DFS tree. 555 /// 557 ///Executes the algorithm until the given target node is reached. 558 559 ///Executes the algorithm until the given target node is reached. 560 /// 561 ///This method runs the %DFS algorithm from the root node 562 ///in order to compute the DFS path to \c dest. 563 /// 564 ///The algorithm computes 565 ///- the %DFS path to \c dest, 566 ///- the distance of \c dest from the root in the %DFS tree. 567 /// 568 ///\pre init() must be called and a root node should be 569 ///added with addSource() before using this function. 556 570 void start(Node dest) 557 571 { … … 564 578 ///Executes the algorithm until a condition is met. 565 579 /// 566 /// \pre init() must be called and at least one node should be added567 /// with addSource() before using this function.568 /// 569 ///\param em must be abool (or convertible) arc map. The algorithm570 ///will stop when it reaches an arc \c e with <tt>em[e]</tt> true.571 /// 572 ///\return The reached arc \c e with <tt>em[e]</tt> true or580 ///This method runs the %DFS algorithm from the root node 581 ///until an arc \c a with <tt>am[a]</tt> true is found. 582 /// 583 ///\param am A \c bool (or convertible) arc map. The algorithm 584 ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. 585 /// 586 ///\return The reached arc \c a with <tt>am[a]</tt> true or 573 587 ///\c INVALID if no such arc was found. 574 588 /// 575 ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map, 589 ///\pre init() must be called and a root node should be 590 ///added with addSource() before using this function. 591 /// 592 ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, 576 593 ///not a node map. 577 template<class EM>578 Arc start(const EM &em)579 { 580 while ( !emptyQueue() && ! em[_stack[_stack_head]] )594 template<class ArcBoolMap> 595 Arc start(const ArcBoolMap &am) 596 { 597 while ( !emptyQueue() && !am[_stack[_stack_head]] ) 581 598 processNextArc(); 582 599 return emptyQueue() ? INVALID : _stack[_stack_head]; 583 600 } 584 601 585 ///Runs %DFS algorithm to visit all nodes in the digraph. 586 587 ///This method runs the %DFS algorithm in order to 588 ///compute the 589 ///%DFS path to each node. The algorithm computes 590 ///- The %DFS tree. 591 ///- The distance of each node from the root in the %DFS tree. 592 /// 593 ///\note d.run() is just a shortcut of the following code. 602 ///Runs the algorithm from the given node. 603 604 ///This method runs the %DFS algorithm from node \c s 605 ///in order to compute the DFS path to each node. 606 /// 607 ///The algorithm computes 608 ///- the %DFS tree, 609 ///- the distance of each node from the root in the %DFS tree. 610 /// 611 ///\note <tt>d.run(s)</tt> is just a shortcut of the following code. 594 612 ///\code 595 613 /// d.init(); 596 /// for (NodeIt it(digraph); it != INVALID; ++it) { 597 /// if (!d.reached(it)) { 598 /// d.addSource(it); 614 /// d.addSource(s); 615 /// d.start(); 616 ///\endcode 617 void run(Node s) { 618 init(); 619 addSource(s); 620 start(); 621 } 622 623 ///Finds the %DFS path between \c s and \c t. 624 625 ///This method runs the %DFS algorithm from node \c s 626 ///in order to compute the DFS path to \c t. 627 /// 628 ///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, 629 ///if \c t is reachable form \c s, \c 0 otherwise. 630 /// 631 ///\note Apart from the return value, <tt>d.run(s,t)</tt> is 632 ///just a shortcut of the following code. 633 ///\code 634 /// d.init(); 635 /// d.addSource(s); 636 /// d.start(t); 637 ///\endcode 638 int run(Node s,Node t) { 639 init(); 640 addSource(s); 641 start(t); 642 return reached(t)?_stack_head+1:0; 643 } 644 645 ///Runs the algorithm to visit all nodes in the digraph. 646 647 ///This method runs the %DFS algorithm in order to compute the 648 ///%DFS path to each node. 649 /// 650 ///The algorithm computes 651 ///- the %DFS tree, 652 ///- the distance of each node from the root in the %DFS tree. 653 /// 654 ///\note <tt>d.run()</tt> is just a shortcut of the following code. 655 ///\code 656 /// d.init(); 657 /// for (NodeIt n(digraph); n != INVALID; ++n) { 658 /// if (!d.reached(n)) { 659 /// d.addSource(n); 599 660 /// d.start(); 600 661 /// } … … 611 672 } 612 673 613 ///Runs %DFS algorithm from node \c s.614 615 ///This method runs the %DFS algorithm from a root node \c s616 ///in order to617 ///compute the618 ///%DFS path to each node. The algorithm computes619 ///- The %DFS tree.620 ///- The distance of each node from the root in the %DFS tree.621 ///622 ///\note d.run(s) is just a shortcut of the following code.623 ///\code624 /// d.init();625 /// d.addSource(s);626 /// d.start();627 ///\endcode628 void run(Node s) {629 init();630 addSource(s);631 start();632 }633 634 ///Finds the %DFS path between \c s and \c t.635 636 ///Finds the %DFS path between \c s and \c t.637 ///638 ///\return The length of the %DFS s---t path if there exists one,639 ///0 otherwise.640 ///\note Apart from the return value, d.run(s,t) is641 ///just a shortcut of the following code.642 ///\code643 /// d.init();644 /// d.addSource(s);645 /// d.start(t);646 ///\endcode647 int run(Node s,Node t) {648 init();649 addSource(s);650 start(t);651 return reached(t)?_stack_head+1:0;652 }653 654 674 ///@} 655 675 … … 657 677 ///The result of the %DFS algorithm can be obtained using these 658 678 ///functions.\n 659 /// Before the use of these functions,660 /// either run() or start() must be called.679 ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() 680 ///"start()" must be called before using them. 661 681 662 682 ///@{ 663 683 664 typedef PredMapPath<Digraph, PredMap> Path; 665 666 ///Gives back the shortest path. 667 668 ///Gives back the shortest path. 669 ///\pre The \c t should be reachable from the source. 670 Path path(Node t) 671 { 672 return Path(*G, *_pred, t); 673 } 674 675 ///The distance of a node from the root(s). 676 677 ///Returns the distance of a node from the root(s). 678 ///\pre \ref run() must be called before using this function. 679 ///\warning If node \c v is unreachable from the root(s) then the return 680 ///value of this funcion is undefined. 684 ///The DFS path to a node. 685 686 ///Returns the DFS path to a node. 687 /// 688 ///\warning \c t should be reachable from the root. 689 /// 690 ///\pre Either \ref run() or \ref start() must be called before 691 ///using this function. 692 Path path(Node t) const { return Path(*G, *_pred, t); } 693 694 ///The distance of a node from the root. 695 696 ///Returns the distance of a node from the root. 697 /// 698 ///\warning If node \c v is not reachable from the root, then 699 ///the return value of this function is undefined. 700 /// 701 ///\pre Either \ref run() or \ref start() must be called before 702 ///using this function. 681 703 int dist(Node v) const { return (*_dist)[v]; } 682 704 683 ///Returns the 'previous arc' of the %DFS tree .684 685 /// For a node \c v it returns the 'previous arc'686 /// of the %DFS path,687 /// i.e. it returns the last arc of a %DFS path from the root(s) to \c688 /// v. It is \ref INVALID689 /// if \c v is unreachable from the root(s) or \c v is a root. The690 /// %DFS tree used here is equal to the %DFS tree used in705 ///Returns the 'previous arc' of the %DFS tree for a node. 706 707 ///This function returns the 'previous arc' of the %DFS tree for the 708 ///node \c v, i.e. it returns the last arc of a %DFS path from the 709 ///root to \c v. It is \c INVALID 710 ///if \c v is not reachable from the root(s) or if \c v is a root. 711 /// 712 ///The %DFS tree used here is equal to the %DFS tree used in 691 713 ///\ref predNode(). 714 /// 692 715 ///\pre Either \ref run() or \ref start() must be called before using 693 716 ///this function. … … 696 719 ///Returns the 'previous node' of the %DFS tree. 697 720 698 /// For a node \c v it returns the 'previous node'699 /// of the %DFS tree,700 /// i.e. it returns the last but one node from a %DFS path from the701 /// root(s) to \c v.702 /// It is INVALID if \c v is unreachable from the root(s) or703 /// if \c v itself a root.704 /// The %DFS tree used here is equal to the %DFS705 /// tree used in \ref predArc().721 ///This function returns the 'previous node' of the %DFS 722 ///tree for the node \c v, i.e. it returns the last but one node 723 ///from a %DFS path from the root to \c v. It is \c INVALID 724 ///if \c v is not reachable from the root(s) or if \c v is a root. 725 /// 726 ///The %DFS tree used here is equal to the %DFS tree used in 727 ///\ref predArc(). 728 /// 706 729 ///\pre Either \ref run() or \ref start() must be called before 707 730 ///using this function. … … 709 732 G->source((*_pred)[v]); } 710 733 711 ///Returns a reference to the NodeMap of distances. 712 713 ///Returns a reference to the NodeMap of distances. 714 ///\pre Either \ref run() or \ref init() must 715 ///be called before using this function. 734 ///\brief Returns a const reference to the node map that stores the 735 ///distances of the nodes. 736 /// 737 ///Returns a const reference to the node map that stores the 738 ///distances of the nodes calculated by the algorithm. 739 /// 740 ///\pre Either \ref run() or \ref init() 741 ///must be called before using this function. 716 742 const DistMap &distMap() const { return *_dist;} 717 743 718 ///Returns a reference to the %DFS arc-tree map. 719 720 ///Returns a reference to the NodeMap of the arcs of the 721 ///%DFS tree. 744 ///\brief Returns a const reference to the node map that stores the 745 ///predecessor arcs. 746 /// 747 ///Returns a const reference to the node map that stores the predecessor 748 ///arcs, which form the DFS tree. 749 /// 722 750 ///\pre Either \ref run() or \ref init() 723 751 ///must be called before using this function. 724 752 const PredMap &predMap() const { return *_pred;} 725 753 726 ///Checks if a node is reachable from the root .754 ///Checks if a node is reachable from the root(s). 727 755 728 756 ///Returns \c true if \c v is reachable from the root(s). 729 ///\warning The source nodes are inditated as unreachable.730 757 ///\pre Either \ref run() or \ref start() 731 758 ///must be called before using this function. 732 /// 733 bool reached(Node v) { return (*_reached)[v]; } 759 bool reached(Node v) const { return (*_reached)[v]; } 734 760 735 761 ///@} 736 762 }; 737 763 738 ///Default traits class of Dfsfunction.739 740 ///Default traits class of Dfsfunction.764 ///Default traits class of dfs() function. 765 766 ///Default traits class of dfs() function. 741 767 ///\tparam GR Digraph type. 742 768 template<class GR> 743 769 struct DfsWizardDefaultTraits 744 770 { 745 ///The digraph typethe algorithm runs on.771 ///The type of the digraph the algorithm runs on. 746 772 typedef GR Digraph; 747 ///\brief The type of the map that stores the last 773 774 ///\brief The type of the map that stores the predecessor 748 775 ///arcs of the %DFS paths. 749 776 /// 750 ///The type of the map that stores the last777 ///The type of the map that stores the predecessor 751 778 ///arcs of the %DFS paths. 752 779 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 753 780 /// 754 typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;755 ///Instantiates a PredMap.781 typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; 782 ///Instantiates a \ref PredMap. 756 783 757 784 ///This function instantiates a \ref PredMap. 758 ///\param g is the digraph, to which we would like to define the PredMap. 785 ///\param g is the digraph, to which we would like to define the 786 ///\ref PredMap. 759 787 ///\todo The digraph alone may be insufficient to initialize 760 788 #ifdef DOXYGEN 761 static PredMap *createPredMap(const GR&g)789 static PredMap *createPredMap(const Digraph &g) 762 790 #else 763 static PredMap *createPredMap(const GR&)791 static PredMap *createPredMap(const Digraph &) 764 792 #endif 765 793 { … … 771 799 ///The type of the map that indicates which nodes are processed. 772 800 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 773 ///\todo named parameter to set this type, function to read and write.774 801 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 775 ///Instantiates a ProcessedMap.802 ///Instantiates a \ref ProcessedMap. 776 803 777 804 ///This function instantiates a \ref ProcessedMap. 778 805 ///\param g is the digraph, to which 779 ///we would like to define the \ref ProcessedMap 806 ///we would like to define the \ref ProcessedMap. 780 807 #ifdef DOXYGEN 781 static ProcessedMap *createProcessedMap(const GR&g)808 static ProcessedMap *createProcessedMap(const Digraph &g) 782 809 #else 783 static ProcessedMap *createProcessedMap(const GR&)810 static ProcessedMap *createProcessedMap(const Digraph &) 784 811 #endif 785 812 { 786 813 return new ProcessedMap(); 787 814 } 815 788 816 ///The type of the map that indicates which nodes are reached. 789 817 790 818 ///The type of the map that indicates which nodes are reached. 819 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 820 typedef typename Digraph::template NodeMap<bool> ReachedMap; 821 ///Instantiates a \ref ReachedMap. 822 823 ///This function instantiates a \ref ReachedMap. 824 ///\param g is the digraph, to which 825 ///we would like to define the \ref ReachedMap. 826 static ReachedMap *createReachedMap(const Digraph &g) 827 { 828 return new ReachedMap(g); 829 } 830 831 ///The type of the map that stores the distances of the nodes. 832 833 ///The type of the map that stores the distances of the nodes. 791 834 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 792 ///\todo named parameter to set this type, function to read and write.793 typedef typename Digraph::template NodeMap<bool> ReachedMap;794 ///Instantiates a ReachedMap.795 796 ///This function instantiates a \ref ReachedMap.797 ///\param G is the digraph, to which798 ///we would like to define the \ref ReachedMap.799 static ReachedMap *createReachedMap(const GR &G)800 {801 return new ReachedMap(G);802 }803 ///The type of the map that stores the dists of the nodes.804 805 ///The type of the map that stores the dists of the nodes.806 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.807 835 /// 808 836 typedef NullMap<typename Digraph::Node,int> DistMap; 809 ///Instantiates a DistMap.837 ///Instantiates a \ref DistMap. 810 838 811 839 ///This function instantiates a \ref DistMap. … … 813 841 ///the \ref DistMap 814 842 #ifdef DOXYGEN 815 static DistMap *createDistMap(const GR&g)843 static DistMap *createDistMap(const Digraph &g) 816 844 #else 817 static DistMap *createDistMap(const GR&)845 static DistMap *createDistMap(const Digraph &) 818 846 #endif 819 847 { … … 822 850 }; 823 851 824 /// Default traits used by \ref DfsWizard852 /// Default traits class used by \ref DfsWizard 825 853 826 854 /// To make it easier to use Dfs algorithm 827 /// we have created a wizard class.855 /// we have created a wizard class. 828 856 /// This \ref DfsWizard class needs default traits, 829 /// as well as the \ref Dfs class.857 /// as well as the \ref Dfs class. 830 858 /// The \ref DfsWizardBase is a class to be the default traits of the 831 859 /// \ref DfsWizard class. … … 836 864 typedef DfsWizardDefaultTraits<GR> Base; 837 865 protected: 838 // / Type of the nodes in the digraph.866 //The type of the nodes in the digraph. 839 867 typedef typename Base::Digraph::Node Node; 840 868 841 // / Pointer to the underlying digraph.869 //Pointer to the digraph the algorithm runs on. 842 870 void *_g; 843 // /Pointer to the map of reached nodes.871 //Pointer to the map of reached nodes. 844 872 void *_reached; 845 // /Pointer to the map of processed nodes.873 //Pointer to the map of processed nodes. 846 874 void *_processed; 847 // /Pointer to the map of predecessors arcs.875 //Pointer to the map of predecessors arcs. 848 876 void *_pred; 849 // /Pointer to the map of distances.877 //Pointer to the map of distances. 850 878 void *_dist; 851 // /Pointer to the source node.879 //Pointer to the source node. 852 880 Node _source; 853 881 … … 858 886 /// all of the attributes to default values (0, INVALID). 859 887 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), 860 888 _dist(0), _source(INVALID) {} 861 889 862 890 /// Constructor. … … 865 893 /// listed in the parameters list. 866 894 /// Others are initiated to 0. 867 /// \param g is the initial value of \ref _g868 /// \param s is the initial value of \ref _source895 /// \param g The digraph the algorithm runs on. 896 /// \param s The source node. 869 897 DfsWizardBase(const GR &g, Node s=INVALID) : 870 898 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), … … 873 901 }; 874 902 875 /// A class to make the usage of the Dfs algorithm easier 876 877 /// This class is created to make it easier to use the Dfs algorithm. 878 /// It uses the functions and features of the plain \ref Dfs, 879 /// but it is much simpler to use it. 903 /// Auxiliary class for the function type interface of DFS algorithm. 904 905 /// This auxiliary class is created to implement the function type 906 /// interface of \ref Dfs algorithm. It uses the functions and features 907 /// of the plain \ref Dfs, but it is much simpler to use it. 908 /// It should only be used through the \ref dfs() function, which makes 909 /// it easier to use the algorithm. 880 910 /// 881 911 /// Simplicity means that the way to change the types defined … … 886 916 /// the original class by using the :: 887 917 /// operator. In the case of \ref DfsWizard only 888 /// a function have to be called and it will918 /// a function have to be called, and it will 889 919 /// return the needed class. 890 920 /// 891 /// It does not have own \ref run method. When its \ref run method is called892 /// i t initiates a plain \ref Dfs object, and calls the \ref Dfs::run893 /// method of it.921 /// It does not have own \ref run() method. When its \ref run() method 922 /// is called, it initiates a plain \ref Dfs object, and calls the 923 /// \ref Dfs::run() method of it. 894 924 template<class TR> 895 925 class DfsWizard : public TR … … 897 927 typedef TR Base; 898 928 899 ///The type of the underlying digraph.929 ///The type of the digraph the algorithm runs on. 900 930 typedef typename TR::Digraph Digraph; 901 //\e 931 902 932 typedef typename Digraph::Node Node; 903 //\e904 933 typedef typename Digraph::NodeIt NodeIt; 905 //\e906 934 typedef typename Digraph::Arc Arc; 907 //\e908 935 typedef typename Digraph::OutArcIt OutArcIt; 909 936 910 ///\brief The type of the map that stores 911 ///the reached nodes 937 ///\brief The type of the map that stores the predecessor 938 ///arcs of the shortest paths. 939 typedef typename TR::PredMap PredMap; 940 ///\brief The type of the map that stores the distances of the nodes. 941 typedef typename TR::DistMap DistMap; 942 ///\brief The type of the map that indicates which nodes are reached. 912 943 typedef typename TR::ReachedMap ReachedMap; 913 ///\brief The type of the map that stores 914 ///the processed nodes 944 ///\brief The type of the map that indicates which nodes are processed. 915 945 typedef typename TR::ProcessedMap ProcessedMap; 916 ///\brief The type of the map that stores the last917 ///arcs of the %DFS paths.918 typedef typename TR::PredMap PredMap;919 ///The type of the map that stores the distances of the nodes.920 typedef typename TR::DistMap DistMap;921 946 922 947 public: 948 923 949 /// Constructor. 924 950 DfsWizard() : TR() {} … … 936 962 ~DfsWizard() {} 937 963 938 ///Runs D fs algorithm from a givennode.939 940 ///Runs D fs algorithm from a givennode.941 ///The node can be given by the \ref sourcefunction.964 ///Runs DFS algorithm from a source node. 965 966 ///Runs DFS algorithm from a source node. 967 ///The node can be given with the \ref source() function. 942 968 void run() 943 969 { … … 955 981 } 956 982 957 ///Runs D fsalgorithm from the given node.958 959 ///Runs D fsalgorithm from the given node.983 ///Runs DFS algorithm from the given node. 984 985 ///Runs DFS algorithm from the given node. 960 986 ///\param s is the given source. 961 987 void run(Node s) … … 963 989 Base::_source=s; 964 990 run(); 991 } 992 993 /// Sets the source node, from which the Dfs algorithm runs. 994 995 /// Sets the source node, from which the Dfs algorithm runs. 996 /// \param s is the source node. 997 DfsWizard<TR> &source(Node s) 998 { 999 Base::_source=s; 1000 return *this; 965 1001 } 966 1002 … … 971 1007 DefPredMapBase(const TR &b) : TR(b) {} 972 1008 }; 973 974 1009 ///\brief \ref named-templ-param "Named parameter" 975 ///function for setting PredMap type 976 /// 977 /// \ref named-templ-param "Named parameter" 978 ///function for setting PredMap type 979 /// 1010 ///for setting \ref PredMap object. 1011 /// 1012 ///\ref named-templ-param "Named parameter" 1013 ///for setting \ref PredMap object. 980 1014 template<class T> 981 1015 DfsWizard<DefPredMapBase<T> > predMap(const T &t) … … 984 1018 return DfsWizard<DefPredMapBase<T> >(*this); 985 1019 } 986 987 1020 988 1021 template<class T> … … 992 1025 DefReachedMapBase(const TR &b) : TR(b) {} 993 1026 }; 994 995 1027 ///\brief \ref named-templ-param "Named parameter" 996 ///f unction for setting ReachedMap1028 ///for setting \ref ReachedMap object. 997 1029 /// 998 1030 /// \ref named-templ-param "Named parameter" 999 ///function for setting ReachedMap 1000 /// 1031 ///for setting \ref ReachedMap object. 1001 1032 template<class T> 1002 1033 DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) … … 1005 1036 return DfsWizard<DefReachedMapBase<T> >(*this); 1006 1037 } 1007 1008 1038 1009 1039 template<class T> … … 1013 1043 DefProcessedMapBase(const TR &b) : TR(b) {} 1014 1044 }; 1015 1016 1045 ///\brief \ref named-templ-param "Named parameter" 1017 ///f unction for setting ProcessedMap1046 ///for setting \ref ProcessedMap object. 1018 1047 /// 1019 1048 /// \ref named-templ-param "Named parameter" 1020 ///function for setting ProcessedMap 1021 /// 1049 ///for setting \ref ProcessedMap object. 1022 1050 template<class T> 1023 1051 DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) … … 1033 1061 DefDistMapBase(const TR &b) : TR(b) {} 1034 1062 }; 1035 1036 1063 ///\brief \ref named-templ-param "Named parameter" 1037 ///function for setting DistMap type 1038 /// 1039 /// \ref named-templ-param "Named parameter" 1040 ///function for setting DistMap type 1041 /// 1064 ///for setting \ref DistMap object. 1065 /// 1066 ///\ref named-templ-param "Named parameter" 1067 ///for setting \ref DistMap object. 1042 1068 template<class T> 1043 1069 DfsWizard<DefDistMapBase<T> > distMap(const T &t) … … 1045 1071 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1046 1072 return DfsWizard<DefDistMapBase<T> >(*this); 1047 }1048 1049 /// Sets the source node, from which the Dfs algorithm runs.1050 1051 /// Sets the source node, from which the Dfs algorithm runs.1052 /// \param s is the source node.1053 DfsWizard<TR> &source(Node s)1054 {1055 Base::_source=s;1056 return *this;1057 1073 } 1058 1074 … … 1084 1100 1085 1101 #ifdef DOXYGEN 1086 /// \brief Visitor class for dfs.1102 /// \brief Visitor class for DFS. 1087 1103 /// 1088 /// It gives a simple interface for a functional interface for dfs1089 /// traversal. The traversal on a linear data structure.1104 /// This class defines the interface of the DfsVisit events, and 1105 /// it could be the base of a real visitor class. 1090 1106 template <typename _Digraph> 1091 1107 struct DfsVisitor { … … 1093 1109 typedef typename Digraph::Arc Arc; 1094 1110 typedef typename Digraph::Node Node; 1095 /// \brief Called when the arc reach a node. 1096 /// 1097 /// It is called when the dfs find an arc which target is not 1098 /// reached yet. 1111 /// \brief Called for the source node of the DFS. 1112 /// 1113 /// This function is called for the source node of the DFS. 1114 void start(const Node& node) {} 1115 /// \brief Called when the source node is leaved. 1116 /// 1117 /// This function is called when the source node is leaved. 1118 void stop(const Node& node) {} 1119 /// \brief Called when a node is reached first time. 1120 /// 1121 /// This function is called when a node is reached first time. 1122 void reach(const Node& node) {} 1123 /// \brief Called when an arc reaches a new node. 1124 /// 1125 /// This function is called when the DFS finds an arc whose target node 1126 /// is not reached yet. 1099 1127 void discover(const Arc& arc) {} 1100 /// \brief Called when the node reached first time. 1101 /// 1102 /// It is Called when the node reached first time. 1103 void reach(const Node& node) {} 1104 /// \brief Called when we step back on an arc. 1105 /// 1106 /// It is called when the dfs should step back on the arc. 1107 void backtrack(const Arc& arc) {} 1108 /// \brief Called when we step back from the node. 1109 /// 1110 /// It is called when we step back from the node. 1111 void leave(const Node& node) {} 1112 /// \brief Called when the arc examined but target of the arc 1128 /// \brief Called when an arc is examined but its target node is 1113 1129 /// already discovered. 1114 1130 /// 1115 /// It called when the arc examined but the target of the arc1131 /// This function is called when an arc is examined but its target node is 1116 1132 /// already discovered. 1117 1133 void examine(const Arc& arc) {} 1118 /// \brief Called for the source node of the dfs. 1119 /// 1120 /// It is called for the source node of the dfs. 1121 void start(const Node& node) {} 1122 /// \brief Called when we leave the source node of the dfs. 1123 /// 1124 /// It is called when we leave the source node of the dfs. 1125 void stop(const Node& node) {} 1126 1134 /// \brief Called when the DFS steps back from a node. 1135 /// 1136 /// This function is called when the DFS steps back from a node. 1137 void leave(const Node& node) {} 1138 /// \brief Called when the DFS steps back on an arc. 1139 /// 1140 /// This function is called when the DFS steps back on an arc. 1141 void backtrack(const Arc& arc) {} 1127 1142 }; 1128 1143 #else … … 1132 1147 typedef typename Digraph::Arc Arc; 1133 1148 typedef typename Digraph::Node Node; 1134 void discover(const Arc&) {}1135 void reach(const Node&) {}1136 void backtrack(const Arc&) {}1137 void leave(const Node&) {}1138 void examine(const Arc&) {}1139 1149 void start(const Node&) {} 1140 1150 void stop(const Node&) {} 1151 void reach(const Node&) {} 1152 void discover(const Arc&) {} 1153 void examine(const Arc&) {} 1154 void leave(const Node&) {} 1155 void backtrack(const Arc&) {} 1141 1156 1142 1157 template <typename _Visitor> … … 1145 1160 Arc arc; 1146 1161 Node node; 1147 visitor.discover(arc);1148 visitor.reach(node);1149 visitor.backtrack(arc);1150 visitor.leave(node);1151 visitor.examine(arc);1152 1162 visitor.start(node); 1153 1163 visitor.stop(arc); 1164 visitor.reach(node); 1165 visitor.discover(arc); 1166 visitor.examine(arc); 1167 visitor.leave(node); 1168 visitor.backtrack(arc); 1154 1169 } 1155 1170 _Visitor& visitor; … … 1161 1176 /// 1162 1177 /// Default traits class of DfsVisit class. 1163 /// \tparam _Digraph Digraph type.1178 /// \tparam _Digraph The type of the digraph the algorithm runs on. 1164 1179 template<class _Digraph> 1165 1180 struct DfsVisitDefaultTraits { 1166 1181 1167 /// \brief The digraph typethe algorithm runs on.1182 /// \brief The type of the digraph the algorithm runs on. 1168 1183 typedef _Digraph Digraph; 1169 1184 … … 1171 1186 /// 1172 1187 /// The type of the map that indicates which nodes are reached. 1173 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. 1174 /// \todo named parameter to set this type, function to read and write. 1188 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 1175 1189 typedef typename Digraph::template NodeMap<bool> ReachedMap; 1176 1190 1177 /// \brief Instantiates a ReachedMap.1191 /// \brief Instantiates a \ref ReachedMap. 1178 1192 /// 1179 1193 /// This function instantiates a \ref ReachedMap. … … 1186 1200 }; 1187 1201 1188 /// %DFS Visit algorithm class.1189 1190 1202 /// \ingroup search 1203 /// 1204 /// \brief %DFS algorithm class with visitor interface. 1205 /// 1191 1206 /// This class provides an efficient implementation of the %DFS algorithm 1192 1207 /// with visitor interface. … … 1194 1209 /// The %DfsVisit class provides an alternative interface to the Dfs 1195 1210 /// class. It works with callback mechanism, the DfsVisit object calls 1196 /// on every dfs event the \c Visitor class member functions.1211 /// the member functions of the \c Visitor class on every DFS event. 1197 1212 /// 1198 /// \tparam _Digraph The digraph typethe algorithm runs on.1213 /// \tparam _Digraph The type of the digraph the algorithm runs on. 1199 1214 /// The default value is 1200 /// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it1201 /// is only passed to \ref DfsDefaultTraits.1202 /// \tparam _Visitor The Visitor object for the algorithm. The1203 /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitorwhich1204 /// does not observe the D fs events. If you want to observe the dfs1205 /// events you should implement your own Visitor class.1215 /// \ref ListDigraph. The value of _Digraph is not used directly by 1216 /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. 1217 /// \tparam _Visitor The Visitor type that is used by the algorithm. 1218 /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which 1219 /// does not observe the DFS events. If you want to observe the DFS 1220 /// events, you should implement your own visitor class. 1206 1221 /// \tparam _Traits Traits class to set various data types used by the 1207 1222 /// algorithm. The default traits class is 1208 1223 /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". 1209 1224 /// See \ref DfsVisitDefaultTraits for the documentation of 1210 /// a Dfs visit traits class. 1211 /// 1212 /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso 1225 /// a DFS visit traits class. 1213 1226 #ifdef DOXYGEN 1214 1227 template <typename _Digraph, typename _Visitor, typename _Traits> … … 1224 1237 /// 1225 1238 /// This error represents problems in the initialization 1226 /// of the parameters of the algorithm s.1239 /// of the parameters of the algorithm. 1227 1240 class UninitializedParameter : public lemon::UninitializedParameter { 1228 1241 public: … … 1233 1246 }; 1234 1247 1248 ///The traits class. 1235 1249 typedef _Traits Traits; 1236 1250 1251 ///The type of the digraph the algorithm runs on. 1237 1252 typedef typename Traits::Digraph Digraph; 1238 1253 1254 ///The visitor type used by the algorithm. 1239 1255 typedef _Visitor Visitor; 1240 1256 1241 ///The type of the map indicatingwhich nodes are reached.1257 ///The type of the map that indicates which nodes are reached. 1242 1258 typedef typename Traits::ReachedMap ReachedMap; 1243 1259 … … 1249 1265 typedef typename Digraph::OutArcIt OutArcIt; 1250 1266 1251 // /Pointer to the underlying digraph.1267 //Pointer to the underlying digraph. 1252 1268 const Digraph *_digraph; 1253 // /Pointer to the visitor object.1269 //Pointer to the visitor object. 1254 1270 Visitor *_visitor; 1255 // /Pointer to the map of reached status of the nodes.1271 //Pointer to the map of reached status of the nodes. 1256 1272 ReachedMap *_reached; 1257 // /Indicates if \ref _reached is locally allocated (\ctrue) or not.1273 //Indicates if _reached is locally allocated (true) or not. 1258 1274 bool local_reached; 1259 1275 … … 1261 1277 int _stack_head; 1262 1278 1263 /// \brief Creates the maps if necessary. 1264 /// 1265 /// Creates the maps if necessary. 1279 ///Creates the maps if necessary. 1280 ///\todo Better memory allocation (instead of new). 1266 1281 void create_maps() { 1267 1282 if(!_reached) { … … 1290 1305 }; 1291 1306 /// \brief \ref named-templ-param "Named parameter" for setting 1292 /// ReachedMap type 1293 /// 1294 /// \ref named-templ-param "Named parameter" for setting ReachedMap type 1307 /// ReachedMap type. 1308 /// 1309 /// \ref named-templ-param "Named parameter" for setting ReachedMap type. 1295 1310 template <class T> 1296 1311 struct DefReachedMap : public DfsVisit< Digraph, Visitor, … … 1306 1321 /// Constructor. 1307 1322 /// 1308 /// \param digraph the digraph the algorithm will run on. 1309 /// \param visitor The visitor of the algorithm. 1310 /// 1323 /// \param digraph The digraph the algorithm runs on. 1324 /// \param visitor The visitor object of the algorithm. 1311 1325 DfsVisit(const Digraph& digraph, Visitor& visitor) 1312 1326 : _digraph(&digraph), _visitor(&visitor), … … 1314 1328 1315 1329 /// \brief Destructor. 1316 ///1317 /// Destructor.1318 1330 ~DfsVisit() { 1319 1331 if(local_reached) delete _reached; 1320 1332 } 1321 1333 1322 /// \brief Sets the map indicating if a node isreached.1323 /// 1324 /// Sets the map indicating if a node isreached.1334 /// \brief Sets the map that indicates which nodes are reached. 1335 /// 1336 /// Sets the map that indicates which nodes are reached. 1325 1337 /// If you don't use this function before calling \ref run(), 1326 /// it will allocate one. The dest uctor deallocates this1338 /// it will allocate one. The destructor deallocates this 1327 1339 /// automatically allocated map, of course. 1328 1340 /// \return <tt> (*this) </tt> … … 1337 1349 1338 1350 public: 1351 1339 1352 /// \name Execution control 1340 1353 /// The simplest way to execute the algorithm is to use 1341 /// one of the member functions called \c run(...). 1354 /// one of the member functions called \ref lemon::DfsVisit::run() 1355 /// "run()". 1342 1356 /// \n 1343 /// If you need more control on the execution, 1344 /// first you must call \ref init(), then you can adda source node1345 /// with \ref addSource().1346 /// Finally \ref start() will perform the actual path1347 /// computation.1357 /// If you need more control on the execution, first you must call 1358 /// \ref lemon::DfsVisit::init() "init()", then you can add several 1359 /// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". 1360 /// Finally \ref lemon::DfsVisit::start() "start()" will perform the 1361 /// actual path computation. 1348 1362 1349 1363 /// @{ 1364 1350 1365 /// \brief Initializes the internal data structures. 1351 1366 /// 1352 1367 /// Initializes the internal data structures. 1353 ///1354 1368 void init() { 1355 1369 create_maps(); … … 1361 1375 } 1362 1376 1363 /// \brief Adds a new source node. 1364 /// 1365 /// Adds a new source node to the set of nodes to be processed. 1366 void addSource(Node s) { 1377 ///Adds a new source node. 1378 1379 ///Adds a new source node to the set of nodes to be processed. 1380 /// 1381 ///\pre The stack must be empty. (Otherwise the algorithm gives 1382 ///false results.) 1383 /// 1384 ///\warning Distances will be wrong (or at least strange) in case of 1385 ///multiple sources. 1386 void addSource(Node s) 1387 { 1388 LEMON_DEBUG(emptyQueue(), "The stack is not empty."); 1367 1389 if(!(*_reached)[s]) { 1368 1390 _reached->set(s,true); … … 1385 1407 /// \return The processed arc. 1386 1408 /// 1387 /// \pre The stack must not be empty !1409 /// \pre The stack must not be empty. 1388 1410 Arc processNextArc() { 1389 1411 Arc e = _stack[_stack_head]; … … 1419 1441 /// \return The next arc to be processed or INVALID if the stack is 1420 1442 /// empty. 1421 Arc nextArc() {1443 Arc nextArc() const { 1422 1444 return _stack_head >= 0 ? _stack[_stack_head] : INVALID; 1423 1445 } 1424 1446 1425 1447 /// \brief Returns \c false if there are nodes 1426 /// to be processed in the queue1448 /// to be processed. 1427 1449 /// 1428 1450 /// Returns \c false if there are nodes 1429 /// to be processed in the queue 1430 bool emptyQueue() { return _stack_head < 0; }1451 /// to be processed in the queue (stack). 1452 bool emptyQueue() const { return _stack_head < 0; } 1431 1453 1432 1454 /// \brief Returns the number of the nodes to be processed. 1433 1455 /// 1434 /// Returns the number of the nodes to be processed in the queue .1435 int queueSize() { return _stack_head + 1; }1456 /// Returns the number of the nodes to be processed in the queue (stack). 1457 int queueSize() const { return _stack_head + 1; } 1436 1458 1437 1459 /// \brief Executes the algorithm. … … 1439 1461 /// Executes the algorithm. 1440 1462 /// 1441 /// \pre init() must be called and at least one node should be added 1442 /// with addSource() before using this function. 1463 /// This method runs the %DFS algorithm from the root node 1464 /// in order to compute the %DFS path to each node. 1465 /// 1466 /// The algorithm computes 1467 /// - the %DFS tree, 1468 /// - the distance of each node from the root in the %DFS tree. 1469 /// 1470 /// \pre init() must be called and a root node should be 1471 /// added with addSource() before using this function. 1472 /// 1473 /// \note <tt>d.start()</tt> is just a shortcut of the following code. 1474 /// \code 1475 /// while ( !d.emptyQueue() ) { 1476 /// d.processNextArc(); 1477 /// } 1478 /// \endcode 1443 1479 void start() { 1444 1480 while ( !emptyQueue() ) processNextArc(); 1445 1481 } 1446 1482 1447 /// \brief Executes the algorithm until \c dest is reached. 1448 /// 1449 /// Executes the algorithm until \c dest is reached. 1450 /// 1451 /// \pre init() must be called and at least one node should be added 1483 /// \brief Executes the algorithm until the given target node is reached. 1484 /// 1485 /// Executes the algorithm until the given target node is reached. 1486 /// 1487 /// This method runs the %DFS algorithm from the root node 1488 /// in order to compute the DFS path to \c dest. 1489 /// 1490 /// The algorithm computes 1491 /// - the %DFS path to \c dest, 1492 /// - the distance of \c dest from the root in the %DFS tree. 1493 /// 1494 /// \pre init() must be called and a root node should be added 1452 1495 /// with addSource() before using this function. 1453 1496 void start(Node dest) { … … 1460 1503 /// Executes the algorithm until a condition is met. 1461 1504 /// 1462 /// \pre init() must be called and at least one node should be added 1505 /// This method runs the %DFS algorithm from the root node 1506 /// until an arc \c a with <tt>am[a]</tt> true is found. 1507 /// 1508 /// \param am A \c bool (or convertible) arc map. The algorithm 1509 /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. 1510 /// 1511 /// \return The reached arc \c a with <tt>am[a]</tt> true or 1512 /// \c INVALID if no such arc was found. 1513 /// 1514 /// \pre init() must be called and a root node should be added 1463 1515 /// with addSource() before using this function. 1464 1516 /// 1465 /// \param em must be a bool (or convertible) arc map. The algorithm 1466 /// will stop when it reaches an arc \c e with <tt>em[e]</tt> true. 1467 /// 1468 ///\return The reached arc \c e with <tt>em[e]</tt> true or 1469 ///\c INVALID if no such arc was found. 1470 /// 1471 /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map, 1517 /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, 1472 1518 /// not a node map. 1473 template <typename EM>1474 Arc start(const EM &em) {1475 while ( !emptyQueue() && ! em[_stack[_stack_head]] )1519 template <typename AM> 1520 Arc start(const AM &am) { 1521 while ( !emptyQueue() && !am[_stack[_stack_head]] ) 1476 1522 processNextArc(); 1477 1523 return emptyQueue() ? INVALID : _stack[_stack_head]; 1478 1524 } 1479 1525 1480 /// \brief Runs %DFSVisit algorithm from node \c s. 1481 /// 1482 /// This method runs the %DFS algorithm from a root node \c s. 1483 /// \note d.run(s) is just a shortcut of the following code. 1526 /// \brief Runs the algorithm from the given node. 1527 /// 1528 /// This method runs the %DFS algorithm from node \c s. 1529 /// in order to compute the DFS path to each node. 1530 /// 1531 /// The algorithm computes 1532 /// - the %DFS tree, 1533 /// - the distance of each node from the root in the %DFS tree. 1534 /// 1535 /// \note <tt>d.run(s)</tt> is just a shortcut of the following code. 1484 1536 ///\code 1485 1537 /// d.init(); … … 1493 1545 } 1494 1546 1495 /// \brief Runs %DFSVisit algorithm to visit all nodes in the digraph. 1547 /// \brief Finds the %DFS path between \c s and \c t. 1548 1549 /// This method runs the %DFS algorithm from node \c s 1550 /// in order to compute the DFS path to \c t. 1551 /// 1552 /// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, 1553 /// if \c t is reachable form \c s, \c 0 otherwise. 1554 /// 1555 /// \note Apart from the return value, <tt>d.run(s,t)</tt> is 1556 /// just a shortcut of the following code. 1557 ///\code 1558 /// d.init(); 1559 /// d.addSource(s); 1560 /// d.start(t); 1561 ///\endcode 1562 int run(Node s,Node t) { 1563 init(); 1564 addSource(s); 1565 start(t); 1566 return reached(t)?_stack_head+1:0; 1567 } 1568 1569 /// \brief Runs the algorithm to visit all nodes in the digraph. 1496 1570 1497 1571 /// This method runs the %DFS algorithm in order to 1498 /// compute the %DFS path to each node. The algorithm computes 1499 /// - The %DFS tree. 1500 /// - The distance of each node from the root in the %DFS tree. 1501 /// 1502 ///\note d.run() is just a shortcut of the following code. 1572 /// compute the %DFS path to each node. 1573 /// 1574 /// The algorithm computes 1575 /// - the %DFS tree, 1576 /// - the distance of each node from the root in the %DFS tree. 1577 /// 1578 /// \note <tt>d.run()</tt> is just a shortcut of the following code. 1503 1579 ///\code 1504 /// d.init();1505 /// for (NodeIt it(digraph); it != INVALID; ++it) {1506 /// if (!d.reached(it)) {1507 /// d.addSource(it);1508 /// d.start();1509 /// }1510 /// }1580 /// d.init(); 1581 /// for (NodeIt n(digraph); n != INVALID; ++n) { 1582 /// if (!d.reached(n)) { 1583 /// d.addSource(n); 1584 /// d.start(); 1585 /// } 1586 /// } 1511 1587 ///\endcode 1512 1588 void run() { … … 1519 1595 } 1520 1596 } 1597 1521 1598 ///@} 1522 1599 … … 1524 1601 /// The result of the %DFS algorithm can be obtained using these 1525 1602 /// functions.\n 1526 /// Before the use of these functions, 1527 /// either run() or start() must be called. 1603 /// Either \ref lemon::DfsVisit::run() "run()" or 1604 /// \ref lemon::DfsVisit::start() "start()" must be called before 1605 /// using them. 1528 1606 ///@{ 1529 /// \brief Checks if a node is reachable from the root. 1607 1608 /// \brief Checks if a node is reachable from the root(s). 1530 1609 /// 1531 1610 /// Returns \c true if \c v is reachable from the root(s). 1532 /// \warning The source nodes are inditated as unreachable.1533 1611 /// \pre Either \ref run() or \ref start() 1534 1612 /// must be called before using this function. 1535 ///1536 1613 bool reached(Node v) { return (*_reached)[v]; } 1614 1537 1615 ///@} 1616 1538 1617 }; 1539 1618 1540 1541 1619 } //END OF NAMESPACE LEMON 1542 1620 1543 1621 #endif 1544
Note: See TracChangeset
for help on using the changeset viewer.