dijkstra-example.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <boost/config.hpp>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <boost/graph/graph_traits.hpp>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/graph/dijkstra_shortest_paths.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. using namespace boost;
  16. int
  17. main(int, char *[])
  18. {
  19. typedef adjacency_list < listS, vecS, directedS,
  20. no_property, property < edge_weight_t, int > > graph_t;
  21. typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
  22. typedef std::pair<int, int> Edge;
  23. const int num_nodes = 5;
  24. enum nodes { A, B, C, D, E };
  25. char name[] = "ABCDE";
  26. Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
  27. Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
  28. };
  29. int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
  30. int num_arcs = sizeof(edge_array) / sizeof(Edge);
  31. graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
  32. property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
  33. std::vector<vertex_descriptor> p(num_vertices(g));
  34. std::vector<int> d(num_vertices(g));
  35. vertex_descriptor s = vertex(A, g);
  36. dijkstra_shortest_paths(g, s,
  37. predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
  38. distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
  39. std::cout << "distances and parents:" << std::endl;
  40. graph_traits < graph_t >::vertex_iterator vi, vend;
  41. for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
  42. std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
  43. std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
  44. endl;
  45. }
  46. std::cout << std::endl;
  47. std::ofstream dot_file("figs/dijkstra-eg.dot");
  48. dot_file << "digraph D {\n"
  49. << " rankdir=LR\n"
  50. << " size=\"4,3\"\n"
  51. << " ratio=\"fill\"\n"
  52. << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
  53. graph_traits < graph_t >::edge_iterator ei, ei_end;
  54. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
  55. graph_traits < graph_t >::edge_descriptor e = *ei;
  56. graph_traits < graph_t >::vertex_descriptor
  57. u = source(e, g), v = target(e, g);
  58. dot_file << name[u] << " -> " << name[v]
  59. << "[label=\"" << get(weightmap, e) << "\"";
  60. if (p[v] == u)
  61. dot_file << ", color=\"black\"";
  62. else
  63. dot_file << ", color=\"grey\"";
  64. dot_file << "]";
  65. }
  66. dot_file << "}";
  67. return EXIT_SUCCESS;
  68. }