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