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