kevin-bacon2.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. /*
  9. IMPORTANT:
  10. ~~~~~~~~~~
  11. This example appears to be broken and crashes at runtime, see https://github.com/boostorg/graph/issues/148
  12. */
  13. #include <boost/config.hpp>
  14. #include <iostream>
  15. #include <fstream>
  16. #include <string>
  17. #include <boost/tuple/tuple.hpp>
  18. #include <boost/graph/adjacency_list.hpp>
  19. #include <boost/graph/visitors.hpp>
  20. #include <boost/graph/breadth_first_search.hpp>
  21. #include <map>
  22. #include <boost/graph/adj_list_serialize.hpp>
  23. #include <boost/archive/text_iarchive.hpp>
  24. #include <boost/serialization/string.hpp>
  25. struct vertex_properties {
  26. std::string name;
  27. template<class Archive>
  28. void serialize(Archive & ar, const unsigned int version) {
  29. ar & name;
  30. }
  31. };
  32. struct edge_properties {
  33. std::string name;
  34. template<class Archive>
  35. void serialize(Archive & ar, const unsigned int version) {
  36. ar & name;
  37. }
  38. };
  39. using namespace boost;
  40. typedef adjacency_list<vecS, vecS, undirectedS,
  41. vertex_properties, edge_properties> Graph;
  42. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  43. typedef graph_traits<Graph>::edge_descriptor Edge;
  44. class bacon_number_recorder : public default_bfs_visitor
  45. {
  46. public:
  47. bacon_number_recorder(int* dist) : d(dist) { }
  48. void tree_edge(Edge e, const Graph& g) const {
  49. Vertex u = source(e, g), v = target(e, g);
  50. d[v] = d[u] + 1;
  51. }
  52. private:
  53. int* d;
  54. };
  55. int main(int argc, const char** argv)
  56. {
  57. std::ifstream ifs(argc >= 2 ? argv[1] : "./kevin-bacon2.dat");
  58. if (!ifs) {
  59. std::cerr << "No ./kevin-bacon2.dat file" << std::endl;
  60. return EXIT_FAILURE;
  61. }
  62. archive::text_iarchive ia(ifs);
  63. Graph g;
  64. ia >> g;
  65. std::vector<int> bacon_number(num_vertices(g));
  66. // Get the vertex for Kevin Bacon
  67. Vertex src;
  68. graph_traits<Graph>::vertex_iterator i, end;
  69. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  70. if (g[*i].name == "Kevin Bacon")
  71. src = *i;
  72. // Set Kevin's number to zero
  73. bacon_number[src] = 0;
  74. // Perform a breadth first search to compute everyone' Bacon number.
  75. breadth_first_search(g, src,
  76. visitor(bacon_number_recorder(&bacon_number[0])));
  77. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  78. std::cout << g[*i].name << " has a Bacon number of "
  79. << bacon_number[*i] << std::endl;
  80. return 0;
  81. }