Modified a bit.
2 * src/lemon/bfs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
22 ///\brief Bfs algorithm.
24 #include <lemon/list_graph.h>
25 #include <lemon/graph_utils.h>
26 #include <lemon/invalid.h>
27 #include <lemon/error.h>
28 #include <lemon/maps.h>
34 ///Default traits class of Bfs class.
36 ///Default traits class of Bfs class.
37 ///\param GR Graph type.
39 struct BfsDefaultTraits
41 ///The graph type the algorithm runs on.
43 ///\brief The type of the map that stores the last
44 ///edges of the shortest paths.
46 ///The type of the map that stores the last
47 ///edges of the shortest paths.
48 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
50 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
51 ///Instantiates a PredMap.
53 ///This function instantiates a \ref PredMap.
54 ///\param G is the graph, to which we would like to define the PredMap.
55 ///\todo The graph alone may be insufficient to initialize
56 static PredMap *createPredMap(const GR &G)
58 return new PredMap(G);
60 // ///\brief The type of the map that stores the last but one
61 // ///nodes of the shortest paths.
63 // ///The type of the map that stores the last but one
64 // ///nodes of the shortest paths.
65 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
67 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
68 // ///Instantiates a PredNodeMap.
70 // ///This function instantiates a \ref PredNodeMap.
71 // ///\param G is the graph, to which
72 // ///we would like to define the \ref PredNodeMap
73 // static PredNodeMap *createPredNodeMap(const GR &G)
75 // return new PredNodeMap();
78 ///The type of the map that indicates which nodes are processed.
80 ///The type of the map that indicates which nodes are processed.
81 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
82 ///\todo named parameter to set this type, function to read and write.
83 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
84 ///Instantiates a ProcessedMap.
86 ///This function instantiates a \ref ProcessedMap.
87 ///\param G is the graph, to which
88 ///we would like to define the \ref ProcessedMap
89 static ProcessedMap *createProcessedMap(const GR &G)
91 return new ProcessedMap();
93 ///The type of the map that indicates which nodes are reached.
95 ///The type of the map that indicates which nodes are reached.
96 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
97 ///\todo named parameter to set this type, function to read and write.
98 typedef typename Graph::template NodeMap<bool> ReachedMap;
99 ///Instantiates a ReachedMap.
101 ///This function instantiates a \ref ReachedMap.
102 ///\param G is the graph, to which
103 ///we would like to define the \ref ReachedMap.
104 static ReachedMap *createReachedMap(const GR &G)
106 return new ReachedMap(G);
108 ///The type of the map that stores the dists of the nodes.
110 ///The type of the map that stores the dists of the nodes.
111 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
113 typedef typename Graph::template NodeMap<int> DistMap;
114 ///Instantiates a DistMap.
116 ///This function instantiates a \ref DistMap.
117 ///\param G is the graph, to which we would like to define the \ref DistMap
118 static DistMap *createDistMap(const GR &G)
120 return new DistMap(G);
124 ///%BFS algorithm class.
127 ///This class provides an efficient implementation of the %BFS algorithm.
129 ///\param GR The graph type the algorithm runs on. The default value is
130 ///\ref ListGraph. The value of GR is not used directly by Bfs, it
131 ///is only passed to \ref BfsDefaultTraits.
132 ///\param TR Traits class to set various data types used by the algorithm.
133 ///The default traits class is
134 ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
135 ///See \ref BfsDefaultTraits for the documentation of
136 ///a Bfs traits class.
138 ///\author Jacint Szabo and Alpar Juttner
139 ///\todo A compare object would be nice.
142 template <typename GR,
145 template <typename GR=ListGraph,
146 typename TR=BfsDefaultTraits<GR> >
151 * \brief \ref Exception for uninitialized parameters.
153 * This error represents problems in the initialization
154 * of the parameters of the algorithms.
156 class UninitializedParameter : public lemon::UninitializedParameter {
158 virtual const char* exceptionName() const {
159 return "lemon::Bfs::UninitializedParameter";
164 ///The type of the underlying graph.
165 typedef typename TR::Graph Graph;
167 typedef typename Graph::Node Node;
169 typedef typename Graph::NodeIt NodeIt;
171 typedef typename Graph::Edge Edge;
173 typedef typename Graph::OutEdgeIt OutEdgeIt;
175 ///\brief The type of the map that stores the last
176 ///edges of the shortest paths.
177 typedef typename TR::PredMap PredMap;
178 // ///\brief The type of the map that stores the last but one
179 // ///nodes of the shortest paths.
180 // typedef typename TR::PredNodeMap PredNodeMap;
181 ///The type of the map indicating which nodes are reached.
182 typedef typename TR::ReachedMap ReachedMap;
183 ///The type of the map indicating which nodes are processed.
184 typedef typename TR::ProcessedMap ProcessedMap;
185 ///The type of the map that stores the dists of the nodes.
186 typedef typename TR::DistMap DistMap;
188 /// Pointer to the underlying graph.
190 ///Pointer to the map of predecessors edges.
192 ///Indicates if \ref _pred is locally allocated (\c true) or not.
194 // ///Pointer to the map of predecessors nodes.
195 // PredNodeMap *_predNode;
196 // ///Indicates if \ref _predNode is locally allocated (\c true) or not.
197 // bool local_predNode;
198 ///Pointer to the map of distances.
200 ///Indicates if \ref _dist is locally allocated (\c true) or not.
202 ///Pointer to the map of reached status of the nodes.
203 ReachedMap *_reached;
204 ///Indicates if \ref _reached is locally allocated (\c true) or not.
206 ///Pointer to the map of processed status of the nodes.
207 ProcessedMap *_processed;
208 ///Indicates if \ref _processed is locally allocated (\c true) or not.
209 bool local_processed;
211 std::vector<typename Graph::Node> _queue;
212 int _queue_head,_queue_tail,_queue_next_dist;
214 // ///The source node of the last execution.
217 ///Creates the maps if necessary.
219 ///\todo Error if \c G are \c NULL.
220 ///\todo Better memory allocation (instead of new).
225 _pred = Traits::createPredMap(*G);
228 // local_predNode = true;
229 // _predNode = Traits::createPredNodeMap(*G);
233 _dist = Traits::createDistMap(*G);
236 local_reached = true;
237 _reached = Traits::createReachedMap(*G);
240 local_processed = true;
241 _processed = Traits::createProcessedMap(*G);
247 ///\name Named template parameters
252 struct DefPredMapTraits : public Traits {
254 static PredMap *createPredMap(const Graph &G)
256 throw UninitializedParameter();
259 ///\ref named-templ-param "Named parameter" for setting PredMap type
261 ///\ref named-templ-param "Named parameter" for setting PredMap type
264 class DefPredMap : public Bfs< Graph,
265 DefPredMapTraits<T> > { };
267 // template <class T>
268 // struct DefPredNodeMapTraits : public Traits {
269 // typedef T PredNodeMap;
270 // static PredNodeMap *createPredNodeMap(const Graph &G)
272 // throw UninitializedParameter();
275 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
277 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
279 // template <class T>
280 // class DefPredNodeMap : public Bfs< Graph,
282 // DefPredNodeMapTraits<T> > { };
285 struct DefDistMapTraits : public Traits {
287 static DistMap *createDistMap(const Graph &G)
289 throw UninitializedParameter();
292 ///\ref named-templ-param "Named parameter" for setting DistMap type
294 ///\ref named-templ-param "Named parameter" for setting DistMap type
297 class DefDistMap : public Bfs< Graph,
298 DefDistMapTraits<T> > { };
301 struct DefReachedMapTraits : public Traits {
302 typedef T ReachedMap;
303 static ReachedMap *createReachedMap(const Graph &G)
305 throw UninitializedParameter();
308 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
310 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
313 class DefReachedMap : public Bfs< Graph,
314 DefReachedMapTraits<T> > { };
316 struct DefGraphReachedMapTraits : public Traits {
317 typedef typename Graph::template NodeMap<bool> ReachedMap;
318 static ReachedMap *createReachedMap(const Graph &G)
320 return new ReachedMap(G);
324 struct DefProcessedMapTraits : public Traits {
325 typedef T ProcessedMap;
326 static ProcessedMap *createProcessedMap(const Graph &G)
328 throw UninitializedParameter();
331 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
333 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
336 class DefProcessedMap : public Bfs< Graph,
337 DefProcessedMapTraits<T> > { };
339 struct DefGraphProcessedMapTraits : public Traits {
340 typedef typename Graph::template NodeMap<bool> ProcessedMap;
341 static ProcessedMap *createProcessedMap(const Graph &G)
343 return new ProcessedMap(G);
346 ///\brief \ref named-templ-param "Named parameter"
347 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
349 ///\ref named-templ-param "Named parameter"
350 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
351 ///If you don't set it explicitely, it will be automatically allocated.
353 class DefProcessedMapToBeDefaultMap :
355 DefGraphProcessedMapTraits> { };
363 ///\param _G the graph the algorithm will run on.
365 Bfs(const Graph& _G) :
367 _pred(NULL), local_pred(false),
368 // _predNode(NULL), local_predNode(false),
369 _dist(NULL), local_dist(false),
370 _reached(NULL), local_reached(false),
371 _processed(NULL), local_processed(false)
377 if(local_pred) delete _pred;
378 // if(local_predNode) delete _predNode;
379 if(local_dist) delete _dist;
380 if(local_reached) delete _reached;
381 if(local_processed) delete _processed;
384 ///Sets the map storing the predecessor edges.
386 ///Sets the map storing the predecessor edges.
387 ///If you don't use this function before calling \ref run(),
388 ///it will allocate one. The destuctor deallocates this
389 ///automatically allocated map, of course.
390 ///\return <tt> (*this) </tt>
391 Bfs &predMap(PredMap &m)
401 ///Sets the map indicating the reached nodes.
403 ///Sets the map indicating the reached nodes.
404 ///If you don't use this function before calling \ref run(),
405 ///it will allocate one. The destuctor deallocates this
406 ///automatically allocated map, of course.
407 ///\return <tt> (*this) </tt>
408 Bfs &reachedMap(ReachedMap &m)
418 ///Sets the map indicating the processed nodes.
420 ///Sets the map indicating the processed nodes.
421 ///If you don't use this function before calling \ref run(),
422 ///it will allocate one. The destuctor deallocates this
423 ///automatically allocated map, of course.
424 ///\return <tt> (*this) </tt>
425 Bfs &processedMap(ProcessedMap &m)
427 if(local_processed) {
429 local_processed=false;
435 // ///Sets the map storing the predecessor nodes.
437 // ///Sets the map storing the predecessor nodes.
438 // ///If you don't use this function before calling \ref run(),
439 // ///it will allocate one. The destuctor deallocates this
440 // ///automatically allocated map, of course.
441 // ///\return <tt> (*this) </tt>
442 // Bfs &predNodeMap(PredNodeMap &m)
444 // if(local_predNode) {
446 // local_predNode=false;
452 ///Sets the map storing the distances calculated by the algorithm.
454 ///Sets the map storing the distances calculated by the algorithm.
455 ///If you don't use this function before calling \ref run(),
456 ///it will allocate one. The destuctor deallocates this
457 ///automatically allocated map, of course.
458 ///\return <tt> (*this) </tt>
459 Bfs &distMap(DistMap &m)
470 ///\name Execution control
471 ///The simplest way to execute the algorithm is to use
472 ///one of the member functions called \c run(...).
474 ///If you need more control on the execution,
475 ///first you must call \ref init(), then you can add several source nodes
476 ///with \ref addSource().
477 ///Finally \ref start() will perform the actual path
482 ///Initializes the internal data structures.
484 ///Initializes the internal data structures.
489 _queue.resize(countNodes(*G));
490 _queue_head=_queue_tail=0;
492 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
493 _pred->set(u,INVALID);
494 // _predNode->set(u,INVALID);
495 _reached->set(u,false);
496 _processed->set(u,false);
500 ///Adds a new source node.
502 ///Adds a new source node to the set of nodes to be processed.
504 void addSource(Node s)
508 _reached->set(s,true);
509 _pred->set(s,INVALID);
511 _queue[_queue_head++]=s;
512 _queue_next_dist=_queue_head;
516 ///Processes the next node.
518 ///Processes the next node.
520 ///\warning The queue must not be empty!
521 void processNextNode()
523 if(_queue_tail==_queue_next_dist) {
525 _queue_next_dist=_queue_head;
527 Node n=_queue[_queue_tail++];
528 _processed->set(n,true);
530 for(OutEdgeIt e(*G,n);e!=INVALID;++e)
531 if(!(*_reached)[m=G->target(e)]) {
532 _queue[_queue_head++]=m;
533 _reached->set(m,true);
535 // _pred_node->set(m,n);
536 _dist->set(m,_curr_dist);
540 ///\brief Returns \c false if there are nodes
541 ///to be processed in the queue
543 ///Returns \c false if there are nodes
544 ///to be processed in the queue
545 bool emptyQueue() { return _queue_tail==_queue_head; }
546 ///Returns the number of the nodes to be processed.
548 ///Returns the number of the nodes to be processed in the queue.
550 int queueSize() { return _queue_head-_queue_tail; }
552 ///Executes the algorithm.
554 ///Executes the algorithm.
556 ///\pre init() must be called and at least one node should be added
557 ///with addSource() before using this function.
559 ///This method runs the %BFS algorithm from the root node(s)
562 ///shortest path to each node. The algorithm computes
563 ///- The shortest path tree.
564 ///- The distance of each node from the root(s).
568 while ( !emptyQueue() ) processNextNode();
571 ///Executes the algorithm until \c dest is reached.
573 ///Executes the algorithm until \c dest is reached.
575 ///\pre init() must be called and at least one node should be added
576 ///with addSource() before using this function.
578 ///This method runs the %BFS algorithm from the root node(s)
581 ///shortest path to \c dest. The algorithm computes
582 ///- The shortest path to \c dest.
583 ///- The distance of \c dest from the root(s).
585 void start(Node dest)
587 while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode();
590 ///Executes the algorithm until a condition is met.
592 ///Executes the algorithm until a condition is met.
594 ///\pre init() must be called and at least one node should be added
595 ///with addSource() before using this function.
597 ///\param nm must be a bool (or convertible) node map. The algorithm
598 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
600 void start(const NM &nm)
602 while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode();
605 ///Runs %BFS algorithm from node \c s.
607 ///This method runs the %BFS algorithm from a root node \c s
610 ///shortest path to each node. The algorithm computes
611 ///- The shortest path tree.
612 ///- The distance of each node from the root.
614 ///\note d.run(s) is just a shortcut of the following code.
626 ///Finds the shortest path between \c s and \c t.
628 ///Finds the shortest path between \c s and \c t.
630 ///\return The length of the shortest s---t path if there exists one,
632 ///\note Apart from the return value, d.run(s) is
633 ///just a shortcut of the following code.
639 int run(Node s,Node t) {
643 return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0;
648 ///\name Query Functions
649 ///The result of the %BFS algorithm can be obtained using these
651 ///Before the use of these functions,
652 ///either run() or start() must be called.
656 ///The distance of a node from the root(s).
658 ///Returns the distance of a node from the root(s).
659 ///\pre \ref run() must be called before using this function.
660 ///\warning If node \c v in unreachable from the root(s) the return value
661 ///of this funcion is undefined.
662 int dist(Node v) const { return (*_dist)[v]; }
664 ///Returns the 'previous edge' of the shortest path tree.
666 ///For a node \c v it returns the 'previous edge'
667 ///of the shortest path tree,
668 ///i.e. it returns the last edge of a shortest path from the root(s) to \c
669 ///v. It is \ref INVALID
670 ///if \c v is unreachable from the root(s) or \c v is a root. The
671 ///shortest path tree used here is equal to the shortest path tree used in
672 ///\ref predNode(Node v).
673 ///\pre Either \ref run() or \ref start() must be called before using
675 ///\todo predEdge could be a better name.
676 Edge pred(Node v) const { return (*_pred)[v];}
678 ///Returns the 'previous node' of the shortest path tree.
680 ///For a node \c v it returns the 'previous node'
681 ///of the shortest path tree,
682 ///i.e. it returns the last but one node from a shortest path from the
684 ///It is INVALID if \c v is unreachable from the root(s) or
685 ///if \c v itself a root.
686 ///The shortest path tree used here is equal to the shortest path
687 ///tree used in \ref pred(Node v).
688 ///\pre Either \ref run() or \ref start() must be called before
689 ///using this function.
690 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
691 G->source((*_pred)[v]); }
693 ///Returns a reference to the NodeMap of distances.
695 ///Returns a reference to the NodeMap of distances.
696 ///\pre Either \ref run() or \ref init() must
697 ///be called before using this function.
698 const DistMap &distMap() const { return *_dist;}
700 ///Returns a reference to the shortest path tree map.
702 ///Returns a reference to the NodeMap of the edges of the
703 ///shortest path tree.
704 ///\pre Either \ref run() or \ref init()
705 ///must be called before using this function.
706 const PredMap &predMap() const { return *_pred;}
708 // ///Returns a reference to the map of nodes of shortest paths.
710 // ///Returns a reference to the NodeMap of the last but one nodes of the
711 // ///shortest path tree.
712 // ///\pre \ref run() must be called before using this function.
713 // const PredNodeMap &predNodeMap() const { return *_predNode;}
715 ///Checks if a node is reachable from the root.
717 ///Returns \c true if \c v is reachable from the root.
718 ///\warning The source nodes are inditated as unreached.
719 ///\pre Either \ref run() or \ref start()
720 ///must be called before using this function.
722 bool reached(Node v) { return (*_reached)[v]; }
727 ///Default traits class of Bfs function.
729 ///Default traits class of Bfs function.
730 ///\param GR Graph type.
732 struct BfsWizardDefaultTraits
734 ///The graph type the algorithm runs on.
736 ///\brief The type of the map that stores the last
737 ///edges of the shortest paths.
739 ///The type of the map that stores the last
740 ///edges of the shortest paths.
741 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
743 typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
744 ///Instantiates a PredMap.
746 ///This function instantiates a \ref PredMap.
747 ///\param G is the graph, to which we would like to define the PredMap.
748 ///\todo The graph alone may be insufficient to initialize
749 static PredMap *createPredMap(const GR &G)
751 return new PredMap();
753 // ///\brief The type of the map that stores the last but one
754 // ///nodes of the shortest paths.
756 // ///The type of the map that stores the last but one
757 // ///nodes of the shortest paths.
758 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
760 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
761 // ///Instantiates a PredNodeMap.
763 // ///This function instantiates a \ref PredNodeMap.
764 // ///\param G is the graph, to which
765 // ///we would like to define the \ref PredNodeMap
766 // static PredNodeMap *createPredNodeMap(const GR &G)
768 // return new PredNodeMap();
771 ///The type of the map that indicates which nodes are processed.
773 ///The type of the map that indicates which nodes are processed.
774 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
775 ///\todo named parameter to set this type, function to read and write.
776 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
777 ///Instantiates a ProcessedMap.
779 ///This function instantiates a \ref ProcessedMap.
780 ///\param G is the graph, to which
781 ///we would like to define the \ref ProcessedMap
782 static ProcessedMap *createProcessedMap(const GR &G)
784 return new ProcessedMap();
786 ///The type of the map that indicates which nodes are reached.
788 ///The type of the map that indicates which nodes are reached.
789 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
790 ///\todo named parameter to set this type, function to read and write.
791 typedef typename Graph::template NodeMap<bool> ReachedMap;
792 ///Instantiates a ReachedMap.
794 ///This function instantiates a \ref ReachedMap.
795 ///\param G is the graph, to which
796 ///we would like to define the \ref ReachedMap.
797 static ReachedMap *createReachedMap(const GR &G)
799 return new ReachedMap(G);
801 ///The type of the map that stores the dists of the nodes.
803 ///The type of the map that stores the dists of the nodes.
804 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
806 typedef NullMap<typename Graph::Node,int> DistMap;
807 ///Instantiates a DistMap.
809 ///This function instantiates a \ref DistMap.
810 ///\param G is the graph, to which we would like to define the \ref DistMap
811 static DistMap *createDistMap(const GR &G)
813 return new DistMap();
817 /// Default traits used by \ref BfsWizard
819 /// To make it easier to use Bfs algorithm
820 ///we have created a wizard class.
821 /// This \ref BfsWizard class needs default traits,
822 ///as well as the \ref Bfs class.
823 /// The \ref BfsWizardBase is a class to be the default traits of the
824 /// \ref BfsWizard class.
826 class BfsWizardBase : public BfsWizardDefaultTraits<GR>
829 typedef BfsWizardDefaultTraits<GR> Base;
831 /// Type of the nodes in the graph.
832 typedef typename Base::Graph::Node Node;
834 /// Pointer to the underlying graph.
836 ///Pointer to the map of reached nodes.
838 ///Pointer to the map of processed nodes.
840 ///Pointer to the map of predecessors edges.
842 // ///Pointer to the map of predecessors nodes.
844 ///Pointer to the map of distances.
846 ///Pointer to the source node.
852 /// This constructor does not require parameters, therefore it initiates
853 /// all of the attributes to default values (0, INVALID).
854 BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
856 _dist(0), _source(INVALID) {}
860 /// This constructor requires some parameters,
861 /// listed in the parameters list.
862 /// Others are initiated to 0.
863 /// \param g is the initial value of \ref _g
864 /// \param s is the initial value of \ref _source
865 BfsWizardBase(const GR &g, Node s=INVALID) :
866 _g((void *)&g), _reached(0), _processed(0), _pred(0),
868 _dist(0), _source(s) {}
872 /// A class to make the usage of Bfs algorithm easier
874 /// This class is created to make it easier to use Bfs algorithm.
875 /// It uses the functions and features of the plain \ref Bfs,
876 /// but it is much simpler to use it.
878 /// Simplicity means that the way to change the types defined
879 /// in the traits class is based on functions that returns the new class
880 /// and not on templatable built-in classes.
881 /// When using the plain \ref Bfs
882 /// the new class with the modified type comes from
883 /// the original class by using the ::
884 /// operator. In the case of \ref BfsWizard only
885 /// a function have to be called and it will
886 /// return the needed class.
888 /// It does not have own \ref run method. When its \ref run method is called
889 /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
892 class BfsWizard : public TR
896 ///The type of the underlying graph.
897 typedef typename TR::Graph Graph;
899 typedef typename Graph::Node Node;
901 typedef typename Graph::NodeIt NodeIt;
903 typedef typename Graph::Edge Edge;
905 typedef typename Graph::OutEdgeIt OutEdgeIt;
907 ///\brief The type of the map that stores
909 typedef typename TR::ReachedMap ReachedMap;
910 ///\brief The type of the map that stores
911 ///the processed nodes
912 typedef typename TR::ProcessedMap ProcessedMap;
913 ///\brief The type of the map that stores the last
914 ///edges of the shortest paths.
915 typedef typename TR::PredMap PredMap;
916 // ///\brief The type of the map that stores the last but one
917 // ///nodes of the shortest paths.
918 // typedef typename TR::PredNodeMap PredNodeMap;
919 ///The type of the map that stores the dists of the nodes.
920 typedef typename TR::DistMap DistMap;
924 BfsWizard() : TR() {}
926 /// Constructor that requires parameters.
928 /// Constructor that requires parameters.
929 /// These parameters will be the default values for the traits class.
930 BfsWizard(const Graph &g, Node s=INVALID) :
934 BfsWizard(const TR &b) : TR(b) {}
938 ///Runs Bfs algorithm from a given node.
940 ///Runs Bfs algorithm from a given node.
941 ///The node can be given by the \ref source function.
944 if(Base::_source==INVALID) throw UninitializedParameter();
945 Bfs<Graph,TR> alg(*(Graph*)Base::_g);
947 alg.reachedMap(*(ReachedMap*)Base::_reached);
948 if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
949 if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
950 // if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode);
951 if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
952 alg.run(Base::_source);
955 ///Runs Bfs algorithm from the given node.
957 ///Runs Bfs algorithm from the given node.
958 ///\param s is the given source.
966 struct DefPredMapBase : public Base {
968 static PredMap *createPredMap(const Graph &G) { return 0; };
969 DefPredMapBase(const TR &b) : TR(b) {}
972 ///\brief \ref named-templ-param "Named parameter"
973 ///function for setting PredMap
975 /// \ref named-templ-param "Named parameter"
976 ///function for setting PredMap
979 BfsWizard<DefPredMapBase<T> > predMap(const T &t)
981 Base::_pred=(void *)&t;
982 return BfsWizard<DefPredMapBase<T> >(*this);
987 struct DefReachedMapBase : public Base {
988 typedef T ReachedMap;
989 static ReachedMap *createReachedMap(const Graph &G) { return 0; };
990 DefReachedMapBase(const TR &b) : TR(b) {}
993 ///\brief \ref named-templ-param "Named parameter"
994 ///function for setting ReachedMap
996 /// \ref named-templ-param "Named parameter"
997 ///function for setting ReachedMap
1000 BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
1002 Base::_pred=(void *)&t;
1003 return BfsWizard<DefReachedMapBase<T> >(*this);
1008 struct DefProcessedMapBase : public Base {
1009 typedef T ProcessedMap;
1010 static ProcessedMap *createProcessedMap(const Graph &G) { return 0; };
1011 DefProcessedMapBase(const TR &b) : TR(b) {}
1014 ///\brief \ref named-templ-param "Named parameter"
1015 ///function for setting ProcessedMap
1017 /// \ref named-templ-param "Named parameter"
1018 ///function for setting ProcessedMap
1021 BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1023 Base::_pred=(void *)&t;
1024 return BfsWizard<DefProcessedMapBase<T> >(*this);
1028 // template<class T>
1029 // struct DefPredNodeMapBase : public Base {
1030 // typedef T PredNodeMap;
1031 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
1032 // DefPredNodeMapBase(const TR &b) : TR(b) {}
1035 // ///\brief \ref named-templ-param "Named parameter"
1036 // ///function for setting PredNodeMap type
1038 // /// \ref named-templ-param "Named parameter"
1039 // ///function for setting PredNodeMap type
1041 // template<class T>
1042 // BfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
1044 // Base::_predNode=(void *)&t;
1045 // return BfsWizard<DefPredNodeMapBase<T> >(*this);
1049 struct DefDistMapBase : public Base {
1051 static DistMap *createDistMap(const Graph &G) { return 0; };
1052 DefDistMapBase(const TR &b) : TR(b) {}
1055 ///\brief \ref named-templ-param "Named parameter"
1056 ///function for setting DistMap type
1058 /// \ref named-templ-param "Named parameter"
1059 ///function for setting DistMap type
1062 BfsWizard<DefDistMapBase<T> > distMap(const T &t)
1064 Base::_dist=(void *)&t;
1065 return BfsWizard<DefDistMapBase<T> >(*this);
1068 /// Sets the source node, from which the Bfs algorithm runs.
1070 /// Sets the source node, from which the Bfs algorithm runs.
1071 /// \param s is the source node.
1072 BfsWizard<TR> &source(Node s)
1080 ///Function type interface for Bfs algorithm.
1082 /// \ingroup flowalgs
1083 ///Function type interface for Bfs algorithm.
1085 ///This function also has several
1086 ///\ref named-templ-func-param "named parameters",
1087 ///they are declared as the members of class \ref BfsWizard.
1089 ///example shows how to use these parameters.
1091 /// bfs(g,source).predMap(preds).run();
1093 ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
1094 ///to the end of the parameter list.
1098 BfsWizard<BfsWizardBase<GR> >
1099 bfs(const GR &g,typename GR::Node s=INVALID)
1101 return BfsWizard<BfsWizardBase<GR> >(g,s);
1104 } //END OF NAMESPACE LEMON