bron_kerbosch_print_cliques.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // (C) Copyright Andrew Sutton 2007
  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. //[code_bron_kerbosch_print_cliques
  7. #include <iostream>
  8. #include <boost/graph/undirected_graph.hpp>
  9. #include <boost/graph/bron_kerbosch_all_cliques.hpp>
  10. #include "helper.hpp"
  11. using namespace std;
  12. using namespace boost;
  13. // The clique_printer is a visitor that will print the vertices that comprise
  14. // a clique. Note that the vertices are not given in any specific order.
  15. template <typename OutputStream>
  16. struct clique_printer
  17. {
  18. clique_printer(OutputStream& stream)
  19. : os(stream)
  20. { }
  21. template <typename Clique, typename Graph>
  22. void clique(const Clique& c, const Graph& g)
  23. {
  24. // Iterate over the clique and print each vertex within it.
  25. typename Clique::const_iterator i, end = c.end();
  26. for(i = c.begin(); i != end; ++i) {
  27. os << g[*i].name << " ";
  28. }
  29. os << endl;
  30. }
  31. OutputStream& os;
  32. };
  33. // The Actor type stores the name of each vertex in the graph.
  34. struct Actor
  35. {
  36. string name;
  37. };
  38. // Declare the graph type and its vertex and edge types.
  39. typedef undirected_graph<Actor> Graph;
  40. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  41. typedef graph_traits<Graph>::edge_descriptor Edge;
  42. // The name map provides an abstract accessor for the names of
  43. // each vertex. This is used during graph creation.
  44. typedef property_map<Graph, string Actor::*>::type NameMap;
  45. int
  46. main(int argc, char *argv[])
  47. {
  48. // Create the graph and and its name map accessor.
  49. Graph g;
  50. NameMap nm(get(&Actor::name, g));
  51. // Read the graph from standard input.
  52. read_graph(g, nm, cin);
  53. // Instantiate the visitor for printing cliques
  54. clique_printer<ostream> vis(cout);
  55. // Use the Bron-Kerbosch algorithm to find all cliques, printing them
  56. // as they are found.
  57. bron_kerbosch_all_cliques(g, vis);
  58. return 0;
  59. }
  60. //]