| ... | ... |
@@ -1259,420 +1259,423 @@ |
| 1259 | 1259 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
| 1260 | 1260 |
{
|
| 1261 | 1261 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
| 1262 | 1262 |
refresh(); |
| 1263 | 1263 |
} |
| 1264 | 1264 |
|
| 1265 | 1265 |
protected: |
| 1266 | 1266 |
|
| 1267 | 1267 |
virtual void add(const Arc& arc) {
|
| 1268 | 1268 |
insert(arc); |
| 1269 | 1269 |
} |
| 1270 | 1270 |
|
| 1271 | 1271 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1272 | 1272 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1273 | 1273 |
insert(arcs[i]); |
| 1274 | 1274 |
} |
| 1275 | 1275 |
} |
| 1276 | 1276 |
|
| 1277 | 1277 |
virtual void erase(const Arc& arc) {
|
| 1278 | 1278 |
remove(arc); |
| 1279 | 1279 |
} |
| 1280 | 1280 |
|
| 1281 | 1281 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1282 | 1282 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1283 | 1283 |
remove(arcs[i]); |
| 1284 | 1284 |
} |
| 1285 | 1285 |
} |
| 1286 | 1286 |
|
| 1287 | 1287 |
virtual void build() {
|
| 1288 | 1288 |
refresh(); |
| 1289 | 1289 |
} |
| 1290 | 1290 |
|
| 1291 | 1291 |
virtual void clear() {
|
| 1292 | 1292 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1293 | 1293 |
_head.set(n, INVALID); |
| 1294 | 1294 |
} |
| 1295 | 1295 |
} |
| 1296 | 1296 |
|
| 1297 | 1297 |
void insert(Arc arc) {
|
| 1298 | 1298 |
Node s = _g.source(arc); |
| 1299 | 1299 |
Node t = _g.target(arc); |
| 1300 | 1300 |
_left.set(arc, INVALID); |
| 1301 | 1301 |
_right.set(arc, INVALID); |
| 1302 | 1302 |
|
| 1303 | 1303 |
Arc e = _head[s]; |
| 1304 | 1304 |
if (e == INVALID) {
|
| 1305 | 1305 |
_head.set(s, arc); |
| 1306 | 1306 |
_parent.set(arc, INVALID); |
| 1307 | 1307 |
return; |
| 1308 | 1308 |
} |
| 1309 | 1309 |
while (true) {
|
| 1310 | 1310 |
if (t < _g.target(e)) {
|
| 1311 | 1311 |
if (_left[e] == INVALID) {
|
| 1312 | 1312 |
_left.set(e, arc); |
| 1313 | 1313 |
_parent.set(arc, e); |
| 1314 | 1314 |
splay(arc); |
| 1315 | 1315 |
return; |
| 1316 | 1316 |
} else {
|
| 1317 | 1317 |
e = _left[e]; |
| 1318 | 1318 |
} |
| 1319 | 1319 |
} else {
|
| 1320 | 1320 |
if (_right[e] == INVALID) {
|
| 1321 | 1321 |
_right.set(e, arc); |
| 1322 | 1322 |
_parent.set(arc, e); |
| 1323 | 1323 |
splay(arc); |
| 1324 | 1324 |
return; |
| 1325 | 1325 |
} else {
|
| 1326 | 1326 |
e = _right[e]; |
| 1327 | 1327 |
} |
| 1328 | 1328 |
} |
| 1329 | 1329 |
} |
| 1330 | 1330 |
} |
| 1331 | 1331 |
|
| 1332 | 1332 |
void remove(Arc arc) {
|
| 1333 | 1333 |
if (_left[arc] == INVALID) {
|
| 1334 | 1334 |
if (_right[arc] != INVALID) {
|
| 1335 | 1335 |
_parent.set(_right[arc], _parent[arc]); |
| 1336 | 1336 |
} |
| 1337 | 1337 |
if (_parent[arc] != INVALID) {
|
| 1338 | 1338 |
if (_left[_parent[arc]] == arc) {
|
| 1339 | 1339 |
_left.set(_parent[arc], _right[arc]); |
| 1340 | 1340 |
} else {
|
| 1341 | 1341 |
_right.set(_parent[arc], _right[arc]); |
| 1342 | 1342 |
} |
| 1343 | 1343 |
} else {
|
| 1344 | 1344 |
_head.set(_g.source(arc), _right[arc]); |
| 1345 | 1345 |
} |
| 1346 | 1346 |
} else if (_right[arc] == INVALID) {
|
| 1347 | 1347 |
_parent.set(_left[arc], _parent[arc]); |
| 1348 | 1348 |
if (_parent[arc] != INVALID) {
|
| 1349 | 1349 |
if (_left[_parent[arc]] == arc) {
|
| 1350 | 1350 |
_left.set(_parent[arc], _left[arc]); |
| 1351 | 1351 |
} else {
|
| 1352 | 1352 |
_right.set(_parent[arc], _left[arc]); |
| 1353 | 1353 |
} |
| 1354 | 1354 |
} else {
|
| 1355 | 1355 |
_head.set(_g.source(arc), _left[arc]); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
} else {
|
| 1358 | 1358 |
Arc e = _left[arc]; |
| 1359 | 1359 |
if (_right[e] != INVALID) {
|
| 1360 | 1360 |
e = _right[e]; |
| 1361 | 1361 |
while (_right[e] != INVALID) {
|
| 1362 | 1362 |
e = _right[e]; |
| 1363 | 1363 |
} |
| 1364 | 1364 |
Arc s = _parent[e]; |
| 1365 | 1365 |
_right.set(_parent[e], _left[e]); |
| 1366 | 1366 |
if (_left[e] != INVALID) {
|
| 1367 | 1367 |
_parent.set(_left[e], _parent[e]); |
| 1368 | 1368 |
} |
| 1369 | 1369 |
|
| 1370 | 1370 |
_left.set(e, _left[arc]); |
| 1371 | 1371 |
_parent.set(_left[arc], e); |
| 1372 | 1372 |
_right.set(e, _right[arc]); |
| 1373 | 1373 |
_parent.set(_right[arc], e); |
| 1374 | 1374 |
|
| 1375 | 1375 |
_parent.set(e, _parent[arc]); |
| 1376 | 1376 |
if (_parent[arc] != INVALID) {
|
| 1377 | 1377 |
if (_left[_parent[arc]] == arc) {
|
| 1378 | 1378 |
_left.set(_parent[arc], e); |
| 1379 | 1379 |
} else {
|
| 1380 | 1380 |
_right.set(_parent[arc], e); |
| 1381 | 1381 |
} |
| 1382 | 1382 |
} |
| 1383 | 1383 |
splay(s); |
| 1384 | 1384 |
} else {
|
| 1385 | 1385 |
_right.set(e, _right[arc]); |
| 1386 | 1386 |
_parent.set(_right[arc], e); |
| 1387 |
_parent.set(e, _parent[arc]); |
|
| 1387 | 1388 |
|
| 1388 | 1389 |
if (_parent[arc] != INVALID) {
|
| 1389 | 1390 |
if (_left[_parent[arc]] == arc) {
|
| 1390 | 1391 |
_left.set(_parent[arc], e); |
| 1391 | 1392 |
} else {
|
| 1392 | 1393 |
_right.set(_parent[arc], e); |
| 1393 | 1394 |
} |
| 1394 | 1395 |
} else {
|
| 1395 | 1396 |
_head.set(_g.source(arc), e); |
| 1396 | 1397 |
} |
| 1397 | 1398 |
} |
| 1398 | 1399 |
} |
| 1399 | 1400 |
} |
| 1400 | 1401 |
|
| 1401 | 1402 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
| 1402 | 1403 |
{
|
| 1403 | 1404 |
int m=(a+b)/2; |
| 1404 | 1405 |
Arc me=v[m]; |
| 1405 | 1406 |
if (a < m) {
|
| 1406 | 1407 |
Arc left = refreshRec(v,a,m-1); |
| 1407 | 1408 |
_left.set(me, left); |
| 1408 | 1409 |
_parent.set(left, me); |
| 1409 | 1410 |
} else {
|
| 1410 | 1411 |
_left.set(me, INVALID); |
| 1411 | 1412 |
} |
| 1412 | 1413 |
if (m < b) {
|
| 1413 | 1414 |
Arc right = refreshRec(v,m+1,b); |
| 1414 | 1415 |
_right.set(me, right); |
| 1415 | 1416 |
_parent.set(right, me); |
| 1416 | 1417 |
} else {
|
| 1417 | 1418 |
_right.set(me, INVALID); |
| 1418 | 1419 |
} |
| 1419 | 1420 |
return me; |
| 1420 | 1421 |
} |
| 1421 | 1422 |
|
| 1422 | 1423 |
void refresh() {
|
| 1423 | 1424 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1424 | 1425 |
std::vector<Arc> v; |
| 1425 | 1426 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
| 1426 | 1427 |
if(v.size()) {
|
| 1427 | 1428 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
| 1428 | 1429 |
Arc head = refreshRec(v,0,v.size()-1); |
| 1429 | 1430 |
_head.set(n, head); |
| 1430 | 1431 |
_parent.set(head, INVALID); |
| 1431 | 1432 |
} |
| 1432 | 1433 |
else _head.set(n, INVALID); |
| 1433 | 1434 |
} |
| 1434 | 1435 |
} |
| 1435 | 1436 |
|
| 1436 | 1437 |
void zig(Arc v) {
|
| 1437 | 1438 |
Arc w = _parent[v]; |
| 1438 | 1439 |
_parent.set(v, _parent[w]); |
| 1439 | 1440 |
_parent.set(w, v); |
| 1440 | 1441 |
_left.set(w, _right[v]); |
| 1441 | 1442 |
_right.set(v, w); |
| 1442 | 1443 |
if (_parent[v] != INVALID) {
|
| 1443 | 1444 |
if (_right[_parent[v]] == w) {
|
| 1444 | 1445 |
_right.set(_parent[v], v); |
| 1445 | 1446 |
} else {
|
| 1446 | 1447 |
_left.set(_parent[v], v); |
| 1447 | 1448 |
} |
| 1448 | 1449 |
} |
| 1449 | 1450 |
if (_left[w] != INVALID){
|
| 1450 | 1451 |
_parent.set(_left[w], w); |
| 1451 | 1452 |
} |
| 1452 | 1453 |
} |
| 1453 | 1454 |
|
| 1454 | 1455 |
void zag(Arc v) {
|
| 1455 | 1456 |
Arc w = _parent[v]; |
| 1456 | 1457 |
_parent.set(v, _parent[w]); |
| 1457 | 1458 |
_parent.set(w, v); |
| 1458 | 1459 |
_right.set(w, _left[v]); |
| 1459 | 1460 |
_left.set(v, w); |
| 1460 | 1461 |
if (_parent[v] != INVALID){
|
| 1461 | 1462 |
if (_left[_parent[v]] == w) {
|
| 1462 | 1463 |
_left.set(_parent[v], v); |
| 1463 | 1464 |
} else {
|
| 1464 | 1465 |
_right.set(_parent[v], v); |
| 1465 | 1466 |
} |
| 1466 | 1467 |
} |
| 1467 | 1468 |
if (_right[w] != INVALID){
|
| 1468 | 1469 |
_parent.set(_right[w], w); |
| 1469 | 1470 |
} |
| 1470 | 1471 |
} |
| 1471 | 1472 |
|
| 1472 | 1473 |
void splay(Arc v) {
|
| 1473 | 1474 |
while (_parent[v] != INVALID) {
|
| 1474 | 1475 |
if (v == _left[_parent[v]]) {
|
| 1475 | 1476 |
if (_parent[_parent[v]] == INVALID) {
|
| 1476 | 1477 |
zig(v); |
| 1477 | 1478 |
} else {
|
| 1478 | 1479 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1479 | 1480 |
zig(_parent[v]); |
| 1480 | 1481 |
zig(v); |
| 1481 | 1482 |
} else {
|
| 1482 | 1483 |
zig(v); |
| 1483 | 1484 |
zag(v); |
| 1484 | 1485 |
} |
| 1485 | 1486 |
} |
| 1486 | 1487 |
} else {
|
| 1487 | 1488 |
if (_parent[_parent[v]] == INVALID) {
|
| 1488 | 1489 |
zag(v); |
| 1489 | 1490 |
} else {
|
| 1490 | 1491 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1491 | 1492 |
zag(v); |
| 1492 | 1493 |
zig(v); |
| 1493 | 1494 |
} else {
|
| 1494 | 1495 |
zag(_parent[v]); |
| 1495 | 1496 |
zag(v); |
| 1496 | 1497 |
} |
| 1497 | 1498 |
} |
| 1498 | 1499 |
} |
| 1499 | 1500 |
} |
| 1500 | 1501 |
_head[_g.source(v)] = v; |
| 1501 | 1502 |
} |
| 1502 | 1503 |
|
| 1503 | 1504 |
|
| 1504 | 1505 |
public: |
| 1505 | 1506 |
|
| 1506 | 1507 |
///Find an arc between two nodes. |
| 1507 | 1508 |
|
| 1508 | 1509 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
| 1509 | 1510 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
| 1510 | 1511 |
///\param s The source node |
| 1511 | 1512 |
///\param t The target node |
| 1512 | 1513 |
///\return An arc from \c s to \c t if there exists, |
| 1513 | 1514 |
///\ref INVALID otherwise. |
| 1514 | 1515 |
Arc operator()(Node s, Node t) const |
| 1515 | 1516 |
{
|
| 1516 | 1517 |
Arc a = _head[s]; |
| 1518 |
if (a == INVALID) return INVALID; |
|
| 1517 | 1519 |
while (true) {
|
| 1518 | 1520 |
if (_g.target(a) == t) {
|
| 1519 | 1521 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1520 | 1522 |
return a; |
| 1521 | 1523 |
} else if (t < _g.target(a)) {
|
| 1522 | 1524 |
if (_left[a] == INVALID) {
|
| 1523 | 1525 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1524 | 1526 |
return INVALID; |
| 1525 | 1527 |
} else {
|
| 1526 | 1528 |
a = _left[a]; |
| 1527 | 1529 |
} |
| 1528 | 1530 |
} else {
|
| 1529 | 1531 |
if (_right[a] == INVALID) {
|
| 1530 | 1532 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1531 | 1533 |
return INVALID; |
| 1532 | 1534 |
} else {
|
| 1533 | 1535 |
a = _right[a]; |
| 1534 | 1536 |
} |
| 1535 | 1537 |
} |
| 1536 | 1538 |
} |
| 1537 | 1539 |
} |
| 1538 | 1540 |
|
| 1539 | 1541 |
///Find the first arc between two nodes. |
| 1540 | 1542 |
|
| 1541 | 1543 |
///Find the first arc between two nodes in time |
| 1542 | 1544 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
| 1543 | 1545 |
/// outgoing arcs of \c s. |
| 1544 | 1546 |
///\param s The source node |
| 1545 | 1547 |
///\param t The target node |
| 1546 | 1548 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
| 1547 | 1549 |
/// otherwise. |
| 1548 | 1550 |
Arc findFirst(Node s, Node t) const |
| 1549 | 1551 |
{
|
| 1550 | 1552 |
Arc a = _head[s]; |
| 1553 |
if (a == INVALID) return INVALID; |
|
| 1551 | 1554 |
Arc r = INVALID; |
| 1552 | 1555 |
while (true) {
|
| 1553 | 1556 |
if (_g.target(a) < t) {
|
| 1554 | 1557 |
if (_right[a] == INVALID) {
|
| 1555 | 1558 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1556 | 1559 |
return r; |
| 1557 | 1560 |
} else {
|
| 1558 | 1561 |
a = _right[a]; |
| 1559 | 1562 |
} |
| 1560 | 1563 |
} else {
|
| 1561 | 1564 |
if (_g.target(a) == t) {
|
| 1562 | 1565 |
r = a; |
| 1563 | 1566 |
} |
| 1564 | 1567 |
if (_left[a] == INVALID) {
|
| 1565 | 1568 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1566 | 1569 |
return r; |
| 1567 | 1570 |
} else {
|
| 1568 | 1571 |
a = _left[a]; |
| 1569 | 1572 |
} |
| 1570 | 1573 |
} |
| 1571 | 1574 |
} |
| 1572 | 1575 |
} |
| 1573 | 1576 |
|
| 1574 | 1577 |
///Find the next arc between two nodes. |
| 1575 | 1578 |
|
| 1576 | 1579 |
///Find the next arc between two nodes in time |
| 1577 | 1580 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
| 1578 | 1581 |
/// outgoing arcs of \c s. |
| 1579 | 1582 |
///\param s The source node |
| 1580 | 1583 |
///\param t The target node |
| 1581 | 1584 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
| 1582 | 1585 |
/// otherwise. |
| 1583 | 1586 |
|
| 1584 | 1587 |
///\note If \c e is not the result of the previous \c findFirst() |
| 1585 | 1588 |
///operation then the amorized time bound can not be guaranteed. |
| 1586 | 1589 |
#ifdef DOXYGEN |
| 1587 | 1590 |
Arc findNext(Node s, Node t, Arc a) const |
| 1588 | 1591 |
#else |
| 1589 | 1592 |
Arc findNext(Node, Node t, Arc a) const |
| 1590 | 1593 |
#endif |
| 1591 | 1594 |
{
|
| 1592 | 1595 |
if (_right[a] != INVALID) {
|
| 1593 | 1596 |
a = _right[a]; |
| 1594 | 1597 |
while (_left[a] != INVALID) {
|
| 1595 | 1598 |
a = _left[a]; |
| 1596 | 1599 |
} |
| 1597 | 1600 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1598 | 1601 |
} else {
|
| 1599 | 1602 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) {
|
| 1600 | 1603 |
a = _parent[a]; |
| 1601 | 1604 |
} |
| 1602 | 1605 |
if (_parent[a] == INVALID) {
|
| 1603 | 1606 |
return INVALID; |
| 1604 | 1607 |
} else {
|
| 1605 | 1608 |
a = _parent[a]; |
| 1606 | 1609 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1607 | 1610 |
} |
| 1608 | 1611 |
} |
| 1609 | 1612 |
if (_g.target(a) == t) return a; |
| 1610 | 1613 |
else return INVALID; |
| 1611 | 1614 |
} |
| 1612 | 1615 |
|
| 1613 | 1616 |
}; |
| 1614 | 1617 |
|
| 1615 | 1618 |
///Fast arc look up between given endpoints. |
| 1616 | 1619 |
|
| 1617 | 1620 |
///Using this class, you can find an arc in a digraph from a given |
| 1618 | 1621 |
///source to a given target in time <em>O(log d)</em>, |
| 1619 | 1622 |
///where <em>d</em> is the out-degree of the source node. |
| 1620 | 1623 |
/// |
| 1621 | 1624 |
///It is not possible to find \e all parallel arcs between two nodes. |
| 1622 | 1625 |
///Use \ref AllArcLookUp for this purpose. |
| 1623 | 1626 |
/// |
| 1624 | 1627 |
///\warning This class is static, so you should refresh() (or at least |
| 1625 | 1628 |
///refresh(Node)) this data structure |
| 1626 | 1629 |
///whenever the digraph changes. This is a time consuming (superlinearly |
| 1627 | 1630 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
| 1628 | 1631 |
/// |
| 1629 | 1632 |
///\tparam G The type of the underlying digraph. |
| 1630 | 1633 |
/// |
| 1631 | 1634 |
///\sa DynArcLookUp |
| 1632 | 1635 |
///\sa AllArcLookUp |
| 1633 | 1636 |
template<class G> |
| 1634 | 1637 |
class ArcLookUp |
| 1635 | 1638 |
{
|
| 1636 | 1639 |
public: |
| 1637 | 1640 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
| 1638 | 1641 |
typedef G Digraph; |
| 1639 | 1642 |
|
| 1640 | 1643 |
protected: |
| 1641 | 1644 |
const Digraph &_g; |
| 1642 | 1645 |
typename Digraph::template NodeMap<Arc> _head; |
| 1643 | 1646 |
typename Digraph::template ArcMap<Arc> _left; |
| 1644 | 1647 |
typename Digraph::template ArcMap<Arc> _right; |
| 1645 | 1648 |
|
| 1646 | 1649 |
class ArcLess {
|
| 1647 | 1650 |
const Digraph &g; |
| 1648 | 1651 |
public: |
| 1649 | 1652 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1650 | 1653 |
bool operator()(Arc a,Arc b) const |
| 1651 | 1654 |
{
|
| 1652 | 1655 |
return g.target(a)<g.target(b); |
| 1653 | 1656 |
} |
| 1654 | 1657 |
}; |
| 1655 | 1658 |
|
| 1656 | 1659 |
public: |
| 1657 | 1660 |
|
| 1658 | 1661 |
///Constructor |
| 1659 | 1662 |
|
| 1660 | 1663 |
///Constructor. |
| 1661 | 1664 |
/// |
| 1662 | 1665 |
///It builds up the search database, which remains valid until the digraph |
| 1663 | 1666 |
///changes. |
| 1664 | 1667 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
| 1665 | 1668 |
|
| 1666 | 1669 |
private: |
| 1667 | 1670 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
| 1668 | 1671 |
{
|
| 1669 | 1672 |
int m=(a+b)/2; |
| 1670 | 1673 |
Arc me=v[m]; |
| 1671 | 1674 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
| 1672 | 1675 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
| 1673 | 1676 |
return me; |
| 1674 | 1677 |
} |
| 1675 | 1678 |
public: |
| 1676 | 1679 |
///Refresh the data structure at a node. |
| 1677 | 1680 |
|
| 1678 | 1681 |
///Build up the search database of node \c n. |
0 comments (0 inline)