1 //
2 //=======================================================================
3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
10 //
11
12 /*
13 This file implements the function
14
15 template <class EdgeListGraph, class Size, class P, class T, class R>
16 bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N,
17 const bgl_named_params<P, T, R>& params)
18
19 */
20
21 #ifndef BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
22 #define BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
23
24 #include <boost/config.hpp>
25 #include <boost/graph/graph_traits.hpp>
26 #include <boost/graph/graph_concepts.hpp>
27 #include <boost/graph/properties.hpp>
28 #include <boost/graph/relax.hpp>
29 #include <boost/graph/visitors.hpp>
30 #include <boost/graph/named_function_params.hpp>
31 #include <boost/concept/assert.hpp>
32
33 namespace boost
34 {
35
36 template < class Visitor, class Graph > struct BellmanFordVisitorConcept
37 {
constraintsboost::BellmanFordVisitorConcept38 void constraints()
39 {
40 BOOST_CONCEPT_ASSERT((CopyConstructibleConcept< Visitor >));
41 vis.examine_edge(e, g);
42 vis.edge_relaxed(e, g);
43 vis.edge_not_relaxed(e, g);
44 vis.edge_minimized(e, g);
45 vis.edge_not_minimized(e, g);
46 }
47 Visitor vis;
48 Graph g;
49 typename graph_traits< Graph >::edge_descriptor e;
50 };
51
52 template < class Visitors = null_visitor > class bellman_visitor
53 {
54 public:
bellman_visitor()55 bellman_visitor() {}
bellman_visitor(Visitors vis)56 bellman_visitor(Visitors vis) : m_vis(vis) {}
57
examine_edge(Edge u,Graph & g)58 template < class Edge, class Graph > void examine_edge(Edge u, Graph& g)
59 {
60 invoke_visitors(m_vis, u, g, on_examine_edge());
61 }
edge_relaxed(Edge u,Graph & g)62 template < class Edge, class Graph > void edge_relaxed(Edge u, Graph& g)
63 {
64 invoke_visitors(m_vis, u, g, on_edge_relaxed());
65 }
edge_not_relaxed(Edge u,Graph & g)66 template < class Edge, class Graph > void edge_not_relaxed(Edge u, Graph& g)
67 {
68 invoke_visitors(m_vis, u, g, on_edge_not_relaxed());
69 }
edge_minimized(Edge u,Graph & g)70 template < class Edge, class Graph > void edge_minimized(Edge u, Graph& g)
71 {
72 invoke_visitors(m_vis, u, g, on_edge_minimized());
73 }
74 template < class Edge, class Graph >
edge_not_minimized(Edge u,Graph & g)75 void edge_not_minimized(Edge u, Graph& g)
76 {
77 invoke_visitors(m_vis, u, g, on_edge_not_minimized());
78 }
79
80 protected:
81 Visitors m_vis;
82 };
83 template < class Visitors >
make_bellman_visitor(Visitors vis)84 bellman_visitor< Visitors > make_bellman_visitor(Visitors vis)
85 {
86 return bellman_visitor< Visitors >(vis);
87 }
88 typedef bellman_visitor<> default_bellman_visitor;
89
90 template < class EdgeListGraph, class Size, class WeightMap,
91 class PredecessorMap, class DistanceMap, class BinaryFunction,
92 class BinaryPredicate, class BellmanFordVisitor >
bellman_ford_shortest_paths(EdgeListGraph & g,Size N,WeightMap weight,PredecessorMap pred,DistanceMap distance,BinaryFunction combine,BinaryPredicate compare,BellmanFordVisitor v)93 bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N, WeightMap weight,
94 PredecessorMap pred, DistanceMap distance, BinaryFunction combine,
95 BinaryPredicate compare, BellmanFordVisitor v)
96 {
97 BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< EdgeListGraph >));
98 typedef graph_traits< EdgeListGraph > GTraits;
99 typedef typename GTraits::edge_descriptor Edge;
100 typedef typename GTraits::vertex_descriptor Vertex;
101 BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< DistanceMap, Vertex >));
102 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< WeightMap, Edge >));
103
104 typename GTraits::edge_iterator i, end;
105
106 for (Size k = 0; k < N; ++k)
107 {
108 bool at_least_one_edge_relaxed = false;
109 for (boost::tie(i, end) = edges(g); i != end; ++i)
110 {
111 v.examine_edge(*i, g);
112 if (relax(*i, g, weight, pred, distance, combine, compare))
113 {
114 at_least_one_edge_relaxed = true;
115 v.edge_relaxed(*i, g);
116 }
117 else
118 v.edge_not_relaxed(*i, g);
119 }
120 if (!at_least_one_edge_relaxed)
121 break;
122 }
123
124 for (boost::tie(i, end) = edges(g); i != end; ++i)
125 if (compare(combine(get(distance, source(*i, g)), get(weight, *i)),
126 get(distance, target(*i, g))))
127 {
128 v.edge_not_minimized(*i, g);
129 return false;
130 }
131 else
132 v.edge_minimized(*i, g);
133
134 return true;
135 }
136
137 namespace detail
138 {
139
140 template < typename VertexAndEdgeListGraph, typename Size,
141 typename WeightMap, typename PredecessorMap, typename DistanceMap,
142 typename P, typename T, typename R >
bellman_dispatch2(VertexAndEdgeListGraph & g,typename graph_traits<VertexAndEdgeListGraph>::vertex_descriptor s,Size N,WeightMap weight,PredecessorMap pred,DistanceMap distance,const bgl_named_params<P,T,R> & params)143 bool bellman_dispatch2(VertexAndEdgeListGraph& g,
144 typename graph_traits< VertexAndEdgeListGraph >::vertex_descriptor s,
145 Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance,
146 const bgl_named_params< P, T, R >& params)
147 {
148 typedef typename property_traits< DistanceMap >::value_type D;
149 bellman_visitor<> null_vis;
150 typedef typename property_traits< WeightMap >::value_type weight_type;
151 typename graph_traits< VertexAndEdgeListGraph >::vertex_iterator v,
152 v_end;
153 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v)
154 {
155 put(distance, *v, (std::numeric_limits< weight_type >::max)());
156 put(pred, *v, *v);
157 }
158 put(distance, s, weight_type(0));
159 return bellman_ford_shortest_paths(g, N, weight, pred, distance,
160 choose_param(
161 get_param(params, distance_combine_t()), closed_plus< D >()),
162 choose_param(
163 get_param(params, distance_compare_t()), std::less< D >()),
164 choose_param(get_param(params, graph_visitor), null_vis));
165 }
166
167 template < typename VertexAndEdgeListGraph, typename Size,
168 typename WeightMap, typename PredecessorMap, typename DistanceMap,
169 typename P, typename T, typename R >
bellman_dispatch2(VertexAndEdgeListGraph & g,param_not_found,Size N,WeightMap weight,PredecessorMap pred,DistanceMap distance,const bgl_named_params<P,T,R> & params)170 bool bellman_dispatch2(VertexAndEdgeListGraph& g, param_not_found, Size N,
171 WeightMap weight, PredecessorMap pred, DistanceMap distance,
172 const bgl_named_params< P, T, R >& params)
173 {
174 typedef typename property_traits< DistanceMap >::value_type D;
175 bellman_visitor<> null_vis;
176 return bellman_ford_shortest_paths(g, N, weight, pred, distance,
177 choose_param(
178 get_param(params, distance_combine_t()), closed_plus< D >()),
179 choose_param(
180 get_param(params, distance_compare_t()), std::less< D >()),
181 choose_param(get_param(params, graph_visitor), null_vis));
182 }
183
184 template < class EdgeListGraph, class Size, class WeightMap,
185 class DistanceMap, class P, class T, class R >
bellman_dispatch(EdgeListGraph & g,Size N,WeightMap weight,DistanceMap distance,const bgl_named_params<P,T,R> & params)186 bool bellman_dispatch(EdgeListGraph& g, Size N, WeightMap weight,
187 DistanceMap distance, const bgl_named_params< P, T, R >& params)
188 {
189 dummy_property_map dummy_pred;
190 return detail::bellman_dispatch2(g, get_param(params, root_vertex_t()),
191 N, weight,
192 choose_param(get_param(params, vertex_predecessor), dummy_pred),
193 distance, params);
194 }
195 } // namespace detail
196
197 template < class EdgeListGraph, class Size, class P, class T, class R >
bellman_ford_shortest_paths(EdgeListGraph & g,Size N,const bgl_named_params<P,T,R> & params)198 bool bellman_ford_shortest_paths(
199 EdgeListGraph& g, Size N, const bgl_named_params< P, T, R >& params)
200 {
201 return detail::bellman_dispatch(g, N,
202 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
203 choose_pmap(get_param(params, vertex_distance), g, vertex_distance),
204 params);
205 }
206
207 template < class EdgeListGraph, class Size >
bellman_ford_shortest_paths(EdgeListGraph & g,Size N)208 bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N)
209 {
210 bgl_named_params< int, int > params(0);
211 return bellman_ford_shortest_paths(g, N, params);
212 }
213
214 template < class VertexAndEdgeListGraph, class P, class T, class R >
bellman_ford_shortest_paths(VertexAndEdgeListGraph & g,const bgl_named_params<P,T,R> & params)215 bool bellman_ford_shortest_paths(
216 VertexAndEdgeListGraph& g, const bgl_named_params< P, T, R >& params)
217 {
218 BOOST_CONCEPT_ASSERT((VertexListGraphConcept< VertexAndEdgeListGraph >));
219 return detail::bellman_dispatch(g, num_vertices(g),
220 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
221 choose_pmap(get_param(params, vertex_distance), g, vertex_distance),
222 params);
223 }
224 } // namespace boost
225
226 #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
227