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 
9 #ifndef BOOST_GRAPH_TEST_CYCLE_TEST_HPP
10 #define BOOST_GRAPH_TEST_CYCLE_TEST_HPP
11 
12 #include <boost/assert.hpp>
13 #include <boost/graph/directed_graph.hpp>
14 #include <boost/graph/erdos_renyi_generator.hpp>
15 #include <boost/graph/graph_traits.hpp>
16 #include <boost/graph/graph_utility.hpp>
17 #include <boost/graph/undirected_graph.hpp>
18 #include <boost/next_prior.hpp>
19 #include <boost/random/linear_congruential.hpp>
20 #include <cstddef>
21 #include <iostream>
22 
23 
24 namespace cycle_test_detail {
25     using namespace boost;
26 
27     struct cycle_validator {
cycle_validatorcycle_test_detail::cycle_validator28         explicit cycle_validator(std::size_t& number_of_cycles)
29             : cycles(number_of_cycles)
30         { }
31 
32         template <typename Path, typename Graph>
cyclecycle_test_detail::cycle_validator33         void cycle(Path const& p, Graph const& g) {
34             ++cycles;
35             // Check to make sure that each of the vertices in the path
36             // is truly connected and that the back is connected to the
37             // front - it's not validating that we find all paths, just
38             // that the paths are valid.
39             typename Path::const_iterator i, j, last = prior(p.end());
40             for (i = p.begin(); i != last; ++i) {
41                 j = boost::next(i);
42                 BOOST_ASSERT(edge(*i, *j, g).second);
43             }
44             BOOST_ASSERT(edge(p.back(), p.front(), g).second);
45         }
46 
47         std::size_t& cycles;
48     };
49 
50     template <typename Graph, typename Algorithm>
test_one(Algorithm algorithm)51     void test_one(Algorithm algorithm) {
52         typedef erdos_renyi_iterator<minstd_rand, Graph> er;
53 
54         // Generate random graphs with 15 vertices and 15% probability
55         // of edge connection.
56         static std::size_t const N = 20;
57         static double const P = 0.1;
58         minstd_rand rng;
59 
60         Graph g(er(rng, N, P), er(), N);
61         renumber_indices(g);
62         print_edges(g, get(vertex_index, g));
63 
64         std::size_t cycles = 0;
65         cycle_validator vis(cycles);
66         algorithm(g, vis);
67         std::cout << "# cycles: " << vis.cycles << "\n";
68     }
69 } // end namespace cycle_test_detail
70 
71 template <typename Algorithm>
cycle_test(Algorithm const & algorithm)72 void cycle_test(Algorithm const& algorithm) {
73     std::cout << "*** undirected ***\n";
74     cycle_test_detail::test_one<boost::undirected_graph<> >(algorithm);
75 
76     std::cout << "*** directed ***\n";
77     cycle_test_detail::test_one<boost::directed_graph<> >(algorithm);
78 }
79 
80 #endif // !BOOST_GRAPH_TEST_CYCLE_TEST_HPP
81