annotate DEPENDENCIES/generic/include/boost/graph/edmonds_karp_max_flow.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 //=======================================================================
Chris@16 2 // Copyright 2000 University of Notre Dame.
Chris@16 3 // Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
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
Chris@16 10 #ifndef EDMONDS_KARP_MAX_FLOW_HPP
Chris@16 11 #define EDMONDS_KARP_MAX_FLOW_HPP
Chris@16 12
Chris@16 13 #include <boost/config.hpp>
Chris@16 14 #include <vector>
Chris@16 15 #include <algorithm> // for std::min and std::max
Chris@16 16 #include <boost/config.hpp>
Chris@16 17 #include <boost/pending/queue.hpp>
Chris@16 18 #include <boost/property_map/property_map.hpp>
Chris@16 19 #include <boost/graph/graph_traits.hpp>
Chris@16 20 #include <boost/graph/properties.hpp>
Chris@16 21 #include <boost/graph/filtered_graph.hpp>
Chris@16 22 #include <boost/graph/breadth_first_search.hpp>
Chris@16 23
Chris@16 24 namespace boost {
Chris@16 25
Chris@16 26 // The "labeling" algorithm from "Network Flows" by Ahuja, Magnanti,
Chris@16 27 // Orlin. I think this is the same as or very similar to the original
Chris@16 28 // Edmonds-Karp algorithm. This solves the maximum flow problem.
Chris@16 29
Chris@16 30 namespace detail {
Chris@16 31
Chris@16 32 template <class Graph, class ResCapMap>
Chris@16 33 filtered_graph<Graph, is_residual_edge<ResCapMap> >
Chris@16 34 residual_graph(Graph& g, ResCapMap residual_capacity) {
Chris@16 35 return filtered_graph<Graph, is_residual_edge<ResCapMap> >
Chris@16 36 (g, is_residual_edge<ResCapMap>(residual_capacity));
Chris@16 37 }
Chris@16 38
Chris@16 39 template <class Graph, class PredEdgeMap, class ResCapMap,
Chris@16 40 class RevEdgeMap>
Chris@16 41 inline void
Chris@16 42 augment(Graph& g,
Chris@16 43 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 44 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 45 PredEdgeMap p,
Chris@16 46 ResCapMap residual_capacity,
Chris@16 47 RevEdgeMap reverse_edge)
Chris@16 48 {
Chris@16 49 typename graph_traits<Graph>::edge_descriptor e;
Chris@16 50 typename graph_traits<Graph>::vertex_descriptor u;
Chris@16 51 typedef typename property_traits<ResCapMap>::value_type FlowValue;
Chris@16 52
Chris@16 53 // find minimum residual capacity along the augmenting path
Chris@16 54 FlowValue delta = (std::numeric_limits<FlowValue>::max)();
Chris@16 55 e = get(p, sink);
Chris@16 56 do {
Chris@16 57 BOOST_USING_STD_MIN();
Chris@16 58 delta = min BOOST_PREVENT_MACRO_SUBSTITUTION(delta, get(residual_capacity, e));
Chris@16 59 u = source(e, g);
Chris@16 60 e = get(p, u);
Chris@16 61 } while (u != src);
Chris@16 62
Chris@16 63 // push delta units of flow along the augmenting path
Chris@16 64 e = get(p, sink);
Chris@16 65 do {
Chris@16 66 put(residual_capacity, e, get(residual_capacity, e) - delta);
Chris@16 67 put(residual_capacity, get(reverse_edge, e), get(residual_capacity, get(reverse_edge, e)) + delta);
Chris@16 68 u = source(e, g);
Chris@16 69 e = get(p, u);
Chris@16 70 } while (u != src);
Chris@16 71 }
Chris@16 72
Chris@16 73 } // namespace detail
Chris@16 74
Chris@16 75 template <class Graph,
Chris@16 76 class CapacityEdgeMap, class ResidualCapacityEdgeMap,
Chris@16 77 class ReverseEdgeMap, class ColorMap, class PredEdgeMap>
Chris@16 78 typename property_traits<CapacityEdgeMap>::value_type
Chris@16 79 edmonds_karp_max_flow
Chris@16 80 (Graph& g,
Chris@16 81 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 82 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 83 CapacityEdgeMap cap,
Chris@16 84 ResidualCapacityEdgeMap res,
Chris@16 85 ReverseEdgeMap rev,
Chris@16 86 ColorMap color,
Chris@16 87 PredEdgeMap pred)
Chris@16 88 {
Chris@16 89 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
Chris@16 90 typedef typename property_traits<ColorMap>::value_type ColorValue;
Chris@16 91 typedef color_traits<ColorValue> Color;
Chris@16 92
Chris@16 93 typename graph_traits<Graph>::vertex_iterator u_iter, u_end;
Chris@16 94 typename graph_traits<Graph>::out_edge_iterator ei, e_end;
Chris@16 95 for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
Chris@16 96 for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
Chris@16 97 put(res, *ei, get(cap, *ei));
Chris@16 98
Chris@16 99 put(color, sink, Color::gray());
Chris@16 100 while (get(color, sink) != Color::white()) {
Chris@16 101 boost::queue<vertex_t> Q;
Chris@16 102 breadth_first_search
Chris@16 103 (detail::residual_graph(g, res), src, Q,
Chris@16 104 make_bfs_visitor(record_edge_predecessors(pred, on_tree_edge())),
Chris@16 105 color);
Chris@16 106 if (get(color, sink) != Color::white())
Chris@16 107 detail::augment(g, src, sink, pred, res, rev);
Chris@16 108 } // while
Chris@16 109
Chris@16 110 typename property_traits<CapacityEdgeMap>::value_type flow = 0;
Chris@16 111 for (boost::tie(ei, e_end) = out_edges(src, g); ei != e_end; ++ei)
Chris@16 112 flow += (get(cap, *ei) - get(res, *ei));
Chris@16 113 return flow;
Chris@16 114 } // edmonds_karp_max_flow()
Chris@16 115
Chris@16 116 namespace detail {
Chris@16 117 //-------------------------------------------------------------------------
Chris@16 118 // Handle default for color property map
Chris@16 119
Chris@16 120 // use of class here is a VC++ workaround
Chris@16 121 template <class ColorMap>
Chris@16 122 struct edmonds_karp_dispatch2 {
Chris@16 123 template <class Graph, class PredMap, class P, class T, class R>
Chris@16 124 static typename edge_capacity_value<Graph, P, T, R>::type
Chris@16 125 apply
Chris@16 126 (Graph& g,
Chris@16 127 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 128 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 129 PredMap pred,
Chris@16 130 const bgl_named_params<P, T, R>& params,
Chris@16 131 ColorMap color)
Chris@16 132 {
Chris@16 133 return edmonds_karp_max_flow
Chris@16 134 (g, src, sink,
Chris@16 135 choose_const_pmap(get_param(params, edge_capacity), g, edge_capacity),
Chris@16 136 choose_pmap(get_param(params, edge_residual_capacity),
Chris@16 137 g, edge_residual_capacity),
Chris@16 138 choose_const_pmap(get_param(params, edge_reverse), g, edge_reverse),
Chris@16 139 color, pred);
Chris@16 140 }
Chris@16 141 };
Chris@16 142 template<>
Chris@16 143 struct edmonds_karp_dispatch2<param_not_found> {
Chris@16 144 template <class Graph, class PredMap, class P, class T, class R>
Chris@16 145 static typename edge_capacity_value<Graph, P, T, R>::type
Chris@16 146 apply
Chris@16 147 (Graph& g,
Chris@16 148 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 149 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 150 PredMap pred,
Chris@16 151 const bgl_named_params<P, T, R>& params,
Chris@16 152 param_not_found)
Chris@16 153 {
Chris@16 154 typedef typename graph_traits<Graph>::vertices_size_type size_type;
Chris@16 155 size_type n = is_default_param(get_param(params, vertex_color)) ?
Chris@16 156 num_vertices(g) : 1;
Chris@16 157 std::vector<default_color_type> color_vec(n);
Chris@16 158 return edmonds_karp_max_flow
Chris@16 159 (g, src, sink,
Chris@16 160 choose_const_pmap(get_param(params, edge_capacity), g, edge_capacity),
Chris@16 161 choose_pmap(get_param(params, edge_residual_capacity),
Chris@16 162 g, edge_residual_capacity),
Chris@16 163 choose_const_pmap(get_param(params, edge_reverse), g, edge_reverse),
Chris@16 164 make_iterator_property_map(color_vec.begin(), choose_const_pmap
Chris@16 165 (get_param(params, vertex_index),
Chris@16 166 g, vertex_index), color_vec[0]),
Chris@16 167 pred);
Chris@16 168 }
Chris@16 169 };
Chris@16 170
Chris@16 171 //-------------------------------------------------------------------------
Chris@16 172 // Handle default for predecessor property map
Chris@16 173
Chris@16 174 // use of class here is a VC++ workaround
Chris@16 175 template <class PredMap>
Chris@16 176 struct edmonds_karp_dispatch1 {
Chris@16 177 template <class Graph, class P, class T, class R>
Chris@16 178 static typename edge_capacity_value<Graph, P, T, R>::type
Chris@16 179 apply(Graph& g,
Chris@16 180 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 181 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 182 const bgl_named_params<P, T, R>& params,
Chris@16 183 PredMap pred)
Chris@16 184 {
Chris@16 185 typedef typename get_param_type< vertex_color_t, bgl_named_params<P,T,R> >::type C;
Chris@16 186 return edmonds_karp_dispatch2<C>::apply
Chris@16 187 (g, src, sink, pred, params, get_param(params, vertex_color));
Chris@16 188 }
Chris@16 189 };
Chris@16 190 template<>
Chris@16 191 struct edmonds_karp_dispatch1<param_not_found> {
Chris@16 192
Chris@16 193 template <class Graph, class P, class T, class R>
Chris@16 194 static typename edge_capacity_value<Graph, P, T, R>::type
Chris@16 195 apply
Chris@16 196 (Graph& g,
Chris@16 197 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 198 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 199 const bgl_named_params<P, T, R>& params,
Chris@16 200 param_not_found)
Chris@16 201 {
Chris@16 202 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Chris@16 203 typedef typename graph_traits<Graph>::vertices_size_type size_type;
Chris@16 204 size_type n = is_default_param(get_param(params, vertex_predecessor)) ?
Chris@16 205 num_vertices(g) : 1;
Chris@16 206 std::vector<edge_descriptor> pred_vec(n);
Chris@16 207
Chris@16 208 typedef typename get_param_type< vertex_color_t, bgl_named_params<P,T,R> >::type C;
Chris@16 209 return edmonds_karp_dispatch2<C>::apply
Chris@16 210 (g, src, sink,
Chris@16 211 make_iterator_property_map(pred_vec.begin(), choose_const_pmap
Chris@16 212 (get_param(params, vertex_index),
Chris@16 213 g, vertex_index), pred_vec[0]),
Chris@16 214 params,
Chris@16 215 get_param(params, vertex_color));
Chris@16 216 }
Chris@16 217 };
Chris@16 218
Chris@16 219 } // namespace detail
Chris@16 220
Chris@16 221 template <class Graph, class P, class T, class R>
Chris@16 222 typename detail::edge_capacity_value<Graph, P, T, R>::type
Chris@16 223 edmonds_karp_max_flow
Chris@16 224 (Graph& g,
Chris@16 225 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 226 typename graph_traits<Graph>::vertex_descriptor sink,
Chris@16 227 const bgl_named_params<P, T, R>& params)
Chris@16 228 {
Chris@16 229 typedef typename get_param_type< vertex_predecessor_t, bgl_named_params<P,T,R> >::type Pred;
Chris@16 230 return detail::edmonds_karp_dispatch1<Pred>::apply
Chris@16 231 (g, src, sink, params, get_param(params, vertex_predecessor));
Chris@16 232 }
Chris@16 233
Chris@16 234 template <class Graph>
Chris@16 235 typename property_traits<
Chris@16 236 typename property_map<Graph, edge_capacity_t>::const_type
Chris@16 237 >::value_type
Chris@16 238 edmonds_karp_max_flow
Chris@16 239 (Graph& g,
Chris@16 240 typename graph_traits<Graph>::vertex_descriptor src,
Chris@16 241 typename graph_traits<Graph>::vertex_descriptor sink)
Chris@16 242 {
Chris@16 243 bgl_named_params<int, buffer_param_t> params(0);
Chris@16 244 return edmonds_karp_max_flow(g, src, sink, params);
Chris@16 245 }
Chris@16 246
Chris@16 247 } // namespace boost
Chris@16 248
Chris@16 249 #endif // EDMONDS_KARP_MAX_FLOW_HPP