Fix multiple executions in matchings (fract. mathcings) (#356)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
24 ///\brief DFS algorithm.
26 #include <lemon/list_graph.h>
27 #include <lemon/bits/path_dump.h>
28 #include <lemon/core.h>
29 #include <lemon/error.h>
30 #include <lemon/maps.h>
31 #include <lemon/path.h>
35 ///Default traits class of Dfs class.
37 ///Default traits class of Dfs class.
38 ///\tparam GR Digraph type.
40 struct DfsDefaultTraits
42 ///The type of the digraph the algorithm runs on.
45 ///\brief The type of the map that stores the predecessor
46 ///arcs of the %DFS paths.
48 ///The type of the map that stores the predecessor
49 ///arcs of the %DFS paths.
50 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
51 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52 ///Instantiates a \c PredMap.
54 ///This function instantiates a \ref PredMap.
55 ///\param g is the digraph, to which we would like to define the
57 static PredMap *createPredMap(const Digraph &g)
59 return new PredMap(g);
62 ///The type of the map that indicates which nodes are processed.
64 ///The type of the map that indicates which nodes are processed.
65 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
66 ///By default, it is a NullMap.
67 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
68 ///Instantiates a \c ProcessedMap.
70 ///This function instantiates a \ref ProcessedMap.
71 ///\param g is the digraph, to which
72 ///we would like to define the \ref ProcessedMap.
74 static ProcessedMap *createProcessedMap(const Digraph &g)
76 static ProcessedMap *createProcessedMap(const Digraph &)
79 return new ProcessedMap();
82 ///The type of the map that indicates which nodes are reached.
84 ///The type of the map that indicates which nodes are reached.
85 ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
86 typedef typename Digraph::template NodeMap<bool> ReachedMap;
87 ///Instantiates a \c ReachedMap.
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)
94 return new ReachedMap(g);
97 ///The type of the map that stores the distances of the nodes.
99 ///The type of the map that stores the distances of the nodes.
100 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
101 typedef typename Digraph::template NodeMap<int> DistMap;
102 ///Instantiates a \c DistMap.
104 ///This function instantiates a \ref DistMap.
105 ///\param g is the digraph, to which we would like to define the
107 static DistMap *createDistMap(const Digraph &g)
109 return new DistMap(g);
113 ///%DFS algorithm class.
116 ///This class provides an efficient implementation of the %DFS algorithm.
118 ///There is also a \ref dfs() "function-type interface" for the DFS
119 ///algorithm, which is convenient in the simplier cases and it can be
122 ///\tparam GR The type of the digraph the algorithm runs on.
123 ///The default type is \ref ListDigraph.
124 ///\tparam TR The traits class that defines various types used by the
125 ///algorithm. By default, it is \ref DfsDefaultTraits
126 ///"DfsDefaultTraits<GR>".
127 ///In most cases, this parameter should not be set directly,
128 ///consider to use the named template parameters instead.
130 template <typename GR,
133 template <typename GR=ListDigraph,
134 typename TR=DfsDefaultTraits<GR> >
139 ///The type of the digraph the algorithm runs on.
140 typedef typename TR::Digraph Digraph;
142 ///\brief The type of the map that stores the predecessor arcs of the
144 typedef typename TR::PredMap PredMap;
145 ///The type of the map that stores the distances of the nodes.
146 typedef typename TR::DistMap DistMap;
147 ///The type of the map that indicates which nodes are reached.
148 typedef typename TR::ReachedMap ReachedMap;
149 ///The type of the map that indicates which nodes are processed.
150 typedef typename TR::ProcessedMap ProcessedMap;
151 ///The type of the paths.
152 typedef PredMapPath<Digraph, PredMap> Path;
154 ///The \ref DfsDefaultTraits "traits class" of the algorithm.
159 typedef typename Digraph::Node Node;
160 typedef typename Digraph::NodeIt NodeIt;
161 typedef typename Digraph::Arc Arc;
162 typedef typename Digraph::OutArcIt OutArcIt;
164 //Pointer to the underlying digraph.
166 //Pointer to the map of predecessor arcs.
168 //Indicates if _pred is locally allocated (true) or not.
170 //Pointer to the map of distances.
172 //Indicates if _dist is locally allocated (true) or not.
174 //Pointer to the map of reached status of the nodes.
175 ReachedMap *_reached;
176 //Indicates if _reached is locally allocated (true) or not.
178 //Pointer to the map of processed status of the nodes.
179 ProcessedMap *_processed;
180 //Indicates if _processed is locally allocated (true) or not.
181 bool local_processed;
183 std::vector<typename Digraph::OutArcIt> _stack;
186 //Creates the maps if necessary.
191 _pred = Traits::createPredMap(*G);
195 _dist = Traits::createDistMap(*G);
198 local_reached = true;
199 _reached = Traits::createReachedMap(*G);
202 local_processed = true;
203 _processed = Traits::createProcessedMap(*G);
215 ///\name Named Template Parameters
220 struct SetPredMapTraits : public Traits {
222 static PredMap *createPredMap(const Digraph &)
224 LEMON_ASSERT(false, "PredMap is not initialized");
225 return 0; // ignore warnings
228 ///\brief \ref named-templ-param "Named parameter" for setting
231 ///\ref named-templ-param "Named parameter" for setting
233 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
235 struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
236 typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
240 struct SetDistMapTraits : public Traits {
242 static DistMap *createDistMap(const Digraph &)
244 LEMON_ASSERT(false, "DistMap is not initialized");
245 return 0; // ignore warnings
248 ///\brief \ref named-templ-param "Named parameter" for setting
251 ///\ref named-templ-param "Named parameter" for setting
253 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
255 struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
256 typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
260 struct SetReachedMapTraits : public Traits {
261 typedef T ReachedMap;
262 static ReachedMap *createReachedMap(const Digraph &)
264 LEMON_ASSERT(false, "ReachedMap is not initialized");
265 return 0; // ignore warnings
268 ///\brief \ref named-templ-param "Named parameter" for setting
269 ///\c ReachedMap type.
271 ///\ref named-templ-param "Named parameter" for setting
272 ///\c ReachedMap type.
273 ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
275 struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
276 typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
280 struct SetProcessedMapTraits : public Traits {
281 typedef T ProcessedMap;
282 static ProcessedMap *createProcessedMap(const Digraph &)
284 LEMON_ASSERT(false, "ProcessedMap is not initialized");
285 return 0; // ignore warnings
288 ///\brief \ref named-templ-param "Named parameter" for setting
289 ///\c ProcessedMap type.
291 ///\ref named-templ-param "Named parameter" for setting
292 ///\c ProcessedMap type.
293 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
295 struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
296 typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
299 struct SetStandardProcessedMapTraits : public Traits {
300 typedef typename Digraph::template NodeMap<bool> ProcessedMap;
301 static ProcessedMap *createProcessedMap(const Digraph &g)
303 return new ProcessedMap(g);
306 ///\brief \ref named-templ-param "Named parameter" for setting
307 ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
309 ///\ref named-templ-param "Named parameter" for setting
310 ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
311 ///If you don't set it explicitly, it will be automatically allocated.
312 struct SetStandardProcessedMap :
313 public Dfs< Digraph, SetStandardProcessedMapTraits > {
314 typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
324 ///\param g The digraph the algorithm runs on.
325 Dfs(const Digraph &g) :
327 _pred(NULL), local_pred(false),
328 _dist(NULL), local_dist(false),
329 _reached(NULL), local_reached(false),
330 _processed(NULL), local_processed(false)
336 if(local_pred) delete _pred;
337 if(local_dist) delete _dist;
338 if(local_reached) delete _reached;
339 if(local_processed) delete _processed;
342 ///Sets the map that stores the predecessor arcs.
344 ///Sets the map that stores the predecessor arcs.
345 ///If you don't use this function before calling \ref run(Node) "run()"
346 ///or \ref init(), an instance will be allocated automatically.
347 ///The destructor deallocates this automatically allocated map,
349 ///\return <tt> (*this) </tt>
350 Dfs &predMap(PredMap &m)
360 ///Sets the map that indicates which nodes are reached.
362 ///Sets the map that indicates which nodes are reached.
363 ///If you don't use this function before calling \ref run(Node) "run()"
364 ///or \ref init(), an instance will be allocated automatically.
365 ///The destructor deallocates this automatically allocated map,
367 ///\return <tt> (*this) </tt>
368 Dfs &reachedMap(ReachedMap &m)
378 ///Sets the map that indicates which nodes are processed.
380 ///Sets the map that indicates which nodes are processed.
381 ///If you don't use this function before calling \ref run(Node) "run()"
382 ///or \ref init(), an instance will be allocated automatically.
383 ///The destructor deallocates this automatically allocated map,
385 ///\return <tt> (*this) </tt>
386 Dfs &processedMap(ProcessedMap &m)
388 if(local_processed) {
390 local_processed=false;
396 ///Sets the map that stores the distances of the nodes.
398 ///Sets the map that stores the distances of the nodes calculated by
400 ///If you don't use this function before calling \ref run(Node) "run()"
401 ///or \ref init(), an instance will be allocated automatically.
402 ///The destructor deallocates this automatically allocated map,
404 ///\return <tt> (*this) </tt>
405 Dfs &distMap(DistMap &m)
417 ///\name Execution Control
418 ///The simplest way to execute the DFS algorithm is to use one of the
419 ///member functions called \ref run(Node) "run()".\n
420 ///If you need better control on the execution, you have to call
421 ///\ref init() first, then you can add a source node with \ref addSource()
422 ///and perform the actual computation with \ref start().
423 ///This procedure can be repeated if there are nodes that have not
428 ///\brief Initializes the internal data structures.
430 ///Initializes the internal data structures.
434 _stack.resize(countNodes(*G));
436 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
437 _pred->set(u,INVALID);
438 _reached->set(u,false);
439 _processed->set(u,false);
443 ///Adds a new source node.
445 ///Adds a new source node to the set of nodes to be processed.
447 ///\pre The stack must be empty. Otherwise the algorithm gives
448 ///wrong results. (One of the outgoing arcs of all the source nodes
449 ///except for the last one will not be visited and distances will
451 void addSource(Node s)
453 LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
456 _reached->set(s,true);
457 _pred->set(s,INVALID);
460 _stack[++_stack_head]=e;
461 _dist->set(s,_stack_head);
464 _processed->set(s,true);
470 ///Processes the next arc.
472 ///Processes the next arc.
474 ///\return The processed arc.
476 ///\pre The stack must not be empty.
480 Arc e=_stack[_stack_head];
481 if(!(*_reached)[m=G->target(e)]) {
483 _reached->set(m,true);
485 _stack[_stack_head] = OutArcIt(*G, m);
486 _dist->set(m,_stack_head);
490 ++_stack[_stack_head];
492 while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
493 _processed->set(m,true);
496 m=G->source(_stack[_stack_head]);
497 ++_stack[_stack_head];
503 ///Next arc to be processed.
505 ///Next arc to be processed.
507 ///\return The next arc to be processed or \c INVALID if the stack
509 OutArcIt nextArc() const
511 return _stack_head>=0?_stack[_stack_head]:INVALID;
514 ///Returns \c false if there are nodes to be processed.
516 ///Returns \c false if there are nodes to be processed
517 ///in the queue (stack).
518 bool emptyQueue() const { return _stack_head<0; }
520 ///Returns the number of the nodes to be processed.
522 ///Returns the number of the nodes to be processed
523 ///in the queue (stack).
524 int queueSize() const { return _stack_head+1; }
526 ///Executes the algorithm.
528 ///Executes the algorithm.
530 ///This method runs the %DFS algorithm from the root node
531 ///in order to compute the DFS path to each node.
533 /// The algorithm computes
535 ///- the distance of each node from the root in the %DFS tree.
537 ///\pre init() must be called and a root node should be
538 ///added with addSource() before using this function.
540 ///\note <tt>d.start()</tt> is just a shortcut of the following code.
542 /// while ( !d.emptyQueue() ) {
543 /// d.processNextArc();
548 while ( !emptyQueue() ) processNextArc();
551 ///Executes the algorithm until the given target node is reached.
553 ///Executes the algorithm until the given target node is reached.
555 ///This method runs the %DFS algorithm from the root node
556 ///in order to compute the DFS path to \c t.
558 ///The algorithm computes
559 ///- the %DFS path to \c t,
560 ///- the distance of \c t from the root in the %DFS tree.
562 ///\pre init() must be called and a root node should be
563 ///added with addSource() before using this function.
566 while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
570 ///Executes the algorithm until a condition is met.
572 ///Executes the algorithm until a condition is met.
574 ///This method runs the %DFS algorithm from the root node
575 ///until an arc \c a with <tt>am[a]</tt> true is found.
577 ///\param am A \c bool (or convertible) arc map. The algorithm
578 ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
580 ///\return The reached arc \c a with <tt>am[a]</tt> true or
581 ///\c INVALID if no such arc was found.
583 ///\pre init() must be called and a root node should be
584 ///added with addSource() before using this function.
586 ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
588 template<class ArcBoolMap>
589 Arc start(const ArcBoolMap &am)
591 while ( !emptyQueue() && !am[_stack[_stack_head]] )
593 return emptyQueue() ? INVALID : _stack[_stack_head];
596 ///Runs the algorithm from the given source node.
598 ///This method runs the %DFS algorithm from node \c s
599 ///in order to compute the DFS path to each node.
601 ///The algorithm computes
603 ///- the distance of each node from the root in the %DFS tree.
605 ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
617 ///Finds the %DFS path between \c s and \c t.
619 ///This method runs the %DFS algorithm from node \c s
620 ///in order to compute the DFS path to node \c t
621 ///(it stops searching when \c t is processed)
623 ///\return \c true if \c t is reachable form \c s.
625 ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
626 ///just a shortcut of the following code.
632 bool run(Node s,Node t) {
639 ///Runs the algorithm to visit all nodes in the digraph.
641 ///This method runs the %DFS algorithm in order to visit all nodes
644 ///\note <tt>d.run()</tt> is just a shortcut of the following code.
647 /// for (NodeIt n(digraph); n != INVALID; ++n) {
648 /// if (!d.reached(n)) {
656 for (NodeIt it(*G); it != INVALID; ++it) {
666 ///\name Query Functions
667 ///The results of the DFS algorithm can be obtained using these
669 ///Either \ref run(Node) "run()" or \ref start() should be called
670 ///before using them.
674 ///The DFS path to the given node.
676 ///Returns the DFS path to the given node from the root(s).
678 ///\warning \c t should be reached from the root(s).
680 ///\pre Either \ref run(Node) "run()" or \ref init()
681 ///must be called before using this function.
682 Path path(Node t) const { return Path(*G, *_pred, t); }
684 ///The distance of the given node from the root(s).
686 ///Returns the distance of the given node from the root(s).
688 ///\warning If node \c v is not reached from the root(s), then
689 ///the return value of this function is undefined.
691 ///\pre Either \ref run(Node) "run()" or \ref init()
692 ///must be called before using this function.
693 int dist(Node v) const { return (*_dist)[v]; }
695 ///Returns the 'previous arc' of the %DFS tree for the given node.
697 ///This function returns the 'previous arc' of the %DFS tree for the
698 ///node \c v, i.e. it returns the last arc of a %DFS path from a
699 ///root to \c v. It is \c INVALID if \c v is not reached from the
700 ///root(s) or if \c v is a root.
702 ///The %DFS tree used here is equal to the %DFS tree used in
703 ///\ref predNode() and \ref predMap().
705 ///\pre Either \ref run(Node) "run()" or \ref init()
706 ///must be called before using this function.
707 Arc predArc(Node v) const { return (*_pred)[v];}
709 ///Returns the 'previous node' of the %DFS tree for the given node.
711 ///This function returns the 'previous node' of the %DFS
712 ///tree for the node \c v, i.e. it returns the last but one node
713 ///of a %DFS path from a root to \c v. It is \c INVALID
714 ///if \c v is not reached from the root(s) or if \c v is a root.
716 ///The %DFS tree used here is equal to the %DFS tree used in
717 ///\ref predArc() and \ref predMap().
719 ///\pre Either \ref run(Node) "run()" or \ref init()
720 ///must be called before using this function.
721 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
722 G->source((*_pred)[v]); }
724 ///\brief Returns a const reference to the node map that stores the
725 ///distances of the nodes.
727 ///Returns a const reference to the node map that stores the
728 ///distances of the nodes calculated by the algorithm.
730 ///\pre Either \ref run(Node) "run()" or \ref init()
731 ///must be called before using this function.
732 const DistMap &distMap() const { return *_dist;}
734 ///\brief Returns a const reference to the node map that stores the
737 ///Returns a const reference to the node map that stores the predecessor
738 ///arcs, which form the DFS tree (forest).
740 ///\pre Either \ref run(Node) "run()" or \ref init()
741 ///must be called before using this function.
742 const PredMap &predMap() const { return *_pred;}
744 ///Checks if the given node. node is reached from the root(s).
746 ///Returns \c true if \c v is reached from the root(s).
748 ///\pre Either \ref run(Node) "run()" or \ref init()
749 ///must be called before using this function.
750 bool reached(Node v) const { return (*_reached)[v]; }
755 ///Default traits class of dfs() function.
757 ///Default traits class of dfs() function.
758 ///\tparam GR Digraph type.
760 struct DfsWizardDefaultTraits
762 ///The type of the digraph the algorithm runs on.
765 ///\brief The type of the map that stores the predecessor
766 ///arcs of the %DFS paths.
768 ///The type of the map that stores the predecessor
769 ///arcs of the %DFS paths.
770 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
771 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
772 ///Instantiates a PredMap.
774 ///This function instantiates a PredMap.
775 ///\param g is the digraph, to which we would like to define the
777 static PredMap *createPredMap(const Digraph &g)
779 return new PredMap(g);
782 ///The type of the map that indicates which nodes are processed.
784 ///The type of the map that indicates which nodes are processed.
785 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
786 ///By default, it is a NullMap.
787 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
788 ///Instantiates a ProcessedMap.
790 ///This function instantiates a ProcessedMap.
791 ///\param g is the digraph, to which
792 ///we would like to define the ProcessedMap.
794 static ProcessedMap *createProcessedMap(const Digraph &g)
796 static ProcessedMap *createProcessedMap(const Digraph &)
799 return new ProcessedMap();
802 ///The type of the map that indicates which nodes are reached.
804 ///The type of the map that indicates which nodes are reached.
805 ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
806 typedef typename Digraph::template NodeMap<bool> ReachedMap;
807 ///Instantiates a ReachedMap.
809 ///This function instantiates a ReachedMap.
810 ///\param g is the digraph, to which
811 ///we would like to define the ReachedMap.
812 static ReachedMap *createReachedMap(const Digraph &g)
814 return new ReachedMap(g);
817 ///The type of the map that stores the distances of the nodes.
819 ///The type of the map that stores the distances of the nodes.
820 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
821 typedef typename Digraph::template NodeMap<int> DistMap;
822 ///Instantiates a DistMap.
824 ///This function instantiates a DistMap.
825 ///\param g is the digraph, to which we would like to define
827 static DistMap *createDistMap(const Digraph &g)
829 return new DistMap(g);
832 ///The type of the DFS paths.
834 ///The type of the DFS paths.
835 ///It must conform to the \ref concepts::Path "Path" concept.
836 typedef lemon::Path<Digraph> Path;
839 /// Default traits class used by DfsWizard
841 /// Default traits class used by DfsWizard.
842 /// \tparam GR The type of the digraph.
844 class DfsWizardBase : public DfsWizardDefaultTraits<GR>
847 typedef DfsWizardDefaultTraits<GR> Base;
849 //The type of the nodes in the digraph.
850 typedef typename Base::Digraph::Node Node;
852 //Pointer to the digraph the algorithm runs on.
854 //Pointer to the map of reached nodes.
856 //Pointer to the map of processed nodes.
858 //Pointer to the map of predecessors arcs.
860 //Pointer to the map of distances.
862 //Pointer to the DFS path to the target node.
864 //Pointer to the distance of the target node.
870 /// This constructor does not require parameters, it initiates
871 /// all of the attributes to \c 0.
872 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
873 _dist(0), _path(0), _di(0) {}
877 /// This constructor requires one parameter,
878 /// others are initiated to \c 0.
879 /// \param g The digraph the algorithm runs on.
880 DfsWizardBase(const GR &g) :
881 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
882 _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
886 /// Auxiliary class for the function-type interface of DFS algorithm.
888 /// This auxiliary class is created to implement the
889 /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
890 /// It does not have own \ref run(Node) "run()" method, it uses the
891 /// functions and features of the plain \ref Dfs.
893 /// This class should only be used through the \ref dfs() function,
894 /// which makes it easier to use the algorithm.
896 /// \tparam TR The traits class that defines various types used by the
899 class DfsWizard : public TR
903 typedef typename TR::Digraph Digraph;
905 typedef typename Digraph::Node Node;
906 typedef typename Digraph::NodeIt NodeIt;
907 typedef typename Digraph::Arc Arc;
908 typedef typename Digraph::OutArcIt OutArcIt;
910 typedef typename TR::PredMap PredMap;
911 typedef typename TR::DistMap DistMap;
912 typedef typename TR::ReachedMap ReachedMap;
913 typedef typename TR::ProcessedMap ProcessedMap;
914 typedef typename TR::Path Path;
919 DfsWizard() : TR() {}
921 /// Constructor that requires parameters.
923 /// Constructor that requires parameters.
924 /// These parameters will be the default values for the traits class.
925 /// \param g The digraph the algorithm runs on.
926 DfsWizard(const Digraph &g) :
930 DfsWizard(const TR &b) : TR(b) {}
934 ///Runs DFS algorithm from the given source node.
936 ///This method runs DFS algorithm from node \c s
937 ///in order to compute the DFS path to each node.
940 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
942 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
944 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
946 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
947 if (Base::_processed)
948 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
955 ///Finds the DFS path between \c s and \c t.
957 ///This method runs DFS algorithm from node \c s
958 ///in order to compute the DFS path to node \c t
959 ///(it stops searching when \c t is processed).
961 ///\return \c true if \c t is reachable form \c s.
962 bool run(Node s, Node t)
964 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
966 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
968 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
970 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
971 if (Base::_processed)
972 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
975 *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
977 *Base::_di = alg.dist(t);
978 return alg.reached(t);
981 ///Runs DFS algorithm to visit all nodes in the digraph.
983 ///This method runs DFS algorithm in order to visit all nodes
991 struct SetPredMapBase : public Base {
993 static PredMap *createPredMap(const Digraph &) { return 0; };
994 SetPredMapBase(const TR &b) : TR(b) {}
997 ///\brief \ref named-templ-param "Named parameter" for setting
998 ///the predecessor map.
1000 ///\ref named-templ-param "Named parameter" function for setting
1001 ///the map that stores the predecessor arcs of the nodes.
1003 DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1005 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1006 return DfsWizard<SetPredMapBase<T> >(*this);
1010 struct SetReachedMapBase : public Base {
1011 typedef T ReachedMap;
1012 static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1013 SetReachedMapBase(const TR &b) : TR(b) {}
1016 ///\brief \ref named-templ-param "Named parameter" for setting
1019 ///\ref named-templ-param "Named parameter" function for setting
1020 ///the map that indicates which nodes are reached.
1022 DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1024 Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1025 return DfsWizard<SetReachedMapBase<T> >(*this);
1029 struct SetDistMapBase : public Base {
1031 static DistMap *createDistMap(const Digraph &) { return 0; };
1032 SetDistMapBase(const TR &b) : TR(b) {}
1035 ///\brief \ref named-templ-param "Named parameter" for setting
1036 ///the distance map.
1038 ///\ref named-templ-param "Named parameter" function for setting
1039 ///the map that stores the distances of the nodes calculated
1040 ///by the algorithm.
1042 DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1044 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1045 return DfsWizard<SetDistMapBase<T> >(*this);
1049 struct SetProcessedMapBase : public Base {
1050 typedef T ProcessedMap;
1051 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1052 SetProcessedMapBase(const TR &b) : TR(b) {}
1055 ///\brief \ref named-func-param "Named parameter" for setting
1056 ///the processed map.
1058 ///\ref named-templ-param "Named parameter" function for setting
1059 ///the map that indicates which nodes are processed.
1061 DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1063 Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1064 return DfsWizard<SetProcessedMapBase<T> >(*this);
1068 struct SetPathBase : public Base {
1070 SetPathBase(const TR &b) : TR(b) {}
1072 ///\brief \ref named-func-param "Named parameter"
1073 ///for getting the DFS path to the target node.
1075 ///\ref named-func-param "Named parameter"
1076 ///for getting the DFS path to the target node.
1078 DfsWizard<SetPathBase<T> > path(const T &t)
1080 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1081 return DfsWizard<SetPathBase<T> >(*this);
1084 ///\brief \ref named-func-param "Named parameter"
1085 ///for getting the distance of the target node.
1087 ///\ref named-func-param "Named parameter"
1088 ///for getting the distance of the target node.
1089 DfsWizard dist(const int &d)
1091 Base::_di=const_cast<int*>(&d);
1097 ///Function-type interface for DFS algorithm.
1100 ///Function-type interface for DFS algorithm.
1102 ///This function also has several \ref named-func-param "named parameters",
1103 ///they are declared as the members of class \ref DfsWizard.
1104 ///The following examples show how to use these parameters.
1106 /// // Compute the DFS tree
1107 /// dfs(g).predMap(preds).distMap(dists).run(s);
1109 /// // Compute the DFS path from s to t
1110 /// bool reached = dfs(g).path(p).dist(d).run(s,t);
1112 ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
1113 ///to the end of the parameter list.
1117 DfsWizard<DfsWizardBase<GR> >
1118 dfs(const GR &digraph)
1120 return DfsWizard<DfsWizardBase<GR> >(digraph);
1124 /// \brief Visitor class for DFS.
1126 /// This class defines the interface of the DfsVisit events, and
1127 /// it could be the base of a real visitor class.
1128 template <typename GR>
1131 typedef typename Digraph::Arc Arc;
1132 typedef typename Digraph::Node Node;
1133 /// \brief Called for the source node of the DFS.
1135 /// This function is called for the source node of the DFS.
1136 void start(const Node& node) {}
1137 /// \brief Called when the source node is leaved.
1139 /// This function is called when the source node is leaved.
1140 void stop(const Node& node) {}
1141 /// \brief Called when a node is reached first time.
1143 /// This function is called when a node is reached first time.
1144 void reach(const Node& node) {}
1145 /// \brief Called when an arc reaches a new node.
1147 /// This function is called when the DFS finds an arc whose target node
1148 /// is not reached yet.
1149 void discover(const Arc& arc) {}
1150 /// \brief Called when an arc is examined but its target node is
1151 /// already discovered.
1153 /// This function is called when an arc is examined but its target node is
1154 /// already discovered.
1155 void examine(const Arc& arc) {}
1156 /// \brief Called when the DFS steps back from a node.
1158 /// This function is called when the DFS steps back from a node.
1159 void leave(const Node& node) {}
1160 /// \brief Called when the DFS steps back on an arc.
1162 /// This function is called when the DFS steps back on an arc.
1163 void backtrack(const Arc& arc) {}
1166 template <typename GR>
1169 typedef typename Digraph::Arc Arc;
1170 typedef typename Digraph::Node Node;
1171 void start(const Node&) {}
1172 void stop(const Node&) {}
1173 void reach(const Node&) {}
1174 void discover(const Arc&) {}
1175 void examine(const Arc&) {}
1176 void leave(const Node&) {}
1177 void backtrack(const Arc&) {}
1179 template <typename _Visitor>
1180 struct Constraints {
1181 void constraints() {
1184 visitor.start(node);
1186 visitor.reach(node);
1187 visitor.discover(arc);
1188 visitor.examine(arc);
1189 visitor.leave(node);
1190 visitor.backtrack(arc);
1197 /// \brief Default traits class of DfsVisit class.
1199 /// Default traits class of DfsVisit class.
1200 /// \tparam _Digraph The type of the digraph the algorithm runs on.
1202 struct DfsVisitDefaultTraits {
1204 /// \brief The type of the digraph the algorithm runs on.
1207 /// \brief The type of the map that indicates which nodes are reached.
1209 /// The type of the map that indicates which nodes are reached.
1210 /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1211 typedef typename Digraph::template NodeMap<bool> ReachedMap;
1213 /// \brief Instantiates a ReachedMap.
1215 /// This function instantiates a ReachedMap.
1216 /// \param digraph is the digraph, to which
1217 /// we would like to define the ReachedMap.
1218 static ReachedMap *createReachedMap(const Digraph &digraph) {
1219 return new ReachedMap(digraph);
1226 /// \brief DFS algorithm class with visitor interface.
1228 /// This class provides an efficient implementation of the DFS algorithm
1229 /// with visitor interface.
1231 /// The DfsVisit class provides an alternative interface to the Dfs
1232 /// class. It works with callback mechanism, the DfsVisit object calls
1233 /// the member functions of the \c Visitor class on every DFS event.
1235 /// This interface of the DFS algorithm should be used in special cases
1236 /// when extra actions have to be performed in connection with certain
1237 /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1240 /// \tparam GR The type of the digraph the algorithm runs on.
1241 /// The default type is \ref ListDigraph.
1242 /// The value of GR is not used directly by \ref DfsVisit,
1243 /// it is only passed to \ref DfsVisitDefaultTraits.
1244 /// \tparam VS The Visitor type that is used by the algorithm.
1245 /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
1246 /// does not observe the DFS events. If you want to observe the DFS
1247 /// events, you should implement your own visitor class.
1248 /// \tparam TR The traits class that defines various types used by the
1249 /// algorithm. By default, it is \ref DfsVisitDefaultTraits
1250 /// "DfsVisitDefaultTraits<GR>".
1251 /// In most cases, this parameter should not be set directly,
1252 /// consider to use the named template parameters instead.
1254 template <typename GR, typename VS, typename TR>
1256 template <typename GR = ListDigraph,
1257 typename VS = DfsVisitor<GR>,
1258 typename TR = DfsVisitDefaultTraits<GR> >
1263 ///The traits class.
1266 ///The type of the digraph the algorithm runs on.
1267 typedef typename Traits::Digraph Digraph;
1269 ///The visitor type used by the algorithm.
1272 ///The type of the map that indicates which nodes are reached.
1273 typedef typename Traits::ReachedMap ReachedMap;
1277 typedef typename Digraph::Node Node;
1278 typedef typename Digraph::NodeIt NodeIt;
1279 typedef typename Digraph::Arc Arc;
1280 typedef typename Digraph::OutArcIt OutArcIt;
1282 //Pointer to the underlying digraph.
1283 const Digraph *_digraph;
1284 //Pointer to the visitor object.
1286 //Pointer to the map of reached status of the nodes.
1287 ReachedMap *_reached;
1288 //Indicates if _reached is locally allocated (true) or not.
1291 std::vector<typename Digraph::Arc> _stack;
1294 //Creates the maps if necessary.
1295 void create_maps() {
1297 local_reached = true;
1298 _reached = Traits::createReachedMap(*_digraph);
1308 typedef DfsVisit Create;
1310 /// \name Named Template Parameters
1314 struct SetReachedMapTraits : public Traits {
1315 typedef T ReachedMap;
1316 static ReachedMap *createReachedMap(const Digraph &digraph) {
1317 LEMON_ASSERT(false, "ReachedMap is not initialized");
1318 return 0; // ignore warnings
1321 /// \brief \ref named-templ-param "Named parameter" for setting
1322 /// ReachedMap type.
1324 /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1326 struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1327 SetReachedMapTraits<T> > {
1328 typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1334 /// \brief Constructor.
1338 /// \param digraph The digraph the algorithm runs on.
1339 /// \param visitor The visitor object of the algorithm.
1340 DfsVisit(const Digraph& digraph, Visitor& visitor)
1341 : _digraph(&digraph), _visitor(&visitor),
1342 _reached(0), local_reached(false) {}
1344 /// \brief Destructor.
1346 if(local_reached) delete _reached;
1349 /// \brief Sets the map that indicates which nodes are reached.
1351 /// Sets the map that indicates which nodes are reached.
1352 /// If you don't use this function before calling \ref run(Node) "run()"
1353 /// or \ref init(), an instance will be allocated automatically.
1354 /// The destructor deallocates this automatically allocated map,
1356 /// \return <tt> (*this) </tt>
1357 DfsVisit &reachedMap(ReachedMap &m) {
1360 local_reached=false;
1368 /// \name Execution Control
1369 /// The simplest way to execute the DFS algorithm is to use one of the
1370 /// member functions called \ref run(Node) "run()".\n
1371 /// If you need better control on the execution, you have to call
1372 /// \ref init() first, then you can add a source node with \ref addSource()
1373 /// and perform the actual computation with \ref start().
1374 /// This procedure can be repeated if there are nodes that have not
1379 /// \brief Initializes the internal data structures.
1381 /// Initializes the internal data structures.
1384 _stack.resize(countNodes(*_digraph));
1386 for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1387 _reached->set(u, false);
1391 /// \brief Adds a new source node.
1393 /// Adds a new source node to the set of nodes to be processed.
1395 /// \pre The stack must be empty. Otherwise the algorithm gives
1396 /// wrong results. (One of the outgoing arcs of all the source nodes
1397 /// except for the last one will not be visited and distances will
1399 void addSource(Node s)
1401 LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1402 if(!(*_reached)[s]) {
1403 _reached->set(s,true);
1407 _digraph->firstOut(e, s);
1409 _stack[++_stack_head] = e;
1417 /// \brief Processes the next arc.
1419 /// Processes the next arc.
1421 /// \return The processed arc.
1423 /// \pre The stack must not be empty.
1424 Arc processNextArc() {
1425 Arc e = _stack[_stack_head];
1426 Node m = _digraph->target(e);
1427 if(!(*_reached)[m]) {
1428 _visitor->discover(e);
1430 _reached->set(m, true);
1431 _digraph->firstOut(_stack[++_stack_head], m);
1433 _visitor->examine(e);
1434 m = _digraph->source(e);
1435 _digraph->nextOut(_stack[_stack_head]);
1437 while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1440 if (_stack_head >= 0) {
1441 _visitor->backtrack(_stack[_stack_head]);
1442 m = _digraph->source(_stack[_stack_head]);
1443 _digraph->nextOut(_stack[_stack_head]);
1451 /// \brief Next arc to be processed.
1453 /// Next arc to be processed.
1455 /// \return The next arc to be processed or INVALID if the stack is
1457 Arc nextArc() const {
1458 return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1461 /// \brief Returns \c false if there are nodes
1462 /// to be processed.
1464 /// Returns \c false if there are nodes
1465 /// to be processed in the queue (stack).
1466 bool emptyQueue() const { return _stack_head < 0; }
1468 /// \brief Returns the number of the nodes to be processed.
1470 /// Returns the number of the nodes to be processed in the queue (stack).
1471 int queueSize() const { return _stack_head + 1; }
1473 /// \brief Executes the algorithm.
1475 /// Executes the algorithm.
1477 /// This method runs the %DFS algorithm from the root node
1478 /// in order to compute the %DFS path to each node.
1480 /// The algorithm computes
1481 /// - the %DFS tree,
1482 /// - the distance of each node from the root in the %DFS tree.
1484 /// \pre init() must be called and a root node should be
1485 /// added with addSource() before using this function.
1487 /// \note <tt>d.start()</tt> is just a shortcut of the following code.
1489 /// while ( !d.emptyQueue() ) {
1490 /// d.processNextArc();
1494 while ( !emptyQueue() ) processNextArc();
1497 /// \brief Executes the algorithm until the given target node is reached.
1499 /// Executes the algorithm until the given target node is reached.
1501 /// This method runs the %DFS algorithm from the root node
1502 /// in order to compute the DFS path to \c t.
1504 /// The algorithm computes
1505 /// - the %DFS path to \c t,
1506 /// - the distance of \c t from the root in the %DFS tree.
1508 /// \pre init() must be called and a root node should be added
1509 /// with addSource() before using this function.
1510 void start(Node t) {
1511 while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
1515 /// \brief Executes the algorithm until a condition is met.
1517 /// Executes the algorithm until a condition is met.
1519 /// This method runs the %DFS algorithm from the root node
1520 /// until an arc \c a with <tt>am[a]</tt> true is found.
1522 /// \param am A \c bool (or convertible) arc map. The algorithm
1523 /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
1525 /// \return The reached arc \c a with <tt>am[a]</tt> true or
1526 /// \c INVALID if no such arc was found.
1528 /// \pre init() must be called and a root node should be added
1529 /// with addSource() before using this function.
1531 /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1533 template <typename AM>
1534 Arc start(const AM &am) {
1535 while ( !emptyQueue() && !am[_stack[_stack_head]] )
1537 return emptyQueue() ? INVALID : _stack[_stack_head];
1540 /// \brief Runs the algorithm from the given source node.
1542 /// This method runs the %DFS algorithm from node \c s.
1543 /// in order to compute the DFS path to each node.
1545 /// The algorithm computes
1546 /// - the %DFS tree,
1547 /// - the distance of each node from the root in the %DFS tree.
1549 /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1561 /// \brief Finds the %DFS path between \c s and \c t.
1563 /// This method runs the %DFS algorithm from node \c s
1564 /// in order to compute the DFS path to node \c t
1565 /// (it stops searching when \c t is processed).
1567 /// \return \c true if \c t is reachable form \c s.
1569 /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1570 /// just a shortcut of the following code.
1576 bool run(Node s,Node t) {
1583 /// \brief Runs the algorithm to visit all nodes in the digraph.
1585 /// This method runs the %DFS algorithm in order to visit all nodes
1588 /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1591 /// for (NodeIt n(digraph); n != INVALID; ++n) {
1592 /// if (!d.reached(n)) {
1600 for (NodeIt it(*_digraph); it != INVALID; ++it) {
1610 /// \name Query Functions
1611 /// The results of the DFS algorithm can be obtained using these
1613 /// Either \ref run(Node) "run()" or \ref start() should be called
1614 /// before using them.
1618 /// \brief Checks if the given node is reached from the root(s).
1620 /// Returns \c true if \c v is reached from the root(s).
1622 /// \pre Either \ref run(Node) "run()" or \ref init()
1623 /// must be called before using this function.
1624 bool reached(Node v) const { return (*_reached)[v]; }
1630 } //END OF NAMESPACE LEMON