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