iterable_maps.h header hes been added. Up to now it contains an iterable bool
map and specialized versions for Node and Edge maps.
2 * lemon/bfs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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
90 static ProcessedMap *createProcessedMap(const GR &g)
92 static ProcessedMap *createProcessedMap(const GR &)
95 return new ProcessedMap();
97 ///The type of the map that indicates which nodes are reached.
99 ///The type of the map that indicates which nodes are reached.
100 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
101 ///\todo named parameter to set this type, function to read and write.
102 typedef typename Graph::template NodeMap<bool> ReachedMap;
103 ///Instantiates a ReachedMap.
105 ///This function instantiates a \ref ReachedMap.
106 ///\param G is the graph, to which
107 ///we would like to define the \ref ReachedMap.
108 static ReachedMap *createReachedMap(const GR &G)
110 return new ReachedMap(G);
112 ///The type of the map that stores the dists of the nodes.
114 ///The type of the map that stores the dists of the nodes.
115 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
117 typedef typename Graph::template NodeMap<int> DistMap;
118 ///Instantiates a DistMap.
120 ///This function instantiates a \ref DistMap.
121 ///\param G is the graph, to which we would like to define the \ref DistMap
122 static DistMap *createDistMap(const GR &G)
124 return new DistMap(G);
128 ///%BFS algorithm class.
131 ///This class provides an efficient implementation of the %BFS algorithm.
133 ///\param GR The graph type the algorithm runs on. The default value is
134 ///\ref ListGraph. The value of GR is not used directly by Bfs, it
135 ///is only passed to \ref BfsDefaultTraits.
136 ///\param TR Traits class to set various data types used by the algorithm.
137 ///The default traits class is
138 ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
139 ///See \ref BfsDefaultTraits for the documentation of
140 ///a Bfs traits class.
142 ///\author Alpar Juttner
143 ///\todo A compare object would be nice.
146 template <typename GR,
149 template <typename GR=ListGraph,
150 typename TR=BfsDefaultTraits<GR> >
155 * \brief \ref Exception for uninitialized parameters.
157 * This error represents problems in the initialization
158 * of the parameters of the algorithms.
160 class UninitializedParameter : public lemon::UninitializedParameter {
162 virtual const char* exceptionName() const {
163 return "lemon::Bfs::UninitializedParameter";
168 ///The type of the underlying graph.
169 typedef typename TR::Graph Graph;
171 typedef typename Graph::Node Node;
173 typedef typename Graph::NodeIt NodeIt;
175 typedef typename Graph::Edge Edge;
177 typedef typename Graph::OutEdgeIt OutEdgeIt;
179 ///\brief The type of the map that stores the last
180 ///edges of the shortest paths.
181 typedef typename TR::PredMap PredMap;
182 // ///\brief The type of the map that stores the last but one
183 // ///nodes of the shortest paths.
184 // typedef typename TR::PredNodeMap PredNodeMap;
185 ///The type of the map indicating which nodes are reached.
186 typedef typename TR::ReachedMap ReachedMap;
187 ///The type of the map indicating which nodes are processed.
188 typedef typename TR::ProcessedMap ProcessedMap;
189 ///The type of the map that stores the dists of the nodes.
190 typedef typename TR::DistMap DistMap;
192 /// Pointer to the underlying graph.
194 ///Pointer to the map of predecessors edges.
196 ///Indicates if \ref _pred is locally allocated (\c true) or not.
198 // ///Pointer to the map of predecessors nodes.
199 // PredNodeMap *_predNode;
200 // ///Indicates if \ref _predNode is locally allocated (\c true) or not.
201 // bool local_predNode;
202 ///Pointer to the map of distances.
204 ///Indicates if \ref _dist is locally allocated (\c true) or not.
206 ///Pointer to the map of reached status of the nodes.
207 ReachedMap *_reached;
208 ///Indicates if \ref _reached is locally allocated (\c true) or not.
210 ///Pointer to the map of processed status of the nodes.
211 ProcessedMap *_processed;
212 ///Indicates if \ref _processed is locally allocated (\c true) or not.
213 bool local_processed;
215 std::vector<typename Graph::Node> _queue;
216 int _queue_head,_queue_tail,_queue_next_dist;
218 // ///The source node of the last execution.
221 ///Creates the maps if necessary.
223 ///\todo Error if \c G are \c NULL.
224 ///\todo Better memory allocation (instead of new).
229 _pred = Traits::createPredMap(*G);
232 // local_predNode = true;
233 // _predNode = Traits::createPredNodeMap(*G);
237 _dist = Traits::createDistMap(*G);
240 local_reached = true;
241 _reached = Traits::createReachedMap(*G);
244 local_processed = true;
245 _processed = Traits::createProcessedMap(*G);
251 ///\name Named template parameters
256 struct DefPredMapTraits : public Traits {
258 static PredMap *createPredMap(const Graph &G)
260 throw UninitializedParameter();
263 ///\ref named-templ-param "Named parameter" for setting PredMap type
265 ///\ref named-templ-param "Named parameter" for setting PredMap type
268 class DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > { };
270 // template <class T>
271 // struct DefPredNodeMapTraits : public Traits {
272 // typedef T PredNodeMap;
273 // static PredNodeMap *createPredNodeMap(const Graph &G)
275 // throw UninitializedParameter();
278 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
280 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
282 // template <class T>
283 // class DefPredNodeMap : public Bfs< Graph,
285 // DefPredNodeMapTraits<T> > { };
288 struct DefDistMapTraits : public Traits {
290 static DistMap *createDistMap(const Graph &G)
292 throw UninitializedParameter();
295 ///\ref named-templ-param "Named parameter" for setting DistMap type
297 ///\ref named-templ-param "Named parameter" for setting DistMap type
300 class DefDistMap : public Bfs< Graph,
301 DefDistMapTraits<T> > { };
304 struct DefReachedMapTraits : public Traits {
305 typedef T ReachedMap;
306 static ReachedMap *createReachedMap(const Graph &G)
308 throw UninitializedParameter();
311 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
313 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
316 class DefReachedMap : public Bfs< Graph,
317 DefReachedMapTraits<T> > { };
319 struct DefGraphReachedMapTraits : public Traits {
320 typedef typename Graph::template NodeMap<bool> ReachedMap;
321 static ReachedMap *createReachedMap(const Graph &G)
323 return new ReachedMap(G);
327 struct DefProcessedMapTraits : public Traits {
328 typedef T ProcessedMap;
329 static ProcessedMap *createProcessedMap(const Graph &G)
331 throw UninitializedParameter();
334 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
336 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
339 class DefProcessedMap : public Bfs< Graph,
340 DefProcessedMapTraits<T> > { };
342 struct DefGraphProcessedMapTraits : public Traits {
343 typedef typename Graph::template NodeMap<bool> ProcessedMap;
344 static ProcessedMap *createProcessedMap(const Graph &G)
346 return new ProcessedMap(G);
349 ///\brief \ref named-templ-param "Named parameter"
350 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
352 ///\ref named-templ-param "Named parameter"
353 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
354 ///If you don't set it explicitly, it will be automatically allocated.
356 class DefProcessedMapToBeDefaultMap :
358 DefGraphProcessedMapTraits> { };
366 ///\param _G the graph the algorithm will run on.
368 Bfs(const Graph& _G) :
370 _pred(NULL), local_pred(false),
371 // _predNode(NULL), local_predNode(false),
372 _dist(NULL), local_dist(false),
373 _reached(NULL), local_reached(false),
374 _processed(NULL), local_processed(false)
380 if(local_pred) delete _pred;
381 // if(local_predNode) delete _predNode;
382 if(local_dist) delete _dist;
383 if(local_reached) delete _reached;
384 if(local_processed) delete _processed;
387 ///Sets the map storing the predecessor edges.
389 ///Sets the map storing the predecessor edges.
390 ///If you don't use this function before calling \ref run(),
391 ///it will allocate one. The destructor deallocates this
392 ///automatically allocated map, of course.
393 ///\return <tt> (*this) </tt>
394 Bfs &predMap(PredMap &m)
404 ///Sets the map indicating the reached nodes.
406 ///Sets the map indicating the reached nodes.
407 ///If you don't use this function before calling \ref run(),
408 ///it will allocate one. The destructor deallocates this
409 ///automatically allocated map, of course.
410 ///\return <tt> (*this) </tt>
411 Bfs &reachedMap(ReachedMap &m)
421 ///Sets the map indicating the processed nodes.
423 ///Sets the map indicating the processed nodes.
424 ///If you don't use this function before calling \ref run(),
425 ///it will allocate one. The destructor deallocates this
426 ///automatically allocated map, of course.
427 ///\return <tt> (*this) </tt>
428 Bfs &processedMap(ProcessedMap &m)
430 if(local_processed) {
432 local_processed=false;
438 // ///Sets the map storing the predecessor nodes.
440 // ///Sets the map storing the predecessor nodes.
441 // ///If you don't use this function before calling \ref run(),
442 // ///it will allocate one. The destructor deallocates this
443 // ///automatically allocated map, of course.
444 // ///\return <tt> (*this) </tt>
445 // Bfs &predNodeMap(PredNodeMap &m)
447 // if(local_predNode) {
449 // local_predNode=false;
455 ///Sets the map storing the distances calculated by the algorithm.
457 ///Sets the map storing the distances calculated by the algorithm.
458 ///If you don't use this function before calling \ref run(),
459 ///it will allocate one. The destructor deallocates this
460 ///automatically allocated map, of course.
461 ///\return <tt> (*this) </tt>
462 Bfs &distMap(DistMap &m)
473 ///\name Execution control
474 ///The simplest way to execute the algorithm is to use
475 ///one of the member functions called \c run(...).
477 ///If you need more control on the execution,
478 ///first you must call \ref init(), then you can add several source nodes
479 ///with \ref addSource().
480 ///Finally \ref start() will perform the actual path
485 ///Initializes the internal data structures.
487 ///Initializes the internal data structures.
492 _queue.resize(countNodes(*G));
493 _queue_head=_queue_tail=0;
495 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
496 _pred->set(u,INVALID);
497 // _predNode->set(u,INVALID);
498 _reached->set(u,false);
499 _processed->set(u,false);
503 ///Adds a new source node.
505 ///Adds a new source node to the set of nodes to be processed.
507 void addSource(Node s)
511 _reached->set(s,true);
512 _pred->set(s,INVALID);
514 _queue[_queue_head++]=s;
515 _queue_next_dist=_queue_head;
519 ///Processes the next node.
521 ///Processes the next node.
523 ///\return The processed node.
525 ///\warning The queue must not be empty!
526 Node processNextNode()
528 if(_queue_tail==_queue_next_dist) {
530 _queue_next_dist=_queue_head;
532 Node n=_queue[_queue_tail++];
533 _processed->set(n,true);
535 for(OutEdgeIt e(*G,n);e!=INVALID;++e)
536 if(!(*_reached)[m=G->target(e)]) {
537 _queue[_queue_head++]=m;
538 _reached->set(m,true);
540 // _pred_node->set(m,n);
541 _dist->set(m,_curr_dist);
546 ///Next node to be processed.
548 ///Next node to be processed.
550 ///\return The next node to be processed or INVALID if the queue is
554 return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
557 ///\brief Returns \c false if there are nodes
558 ///to be processed in the queue
560 ///Returns \c false if there are nodes
561 ///to be processed in the queue
562 bool emptyQueue() { return _queue_tail==_queue_head; }
563 ///Returns the number of the nodes to be processed.
565 ///Returns the number of the nodes to be processed in the queue.
567 int queueSize() { return _queue_head-_queue_tail; }
569 ///Executes the algorithm.
571 ///Executes the algorithm.
573 ///\pre init() must be called and at least one node should be added
574 ///with addSource() before using this function.
576 ///This method runs the %BFS algorithm from the root node(s)
579 ///shortest path to each node. The algorithm computes
580 ///- The shortest path tree.
581 ///- The distance of each node from the root(s).
585 while ( !emptyQueue() ) processNextNode();
588 ///Executes the algorithm until \c dest is reached.
590 ///Executes the algorithm until \c dest is reached.
592 ///\pre init() must be called and at least one node should be added
593 ///with addSource() before using this function.
595 ///This method runs the %BFS algorithm from the root node(s)
598 ///shortest path to \c dest. The algorithm computes
599 ///- The shortest path to \c dest.
600 ///- The distance of \c dest from the root(s).
602 void start(Node dest)
604 while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode();
607 ///Executes the algorithm until a condition is met.
609 ///Executes the algorithm until a condition is met.
611 ///\pre init() must be called and at least one node should be added
612 ///with addSource() before using this function.
614 ///\param nm must be a bool (or convertible) node map. The algorithm
615 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
617 void start(const NM &nm)
619 while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode();
622 ///Runs %BFS algorithm from node \c s.
624 ///This method runs the %BFS algorithm from a root node \c s
627 ///shortest path to each node. The algorithm computes
628 ///- The shortest path tree.
629 ///- The distance of each node from the root.
631 ///\note d.run(s) is just a shortcut of the following code.
643 ///Finds the shortest path between \c s and \c t.
645 ///Finds the shortest path between \c s and \c t.
647 ///\return The length of the shortest s---t path if there exists one,
649 ///\note Apart from the return value, d.run(s) is
650 ///just a shortcut of the following code.
656 int run(Node s,Node t) {
660 return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0;
665 ///\name Query Functions
666 ///The result of the %BFS algorithm can be obtained using these
668 ///Before the use of these functions,
669 ///either run() or start() must be called.
673 ///Copies the shortest path to \c t into \c p
675 ///This function copies the shortest path to \c t into \c p.
676 ///If \c t is a source itself or unreachable, then it does not
678 ///\todo Is it the right way to handle unreachable nodes?
679 ///\return Returns \c true if a path to \c t was actually copied to \c p,
680 ///\c false otherwise.
683 bool getPath(P &p,Node t)
687 typename P::Builder b(p);
688 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
689 b.pushFront(pred(t));
696 ///The distance of a node from the root(s).
698 ///Returns the distance of a node from the root(s).
699 ///\pre \ref run() must be called before using this function.
700 ///\warning If node \c v in unreachable from the root(s) the return value
701 ///of this function is undefined.
702 int dist(Node v) const { return (*_dist)[v]; }
704 ///Returns the 'previous edge' of the shortest path tree.
706 ///For a node \c v it returns the 'previous edge'
707 ///of the shortest path tree,
708 ///i.e. it returns the last edge of a shortest path from the root(s) to \c
709 ///v. It is \ref INVALID
710 ///if \c v is unreachable from the root(s) or \c v is a root. The
711 ///shortest path tree used here is equal to the shortest path tree used in
713 ///\pre Either \ref run() or \ref start() must be called before using
715 ///\todo predEdge could be a better name.
716 Edge pred(Node v) const { return (*_pred)[v];}
718 ///Returns the 'previous node' of the shortest path tree.
720 ///For a node \c v it returns the 'previous node'
721 ///of the shortest path tree,
722 ///i.e. it returns the last but one node from a shortest path from the
724 ///It is INVALID if \c v is unreachable from the root(s) or
725 ///if \c v itself a root.
726 ///The shortest path tree used here is equal to the shortest path
727 ///tree used in \ref pred().
728 ///\pre Either \ref run() or \ref start() must be called before
729 ///using this function.
730 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
731 G->source((*_pred)[v]); }
733 ///Returns a reference to the NodeMap of distances.
735 ///Returns a reference to the NodeMap of distances.
736 ///\pre Either \ref run() or \ref init() must
737 ///be called before using this function.
738 const DistMap &distMap() const { return *_dist;}
740 ///Returns a reference to the shortest path tree map.
742 ///Returns a reference to the NodeMap of the edges of the
743 ///shortest path tree.
744 ///\pre Either \ref run() or \ref init()
745 ///must be called before using this function.
746 const PredMap &predMap() const { return *_pred;}
748 // ///Returns a reference to the map of nodes of shortest paths.
750 // ///Returns a reference to the NodeMap of the last but one nodes of the
751 // ///shortest path tree.
752 // ///\pre \ref run() must be called before using this function.
753 // const PredNodeMap &predNodeMap() const { return *_predNode;}
755 ///Checks if a node is reachable from the root.
757 ///Returns \c true if \c v is reachable from the root.
758 ///\warning The source nodes are indicated as unreached.
759 ///\pre Either \ref run() or \ref start()
760 ///must be called before using this function.
762 bool reached(Node v) { return (*_reached)[v]; }
767 ///Default traits class of Bfs function.
769 ///Default traits class of Bfs function.
770 ///\param GR Graph type.
772 struct BfsWizardDefaultTraits
774 ///The graph type the algorithm runs on.
776 ///\brief The type of the map that stores the last
777 ///edges of the shortest paths.
779 ///The type of the map that stores the last
780 ///edges of the shortest paths.
781 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
783 typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
784 ///Instantiates a PredMap.
786 ///This function instantiates a \ref PredMap.
787 ///\param g is the graph, to which we would like to define the PredMap.
788 ///\todo The graph alone may be insufficient to initialize
790 static PredMap *createPredMap(const GR &g)
792 static PredMap *createPredMap(const GR &)
795 return new PredMap();
797 // ///\brief The type of the map that stores the last but one
798 // ///nodes of the shortest paths.
800 // ///The type of the map that stores the last but one
801 // ///nodes of the shortest paths.
802 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
804 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
805 // ///Instantiates a PredNodeMap.
807 // ///This function instantiates a \ref PredNodeMap.
808 // ///\param G is the graph, to which
809 // ///we would like to define the \ref PredNodeMap
810 // static PredNodeMap *createPredNodeMap(const GR &G)
812 // return new PredNodeMap();
815 ///The type of the map that indicates which nodes are processed.
817 ///The type of the map that indicates which nodes are processed.
818 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
819 ///\todo named parameter to set this type, function to read and write.
820 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
821 ///Instantiates a ProcessedMap.
823 ///This function instantiates a \ref ProcessedMap.
824 ///\param g is the graph, to which
825 ///we would like to define the \ref ProcessedMap
827 static ProcessedMap *createProcessedMap(const GR &g)
829 static ProcessedMap *createProcessedMap(const GR &)
832 return new ProcessedMap();
834 ///The type of the map that indicates which nodes are reached.
836 ///The type of the map that indicates which nodes are reached.
837 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
838 ///\todo named parameter to set this type, function to read and write.
839 typedef typename Graph::template NodeMap<bool> ReachedMap;
840 ///Instantiates a ReachedMap.
842 ///This function instantiates a \ref ReachedMap.
843 ///\param G is the graph, to which
844 ///we would like to define the \ref ReachedMap.
845 static ReachedMap *createReachedMap(const GR &G)
847 return new ReachedMap(G);
849 ///The type of the map that stores the dists of the nodes.
851 ///The type of the map that stores the dists of the nodes.
852 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
854 typedef NullMap<typename Graph::Node,int> DistMap;
855 ///Instantiates a DistMap.
857 ///This function instantiates a \ref DistMap.
858 ///\param g is the graph, to which we would like to define the \ref DistMap
860 static DistMap *createDistMap(const GR &g)
862 static DistMap *createDistMap(const GR &)
865 return new DistMap();
869 /// Default traits used by \ref BfsWizard
871 /// To make it easier to use Bfs algorithm
872 ///we have created a wizard class.
873 /// This \ref BfsWizard class needs default traits,
874 ///as well as the \ref Bfs class.
875 /// The \ref BfsWizardBase is a class to be the default traits of the
876 /// \ref BfsWizard class.
878 class BfsWizardBase : public BfsWizardDefaultTraits<GR>
881 typedef BfsWizardDefaultTraits<GR> Base;
883 /// Type of the nodes in the graph.
884 typedef typename Base::Graph::Node Node;
886 /// Pointer to the underlying graph.
888 ///Pointer to the map of reached nodes.
890 ///Pointer to the map of processed nodes.
892 ///Pointer to the map of predecessors edges.
894 // ///Pointer to the map of predecessors nodes.
896 ///Pointer to the map of distances.
898 ///Pointer to the source node.
904 /// This constructor does not require parameters, therefore it initiates
905 /// all of the attributes to default values (0, INVALID).
906 BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
908 _dist(0), _source(INVALID) {}
912 /// This constructor requires some parameters,
913 /// listed in the parameters list.
914 /// Others are initiated to 0.
915 /// \param g is the initial value of \ref _g
916 /// \param s is the initial value of \ref _source
917 BfsWizardBase(const GR &g, Node s=INVALID) :
918 _g((void *)&g), _reached(0), _processed(0), _pred(0),
920 _dist(0), _source(s) {}
924 /// A class to make the usage of Bfs algorithm easier
926 /// This class is created to make it easier to use Bfs algorithm.
927 /// It uses the functions and features of the plain \ref Bfs,
928 /// but it is much simpler to use it.
930 /// Simplicity means that the way to change the types defined
931 /// in the traits class is based on functions that returns the new class
932 /// and not on templatable built-in classes.
933 /// When using the plain \ref Bfs
934 /// the new class with the modified type comes from
935 /// the original class by using the ::
936 /// operator. In the case of \ref BfsWizard only
937 /// a function have to be called and it will
938 /// return the needed class.
940 /// It does not have own \ref run method. When its \ref run method is called
941 /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
944 class BfsWizard : public TR
948 ///The type of the underlying graph.
949 typedef typename TR::Graph Graph;
951 typedef typename Graph::Node Node;
953 typedef typename Graph::NodeIt NodeIt;
955 typedef typename Graph::Edge Edge;
957 typedef typename Graph::OutEdgeIt OutEdgeIt;
959 ///\brief The type of the map that stores
961 typedef typename TR::ReachedMap ReachedMap;
962 ///\brief The type of the map that stores
963 ///the processed nodes
964 typedef typename TR::ProcessedMap ProcessedMap;
965 ///\brief The type of the map that stores the last
966 ///edges of the shortest paths.
967 typedef typename TR::PredMap PredMap;
968 // ///\brief The type of the map that stores the last but one
969 // ///nodes of the shortest paths.
970 // typedef typename TR::PredNodeMap PredNodeMap;
971 ///The type of the map that stores the dists of the nodes.
972 typedef typename TR::DistMap DistMap;
976 BfsWizard() : TR() {}
978 /// Constructor that requires parameters.
980 /// Constructor that requires parameters.
981 /// These parameters will be the default values for the traits class.
982 BfsWizard(const Graph &g, Node s=INVALID) :
986 BfsWizard(const TR &b) : TR(b) {}
990 ///Runs Bfs algorithm from a given node.
992 ///Runs Bfs algorithm from a given node.
993 ///The node can be given by the \ref source function.
996 if(Base::_source==INVALID) throw UninitializedParameter();
997 Bfs<Graph,TR> alg(*(Graph*)Base::_g);
999 alg.reachedMap(*(ReachedMap*)Base::_reached);
1000 if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
1001 if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
1002 // if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode);
1003 if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
1004 alg.run(Base::_source);
1007 ///Runs Bfs algorithm from the given node.
1009 ///Runs Bfs algorithm from the given node.
1010 ///\param s is the given source.
1018 struct DefPredMapBase : public Base {
1020 static PredMap *createPredMap(const Graph &) { return 0; };
1021 DefPredMapBase(const TR &b) : TR(b) {}
1024 ///\brief \ref named-templ-param "Named parameter"
1025 ///function for setting PredMap
1027 /// \ref named-templ-param "Named parameter"
1028 ///function for setting PredMap
1031 BfsWizard<DefPredMapBase<T> > predMap(const T &t)
1033 Base::_pred=(void *)&t;
1034 return BfsWizard<DefPredMapBase<T> >(*this);
1039 struct DefReachedMapBase : public Base {
1040 typedef T ReachedMap;
1041 static ReachedMap *createReachedMap(const Graph &) { return 0; };
1042 DefReachedMapBase(const TR &b) : TR(b) {}
1045 ///\brief \ref named-templ-param "Named parameter"
1046 ///function for setting ReachedMap
1048 /// \ref named-templ-param "Named parameter"
1049 ///function for setting ReachedMap
1052 BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
1054 Base::_pred=(void *)&t;
1055 return BfsWizard<DefReachedMapBase<T> >(*this);
1060 struct DefProcessedMapBase : public Base {
1061 typedef T ProcessedMap;
1062 static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
1063 DefProcessedMapBase(const TR &b) : TR(b) {}
1066 ///\brief \ref named-templ-param "Named parameter"
1067 ///function for setting ProcessedMap
1069 /// \ref named-templ-param "Named parameter"
1070 ///function for setting ProcessedMap
1073 BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1075 Base::_pred=(void *)&t;
1076 return BfsWizard<DefProcessedMapBase<T> >(*this);
1080 // template<class T>
1081 // struct DefPredNodeMapBase : public Base {
1082 // typedef T PredNodeMap;
1083 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
1084 // DefPredNodeMapBase(const TR &b) : TR(b) {}
1087 // ///\brief \ref named-templ-param "Named parameter"
1088 // ///function for setting PredNodeMap type
1090 // /// \ref named-templ-param "Named parameter"
1091 // ///function for setting PredNodeMap type
1093 // template<class T>
1094 // BfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
1096 // Base::_predNode=(void *)&t;
1097 // return BfsWizard<DefPredNodeMapBase<T> >(*this);
1101 struct DefDistMapBase : public Base {
1103 static DistMap *createDistMap(const Graph &) { return 0; };
1104 DefDistMapBase(const TR &b) : TR(b) {}
1107 ///\brief \ref named-templ-param "Named parameter"
1108 ///function for setting DistMap type
1110 /// \ref named-templ-param "Named parameter"
1111 ///function for setting DistMap type
1114 BfsWizard<DefDistMapBase<T> > distMap(const T &t)
1116 Base::_dist=(void *)&t;
1117 return BfsWizard<DefDistMapBase<T> >(*this);
1120 /// Sets the source node, from which the Bfs algorithm runs.
1122 /// Sets the source node, from which the Bfs algorithm runs.
1123 /// \param s is the source node.
1124 BfsWizard<TR> &source(Node s)
1132 ///Function type interface for Bfs algorithm.
1134 /// \ingroup flowalgs
1135 ///Function type interface for Bfs algorithm.
1137 ///This function also has several
1138 ///\ref named-templ-func-param "named parameters",
1139 ///they are declared as the members of class \ref BfsWizard.
1141 ///example shows how to use these parameters.
1143 /// bfs(g,source).predMap(preds).run();
1145 ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
1146 ///to the end of the parameter list.
1150 BfsWizard<BfsWizardBase<GR> >
1151 bfs(const GR &g,typename GR::Node s=INVALID)
1153 return BfsWizard<BfsWizardBase<GR> >(g,s);
1156 } //END OF NAMESPACE LEMON