... | ... |
@@ -352,193 +352,193 @@ |
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 |
bool |
|
448 |
bool init(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(); |
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 | 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 |
preflow_test. |
|
58 |
preflow_test.init(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 |
preflow_test. |
|
156 |
preflow_test.init(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)