namespace boost {
struct on_initialize_vertex { };
struct on_start_vertex { };
struct on_discover_vertex { };
struct on_examine_edge { };
struct on_tree_edge { };
struct on_cycle_edge { };
struct on_finish_vertex { };
struct on_forward_or_cross_edge { };
struct on_back_edge { };
struct on_edge_relaxed { };
struct on_edge_not_relaxed { };
struct on_edge_minimized { };
struct on_edge_not_minimized { };
} // namespace boost
Suppose we would like to print out the parenthesis structure of the discover/finish times of vertices in a depth-first search. We can use the BGL algorithm depth_first_search() and two event visitors to accomplish this. The complete source code for the following example is in examples/dfs_parenthesis.cpp. First we define the two event visitors. We use on_discover_vertex and on_finish_vertex as the event points, selected from the list of event points specified in DFSVisitor.
struct open_paren : public base_visitor<open_paren> {
typedef on_discover_vertex event_filter;
template <class Vertex, class Graph>
void operator()(Vertex v, Graph& G) {
std::cout << "(" << v;
}
};
struct close_paren : public base_visitor<close_paren> {
typedef on_finish_vertex event_filter;
template <class Vertex, class Graph>
void operator()(Vertex v, Graph& G) {
std::cout << v << ")";
}
};
Next we create two event visitor objects and make an EventVisitorList
out of them using a std::pair which created by
std::make_pair.
std::make_pair(open_paren(), close_paren())Next we want to pass this list into depth_first_search(), but depth_first_search() is expecting a DFSVisitor, not a EventVisitorList. We therefore use the dfs_visitor adaptor which turns an EventVisitor list into a DFSVisitor. Like all of the visitor adaptors, dfs_visitor has a creation function called make_dfs_visitor().
make_dfs_visitor(std::make_pair(open_paren(), close_paren()))Now we can pass the resulting visitor object into depth_first_search() as follows.
// graph object G is created ... std::vector<default_color_type> color(num_vertices(G)); depth_first_search(G, make_dfs_visitor(std::make_pair(open_paren(), close_paren())), color.begin());
| G | A type that is a model of Graph. |
| g | An object of type G. |
| V | A type that is a model of EventVisitor. |
| vis | An object of type V. |
| x | An object of type boost::graph_traits<G>::vertex_descriptor or boost::graph_traits<G>::edge_descriptor. |
| Event Filter | V::event_filter | A tag struct to specify on which event the visitor should be invoked. |
| Name | Expression | Return Type | Description |
|---|---|---|---|
| Apply Visitor | vis(x, g) | void | Invokes the visitor operation on object x, which is either a vertex or edge descriptor of the graph. |
| Copyright © 2000 |
Jeremy Siek,
Univ.of Notre Dame (jsiek@lsc.nd.edu) Lie-Quan Lee, Univ.of Notre Dame (llee1@lsc.nd.edu) Andrew Lumsdaine, Univ.of Notre Dame (lums@lsc.nd.edu) |