1 // (C) Copyright 2007-2009 Andrew Sutton
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)
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>
15 #include <boost/random/linear_congruential.hpp>
18 using namespace boost;
20 struct cycle_validator
22 cycle_validator(size_t& c)
26 template <typename Path, typename Graph>
27 void cycle(const Path& p, const Graph& g)
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) {
39 BOOST_ASSERT(edge(*i, *j, g).second);
41 BOOST_ASSERT(edge(p.back(), p.front(), g).second);
47 template <typename Graph>
50 typedef erdos_renyi_iterator<boost::minstd_rand, Graph> er;
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;
58 Graph g(er(rng, N, P), er(), N);
60 print_edges(g, get(vertex_index, g));
63 cycle_validator vis(cycles);
64 tiernan_all_cycles(g, vis);
65 cout << "# cycles: " << vis.cycles << "\n";
71 typedef undirected_graph<> Graph;
72 typedef directed_graph<> DiGraph;
74 std::cout << "*** undirected ***\n";
77 std::cout << "*** directed ***\n";