Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/test/csr_graph_test.cpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 14.2 KB
Line 
1// Copyright 2005 The Trustees of Indiana University.
2
3// Use, modification and distribution is subject to the Boost Software
4// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7//  Authors: Jeremiah Willcock
8//           Douglas Gregor
9//           Andrew Lumsdaine
10
11// The libstdc++ debug mode makes this test run for hours...
12#ifdef _GLIBCXX_DEBUG
13#  undef _GLIBCXX_DEBUG
14#endif
15
16// Test for the compressed sparse row graph type
17#include <boost/graph/compressed_sparse_row_graph.hpp>
18#include <boost/test/minimal.hpp>
19#include <boost/graph/adjacency_list.hpp>
20#include <boost/graph/erdos_renyi_generator.hpp>
21#include <boost/graph/graph_utility.hpp>
22#include <boost/random/linear_congruential.hpp>
23#include <cassert>
24#include <iostream>
25#include <vector>
26#include <algorithm>
27#include <ctime>
28#include <boost/lexical_cast.hpp>
29#include <boost/iterator/transform_iterator.hpp>
30#include <boost/limits.hpp>
31#include <string>
32#include <boost/graph/iteration_macros.hpp>
33
34// Algorithms to test against
35#include <boost/graph/betweenness_centrality.hpp>
36#include <boost/graph/kruskal_min_spanning_tree.hpp>
37
38typedef boost::adjacency_list<> GraphT;
39typedef boost::erdos_renyi_iterator<boost::minstd_rand, GraphT> ERGen;
40
41typedef boost::compressed_sparse_row_graph<> CSRGraphT;
42
43template <class G1, class VI1, class G2, class VI2, class IsomorphismMap>
44void assert_graphs_equal(const G1& g1, const VI1& vi1,
45                         const G2& g2, const VI2& vi2,
46                         const IsomorphismMap& iso) {
47  using boost::out_degree;
48
49  BOOST_CHECK (num_vertices(g1) == num_vertices(g2));
50  BOOST_CHECK (num_edges(g1) == num_edges(g2));
51
52  typedef typename boost::graph_traits<G1>::vertex_iterator vertiter1;
53  {
54    vertiter1 i, iend;
55    for (boost::tie(i, iend) = vertices(g1); i != iend; ++i) {
56      typename boost::graph_traits<G1>::vertex_descriptor v1 = *i;
57      typename boost::graph_traits<G2>::vertex_descriptor v2 = iso[v1];
58
59      BOOST_CHECK (vi1[v1] == vi2[v2]);
60
61      BOOST_CHECK (out_degree(v1, g1) == out_degree(v2, g2));
62      std::vector<std::size_t> edges1(out_degree(v1, g1));
63      typename boost::graph_traits<G1>::out_edge_iterator oe1, oe1end;
64      for (boost::tie(oe1, oe1end) = out_edges(v1, g1); oe1 != oe1end; ++oe1) {
65        BOOST_CHECK (source(*oe1, g1) == v1);
66        edges1.push_back(vi1[target(*oe1, g1)]);
67      }
68      std::vector<std::size_t> edges2(out_degree(v2, g2));
69      typename boost::graph_traits<G2>::out_edge_iterator oe2, oe2end;
70      for (boost::tie(oe2, oe2end) = out_edges(v2, g2); oe2 != oe2end; ++oe2) {
71        BOOST_CHECK (source(*oe2, g2) == v2);
72        edges2.push_back(vi2[target(*oe2, g2)]);
73      }
74
75      std::sort(edges1.begin(), edges1.end());
76      std::sort(edges2.begin(), edges2.end());
77      BOOST_CHECK (edges1 == edges2);
78    }
79  }
80
81  {
82    std::vector<std::pair<std::size_t, std::size_t> > all_edges1;
83    std::vector<std::pair<std::size_t, std::size_t> > all_edges2;
84    typename boost::graph_traits<G1>::edge_iterator ei1, ei1end;
85    for (boost::tie(ei1, ei1end) = edges(g1); ei1 != ei1end; ++ei1)
86      all_edges1.push_back(std::make_pair(vi1[source(*ei1, g1)],
87                                          vi1[target(*ei1, g1)]));
88    typename boost::graph_traits<G2>::edge_iterator ei2, ei2end;
89    for (boost::tie(ei2, ei2end) = edges(g2); ei2 != ei2end; ++ei2)
90      all_edges2.push_back(std::make_pair(vi2[source(*ei2, g2)],
91                                          vi2[target(*ei2, g2)]));
92    std::sort(all_edges1.begin(), all_edges1.end());
93    std::sort(all_edges2.begin(), all_edges2.end());
94    BOOST_CHECK (all_edges1 == all_edges2);
95  }
96}
97
98template<typename GraphT, typename VertexIndexMap>
99class edge_to_index_pair
100{
101  typedef typename boost::graph_traits<GraphT>::vertices_size_type
102    vertices_size_type;
103  typedef typename boost::graph_traits<GraphT>::edge_descriptor edge_descriptor;
104
105 public:
106  typedef std::pair<vertices_size_type, vertices_size_type> result_type;
107
108  edge_to_index_pair() : g(0), index() { }
109  edge_to_index_pair(const GraphT& g, const VertexIndexMap& index)
110    : g(&g), index(index)
111  { }
112
113  result_type operator()(edge_descriptor e) const
114  {
115    return result_type(get(index, source(e, *g)), get(index, target(e, *g)));
116  }
117
118 private:
119  const GraphT* g;
120  VertexIndexMap index;
121};
122
123template<typename GraphT, typename VertexIndexMap>
124edge_to_index_pair<GraphT, VertexIndexMap>
125make_edge_to_index_pair(const GraphT& g, const VertexIndexMap& index)
126{
127  return edge_to_index_pair<GraphT, VertexIndexMap>(g, index);
128}
129
130template<typename GraphT>
131edge_to_index_pair
132  <GraphT,
133   typename boost::property_map<GraphT,boost::vertex_index_t>::const_type>
134make_edge_to_index_pair(const GraphT& g)
135{
136  typedef typename boost::property_map<GraphT,
137                                       boost::vertex_index_t>::const_type
138    VertexIndexMap;
139  return edge_to_index_pair<GraphT, VertexIndexMap>(g,
140                                                   get(boost::vertex_index,
141                                                       g));
142}
143
144void check_consistency(const CSRGraphT& g) {
145  // Do a bunch of tests on the graph internal data
146  // Check that m_last_source is valid
147  BOOST_CHECK(g.m_last_source <= num_vertices(g));
148  // Check that m_rowstart entries are valid, and that entries after
149  // m_last_source + 1 are all zero
150  BOOST_CHECK(g.m_rowstart[0] == 0);
151  for (CSRGraphT::vertices_size_type i = 0; i < g.m_last_source; ++i) {
152    BOOST_CHECK(g.m_rowstart[i + 1] >= g.m_rowstart[i]);
153    BOOST_CHECK(g.m_rowstart[i + 1] <= num_edges(g));
154  }
155  for (CSRGraphT::vertices_size_type i = g.m_last_source + 1;
156       i < g.m_rowstart.size(); ++i) {
157    BOOST_CHECK(g.m_rowstart[i] == 0);
158  }
159  // Check that m_column entries are within range
160  for (CSRGraphT::edges_size_type i = 0; i < num_edges(g); ++i) {
161    BOOST_CHECK(g.m_column[i] < num_vertices(g));
162  }
163}
164
165template<typename OrigGraph>
166void test(const OrigGraph& g)
167{
168  // Check copying of a graph
169  CSRGraphT g2(g);
170  check_consistency(g2);
171  BOOST_CHECK((std::size_t)std::distance(edges(g2).first, edges(g2).second)
172              == num_edges(g2));
173  assert_graphs_equal(g, boost::identity_property_map(),
174                      g2, boost::identity_property_map(),
175                      boost::identity_property_map());
176
177  // Check constructing a graph from iterators
178  CSRGraphT g3(boost::make_transform_iterator(edges(g2).first,
179                                             make_edge_to_index_pair(g2)),
180              boost::make_transform_iterator(edges(g2).second,
181                                             make_edge_to_index_pair(g2)),
182              num_vertices(g));
183  check_consistency(g3);
184  BOOST_CHECK((std::size_t)std::distance(edges(g3).first, edges(g3).second)
185              == num_edges(g3));
186  assert_graphs_equal(g2, boost::identity_property_map(),
187                      g3, boost::identity_property_map(),
188                      boost::identity_property_map());
189
190  // Check constructing a graph using add_edge and add_vertices
191  CSRGraphT g4;
192  BOOST_CHECK(num_vertices(g4) == 0);
193  std::size_t first_vert = add_vertices(num_vertices(g3), g4);
194  BOOST_CHECK(first_vert == 0);
195  BOOST_CHECK(num_vertices(g4) == num_vertices(g3));
196  CSRGraphT::edge_iterator ei, ei_end;
197  int i;
198  for (boost::tie(ei, ei_end) = edges(g3), i = 0; ei != ei_end; ++ei, ++i) {
199    CSRGraphT::edge_descriptor e = add_edge(source(*ei, g3), target(*ei, g3), g4);
200    BOOST_CHECK(source(e, g4) == source(*ei, g3));
201    BOOST_CHECK(target(e, g4) == target(*ei, g3));
202    if (i % 13 == 0) check_consistency(g4);
203  }
204  assert_graphs_equal(g3, boost::identity_property_map(),
205                      g4, boost::identity_property_map(),
206                      boost::identity_property_map());
207
208  // Check edge_from_index (and implicitly the edge_index property map) for
209  // each edge in g2
210  // This test also checks for the correct sorting of the edge iteration
211  std::size_t last_src = 0, last_tgt = 0;
212  for (boost::tie(ei, ei_end) = edges(g2); ei != ei_end; ++ei) {
213    BOOST_CHECK(edge_from_index(get(boost::edge_index, g2, *ei), g2) == *ei);
214    std::size_t src = get(boost::vertex_index, g2, source(*ei, g2));
215    std::size_t tgt = get(boost::vertex_index, g2, target(*ei, g2));
216    BOOST_CHECK(src > last_src || (src == last_src && tgt >= last_tgt));
217    last_src = src;
218    last_tgt = tgt;
219  }
220
221  // Check out edge iteration and vertex iteration for sortedness
222  // Also, check a few runs of edge and edge_range
223  CSRGraphT::vertex_iterator vi, vi_end;
224  std::size_t last_vertex = 0;
225  bool first_iter = true;
226  for (boost::tie(vi, vi_end) = vertices(g2); vi != vi_end; ++vi) {
227    std::size_t v = get(boost::vertex_index, g2, *vi);
228    BOOST_CHECK(first_iter || v > last_vertex);
229    last_vertex = v;
230    first_iter = false;
231
232    CSRGraphT::out_edge_iterator oei, oei_end;
233    std::size_t last_tgt = 0;
234    for (boost::tie(oei, oei_end) = out_edges(*vi, g2); oei != oei_end; ++oei) {
235      BOOST_CHECK(source(*oei, g2) == *vi);
236      CSRGraphT::vertex_descriptor tgtd = target(*oei, g2);
237      std::size_t tgt = get(boost::vertex_index, g2, tgtd);
238      BOOST_CHECK(tgt >= last_tgt);
239      last_tgt = tgt;
240
241      std::pair<CSRGraphT::edge_descriptor, bool> edge_info = edge(*vi, tgtd, g2);
242      BOOST_CHECK(edge_info.second == true);
243      BOOST_CHECK(source(edge_info.first, g2) == *vi);
244      BOOST_CHECK(target(edge_info.first, g2) == tgtd);
245      std::pair<CSRGraphT::out_edge_iterator, CSRGraphT::out_edge_iterator> er =
246        edge_range(*vi, tgtd, g2);
247      BOOST_CHECK(er.first != er.second);
248      for (; er.first != er.second; ++er.first) {
249        BOOST_CHECK(source(*er.first, g2) == *vi);
250        BOOST_CHECK(target(*er.first, g2) == tgtd);
251      }
252    }
253
254    // Find a vertex for testing
255    CSRGraphT::vertex_descriptor test_vertex = vertex(num_vertices(g2) / 2, g2);
256    int edge_count = 0;
257    CSRGraphT::out_edge_iterator oei2, oei2_end;
258    for (boost::tie(oei2, oei_end) = out_edges(*vi, g2); oei2 != oei_end; ++oei2) {
259      if (target(*oei2, g2) == test_vertex)
260        ++edge_count;
261    }
262
263    // Test edge and edge_range on an edge that may not be present
264    std::pair<CSRGraphT::edge_descriptor, bool> edge_info = 
265      edge(*vi, test_vertex, g2);
266    BOOST_CHECK(edge_info.second == (edge_count != 0));
267    std::pair<CSRGraphT::out_edge_iterator, CSRGraphT::out_edge_iterator> er =
268      edge_range(*vi, test_vertex, g2);
269    BOOST_CHECK(er.second - er.first == edge_count);
270  }
271
272  // Run brandes_betweenness_centrality, which touches on a whole lot
273  // of things, including VertexListGraph and IncidenceGraph
274  using namespace boost;
275  std::vector<double> vertex_centralities(num_vertices(g3));
276  std::vector<double> edge_centralities(num_edges(g3));
277  brandes_betweenness_centrality
278    (g3,
279     make_iterator_property_map(vertex_centralities.begin(),
280                                get(boost::vertex_index, g3)),
281     make_iterator_property_map(edge_centralities.begin(),
282                                get(boost::edge_index, g3)));
283    // Extra qualifications for aCC
284
285  // Invert the edge centralities and use these as weights to
286  // Kruskal's MST algorithm, which will test the EdgeListGraph
287  // capabilities.
288  double max_val = (std::numeric_limits<double>::max)();
289  for (std::size_t i = 0; i < num_edges(g3); ++i)
290    edge_centralities[i] =
291      edge_centralities[i] == 0.0? max_val : 1.0 / edge_centralities[i];
292
293  typedef boost::graph_traits<CSRGraphT>::edge_descriptor edge_descriptor;
294  std::vector<edge_descriptor> mst_edges;
295  mst_edges.reserve(num_vertices(g3));
296  kruskal_minimum_spanning_tree
297    (g3, std::back_inserter(mst_edges),
298     weight_map(make_iterator_property_map(edge_centralities.begin(),
299                                           get(boost::edge_index, g3))));
300}
301
302void test(int nnodes, double density, int seed)
303{
304  boost::minstd_rand gen(seed);
305  std::cout << "Testing " << nnodes << " density " << density << std::endl;
306  GraphT g(ERGen(gen, nnodes, density), ERGen(), nnodes);
307  test(g);
308}
309
310void test_graph_properties()
311{
312  using namespace boost;
313
314  typedef compressed_sparse_row_graph<directedS,
315                                      no_property,
316                                      no_property,
317                                      property<graph_name_t, std::string> >
318    CSRGraphT;
319
320  CSRGraphT g;
321  BOOST_CHECK(get_property(g, graph_name) == "");
322  set_property(g, graph_name, "beep");
323  BOOST_CHECK(get_property(g, graph_name) == "beep");
324}
325
326struct Vertex
327{
328  double centrality;
329};
330
331struct Edge
332{
333  Edge(double weight) : weight(weight), centrality(0.0) { }
334
335  double weight;
336  double centrality;
337};
338
339void test_vertex_and_edge_properties()
340{
341  using namespace boost;
342  typedef compressed_sparse_row_graph<directedS, Vertex, Edge>
343    CSRGraphWithPropsT;
344
345  typedef std::pair<int, int> E;
346  E edges_init[6] = { E(0, 1), E(0, 3), E(1, 2), E(3, 1), E(3, 4), E(4, 2) };
347  double weights[6] = { 1.0, 1.0, 0.5, 1.0, 1.0, 0.5 };
348  double centrality[5] = { 0.0, 1.5, 0.0, 1.0, 0.5 };
349
350  CSRGraphWithPropsT g(&edges_init[0], &edges_init[0] + 6, &weights[0], 5, 6);
351  brandes_betweenness_centrality
352    (g,
353     centrality_map(get(&Vertex::centrality, g)).
354     weight_map(get(&Edge::weight, g)).
355     edge_centrality_map(get(&Edge::centrality, g)));
356
357  BGL_FORALL_VERTICES(v, g, CSRGraphWithPropsT)
358    BOOST_CHECK(g[v].centrality == centrality[v]);
359}
360
361int test_main(int argc, char* argv[])
362{
363  // Optionally accept a seed value
364  int seed = std::time(0);
365  if (argc > 1) seed = boost::lexical_cast<int>(argv[1]);
366
367  std::cout << "Seed = " << seed << std::endl;
368  {
369    std::cout << "Testing empty graph" << std::endl;
370    CSRGraphT g;
371    test(g);
372  }
373  //  test(1000, 0.05, seed);
374  //  test(1000, 0.0, seed);
375  //  test(1000, 0.1, seed);
376  test(1000, 0.001, seed);
377  test(1000, 0.0005, seed);
378  {
379    std::cout << "Testing partially constructed CSR graph" << std::endl;
380    CSRGraphT g;
381    add_vertices(std::size_t(5), g);
382    add_edge(std::size_t(1), std::size_t(2), g);
383    check_consistency(g);
384    add_edge(std::size_t(2), std::size_t(3), g);
385    check_consistency(g);
386    add_edge(std::size_t(2), std::size_t(4), g);
387    check_consistency(g);
388    CSRGraphT::edge_iterator ei, ei_end;
389    for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
390      BOOST_CHECK(edge_from_index(get(boost::edge_index, g, *ei), g) == *ei);
391    }
392    test(g);
393  }
394
395  test_graph_properties();
396  test_vertex_and_edge_properties();
397
398  return 0;
399}
Note: See TracBrowser for help on using the repository browser.