sl@0
|
1 |
//=======================================================================
|
sl@0
|
2 |
// Copyright 2001 University of Notre Dame.
|
sl@0
|
3 |
// Author: Jeremy G. Siek
|
sl@0
|
4 |
//
|
sl@0
|
5 |
// Distributed under the Boost Software License, Version 1.0. (See
|
sl@0
|
6 |
// accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
7 |
// http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
8 |
//=======================================================================
|
sl@0
|
9 |
/*
|
sl@0
|
10 |
* © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
|
sl@0
|
11 |
*/
|
sl@0
|
12 |
|
sl@0
|
13 |
#include <boost/config.hpp>
|
sl@0
|
14 |
#include <boost/test/minimal.hpp>
|
sl@0
|
15 |
#include <stdlib.h>
|
sl@0
|
16 |
|
sl@0
|
17 |
#include <boost/graph/depth_first_search.hpp>
|
sl@0
|
18 |
#include <boost/graph/adjacency_list.hpp>
|
sl@0
|
19 |
#include <boost/graph/graph_archetypes.hpp>
|
sl@0
|
20 |
#include <boost/graph/graph_utility.hpp>
|
sl@0
|
21 |
#include <boost/graph/random.hpp>
|
sl@0
|
22 |
|
sl@0
|
23 |
#include <boost/random/mersenne_twister.hpp>
|
sl@0
|
24 |
#ifdef __SYMBIAN32__
|
sl@0
|
25 |
#include "std_log_result.h"
|
sl@0
|
26 |
#define LOG_FILENAME_LINE __FILE__, __LINE__
|
sl@0
|
27 |
#endif
|
sl@0
|
28 |
template <typename ColorMap, typename ParentMap,
|
sl@0
|
29 |
typename DiscoverTimeMap, typename FinishTimeMap>
|
sl@0
|
30 |
class dfs_test_visitor {
|
sl@0
|
31 |
typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
|
sl@0
|
32 |
typedef typename boost::color_traits<ColorValue> Color;
|
sl@0
|
33 |
public:
|
sl@0
|
34 |
dfs_test_visitor(ColorMap color, ParentMap p, DiscoverTimeMap d,
|
sl@0
|
35 |
FinishTimeMap f)
|
sl@0
|
36 |
: m_color(color), m_parent(p),
|
sl@0
|
37 |
m_discover_time(d), m_finish_time(f), m_time(0) { }
|
sl@0
|
38 |
|
sl@0
|
39 |
template <class Vertex, class Graph>
|
sl@0
|
40 |
void initialize_vertex(Vertex u, Graph& g) {
|
sl@0
|
41 |
BOOST_CHECK( boost::get(m_color, u) == Color::white() );
|
sl@0
|
42 |
}
|
sl@0
|
43 |
template <class Vertex, class Graph>
|
sl@0
|
44 |
void start_vertex(Vertex u, Graph& g) {
|
sl@0
|
45 |
BOOST_CHECK( boost::get(m_color, u) == Color::white() );
|
sl@0
|
46 |
}
|
sl@0
|
47 |
template <class Vertex, class Graph>
|
sl@0
|
48 |
void discover_vertex(Vertex u, Graph& g) {
|
sl@0
|
49 |
using namespace boost;
|
sl@0
|
50 |
BOOST_CHECK( get(m_color, u) == Color::gray() );
|
sl@0
|
51 |
BOOST_CHECK( get(m_color, get(m_parent, u)) == Color::gray() );
|
sl@0
|
52 |
|
sl@0
|
53 |
put(m_discover_time, u, m_time++);
|
sl@0
|
54 |
}
|
sl@0
|
55 |
template <class Edge, class Graph>
|
sl@0
|
56 |
void examine_edge(Edge e, Graph& g) {
|
sl@0
|
57 |
using namespace boost;
|
sl@0
|
58 |
BOOST_CHECK( get(m_color, source(e, g)) == Color::gray() );
|
sl@0
|
59 |
}
|
sl@0
|
60 |
template <class Edge, class Graph>
|
sl@0
|
61 |
void tree_edge(Edge e, Graph& g) {
|
sl@0
|
62 |
using namespace boost;
|
sl@0
|
63 |
BOOST_CHECK( get(m_color, target(e, g)) == Color::white() );
|
sl@0
|
64 |
|
sl@0
|
65 |
put(m_parent, target(e, g), source(e, g));
|
sl@0
|
66 |
}
|
sl@0
|
67 |
template <class Edge, class Graph>
|
sl@0
|
68 |
void back_edge(Edge e, Graph& g) {
|
sl@0
|
69 |
using namespace boost;
|
sl@0
|
70 |
BOOST_CHECK( get(m_color, target(e, g)) == Color::gray() );
|
sl@0
|
71 |
}
|
sl@0
|
72 |
template <class Edge, class Graph>
|
sl@0
|
73 |
void forward_or_cross_edge(Edge e, Graph& g) {
|
sl@0
|
74 |
using namespace boost;
|
sl@0
|
75 |
BOOST_CHECK( get(m_color, target(e, g)) == Color::black() );
|
sl@0
|
76 |
}
|
sl@0
|
77 |
template <class Vertex, class Graph>
|
sl@0
|
78 |
void finish_vertex(Vertex u, Graph& g) {
|
sl@0
|
79 |
using namespace boost;
|
sl@0
|
80 |
BOOST_CHECK( get(m_color, u) == Color::black() );
|
sl@0
|
81 |
|
sl@0
|
82 |
put(m_finish_time, u, m_time++);
|
sl@0
|
83 |
}
|
sl@0
|
84 |
private:
|
sl@0
|
85 |
ColorMap m_color;
|
sl@0
|
86 |
ParentMap m_parent;
|
sl@0
|
87 |
DiscoverTimeMap m_discover_time;
|
sl@0
|
88 |
FinishTimeMap m_finish_time;
|
sl@0
|
89 |
typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
|
sl@0
|
90 |
};
|
sl@0
|
91 |
|
sl@0
|
92 |
template <typename Graph>
|
sl@0
|
93 |
struct dfs_test
|
sl@0
|
94 |
{
|
sl@0
|
95 |
typedef boost::graph_traits<Graph> Traits;
|
sl@0
|
96 |
typedef typename Traits::vertices_size_type
|
sl@0
|
97 |
vertices_size_type;
|
sl@0
|
98 |
|
sl@0
|
99 |
static void go(vertices_size_type max_V) {
|
sl@0
|
100 |
using namespace boost;
|
sl@0
|
101 |
typedef typename Traits::vertex_descriptor vertex_descriptor;
|
sl@0
|
102 |
typedef typename boost::property_map<Graph,
|
sl@0
|
103 |
boost::vertex_color_t>::type ColorMap;
|
sl@0
|
104 |
typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
|
sl@0
|
105 |
typedef typename boost::color_traits<ColorValue> Color;
|
sl@0
|
106 |
|
sl@0
|
107 |
vertices_size_type i, k;
|
sl@0
|
108 |
typename Traits::edges_size_type j;
|
sl@0
|
109 |
typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
|
sl@0
|
110 |
|
sl@0
|
111 |
boost::mt19937 gen;
|
sl@0
|
112 |
|
sl@0
|
113 |
for (i = 0; i < max_V; ++i)
|
sl@0
|
114 |
for (j = 0; j < i*i; ++j) {
|
sl@0
|
115 |
Graph g;
|
sl@0
|
116 |
generate_random_graph(g, i, j, gen);
|
sl@0
|
117 |
|
sl@0
|
118 |
ColorMap color = get(boost::vertex_color, g);
|
sl@0
|
119 |
std::vector<vertex_descriptor> parent(num_vertices(g));
|
sl@0
|
120 |
for (k = 0; k < num_vertices(g); ++k)
|
sl@0
|
121 |
parent[k] = k;
|
sl@0
|
122 |
std::vector<int> discover_time(num_vertices(g)),
|
sl@0
|
123 |
finish_time(num_vertices(g));
|
sl@0
|
124 |
|
sl@0
|
125 |
dfs_test_visitor<ColorMap, vertex_descriptor*,
|
sl@0
|
126 |
int*, int*> vis(color, &parent[0],
|
sl@0
|
127 |
&discover_time[0], &finish_time[0]);
|
sl@0
|
128 |
|
sl@0
|
129 |
boost::depth_first_search(g, visitor(vis).color_map(color));
|
sl@0
|
130 |
|
sl@0
|
131 |
// all vertices should be black
|
sl@0
|
132 |
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
|
sl@0
|
133 |
BOOST_CHECK(get(color, *vi) == Color::black());
|
sl@0
|
134 |
|
sl@0
|
135 |
// check parenthesis structure of discover/finish times
|
sl@0
|
136 |
// See CLR p.480
|
sl@0
|
137 |
for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
|
sl@0
|
138 |
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
|
sl@0
|
139 |
vertex_descriptor u = *ui, v = *vi;
|
sl@0
|
140 |
if (u != v) {
|
sl@0
|
141 |
BOOST_CHECK( finish_time[u] < discover_time[v]
|
sl@0
|
142 |
|| finish_time[v] < discover_time[u]
|
sl@0
|
143 |
|| (discover_time[v] < discover_time[u]
|
sl@0
|
144 |
&& finish_time[u] < finish_time[v]
|
sl@0
|
145 |
&& boost::is_descendant(u, v, &parent[0]))
|
sl@0
|
146 |
|| (discover_time[u] < discover_time[v]
|
sl@0
|
147 |
&& finish_time[v] < finish_time[u]
|
sl@0
|
148 |
&& boost::is_descendant(v, u, &parent[0]))
|
sl@0
|
149 |
);
|
sl@0
|
150 |
}
|
sl@0
|
151 |
}
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
}
|
sl@0
|
155 |
};
|
sl@0
|
156 |
|
sl@0
|
157 |
|
sl@0
|
158 |
// usage: dfs.exe [max-vertices=15]
|
sl@0
|
159 |
|
sl@0
|
160 |
int test_main(int argc, char* argv[])
|
sl@0
|
161 |
{
|
sl@0
|
162 |
int max_V = 7;
|
sl@0
|
163 |
if (argc > 1)
|
sl@0
|
164 |
max_V = atoi(argv[1]);
|
sl@0
|
165 |
|
sl@0
|
166 |
// Test directed graphs.
|
sl@0
|
167 |
dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
|
sl@0
|
168 |
boost::property<boost::vertex_color_t, boost::default_color_type> >
|
sl@0
|
169 |
>::go(max_V);
|
sl@0
|
170 |
// Test undirected graphs.
|
sl@0
|
171 |
dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
|
sl@0
|
172 |
boost::property<boost::vertex_color_t, boost::default_color_type> >
|
sl@0
|
173 |
>::go(max_V);
|
sl@0
|
174 |
|
sl@0
|
175 |
#ifdef __SYMBIAN32__
|
sl@0
|
176 |
std_log(LOG_FILENAME_LINE,"[End Test Case ]");
|
sl@0
|
177 |
|
sl@0
|
178 |
testResultXml("dfs");
|
sl@0
|
179 |
close_log_file();
|
sl@0
|
180 |
#endif
|
sl@0
|
181 |
return 0;
|
sl@0
|
182 |
}
|
sl@0
|
183 |
|