deba@2034
|
1 |
/* -*- C++ -*-
|
deba@2034
|
2 |
*
|
deba@2034
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2034
|
4 |
*
|
alpar@2391
|
5 |
* Copyright (C) 2003-2007
|
deba@2034
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2034
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2034
|
8 |
*
|
deba@2034
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2034
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2034
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2034
|
12 |
*
|
deba@2034
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2034
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2034
|
15 |
* purpose.
|
deba@2034
|
16 |
*
|
deba@2034
|
17 |
*/
|
deba@2034
|
18 |
|
deba@2034
|
19 |
#ifndef LEMON_EDMONDS_KARP_H
|
deba@2034
|
20 |
#define LEMON_EDMONDS_KARP_H
|
deba@2034
|
21 |
|
deba@2034
|
22 |
/// \file
|
deba@2376
|
23 |
/// \ingroup max_flow
|
deba@2034
|
24 |
/// \brief Implementation of the Edmonds-Karp algorithm.
|
deba@2034
|
25 |
|
deba@2034
|
26 |
#include <lemon/tolerance.h>
|
deba@2514
|
27 |
#include <vector>
|
deba@2034
|
28 |
|
deba@2034
|
29 |
namespace lemon {
|
deba@2034
|
30 |
|
deba@2514
|
31 |
/// \brief Default traits class of EdmondsKarp class.
|
deba@2514
|
32 |
///
|
deba@2514
|
33 |
/// Default traits class of EdmondsKarp class.
|
deba@2514
|
34 |
/// \param _Graph Graph type.
|
deba@2514
|
35 |
/// \param _CapacityMap Type of capacity map.
|
deba@2514
|
36 |
template <typename _Graph, typename _CapacityMap>
|
deba@2514
|
37 |
struct EdmondsKarpDefaultTraits {
|
deba@2514
|
38 |
|
deba@2514
|
39 |
/// \brief The graph type the algorithm runs on.
|
deba@2514
|
40 |
typedef _Graph Graph;
|
deba@2514
|
41 |
|
deba@2514
|
42 |
/// \brief The type of the map that stores the edge capacities.
|
deba@2514
|
43 |
///
|
deba@2514
|
44 |
/// The type of the map that stores the edge capacities.
|
deba@2514
|
45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@2514
|
46 |
typedef _CapacityMap CapacityMap;
|
deba@2514
|
47 |
|
deba@2514
|
48 |
/// \brief The type of the length of the edges.
|
deba@2514
|
49 |
typedef typename CapacityMap::Value Value;
|
deba@2514
|
50 |
|
deba@2514
|
51 |
/// \brief The map type that stores the flow values.
|
deba@2514
|
52 |
///
|
deba@2514
|
53 |
/// The map type that stores the flow values.
|
deba@2514
|
54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
deba@2514
|
55 |
typedef typename Graph::template EdgeMap<Value> FlowMap;
|
deba@2514
|
56 |
|
deba@2514
|
57 |
/// \brief Instantiates a FlowMap.
|
deba@2514
|
58 |
///
|
deba@2514
|
59 |
/// This function instantiates a \ref FlowMap.
|
deba@2514
|
60 |
/// \param graph The graph, to which we would like to define the flow map.
|
deba@2514
|
61 |
static FlowMap* createFlowMap(const Graph& graph) {
|
deba@2514
|
62 |
return new FlowMap(graph);
|
deba@2514
|
63 |
}
|
deba@2514
|
64 |
|
deba@2514
|
65 |
/// \brief The tolerance used by the algorithm
|
deba@2514
|
66 |
///
|
deba@2514
|
67 |
/// The tolerance used by the algorithm to handle inexact computation.
|
deba@2514
|
68 |
typedef Tolerance<Value> Tolerance;
|
deba@2514
|
69 |
|
deba@2514
|
70 |
};
|
deba@2514
|
71 |
|
deba@2376
|
72 |
/// \ingroup max_flow
|
deba@2514
|
73 |
///
|
deba@2034
|
74 |
/// \brief Edmonds-Karp algorithms class.
|
deba@2034
|
75 |
///
|
deba@2034
|
76 |
/// This class provides an implementation of the \e Edmonds-Karp \e
|
deba@2034
|
77 |
/// algorithm producing a flow of maximum value in a directed
|
deba@2514
|
78 |
/// graphs. The Edmonds-Karp algorithm is slower than the Preflow
|
deba@2514
|
79 |
/// algorithm but it has an advantage of the step-by-step execution
|
deba@2514
|
80 |
/// control with feasible flow solutions. The \e source node, the \e
|
deba@2514
|
81 |
/// target node, the \e capacity of the edges and the \e starting \e
|
deba@2514
|
82 |
/// flow value of the edges should be passed to the algorithm
|
deba@2514
|
83 |
/// through the constructor.
|
deba@2034
|
84 |
///
|
deba@2514
|
85 |
/// The time complexity of the algorithm is \f$ O(nm^2) \f$ in
|
deba@2059
|
86 |
/// worst case. Always try the preflow algorithm instead of this if
|
deba@2113
|
87 |
/// you just want to compute the optimal flow.
|
deba@2034
|
88 |
///
|
deba@2034
|
89 |
/// \param _Graph The directed graph type the algorithm runs on.
|
deba@2034
|
90 |
/// \param _CapacityMap The capacity map type.
|
deba@2514
|
91 |
/// \param _Traits Traits class to set various data types used by
|
deba@2514
|
92 |
/// the algorithm. The default traits class is \ref
|
deba@2514
|
93 |
/// EdmondsKarpDefaultTraits. See \ref EdmondsKarpDefaultTraits for the
|
deba@2514
|
94 |
/// documentation of a Edmonds-Karp traits class.
|
deba@2034
|
95 |
///
|
deba@2034
|
96 |
/// \author Balazs Dezso
|
deba@2059
|
97 |
#ifdef DOXYGEN
|
deba@2514
|
98 |
template <typename _Graph, typename _CapacityMap, typename _Traits>
|
deba@2514
|
99 |
#else
|
deba@2514
|
100 |
template <typename _Graph,
|
deba@2514
|
101 |
typename _CapacityMap = typename _Graph::template EdgeMap<int>,
|
deba@2514
|
102 |
typename _Traits = EdmondsKarpDefaultTraits<_Graph, _CapacityMap> >
|
deba@2059
|
103 |
#endif
|
deba@2034
|
104 |
class EdmondsKarp {
|
deba@2034
|
105 |
public:
|
deba@2034
|
106 |
|
deba@2514
|
107 |
typedef _Traits Traits;
|
deba@2514
|
108 |
typedef typename Traits::Graph Graph;
|
deba@2514
|
109 |
typedef typename Traits::CapacityMap CapacityMap;
|
deba@2514
|
110 |
typedef typename Traits::Value Value;
|
deba@2514
|
111 |
|
deba@2514
|
112 |
typedef typename Traits::FlowMap FlowMap;
|
deba@2514
|
113 |
typedef typename Traits::Tolerance Tolerance;
|
deba@2514
|
114 |
|
deba@2034
|
115 |
/// \brief \ref Exception for the case when the source equals the target.
|
deba@2034
|
116 |
///
|
deba@2034
|
117 |
/// \ref Exception for the case when the source equals the target.
|
deba@2034
|
118 |
///
|
deba@2034
|
119 |
class InvalidArgument : public lemon::LogicError {
|
deba@2034
|
120 |
public:
|
alpar@2151
|
121 |
virtual const char* what() const throw() {
|
deba@2034
|
122 |
return "lemon::EdmondsKarp::InvalidArgument";
|
deba@2034
|
123 |
}
|
deba@2034
|
124 |
};
|
deba@2034
|
125 |
|
deba@2034
|
126 |
|
deba@2034
|
127 |
private:
|
deba@2034
|
128 |
|
deba@2514
|
129 |
GRAPH_TYPEDEFS(typename Graph);
|
deba@2514
|
130 |
typedef typename Graph::template NodeMap<Edge> PredMap;
|
deba@2034
|
131 |
|
deba@2514
|
132 |
const Graph& _graph;
|
deba@2514
|
133 |
const CapacityMap* _capacity;
|
deba@2514
|
134 |
|
deba@2514
|
135 |
Node _source, _target;
|
deba@2514
|
136 |
|
deba@2514
|
137 |
FlowMap* _flow;
|
deba@2514
|
138 |
bool _local_flow;
|
deba@2514
|
139 |
|
deba@2514
|
140 |
PredMap* _pred;
|
deba@2514
|
141 |
std::vector<Node> _queue;
|
deba@2514
|
142 |
|
deba@2514
|
143 |
Tolerance _tolerance;
|
deba@2514
|
144 |
Value _flow_value;
|
deba@2514
|
145 |
|
deba@2514
|
146 |
void createStructures() {
|
deba@2514
|
147 |
if (!_flow) {
|
deba@2514
|
148 |
_flow = Traits::createFlowMap(_graph);
|
deba@2514
|
149 |
_local_flow = true;
|
deba@2514
|
150 |
}
|
deba@2514
|
151 |
if (!_pred) {
|
deba@2514
|
152 |
_pred = new PredMap(_graph);
|
deba@2514
|
153 |
}
|
deba@2514
|
154 |
_queue.resize(countNodes(_graph));
|
deba@2514
|
155 |
}
|
deba@2514
|
156 |
|
deba@2514
|
157 |
void destroyStructures() {
|
deba@2514
|
158 |
if (_local_flow) {
|
deba@2514
|
159 |
delete _flow;
|
deba@2514
|
160 |
}
|
deba@2514
|
161 |
if (_pred) {
|
deba@2514
|
162 |
delete _pred;
|
deba@2514
|
163 |
}
|
deba@2514
|
164 |
}
|
deba@2034
|
165 |
|
deba@2034
|
166 |
public:
|
deba@2034
|
167 |
|
deba@2514
|
168 |
///\name Named template parameters
|
deba@2514
|
169 |
|
deba@2514
|
170 |
///@{
|
deba@2514
|
171 |
|
deba@2514
|
172 |
template <typename _FlowMap>
|
deba@2514
|
173 |
struct DefFlowMapTraits : public Traits {
|
deba@2514
|
174 |
typedef _FlowMap FlowMap;
|
deba@2514
|
175 |
static FlowMap *createFlowMap(const Graph&) {
|
deba@2514
|
176 |
throw UninitializedParameter();
|
deba@2514
|
177 |
}
|
deba@2514
|
178 |
};
|
deba@2514
|
179 |
|
deba@2514
|
180 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@2514
|
181 |
/// FlowMap type
|
deba@2514
|
182 |
///
|
deba@2514
|
183 |
/// \ref named-templ-param "Named parameter" for setting FlowMap
|
deba@2514
|
184 |
/// type
|
deba@2514
|
185 |
template <typename _FlowMap>
|
deba@2514
|
186 |
struct DefFlowMap
|
deba@2514
|
187 |
: public EdmondsKarp<Graph, CapacityMap, DefFlowMapTraits<_FlowMap> > {
|
deba@2514
|
188 |
typedef EdmondsKarp<Graph, CapacityMap, DefFlowMapTraits<_FlowMap> >
|
deba@2514
|
189 |
Create;
|
deba@2514
|
190 |
};
|
deba@2514
|
191 |
|
deba@2514
|
192 |
|
deba@2514
|
193 |
/// @}
|
deba@2514
|
194 |
|
deba@2034
|
195 |
/// \brief The constructor of the class.
|
deba@2034
|
196 |
///
|
deba@2034
|
197 |
/// The constructor of the class.
|
deba@2037
|
198 |
/// \param graph The directed graph the algorithm runs on.
|
deba@2514
|
199 |
/// \param capacity The capacity of the edges.
|
deba@2037
|
200 |
/// \param source The source node.
|
deba@2037
|
201 |
/// \param target The target node.
|
deba@2514
|
202 |
EdmondsKarp(const Graph& graph, const CapacityMap& capacity,
|
deba@2514
|
203 |
Node source, Node target)
|
deba@2514
|
204 |
: _graph(graph), _capacity(&capacity), _source(source), _target(target),
|
deba@2514
|
205 |
_flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value()
|
deba@2034
|
206 |
{
|
deba@2034
|
207 |
if (_source == _target) {
|
deba@2034
|
208 |
throw InvalidArgument();
|
deba@2034
|
209 |
}
|
deba@2034
|
210 |
}
|
deba@2034
|
211 |
|
deba@2514
|
212 |
/// \brief Destrcutor.
|
deba@2514
|
213 |
///
|
deba@2514
|
214 |
/// Destructor.
|
deba@2514
|
215 |
~EdmondsKarp() {
|
deba@2514
|
216 |
destroyStructures();
|
deba@2514
|
217 |
}
|
deba@2514
|
218 |
|
deba@2514
|
219 |
/// \brief Sets the capacity map.
|
deba@2514
|
220 |
///
|
deba@2514
|
221 |
/// Sets the capacity map.
|
deba@2514
|
222 |
/// \return \c (*this)
|
deba@2514
|
223 |
EdmondsKarp& capacityMap(const CapacityMap& map) {
|
deba@2514
|
224 |
_capacity = ↦
|
deba@2514
|
225 |
return *this;
|
deba@2514
|
226 |
}
|
deba@2514
|
227 |
|
deba@2514
|
228 |
/// \brief Sets the flow map.
|
deba@2514
|
229 |
///
|
deba@2514
|
230 |
/// Sets the flow map.
|
deba@2514
|
231 |
/// \return \c (*this)
|
deba@2514
|
232 |
EdmondsKarp& flowMap(FlowMap& map) {
|
deba@2514
|
233 |
if (_local_flow) {
|
deba@2514
|
234 |
delete _flow;
|
deba@2514
|
235 |
_local_flow = false;
|
deba@2514
|
236 |
}
|
deba@2514
|
237 |
_flow = ↦
|
deba@2514
|
238 |
return *this;
|
deba@2514
|
239 |
}
|
deba@2514
|
240 |
|
deba@2514
|
241 |
/// \brief Returns the flow map.
|
deba@2514
|
242 |
///
|
deba@2514
|
243 |
/// \return The flow map.
|
deba@2514
|
244 |
const FlowMap& flowMap() {
|
deba@2514
|
245 |
return *_flow;
|
deba@2514
|
246 |
}
|
deba@2514
|
247 |
|
deba@2514
|
248 |
/// \brief Sets the source node.
|
deba@2514
|
249 |
///
|
deba@2514
|
250 |
/// Sets the source node.
|
deba@2514
|
251 |
/// \return \c (*this)
|
deba@2514
|
252 |
EdmondsKarp& source(const Node& node) {
|
deba@2514
|
253 |
_source = node;
|
deba@2514
|
254 |
return *this;
|
deba@2514
|
255 |
}
|
deba@2514
|
256 |
|
deba@2514
|
257 |
/// \brief Sets the target node.
|
deba@2514
|
258 |
///
|
deba@2514
|
259 |
/// Sets the target node.
|
deba@2514
|
260 |
/// \return \c (*this)
|
deba@2514
|
261 |
EdmondsKarp& target(const Node& node) {
|
deba@2514
|
262 |
_target = node;
|
deba@2514
|
263 |
return *this;
|
deba@2514
|
264 |
}
|
deba@2514
|
265 |
|
deba@2514
|
266 |
/// \brief Sets the tolerance used by algorithm.
|
deba@2514
|
267 |
///
|
deba@2514
|
268 |
/// Sets the tolerance used by algorithm.
|
deba@2514
|
269 |
EdmondsKarp& tolerance(const Tolerance& tolerance) const {
|
deba@2514
|
270 |
_tolerance = tolerance;
|
deba@2514
|
271 |
return *this;
|
deba@2514
|
272 |
}
|
deba@2514
|
273 |
|
deba@2514
|
274 |
/// \brief Returns the tolerance used by algorithm.
|
deba@2514
|
275 |
///
|
deba@2514
|
276 |
/// Returns the tolerance used by algorithm.
|
deba@2514
|
277 |
const Tolerance& tolerance() const {
|
deba@2514
|
278 |
return tolerance;
|
deba@2514
|
279 |
}
|
deba@2514
|
280 |
|
deba@2514
|
281 |
/// \name Execution control The simplest way to execute the
|
deba@2514
|
282 |
/// algorithm is to use the \c run() member functions.
|
deba@2514
|
283 |
/// \n
|
deba@2514
|
284 |
/// If you need more control on initial solution or
|
deba@2514
|
285 |
/// execution then you have to call one \ref init() function and then
|
deba@2514
|
286 |
/// the start() or multiple times the \c augment() member function.
|
deba@2514
|
287 |
|
deba@2514
|
288 |
///@{
|
deba@2514
|
289 |
|
deba@2034
|
290 |
/// \brief Initializes the algorithm
|
deba@2034
|
291 |
///
|
deba@2034
|
292 |
/// It sets the flow to empty flow.
|
deba@2034
|
293 |
void init() {
|
deba@2514
|
294 |
createStructures();
|
deba@2034
|
295 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
296 |
_flow->set(it, 0);
|
deba@2034
|
297 |
}
|
deba@2514
|
298 |
_flow_value = 0;
|
deba@2034
|
299 |
}
|
deba@2034
|
300 |
|
deba@2034
|
301 |
/// \brief Initializes the algorithm
|
deba@2034
|
302 |
///
|
deba@2514
|
303 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
304 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
305 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
306 |
/// outgoing flow.
|
deba@2514
|
307 |
template <typename FlowMap>
|
deba@2514
|
308 |
void flowInit(const FlowMap& flowMap) {
|
deba@2514
|
309 |
createStructures();
|
deba@2514
|
310 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
311 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
312 |
}
|
deba@2514
|
313 |
_flow_value = 0;
|
deba@2034
|
314 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
315 |
_flow_value += (*_flow)[jt];
|
deba@2034
|
316 |
}
|
deba@2034
|
317 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
318 |
_flow_value -= (*_flow)[jt];
|
deba@2034
|
319 |
}
|
deba@2034
|
320 |
}
|
deba@2034
|
321 |
|
deba@2034
|
322 |
/// \brief Initializes the algorithm
|
deba@2034
|
323 |
///
|
deba@2514
|
324 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
325 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
326 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
327 |
/// outgoing flow.
|
deba@2514
|
328 |
/// \return %False when the given flowMap does not contain
|
deba@2514
|
329 |
/// feasible flow.
|
deba@2514
|
330 |
template <typename FlowMap>
|
deba@2514
|
331 |
bool checkedFlowInit(const FlowMap& flowMap) {
|
deba@2514
|
332 |
createStructures();
|
deba@2514
|
333 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
334 |
_flow->set(e, flowMap[e]);
|
deba@2034
|
335 |
}
|
deba@2034
|
336 |
for (NodeIt it(_graph); it != INVALID; ++it) {
|
deba@2034
|
337 |
if (it == _source || it == _target) continue;
|
deba@2514
|
338 |
Value outFlow = 0;
|
deba@2034
|
339 |
for (OutEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
340 |
outFlow += (*_flow)[jt];
|
deba@2034
|
341 |
}
|
deba@2514
|
342 |
Value inFlow = 0;
|
deba@2034
|
343 |
for (InEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
344 |
inFlow += (*_flow)[jt];
|
deba@2034
|
345 |
}
|
deba@2034
|
346 |
if (_tolerance.different(outFlow, inFlow)) {
|
deba@2034
|
347 |
return false;
|
deba@2034
|
348 |
}
|
deba@2034
|
349 |
}
|
deba@2034
|
350 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
351 |
if (_tolerance.less((*_flow)[it], 0)) return false;
|
deba@2514
|
352 |
if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false;
|
deba@2514
|
353 |
}
|
deba@2514
|
354 |
_flow_value = 0;
|
deba@2514
|
355 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
356 |
_flow_value += (*_flow)[jt];
|
deba@2514
|
357 |
}
|
deba@2514
|
358 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
359 |
_flow_value -= (*_flow)[jt];
|
deba@2034
|
360 |
}
|
deba@2034
|
361 |
return true;
|
deba@2034
|
362 |
}
|
deba@2034
|
363 |
|
deba@2034
|
364 |
/// \brief Augment the solution on an edge shortest path.
|
deba@2034
|
365 |
///
|
deba@2034
|
366 |
/// Augment the solution on an edge shortest path. It search an
|
deba@2034
|
367 |
/// edge shortest path between the source and the target
|
deba@2034
|
368 |
/// in the residual graph with the bfs algoritm.
|
deba@2034
|
369 |
/// Then it increase the flow on this path with the minimal residual
|
deba@2034
|
370 |
/// capacity on the path. If there is not such path it gives back
|
deba@2034
|
371 |
/// false.
|
deba@2034
|
372 |
/// \return %False when the augmenting is not success so the
|
deba@2034
|
373 |
/// current flow is a feasible and optimal solution.
|
deba@2034
|
374 |
bool augment() {
|
deba@2514
|
375 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
376 |
_pred->set(n, INVALID);
|
deba@2514
|
377 |
}
|
deba@2514
|
378 |
|
deba@2514
|
379 |
int first = 0, last = 1;
|
deba@2514
|
380 |
|
deba@2514
|
381 |
_queue[0] = _source;
|
deba@2514
|
382 |
_pred->set(_source, OutEdgeIt(_graph, _source));
|
deba@2034
|
383 |
|
deba@2514
|
384 |
while (first != last && (*_pred)[_target] == INVALID) {
|
deba@2514
|
385 |
Node n = _queue[first++];
|
deba@2514
|
386 |
|
deba@2514
|
387 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
388 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
389 |
Node t = _graph.target(e);
|
deba@2514
|
390 |
if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
|
deba@2514
|
391 |
_pred->set(t, e);
|
deba@2514
|
392 |
_queue[last++] = t;
|
deba@2514
|
393 |
}
|
deba@2514
|
394 |
}
|
deba@2514
|
395 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
396 |
Value rem = (*_flow)[e];
|
deba@2514
|
397 |
Node t = _graph.source(e);
|
deba@2514
|
398 |
if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
|
deba@2514
|
399 |
_pred->set(t, e);
|
deba@2514
|
400 |
_queue[last++] = t;
|
deba@2514
|
401 |
}
|
deba@2514
|
402 |
}
|
deba@2514
|
403 |
}
|
deba@2034
|
404 |
|
deba@2514
|
405 |
if ((*_pred)[_target] != INVALID) {
|
deba@2514
|
406 |
Node n = _target;
|
deba@2514
|
407 |
Edge e = (*_pred)[n];
|
deba@2514
|
408 |
|
deba@2514
|
409 |
Value prem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
410 |
n = _graph.source(e);
|
deba@2514
|
411 |
while (n != _source) {
|
deba@2514
|
412 |
e = (*_pred)[n];
|
deba@2514
|
413 |
if (_graph.target(e) == n) {
|
deba@2514
|
414 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
415 |
if (rem < prem) prem = rem;
|
deba@2514
|
416 |
n = _graph.source(e);
|
deba@2514
|
417 |
} else {
|
deba@2514
|
418 |
Value rem = (*_flow)[e];
|
deba@2514
|
419 |
if (rem < prem) prem = rem;
|
deba@2514
|
420 |
n = _graph.target(e);
|
deba@2514
|
421 |
}
|
deba@2514
|
422 |
}
|
deba@2514
|
423 |
|
deba@2514
|
424 |
n = _target;
|
deba@2514
|
425 |
e = (*_pred)[n];
|
deba@2514
|
426 |
|
deba@2514
|
427 |
_flow->set(e, (*_flow)[e] + prem);
|
deba@2514
|
428 |
n = _graph.source(e);
|
deba@2514
|
429 |
while (n != _source) {
|
deba@2514
|
430 |
e = (*_pred)[n];
|
deba@2514
|
431 |
if (_graph.target(e) == n) {
|
deba@2514
|
432 |
_flow->set(e, (*_flow)[e] + prem);
|
deba@2514
|
433 |
n = _graph.source(e);
|
deba@2514
|
434 |
} else {
|
deba@2514
|
435 |
_flow->set(e, (*_flow)[e] - prem);
|
deba@2514
|
436 |
n = _graph.target(e);
|
deba@2514
|
437 |
}
|
deba@2514
|
438 |
}
|
deba@2514
|
439 |
|
deba@2514
|
440 |
_flow_value += prem;
|
deba@2514
|
441 |
return true;
|
deba@2514
|
442 |
} else {
|
deba@2514
|
443 |
return false;
|
deba@2034
|
444 |
}
|
deba@2034
|
445 |
}
|
deba@2034
|
446 |
|
deba@2034
|
447 |
/// \brief Executes the algorithm
|
deba@2034
|
448 |
///
|
deba@2034
|
449 |
/// It runs augmenting phases until the optimal solution is reached.
|
deba@2034
|
450 |
void start() {
|
deba@2034
|
451 |
while (augment()) {}
|
deba@2034
|
452 |
}
|
deba@2034
|
453 |
|
deba@2034
|
454 |
/// \brief runs the algorithm.
|
deba@2034
|
455 |
///
|
deba@2034
|
456 |
/// It is just a shorthand for:
|
deba@2059
|
457 |
///
|
deba@2059
|
458 |
///\code
|
deba@2034
|
459 |
/// ek.init();
|
deba@2034
|
460 |
/// ek.start();
|
deba@2059
|
461 |
///\endcode
|
deba@2034
|
462 |
void run() {
|
deba@2034
|
463 |
init();
|
deba@2034
|
464 |
start();
|
deba@2034
|
465 |
}
|
deba@2034
|
466 |
|
deba@2514
|
467 |
/// @}
|
deba@2514
|
468 |
|
deba@2514
|
469 |
/// \name Query Functions
|
deba@2514
|
470 |
/// The result of the %Dijkstra algorithm can be obtained using these
|
deba@2514
|
471 |
/// functions.\n
|
deba@2514
|
472 |
/// Before the use of these functions,
|
deba@2514
|
473 |
/// either run() or start() must be called.
|
deba@2514
|
474 |
|
deba@2514
|
475 |
///@{
|
deba@2514
|
476 |
|
deba@2514
|
477 |
/// \brief Returns the value of the maximum flow.
|
deba@2514
|
478 |
///
|
deba@2514
|
479 |
/// Returns the value of the maximum flow by returning the excess
|
deba@2514
|
480 |
/// of the target node \c t. This value equals to the value of
|
deba@2514
|
481 |
/// the maximum flow already after the first phase.
|
deba@2514
|
482 |
Value flowValue() const {
|
deba@2514
|
483 |
return _flow_value;
|
deba@2514
|
484 |
}
|
deba@2514
|
485 |
|
deba@2514
|
486 |
|
deba@2514
|
487 |
/// \brief Returns the flow on the edge.
|
deba@2514
|
488 |
///
|
deba@2514
|
489 |
/// Sets the \c flowMap to the flow on the edges. This method can
|
deba@2514
|
490 |
/// be called after the second phase of algorithm.
|
deba@2514
|
491 |
Value flow(const Edge& edge) const {
|
deba@2514
|
492 |
return (*_flow)[edge];
|
deba@2514
|
493 |
}
|
deba@2514
|
494 |
|
deba@2514
|
495 |
/// \brief Returns true when the node is on the source side of minimum cut.
|
deba@2514
|
496 |
///
|
deba@2514
|
497 |
|
deba@2514
|
498 |
/// Returns true when the node is on the source side of minimum
|
deba@2514
|
499 |
/// cut. This method can be called both after running \ref
|
deba@2514
|
500 |
/// startFirstPhase() and \ref startSecondPhase().
|
deba@2514
|
501 |
bool minCut(const Node& node) const {
|
deba@2514
|
502 |
return (*_pred)[node] != INVALID;
|
deba@2514
|
503 |
}
|
deba@2514
|
504 |
|
deba@2034
|
505 |
/// \brief Returns a minimum value cut.
|
deba@2034
|
506 |
///
|
deba@2034
|
507 |
/// Sets \c cut to the characteristic vector of a minimum value cut
|
deba@2034
|
508 |
/// It simply calls the minMinCut member.
|
deba@2037
|
509 |
/// \retval cut Write node bool map.
|
deba@2034
|
510 |
template <typename CutMap>
|
deba@2514
|
511 |
void minCutMap(CutMap& cutMap) const {
|
deba@2514
|
512 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
513 |
cutMap.set(n, (*_pred)[n] != INVALID);
|
deba@2514
|
514 |
}
|
deba@2514
|
515 |
cutMap.set(_source, true);
|
deba@2514
|
516 |
}
|
deba@2034
|
517 |
|
deba@2514
|
518 |
/// @}
|
deba@2034
|
519 |
|
deba@2034
|
520 |
};
|
deba@2034
|
521 |
|
deba@2034
|
522 |
}
|
deba@2034
|
523 |
|
deba@2034
|
524 |
#endif
|