1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
//=======================================================================
// Copyright 2001 University of Notre Dame.
// Author: Andrew Janiszewski, Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/graph_archetypes.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/random/mersenne_twister.hpp>
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using namespace boost;
#endif
template < typename DistanceMap, typename ParentMap, typename Graph,
typename ColorMap >
class bfs_testing_visitor
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
typedef typename boost::graph_traits< Graph >::edge_descriptor Edge;
typedef typename boost::color_traits<
typename boost::property_traits< ColorMap >::value_type >
Color;
public:
bfs_testing_visitor(Vertex s, DistanceMap d, ParentMap p, ColorMap c)
: current_distance(0), distance(d), parent(p), color(c), src(s)
{
}
void initialize_vertex(const Vertex& u, const Graph&) const
{
BOOST_TEST(get(color, u) == Color::white());
}
void examine_vertex(const Vertex& u, const Graph&) const
{
current_vertex = u;
// Ensure that the distances monotonically increase.
BOOST_TEST(distance[u] == current_distance
|| distance[u] == current_distance + 1);
if (distance[u] == current_distance + 1) // new level
++current_distance;
}
void discover_vertex(const Vertex& u, const Graph&) const
{
BOOST_TEST(get(color, u) == Color::gray());
if (u == src)
{
current_vertex = src;
}
else
{
BOOST_TEST(parent[u] == current_vertex);
BOOST_TEST(distance[u] == current_distance + 1);
BOOST_TEST(distance[u] == distance[parent[u]] + 1);
}
}
void examine_edge(const Edge& e, const Graph& g) const
{
BOOST_TEST(source(e, g) == current_vertex);
}
void tree_edge(const Edge& e, const Graph& g) const
{
BOOST_TEST(get(color, target(e, g)) == Color::white());
Vertex u = source(e, g), v = target(e, g);
BOOST_TEST(distance[u] == current_distance);
parent[v] = u;
distance[v] = distance[u] + 1;
}
void non_tree_edge(const Edge& e, const Graph& g) const
{
BOOST_TEST(color[target(e, g)] != Color::white());
if (boost::is_directed(g))
// cross or back edge
BOOST_TEST(distance[target(e, g)] <= distance[source(e, g)] + 1);
else
{
// cross edge (or going backwards on a tree edge)
BOOST_TEST(distance[target(e, g)] == distance[source(e, g)]
|| distance[target(e, g)] == distance[source(e, g)] + 1
|| distance[target(e, g)] == distance[source(e, g)] - 1);
}
}
void gray_target(const Edge& e, const Graph& g) const
{
BOOST_TEST(color[target(e, g)] == Color::gray());
}
void black_target(const Edge& e, const Graph& g) const
{
BOOST_TEST(color[target(e, g)] == Color::black());
// All vertices adjacent to a black vertex must already be discovered
typename boost::graph_traits< Graph >::adjacency_iterator ai, ai_end;
for (boost::tie(ai, ai_end) = adjacent_vertices(target(e, g), g);
ai != ai_end; ++ai)
BOOST_TEST(color[*ai] != Color::white());
}
void finish_vertex(const Vertex& u, const Graph&) const
{
BOOST_TEST(color[u] == Color::black());
}
private:
mutable Vertex current_vertex;
mutable typename boost::property_traits< DistanceMap >::value_type
current_distance;
DistanceMap distance;
ParentMap parent;
ColorMap color;
Vertex src;
};
template < class Graph > struct bfs_test
{
typedef boost::graph_traits< Graph > Traits;
typedef typename Traits::vertices_size_type vertices_size_type;
static void go(vertices_size_type max_V)
{
typedef typename Traits::vertex_descriptor vertex_descriptor;
typedef boost::color_traits< boost::default_color_type > Color;
vertices_size_type i;
typename Traits::edges_size_type j;
typename Traits::vertex_iterator ui, ui_end;
boost::mt19937 gen;
for (i = 0; i < max_V; ++i)
for (j = 0; j < i * i; ++j)
{
Graph g;
boost::generate_random_graph(g, i, j, gen);
// declare the "start" variable
vertex_descriptor start = boost::random_vertex(g, gen);
// vertex properties
std::vector< int > distance(
i, (std::numeric_limits< int >::max)());
distance[start] = 0;
std::vector< vertex_descriptor > parent(i);
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
parent[*ui] = *ui;
std::vector< boost::default_color_type > color(i);
// Get vertex index map
typedef typename boost::property_map< Graph,
boost::vertex_index_t >::const_type idx_type;
idx_type idx = get(boost::vertex_index, g);
// Make property maps from vectors
typedef boost::iterator_property_map<
std::vector< int >::iterator, idx_type >
distance_pm_type;
distance_pm_type distance_pm(distance.begin(), idx);
typedef boost::iterator_property_map<
typename std::vector< vertex_descriptor >::iterator,
idx_type >
parent_pm_type;
parent_pm_type parent_pm(parent.begin(), idx);
typedef boost::iterator_property_map<
std::vector< boost::default_color_type >::iterator,
idx_type >
color_pm_type;
color_pm_type color_pm(color.begin(), idx);
// Create the testing visitor.
bfs_testing_visitor< distance_pm_type, parent_pm_type, Graph,
color_pm_type >
vis(start, distance_pm, parent_pm, color_pm);
boost::breadth_first_search(
g, start, visitor(vis).color_map(color_pm));
// All white vertices should be unreachable from the source.
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
if (color[*ui] == Color::white())
{
std::vector< boost::default_color_type > color2(
i, Color::white());
BOOST_TEST(!boost::is_reachable(
start, *ui, g, color_pm_type(color2.begin(), idx)));
}
// The shortest path to a child should be one longer than
// shortest path to the parent.
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
if (parent[*ui] != *ui) // *ui not the root of the bfs tree
BOOST_TEST(distance[*ui] == distance[parent[*ui]] + 1);
}
}
};
int main(int argc, char* argv[])
{
using namespace boost;
int max_V = 7;
if (argc > 1)
max_V = atoi(argv[1]);
bfs_test< adjacency_list< vecS, vecS, directedS > >::go(max_V);
bfs_test< adjacency_list< vecS, vecS, undirectedS > >::go(max_V);
return boost::report_errors();
}
|