[29] | 1 | //======================================================================= |
---|
| 2 | // Copyright 2001 University of Notre Dame. |
---|
| 3 | // Author: Jeremy G. Siek |
---|
| 4 | // |
---|
| 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 | //======================================================================= |
---|
| 9 | |
---|
| 10 | #include <boost/config.hpp> |
---|
| 11 | #include <boost/test/minimal.hpp> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | |
---|
| 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> |
---|
| 19 | |
---|
| 20 | #include <boost/random/mersenne_twister.hpp> |
---|
| 21 | |
---|
| 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; |
---|
| 27 | public: |
---|
| 28 | dfs_test_visitor(ColorMap color, ParentMap p, DiscoverTimeMap d, |
---|
| 29 | FinishTimeMap f) |
---|
| 30 | : m_color(color), m_parent(p), |
---|
| 31 | m_discover_time(d), m_finish_time(f), m_time(0) { } |
---|
| 32 | |
---|
| 33 | template <class Vertex, class Graph> |
---|
| 34 | void initialize_vertex(Vertex u, Graph& g) { |
---|
| 35 | BOOST_CHECK( boost::get(m_color, u) == Color::white() ); |
---|
| 36 | } |
---|
| 37 | template <class Vertex, class Graph> |
---|
| 38 | void start_vertex(Vertex u, Graph& g) { |
---|
| 39 | BOOST_CHECK( boost::get(m_color, u) == Color::white() ); |
---|
| 40 | } |
---|
| 41 | template <class Vertex, class Graph> |
---|
| 42 | void discover_vertex(Vertex u, Graph& g) { |
---|
| 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() ); |
---|
| 46 | |
---|
| 47 | put(m_discover_time, u, m_time++); |
---|
| 48 | } |
---|
| 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() ); |
---|
| 53 | } |
---|
| 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() ); |
---|
| 58 | |
---|
| 59 | put(m_parent, target(e, g), source(e, g)); |
---|
| 60 | } |
---|
| 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() ); |
---|
| 65 | } |
---|
| 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() ); |
---|
| 70 | } |
---|
| 71 | template <class Vertex, class Graph> |
---|
| 72 | void finish_vertex(Vertex u, Graph& g) { |
---|
| 73 | using namespace boost; |
---|
| 74 | BOOST_CHECK( get(m_color, u) == Color::black() ); |
---|
| 75 | |
---|
| 76 | put(m_finish_time, u, m_time++); |
---|
| 77 | } |
---|
| 78 | private: |
---|
| 79 | ColorMap m_color; |
---|
| 80 | ParentMap m_parent; |
---|
| 81 | DiscoverTimeMap m_discover_time; |
---|
| 82 | FinishTimeMap m_finish_time; |
---|
| 83 | typename boost::property_traits<DiscoverTimeMap>::value_type m_time; |
---|
| 84 | }; |
---|
| 85 | |
---|
| 86 | template <typename Graph> |
---|
| 87 | struct dfs_test |
---|
| 88 | { |
---|
| 89 | typedef boost::graph_traits<Graph> Traits; |
---|
| 90 | typedef typename Traits::vertices_size_type |
---|
| 91 | vertices_size_type; |
---|
| 92 | |
---|
| 93 | static void go(vertices_size_type max_V) { |
---|
| 94 | using namespace boost; |
---|
| 95 | typedef typename Traits::vertex_descriptor vertex_descriptor; |
---|
| 96 | typedef typename boost::property_map<Graph, |
---|
| 97 | boost::vertex_color_t>::type ColorMap; |
---|
| 98 | typedef typename boost::property_traits<ColorMap>::value_type ColorValue; |
---|
| 99 | typedef typename boost::color_traits<ColorValue> Color; |
---|
| 100 | |
---|
| 101 | vertices_size_type i, k; |
---|
| 102 | typename Traits::edges_size_type j; |
---|
| 103 | typename Traits::vertex_iterator vi, vi_end, ui, ui_end; |
---|
| 104 | |
---|
| 105 | boost::mt19937 gen; |
---|
| 106 | |
---|
| 107 | for (i = 0; i < max_V; ++i) |
---|
| 108 | for (j = 0; j < i*i; ++j) { |
---|
| 109 | Graph g; |
---|
| 110 | generate_random_graph(g, i, j, gen); |
---|
| 111 | |
---|
| 112 | ColorMap color = get(boost::vertex_color, g); |
---|
| 113 | std::vector<vertex_descriptor> parent(num_vertices(g)); |
---|
| 114 | for (k = 0; k < num_vertices(g); ++k) |
---|
| 115 | parent[k] = k; |
---|
| 116 | std::vector<int> discover_time(num_vertices(g)), |
---|
| 117 | finish_time(num_vertices(g)); |
---|
| 118 | |
---|
| 119 | dfs_test_visitor<ColorMap, vertex_descriptor*, |
---|
| 120 | int*, int*> vis(color, &parent[0], |
---|
| 121 | &discover_time[0], &finish_time[0]); |
---|
| 122 | |
---|
| 123 | boost::depth_first_search(g, visitor(vis).color_map(color)); |
---|
| 124 | |
---|
| 125 | // all vertices should be black |
---|
| 126 | for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) |
---|
| 127 | BOOST_CHECK(get(color, *vi) == Color::black()); |
---|
| 128 | |
---|
| 129 | // check parenthesis structure of discover/finish times |
---|
| 130 | // See CLR p.480 |
---|
| 131 | for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) |
---|
| 132 | for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { |
---|
| 133 | vertex_descriptor u = *ui, v = *vi; |
---|
| 134 | if (u != v) { |
---|
| 135 | BOOST_CHECK( finish_time[u] < discover_time[v] |
---|
| 136 | || finish_time[v] < discover_time[u] |
---|
| 137 | || (discover_time[v] < discover_time[u] |
---|
| 138 | && finish_time[u] < finish_time[v] |
---|
| 139 | && boost::is_descendant(u, v, &parent[0])) |
---|
| 140 | || (discover_time[u] < discover_time[v] |
---|
| 141 | && finish_time[v] < finish_time[u] |
---|
| 142 | && boost::is_descendant(v, u, &parent[0])) |
---|
| 143 | ); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | // usage: dfs.exe [max-vertices=15] |
---|
| 153 | |
---|
| 154 | int test_main(int argc, char* argv[]) |
---|
| 155 | { |
---|
| 156 | int max_V = 7; |
---|
| 157 | if (argc > 1) |
---|
| 158 | max_V = atoi(argv[1]); |
---|
| 159 | |
---|
| 160 | // Test directed graphs. |
---|
| 161 | dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, |
---|
| 162 | boost::property<boost::vertex_color_t, boost::default_color_type> > |
---|
| 163 | >::go(max_V); |
---|
| 164 | // Test undirected graphs. |
---|
| 165 | dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, |
---|
| 166 | boost::property<boost::vertex_color_t, boost::default_color_type> > |
---|
| 167 | >::go(max_V); |
---|
| 168 | |
---|
| 169 | return 0; |
---|
| 170 | } |
---|
| 171 | |
---|