1 // (C) Copyright 2007-2009 Andrew Sutton
2 //
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0 (See accompanying file
5 // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <iostream>
8 
9 #include <boost/graph/graph_utility.hpp>
10 #include <boost/graph/undirected_graph.hpp>
11 #include <boost/graph/directed_graph.hpp>
12 #include <boost/graph/tiernan_all_cycles.hpp>
13 #include <boost/graph/erdos_renyi_generator.hpp>
14 
15 #include <boost/random/linear_congruential.hpp>
16 
17 using namespace std;
18 using namespace boost;
19 
20 struct cycle_validator
21 {
cycle_validatorcycle_validator22     cycle_validator(size_t& c)
23         : cycles(c)
24     { }
25 
26     template <typename Path, typename Graph>
cyclecycle_validator27     void cycle(const Path& p, const Graph& g)
28     {
29         ++cycles;
30         // Check to make sure that each of the vertices in the path
31         // is truly connected and that the back is connected to the
32         // front - it's not validating that we find all paths, just
33         // that the paths are valid.
34         typename Path::const_iterator i, j, last = prior(p.end());
35         for(i = p.begin(); i != last; ++i) {
36             j = boost::next(i);
37             BOOST_ASSERT(edge(*i, *j, g).second);
38         }
39         BOOST_ASSERT(edge(p.back(), p.front(), g).second);
40     }
41 
42     size_t& cycles;
43 };
44 
45 template <typename Graph>
test()46 void test()
47 {
48     typedef erdos_renyi_iterator<boost::minstd_rand, Graph> er;
49 
50     // Generate random graphs with 15 vertices and 15% probability
51     // of edge connection.
52     static const size_t N = 20;
53     static const double P = 0.1;
54     boost::minstd_rand rng;
55 
56     Graph g(er(rng, N, P), er(), N);
57     renumber_indices(g);
58     print_edges(g, get(vertex_index, g));
59 
60     size_t cycles = 0;
61     cycle_validator vis(cycles);
62     tiernan_all_cycles(g, vis);
63     cout << "# cycles: " << vis.cycles << "\n";
64 }
65 
66 int
main(int,char * [])67 main(int, char *[])
68 {
69     typedef undirected_graph<> Graph;
70     typedef directed_graph<> DiGraph;
71 
72     std::cout << "*** undirected ***\n";
73     test<Graph>();
74 
75     std::cout << "*** directed ***\n";
76     test<DiGraph>();
77 }
78