1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
12 // DFS categorized directed graph
21 // forward or cross: 2 --> 3
23 // BFS categorized directed graph
34 #include <boost/config.hpp>
41 #include <boost/graph/visitors.hpp>
42 #include <boost/graph/graph_utility.hpp>
43 #include <boost/graph/adjacency_list.hpp>
44 #include <boost/graph/breadth_first_search.hpp>
45 #include <boost/graph/depth_first_search.hpp>
47 using namespace boost
;
50 template < class Tag
>
51 struct edge_printer
: public base_visitor
< edge_printer
< Tag
> >
53 typedef Tag event_filter
;
54 edge_printer(std::string edge_t
) : m_edge_type(edge_t
) {}
55 template < class Edge
, class Graph
> void operator()(Edge e
, Graph
& G
)
57 std::cout
<< m_edge_type
<< ": " << source(e
, G
) << " --> "
58 << target(e
, G
) << std::endl
;
60 std::string m_edge_type
;
62 template < class Tag
> edge_printer
< Tag
> print_edge(std::string type
, Tag
)
64 return edge_printer
< Tag
>(type
);
67 int main(int, char*[])
70 using namespace boost
;
72 typedef adjacency_list
<> Graph
;
73 typedef std::pair
< int, int > E
;
74 E edges
[] = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1), E(3, 4),
76 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
78 for (std::size_t j
= 0; j
< sizeof(edges
) / sizeof(E
); ++j
)
79 add_edge(edges
[j
].first
, edges
[j
].second
, G
);
81 Graph
G(edges
, edges
+ sizeof(edges
) / sizeof(E
), 5);
84 typedef boost::graph_traits
< Graph
>::vertices_size_type size_type
;
86 std::vector
< size_type
> d(num_vertices(G
));
87 std::vector
< size_type
> f(num_vertices(G
));
89 cout
<< "DFS categorized directed graph" << endl
;
91 visitor(make_dfs_visitor(make_list(print_edge("tree", on_tree_edge()),
92 print_edge("back", on_back_edge()),
93 print_edge("forward or cross", on_forward_or_cross_edge())))));
95 cout
<< endl
<< "BFS categorized directed graph" << endl
;
96 boost::breadth_first_search(G
, vertex(0, G
),
98 make_bfs_visitor(std::make_pair(print_edge("tree", on_tree_edge()),
99 print_edge("cycle", on_non_tree_edge())))));