bellman-test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // (C) Copyright Jeremy Siek 2004
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // From Louis Lavery <Louis@devilsChimney.co.uk>
  6. /*Expected Output:-
  7. A: 0 A
  8. B: 11 A
  9. Actual Output:-
  10. A: 0 A
  11. B: 2147483647 B
  12. */
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  17. #include <boost/cstdlib.hpp>
  18. #include <boost/test/minimal.hpp>
  19. int test_main(int, char*[])
  20. {
  21. using namespace boost;
  22. enum { A, B, Z };
  23. char const name[] = "ABZ";
  24. int const numVertex = static_cast<int>(Z) + 1;
  25. typedef std::pair<int,int> Edge;
  26. Edge edge_array[] = {Edge(B,A)};
  27. int const numEdges = sizeof(edge_array) / sizeof(Edge);
  28. int const weight[numEdges] = {11};
  29. typedef adjacency_list<vecS,vecS,undirectedS,
  30. no_property,property<edge_weight_t,int> > Graph;
  31. Graph g(edge_array, edge_array + numEdges, numVertex);
  32. Graph::edge_iterator ei, ei_end;
  33. property_map<Graph,edge_weight_t>::type
  34. weight_pmap = get(edge_weight, g);
  35. int i = 0;
  36. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
  37. weight_pmap[*ei] = weight[i];
  38. std::vector<int> parent(numVertex);
  39. for(i = 0; i < numVertex; ++i)
  40. parent[i] = i;
  41. int inf = (std::numeric_limits<int>::max)();
  42. std::vector<int> distance(numVertex, inf);
  43. distance[A] = 0; // Set source distance to zero
  44. bool const r = bellman_ford_shortest_paths
  45. (g, int (numVertex),
  46. weight_pmap,
  47. boost::make_iterator_property_map(parent.begin(), get(boost::vertex_index, g)),
  48. boost::make_iterator_property_map(distance.begin(), get(boost::vertex_index, g)),
  49. closed_plus<int>(),
  50. std::less<int>(),
  51. default_bellman_visitor());
  52. if (r) {
  53. for(int i = 0; i < numVertex; ++i) {
  54. std::cout << name[i] << ": ";
  55. if (distance[i] == inf)
  56. std::cout << std::setw(3) << "inf";
  57. else
  58. std::cout << std::setw(3) << distance[i];
  59. std::cout << " " << name[parent[i]] << std::endl;
  60. }
  61. } else {
  62. std::cout << "negative cycle" << std::endl;
  63. }
  64. #if !(defined(__INTEL_COMPILER) && __INTEL_COMPILER <= 700) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
  65. graph_traits<Graph>::vertex_descriptor s = vertex(A, g);
  66. std::vector<int> parent2(numVertex);
  67. std::vector<int> distance2(numVertex, 17);
  68. bool const r2 = bellman_ford_shortest_paths
  69. (g,
  70. weight_map(weight_pmap).
  71. distance_map(boost::make_iterator_property_map(distance2.begin(), get(boost::vertex_index, g))).
  72. predecessor_map(boost::make_iterator_property_map(parent2.begin(), get(boost::vertex_index, g))).
  73. root_vertex(s));
  74. if (r2) {
  75. for(int i = 0; i < numVertex; ++i) {
  76. std::cout << name[i] << ": ";
  77. if (distance2[i] == inf)
  78. std::cout << std::setw(3) << "inf";
  79. else
  80. std::cout << std::setw(3) << distance2[i];
  81. std::cout << " " << name[parent2[i]] << std::endl;
  82. }
  83. } else {
  84. std::cout << "negative cycle" << std::endl;
  85. }
  86. BOOST_CHECK(r == r2);
  87. if (r && r2) {
  88. BOOST_CHECK(parent == parent2);
  89. BOOST_CHECK(distance == distance2);
  90. }
  91. #endif
  92. return boost::exit_success;
  93. }