alpar@389
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@389
|
2 |
*
|
alpar@389
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@389
|
4 |
*
|
alpar@877
|
5 |
* Copyright (C) 2003-2010
|
alpar@389
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@389
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@389
|
8 |
*
|
alpar@389
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@389
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@389
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@389
|
12 |
*
|
alpar@389
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@389
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@389
|
15 |
* purpose.
|
alpar@389
|
16 |
*
|
alpar@389
|
17 |
*/
|
alpar@389
|
18 |
|
alpar@389
|
19 |
#ifndef LEMON_PREFLOW_H
|
alpar@389
|
20 |
#define LEMON_PREFLOW_H
|
alpar@389
|
21 |
|
alpar@389
|
22 |
#include <lemon/tolerance.h>
|
alpar@389
|
23 |
#include <lemon/elevator.h>
|
alpar@389
|
24 |
|
alpar@389
|
25 |
/// \file
|
alpar@389
|
26 |
/// \ingroup max_flow
|
alpar@389
|
27 |
/// \brief Implementation of the preflow algorithm.
|
alpar@389
|
28 |
|
alpar@389
|
29 |
namespace lemon {
|
alpar@389
|
30 |
|
alpar@389
|
31 |
/// \brief Default traits class of Preflow class.
|
alpar@389
|
32 |
///
|
alpar@389
|
33 |
/// Default traits class of Preflow class.
|
kpeter@503
|
34 |
/// \tparam GR Digraph type.
|
kpeter@559
|
35 |
/// \tparam CAP Capacity map type.
|
kpeter@559
|
36 |
template <typename GR, typename CAP>
|
alpar@389
|
37 |
struct PreflowDefaultTraits {
|
alpar@389
|
38 |
|
kpeter@393
|
39 |
/// \brief The type of the digraph the algorithm runs on.
|
kpeter@503
|
40 |
typedef GR Digraph;
|
alpar@389
|
41 |
|
alpar@389
|
42 |
/// \brief The type of the map that stores the arc capacities.
|
alpar@389
|
43 |
///
|
alpar@389
|
44 |
/// The type of the map that stores the arc capacities.
|
alpar@389
|
45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
kpeter@559
|
46 |
typedef CAP CapacityMap;
|
alpar@389
|
47 |
|
kpeter@393
|
48 |
/// \brief The type of the flow values.
|
kpeter@641
|
49 |
typedef typename CapacityMap::Value Value;
|
alpar@389
|
50 |
|
kpeter@393
|
51 |
/// \brief The type of the map that stores the flow values.
|
alpar@389
|
52 |
///
|
kpeter@393
|
53 |
/// The type of the map that stores the flow values.
|
alpar@389
|
54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
kpeter@713
|
55 |
#ifdef DOXYGEN
|
kpeter@713
|
56 |
typedef GR::ArcMap<Value> FlowMap;
|
kpeter@713
|
57 |
#else
|
kpeter@641
|
58 |
typedef typename Digraph::template ArcMap<Value> FlowMap;
|
kpeter@713
|
59 |
#endif
|
alpar@389
|
60 |
|
alpar@389
|
61 |
/// \brief Instantiates a FlowMap.
|
alpar@389
|
62 |
///
|
alpar@389
|
63 |
/// This function instantiates a \ref FlowMap.
|
kpeter@610
|
64 |
/// \param digraph The digraph for which we would like to define
|
alpar@389
|
65 |
/// the flow map.
|
alpar@389
|
66 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
alpar@389
|
67 |
return new FlowMap(digraph);
|
alpar@389
|
68 |
}
|
alpar@389
|
69 |
|
kpeter@393
|
70 |
/// \brief The elevator type used by Preflow algorithm.
|
alpar@389
|
71 |
///
|
alpar@389
|
72 |
/// The elevator type used by Preflow algorithm.
|
alpar@389
|
73 |
///
|
kpeter@713
|
74 |
/// \sa Elevator, LinkedElevator
|
kpeter@713
|
75 |
#ifdef DOXYGEN
|
kpeter@713
|
76 |
typedef lemon::Elevator<GR, GR::Node> Elevator;
|
kpeter@713
|
77 |
#else
|
kpeter@713
|
78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
|
kpeter@713
|
79 |
#endif
|
alpar@389
|
80 |
|
alpar@389
|
81 |
/// \brief Instantiates an Elevator.
|
alpar@389
|
82 |
///
|
kpeter@393
|
83 |
/// This function instantiates an \ref Elevator.
|
kpeter@610
|
84 |
/// \param digraph The digraph for which we would like to define
|
alpar@389
|
85 |
/// the elevator.
|
alpar@389
|
86 |
/// \param max_level The maximum level of the elevator.
|
alpar@389
|
87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
alpar@389
|
88 |
return new Elevator(digraph, max_level);
|
alpar@389
|
89 |
}
|
alpar@389
|
90 |
|
alpar@389
|
91 |
/// \brief The tolerance used by the algorithm
|
alpar@389
|
92 |
///
|
alpar@389
|
93 |
/// The tolerance used by the algorithm to handle inexact computation.
|
kpeter@641
|
94 |
typedef lemon::Tolerance<Value> Tolerance;
|
alpar@389
|
95 |
|
alpar@389
|
96 |
};
|
alpar@389
|
97 |
|
alpar@389
|
98 |
|
alpar@389
|
99 |
/// \ingroup max_flow
|
alpar@389
|
100 |
///
|
kpeter@393
|
101 |
/// \brief %Preflow algorithm class.
|
alpar@389
|
102 |
///
|
kpeter@393
|
103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow
|
kpeter@559
|
104 |
/// \e push-relabel algorithm producing a \ref max_flow
|
alpar@1053
|
105 |
/// "flow of maximum value" in a digraph \cite clrs01algorithms,
|
alpar@1053
|
106 |
/// \cite amo93networkflows, \cite goldberg88newapproach.
|
kpeter@559
|
107 |
/// The preflow algorithms are the fastest known maximum
|
kpeter@689
|
108 |
/// flow algorithms. The current implementation uses a mixture of the
|
alpar@389
|
109 |
/// \e "highest label" and the \e "bound decrease" heuristics.
|
kpeter@1080
|
110 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{m})\f$.
|
alpar@389
|
111 |
///
|
kpeter@393
|
112 |
/// The algorithm consists of two phases. After the first phase
|
kpeter@393
|
113 |
/// the maximum flow value and the minimum cut is obtained. The
|
kpeter@393
|
114 |
/// second phase constructs a feasible maximum flow on each arc.
|
alpar@389
|
115 |
///
|
kpeter@823
|
116 |
/// \warning This implementation cannot handle infinite or very large
|
kpeter@823
|
117 |
/// capacities (e.g. the maximum value of \c CAP::Value).
|
kpeter@823
|
118 |
///
|
kpeter@503
|
119 |
/// \tparam GR The type of the digraph the algorithm runs on.
|
kpeter@559
|
120 |
/// \tparam CAP The type of the capacity map. The default map
|
kpeter@503
|
121 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
|
kpeter@825
|
122 |
/// \tparam TR The traits class that defines various types used by the
|
kpeter@825
|
123 |
/// algorithm. By default, it is \ref PreflowDefaultTraits
|
kpeter@825
|
124 |
/// "PreflowDefaultTraits<GR, CAP>".
|
kpeter@825
|
125 |
/// In most cases, this parameter should not be set directly,
|
kpeter@825
|
126 |
/// consider to use the named template parameters instead.
|
alpar@389
|
127 |
#ifdef DOXYGEN
|
kpeter@559
|
128 |
template <typename GR, typename CAP, typename TR>
|
alpar@389
|
129 |
#else
|
kpeter@503
|
130 |
template <typename GR,
|
kpeter@559
|
131 |
typename CAP = typename GR::template ArcMap<int>,
|
kpeter@559
|
132 |
typename TR = PreflowDefaultTraits<GR, CAP> >
|
alpar@389
|
133 |
#endif
|
alpar@389
|
134 |
class Preflow {
|
alpar@389
|
135 |
public:
|
alpar@389
|
136 |
|
alpar@1074
|
137 |
///The \ref lemon::PreflowDefaultTraits "traits class" of the algorithm.
|
kpeter@503
|
138 |
typedef TR Traits;
|
kpeter@393
|
139 |
///The type of the digraph the algorithm runs on.
|
alpar@389
|
140 |
typedef typename Traits::Digraph Digraph;
|
kpeter@393
|
141 |
///The type of the capacity map.
|
alpar@389
|
142 |
typedef typename Traits::CapacityMap CapacityMap;
|
kpeter@393
|
143 |
///The type of the flow values.
|
kpeter@641
|
144 |
typedef typename Traits::Value Value;
|
alpar@389
|
145 |
|
kpeter@393
|
146 |
///The type of the flow map.
|
alpar@389
|
147 |
typedef typename Traits::FlowMap FlowMap;
|
kpeter@393
|
148 |
///The type of the elevator.
|
alpar@389
|
149 |
typedef typename Traits::Elevator Elevator;
|
kpeter@393
|
150 |
///The type of the tolerance.
|
alpar@389
|
151 |
typedef typename Traits::Tolerance Tolerance;
|
alpar@389
|
152 |
|
alpar@389
|
153 |
private:
|
alpar@389
|
154 |
|
alpar@389
|
155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
alpar@389
|
156 |
|
alpar@389
|
157 |
const Digraph& _graph;
|
alpar@389
|
158 |
const CapacityMap* _capacity;
|
alpar@389
|
159 |
|
alpar@389
|
160 |
int _node_num;
|
alpar@389
|
161 |
|
alpar@389
|
162 |
Node _source, _target;
|
alpar@389
|
163 |
|
alpar@389
|
164 |
FlowMap* _flow;
|
alpar@389
|
165 |
bool _local_flow;
|
alpar@389
|
166 |
|
alpar@389
|
167 |
Elevator* _level;
|
alpar@389
|
168 |
bool _local_level;
|
alpar@389
|
169 |
|
kpeter@641
|
170 |
typedef typename Digraph::template NodeMap<Value> ExcessMap;
|
alpar@389
|
171 |
ExcessMap* _excess;
|
alpar@389
|
172 |
|
alpar@389
|
173 |
Tolerance _tolerance;
|
alpar@389
|
174 |
|
alpar@389
|
175 |
bool _phase;
|
alpar@389
|
176 |
|
alpar@389
|
177 |
|
alpar@389
|
178 |
void createStructures() {
|
alpar@389
|
179 |
_node_num = countNodes(_graph);
|
alpar@389
|
180 |
|
alpar@389
|
181 |
if (!_flow) {
|
alpar@389
|
182 |
_flow = Traits::createFlowMap(_graph);
|
alpar@389
|
183 |
_local_flow = true;
|
alpar@389
|
184 |
}
|
alpar@389
|
185 |
if (!_level) {
|
alpar@389
|
186 |
_level = Traits::createElevator(_graph, _node_num);
|
alpar@389
|
187 |
_local_level = true;
|
alpar@389
|
188 |
}
|
alpar@389
|
189 |
if (!_excess) {
|
alpar@389
|
190 |
_excess = new ExcessMap(_graph);
|
alpar@389
|
191 |
}
|
alpar@389
|
192 |
}
|
alpar@389
|
193 |
|
alpar@389
|
194 |
void destroyStructures() {
|
alpar@389
|
195 |
if (_local_flow) {
|
alpar@389
|
196 |
delete _flow;
|
alpar@389
|
197 |
}
|
alpar@389
|
198 |
if (_local_level) {
|
alpar@389
|
199 |
delete _level;
|
alpar@389
|
200 |
}
|
alpar@389
|
201 |
if (_excess) {
|
alpar@389
|
202 |
delete _excess;
|
alpar@389
|
203 |
}
|
alpar@389
|
204 |
}
|
alpar@389
|
205 |
|
alpar@389
|
206 |
public:
|
alpar@389
|
207 |
|
alpar@389
|
208 |
typedef Preflow Create;
|
alpar@389
|
209 |
|
kpeter@393
|
210 |
///\name Named Template Parameters
|
alpar@389
|
211 |
|
alpar@389
|
212 |
///@{
|
alpar@389
|
213 |
|
kpeter@559
|
214 |
template <typename T>
|
alpar@391
|
215 |
struct SetFlowMapTraits : public Traits {
|
kpeter@559
|
216 |
typedef T FlowMap;
|
alpar@389
|
217 |
static FlowMap *createFlowMap(const Digraph&) {
|
alpar@390
|
218 |
LEMON_ASSERT(false, "FlowMap is not initialized");
|
alpar@390
|
219 |
return 0; // ignore warnings
|
alpar@389
|
220 |
}
|
alpar@389
|
221 |
};
|
alpar@389
|
222 |
|
alpar@389
|
223 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
alpar@389
|
224 |
/// FlowMap type
|
alpar@389
|
225 |
///
|
alpar@389
|
226 |
/// \ref named-templ-param "Named parameter" for setting FlowMap
|
kpeter@393
|
227 |
/// type.
|
kpeter@559
|
228 |
template <typename T>
|
alpar@391
|
229 |
struct SetFlowMap
|
kpeter@559
|
230 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > {
|
alpar@389
|
231 |
typedef Preflow<Digraph, CapacityMap,
|
kpeter@559
|
232 |
SetFlowMapTraits<T> > Create;
|
alpar@389
|
233 |
};
|
alpar@389
|
234 |
|
kpeter@559
|
235 |
template <typename T>
|
alpar@391
|
236 |
struct SetElevatorTraits : public Traits {
|
kpeter@559
|
237 |
typedef T Elevator;
|
alpar@389
|
238 |
static Elevator *createElevator(const Digraph&, int) {
|
alpar@390
|
239 |
LEMON_ASSERT(false, "Elevator is not initialized");
|
alpar@390
|
240 |
return 0; // ignore warnings
|
alpar@389
|
241 |
}
|
alpar@389
|
242 |
};
|
alpar@389
|
243 |
|
alpar@389
|
244 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
alpar@389
|
245 |
/// Elevator type
|
alpar@389
|
246 |
///
|
alpar@389
|
247 |
/// \ref named-templ-param "Named parameter" for setting Elevator
|
kpeter@393
|
248 |
/// type. If this named parameter is used, then an external
|
kpeter@393
|
249 |
/// elevator object must be passed to the algorithm using the
|
kpeter@393
|
250 |
/// \ref elevator(Elevator&) "elevator()" function before calling
|
kpeter@393
|
251 |
/// \ref run() or \ref init().
|
kpeter@393
|
252 |
/// \sa SetStandardElevator
|
kpeter@559
|
253 |
template <typename T>
|
alpar@391
|
254 |
struct SetElevator
|
kpeter@559
|
255 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > {
|
alpar@389
|
256 |
typedef Preflow<Digraph, CapacityMap,
|
kpeter@559
|
257 |
SetElevatorTraits<T> > Create;
|
alpar@389
|
258 |
};
|
alpar@389
|
259 |
|
kpeter@559
|
260 |
template <typename T>
|
alpar@391
|
261 |
struct SetStandardElevatorTraits : public Traits {
|
kpeter@559
|
262 |
typedef T Elevator;
|
alpar@389
|
263 |
static Elevator *createElevator(const Digraph& digraph, int max_level) {
|
alpar@389
|
264 |
return new Elevator(digraph, max_level);
|
alpar@389
|
265 |
}
|
alpar@389
|
266 |
};
|
alpar@389
|
267 |
|
alpar@389
|
268 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@393
|
269 |
/// Elevator type with automatic allocation
|
alpar@389
|
270 |
///
|
alpar@389
|
271 |
/// \ref named-templ-param "Named parameter" for setting Elevator
|
kpeter@393
|
272 |
/// type with automatic allocation.
|
kpeter@393
|
273 |
/// The Elevator should have standard constructor interface to be
|
kpeter@393
|
274 |
/// able to automatically created by the algorithm (i.e. the
|
kpeter@393
|
275 |
/// digraph and the maximum level should be passed to it).
|
kpeter@786
|
276 |
/// However, an external elevator object could also be passed to the
|
kpeter@393
|
277 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function
|
kpeter@393
|
278 |
/// before calling \ref run() or \ref init().
|
kpeter@393
|
279 |
/// \sa SetElevator
|
kpeter@559
|
280 |
template <typename T>
|
alpar@391
|
281 |
struct SetStandardElevator
|
alpar@389
|
282 |
: public Preflow<Digraph, CapacityMap,
|
kpeter@559
|
283 |
SetStandardElevatorTraits<T> > {
|
alpar@389
|
284 |
typedef Preflow<Digraph, CapacityMap,
|
kpeter@559
|
285 |
SetStandardElevatorTraits<T> > Create;
|
alpar@389
|
286 |
};
|
alpar@389
|
287 |
|
alpar@389
|
288 |
/// @}
|
alpar@389
|
289 |
|
alpar@389
|
290 |
protected:
|
alpar@389
|
291 |
|
alpar@389
|
292 |
Preflow() {}
|
alpar@389
|
293 |
|
alpar@389
|
294 |
public:
|
alpar@389
|
295 |
|
alpar@389
|
296 |
|
alpar@389
|
297 |
/// \brief The constructor of the class.
|
alpar@389
|
298 |
///
|
alpar@389
|
299 |
/// The constructor of the class.
|
alpar@389
|
300 |
/// \param digraph The digraph the algorithm runs on.
|
alpar@389
|
301 |
/// \param capacity The capacity of the arcs.
|
alpar@389
|
302 |
/// \param source The source node.
|
alpar@389
|
303 |
/// \param target The target node.
|
alpar@389
|
304 |
Preflow(const Digraph& digraph, const CapacityMap& capacity,
|
kpeter@393
|
305 |
Node source, Node target)
|
alpar@389
|
306 |
: _graph(digraph), _capacity(&capacity),
|
alpar@389
|
307 |
_node_num(0), _source(source), _target(target),
|
alpar@389
|
308 |
_flow(0), _local_flow(false),
|
alpar@389
|
309 |
_level(0), _local_level(false),
|
alpar@389
|
310 |
_excess(0), _tolerance(), _phase() {}
|
alpar@389
|
311 |
|
kpeter@393
|
312 |
/// \brief Destructor.
|
alpar@389
|
313 |
///
|
alpar@389
|
314 |
/// Destructor.
|
alpar@389
|
315 |
~Preflow() {
|
alpar@389
|
316 |
destroyStructures();
|
alpar@389
|
317 |
}
|
alpar@389
|
318 |
|
alpar@389
|
319 |
/// \brief Sets the capacity map.
|
alpar@389
|
320 |
///
|
alpar@389
|
321 |
/// Sets the capacity map.
|
kpeter@393
|
322 |
/// \return <tt>(*this)</tt>
|
alpar@389
|
323 |
Preflow& capacityMap(const CapacityMap& map) {
|
alpar@389
|
324 |
_capacity = ↦
|
alpar@389
|
325 |
return *this;
|
alpar@389
|
326 |
}
|
alpar@389
|
327 |
|
alpar@389
|
328 |
/// \brief Sets the flow map.
|
alpar@389
|
329 |
///
|
alpar@389
|
330 |
/// Sets the flow map.
|
kpeter@393
|
331 |
/// If you don't use this function before calling \ref run() or
|
kpeter@393
|
332 |
/// \ref init(), an instance will be allocated automatically.
|
kpeter@393
|
333 |
/// The destructor deallocates this automatically allocated map,
|
kpeter@393
|
334 |
/// of course.
|
kpeter@393
|
335 |
/// \return <tt>(*this)</tt>
|
alpar@389
|
336 |
Preflow& flowMap(FlowMap& map) {
|
alpar@389
|
337 |
if (_local_flow) {
|
alpar@389
|
338 |
delete _flow;
|
alpar@389
|
339 |
_local_flow = false;
|
alpar@389
|
340 |
}
|
alpar@389
|
341 |
_flow = ↦
|
alpar@389
|
342 |
return *this;
|
alpar@389
|
343 |
}
|
alpar@389
|
344 |
|
kpeter@393
|
345 |
/// \brief Sets the source node.
|
alpar@389
|
346 |
///
|
kpeter@393
|
347 |
/// Sets the source node.
|
kpeter@393
|
348 |
/// \return <tt>(*this)</tt>
|
kpeter@393
|
349 |
Preflow& source(const Node& node) {
|
kpeter@393
|
350 |
_source = node;
|
kpeter@393
|
351 |
return *this;
|
alpar@389
|
352 |
}
|
alpar@389
|
353 |
|
kpeter@393
|
354 |
/// \brief Sets the target node.
|
alpar@389
|
355 |
///
|
kpeter@393
|
356 |
/// Sets the target node.
|
kpeter@393
|
357 |
/// \return <tt>(*this)</tt>
|
kpeter@393
|
358 |
Preflow& target(const Node& node) {
|
kpeter@393
|
359 |
_target = node;
|
kpeter@393
|
360 |
return *this;
|
kpeter@393
|
361 |
}
|
kpeter@393
|
362 |
|
kpeter@393
|
363 |
/// \brief Sets the elevator used by algorithm.
|
kpeter@393
|
364 |
///
|
kpeter@393
|
365 |
/// Sets the elevator used by algorithm.
|
kpeter@393
|
366 |
/// If you don't use this function before calling \ref run() or
|
kpeter@393
|
367 |
/// \ref init(), an instance will be allocated automatically.
|
kpeter@393
|
368 |
/// The destructor deallocates this automatically allocated elevator,
|
kpeter@393
|
369 |
/// of course.
|
kpeter@393
|
370 |
/// \return <tt>(*this)</tt>
|
alpar@389
|
371 |
Preflow& elevator(Elevator& elevator) {
|
alpar@389
|
372 |
if (_local_level) {
|
alpar@389
|
373 |
delete _level;
|
alpar@389
|
374 |
_local_level = false;
|
alpar@389
|
375 |
}
|
alpar@389
|
376 |
_level = &elevator;
|
alpar@389
|
377 |
return *this;
|
alpar@389
|
378 |
}
|
alpar@389
|
379 |
|
kpeter@393
|
380 |
/// \brief Returns a const reference to the elevator.
|
alpar@389
|
381 |
///
|
kpeter@393
|
382 |
/// Returns a const reference to the elevator.
|
kpeter@393
|
383 |
///
|
kpeter@393
|
384 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
385 |
/// using this function.
|
kpeter@420
|
386 |
const Elevator& elevator() const {
|
alpar@389
|
387 |
return *_level;
|
alpar@389
|
388 |
}
|
alpar@389
|
389 |
|
kpeter@689
|
390 |
/// \brief Sets the tolerance used by the algorithm.
|
alpar@389
|
391 |
///
|
kpeter@689
|
392 |
/// Sets the tolerance object used by the algorithm.
|
kpeter@689
|
393 |
/// \return <tt>(*this)</tt>
|
kpeter@688
|
394 |
Preflow& tolerance(const Tolerance& tolerance) {
|
alpar@389
|
395 |
_tolerance = tolerance;
|
alpar@389
|
396 |
return *this;
|
alpar@389
|
397 |
}
|
alpar@389
|
398 |
|
kpeter@393
|
399 |
/// \brief Returns a const reference to the tolerance.
|
alpar@389
|
400 |
///
|
kpeter@689
|
401 |
/// Returns a const reference to the tolerance object used by
|
kpeter@689
|
402 |
/// the algorithm.
|
alpar@389
|
403 |
const Tolerance& tolerance() const {
|
kpeter@688
|
404 |
return _tolerance;
|
alpar@389
|
405 |
}
|
alpar@389
|
406 |
|
kpeter@393
|
407 |
/// \name Execution Control
|
kpeter@393
|
408 |
/// The simplest way to execute the preflow algorithm is to use
|
kpeter@393
|
409 |
/// \ref run() or \ref runMinCut().\n
|
kpeter@713
|
410 |
/// If you need better control on the initial solution or the execution,
|
kpeter@713
|
411 |
/// you have to call one of the \ref init() functions first, then
|
kpeter@393
|
412 |
/// \ref startFirstPhase() and if you need it \ref startSecondPhase().
|
alpar@389
|
413 |
|
alpar@389
|
414 |
///@{
|
alpar@389
|
415 |
|
alpar@389
|
416 |
/// \brief Initializes the internal data structures.
|
alpar@389
|
417 |
///
|
kpeter@393
|
418 |
/// Initializes the internal data structures and sets the initial
|
kpeter@393
|
419 |
/// flow to zero on each arc.
|
alpar@389
|
420 |
void init() {
|
alpar@389
|
421 |
createStructures();
|
alpar@389
|
422 |
|
alpar@389
|
423 |
_phase = true;
|
alpar@389
|
424 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@581
|
425 |
(*_excess)[n] = 0;
|
alpar@389
|
426 |
}
|
alpar@389
|
427 |
|
alpar@389
|
428 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
alpar@389
|
429 |
_flow->set(e, 0);
|
alpar@389
|
430 |
}
|
alpar@389
|
431 |
|
alpar@389
|
432 |
typename Digraph::template NodeMap<bool> reached(_graph, false);
|
alpar@389
|
433 |
|
alpar@389
|
434 |
_level->initStart();
|
alpar@389
|
435 |
_level->initAddItem(_target);
|
alpar@389
|
436 |
|
alpar@389
|
437 |
std::vector<Node> queue;
|
kpeter@581
|
438 |
reached[_source] = true;
|
alpar@389
|
439 |
|
alpar@389
|
440 |
queue.push_back(_target);
|
kpeter@581
|
441 |
reached[_target] = true;
|
alpar@389
|
442 |
while (!queue.empty()) {
|
alpar@389
|
443 |
_level->initNewLevel();
|
alpar@389
|
444 |
std::vector<Node> nqueue;
|
alpar@389
|
445 |
for (int i = 0; i < int(queue.size()); ++i) {
|
alpar@389
|
446 |
Node n = queue[i];
|
alpar@389
|
447 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
448 |
Node u = _graph.source(e);
|
alpar@389
|
449 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
|
kpeter@581
|
450 |
reached[u] = true;
|
alpar@389
|
451 |
_level->initAddItem(u);
|
alpar@389
|
452 |
nqueue.push_back(u);
|
alpar@389
|
453 |
}
|
alpar@389
|
454 |
}
|
alpar@389
|
455 |
}
|
alpar@389
|
456 |
queue.swap(nqueue);
|
alpar@389
|
457 |
}
|
alpar@389
|
458 |
_level->initFinish();
|
alpar@389
|
459 |
|
alpar@389
|
460 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
|
alpar@389
|
461 |
if (_tolerance.positive((*_capacity)[e])) {
|
alpar@389
|
462 |
Node u = _graph.target(e);
|
alpar@389
|
463 |
if ((*_level)[u] == _level->maxLevel()) continue;
|
alpar@389
|
464 |
_flow->set(e, (*_capacity)[e]);
|
kpeter@581
|
465 |
(*_excess)[u] += (*_capacity)[e];
|
alpar@389
|
466 |
if (u != _target && !_level->active(u)) {
|
alpar@389
|
467 |
_level->activate(u);
|
alpar@389
|
468 |
}
|
alpar@389
|
469 |
}
|
alpar@389
|
470 |
}
|
alpar@389
|
471 |
}
|
alpar@389
|
472 |
|
kpeter@393
|
473 |
/// \brief Initializes the internal data structures using the
|
kpeter@393
|
474 |
/// given flow map.
|
alpar@389
|
475 |
///
|
alpar@389
|
476 |
/// Initializes the internal data structures and sets the initial
|
alpar@389
|
477 |
/// flow to the given \c flowMap. The \c flowMap should contain a
|
kpeter@393
|
478 |
/// flow or at least a preflow, i.e. at each node excluding the
|
kpeter@393
|
479 |
/// source node the incoming flow should greater or equal to the
|
alpar@389
|
480 |
/// outgoing flow.
|
kpeter@393
|
481 |
/// \return \c false if the given \c flowMap is not a preflow.
|
alpar@389
|
482 |
template <typename FlowMap>
|
kpeter@392
|
483 |
bool init(const FlowMap& flowMap) {
|
alpar@389
|
484 |
createStructures();
|
alpar@389
|
485 |
|
alpar@389
|
486 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
alpar@389
|
487 |
_flow->set(e, flowMap[e]);
|
alpar@389
|
488 |
}
|
alpar@389
|
489 |
|
alpar@389
|
490 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@641
|
491 |
Value excess = 0;
|
alpar@389
|
492 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
493 |
excess += (*_flow)[e];
|
alpar@389
|
494 |
}
|
alpar@389
|
495 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
496 |
excess -= (*_flow)[e];
|
alpar@389
|
497 |
}
|
alpar@389
|
498 |
if (excess < 0 && n != _source) return false;
|
kpeter@581
|
499 |
(*_excess)[n] = excess;
|
alpar@389
|
500 |
}
|
alpar@389
|
501 |
|
alpar@389
|
502 |
typename Digraph::template NodeMap<bool> reached(_graph, false);
|
alpar@389
|
503 |
|
alpar@389
|
504 |
_level->initStart();
|
alpar@389
|
505 |
_level->initAddItem(_target);
|
alpar@389
|
506 |
|
alpar@389
|
507 |
std::vector<Node> queue;
|
kpeter@581
|
508 |
reached[_source] = true;
|
alpar@389
|
509 |
|
alpar@389
|
510 |
queue.push_back(_target);
|
kpeter@581
|
511 |
reached[_target] = true;
|
alpar@389
|
512 |
while (!queue.empty()) {
|
alpar@389
|
513 |
_level->initNewLevel();
|
alpar@389
|
514 |
std::vector<Node> nqueue;
|
alpar@389
|
515 |
for (int i = 0; i < int(queue.size()); ++i) {
|
alpar@389
|
516 |
Node n = queue[i];
|
alpar@389
|
517 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
518 |
Node u = _graph.source(e);
|
alpar@389
|
519 |
if (!reached[u] &&
|
alpar@389
|
520 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
kpeter@581
|
521 |
reached[u] = true;
|
alpar@389
|
522 |
_level->initAddItem(u);
|
alpar@389
|
523 |
nqueue.push_back(u);
|
alpar@389
|
524 |
}
|
alpar@389
|
525 |
}
|
alpar@389
|
526 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
527 |
Node v = _graph.target(e);
|
alpar@389
|
528 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
|
kpeter@581
|
529 |
reached[v] = true;
|
alpar@389
|
530 |
_level->initAddItem(v);
|
alpar@389
|
531 |
nqueue.push_back(v);
|
alpar@389
|
532 |
}
|
alpar@389
|
533 |
}
|
alpar@389
|
534 |
}
|
alpar@389
|
535 |
queue.swap(nqueue);
|
alpar@389
|
536 |
}
|
alpar@389
|
537 |
_level->initFinish();
|
alpar@389
|
538 |
|
alpar@389
|
539 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
|
kpeter@641
|
540 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
alpar@389
|
541 |
if (_tolerance.positive(rem)) {
|
alpar@389
|
542 |
Node u = _graph.target(e);
|
alpar@389
|
543 |
if ((*_level)[u] == _level->maxLevel()) continue;
|
alpar@389
|
544 |
_flow->set(e, (*_capacity)[e]);
|
kpeter@581
|
545 |
(*_excess)[u] += rem;
|
alpar@389
|
546 |
}
|
alpar@389
|
547 |
}
|
alpar@389
|
548 |
for (InArcIt e(_graph, _source); e != INVALID; ++e) {
|
kpeter@641
|
549 |
Value rem = (*_flow)[e];
|
alpar@389
|
550 |
if (_tolerance.positive(rem)) {
|
alpar@389
|
551 |
Node v = _graph.source(e);
|
alpar@389
|
552 |
if ((*_level)[v] == _level->maxLevel()) continue;
|
alpar@389
|
553 |
_flow->set(e, 0);
|
kpeter@581
|
554 |
(*_excess)[v] += rem;
|
alpar@389
|
555 |
}
|
alpar@389
|
556 |
}
|
alpar@923
|
557 |
for (NodeIt n(_graph); n != INVALID; ++n)
|
alpar@923
|
558 |
if(n!=_source && n!=_target && _tolerance.positive((*_excess)[n]))
|
alpar@923
|
559 |
_level->activate(n);
|
alpar@923
|
560 |
|
alpar@389
|
561 |
return true;
|
alpar@389
|
562 |
}
|
alpar@389
|
563 |
|
alpar@389
|
564 |
/// \brief Starts the first phase of the preflow algorithm.
|
alpar@389
|
565 |
///
|
alpar@389
|
566 |
/// The preflow algorithm consists of two phases, this method runs
|
alpar@389
|
567 |
/// the first phase. After the first phase the maximum flow value
|
alpar@389
|
568 |
/// and a minimum value cut can already be computed, although a
|
alpar@389
|
569 |
/// maximum flow is not yet obtained. So after calling this method
|
alpar@389
|
570 |
/// \ref flowValue() returns the value of a maximum flow and \ref
|
alpar@389
|
571 |
/// minCut() returns a minimum cut.
|
kpeter@393
|
572 |
/// \pre One of the \ref init() functions must be called before
|
kpeter@393
|
573 |
/// using this function.
|
alpar@389
|
574 |
void startFirstPhase() {
|
alpar@389
|
575 |
_phase = true;
|
alpar@389
|
576 |
|
deba@891
|
577 |
while (true) {
|
alpar@389
|
578 |
int num = _node_num;
|
alpar@389
|
579 |
|
deba@891
|
580 |
Node n = INVALID;
|
deba@891
|
581 |
int level = -1;
|
deba@891
|
582 |
|
deba@891
|
583 |
while (num > 0) {
|
deba@891
|
584 |
n = _level->highestActive();
|
deba@891
|
585 |
if (n == INVALID) goto first_phase_done;
|
deba@891
|
586 |
level = _level->highestActiveLevel();
|
deba@891
|
587 |
--num;
|
deba@891
|
588 |
|
kpeter@641
|
589 |
Value excess = (*_excess)[n];
|
alpar@389
|
590 |
int new_level = _level->maxLevel();
|
alpar@389
|
591 |
|
alpar@389
|
592 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
593 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
alpar@389
|
594 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
595 |
Node v = _graph.target(e);
|
alpar@389
|
596 |
if ((*_level)[v] < level) {
|
alpar@389
|
597 |
if (!_level->active(v) && v != _target) {
|
alpar@389
|
598 |
_level->activate(v);
|
alpar@389
|
599 |
}
|
alpar@389
|
600 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
601 |
_flow->set(e, (*_flow)[e] + excess);
|
kpeter@581
|
602 |
(*_excess)[v] += excess;
|
alpar@389
|
603 |
excess = 0;
|
alpar@389
|
604 |
goto no_more_push_1;
|
alpar@389
|
605 |
} else {
|
alpar@389
|
606 |
excess -= rem;
|
kpeter@581
|
607 |
(*_excess)[v] += rem;
|
alpar@389
|
608 |
_flow->set(e, (*_capacity)[e]);
|
alpar@389
|
609 |
}
|
alpar@389
|
610 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
611 |
new_level = (*_level)[v];
|
alpar@389
|
612 |
}
|
alpar@389
|
613 |
}
|
alpar@389
|
614 |
|
alpar@389
|
615 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
616 |
Value rem = (*_flow)[e];
|
alpar@389
|
617 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
618 |
Node v = _graph.source(e);
|
alpar@389
|
619 |
if ((*_level)[v] < level) {
|
alpar@389
|
620 |
if (!_level->active(v) && v != _target) {
|
alpar@389
|
621 |
_level->activate(v);
|
alpar@389
|
622 |
}
|
alpar@389
|
623 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
624 |
_flow->set(e, (*_flow)[e] - excess);
|
kpeter@581
|
625 |
(*_excess)[v] += excess;
|
alpar@389
|
626 |
excess = 0;
|
alpar@389
|
627 |
goto no_more_push_1;
|
alpar@389
|
628 |
} else {
|
alpar@389
|
629 |
excess -= rem;
|
kpeter@581
|
630 |
(*_excess)[v] += rem;
|
alpar@389
|
631 |
_flow->set(e, 0);
|
alpar@389
|
632 |
}
|
alpar@389
|
633 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
634 |
new_level = (*_level)[v];
|
alpar@389
|
635 |
}
|
alpar@389
|
636 |
}
|
alpar@389
|
637 |
|
alpar@389
|
638 |
no_more_push_1:
|
alpar@389
|
639 |
|
kpeter@581
|
640 |
(*_excess)[n] = excess;
|
alpar@389
|
641 |
|
alpar@389
|
642 |
if (excess != 0) {
|
alpar@389
|
643 |
if (new_level + 1 < _level->maxLevel()) {
|
alpar@389
|
644 |
_level->liftHighestActive(new_level + 1);
|
alpar@389
|
645 |
} else {
|
alpar@389
|
646 |
_level->liftHighestActiveToTop();
|
alpar@389
|
647 |
}
|
alpar@389
|
648 |
if (_level->emptyLevel(level)) {
|
alpar@389
|
649 |
_level->liftToTop(level);
|
alpar@389
|
650 |
}
|
alpar@389
|
651 |
} else {
|
alpar@389
|
652 |
_level->deactivate(n);
|
alpar@389
|
653 |
}
|
alpar@389
|
654 |
}
|
alpar@389
|
655 |
|
alpar@389
|
656 |
num = _node_num * 20;
|
deba@891
|
657 |
while (num > 0) {
|
deba@891
|
658 |
while (level >= 0 && _level->activeFree(level)) {
|
deba@891
|
659 |
--level;
|
deba@891
|
660 |
}
|
deba@891
|
661 |
if (level == -1) {
|
deba@891
|
662 |
n = _level->highestActive();
|
deba@891
|
663 |
level = _level->highestActiveLevel();
|
deba@891
|
664 |
if (n == INVALID) goto first_phase_done;
|
deba@891
|
665 |
} else {
|
deba@891
|
666 |
n = _level->activeOn(level);
|
deba@891
|
667 |
}
|
deba@891
|
668 |
--num;
|
deba@891
|
669 |
|
kpeter@641
|
670 |
Value excess = (*_excess)[n];
|
alpar@389
|
671 |
int new_level = _level->maxLevel();
|
alpar@389
|
672 |
|
alpar@389
|
673 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
674 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
alpar@389
|
675 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
676 |
Node v = _graph.target(e);
|
alpar@389
|
677 |
if ((*_level)[v] < level) {
|
alpar@389
|
678 |
if (!_level->active(v) && v != _target) {
|
alpar@389
|
679 |
_level->activate(v);
|
alpar@389
|
680 |
}
|
alpar@389
|
681 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
682 |
_flow->set(e, (*_flow)[e] + excess);
|
kpeter@581
|
683 |
(*_excess)[v] += excess;
|
alpar@389
|
684 |
excess = 0;
|
alpar@389
|
685 |
goto no_more_push_2;
|
alpar@389
|
686 |
} else {
|
alpar@389
|
687 |
excess -= rem;
|
kpeter@581
|
688 |
(*_excess)[v] += rem;
|
alpar@389
|
689 |
_flow->set(e, (*_capacity)[e]);
|
alpar@389
|
690 |
}
|
alpar@389
|
691 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
692 |
new_level = (*_level)[v];
|
alpar@389
|
693 |
}
|
alpar@389
|
694 |
}
|
alpar@389
|
695 |
|
alpar@389
|
696 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
697 |
Value rem = (*_flow)[e];
|
alpar@389
|
698 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
699 |
Node v = _graph.source(e);
|
alpar@389
|
700 |
if ((*_level)[v] < level) {
|
alpar@389
|
701 |
if (!_level->active(v) && v != _target) {
|
alpar@389
|
702 |
_level->activate(v);
|
alpar@389
|
703 |
}
|
alpar@389
|
704 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
705 |
_flow->set(e, (*_flow)[e] - excess);
|
kpeter@581
|
706 |
(*_excess)[v] += excess;
|
alpar@389
|
707 |
excess = 0;
|
alpar@389
|
708 |
goto no_more_push_2;
|
alpar@389
|
709 |
} else {
|
alpar@389
|
710 |
excess -= rem;
|
kpeter@581
|
711 |
(*_excess)[v] += rem;
|
alpar@389
|
712 |
_flow->set(e, 0);
|
alpar@389
|
713 |
}
|
alpar@389
|
714 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
715 |
new_level = (*_level)[v];
|
alpar@389
|
716 |
}
|
alpar@389
|
717 |
}
|
alpar@389
|
718 |
|
alpar@389
|
719 |
no_more_push_2:
|
alpar@389
|
720 |
|
kpeter@581
|
721 |
(*_excess)[n] = excess;
|
alpar@389
|
722 |
|
alpar@389
|
723 |
if (excess != 0) {
|
alpar@389
|
724 |
if (new_level + 1 < _level->maxLevel()) {
|
alpar@389
|
725 |
_level->liftActiveOn(level, new_level + 1);
|
alpar@389
|
726 |
} else {
|
alpar@389
|
727 |
_level->liftActiveToTop(level);
|
alpar@389
|
728 |
}
|
alpar@389
|
729 |
if (_level->emptyLevel(level)) {
|
alpar@389
|
730 |
_level->liftToTop(level);
|
alpar@389
|
731 |
}
|
alpar@389
|
732 |
} else {
|
alpar@389
|
733 |
_level->deactivate(n);
|
alpar@389
|
734 |
}
|
alpar@389
|
735 |
}
|
alpar@389
|
736 |
}
|
deba@891
|
737 |
first_phase_done:;
|
alpar@389
|
738 |
}
|
alpar@389
|
739 |
|
alpar@389
|
740 |
/// \brief Starts the second phase of the preflow algorithm.
|
alpar@389
|
741 |
///
|
alpar@389
|
742 |
/// The preflow algorithm consists of two phases, this method runs
|
kpeter@393
|
743 |
/// the second phase. After calling one of the \ref init() functions
|
kpeter@393
|
744 |
/// and \ref startFirstPhase() and then \ref startSecondPhase(),
|
kpeter@393
|
745 |
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the
|
alpar@389
|
746 |
/// value of a maximum flow, \ref minCut() returns a minimum cut
|
kpeter@393
|
747 |
/// \pre One of the \ref init() functions and \ref startFirstPhase()
|
kpeter@393
|
748 |
/// must be called before using this function.
|
alpar@389
|
749 |
void startSecondPhase() {
|
alpar@389
|
750 |
_phase = false;
|
alpar@389
|
751 |
|
alpar@389
|
752 |
typename Digraph::template NodeMap<bool> reached(_graph);
|
alpar@389
|
753 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@581
|
754 |
reached[n] = (*_level)[n] < _level->maxLevel();
|
alpar@389
|
755 |
}
|
alpar@389
|
756 |
|
alpar@389
|
757 |
_level->initStart();
|
alpar@389
|
758 |
_level->initAddItem(_source);
|
alpar@389
|
759 |
|
alpar@389
|
760 |
std::vector<Node> queue;
|
alpar@389
|
761 |
queue.push_back(_source);
|
kpeter@581
|
762 |
reached[_source] = true;
|
alpar@389
|
763 |
|
alpar@389
|
764 |
while (!queue.empty()) {
|
alpar@389
|
765 |
_level->initNewLevel();
|
alpar@389
|
766 |
std::vector<Node> nqueue;
|
alpar@389
|
767 |
for (int i = 0; i < int(queue.size()); ++i) {
|
alpar@389
|
768 |
Node n = queue[i];
|
alpar@389
|
769 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
770 |
Node v = _graph.target(e);
|
alpar@389
|
771 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
|
kpeter@581
|
772 |
reached[v] = true;
|
alpar@389
|
773 |
_level->initAddItem(v);
|
alpar@389
|
774 |
nqueue.push_back(v);
|
alpar@389
|
775 |
}
|
alpar@389
|
776 |
}
|
alpar@389
|
777 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
alpar@389
|
778 |
Node u = _graph.source(e);
|
alpar@389
|
779 |
if (!reached[u] &&
|
alpar@389
|
780 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
kpeter@581
|
781 |
reached[u] = true;
|
alpar@389
|
782 |
_level->initAddItem(u);
|
alpar@389
|
783 |
nqueue.push_back(u);
|
alpar@389
|
784 |
}
|
alpar@389
|
785 |
}
|
alpar@389
|
786 |
}
|
alpar@389
|
787 |
queue.swap(nqueue);
|
alpar@389
|
788 |
}
|
alpar@389
|
789 |
_level->initFinish();
|
alpar@389
|
790 |
|
alpar@389
|
791 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@389
|
792 |
if (!reached[n]) {
|
alpar@389
|
793 |
_level->dirtyTopButOne(n);
|
alpar@389
|
794 |
} else if ((*_excess)[n] > 0 && _target != n) {
|
alpar@389
|
795 |
_level->activate(n);
|
alpar@389
|
796 |
}
|
alpar@389
|
797 |
}
|
alpar@389
|
798 |
|
alpar@389
|
799 |
Node n;
|
alpar@389
|
800 |
while ((n = _level->highestActive()) != INVALID) {
|
kpeter@641
|
801 |
Value excess = (*_excess)[n];
|
alpar@389
|
802 |
int level = _level->highestActiveLevel();
|
alpar@389
|
803 |
int new_level = _level->maxLevel();
|
alpar@389
|
804 |
|
alpar@389
|
805 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
806 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
alpar@389
|
807 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
808 |
Node v = _graph.target(e);
|
alpar@389
|
809 |
if ((*_level)[v] < level) {
|
alpar@389
|
810 |
if (!_level->active(v) && v != _source) {
|
alpar@389
|
811 |
_level->activate(v);
|
alpar@389
|
812 |
}
|
alpar@389
|
813 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
814 |
_flow->set(e, (*_flow)[e] + excess);
|
kpeter@581
|
815 |
(*_excess)[v] += excess;
|
alpar@389
|
816 |
excess = 0;
|
alpar@389
|
817 |
goto no_more_push;
|
alpar@389
|
818 |
} else {
|
alpar@389
|
819 |
excess -= rem;
|
kpeter@581
|
820 |
(*_excess)[v] += rem;
|
alpar@389
|
821 |
_flow->set(e, (*_capacity)[e]);
|
alpar@389
|
822 |
}
|
alpar@389
|
823 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
824 |
new_level = (*_level)[v];
|
alpar@389
|
825 |
}
|
alpar@389
|
826 |
}
|
alpar@389
|
827 |
|
alpar@389
|
828 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
kpeter@641
|
829 |
Value rem = (*_flow)[e];
|
alpar@389
|
830 |
if (!_tolerance.positive(rem)) continue;
|
alpar@389
|
831 |
Node v = _graph.source(e);
|
alpar@389
|
832 |
if ((*_level)[v] < level) {
|
alpar@389
|
833 |
if (!_level->active(v) && v != _source) {
|
alpar@389
|
834 |
_level->activate(v);
|
alpar@389
|
835 |
}
|
alpar@389
|
836 |
if (!_tolerance.less(rem, excess)) {
|
alpar@389
|
837 |
_flow->set(e, (*_flow)[e] - excess);
|
kpeter@581
|
838 |
(*_excess)[v] += excess;
|
alpar@389
|
839 |
excess = 0;
|
alpar@389
|
840 |
goto no_more_push;
|
alpar@389
|
841 |
} else {
|
alpar@389
|
842 |
excess -= rem;
|
kpeter@581
|
843 |
(*_excess)[v] += rem;
|
alpar@389
|
844 |
_flow->set(e, 0);
|
alpar@389
|
845 |
}
|
alpar@389
|
846 |
} else if (new_level > (*_level)[v]) {
|
alpar@389
|
847 |
new_level = (*_level)[v];
|
alpar@389
|
848 |
}
|
alpar@389
|
849 |
}
|
alpar@389
|
850 |
|
alpar@389
|
851 |
no_more_push:
|
alpar@389
|
852 |
|
kpeter@581
|
853 |
(*_excess)[n] = excess;
|
alpar@389
|
854 |
|
alpar@389
|
855 |
if (excess != 0) {
|
alpar@389
|
856 |
if (new_level + 1 < _level->maxLevel()) {
|
alpar@389
|
857 |
_level->liftHighestActive(new_level + 1);
|
alpar@389
|
858 |
} else {
|
alpar@389
|
859 |
// Calculation error
|
alpar@389
|
860 |
_level->liftHighestActiveToTop();
|
alpar@389
|
861 |
}
|
alpar@389
|
862 |
if (_level->emptyLevel(level)) {
|
alpar@389
|
863 |
// Calculation error
|
alpar@389
|
864 |
_level->liftToTop(level);
|
alpar@389
|
865 |
}
|
alpar@389
|
866 |
} else {
|
alpar@389
|
867 |
_level->deactivate(n);
|
alpar@389
|
868 |
}
|
alpar@389
|
869 |
|
alpar@389
|
870 |
}
|
alpar@389
|
871 |
}
|
alpar@389
|
872 |
|
alpar@389
|
873 |
/// \brief Runs the preflow algorithm.
|
alpar@389
|
874 |
///
|
alpar@389
|
875 |
/// Runs the preflow algorithm.
|
alpar@389
|
876 |
/// \note pf.run() is just a shortcut of the following code.
|
alpar@389
|
877 |
/// \code
|
alpar@389
|
878 |
/// pf.init();
|
alpar@389
|
879 |
/// pf.startFirstPhase();
|
alpar@389
|
880 |
/// pf.startSecondPhase();
|
alpar@389
|
881 |
/// \endcode
|
alpar@389
|
882 |
void run() {
|
alpar@389
|
883 |
init();
|
alpar@389
|
884 |
startFirstPhase();
|
alpar@389
|
885 |
startSecondPhase();
|
alpar@389
|
886 |
}
|
alpar@389
|
887 |
|
alpar@389
|
888 |
/// \brief Runs the preflow algorithm to compute the minimum cut.
|
alpar@389
|
889 |
///
|
alpar@389
|
890 |
/// Runs the preflow algorithm to compute the minimum cut.
|
alpar@389
|
891 |
/// \note pf.runMinCut() is just a shortcut of the following code.
|
alpar@389
|
892 |
/// \code
|
alpar@389
|
893 |
/// pf.init();
|
alpar@389
|
894 |
/// pf.startFirstPhase();
|
alpar@389
|
895 |
/// \endcode
|
alpar@389
|
896 |
void runMinCut() {
|
alpar@389
|
897 |
init();
|
alpar@389
|
898 |
startFirstPhase();
|
alpar@389
|
899 |
}
|
alpar@389
|
900 |
|
alpar@389
|
901 |
/// @}
|
alpar@389
|
902 |
|
alpar@389
|
903 |
/// \name Query Functions
|
kpeter@393
|
904 |
/// The results of the preflow algorithm can be obtained using these
|
alpar@389
|
905 |
/// functions.\n
|
kpeter@393
|
906 |
/// Either one of the \ref run() "run*()" functions or one of the
|
kpeter@393
|
907 |
/// \ref startFirstPhase() "start*()" functions should be called
|
kpeter@393
|
908 |
/// before using them.
|
alpar@389
|
909 |
|
alpar@389
|
910 |
///@{
|
alpar@389
|
911 |
|
alpar@389
|
912 |
/// \brief Returns the value of the maximum flow.
|
alpar@389
|
913 |
///
|
alpar@389
|
914 |
/// Returns the value of the maximum flow by returning the excess
|
kpeter@393
|
915 |
/// of the target node. This value equals to the value of
|
kpeter@393
|
916 |
/// the maximum flow already after the first phase of the algorithm.
|
kpeter@393
|
917 |
///
|
kpeter@393
|
918 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
919 |
/// using this function.
|
kpeter@641
|
920 |
Value flowValue() const {
|
alpar@389
|
921 |
return (*_excess)[_target];
|
alpar@389
|
922 |
}
|
alpar@389
|
923 |
|
kpeter@641
|
924 |
/// \brief Returns the flow value on the given arc.
|
alpar@389
|
925 |
///
|
kpeter@641
|
926 |
/// Returns the flow value on the given arc. This method can
|
kpeter@393
|
927 |
/// be called after the second phase of the algorithm.
|
kpeter@393
|
928 |
///
|
kpeter@393
|
929 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
930 |
/// using this function.
|
kpeter@641
|
931 |
Value flow(const Arc& arc) const {
|
kpeter@393
|
932 |
return (*_flow)[arc];
|
kpeter@393
|
933 |
}
|
kpeter@393
|
934 |
|
kpeter@393
|
935 |
/// \brief Returns a const reference to the flow map.
|
kpeter@393
|
936 |
///
|
kpeter@393
|
937 |
/// Returns a const reference to the arc map storing the found flow.
|
kpeter@393
|
938 |
/// This method can be called after the second phase of the algorithm.
|
kpeter@393
|
939 |
///
|
kpeter@393
|
940 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
941 |
/// using this function.
|
kpeter@420
|
942 |
const FlowMap& flowMap() const {
|
kpeter@393
|
943 |
return *_flow;
|
kpeter@393
|
944 |
}
|
kpeter@393
|
945 |
|
kpeter@393
|
946 |
/// \brief Returns \c true when the node is on the source side of the
|
kpeter@393
|
947 |
/// minimum cut.
|
kpeter@393
|
948 |
///
|
kpeter@393
|
949 |
/// Returns true when the node is on the source side of the found
|
kpeter@393
|
950 |
/// minimum cut. This method can be called both after running \ref
|
alpar@389
|
951 |
/// startFirstPhase() and \ref startSecondPhase().
|
kpeter@393
|
952 |
///
|
kpeter@393
|
953 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
954 |
/// using this function.
|
alpar@389
|
955 |
bool minCut(const Node& node) const {
|
alpar@389
|
956 |
return ((*_level)[node] == _level->maxLevel()) == _phase;
|
alpar@389
|
957 |
}
|
alpar@389
|
958 |
|
kpeter@393
|
959 |
/// \brief Gives back a minimum value cut.
|
alpar@389
|
960 |
///
|
kpeter@393
|
961 |
/// Sets \c cutMap to the characteristic vector of a minimum value
|
kpeter@393
|
962 |
/// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
|
kpeter@393
|
963 |
/// node map with \c bool (or convertible) value type.
|
kpeter@393
|
964 |
///
|
kpeter@393
|
965 |
/// This method can be called both after running \ref startFirstPhase()
|
kpeter@393
|
966 |
/// and \ref startSecondPhase(). The result after the second phase
|
kpeter@393
|
967 |
/// could be slightly different if inexact computation is used.
|
kpeter@393
|
968 |
///
|
kpeter@393
|
969 |
/// \note This function calls \ref minCut() for each node, so it runs in
|
kpeter@559
|
970 |
/// O(n) time.
|
kpeter@393
|
971 |
///
|
kpeter@393
|
972 |
/// \pre Either \ref run() or \ref init() must be called before
|
kpeter@393
|
973 |
/// using this function.
|
alpar@389
|
974 |
template <typename CutMap>
|
alpar@389
|
975 |
void minCutMap(CutMap& cutMap) const {
|
alpar@389
|
976 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@389
|
977 |
cutMap.set(n, minCut(n));
|
alpar@389
|
978 |
}
|
alpar@389
|
979 |
}
|
alpar@389
|
980 |
|
alpar@389
|
981 |
/// @}
|
alpar@389
|
982 |
};
|
alpar@389
|
983 |
}
|
alpar@389
|
984 |
|
alpar@389
|
985 |
#endif
|