csr_graph_test.cpp 18 KB

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