DfsVisit
Declared in lemon/dfs.h, in the group Graph Search; the same class on the Doxygen site.
-
template< typename GR , typename VS , typename TR >
class DfsVisit DFS algorithm class with visitor interface.
This class provides an efficient implementation of the DFS algorithm with visitor interface.
The DfsVisit class provides an alternative interface to the Dfs class. It works with callback mechanism, the DfsVisit object calls the member functions of the
Visitorclass on every DFS event.This interface of the DFS algorithm should be used in special cases when extra actions have to be performed in connection with certain events of the DFS algorithm. Otherwise consider to use Dfs or dfs() instead.
- Template Parameters:
GR – The type of the digraph the algorithm runs on. The default type is ListDigraph. The value of GR is not used directly by DfsVisit, it is only passed to DfsVisitDefaultTraits.
VS – The Visitor type that is used by the algorithm. DfsVisitor< GR > is an empty visitor, which does not observe the DFS events. If you want to observe the DFS events, you should implement your own visitor class.
TR – The traits class that defines various types used by the algorithm. By default, it is DfsVisitDefaultTraits< GR >. In most cases, this parameter should not be set directly, consider to use the named template parameters instead.
Execution Control
The simplest way to execute the DFS algorithm is to use one of the member functions called run()
.
If you need better control on the execution, you have to call
init() first, then you can add a source node with addSource() and perform the actual computation with start(). This procedure can be repeated if there are nodes that have not been reached.-
inline void init()
Initializes the internal data structures.
-
inline void addSource( Node s )
Adds a new source node.
Adds a new source node to the set of nodes to be processed.
- Pre:
The stack must be empty. Otherwise the algorithm gives wrong results. (One of the outgoing arcs of all the source nodes except for the last one will not be visited and distances will also be wrong.)
-
inline Arc processNextArc()
Processes the next arc.
- Returns:
The processed arc.
- Pre:
The stack must not be empty.
-
inline Arc nextArc() const
Next arc to be processed.
- Returns:
The next arc to be processed or INVALID if the stack is empty.
-
inline bool emptyQueue() const
Returns
falseif there are nodes to be processed.Returns
falseif there are nodes to be processed in the queue (stack).
-
inline int queueSize() const
Returns the number of the nodes to be processed.
Returns the number of the nodes to be processed in the queue (stack).
-
inline void start()
Executes the algorithm.
This method runs the DFS algorithm from the root node in order to compute the DFS path to each node.
The algorithm computes
the DFS tree,
the distance of each node from the root in the DFS tree.
Note
d.start()is just a shortcut of the following code.while( ! d.emptyQueue() ) { d.processNextArc(); }
- Pre:
init() must be called and a root node should be added with addSource() before using this function.
-
inline void start( Node t )
Executes the algorithm until the given target node is reached.
This method runs the DFS algorithm from the root node in order to compute the DFS path to
t.The algorithm computes
the DFS path to
t,the distance of
tfrom the root in the DFS tree.
- Pre:
init() must be called and a root node should be added with addSource() before using this function.
-
template< typename AM >
inline Arc start( const AM & am ) Executes the algorithm until a condition is met.
This method runs the DFS algorithm from the root node until an arc
awitham[a]true is found.- Parameters:
am – A
bool(or convertible) arc map. The algorithm will stop when it reaches an arcawitham[a]true.- Returns:
The reached arc
awitham[a]true orINVALIDif no such arc was found.- Pre:
init() must be called and a root node should be added with addSource() before using this function.
-
inline void run( Node s )
Runs the algorithm from the given source node.
This method runs the DFS algorithm from node
s. in order to compute the DFS path to each node.The algorithm computes
the DFS tree,
the distance of each node from the root in the DFS tree.
Note
d.run(s)is just a shortcut of the following code.d.init(); d.addSource( s ); d.start();
-
inline bool run( Node s , Node t )
Finds the DFS path between
sandt.This method runs the DFS algorithm from node
sin order to compute the DFS path to nodet(it stops searching whentis reached).Note
Apart from the return value,
d.run(s,t)is just a shortcut of the following code.d.init(); d.addSource( s ); d.start( t );
- Returns:
trueiftis reachable forms.
-
inline void run()
Runs the algorithm to visit all nodes in the digraph.
This method runs the DFS algorithm in order to visit all nodes in the digraph.
Note
d.run()is just a shortcut of the following code.d.init(); for( NodeIt n( digraph ) ; n != INVALID ; ++n ) { if( ! d.reached( n ) ) { d.addSource( n ); d.start(); } }
Query Functions
The results of the DFS algorithm can be obtained using these functions.
Either
run() or start() should be called before using them.Public Types
Public Functions
-
inline DfsVisit( const Digraph & digraph , Visitor & visitor )
Constructor.
- Parameters:
digraph – The digraph the algorithm runs on.
visitor – The visitor object of the algorithm.
-
inline ~DfsVisit()
Destructor.
-
inline DfsVisit & reachedMap( ReachedMap & m )
Sets the map that indicates which nodes are reached. If you don’t use this function before calling run() or init(), an instance will be allocated automatically. The destructor deallocates this automatically allocated map, of course.
- Returns:
(*this)
Protected Functions
-
inline DfsVisit()
-
template< class T >
struct SetReachedMapTraits : public Traits -
Public Static Functions
-
static inline ReachedMap * createReachedMap( const Digraph & )
-
static inline ReachedMap * createReachedMap( const Digraph & )
-
template< class T >
struct SetReachedMap : public DfsVisit< Digraph , Visitor , SetReachedMapTraits< T > > Named parameter for setting ReachedMap type.