Imported Upstream version 1.49.0
[platform/upstream/boost.git] / libs / graph / test / tiernan_all_cycles.cpp
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 {
22     cycle_validator(size_t& c)
23         : cycles(c)
24     { }
25
26     template <typename Path, typename Graph>
27     void cycle(const Path& p, const Graph& g)
28     {
29         ++cycles;
30         typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
31         typedef typename graph_traits<Graph>::edge_descriptor Edge;
32         // Check to make sure that each of the vertices in the path
33         // is truly connected and that the back is connected to the
34         // front - it's not validating that we find all paths, just
35         // that the paths are valid.
36         typename Path::const_iterator i, j, last = prior(p.end());
37         for(i = p.begin(); i != last; ++i) {
38             j = boost::next(i);
39             BOOST_ASSERT(edge(*i, *j, g).second);
40         }
41         BOOST_ASSERT(edge(p.back(), p.front(), g).second);
42     }
43
44     size_t& cycles;
45 };
46
47 template <typename Graph>
48 void test()
49 {
50     typedef erdos_renyi_iterator<boost::minstd_rand, Graph> er;
51
52     // Generate random graphs with 15 vertices and 15% probability
53     // of edge connection.
54     static const size_t N = 20;
55     static const double P = 0.1;
56     boost::minstd_rand rng;
57
58     Graph g(er(rng, N, P), er(), N);
59     renumber_indices(g);
60     print_edges(g, get(vertex_index, g));
61
62     size_t cycles = 0;
63     cycle_validator vis(cycles);
64     tiernan_all_cycles(g, vis);
65     cout << "# cycles: " << vis.cycles << "\n";
66 }
67
68 int
69 main(int, char *[])
70 {
71     typedef undirected_graph<> Graph;
72     typedef directed_graph<> DiGraph;
73
74     std::cout << "*** undirected ***\n";
75     test<Graph>();
76
77     std::cout << "*** directed ***\n";
78     test<DiGraph>();
79 }