0
9
0
17
17
55
55
73
73
14
14
89
89
50
50
31
31
10
10
... | ... |
@@ -444,70 +444,70 @@ |
444 | 444 |
|
445 | 445 |
///@{ |
446 | 446 |
|
447 | 447 |
/// Initializes the internal data structures. |
448 | 448 |
|
449 | 449 |
/// Initializes the internal data structures and sets all flow values |
450 | 450 |
/// to the lower bound. |
451 | 451 |
void init() |
452 | 452 |
{ |
453 | 453 |
createStructures(); |
454 | 454 |
|
455 | 455 |
for(NodeIt n(_g);n!=INVALID;++n) { |
456 |
_excess |
|
456 |
(*_excess)[n] = (*_delta)[n]; |
|
457 | 457 |
} |
458 | 458 |
|
459 | 459 |
for (ArcIt e(_g);e!=INVALID;++e) { |
460 | 460 |
_flow->set(e, (*_lo)[e]); |
461 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]); |
|
462 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]); |
|
461 |
(*_excess)[_g.target(e)] += (*_flow)[e]; |
|
462 |
(*_excess)[_g.source(e)] -= (*_flow)[e]; |
|
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
// global relabeling tested, but in general case it provides |
466 | 466 |
// worse performance for random digraphs |
467 | 467 |
_level->initStart(); |
468 | 468 |
for(NodeIt n(_g);n!=INVALID;++n) |
469 | 469 |
_level->initAddItem(n); |
470 | 470 |
_level->initFinish(); |
471 | 471 |
for(NodeIt n(_g);n!=INVALID;++n) |
472 | 472 |
if(_tol.positive((*_excess)[n])) |
473 | 473 |
_level->activate(n); |
474 | 474 |
} |
475 | 475 |
|
476 | 476 |
/// Initializes the internal data structures using a greedy approach. |
477 | 477 |
|
478 | 478 |
/// Initializes the internal data structures using a greedy approach |
479 | 479 |
/// to construct the initial solution. |
480 | 480 |
void greedyInit() |
481 | 481 |
{ |
482 | 482 |
createStructures(); |
483 | 483 |
|
484 | 484 |
for(NodeIt n(_g);n!=INVALID;++n) { |
485 |
_excess |
|
485 |
(*_excess)[n] = (*_delta)[n]; |
|
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
for (ArcIt e(_g);e!=INVALID;++e) { |
489 | 489 |
if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) { |
490 | 490 |
_flow->set(e, (*_up)[e]); |
491 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]); |
|
492 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]); |
|
491 |
(*_excess)[_g.target(e)] += (*_up)[e]; |
|
492 |
(*_excess)[_g.source(e)] -= (*_up)[e]; |
|
493 | 493 |
} else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) { |
494 | 494 |
_flow->set(e, (*_lo)[e]); |
495 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]); |
|
496 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]); |
|
495 |
(*_excess)[_g.target(e)] += (*_lo)[e]; |
|
496 |
(*_excess)[_g.source(e)] -= (*_lo)[e]; |
|
497 | 497 |
} else { |
498 | 498 |
Value fc = -(*_excess)[_g.target(e)]; |
499 | 499 |
_flow->set(e, fc); |
500 |
_excess->set(_g.target(e), 0); |
|
501 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc); |
|
500 |
(*_excess)[_g.target(e)] = 0; |
|
501 |
(*_excess)[_g.source(e)] -= fc; |
|
502 | 502 |
} |
503 | 503 |
} |
504 | 504 |
|
505 | 505 |
_level->initStart(); |
506 | 506 |
for(NodeIt n(_g);n!=INVALID;++n) |
507 | 507 |
_level->initAddItem(n); |
508 | 508 |
_level->initFinish(); |
509 | 509 |
for(NodeIt n(_g);n!=INVALID;++n) |
510 | 510 |
if(_tol.positive((*_excess)[n])) |
511 | 511 |
_level->activate(n); |
512 | 512 |
} |
513 | 513 |
|
... | ... |
@@ -528,67 +528,67 @@ |
528 | 528 |
while((act=_level->highestActive())!=INVALID) { |
529 | 529 |
int actlevel=(*_level)[act]; |
530 | 530 |
int mlevel=_node_num; |
531 | 531 |
Value exc=(*_excess)[act]; |
532 | 532 |
|
533 | 533 |
for(OutArcIt e(_g,act);e!=INVALID; ++e) { |
534 | 534 |
Node v = _g.target(e); |
535 | 535 |
Value fc=(*_up)[e]-(*_flow)[e]; |
536 | 536 |
if(!_tol.positive(fc)) continue; |
537 | 537 |
if((*_level)[v]<actlevel) { |
538 | 538 |
if(!_tol.less(fc, exc)) { |
539 | 539 |
_flow->set(e, (*_flow)[e] + exc); |
540 |
|
|
540 |
(*_excess)[v] += exc; |
|
541 | 541 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
542 | 542 |
_level->activate(v); |
543 |
|
|
543 |
(*_excess)[act] = 0; |
|
544 | 544 |
_level->deactivate(act); |
545 | 545 |
goto next_l; |
546 | 546 |
} |
547 | 547 |
else { |
548 | 548 |
_flow->set(e, (*_up)[e]); |
549 |
|
|
549 |
(*_excess)[v] += fc; |
|
550 | 550 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
551 | 551 |
_level->activate(v); |
552 | 552 |
exc-=fc; |
553 | 553 |
} |
554 | 554 |
} |
555 | 555 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
556 | 556 |
} |
557 | 557 |
for(InArcIt e(_g,act);e!=INVALID; ++e) { |
558 | 558 |
Node v = _g.source(e); |
559 | 559 |
Value fc=(*_flow)[e]-(*_lo)[e]; |
560 | 560 |
if(!_tol.positive(fc)) continue; |
561 | 561 |
if((*_level)[v]<actlevel) { |
562 | 562 |
if(!_tol.less(fc, exc)) { |
563 | 563 |
_flow->set(e, (*_flow)[e] - exc); |
564 |
|
|
564 |
(*_excess)[v] += exc; |
|
565 | 565 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
566 | 566 |
_level->activate(v); |
567 |
|
|
567 |
(*_excess)[act] = 0; |
|
568 | 568 |
_level->deactivate(act); |
569 | 569 |
goto next_l; |
570 | 570 |
} |
571 | 571 |
else { |
572 | 572 |
_flow->set(e, (*_lo)[e]); |
573 |
|
|
573 |
(*_excess)[v] += fc; |
|
574 | 574 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
575 | 575 |
_level->activate(v); |
576 | 576 |
exc-=fc; |
577 | 577 |
} |
578 | 578 |
} |
579 | 579 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
580 | 580 |
} |
581 | 581 |
|
582 |
_excess |
|
582 |
(*_excess)[act] = exc; |
|
583 | 583 |
if(!_tol.positive(exc)) _level->deactivate(act); |
584 | 584 |
else if(mlevel==_node_num) { |
585 | 585 |
_level->liftHighestActiveToTop(); |
586 | 586 |
_el = _node_num; |
587 | 587 |
return false; |
588 | 588 |
} |
589 | 589 |
else { |
590 | 590 |
_level->liftHighestActive(mlevel+1); |
591 | 591 |
if(_level->onLevel(actlevel)==0) { |
592 | 592 |
_el = actlevel; |
593 | 593 |
return false; |
594 | 594 |
} |
... | ... |
@@ -1306,201 +1306,201 @@ |
1306 | 1306 |
virtual void erase(const std::vector<Arc>& arcs) { |
1307 | 1307 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1308 | 1308 |
remove(arcs[i]); |
1309 | 1309 |
} |
1310 | 1310 |
} |
1311 | 1311 |
|
1312 | 1312 |
virtual void build() { |
1313 | 1313 |
refresh(); |
1314 | 1314 |
} |
1315 | 1315 |
|
1316 | 1316 |
virtual void clear() { |
1317 | 1317 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1318 |
_head |
|
1318 |
_head[n] = INVALID; |
|
1319 | 1319 |
} |
1320 | 1320 |
} |
1321 | 1321 |
|
1322 | 1322 |
void insert(Arc arc) { |
1323 | 1323 |
Node s = _g.source(arc); |
1324 | 1324 |
Node t = _g.target(arc); |
1325 |
_left.set(arc, INVALID); |
|
1326 |
_right.set(arc, INVALID); |
|
1325 |
_left[arc] = INVALID; |
|
1326 |
_right[arc] = INVALID; |
|
1327 | 1327 |
|
1328 | 1328 |
Arc e = _head[s]; |
1329 | 1329 |
if (e == INVALID) { |
1330 |
_head.set(s, arc); |
|
1331 |
_parent.set(arc, INVALID); |
|
1330 |
_head[s] = arc; |
|
1331 |
_parent[arc] = INVALID; |
|
1332 | 1332 |
return; |
1333 | 1333 |
} |
1334 | 1334 |
while (true) { |
1335 | 1335 |
if (t < _g.target(e)) { |
1336 | 1336 |
if (_left[e] == INVALID) { |
1337 |
_left.set(e, arc); |
|
1338 |
_parent.set(arc, e); |
|
1337 |
_left[e] = arc; |
|
1338 |
_parent[arc] = e; |
|
1339 | 1339 |
splay(arc); |
1340 | 1340 |
return; |
1341 | 1341 |
} else { |
1342 | 1342 |
e = _left[e]; |
1343 | 1343 |
} |
1344 | 1344 |
} else { |
1345 | 1345 |
if (_right[e] == INVALID) { |
1346 |
_right.set(e, arc); |
|
1347 |
_parent.set(arc, e); |
|
1346 |
_right[e] = arc; |
|
1347 |
_parent[arc] = e; |
|
1348 | 1348 |
splay(arc); |
1349 | 1349 |
return; |
1350 | 1350 |
} else { |
1351 | 1351 |
e = _right[e]; |
1352 | 1352 |
} |
1353 | 1353 |
} |
1354 | 1354 |
} |
1355 | 1355 |
} |
1356 | 1356 |
|
1357 | 1357 |
void remove(Arc arc) { |
1358 | 1358 |
if (_left[arc] == INVALID) { |
1359 | 1359 |
if (_right[arc] != INVALID) { |
1360 |
_parent |
|
1360 |
_parent[_right[arc]] = _parent[arc]; |
|
1361 | 1361 |
} |
1362 | 1362 |
if (_parent[arc] != INVALID) { |
1363 | 1363 |
if (_left[_parent[arc]] == arc) { |
1364 |
_left |
|
1364 |
_left[_parent[arc]] = _right[arc]; |
|
1365 | 1365 |
} else { |
1366 |
_right |
|
1366 |
_right[_parent[arc]] = _right[arc]; |
|
1367 | 1367 |
} |
1368 | 1368 |
} else { |
1369 |
_head |
|
1369 |
_head[_g.source(arc)] = _right[arc]; |
|
1370 | 1370 |
} |
1371 | 1371 |
} else if (_right[arc] == INVALID) { |
1372 |
_parent |
|
1372 |
_parent[_left[arc]] = _parent[arc]; |
|
1373 | 1373 |
if (_parent[arc] != INVALID) { |
1374 | 1374 |
if (_left[_parent[arc]] == arc) { |
1375 |
_left |
|
1375 |
_left[_parent[arc]] = _left[arc]; |
|
1376 | 1376 |
} else { |
1377 |
_right |
|
1377 |
_right[_parent[arc]] = _left[arc]; |
|
1378 | 1378 |
} |
1379 | 1379 |
} else { |
1380 |
_head |
|
1380 |
_head[_g.source(arc)] = _left[arc]; |
|
1381 | 1381 |
} |
1382 | 1382 |
} else { |
1383 | 1383 |
Arc e = _left[arc]; |
1384 | 1384 |
if (_right[e] != INVALID) { |
1385 | 1385 |
e = _right[e]; |
1386 | 1386 |
while (_right[e] != INVALID) { |
1387 | 1387 |
e = _right[e]; |
1388 | 1388 |
} |
1389 | 1389 |
Arc s = _parent[e]; |
1390 |
_right |
|
1390 |
_right[_parent[e]] = _left[e]; |
|
1391 | 1391 |
if (_left[e] != INVALID) { |
1392 |
_parent |
|
1392 |
_parent[_left[e]] = _parent[e]; |
|
1393 | 1393 |
} |
1394 | 1394 |
|
1395 |
_left.set(e, _left[arc]); |
|
1396 |
_parent.set(_left[arc], e); |
|
1397 |
_right.set(e, _right[arc]); |
|
1398 |
_parent.set(_right[arc], e); |
|
1395 |
_left[e] = _left[arc]; |
|
1396 |
_parent[_left[arc]] = e; |
|
1397 |
_right[e] = _right[arc]; |
|
1398 |
_parent[_right[arc]] = e; |
|
1399 | 1399 |
|
1400 |
_parent |
|
1400 |
_parent[e] = _parent[arc]; |
|
1401 | 1401 |
if (_parent[arc] != INVALID) { |
1402 | 1402 |
if (_left[_parent[arc]] == arc) { |
1403 |
_left |
|
1403 |
_left[_parent[arc]] = e; |
|
1404 | 1404 |
} else { |
1405 |
_right |
|
1405 |
_right[_parent[arc]] = e; |
|
1406 | 1406 |
} |
1407 | 1407 |
} |
1408 | 1408 |
splay(s); |
1409 | 1409 |
} else { |
1410 |
_right.set(e, _right[arc]); |
|
1411 |
_parent.set(_right[arc], e); |
|
1412 |
|
|
1410 |
_right[e] = _right[arc]; |
|
1411 |
_parent[_right[arc]] = e; |
|
1412 |
_parent[e] = _parent[arc]; |
|
1413 | 1413 |
|
1414 | 1414 |
if (_parent[arc] != INVALID) { |
1415 | 1415 |
if (_left[_parent[arc]] == arc) { |
1416 |
_left |
|
1416 |
_left[_parent[arc]] = e; |
|
1417 | 1417 |
} else { |
1418 |
_right |
|
1418 |
_right[_parent[arc]] = e; |
|
1419 | 1419 |
} |
1420 | 1420 |
} else { |
1421 |
_head |
|
1421 |
_head[_g.source(arc)] = e; |
|
1422 | 1422 |
} |
1423 | 1423 |
} |
1424 | 1424 |
} |
1425 | 1425 |
} |
1426 | 1426 |
|
1427 | 1427 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1428 | 1428 |
{ |
1429 | 1429 |
int m=(a+b)/2; |
1430 | 1430 |
Arc me=v[m]; |
1431 | 1431 |
if (a < m) { |
1432 | 1432 |
Arc left = refreshRec(v,a,m-1); |
1433 |
_left.set(me, left); |
|
1434 |
_parent.set(left, me); |
|
1433 |
_left[me] = left; |
|
1434 |
_parent[left] = me; |
|
1435 | 1435 |
} else { |
1436 |
_left |
|
1436 |
_left[me] = INVALID; |
|
1437 | 1437 |
} |
1438 | 1438 |
if (m < b) { |
1439 | 1439 |
Arc right = refreshRec(v,m+1,b); |
1440 |
_right.set(me, right); |
|
1441 |
_parent.set(right, me); |
|
1440 |
_right[me] = right; |
|
1441 |
_parent[right] = me; |
|
1442 | 1442 |
} else { |
1443 |
_right |
|
1443 |
_right[me] = INVALID; |
|
1444 | 1444 |
} |
1445 | 1445 |
return me; |
1446 | 1446 |
} |
1447 | 1447 |
|
1448 | 1448 |
void refresh() { |
1449 | 1449 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1450 | 1450 |
std::vector<Arc> v; |
1451 | 1451 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
1452 | 1452 |
if (!v.empty()) { |
1453 | 1453 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1454 | 1454 |
Arc head = refreshRec(v,0,v.size()-1); |
1455 |
_head.set(n, head); |
|
1456 |
_parent.set(head, INVALID); |
|
1455 |
_head[n] = head; |
|
1456 |
_parent[head] = INVALID; |
|
1457 | 1457 |
} |
1458 |
else _head |
|
1458 |
else _head[n] = INVALID; |
|
1459 | 1459 |
} |
1460 | 1460 |
} |
1461 | 1461 |
|
1462 | 1462 |
void zig(Arc v) { |
1463 | 1463 |
Arc w = _parent[v]; |
1464 |
_parent.set(v, _parent[w]); |
|
1465 |
_parent.set(w, v); |
|
1466 |
_left.set(w, _right[v]); |
|
1467 |
_right.set(v, w); |
|
1464 |
_parent[v] = _parent[w]; |
|
1465 |
_parent[w] = v; |
|
1466 |
_left[w] = _right[v]; |
|
1467 |
_right[v] = w; |
|
1468 | 1468 |
if (_parent[v] != INVALID) { |
1469 | 1469 |
if (_right[_parent[v]] == w) { |
1470 |
_right |
|
1470 |
_right[_parent[v]] = v; |
|
1471 | 1471 |
} else { |
1472 |
_left |
|
1472 |
_left[_parent[v]] = v; |
|
1473 | 1473 |
} |
1474 | 1474 |
} |
1475 | 1475 |
if (_left[w] != INVALID){ |
1476 |
_parent |
|
1476 |
_parent[_left[w]] = w; |
|
1477 | 1477 |
} |
1478 | 1478 |
} |
1479 | 1479 |
|
1480 | 1480 |
void zag(Arc v) { |
1481 | 1481 |
Arc w = _parent[v]; |
1482 |
_parent.set(v, _parent[w]); |
|
1483 |
_parent.set(w, v); |
|
1484 |
_right.set(w, _left[v]); |
|
1485 |
_left.set(v, w); |
|
1482 |
_parent[v] = _parent[w]; |
|
1483 |
_parent[w] = v; |
|
1484 |
_right[w] = _left[v]; |
|
1485 |
_left[v] = w; |
|
1486 | 1486 |
if (_parent[v] != INVALID){ |
1487 | 1487 |
if (_left[_parent[v]] == w) { |
1488 |
_left |
|
1488 |
_left[_parent[v]] = v; |
|
1489 | 1489 |
} else { |
1490 |
_right |
|
1490 |
_right[_parent[v]] = v; |
|
1491 | 1491 |
} |
1492 | 1492 |
} |
1493 | 1493 |
if (_right[w] != INVALID){ |
1494 |
_parent |
|
1494 |
_parent[_right[w]] = w; |
|
1495 | 1495 |
} |
1496 | 1496 |
} |
1497 | 1497 |
|
1498 | 1498 |
void splay(Arc v) { |
1499 | 1499 |
while (_parent[v] != INVALID) { |
1500 | 1500 |
if (v == _left[_parent[v]]) { |
1501 | 1501 |
if (_parent[_parent[v]] == INVALID) { |
1502 | 1502 |
zig(v); |
1503 | 1503 |
} else { |
1504 | 1504 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1505 | 1505 |
zig(_parent[v]); |
1506 | 1506 |
zig(v); |
... | ... |
@@ -67,41 +67,41 @@ |
67 | 67 |
int _max_level; |
68 | 68 |
int _item_num; |
69 | 69 |
VitMap _where; |
70 | 70 |
IntMap _level; |
71 | 71 |
std::vector<Item> _items; |
72 | 72 |
std::vector<Vit> _first; |
73 | 73 |
std::vector<Vit> _last_active; |
74 | 74 |
|
75 | 75 |
int _highest_active; |
76 | 76 |
|
77 | 77 |
void copy(Item i, Vit p) |
78 | 78 |
{ |
79 |
_where |
|
79 |
_where[*p=i] = p; |
|
80 | 80 |
} |
81 | 81 |
void copy(Vit s, Vit p) |
82 | 82 |
{ |
83 | 83 |
if(s!=p) |
84 | 84 |
{ |
85 | 85 |
Item i=*s; |
86 | 86 |
*p=i; |
87 |
_where |
|
87 |
_where[i] = p; |
|
88 | 88 |
} |
89 | 89 |
} |
90 | 90 |
void swap(Vit i, Vit j) |
91 | 91 |
{ |
92 | 92 |
Item ti=*i; |
93 | 93 |
Vit ct = _where[ti]; |
94 |
_where.set(ti,_where[*i=*j]); |
|
95 |
_where.set(*j,ct); |
|
94 |
_where[ti] = _where[*i=*j]; |
|
95 |
_where[*j] = ct; |
|
96 | 96 |
*j=ti; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
public: |
100 | 100 |
|
101 | 101 |
///Constructor with given maximum level. |
102 | 102 |
|
103 | 103 |
///Constructor with given maximum level. |
104 | 104 |
/// |
105 | 105 |
///\param graph The underlying graph. |
106 | 106 |
///\param max_level The maximum allowed level. |
107 | 107 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
... | ... |
@@ -217,68 +217,68 @@ |
217 | 217 |
int highestActiveLevel() const |
218 | 218 |
{ |
219 | 219 |
return _highest_active; |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
///Lift the highest active item by one. |
223 | 223 |
|
224 | 224 |
///Lift the item returned by highestActive() by one. |
225 | 225 |
/// |
226 | 226 |
void liftHighestActive() |
227 | 227 |
{ |
228 | 228 |
Item it = *_last_active[_highest_active]; |
229 |
|
|
229 |
++_level[it]; |
|
230 | 230 |
swap(_last_active[_highest_active]--,_last_active[_highest_active+1]); |
231 | 231 |
--_first[++_highest_active]; |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
///Lift the highest active item to the given level. |
235 | 235 |
|
236 | 236 |
///Lift the item returned by highestActive() to level \c new_level. |
237 | 237 |
/// |
238 | 238 |
///\warning \c new_level must be strictly higher |
239 | 239 |
///than the current level. |
240 | 240 |
/// |
241 | 241 |
void liftHighestActive(int new_level) |
242 | 242 |
{ |
243 | 243 |
const Item li = *_last_active[_highest_active]; |
244 | 244 |
|
245 | 245 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
246 | 246 |
for(int l=_highest_active+1;l<new_level;l++) |
247 | 247 |
{ |
248 | 248 |
copy(--_first[l+1],_first[l]); |
249 | 249 |
--_last_active[l]; |
250 | 250 |
} |
251 | 251 |
copy(li,_first[new_level]); |
252 |
_level |
|
252 |
_level[li] = new_level; |
|
253 | 253 |
_highest_active=new_level; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
///Lift the highest active item to the top level. |
257 | 257 |
|
258 | 258 |
///Lift the item returned by highestActive() to the top level and |
259 | 259 |
///deactivate it. |
260 | 260 |
void liftHighestActiveToTop() |
261 | 261 |
{ |
262 | 262 |
const Item li = *_last_active[_highest_active]; |
263 | 263 |
|
264 | 264 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
265 | 265 |
for(int l=_highest_active+1;l<_max_level;l++) |
266 | 266 |
{ |
267 | 267 |
copy(--_first[l+1],_first[l]); |
268 | 268 |
--_last_active[l]; |
269 | 269 |
} |
270 | 270 |
copy(li,_first[_max_level]); |
271 | 271 |
--_last_active[_max_level]; |
272 |
_level |
|
272 |
_level[li] = _max_level; |
|
273 | 273 |
|
274 | 274 |
while(_highest_active>=0 && |
275 | 275 |
_last_active[_highest_active]<_first[_highest_active]) |
276 | 276 |
_highest_active--; |
277 | 277 |
} |
278 | 278 |
|
279 | 279 |
///@} |
280 | 280 |
|
281 | 281 |
///\name Active Item on Certain Level |
282 | 282 |
///Functions for working with the active items. |
283 | 283 |
|
284 | 284 |
///@{ |
... | ... |
@@ -290,65 +290,65 @@ |
290 | 290 |
Item activeOn(int l) const |
291 | 291 |
{ |
292 | 292 |
return _last_active[l]>=_first[l]?*_last_active[l]:INVALID; |
293 | 293 |
} |
294 | 294 |
|
295 | 295 |
///Lift the active item returned by \c activeOn(level) by one. |
296 | 296 |
|
297 | 297 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
298 | 298 |
///by one. |
299 | 299 |
Item liftActiveOn(int level) |
300 | 300 |
{ |
301 | 301 |
Item it =*_last_active[level]; |
302 |
|
|
302 |
++_level[it]; |
|
303 | 303 |
swap(_last_active[level]--, --_first[level+1]); |
304 | 304 |
if (level+1>_highest_active) ++_highest_active; |
305 | 305 |
} |
306 | 306 |
|
307 | 307 |
///Lift the active item returned by \c activeOn(level) to the given level. |
308 | 308 |
|
309 | 309 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
310 | 310 |
///to the given level. |
311 | 311 |
void liftActiveOn(int level, int new_level) |
312 | 312 |
{ |
313 | 313 |
const Item ai = *_last_active[level]; |
314 | 314 |
|
315 | 315 |
copy(--_first[level+1], _last_active[level]--); |
316 | 316 |
for(int l=level+1;l<new_level;l++) |
317 | 317 |
{ |
318 | 318 |
copy(_last_active[l],_first[l]); |
319 | 319 |
copy(--_first[l+1], _last_active[l]--); |
320 | 320 |
} |
321 | 321 |
copy(ai,_first[new_level]); |
322 |
_level |
|
322 |
_level[ai] = new_level; |
|
323 | 323 |
if (new_level>_highest_active) _highest_active=new_level; |
324 | 324 |
} |
325 | 325 |
|
326 | 326 |
///Lift the active item returned by \c activeOn(level) to the top level. |
327 | 327 |
|
328 | 328 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
329 | 329 |
///to the top level and deactivate it. |
330 | 330 |
void liftActiveToTop(int level) |
331 | 331 |
{ |
332 | 332 |
const Item ai = *_last_active[level]; |
333 | 333 |
|
334 | 334 |
copy(--_first[level+1],_last_active[level]--); |
335 | 335 |
for(int l=level+1;l<_max_level;l++) |
336 | 336 |
{ |
337 | 337 |
copy(_last_active[l],_first[l]); |
338 | 338 |
copy(--_first[l+1], _last_active[l]--); |
339 | 339 |
} |
340 | 340 |
copy(ai,_first[_max_level]); |
341 | 341 |
--_last_active[_max_level]; |
342 |
_level |
|
342 |
_level[ai] = _max_level; |
|
343 | 343 |
|
344 | 344 |
if (_highest_active==level) { |
345 | 345 |
while(_highest_active>=0 && |
346 | 346 |
_last_active[_highest_active]<_first[_highest_active]) |
347 | 347 |
_highest_active--; |
348 | 348 |
} |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
///@} |
352 | 352 |
|
353 | 353 |
///Lift an active item to a higher level. |
354 | 354 |
|
... | ... |
@@ -361,49 +361,49 @@ |
361 | 361 |
{ |
362 | 362 |
const int lo = _level[i]; |
363 | 363 |
const Vit w = _where[i]; |
364 | 364 |
|
365 | 365 |
copy(_last_active[lo],w); |
366 | 366 |
copy(--_first[lo+1],_last_active[lo]--); |
367 | 367 |
for(int l=lo+1;l<new_level;l++) |
368 | 368 |
{ |
369 | 369 |
copy(_last_active[l],_first[l]); |
370 | 370 |
copy(--_first[l+1],_last_active[l]--); |
371 | 371 |
} |
372 | 372 |
copy(i,_first[new_level]); |
373 |
_level |
|
373 |
_level[i] = new_level; |
|
374 | 374 |
if(new_level>_highest_active) _highest_active=new_level; |
375 | 375 |
} |
376 | 376 |
|
377 | 377 |
///Move an inactive item to the top but one level (in a dirty way). |
378 | 378 |
|
379 | 379 |
///This function moves an inactive item from the top level to the top |
380 | 380 |
///but one level (in a dirty way). |
381 | 381 |
///\warning It makes the underlying datastructure corrupt, so use it |
382 | 382 |
///only if you really know what it is for. |
383 | 383 |
///\pre The item is on the top level. |
384 | 384 |
void dirtyTopButOne(Item i) { |
385 |
_level |
|
385 |
_level[i] = _max_level - 1; |
|
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
///Lift all items on and above the given level to the top level. |
389 | 389 |
|
390 | 390 |
///This function lifts all items on and above level \c l to the top |
391 | 391 |
///level and deactivates them. |
392 | 392 |
void liftToTop(int l) |
393 | 393 |
{ |
394 | 394 |
const Vit f=_first[l]; |
395 | 395 |
const Vit tl=_first[_max_level]; |
396 | 396 |
for(Vit i=f;i!=tl;++i) |
397 |
_level |
|
397 |
_level[*i] = _max_level; |
|
398 | 398 |
for(int i=l;i<=_max_level;i++) |
399 | 399 |
{ |
400 | 400 |
_first[i]=f; |
401 | 401 |
_last_active[i]=f-1; |
402 | 402 |
} |
403 | 403 |
for(_highest_active=l-1; |
404 | 404 |
_highest_active>=0 && |
405 | 405 |
_last_active[_highest_active]<_first[_highest_active]; |
406 | 406 |
_highest_active--) ; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
private: |
... | ... |
@@ -424,35 +424,35 @@ |
424 | 424 |
|
425 | 425 |
///Start the initialization process. |
426 | 426 |
void initStart() |
427 | 427 |
{ |
428 | 428 |
_init_lev=0; |
429 | 429 |
_init_num=&_items[0]; |
430 | 430 |
_first[0]=&_items[0]; |
431 | 431 |
_last_active[0]=&_items[0]-1; |
432 | 432 |
Vit n=&_items[0]; |
433 | 433 |
for(typename ItemSetTraits<GR,Item>::ItemIt i(_g);i!=INVALID;++i) |
434 | 434 |
{ |
435 | 435 |
*n=i; |
436 |
_where.set(i,n); |
|
437 |
_level.set(i,_max_level); |
|
436 |
_where[i] = n; |
|
437 |
_level[i] = _max_level; |
|
438 | 438 |
++n; |
439 | 439 |
} |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
///Add an item to the current level. |
443 | 443 |
void initAddItem(Item i) |
444 | 444 |
{ |
445 | 445 |
swap(_where[i],_init_num); |
446 |
_level |
|
446 |
_level[i] = _init_lev; |
|
447 | 447 |
++_init_num; |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
///Start a new level. |
451 | 451 |
|
452 | 452 |
///Start a new level. |
453 | 453 |
///It shouldn't be used before the items on level 0 are listed. |
454 | 454 |
void initNewLevel() |
455 | 455 |
{ |
456 | 456 |
_init_lev++; |
457 | 457 |
_first[_init_lev]=_init_num; |
458 | 458 |
_last_active[_init_lev]=_init_num-1; |
... | ... |
@@ -542,69 +542,69 @@ |
542 | 542 |
: _graph(graph), _max_level(countItems<GR, Item>(graph)), |
543 | 543 |
_item_num(_max_level), |
544 | 544 |
_first(_max_level + 1), _last(_max_level + 1), |
545 | 545 |
_prev(graph, INVALID), _next(graph, INVALID), |
546 | 546 |
_highest_active(-1), _level(graph), _active(graph) {} |
547 | 547 |
|
548 | 548 |
|
549 | 549 |
///Activate item \c i. |
550 | 550 |
|
551 | 551 |
///Activate item \c i. |
552 | 552 |
///\pre Item \c i shouldn't be active before. |
553 | 553 |
void activate(Item i) { |
554 |
_active |
|
554 |
_active[i] = true; |
|
555 | 555 |
|
556 | 556 |
int level = _level[i]; |
557 | 557 |
if (level > _highest_active) { |
558 | 558 |
_highest_active = level; |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
if (_prev[i] == INVALID || _active[_prev[i]]) return; |
562 | 562 |
//unlace |
563 |
_next |
|
563 |
_next[_prev[i]] = _next[i]; |
|
564 | 564 |
if (_next[i] != INVALID) { |
565 |
_prev |
|
565 |
_prev[_next[i]] = _prev[i]; |
|
566 | 566 |
} else { |
567 | 567 |
_last[level] = _prev[i]; |
568 | 568 |
} |
569 | 569 |
//lace |
570 |
_next.set(i, _first[level]); |
|
571 |
_prev.set(_first[level], i); |
|
572 |
|
|
570 |
_next[i] = _first[level]; |
|
571 |
_prev[_first[level]] = i; |
|
572 |
_prev[i] = INVALID; |
|
573 | 573 |
_first[level] = i; |
574 | 574 |
|
575 | 575 |
} |
576 | 576 |
|
577 | 577 |
///Deactivate item \c i. |
578 | 578 |
|
579 | 579 |
///Deactivate item \c i. |
580 | 580 |
///\pre Item \c i must be active before. |
581 | 581 |
void deactivate(Item i) { |
582 |
_active |
|
582 |
_active[i] = false; |
|
583 | 583 |
int level = _level[i]; |
584 | 584 |
|
585 | 585 |
if (_next[i] == INVALID || !_active[_next[i]]) |
586 | 586 |
goto find_highest_level; |
587 | 587 |
|
588 | 588 |
//unlace |
589 |
_prev |
|
589 |
_prev[_next[i]] = _prev[i]; |
|
590 | 590 |
if (_prev[i] != INVALID) { |
591 |
_next |
|
591 |
_next[_prev[i]] = _next[i]; |
|
592 | 592 |
} else { |
593 | 593 |
_first[_level[i]] = _next[i]; |
594 | 594 |
} |
595 | 595 |
//lace |
596 |
_prev.set(i, _last[level]); |
|
597 |
_next.set(_last[level], i); |
|
598 |
|
|
596 |
_prev[i] = _last[level]; |
|
597 |
_next[_last[level]] = i; |
|
598 |
_next[i] = INVALID; |
|
599 | 599 |
_last[level] = i; |
600 | 600 |
|
601 | 601 |
find_highest_level: |
602 | 602 |
if (level == _highest_active) { |
603 | 603 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
604 | 604 |
--_highest_active; |
605 | 605 |
} |
606 | 606 |
} |
607 | 607 |
|
608 | 608 |
///Query whether item \c i is active |
609 | 609 |
bool active(Item i) const { return _active[i]; } |
610 | 610 |
|
... | ... |
@@ -676,80 +676,80 @@ |
676 | 676 |
///item. |
677 | 677 |
int highestActiveLevel() const { |
678 | 678 |
return _highest_active; |
679 | 679 |
} |
680 | 680 |
|
681 | 681 |
///Lift the highest active item by one. |
682 | 682 |
|
683 | 683 |
///Lift the item returned by highestActive() by one. |
684 | 684 |
/// |
685 | 685 |
void liftHighestActive() { |
686 | 686 |
Item i = _first[_highest_active]; |
687 | 687 |
if (_next[i] != INVALID) { |
688 |
_prev |
|
688 |
_prev[_next[i]] = INVALID; |
|
689 | 689 |
_first[_highest_active] = _next[i]; |
690 | 690 |
} else { |
691 | 691 |
_first[_highest_active] = INVALID; |
692 | 692 |
_last[_highest_active] = INVALID; |
693 | 693 |
} |
694 |
_level |
|
694 |
_level[i] = ++_highest_active; |
|
695 | 695 |
if (_first[_highest_active] == INVALID) { |
696 | 696 |
_first[_highest_active] = i; |
697 | 697 |
_last[_highest_active] = i; |
698 |
_prev.set(i, INVALID); |
|
699 |
_next.set(i, INVALID); |
|
698 |
_prev[i] = INVALID; |
|
699 |
_next[i] = INVALID; |
|
700 | 700 |
} else { |
701 |
_prev.set(_first[_highest_active], i); |
|
702 |
_next.set(i, _first[_highest_active]); |
|
701 |
_prev[_first[_highest_active]] = i; |
|
702 |
_next[i] = _first[_highest_active]; |
|
703 | 703 |
_first[_highest_active] = i; |
704 | 704 |
} |
705 | 705 |
} |
706 | 706 |
|
707 | 707 |
///Lift the highest active item to the given level. |
708 | 708 |
|
709 | 709 |
///Lift the item returned by highestActive() to level \c new_level. |
710 | 710 |
/// |
711 | 711 |
///\warning \c new_level must be strictly higher |
712 | 712 |
///than the current level. |
713 | 713 |
/// |
714 | 714 |
void liftHighestActive(int new_level) { |
715 | 715 |
Item i = _first[_highest_active]; |
716 | 716 |
if (_next[i] != INVALID) { |
717 |
_prev |
|
717 |
_prev[_next[i]] = INVALID; |
|
718 | 718 |
_first[_highest_active] = _next[i]; |
719 | 719 |
} else { |
720 | 720 |
_first[_highest_active] = INVALID; |
721 | 721 |
_last[_highest_active] = INVALID; |
722 | 722 |
} |
723 |
_level |
|
723 |
_level[i] = _highest_active = new_level; |
|
724 | 724 |
if (_first[_highest_active] == INVALID) { |
725 | 725 |
_first[_highest_active] = _last[_highest_active] = i; |
726 |
_prev.set(i, INVALID); |
|
727 |
_next.set(i, INVALID); |
|
726 |
_prev[i] = INVALID; |
|
727 |
_next[i] = INVALID; |
|
728 | 728 |
} else { |
729 |
_prev.set(_first[_highest_active], i); |
|
730 |
_next.set(i, _first[_highest_active]); |
|
729 |
_prev[_first[_highest_active]] = i; |
|
730 |
_next[i] = _first[_highest_active]; |
|
731 | 731 |
_first[_highest_active] = i; |
732 | 732 |
} |
733 | 733 |
} |
734 | 734 |
|
735 | 735 |
///Lift the highest active item to the top level. |
736 | 736 |
|
737 | 737 |
///Lift the item returned by highestActive() to the top level and |
738 | 738 |
///deactivate it. |
739 | 739 |
void liftHighestActiveToTop() { |
740 | 740 |
Item i = _first[_highest_active]; |
741 |
_level |
|
741 |
_level[i] = _max_level; |
|
742 | 742 |
if (_next[i] != INVALID) { |
743 |
_prev |
|
743 |
_prev[_next[i]] = INVALID; |
|
744 | 744 |
_first[_highest_active] = _next[i]; |
745 | 745 |
} else { |
746 | 746 |
_first[_highest_active] = INVALID; |
747 | 747 |
_last[_highest_active] = INVALID; |
748 | 748 |
} |
749 | 749 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
750 | 750 |
--_highest_active; |
751 | 751 |
} |
752 | 752 |
|
753 | 753 |
///@} |
754 | 754 |
|
755 | 755 |
///\name Active Item on Certain Level |
... | ... |
@@ -765,150 +765,150 @@ |
765 | 765 |
{ |
766 | 766 |
return _active[_first[l]] ? _first[l] : INVALID; |
767 | 767 |
} |
768 | 768 |
|
769 | 769 |
///Lift the active item returned by \c activeOn(l) by one. |
770 | 770 |
|
771 | 771 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
772 | 772 |
///by one. |
773 | 773 |
Item liftActiveOn(int l) |
774 | 774 |
{ |
775 | 775 |
Item i = _first[l]; |
776 | 776 |
if (_next[i] != INVALID) { |
777 |
_prev |
|
777 |
_prev[_next[i]] = INVALID; |
|
778 | 778 |
_first[l] = _next[i]; |
779 | 779 |
} else { |
780 | 780 |
_first[l] = INVALID; |
781 | 781 |
_last[l] = INVALID; |
782 | 782 |
} |
783 |
_level |
|
783 |
_level[i] = ++l; |
|
784 | 784 |
if (_first[l] == INVALID) { |
785 | 785 |
_first[l] = _last[l] = i; |
786 |
_prev.set(i, INVALID); |
|
787 |
_next.set(i, INVALID); |
|
786 |
_prev[i] = INVALID; |
|
787 |
_next[i] = INVALID; |
|
788 | 788 |
} else { |
789 |
_prev.set(_first[l], i); |
|
790 |
_next.set(i, _first[l]); |
|
789 |
_prev[_first[l]] = i; |
|
790 |
_next[i] = _first[l]; |
|
791 | 791 |
_first[l] = i; |
792 | 792 |
} |
793 | 793 |
if (_highest_active < l) { |
794 | 794 |
_highest_active = l; |
795 | 795 |
} |
796 | 796 |
} |
797 | 797 |
|
798 | 798 |
///Lift the active item returned by \c activeOn(l) to the given level. |
799 | 799 |
|
800 | 800 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
801 | 801 |
///to the given level. |
802 | 802 |
void liftActiveOn(int l, int new_level) |
803 | 803 |
{ |
804 | 804 |
Item i = _first[l]; |
805 | 805 |
if (_next[i] != INVALID) { |
806 |
_prev |
|
806 |
_prev[_next[i]] = INVALID; |
|
807 | 807 |
_first[l] = _next[i]; |
808 | 808 |
} else { |
809 | 809 |
_first[l] = INVALID; |
810 | 810 |
_last[l] = INVALID; |
811 | 811 |
} |
812 |
_level |
|
812 |
_level[i] = l = new_level; |
|
813 | 813 |
if (_first[l] == INVALID) { |
814 | 814 |
_first[l] = _last[l] = i; |
815 |
_prev.set(i, INVALID); |
|
816 |
_next.set(i, INVALID); |
|
815 |
_prev[i] = INVALID; |
|
816 |
_next[i] = INVALID; |
|
817 | 817 |
} else { |
818 |
_prev.set(_first[l], i); |
|
819 |
_next.set(i, _first[l]); |
|
818 |
_prev[_first[l]] = i; |
|
819 |
_next[i] = _first[l]; |
|
820 | 820 |
_first[l] = i; |
821 | 821 |
} |
822 | 822 |
if (_highest_active < l) { |
823 | 823 |
_highest_active = l; |
824 | 824 |
} |
825 | 825 |
} |
826 | 826 |
|
827 | 827 |
///Lift the active item returned by \c activeOn(l) to the top level. |
828 | 828 |
|
829 | 829 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
830 | 830 |
///to the top level and deactivate it. |
831 | 831 |
void liftActiveToTop(int l) |
832 | 832 |
{ |
833 | 833 |
Item i = _first[l]; |
834 | 834 |
if (_next[i] != INVALID) { |
835 |
_prev |
|
835 |
_prev[_next[i]] = INVALID; |
|
836 | 836 |
_first[l] = _next[i]; |
837 | 837 |
} else { |
838 | 838 |
_first[l] = INVALID; |
839 | 839 |
_last[l] = INVALID; |
840 | 840 |
} |
841 |
_level |
|
841 |
_level[i] = _max_level; |
|
842 | 842 |
if (l == _highest_active) { |
843 | 843 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
844 | 844 |
--_highest_active; |
845 | 845 |
} |
846 | 846 |
} |
847 | 847 |
|
848 | 848 |
///@} |
849 | 849 |
|
850 | 850 |
/// \brief Lift an active item to a higher level. |
851 | 851 |
/// |
852 | 852 |
/// Lift an active item to a higher level. |
853 | 853 |
/// \param i The item to be lifted. It must be active. |
854 | 854 |
/// \param new_level The new level of \c i. It must be strictly higher |
855 | 855 |
/// than the current level. |
856 | 856 |
/// |
857 | 857 |
void lift(Item i, int new_level) { |
858 | 858 |
if (_next[i] != INVALID) { |
859 |
_prev |
|
859 |
_prev[_next[i]] = _prev[i]; |
|
860 | 860 |
} else { |
861 | 861 |
_last[new_level] = _prev[i]; |
862 | 862 |
} |
863 | 863 |
if (_prev[i] != INVALID) { |
864 |
_next |
|
864 |
_next[_prev[i]] = _next[i]; |
|
865 | 865 |
} else { |
866 | 866 |
_first[new_level] = _next[i]; |
867 | 867 |
} |
868 |
_level |
|
868 |
_level[i] = new_level; |
|
869 | 869 |
if (_first[new_level] == INVALID) { |
870 | 870 |
_first[new_level] = _last[new_level] = i; |
871 |
_prev.set(i, INVALID); |
|
872 |
_next.set(i, INVALID); |
|
871 |
_prev[i] = INVALID; |
|
872 |
_next[i] = INVALID; |
|
873 | 873 |
} else { |
874 |
_prev.set(_first[new_level], i); |
|
875 |
_next.set(i, _first[new_level]); |
|
874 |
_prev[_first[new_level]] = i; |
|
875 |
_next[i] = _first[new_level]; |
|
876 | 876 |
_first[new_level] = i; |
877 | 877 |
} |
878 | 878 |
if (_highest_active < new_level) { |
879 | 879 |
_highest_active = new_level; |
880 | 880 |
} |
881 | 881 |
} |
882 | 882 |
|
883 | 883 |
///Move an inactive item to the top but one level (in a dirty way). |
884 | 884 |
|
885 | 885 |
///This function moves an inactive item from the top level to the top |
886 | 886 |
///but one level (in a dirty way). |
887 | 887 |
///\warning It makes the underlying datastructure corrupt, so use it |
888 | 888 |
///only if you really know what it is for. |
889 | 889 |
///\pre The item is on the top level. |
890 | 890 |
void dirtyTopButOne(Item i) { |
891 |
_level |
|
891 |
_level[i] = _max_level - 1; |
|
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
///Lift all items on and above the given level to the top level. |
895 | 895 |
|
896 | 896 |
///This function lifts all items on and above level \c l to the top |
897 | 897 |
///level and deactivates them. |
898 | 898 |
void liftToTop(int l) { |
899 | 899 |
for (int i = l + 1; _first[i] != INVALID; ++i) { |
900 | 900 |
Item n = _first[i]; |
901 | 901 |
while (n != INVALID) { |
902 |
_level |
|
902 |
_level[n] = _max_level; |
|
903 | 903 |
n = _next[n]; |
904 | 904 |
} |
905 | 905 |
_first[i] = INVALID; |
906 | 906 |
_last[i] = INVALID; |
907 | 907 |
} |
908 | 908 |
if (_highest_active > l - 1) { |
909 | 909 |
_highest_active = l - 1; |
910 | 910 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
911 | 911 |
--_highest_active; |
912 | 912 |
} |
913 | 913 |
} |
914 | 914 |
|
... | ... |
@@ -928,41 +928,41 @@ |
928 | 928 |
///The items not listed are put on the highest level. |
929 | 929 |
///@{ |
930 | 930 |
|
931 | 931 |
///Start the initialization process. |
932 | 932 |
void initStart() { |
933 | 933 |
|
934 | 934 |
for (int i = 0; i <= _max_level; ++i) { |
935 | 935 |
_first[i] = _last[i] = INVALID; |
936 | 936 |
} |
937 | 937 |
_init_level = 0; |
938 | 938 |
for(typename ItemSetTraits<GR,Item>::ItemIt i(_graph); |
939 | 939 |
i != INVALID; ++i) { |
940 |
_level.set(i, _max_level); |
|
941 |
_active.set(i, false); |
|
940 |
_level[i] = _max_level; |
|
941 |
_active[i] = false; |
|
942 | 942 |
} |
943 | 943 |
} |
944 | 944 |
|
945 | 945 |
///Add an item to the current level. |
946 | 946 |
void initAddItem(Item i) { |
947 |
_level |
|
947 |
_level[i] = _init_level; |
|
948 | 948 |
if (_last[_init_level] == INVALID) { |
949 | 949 |
_first[_init_level] = i; |
950 | 950 |
_last[_init_level] = i; |
951 |
_prev.set(i, INVALID); |
|
952 |
_next.set(i, INVALID); |
|
951 |
_prev[i] = INVALID; |
|
952 |
_next[i] = INVALID; |
|
953 | 953 |
} else { |
954 |
_prev.set(i, _last[_init_level]); |
|
955 |
_next.set(i, INVALID); |
|
956 |
|
|
954 |
_prev[i] = _last[_init_level]; |
|
955 |
_next[i] = INVALID; |
|
956 |
_next[_last[_init_level]] = i; |
|
957 | 957 |
_last[_init_level] = i; |
958 | 958 |
} |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
///Start a new level. |
962 | 962 |
|
963 | 963 |
///Start a new level. |
964 | 964 |
///It shouldn't be used before the items on level 0 are listed. |
965 | 965 |
void initNewLevel() { |
966 | 966 |
++_init_level; |
967 | 967 |
} |
968 | 968 |
... | ... |
@@ -134,72 +134,72 @@ |
134 | 134 |
~GomoryHu() { |
135 | 135 |
destroyStructures(); |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
private: |
139 | 139 |
|
140 | 140 |
// Initialize the internal data structures |
141 | 141 |
void init() { |
142 | 142 |
createStructures(); |
143 | 143 |
|
144 | 144 |
_root = NodeIt(_graph); |
145 | 145 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
146 |
_pred->set(n, _root); |
|
147 |
_order->set(n, -1); |
|
146 |
(*_pred)[n] = _root; |
|
147 |
(*_order)[n] = -1; |
|
148 | 148 |
} |
149 |
_pred->set(_root, INVALID); |
|
150 |
_weight->set(_root, std::numeric_limits<Value>::max()); |
|
149 |
(*_pred)[_root] = INVALID; |
|
150 |
(*_weight)[_root] = std::numeric_limits<Value>::max(); |
|
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
|
154 | 154 |
// Start the algorithm |
155 | 155 |
void start() { |
156 | 156 |
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID); |
157 | 157 |
|
158 | 158 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
159 | 159 |
if (n == _root) continue; |
160 | 160 |
|
161 | 161 |
Node pn = (*_pred)[n]; |
162 | 162 |
fa.source(n); |
163 | 163 |
fa.target(pn); |
164 | 164 |
|
165 | 165 |
fa.runMinCut(); |
166 | 166 |
|
167 |
_weight |
|
167 |
(*_weight)[n] = fa.flowValue(); |
|
168 | 168 |
|
169 | 169 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) { |
170 | 170 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) { |
171 |
_pred |
|
171 |
(*_pred)[nn] = n; |
|
172 | 172 |
} |
173 | 173 |
} |
174 | 174 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) { |
175 |
_pred->set(n, (*_pred)[pn]); |
|
176 |
_pred->set(pn, n); |
|
177 |
_weight->set(n, (*_weight)[pn]); |
|
178 |
_weight->set(pn, fa.flowValue()); |
|
175 |
(*_pred)[n] = (*_pred)[pn]; |
|
176 |
(*_pred)[pn] = n; |
|
177 |
(*_weight)[n] = (*_weight)[pn]; |
|
178 |
(*_weight)[pn] = fa.flowValue(); |
|
179 | 179 |
} |
180 | 180 |
} |
181 | 181 |
|
182 |
_order |
|
182 |
(*_order)[_root] = 0; |
|
183 | 183 |
int index = 1; |
184 | 184 |
|
185 | 185 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
186 | 186 |
std::vector<Node> st; |
187 | 187 |
Node nn = n; |
188 | 188 |
while ((*_order)[nn] == -1) { |
189 | 189 |
st.push_back(nn); |
190 | 190 |
nn = (*_pred)[nn]; |
191 | 191 |
} |
192 | 192 |
while (!st.empty()) { |
193 |
_order |
|
193 |
(*_order)[st.back()] = index++; |
|
194 | 194 |
st.pop_back(); |
195 | 195 |
} |
196 | 196 |
} |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
public: |
200 | 200 |
|
201 | 201 |
///\name Execution Control |
202 | 202 |
|
203 | 203 |
///@{ |
204 | 204 |
|
205 | 205 |
/// \brief Run the Gomory-Hu algorithm. |
... | ... |
@@ -300,27 +300,27 @@ |
300 | 300 |
tn = (*_pred)[tn]; |
301 | 301 |
} else { |
302 | 302 |
if ((*_weight)[sn] <= value) { |
303 | 303 |
rn = sn; |
304 | 304 |
s_root = true; |
305 | 305 |
value = (*_weight)[sn]; |
306 | 306 |
} |
307 | 307 |
sn = (*_pred)[sn]; |
308 | 308 |
} |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
typename Graph::template NodeMap<bool> reached(_graph, false); |
312 |
reached |
|
312 |
reached[_root] = true; |
|
313 | 313 |
cutMap.set(_root, !s_root); |
314 |
reached |
|
314 |
reached[rn] = true; |
|
315 | 315 |
cutMap.set(rn, s_root); |
316 | 316 |
|
317 | 317 |
std::vector<Node> st; |
318 | 318 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
319 | 319 |
st.clear(); |
320 | 320 |
Node nn = n; |
321 | 321 |
while (!reached[nn]) { |
322 | 322 |
st.push_back(nn); |
323 | 323 |
nn = (*_pred)[nn]; |
324 | 324 |
} |
325 | 325 |
while (!st.empty()) { |
326 | 326 |
cutMap.set(st.back(), cutMap[nn]); |
... | ... |
@@ -152,141 +152,141 @@ |
152 | 152 |
} |
153 | 153 |
if (_bucket) { |
154 | 154 |
delete _bucket; |
155 | 155 |
} |
156 | 156 |
if (_flow) { |
157 | 157 |
delete _flow; |
158 | 158 |
} |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
private: |
162 | 162 |
|
163 | 163 |
void activate(const Node& i) { |
164 |
_active |
|
164 |
(*_active)[i] = true; |
|
165 | 165 |
|
166 | 166 |
int bucket = (*_bucket)[i]; |
167 | 167 |
|
168 | 168 |
if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return; |
169 | 169 |
//unlace |
170 |
_next |
|
170 |
(*_next)[(*_prev)[i]] = (*_next)[i]; |
|
171 | 171 |
if ((*_next)[i] != INVALID) { |
172 |
_prev |
|
172 |
(*_prev)[(*_next)[i]] = (*_prev)[i]; |
|
173 | 173 |
} else { |
174 | 174 |
_last[bucket] = (*_prev)[i]; |
175 | 175 |
} |
176 | 176 |
//lace |
177 |
_next->set(i, _first[bucket]); |
|
178 |
_prev->set(_first[bucket], i); |
|
179 |
|
|
177 |
(*_next)[i] = _first[bucket]; |
|
178 |
(*_prev)[_first[bucket]] = i; |
|
179 |
(*_prev)[i] = INVALID; |
|
180 | 180 |
_first[bucket] = i; |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
void deactivate(const Node& i) { |
184 |
_active |
|
184 |
(*_active)[i] = false; |
|
185 | 185 |
int bucket = (*_bucket)[i]; |
186 | 186 |
|
187 | 187 |
if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return; |
188 | 188 |
|
189 | 189 |
//unlace |
190 |
_prev |
|
190 |
(*_prev)[(*_next)[i]] = (*_prev)[i]; |
|
191 | 191 |
if ((*_prev)[i] != INVALID) { |
192 |
_next |
|
192 |
(*_next)[(*_prev)[i]] = (*_next)[i]; |
|
193 | 193 |
} else { |
194 | 194 |
_first[bucket] = (*_next)[i]; |
195 | 195 |
} |
196 | 196 |
//lace |
197 |
_prev->set(i, _last[bucket]); |
|
198 |
_next->set(_last[bucket], i); |
|
199 |
|
|
197 |
(*_prev)[i] = _last[bucket]; |
|
198 |
(*_next)[_last[bucket]] = i; |
|
199 |
(*_next)[i] = INVALID; |
|
200 | 200 |
_last[bucket] = i; |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
void addItem(const Node& i, int bucket) { |
204 | 204 |
(*_bucket)[i] = bucket; |
205 | 205 |
if (_last[bucket] != INVALID) { |
206 |
_prev->set(i, _last[bucket]); |
|
207 |
_next->set(_last[bucket], i); |
|
208 |
|
|
206 |
(*_prev)[i] = _last[bucket]; |
|
207 |
(*_next)[_last[bucket]] = i; |
|
208 |
(*_next)[i] = INVALID; |
|
209 | 209 |
_last[bucket] = i; |
210 | 210 |
} else { |
211 |
_prev |
|
211 |
(*_prev)[i] = INVALID; |
|
212 | 212 |
_first[bucket] = i; |
213 |
_next |
|
213 |
(*_next)[i] = INVALID; |
|
214 | 214 |
_last[bucket] = i; |
215 | 215 |
} |
216 | 216 |
} |
217 | 217 |
|
218 | 218 |
void findMinCutOut() { |
219 | 219 |
|
220 | 220 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
221 |
_excess |
|
221 |
(*_excess)[n] = 0; |
|
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
225 |
_flow |
|
225 |
(*_flow)[a] = 0; |
|
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
int bucket_num = 0; |
229 | 229 |
std::vector<Node> queue(_node_num); |
230 | 230 |
int qfirst = 0, qlast = 0, qsep = 0; |
231 | 231 |
|
232 | 232 |
{ |
233 | 233 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
234 | 234 |
|
235 |
reached |
|
235 |
reached[_source] = true; |
|
236 | 236 |
bool first_set = true; |
237 | 237 |
|
238 | 238 |
for (NodeIt t(_graph); t != INVALID; ++t) { |
239 | 239 |
if (reached[t]) continue; |
240 | 240 |
_sets.push_front(std::list<int>()); |
241 | 241 |
|
242 | 242 |
queue[qlast++] = t; |
243 |
reached |
|
243 |
reached[t] = true; |
|
244 | 244 |
|
245 | 245 |
while (qfirst != qlast) { |
246 | 246 |
if (qsep == qfirst) { |
247 | 247 |
++bucket_num; |
248 | 248 |
_sets.front().push_front(bucket_num); |
249 | 249 |
_dormant[bucket_num] = !first_set; |
250 | 250 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
251 | 251 |
qsep = qlast; |
252 | 252 |
} |
253 | 253 |
|
254 | 254 |
Node n = queue[qfirst++]; |
255 | 255 |
addItem(n, bucket_num); |
256 | 256 |
|
257 | 257 |
for (InArcIt a(_graph, n); a != INVALID; ++a) { |
258 | 258 |
Node u = _graph.source(a); |
259 | 259 |
if (!reached[u] && _tolerance.positive((*_capacity)[a])) { |
260 |
reached |
|
260 |
reached[u] = true; |
|
261 | 261 |
queue[qlast++] = u; |
262 | 262 |
} |
263 | 263 |
} |
264 | 264 |
} |
265 | 265 |
first_set = false; |
266 | 266 |
} |
267 | 267 |
|
268 | 268 |
++bucket_num; |
269 |
_bucket |
|
269 |
(*_bucket)[_source] = 0; |
|
270 | 270 |
_dormant[0] = true; |
271 | 271 |
} |
272 |
_source_set |
|
272 |
(*_source_set)[_source] = true; |
|
273 | 273 |
|
274 | 274 |
Node target = _last[_sets.back().back()]; |
275 | 275 |
{ |
276 | 276 |
for (OutArcIt a(_graph, _source); a != INVALID; ++a) { |
277 | 277 |
if (_tolerance.positive((*_capacity)[a])) { |
278 | 278 |
Node u = _graph.target(a); |
279 |
_flow->set(a, (*_capacity)[a]); |
|
280 |
_excess->set(u, (*_excess)[u] + (*_capacity)[a]); |
|
279 |
(*_flow)[a] = (*_capacity)[a]; |
|
280 |
(*_excess)[u] += (*_capacity)[a]; |
|
281 | 281 |
if (!(*_active)[u] && u != _source) { |
282 | 282 |
activate(u); |
283 | 283 |
} |
284 | 284 |
} |
285 | 285 |
} |
286 | 286 |
|
287 | 287 |
if ((*_active)[target]) { |
288 | 288 |
deactivate(target); |
289 | 289 |
} |
290 | 290 |
|
291 | 291 |
_highest = _sets.back().begin(); |
292 | 292 |
while (_highest != _sets.back().end() && |
... | ... |
@@ -309,284 +309,284 @@ |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) { |
312 | 312 |
Node v = _graph.target(a); |
313 | 313 |
if (_dormant[(*_bucket)[v]]) continue; |
314 | 314 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
315 | 315 |
if (!_tolerance.positive(rem)) continue; |
316 | 316 |
if ((*_bucket)[v] == under_bucket) { |
317 | 317 |
if (!(*_active)[v] && v != target) { |
318 | 318 |
activate(v); |
319 | 319 |
} |
320 | 320 |
if (!_tolerance.less(rem, excess)) { |
321 |
_flow->set(a, (*_flow)[a] + excess); |
|
322 |
_excess->set(v, (*_excess)[v] + excess); |
|
321 |
(*_flow)[a] += excess; |
|
322 |
(*_excess)[v] += excess; |
|
323 | 323 |
excess = 0; |
324 | 324 |
goto no_more_push; |
325 | 325 |
} else { |
326 | 326 |
excess -= rem; |
327 |
_excess->set(v, (*_excess)[v] + rem); |
|
328 |
_flow->set(a, (*_capacity)[a]); |
|
327 |
(*_excess)[v] += rem; |
|
328 |
(*_flow)[a] = (*_capacity)[a]; |
|
329 | 329 |
} |
330 | 330 |
} else if (next_bucket > (*_bucket)[v]) { |
331 | 331 |
next_bucket = (*_bucket)[v]; |
332 | 332 |
} |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
for (InArcIt a(_graph, n); a != INVALID; ++a) { |
336 | 336 |
Node v = _graph.source(a); |
337 | 337 |
if (_dormant[(*_bucket)[v]]) continue; |
338 | 338 |
Value rem = (*_flow)[a]; |
339 | 339 |
if (!_tolerance.positive(rem)) continue; |
340 | 340 |
if ((*_bucket)[v] == under_bucket) { |
341 | 341 |
if (!(*_active)[v] && v != target) { |
342 | 342 |
activate(v); |
343 | 343 |
} |
344 | 344 |
if (!_tolerance.less(rem, excess)) { |
345 |
_flow->set(a, (*_flow)[a] - excess); |
|
346 |
_excess->set(v, (*_excess)[v] + excess); |
|
345 |
(*_flow)[a] -= excess; |
|
346 |
(*_excess)[v] += excess; |
|
347 | 347 |
excess = 0; |
348 | 348 |
goto no_more_push; |
349 | 349 |
} else { |
350 | 350 |
excess -= rem; |
351 |
_excess->set(v, (*_excess)[v] + rem); |
|
352 |
_flow->set(a, 0); |
|
351 |
(*_excess)[v] += rem; |
|
352 |
(*_flow)[a] = 0; |
|
353 | 353 |
} |
354 | 354 |
} else if (next_bucket > (*_bucket)[v]) { |
355 | 355 |
next_bucket = (*_bucket)[v]; |
356 | 356 |
} |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
no_more_push: |
360 | 360 |
|
361 |
_excess |
|
361 |
(*_excess)[n] = excess; |
|
362 | 362 |
|
363 | 363 |
if (excess != 0) { |
364 | 364 |
if ((*_next)[n] == INVALID) { |
365 | 365 |
typename std::list<std::list<int> >::iterator new_set = |
366 | 366 |
_sets.insert(--_sets.end(), std::list<int>()); |
367 | 367 |
new_set->splice(new_set->end(), _sets.back(), |
368 | 368 |
_sets.back().begin(), ++_highest); |
369 | 369 |
for (std::list<int>::iterator it = new_set->begin(); |
370 | 370 |
it != new_set->end(); ++it) { |
371 | 371 |
_dormant[*it] = true; |
372 | 372 |
} |
373 | 373 |
while (_highest != _sets.back().end() && |
374 | 374 |
!(*_active)[_first[*_highest]]) { |
375 | 375 |
++_highest; |
376 | 376 |
} |
377 | 377 |
} else if (next_bucket == _node_num) { |
378 | 378 |
_first[(*_bucket)[n]] = (*_next)[n]; |
379 |
_prev |
|
379 |
(*_prev)[(*_next)[n]] = INVALID; |
|
380 | 380 |
|
381 | 381 |
std::list<std::list<int> >::iterator new_set = |
382 | 382 |
_sets.insert(--_sets.end(), std::list<int>()); |
383 | 383 |
|
384 | 384 |
new_set->push_front(bucket_num); |
385 |
_bucket |
|
385 |
(*_bucket)[n] = bucket_num; |
|
386 | 386 |
_first[bucket_num] = _last[bucket_num] = n; |
387 |
_next->set(n, INVALID); |
|
388 |
_prev->set(n, INVALID); |
|
387 |
(*_next)[n] = INVALID; |
|
388 |
(*_prev)[n] = INVALID; |
|
389 | 389 |
_dormant[bucket_num] = true; |
390 | 390 |
++bucket_num; |
391 | 391 |
|
392 | 392 |
while (_highest != _sets.back().end() && |
393 | 393 |
!(*_active)[_first[*_highest]]) { |
394 | 394 |
++_highest; |
395 | 395 |
} |
396 | 396 |
} else { |
397 | 397 |
_first[*_highest] = (*_next)[n]; |
398 |
_prev |
|
398 |
(*_prev)[(*_next)[n]] = INVALID; |
|
399 | 399 |
|
400 | 400 |
while (next_bucket != *_highest) { |
401 | 401 |
--_highest; |
402 | 402 |
} |
403 | 403 |
|
404 | 404 |
if (_highest == _sets.back().begin()) { |
405 | 405 |
_sets.back().push_front(bucket_num); |
406 | 406 |
_dormant[bucket_num] = false; |
407 | 407 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
408 | 408 |
++bucket_num; |
409 | 409 |
} |
410 | 410 |
--_highest; |
411 | 411 |
|
412 |
_bucket->set(n, *_highest); |
|
413 |
_next->set(n, _first[*_highest]); |
|
412 |
(*_bucket)[n] = *_highest; |
|
413 |
(*_next)[n] = _first[*_highest]; |
|
414 | 414 |
if (_first[*_highest] != INVALID) { |
415 |
_prev |
|
415 |
(*_prev)[_first[*_highest]] = n; |
|
416 | 416 |
} else { |
417 | 417 |
_last[*_highest] = n; |
418 | 418 |
} |
419 | 419 |
_first[*_highest] = n; |
420 | 420 |
} |
421 | 421 |
} else { |
422 | 422 |
|
423 | 423 |
deactivate(n); |
424 | 424 |
if (!(*_active)[_first[*_highest]]) { |
425 | 425 |
++_highest; |
426 | 426 |
if (_highest != _sets.back().end() && |
427 | 427 |
!(*_active)[_first[*_highest]]) { |
428 | 428 |
_highest = _sets.back().end(); |
429 | 429 |
} |
430 | 430 |
} |
431 | 431 |
} |
432 | 432 |
} |
433 | 433 |
|
434 | 434 |
if ((*_excess)[target] < _min_cut) { |
435 | 435 |
_min_cut = (*_excess)[target]; |
436 | 436 |
for (NodeIt i(_graph); i != INVALID; ++i) { |
437 |
_min_cut_map |
|
437 |
(*_min_cut_map)[i] = true; |
|
438 | 438 |
} |
439 | 439 |
for (std::list<int>::iterator it = _sets.back().begin(); |
440 | 440 |
it != _sets.back().end(); ++it) { |
441 | 441 |
Node n = _first[*it]; |
442 | 442 |
while (n != INVALID) { |
443 |
_min_cut_map |
|
443 |
(*_min_cut_map)[n] = false; |
|
444 | 444 |
n = (*_next)[n]; |
445 | 445 |
} |
446 | 446 |
} |
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
{ |
450 | 450 |
Node new_target; |
451 | 451 |
if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) { |
452 | 452 |
if ((*_next)[target] == INVALID) { |
453 | 453 |
_last[(*_bucket)[target]] = (*_prev)[target]; |
454 | 454 |
new_target = (*_prev)[target]; |
455 | 455 |
} else { |
456 |
_prev |
|
456 |
(*_prev)[(*_next)[target]] = (*_prev)[target]; |
|
457 | 457 |
new_target = (*_next)[target]; |
458 | 458 |
} |
459 | 459 |
if ((*_prev)[target] == INVALID) { |
460 | 460 |
_first[(*_bucket)[target]] = (*_next)[target]; |
461 | 461 |
} else { |
462 |
_next |
|
462 |
(*_next)[(*_prev)[target]] = (*_next)[target]; |
|
463 | 463 |
} |
464 | 464 |
} else { |
465 | 465 |
_sets.back().pop_back(); |
466 | 466 |
if (_sets.back().empty()) { |
467 | 467 |
_sets.pop_back(); |
468 | 468 |
if (_sets.empty()) |
469 | 469 |
break; |
470 | 470 |
for (std::list<int>::iterator it = _sets.back().begin(); |
471 | 471 |
it != _sets.back().end(); ++it) { |
472 | 472 |
_dormant[*it] = false; |
473 | 473 |
} |
474 | 474 |
} |
475 | 475 |
new_target = _last[_sets.back().back()]; |
476 | 476 |
} |
477 | 477 |
|
478 |
_bucket |
|
478 |
(*_bucket)[target] = 0; |
|
479 | 479 |
|
480 |
_source_set |
|
480 |
(*_source_set)[target] = true; |
|
481 | 481 |
for (OutArcIt a(_graph, target); a != INVALID; ++a) { |
482 | 482 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
483 | 483 |
if (!_tolerance.positive(rem)) continue; |
484 | 484 |
Node v = _graph.target(a); |
485 | 485 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
486 | 486 |
activate(v); |
487 | 487 |
} |
488 |
_excess->set(v, (*_excess)[v] + rem); |
|
489 |
_flow->set(a, (*_capacity)[a]); |
|
488 |
(*_excess)[v] += rem; |
|
489 |
(*_flow)[a] = (*_capacity)[a]; |
|
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
for (InArcIt a(_graph, target); a != INVALID; ++a) { |
493 | 493 |
Value rem = (*_flow)[a]; |
494 | 494 |
if (!_tolerance.positive(rem)) continue; |
495 | 495 |
Node v = _graph.source(a); |
496 | 496 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
497 | 497 |
activate(v); |
498 | 498 |
} |
499 |
_excess->set(v, (*_excess)[v] + rem); |
|
500 |
_flow->set(a, 0); |
|
499 |
(*_excess)[v] += rem; |
|
500 |
(*_flow)[a] = 0; |
|
501 | 501 |
} |
502 | 502 |
|
503 | 503 |
target = new_target; |
504 | 504 |
if ((*_active)[target]) { |
505 | 505 |
deactivate(target); |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
_highest = _sets.back().begin(); |
509 | 509 |
while (_highest != _sets.back().end() && |
510 | 510 |
!(*_active)[_first[*_highest]]) { |
511 | 511 |
++_highest; |
512 | 512 |
} |
513 | 513 |
} |
514 | 514 |
} |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
void findMinCutIn() { |
518 | 518 |
|
519 | 519 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
520 |
_excess |
|
520 |
(*_excess)[n] = 0; |
|
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
524 |
_flow |
|
524 |
(*_flow)[a] = 0; |
|
525 | 525 |
} |
526 | 526 |
|
527 | 527 |
int bucket_num = 0; |
528 | 528 |
std::vector<Node> queue(_node_num); |
529 | 529 |
int qfirst = 0, qlast = 0, qsep = 0; |
530 | 530 |
|
531 | 531 |
{ |
532 | 532 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
533 | 533 |
|
534 |
reached |
|
534 |
reached[_source] = true; |
|
535 | 535 |
|
536 | 536 |
bool first_set = true; |
537 | 537 |
|
538 | 538 |
for (NodeIt t(_graph); t != INVALID; ++t) { |
539 | 539 |
if (reached[t]) continue; |
540 | 540 |
_sets.push_front(std::list<int>()); |
541 | 541 |
|
542 | 542 |
queue[qlast++] = t; |
543 |
reached |
|
543 |
reached[t] = true; |
|
544 | 544 |
|
545 | 545 |
while (qfirst != qlast) { |
546 | 546 |
if (qsep == qfirst) { |
547 | 547 |
++bucket_num; |
548 | 548 |
_sets.front().push_front(bucket_num); |
549 | 549 |
_dormant[bucket_num] = !first_set; |
550 | 550 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
551 | 551 |
qsep = qlast; |
552 | 552 |
} |
553 | 553 |
|
554 | 554 |
Node n = queue[qfirst++]; |
555 | 555 |
addItem(n, bucket_num); |
556 | 556 |
|
557 | 557 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) { |
558 | 558 |
Node u = _graph.target(a); |
559 | 559 |
if (!reached[u] && _tolerance.positive((*_capacity)[a])) { |
560 |
reached |
|
560 |
reached[u] = true; |
|
561 | 561 |
queue[qlast++] = u; |
562 | 562 |
} |
563 | 563 |
} |
564 | 564 |
} |
565 | 565 |
first_set = false; |
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
++bucket_num; |
569 |
_bucket |
|
569 |
(*_bucket)[_source] = 0; |
|
570 | 570 |
_dormant[0] = true; |
571 | 571 |
} |
572 |
_source_set |
|
572 |
(*_source_set)[_source] = true; |
|
573 | 573 |
|
574 | 574 |
Node target = _last[_sets.back().back()]; |
575 | 575 |
{ |
576 | 576 |
for (InArcIt a(_graph, _source); a != INVALID; ++a) { |
577 | 577 |
if (_tolerance.positive((*_capacity)[a])) { |
578 | 578 |
Node u = _graph.source(a); |
579 |
_flow->set(a, (*_capacity)[a]); |
|
580 |
_excess->set(u, (*_excess)[u] + (*_capacity)[a]); |
|
579 |
(*_flow)[a] = (*_capacity)[a]; |
|
580 |
(*_excess)[u] += (*_capacity)[a]; |
|
581 | 581 |
if (!(*_active)[u] && u != _source) { |
582 | 582 |
activate(u); |
583 | 583 |
} |
584 | 584 |
} |
585 | 585 |
} |
586 | 586 |
if ((*_active)[target]) { |
587 | 587 |
deactivate(target); |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
_highest = _sets.back().begin(); |
591 | 591 |
while (_highest != _sets.back().end() && |
592 | 592 |
!(*_active)[_first[*_highest]]) { |
... | ... |
@@ -609,203 +609,203 @@ |
609 | 609 |
} |
610 | 610 |
|
611 | 611 |
for (InArcIt a(_graph, n); a != INVALID; ++a) { |
612 | 612 |
Node v = _graph.source(a); |
613 | 613 |
if (_dormant[(*_bucket)[v]]) continue; |
614 | 614 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
615 | 615 |
if (!_tolerance.positive(rem)) continue; |
616 | 616 |
if ((*_bucket)[v] == under_bucket) { |
617 | 617 |
if (!(*_active)[v] && v != target) { |
618 | 618 |
activate(v); |
619 | 619 |
} |
620 | 620 |
if (!_tolerance.less(rem, excess)) { |
621 |
_flow->set(a, (*_flow)[a] + excess); |
|
622 |
_excess->set(v, (*_excess)[v] + excess); |
|
621 |
(*_flow)[a] += excess; |
|
622 |
(*_excess)[v] += excess; |
|
623 | 623 |
excess = 0; |
624 | 624 |
goto no_more_push; |
625 | 625 |
} else { |
626 | 626 |
excess -= rem; |
627 |
_excess->set(v, (*_excess)[v] + rem); |
|
628 |
_flow->set(a, (*_capacity)[a]); |
|
627 |
(*_excess)[v] += rem; |
|
628 |
(*_flow)[a] = (*_capacity)[a]; |
|
629 | 629 |
} |
630 | 630 |
} else if (next_bucket > (*_bucket)[v]) { |
631 | 631 |
next_bucket = (*_bucket)[v]; |
632 | 632 |
} |
633 | 633 |
} |
634 | 634 |
|
635 | 635 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) { |
636 | 636 |
Node v = _graph.target(a); |
637 | 637 |
if (_dormant[(*_bucket)[v]]) continue; |
638 | 638 |
Value rem = (*_flow)[a]; |
639 | 639 |
if (!_tolerance.positive(rem)) continue; |
640 | 640 |
if ((*_bucket)[v] == under_bucket) { |
641 | 641 |
if (!(*_active)[v] && v != target) { |
642 | 642 |
activate(v); |
643 | 643 |
} |
644 | 644 |
if (!_tolerance.less(rem, excess)) { |
645 |
_flow->set(a, (*_flow)[a] - excess); |
|
646 |
_excess->set(v, (*_excess)[v] + excess); |
|
645 |
(*_flow)[a] -= excess; |
|
646 |
(*_excess)[v] += excess; |
|
647 | 647 |
excess = 0; |
648 | 648 |
goto no_more_push; |
649 | 649 |
} else { |
650 | 650 |
excess -= rem; |
651 |
_excess->set(v, (*_excess)[v] + rem); |
|
652 |
_flow->set(a, 0); |
|
651 |
(*_excess)[v] += rem; |
|
652 |
(*_flow)[a] = 0; |
|
653 | 653 |
} |
654 | 654 |
} else if (next_bucket > (*_bucket)[v]) { |
655 | 655 |
next_bucket = (*_bucket)[v]; |
656 | 656 |
} |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
no_more_push: |
660 | 660 |
|
661 |
_excess |
|
661 |
(*_excess)[n] = excess; |
|
662 | 662 |
|
663 | 663 |
if (excess != 0) { |
664 | 664 |
if ((*_next)[n] == INVALID) { |
665 | 665 |
typename std::list<std::list<int> >::iterator new_set = |
666 | 666 |
_sets.insert(--_sets.end(), std::list<int>()); |
667 | 667 |
new_set->splice(new_set->end(), _sets.back(), |
668 | 668 |
_sets.back().begin(), ++_highest); |
669 | 669 |
for (std::list<int>::iterator it = new_set->begin(); |
670 | 670 |
it != new_set->end(); ++it) { |
671 | 671 |
_dormant[*it] = true; |
672 | 672 |
} |
673 | 673 |
while (_highest != _sets.back().end() && |
674 | 674 |
!(*_active)[_first[*_highest]]) { |
675 | 675 |
++_highest; |
676 | 676 |
} |
677 | 677 |
} else if (next_bucket == _node_num) { |
678 | 678 |
_first[(*_bucket)[n]] = (*_next)[n]; |
679 |
_prev |
|
679 |
(*_prev)[(*_next)[n]] = INVALID; |
|
680 | 680 |
|
681 | 681 |
std::list<std::list<int> >::iterator new_set = |
682 | 682 |
_sets.insert(--_sets.end(), std::list<int>()); |
683 | 683 |
|
684 | 684 |
new_set->push_front(bucket_num); |
685 |
_bucket |
|
685 |
(*_bucket)[n] = bucket_num; |
|
686 | 686 |
_first[bucket_num] = _last[bucket_num] = n; |
687 |
_next->set(n, INVALID); |
|
688 |
_prev->set(n, INVALID); |
|
687 |
(*_next)[n] = INVALID; |
|
688 |
(*_prev)[n] = INVALID; |
|
689 | 689 |
_dormant[bucket_num] = true; |
690 | 690 |
++bucket_num; |
691 | 691 |
|
692 | 692 |
while (_highest != _sets.back().end() && |
693 | 693 |
!(*_active)[_first[*_highest]]) { |
694 | 694 |
++_highest; |
695 | 695 |
} |
696 | 696 |
} else { |
697 | 697 |
_first[*_highest] = (*_next)[n]; |
698 |
_prev |
|
698 |
(*_prev)[(*_next)[n]] = INVALID; |
|
699 | 699 |
|
700 | 700 |
while (next_bucket != *_highest) { |
701 | 701 |
--_highest; |
702 | 702 |
} |
703 | 703 |
if (_highest == _sets.back().begin()) { |
704 | 704 |
_sets.back().push_front(bucket_num); |
705 | 705 |
_dormant[bucket_num] = false; |
706 | 706 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
707 | 707 |
++bucket_num; |
708 | 708 |
} |
709 | 709 |
--_highest; |
710 | 710 |
|
711 |
_bucket->set(n, *_highest); |
|
712 |
_next->set(n, _first[*_highest]); |
|
711 |
(*_bucket)[n] = *_highest; |
|
712 |
(*_next)[n] = _first[*_highest]; |
|
713 | 713 |
if (_first[*_highest] != INVALID) { |
714 |
_prev |
|
714 |
(*_prev)[_first[*_highest]] = n; |
|
715 | 715 |
} else { |
716 | 716 |
_last[*_highest] = n; |
717 | 717 |
} |
718 | 718 |
_first[*_highest] = n; |
719 | 719 |
} |
720 | 720 |
} else { |
721 | 721 |
|
722 | 722 |
deactivate(n); |
723 | 723 |
if (!(*_active)[_first[*_highest]]) { |
724 | 724 |
++_highest; |
725 | 725 |
if (_highest != _sets.back().end() && |
726 | 726 |
!(*_active)[_first[*_highest]]) { |
727 | 727 |
_highest = _sets.back().end(); |
728 | 728 |
} |
729 | 729 |
} |
730 | 730 |
} |
731 | 731 |
} |
732 | 732 |
|
733 | 733 |
if ((*_excess)[target] < _min_cut) { |
734 | 734 |
_min_cut = (*_excess)[target]; |
735 | 735 |
for (NodeIt i(_graph); i != INVALID; ++i) { |
736 |
_min_cut_map |
|
736 |
(*_min_cut_map)[i] = false; |
|
737 | 737 |
} |
738 | 738 |
for (std::list<int>::iterator it = _sets.back().begin(); |
739 | 739 |
it != _sets.back().end(); ++it) { |
740 | 740 |
Node n = _first[*it]; |
741 | 741 |
while (n != INVALID) { |
742 |
_min_cut_map |
|
742 |
(*_min_cut_map)[n] = true; |
|
743 | 743 |
n = (*_next)[n]; |
744 | 744 |
} |
745 | 745 |
} |
746 | 746 |
} |
747 | 747 |
|
748 | 748 |
{ |
749 | 749 |
Node new_target; |
750 | 750 |
if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) { |
751 | 751 |
if ((*_next)[target] == INVALID) { |
752 | 752 |
_last[(*_bucket)[target]] = (*_prev)[target]; |
753 | 753 |
new_target = (*_prev)[target]; |
754 | 754 |
} else { |
755 |
_prev |
|
755 |
(*_prev)[(*_next)[target]] = (*_prev)[target]; |
|
756 | 756 |
new_target = (*_next)[target]; |
757 | 757 |
} |
758 | 758 |
if ((*_prev)[target] == INVALID) { |
759 | 759 |
_first[(*_bucket)[target]] = (*_next)[target]; |
760 | 760 |
} else { |
761 |
_next |
|
761 |
(*_next)[(*_prev)[target]] = (*_next)[target]; |
|
762 | 762 |
} |
763 | 763 |
} else { |
764 | 764 |
_sets.back().pop_back(); |
765 | 765 |
if (_sets.back().empty()) { |
766 | 766 |
_sets.pop_back(); |
767 | 767 |
if (_sets.empty()) |
768 | 768 |
break; |
769 | 769 |
for (std::list<int>::iterator it = _sets.back().begin(); |
770 | 770 |
it != _sets.back().end(); ++it) { |
771 | 771 |
_dormant[*it] = false; |
772 | 772 |
} |
773 | 773 |
} |
774 | 774 |
new_target = _last[_sets.back().back()]; |
775 | 775 |
} |
776 | 776 |
|
777 |
_bucket |
|
777 |
(*_bucket)[target] = 0; |
|
778 | 778 |
|
779 |
_source_set |
|
779 |
(*_source_set)[target] = true; |
|
780 | 780 |
for (InArcIt a(_graph, target); a != INVALID; ++a) { |
781 | 781 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
782 | 782 |
if (!_tolerance.positive(rem)) continue; |
783 | 783 |
Node v = _graph.source(a); |
784 | 784 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
785 | 785 |
activate(v); |
786 | 786 |
} |
787 |
_excess->set(v, (*_excess)[v] + rem); |
|
788 |
_flow->set(a, (*_capacity)[a]); |
|
787 |
(*_excess)[v] += rem; |
|
788 |
(*_flow)[a] = (*_capacity)[a]; |
|
789 | 789 |
} |
790 | 790 |
|
791 | 791 |
for (OutArcIt a(_graph, target); a != INVALID; ++a) { |
792 | 792 |
Value rem = (*_flow)[a]; |
793 | 793 |
if (!_tolerance.positive(rem)) continue; |
794 | 794 |
Node v = _graph.target(a); |
795 | 795 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
796 | 796 |
activate(v); |
797 | 797 |
} |
798 |
_excess->set(v, (*_excess)[v] + rem); |
|
799 |
_flow->set(a, 0); |
|
798 |
(*_excess)[v] += rem; |
|
799 |
(*_flow)[a] = 0; |
|
800 | 800 |
} |
801 | 801 |
|
802 | 802 |
target = new_target; |
803 | 803 |
if ((*_active)[target]) { |
804 | 804 |
deactivate(target); |
805 | 805 |
} |
806 | 806 |
|
807 | 807 |
_highest = _sets.back().begin(); |
808 | 808 |
while (_highest != _sets.back().end() && |
809 | 809 |
!(*_active)[_first[*_highest]]) { |
810 | 810 |
++_highest; |
811 | 811 |
} |
... | ... |
@@ -273,128 +273,128 @@ |
273 | 273 |
} |
274 | 274 |
} |
275 | 275 |
} |
276 | 276 |
} |
277 | 277 |
|
278 | 278 |
{ |
279 | 279 |
|
280 | 280 |
Node node = _graph.u(e); |
281 | 281 |
Arc arc = _graph.direct(e, true); |
282 | 282 |
Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
283 | 283 |
|
284 | 284 |
while (base != nca) { |
285 |
_ear |
|
285 |
(*_ear)[node] = arc; |
|
286 | 286 |
|
287 | 287 |
Node n = node; |
288 | 288 |
while (n != base) { |
289 | 289 |
n = _graph.target((*_matching)[n]); |
290 | 290 |
Arc a = (*_ear)[n]; |
291 | 291 |
n = _graph.target(a); |
292 |
_ear |
|
292 |
(*_ear)[n] = _graph.oppositeArc(a); |
|
293 | 293 |
} |
294 | 294 |
node = _graph.target((*_matching)[base]); |
295 | 295 |
_tree_set->erase(base); |
296 | 296 |
_tree_set->erase(node); |
297 | 297 |
_blossom_set->insert(node, _blossom_set->find(base)); |
298 |
_status |
|
298 |
(*_status)[node] = EVEN; |
|
299 | 299 |
_node_queue[_last++] = node; |
300 | 300 |
arc = _graph.oppositeArc((*_ear)[node]); |
301 | 301 |
node = _graph.target((*_ear)[node]); |
302 | 302 |
base = (*_blossom_rep)[_blossom_set->find(node)]; |
303 | 303 |
_blossom_set->join(_graph.target(arc), base); |
304 | 304 |
} |
305 | 305 |
} |
306 | 306 |
|
307 |
_blossom_rep |
|
307 |
(*_blossom_rep)[_blossom_set->find(nca)] = nca; |
|
308 | 308 |
|
309 | 309 |
{ |
310 | 310 |
|
311 | 311 |
Node node = _graph.v(e); |
312 | 312 |
Arc arc = _graph.direct(e, false); |
313 | 313 |
Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
314 | 314 |
|
315 | 315 |
while (base != nca) { |
316 |
_ear |
|
316 |
(*_ear)[node] = arc; |
|
317 | 317 |
|
318 | 318 |
Node n = node; |
319 | 319 |
while (n != base) { |
320 | 320 |
n = _graph.target((*_matching)[n]); |
321 | 321 |
Arc a = (*_ear)[n]; |
322 | 322 |
n = _graph.target(a); |
323 |
_ear |
|
323 |
(*_ear)[n] = _graph.oppositeArc(a); |
|
324 | 324 |
} |
325 | 325 |
node = _graph.target((*_matching)[base]); |
326 | 326 |
_tree_set->erase(base); |
327 | 327 |
_tree_set->erase(node); |
328 | 328 |
_blossom_set->insert(node, _blossom_set->find(base)); |
329 |
_status |
|
329 |
(*_status)[node] = EVEN; |
|
330 | 330 |
_node_queue[_last++] = node; |
331 | 331 |
arc = _graph.oppositeArc((*_ear)[node]); |
332 | 332 |
node = _graph.target((*_ear)[node]); |
333 | 333 |
base = (*_blossom_rep)[_blossom_set->find(node)]; |
334 | 334 |
_blossom_set->join(_graph.target(arc), base); |
335 | 335 |
} |
336 | 336 |
} |
337 | 337 |
|
338 |
_blossom_rep |
|
338 |
(*_blossom_rep)[_blossom_set->find(nca)] = nca; |
|
339 | 339 |
} |
340 | 340 |
|
341 | 341 |
|
342 | 342 |
|
343 | 343 |
void extendOnArc(const Arc& a) { |
344 | 344 |
Node base = _graph.source(a); |
345 | 345 |
Node odd = _graph.target(a); |
346 | 346 |
|
347 |
_ear |
|
347 |
(*_ear)[odd] = _graph.oppositeArc(a); |
|
348 | 348 |
Node even = _graph.target((*_matching)[odd]); |
349 |
_blossom_rep->set(_blossom_set->insert(even), even); |
|
350 |
_status->set(odd, ODD); |
|
351 |
|
|
349 |
(*_blossom_rep)[_blossom_set->insert(even)] = even; |
|
350 |
(*_status)[odd] = ODD; |
|
351 |
(*_status)[even] = EVEN; |
|
352 | 352 |
int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]); |
353 | 353 |
_tree_set->insert(odd, tree); |
354 | 354 |
_tree_set->insert(even, tree); |
355 | 355 |
_node_queue[_last++] = even; |
356 | 356 |
|
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
void augmentOnArc(const Arc& a) { |
360 | 360 |
Node even = _graph.source(a); |
361 | 361 |
Node odd = _graph.target(a); |
362 | 362 |
|
363 | 363 |
int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]); |
364 | 364 |
|
365 |
_matching->set(odd, _graph.oppositeArc(a)); |
|
366 |
_status->set(odd, MATCHED); |
|
365 |
(*_matching)[odd] = _graph.oppositeArc(a); |
|
366 |
(*_status)[odd] = MATCHED; |
|
367 | 367 |
|
368 | 368 |
Arc arc = (*_matching)[even]; |
369 |
_matching |
|
369 |
(*_matching)[even] = a; |
|
370 | 370 |
|
371 | 371 |
while (arc != INVALID) { |
372 | 372 |
odd = _graph.target(arc); |
373 | 373 |
arc = (*_ear)[odd]; |
374 | 374 |
even = _graph.target(arc); |
375 |
_matching |
|
375 |
(*_matching)[odd] = arc; |
|
376 | 376 |
arc = (*_matching)[even]; |
377 |
_matching |
|
377 |
(*_matching)[even] = _graph.oppositeArc((*_matching)[odd]); |
|
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
for (typename TreeSet::ItemIt it(*_tree_set, tree); |
381 | 381 |
it != INVALID; ++it) { |
382 | 382 |
if ((*_status)[it] == ODD) { |
383 |
_status |
|
383 |
(*_status)[it] = MATCHED; |
|
384 | 384 |
} else { |
385 | 385 |
int blossom = _blossom_set->find(it); |
386 | 386 |
for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom); |
387 | 387 |
jt != INVALID; ++jt) { |
388 |
_status |
|
388 |
(*_status)[jt] = MATCHED; |
|
389 | 389 |
} |
390 | 390 |
_blossom_set->eraseClass(blossom); |
391 | 391 |
} |
392 | 392 |
} |
393 | 393 |
_tree_set->eraseClass(tree); |
394 | 394 |
|
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
public: |
398 | 398 |
|
399 | 399 |
/// \brief Constructor |
400 | 400 |
/// |
... | ... |
@@ -418,110 +418,110 @@ |
418 | 418 |
/// functions first, then you can start the algorithm with the \ref |
419 | 419 |
/// startSparse() or startDense() functions. |
420 | 420 |
|
421 | 421 |
///@{ |
422 | 422 |
|
423 | 423 |
/// \brief Sets the actual matching to the empty matching. |
424 | 424 |
/// |
425 | 425 |
/// Sets the actual matching to the empty matching. |
426 | 426 |
/// |
427 | 427 |
void init() { |
428 | 428 |
createStructures(); |
429 | 429 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
430 |
_matching->set(n, INVALID); |
|
431 |
_status->set(n, UNMATCHED); |
|
430 |
(*_matching)[n] = INVALID; |
|
431 |
(*_status)[n] = UNMATCHED; |
|
432 | 432 |
} |
433 | 433 |
} |
434 | 434 |
|
435 | 435 |
///\brief Finds an initial matching in a greedy way |
436 | 436 |
/// |
437 | 437 |
///It finds an initial matching in a greedy way. |
438 | 438 |
void greedyInit() { |
439 | 439 |
createStructures(); |
440 | 440 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
441 |
_matching->set(n, INVALID); |
|
442 |
_status->set(n, UNMATCHED); |
|
441 |
(*_matching)[n] = INVALID; |
|
442 |
(*_status)[n] = UNMATCHED; |
|
443 | 443 |
} |
444 | 444 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
445 | 445 |
if ((*_matching)[n] == INVALID) { |
446 | 446 |
for (OutArcIt a(_graph, n); a != INVALID ; ++a) { |
447 | 447 |
Node v = _graph.target(a); |
448 | 448 |
if ((*_matching)[v] == INVALID && v != n) { |
449 |
_matching->set(n, a); |
|
450 |
_status->set(n, MATCHED); |
|
451 |
_matching->set(v, _graph.oppositeArc(a)); |
|
452 |
_status->set(v, MATCHED); |
|
449 |
(*_matching)[n] = a; |
|
450 |
(*_status)[n] = MATCHED; |
|
451 |
(*_matching)[v] = _graph.oppositeArc(a); |
|
452 |
(*_status)[v] = MATCHED; |
|
453 | 453 |
break; |
454 | 454 |
} |
455 | 455 |
} |
456 | 456 |
} |
457 | 457 |
} |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
|
461 | 461 |
/// \brief Initialize the matching from a map containing. |
462 | 462 |
/// |
463 | 463 |
/// Initialize the matching from a \c bool valued \c Edge map. This |
464 | 464 |
/// map must have the property that there are no two incident edges |
465 | 465 |
/// with true value, ie. it contains a matching. |
466 | 466 |
/// \return \c true if the map contains a matching. |
467 | 467 |
template <typename MatchingMap> |
468 | 468 |
bool matchingInit(const MatchingMap& matching) { |
469 | 469 |
createStructures(); |
470 | 470 |
|
471 | 471 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
472 |
_matching->set(n, INVALID); |
|
473 |
_status->set(n, UNMATCHED); |
|
472 |
(*_matching)[n] = INVALID; |
|
473 |
(*_status)[n] = UNMATCHED; |
|
474 | 474 |
} |
475 | 475 |
for(EdgeIt e(_graph); e!=INVALID; ++e) { |
476 | 476 |
if (matching[e]) { |
477 | 477 |
|
478 | 478 |
Node u = _graph.u(e); |
479 | 479 |
if ((*_matching)[u] != INVALID) return false; |
480 |
_matching->set(u, _graph.direct(e, true)); |
|
481 |
_status->set(u, MATCHED); |
|
480 |
(*_matching)[u] = _graph.direct(e, true); |
|
481 |
(*_status)[u] = MATCHED; |
|
482 | 482 |
|
483 | 483 |
Node v = _graph.v(e); |
484 | 484 |
if ((*_matching)[v] != INVALID) return false; |
485 |
_matching->set(v, _graph.direct(e, false)); |
|
486 |
_status->set(v, MATCHED); |
|
485 |
(*_matching)[v] = _graph.direct(e, false); |
|
486 |
(*_status)[v] = MATCHED; |
|
487 | 487 |
} |
488 | 488 |
} |
489 | 489 |
return true; |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
/// \brief Starts Edmonds' algorithm |
493 | 493 |
/// |
494 | 494 |
/// If runs the original Edmonds' algorithm. |
495 | 495 |
void startSparse() { |
496 | 496 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
497 | 497 |
if ((*_status)[n] == UNMATCHED) { |
498 | 498 |
(*_blossom_rep)[_blossom_set->insert(n)] = n; |
499 | 499 |
_tree_set->insert(n); |
500 |
_status |
|
500 |
(*_status)[n] = EVEN; |
|
501 | 501 |
processSparse(n); |
502 | 502 |
} |
503 | 503 |
} |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
/// \brief Starts Edmonds' algorithm. |
507 | 507 |
/// |
508 | 508 |
/// It runs Edmonds' algorithm with a heuristic of postponing |
509 | 509 |
/// shrinks, therefore resulting in a faster algorithm for dense graphs. |
510 | 510 |
void startDense() { |
511 | 511 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
512 | 512 |
if ((*_status)[n] == UNMATCHED) { |
513 | 513 |
(*_blossom_rep)[_blossom_set->insert(n)] = n; |
514 | 514 |
_tree_set->insert(n); |
515 |
_status |
|
515 |
(*_status)[n] = EVEN; |
|
516 | 516 |
processDense(n); |
517 | 517 |
} |
518 | 518 |
} |
519 | 519 |
} |
520 | 520 |
|
521 | 521 |
|
522 | 522 |
/// \brief Runs Edmonds' algorithm |
523 | 523 |
/// |
524 | 524 |
/// Runs Edmonds' algorithm for sparse graphs (<tt>m<2*n</tt>) |
525 | 525 |
/// or Edmonds' algorithm with a heuristic of |
526 | 526 |
/// postponing shrinks for dense graphs. |
527 | 527 |
void run() { |
... | ... |
@@ -1539,27 +1539,27 @@ |
1539 | 1539 |
_tree_set->insert(subblossoms[ib], tree); |
1540 | 1540 |
(*_blossom_data)[subblossoms[ib]].next = next; |
1541 | 1541 |
(*_blossom_data)[subblossoms[ib]].pred = pred; |
1542 | 1542 |
} |
1543 | 1543 |
_tree_set->erase(blossom); |
1544 | 1544 |
} |
1545 | 1545 |
|
1546 | 1546 |
void extractBlossom(int blossom, const Node& base, const Arc& matching) { |
1547 | 1547 |
if (_blossom_set->trivial(blossom)) { |
1548 | 1548 |
int bi = (*_node_index)[base]; |
1549 | 1549 |
Value pot = (*_node_data)[bi].pot; |
1550 | 1550 |
|
1551 |
_matching |
|
1551 |
(*_matching)[base] = matching; |
|
1552 | 1552 |
_blossom_node_list.push_back(base); |
1553 |
_node_potential |
|
1553 |
(*_node_potential)[base] = pot; |
|
1554 | 1554 |
} else { |
1555 | 1555 |
|
1556 | 1556 |
Value pot = (*_blossom_data)[blossom].pot; |
1557 | 1557 |
int bn = _blossom_node_list.size(); |
1558 | 1558 |
|
1559 | 1559 |
std::vector<int> subblossoms; |
1560 | 1560 |
_blossom_set->split(blossom, std::back_inserter(subblossoms)); |
1561 | 1561 |
int b = _blossom_set->find(base); |
1562 | 1562 |
int ib = -1; |
1563 | 1563 |
for (int i = 0; i < int(subblossoms.size()); ++i) { |
1564 | 1564 |
if (subblossoms[i] == b) { ib = i; break; } |
1565 | 1565 |
} |
... | ... |
@@ -1635,47 +1635,47 @@ |
1635 | 1635 |
/// The simplest way to execute the algorithm is to use the |
1636 | 1636 |
/// \c run() member function. |
1637 | 1637 |
|
1638 | 1638 |
///@{ |
1639 | 1639 |
|
1640 | 1640 |
/// \brief Initialize the algorithm |
1641 | 1641 |
/// |
1642 | 1642 |
/// Initialize the algorithm |
1643 | 1643 |
void init() { |
1644 | 1644 |
createStructures(); |
1645 | 1645 |
|
1646 | 1646 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
1647 |
_node_heap_index |
|
1647 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
|
1648 | 1648 |
} |
1649 | 1649 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1650 |
_delta1_index |
|
1650 |
(*_delta1_index)[n] = _delta1->PRE_HEAP; |
|
1651 | 1651 |
} |
1652 | 1652 |
for (EdgeIt e(_graph); e != INVALID; ++e) { |
1653 |
_delta3_index |
|
1653 |
(*_delta3_index)[e] = _delta3->PRE_HEAP; |
|
1654 | 1654 |
} |
1655 | 1655 |
for (int i = 0; i < _blossom_num; ++i) { |
1656 |
_delta2_index->set(i, _delta2->PRE_HEAP); |
|
1657 |
_delta4_index->set(i, _delta4->PRE_HEAP); |
|
1656 |
(*_delta2_index)[i] = _delta2->PRE_HEAP; |
|
1657 |
(*_delta4_index)[i] = _delta4->PRE_HEAP; |
|
1658 | 1658 |
} |
1659 | 1659 |
|
1660 | 1660 |
int index = 0; |
1661 | 1661 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1662 | 1662 |
Value max = 0; |
1663 | 1663 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
1664 | 1664 |
if (_graph.target(e) == n) continue; |
1665 | 1665 |
if ((dualScale * _weight[e]) / 2 > max) { |
1666 | 1666 |
max = (dualScale * _weight[e]) / 2; |
1667 | 1667 |
} |
1668 | 1668 |
} |
1669 |
_node_index |
|
1669 |
(*_node_index)[n] = index; |
|
1670 | 1670 |
(*_node_data)[index].pot = max; |
1671 | 1671 |
_delta1->push(n, max); |
1672 | 1672 |
int blossom = |
1673 | 1673 |
_blossom_set->insert(n, std::numeric_limits<Value>::max()); |
1674 | 1674 |
|
1675 | 1675 |
_tree_set->insert(blossom); |
1676 | 1676 |
|
1677 | 1677 |
(*_blossom_data)[blossom].status = EVEN; |
1678 | 1678 |
(*_blossom_data)[blossom].pred = INVALID; |
1679 | 1679 |
(*_blossom_data)[blossom].next = INVALID; |
1680 | 1680 |
(*_blossom_data)[blossom].pot = 0; |
1681 | 1681 |
(*_blossom_data)[blossom].offset = 0; |
... | ... |
@@ -2732,27 +2732,27 @@ |
2732 | 2732 |
_tree_set->insert(subblossoms[ib], tree); |
2733 | 2733 |
(*_blossom_data)[subblossoms[ib]].next = next; |
2734 | 2734 |
(*_blossom_data)[subblossoms[ib]].pred = pred; |
2735 | 2735 |
} |
2736 | 2736 |
_tree_set->erase(blossom); |
2737 | 2737 |
} |
2738 | 2738 |
|
2739 | 2739 |
void extractBlossom(int blossom, const Node& base, const Arc& matching) { |
2740 | 2740 |
if (_blossom_set->trivial(blossom)) { |
2741 | 2741 |
int bi = (*_node_index)[base]; |
2742 | 2742 |
Value pot = (*_node_data)[bi].pot; |
2743 | 2743 |
|
2744 |
_matching |
|
2744 |
(*_matching)[base] = matching; |
|
2745 | 2745 |
_blossom_node_list.push_back(base); |
2746 |
_node_potential |
|
2746 |
(*_node_potential)[base] = pot; |
|
2747 | 2747 |
} else { |
2748 | 2748 |
|
2749 | 2749 |
Value pot = (*_blossom_data)[blossom].pot; |
2750 | 2750 |
int bn = _blossom_node_list.size(); |
2751 | 2751 |
|
2752 | 2752 |
std::vector<int> subblossoms; |
2753 | 2753 |
_blossom_set->split(blossom, std::back_inserter(subblossoms)); |
2754 | 2754 |
int b = _blossom_set->find(base); |
2755 | 2755 |
int ib = -1; |
2756 | 2756 |
for (int i = 0; i < int(subblossoms.size()); ++i) { |
2757 | 2757 |
if (subblossoms[i] == b) { ib = i; break; } |
2758 | 2758 |
} |
... | ... |
@@ -2822,44 +2822,44 @@ |
2822 | 2822 |
/// The simplest way to execute the algorithm is to use the |
2823 | 2823 |
/// \c run() member function. |
2824 | 2824 |
|
2825 | 2825 |
///@{ |
2826 | 2826 |
|
2827 | 2827 |
/// \brief Initialize the algorithm |
2828 | 2828 |
/// |
2829 | 2829 |
/// Initialize the algorithm |
2830 | 2830 |
void init() { |
2831 | 2831 |
createStructures(); |
2832 | 2832 |
|
2833 | 2833 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
2834 |
_node_heap_index |
|
2834 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
|
2835 | 2835 |
} |
2836 | 2836 |
for (EdgeIt e(_graph); e != INVALID; ++e) { |
2837 |
_delta3_index |
|
2837 |
(*_delta3_index)[e] = _delta3->PRE_HEAP; |
|
2838 | 2838 |
} |
2839 | 2839 |
for (int i = 0; i < _blossom_num; ++i) { |
2840 |
_delta2_index->set(i, _delta2->PRE_HEAP); |
|
2841 |
_delta4_index->set(i, _delta4->PRE_HEAP); |
|
2840 |
(*_delta2_index)[i] = _delta2->PRE_HEAP; |
|
2841 |
(*_delta4_index)[i] = _delta4->PRE_HEAP; |
|
2842 | 2842 |
} |
2843 | 2843 |
|
2844 | 2844 |
int index = 0; |
2845 | 2845 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
2846 | 2846 |
Value max = - std::numeric_limits<Value>::max(); |
2847 | 2847 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
2848 | 2848 |
if (_graph.target(e) == n) continue; |
2849 | 2849 |
if ((dualScale * _weight[e]) / 2 > max) { |
2850 | 2850 |
max = (dualScale * _weight[e]) / 2; |
2851 | 2851 |
} |
2852 | 2852 |
} |
2853 |
_node_index |
|
2853 |
(*_node_index)[n] = index; |
|
2854 | 2854 |
(*_node_data)[index].pot = max; |
2855 | 2855 |
int blossom = |
2856 | 2856 |
_blossom_set->insert(n, std::numeric_limits<Value>::max()); |
2857 | 2857 |
|
2858 | 2858 |
_tree_set->insert(blossom); |
2859 | 2859 |
|
2860 | 2860 |
(*_blossom_data)[blossom].status = EVEN; |
2861 | 2861 |
(*_blossom_data)[blossom].pred = INVALID; |
2862 | 2862 |
(*_blossom_data)[blossom].next = INVALID; |
2863 | 2863 |
(*_blossom_data)[blossom].pot = 0; |
2864 | 2864 |
(*_blossom_data)[blossom].offset = 0; |
2865 | 2865 |
++index; |
... | ... |
@@ -284,25 +284,25 @@ |
284 | 284 |
if ((*_cost_arcs)[source].value > value) { |
285 | 285 |
(*_cost_arcs)[source].arc = arc; |
286 | 286 |
(*_cost_arcs)[source].value = value; |
287 | 287 |
} |
288 | 288 |
} |
289 | 289 |
} |
290 | 290 |
CostArc minimum = (*_cost_arcs)[nodes[0]]; |
291 | 291 |
for (int i = 1; i < int(nodes.size()); ++i) { |
292 | 292 |
if ((*_cost_arcs)[nodes[i]].value < minimum.value) { |
293 | 293 |
minimum = (*_cost_arcs)[nodes[i]]; |
294 | 294 |
} |
295 | 295 |
} |
296 |
_arc_order |
|
296 |
(*_arc_order)[minimum.arc] = _dual_variables.size(); |
|
297 | 297 |
DualVariable var(_dual_node_list.size() - 1, |
298 | 298 |
_dual_node_list.size(), minimum.value); |
299 | 299 |
_dual_variables.push_back(var); |
300 | 300 |
for (int i = 0; i < int(nodes.size()); ++i) { |
301 | 301 |
(*_cost_arcs)[nodes[i]].value -= minimum.value; |
302 | 302 |
level.arcs.push_back((*_cost_arcs)[nodes[i]]); |
303 | 303 |
(*_cost_arcs)[nodes[i]].arc = INVALID; |
304 | 304 |
} |
305 | 305 |
level_stack.push_back(level); |
306 | 306 |
return minimum.arc; |
307 | 307 |
} |
308 | 308 |
|
... | ... |
@@ -326,25 +326,25 @@ |
326 | 326 |
(*_cost_arcs)[source].value = value; |
327 | 327 |
} |
328 | 328 |
} |
329 | 329 |
} |
330 | 330 |
level_stack.pop_back(); |
331 | 331 |
} |
332 | 332 |
CostArc minimum = (*_cost_arcs)[nodes[0]]; |
333 | 333 |
for (int i = 1; i < int(nodes.size()); ++i) { |
334 | 334 |
if ((*_cost_arcs)[nodes[i]].value < minimum.value) { |
335 | 335 |
minimum = (*_cost_arcs)[nodes[i]]; |
336 | 336 |
} |
337 | 337 |
} |
338 |
_arc_order |
|
338 |
(*_arc_order)[minimum.arc] = _dual_variables.size(); |
|
339 | 339 |
DualVariable var(node_bottom, _dual_node_list.size(), minimum.value); |
340 | 340 |
_dual_variables.push_back(var); |
341 | 341 |
StackLevel level; |
342 | 342 |
level.node_level = node_bottom; |
343 | 343 |
for (int i = 0; i < int(nodes.size()); ++i) { |
344 | 344 |
(*_cost_arcs)[nodes[i]].value -= minimum.value; |
345 | 345 |
level.arcs.push_back((*_cost_arcs)[nodes[i]]); |
346 | 346 |
(*_cost_arcs)[nodes[i]].arc = INVALID; |
347 | 347 |
} |
348 | 348 |
level_stack.push_back(level); |
349 | 349 |
return minimum.arc; |
350 | 350 |
} |
... | ... |
@@ -355,25 +355,25 @@ |
355 | 355 |
--k; |
356 | 356 |
} |
357 | 357 |
return level_stack[k].node_level; |
358 | 358 |
} |
359 | 359 |
|
360 | 360 |
void finalize(Arc arc) { |
361 | 361 |
Node node = _digraph->target(arc); |
362 | 362 |
_heap->push(node, (*_arc_order)[arc]); |
363 | 363 |
_pred->set(node, arc); |
364 | 364 |
while (!_heap->empty()) { |
365 | 365 |
Node source = _heap->top(); |
366 | 366 |
_heap->pop(); |
367 |
_node_order |
|
367 |
(*_node_order)[source] = -1; |
|
368 | 368 |
for (OutArcIt it(*_digraph, source); it != INVALID; ++it) { |
369 | 369 |
if ((*_arc_order)[it] < 0) continue; |
370 | 370 |
Node target = _digraph->target(it); |
371 | 371 |
switch(_heap->state(target)) { |
372 | 372 |
case Heap::PRE_HEAP: |
373 | 373 |
_heap->push(target, (*_arc_order)[it]); |
374 | 374 |
_pred->set(target, it); |
375 | 375 |
break; |
376 | 376 |
case Heap::IN_HEAP: |
377 | 377 |
if ((*_arc_order)[it] < (*_heap)[target]) { |
378 | 378 |
_heap->decrease(target, (*_arc_order)[it]); |
379 | 379 |
_pred->set(target, it); |
... | ... |
@@ -641,31 +641,31 @@ |
641 | 641 |
|
642 | 642 |
///@{ |
643 | 643 |
|
644 | 644 |
/// \brief Initializes the internal data structures. |
645 | 645 |
/// |
646 | 646 |
/// Initializes the internal data structures. |
647 | 647 |
/// |
648 | 648 |
void init() { |
649 | 649 |
createStructures(); |
650 | 650 |
_heap->clear(); |
651 | 651 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
652 | 652 |
(*_cost_arcs)[it].arc = INVALID; |
653 |
_node_order->set(it, -3); |
|
654 |
_heap_cross_ref->set(it, Heap::PRE_HEAP); |
|
653 |
(*_node_order)[it] = -3; |
|
654 |
(*_heap_cross_ref)[it] = Heap::PRE_HEAP; |
|
655 | 655 |
_pred->set(it, INVALID); |
656 | 656 |
} |
657 | 657 |
for (ArcIt it(*_digraph); it != INVALID; ++it) { |
658 | 658 |
_arborescence->set(it, false); |
659 |
_arc_order |
|
659 |
(*_arc_order)[it] = -1; |
|
660 | 660 |
} |
661 | 661 |
_dual_node_list.clear(); |
662 | 662 |
_dual_variables.clear(); |
663 | 663 |
} |
664 | 664 |
|
665 | 665 |
/// \brief Adds a new source node. |
666 | 666 |
/// |
667 | 667 |
/// Adds a new source node to the algorithm. |
668 | 668 |
void addSource(Node source) { |
669 | 669 |
std::vector<Node> nodes; |
670 | 670 |
nodes.push_back(source); |
671 | 671 |
while (!nodes.empty()) { |
... | ... |
@@ -395,65 +395,65 @@ |
395 | 395 |
|
396 | 396 |
///@{ |
397 | 397 |
|
398 | 398 |
/// \brief Initializes the internal data structures. |
399 | 399 |
/// |
400 | 400 |
/// Initializes the internal data structures and sets the initial |
401 | 401 |
/// flow to zero on each arc. |
402 | 402 |
void init() { |
403 | 403 |
createStructures(); |
404 | 404 |
|
405 | 405 |
_phase = true; |
406 | 406 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
407 |
_excess |
|
407 |
(*_excess)[n] = 0; |
|
408 | 408 |
} |
409 | 409 |
|
410 | 410 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
411 | 411 |
_flow->set(e, 0); |
412 | 412 |
} |
413 | 413 |
|
414 | 414 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
415 | 415 |
|
416 | 416 |
_level->initStart(); |
417 | 417 |
_level->initAddItem(_target); |
418 | 418 |
|
419 | 419 |
std::vector<Node> queue; |
420 |
reached |
|
420 |
reached[_source] = true; |
|
421 | 421 |
|
422 | 422 |
queue.push_back(_target); |
423 |
reached |
|
423 |
reached[_target] = true; |
|
424 | 424 |
while (!queue.empty()) { |
425 | 425 |
_level->initNewLevel(); |
426 | 426 |
std::vector<Node> nqueue; |
427 | 427 |
for (int i = 0; i < int(queue.size()); ++i) { |
428 | 428 |
Node n = queue[i]; |
429 | 429 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
430 | 430 |
Node u = _graph.source(e); |
431 | 431 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) { |
432 |
reached |
|
432 |
reached[u] = true; |
|
433 | 433 |
_level->initAddItem(u); |
434 | 434 |
nqueue.push_back(u); |
435 | 435 |
} |
436 | 436 |
} |
437 | 437 |
} |
438 | 438 |
queue.swap(nqueue); |
439 | 439 |
} |
440 | 440 |
_level->initFinish(); |
441 | 441 |
|
442 | 442 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
443 | 443 |
if (_tolerance.positive((*_capacity)[e])) { |
444 | 444 |
Node u = _graph.target(e); |
445 | 445 |
if ((*_level)[u] == _level->maxLevel()) continue; |
446 | 446 |
_flow->set(e, (*_capacity)[e]); |
447 |
|
|
447 |
(*_excess)[u] += (*_capacity)[e]; |
|
448 | 448 |
if (u != _target && !_level->active(u)) { |
449 | 449 |
_level->activate(u); |
450 | 450 |
} |
451 | 451 |
} |
452 | 452 |
} |
453 | 453 |
} |
454 | 454 |
|
455 | 455 |
/// \brief Initializes the internal data structures using the |
456 | 456 |
/// given flow map. |
457 | 457 |
/// |
458 | 458 |
/// Initializes the internal data structures and sets the initial |
459 | 459 |
/// flow to the given \c flowMap. The \c flowMap should contain a |
... | ... |
@@ -469,83 +469,83 @@ |
469 | 469 |
_flow->set(e, flowMap[e]); |
470 | 470 |
} |
471 | 471 |
|
472 | 472 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
473 | 473 |
Value excess = 0; |
474 | 474 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
475 | 475 |
excess += (*_flow)[e]; |
476 | 476 |
} |
477 | 477 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
478 | 478 |
excess -= (*_flow)[e]; |
479 | 479 |
} |
480 | 480 |
if (excess < 0 && n != _source) return false; |
481 |
_excess |
|
481 |
(*_excess)[n] = excess; |
|
482 | 482 |
} |
483 | 483 |
|
484 | 484 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
485 | 485 |
|
486 | 486 |
_level->initStart(); |
487 | 487 |
_level->initAddItem(_target); |
488 | 488 |
|
489 | 489 |
std::vector<Node> queue; |
490 |
reached |
|
490 |
reached[_source] = true; |
|
491 | 491 |
|
492 | 492 |
queue.push_back(_target); |
493 |
reached |
|
493 |
reached[_target] = true; |
|
494 | 494 |
while (!queue.empty()) { |
495 | 495 |
_level->initNewLevel(); |
496 | 496 |
std::vector<Node> nqueue; |
497 | 497 |
for (int i = 0; i < int(queue.size()); ++i) { |
498 | 498 |
Node n = queue[i]; |
499 | 499 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
500 | 500 |
Node u = _graph.source(e); |
501 | 501 |
if (!reached[u] && |
502 | 502 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
503 |
reached |
|
503 |
reached[u] = true; |
|
504 | 504 |
_level->initAddItem(u); |
505 | 505 |
nqueue.push_back(u); |
506 | 506 |
} |
507 | 507 |
} |
508 | 508 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
509 | 509 |
Node v = _graph.target(e); |
510 | 510 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
511 |
reached |
|
511 |
reached[v] = true; |
|
512 | 512 |
_level->initAddItem(v); |
513 | 513 |
nqueue.push_back(v); |
514 | 514 |
} |
515 | 515 |
} |
516 | 516 |
} |
517 | 517 |
queue.swap(nqueue); |
518 | 518 |
} |
519 | 519 |
_level->initFinish(); |
520 | 520 |
|
521 | 521 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
522 | 522 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
523 | 523 |
if (_tolerance.positive(rem)) { |
524 | 524 |
Node u = _graph.target(e); |
525 | 525 |
if ((*_level)[u] == _level->maxLevel()) continue; |
526 | 526 |
_flow->set(e, (*_capacity)[e]); |
527 |
|
|
527 |
(*_excess)[u] += rem; |
|
528 | 528 |
if (u != _target && !_level->active(u)) { |
529 | 529 |
_level->activate(u); |
530 | 530 |
} |
531 | 531 |
} |
532 | 532 |
} |
533 | 533 |
for (InArcIt e(_graph, _source); e != INVALID; ++e) { |
534 | 534 |
Value rem = (*_flow)[e]; |
535 | 535 |
if (_tolerance.positive(rem)) { |
536 | 536 |
Node v = _graph.source(e); |
537 | 537 |
if ((*_level)[v] == _level->maxLevel()) continue; |
538 | 538 |
_flow->set(e, 0); |
539 |
|
|
539 |
(*_excess)[v] += rem; |
|
540 | 540 |
if (v != _target && !_level->active(v)) { |
541 | 541 |
_level->activate(v); |
542 | 542 |
} |
543 | 543 |
} |
544 | 544 |
} |
545 | 545 |
return true; |
546 | 546 |
} |
547 | 547 |
|
548 | 548 |
/// \brief Starts the first phase of the preflow algorithm. |
549 | 549 |
/// |
550 | 550 |
/// The preflow algorithm consists of two phases, this method runs |
551 | 551 |
/// the first phase. After the first phase the maximum flow value |
... | ... |
@@ -568,63 +568,63 @@ |
568 | 568 |
int new_level = _level->maxLevel(); |
569 | 569 |
|
570 | 570 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
571 | 571 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
572 | 572 |
if (!_tolerance.positive(rem)) continue; |
573 | 573 |
Node v = _graph.target(e); |
574 | 574 |
if ((*_level)[v] < level) { |
575 | 575 |
if (!_level->active(v) && v != _target) { |
576 | 576 |
_level->activate(v); |
577 | 577 |
} |
578 | 578 |
if (!_tolerance.less(rem, excess)) { |
579 | 579 |
_flow->set(e, (*_flow)[e] + excess); |
580 |
|
|
580 |
(*_excess)[v] += excess; |
|
581 | 581 |
excess = 0; |
582 | 582 |
goto no_more_push_1; |
583 | 583 |
} else { |
584 | 584 |
excess -= rem; |
585 |
|
|
585 |
(*_excess)[v] += rem; |
|
586 | 586 |
_flow->set(e, (*_capacity)[e]); |
587 | 587 |
} |
588 | 588 |
} else if (new_level > (*_level)[v]) { |
589 | 589 |
new_level = (*_level)[v]; |
590 | 590 |
} |
591 | 591 |
} |
592 | 592 |
|
593 | 593 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
594 | 594 |
Value rem = (*_flow)[e]; |
595 | 595 |
if (!_tolerance.positive(rem)) continue; |
596 | 596 |
Node v = _graph.source(e); |
597 | 597 |
if ((*_level)[v] < level) { |
598 | 598 |
if (!_level->active(v) && v != _target) { |
599 | 599 |
_level->activate(v); |
600 | 600 |
} |
601 | 601 |
if (!_tolerance.less(rem, excess)) { |
602 | 602 |
_flow->set(e, (*_flow)[e] - excess); |
603 |
|
|
603 |
(*_excess)[v] += excess; |
|
604 | 604 |
excess = 0; |
605 | 605 |
goto no_more_push_1; |
606 | 606 |
} else { |
607 | 607 |
excess -= rem; |
608 |
|
|
608 |
(*_excess)[v] += rem; |
|
609 | 609 |
_flow->set(e, 0); |
610 | 610 |
} |
611 | 611 |
} else if (new_level > (*_level)[v]) { |
612 | 612 |
new_level = (*_level)[v]; |
613 | 613 |
} |
614 | 614 |
} |
615 | 615 |
|
616 | 616 |
no_more_push_1: |
617 | 617 |
|
618 |
_excess |
|
618 |
(*_excess)[n] = excess; |
|
619 | 619 |
|
620 | 620 |
if (excess != 0) { |
621 | 621 |
if (new_level + 1 < _level->maxLevel()) { |
622 | 622 |
_level->liftHighestActive(new_level + 1); |
623 | 623 |
} else { |
624 | 624 |
_level->liftHighestActiveToTop(); |
625 | 625 |
} |
626 | 626 |
if (_level->emptyLevel(level)) { |
627 | 627 |
_level->liftToTop(level); |
628 | 628 |
} |
629 | 629 |
} else { |
630 | 630 |
_level->deactivate(n); |
... | ... |
@@ -641,63 +641,63 @@ |
641 | 641 |
int new_level = _level->maxLevel(); |
642 | 642 |
|
643 | 643 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
644 | 644 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
645 | 645 |
if (!_tolerance.positive(rem)) continue; |
646 | 646 |
Node v = _graph.target(e); |
647 | 647 |
if ((*_level)[v] < level) { |
648 | 648 |
if (!_level->active(v) && v != _target) { |
649 | 649 |
_level->activate(v); |
650 | 650 |
} |
651 | 651 |
if (!_tolerance.less(rem, excess)) { |
652 | 652 |
_flow->set(e, (*_flow)[e] + excess); |
653 |
|
|
653 |
(*_excess)[v] += excess; |
|
654 | 654 |
excess = 0; |
655 | 655 |
goto no_more_push_2; |
656 | 656 |
} else { |
657 | 657 |
excess -= rem; |
658 |
|
|
658 |
(*_excess)[v] += rem; |
|
659 | 659 |
_flow->set(e, (*_capacity)[e]); |
660 | 660 |
} |
661 | 661 |
} else if (new_level > (*_level)[v]) { |
662 | 662 |
new_level = (*_level)[v]; |
663 | 663 |
} |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
667 | 667 |
Value rem = (*_flow)[e]; |
668 | 668 |
if (!_tolerance.positive(rem)) continue; |
669 | 669 |
Node v = _graph.source(e); |
670 | 670 |
if ((*_level)[v] < level) { |
671 | 671 |
if (!_level->active(v) && v != _target) { |
672 | 672 |
_level->activate(v); |
673 | 673 |
} |
674 | 674 |
if (!_tolerance.less(rem, excess)) { |
675 | 675 |
_flow->set(e, (*_flow)[e] - excess); |
676 |
|
|
676 |
(*_excess)[v] += excess; |
|
677 | 677 |
excess = 0; |
678 | 678 |
goto no_more_push_2; |
679 | 679 |
} else { |
680 | 680 |
excess -= rem; |
681 |
|
|
681 |
(*_excess)[v] += rem; |
|
682 | 682 |
_flow->set(e, 0); |
683 | 683 |
} |
684 | 684 |
} else if (new_level > (*_level)[v]) { |
685 | 685 |
new_level = (*_level)[v]; |
686 | 686 |
} |
687 | 687 |
} |
688 | 688 |
|
689 | 689 |
no_more_push_2: |
690 | 690 |
|
691 |
_excess |
|
691 |
(*_excess)[n] = excess; |
|
692 | 692 |
|
693 | 693 |
if (excess != 0) { |
694 | 694 |
if (new_level + 1 < _level->maxLevel()) { |
695 | 695 |
_level->liftActiveOn(level, new_level + 1); |
696 | 696 |
} else { |
697 | 697 |
_level->liftActiveToTop(level); |
698 | 698 |
} |
699 | 699 |
if (_level->emptyLevel(level)) { |
700 | 700 |
_level->liftToTop(level); |
701 | 701 |
} |
702 | 702 |
} else { |
703 | 703 |
_level->deactivate(n); |
... | ... |
@@ -722,52 +722,52 @@ |
722 | 722 |
/// The preflow algorithm consists of two phases, this method runs |
723 | 723 |
/// the second phase. After calling one of the \ref init() functions |
724 | 724 |
/// and \ref startFirstPhase() and then \ref startSecondPhase(), |
725 | 725 |
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the |
726 | 726 |
/// value of a maximum flow, \ref minCut() returns a minimum cut |
727 | 727 |
/// \pre One of the \ref init() functions and \ref startFirstPhase() |
728 | 728 |
/// must be called before using this function. |
729 | 729 |
void startSecondPhase() { |
730 | 730 |
_phase = false; |
731 | 731 |
|
732 | 732 |
typename Digraph::template NodeMap<bool> reached(_graph); |
733 | 733 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
734 |
reached |
|
734 |
reached[n] = (*_level)[n] < _level->maxLevel(); |
|
735 | 735 |
} |
736 | 736 |
|
737 | 737 |
_level->initStart(); |
738 | 738 |
_level->initAddItem(_source); |
739 | 739 |
|
740 | 740 |
std::vector<Node> queue; |
741 | 741 |
queue.push_back(_source); |
742 |
reached |
|
742 |
reached[_source] = true; |
|
743 | 743 |
|
744 | 744 |
while (!queue.empty()) { |
745 | 745 |
_level->initNewLevel(); |
746 | 746 |
std::vector<Node> nqueue; |
747 | 747 |
for (int i = 0; i < int(queue.size()); ++i) { |
748 | 748 |
Node n = queue[i]; |
749 | 749 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
750 | 750 |
Node v = _graph.target(e); |
751 | 751 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
752 |
reached |
|
752 |
reached[v] = true; |
|
753 | 753 |
_level->initAddItem(v); |
754 | 754 |
nqueue.push_back(v); |
755 | 755 |
} |
756 | 756 |
} |
757 | 757 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
758 | 758 |
Node u = _graph.source(e); |
759 | 759 |
if (!reached[u] && |
760 | 760 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
761 |
reached |
|
761 |
reached[u] = true; |
|
762 | 762 |
_level->initAddItem(u); |
763 | 763 |
nqueue.push_back(u); |
764 | 764 |
} |
765 | 765 |
} |
766 | 766 |
} |
767 | 767 |
queue.swap(nqueue); |
768 | 768 |
} |
769 | 769 |
_level->initFinish(); |
770 | 770 |
|
771 | 771 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
772 | 772 |
if (!reached[n]) { |
773 | 773 |
_level->dirtyTopButOne(n); |
... | ... |
@@ -783,63 +783,63 @@ |
783 | 783 |
int new_level = _level->maxLevel(); |
784 | 784 |
|
785 | 785 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
786 | 786 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
787 | 787 |
if (!_tolerance.positive(rem)) continue; |
788 | 788 |
Node v = _graph.target(e); |
789 | 789 |
if ((*_level)[v] < level) { |
790 | 790 |
if (!_level->active(v) && v != _source) { |
791 | 791 |
_level->activate(v); |
792 | 792 |
} |
793 | 793 |
if (!_tolerance.less(rem, excess)) { |
794 | 794 |
_flow->set(e, (*_flow)[e] + excess); |
795 |
|
|
795 |
(*_excess)[v] += excess; |
|
796 | 796 |
excess = 0; |
797 | 797 |
goto no_more_push; |
798 | 798 |
} else { |
799 | 799 |
excess -= rem; |
800 |
|
|
800 |
(*_excess)[v] += rem; |
|
801 | 801 |
_flow->set(e, (*_capacity)[e]); |
802 | 802 |
} |
803 | 803 |
} else if (new_level > (*_level)[v]) { |
804 | 804 |
new_level = (*_level)[v]; |
805 | 805 |
} |
806 | 806 |
} |
807 | 807 |
|
808 | 808 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
809 | 809 |
Value rem = (*_flow)[e]; |
810 | 810 |
if (!_tolerance.positive(rem)) continue; |
811 | 811 |
Node v = _graph.source(e); |
812 | 812 |
if ((*_level)[v] < level) { |
813 | 813 |
if (!_level->active(v) && v != _source) { |
814 | 814 |
_level->activate(v); |
815 | 815 |
} |
816 | 816 |
if (!_tolerance.less(rem, excess)) { |
817 | 817 |
_flow->set(e, (*_flow)[e] - excess); |
818 |
|
|
818 |
(*_excess)[v] += excess; |
|
819 | 819 |
excess = 0; |
820 | 820 |
goto no_more_push; |
821 | 821 |
} else { |
822 | 822 |
excess -= rem; |
823 |
|
|
823 |
(*_excess)[v] += rem; |
|
824 | 824 |
_flow->set(e, 0); |
825 | 825 |
} |
826 | 826 |
} else if (new_level > (*_level)[v]) { |
827 | 827 |
new_level = (*_level)[v]; |
828 | 828 |
} |
829 | 829 |
} |
830 | 830 |
|
831 | 831 |
no_more_push: |
832 | 832 |
|
833 |
_excess |
|
833 |
(*_excess)[n] = excess; |
|
834 | 834 |
|
835 | 835 |
if (excess != 0) { |
836 | 836 |
if (new_level + 1 < _level->maxLevel()) { |
837 | 837 |
_level->liftHighestActive(new_level + 1); |
838 | 838 |
} else { |
839 | 839 |
// Calculation error |
840 | 840 |
_level->liftHighestActiveToTop(); |
841 | 841 |
} |
842 | 842 |
if (_level->emptyLevel(level)) { |
843 | 843 |
// Calculation error |
844 | 844 |
_level->liftToTop(level); |
845 | 845 |
} |
... | ... |
@@ -90,34 +90,34 @@ |
90 | 90 |
|
91 | 91 |
ECostMap edge_cost_map(G, 2); |
92 | 92 |
EBoolMap tree_map(G); |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
//Test with const map. |
96 | 96 |
check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10, |
97 | 97 |
"Total cost should be 10"); |
98 | 98 |
//Test with an edge map (filled with uniform costs). |
99 | 99 |
check(kruskal(G, edge_cost_map, tree_map)==10, |
100 | 100 |
"Total cost should be 10"); |
101 | 101 |
|
102 |
edge_cost_map.set(e1, -10); |
|
103 |
edge_cost_map.set(e2, -9); |
|
104 |
edge_cost_map.set(e3, -8); |
|
105 |
edge_cost_map.set(e4, -7); |
|
106 |
edge_cost_map.set(e5, -6); |
|
107 |
edge_cost_map.set(e6, -5); |
|
108 |
edge_cost_map.set(e7, -4); |
|
109 |
edge_cost_map.set(e8, -3); |
|
110 |
edge_cost_map.set(e9, -2); |
|
111 |
edge_cost_map.set(e10, -1); |
|
102 |
edge_cost_map[e1] = -10; |
|
103 |
edge_cost_map[e2] = -9; |
|
104 |
edge_cost_map[e3] = -8; |
|
105 |
edge_cost_map[e4] = -7; |
|
106 |
edge_cost_map[e5] = -6; |
|
107 |
edge_cost_map[e6] = -5; |
|
108 |
edge_cost_map[e7] = -4; |
|
109 |
edge_cost_map[e8] = -3; |
|
110 |
edge_cost_map[e9] = -2; |
|
111 |
edge_cost_map[e10] = -1; |
|
112 | 112 |
|
113 | 113 |
vector<Edge> tree_edge_vec(5); |
114 | 114 |
|
115 | 115 |
//Test with a edge map and inserter. |
116 | 116 |
check(kruskal(G, edge_cost_map, |
117 | 117 |
tree_edge_vec.begin()) |
118 | 118 |
==-31, |
119 | 119 |
"Total cost should be -31."); |
120 | 120 |
|
121 | 121 |
tree_edge_vec.clear(); |
122 | 122 |
|
123 | 123 |
check(kruskal(G, edge_cost_map, |
0 comments (0 inline)