| ... |
... |
@@ -256,385 +256,385 @@
|
| 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 |
|
bool flowInit(const FlowMap& flowMap) {
|
|
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();
|
| 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);
|