... | ... |
@@ -497,1215 +497,1220 @@ |
497 | 497 |
///\retval reach Indicates if the target node is reached. |
498 | 498 |
///It should be initially \c false. |
499 | 499 |
/// |
500 | 500 |
///\return The processed node. |
501 | 501 |
/// |
502 | 502 |
///\pre The queue must not be empty. |
503 | 503 |
Node processNextNode(Node target, bool& reach) |
504 | 504 |
{ |
505 | 505 |
if(_queue_tail==_queue_next_dist) { |
506 | 506 |
_curr_dist++; |
507 | 507 |
_queue_next_dist=_queue_head; |
508 | 508 |
} |
509 | 509 |
Node n=_queue[_queue_tail++]; |
510 | 510 |
_processed->set(n,true); |
511 | 511 |
Node m; |
512 | 512 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
513 | 513 |
if(!(*_reached)[m=G->target(e)]) { |
514 | 514 |
_queue[_queue_head++]=m; |
515 | 515 |
_reached->set(m,true); |
516 | 516 |
_pred->set(m,e); |
517 | 517 |
_dist->set(m,_curr_dist); |
518 | 518 |
reach = reach || (target == m); |
519 | 519 |
} |
520 | 520 |
return n; |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
///Processes the next node. |
524 | 524 |
|
525 | 525 |
///Processes the next node and checks if at least one of reached |
526 | 526 |
///nodes has \c true value in the \c nm node map. If one node |
527 | 527 |
///with \c true value is reachable from the processed node, then the |
528 | 528 |
///\c rnode parameter will be set to the first of such nodes. |
529 | 529 |
/// |
530 | 530 |
///\param nm A \c bool (or convertible) node map that indicates the |
531 | 531 |
///possible targets. |
532 | 532 |
///\retval rnode The reached target node. |
533 | 533 |
///It should be initially \c INVALID. |
534 | 534 |
/// |
535 | 535 |
///\return The processed node. |
536 | 536 |
/// |
537 | 537 |
///\pre The queue must not be empty. |
538 | 538 |
template<class NM> |
539 | 539 |
Node processNextNode(const NM& nm, Node& rnode) |
540 | 540 |
{ |
541 | 541 |
if(_queue_tail==_queue_next_dist) { |
542 | 542 |
_curr_dist++; |
543 | 543 |
_queue_next_dist=_queue_head; |
544 | 544 |
} |
545 | 545 |
Node n=_queue[_queue_tail++]; |
546 | 546 |
_processed->set(n,true); |
547 | 547 |
Node m; |
548 | 548 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
549 | 549 |
if(!(*_reached)[m=G->target(e)]) { |
550 | 550 |
_queue[_queue_head++]=m; |
551 | 551 |
_reached->set(m,true); |
552 | 552 |
_pred->set(m,e); |
553 | 553 |
_dist->set(m,_curr_dist); |
554 | 554 |
if (nm[m] && rnode == INVALID) rnode = m; |
555 | 555 |
} |
556 | 556 |
return n; |
557 | 557 |
} |
558 | 558 |
|
559 | 559 |
///The next node to be processed. |
560 | 560 |
|
561 | 561 |
///Returns the next node to be processed or \c INVALID if the queue |
562 | 562 |
///is empty. |
563 | 563 |
Node nextNode() const |
564 | 564 |
{ |
565 | 565 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
///\brief Returns \c false if there are nodes |
569 | 569 |
///to be processed. |
570 | 570 |
/// |
571 | 571 |
///Returns \c false if there are nodes |
572 | 572 |
///to be processed in the queue. |
573 | 573 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
574 | 574 |
|
575 | 575 |
///Returns the number of the nodes to be processed. |
576 | 576 |
|
577 | 577 |
///Returns the number of the nodes to be processed in the queue. |
578 | 578 |
int queueSize() const { return _queue_head-_queue_tail; } |
579 | 579 |
|
580 | 580 |
///Executes the algorithm. |
581 | 581 |
|
582 | 582 |
///Executes the algorithm. |
583 | 583 |
/// |
584 | 584 |
///This method runs the %BFS algorithm from the root node(s) |
585 | 585 |
///in order to compute the shortest path to each node. |
586 | 586 |
/// |
587 | 587 |
///The algorithm computes |
588 | 588 |
///- the shortest path tree (forest), |
589 | 589 |
///- the distance of each node from the root(s). |
590 | 590 |
/// |
591 | 591 |
///\pre init() must be called and at least one root node should be |
592 | 592 |
///added with addSource() before using this function. |
593 | 593 |
/// |
594 | 594 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
595 | 595 |
///\code |
596 | 596 |
/// while ( !b.emptyQueue() ) { |
597 | 597 |
/// b.processNextNode(); |
598 | 598 |
/// } |
599 | 599 |
///\endcode |
600 | 600 |
void start() |
601 | 601 |
{ |
602 | 602 |
while ( !emptyQueue() ) processNextNode(); |
603 | 603 |
} |
604 | 604 |
|
605 | 605 |
///Executes the algorithm until the given target node is reached. |
606 | 606 |
|
607 | 607 |
///Executes the algorithm until the given target node is reached. |
608 | 608 |
/// |
609 | 609 |
///This method runs the %BFS algorithm from the root node(s) |
610 | 610 |
///in order to compute the shortest path to \c dest. |
611 | 611 |
/// |
612 | 612 |
///The algorithm computes |
613 | 613 |
///- the shortest path to \c dest, |
614 | 614 |
///- the distance of \c dest from the root(s). |
615 | 615 |
/// |
616 | 616 |
///\pre init() must be called and at least one root node should be |
617 | 617 |
///added with addSource() before using this function. |
618 | 618 |
/// |
619 | 619 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
620 | 620 |
///\code |
621 | 621 |
/// bool reach = false; |
622 | 622 |
/// while ( !b.emptyQueue() && !reach ) { |
623 | 623 |
/// b.processNextNode(t, reach); |
624 | 624 |
/// } |
625 | 625 |
///\endcode |
626 | 626 |
void start(Node dest) |
627 | 627 |
{ |
628 | 628 |
bool reach = false; |
629 | 629 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
630 | 630 |
} |
631 | 631 |
|
632 | 632 |
///Executes the algorithm until a condition is met. |
633 | 633 |
|
634 | 634 |
///Executes the algorithm until a condition is met. |
635 | 635 |
/// |
636 | 636 |
///This method runs the %BFS algorithm from the root node(s) in |
637 | 637 |
///order to compute the shortest path to a node \c v with |
638 | 638 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
639 | 639 |
/// |
640 | 640 |
///\param nm A \c bool (or convertible) node map. The algorithm |
641 | 641 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
642 | 642 |
/// |
643 | 643 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
644 | 644 |
///\c INVALID if no such node was found. |
645 | 645 |
/// |
646 | 646 |
///\pre init() must be called and at least one root node should be |
647 | 647 |
///added with addSource() before using this function. |
648 | 648 |
/// |
649 | 649 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
650 | 650 |
///\code |
651 | 651 |
/// Node rnode = INVALID; |
652 | 652 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
653 | 653 |
/// b.processNextNode(nm, rnode); |
654 | 654 |
/// } |
655 | 655 |
/// return rnode; |
656 | 656 |
///\endcode |
657 | 657 |
template<class NodeBoolMap> |
658 | 658 |
Node start(const NodeBoolMap &nm) |
659 | 659 |
{ |
660 | 660 |
Node rnode = INVALID; |
661 | 661 |
while ( !emptyQueue() && rnode == INVALID ) { |
662 | 662 |
processNextNode(nm, rnode); |
663 | 663 |
} |
664 | 664 |
return rnode; |
665 | 665 |
} |
666 | 666 |
|
667 | 667 |
///Runs the algorithm from the given node. |
668 | 668 |
|
669 | 669 |
///This method runs the %BFS algorithm from node \c s |
670 | 670 |
///in order to compute the shortest path to each node. |
671 | 671 |
/// |
672 | 672 |
///The algorithm computes |
673 | 673 |
///- the shortest path tree, |
674 | 674 |
///- the distance of each node from the root. |
675 | 675 |
/// |
676 | 676 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
677 | 677 |
///\code |
678 | 678 |
/// b.init(); |
679 | 679 |
/// b.addSource(s); |
680 | 680 |
/// b.start(); |
681 | 681 |
///\endcode |
682 | 682 |
void run(Node s) { |
683 | 683 |
init(); |
684 | 684 |
addSource(s); |
685 | 685 |
start(); |
686 | 686 |
} |
687 | 687 |
|
688 | 688 |
///Finds the shortest path between \c s and \c t. |
689 | 689 |
|
690 | 690 |
///This method runs the %BFS algorithm from node \c s |
691 | 691 |
///in order to compute the shortest path to \c t. |
692 | 692 |
/// |
693 | 693 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
694 | 694 |
///if \c t is reachable form \c s, \c 0 otherwise. |
695 | 695 |
/// |
696 | 696 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
697 | 697 |
///shortcut of the following code. |
698 | 698 |
///\code |
699 | 699 |
/// b.init(); |
700 | 700 |
/// b.addSource(s); |
701 | 701 |
/// b.start(t); |
702 | 702 |
///\endcode |
703 | 703 |
int run(Node s,Node t) { |
704 | 704 |
init(); |
705 | 705 |
addSource(s); |
706 | 706 |
start(t); |
707 | 707 |
return reached(t) ? _curr_dist : 0; |
708 | 708 |
} |
709 | 709 |
|
710 | 710 |
///Runs the algorithm to visit all nodes in the digraph. |
711 | 711 |
|
712 | 712 |
///This method runs the %BFS algorithm in order to |
713 | 713 |
///compute the shortest path to each node. |
714 | 714 |
/// |
715 | 715 |
///The algorithm computes |
716 | 716 |
///- the shortest path tree (forest), |
717 | 717 |
///- the distance of each node from the root(s). |
718 | 718 |
/// |
719 | 719 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
720 | 720 |
///\code |
721 | 721 |
/// b.init(); |
722 | 722 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
723 | 723 |
/// if (!b.reached(n)) { |
724 | 724 |
/// b.addSource(n); |
725 | 725 |
/// b.start(); |
726 | 726 |
/// } |
727 | 727 |
/// } |
728 | 728 |
///\endcode |
729 | 729 |
void run() { |
730 | 730 |
init(); |
731 | 731 |
for (NodeIt n(*G); n != INVALID; ++n) { |
732 | 732 |
if (!reached(n)) { |
733 | 733 |
addSource(n); |
734 | 734 |
start(); |
735 | 735 |
} |
736 | 736 |
} |
737 | 737 |
} |
738 | 738 |
|
739 | 739 |
///@} |
740 | 740 |
|
741 | 741 |
///\name Query Functions |
742 | 742 |
///The result of the %BFS algorithm can be obtained using these |
743 | 743 |
///functions.\n |
744 | 744 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
745 | 745 |
///"start()" must be called before using them. |
746 | 746 |
|
747 | 747 |
///@{ |
748 | 748 |
|
749 | 749 |
///The shortest path to a node. |
750 | 750 |
|
751 | 751 |
///Returns the shortest path to a node. |
752 | 752 |
/// |
753 | 753 |
///\warning \c t should be reachable from the root(s). |
754 | 754 |
/// |
755 | 755 |
///\pre Either \ref run() or \ref start() must be called before |
756 | 756 |
///using this function. |
757 | 757 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
758 | 758 |
|
759 | 759 |
///The distance of a node from the root(s). |
760 | 760 |
|
761 | 761 |
///Returns the distance of a node from the root(s). |
762 | 762 |
/// |
763 | 763 |
///\warning If node \c v is not reachable from the root(s), then |
764 | 764 |
///the return value of this function is undefined. |
765 | 765 |
/// |
766 | 766 |
///\pre Either \ref run() or \ref start() must be called before |
767 | 767 |
///using this function. |
768 | 768 |
int dist(Node v) const { return (*_dist)[v]; } |
769 | 769 |
|
770 | 770 |
///Returns the 'previous arc' of the shortest path tree for a node. |
771 | 771 |
|
772 | 772 |
///This function returns the 'previous arc' of the shortest path |
773 | 773 |
///tree for the node \c v, i.e. it returns the last arc of a |
774 | 774 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
775 | 775 |
///is not reachable from the root(s) or if \c v is a root. |
776 | 776 |
/// |
777 | 777 |
///The shortest path tree used here is equal to the shortest path |
778 | 778 |
///tree used in \ref predNode(). |
779 | 779 |
/// |
780 | 780 |
///\pre Either \ref run() or \ref start() must be called before |
781 | 781 |
///using this function. |
782 | 782 |
Arc predArc(Node v) const { return (*_pred)[v];} |
783 | 783 |
|
784 | 784 |
///Returns the 'previous node' of the shortest path tree for a node. |
785 | 785 |
|
786 | 786 |
///This function returns the 'previous node' of the shortest path |
787 | 787 |
///tree for the node \c v, i.e. it returns the last but one node |
788 | 788 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
789 | 789 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
790 | 790 |
/// |
791 | 791 |
///The shortest path tree used here is equal to the shortest path |
792 | 792 |
///tree used in \ref predArc(). |
793 | 793 |
/// |
794 | 794 |
///\pre Either \ref run() or \ref start() must be called before |
795 | 795 |
///using this function. |
796 | 796 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
797 | 797 |
G->source((*_pred)[v]); } |
798 | 798 |
|
799 | 799 |
///\brief Returns a const reference to the node map that stores the |
800 | 800 |
/// distances of the nodes. |
801 | 801 |
/// |
802 | 802 |
///Returns a const reference to the node map that stores the distances |
803 | 803 |
///of the nodes calculated by the algorithm. |
804 | 804 |
/// |
805 | 805 |
///\pre Either \ref run() or \ref init() |
806 | 806 |
///must be called before using this function. |
807 | 807 |
const DistMap &distMap() const { return *_dist;} |
808 | 808 |
|
809 | 809 |
///\brief Returns a const reference to the node map that stores the |
810 | 810 |
///predecessor arcs. |
811 | 811 |
/// |
812 | 812 |
///Returns a const reference to the node map that stores the predecessor |
813 | 813 |
///arcs, which form the shortest path tree. |
814 | 814 |
/// |
815 | 815 |
///\pre Either \ref run() or \ref init() |
816 | 816 |
///must be called before using this function. |
817 | 817 |
const PredMap &predMap() const { return *_pred;} |
818 | 818 |
|
819 | 819 |
///Checks if a node is reachable from the root(s). |
820 | 820 |
|
821 | 821 |
///Returns \c true if \c v is reachable from the root(s). |
822 | 822 |
///\pre Either \ref run() or \ref start() |
823 | 823 |
///must be called before using this function. |
824 | 824 |
bool reached(Node v) const { return (*_reached)[v]; } |
825 | 825 |
|
826 | 826 |
///@} |
827 | 827 |
}; |
828 | 828 |
|
829 | 829 |
///Default traits class of bfs() function. |
830 | 830 |
|
831 | 831 |
///Default traits class of bfs() function. |
832 | 832 |
///\tparam GR Digraph type. |
833 | 833 |
template<class GR> |
834 | 834 |
struct BfsWizardDefaultTraits |
835 | 835 |
{ |
836 | 836 |
///The type of the digraph the algorithm runs on. |
837 | 837 |
typedef GR Digraph; |
838 | 838 |
|
839 | 839 |
///\brief The type of the map that stores the predecessor |
840 | 840 |
///arcs of the shortest paths. |
841 | 841 |
/// |
842 | 842 |
///The type of the map that stores the predecessor |
843 | 843 |
///arcs of the shortest paths. |
844 | 844 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
845 | 845 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
846 | 846 |
///Instantiates a \ref PredMap. |
847 | 847 |
|
848 | 848 |
///This function instantiates a \ref PredMap. |
849 | 849 |
///\param g is the digraph, to which we would like to define the |
850 | 850 |
///\ref PredMap. |
851 | 851 |
///\todo The digraph alone may be insufficient to initialize |
852 | 852 |
#ifdef DOXYGEN |
853 | 853 |
static PredMap *createPredMap(const Digraph &g) |
854 | 854 |
#else |
855 | 855 |
static PredMap *createPredMap(const Digraph &) |
856 | 856 |
#endif |
857 | 857 |
{ |
858 | 858 |
return new PredMap(); |
859 | 859 |
} |
860 | 860 |
|
861 | 861 |
///The type of the map that indicates which nodes are processed. |
862 | 862 |
|
863 | 863 |
///The type of the map that indicates which nodes are processed. |
864 | 864 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
865 | 865 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
866 | 866 |
///Instantiates a \ref ProcessedMap. |
867 | 867 |
|
868 | 868 |
///This function instantiates a \ref ProcessedMap. |
869 | 869 |
///\param g is the digraph, to which |
870 | 870 |
///we would like to define the \ref ProcessedMap. |
871 | 871 |
#ifdef DOXYGEN |
872 | 872 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
873 | 873 |
#else |
874 | 874 |
static ProcessedMap *createProcessedMap(const Digraph &) |
875 | 875 |
#endif |
876 | 876 |
{ |
877 | 877 |
return new ProcessedMap(); |
878 | 878 |
} |
879 | 879 |
|
880 | 880 |
///The type of the map that indicates which nodes are reached. |
881 | 881 |
|
882 | 882 |
///The type of the map that indicates which nodes are reached. |
883 | 883 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
884 | 884 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
885 | 885 |
///Instantiates a \ref ReachedMap. |
886 | 886 |
|
887 | 887 |
///This function instantiates a \ref ReachedMap. |
888 | 888 |
///\param g is the digraph, to which |
889 | 889 |
///we would like to define the \ref ReachedMap. |
890 | 890 |
static ReachedMap *createReachedMap(const Digraph &g) |
891 | 891 |
{ |
892 | 892 |
return new ReachedMap(g); |
893 | 893 |
} |
894 | 894 |
|
895 | 895 |
///The type of the map that stores the distances of the nodes. |
896 | 896 |
|
897 | 897 |
///The type of the map that stores the distances of the nodes. |
898 | 898 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
899 | 899 |
/// |
900 | 900 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
901 | 901 |
///Instantiates a \ref DistMap. |
902 | 902 |
|
903 | 903 |
///This function instantiates a \ref DistMap. |
904 | 904 |
///\param g is the digraph, to which we would like to define |
905 | 905 |
///the \ref DistMap |
906 | 906 |
#ifdef DOXYGEN |
907 | 907 |
static DistMap *createDistMap(const Digraph &g) |
908 | 908 |
#else |
909 | 909 |
static DistMap *createDistMap(const Digraph &) |
910 | 910 |
#endif |
911 | 911 |
{ |
912 | 912 |
return new DistMap(); |
913 | 913 |
} |
914 | 914 |
}; |
915 | 915 |
|
916 | 916 |
/// Default traits class used by \ref BfsWizard |
917 | 917 |
|
918 | 918 |
/// To make it easier to use Bfs algorithm |
919 | 919 |
/// we have created a wizard class. |
920 | 920 |
/// This \ref BfsWizard class needs default traits, |
921 | 921 |
/// as well as the \ref Bfs class. |
922 | 922 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
923 | 923 |
/// \ref BfsWizard class. |
924 | 924 |
template<class GR> |
925 | 925 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
926 | 926 |
{ |
927 | 927 |
|
928 | 928 |
typedef BfsWizardDefaultTraits<GR> Base; |
929 | 929 |
protected: |
930 | 930 |
//The type of the nodes in the digraph. |
931 | 931 |
typedef typename Base::Digraph::Node Node; |
932 | 932 |
|
933 | 933 |
//Pointer to the digraph the algorithm runs on. |
934 | 934 |
void *_g; |
935 | 935 |
//Pointer to the map of reached nodes. |
936 | 936 |
void *_reached; |
937 | 937 |
//Pointer to the map of processed nodes. |
938 | 938 |
void *_processed; |
939 | 939 |
//Pointer to the map of predecessors arcs. |
940 | 940 |
void *_pred; |
941 | 941 |
//Pointer to the map of distances. |
942 | 942 |
void *_dist; |
943 | 943 |
//Pointer to the source node. |
944 | 944 |
Node _source; |
945 | 945 |
|
946 | 946 |
public: |
947 | 947 |
/// Constructor. |
948 | 948 |
|
949 | 949 |
/// This constructor does not require parameters, therefore it initiates |
950 | 950 |
/// all of the attributes to default values (0, INVALID). |
951 | 951 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
952 | 952 |
_dist(0), _source(INVALID) {} |
953 | 953 |
|
954 | 954 |
/// Constructor. |
955 | 955 |
|
956 | 956 |
/// This constructor requires some parameters, |
957 | 957 |
/// listed in the parameters list. |
958 | 958 |
/// Others are initiated to 0. |
959 | 959 |
/// \param g The digraph the algorithm runs on. |
960 | 960 |
/// \param s The source node. |
961 | 961 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
962 | 962 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
963 | 963 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
964 | 964 |
|
965 | 965 |
}; |
966 | 966 |
|
967 | 967 |
/// Auxiliary class for the function type interface of BFS algorithm. |
968 | 968 |
|
969 | 969 |
/// This auxiliary class is created to implement the function type |
970 | 970 |
/// interface of \ref Bfs algorithm. It uses the functions and features |
971 | 971 |
/// of the plain \ref Bfs, but it is much simpler to use it. |
972 | 972 |
/// It should only be used through the \ref bfs() function, which makes |
973 | 973 |
/// it easier to use the algorithm. |
974 | 974 |
/// |
975 | 975 |
/// Simplicity means that the way to change the types defined |
976 | 976 |
/// in the traits class is based on functions that returns the new class |
977 | 977 |
/// and not on templatable built-in classes. |
978 | 978 |
/// When using the plain \ref Bfs |
979 | 979 |
/// the new class with the modified type comes from |
980 | 980 |
/// the original class by using the :: |
981 | 981 |
/// operator. In the case of \ref BfsWizard only |
982 | 982 |
/// a function have to be called, and it will |
983 | 983 |
/// return the needed class. |
984 | 984 |
/// |
985 | 985 |
/// It does not have own \ref run() method. When its \ref run() method |
986 | 986 |
/// is called, it initiates a plain \ref Bfs object, and calls the |
987 | 987 |
/// \ref Bfs::run() method of it. |
988 | 988 |
template<class TR> |
989 | 989 |
class BfsWizard : public TR |
990 | 990 |
{ |
991 | 991 |
typedef TR Base; |
992 | 992 |
|
993 | 993 |
///The type of the digraph the algorithm runs on. |
994 | 994 |
typedef typename TR::Digraph Digraph; |
995 | 995 |
|
996 | 996 |
typedef typename Digraph::Node Node; |
997 | 997 |
typedef typename Digraph::NodeIt NodeIt; |
998 | 998 |
typedef typename Digraph::Arc Arc; |
999 | 999 |
typedef typename Digraph::OutArcIt OutArcIt; |
1000 | 1000 |
|
1001 | 1001 |
///\brief The type of the map that stores the predecessor |
1002 | 1002 |
///arcs of the shortest paths. |
1003 | 1003 |
typedef typename TR::PredMap PredMap; |
1004 | 1004 |
///\brief The type of the map that stores the distances of the nodes. |
1005 | 1005 |
typedef typename TR::DistMap DistMap; |
1006 | 1006 |
///\brief The type of the map that indicates which nodes are reached. |
1007 | 1007 |
typedef typename TR::ReachedMap ReachedMap; |
1008 | 1008 |
///\brief The type of the map that indicates which nodes are processed. |
1009 | 1009 |
typedef typename TR::ProcessedMap ProcessedMap; |
1010 | 1010 |
|
1011 | 1011 |
public: |
1012 | 1012 |
|
1013 | 1013 |
/// Constructor. |
1014 | 1014 |
BfsWizard() : TR() {} |
1015 | 1015 |
|
1016 | 1016 |
/// Constructor that requires parameters. |
1017 | 1017 |
|
1018 | 1018 |
/// Constructor that requires parameters. |
1019 | 1019 |
/// These parameters will be the default values for the traits class. |
1020 | 1020 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
1021 | 1021 |
TR(g,s) {} |
1022 | 1022 |
|
1023 | 1023 |
///Copy constructor |
1024 | 1024 |
BfsWizard(const TR &b) : TR(b) {} |
1025 | 1025 |
|
1026 | 1026 |
~BfsWizard() {} |
1027 | 1027 |
|
1028 | 1028 |
///Runs BFS algorithm from a source node. |
1029 | 1029 |
|
1030 | 1030 |
///Runs BFS algorithm from a source node. |
1031 | 1031 |
///The node can be given with the \ref source() function. |
1032 | 1032 |
void run() |
1033 | 1033 |
{ |
1034 | 1034 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1035 | 1035 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1036 | 1036 |
if(Base::_reached) |
1037 | 1037 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1038 | 1038 |
if(Base::_processed) |
1039 | 1039 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1040 | 1040 |
if(Base::_pred) |
1041 | 1041 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1042 | 1042 |
if(Base::_dist) |
1043 | 1043 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1044 | 1044 |
alg.run(Base::_source); |
1045 | 1045 |
} |
1046 | 1046 |
|
1047 | 1047 |
///Runs BFS algorithm from the given node. |
1048 | 1048 |
|
1049 | 1049 |
///Runs BFS algorithm from the given node. |
1050 | 1050 |
///\param s is the given source. |
1051 | 1051 |
void run(Node s) |
1052 | 1052 |
{ |
1053 | 1053 |
Base::_source=s; |
1054 | 1054 |
run(); |
1055 | 1055 |
} |
1056 | 1056 |
|
1057 | 1057 |
/// Sets the source node, from which the Bfs algorithm runs. |
1058 | 1058 |
|
1059 | 1059 |
/// Sets the source node, from which the Bfs algorithm runs. |
1060 | 1060 |
/// \param s is the source node. |
1061 | 1061 |
BfsWizard<TR> &source(Node s) |
1062 | 1062 |
{ |
1063 | 1063 |
Base::_source=s; |
1064 | 1064 |
return *this; |
1065 | 1065 |
} |
1066 | 1066 |
|
1067 | 1067 |
template<class T> |
1068 | 1068 |
struct DefPredMapBase : public Base { |
1069 | 1069 |
typedef T PredMap; |
1070 | 1070 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1071 | 1071 |
DefPredMapBase(const TR &b) : TR(b) {} |
1072 | 1072 |
}; |
1073 | 1073 |
///\brief \ref named-templ-param "Named parameter" |
1074 | 1074 |
///for setting \ref PredMap object. |
1075 | 1075 |
/// |
1076 | 1076 |
/// \ref named-templ-param "Named parameter" |
1077 | 1077 |
///for setting \ref PredMap object. |
1078 | 1078 |
template<class T> |
1079 | 1079 |
BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
1080 | 1080 |
{ |
1081 | 1081 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1082 | 1082 |
return BfsWizard<DefPredMapBase<T> >(*this); |
1083 | 1083 |
} |
1084 | 1084 |
|
1085 | 1085 |
template<class T> |
1086 | 1086 |
struct DefReachedMapBase : public Base { |
1087 | 1087 |
typedef T ReachedMap; |
1088 | 1088 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1089 | 1089 |
DefReachedMapBase(const TR &b) : TR(b) {} |
1090 | 1090 |
}; |
1091 | 1091 |
///\brief \ref named-templ-param "Named parameter" |
1092 | 1092 |
///for setting \ref ReachedMap object. |
1093 | 1093 |
/// |
1094 | 1094 |
/// \ref named-templ-param "Named parameter" |
1095 | 1095 |
///for setting \ref ReachedMap object. |
1096 | 1096 |
template<class T> |
1097 | 1097 |
BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1098 | 1098 |
{ |
1099 | 1099 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1100 | 1100 |
return BfsWizard<DefReachedMapBase<T> >(*this); |
1101 | 1101 |
} |
1102 | 1102 |
|
1103 | 1103 |
template<class T> |
1104 | 1104 |
struct DefProcessedMapBase : public Base { |
1105 | 1105 |
typedef T ProcessedMap; |
1106 | 1106 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1107 | 1107 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1108 | 1108 |
}; |
1109 | 1109 |
///\brief \ref named-templ-param "Named parameter" |
1110 | 1110 |
///for setting \ref ProcessedMap object. |
1111 | 1111 |
/// |
1112 | 1112 |
/// \ref named-templ-param "Named parameter" |
1113 | 1113 |
///for setting \ref ProcessedMap object. |
1114 | 1114 |
template<class T> |
1115 | 1115 |
BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1116 | 1116 |
{ |
1117 | 1117 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1118 | 1118 |
return BfsWizard<DefProcessedMapBase<T> >(*this); |
1119 | 1119 |
} |
1120 | 1120 |
|
1121 | 1121 |
template<class T> |
1122 | 1122 |
struct DefDistMapBase : public Base { |
1123 | 1123 |
typedef T DistMap; |
1124 | 1124 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1125 | 1125 |
DefDistMapBase(const TR &b) : TR(b) {} |
1126 | 1126 |
}; |
1127 | 1127 |
///\brief \ref named-templ-param "Named parameter" |
1128 | 1128 |
///for setting \ref DistMap object. |
1129 | 1129 |
/// |
1130 | 1130 |
/// \ref named-templ-param "Named parameter" |
1131 | 1131 |
///for setting \ref DistMap object. |
1132 | 1132 |
template<class T> |
1133 | 1133 |
BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1134 | 1134 |
{ |
1135 | 1135 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1136 | 1136 |
return BfsWizard<DefDistMapBase<T> >(*this); |
1137 | 1137 |
} |
1138 | 1138 |
|
1139 | 1139 |
}; |
1140 | 1140 |
|
1141 | 1141 |
///Function type interface for Bfs algorithm. |
1142 | 1142 |
|
1143 | 1143 |
/// \ingroup search |
1144 | 1144 |
///Function type interface for Bfs algorithm. |
1145 | 1145 |
/// |
1146 | 1146 |
///This function also has several |
1147 | 1147 |
///\ref named-templ-func-param "named parameters", |
1148 | 1148 |
///they are declared as the members of class \ref BfsWizard. |
1149 | 1149 |
///The following |
1150 | 1150 |
///example shows how to use these parameters. |
1151 | 1151 |
///\code |
1152 | 1152 |
/// bfs(g,source).predMap(preds).run(); |
1153 | 1153 |
///\endcode |
1154 | 1154 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1155 | 1155 |
///to the end of the parameter list. |
1156 | 1156 |
///\sa BfsWizard |
1157 | 1157 |
///\sa Bfs |
1158 | 1158 |
template<class GR> |
1159 | 1159 |
BfsWizard<BfsWizardBase<GR> > |
1160 | 1160 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1161 | 1161 |
{ |
1162 | 1162 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1163 | 1163 |
} |
1164 | 1164 |
|
1165 | 1165 |
#ifdef DOXYGEN |
1166 | 1166 |
/// \brief Visitor class for BFS. |
1167 | 1167 |
/// |
1168 | 1168 |
/// This class defines the interface of the BfsVisit events, and |
1169 | 1169 |
/// it could be the base of a real visitor class. |
1170 | 1170 |
template <typename _Digraph> |
1171 | 1171 |
struct BfsVisitor { |
1172 | 1172 |
typedef _Digraph Digraph; |
1173 | 1173 |
typedef typename Digraph::Arc Arc; |
1174 | 1174 |
typedef typename Digraph::Node Node; |
1175 | 1175 |
/// \brief Called for the source node(s) of the BFS. |
1176 | 1176 |
/// |
1177 | 1177 |
/// This function is called for the source node(s) of the BFS. |
1178 | 1178 |
void start(const Node& node) {} |
1179 | 1179 |
/// \brief Called when a node is reached first time. |
1180 | 1180 |
/// |
1181 | 1181 |
/// This function is called when a node is reached first time. |
1182 | 1182 |
void reach(const Node& node) {} |
1183 | 1183 |
/// \brief Called when a node is processed. |
1184 | 1184 |
/// |
1185 | 1185 |
/// This function is called when a node is processed. |
1186 | 1186 |
void process(const Node& node) {} |
1187 | 1187 |
/// \brief Called when an arc reaches a new node. |
1188 | 1188 |
/// |
1189 | 1189 |
/// This function is called when the BFS finds an arc whose target node |
1190 | 1190 |
/// is not reached yet. |
1191 | 1191 |
void discover(const Arc& arc) {} |
1192 | 1192 |
/// \brief Called when an arc is examined but its target node is |
1193 | 1193 |
/// already discovered. |
1194 | 1194 |
/// |
1195 | 1195 |
/// This function is called when an arc is examined but its target node is |
1196 | 1196 |
/// already discovered. |
1197 | 1197 |
void examine(const Arc& arc) {} |
1198 | 1198 |
}; |
1199 | 1199 |
#else |
1200 | 1200 |
template <typename _Digraph> |
1201 | 1201 |
struct BfsVisitor { |
1202 | 1202 |
typedef _Digraph Digraph; |
1203 | 1203 |
typedef typename Digraph::Arc Arc; |
1204 | 1204 |
typedef typename Digraph::Node Node; |
1205 | 1205 |
void start(const Node&) {} |
1206 | 1206 |
void reach(const Node&) {} |
1207 | 1207 |
void process(const Node&) {} |
1208 | 1208 |
void discover(const Arc&) {} |
1209 | 1209 |
void examine(const Arc&) {} |
1210 | 1210 |
|
1211 | 1211 |
template <typename _Visitor> |
1212 | 1212 |
struct Constraints { |
1213 | 1213 |
void constraints() { |
1214 | 1214 |
Arc arc; |
1215 | 1215 |
Node node; |
1216 | 1216 |
visitor.start(node); |
1217 | 1217 |
visitor.reach(node); |
1218 | 1218 |
visitor.process(node); |
1219 | 1219 |
visitor.discover(arc); |
1220 | 1220 |
visitor.examine(arc); |
1221 | 1221 |
} |
1222 | 1222 |
_Visitor& visitor; |
1223 | 1223 |
}; |
1224 | 1224 |
}; |
1225 | 1225 |
#endif |
1226 | 1226 |
|
1227 | 1227 |
/// \brief Default traits class of BfsVisit class. |
1228 | 1228 |
/// |
1229 | 1229 |
/// Default traits class of BfsVisit class. |
1230 | 1230 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1231 | 1231 |
template<class _Digraph> |
1232 | 1232 |
struct BfsVisitDefaultTraits { |
1233 | 1233 |
|
1234 | 1234 |
/// \brief The type of the digraph the algorithm runs on. |
1235 | 1235 |
typedef _Digraph Digraph; |
1236 | 1236 |
|
1237 | 1237 |
/// \brief The type of the map that indicates which nodes are reached. |
1238 | 1238 |
/// |
1239 | 1239 |
/// The type of the map that indicates which nodes are reached. |
1240 | 1240 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1241 | 1241 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1242 | 1242 |
|
1243 | 1243 |
/// \brief Instantiates a \ref ReachedMap. |
1244 | 1244 |
/// |
1245 | 1245 |
/// This function instantiates a \ref ReachedMap. |
1246 | 1246 |
/// \param digraph is the digraph, to which |
1247 | 1247 |
/// we would like to define the \ref ReachedMap. |
1248 | 1248 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1249 | 1249 |
return new ReachedMap(digraph); |
1250 | 1250 |
} |
1251 | 1251 |
|
1252 | 1252 |
}; |
1253 | 1253 |
|
1254 | 1254 |
/// \ingroup search |
1255 | 1255 |
/// |
1256 | 1256 |
/// \brief %BFS algorithm class with visitor interface. |
1257 | 1257 |
/// |
1258 | 1258 |
/// This class provides an efficient implementation of the %BFS algorithm |
1259 | 1259 |
/// with visitor interface. |
1260 | 1260 |
/// |
1261 | 1261 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1262 | 1262 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1263 | 1263 |
/// the member functions of the \c Visitor class on every BFS event. |
1264 | 1264 |
/// |
1265 |
/// This interface of the BFS algorithm should be used in special cases |
|
1266 |
/// when extra actions have to be performed in connection with certain |
|
1267 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
|
1268 |
/// instead. |
|
1269 |
/// |
|
1265 | 1270 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1266 | 1271 |
/// The default value is |
1267 | 1272 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1268 | 1273 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
1269 | 1274 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1270 | 1275 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
1271 | 1276 |
/// does not observe the BFS events. If you want to observe the BFS |
1272 | 1277 |
/// events, you should implement your own visitor class. |
1273 | 1278 |
/// \tparam _Traits Traits class to set various data types used by the |
1274 | 1279 |
/// algorithm. The default traits class is |
1275 | 1280 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1276 | 1281 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1277 | 1282 |
/// a BFS visit traits class. |
1278 | 1283 |
#ifdef DOXYGEN |
1279 | 1284 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1280 | 1285 |
#else |
1281 | 1286 |
template <typename _Digraph = ListDigraph, |
1282 | 1287 |
typename _Visitor = BfsVisitor<_Digraph>, |
1283 | 1288 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1284 | 1289 |
#endif |
1285 | 1290 |
class BfsVisit { |
1286 | 1291 |
public: |
1287 | 1292 |
|
1288 | 1293 |
/// \brief \ref Exception for uninitialized parameters. |
1289 | 1294 |
/// |
1290 | 1295 |
/// This error represents problems in the initialization |
1291 | 1296 |
/// of the parameters of the algorithm. |
1292 | 1297 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1293 | 1298 |
public: |
1294 | 1299 |
virtual const char* what() const throw() |
1295 | 1300 |
{ |
1296 | 1301 |
return "lemon::BfsVisit::UninitializedParameter"; |
1297 | 1302 |
} |
1298 | 1303 |
}; |
1299 | 1304 |
|
1300 | 1305 |
///The traits class. |
1301 | 1306 |
typedef _Traits Traits; |
1302 | 1307 |
|
1303 | 1308 |
///The type of the digraph the algorithm runs on. |
1304 | 1309 |
typedef typename Traits::Digraph Digraph; |
1305 | 1310 |
|
1306 | 1311 |
///The visitor type used by the algorithm. |
1307 | 1312 |
typedef _Visitor Visitor; |
1308 | 1313 |
|
1309 | 1314 |
///The type of the map that indicates which nodes are reached. |
1310 | 1315 |
typedef typename Traits::ReachedMap ReachedMap; |
1311 | 1316 |
|
1312 | 1317 |
private: |
1313 | 1318 |
|
1314 | 1319 |
typedef typename Digraph::Node Node; |
1315 | 1320 |
typedef typename Digraph::NodeIt NodeIt; |
1316 | 1321 |
typedef typename Digraph::Arc Arc; |
1317 | 1322 |
typedef typename Digraph::OutArcIt OutArcIt; |
1318 | 1323 |
|
1319 | 1324 |
//Pointer to the underlying digraph. |
1320 | 1325 |
const Digraph *_digraph; |
1321 | 1326 |
//Pointer to the visitor object. |
1322 | 1327 |
Visitor *_visitor; |
1323 | 1328 |
//Pointer to the map of reached status of the nodes. |
1324 | 1329 |
ReachedMap *_reached; |
1325 | 1330 |
//Indicates if _reached is locally allocated (true) or not. |
1326 | 1331 |
bool local_reached; |
1327 | 1332 |
|
1328 | 1333 |
std::vector<typename Digraph::Node> _list; |
1329 | 1334 |
int _list_front, _list_back; |
1330 | 1335 |
|
1331 | 1336 |
///Creates the maps if necessary. |
1332 | 1337 |
///\todo Better memory allocation (instead of new). |
1333 | 1338 |
void create_maps() { |
1334 | 1339 |
if(!_reached) { |
1335 | 1340 |
local_reached = true; |
1336 | 1341 |
_reached = Traits::createReachedMap(*_digraph); |
1337 | 1342 |
} |
1338 | 1343 |
} |
1339 | 1344 |
|
1340 | 1345 |
protected: |
1341 | 1346 |
|
1342 | 1347 |
BfsVisit() {} |
1343 | 1348 |
|
1344 | 1349 |
public: |
1345 | 1350 |
|
1346 | 1351 |
typedef BfsVisit Create; |
1347 | 1352 |
|
1348 | 1353 |
/// \name Named template parameters |
1349 | 1354 |
|
1350 | 1355 |
///@{ |
1351 | 1356 |
template <class T> |
1352 | 1357 |
struct DefReachedMapTraits : public Traits { |
1353 | 1358 |
typedef T ReachedMap; |
1354 | 1359 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1355 | 1360 |
throw UninitializedParameter(); |
1356 | 1361 |
} |
1357 | 1362 |
}; |
1358 | 1363 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1359 | 1364 |
/// ReachedMap type. |
1360 | 1365 |
/// |
1361 | 1366 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1362 | 1367 |
template <class T> |
1363 | 1368 |
struct DefReachedMap : public BfsVisit< Digraph, Visitor, |
1364 | 1369 |
DefReachedMapTraits<T> > { |
1365 | 1370 |
typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1366 | 1371 |
}; |
1367 | 1372 |
///@} |
1368 | 1373 |
|
1369 | 1374 |
public: |
1370 | 1375 |
|
1371 | 1376 |
/// \brief Constructor. |
1372 | 1377 |
/// |
1373 | 1378 |
/// Constructor. |
1374 | 1379 |
/// |
1375 | 1380 |
/// \param digraph The digraph the algorithm runs on. |
1376 | 1381 |
/// \param visitor The visitor object of the algorithm. |
1377 | 1382 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1378 | 1383 |
: _digraph(&digraph), _visitor(&visitor), |
1379 | 1384 |
_reached(0), local_reached(false) {} |
1380 | 1385 |
|
1381 | 1386 |
/// \brief Destructor. |
1382 | 1387 |
~BfsVisit() { |
1383 | 1388 |
if(local_reached) delete _reached; |
1384 | 1389 |
} |
1385 | 1390 |
|
1386 | 1391 |
/// \brief Sets the map that indicates which nodes are reached. |
1387 | 1392 |
/// |
1388 | 1393 |
/// Sets the map that indicates which nodes are reached. |
1389 | 1394 |
/// If you don't use this function before calling \ref run(), |
1390 | 1395 |
/// it will allocate one. The destructor deallocates this |
1391 | 1396 |
/// automatically allocated map, of course. |
1392 | 1397 |
/// \return <tt> (*this) </tt> |
1393 | 1398 |
BfsVisit &reachedMap(ReachedMap &m) { |
1394 | 1399 |
if(local_reached) { |
1395 | 1400 |
delete _reached; |
1396 | 1401 |
local_reached = false; |
1397 | 1402 |
} |
1398 | 1403 |
_reached = &m; |
1399 | 1404 |
return *this; |
1400 | 1405 |
} |
1401 | 1406 |
|
1402 | 1407 |
public: |
1403 | 1408 |
|
1404 | 1409 |
/// \name Execution control |
1405 | 1410 |
/// The simplest way to execute the algorithm is to use |
1406 | 1411 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1407 | 1412 |
/// "run()". |
1408 | 1413 |
/// \n |
1409 | 1414 |
/// If you need more control on the execution, first you must call |
1410 | 1415 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1411 | 1416 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1412 | 1417 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1413 | 1418 |
/// actual path computation. |
1414 | 1419 |
|
1415 | 1420 |
/// @{ |
1416 | 1421 |
|
1417 | 1422 |
/// \brief Initializes the internal data structures. |
1418 | 1423 |
/// |
1419 | 1424 |
/// Initializes the internal data structures. |
1420 | 1425 |
void init() { |
1421 | 1426 |
create_maps(); |
1422 | 1427 |
_list.resize(countNodes(*_digraph)); |
1423 | 1428 |
_list_front = _list_back = -1; |
1424 | 1429 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1425 | 1430 |
_reached->set(u, false); |
1426 | 1431 |
} |
1427 | 1432 |
} |
1428 | 1433 |
|
1429 | 1434 |
/// \brief Adds a new source node. |
1430 | 1435 |
/// |
1431 | 1436 |
/// Adds a new source node to the set of nodes to be processed. |
1432 | 1437 |
void addSource(Node s) { |
1433 | 1438 |
if(!(*_reached)[s]) { |
1434 | 1439 |
_reached->set(s,true); |
1435 | 1440 |
_visitor->start(s); |
1436 | 1441 |
_visitor->reach(s); |
1437 | 1442 |
_list[++_list_back] = s; |
1438 | 1443 |
} |
1439 | 1444 |
} |
1440 | 1445 |
|
1441 | 1446 |
/// \brief Processes the next node. |
1442 | 1447 |
/// |
1443 | 1448 |
/// Processes the next node. |
1444 | 1449 |
/// |
1445 | 1450 |
/// \return The processed node. |
1446 | 1451 |
/// |
1447 | 1452 |
/// \pre The queue must not be empty. |
1448 | 1453 |
Node processNextNode() { |
1449 | 1454 |
Node n = _list[++_list_front]; |
1450 | 1455 |
_visitor->process(n); |
1451 | 1456 |
Arc e; |
1452 | 1457 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1453 | 1458 |
Node m = _digraph->target(e); |
1454 | 1459 |
if (!(*_reached)[m]) { |
1455 | 1460 |
_visitor->discover(e); |
1456 | 1461 |
_visitor->reach(m); |
1457 | 1462 |
_reached->set(m, true); |
1458 | 1463 |
_list[++_list_back] = m; |
1459 | 1464 |
} else { |
1460 | 1465 |
_visitor->examine(e); |
1461 | 1466 |
} |
1462 | 1467 |
} |
1463 | 1468 |
return n; |
1464 | 1469 |
} |
1465 | 1470 |
|
1466 | 1471 |
/// \brief Processes the next node. |
1467 | 1472 |
/// |
1468 | 1473 |
/// Processes the next node and checks if the given target node |
1469 | 1474 |
/// is reached. If the target node is reachable from the processed |
1470 | 1475 |
/// node, then the \c reach parameter will be set to \c true. |
1471 | 1476 |
/// |
1472 | 1477 |
/// \param target The target node. |
1473 | 1478 |
/// \retval reach Indicates if the target node is reached. |
1474 | 1479 |
/// It should be initially \c false. |
1475 | 1480 |
/// |
1476 | 1481 |
/// \return The processed node. |
1477 | 1482 |
/// |
1478 | 1483 |
/// \pre The queue must not be empty. |
1479 | 1484 |
Node processNextNode(Node target, bool& reach) { |
1480 | 1485 |
Node n = _list[++_list_front]; |
1481 | 1486 |
_visitor->process(n); |
1482 | 1487 |
Arc e; |
1483 | 1488 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1484 | 1489 |
Node m = _digraph->target(e); |
1485 | 1490 |
if (!(*_reached)[m]) { |
1486 | 1491 |
_visitor->discover(e); |
1487 | 1492 |
_visitor->reach(m); |
1488 | 1493 |
_reached->set(m, true); |
1489 | 1494 |
_list[++_list_back] = m; |
1490 | 1495 |
reach = reach || (target == m); |
1491 | 1496 |
} else { |
1492 | 1497 |
_visitor->examine(e); |
1493 | 1498 |
} |
1494 | 1499 |
} |
1495 | 1500 |
return n; |
1496 | 1501 |
} |
1497 | 1502 |
|
1498 | 1503 |
/// \brief Processes the next node. |
1499 | 1504 |
/// |
1500 | 1505 |
/// Processes the next node and checks if at least one of reached |
1501 | 1506 |
/// nodes has \c true value in the \c nm node map. If one node |
1502 | 1507 |
/// with \c true value is reachable from the processed node, then the |
1503 | 1508 |
/// \c rnode parameter will be set to the first of such nodes. |
1504 | 1509 |
/// |
1505 | 1510 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1506 | 1511 |
/// possible targets. |
1507 | 1512 |
/// \retval rnode The reached target node. |
1508 | 1513 |
/// It should be initially \c INVALID. |
1509 | 1514 |
/// |
1510 | 1515 |
/// \return The processed node. |
1511 | 1516 |
/// |
1512 | 1517 |
/// \pre The queue must not be empty. |
1513 | 1518 |
template <typename NM> |
1514 | 1519 |
Node processNextNode(const NM& nm, Node& rnode) { |
1515 | 1520 |
Node n = _list[++_list_front]; |
1516 | 1521 |
_visitor->process(n); |
1517 | 1522 |
Arc e; |
1518 | 1523 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1519 | 1524 |
Node m = _digraph->target(e); |
1520 | 1525 |
if (!(*_reached)[m]) { |
1521 | 1526 |
_visitor->discover(e); |
1522 | 1527 |
_visitor->reach(m); |
1523 | 1528 |
_reached->set(m, true); |
1524 | 1529 |
_list[++_list_back] = m; |
1525 | 1530 |
if (nm[m] && rnode == INVALID) rnode = m; |
1526 | 1531 |
} else { |
1527 | 1532 |
_visitor->examine(e); |
1528 | 1533 |
} |
1529 | 1534 |
} |
1530 | 1535 |
return n; |
1531 | 1536 |
} |
1532 | 1537 |
|
1533 | 1538 |
/// \brief The next node to be processed. |
1534 | 1539 |
/// |
1535 | 1540 |
/// Returns the next node to be processed or \c INVALID if the queue |
1536 | 1541 |
/// is empty. |
1537 | 1542 |
Node nextNode() const { |
1538 | 1543 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1539 | 1544 |
} |
1540 | 1545 |
|
1541 | 1546 |
/// \brief Returns \c false if there are nodes |
1542 | 1547 |
/// to be processed. |
1543 | 1548 |
/// |
1544 | 1549 |
/// Returns \c false if there are nodes |
1545 | 1550 |
/// to be processed in the queue. |
1546 | 1551 |
bool emptyQueue() const { return _list_front == _list_back; } |
1547 | 1552 |
|
1548 | 1553 |
/// \brief Returns the number of the nodes to be processed. |
1549 | 1554 |
/// |
1550 | 1555 |
/// Returns the number of the nodes to be processed in the queue. |
1551 | 1556 |
int queueSize() const { return _list_back - _list_front; } |
1552 | 1557 |
|
1553 | 1558 |
/// \brief Executes the algorithm. |
1554 | 1559 |
/// |
1555 | 1560 |
/// Executes the algorithm. |
1556 | 1561 |
/// |
1557 | 1562 |
/// This method runs the %BFS algorithm from the root node(s) |
1558 | 1563 |
/// in order to compute the shortest path to each node. |
1559 | 1564 |
/// |
1560 | 1565 |
/// The algorithm computes |
1561 | 1566 |
/// - the shortest path tree (forest), |
1562 | 1567 |
/// - the distance of each node from the root(s). |
1563 | 1568 |
/// |
1564 | 1569 |
/// \pre init() must be called and at least one root node should be added |
1565 | 1570 |
/// with addSource() before using this function. |
1566 | 1571 |
/// |
1567 | 1572 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1568 | 1573 |
/// \code |
1569 | 1574 |
/// while ( !b.emptyQueue() ) { |
1570 | 1575 |
/// b.processNextNode(); |
1571 | 1576 |
/// } |
1572 | 1577 |
/// \endcode |
1573 | 1578 |
void start() { |
1574 | 1579 |
while ( !emptyQueue() ) processNextNode(); |
1575 | 1580 |
} |
1576 | 1581 |
|
1577 | 1582 |
/// \brief Executes the algorithm until the given target node is reached. |
1578 | 1583 |
/// |
1579 | 1584 |
/// Executes the algorithm until the given target node is reached. |
1580 | 1585 |
/// |
1581 | 1586 |
/// This method runs the %BFS algorithm from the root node(s) |
1582 | 1587 |
/// in order to compute the shortest path to \c dest. |
1583 | 1588 |
/// |
1584 | 1589 |
/// The algorithm computes |
1585 | 1590 |
/// - the shortest path to \c dest, |
1586 | 1591 |
/// - the distance of \c dest from the root(s). |
1587 | 1592 |
/// |
1588 | 1593 |
/// \pre init() must be called and at least one root node should be |
1589 | 1594 |
/// added with addSource() before using this function. |
1590 | 1595 |
/// |
1591 | 1596 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1592 | 1597 |
/// \code |
1593 | 1598 |
/// bool reach = false; |
1594 | 1599 |
/// while ( !b.emptyQueue() && !reach ) { |
1595 | 1600 |
/// b.processNextNode(t, reach); |
1596 | 1601 |
/// } |
1597 | 1602 |
/// \endcode |
1598 | 1603 |
void start(Node dest) { |
1599 | 1604 |
bool reach = false; |
1600 | 1605 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1601 | 1606 |
} |
1602 | 1607 |
|
1603 | 1608 |
/// \brief Executes the algorithm until a condition is met. |
1604 | 1609 |
/// |
1605 | 1610 |
/// Executes the algorithm until a condition is met. |
1606 | 1611 |
/// |
1607 | 1612 |
/// This method runs the %BFS algorithm from the root node(s) in |
1608 | 1613 |
/// order to compute the shortest path to a node \c v with |
1609 | 1614 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1610 | 1615 |
/// |
1611 | 1616 |
/// \param nm must be a bool (or convertible) node map. The |
1612 | 1617 |
/// algorithm will stop when it reaches a node \c v with |
1613 | 1618 |
/// <tt>nm[v]</tt> true. |
1614 | 1619 |
/// |
1615 | 1620 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1616 | 1621 |
/// \c INVALID if no such node was found. |
1617 | 1622 |
/// |
1618 | 1623 |
/// \pre init() must be called and at least one root node should be |
1619 | 1624 |
/// added with addSource() before using this function. |
1620 | 1625 |
/// |
1621 | 1626 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1622 | 1627 |
/// \code |
1623 | 1628 |
/// Node rnode = INVALID; |
1624 | 1629 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1625 | 1630 |
/// b.processNextNode(nm, rnode); |
1626 | 1631 |
/// } |
1627 | 1632 |
/// return rnode; |
1628 | 1633 |
/// \endcode |
1629 | 1634 |
template <typename NM> |
1630 | 1635 |
Node start(const NM &nm) { |
1631 | 1636 |
Node rnode = INVALID; |
1632 | 1637 |
while ( !emptyQueue() && rnode == INVALID ) { |
1633 | 1638 |
processNextNode(nm, rnode); |
1634 | 1639 |
} |
1635 | 1640 |
return rnode; |
1636 | 1641 |
} |
1637 | 1642 |
|
1638 | 1643 |
/// \brief Runs the algorithm from the given node. |
1639 | 1644 |
/// |
1640 | 1645 |
/// This method runs the %BFS algorithm from node \c s |
1641 | 1646 |
/// in order to compute the shortest path to each node. |
1642 | 1647 |
/// |
1643 | 1648 |
/// The algorithm computes |
1644 | 1649 |
/// - the shortest path tree, |
1645 | 1650 |
/// - the distance of each node from the root. |
1646 | 1651 |
/// |
1647 | 1652 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1648 | 1653 |
///\code |
1649 | 1654 |
/// b.init(); |
1650 | 1655 |
/// b.addSource(s); |
1651 | 1656 |
/// b.start(); |
1652 | 1657 |
///\endcode |
1653 | 1658 |
void run(Node s) { |
1654 | 1659 |
init(); |
1655 | 1660 |
addSource(s); |
1656 | 1661 |
start(); |
1657 | 1662 |
} |
1658 | 1663 |
|
1659 | 1664 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1660 | 1665 |
/// |
1661 | 1666 |
/// This method runs the %BFS algorithm in order to |
1662 | 1667 |
/// compute the shortest path to each node. |
1663 | 1668 |
/// |
1664 | 1669 |
/// The algorithm computes |
1665 | 1670 |
/// - the shortest path tree (forest), |
1666 | 1671 |
/// - the distance of each node from the root(s). |
1667 | 1672 |
/// |
1668 | 1673 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1669 | 1674 |
///\code |
1670 | 1675 |
/// b.init(); |
1671 | 1676 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1672 | 1677 |
/// if (!b.reached(n)) { |
1673 | 1678 |
/// b.addSource(n); |
1674 | 1679 |
/// b.start(); |
1675 | 1680 |
/// } |
1676 | 1681 |
/// } |
1677 | 1682 |
///\endcode |
1678 | 1683 |
void run() { |
1679 | 1684 |
init(); |
1680 | 1685 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1681 | 1686 |
if (!reached(it)) { |
1682 | 1687 |
addSource(it); |
1683 | 1688 |
start(); |
1684 | 1689 |
} |
1685 | 1690 |
} |
1686 | 1691 |
} |
1687 | 1692 |
|
1688 | 1693 |
///@} |
1689 | 1694 |
|
1690 | 1695 |
/// \name Query Functions |
1691 | 1696 |
/// The result of the %BFS algorithm can be obtained using these |
1692 | 1697 |
/// functions.\n |
1693 | 1698 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1694 | 1699 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1695 | 1700 |
/// using them. |
1696 | 1701 |
///@{ |
1697 | 1702 |
|
1698 | 1703 |
/// \brief Checks if a node is reachable from the root(s). |
1699 | 1704 |
/// |
1700 | 1705 |
/// Returns \c true if \c v is reachable from the root(s). |
1701 | 1706 |
/// \pre Either \ref run() or \ref start() |
1702 | 1707 |
/// must be called before using this function. |
1703 | 1708 |
bool reached(Node v) { return (*_reached)[v]; } |
1704 | 1709 |
|
1705 | 1710 |
///@} |
1706 | 1711 |
|
1707 | 1712 |
}; |
1708 | 1713 |
|
1709 | 1714 |
} //END OF NAMESPACE LEMON |
1710 | 1715 |
|
1711 | 1716 |
#endif |
... | ... |
@@ -444,1177 +444,1182 @@ |
444 | 444 |
} |
445 | 445 |
} |
446 | 446 |
|
447 | 447 |
///Adds a new source node. |
448 | 448 |
|
449 | 449 |
///Adds a new source node to the set of nodes to be processed. |
450 | 450 |
/// |
451 | 451 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
452 | 452 |
///false results.) |
453 | 453 |
/// |
454 | 454 |
///\warning Distances will be wrong (or at least strange) in case of |
455 | 455 |
///multiple sources. |
456 | 456 |
void addSource(Node s) |
457 | 457 |
{ |
458 | 458 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
459 | 459 |
if(!(*_reached)[s]) |
460 | 460 |
{ |
461 | 461 |
_reached->set(s,true); |
462 | 462 |
_pred->set(s,INVALID); |
463 | 463 |
OutArcIt e(*G,s); |
464 | 464 |
if(e!=INVALID) { |
465 | 465 |
_stack[++_stack_head]=e; |
466 | 466 |
_dist->set(s,_stack_head); |
467 | 467 |
} |
468 | 468 |
else { |
469 | 469 |
_processed->set(s,true); |
470 | 470 |
_dist->set(s,0); |
471 | 471 |
} |
472 | 472 |
} |
473 | 473 |
} |
474 | 474 |
|
475 | 475 |
///Processes the next arc. |
476 | 476 |
|
477 | 477 |
///Processes the next arc. |
478 | 478 |
/// |
479 | 479 |
///\return The processed arc. |
480 | 480 |
/// |
481 | 481 |
///\pre The stack must not be empty. |
482 | 482 |
Arc processNextArc() |
483 | 483 |
{ |
484 | 484 |
Node m; |
485 | 485 |
Arc e=_stack[_stack_head]; |
486 | 486 |
if(!(*_reached)[m=G->target(e)]) { |
487 | 487 |
_pred->set(m,e); |
488 | 488 |
_reached->set(m,true); |
489 | 489 |
++_stack_head; |
490 | 490 |
_stack[_stack_head] = OutArcIt(*G, m); |
491 | 491 |
_dist->set(m,_stack_head); |
492 | 492 |
} |
493 | 493 |
else { |
494 | 494 |
m=G->source(e); |
495 | 495 |
++_stack[_stack_head]; |
496 | 496 |
} |
497 | 497 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
498 | 498 |
_processed->set(m,true); |
499 | 499 |
--_stack_head; |
500 | 500 |
if(_stack_head>=0) { |
501 | 501 |
m=G->source(_stack[_stack_head]); |
502 | 502 |
++_stack[_stack_head]; |
503 | 503 |
} |
504 | 504 |
} |
505 | 505 |
return e; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Next arc to be processed. |
509 | 509 |
|
510 | 510 |
///Next arc to be processed. |
511 | 511 |
/// |
512 | 512 |
///\return The next arc to be processed or \c INVALID if the stack |
513 | 513 |
///is empty. |
514 | 514 |
OutArcIt nextArc() const |
515 | 515 |
{ |
516 | 516 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
///\brief Returns \c false if there are nodes |
520 | 520 |
///to be processed. |
521 | 521 |
/// |
522 | 522 |
///Returns \c false if there are nodes |
523 | 523 |
///to be processed in the queue (stack). |
524 | 524 |
bool emptyQueue() const { return _stack_head<0; } |
525 | 525 |
|
526 | 526 |
///Returns the number of the nodes to be processed. |
527 | 527 |
|
528 | 528 |
///Returns the number of the nodes to be processed in the queue (stack). |
529 | 529 |
int queueSize() const { return _stack_head+1; } |
530 | 530 |
|
531 | 531 |
///Executes the algorithm. |
532 | 532 |
|
533 | 533 |
///Executes the algorithm. |
534 | 534 |
/// |
535 | 535 |
///This method runs the %DFS algorithm from the root node |
536 | 536 |
///in order to compute the DFS path to each node. |
537 | 537 |
/// |
538 | 538 |
/// The algorithm computes |
539 | 539 |
///- the %DFS tree, |
540 | 540 |
///- the distance of each node from the root in the %DFS tree. |
541 | 541 |
/// |
542 | 542 |
///\pre init() must be called and a root node should be |
543 | 543 |
///added with addSource() before using this function. |
544 | 544 |
/// |
545 | 545 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
546 | 546 |
///\code |
547 | 547 |
/// while ( !d.emptyQueue() ) { |
548 | 548 |
/// d.processNextArc(); |
549 | 549 |
/// } |
550 | 550 |
///\endcode |
551 | 551 |
void start() |
552 | 552 |
{ |
553 | 553 |
while ( !emptyQueue() ) processNextArc(); |
554 | 554 |
} |
555 | 555 |
|
556 | 556 |
///Executes the algorithm until the given target node is reached. |
557 | 557 |
|
558 | 558 |
///Executes the algorithm until the given target node is reached. |
559 | 559 |
/// |
560 | 560 |
///This method runs the %DFS algorithm from the root node |
561 | 561 |
///in order to compute the DFS path to \c dest. |
562 | 562 |
/// |
563 | 563 |
///The algorithm computes |
564 | 564 |
///- the %DFS path to \c dest, |
565 | 565 |
///- the distance of \c dest from the root in the %DFS tree. |
566 | 566 |
/// |
567 | 567 |
///\pre init() must be called and a root node should be |
568 | 568 |
///added with addSource() before using this function. |
569 | 569 |
void start(Node dest) |
570 | 570 |
{ |
571 | 571 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) |
572 | 572 |
processNextArc(); |
573 | 573 |
} |
574 | 574 |
|
575 | 575 |
///Executes the algorithm until a condition is met. |
576 | 576 |
|
577 | 577 |
///Executes the algorithm until a condition is met. |
578 | 578 |
/// |
579 | 579 |
///This method runs the %DFS algorithm from the root node |
580 | 580 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
581 | 581 |
/// |
582 | 582 |
///\param am A \c bool (or convertible) arc map. The algorithm |
583 | 583 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
584 | 584 |
/// |
585 | 585 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
586 | 586 |
///\c INVALID if no such arc was found. |
587 | 587 |
/// |
588 | 588 |
///\pre init() must be called and a root node should be |
589 | 589 |
///added with addSource() before using this function. |
590 | 590 |
/// |
591 | 591 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
592 | 592 |
///not a node map. |
593 | 593 |
template<class ArcBoolMap> |
594 | 594 |
Arc start(const ArcBoolMap &am) |
595 | 595 |
{ |
596 | 596 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
597 | 597 |
processNextArc(); |
598 | 598 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
599 | 599 |
} |
600 | 600 |
|
601 | 601 |
///Runs the algorithm from the given node. |
602 | 602 |
|
603 | 603 |
///This method runs the %DFS algorithm from node \c s |
604 | 604 |
///in order to compute the DFS path to each node. |
605 | 605 |
/// |
606 | 606 |
///The algorithm computes |
607 | 607 |
///- the %DFS tree, |
608 | 608 |
///- the distance of each node from the root in the %DFS tree. |
609 | 609 |
/// |
610 | 610 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
611 | 611 |
///\code |
612 | 612 |
/// d.init(); |
613 | 613 |
/// d.addSource(s); |
614 | 614 |
/// d.start(); |
615 | 615 |
///\endcode |
616 | 616 |
void run(Node s) { |
617 | 617 |
init(); |
618 | 618 |
addSource(s); |
619 | 619 |
start(); |
620 | 620 |
} |
621 | 621 |
|
622 | 622 |
///Finds the %DFS path between \c s and \c t. |
623 | 623 |
|
624 | 624 |
///This method runs the %DFS algorithm from node \c s |
625 | 625 |
///in order to compute the DFS path to \c t. |
626 | 626 |
/// |
627 | 627 |
///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
628 | 628 |
///if \c t is reachable form \c s, \c 0 otherwise. |
629 | 629 |
/// |
630 | 630 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
631 | 631 |
///just a shortcut of the following code. |
632 | 632 |
///\code |
633 | 633 |
/// d.init(); |
634 | 634 |
/// d.addSource(s); |
635 | 635 |
/// d.start(t); |
636 | 636 |
///\endcode |
637 | 637 |
int run(Node s,Node t) { |
638 | 638 |
init(); |
639 | 639 |
addSource(s); |
640 | 640 |
start(t); |
641 | 641 |
return reached(t)?_stack_head+1:0; |
642 | 642 |
} |
643 | 643 |
|
644 | 644 |
///Runs the algorithm to visit all nodes in the digraph. |
645 | 645 |
|
646 | 646 |
///This method runs the %DFS algorithm in order to compute the |
647 | 647 |
///%DFS path to each node. |
648 | 648 |
/// |
649 | 649 |
///The algorithm computes |
650 | 650 |
///- the %DFS tree, |
651 | 651 |
///- the distance of each node from the root in the %DFS tree. |
652 | 652 |
/// |
653 | 653 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
654 | 654 |
///\code |
655 | 655 |
/// d.init(); |
656 | 656 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
657 | 657 |
/// if (!d.reached(n)) { |
658 | 658 |
/// d.addSource(n); |
659 | 659 |
/// d.start(); |
660 | 660 |
/// } |
661 | 661 |
/// } |
662 | 662 |
///\endcode |
663 | 663 |
void run() { |
664 | 664 |
init(); |
665 | 665 |
for (NodeIt it(*G); it != INVALID; ++it) { |
666 | 666 |
if (!reached(it)) { |
667 | 667 |
addSource(it); |
668 | 668 |
start(); |
669 | 669 |
} |
670 | 670 |
} |
671 | 671 |
} |
672 | 672 |
|
673 | 673 |
///@} |
674 | 674 |
|
675 | 675 |
///\name Query Functions |
676 | 676 |
///The result of the %DFS algorithm can be obtained using these |
677 | 677 |
///functions.\n |
678 | 678 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
679 | 679 |
///"start()" must be called before using them. |
680 | 680 |
|
681 | 681 |
///@{ |
682 | 682 |
|
683 | 683 |
///The DFS path to a node. |
684 | 684 |
|
685 | 685 |
///Returns the DFS path to a node. |
686 | 686 |
/// |
687 | 687 |
///\warning \c t should be reachable from the root. |
688 | 688 |
/// |
689 | 689 |
///\pre Either \ref run() or \ref start() must be called before |
690 | 690 |
///using this function. |
691 | 691 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
692 | 692 |
|
693 | 693 |
///The distance of a node from the root. |
694 | 694 |
|
695 | 695 |
///Returns the distance of a node from the root. |
696 | 696 |
/// |
697 | 697 |
///\warning If node \c v is not reachable from the root, then |
698 | 698 |
///the return value of this function is undefined. |
699 | 699 |
/// |
700 | 700 |
///\pre Either \ref run() or \ref start() must be called before |
701 | 701 |
///using this function. |
702 | 702 |
int dist(Node v) const { return (*_dist)[v]; } |
703 | 703 |
|
704 | 704 |
///Returns the 'previous arc' of the %DFS tree for a node. |
705 | 705 |
|
706 | 706 |
///This function returns the 'previous arc' of the %DFS tree for the |
707 | 707 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
708 | 708 |
///root to \c v. It is \c INVALID |
709 | 709 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
710 | 710 |
/// |
711 | 711 |
///The %DFS tree used here is equal to the %DFS tree used in |
712 | 712 |
///\ref predNode(). |
713 | 713 |
/// |
714 | 714 |
///\pre Either \ref run() or \ref start() must be called before using |
715 | 715 |
///this function. |
716 | 716 |
Arc predArc(Node v) const { return (*_pred)[v];} |
717 | 717 |
|
718 | 718 |
///Returns the 'previous node' of the %DFS tree. |
719 | 719 |
|
720 | 720 |
///This function returns the 'previous node' of the %DFS |
721 | 721 |
///tree for the node \c v, i.e. it returns the last but one node |
722 | 722 |
///from a %DFS path from the root to \c v. It is \c INVALID |
723 | 723 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
724 | 724 |
/// |
725 | 725 |
///The %DFS tree used here is equal to the %DFS tree used in |
726 | 726 |
///\ref predArc(). |
727 | 727 |
/// |
728 | 728 |
///\pre Either \ref run() or \ref start() must be called before |
729 | 729 |
///using this function. |
730 | 730 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
731 | 731 |
G->source((*_pred)[v]); } |
732 | 732 |
|
733 | 733 |
///\brief Returns a const reference to the node map that stores the |
734 | 734 |
///distances of the nodes. |
735 | 735 |
/// |
736 | 736 |
///Returns a const reference to the node map that stores the |
737 | 737 |
///distances of the nodes calculated by the algorithm. |
738 | 738 |
/// |
739 | 739 |
///\pre Either \ref run() or \ref init() |
740 | 740 |
///must be called before using this function. |
741 | 741 |
const DistMap &distMap() const { return *_dist;} |
742 | 742 |
|
743 | 743 |
///\brief Returns a const reference to the node map that stores the |
744 | 744 |
///predecessor arcs. |
745 | 745 |
/// |
746 | 746 |
///Returns a const reference to the node map that stores the predecessor |
747 | 747 |
///arcs, which form the DFS tree. |
748 | 748 |
/// |
749 | 749 |
///\pre Either \ref run() or \ref init() |
750 | 750 |
///must be called before using this function. |
751 | 751 |
const PredMap &predMap() const { return *_pred;} |
752 | 752 |
|
753 | 753 |
///Checks if a node is reachable from the root(s). |
754 | 754 |
|
755 | 755 |
///Returns \c true if \c v is reachable from the root(s). |
756 | 756 |
///\pre Either \ref run() or \ref start() |
757 | 757 |
///must be called before using this function. |
758 | 758 |
bool reached(Node v) const { return (*_reached)[v]; } |
759 | 759 |
|
760 | 760 |
///@} |
761 | 761 |
}; |
762 | 762 |
|
763 | 763 |
///Default traits class of dfs() function. |
764 | 764 |
|
765 | 765 |
///Default traits class of dfs() function. |
766 | 766 |
///\tparam GR Digraph type. |
767 | 767 |
template<class GR> |
768 | 768 |
struct DfsWizardDefaultTraits |
769 | 769 |
{ |
770 | 770 |
///The type of the digraph the algorithm runs on. |
771 | 771 |
typedef GR Digraph; |
772 | 772 |
|
773 | 773 |
///\brief The type of the map that stores the predecessor |
774 | 774 |
///arcs of the %DFS paths. |
775 | 775 |
/// |
776 | 776 |
///The type of the map that stores the predecessor |
777 | 777 |
///arcs of the %DFS paths. |
778 | 778 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
779 | 779 |
/// |
780 | 780 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
781 | 781 |
///Instantiates a \ref PredMap. |
782 | 782 |
|
783 | 783 |
///This function instantiates a \ref PredMap. |
784 | 784 |
///\param g is the digraph, to which we would like to define the |
785 | 785 |
///\ref PredMap. |
786 | 786 |
///\todo The digraph alone may be insufficient to initialize |
787 | 787 |
#ifdef DOXYGEN |
788 | 788 |
static PredMap *createPredMap(const Digraph &g) |
789 | 789 |
#else |
790 | 790 |
static PredMap *createPredMap(const Digraph &) |
791 | 791 |
#endif |
792 | 792 |
{ |
793 | 793 |
return new PredMap(); |
794 | 794 |
} |
795 | 795 |
|
796 | 796 |
///The type of the map that indicates which nodes are processed. |
797 | 797 |
|
798 | 798 |
///The type of the map that indicates which nodes are processed. |
799 | 799 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
800 | 800 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
801 | 801 |
///Instantiates a \ref ProcessedMap. |
802 | 802 |
|
803 | 803 |
///This function instantiates a \ref ProcessedMap. |
804 | 804 |
///\param g is the digraph, to which |
805 | 805 |
///we would like to define the \ref ProcessedMap. |
806 | 806 |
#ifdef DOXYGEN |
807 | 807 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
808 | 808 |
#else |
809 | 809 |
static ProcessedMap *createProcessedMap(const Digraph &) |
810 | 810 |
#endif |
811 | 811 |
{ |
812 | 812 |
return new ProcessedMap(); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
///The type of the map that indicates which nodes are reached. |
816 | 816 |
|
817 | 817 |
///The type of the map that indicates which nodes are reached. |
818 | 818 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
819 | 819 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
820 | 820 |
///Instantiates a \ref ReachedMap. |
821 | 821 |
|
822 | 822 |
///This function instantiates a \ref ReachedMap. |
823 | 823 |
///\param g is the digraph, to which |
824 | 824 |
///we would like to define the \ref ReachedMap. |
825 | 825 |
static ReachedMap *createReachedMap(const Digraph &g) |
826 | 826 |
{ |
827 | 827 |
return new ReachedMap(g); |
828 | 828 |
} |
829 | 829 |
|
830 | 830 |
///The type of the map that stores the distances of the nodes. |
831 | 831 |
|
832 | 832 |
///The type of the map that stores the distances of the nodes. |
833 | 833 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
834 | 834 |
/// |
835 | 835 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
836 | 836 |
///Instantiates a \ref DistMap. |
837 | 837 |
|
838 | 838 |
///This function instantiates a \ref DistMap. |
839 | 839 |
///\param g is the digraph, to which we would like to define |
840 | 840 |
///the \ref DistMap |
841 | 841 |
#ifdef DOXYGEN |
842 | 842 |
static DistMap *createDistMap(const Digraph &g) |
843 | 843 |
#else |
844 | 844 |
static DistMap *createDistMap(const Digraph &) |
845 | 845 |
#endif |
846 | 846 |
{ |
847 | 847 |
return new DistMap(); |
848 | 848 |
} |
849 | 849 |
}; |
850 | 850 |
|
851 | 851 |
/// Default traits class used by \ref DfsWizard |
852 | 852 |
|
853 | 853 |
/// To make it easier to use Dfs algorithm |
854 | 854 |
/// we have created a wizard class. |
855 | 855 |
/// This \ref DfsWizard class needs default traits, |
856 | 856 |
/// as well as the \ref Dfs class. |
857 | 857 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
858 | 858 |
/// \ref DfsWizard class. |
859 | 859 |
template<class GR> |
860 | 860 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
861 | 861 |
{ |
862 | 862 |
|
863 | 863 |
typedef DfsWizardDefaultTraits<GR> Base; |
864 | 864 |
protected: |
865 | 865 |
//The type of the nodes in the digraph. |
866 | 866 |
typedef typename Base::Digraph::Node Node; |
867 | 867 |
|
868 | 868 |
//Pointer to the digraph the algorithm runs on. |
869 | 869 |
void *_g; |
870 | 870 |
//Pointer to the map of reached nodes. |
871 | 871 |
void *_reached; |
872 | 872 |
//Pointer to the map of processed nodes. |
873 | 873 |
void *_processed; |
874 | 874 |
//Pointer to the map of predecessors arcs. |
875 | 875 |
void *_pred; |
876 | 876 |
//Pointer to the map of distances. |
877 | 877 |
void *_dist; |
878 | 878 |
//Pointer to the source node. |
879 | 879 |
Node _source; |
880 | 880 |
|
881 | 881 |
public: |
882 | 882 |
/// Constructor. |
883 | 883 |
|
884 | 884 |
/// This constructor does not require parameters, therefore it initiates |
885 | 885 |
/// all of the attributes to default values (0, INVALID). |
886 | 886 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
887 | 887 |
_dist(0), _source(INVALID) {} |
888 | 888 |
|
889 | 889 |
/// Constructor. |
890 | 890 |
|
891 | 891 |
/// This constructor requires some parameters, |
892 | 892 |
/// listed in the parameters list. |
893 | 893 |
/// Others are initiated to 0. |
894 | 894 |
/// \param g The digraph the algorithm runs on. |
895 | 895 |
/// \param s The source node. |
896 | 896 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
897 | 897 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
898 | 898 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
899 | 899 |
|
900 | 900 |
}; |
901 | 901 |
|
902 | 902 |
/// Auxiliary class for the function type interface of DFS algorithm. |
903 | 903 |
|
904 | 904 |
/// This auxiliary class is created to implement the function type |
905 | 905 |
/// interface of \ref Dfs algorithm. It uses the functions and features |
906 | 906 |
/// of the plain \ref Dfs, but it is much simpler to use it. |
907 | 907 |
/// It should only be used through the \ref dfs() function, which makes |
908 | 908 |
/// it easier to use the algorithm. |
909 | 909 |
/// |
910 | 910 |
/// Simplicity means that the way to change the types defined |
911 | 911 |
/// in the traits class is based on functions that returns the new class |
912 | 912 |
/// and not on templatable built-in classes. |
913 | 913 |
/// When using the plain \ref Dfs |
914 | 914 |
/// the new class with the modified type comes from |
915 | 915 |
/// the original class by using the :: |
916 | 916 |
/// operator. In the case of \ref DfsWizard only |
917 | 917 |
/// a function have to be called, and it will |
918 | 918 |
/// return the needed class. |
919 | 919 |
/// |
920 | 920 |
/// It does not have own \ref run() method. When its \ref run() method |
921 | 921 |
/// is called, it initiates a plain \ref Dfs object, and calls the |
922 | 922 |
/// \ref Dfs::run() method of it. |
923 | 923 |
template<class TR> |
924 | 924 |
class DfsWizard : public TR |
925 | 925 |
{ |
926 | 926 |
typedef TR Base; |
927 | 927 |
|
928 | 928 |
///The type of the digraph the algorithm runs on. |
929 | 929 |
typedef typename TR::Digraph Digraph; |
930 | 930 |
|
931 | 931 |
typedef typename Digraph::Node Node; |
932 | 932 |
typedef typename Digraph::NodeIt NodeIt; |
933 | 933 |
typedef typename Digraph::Arc Arc; |
934 | 934 |
typedef typename Digraph::OutArcIt OutArcIt; |
935 | 935 |
|
936 | 936 |
///\brief The type of the map that stores the predecessor |
937 | 937 |
///arcs of the shortest paths. |
938 | 938 |
typedef typename TR::PredMap PredMap; |
939 | 939 |
///\brief The type of the map that stores the distances of the nodes. |
940 | 940 |
typedef typename TR::DistMap DistMap; |
941 | 941 |
///\brief The type of the map that indicates which nodes are reached. |
942 | 942 |
typedef typename TR::ReachedMap ReachedMap; |
943 | 943 |
///\brief The type of the map that indicates which nodes are processed. |
944 | 944 |
typedef typename TR::ProcessedMap ProcessedMap; |
945 | 945 |
|
946 | 946 |
public: |
947 | 947 |
|
948 | 948 |
/// Constructor. |
949 | 949 |
DfsWizard() : TR() {} |
950 | 950 |
|
951 | 951 |
/// Constructor that requires parameters. |
952 | 952 |
|
953 | 953 |
/// Constructor that requires parameters. |
954 | 954 |
/// These parameters will be the default values for the traits class. |
955 | 955 |
DfsWizard(const Digraph &g, Node s=INVALID) : |
956 | 956 |
TR(g,s) {} |
957 | 957 |
|
958 | 958 |
///Copy constructor |
959 | 959 |
DfsWizard(const TR &b) : TR(b) {} |
960 | 960 |
|
961 | 961 |
~DfsWizard() {} |
962 | 962 |
|
963 | 963 |
///Runs DFS algorithm from a source node. |
964 | 964 |
|
965 | 965 |
///Runs DFS algorithm from a source node. |
966 | 966 |
///The node can be given with the \ref source() function. |
967 | 967 |
void run() |
968 | 968 |
{ |
969 | 969 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
970 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
971 | 971 |
if(Base::_reached) |
972 | 972 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
973 | 973 |
if(Base::_processed) |
974 | 974 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
975 | 975 |
if(Base::_pred) |
976 | 976 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
977 | 977 |
if(Base::_dist) |
978 | 978 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
979 | 979 |
alg.run(Base::_source); |
980 | 980 |
} |
981 | 981 |
|
982 | 982 |
///Runs DFS algorithm from the given node. |
983 | 983 |
|
984 | 984 |
///Runs DFS algorithm from the given node. |
985 | 985 |
///\param s is the given source. |
986 | 986 |
void run(Node s) |
987 | 987 |
{ |
988 | 988 |
Base::_source=s; |
989 | 989 |
run(); |
990 | 990 |
} |
991 | 991 |
|
992 | 992 |
/// Sets the source node, from which the Dfs algorithm runs. |
993 | 993 |
|
994 | 994 |
/// Sets the source node, from which the Dfs algorithm runs. |
995 | 995 |
/// \param s is the source node. |
996 | 996 |
DfsWizard<TR> &source(Node s) |
997 | 997 |
{ |
998 | 998 |
Base::_source=s; |
999 | 999 |
return *this; |
1000 | 1000 |
} |
1001 | 1001 |
|
1002 | 1002 |
template<class T> |
1003 | 1003 |
struct DefPredMapBase : public Base { |
1004 | 1004 |
typedef T PredMap; |
1005 | 1005 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1006 | 1006 |
DefPredMapBase(const TR &b) : TR(b) {} |
1007 | 1007 |
}; |
1008 | 1008 |
///\brief \ref named-templ-param "Named parameter" |
1009 | 1009 |
///for setting \ref PredMap object. |
1010 | 1010 |
/// |
1011 | 1011 |
///\ref named-templ-param "Named parameter" |
1012 | 1012 |
///for setting \ref PredMap object. |
1013 | 1013 |
template<class T> |
1014 | 1014 |
DfsWizard<DefPredMapBase<T> > predMap(const T &t) |
1015 | 1015 |
{ |
1016 | 1016 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1017 | 1017 |
return DfsWizard<DefPredMapBase<T> >(*this); |
1018 | 1018 |
} |
1019 | 1019 |
|
1020 | 1020 |
template<class T> |
1021 | 1021 |
struct DefReachedMapBase : public Base { |
1022 | 1022 |
typedef T ReachedMap; |
1023 | 1023 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1024 | 1024 |
DefReachedMapBase(const TR &b) : TR(b) {} |
1025 | 1025 |
}; |
1026 | 1026 |
///\brief \ref named-templ-param "Named parameter" |
1027 | 1027 |
///for setting \ref ReachedMap object. |
1028 | 1028 |
/// |
1029 | 1029 |
/// \ref named-templ-param "Named parameter" |
1030 | 1030 |
///for setting \ref ReachedMap object. |
1031 | 1031 |
template<class T> |
1032 | 1032 |
DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1033 | 1033 |
{ |
1034 | 1034 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1035 | 1035 |
return DfsWizard<DefReachedMapBase<T> >(*this); |
1036 | 1036 |
} |
1037 | 1037 |
|
1038 | 1038 |
template<class T> |
1039 | 1039 |
struct DefProcessedMapBase : public Base { |
1040 | 1040 |
typedef T ProcessedMap; |
1041 | 1041 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1042 | 1042 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1043 | 1043 |
}; |
1044 | 1044 |
///\brief \ref named-templ-param "Named parameter" |
1045 | 1045 |
///for setting \ref ProcessedMap object. |
1046 | 1046 |
/// |
1047 | 1047 |
/// \ref named-templ-param "Named parameter" |
1048 | 1048 |
///for setting \ref ProcessedMap object. |
1049 | 1049 |
template<class T> |
1050 | 1050 |
DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1051 | 1051 |
{ |
1052 | 1052 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1053 | 1053 |
return DfsWizard<DefProcessedMapBase<T> >(*this); |
1054 | 1054 |
} |
1055 | 1055 |
|
1056 | 1056 |
template<class T> |
1057 | 1057 |
struct DefDistMapBase : public Base { |
1058 | 1058 |
typedef T DistMap; |
1059 | 1059 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1060 | 1060 |
DefDistMapBase(const TR &b) : TR(b) {} |
1061 | 1061 |
}; |
1062 | 1062 |
///\brief \ref named-templ-param "Named parameter" |
1063 | 1063 |
///for setting \ref DistMap object. |
1064 | 1064 |
/// |
1065 | 1065 |
///\ref named-templ-param "Named parameter" |
1066 | 1066 |
///for setting \ref DistMap object. |
1067 | 1067 |
template<class T> |
1068 | 1068 |
DfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1069 | 1069 |
{ |
1070 | 1070 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1071 | 1071 |
return DfsWizard<DefDistMapBase<T> >(*this); |
1072 | 1072 |
} |
1073 | 1073 |
|
1074 | 1074 |
}; |
1075 | 1075 |
|
1076 | 1076 |
///Function type interface for Dfs algorithm. |
1077 | 1077 |
|
1078 | 1078 |
///\ingroup search |
1079 | 1079 |
///Function type interface for Dfs algorithm. |
1080 | 1080 |
/// |
1081 | 1081 |
///This function also has several |
1082 | 1082 |
///\ref named-templ-func-param "named parameters", |
1083 | 1083 |
///they are declared as the members of class \ref DfsWizard. |
1084 | 1084 |
///The following |
1085 | 1085 |
///example shows how to use these parameters. |
1086 | 1086 |
///\code |
1087 | 1087 |
/// dfs(g,source).predMap(preds).run(); |
1088 | 1088 |
///\endcode |
1089 | 1089 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1090 | 1090 |
///to the end of the parameter list. |
1091 | 1091 |
///\sa DfsWizard |
1092 | 1092 |
///\sa Dfs |
1093 | 1093 |
template<class GR> |
1094 | 1094 |
DfsWizard<DfsWizardBase<GR> > |
1095 | 1095 |
dfs(const GR &g,typename GR::Node s=INVALID) |
1096 | 1096 |
{ |
1097 | 1097 |
return DfsWizard<DfsWizardBase<GR> >(g,s); |
1098 | 1098 |
} |
1099 | 1099 |
|
1100 | 1100 |
#ifdef DOXYGEN |
1101 | 1101 |
/// \brief Visitor class for DFS. |
1102 | 1102 |
/// |
1103 | 1103 |
/// This class defines the interface of the DfsVisit events, and |
1104 | 1104 |
/// it could be the base of a real visitor class. |
1105 | 1105 |
template <typename _Digraph> |
1106 | 1106 |
struct DfsVisitor { |
1107 | 1107 |
typedef _Digraph Digraph; |
1108 | 1108 |
typedef typename Digraph::Arc Arc; |
1109 | 1109 |
typedef typename Digraph::Node Node; |
1110 | 1110 |
/// \brief Called for the source node of the DFS. |
1111 | 1111 |
/// |
1112 | 1112 |
/// This function is called for the source node of the DFS. |
1113 | 1113 |
void start(const Node& node) {} |
1114 | 1114 |
/// \brief Called when the source node is leaved. |
1115 | 1115 |
/// |
1116 | 1116 |
/// This function is called when the source node is leaved. |
1117 | 1117 |
void stop(const Node& node) {} |
1118 | 1118 |
/// \brief Called when a node is reached first time. |
1119 | 1119 |
/// |
1120 | 1120 |
/// This function is called when a node is reached first time. |
1121 | 1121 |
void reach(const Node& node) {} |
1122 | 1122 |
/// \brief Called when an arc reaches a new node. |
1123 | 1123 |
/// |
1124 | 1124 |
/// This function is called when the DFS finds an arc whose target node |
1125 | 1125 |
/// is not reached yet. |
1126 | 1126 |
void discover(const Arc& arc) {} |
1127 | 1127 |
/// \brief Called when an arc is examined but its target node is |
1128 | 1128 |
/// already discovered. |
1129 | 1129 |
/// |
1130 | 1130 |
/// This function is called when an arc is examined but its target node is |
1131 | 1131 |
/// already discovered. |
1132 | 1132 |
void examine(const Arc& arc) {} |
1133 | 1133 |
/// \brief Called when the DFS steps back from a node. |
1134 | 1134 |
/// |
1135 | 1135 |
/// This function is called when the DFS steps back from a node. |
1136 | 1136 |
void leave(const Node& node) {} |
1137 | 1137 |
/// \brief Called when the DFS steps back on an arc. |
1138 | 1138 |
/// |
1139 | 1139 |
/// This function is called when the DFS steps back on an arc. |
1140 | 1140 |
void backtrack(const Arc& arc) {} |
1141 | 1141 |
}; |
1142 | 1142 |
#else |
1143 | 1143 |
template <typename _Digraph> |
1144 | 1144 |
struct DfsVisitor { |
1145 | 1145 |
typedef _Digraph Digraph; |
1146 | 1146 |
typedef typename Digraph::Arc Arc; |
1147 | 1147 |
typedef typename Digraph::Node Node; |
1148 | 1148 |
void start(const Node&) {} |
1149 | 1149 |
void stop(const Node&) {} |
1150 | 1150 |
void reach(const Node&) {} |
1151 | 1151 |
void discover(const Arc&) {} |
1152 | 1152 |
void examine(const Arc&) {} |
1153 | 1153 |
void leave(const Node&) {} |
1154 | 1154 |
void backtrack(const Arc&) {} |
1155 | 1155 |
|
1156 | 1156 |
template <typename _Visitor> |
1157 | 1157 |
struct Constraints { |
1158 | 1158 |
void constraints() { |
1159 | 1159 |
Arc arc; |
1160 | 1160 |
Node node; |
1161 | 1161 |
visitor.start(node); |
1162 | 1162 |
visitor.stop(arc); |
1163 | 1163 |
visitor.reach(node); |
1164 | 1164 |
visitor.discover(arc); |
1165 | 1165 |
visitor.examine(arc); |
1166 | 1166 |
visitor.leave(node); |
1167 | 1167 |
visitor.backtrack(arc); |
1168 | 1168 |
} |
1169 | 1169 |
_Visitor& visitor; |
1170 | 1170 |
}; |
1171 | 1171 |
}; |
1172 | 1172 |
#endif |
1173 | 1173 |
|
1174 | 1174 |
/// \brief Default traits class of DfsVisit class. |
1175 | 1175 |
/// |
1176 | 1176 |
/// Default traits class of DfsVisit class. |
1177 | 1177 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1178 | 1178 |
template<class _Digraph> |
1179 | 1179 |
struct DfsVisitDefaultTraits { |
1180 | 1180 |
|
1181 | 1181 |
/// \brief The type of the digraph the algorithm runs on. |
1182 | 1182 |
typedef _Digraph Digraph; |
1183 | 1183 |
|
1184 | 1184 |
/// \brief The type of the map that indicates which nodes are reached. |
1185 | 1185 |
/// |
1186 | 1186 |
/// The type of the map that indicates which nodes are reached. |
1187 | 1187 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1188 | 1188 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1189 | 1189 |
|
1190 | 1190 |
/// \brief Instantiates a \ref ReachedMap. |
1191 | 1191 |
/// |
1192 | 1192 |
/// This function instantiates a \ref ReachedMap. |
1193 | 1193 |
/// \param digraph is the digraph, to which |
1194 | 1194 |
/// we would like to define the \ref ReachedMap. |
1195 | 1195 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1196 | 1196 |
return new ReachedMap(digraph); |
1197 | 1197 |
} |
1198 | 1198 |
|
1199 | 1199 |
}; |
1200 | 1200 |
|
1201 | 1201 |
/// \ingroup search |
1202 | 1202 |
/// |
1203 | 1203 |
/// \brief %DFS algorithm class with visitor interface. |
1204 | 1204 |
/// |
1205 | 1205 |
/// This class provides an efficient implementation of the %DFS algorithm |
1206 | 1206 |
/// with visitor interface. |
1207 | 1207 |
/// |
1208 | 1208 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1209 | 1209 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1210 | 1210 |
/// the member functions of the \c Visitor class on every DFS event. |
1211 | 1211 |
/// |
1212 |
/// This interface of the DFS algorithm should be used in special cases |
|
1213 |
/// when extra actions have to be performed in connection with certain |
|
1214 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
|
1215 |
/// instead. |
|
1216 |
/// |
|
1212 | 1217 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1213 | 1218 |
/// The default value is |
1214 | 1219 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1215 | 1220 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
1216 | 1221 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1217 | 1222 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
1218 | 1223 |
/// does not observe the DFS events. If you want to observe the DFS |
1219 | 1224 |
/// events, you should implement your own visitor class. |
1220 | 1225 |
/// \tparam _Traits Traits class to set various data types used by the |
1221 | 1226 |
/// algorithm. The default traits class is |
1222 | 1227 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1223 | 1228 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1224 | 1229 |
/// a DFS visit traits class. |
1225 | 1230 |
#ifdef DOXYGEN |
1226 | 1231 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1227 | 1232 |
#else |
1228 | 1233 |
template <typename _Digraph = ListDigraph, |
1229 | 1234 |
typename _Visitor = DfsVisitor<_Digraph>, |
1230 | 1235 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1231 | 1236 |
#endif |
1232 | 1237 |
class DfsVisit { |
1233 | 1238 |
public: |
1234 | 1239 |
|
1235 | 1240 |
/// \brief \ref Exception for uninitialized parameters. |
1236 | 1241 |
/// |
1237 | 1242 |
/// This error represents problems in the initialization |
1238 | 1243 |
/// of the parameters of the algorithm. |
1239 | 1244 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1240 | 1245 |
public: |
1241 | 1246 |
virtual const char* what() const throw() |
1242 | 1247 |
{ |
1243 | 1248 |
return "lemon::DfsVisit::UninitializedParameter"; |
1244 | 1249 |
} |
1245 | 1250 |
}; |
1246 | 1251 |
|
1247 | 1252 |
///The traits class. |
1248 | 1253 |
typedef _Traits Traits; |
1249 | 1254 |
|
1250 | 1255 |
///The type of the digraph the algorithm runs on. |
1251 | 1256 |
typedef typename Traits::Digraph Digraph; |
1252 | 1257 |
|
1253 | 1258 |
///The visitor type used by the algorithm. |
1254 | 1259 |
typedef _Visitor Visitor; |
1255 | 1260 |
|
1256 | 1261 |
///The type of the map that indicates which nodes are reached. |
1257 | 1262 |
typedef typename Traits::ReachedMap ReachedMap; |
1258 | 1263 |
|
1259 | 1264 |
private: |
1260 | 1265 |
|
1261 | 1266 |
typedef typename Digraph::Node Node; |
1262 | 1267 |
typedef typename Digraph::NodeIt NodeIt; |
1263 | 1268 |
typedef typename Digraph::Arc Arc; |
1264 | 1269 |
typedef typename Digraph::OutArcIt OutArcIt; |
1265 | 1270 |
|
1266 | 1271 |
//Pointer to the underlying digraph. |
1267 | 1272 |
const Digraph *_digraph; |
1268 | 1273 |
//Pointer to the visitor object. |
1269 | 1274 |
Visitor *_visitor; |
1270 | 1275 |
//Pointer to the map of reached status of the nodes. |
1271 | 1276 |
ReachedMap *_reached; |
1272 | 1277 |
//Indicates if _reached is locally allocated (true) or not. |
1273 | 1278 |
bool local_reached; |
1274 | 1279 |
|
1275 | 1280 |
std::vector<typename Digraph::Arc> _stack; |
1276 | 1281 |
int _stack_head; |
1277 | 1282 |
|
1278 | 1283 |
///Creates the maps if necessary. |
1279 | 1284 |
///\todo Better memory allocation (instead of new). |
1280 | 1285 |
void create_maps() { |
1281 | 1286 |
if(!_reached) { |
1282 | 1287 |
local_reached = true; |
1283 | 1288 |
_reached = Traits::createReachedMap(*_digraph); |
1284 | 1289 |
} |
1285 | 1290 |
} |
1286 | 1291 |
|
1287 | 1292 |
protected: |
1288 | 1293 |
|
1289 | 1294 |
DfsVisit() {} |
1290 | 1295 |
|
1291 | 1296 |
public: |
1292 | 1297 |
|
1293 | 1298 |
typedef DfsVisit Create; |
1294 | 1299 |
|
1295 | 1300 |
/// \name Named template parameters |
1296 | 1301 |
|
1297 | 1302 |
///@{ |
1298 | 1303 |
template <class T> |
1299 | 1304 |
struct DefReachedMapTraits : public Traits { |
1300 | 1305 |
typedef T ReachedMap; |
1301 | 1306 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1302 | 1307 |
throw UninitializedParameter(); |
1303 | 1308 |
} |
1304 | 1309 |
}; |
1305 | 1310 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1306 | 1311 |
/// ReachedMap type. |
1307 | 1312 |
/// |
1308 | 1313 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1309 | 1314 |
template <class T> |
1310 | 1315 |
struct DefReachedMap : public DfsVisit< Digraph, Visitor, |
1311 | 1316 |
DefReachedMapTraits<T> > { |
1312 | 1317 |
typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1313 | 1318 |
}; |
1314 | 1319 |
///@} |
1315 | 1320 |
|
1316 | 1321 |
public: |
1317 | 1322 |
|
1318 | 1323 |
/// \brief Constructor. |
1319 | 1324 |
/// |
1320 | 1325 |
/// Constructor. |
1321 | 1326 |
/// |
1322 | 1327 |
/// \param digraph The digraph the algorithm runs on. |
1323 | 1328 |
/// \param visitor The visitor object of the algorithm. |
1324 | 1329 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1325 | 1330 |
: _digraph(&digraph), _visitor(&visitor), |
1326 | 1331 |
_reached(0), local_reached(false) {} |
1327 | 1332 |
|
1328 | 1333 |
/// \brief Destructor. |
1329 | 1334 |
~DfsVisit() { |
1330 | 1335 |
if(local_reached) delete _reached; |
1331 | 1336 |
} |
1332 | 1337 |
|
1333 | 1338 |
/// \brief Sets the map that indicates which nodes are reached. |
1334 | 1339 |
/// |
1335 | 1340 |
/// Sets the map that indicates which nodes are reached. |
1336 | 1341 |
/// If you don't use this function before calling \ref run(), |
1337 | 1342 |
/// it will allocate one. The destructor deallocates this |
1338 | 1343 |
/// automatically allocated map, of course. |
1339 | 1344 |
/// \return <tt> (*this) </tt> |
1340 | 1345 |
DfsVisit &reachedMap(ReachedMap &m) { |
1341 | 1346 |
if(local_reached) { |
1342 | 1347 |
delete _reached; |
1343 | 1348 |
local_reached=false; |
1344 | 1349 |
} |
1345 | 1350 |
_reached = &m; |
1346 | 1351 |
return *this; |
1347 | 1352 |
} |
1348 | 1353 |
|
1349 | 1354 |
public: |
1350 | 1355 |
|
1351 | 1356 |
/// \name Execution control |
1352 | 1357 |
/// The simplest way to execute the algorithm is to use |
1353 | 1358 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1354 | 1359 |
/// "run()". |
1355 | 1360 |
/// \n |
1356 | 1361 |
/// If you need more control on the execution, first you must call |
1357 | 1362 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1358 | 1363 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1359 | 1364 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1360 | 1365 |
/// actual path computation. |
1361 | 1366 |
|
1362 | 1367 |
/// @{ |
1363 | 1368 |
|
1364 | 1369 |
/// \brief Initializes the internal data structures. |
1365 | 1370 |
/// |
1366 | 1371 |
/// Initializes the internal data structures. |
1367 | 1372 |
void init() { |
1368 | 1373 |
create_maps(); |
1369 | 1374 |
_stack.resize(countNodes(*_digraph)); |
1370 | 1375 |
_stack_head = -1; |
1371 | 1376 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1372 | 1377 |
_reached->set(u, false); |
1373 | 1378 |
} |
1374 | 1379 |
} |
1375 | 1380 |
|
1376 | 1381 |
///Adds a new source node. |
1377 | 1382 |
|
1378 | 1383 |
///Adds a new source node to the set of nodes to be processed. |
1379 | 1384 |
/// |
1380 | 1385 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1381 | 1386 |
///false results.) |
1382 | 1387 |
/// |
1383 | 1388 |
///\warning Distances will be wrong (or at least strange) in case of |
1384 | 1389 |
///multiple sources. |
1385 | 1390 |
void addSource(Node s) |
1386 | 1391 |
{ |
1387 | 1392 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1388 | 1393 |
if(!(*_reached)[s]) { |
1389 | 1394 |
_reached->set(s,true); |
1390 | 1395 |
_visitor->start(s); |
1391 | 1396 |
_visitor->reach(s); |
1392 | 1397 |
Arc e; |
1393 | 1398 |
_digraph->firstOut(e, s); |
1394 | 1399 |
if (e != INVALID) { |
1395 | 1400 |
_stack[++_stack_head] = e; |
1396 | 1401 |
} else { |
1397 | 1402 |
_visitor->leave(s); |
1398 | 1403 |
} |
1399 | 1404 |
} |
1400 | 1405 |
} |
1401 | 1406 |
|
1402 | 1407 |
/// \brief Processes the next arc. |
1403 | 1408 |
/// |
1404 | 1409 |
/// Processes the next arc. |
1405 | 1410 |
/// |
1406 | 1411 |
/// \return The processed arc. |
1407 | 1412 |
/// |
1408 | 1413 |
/// \pre The stack must not be empty. |
1409 | 1414 |
Arc processNextArc() { |
1410 | 1415 |
Arc e = _stack[_stack_head]; |
1411 | 1416 |
Node m = _digraph->target(e); |
1412 | 1417 |
if(!(*_reached)[m]) { |
1413 | 1418 |
_visitor->discover(e); |
1414 | 1419 |
_visitor->reach(m); |
1415 | 1420 |
_reached->set(m, true); |
1416 | 1421 |
_digraph->firstOut(_stack[++_stack_head], m); |
1417 | 1422 |
} else { |
1418 | 1423 |
_visitor->examine(e); |
1419 | 1424 |
m = _digraph->source(e); |
1420 | 1425 |
_digraph->nextOut(_stack[_stack_head]); |
1421 | 1426 |
} |
1422 | 1427 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1423 | 1428 |
_visitor->leave(m); |
1424 | 1429 |
--_stack_head; |
1425 | 1430 |
if (_stack_head >= 0) { |
1426 | 1431 |
_visitor->backtrack(_stack[_stack_head]); |
1427 | 1432 |
m = _digraph->source(_stack[_stack_head]); |
1428 | 1433 |
_digraph->nextOut(_stack[_stack_head]); |
1429 | 1434 |
} else { |
1430 | 1435 |
_visitor->stop(m); |
1431 | 1436 |
} |
1432 | 1437 |
} |
1433 | 1438 |
return e; |
1434 | 1439 |
} |
1435 | 1440 |
|
1436 | 1441 |
/// \brief Next arc to be processed. |
1437 | 1442 |
/// |
1438 | 1443 |
/// Next arc to be processed. |
1439 | 1444 |
/// |
1440 | 1445 |
/// \return The next arc to be processed or INVALID if the stack is |
1441 | 1446 |
/// empty. |
1442 | 1447 |
Arc nextArc() const { |
1443 | 1448 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1444 | 1449 |
} |
1445 | 1450 |
|
1446 | 1451 |
/// \brief Returns \c false if there are nodes |
1447 | 1452 |
/// to be processed. |
1448 | 1453 |
/// |
1449 | 1454 |
/// Returns \c false if there are nodes |
1450 | 1455 |
/// to be processed in the queue (stack). |
1451 | 1456 |
bool emptyQueue() const { return _stack_head < 0; } |
1452 | 1457 |
|
1453 | 1458 |
/// \brief Returns the number of the nodes to be processed. |
1454 | 1459 |
/// |
1455 | 1460 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1456 | 1461 |
int queueSize() const { return _stack_head + 1; } |
1457 | 1462 |
|
1458 | 1463 |
/// \brief Executes the algorithm. |
1459 | 1464 |
/// |
1460 | 1465 |
/// Executes the algorithm. |
1461 | 1466 |
/// |
1462 | 1467 |
/// This method runs the %DFS algorithm from the root node |
1463 | 1468 |
/// in order to compute the %DFS path to each node. |
1464 | 1469 |
/// |
1465 | 1470 |
/// The algorithm computes |
1466 | 1471 |
/// - the %DFS tree, |
1467 | 1472 |
/// - the distance of each node from the root in the %DFS tree. |
1468 | 1473 |
/// |
1469 | 1474 |
/// \pre init() must be called and a root node should be |
1470 | 1475 |
/// added with addSource() before using this function. |
1471 | 1476 |
/// |
1472 | 1477 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1473 | 1478 |
/// \code |
1474 | 1479 |
/// while ( !d.emptyQueue() ) { |
1475 | 1480 |
/// d.processNextArc(); |
1476 | 1481 |
/// } |
1477 | 1482 |
/// \endcode |
1478 | 1483 |
void start() { |
1479 | 1484 |
while ( !emptyQueue() ) processNextArc(); |
1480 | 1485 |
} |
1481 | 1486 |
|
1482 | 1487 |
/// \brief Executes the algorithm until the given target node is reached. |
1483 | 1488 |
/// |
1484 | 1489 |
/// Executes the algorithm until the given target node is reached. |
1485 | 1490 |
/// |
1486 | 1491 |
/// This method runs the %DFS algorithm from the root node |
1487 | 1492 |
/// in order to compute the DFS path to \c dest. |
1488 | 1493 |
/// |
1489 | 1494 |
/// The algorithm computes |
1490 | 1495 |
/// - the %DFS path to \c dest, |
1491 | 1496 |
/// - the distance of \c dest from the root in the %DFS tree. |
1492 | 1497 |
/// |
1493 | 1498 |
/// \pre init() must be called and a root node should be added |
1494 | 1499 |
/// with addSource() before using this function. |
1495 | 1500 |
void start(Node dest) { |
1496 | 1501 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
1497 | 1502 |
processNextArc(); |
1498 | 1503 |
} |
1499 | 1504 |
|
1500 | 1505 |
/// \brief Executes the algorithm until a condition is met. |
1501 | 1506 |
/// |
1502 | 1507 |
/// Executes the algorithm until a condition is met. |
1503 | 1508 |
/// |
1504 | 1509 |
/// This method runs the %DFS algorithm from the root node |
1505 | 1510 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1506 | 1511 |
/// |
1507 | 1512 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1508 | 1513 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1509 | 1514 |
/// |
1510 | 1515 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1511 | 1516 |
/// \c INVALID if no such arc was found. |
1512 | 1517 |
/// |
1513 | 1518 |
/// \pre init() must be called and a root node should be added |
1514 | 1519 |
/// with addSource() before using this function. |
1515 | 1520 |
/// |
1516 | 1521 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1517 | 1522 |
/// not a node map. |
1518 | 1523 |
template <typename AM> |
1519 | 1524 |
Arc start(const AM &am) { |
1520 | 1525 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1521 | 1526 |
processNextArc(); |
1522 | 1527 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1523 | 1528 |
} |
1524 | 1529 |
|
1525 | 1530 |
/// \brief Runs the algorithm from the given node. |
1526 | 1531 |
/// |
1527 | 1532 |
/// This method runs the %DFS algorithm from node \c s. |
1528 | 1533 |
/// in order to compute the DFS path to each node. |
1529 | 1534 |
/// |
1530 | 1535 |
/// The algorithm computes |
1531 | 1536 |
/// - the %DFS tree, |
1532 | 1537 |
/// - the distance of each node from the root in the %DFS tree. |
1533 | 1538 |
/// |
1534 | 1539 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1535 | 1540 |
///\code |
1536 | 1541 |
/// d.init(); |
1537 | 1542 |
/// d.addSource(s); |
1538 | 1543 |
/// d.start(); |
1539 | 1544 |
///\endcode |
1540 | 1545 |
void run(Node s) { |
1541 | 1546 |
init(); |
1542 | 1547 |
addSource(s); |
1543 | 1548 |
start(); |
1544 | 1549 |
} |
1545 | 1550 |
|
1546 | 1551 |
/// \brief Finds the %DFS path between \c s and \c t. |
1547 | 1552 |
|
1548 | 1553 |
/// This method runs the %DFS algorithm from node \c s |
1549 | 1554 |
/// in order to compute the DFS path to \c t. |
1550 | 1555 |
/// |
1551 | 1556 |
/// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
1552 | 1557 |
/// if \c t is reachable form \c s, \c 0 otherwise. |
1553 | 1558 |
/// |
1554 | 1559 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1555 | 1560 |
/// just a shortcut of the following code. |
1556 | 1561 |
///\code |
1557 | 1562 |
/// d.init(); |
1558 | 1563 |
/// d.addSource(s); |
1559 | 1564 |
/// d.start(t); |
1560 | 1565 |
///\endcode |
1561 | 1566 |
int run(Node s,Node t) { |
1562 | 1567 |
init(); |
1563 | 1568 |
addSource(s); |
1564 | 1569 |
start(t); |
1565 | 1570 |
return reached(t)?_stack_head+1:0; |
1566 | 1571 |
} |
1567 | 1572 |
|
1568 | 1573 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1569 | 1574 |
|
1570 | 1575 |
/// This method runs the %DFS algorithm in order to |
1571 | 1576 |
/// compute the %DFS path to each node. |
1572 | 1577 |
/// |
1573 | 1578 |
/// The algorithm computes |
1574 | 1579 |
/// - the %DFS tree, |
1575 | 1580 |
/// - the distance of each node from the root in the %DFS tree. |
1576 | 1581 |
/// |
1577 | 1582 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1578 | 1583 |
///\code |
1579 | 1584 |
/// d.init(); |
1580 | 1585 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1581 | 1586 |
/// if (!d.reached(n)) { |
1582 | 1587 |
/// d.addSource(n); |
1583 | 1588 |
/// d.start(); |
1584 | 1589 |
/// } |
1585 | 1590 |
/// } |
1586 | 1591 |
///\endcode |
1587 | 1592 |
void run() { |
1588 | 1593 |
init(); |
1589 | 1594 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1590 | 1595 |
if (!reached(it)) { |
1591 | 1596 |
addSource(it); |
1592 | 1597 |
start(); |
1593 | 1598 |
} |
1594 | 1599 |
} |
1595 | 1600 |
} |
1596 | 1601 |
|
1597 | 1602 |
///@} |
1598 | 1603 |
|
1599 | 1604 |
/// \name Query Functions |
1600 | 1605 |
/// The result of the %DFS algorithm can be obtained using these |
1601 | 1606 |
/// functions.\n |
1602 | 1607 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1603 | 1608 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1604 | 1609 |
/// using them. |
1605 | 1610 |
///@{ |
1606 | 1611 |
|
1607 | 1612 |
/// \brief Checks if a node is reachable from the root(s). |
1608 | 1613 |
/// |
1609 | 1614 |
/// Returns \c true if \c v is reachable from the root(s). |
1610 | 1615 |
/// \pre Either \ref run() or \ref start() |
1611 | 1616 |
/// must be called before using this function. |
1612 | 1617 |
bool reached(Node v) { return (*_reached)[v]; } |
1613 | 1618 |
|
1614 | 1619 |
///@} |
1615 | 1620 |
|
1616 | 1621 |
}; |
1617 | 1622 |
|
1618 | 1623 |
} //END OF NAMESPACE LEMON |
1619 | 1624 |
|
1620 | 1625 |
#endif |
0 comments (0 inline)