1 //=======================================================================
2 // Copyright 2001 University of Notre Dame.
3 // Author: 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 //=======================================================================
10 #include <boost/config.hpp>
11 #include <boost/test/minimal.hpp>
14 #include <boost/graph/depth_first_search.hpp>
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/graph/graph_archetypes.hpp>
17 #include <boost/graph/graph_utility.hpp>
18 #include <boost/graph/random.hpp>
20 #include <boost/random/mersenne_twister.hpp>
22 template <typename ColorMap, typename ParentMap,
23 typename DiscoverTimeMap, typename FinishTimeMap>
24 class dfs_test_visitor {
25 typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
26 typedef typename boost::color_traits<ColorValue> Color;
28 dfs_test_visitor(ColorMap color, ParentMap p, DiscoverTimeMap d,
30 : m_color(color), m_parent(p),
31 m_discover_time(d), m_finish_time(f), m_time(0) { }
33 template <class Vertex, class Graph>
34 void initialize_vertex(Vertex u, Graph&) {
35 BOOST_CHECK( boost::get(m_color, u) == Color::white() );
37 template <class Vertex, class Graph>
38 void start_vertex(Vertex u, Graph&) {
39 BOOST_CHECK( boost::get(m_color, u) == Color::white() );
41 template <class Vertex, class Graph>
42 void discover_vertex(Vertex u, Graph&) {
43 using namespace boost;
44 BOOST_CHECK( get(m_color, u) == Color::gray() );
45 BOOST_CHECK( get(m_color, get(m_parent, u)) == Color::gray() );
47 put(m_discover_time, u, m_time++);
49 template <class Edge, class Graph>
50 void examine_edge(Edge e, Graph& g) {
51 using namespace boost;
52 BOOST_CHECK( get(m_color, source(e, g)) == Color::gray() );
54 template <class Edge, class Graph>
55 void tree_edge(Edge e, Graph& g) {
56 using namespace boost;
57 BOOST_CHECK( get(m_color, target(e, g)) == Color::white() );
59 put(m_parent, target(e, g), source(e, g));
61 template <class Edge, class Graph>
62 void back_edge(Edge e, Graph& g) {
63 using namespace boost;
64 BOOST_CHECK( get(m_color, target(e, g)) == Color::gray() );
66 template <class Edge, class Graph>
67 void forward_or_cross_edge(Edge e, Graph& g) {
68 using namespace boost;
69 BOOST_CHECK( get(m_color, target(e, g)) == Color::black() );
71 template <class Edge, class Graph>
72 void finish_edge(Edge e, Graph& g) {
73 using namespace boost;
74 BOOST_CHECK( get(m_color, target(e, g)) == Color::gray() ||
75 get(m_color, target(e, g)) == Color::black() );
77 template <class Vertex, class Graph>
78 void finish_vertex(Vertex u, Graph&) {
79 using namespace boost;
80 BOOST_CHECK( get(m_color, u) == Color::black() );
82 put(m_finish_time, u, m_time++);
87 DiscoverTimeMap m_discover_time;
88 FinishTimeMap m_finish_time;
89 typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
92 template <typename Graph>
95 typedef boost::graph_traits<Graph> Traits;
96 typedef typename Traits::vertices_size_type
99 static void go(vertices_size_type max_V) {
100 using namespace boost;
101 typedef typename Traits::vertex_descriptor vertex_descriptor;
102 typedef typename boost::property_map<Graph,
103 boost::vertex_color_t>::type ColorMap;
104 typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
105 typedef typename boost::color_traits<ColorValue> Color;
107 vertices_size_type i, k;
108 typename Traits::edges_size_type j;
109 typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
113 for (i = 0; i < max_V; ++i)
114 for (j = 0; j < i*i; ++j) {
116 generate_random_graph(g, i, j, gen);
118 ColorMap color = get(boost::vertex_color, g);
119 std::vector<vertex_descriptor> parent(num_vertices(g));
120 for (k = 0; k < num_vertices(g); ++k)
122 std::vector<int> discover_time(num_vertices(g)),
123 finish_time(num_vertices(g));
125 // Get vertex index map
126 typedef typename boost::property_map<Graph, boost::vertex_index_t>::const_type idx_type;
127 idx_type idx = get(boost::vertex_index, g);
130 boost::iterator_property_map<typename std::vector<vertex_descriptor>::iterator, idx_type>
132 parent_pm_type parent_pm(parent.begin(), idx);
134 boost::iterator_property_map<std::vector<int>::iterator, idx_type>
136 time_pm_type discover_time_pm(discover_time.begin(), idx);
137 time_pm_type finish_time_pm(finish_time.begin(), idx);
139 dfs_test_visitor<ColorMap, parent_pm_type,
140 time_pm_type, time_pm_type>
141 vis(color, parent_pm,
142 discover_time_pm, finish_time_pm);
144 boost::depth_first_search(g, visitor(vis).color_map(color));
146 // all vertices should be black
147 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
148 BOOST_CHECK(get(color, *vi) == Color::black());
150 // check parenthesis structure of discover/finish times
152 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
153 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
154 vertex_descriptor u = *ui, v = *vi;
156 BOOST_CHECK( finish_time[u] < discover_time[v]
157 || finish_time[v] < discover_time[u]
158 || (discover_time[v] < discover_time[u]
159 && finish_time[u] < finish_time[v]
160 && boost::is_descendant(u, v, parent_pm))
161 || (discover_time[u] < discover_time[v]
162 && finish_time[v] < finish_time[u]
163 && boost::is_descendant(v, u, parent_pm))
173 // usage: dfs.exe [max-vertices=15]
175 int test_main(int argc, char* argv[])
179 max_V = atoi(argv[1]);
181 // Test directed graphs.
182 dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
183 boost::property<boost::vertex_color_t, boost::default_color_type> >
185 // Test undirected graphs.
186 dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
187 boost::property<boost::vertex_color_t, boost::default_color_type> >