1 //=======================================================================
2 // Copyright 2013 University of Warsaw.
3 // Authors: Piotr Wygocki
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 //=======================================================================
10 //This algorithm is described in "Network Flows: Theory, Algorithms, and Applications"
11 // by Ahuja, Magnanti, Orlin.
13 #ifndef BOOST_GRAPH_SUCCESSIVE_SHORTEST_PATH_HPP
14 #define BOOST_GRAPH_SUCCESSIVE_SHORTEST_PATH_HPP
18 #include <boost/property_map/property_map.hpp>
19 #include <boost/graph/graph_traits.hpp>
20 #include <boost/graph/graph_concepts.hpp>
21 #include <boost/pending/indirect_cmp.hpp>
22 #include <boost/graph/dijkstra_shortest_paths.hpp>
23 #include <boost/graph/properties.hpp>
24 #include <boost/graph/iteration_macros.hpp>
25 #include <boost/graph/detail/augment.hpp>
32 template <class Graph, class Weight, class Distance, class Reversed>
33 class MapReducedWeight :
34 public put_get_helper<typename property_traits<Weight>::value_type, MapReducedWeight<Graph, Weight, Distance, Reversed> > {
35 typedef graph_traits<Graph> gtraits;
37 typedef boost::readable_property_map_tag category;
38 typedef typename property_traits<Weight>::value_type value_type;
39 typedef value_type reference;
40 typedef typename gtraits::edge_descriptor key_type;
41 MapReducedWeight(const Graph & g, Weight w, Distance d, Reversed r) :
42 g_(g), weight_(w), distance_(d), rev_(r) {}
44 reference operator[](key_type v) const {
45 return get(distance_, source(v, g_)) - get(distance_,target(v, g_)) + get(weight_, v);
54 template <class Graph, class Weight, class Distance, class Reversed>
55 MapReducedWeight<Graph, Weight, Distance, Reversed>
56 make_mapReducedWeight(const Graph & g, Weight w, Distance d, Reversed r) {
57 return MapReducedWeight<Graph, Weight, Distance, Reversed>(g, w, d, r);
63 template <class Graph, class Capacity, class ResidualCapacity, class Reversed, class Pred, class Weight, class Distance, class Distance2, class VertexIndex>
64 void successive_shortest_path_nonnegative_weights(
66 typename graph_traits<Graph>::vertex_descriptor s,
67 typename graph_traits<Graph>::vertex_descriptor t,
69 ResidualCapacity residual_capacity,
75 Distance2 distance_prev) {
76 filtered_graph<const Graph, is_residual_edge<ResidualCapacity> >
77 gres = detail::residual_graph(g, residual_capacity);
78 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
80 BGL_FORALL_EDGES_T(e, g, Graph) {
81 put(residual_capacity, e, get(capacity, e));
84 BGL_FORALL_VERTICES_T(v, g, Graph) {
85 put(distance_prev, v, 0);
89 BGL_FORALL_VERTICES_T(v, g, Graph) {
90 put(pred, v, edge_descriptor());
92 dijkstra_shortest_paths(gres, s,
93 weight_map(detail::make_mapReducedWeight(gres, weight, distance_prev, rev)).
94 distance_map(distance).
95 vertex_index_map(index).
96 visitor(make_dijkstra_visitor(record_edge_predecessors(pred, on_edge_relaxed()))));
98 if(get(pred, t) == edge_descriptor()) {
102 BGL_FORALL_VERTICES_T(v, g, Graph) {
103 put(distance_prev, v, get(distance_prev, v) + get(distance, v));
106 detail::augment(g, s, t, pred, residual_capacity, rev);
110 //in this namespace argument dispatching tak place
113 template <class Graph, class Capacity, class ResidualCapacity, class Weight, class Reversed, class Pred, class Distance, class Distance2, class VertexIndex>
114 void successive_shortest_path_nonnegative_weights_dispatch3(
116 typename graph_traits<Graph>::vertex_descriptor s,
117 typename graph_traits<Graph>::vertex_descriptor t,
119 ResidualCapacity residual_capacity,
125 Distance2 dist_pred) {
126 successive_shortest_path_nonnegative_weights(g, s, t, capacity, residual_capacity, weight, rev, index, pred, dist, dist_pred);
129 //setting default distance map
130 template <class Graph, class Capacity, class ResidualCapacity, class Weight, class Reversed, class Pred, class Distance, class VertexIndex>
131 void successive_shortest_path_nonnegative_weights_dispatch3(
133 typename graph_traits<Graph>::vertex_descriptor s,
134 typename graph_traits<Graph>::vertex_descriptor t,
136 ResidualCapacity residual_capacity,
143 typedef typename property_traits<Weight>::value_type D;
145 std::vector<D> d_map(num_vertices(g));
147 successive_shortest_path_nonnegative_weights(g, s, t, capacity, residual_capacity, weight, rev, index, pred, dist,
148 make_iterator_property_map(d_map.begin(), index));
151 template <class Graph, class P, class T, class R, class Capacity, class ResidualCapacity, class Weight, class Reversed, class Pred, class Distance, class VertexIndex>
152 void successive_shortest_path_nonnegative_weights_dispatch2(
154 typename graph_traits<Graph>::vertex_descriptor s,
155 typename graph_traits<Graph>::vertex_descriptor t,
157 ResidualCapacity residual_capacity,
163 const bgl_named_params<P, T, R>& params) {
164 successive_shortest_path_nonnegative_weights_dispatch3(g, s, t, capacity, residual_capacity, weight, rev, index, pred, dist, get_param(params, vertex_distance2));
167 //setting default distance map
168 template <class Graph, class P, class T, class R, class Capacity, class ResidualCapacity, class Weight, class Reversed, class Pred, class VertexIndex>
169 void successive_shortest_path_nonnegative_weights_dispatch2(
171 typename graph_traits<Graph>::vertex_descriptor s,
172 typename graph_traits<Graph>::vertex_descriptor t,
174 ResidualCapacity residual_capacity,
180 const bgl_named_params<P, T, R>& params) {
181 typedef typename property_traits<Weight>::value_type D;
183 std::vector<D> d_map(num_vertices(g));
185 successive_shortest_path_nonnegative_weights_dispatch3(g, s, t, capacity, residual_capacity, weight, rev, index, pred,
186 make_iterator_property_map(d_map.begin(), index),
187 get_param(params, vertex_distance2));
190 template <class Graph, class P, class T, class R, class Capacity, class ResidualCapacity, class Weight, class Reversed, class Pred, class VertexIndex>
191 void successive_shortest_path_nonnegative_weights_dispatch1(
193 typename graph_traits<Graph>::vertex_descriptor s,
194 typename graph_traits<Graph>::vertex_descriptor t,
196 ResidualCapacity residual_capacity,
201 const bgl_named_params<P, T, R>& params) {
202 successive_shortest_path_nonnegative_weights_dispatch2(g, s, t, capacity, residual_capacity, weight, rev, index, pred,
203 get_param(params, vertex_distance), params);
206 //setting default predecessors map
207 template <class Graph, class P, class T, class R, class Capacity, class ResidualCapacity, class Weight, class Reversed, class VertexIndex>
208 void successive_shortest_path_nonnegative_weights_dispatch1(
210 typename graph_traits<Graph>::vertex_descriptor s,
211 typename graph_traits<Graph>::vertex_descriptor t,
213 ResidualCapacity residual_capacity,
218 const bgl_named_params<P, T, R>& params) {
219 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
220 std::vector<edge_descriptor> pred_vec(num_vertices(g));
222 successive_shortest_path_nonnegative_weights_dispatch2(g, s, t, capacity, residual_capacity, weight, rev, index,
223 make_iterator_property_map(pred_vec.begin(), index),
224 get_param(params, vertex_distance), params);
230 template <class Graph, class P, class T, class R>
231 void successive_shortest_path_nonnegative_weights(
233 typename graph_traits<Graph>::vertex_descriptor s,
234 typename graph_traits<Graph>::vertex_descriptor t,
235 const bgl_named_params<P, T, R>& params) {
237 return detail::successive_shortest_path_nonnegative_weights_dispatch1(g, s, t,
238 choose_const_pmap(get_param(params, edge_capacity), g, edge_capacity),
239 choose_pmap(get_param(params, edge_residual_capacity),
240 g, edge_residual_capacity),
241 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
242 choose_const_pmap(get_param(params, edge_reverse), g, edge_reverse),
243 choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
244 get_param(params, vertex_predecessor),
248 template <class Graph>
249 void successive_shortest_path_nonnegative_weights(
251 typename graph_traits<Graph>::vertex_descriptor s,
252 typename graph_traits<Graph>::vertex_descriptor t) {
253 bgl_named_params<int, buffer_param_t> params(0);
254 successive_shortest_path_nonnegative_weights(g, s, t, params);
259 #endif /* BOOST_GRAPH_SUCCESSIVE_SHORTEST_PATH_HPP */