relax.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek,
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #ifndef BOOST_GRAPH_RELAX_HPP
  10. #define BOOST_GRAPH_RELAX_HPP
  11. #include <functional>
  12. #include <boost/limits.hpp> // for numeric limits
  13. #include <boost/graph/graph_traits.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. namespace boost
  16. {
  17. // The following version of the plus functor prevents
  18. // problems due to overflow at positive infinity.
  19. template < class T > struct closed_plus
  20. {
  21. const T inf;
  22. closed_plus() : inf((std::numeric_limits< T >::max)()) {}
  23. closed_plus(T inf) : inf(inf) {}
  24. T operator()(const T& a, const T& b) const
  25. {
  26. using namespace std;
  27. if (a == inf)
  28. return inf;
  29. if (b == inf)
  30. return inf;
  31. return a + b;
  32. }
  33. };
  34. template < class Graph, class WeightMap, class PredecessorMap,
  35. class DistanceMap, class BinaryFunction, class BinaryPredicate >
  36. bool relax(typename graph_traits< Graph >::edge_descriptor e, const Graph& g,
  37. const WeightMap& w, PredecessorMap& p, DistanceMap& d,
  38. const BinaryFunction& combine, const BinaryPredicate& compare)
  39. {
  40. typedef typename graph_traits< Graph >::directed_category DirCat;
  41. bool is_undirected = is_same< DirCat, undirected_tag >::value;
  42. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  43. Vertex u = source(e, g), v = target(e, g);
  44. typedef typename property_traits< DistanceMap >::value_type D;
  45. typedef typename property_traits< WeightMap >::value_type W;
  46. const D d_u = get(d, u);
  47. const D d_v = get(d, v);
  48. const W& w_e = get(w, e);
  49. // The seemingly redundant comparisons after the distance puts are to
  50. // ensure that extra floating-point precision in x87 registers does not
  51. // lead to relax() returning true when the distance did not actually
  52. // change.
  53. if (compare(combine(d_u, w_e), d_v))
  54. {
  55. put(d, v, combine(d_u, w_e));
  56. if (compare(get(d, v), d_v))
  57. {
  58. put(p, v, u);
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. else if (is_undirected && compare(combine(d_v, w_e), d_u))
  67. {
  68. put(d, u, combine(d_v, w_e));
  69. if (compare(get(d, u), d_u))
  70. {
  71. put(p, u, v);
  72. return true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. else
  80. return false;
  81. }
  82. template < class Graph, class WeightMap, class PredecessorMap,
  83. class DistanceMap, class BinaryFunction, class BinaryPredicate >
  84. bool relax_target(typename graph_traits< Graph >::edge_descriptor e,
  85. const Graph& g, const WeightMap& w, PredecessorMap& p, DistanceMap& d,
  86. const BinaryFunction& combine, const BinaryPredicate& compare)
  87. {
  88. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  89. typedef typename property_traits< DistanceMap >::value_type D;
  90. typedef typename property_traits< WeightMap >::value_type W;
  91. const Vertex u = source(e, g);
  92. const Vertex v = target(e, g);
  93. const D d_u = get(d, u);
  94. const D d_v = get(d, v);
  95. const W& w_e = get(w, e);
  96. // The seemingly redundant comparisons after the distance puts are to
  97. // ensure that extra floating-point precision in x87 registers does not
  98. // lead to relax() returning true when the distance did not actually
  99. // change.
  100. if (compare(combine(d_u, w_e), d_v))
  101. {
  102. put(d, v, combine(d_u, w_e));
  103. if (compare(get(d, v), d_v))
  104. {
  105. put(p, v, u);
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. template < class Graph, class WeightMap, class PredecessorMap,
  112. class DistanceMap >
  113. bool relax(typename graph_traits< Graph >::edge_descriptor e, const Graph& g,
  114. WeightMap w, PredecessorMap p, DistanceMap d)
  115. {
  116. typedef typename property_traits< DistanceMap >::value_type D;
  117. typedef closed_plus< D > Combine;
  118. typedef std::less< D > Compare;
  119. return relax(e, g, w, p, d, Combine(), Compare());
  120. }
  121. } // namespace boost
  122. #endif /* BOOST_GRAPH_RELAX_HPP */