breadth_first_search.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2004-2008 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. // Example usage of breadth_first_search algorithm
  8. // Enable PBGL interfaces to BGL algorithms
  9. #include <boost/graph/use_mpi.hpp>
  10. // Communicate via MPI
  11. #include <boost/graph/distributed/mpi_process_group.hpp>
  12. // Breadth-first search algorithm
  13. #include <boost/graph/breadth_first_search.hpp>
  14. // Distributed adjacency list
  15. #include <boost/graph/distributed/adjacency_list.hpp>
  16. // METIS Input
  17. #include <boost/graph/metis.hpp>
  18. // Graphviz Output
  19. #include <boost/graph/distributed/graphviz.hpp>
  20. // For choose_min_reducer
  21. #include <boost/graph/distributed/distributed_graph_utility.hpp>
  22. // Standard Library includes
  23. #include <fstream>
  24. #include <string>
  25. #ifdef BOOST_NO_EXCEPTIONS
  26. void
  27. boost::throw_exception(std::exception const& ex)
  28. {
  29. std::cout << ex.what() << std::endl;
  30. abort();
  31. }
  32. #endif
  33. using namespace boost;
  34. using boost::graph::distributed::mpi_process_group;
  35. /* An undirected graph with distance values stored on the vertices. */
  36. typedef adjacency_list<vecS, distributedS<mpi_process_group, vecS>, undirectedS,
  37. /*Vertex properties=*/property<vertex_distance_t, std::size_t> >
  38. Graph;
  39. int main(int argc, char* argv[])
  40. {
  41. boost::mpi::environment env(argc,argv);
  42. // Parse command-line options
  43. const char* filename = "weighted_graph.gr";
  44. if (argc > 1) filename = argv[1];
  45. // Open the METIS input file
  46. std::ifstream in(filename);
  47. graph::metis_reader reader(in);
  48. // Load the graph using the default distribution
  49. Graph g(reader.begin(), reader.end(),
  50. reader.num_vertices());
  51. // Get vertex 0 in the graph
  52. graph_traits<Graph>::vertex_descriptor start = vertex(0, g);
  53. // Compute BFS levels from vertex 0
  54. property_map<Graph, vertex_distance_t>::type distance =
  55. get(vertex_distance, g);
  56. // Initialize distances to infinity and set reduction operation to 'min'
  57. BGL_FORALL_VERTICES(v, g, Graph) {
  58. put(distance, v, (std::numeric_limits<std::size_t>::max)());
  59. }
  60. distance.set_reduce(boost::graph::distributed::choose_min_reducer<std::size_t>());
  61. put(distance, start, 0);
  62. breadth_first_search
  63. (g, start,
  64. visitor(make_bfs_visitor(record_distances(distance, on_tree_edge()))));
  65. // Output a Graphviz DOT file
  66. std::string outfile;
  67. if (argc > 2)
  68. outfile = argv[2];
  69. else {
  70. outfile = filename;
  71. size_t i = outfile.rfind('.');
  72. if (i != std::string::npos)
  73. outfile.erase(outfile.begin() + i, outfile.end());
  74. outfile += "-bfs.dot";
  75. }
  76. if (process_id(process_group(g)) == 0) {
  77. std::cout << "Writing GraphViz output to " << outfile << "... ";
  78. std::cout.flush();
  79. }
  80. write_graphviz(outfile, g,
  81. make_label_writer(distance));
  82. if (process_id(process_group(g)) == 0)
  83. std::cout << "Done." << std::endl;
  84. return 0;
  85. }