dijkstra_shortest_paths.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2004-2006 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 dijkstra_shortest_paths algorithm
  8. // Enable PBGL interfaces to BGL algorithms
  9. #include <boost/graph/use_mpi.hpp>
  10. // Communication via MPI
  11. #include <boost/graph/distributed/mpi_process_group.hpp>
  12. // Dijkstra's single-source shortest paths algorithm
  13. #include <boost/graph/dijkstra_shortest_paths.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. // Standard Library includes
  21. #include <fstream>
  22. #include <string>
  23. #ifdef BOOST_NO_EXCEPTIONS
  24. void
  25. boost::throw_exception(std::exception const& ex)
  26. {
  27. std::cout << ex.what() << std::endl;
  28. abort();
  29. }
  30. #endif
  31. using namespace boost;
  32. using boost::graph::distributed::mpi_process_group;
  33. /* An undirected, weighted graph with distance values stored on the
  34. vertices. */
  35. typedef adjacency_list<vecS, distributedS<mpi_process_group, vecS>, undirectedS,
  36. /*Vertex properties=*/property<vertex_distance_t, float>,
  37. /*Edge properties=*/property<edge_weight_t, float> >
  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(), reader.weight_begin(),
  50. reader.num_vertices());
  51. // Get vertex 0 in the graph
  52. graph_traits<Graph>::vertex_descriptor start = vertex(0, g);
  53. // Compute shortest paths from vertex 0
  54. dijkstra_shortest_paths(g, start,
  55. distance_map(get(vertex_distance, g)));
  56. // Output a Graphviz DOT file
  57. std::string outfile = filename;
  58. if (argc > 2)
  59. outfile = argv[2];
  60. else {
  61. size_t i = outfile.rfind('.');
  62. if (i != std::string::npos)
  63. outfile.erase(outfile.begin() + i, outfile.end());
  64. outfile += "-dijkstra.dot";
  65. }
  66. if (process_id(process_group(g)) == 0) {
  67. std::cout << "Writing GraphViz output to " << outfile << "... ";
  68. std::cout.flush();
  69. }
  70. write_graphviz(outfile, g,
  71. make_label_writer(get(vertex_distance, g)),
  72. make_label_writer(get(edge_weight, g)));
  73. if (process_id(process_group(g)) == 0)
  74. std::cout << "Done." << std::endl;
  75. return 0;
  76. }