cycle_test.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // (C) Copyright 2013 Louis Dionne
  2. //
  3. // Modified from `tiernan_all_cycles.cpp`.
  4. //
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0 (See accompanying file
  7. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GRAPH_TEST_CYCLE_TEST_HPP
  9. #define BOOST_GRAPH_TEST_CYCLE_TEST_HPP
  10. #include <boost/assert.hpp>
  11. #include <boost/graph/directed_graph.hpp>
  12. #include <boost/graph/erdos_renyi_generator.hpp>
  13. #include <boost/graph/graph_traits.hpp>
  14. #include <boost/graph/graph_utility.hpp>
  15. #include <boost/graph/undirected_graph.hpp>
  16. #include <boost/next_prior.hpp>
  17. #include <boost/random/linear_congruential.hpp>
  18. #include <cstddef>
  19. #include <iostream>
  20. namespace cycle_test_detail {
  21. using namespace boost;
  22. struct cycle_validator {
  23. explicit cycle_validator(std::size_t& number_of_cycles)
  24. : cycles(number_of_cycles)
  25. { }
  26. template <typename Path, typename Graph>
  27. void cycle(Path const& p, Graph const& g) {
  28. ++cycles;
  29. // Check to make sure that each of the vertices in the path
  30. // is truly connected and that the back is connected to the
  31. // front - it's not validating that we find all paths, just
  32. // that the paths are valid.
  33. typename Path::const_iterator i, j, last = prior(p.end());
  34. for (i = p.begin(); i != last; ++i) {
  35. j = boost::next(i);
  36. BOOST_ASSERT(edge(*i, *j, g).second);
  37. }
  38. BOOST_ASSERT(edge(p.back(), p.front(), g).second);
  39. }
  40. std::size_t& cycles;
  41. };
  42. template <typename Graph, typename Algorithm>
  43. void test_one(Algorithm algorithm) {
  44. typedef erdos_renyi_iterator<minstd_rand, Graph> er;
  45. // Generate random graphs with 15 vertices and 15% probability
  46. // of edge connection.
  47. static std::size_t const N = 20;
  48. static double const P = 0.1;
  49. minstd_rand rng;
  50. Graph g(er(rng, N, P), er(), N);
  51. renumber_indices(g);
  52. print_edges(g, get(vertex_index, g));
  53. std::size_t cycles = 0;
  54. cycle_validator vis(cycles);
  55. algorithm(g, vis);
  56. std::cout << "# cycles: " << vis.cycles << "\n";
  57. }
  58. } // end namespace cycle_test_detail
  59. template <typename Algorithm>
  60. void cycle_test(Algorithm const& algorithm) {
  61. std::cout << "*** undirected ***\n";
  62. cycle_test_detail::test_one<boost::undirected_graph<> >(algorithm);
  63. std::cout << "*** directed ***\n";
  64. cycle_test_detail::test_one<boost::directed_graph<> >(algorithm);
  65. }
  66. #endif // !BOOST_GRAPH_TEST_CYCLE_TEST_HPP