... |
... |
@@ -472,193 +472,193 @@
|
472 |
472 |
///Processes the next arc.
|
473 |
473 |
|
474 |
474 |
///Processes the next arc.
|
475 |
475 |
///
|
476 |
476 |
///\return The processed arc.
|
477 |
477 |
///
|
478 |
478 |
///\pre The stack must not be empty.
|
479 |
479 |
Arc processNextArc()
|
480 |
480 |
{
|
481 |
481 |
Node m;
|
482 |
482 |
Arc e=_stack[_stack_head];
|
483 |
483 |
if(!(*_reached)[m=G->target(e)]) {
|
484 |
484 |
_pred->set(m,e);
|
485 |
485 |
_reached->set(m,true);
|
486 |
486 |
++_stack_head;
|
487 |
487 |
_stack[_stack_head] = OutArcIt(*G, m);
|
488 |
488 |
_dist->set(m,_stack_head);
|
489 |
489 |
}
|
490 |
490 |
else {
|
491 |
491 |
m=G->source(e);
|
492 |
492 |
++_stack[_stack_head];
|
493 |
493 |
}
|
494 |
494 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
|
495 |
495 |
_processed->set(m,true);
|
496 |
496 |
--_stack_head;
|
497 |
497 |
if(_stack_head>=0) {
|
498 |
498 |
m=G->source(_stack[_stack_head]);
|
499 |
499 |
++_stack[_stack_head];
|
500 |
500 |
}
|
501 |
501 |
}
|
502 |
502 |
return e;
|
503 |
503 |
}
|
504 |
504 |
|
505 |
505 |
///Next arc to be processed.
|
506 |
506 |
|
507 |
507 |
///Next arc to be processed.
|
508 |
508 |
///
|
509 |
509 |
///\return The next arc to be processed or \c INVALID if the stack
|
510 |
510 |
///is empty.
|
511 |
511 |
OutArcIt nextArc() const
|
512 |
512 |
{
|
513 |
513 |
return _stack_head>=0?_stack[_stack_head]:INVALID;
|
514 |
514 |
}
|
515 |
515 |
|
516 |
516 |
///Returns \c false if there are nodes to be processed.
|
517 |
517 |
|
518 |
518 |
///Returns \c false if there are nodes to be processed
|
519 |
519 |
///in the queue (stack).
|
520 |
520 |
bool emptyQueue() const { return _stack_head<0; }
|
521 |
521 |
|
522 |
522 |
///Returns the number of the nodes to be processed.
|
523 |
523 |
|
524 |
524 |
///Returns the number of the nodes to be processed
|
525 |
525 |
///in the queue (stack).
|
526 |
526 |
int queueSize() const { return _stack_head+1; }
|
527 |
527 |
|
528 |
528 |
///Executes the algorithm.
|
529 |
529 |
|
530 |
530 |
///Executes the algorithm.
|
531 |
531 |
///
|
532 |
532 |
///This method runs the %DFS algorithm from the root node
|
533 |
533 |
///in order to compute the DFS path to each node.
|
534 |
534 |
///
|
535 |
535 |
/// The algorithm computes
|
536 |
536 |
///- the %DFS tree,
|
537 |
537 |
///- the distance of each node from the root in the %DFS tree.
|
538 |
538 |
///
|
539 |
539 |
///\pre init() must be called and a root node should be
|
540 |
540 |
///added with addSource() before using this function.
|
541 |
541 |
///
|
542 |
542 |
///\note <tt>d.start()</tt> is just a shortcut of the following code.
|
543 |
543 |
///\code
|
544 |
544 |
/// while ( !d.emptyQueue() ) {
|
545 |
545 |
/// d.processNextArc();
|
546 |
546 |
/// }
|
547 |
547 |
///\endcode
|
548 |
548 |
void start()
|
549 |
549 |
{
|
550 |
550 |
while ( !emptyQueue() ) processNextArc();
|
551 |
551 |
}
|
552 |
552 |
|
553 |
553 |
///Executes the algorithm until the given target node is reached.
|
554 |
554 |
|
555 |
555 |
///Executes the algorithm until the given target node is reached.
|
556 |
556 |
///
|
557 |
557 |
///This method runs the %DFS algorithm from the root node
|
558 |
558 |
///in order to compute the DFS path to \c t.
|
559 |
559 |
///
|
560 |
560 |
///The algorithm computes
|
561 |
561 |
///- the %DFS path to \c t,
|
562 |
562 |
///- the distance of \c t from the root in the %DFS tree.
|
563 |
563 |
///
|
564 |
564 |
///\pre init() must be called and a root node should be
|
565 |
565 |
///added with addSource() before using this function.
|
566 |
566 |
void start(Node t)
|
567 |
567 |
{
|
568 |
|
while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
|
|
568 |
while ( !emptyQueue() && !(*_reached)[t] )
|
569 |
569 |
processNextArc();
|
570 |
570 |
}
|
571 |
571 |
|
572 |
572 |
///Executes the algorithm until a condition is met.
|
573 |
573 |
|
574 |
574 |
///Executes the algorithm until a condition is met.
|
575 |
575 |
///
|
576 |
576 |
///This method runs the %DFS algorithm from the root node
|
577 |
577 |
///until an arc \c a with <tt>am[a]</tt> true is found.
|
578 |
578 |
///
|
579 |
579 |
///\param am A \c bool (or convertible) arc map. The algorithm
|
580 |
580 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
|
581 |
581 |
///
|
582 |
582 |
///\return The reached arc \c a with <tt>am[a]</tt> true or
|
583 |
583 |
///\c INVALID if no such arc was found.
|
584 |
584 |
///
|
585 |
585 |
///\pre init() must be called and a root node should be
|
586 |
586 |
///added with addSource() before using this function.
|
587 |
587 |
///
|
588 |
588 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
|
589 |
589 |
///not a node map.
|
590 |
590 |
template<class ArcBoolMap>
|
591 |
591 |
Arc start(const ArcBoolMap &am)
|
592 |
592 |
{
|
593 |
593 |
while ( !emptyQueue() && !am[_stack[_stack_head]] )
|
594 |
594 |
processNextArc();
|
595 |
595 |
return emptyQueue() ? INVALID : _stack[_stack_head];
|
596 |
596 |
}
|
597 |
597 |
|
598 |
598 |
///Runs the algorithm from the given source node.
|
599 |
599 |
|
600 |
600 |
///This method runs the %DFS algorithm from node \c s
|
601 |
601 |
///in order to compute the DFS path to each node.
|
602 |
602 |
///
|
603 |
603 |
///The algorithm computes
|
604 |
604 |
///- the %DFS tree,
|
605 |
605 |
///- the distance of each node from the root in the %DFS tree.
|
606 |
606 |
///
|
607 |
607 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
|
608 |
608 |
///\code
|
609 |
609 |
/// d.init();
|
610 |
610 |
/// d.addSource(s);
|
611 |
611 |
/// d.start();
|
612 |
612 |
///\endcode
|
613 |
613 |
void run(Node s) {
|
614 |
614 |
init();
|
615 |
615 |
addSource(s);
|
616 |
616 |
start();
|
617 |
617 |
}
|
618 |
618 |
|
619 |
619 |
///Finds the %DFS path between \c s and \c t.
|
620 |
620 |
|
621 |
621 |
///This method runs the %DFS algorithm from node \c s
|
622 |
622 |
///in order to compute the DFS path to node \c t
|
623 |
623 |
///(it stops searching when \c t is processed)
|
624 |
624 |
///
|
625 |
625 |
///\return \c true if \c t is reachable form \c s.
|
626 |
626 |
///
|
627 |
627 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is
|
628 |
628 |
///just a shortcut of the following code.
|
629 |
629 |
///\code
|
630 |
630 |
/// d.init();
|
631 |
631 |
/// d.addSource(s);
|
632 |
632 |
/// d.start(t);
|
633 |
633 |
///\endcode
|
634 |
634 |
bool run(Node s,Node t) {
|
635 |
635 |
init();
|
636 |
636 |
addSource(s);
|
637 |
637 |
start(t);
|
638 |
638 |
return reached(t);
|
639 |
639 |
}
|
640 |
640 |
|
641 |
641 |
///Runs the algorithm to visit all nodes in the digraph.
|
642 |
642 |
|
643 |
643 |
///This method runs the %DFS algorithm in order to visit all nodes
|
644 |
644 |
///in the digraph.
|
645 |
645 |
///
|
646 |
646 |
///\note <tt>d.run()</tt> is just a shortcut of the following code.
|
647 |
647 |
///\code
|
648 |
648 |
/// d.init();
|
649 |
649 |
/// for (NodeIt n(digraph); n != INVALID; ++n) {
|
650 |
650 |
/// if (!d.reached(n)) {
|
651 |
651 |
/// d.addSource(n);
|
652 |
652 |
/// d.start();
|
653 |
653 |
/// }
|
654 |
654 |
/// }
|
655 |
655 |
///\endcode
|
656 |
656 |
void run() {
|
657 |
657 |
init();
|
658 |
658 |
for (NodeIt it(*G); it != INVALID; ++it) {
|
659 |
659 |
if (!reached(it)) {
|
660 |
660 |
addSource(it);
|
661 |
661 |
start();
|
662 |
662 |
}
|
663 |
663 |
}
|
664 |
664 |
}
|
... |
... |
@@ -1419,193 +1419,193 @@
|
1419 |
1419 |
}
|
1420 |
1420 |
|
1421 |
1421 |
/// \brief Processes the next arc.
|
1422 |
1422 |
///
|
1423 |
1423 |
/// Processes the next arc.
|
1424 |
1424 |
///
|
1425 |
1425 |
/// \return The processed arc.
|
1426 |
1426 |
///
|
1427 |
1427 |
/// \pre The stack must not be empty.
|
1428 |
1428 |
Arc processNextArc() {
|
1429 |
1429 |
Arc e = _stack[_stack_head];
|
1430 |
1430 |
Node m = _digraph->target(e);
|
1431 |
1431 |
if(!(*_reached)[m]) {
|
1432 |
1432 |
_visitor->discover(e);
|
1433 |
1433 |
_visitor->reach(m);
|
1434 |
1434 |
_reached->set(m, true);
|
1435 |
1435 |
_digraph->firstOut(_stack[++_stack_head], m);
|
1436 |
1436 |
} else {
|
1437 |
1437 |
_visitor->examine(e);
|
1438 |
1438 |
m = _digraph->source(e);
|
1439 |
1439 |
_digraph->nextOut(_stack[_stack_head]);
|
1440 |
1440 |
}
|
1441 |
1441 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
|
1442 |
1442 |
_visitor->leave(m);
|
1443 |
1443 |
--_stack_head;
|
1444 |
1444 |
if (_stack_head >= 0) {
|
1445 |
1445 |
_visitor->backtrack(_stack[_stack_head]);
|
1446 |
1446 |
m = _digraph->source(_stack[_stack_head]);
|
1447 |
1447 |
_digraph->nextOut(_stack[_stack_head]);
|
1448 |
1448 |
} else {
|
1449 |
1449 |
_visitor->stop(m);
|
1450 |
1450 |
}
|
1451 |
1451 |
}
|
1452 |
1452 |
return e;
|
1453 |
1453 |
}
|
1454 |
1454 |
|
1455 |
1455 |
/// \brief Next arc to be processed.
|
1456 |
1456 |
///
|
1457 |
1457 |
/// Next arc to be processed.
|
1458 |
1458 |
///
|
1459 |
1459 |
/// \return The next arc to be processed or INVALID if the stack is
|
1460 |
1460 |
/// empty.
|
1461 |
1461 |
Arc nextArc() const {
|
1462 |
1462 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
|
1463 |
1463 |
}
|
1464 |
1464 |
|
1465 |
1465 |
/// \brief Returns \c false if there are nodes
|
1466 |
1466 |
/// to be processed.
|
1467 |
1467 |
///
|
1468 |
1468 |
/// Returns \c false if there are nodes
|
1469 |
1469 |
/// to be processed in the queue (stack).
|
1470 |
1470 |
bool emptyQueue() const { return _stack_head < 0; }
|
1471 |
1471 |
|
1472 |
1472 |
/// \brief Returns the number of the nodes to be processed.
|
1473 |
1473 |
///
|
1474 |
1474 |
/// Returns the number of the nodes to be processed in the queue (stack).
|
1475 |
1475 |
int queueSize() const { return _stack_head + 1; }
|
1476 |
1476 |
|
1477 |
1477 |
/// \brief Executes the algorithm.
|
1478 |
1478 |
///
|
1479 |
1479 |
/// Executes the algorithm.
|
1480 |
1480 |
///
|
1481 |
1481 |
/// This method runs the %DFS algorithm from the root node
|
1482 |
1482 |
/// in order to compute the %DFS path to each node.
|
1483 |
1483 |
///
|
1484 |
1484 |
/// The algorithm computes
|
1485 |
1485 |
/// - the %DFS tree,
|
1486 |
1486 |
/// - the distance of each node from the root in the %DFS tree.
|
1487 |
1487 |
///
|
1488 |
1488 |
/// \pre init() must be called and a root node should be
|
1489 |
1489 |
/// added with addSource() before using this function.
|
1490 |
1490 |
///
|
1491 |
1491 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code.
|
1492 |
1492 |
/// \code
|
1493 |
1493 |
/// while ( !d.emptyQueue() ) {
|
1494 |
1494 |
/// d.processNextArc();
|
1495 |
1495 |
/// }
|
1496 |
1496 |
/// \endcode
|
1497 |
1497 |
void start() {
|
1498 |
1498 |
while ( !emptyQueue() ) processNextArc();
|
1499 |
1499 |
}
|
1500 |
1500 |
|
1501 |
1501 |
/// \brief Executes the algorithm until the given target node is reached.
|
1502 |
1502 |
///
|
1503 |
1503 |
/// Executes the algorithm until the given target node is reached.
|
1504 |
1504 |
///
|
1505 |
1505 |
/// This method runs the %DFS algorithm from the root node
|
1506 |
1506 |
/// in order to compute the DFS path to \c t.
|
1507 |
1507 |
///
|
1508 |
1508 |
/// The algorithm computes
|
1509 |
1509 |
/// - the %DFS path to \c t,
|
1510 |
1510 |
/// - the distance of \c t from the root in the %DFS tree.
|
1511 |
1511 |
///
|
1512 |
1512 |
/// \pre init() must be called and a root node should be added
|
1513 |
1513 |
/// with addSource() before using this function.
|
1514 |
1514 |
void start(Node t) {
|
1515 |
|
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
|
|
1515 |
while ( !emptyQueue() && !(*_reached)[t] )
|
1516 |
1516 |
processNextArc();
|
1517 |
1517 |
}
|
1518 |
1518 |
|
1519 |
1519 |
/// \brief Executes the algorithm until a condition is met.
|
1520 |
1520 |
///
|
1521 |
1521 |
/// Executes the algorithm until a condition is met.
|
1522 |
1522 |
///
|
1523 |
1523 |
/// This method runs the %DFS algorithm from the root node
|
1524 |
1524 |
/// until an arc \c a with <tt>am[a]</tt> true is found.
|
1525 |
1525 |
///
|
1526 |
1526 |
/// \param am A \c bool (or convertible) arc map. The algorithm
|
1527 |
1527 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
|
1528 |
1528 |
///
|
1529 |
1529 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or
|
1530 |
1530 |
/// \c INVALID if no such arc was found.
|
1531 |
1531 |
///
|
1532 |
1532 |
/// \pre init() must be called and a root node should be added
|
1533 |
1533 |
/// with addSource() before using this function.
|
1534 |
1534 |
///
|
1535 |
1535 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
|
1536 |
1536 |
/// not a node map.
|
1537 |
1537 |
template <typename AM>
|
1538 |
1538 |
Arc start(const AM &am) {
|
1539 |
1539 |
while ( !emptyQueue() && !am[_stack[_stack_head]] )
|
1540 |
1540 |
processNextArc();
|
1541 |
1541 |
return emptyQueue() ? INVALID : _stack[_stack_head];
|
1542 |
1542 |
}
|
1543 |
1543 |
|
1544 |
1544 |
/// \brief Runs the algorithm from the given source node.
|
1545 |
1545 |
///
|
1546 |
1546 |
/// This method runs the %DFS algorithm from node \c s.
|
1547 |
1547 |
/// in order to compute the DFS path to each node.
|
1548 |
1548 |
///
|
1549 |
1549 |
/// The algorithm computes
|
1550 |
1550 |
/// - the %DFS tree,
|
1551 |
1551 |
/// - the distance of each node from the root in the %DFS tree.
|
1552 |
1552 |
///
|
1553 |
1553 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
|
1554 |
1554 |
///\code
|
1555 |
1555 |
/// d.init();
|
1556 |
1556 |
/// d.addSource(s);
|
1557 |
1557 |
/// d.start();
|
1558 |
1558 |
///\endcode
|
1559 |
1559 |
void run(Node s) {
|
1560 |
1560 |
init();
|
1561 |
1561 |
addSource(s);
|
1562 |
1562 |
start();
|
1563 |
1563 |
}
|
1564 |
1564 |
|
1565 |
1565 |
/// \brief Finds the %DFS path between \c s and \c t.
|
1566 |
1566 |
|
1567 |
1567 |
/// This method runs the %DFS algorithm from node \c s
|
1568 |
1568 |
/// in order to compute the DFS path to node \c t
|
1569 |
1569 |
/// (it stops searching when \c t is processed).
|
1570 |
1570 |
///
|
1571 |
1571 |
/// \return \c true if \c t is reachable form \c s.
|
1572 |
1572 |
///
|
1573 |
1573 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is
|
1574 |
1574 |
/// just a shortcut of the following code.
|
1575 |
1575 |
///\code
|
1576 |
1576 |
/// d.init();
|
1577 |
1577 |
/// d.addSource(s);
|
1578 |
1578 |
/// d.start(t);
|
1579 |
1579 |
///\endcode
|
1580 |
1580 |
bool run(Node s,Node t) {
|
1581 |
1581 |
init();
|
1582 |
1582 |
addSource(s);
|
1583 |
1583 |
start(t);
|
1584 |
1584 |
return reached(t);
|
1585 |
1585 |
}
|
1586 |
1586 |
|
1587 |
1587 |
/// \brief Runs the algorithm to visit all nodes in the digraph.
|
1588 |
1588 |
|
1589 |
1589 |
/// This method runs the %DFS algorithm in order to visit all nodes
|
1590 |
1590 |
/// in the digraph.
|
1591 |
1591 |
///
|
1592 |
1592 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code.
|
1593 |
1593 |
///\code
|
1594 |
1594 |
/// d.init();
|
1595 |
1595 |
/// for (NodeIt n(digraph); n != INVALID; ++n) {
|
1596 |
1596 |
/// if (!d.reached(n)) {
|
1597 |
1597 |
/// d.addSource(n);
|
1598 |
1598 |
/// d.start();
|
1599 |
1599 |
/// }
|
1600 |
1600 |
/// }
|
1601 |
1601 |
///\endcode
|
1602 |
1602 |
void run() {
|
1603 |
1603 |
init();
|
1604 |
1604 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
1605 |
1605 |
if (!reached(it)) {
|
1606 |
1606 |
addSource(it);
|
1607 |
1607 |
start();
|
1608 |
1608 |
}
|
1609 |
1609 |
}
|
1610 |
1610 |
}
|
1611 |
1611 |
|