| 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/undirected_dfs.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&) {
|
|---|
| 35 | BOOST_CHECK( boost::get(m_color, u) == Color::white() );
|
|---|
| 36 | }
|
|---|
| 37 | template <class Vertex, class Graph>
|
|---|
| 38 | void start_vertex(Vertex u, Graph&) {
|
|---|
| 39 | BOOST_CHECK( boost::get(m_color, u) == Color::white() );
|
|---|
| 40 | }
|
|---|
| 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() );
|
|---|
| 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 Edge, class Graph>
|
|---|
| 72 | void finish_edge(Edge e, Graph& g) {
|
|---|
| 73 | using namespace boost;
|
|---|
| 74 | BOOST_CHECK(
|
|---|
| 75 | (get(m_color, target(e, g)) == Color::gray())
|
|---|
| 76 | || (get(m_color, target(e, g)) == Color::black())
|
|---|
| 77 | );
|
|---|
| 78 | }
|
|---|
| 79 | template <class Vertex, class Graph>
|
|---|
| 80 | void finish_vertex(Vertex u, Graph&) {
|
|---|
| 81 | using namespace boost;
|
|---|
| 82 | BOOST_CHECK( get(m_color, u) == Color::black() );
|
|---|
| 83 |
|
|---|
| 84 | put(m_finish_time, u, m_time++);
|
|---|
| 85 | }
|
|---|
| 86 | private:
|
|---|
| 87 | ColorMap m_color;
|
|---|
| 88 | ParentMap m_parent;
|
|---|
| 89 | DiscoverTimeMap m_discover_time;
|
|---|
| 90 | FinishTimeMap m_finish_time;
|
|---|
| 91 | typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | template <typename Graph>
|
|---|
| 95 | struct dfs_test
|
|---|
| 96 | {
|
|---|
| 97 | typedef boost::graph_traits<Graph> Traits;
|
|---|
| 98 | typedef typename Traits::vertices_size_type
|
|---|
| 99 | vertices_size_type;
|
|---|
| 100 |
|
|---|
| 101 | static void go(vertices_size_type max_V) {
|
|---|
| 102 | using namespace boost;
|
|---|
| 103 | typedef typename Traits::vertex_descriptor vertex_descriptor;
|
|---|
| 104 | typedef typename boost::property_map<Graph,
|
|---|
| 105 | boost::vertex_color_t>::type ColorMap;
|
|---|
| 106 | typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
|
|---|
| 107 | typedef typename boost::color_traits<ColorValue> Color;
|
|---|
| 108 | typedef typename boost::property_map<Graph,
|
|---|
| 109 | boost::edge_color_t>::type EColorMap;
|
|---|
| 110 | typedef typename boost::property_traits<EColorMap>::value_type EColorValue;
|
|---|
| 111 | typedef typename boost::color_traits<EColorValue> EColor;
|
|---|
| 112 |
|
|---|
| 113 | vertices_size_type i, k;
|
|---|
| 114 | typename Traits::edges_size_type j;
|
|---|
| 115 | typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
|
|---|
| 116 | typename Traits::edge_iterator ei, ei_end;
|
|---|
| 117 |
|
|---|
| 118 | boost::mt19937 gen;
|
|---|
| 119 |
|
|---|
| 120 | for (i = 0; i < max_V; ++i)
|
|---|
| 121 | for (j = 0; j < i*i; ++j) {
|
|---|
| 122 | Graph g;
|
|---|
| 123 | generate_random_graph(g, i, j, gen);
|
|---|
| 124 |
|
|---|
| 125 | ColorMap color = get(boost::vertex_color, g);
|
|---|
| 126 | EColorMap e_color = get(boost::edge_color, g);
|
|---|
| 127 | std::vector<vertex_descriptor> parent(num_vertices(g));
|
|---|
| 128 | for (k = 0; k < num_vertices(g); ++k)
|
|---|
| 129 | parent[k] = k;
|
|---|
| 130 | std::vector<int> discover_time(num_vertices(g)),
|
|---|
| 131 | finish_time(num_vertices(g));
|
|---|
| 132 |
|
|---|
| 133 | dfs_test_visitor<ColorMap, vertex_descriptor*,
|
|---|
| 134 | int*, int*> vis(color, &parent[0],
|
|---|
| 135 | &discover_time[0], &finish_time[0]);
|
|---|
| 136 |
|
|---|
| 137 | boost::undirected_dfs(g, visitor(vis).color_map(color)
|
|---|
| 138 | .edge_color_map(e_color));
|
|---|
| 139 |
|
|---|
| 140 | // all vertices should be black
|
|---|
| 141 | for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
|
|---|
| 142 | BOOST_CHECK(get(color, *vi) == Color::black());
|
|---|
| 143 |
|
|---|
| 144 | // all edges should be black
|
|---|
| 145 | for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
|
|---|
| 146 | BOOST_CHECK(get(e_color, *ei) == EColor::black());
|
|---|
| 147 |
|
|---|
| 148 | // check parenthesis structure of discover/finish times
|
|---|
| 149 | // See CLR p.480
|
|---|
| 150 | for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
|
|---|
| 151 | for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
|
|---|
| 152 | vertex_descriptor u = *ui, v = *vi;
|
|---|
| 153 | if (u != v) {
|
|---|
| 154 | BOOST_CHECK( finish_time[u] < discover_time[v]
|
|---|
| 155 | || finish_time[v] < discover_time[u]
|
|---|
| 156 | || (discover_time[v] < discover_time[u]
|
|---|
| 157 | && finish_time[u] < finish_time[v]
|
|---|
| 158 | && boost::is_descendant(u, v, &parent[0]))
|
|---|
| 159 | || (discover_time[u] < discover_time[v]
|
|---|
| 160 | && finish_time[v] < finish_time[u]
|
|---|
| 161 | && boost::is_descendant(v, u, &parent[0]))
|
|---|
| 162 | );
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | }
|
|---|
| 168 | };
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | // usage: undirected_dfs.exe [max-vertices=15]
|
|---|
| 172 |
|
|---|
| 173 | int test_main(int argc, char* argv[])
|
|---|
| 174 | {
|
|---|
| 175 | int max_V = 7;
|
|---|
| 176 | if (argc > 1)
|
|---|
| 177 | max_V = atoi(argv[1]);
|
|---|
| 178 |
|
|---|
| 179 | // Test undirected graphs.
|
|---|
| 180 | dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
|
|---|
| 181 | boost::property<boost::vertex_color_t, boost::default_color_type>,
|
|---|
| 182 | boost::property<boost::edge_color_t, boost::default_color_type> >
|
|---|
| 183 | >::go(max_V);
|
|---|
| 184 |
|
|---|
| 185 | return 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|