undirected_dfs
Performs a depth-first traversal of the vertices in an undirected graph.
Complexity: O(E + V)
Defined in: <boost/graph/undirected_dfs.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_dfs.hpp>
#include <iostream>
struct VertexProps { int id; };
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProps>;
using Edge = boost::graph_traits<Graph>::edge_descriptor;
struct Visitor : boost::default_dfs_visitor {
void discover_vertex(Graph::vertex_descriptor v, const Graph& g) const {
std::cout << "discover " << g[v].id << "\n";
}
void finish_vertex(Graph::vertex_descriptor v, const Graph& g) const {
std::cout << "finish " << g[v].id << "\n";
}
};
int main() {
Graph g{4};
for (int i = 0; i < 4; ++i) { g[i].id = i; }
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(1, 3, g);
using ColorMap = std::map<Graph::vertex_descriptor, boost::default_color_type>;
using EdgeColorMap = std::map<Edge, boost::default_color_type>;
ColorMap vcmap;
EdgeColorMap ecmap;
boost::undirected_dfs(g, Visitor{},
boost::make_assoc_property_map(vcmap),
boost::make_assoc_property_map(ecmap));
}
discover 0
discover 1
discover 3
finish 3
finish 1
discover 2
finish 2
finish 0
(1) Positional version
template <typename Graph,
typename DFSVisitor,
typename VertexColorMap,
typename EdgeColorMap>
void undirected_dfs(const Graph& g, DFSVisitor vis,
VertexColorMap vertex_color, EdgeColorMap edge_color);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Incidence Graph, Vertex List Graph, and Edge List Graph. |
IN |
|
A visitor object that is invoked inside the algorithm at the event-points specified by the DFS Visitor concept. The visitor object is passed by value [1]. |
UTIL/OUT |
|
This is used by the algorithm to keep track of its progress through the
graph. The type |
UTIL |
|
This is used by the algorithm to keep track of which edges have been
visited. The type |
(2) With explicit start vertex
template <typename Graph,
typename DFSVisitor,
typename VertexColorMap,
typename EdgeColorMap>
void undirected_dfs(const Graph& g, DFSVisitor vis,
VertexColorMap vertex_color, EdgeColorMap edge_color,
typename graph_traits<Graph>::vertex_descriptor start);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Incidence Graph, Vertex List Graph, and Edge List Graph. |
IN |
|
A visitor object that is invoked inside the algorithm at the event-points specified by the DFS Visitor concept. The visitor object is passed by value [1]. |
UTIL/OUT |
|
This is used by the algorithm to keep track of its progress through the
graph. The type |
UTIL |
|
This is used by the algorithm to keep track of which edges have been
visited. The type |
IN |
|
The vertex that the depth-first search should originate from. |
(3) Named parameter version
template <typename Graph,
typename P,
typename T,
typename R>
void undirected_dfs(Graph& G,
const bgl_named_params<P, T, R>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Incidence Graph, Vertex List Graph, and Edge List Graph. |
IN |
|
A visitor object that is invoked inside the algorithm at the
event-points specified by the DFS Visitor concept.
The visitor object is passed by value [1]. |
UTIL/OUT |
|
This is used by the algorithm to keep track of its progress through the
graph. The type |
UTIL |
|
This is used by the algorithm to keep track of which edges have been
visited. The type |
IN |
|
This specifies the vertex that the depth-first search should originate
from. The type is the type of a vertex descriptor for the given graph. |
IN |
|
This maps each vertex to an integer in the range |
Description
The undirected_dfs() function performs a depth-first traversal of
the vertices in an undirected graph. When possible, a depth-first
traversal chooses a vertex adjacent to the current vertex to visit next.
If all adjacent vertices have already been discovered, or there are no
adjacent vertices, then the algorithm backtracks to the last vertex that
had undiscovered neighbors. Once all reachable vertices have been
visited, the algorithm selects from any remaining undiscovered vertices
and continues the traversal. The algorithm finishes when all vertices
have been visited. Depth-first search is useful for categorizing edges
in a graph, and for imposing an ordering on the vertices. Section
Depth-First Search
describes the various properties of DFS and walks through an example.
Similar to BFS, color markers are used to keep track of which vertices have been discovered. White marks vertices that have yet to be discovered, gray marks a vertex that is discovered but still has vertices adjacent to it that are undiscovered. A black vertex is discovered vertex that is not adjacent to any white vertices.
Edges are also colored during the search to disambiguate tree and back edges.
The undirected_dfs() function invokes user-defined actions at
certain event-points within the algorithm. This provides a mechanism for
adapting the generic DFS algorithm to the many situations in which it
can be used. In the pseudo-code below, the event points for DFS are
the labels on the right. The user-defined actions must be provided in
the form of a visitor object, that is, an object whose type meets the
requirements for a DFS Visitor. In the
pseudo-code we show the algorithm computing predecessors p, discover
time d and finish time t. By default, the
undirected_dfs() function does not compute these
properties, however there are pre-defined visitors such as
predecessor_recorder and
time_stamper that can be used to do
this.
| Algorithm | Event Points |
|---|---|
|
|
Visitor Event Points
-
vis.initialize_vertex(s, g)is invoked on every vertex of the graph before the start of the graph search. -
vis.start_vertex(s, g)is invoked on the source vertex once before the start of the search. -
vis.discover_vertex(u, g)is invoked when a vertex is encountered for the first time. -
vis.examine_edge(e, g)is invoked on every out-edge of each vertex after it is discovered. -
vis.tree_edge(e, g)is invoked on each edge as it becomes a member of the edges that form the search tree. If you wish to record predecessors, do so at this event point. -
vis.back_edge(e, g)is invoked on the back edges in the graph. -
vis.finish_edge(e, g)is invoked on the back edges in the graph as well as on each tree edge after its target vertex is finished. -
vis.finish_vertex(u, g)is invoked on a vertex after all of its out edges have been added to the search tree and all of the adjacent vertices have been discovered (but before their out-edges have been examined).