bellman-example.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <vector>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <iomanip>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  15. using namespace boost;
  16. template < typename Graph, typename ParentMap >
  17. struct edge_writer
  18. {
  19. edge_writer(const Graph & g, const ParentMap & p)
  20. : m_g(g), m_parent(p)
  21. {
  22. }
  23. template < typename Edge >
  24. void operator() (std::ostream & out, const Edge & e) const
  25. {
  26. out << "[label=\"" << get(edge_weight, m_g, e) << "\"";
  27. typename graph_traits < Graph >::vertex_descriptor
  28. u = source(e, m_g), v = target(e, m_g);
  29. if (m_parent[v] == u)
  30. out << ", color=\"black\"";
  31. else
  32. out << ", color=\"grey\"";
  33. out << "]";
  34. }
  35. const Graph & m_g;
  36. ParentMap m_parent;
  37. };
  38. template < typename Graph, typename Parent >
  39. edge_writer < Graph, Parent >
  40. make_edge_writer(const Graph & g, const Parent & p)
  41. {
  42. return edge_writer < Graph, Parent > (g, p);
  43. }
  44. struct EdgeProperties {
  45. int weight;
  46. };
  47. int
  48. main()
  49. {
  50. enum { u, v, x, y, z, N };
  51. char name[] = { 'u', 'v', 'x', 'y', 'z' };
  52. typedef std::pair < int, int >E;
  53. const int n_edges = 10;
  54. E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u),
  55. E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z,x) };
  56. int weight[n_edges] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 };
  57. typedef adjacency_list < vecS, vecS, directedS,
  58. no_property, EdgeProperties> Graph;
  59. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  60. // VC++ can't handle the iterator constructor
  61. Graph g(N);
  62. for (std::size_t j = 0; j < n_edges; ++j)
  63. add_edge(edge_array[j].first, edge_array[j].second, g);
  64. #else
  65. Graph g(edge_array, edge_array + n_edges, N);
  66. #endif
  67. graph_traits < Graph >::edge_iterator ei, ei_end;
  68. property_map<Graph, int EdgeProperties::*>::type
  69. weight_pmap = get(&EdgeProperties::weight, g);
  70. int i = 0;
  71. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
  72. weight_pmap[*ei] = weight[i];
  73. std::vector<int> distance(N, (std::numeric_limits < short >::max)());
  74. std::vector<std::size_t> parent(N);
  75. for (i = 0; i < N; ++i)
  76. parent[i] = i;
  77. distance[z] = 0;
  78. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  79. bool r = bellman_ford_shortest_paths
  80. (g, int(N), weight_pmap, &parent[0], &distance[0],
  81. closed_plus<int>(), std::less<int>(), default_bellman_visitor());
  82. #else
  83. bool r = bellman_ford_shortest_paths
  84. (g, int (N), weight_map(weight_pmap).distance_map(&distance[0]).
  85. predecessor_map(&parent[0]));
  86. #endif
  87. if (r)
  88. for (i = 0; i < N; ++i)
  89. std::cout << name[i] << ": " << std::setw(3) << distance[i]
  90. << " " << name[parent[i]] << std::endl;
  91. else
  92. std::cout << "negative cycle" << std::endl;
  93. std::ofstream dot_file("figs/bellman-eg.dot");
  94. dot_file << "digraph D {\n"
  95. << " rankdir=LR\n"
  96. << " size=\"5,3\"\n"
  97. << " ratio=\"fill\"\n"
  98. << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
  99. {
  100. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
  101. graph_traits < Graph >::edge_descriptor e = *ei;
  102. graph_traits < Graph >::vertex_descriptor
  103. u = source(e, g), v = target(e, g);
  104. // VC++ doesn't like the 3-argument get function, so here
  105. // we workaround by using 2-nested get()'s.
  106. dot_file << name[u] << " -> " << name[v]
  107. << "[label=\"" << get(get(&EdgeProperties::weight, g), e) << "\"";
  108. if (parent[v] == u)
  109. dot_file << ", color=\"black\"";
  110. else
  111. dot_file << ", color=\"grey\"";
  112. dot_file << "]";
  113. }
  114. }
  115. dot_file << "}";
  116. return EXIT_SUCCESS;
  117. }