bron_kerbosch_all_cliques.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <iostream>
  7. #include <iterator>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <map>
  11. #include <boost/graph/graph_utility.hpp>
  12. #include <boost/graph/undirected_graph.hpp>
  13. #include <boost/graph/directed_graph.hpp>
  14. #include <boost/graph/bron_kerbosch_all_cliques.hpp>
  15. #include <boost/graph/erdos_renyi_generator.hpp>
  16. #include <boost/random/linear_congruential.hpp>
  17. using namespace std;
  18. using namespace boost;
  19. // TODO: This is probably not a very good test. We should be able to define
  20. // an identity test - something like find the clique of K5.
  21. struct clique_validator
  22. {
  23. clique_validator()
  24. { }
  25. template <typename Clique, typename Graph>
  26. inline void
  27. clique(const Clique& c, Graph& g)
  28. {
  29. // Simply assert that each vertex in the clique is connected
  30. // to all others in the clique.
  31. typename Clique::const_iterator i, j, end = c.end();
  32. for(i = c.begin(); i != end; ++i) {
  33. for(j = c.begin(); j != end; ++j) {
  34. if(i != j) {
  35. BOOST_ASSERT(edge(*i, *j, g).second);
  36. }
  37. }
  38. }
  39. }
  40. };
  41. template <typename Graph>
  42. void test()
  43. {
  44. typedef erdos_renyi_iterator<boost::minstd_rand, Graph> er;
  45. // Generate random graphs with 15 vertices and 15% probability
  46. // of edge connection.
  47. static const size_t N = 20;
  48. static const double P = 0.1;
  49. boost::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. bron_kerbosch_all_cliques(g, clique_validator());
  54. }
  55. int
  56. main(int, char *[])
  57. {
  58. typedef undirected_graph<> Graph;
  59. typedef directed_graph<> DiGraph;
  60. std::cout << "*** undirected ***\n";
  61. test<Graph>();
  62. std::cout << "*** directed ***\n";
  63. test<DiGraph>();
  64. }