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