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@2553
|
5 |
* Copyright (C) 2003-2008
|
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@2525
|
75 |
typedef LinkedElevator<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@2527
|
171 |
|
deba@2514
|
172 |
void createStructures() {
|
deba@2514
|
173 |
_node_num = countNodes(_graph);
|
deba@2514
|
174 |
|
deba@2514
|
175 |
if (!_flow) {
|
deba@2514
|
176 |
_flow = Traits::createFlowMap(_graph);
|
deba@2514
|
177 |
_local_flow = true;
|
deba@2514
|
178 |
}
|
deba@2514
|
179 |
if (!_level) {
|
deba@2514
|
180 |
_level = Traits::createElevator(_graph, _node_num);
|
deba@2514
|
181 |
_local_level = true;
|
deba@2514
|
182 |
}
|
deba@2514
|
183 |
if (!_excess) {
|
deba@2514
|
184 |
_excess = new ExcessMap(_graph);
|
deba@2514
|
185 |
}
|
deba@2514
|
186 |
}
|
deba@2514
|
187 |
|
deba@2514
|
188 |
void destroyStructures() {
|
deba@2514
|
189 |
if (_local_flow) {
|
deba@2514
|
190 |
delete _flow;
|
deba@2514
|
191 |
}
|
deba@2514
|
192 |
if (_local_level) {
|
deba@2514
|
193 |
delete _level;
|
deba@2514
|
194 |
}
|
deba@2514
|
195 |
if (_excess) {
|
deba@2514
|
196 |
delete _excess;
|
deba@2514
|
197 |
}
|
deba@2514
|
198 |
}
|
jacint@836
|
199 |
|
jacint@1762
|
200 |
public:
|
jacint@1762
|
201 |
|
deba@2514
|
202 |
typedef Preflow Create;
|
jacint@1762
|
203 |
|
deba@2514
|
204 |
///\name Named template parameters
|
deba@2514
|
205 |
|
deba@2514
|
206 |
///@{
|
deba@2514
|
207 |
|
deba@2514
|
208 |
template <typename _FlowMap>
|
deba@2514
|
209 |
struct DefFlowMapTraits : public Traits {
|
deba@2514
|
210 |
typedef _FlowMap FlowMap;
|
deba@2514
|
211 |
static FlowMap *createFlowMap(const Graph&) {
|
deba@2514
|
212 |
throw UninitializedParameter();
|
jacint@1762
|
213 |
}
|
jacint@1762
|
214 |
};
|
deba@2514
|
215 |
|
deba@2514
|
216 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@2514
|
217 |
/// FlowMap type
|
jacint@836
|
218 |
///
|
deba@2514
|
219 |
/// \ref named-templ-param "Named parameter" for setting FlowMap
|
deba@2514
|
220 |
/// type
|
deba@2514
|
221 |
template <typename _FlowMap>
|
deba@2514
|
222 |
struct DefFlowMap
|
deba@2514
|
223 |
: public Preflow<Graph, CapacityMap, DefFlowMapTraits<_FlowMap> > {
|
deba@2514
|
224 |
typedef Preflow<Graph, CapacityMap, DefFlowMapTraits<_FlowMap> > Create;
|
jacint@836
|
225 |
};
|
jacint@836
|
226 |
|
deba@2514
|
227 |
template <typename _Elevator>
|
deba@2514
|
228 |
struct DefElevatorTraits : public Traits {
|
deba@2514
|
229 |
typedef _Elevator Elevator;
|
deba@2514
|
230 |
static Elevator *createElevator(const Graph&, int) {
|
deba@2514
|
231 |
throw UninitializedParameter();
|
deba@2514
|
232 |
}
|
deba@2514
|
233 |
};
|
jacint@836
|
234 |
|
deba@2514
|
235 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@2514
|
236 |
/// Elevator type
|
jacint@836
|
237 |
///
|
deba@2514
|
238 |
/// \ref named-templ-param "Named parameter" for setting Elevator
|
deba@2514
|
239 |
/// type
|
deba@2514
|
240 |
template <typename _Elevator>
|
deba@2514
|
241 |
struct DefElevator
|
deba@2514
|
242 |
: public Preflow<Graph, CapacityMap, DefElevatorTraits<_Elevator> > {
|
deba@2514
|
243 |
typedef Preflow<Graph, CapacityMap, DefElevatorTraits<_Elevator> > Create;
|
jacint@836
|
244 |
};
|
jacint@836
|
245 |
|
deba@2514
|
246 |
template <typename _Elevator>
|
deba@2514
|
247 |
struct DefStandardElevatorTraits : public Traits {
|
deba@2514
|
248 |
typedef _Elevator Elevator;
|
deba@2514
|
249 |
static Elevator *createElevator(const Graph& graph, int max_level) {
|
deba@2514
|
250 |
return new Elevator(graph, max_level);
|
deba@2514
|
251 |
}
|
deba@2514
|
252 |
};
|
jacint@836
|
253 |
|
deba@2514
|
254 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@2514
|
255 |
/// Elevator type
|
deba@2514
|
256 |
///
|
deba@2514
|
257 |
/// \ref named-templ-param "Named parameter" for setting Elevator
|
deba@2514
|
258 |
/// type. The Elevator should be standard constructor interface, ie.
|
deba@2514
|
259 |
/// the graph and the maximum level should be passed to it.
|
deba@2514
|
260 |
template <typename _Elevator>
|
deba@2514
|
261 |
struct DefStandardElevator
|
deba@2514
|
262 |
: public Preflow<Graph, CapacityMap,
|
deba@2514
|
263 |
DefStandardElevatorTraits<_Elevator> > {
|
deba@2514
|
264 |
typedef Preflow<Graph, CapacityMap,
|
deba@2514
|
265 |
DefStandardElevatorTraits<_Elevator> > Create;
|
deba@2514
|
266 |
};
|
alpar@1898
|
267 |
|
deba@2514
|
268 |
/// @}
|
jacint@836
|
269 |
|
deba@2527
|
270 |
protected:
|
deba@2527
|
271 |
|
deba@2527
|
272 |
Preflow() {}
|
deba@2527
|
273 |
|
deba@2527
|
274 |
public:
|
deba@2527
|
275 |
|
deba@2527
|
276 |
|
deba@2514
|
277 |
/// \brief The constructor of the class.
|
alpar@851
|
278 |
///
|
deba@2514
|
279 |
/// The constructor of the class.
|
deba@2514
|
280 |
/// \param graph The directed graph the algorithm runs on.
|
deba@2514
|
281 |
/// \param capacity The capacity of the edges.
|
deba@2514
|
282 |
/// \param source The source node.
|
deba@2514
|
283 |
/// \param target The target node.
|
deba@2514
|
284 |
Preflow(const Graph& graph, const CapacityMap& capacity,
|
deba@2514
|
285 |
Node source, Node target)
|
deba@2514
|
286 |
: _graph(graph), _capacity(&capacity),
|
deba@2514
|
287 |
_node_num(0), _source(source), _target(target),
|
deba@2514
|
288 |
_flow(0), _local_flow(false),
|
deba@2514
|
289 |
_level(0), _local_level(false),
|
deba@2514
|
290 |
_excess(0), _tolerance(), _phase() {}
|
jacint@836
|
291 |
|
deba@2514
|
292 |
/// \brief Destrcutor.
|
deba@2514
|
293 |
///
|
deba@2514
|
294 |
/// Destructor.
|
deba@2514
|
295 |
~Preflow() {
|
deba@2514
|
296 |
destroyStructures();
|
jacint@836
|
297 |
}
|
jacint@836
|
298 |
|
deba@2514
|
299 |
/// \brief Sets the capacity map.
|
deba@2514
|
300 |
///
|
deba@2514
|
301 |
/// Sets the capacity map.
|
deba@2514
|
302 |
/// \return \c (*this)
|
deba@2514
|
303 |
Preflow& capacityMap(const CapacityMap& map) {
|
deba@2514
|
304 |
_capacity = ↦
|
deba@2514
|
305 |
return *this;
|
deba@2514
|
306 |
}
|
deba@2514
|
307 |
|
deba@2514
|
308 |
/// \brief Sets the flow map.
|
deba@2514
|
309 |
///
|
deba@2514
|
310 |
/// Sets the flow map.
|
deba@2514
|
311 |
/// \return \c (*this)
|
deba@2514
|
312 |
Preflow& flowMap(FlowMap& map) {
|
deba@2514
|
313 |
if (_local_flow) {
|
deba@2514
|
314 |
delete _flow;
|
deba@2514
|
315 |
_local_flow = false;
|
deba@2514
|
316 |
}
|
deba@2514
|
317 |
_flow = ↦
|
deba@2514
|
318 |
return *this;
|
deba@2514
|
319 |
}
|
deba@2514
|
320 |
|
deba@2514
|
321 |
/// \brief Returns the flow map.
|
deba@2514
|
322 |
///
|
deba@2514
|
323 |
/// \return The flow map.
|
deba@2514
|
324 |
const FlowMap& flowMap() {
|
deba@2514
|
325 |
return *_flow;
|
deba@2514
|
326 |
}
|
deba@2514
|
327 |
|
deba@2514
|
328 |
/// \brief Sets the elevator.
|
deba@2514
|
329 |
///
|
deba@2514
|
330 |
/// Sets the elevator.
|
deba@2514
|
331 |
/// \return \c (*this)
|
deba@2514
|
332 |
Preflow& elevator(Elevator& elevator) {
|
deba@2514
|
333 |
if (_local_level) {
|
deba@2514
|
334 |
delete _level;
|
deba@2514
|
335 |
_local_level = false;
|
deba@2514
|
336 |
}
|
deba@2514
|
337 |
_level = &elevator;
|
deba@2514
|
338 |
return *this;
|
deba@2514
|
339 |
}
|
deba@2514
|
340 |
|
deba@2514
|
341 |
/// \brief Returns the elevator.
|
deba@2514
|
342 |
///
|
deba@2514
|
343 |
/// \return The elevator.
|
deba@2514
|
344 |
const Elevator& elevator() {
|
deba@2514
|
345 |
return *_level;
|
deba@2514
|
346 |
}
|
deba@2514
|
347 |
|
deba@2514
|
348 |
/// \brief Sets the source node.
|
deba@2514
|
349 |
///
|
deba@2514
|
350 |
/// Sets the source node.
|
deba@2514
|
351 |
/// \return \c (*this)
|
deba@2514
|
352 |
Preflow& source(const Node& node) {
|
deba@2514
|
353 |
_source = node;
|
deba@2514
|
354 |
return *this;
|
deba@2514
|
355 |
}
|
deba@2514
|
356 |
|
deba@2514
|
357 |
/// \brief Sets the target node.
|
deba@2514
|
358 |
///
|
deba@2514
|
359 |
/// Sets the target node.
|
deba@2514
|
360 |
/// \return \c (*this)
|
deba@2514
|
361 |
Preflow& target(const Node& node) {
|
deba@2514
|
362 |
_target = node;
|
deba@2514
|
363 |
return *this;
|
deba@2514
|
364 |
}
|
deba@2514
|
365 |
|
deba@2514
|
366 |
/// \brief Sets the tolerance used by algorithm.
|
deba@2514
|
367 |
///
|
deba@2514
|
368 |
/// Sets the tolerance used by algorithm.
|
deba@2514
|
369 |
Preflow& tolerance(const Tolerance& tolerance) const {
|
deba@2514
|
370 |
_tolerance = tolerance;
|
deba@2514
|
371 |
return *this;
|
deba@2514
|
372 |
}
|
deba@2514
|
373 |
|
deba@2514
|
374 |
/// \brief Returns the tolerance used by algorithm.
|
deba@2514
|
375 |
///
|
deba@2514
|
376 |
/// Returns the tolerance used by algorithm.
|
deba@2514
|
377 |
const Tolerance& tolerance() const {
|
deba@2514
|
378 |
return tolerance;
|
deba@2514
|
379 |
}
|
deba@2514
|
380 |
|
deba@2514
|
381 |
/// \name Execution control The simplest way to execute the
|
deba@2514
|
382 |
/// algorithm is to use one of the member functions called \c
|
deba@2514
|
383 |
/// run().
|
deba@2514
|
384 |
/// \n
|
deba@2514
|
385 |
/// If you need more control on initial solution or
|
deba@2514
|
386 |
/// execution then you have to call one \ref init() function and then
|
deba@2514
|
387 |
/// the startFirstPhase() and if you need the startSecondPhase().
|
jacint@836
|
388 |
|
deba@2514
|
389 |
///@{
|
jacint@836
|
390 |
|
deba@2514
|
391 |
/// \brief Initializes the internal data structures.
|
deba@2514
|
392 |
///
|
deba@2514
|
393 |
/// Initializes the internal data structures.
|
deba@2514
|
394 |
///
|
deba@2514
|
395 |
void init() {
|
deba@2514
|
396 |
createStructures();
|
jacint@836
|
397 |
|
deba@2514
|
398 |
_phase = true;
|
deba@2514
|
399 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
400 |
_excess->set(n, 0);
|
deba@2514
|
401 |
}
|
jacint@836
|
402 |
|
deba@2514
|
403 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
404 |
_flow->set(e, 0);
|
deba@2514
|
405 |
}
|
jacint@836
|
406 |
|
deba@2514
|
407 |
typename Graph::template NodeMap<bool> reached(_graph, false);
|
jacint@836
|
408 |
|
deba@2514
|
409 |
_level->initStart();
|
deba@2514
|
410 |
_level->initAddItem(_target);
|
jacint@836
|
411 |
|
deba@2514
|
412 |
std::vector<Node> queue;
|
deba@2514
|
413 |
reached.set(_source, true);
|
jacint@836
|
414 |
|
deba@2514
|
415 |
queue.push_back(_target);
|
deba@2514
|
416 |
reached.set(_target, true);
|
deba@2514
|
417 |
while (!queue.empty()) {
|
deba@2522
|
418 |
_level->initNewLevel();
|
deba@2514
|
419 |
std::vector<Node> nqueue;
|
deba@2514
|
420 |
for (int i = 0; i < int(queue.size()); ++i) {
|
deba@2514
|
421 |
Node n = queue[i];
|
deba@2514
|
422 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
423 |
Node u = _graph.source(e);
|
deba@2514
|
424 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
|
deba@2514
|
425 |
reached.set(u, true);
|
deba@2514
|
426 |
_level->initAddItem(u);
|
deba@2514
|
427 |
nqueue.push_back(u);
|
jacint@836
|
428 |
}
|
jacint@836
|
429 |
}
|
jacint@836
|
430 |
}
|
deba@2514
|
431 |
queue.swap(nqueue);
|
jacint@836
|
432 |
}
|
deba@2514
|
433 |
_level->initFinish();
|
jacint@836
|
434 |
|
deba@2514
|
435 |
for (OutEdgeIt e(_graph, _source); e != INVALID; ++e) {
|
deba@2514
|
436 |
if (_tolerance.positive((*_capacity)[e])) {
|
deba@2514
|
437 |
Node u = _graph.target(e);
|
deba@2514
|
438 |
if ((*_level)[u] == _level->maxLevel()) continue;
|
deba@2514
|
439 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
440 |
_excess->set(u, (*_excess)[u] + (*_capacity)[e]);
|
deba@2514
|
441 |
if (u != _target && !_level->active(u)) {
|
deba@2514
|
442 |
_level->activate(u);
|
jacint@836
|
443 |
}
|
jacint@836
|
444 |
}
|
jacint@836
|
445 |
}
|
jacint@836
|
446 |
}
|
jacint@836
|
447 |
|
deba@2514
|
448 |
/// \brief Initializes the internal data structures.
|
deba@2514
|
449 |
///
|
deba@2514
|
450 |
/// Initializes the internal data structures and sets the initial
|
deba@2514
|
451 |
/// flow to the given \c flowMap. The \c flowMap should contain a
|
deba@2514
|
452 |
/// flow or at least a preflow, ie. in each node excluding the
|
deba@2514
|
453 |
/// target the incoming flow should greater or equal to the
|
deba@2514
|
454 |
/// outgoing flow.
|
deba@2514
|
455 |
/// \return %False when the given \c flowMap is not a preflow.
|
deba@2514
|
456 |
template <typename FlowMap>
|
deba@2514
|
457 |
bool flowInit(const FlowMap& flowMap) {
|
deba@2514
|
458 |
createStructures();
|
jacint@836
|
459 |
|
deba@2514
|
460 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
461 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
462 |
}
|
jacint@836
|
463 |
|
deba@2514
|
464 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
465 |
Value excess = 0;
|
deba@2514
|
466 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
467 |
excess += (*_flow)[e];
|
deba@2514
|
468 |
}
|
deba@2514
|
469 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
470 |
excess -= (*_flow)[e];
|
deba@2514
|
471 |
}
|
deba@2514
|
472 |
if (excess < 0 && n != _source) return false;
|
deba@2514
|
473 |
_excess->set(n, excess);
|
deba@2514
|
474 |
}
|
alpar@1222
|
475 |
|
deba@2514
|
476 |
typename Graph::template NodeMap<bool> reached(_graph, false);
|
alpar@1222
|
477 |
|
deba@2514
|
478 |
_level->initStart();
|
deba@2514
|
479 |
_level->initAddItem(_target);
|
jacint@836
|
480 |
|
deba@2514
|
481 |
std::vector<Node> queue;
|
deba@2514
|
482 |
reached.set(_source, true);
|
jacint@836
|
483 |
|
deba@2514
|
484 |
queue.push_back(_target);
|
deba@2514
|
485 |
reached.set(_target, true);
|
deba@2514
|
486 |
while (!queue.empty()) {
|
deba@2514
|
487 |
_level->initNewLevel();
|
deba@2514
|
488 |
std::vector<Node> nqueue;
|
deba@2514
|
489 |
for (int i = 0; i < int(queue.size()); ++i) {
|
deba@2514
|
490 |
Node n = queue[i];
|
deba@2514
|
491 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
492 |
Node u = _graph.source(e);
|
deba@2514
|
493 |
if (!reached[u] &&
|
deba@2514
|
494 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
deba@2514
|
495 |
reached.set(u, true);
|
deba@2514
|
496 |
_level->initAddItem(u);
|
deba@2514
|
497 |
nqueue.push_back(u);
|
jacint@836
|
498 |
}
|
jacint@836
|
499 |
}
|
deba@2514
|
500 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
501 |
Node v = _graph.target(e);
|
deba@2514
|
502 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
|
deba@2514
|
503 |
reached.set(v, true);
|
deba@2514
|
504 |
_level->initAddItem(v);
|
deba@2514
|
505 |
nqueue.push_back(v);
|
jacint@836
|
506 |
}
|
jacint@836
|
507 |
}
|
jacint@836
|
508 |
}
|
deba@2514
|
509 |
queue.swap(nqueue);
|
deba@2514
|
510 |
}
|
deba@2514
|
511 |
_level->initFinish();
|
deba@2514
|
512 |
|
deba@2514
|
513 |
for (OutEdgeIt e(_graph, _source); e != INVALID; ++e) {
|
deba@2514
|
514 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
515 |
if (_tolerance.positive(rem)) {
|
deba@2514
|
516 |
Node u = _graph.target(e);
|
deba@2514
|
517 |
if ((*_level)[u] == _level->maxLevel()) continue;
|
deba@2514
|
518 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
519 |
_excess->set(u, (*_excess)[u] + rem);
|
deba@2514
|
520 |
if (u != _target && !_level->active(u)) {
|
deba@2514
|
521 |
_level->activate(u);
|
jacint@836
|
522 |
}
|
jacint@836
|
523 |
}
|
deba@2514
|
524 |
}
|
deba@2514
|
525 |
for (InEdgeIt e(_graph, _source); e != INVALID; ++e) {
|
deba@2514
|
526 |
Value rem = (*_flow)[e];
|
deba@2514
|
527 |
if (_tolerance.positive(rem)) {
|
deba@2514
|
528 |
Node v = _graph.source(e);
|
deba@2514
|
529 |
if ((*_level)[v] == _level->maxLevel()) continue;
|
deba@2514
|
530 |
_flow->set(e, 0);
|
deba@2514
|
531 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
532 |
if (v != _target && !_level->active(v)) {
|
deba@2514
|
533 |
_level->activate(v);
|
deba@2514
|
534 |
}
|
deba@2514
|
535 |
}
|
deba@2514
|
536 |
}
|
deba@2514
|
537 |
return true;
|
deba@2514
|
538 |
}
|
jacint@836
|
539 |
|
deba@2514
|
540 |
/// \brief Starts the first phase of the preflow algorithm.
|
deba@2514
|
541 |
///
|
deba@2514
|
542 |
/// The preflow algorithm consists of two phases, this method runs
|
deba@2514
|
543 |
/// the first phase. After the first phase the maximum flow value
|
deba@2514
|
544 |
/// and a minimum value cut can already be computed, although a
|
deba@2514
|
545 |
/// maximum flow is not yet obtained. So after calling this method
|
deba@2514
|
546 |
/// \ref flowValue() returns the value of a maximum flow and \ref
|
deba@2514
|
547 |
/// minCut() returns a minimum cut.
|
deba@2514
|
548 |
/// \pre One of the \ref init() functions should be called.
|
deba@2514
|
549 |
void startFirstPhase() {
|
deba@2514
|
550 |
_phase = true;
|
deba@2514
|
551 |
|
deba@2514
|
552 |
Node n = _level->highestActive();
|
deba@2514
|
553 |
int level = _level->highestActiveLevel();
|
deba@2514
|
554 |
while (n != INVALID) {
|
deba@2514
|
555 |
int num = _node_num;
|
deba@2514
|
556 |
|
deba@2514
|
557 |
while (num > 0 && n != INVALID) {
|
deba@2514
|
558 |
Value excess = (*_excess)[n];
|
deba@2514
|
559 |
int new_level = _level->maxLevel();
|
deba@2514
|
560 |
|
deba@2514
|
561 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
562 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
563 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
564 |
Node v = _graph.target(e);
|
deba@2514
|
565 |
if ((*_level)[v] < level) {
|
deba@2514
|
566 |
if (!_level->active(v) && v != _target) {
|
deba@2514
|
567 |
_level->activate(v);
|
deba@2514
|
568 |
}
|
deba@2514
|
569 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
570 |
_flow->set(e, (*_flow)[e] + excess);
|
deba@2514
|
571 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
572 |
excess = 0;
|
deba@2514
|
573 |
goto no_more_push_1;
|
deba@2514
|
574 |
} else {
|
deba@2514
|
575 |
excess -= rem;
|
deba@2514
|
576 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
577 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
578 |
}
|
deba@2514
|
579 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
580 |
new_level = (*_level)[v];
|
deba@2514
|
581 |
}
|
deba@2514
|
582 |
}
|
deba@2514
|
583 |
|
deba@2514
|
584 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
585 |
Value rem = (*_flow)[e];
|
deba@2514
|
586 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
587 |
Node v = _graph.source(e);
|
deba@2514
|
588 |
if ((*_level)[v] < level) {
|
deba@2514
|
589 |
if (!_level->active(v) && v != _target) {
|
deba@2514
|
590 |
_level->activate(v);
|
deba@2514
|
591 |
}
|
deba@2514
|
592 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
593 |
_flow->set(e, (*_flow)[e] - excess);
|
deba@2514
|
594 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
595 |
excess = 0;
|
deba@2514
|
596 |
goto no_more_push_1;
|
deba@2514
|
597 |
} else {
|
deba@2514
|
598 |
excess -= rem;
|
deba@2514
|
599 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
600 |
_flow->set(e, 0);
|
deba@2514
|
601 |
}
|
deba@2514
|
602 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
603 |
new_level = (*_level)[v];
|
deba@2514
|
604 |
}
|
deba@2514
|
605 |
}
|
deba@2514
|
606 |
|
deba@2514
|
607 |
no_more_push_1:
|
deba@2514
|
608 |
|
deba@2514
|
609 |
_excess->set(n, excess);
|
deba@2514
|
610 |
|
deba@2514
|
611 |
if (excess != 0) {
|
deba@2514
|
612 |
if (new_level + 1 < _level->maxLevel()) {
|
deba@2514
|
613 |
_level->liftHighestActive(new_level + 1);
|
deba@2514
|
614 |
} else {
|
deba@2514
|
615 |
_level->liftHighestActiveToTop();
|
deba@2514
|
616 |
}
|
deba@2514
|
617 |
if (_level->emptyLevel(level)) {
|
deba@2514
|
618 |
_level->liftToTop(level);
|
deba@2514
|
619 |
}
|
deba@2514
|
620 |
} else {
|
deba@2514
|
621 |
_level->deactivate(n);
|
deba@2514
|
622 |
}
|
deba@2514
|
623 |
|
deba@2514
|
624 |
n = _level->highestActive();
|
deba@2514
|
625 |
level = _level->highestActiveLevel();
|
deba@2514
|
626 |
--num;
|
deba@2514
|
627 |
}
|
deba@2514
|
628 |
|
deba@2514
|
629 |
num = _node_num * 20;
|
deba@2514
|
630 |
while (num > 0 && n != INVALID) {
|
deba@2514
|
631 |
Value excess = (*_excess)[n];
|
deba@2514
|
632 |
int new_level = _level->maxLevel();
|
deba@2514
|
633 |
|
deba@2514
|
634 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
635 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
636 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
637 |
Node v = _graph.target(e);
|
deba@2514
|
638 |
if ((*_level)[v] < level) {
|
deba@2514
|
639 |
if (!_level->active(v) && v != _target) {
|
deba@2514
|
640 |
_level->activate(v);
|
deba@2514
|
641 |
}
|
deba@2514
|
642 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
643 |
_flow->set(e, (*_flow)[e] + excess);
|
deba@2514
|
644 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
645 |
excess = 0;
|
deba@2514
|
646 |
goto no_more_push_2;
|
deba@2514
|
647 |
} else {
|
deba@2514
|
648 |
excess -= rem;
|
deba@2514
|
649 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
650 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
651 |
}
|
deba@2514
|
652 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
653 |
new_level = (*_level)[v];
|
deba@2514
|
654 |
}
|
deba@2514
|
655 |
}
|
deba@2514
|
656 |
|
deba@2514
|
657 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
658 |
Value rem = (*_flow)[e];
|
deba@2514
|
659 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
660 |
Node v = _graph.source(e);
|
deba@2514
|
661 |
if ((*_level)[v] < level) {
|
deba@2514
|
662 |
if (!_level->active(v) && v != _target) {
|
deba@2514
|
663 |
_level->activate(v);
|
deba@2514
|
664 |
}
|
deba@2514
|
665 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
666 |
_flow->set(e, (*_flow)[e] - excess);
|
deba@2514
|
667 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
668 |
excess = 0;
|
deba@2514
|
669 |
goto no_more_push_2;
|
deba@2514
|
670 |
} else {
|
deba@2514
|
671 |
excess -= rem;
|
deba@2514
|
672 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
673 |
_flow->set(e, 0);
|
deba@2514
|
674 |
}
|
deba@2514
|
675 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
676 |
new_level = (*_level)[v];
|
deba@2514
|
677 |
}
|
deba@2514
|
678 |
}
|
deba@2514
|
679 |
|
deba@2514
|
680 |
no_more_push_2:
|
deba@2514
|
681 |
|
deba@2514
|
682 |
_excess->set(n, excess);
|
deba@2514
|
683 |
|
deba@2514
|
684 |
if (excess != 0) {
|
deba@2514
|
685 |
if (new_level + 1 < _level->maxLevel()) {
|
deba@2514
|
686 |
_level->liftActiveOn(level, new_level + 1);
|
deba@2514
|
687 |
} else {
|
deba@2514
|
688 |
_level->liftActiveToTop(level);
|
deba@2514
|
689 |
}
|
deba@2514
|
690 |
if (_level->emptyLevel(level)) {
|
deba@2514
|
691 |
_level->liftToTop(level);
|
deba@2514
|
692 |
}
|
deba@2514
|
693 |
} else {
|
deba@2514
|
694 |
_level->deactivate(n);
|
deba@2514
|
695 |
}
|
deba@2514
|
696 |
|
deba@2514
|
697 |
while (level >= 0 && _level->activeFree(level)) {
|
deba@2514
|
698 |
--level;
|
deba@2514
|
699 |
}
|
deba@2514
|
700 |
if (level == -1) {
|
deba@2514
|
701 |
n = _level->highestActive();
|
deba@2514
|
702 |
level = _level->highestActiveLevel();
|
deba@2514
|
703 |
} else {
|
deba@2514
|
704 |
n = _level->activeOn(level);
|
deba@2514
|
705 |
}
|
deba@2514
|
706 |
--num;
|
deba@2514
|
707 |
}
|
deba@2514
|
708 |
}
|
deba@2514
|
709 |
}
|
deba@2514
|
710 |
|
deba@2514
|
711 |
/// \brief Starts the second phase of the preflow algorithm.
|
deba@2514
|
712 |
///
|
deba@2514
|
713 |
/// The preflow algorithm consists of two phases, this method runs
|
deba@2514
|
714 |
/// the second phase. After calling \ref init() and \ref
|
deba@2514
|
715 |
/// startFirstPhase() and then \ref startSecondPhase(), \ref
|
deba@2514
|
716 |
/// flowMap() return a maximum flow, \ref flowValue() returns the
|
deba@2514
|
717 |
/// value of a maximum flow, \ref minCut() returns a minimum cut
|
deba@2514
|
718 |
/// \pre The \ref init() and startFirstPhase() functions should be
|
deba@2514
|
719 |
/// called before.
|
deba@2514
|
720 |
void startSecondPhase() {
|
deba@2514
|
721 |
_phase = false;
|
deba@2514
|
722 |
|
deba@2518
|
723 |
typename Graph::template NodeMap<bool> reached(_graph);
|
deba@2514
|
724 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
725 |
reached.set(n, (*_level)[n] < _level->maxLevel());
|
deba@2514
|
726 |
}
|
deba@2514
|
727 |
|
deba@2514
|
728 |
_level->initStart();
|
deba@2514
|
729 |
_level->initAddItem(_source);
|
deba@2514
|
730 |
|
deba@2514
|
731 |
std::vector<Node> queue;
|
deba@2514
|
732 |
queue.push_back(_source);
|
deba@2514
|
733 |
reached.set(_source, true);
|
deba@2514
|
734 |
|
deba@2514
|
735 |
while (!queue.empty()) {
|
deba@2514
|
736 |
_level->initNewLevel();
|
deba@2514
|
737 |
std::vector<Node> nqueue;
|
deba@2514
|
738 |
for (int i = 0; i < int(queue.size()); ++i) {
|
deba@2514
|
739 |
Node n = queue[i];
|
deba@2514
|
740 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
741 |
Node v = _graph.target(e);
|
deba@2514
|
742 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
|
deba@2514
|
743 |
reached.set(v, true);
|
deba@2514
|
744 |
_level->initAddItem(v);
|
deba@2514
|
745 |
nqueue.push_back(v);
|
deba@2514
|
746 |
}
|
deba@2514
|
747 |
}
|
deba@2514
|
748 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
749 |
Node u = _graph.source(e);
|
deba@2514
|
750 |
if (!reached[u] &&
|
deba@2514
|
751 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
deba@2514
|
752 |
reached.set(u, true);
|
deba@2514
|
753 |
_level->initAddItem(u);
|
deba@2514
|
754 |
nqueue.push_back(u);
|
deba@2514
|
755 |
}
|
deba@2514
|
756 |
}
|
deba@2514
|
757 |
}
|
deba@2514
|
758 |
queue.swap(nqueue);
|
deba@2514
|
759 |
}
|
deba@2514
|
760 |
_level->initFinish();
|
deba@2514
|
761 |
|
deba@2514
|
762 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2518
|
763 |
if (!reached[n]) {
|
deba@2518
|
764 |
_level->markToBottom(n);
|
deba@2518
|
765 |
} else if ((*_excess)[n] > 0 && _target != n) {
|
deba@2514
|
766 |
_level->activate(n);
|
deba@2514
|
767 |
}
|
deba@2514
|
768 |
}
|
deba@2514
|
769 |
|
deba@2514
|
770 |
Node n;
|
deba@2514
|
771 |
while ((n = _level->highestActive()) != INVALID) {
|
deba@2514
|
772 |
Value excess = (*_excess)[n];
|
deba@2514
|
773 |
int level = _level->highestActiveLevel();
|
deba@2514
|
774 |
int new_level = _level->maxLevel();
|
deba@2514
|
775 |
|
deba@2514
|
776 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
777 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
778 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
779 |
Node v = _graph.target(e);
|
deba@2514
|
780 |
if ((*_level)[v] < level) {
|
deba@2514
|
781 |
if (!_level->active(v) && v != _source) {
|
deba@2514
|
782 |
_level->activate(v);
|
deba@2514
|
783 |
}
|
deba@2514
|
784 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
785 |
_flow->set(e, (*_flow)[e] + excess);
|
deba@2514
|
786 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
787 |
excess = 0;
|
deba@2514
|
788 |
goto no_more_push;
|
deba@2514
|
789 |
} else {
|
deba@2514
|
790 |
excess -= rem;
|
deba@2514
|
791 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
792 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
793 |
}
|
deba@2514
|
794 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
795 |
new_level = (*_level)[v];
|
deba@2514
|
796 |
}
|
jacint@836
|
797 |
}
|
jacint@836
|
798 |
|
deba@2514
|
799 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
800 |
Value rem = (*_flow)[e];
|
deba@2514
|
801 |
if (!_tolerance.positive(rem)) continue;
|
deba@2514
|
802 |
Node v = _graph.source(e);
|
deba@2514
|
803 |
if ((*_level)[v] < level) {
|
deba@2514
|
804 |
if (!_level->active(v) && v != _source) {
|
deba@2514
|
805 |
_level->activate(v);
|
deba@2514
|
806 |
}
|
deba@2514
|
807 |
if (!_tolerance.less(rem, excess)) {
|
deba@2514
|
808 |
_flow->set(e, (*_flow)[e] - excess);
|
deba@2514
|
809 |
_excess->set(v, (*_excess)[v] + excess);
|
deba@2514
|
810 |
excess = 0;
|
deba@2514
|
811 |
goto no_more_push;
|
deba@2514
|
812 |
} else {
|
deba@2514
|
813 |
excess -= rem;
|
deba@2514
|
814 |
_excess->set(v, (*_excess)[v] + rem);
|
deba@2514
|
815 |
_flow->set(e, 0);
|
deba@2514
|
816 |
}
|
deba@2514
|
817 |
} else if (new_level > (*_level)[v]) {
|
deba@2514
|
818 |
new_level = (*_level)[v];
|
jacint@836
|
819 |
}
|
jacint@836
|
820 |
}
|
deba@2514
|
821 |
|
deba@2514
|
822 |
no_more_push:
|
deba@2514
|
823 |
|
deba@2514
|
824 |
_excess->set(n, excess);
|
deba@2514
|
825 |
|
deba@2514
|
826 |
if (excess != 0) {
|
deba@2514
|
827 |
if (new_level + 1 < _level->maxLevel()) {
|
deba@2514
|
828 |
_level->liftHighestActive(new_level + 1);
|
deba@2514
|
829 |
} else {
|
deba@2514
|
830 |
// Calculation error
|
deba@2514
|
831 |
_level->liftHighestActiveToTop();
|
jacint@836
|
832 |
}
|
deba@2514
|
833 |
if (_level->emptyLevel(level)) {
|
deba@2514
|
834 |
// Calculation error
|
deba@2514
|
835 |
_level->liftToTop(level);
|
deba@2514
|
836 |
}
|
jacint@836
|
837 |
} else {
|
deba@2514
|
838 |
_level->deactivate(n);
|
jacint@836
|
839 |
}
|
jacint@836
|
840 |
|
deba@2514
|
841 |
}
|
deba@2514
|
842 |
}
|
jacint@836
|
843 |
|
deba@2514
|
844 |
/// \brief Runs the preflow algorithm.
|
deba@2514
|
845 |
///
|
deba@2514
|
846 |
/// Runs the preflow algorithm.
|
deba@2514
|
847 |
/// \note pf.run() is just a shortcut of the following code.
|
deba@2514
|
848 |
/// \code
|
deba@2514
|
849 |
/// pf.init();
|
deba@2514
|
850 |
/// pf.startFirstPhase();
|
deba@2514
|
851 |
/// pf.startSecondPhase();
|
deba@2514
|
852 |
/// \endcode
|
deba@2514
|
853 |
void run() {
|
deba@2514
|
854 |
init();
|
deba@2514
|
855 |
startFirstPhase();
|
deba@2514
|
856 |
startSecondPhase();
|
deba@2514
|
857 |
}
|
jacint@836
|
858 |
|
deba@2514
|
859 |
/// \brief Runs the preflow algorithm to compute the minimum cut.
|
deba@2514
|
860 |
///
|
deba@2514
|
861 |
/// Runs the preflow algorithm to compute the minimum cut.
|
deba@2514
|
862 |
/// \note pf.runMinCut() is just a shortcut of the following code.
|
deba@2514
|
863 |
/// \code
|
deba@2514
|
864 |
/// pf.init();
|
deba@2514
|
865 |
/// pf.startFirstPhase();
|
deba@2514
|
866 |
/// \endcode
|
deba@2514
|
867 |
void runMinCut() {
|
deba@2514
|
868 |
init();
|
deba@2514
|
869 |
startFirstPhase();
|
deba@2514
|
870 |
}
|
deba@2514
|
871 |
|
deba@2514
|
872 |
/// @}
|
deba@2514
|
873 |
|
deba@2514
|
874 |
/// \name Query Functions
|
deba@2522
|
875 |
/// The result of the %Preflow algorithm can be obtained using these
|
deba@2514
|
876 |
/// functions.\n
|
deba@2514
|
877 |
/// Before the use of these functions,
|
deba@2514
|
878 |
/// either run() or start() must be called.
|
deba@2514
|
879 |
|
deba@2514
|
880 |
///@{
|
deba@2514
|
881 |
|
deba@2514
|
882 |
/// \brief Returns the value of the maximum flow.
|
deba@2514
|
883 |
///
|
deba@2514
|
884 |
/// Returns the value of the maximum flow by returning the excess
|
deba@2514
|
885 |
/// of the target node \c t. This value equals to the value of
|
deba@2514
|
886 |
/// the maximum flow already after the first phase.
|
deba@2514
|
887 |
Value flowValue() const {
|
deba@2514
|
888 |
return (*_excess)[_target];
|
deba@2514
|
889 |
}
|
deba@2514
|
890 |
|
deba@2514
|
891 |
/// \brief Returns true when the node is on the source side of minimum cut.
|
deba@2514
|
892 |
///
|
deba@2514
|
893 |
/// Returns true when the node is on the source side of minimum
|
deba@2514
|
894 |
/// cut. This method can be called both after running \ref
|
deba@2514
|
895 |
/// startFirstPhase() and \ref startSecondPhase().
|
deba@2514
|
896 |
bool minCut(const Node& node) const {
|
deba@2514
|
897 |
return ((*_level)[node] == _level->maxLevel()) == _phase;
|
deba@2514
|
898 |
}
|
deba@2514
|
899 |
|
deba@2514
|
900 |
/// \brief Returns a minimum value cut.
|
deba@2514
|
901 |
///
|
deba@2514
|
902 |
/// Sets the \c cutMap to the characteristic vector of a minimum value
|
deba@2514
|
903 |
/// cut. This method can be called both after running \ref
|
deba@2514
|
904 |
/// startFirstPhase() and \ref startSecondPhase(). The result after second
|
deba@2514
|
905 |
/// phase could be changed slightly if inexact computation is used.
|
deba@2514
|
906 |
/// \pre The \c cutMap should be a bool-valued node-map.
|
deba@2514
|
907 |
template <typename CutMap>
|
deba@2514
|
908 |
void minCutMap(CutMap& cutMap) const {
|
deba@2514
|
909 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
910 |
cutMap.set(n, minCut(n));
|
jacint@836
|
911 |
}
|
deba@2514
|
912 |
}
|
jacint@836
|
913 |
|
deba@2514
|
914 |
/// \brief Returns the flow on the edge.
|
deba@2514
|
915 |
///
|
deba@2514
|
916 |
/// Sets the \c flowMap to the flow on the edges. This method can
|
deba@2514
|
917 |
/// be called after the second phase of algorithm.
|
deba@2514
|
918 |
Value flow(const Edge& edge) const {
|
deba@2514
|
919 |
return (*_flow)[edge];
|
deba@2514
|
920 |
}
|
deba@2514
|
921 |
|
deba@2514
|
922 |
/// @}
|
deba@2514
|
923 |
};
|
deba@2514
|
924 |
}
|
alpar@1227
|
925 |
|
deba@2514
|
926 |
#endif
|