Changeset 247:f1158744a112 in lemon for lemon/bfs.h
 Timestamp:
 08/04/08 22:00:36 (16 years ago)
 Branch:
 default
 Children:
 248:8fada33fc60a, 365:37557a46e298
 Parents:
 246:7c67988fca07 (diff), 244:c30731a37f91 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.  Phase:
 public
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

lemon/bfs.h
r244 r247 25 25 26 26 #include <lemon/list_graph.h> 27 #include <lemon/graph_utils.h>28 27 #include <lemon/bits/path_dump.h> 29 #include <lemon/ bits/invalid.h>28 #include <lemon/core.h> 30 29 #include <lemon/error.h> 31 30 #include <lemon/maps.h> 
lemon/bfs.h
r220 r247 22 22 ///\ingroup search 23 23 ///\file 24 ///\brief B fsalgorithm.24 ///\brief BFS algorithm. 25 25 26 26 #include <lemon/list_graph.h> … … 32 32 namespace lemon { 33 33 34 35 36 34 ///Default traits class of Bfs class. 37 35 … … 41 39 struct BfsDefaultTraits 42 40 { 43 ///The digraph typethe algorithm runs on.41 ///The type of the digraph the algorithm runs on. 44 42 typedef GR Digraph; 45 ///\brief The type of the map that stores the last 43 44 ///\brief The type of the map that stores the predecessor 46 45 ///arcs of the shortest paths. 47 46 /// 48 ///The type of the map that stores the last47 ///The type of the map that stores the predecessor 49 48 ///arcs of the shortest paths. 50 49 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 51 /// 52 typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; 53 ///Instantiates a PredMap. 50 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 51 ///Instantiates a \ref PredMap. 54 52 55 53 ///This function instantiates a \ref PredMap. 56 ///\param G is the digraph, to which we would like to define the PredMap. 54 ///\param g is the digraph, to which we would like to define the 55 ///\ref PredMap. 57 56 ///\todo The digraph alone may be insufficient to initialize 58 static PredMap *createPredMap(const GR &G) 59 { 60 return new PredMap(G); 61 } 57 static PredMap *createPredMap(const Digraph &g) 58 { 59 return new PredMap(g); 60 } 61 62 62 ///The type of the map that indicates which nodes are processed. 63 63 64 64 ///The type of the map that indicates which nodes are processed. 65 65 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 66 /// \todo named parameter to set this type, function to read and write.66 ///By default it is a NullMap. 67 67 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 68 ///Instantiates a ProcessedMap.68 ///Instantiates a \ref ProcessedMap. 69 69 70 70 ///This function instantiates a \ref ProcessedMap. … … 72 72 ///we would like to define the \ref ProcessedMap 73 73 #ifdef DOXYGEN 74 static ProcessedMap *createProcessedMap(const GR&g)74 static ProcessedMap *createProcessedMap(const Digraph &g) 75 75 #else 76 static ProcessedMap *createProcessedMap(const GR&)76 static ProcessedMap *createProcessedMap(const Digraph &) 77 77 #endif 78 78 { 79 79 return new ProcessedMap(); 80 80 } 81 81 82 ///The type of the map that indicates which nodes are reached. 82 83 83 84 ///The type of the map that indicates which nodes are reached. 85 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 86 typedef typename Digraph::template NodeMap<bool> ReachedMap; 87 ///Instantiates a \ref ReachedMap. 88 89 ///This function instantiates a \ref ReachedMap. 90 ///\param g is the digraph, to which 91 ///we would like to define the \ref ReachedMap. 92 static ReachedMap *createReachedMap(const Digraph &g) 93 { 94 return new ReachedMap(g); 95 } 96 97 ///The type of the map that stores the distances of the nodes. 98 99 ///The type of the map that stores the distances of the nodes. 84 100 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 85 ///\todo named parameter to set this type, function to read and write.86 typedef typename Digraph::template NodeMap<bool> ReachedMap;87 ///Instantiates a ReachedMap.88 89 ///This function instantiates a \ref ReachedMap.90 ///\param G is the digraph, to which91 ///we would like to define the \ref ReachedMap.92 static ReachedMap *createReachedMap(const GR &G)93 {94 return new ReachedMap(G);95 }96 ///The type of the map that stores the dists of the nodes.97 98 ///The type of the map that stores the dists of the nodes.99 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.100 ///101 101 typedef typename Digraph::template NodeMap<int> DistMap; 102 ///Instantiates a DistMap.102 ///Instantiates a \ref DistMap. 103 103 104 104 ///This function instantiates a \ref DistMap. 105 ///\param G is the digraph, to which we would like to define106 /// the \ref DistMap107 static DistMap *createDistMap(const GR &G)108 { 109 return new DistMap( G);105 ///\param g is the digraph, to which we would like to define the 106 ///\ref DistMap. 107 static DistMap *createDistMap(const Digraph &g) 108 { 109 return new DistMap(g); 110 110 } 111 111 }; … … 116 116 ///This class provides an efficient implementation of the %BFS algorithm. 117 117 /// 118 ///\tparam GR The digraph type the algorithm runs on. The default value is 119 ///\ref ListDigraph. The value of GR is not used directly by Bfs, it 120 ///is only passed to \ref BfsDefaultTraits. 118 ///There is also a \ref bfs() "function type interface" for the BFS 119 ///algorithm, which is convenient in the simplier cases and it can be 120 ///used easier. 121 /// 122 ///\tparam GR The type of the digraph the algorithm runs on. 123 ///The default value is \ref ListDigraph. The value of GR is not used 124 ///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. 121 125 ///\tparam TR Traits class to set various data types used by the algorithm. 122 126 ///The default traits class is … … 124 128 ///See \ref BfsDefaultTraits for the documentation of 125 129 ///a Bfs traits class. 126 127 130 #ifdef DOXYGEN 128 131 template <typename GR, … … 134 137 class Bfs { 135 138 public: 136 /** 137 * \brief \ref Exception for uninitialized parameters. 138 * 139 * This error represents problems in the initialization 140 * of the parameters of the algorithms. 141 */ 139 ///\ref Exception for uninitialized parameters. 140 141 ///This error represents problems in the initialization of the 142 ///parameters of the algorithm. 142 143 class UninitializedParameter : public lemon::UninitializedParameter { 143 144 public: … … 147 148 }; 148 149 150 ///The type of the digraph the algorithm runs on. 151 typedef typename TR::Digraph Digraph; 152 153 ///\brief The type of the map that stores the predecessor arcs of the 154 ///shortest paths. 155 typedef typename TR::PredMap PredMap; 156 ///The type of the map that stores the distances of the nodes. 157 typedef typename TR::DistMap DistMap; 158 ///The type of the map that indicates which nodes are reached. 159 typedef typename TR::ReachedMap ReachedMap; 160 ///The type of the map that indicates which nodes are processed. 161 typedef typename TR::ProcessedMap ProcessedMap; 162 ///The type of the paths. 163 typedef PredMapPath<Digraph, PredMap> Path; 164 165 ///The traits class. 149 166 typedef TR Traits; 150 ///The type of the underlying digraph. 151 typedef typename TR::Digraph Digraph; 152 153 ///\brief The type of the map that stores the last 154 ///arcs of the shortest paths. 155 typedef typename TR::PredMap PredMap; 156 ///The type of the map indicating which nodes are reached. 157 typedef typename TR::ReachedMap ReachedMap; 158 ///The type of the map indicating which nodes are processed. 159 typedef typename TR::ProcessedMap ProcessedMap; 160 ///The type of the map that stores the dists of the nodes. 161 typedef typename TR::DistMap DistMap; 167 162 168 private: 163 169 … … 167 173 typedef typename Digraph::OutArcIt OutArcIt; 168 174 169 // /Pointer to the underlying digraph.175 //Pointer to the underlying digraph. 170 176 const Digraph *G; 171 // /Pointer to the map of predecessorsarcs.177 //Pointer to the map of predecessor arcs. 172 178 PredMap *_pred; 173 // /Indicates if \ref _pred is locally allocated (\ctrue) or not.179 //Indicates if _pred is locally allocated (true) or not. 174 180 bool local_pred; 175 // /Pointer to the map of distances.181 //Pointer to the map of distances. 176 182 DistMap *_dist; 177 // /Indicates if \ref _dist is locally allocated (\ctrue) or not.183 //Indicates if _dist is locally allocated (true) or not. 178 184 bool local_dist; 179 // /Pointer to the map of reached status of the nodes.185 //Pointer to the map of reached status of the nodes. 180 186 ReachedMap *_reached; 181 // /Indicates if \ref _reached is locally allocated (\ctrue) or not.187 //Indicates if _reached is locally allocated (true) or not. 182 188 bool local_reached; 183 // /Pointer to the map of processed status of the nodes.189 //Pointer to the map of processed status of the nodes. 184 190 ProcessedMap *_processed; 185 // /Indicates if \ref _processed is locally allocated (\ctrue) or not.191 //Indicates if _processed is locally allocated (true) or not. 186 192 bool local_processed; 187 193 … … 191 197 192 198 ///Creates the maps if necessary. 193 194 199 ///\todo Better memory allocation (instead of new). 195 200 void create_maps() … … 234 239 }; 235 240 ///\brief \ref namedtemplparam "Named parameter" for setting 236 /// PredMap type237 /// 238 ///\ref namedtemplparam "Named parameter" for setting PredMap type239 /// 241 ///\ref PredMap type. 242 /// 243 ///\ref namedtemplparam "Named parameter" for setting 244 ///\ref PredMap type. 240 245 template <class T> 241 246 struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { … … 252 257 }; 253 258 ///\brief \ref namedtemplparam "Named parameter" for setting 254 /// DistMap type255 /// 256 ///\ref namedtemplparam "Named parameter" for setting DistMap type257 /// 259 ///\ref DistMap type. 260 /// 261 ///\ref namedtemplparam "Named parameter" for setting 262 ///\ref DistMap type. 258 263 template <class T> 259 264 struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { … … 270 275 }; 271 276 ///\brief \ref namedtemplparam "Named parameter" for setting 272 /// ReachedMap type273 /// 274 ///\ref namedtemplparam "Named parameter" for setting ReachedMap type275 /// 277 ///\ref ReachedMap type. 278 /// 279 ///\ref namedtemplparam "Named parameter" for setting 280 ///\ref ReachedMap type. 276 281 template <class T> 277 282 struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { … … 288 293 }; 289 294 ///\brief \ref namedtemplparam "Named parameter" for setting 290 /// ProcessedMap type291 /// 292 ///\ref namedtemplparam "Named parameter" for setting ProcessedMap type293 /// 295 ///\ref ProcessedMap type. 296 /// 297 ///\ref namedtemplparam "Named parameter" for setting 298 ///\ref ProcessedMap type. 294 299 template <class T> 295 300 struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > { … … 299 304 struct DefDigraphProcessedMapTraits : public Traits { 300 305 typedef typename Digraph::template NodeMap<bool> ProcessedMap; 301 static ProcessedMap *createProcessedMap(const Digraph & G)306 static ProcessedMap *createProcessedMap(const Digraph &g) 302 307 { 303 return new ProcessedMap( G);304 } 305 }; 306 ///\brief \ref namedtemplparam "Named parameter" 307 /// for setting the ProcessedMap type to be Digraph::NodeMap<bool>.308 /// 309 ///\ref namedtemplparam "Named parameter" 310 /// for setting the ProcessedMap type to be Digraph::NodeMap<bool>.308 return new ProcessedMap(g); 309 } 310 }; 311 ///\brief \ref namedtemplparam "Named parameter" for setting 312 ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 313 /// 314 ///\ref namedtemplparam "Named parameter" for setting 315 ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 311 316 ///If you don't set it explicitly, it will be automatically allocated. 312 317 template <class T> … … 322 327 ///Constructor. 323 328 324 /// \param _G the digraph the algorithm will run on.325 /// 326 Bfs(const Digraph & _G) :327 G(& _G),329 ///Constructor. 330 ///\param g The digraph the algorithm runs on. 331 Bfs(const Digraph &g) : 332 G(&g), 328 333 _pred(NULL), local_pred(false), 329 334 _dist(NULL), local_dist(false), … … 341 346 } 342 347 343 ///Sets the map storingthe predecessor arcs.344 345 ///Sets the map storingthe predecessor arcs.348 ///Sets the map that stores the predecessor arcs. 349 350 ///Sets the map that stores the predecessor arcs. 346 351 ///If you don't use this function before calling \ref run(), 347 352 ///it will allocate one. The destructor deallocates this … … 358 363 } 359 364 360 ///Sets the map indicating the reached nodes.361 362 ///Sets the map indicating the reached nodes.365 ///Sets the map that indicates which nodes are reached. 366 367 ///Sets the map that indicates which nodes are reached. 363 368 ///If you don't use this function before calling \ref run(), 364 369 ///it will allocate one. The destructor deallocates this … … 375 380 } 376 381 377 ///Sets the map indicating the processed nodes.378 379 ///Sets the map indicating the processed nodes.382 ///Sets the map that indicates which nodes are processed. 383 384 ///Sets the map that indicates which nodes are processed. 380 385 ///If you don't use this function before calling \ref run(), 381 386 ///it will allocate one. The destructor deallocates this … … 392 397 } 393 398 394 ///Sets the map storing the distances calculated by the algorithm. 395 396 ///Sets the map storing the distances calculated by the algorithm. 399 ///Sets the map that stores the distances of the nodes. 400 401 ///Sets the map that stores the distances of the nodes calculated by 402 ///the algorithm. 397 403 ///If you don't use this function before calling \ref run(), 398 404 ///it will allocate one. The destructor deallocates this … … 410 416 411 417 public: 418 412 419 ///\name Execution control 413 420 ///The simplest way to execute the algorithm is to use 414 ///one of the member functions called \ c run(...).421 ///one of the member functions called \ref lemon::Bfs::run() "run()". 415 422 ///\n 416 ///If you need more control on the execution, 417 /// first you must call \ref init(), then you can add several source nodes418 /// with \ref addSource().419 ///Finally \ref start() will perform the actual path420 /// computation.423 ///If you need more control on the execution, first you must call 424 ///\ref lemon::Bfs::init() "init()", then you can add several source 425 ///nodes with \ref lemon::Bfs::addSource() "addSource()". 426 ///Finally \ref lemon::Bfs::start() "start()" will perform the 427 ///actual path computation. 421 428 422 429 ///@{ 423 430 424 /// \briefInitializes the internal data structures.425 /// 431 ///Initializes the internal data structures. 432 426 433 ///Initializes the internal data structures. 427 434 /// … … 461 468 ///\return The processed node. 462 469 /// 463 ///\ warning The queue must not be empty!470 ///\pre The queue must not be empty. 464 471 Node processNextNode() 465 472 { … … 483 490 ///Processes the next node. 484 491 485 ///Processes the next node . And checks thatthe given target node492 ///Processes the next node and checks if the given target node 486 493 ///is reached. If the target node is reachable from the processed 487 ///node then the reached parameter will be set true. The reached 488 ///parameter should be initially false. 494 ///node, then the \c reach parameter will be set to \c true. 489 495 /// 490 496 ///\param target The target node. 491 ///\retval reach Indicates that the target node is reached. 497 ///\retval reach Indicates if the target node is reached. 498 ///It should be initially \c false. 499 /// 492 500 ///\return The processed node. 493 501 /// 494 ///\ warning The queue must not be empty!502 ///\pre The queue must not be empty. 495 503 Node processNextNode(Node target, bool& reach) 496 504 { … … 515 523 ///Processes the next node. 516 524 517 ///Processes the next node. And checks that at least one of 518 ///reached node has true value in the \c nm node map. If one node 519 ///with true value is reachable from the processed node then the 520 ///rnode parameter will be set to the first of such nodes. 521 /// 522 ///\param nm The node map of possible targets. 525 ///Processes the next node and checks if at least one of reached 526 ///nodes has \c true value in the \c nm node map. If one node 527 ///with \c true value is reachable from the processed node, then the 528 ///\c rnode parameter will be set to the first of such nodes. 529 /// 530 ///\param nm A \c bool (or convertible) node map that indicates the 531 ///possible targets. 523 532 ///\retval rnode The reached target node. 533 ///It should be initially \c INVALID. 534 /// 524 535 ///\return The processed node. 525 536 /// 526 ///\ warning The queue must not be empty!537 ///\pre The queue must not be empty. 527 538 template<class NM> 528 539 Node processNextNode(const NM& nm, Node& rnode) … … 546 557 } 547 558 548 ///Next node to be processed. 549 550 ///Next node to be processed. 551 /// 552 ///\return The next node to be processed or INVALID if the queue is 553 /// empty. 554 Node nextNode() 559 ///The next node to be processed. 560 561 ///Returns the next node to be processed or \c INVALID if the queue 562 ///is empty. 563 Node nextNode() const 555 564 { 556 565 return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; … … 558 567 559 568 ///\brief Returns \c false if there are nodes 560 ///to be processed in the queue569 ///to be processed. 561 570 /// 562 571 ///Returns \c false if there are nodes 563 ///to be processed in the queue 564 bool emptyQueue() { return _queue_tail==_queue_head; } 572 ///to be processed in the queue. 573 bool emptyQueue() const { return _queue_tail==_queue_head; } 574 565 575 ///Returns the number of the nodes to be processed. 566 576 567 577 ///Returns the number of the nodes to be processed in the queue. 568 int queueSize() { return _queue_head_queue_tail; }578 int queueSize() const { return _queue_head_queue_tail; } 569 579 570 580 ///Executes the algorithm. … … 572 582 ///Executes the algorithm. 573 583 /// 574 ///\pre init() must be called and at least one node should be added575 ///with addSource() before using this function.576 ///577 584 ///This method runs the %BFS algorithm from the root node(s) 578 ///in order to 579 ///compute the 580 ///shortest path to each node. The algorithm computes 581 /// The shortest path tree. 582 /// The distance of each node from the root(s). 585 ///in order to compute the shortest path to each node. 586 /// 587 ///The algorithm computes 588 /// the shortest path tree (forest), 589 /// the distance of each node from the root(s). 590 /// 591 ///\pre init() must be called and at least one root node should be 592 ///added with addSource() before using this function. 593 /// 594 ///\note <tt>b.start()</tt> is just a shortcut of the following code. 595 ///\code 596 /// while ( !b.emptyQueue() ) { 597 /// b.processNextNode(); 598 /// } 599 ///\endcode 583 600 void start() 584 601 { … … 586 603 } 587 604 588 ///Executes the algorithm until \c dest is reached. 589 590 ///Executes the algorithm until \c dest is reached. 591 /// 592 ///\pre init() must be called and at least one node should be added 593 ///with addSource() before using this function. 605 ///Executes the algorithm until the given target node is reached. 606 607 ///Executes the algorithm until the given target node is reached. 594 608 /// 595 609 ///This method runs the %BFS algorithm from the root node(s) 596 610 ///in order to compute the shortest path to \c dest. 611 /// 597 612 ///The algorithm computes 598 /// The shortest path to \c dest. 599 /// The distance of \c dest from the root(s). 613 /// the shortest path to \c dest, 614 /// the distance of \c dest from the root(s). 615 /// 616 ///\pre init() must be called and at least one root node should be 617 ///added with addSource() before using this function. 618 /// 619 ///\note <tt>b.start(t)</tt> is just a shortcut of the following code. 620 ///\code 621 /// bool reach = false; 622 /// while ( !b.emptyQueue() && !reach ) { 623 /// b.processNextNode(t, reach); 624 /// } 625 ///\endcode 600 626 void start(Node dest) 601 627 { … … 608 634 ///Executes the algorithm until a condition is met. 609 635 /// 610 /// \pre init() must be called and at least one node should be added611 /// with addSource() before using this function.612 /// 613 /// \param nm must be a bool (or convertible) node map. The614 /// algorithm will stop when it reaches a node \c v with615 /// <tt>nm[v]</tt> true.636 ///This method runs the %BFS algorithm from the root node(s) in 637 ///order to compute the shortest path to a node \c v with 638 /// <tt>nm[v]</tt> true, if such a node can be found. 639 /// 640 ///\param nm A \c bool (or convertible) node map. The algorithm 641 ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. 616 642 /// 617 643 ///\return The reached node \c v with <tt>nm[v]</tt> true or 618 644 ///\c INVALID if no such node was found. 619 template<class NM> 620 Node start(const NM &nm) 645 /// 646 ///\pre init() must be called and at least one root node should be 647 ///added with addSource() before using this function. 648 /// 649 ///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. 650 ///\code 651 /// Node rnode = INVALID; 652 /// while ( !b.emptyQueue() && rnode == INVALID ) { 653 /// b.processNextNode(nm, rnode); 654 /// } 655 /// return rnode; 656 ///\endcode 657 template<class NodeBoolMap> 658 Node start(const NodeBoolMap &nm) 621 659 { 622 660 Node rnode = INVALID; … … 627 665 } 628 666 629 ///Runs %BFS algorithm from node \c s.630 631 ///This method runs the %BFS algorithm from a rootnode \c s632 ///in order to 633 /// compute the634 /// shortest path to each node.The algorithm computes635 /// The shortest path tree.636 /// The distance of each node from the root.637 /// 638 ///\note b.run(s)is just a shortcut of the following code.667 ///Runs the algorithm from the given node. 668 669 ///This method runs the %BFS algorithm from node \c s 670 ///in order to compute the shortest path to each node. 671 /// 672 ///The algorithm computes 673 /// the shortest path tree, 674 /// the distance of each node from the root. 675 /// 676 ///\note <tt>b.run(s)</tt> is just a shortcut of the following code. 639 677 ///\code 640 678 /// b.init(); … … 650 688 ///Finds the shortest path between \c s and \c t. 651 689 652 ///Finds the shortest path between \c s and \c t. 653 /// 654 ///\return The length of the shortest st path if there exists one, 655 ///0 otherwise. 656 ///\note Apart from the return value, b.run(s) is 657 ///just a shortcut of the following code. 690 ///This method runs the %BFS algorithm from node \c s 691 ///in order to compute the shortest path to \c t. 692 /// 693 ///\return The length of the shortest <tt>s</tt><tt>t</tt> path, 694 ///if \c t is reachable form \c s, \c 0 otherwise. 695 /// 696 ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a 697 ///shortcut of the following code. 658 698 ///\code 659 699 /// b.init(); … … 668 708 } 669 709 710 ///Runs the algorithm to visit all nodes in the digraph. 711 712 ///This method runs the %BFS algorithm in order to 713 ///compute the shortest path to each node. 714 /// 715 ///The algorithm computes 716 /// the shortest path tree (forest), 717 /// the distance of each node from the root(s). 718 /// 719 ///\note <tt>b.run(s)</tt> is just a shortcut of the following code. 720 ///\code 721 /// b.init(); 722 /// for (NodeIt n(gr); n != INVALID; ++n) { 723 /// if (!b.reached(n)) { 724 /// b.addSource(n); 725 /// b.start(); 726 /// } 727 /// } 728 ///\endcode 729 void run() { 730 init(); 731 for (NodeIt n(*G); n != INVALID; ++n) { 732 if (!reached(n)) { 733 addSource(n); 734 start(); 735 } 736 } 737 } 738 670 739 ///@} 671 740 … … 673 742 ///The result of the %BFS algorithm can be obtained using these 674 743 ///functions.\n 675 /// Before the use of these functions,676 /// either run() or start() must be calleb.744 ///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() 745 ///"start()" must be called before using them. 677 746 678 747 ///@{ 679 748 680 typedef PredMapPath<Digraph, PredMap> Path; 681 682 ///Gives back the shortest path. 683 684 ///Gives back the shortest path. 685 ///\pre The \c t should be reachable from the source. 686 Path path(Node t) 687 { 688 return Path(*G, *_pred, t); 689 } 749 ///The shortest path to a node. 750 751 ///Returns the shortest path to a node. 752 /// 753 ///\warning \c t should be reachable from the root(s). 754 /// 755 ///\pre Either \ref run() or \ref start() must be called before 756 ///using this function. 757 Path path(Node t) const { return Path(*G, *_pred, t); } 690 758 691 759 ///The distance of a node from the root(s). 692 760 693 761 ///Returns the distance of a node from the root(s). 694 ///\pre \ref run() must be called before using this function. 695 ///\warning If node \c v in unreachable from the root(s) the return value 696 ///of this function is undefined. 762 /// 763 ///\warning If node \c v is not reachable from the root(s), then 764 ///the return value of this function is undefined. 765 /// 766 ///\pre Either \ref run() or \ref start() must be called before 767 ///using this function. 697 768 int dist(Node v) const { return (*_dist)[v]; } 698 769 699 ///Returns the 'previous arc' of the shortest path tree. 700 701 ///For a node \c v it returns the 'previous arc' 702 ///of the shortest path tree, 703 ///i.e. it returns the last arc of a shortest path from the root(s) to \c 704 ///v. It is \ref INVALID 705 ///if \c v is unreachable from the root(s) or \c v is a root. The 706 ///shortest path tree used here is equal to the shortest path tree used in 707 ///\ref predNode(). 708 ///\pre Either \ref run() or \ref start() must be called before using 709 ///this function. 770 ///Returns the 'previous arc' of the shortest path tree for a node. 771 772 ///This function returns the 'previous arc' of the shortest path 773 ///tree for the node \c v, i.e. it returns the last arc of a 774 ///shortest path from the root(s) to \c v. It is \c INVALID if \c v 775 ///is not reachable from the root(s) or if \c v is a root. 776 /// 777 ///The shortest path tree used here is equal to the shortest path 778 ///tree used in \ref predNode(). 779 /// 780 ///\pre Either \ref run() or \ref start() must be called before 781 ///using this function. 710 782 Arc predArc(Node v) const { return (*_pred)[v];} 711 783 712 ///Returns the 'previous node' of the shortest path tree. 713 714 ///For a node \c v it returns the 'previous node' 715 ///of the shortest path tree, 716 ///i.e. it returns the last but one node from a shortest path from the 717 ///root(a) to \c /v. 718 ///It is INVALID if \c v is unreachable from the root(s) or 719 ///if \c v itself a root. 784 ///Returns the 'previous node' of the shortest path tree for a node. 785 786 ///This function returns the 'previous node' of the shortest path 787 ///tree for the node \c v, i.e. it returns the last but one node 788 ///from a shortest path from the root(s) to \c v. It is \c INVALID 789 ///if \c v is not reachable from the root(s) or if \c v is a root. 790 /// 720 791 ///The shortest path tree used here is equal to the shortest path 721 792 ///tree used in \ref predArc(). 793 /// 722 794 ///\pre Either \ref run() or \ref start() must be called before 723 795 ///using this function. … … 725 797 G>source((*_pred)[v]); } 726 798 727 ///Returns a reference to the NodeMap of distances. 728 729 ///Returns a reference to the NodeMap of distances. 730 ///\pre Either \ref run() or \ref init() must 731 ///be called before using this function. 799 ///\brief Returns a const reference to the node map that stores the 800 /// distances of the nodes. 801 /// 802 ///Returns a const reference to the node map that stores the distances 803 ///of the nodes calculated by the algorithm. 804 /// 805 ///\pre Either \ref run() or \ref init() 806 ///must be called before using this function. 732 807 const DistMap &distMap() const { return *_dist;} 733 808 734 ///Returns a reference to the shortest path tree map. 735 736 ///Returns a reference to the NodeMap of the arcs of the 737 ///shortest path tree. 809 ///\brief Returns a const reference to the node map that stores the 810 ///predecessor arcs. 811 /// 812 ///Returns a const reference to the node map that stores the predecessor 813 ///arcs, which form the shortest path tree. 814 /// 738 815 ///\pre Either \ref run() or \ref init() 739 816 ///must be called before using this function. 740 817 const PredMap &predMap() const { return *_pred;} 741 818 742 ///Checks if a node is reachable from the root. 743 744 ///Returns \c true if \c v is reachable from the root. 745 ///\warning The source nodes are indicated as unreached. 819 ///Checks if a node is reachable from the root(s). 820 821 ///Returns \c true if \c v is reachable from the root(s). 746 822 ///\pre Either \ref run() or \ref start() 747 823 ///must be called before using this function. 748 /// 749 bool reached(Node v) { return (*_reached)[v]; } 824 bool reached(Node v) const { return (*_reached)[v]; } 750 825 751 826 ///@} 752 827 }; 753 828 754 ///Default traits class of Bfsfunction.755 756 ///Default traits class of Bfsfunction.829 ///Default traits class of bfs() function. 830 831 ///Default traits class of bfs() function. 757 832 ///\tparam GR Digraph type. 758 833 template<class GR> 759 834 struct BfsWizardDefaultTraits 760 835 { 761 ///The digraph typethe algorithm runs on.836 ///The type of the digraph the algorithm runs on. 762 837 typedef GR Digraph; 763 ///\brief The type of the map that stores the last 838 839 ///\brief The type of the map that stores the predecessor 764 840 ///arcs of the shortest paths. 765 841 /// 766 ///The type of the map that stores the last842 ///The type of the map that stores the predecessor 767 843 ///arcs of the shortest paths. 768 844 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 769 /// 770 typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; 771 ///Instantiates a PredMap. 845 typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; 846 ///Instantiates a \ref PredMap. 772 847 773 848 ///This function instantiates a \ref PredMap. 774 ///\param g is the digraph, to which we would like to define the PredMap. 849 ///\param g is the digraph, to which we would like to define the 850 ///\ref PredMap. 775 851 ///\todo The digraph alone may be insufficient to initialize 776 852 #ifdef DOXYGEN 777 static PredMap *createPredMap(const GR&g)853 static PredMap *createPredMap(const Digraph &g) 778 854 #else 779 static PredMap *createPredMap(const GR&)855 static PredMap *createPredMap(const Digraph &) 780 856 #endif 781 857 { … … 787 863 ///The type of the map that indicates which nodes are processed. 788 864 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 789 ///\todo named parameter to set this type, function to read and write.790 865 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 791 ///Instantiates a ProcessedMap.866 ///Instantiates a \ref ProcessedMap. 792 867 793 868 ///This function instantiates a \ref ProcessedMap. 794 869 ///\param g is the digraph, to which 795 ///we would like to define the \ref ProcessedMap 870 ///we would like to define the \ref ProcessedMap. 796 871 #ifdef DOXYGEN 797 static ProcessedMap *createProcessedMap(const GR&g)872 static ProcessedMap *createProcessedMap(const Digraph &g) 798 873 #else 799 static ProcessedMap *createProcessedMap(const GR&)874 static ProcessedMap *createProcessedMap(const Digraph &) 800 875 #endif 801 876 { 802 877 return new ProcessedMap(); 803 878 } 879 804 880 ///The type of the map that indicates which nodes are reached. 805 881 806 882 ///The type of the map that indicates which nodes are reached. 883 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 884 typedef typename Digraph::template NodeMap<bool> ReachedMap; 885 ///Instantiates a \ref ReachedMap. 886 887 ///This function instantiates a \ref ReachedMap. 888 ///\param g is the digraph, to which 889 ///we would like to define the \ref ReachedMap. 890 static ReachedMap *createReachedMap(const Digraph &g) 891 { 892 return new ReachedMap(g); 893 } 894 895 ///The type of the map that stores the distances of the nodes. 896 897 ///The type of the map that stores the distances of the nodes. 807 898 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 808 ///\todo named parameter to set this type, function to read and write.809 typedef typename Digraph::template NodeMap<bool> ReachedMap;810 ///Instantiates a ReachedMap.811 812 ///This function instantiates a \ref ReachedMap.813 ///\param G is the digraph, to which814 ///we would like to define the \ref ReachedMap.815 static ReachedMap *createReachedMap(const GR &G)816 {817 return new ReachedMap(G);818 }819 ///The type of the map that stores the dists of the nodes.820 821 ///The type of the map that stores the dists of the nodes.822 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.823 899 /// 824 900 typedef NullMap<typename Digraph::Node,int> DistMap; 825 ///Instantiates a DistMap.901 ///Instantiates a \ref DistMap. 826 902 827 903 ///This function instantiates a \ref DistMap. … … 829 905 ///the \ref DistMap 830 906 #ifdef DOXYGEN 831 static DistMap *createDistMap(const GR&g)907 static DistMap *createDistMap(const Digraph &g) 832 908 #else 833 static DistMap *createDistMap(const GR&)909 static DistMap *createDistMap(const Digraph &) 834 910 #endif 835 911 { … … 838 914 }; 839 915 840 /// Default traits used by \ref BfsWizard916 /// Default traits class used by \ref BfsWizard 841 917 842 918 /// To make it easier to use Bfs algorithm 843 /// we have created a wizard class.919 /// we have created a wizard class. 844 920 /// This \ref BfsWizard class needs default traits, 845 /// as well as the \ref Bfs class.921 /// as well as the \ref Bfs class. 846 922 /// The \ref BfsWizardBase is a class to be the default traits of the 847 923 /// \ref BfsWizard class. … … 852 928 typedef BfsWizardDefaultTraits<GR> Base; 853 929 protected: 854 // / Type of the nodes in the digraph.930 //The type of the nodes in the digraph. 855 931 typedef typename Base::Digraph::Node Node; 856 932 857 // / Pointer to the underlying digraph.933 //Pointer to the digraph the algorithm runs on. 858 934 void *_g; 859 // /Pointer to the map of reached nodes.935 //Pointer to the map of reached nodes. 860 936 void *_reached; 861 // /Pointer to the map of processed nodes.937 //Pointer to the map of processed nodes. 862 938 void *_processed; 863 // /Pointer to the map of predecessors arcs.939 //Pointer to the map of predecessors arcs. 864 940 void *_pred; 865 // /Pointer to the map of distances.941 //Pointer to the map of distances. 866 942 void *_dist; 867 // /Pointer to the source node.943 //Pointer to the source node. 868 944 Node _source; 869 945 … … 874 950 /// all of the attributes to default values (0, INVALID). 875 951 BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), 876 952 _dist(0), _source(INVALID) {} 877 953 878 954 /// Constructor. … … 881 957 /// listed in the parameters list. 882 958 /// Others are initiated to 0. 883 /// \param g is the initial value of \ref _g884 /// \param s is the initial value of \ref _source959 /// \param g The digraph the algorithm runs on. 960 /// \param s The source node. 885 961 BfsWizardBase(const GR &g, Node s=INVALID) : 886 962 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), … … 889 965 }; 890 966 891 /// A class to make the usage of Bfs algorithm easier 892 893 /// This class is created to make it easier to use Bfs algorithm. 894 /// It uses the functions and features of the plain \ref Bfs, 895 /// but it is much simpler to use it. 967 /// Auxiliary class for the function type interface of BFS algorithm. 968 969 /// This auxiliary class is created to implement the function type 970 /// interface of \ref Bfs algorithm. It uses the functions and features 971 /// of the plain \ref Bfs, but it is much simpler to use it. 972 /// It should only be used through the \ref bfs() function, which makes 973 /// it easier to use the algorithm. 896 974 /// 897 975 /// Simplicity means that the way to change the types defined … … 902 980 /// the original class by using the :: 903 981 /// operator. In the case of \ref BfsWizard only 904 /// a function have to be called and it will982 /// a function have to be called, and it will 905 983 /// return the needed class. 906 984 /// 907 /// It does not have own \ref run method. When its \ref run method is called908 /// i t initiates a plain \ref Bfs class, and calls the \ref Bfs::run909 /// method of it.985 /// It does not have own \ref run() method. When its \ref run() method 986 /// is called, it initiates a plain \ref Bfs object, and calls the 987 /// \ref Bfs::run() method of it. 910 988 template<class TR> 911 989 class BfsWizard : public TR … … 913 991 typedef TR Base; 914 992 915 ///The type of the underlying digraph.993 ///The type of the digraph the algorithm runs on. 916 994 typedef typename TR::Digraph Digraph; 917 //\e 995 918 996 typedef typename Digraph::Node Node; 919 //\e920 997 typedef typename Digraph::NodeIt NodeIt; 921 //\e922 998 typedef typename Digraph::Arc Arc; 923 //\e924 999 typedef typename Digraph::OutArcIt OutArcIt; 925 1000 926 ///\brief The type of the map that stores 927 ///the reached nodes 928 typedef typename TR::ReachedMap ReachedMap; 929 ///\brief The type of the map that stores 930 ///the processed nodes 931 typedef typename TR::ProcessedMap ProcessedMap; 932 ///\brief The type of the map that stores the last 1001 ///\brief The type of the map that stores the predecessor 933 1002 ///arcs of the shortest paths. 934 1003 typedef typename TR::PredMap PredMap; 935 /// The type of the map that stores the dists of the nodes.1004 ///\brief The type of the map that stores the distances of the nodes. 936 1005 typedef typename TR::DistMap DistMap; 1006 ///\brief The type of the map that indicates which nodes are reached. 1007 typedef typename TR::ReachedMap ReachedMap; 1008 ///\brief The type of the map that indicates which nodes are processed. 1009 typedef typename TR::ProcessedMap ProcessedMap; 937 1010 938 1011 public: 1012 939 1013 /// Constructor. 940 1014 BfsWizard() : TR() {} … … 952 1026 ~BfsWizard() {} 953 1027 954 ///Runs B fs algorithm from a givennode.955 956 ///Runs B fs algorithm from a givennode.957 ///The node can be given by the \ref sourcefunction.1028 ///Runs BFS algorithm from a source node. 1029 1030 ///Runs BFS algorithm from a source node. 1031 ///The node can be given with the \ref source() function. 958 1032 void run() 959 1033 { … … 971 1045 } 972 1046 973 ///Runs B fsalgorithm from the given node.974 975 ///Runs B fsalgorithm from the given node.1047 ///Runs BFS algorithm from the given node. 1048 1049 ///Runs BFS algorithm from the given node. 976 1050 ///\param s is the given source. 977 1051 void run(Node s) … … 979 1053 Base::_source=s; 980 1054 run(); 1055 } 1056 1057 /// Sets the source node, from which the Bfs algorithm runs. 1058 1059 /// Sets the source node, from which the Bfs algorithm runs. 1060 /// \param s is the source node. 1061 BfsWizard<TR> &source(Node s) 1062 { 1063 Base::_source=s; 1064 return *this; 981 1065 } 982 1066 … … 987 1071 DefPredMapBase(const TR &b) : TR(b) {} 988 1072 }; 989 990 1073 ///\brief \ref namedtemplparam "Named parameter" 991 ///f unction for setting PredMap1074 ///for setting \ref PredMap object. 992 1075 /// 993 1076 /// \ref namedtemplparam "Named parameter" 994 ///function for setting PredMap 995 /// 1077 ///for setting \ref PredMap object. 996 1078 template<class T> 997 1079 BfsWizard<DefPredMapBase<T> > predMap(const T &t) … … 1000 1082 return BfsWizard<DefPredMapBase<T> >(*this); 1001 1083 } 1002 1003 1084 1004 1085 template<class T> … … 1008 1089 DefReachedMapBase(const TR &b) : TR(b) {} 1009 1090 }; 1010 1011 1091 ///\brief \ref namedtemplparam "Named parameter" 1012 ///f unction for setting ReachedMap1092 ///for setting \ref ReachedMap object. 1013 1093 /// 1014 1094 /// \ref namedtemplparam "Named parameter" 1015 ///function for setting ReachedMap 1016 /// 1095 ///for setting \ref ReachedMap object. 1017 1096 template<class T> 1018 1097 BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) … … 1021 1100 return BfsWizard<DefReachedMapBase<T> >(*this); 1022 1101 } 1023 1024 1102 1025 1103 template<class T> … … 1029 1107 DefProcessedMapBase(const TR &b) : TR(b) {} 1030 1108 }; 1031 1032 1109 ///\brief \ref namedtemplparam "Named parameter" 1033 ///f unction for setting ProcessedMap1110 ///for setting \ref ProcessedMap object. 1034 1111 /// 1035 1112 /// \ref namedtemplparam "Named parameter" 1036 ///function for setting ProcessedMap 1037 /// 1113 ///for setting \ref ProcessedMap object. 1038 1114 template<class T> 1039 1115 BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) … … 1042 1118 return BfsWizard<DefProcessedMapBase<T> >(*this); 1043 1119 } 1044 1045 1120 1046 1121 template<class T> … … 1050 1125 DefDistMapBase(const TR &b) : TR(b) {} 1051 1126 }; 1052 1053 1127 ///\brief \ref namedtemplparam "Named parameter" 1054 ///f unction for setting DistMap type1128 ///for setting \ref DistMap object. 1055 1129 /// 1056 1130 /// \ref namedtemplparam "Named parameter" 1057 ///function for setting DistMap type 1058 /// 1131 ///for setting \ref DistMap object. 1059 1132 template<class T> 1060 1133 BfsWizard<DefDistMapBase<T> > distMap(const T &t) … … 1062 1135 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1063 1136 return BfsWizard<DefDistMapBase<T> >(*this); 1064 }1065 1066 /// Sets the source node, from which the Bfs algorithm runs.1067 1068 /// Sets the source node, from which the Bfs algorithm runs.1069 /// \param s is the source node.1070 BfsWizard<TR> &source(Node s)1071 {1072 Base::_source=s;1073 return *this;1074 1137 } 1075 1138 … … 1101 1164 1102 1165 #ifdef DOXYGEN 1103 /// \brief Visitor class for bfs.1166 /// \brief Visitor class for BFS. 1104 1167 /// 1105 1168 /// This class defines the interface of the BfsVisit events, and 1106 /// it could be the base of a real Visitor class.1169 /// it could be the base of a real visitor class. 1107 1170 template <typename _Digraph> 1108 1171 struct BfsVisitor { … … 1110 1173 typedef typename Digraph::Arc Arc; 1111 1174 typedef typename Digraph::Node Node; 1112 /// \brief Called when the arc reach a node. 1113 /// 1114 /// It is called when the bfs find an arc which target is not 1115 /// reached yet. 1175 /// \brief Called for the source node(s) of the BFS. 1176 /// 1177 /// This function is called for the source node(s) of the BFS. 1178 void start(const Node& node) {} 1179 /// \brief Called when a node is reached first time. 1180 /// 1181 /// This function is called when a node is reached first time. 1182 void reach(const Node& node) {} 1183 /// \brief Called when a node is processed. 1184 /// 1185 /// This function is called when a node is processed. 1186 void process(const Node& node) {} 1187 /// \brief Called when an arc reaches a new node. 1188 /// 1189 /// This function is called when the BFS finds an arc whose target node 1190 /// is not reached yet. 1116 1191 void discover(const Arc& arc) {} 1117 /// \brief Called when the node reached first time. 1118 /// 1119 /// It is Called when the node reached first time. 1120 void reach(const Node& node) {} 1121 /// \brief Called when the arc examined but target of the arc 1192 /// \brief Called when an arc is examined but its target node is 1122 1193 /// already discovered. 1123 1194 /// 1124 /// It called when the arc examined but the target of the arc1195 /// This function is called when an arc is examined but its target node is 1125 1196 /// already discovered. 1126 1197 void examine(const Arc& arc) {} 1127 /// \brief Called for the source node of the bfs.1128 ///1129 /// It is called for the source node of the bfs.1130 void start(const Node& node) {}1131 /// \brief Called when the node processed.1132 ///1133 /// It is Called when the node processed.1134 void process(const Node& node) {}1135 1198 }; 1136 1199 #else … … 1140 1203 typedef typename Digraph::Arc Arc; 1141 1204 typedef typename Digraph::Node Node; 1205 void start(const Node&) {} 1206 void reach(const Node&) {} 1207 void process(const Node&) {} 1142 1208 void discover(const Arc&) {} 1143 void reach(const Node&) {}1144 1209 void examine(const Arc&) {} 1145 void start(const Node&) {}1146 void process(const Node&) {}1147 1210 1148 1211 template <typename _Visitor> … … 1151 1214 Arc arc; 1152 1215 Node node; 1216 visitor.start(node); 1217 visitor.reach(node); 1218 visitor.process(node); 1153 1219 visitor.discover(arc); 1154 visitor.reach(node);1155 1220 visitor.examine(arc); 1156 visitor.start(node);1157 visitor.process(node);1158 1221 } 1159 1222 _Visitor& visitor; … … 1165 1228 /// 1166 1229 /// Default traits class of BfsVisit class. 1167 /// \tparam _Digraph Digraph type.1230 /// \tparam _Digraph The type of the digraph the algorithm runs on. 1168 1231 template<class _Digraph> 1169 1232 struct BfsVisitDefaultTraits { 1170 1233 1171 /// \brief The digraph typethe algorithm runs on.1234 /// \brief The type of the digraph the algorithm runs on. 1172 1235 typedef _Digraph Digraph; 1173 1236 … … 1175 1238 /// 1176 1239 /// The type of the map that indicates which nodes are reached. 1177 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. 1178 /// \todo named parameter to set this type, function to read and write. 1240 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 1179 1241 typedef typename Digraph::template NodeMap<bool> ReachedMap; 1180 1242 1181 /// \brief Instantiates a ReachedMap.1243 /// \brief Instantiates a \ref ReachedMap. 1182 1244 /// 1183 1245 /// This function instantiates a \ref ReachedMap. … … 1192 1254 /// \ingroup search 1193 1255 /// 1194 /// \brief %BFS Visit algorithm class.1256 /// \brief %BFS algorithm class with visitor interface. 1195 1257 /// 1196 1258 /// This class provides an efficient implementation of the %BFS algorithm … … 1199 1261 /// The %BfsVisit class provides an alternative interface to the Bfs 1200 1262 /// class. It works with callback mechanism, the BfsVisit object calls 1201 /// on every bfs event the \c Visitor class member functions.1263 /// the member functions of the \c Visitor class on every BFS event. 1202 1264 /// 1203 /// \tparam _Digraph The digraph typethe algorithm runs on.1265 /// \tparam _Digraph The type of the digraph the algorithm runs on. 1204 1266 /// The default value is 1205 /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it1206 /// is only passed to \ref BfsDefaultTraits.1207 /// \tparam _Visitor The Visitor object for the algorithm. The1208 /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitorwhich1209 /// does not observe the B fs events. If you want to observe the bfs1210 /// events you should implement your own Visitor class.1267 /// \ref ListDigraph. The value of _Digraph is not used directly by 1268 /// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. 1269 /// \tparam _Visitor The Visitor type that is used by the algorithm. 1270 /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which 1271 /// does not observe the BFS events. If you want to observe the BFS 1272 /// events, you should implement your own visitor class. 1211 1273 /// \tparam _Traits Traits class to set various data types used by the 1212 1274 /// algorithm. The default traits class is 1213 1275 /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". 1214 1276 /// See \ref BfsVisitDefaultTraits for the documentation of 1215 /// a B fsvisit traits class.1277 /// a BFS visit traits class. 1216 1278 #ifdef DOXYGEN 1217 1279 template <typename _Digraph, typename _Visitor, typename _Traits> … … 1227 1289 /// 1228 1290 /// This error represents problems in the initialization 1229 /// of the parameters of the algorithm s.1291 /// of the parameters of the algorithm. 1230 1292 class UninitializedParameter : public lemon::UninitializedParameter { 1231 1293 public: … … 1236 1298 }; 1237 1299 1300 ///The traits class. 1238 1301 typedef _Traits Traits; 1239 1302 1303 ///The type of the digraph the algorithm runs on. 1240 1304 typedef typename Traits::Digraph Digraph; 1241 1305 1306 ///The visitor type used by the algorithm. 1242 1307 typedef _Visitor Visitor; 1243 1308 1244 ///The type of the map indicatingwhich nodes are reached.1309 ///The type of the map that indicates which nodes are reached. 1245 1310 typedef typename Traits::ReachedMap ReachedMap; 1246 1311 … … 1252 1317 typedef typename Digraph::OutArcIt OutArcIt; 1253 1318 1254 // /Pointer to the underlying digraph.1319 //Pointer to the underlying digraph. 1255 1320 const Digraph *_digraph; 1256 // /Pointer to the visitor object.1321 //Pointer to the visitor object. 1257 1322 Visitor *_visitor; 1258 // /Pointer to the map of reached status of the nodes.1323 //Pointer to the map of reached status of the nodes. 1259 1324 ReachedMap *_reached; 1260 // /Indicates if \ref _reached is locally allocated (\ctrue) or not.1325 //Indicates if _reached is locally allocated (true) or not. 1261 1326 bool local_reached; 1262 1327 … … 1264 1329 int _list_front, _list_back; 1265 1330 1266 /// \brief Creates the maps if necessary. 1267 /// 1268 /// Creates the maps if necessary. 1331 ///Creates the maps if necessary. 1332 ///\todo Better memory allocation (instead of new). 1269 1333 void create_maps() { 1270 1334 if(!_reached) { … … 1293 1357 }; 1294 1358 /// \brief \ref namedtemplparam "Named parameter" for setting 1295 /// ReachedMap type 1296 /// 1297 /// \ref namedtemplparam "Named parameter" for setting ReachedMap type 1359 /// ReachedMap type. 1360 /// 1361 /// \ref namedtemplparam "Named parameter" for setting ReachedMap type. 1298 1362 template <class T> 1299 1363 struct DefReachedMap : public BfsVisit< Digraph, Visitor, … … 1309 1373 /// Constructor. 1310 1374 /// 1311 /// \param digraph the digraph the algorithm will run on. 1312 /// \param visitor The visitor of the algorithm. 1313 /// 1375 /// \param digraph The digraph the algorithm runs on. 1376 /// \param visitor The visitor object of the algorithm. 1314 1377 BfsVisit(const Digraph& digraph, Visitor& visitor) 1315 1378 : _digraph(&digraph), _visitor(&visitor), … … 1317 1380 1318 1381 /// \brief Destructor. 1319 ///1320 /// Destructor.1321 1382 ~BfsVisit() { 1322 1383 if(local_reached) delete _reached; 1323 1384 } 1324 1385 1325 /// \brief Sets the map indicating if a node isreached.1326 /// 1327 /// Sets the map indicating if a node isreached.1386 /// \brief Sets the map that indicates which nodes are reached. 1387 /// 1388 /// Sets the map that indicates which nodes are reached. 1328 1389 /// If you don't use this function before calling \ref run(), 1329 /// it will allocate one. The dest uctor deallocates this1390 /// it will allocate one. The destructor deallocates this 1330 1391 /// automatically allocated map, of course. 1331 1392 /// \return <tt> (*this) </tt> … … 1340 1401 1341 1402 public: 1403 1342 1404 /// \name Execution control 1343 1405 /// The simplest way to execute the algorithm is to use 1344 /// one of the member functions called \c run(...). 1406 /// one of the member functions called \ref lemon::BfsVisit::run() 1407 /// "run()". 1345 1408 /// \n 1346 /// If you need more control on the execution, 1347 /// first you must call \ref init(), then you can adda source node1348 /// with \ref addSource().1349 /// Finally \ref start() will perform the actual path1350 /// computation.1409 /// If you need more control on the execution, first you must call 1410 /// \ref lemon::BfsVisit::init() "init()", then you can add several 1411 /// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". 1412 /// Finally \ref lemon::BfsVisit::start() "start()" will perform the 1413 /// actual path computation. 1351 1414 1352 1415 /// @{ 1416 1353 1417 /// \brief Initializes the internal data structures. 1354 1418 /// 1355 1419 /// Initializes the internal data structures. 1356 ///1357 1420 void init() { 1358 1421 create_maps(); … … 1382 1445 /// \return The processed node. 1383 1446 /// 1384 /// \pre The queue must not be empty !1447 /// \pre The queue must not be empty. 1385 1448 Node processNextNode() { 1386 1449 Node n = _list[++_list_front]; … … 1403 1466 /// \brief Processes the next node. 1404 1467 /// 1405 /// Processes the next node . And checks thatthe given target node1468 /// Processes the next node and checks if the given target node 1406 1469 /// is reached. If the target node is reachable from the processed 1407 /// node then the reached parameter will be set true. The reached 1408 /// parameter should be initially false. 1470 /// node, then the \c reach parameter will be set to \c true. 1409 1471 /// 1410 1472 /// \param target The target node. 1411 /// \retval reach Indicates that the target node is reached. 1473 /// \retval reach Indicates if the target node is reached. 1474 /// It should be initially \c false. 1475 /// 1412 1476 /// \return The processed node. 1413 1477 /// 1414 /// \ warning The queue must not be empty!1478 /// \pre The queue must not be empty. 1415 1479 Node processNextNode(Node target, bool& reach) { 1416 1480 Node n = _list[++_list_front]; … … 1434 1498 /// \brief Processes the next node. 1435 1499 /// 1436 /// Processes the next node. And checks that at least one of 1437 /// reached node has true value in the \c nm node map. If one node 1438 /// with true value is reachable from the processed node then the 1439 /// rnode parameter will be set to the first of such nodes. 1440 /// 1441 /// \param nm The node map of possible targets. 1500 /// Processes the next node and checks if at least one of reached 1501 /// nodes has \c true value in the \c nm node map. If one node 1502 /// with \c true value is reachable from the processed node, then the 1503 /// \c rnode parameter will be set to the first of such nodes. 1504 /// 1505 /// \param nm A \c bool (or convertible) node map that indicates the 1506 /// possible targets. 1442 1507 /// \retval rnode The reached target node. 1508 /// It should be initially \c INVALID. 1509 /// 1443 1510 /// \return The processed node. 1444 1511 /// 1445 /// \ warning The queue must not be empty!1512 /// \pre The queue must not be empty. 1446 1513 template <typename NM> 1447 1514 Node processNextNode(const NM& nm, Node& rnode) { … … 1464 1531 } 1465 1532 1466 /// \brief Next node to be processed. 1467 /// 1468 /// Next node to be processed. 1469 /// 1470 /// \return The next node to be processed or INVALID if the stack is 1471 /// empty. 1472 Node nextNode() { 1533 /// \brief The next node to be processed. 1534 /// 1535 /// Returns the next node to be processed or \c INVALID if the queue 1536 /// is empty. 1537 Node nextNode() const { 1473 1538 return _list_front != _list_back ? _list[_list_front + 1] : INVALID; 1474 1539 } 1475 1540 1476 1541 /// \brief Returns \c false if there are nodes 1477 /// to be processed in the queue1542 /// to be processed. 1478 1543 /// 1479 1544 /// Returns \c false if there are nodes 1480 /// to be processed in the queue 1481 bool emptyQueue() { return _list_front == _list_back; }1545 /// to be processed in the queue. 1546 bool emptyQueue() const { return _list_front == _list_back; } 1482 1547 1483 1548 /// \brief Returns the number of the nodes to be processed. 1484 1549 /// 1485 1550 /// Returns the number of the nodes to be processed in the queue. 1486 int queueSize() { return _list_back  _list_front; }1551 int queueSize() const { return _list_back  _list_front; } 1487 1552 1488 1553 /// \brief Executes the algorithm. … … 1490 1555 /// Executes the algorithm. 1491 1556 /// 1492 /// \pre init() must be called and at least one node should be added 1557 /// This method runs the %BFS algorithm from the root node(s) 1558 /// in order to compute the shortest path to each node. 1559 /// 1560 /// The algorithm computes 1561 ///  the shortest path tree (forest), 1562 ///  the distance of each node from the root(s). 1563 /// 1564 /// \pre init() must be called and at least one root node should be added 1493 1565 /// with addSource() before using this function. 1566 /// 1567 /// \note <tt>b.start()</tt> is just a shortcut of the following code. 1568 /// \code 1569 /// while ( !b.emptyQueue() ) { 1570 /// b.processNextNode(); 1571 /// } 1572 /// \endcode 1494 1573 void start() { 1495 1574 while ( !emptyQueue() ) processNextNode(); 1496 1575 } 1497 1576 1498 /// \brief Executes the algorithm until \c dest is reached. 1499 /// 1500 /// Executes the algorithm until \c dest is reached. 1501 /// 1502 /// \pre init() must be called and at least one node should be added 1503 /// with addSource() before using this function. 1577 /// \brief Executes the algorithm until the given target node is reached. 1578 /// 1579 /// Executes the algorithm until the given target node is reached. 1580 /// 1581 /// This method runs the %BFS algorithm from the root node(s) 1582 /// in order to compute the shortest path to \c dest. 1583 /// 1584 /// The algorithm computes 1585 ///  the shortest path to \c dest, 1586 ///  the distance of \c dest from the root(s). 1587 /// 1588 /// \pre init() must be called and at least one root node should be 1589 /// added with addSource() before using this function. 1590 /// 1591 /// \note <tt>b.start(t)</tt> is just a shortcut of the following code. 1592 /// \code 1593 /// bool reach = false; 1594 /// while ( !b.emptyQueue() && !reach ) { 1595 /// b.processNextNode(t, reach); 1596 /// } 1597 /// \endcode 1504 1598 void start(Node dest) { 1505 1599 bool reach = false; … … 1511 1605 /// Executes the algorithm until a condition is met. 1512 1606 /// 1513 /// \pre init() must be called and at least one node should be added 1514 /// with addSource() before using this function. 1515 /// 1516 ///\param nm must be a bool (or convertible) node map. The 1517 ///algorithm will stop when it reaches a node \c v with 1607 /// This method runs the %BFS algorithm from the root node(s) in 1608 /// order to compute the shortest path to a node \c v with 1609 /// <tt>nm[v]</tt> true, if such a node can be found. 1610 /// 1611 /// \param nm must be a bool (or convertible) node map. The 1612 /// algorithm will stop when it reaches a node \c v with 1518 1613 /// <tt>nm[v]</tt> true. 1519 1614 /// 1520 ///\return The reached node \c v with <tt>nm[v]</tt> true or 1521 ///\c INVALID if no such node was found. 1615 /// \return The reached node \c v with <tt>nm[v]</tt> true or 1616 /// \c INVALID if no such node was found. 1617 /// 1618 /// \pre init() must be called and at least one root node should be 1619 /// added with addSource() before using this function. 1620 /// 1621 /// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. 1622 /// \code 1623 /// Node rnode = INVALID; 1624 /// while ( !b.emptyQueue() && rnode == INVALID ) { 1625 /// b.processNextNode(nm, rnode); 1626 /// } 1627 /// return rnode; 1628 /// \endcode 1522 1629 template <typename NM> 1523 1630 Node start(const NM &nm) { … … 1529 1636 } 1530 1637 1531 /// \brief Runs %BFSVisit algorithm from node \c s. 1532 /// 1533 /// This method runs the %BFS algorithm from a root node \c s. 1534 /// \note b.run(s) is just a shortcut of the following code. 1638 /// \brief Runs the algorithm from the given node. 1639 /// 1640 /// This method runs the %BFS algorithm from node \c s 1641 /// in order to compute the shortest path to each node. 1642 /// 1643 /// The algorithm computes 1644 ///  the shortest path tree, 1645 ///  the distance of each node from the root. 1646 /// 1647 /// \note <tt>b.run(s)</tt> is just a shortcut of the following code. 1535 1648 ///\code 1536 1649 /// b.init(); … … 1544 1657 } 1545 1658 1546 /// \brief Runs %BFSVisitalgorithm to visit all nodes in the digraph.1659 /// \brief Runs the algorithm to visit all nodes in the digraph. 1547 1660 /// 1548 1661 /// This method runs the %BFS algorithm in order to 1549 /// compute the %BFS path to each node. The algorithm computes 1550 ///  The %BFS tree. 1551 ///  The distance of each node from the root in the %BFS tree. 1552 /// 1553 ///\note b.run() is just a shortcut of the following code. 1662 /// compute the shortest path to each node. 1663 /// 1664 /// The algorithm computes 1665 ///  the shortest path tree (forest), 1666 ///  the distance of each node from the root(s). 1667 /// 1668 /// \note <tt>b.run(s)</tt> is just a shortcut of the following code. 1554 1669 ///\code 1555 1670 /// b.init(); 1556 /// for (NodeIt it(digraph); it != INVALID; ++it) {1557 /// if (!b.reached( it)) {1558 /// b.addSource( it);1671 /// for (NodeIt n(gr); n != INVALID; ++n) { 1672 /// if (!b.reached(n)) { 1673 /// b.addSource(n); 1559 1674 /// b.start(); 1560 1675 /// } … … 1570 1685 } 1571 1686 } 1687 1572 1688 ///@} 1573 1689 … … 1575 1691 /// The result of the %BFS algorithm can be obtained using these 1576 1692 /// functions.\n 1577 /// Before the use of these functions, 1578 /// either run() or start() must be called. 1693 /// Either \ref lemon::BfsVisit::run() "run()" or 1694 /// \ref lemon::BfsVisit::start() "start()" must be called before 1695 /// using them. 1579 1696 ///@{ 1580 1697 1581 /// \brief Checks if a node is reachable from the root .1698 /// \brief Checks if a node is reachable from the root(s). 1582 1699 /// 1583 1700 /// Returns \c true if \c v is reachable from the root(s). 1584 /// \warning The source nodes are inditated as unreachable.1585 1701 /// \pre Either \ref run() or \ref start() 1586 1702 /// must be called before using this function. 1587 ///1588 1703 bool reached(Node v) { return (*_reached)[v]; } 1704 1589 1705 ///@} 1706 1590 1707 }; 1591 1708 … … 1593 1710 1594 1711 #endif 1595
Note: See TracChangeset
for help on using the changeset viewer.