relax.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // The following version of the plus functor prevents
  17. // problems due to overflow at positive infinity.
  18. template <class T>
  19. 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. using namespace std;
  26. if (a == inf) return inf;
  27. if (b == inf) return inf;
  28. return a + b;
  29. }
  30. };
  31. template <class Graph, class WeightMap,
  32. class PredecessorMap, class DistanceMap,
  33. class BinaryFunction, class BinaryPredicate>
  34. bool relax(typename graph_traits<Graph>::edge_descriptor e,
  35. const Graph& g, const WeightMap& w,
  36. PredecessorMap& p, DistanceMap& d,
  37. const BinaryFunction& combine, const BinaryPredicate& compare)
  38. {
  39. typedef typename graph_traits<Graph>::directed_category DirCat;
  40. bool is_undirected = is_same<DirCat, undirected_tag>::value;
  41. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  42. Vertex u = source(e, g), v = target(e, g);
  43. typedef typename property_traits<DistanceMap>::value_type D;
  44. typedef typename property_traits<WeightMap>::value_type W;
  45. const D d_u = get(d, u);
  46. const D d_v = get(d, v);
  47. const W& w_e = get(w, e);
  48. // The seemingly redundant comparisons after the distance puts are to
  49. // ensure that extra floating-point precision in x87 registers does not
  50. // lead to relax() returning true when the distance did not actually
  51. // change.
  52. if ( compare(combine(d_u, w_e), d_v) ) {
  53. put(d, v, combine(d_u, w_e));
  54. if (compare(get(d, v), d_v)) {
  55. put(p, v, u);
  56. return true;
  57. } else {
  58. return false;
  59. }
  60. } else if (is_undirected && compare(combine(d_v, w_e), d_u)) {
  61. put(d, u, combine(d_v, w_e));
  62. if (compare(get(d, u), d_u)) {
  63. put(p, u, v);
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. } else
  69. return false;
  70. }
  71. template <class Graph, class WeightMap,
  72. class PredecessorMap, class DistanceMap,
  73. class BinaryFunction, class BinaryPredicate>
  74. bool relax_target(typename graph_traits<Graph>::edge_descriptor e,
  75. const Graph& g, const WeightMap& w,
  76. PredecessorMap& p, DistanceMap& d,
  77. const BinaryFunction& combine, const BinaryPredicate& compare)
  78. {
  79. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  80. typedef typename property_traits<DistanceMap>::value_type D;
  81. typedef typename property_traits<WeightMap>::value_type W;
  82. const Vertex u = source(e, g);
  83. const Vertex v = target(e, g);
  84. const D d_u = get(d, u);
  85. const D d_v = get(d, v);
  86. const W& w_e = get(w, e);
  87. // The seemingly redundant comparisons after the distance puts are to
  88. // ensure that extra floating-point precision in x87 registers does not
  89. // lead to relax() returning true when the distance did not actually
  90. // change.
  91. if (compare(combine(d_u, w_e), d_v)) {
  92. put(d, v, combine(d_u, w_e));
  93. if (compare(get(d, v), d_v)) {
  94. put(p, v, u);
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. template <class Graph, class WeightMap,
  101. class PredecessorMap, class DistanceMap>
  102. bool relax(typename graph_traits<Graph>::edge_descriptor e,
  103. const Graph& g, WeightMap w, PredecessorMap p, DistanceMap d)
  104. {
  105. typedef typename property_traits<DistanceMap>::value_type D;
  106. typedef closed_plus<D> Combine;
  107. typedef std::less<D> Compare;
  108. return relax(e, g, w, p, d, Combine(), Compare());
  109. }
  110. } // namespace boost
  111. #endif /* BOOST_GRAPH_RELAX_HPP */