0
7
0
10
4
85
45
16
16
... | ... |
@@ -496,1215 +496,1220 @@ |
496 | 496 |
///\retval reach Indicates if the target node is reached. |
497 | 497 |
///It should be initially \c false. |
498 | 498 |
/// |
499 | 499 |
///\return The processed node. |
500 | 500 |
/// |
501 | 501 |
///\pre The queue must not be empty. |
502 | 502 |
Node processNextNode(Node target, bool& reach) |
503 | 503 |
{ |
504 | 504 |
if(_queue_tail==_queue_next_dist) { |
505 | 505 |
_curr_dist++; |
506 | 506 |
_queue_next_dist=_queue_head; |
507 | 507 |
} |
508 | 508 |
Node n=_queue[_queue_tail++]; |
509 | 509 |
_processed->set(n,true); |
510 | 510 |
Node m; |
511 | 511 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
512 | 512 |
if(!(*_reached)[m=G->target(e)]) { |
513 | 513 |
_queue[_queue_head++]=m; |
514 | 514 |
_reached->set(m,true); |
515 | 515 |
_pred->set(m,e); |
516 | 516 |
_dist->set(m,_curr_dist); |
517 | 517 |
reach = reach || (target == m); |
518 | 518 |
} |
519 | 519 |
return n; |
520 | 520 |
} |
521 | 521 |
|
522 | 522 |
///Processes the next node. |
523 | 523 |
|
524 | 524 |
///Processes the next node and checks if at least one of reached |
525 | 525 |
///nodes has \c true value in the \c nm node map. If one node |
526 | 526 |
///with \c true value is reachable from the processed node, then the |
527 | 527 |
///\c rnode parameter will be set to the first of such nodes. |
528 | 528 |
/// |
529 | 529 |
///\param nm A \c bool (or convertible) node map that indicates the |
530 | 530 |
///possible targets. |
531 | 531 |
///\retval rnode The reached target node. |
532 | 532 |
///It should be initially \c INVALID. |
533 | 533 |
/// |
534 | 534 |
///\return The processed node. |
535 | 535 |
/// |
536 | 536 |
///\pre The queue must not be empty. |
537 | 537 |
template<class NM> |
538 | 538 |
Node processNextNode(const NM& nm, Node& rnode) |
539 | 539 |
{ |
540 | 540 |
if(_queue_tail==_queue_next_dist) { |
541 | 541 |
_curr_dist++; |
542 | 542 |
_queue_next_dist=_queue_head; |
543 | 543 |
} |
544 | 544 |
Node n=_queue[_queue_tail++]; |
545 | 545 |
_processed->set(n,true); |
546 | 546 |
Node m; |
547 | 547 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
548 | 548 |
if(!(*_reached)[m=G->target(e)]) { |
549 | 549 |
_queue[_queue_head++]=m; |
550 | 550 |
_reached->set(m,true); |
551 | 551 |
_pred->set(m,e); |
552 | 552 |
_dist->set(m,_curr_dist); |
553 | 553 |
if (nm[m] && rnode == INVALID) rnode = m; |
554 | 554 |
} |
555 | 555 |
return n; |
556 | 556 |
} |
557 | 557 |
|
558 | 558 |
///The next node to be processed. |
559 | 559 |
|
560 | 560 |
///Returns the next node to be processed or \c INVALID if the queue |
561 | 561 |
///is empty. |
562 | 562 |
Node nextNode() const |
563 | 563 |
{ |
564 | 564 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
565 | 565 |
} |
566 | 566 |
|
567 | 567 |
///\brief Returns \c false if there are nodes |
568 | 568 |
///to be processed. |
569 | 569 |
/// |
570 | 570 |
///Returns \c false if there are nodes |
571 | 571 |
///to be processed in the queue. |
572 | 572 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
573 | 573 |
|
574 | 574 |
///Returns the number of the nodes to be processed. |
575 | 575 |
|
576 | 576 |
///Returns the number of the nodes to be processed in the queue. |
577 | 577 |
int queueSize() const { return _queue_head-_queue_tail; } |
578 | 578 |
|
579 | 579 |
///Executes the algorithm. |
580 | 580 |
|
581 | 581 |
///Executes the algorithm. |
582 | 582 |
/// |
583 | 583 |
///This method runs the %BFS algorithm from the root node(s) |
584 | 584 |
///in order to compute the shortest path to each node. |
585 | 585 |
/// |
586 | 586 |
///The algorithm computes |
587 | 587 |
///- the shortest path tree (forest), |
588 | 588 |
///- the distance of each node from the root(s). |
589 | 589 |
/// |
590 | 590 |
///\pre init() must be called and at least one root node should be |
591 | 591 |
///added with addSource() before using this function. |
592 | 592 |
/// |
593 | 593 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
594 | 594 |
///\code |
595 | 595 |
/// while ( !b.emptyQueue() ) { |
596 | 596 |
/// b.processNextNode(); |
597 | 597 |
/// } |
598 | 598 |
///\endcode |
599 | 599 |
void start() |
600 | 600 |
{ |
601 | 601 |
while ( !emptyQueue() ) processNextNode(); |
602 | 602 |
} |
603 | 603 |
|
604 | 604 |
///Executes the algorithm until the given target node is reached. |
605 | 605 |
|
606 | 606 |
///Executes the algorithm until the given target node is reached. |
607 | 607 |
/// |
608 | 608 |
///This method runs the %BFS algorithm from the root node(s) |
609 | 609 |
///in order to compute the shortest path to \c dest. |
610 | 610 |
/// |
611 | 611 |
///The algorithm computes |
612 | 612 |
///- the shortest path to \c dest, |
613 | 613 |
///- the distance of \c dest from the root(s). |
614 | 614 |
/// |
615 | 615 |
///\pre init() must be called and at least one root node should be |
616 | 616 |
///added with addSource() before using this function. |
617 | 617 |
/// |
618 | 618 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
619 | 619 |
///\code |
620 | 620 |
/// bool reach = false; |
621 | 621 |
/// while ( !b.emptyQueue() && !reach ) { |
622 | 622 |
/// b.processNextNode(t, reach); |
623 | 623 |
/// } |
624 | 624 |
///\endcode |
625 | 625 |
void start(Node dest) |
626 | 626 |
{ |
627 | 627 |
bool reach = false; |
628 | 628 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
629 | 629 |
} |
630 | 630 |
|
631 | 631 |
///Executes the algorithm until a condition is met. |
632 | 632 |
|
633 | 633 |
///Executes the algorithm until a condition is met. |
634 | 634 |
/// |
635 | 635 |
///This method runs the %BFS algorithm from the root node(s) in |
636 | 636 |
///order to compute the shortest path to a node \c v with |
637 | 637 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
638 | 638 |
/// |
639 | 639 |
///\param nm A \c bool (or convertible) node map. The algorithm |
640 | 640 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
641 | 641 |
/// |
642 | 642 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
643 | 643 |
///\c INVALID if no such node was found. |
644 | 644 |
/// |
645 | 645 |
///\pre init() must be called and at least one root node should be |
646 | 646 |
///added with addSource() before using this function. |
647 | 647 |
/// |
648 | 648 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
649 | 649 |
///\code |
650 | 650 |
/// Node rnode = INVALID; |
651 | 651 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
652 | 652 |
/// b.processNextNode(nm, rnode); |
653 | 653 |
/// } |
654 | 654 |
/// return rnode; |
655 | 655 |
///\endcode |
656 | 656 |
template<class NodeBoolMap> |
657 | 657 |
Node start(const NodeBoolMap &nm) |
658 | 658 |
{ |
659 | 659 |
Node rnode = INVALID; |
660 | 660 |
while ( !emptyQueue() && rnode == INVALID ) { |
661 | 661 |
processNextNode(nm, rnode); |
662 | 662 |
} |
663 | 663 |
return rnode; |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
///Runs the algorithm from the given node. |
667 | 667 |
|
668 | 668 |
///This method runs the %BFS algorithm from node \c s |
669 | 669 |
///in order to compute the shortest path to each node. |
670 | 670 |
/// |
671 | 671 |
///The algorithm computes |
672 | 672 |
///- the shortest path tree, |
673 | 673 |
///- the distance of each node from the root. |
674 | 674 |
/// |
675 | 675 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
676 | 676 |
///\code |
677 | 677 |
/// b.init(); |
678 | 678 |
/// b.addSource(s); |
679 | 679 |
/// b.start(); |
680 | 680 |
///\endcode |
681 | 681 |
void run(Node s) { |
682 | 682 |
init(); |
683 | 683 |
addSource(s); |
684 | 684 |
start(); |
685 | 685 |
} |
686 | 686 |
|
687 | 687 |
///Finds the shortest path between \c s and \c t. |
688 | 688 |
|
689 | 689 |
///This method runs the %BFS algorithm from node \c s |
690 | 690 |
///in order to compute the shortest path to \c t. |
691 | 691 |
/// |
692 | 692 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
693 | 693 |
///if \c t is reachable form \c s, \c 0 otherwise. |
694 | 694 |
/// |
695 | 695 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
696 | 696 |
///shortcut of the following code. |
697 | 697 |
///\code |
698 | 698 |
/// b.init(); |
699 | 699 |
/// b.addSource(s); |
700 | 700 |
/// b.start(t); |
701 | 701 |
///\endcode |
702 | 702 |
int run(Node s,Node t) { |
703 | 703 |
init(); |
704 | 704 |
addSource(s); |
705 | 705 |
start(t); |
706 | 706 |
return reached(t) ? _curr_dist : 0; |
707 | 707 |
} |
708 | 708 |
|
709 | 709 |
///Runs the algorithm to visit all nodes in the digraph. |
710 | 710 |
|
711 | 711 |
///This method runs the %BFS algorithm in order to |
712 | 712 |
///compute the shortest path to each node. |
713 | 713 |
/// |
714 | 714 |
///The algorithm computes |
715 | 715 |
///- the shortest path tree (forest), |
716 | 716 |
///- the distance of each node from the root(s). |
717 | 717 |
/// |
718 | 718 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
719 | 719 |
///\code |
720 | 720 |
/// b.init(); |
721 | 721 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
722 | 722 |
/// if (!b.reached(n)) { |
723 | 723 |
/// b.addSource(n); |
724 | 724 |
/// b.start(); |
725 | 725 |
/// } |
726 | 726 |
/// } |
727 | 727 |
///\endcode |
728 | 728 |
void run() { |
729 | 729 |
init(); |
730 | 730 |
for (NodeIt n(*G); n != INVALID; ++n) { |
731 | 731 |
if (!reached(n)) { |
732 | 732 |
addSource(n); |
733 | 733 |
start(); |
734 | 734 |
} |
735 | 735 |
} |
736 | 736 |
} |
737 | 737 |
|
738 | 738 |
///@} |
739 | 739 |
|
740 | 740 |
///\name Query Functions |
741 | 741 |
///The result of the %BFS algorithm can be obtained using these |
742 | 742 |
///functions.\n |
743 | 743 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
744 | 744 |
///"start()" must be called before using them. |
745 | 745 |
|
746 | 746 |
///@{ |
747 | 747 |
|
748 | 748 |
///The shortest path to a node. |
749 | 749 |
|
750 | 750 |
///Returns the shortest path to a node. |
751 | 751 |
/// |
752 | 752 |
///\warning \c t should be reachable from the root(s). |
753 | 753 |
/// |
754 | 754 |
///\pre Either \ref run() or \ref start() must be called before |
755 | 755 |
///using this function. |
756 | 756 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
757 | 757 |
|
758 | 758 |
///The distance of a node from the root(s). |
759 | 759 |
|
760 | 760 |
///Returns the distance of a node from the root(s). |
761 | 761 |
/// |
762 | 762 |
///\warning If node \c v is not reachable from the root(s), then |
763 | 763 |
///the return value of this function is undefined. |
764 | 764 |
/// |
765 | 765 |
///\pre Either \ref run() or \ref start() must be called before |
766 | 766 |
///using this function. |
767 | 767 |
int dist(Node v) const { return (*_dist)[v]; } |
768 | 768 |
|
769 | 769 |
///Returns the 'previous arc' of the shortest path tree for a node. |
770 | 770 |
|
771 | 771 |
///This function returns the 'previous arc' of the shortest path |
772 | 772 |
///tree for the node \c v, i.e. it returns the last arc of a |
773 | 773 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
774 | 774 |
///is not reachable from the root(s) or if \c v is a root. |
775 | 775 |
/// |
776 | 776 |
///The shortest path tree used here is equal to the shortest path |
777 | 777 |
///tree used in \ref predNode(). |
778 | 778 |
/// |
779 | 779 |
///\pre Either \ref run() or \ref start() must be called before |
780 | 780 |
///using this function. |
781 | 781 |
Arc predArc(Node v) const { return (*_pred)[v];} |
782 | 782 |
|
783 | 783 |
///Returns the 'previous node' of the shortest path tree for a node. |
784 | 784 |
|
785 | 785 |
///This function returns the 'previous node' of the shortest path |
786 | 786 |
///tree for the node \c v, i.e. it returns the last but one node |
787 | 787 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
788 | 788 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
789 | 789 |
/// |
790 | 790 |
///The shortest path tree used here is equal to the shortest path |
791 | 791 |
///tree used in \ref predArc(). |
792 | 792 |
/// |
793 | 793 |
///\pre Either \ref run() or \ref start() must be called before |
794 | 794 |
///using this function. |
795 | 795 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
796 | 796 |
G->source((*_pred)[v]); } |
797 | 797 |
|
798 | 798 |
///\brief Returns a const reference to the node map that stores the |
799 | 799 |
/// distances of the nodes. |
800 | 800 |
/// |
801 | 801 |
///Returns a const reference to the node map that stores the distances |
802 | 802 |
///of the nodes calculated by the algorithm. |
803 | 803 |
/// |
804 | 804 |
///\pre Either \ref run() or \ref init() |
805 | 805 |
///must be called before using this function. |
806 | 806 |
const DistMap &distMap() const { return *_dist;} |
807 | 807 |
|
808 | 808 |
///\brief Returns a const reference to the node map that stores the |
809 | 809 |
///predecessor arcs. |
810 | 810 |
/// |
811 | 811 |
///Returns a const reference to the node map that stores the predecessor |
812 | 812 |
///arcs, which form the shortest path tree. |
813 | 813 |
/// |
814 | 814 |
///\pre Either \ref run() or \ref init() |
815 | 815 |
///must be called before using this function. |
816 | 816 |
const PredMap &predMap() const { return *_pred;} |
817 | 817 |
|
818 | 818 |
///Checks if a node is reachable from the root(s). |
819 | 819 |
|
820 | 820 |
///Returns \c true if \c v is reachable from the root(s). |
821 | 821 |
///\pre Either \ref run() or \ref start() |
822 | 822 |
///must be called before using this function. |
823 | 823 |
bool reached(Node v) const { return (*_reached)[v]; } |
824 | 824 |
|
825 | 825 |
///@} |
826 | 826 |
}; |
827 | 827 |
|
828 | 828 |
///Default traits class of bfs() function. |
829 | 829 |
|
830 | 830 |
///Default traits class of bfs() function. |
831 | 831 |
///\tparam GR Digraph type. |
832 | 832 |
template<class GR> |
833 | 833 |
struct BfsWizardDefaultTraits |
834 | 834 |
{ |
835 | 835 |
///The type of the digraph the algorithm runs on. |
836 | 836 |
typedef GR Digraph; |
837 | 837 |
|
838 | 838 |
///\brief The type of the map that stores the predecessor |
839 | 839 |
///arcs of the shortest paths. |
840 | 840 |
/// |
841 | 841 |
///The type of the map that stores the predecessor |
842 | 842 |
///arcs of the shortest paths. |
843 | 843 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
844 | 844 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
845 | 845 |
///Instantiates a \ref PredMap. |
846 | 846 |
|
847 | 847 |
///This function instantiates a \ref PredMap. |
848 | 848 |
///\param g is the digraph, to which we would like to define the |
849 | 849 |
///\ref PredMap. |
850 | 850 |
///\todo The digraph alone may be insufficient to initialize |
851 | 851 |
#ifdef DOXYGEN |
852 | 852 |
static PredMap *createPredMap(const Digraph &g) |
853 | 853 |
#else |
854 | 854 |
static PredMap *createPredMap(const Digraph &) |
855 | 855 |
#endif |
856 | 856 |
{ |
857 | 857 |
return new PredMap(); |
858 | 858 |
} |
859 | 859 |
|
860 | 860 |
///The type of the map that indicates which nodes are processed. |
861 | 861 |
|
862 | 862 |
///The type of the map that indicates which nodes are processed. |
863 | 863 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
864 | 864 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
865 | 865 |
///Instantiates a \ref ProcessedMap. |
866 | 866 |
|
867 | 867 |
///This function instantiates a \ref ProcessedMap. |
868 | 868 |
///\param g is the digraph, to which |
869 | 869 |
///we would like to define the \ref ProcessedMap. |
870 | 870 |
#ifdef DOXYGEN |
871 | 871 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
872 | 872 |
#else |
873 | 873 |
static ProcessedMap *createProcessedMap(const Digraph &) |
874 | 874 |
#endif |
875 | 875 |
{ |
876 | 876 |
return new ProcessedMap(); |
877 | 877 |
} |
878 | 878 |
|
879 | 879 |
///The type of the map that indicates which nodes are reached. |
880 | 880 |
|
881 | 881 |
///The type of the map that indicates which nodes are reached. |
882 | 882 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
883 | 883 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
884 | 884 |
///Instantiates a \ref ReachedMap. |
885 | 885 |
|
886 | 886 |
///This function instantiates a \ref ReachedMap. |
887 | 887 |
///\param g is the digraph, to which |
888 | 888 |
///we would like to define the \ref ReachedMap. |
889 | 889 |
static ReachedMap *createReachedMap(const Digraph &g) |
890 | 890 |
{ |
891 | 891 |
return new ReachedMap(g); |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
///The type of the map that stores the distances of the nodes. |
895 | 895 |
|
896 | 896 |
///The type of the map that stores the distances of the nodes. |
897 | 897 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
898 | 898 |
/// |
899 | 899 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
900 | 900 |
///Instantiates a \ref DistMap. |
901 | 901 |
|
902 | 902 |
///This function instantiates a \ref DistMap. |
903 | 903 |
///\param g is the digraph, to which we would like to define |
904 | 904 |
///the \ref DistMap |
905 | 905 |
#ifdef DOXYGEN |
906 | 906 |
static DistMap *createDistMap(const Digraph &g) |
907 | 907 |
#else |
908 | 908 |
static DistMap *createDistMap(const Digraph &) |
909 | 909 |
#endif |
910 | 910 |
{ |
911 | 911 |
return new DistMap(); |
912 | 912 |
} |
913 | 913 |
}; |
914 | 914 |
|
915 | 915 |
/// Default traits class used by \ref BfsWizard |
916 | 916 |
|
917 | 917 |
/// To make it easier to use Bfs algorithm |
918 | 918 |
/// we have created a wizard class. |
919 | 919 |
/// This \ref BfsWizard class needs default traits, |
920 | 920 |
/// as well as the \ref Bfs class. |
921 | 921 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
922 | 922 |
/// \ref BfsWizard class. |
923 | 923 |
template<class GR> |
924 | 924 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
925 | 925 |
{ |
926 | 926 |
|
927 | 927 |
typedef BfsWizardDefaultTraits<GR> Base; |
928 | 928 |
protected: |
929 | 929 |
//The type of the nodes in the digraph. |
930 | 930 |
typedef typename Base::Digraph::Node Node; |
931 | 931 |
|
932 | 932 |
//Pointer to the digraph the algorithm runs on. |
933 | 933 |
void *_g; |
934 | 934 |
//Pointer to the map of reached nodes. |
935 | 935 |
void *_reached; |
936 | 936 |
//Pointer to the map of processed nodes. |
937 | 937 |
void *_processed; |
938 | 938 |
//Pointer to the map of predecessors arcs. |
939 | 939 |
void *_pred; |
940 | 940 |
//Pointer to the map of distances. |
941 | 941 |
void *_dist; |
942 | 942 |
//Pointer to the source node. |
943 | 943 |
Node _source; |
944 | 944 |
|
945 | 945 |
public: |
946 | 946 |
/// Constructor. |
947 | 947 |
|
948 | 948 |
/// This constructor does not require parameters, therefore it initiates |
949 | 949 |
/// all of the attributes to default values (0, INVALID). |
950 | 950 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
951 | 951 |
_dist(0), _source(INVALID) {} |
952 | 952 |
|
953 | 953 |
/// Constructor. |
954 | 954 |
|
955 | 955 |
/// This constructor requires some parameters, |
956 | 956 |
/// listed in the parameters list. |
957 | 957 |
/// Others are initiated to 0. |
958 | 958 |
/// \param g The digraph the algorithm runs on. |
959 | 959 |
/// \param s The source node. |
960 | 960 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
961 | 961 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
962 | 962 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
963 | 963 |
|
964 | 964 |
}; |
965 | 965 |
|
966 | 966 |
/// Auxiliary class for the function type interface of BFS algorithm. |
967 | 967 |
|
968 | 968 |
/// This auxiliary class is created to implement the function type |
969 | 969 |
/// interface of \ref Bfs algorithm. It uses the functions and features |
970 | 970 |
/// of the plain \ref Bfs, but it is much simpler to use it. |
971 | 971 |
/// It should only be used through the \ref bfs() function, which makes |
972 | 972 |
/// it easier to use the algorithm. |
973 | 973 |
/// |
974 | 974 |
/// Simplicity means that the way to change the types defined |
975 | 975 |
/// in the traits class is based on functions that returns the new class |
976 | 976 |
/// and not on templatable built-in classes. |
977 | 977 |
/// When using the plain \ref Bfs |
978 | 978 |
/// the new class with the modified type comes from |
979 | 979 |
/// the original class by using the :: |
980 | 980 |
/// operator. In the case of \ref BfsWizard only |
981 | 981 |
/// a function have to be called, and it will |
982 | 982 |
/// return the needed class. |
983 | 983 |
/// |
984 | 984 |
/// It does not have own \ref run() method. When its \ref run() method |
985 | 985 |
/// is called, it initiates a plain \ref Bfs object, and calls the |
986 | 986 |
/// \ref Bfs::run() method of it. |
987 | 987 |
template<class TR> |
988 | 988 |
class BfsWizard : public TR |
989 | 989 |
{ |
990 | 990 |
typedef TR Base; |
991 | 991 |
|
992 | 992 |
///The type of the digraph the algorithm runs on. |
993 | 993 |
typedef typename TR::Digraph Digraph; |
994 | 994 |
|
995 | 995 |
typedef typename Digraph::Node Node; |
996 | 996 |
typedef typename Digraph::NodeIt NodeIt; |
997 | 997 |
typedef typename Digraph::Arc Arc; |
998 | 998 |
typedef typename Digraph::OutArcIt OutArcIt; |
999 | 999 |
|
1000 | 1000 |
///\brief The type of the map that stores the predecessor |
1001 | 1001 |
///arcs of the shortest paths. |
1002 | 1002 |
typedef typename TR::PredMap PredMap; |
1003 | 1003 |
///\brief The type of the map that stores the distances of the nodes. |
1004 | 1004 |
typedef typename TR::DistMap DistMap; |
1005 | 1005 |
///\brief The type of the map that indicates which nodes are reached. |
1006 | 1006 |
typedef typename TR::ReachedMap ReachedMap; |
1007 | 1007 |
///\brief The type of the map that indicates which nodes are processed. |
1008 | 1008 |
typedef typename TR::ProcessedMap ProcessedMap; |
1009 | 1009 |
|
1010 | 1010 |
public: |
1011 | 1011 |
|
1012 | 1012 |
/// Constructor. |
1013 | 1013 |
BfsWizard() : TR() {} |
1014 | 1014 |
|
1015 | 1015 |
/// Constructor that requires parameters. |
1016 | 1016 |
|
1017 | 1017 |
/// Constructor that requires parameters. |
1018 | 1018 |
/// These parameters will be the default values for the traits class. |
1019 | 1019 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
1020 | 1020 |
TR(g,s) {} |
1021 | 1021 |
|
1022 | 1022 |
///Copy constructor |
1023 | 1023 |
BfsWizard(const TR &b) : TR(b) {} |
1024 | 1024 |
|
1025 | 1025 |
~BfsWizard() {} |
1026 | 1026 |
|
1027 | 1027 |
///Runs BFS algorithm from a source node. |
1028 | 1028 |
|
1029 | 1029 |
///Runs BFS algorithm from a source node. |
1030 | 1030 |
///The node can be given with the \ref source() function. |
1031 | 1031 |
void run() |
1032 | 1032 |
{ |
1033 | 1033 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1034 | 1034 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1035 | 1035 |
if(Base::_reached) |
1036 | 1036 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1037 | 1037 |
if(Base::_processed) |
1038 | 1038 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1039 | 1039 |
if(Base::_pred) |
1040 | 1040 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1041 | 1041 |
if(Base::_dist) |
1042 | 1042 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1043 | 1043 |
alg.run(Base::_source); |
1044 | 1044 |
} |
1045 | 1045 |
|
1046 | 1046 |
///Runs BFS algorithm from the given node. |
1047 | 1047 |
|
1048 | 1048 |
///Runs BFS algorithm from the given node. |
1049 | 1049 |
///\param s is the given source. |
1050 | 1050 |
void run(Node s) |
1051 | 1051 |
{ |
1052 | 1052 |
Base::_source=s; |
1053 | 1053 |
run(); |
1054 | 1054 |
} |
1055 | 1055 |
|
1056 | 1056 |
/// Sets the source node, from which the Bfs algorithm runs. |
1057 | 1057 |
|
1058 | 1058 |
/// Sets the source node, from which the Bfs algorithm runs. |
1059 | 1059 |
/// \param s is the source node. |
1060 | 1060 |
BfsWizard<TR> &source(Node s) |
1061 | 1061 |
{ |
1062 | 1062 |
Base::_source=s; |
1063 | 1063 |
return *this; |
1064 | 1064 |
} |
1065 | 1065 |
|
1066 | 1066 |
template<class T> |
1067 | 1067 |
struct SetPredMapBase : public Base { |
1068 | 1068 |
typedef T PredMap; |
1069 | 1069 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1070 | 1070 |
SetPredMapBase(const TR &b) : TR(b) {} |
1071 | 1071 |
}; |
1072 | 1072 |
///\brief \ref named-templ-param "Named parameter" |
1073 | 1073 |
///for setting \ref PredMap object. |
1074 | 1074 |
/// |
1075 | 1075 |
/// \ref named-templ-param "Named parameter" |
1076 | 1076 |
///for setting \ref PredMap object. |
1077 | 1077 |
template<class T> |
1078 | 1078 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1079 | 1079 |
{ |
1080 | 1080 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1081 | 1081 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1082 | 1082 |
} |
1083 | 1083 |
|
1084 | 1084 |
template<class T> |
1085 | 1085 |
struct SetReachedMapBase : public Base { |
1086 | 1086 |
typedef T ReachedMap; |
1087 | 1087 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1088 | 1088 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1089 | 1089 |
}; |
1090 | 1090 |
///\brief \ref named-templ-param "Named parameter" |
1091 | 1091 |
///for setting \ref ReachedMap object. |
1092 | 1092 |
/// |
1093 | 1093 |
/// \ref named-templ-param "Named parameter" |
1094 | 1094 |
///for setting \ref ReachedMap object. |
1095 | 1095 |
template<class T> |
1096 | 1096 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1097 | 1097 |
{ |
1098 | 1098 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1099 | 1099 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1100 | 1100 |
} |
1101 | 1101 |
|
1102 | 1102 |
template<class T> |
1103 | 1103 |
struct SetProcessedMapBase : public Base { |
1104 | 1104 |
typedef T ProcessedMap; |
1105 | 1105 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1106 | 1106 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1107 | 1107 |
}; |
1108 | 1108 |
///\brief \ref named-templ-param "Named parameter" |
1109 | 1109 |
///for setting \ref ProcessedMap object. |
1110 | 1110 |
/// |
1111 | 1111 |
/// \ref named-templ-param "Named parameter" |
1112 | 1112 |
///for setting \ref ProcessedMap object. |
1113 | 1113 |
template<class T> |
1114 | 1114 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1115 | 1115 |
{ |
1116 | 1116 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1117 | 1117 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1118 | 1118 |
} |
1119 | 1119 |
|
1120 | 1120 |
template<class T> |
1121 | 1121 |
struct SetDistMapBase : public Base { |
1122 | 1122 |
typedef T DistMap; |
1123 | 1123 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1124 | 1124 |
SetDistMapBase(const TR &b) : TR(b) {} |
1125 | 1125 |
}; |
1126 | 1126 |
///\brief \ref named-templ-param "Named parameter" |
1127 | 1127 |
///for setting \ref DistMap object. |
1128 | 1128 |
/// |
1129 | 1129 |
/// \ref named-templ-param "Named parameter" |
1130 | 1130 |
///for setting \ref DistMap object. |
1131 | 1131 |
template<class T> |
1132 | 1132 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1133 | 1133 |
{ |
1134 | 1134 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1135 | 1135 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1136 | 1136 |
} |
1137 | 1137 |
|
1138 | 1138 |
}; |
1139 | 1139 |
|
1140 | 1140 |
///Function type interface for Bfs algorithm. |
1141 | 1141 |
|
1142 | 1142 |
/// \ingroup search |
1143 | 1143 |
///Function type interface for Bfs algorithm. |
1144 | 1144 |
/// |
1145 | 1145 |
///This function also has several |
1146 | 1146 |
///\ref named-templ-func-param "named parameters", |
1147 | 1147 |
///they are declared as the members of class \ref BfsWizard. |
1148 | 1148 |
///The following |
1149 | 1149 |
///example shows how to use these parameters. |
1150 | 1150 |
///\code |
1151 | 1151 |
/// bfs(g,source).predMap(preds).run(); |
1152 | 1152 |
///\endcode |
1153 | 1153 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1154 | 1154 |
///to the end of the parameter list. |
1155 | 1155 |
///\sa BfsWizard |
1156 | 1156 |
///\sa Bfs |
1157 | 1157 |
template<class GR> |
1158 | 1158 |
BfsWizard<BfsWizardBase<GR> > |
1159 | 1159 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1160 | 1160 |
{ |
1161 | 1161 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
#ifdef DOXYGEN |
1165 | 1165 |
/// \brief Visitor class for BFS. |
1166 | 1166 |
/// |
1167 | 1167 |
/// This class defines the interface of the BfsVisit events, and |
1168 | 1168 |
/// it could be the base of a real visitor class. |
1169 | 1169 |
template <typename _Digraph> |
1170 | 1170 |
struct BfsVisitor { |
1171 | 1171 |
typedef _Digraph Digraph; |
1172 | 1172 |
typedef typename Digraph::Arc Arc; |
1173 | 1173 |
typedef typename Digraph::Node Node; |
1174 | 1174 |
/// \brief Called for the source node(s) of the BFS. |
1175 | 1175 |
/// |
1176 | 1176 |
/// This function is called for the source node(s) of the BFS. |
1177 | 1177 |
void start(const Node& node) {} |
1178 | 1178 |
/// \brief Called when a node is reached first time. |
1179 | 1179 |
/// |
1180 | 1180 |
/// This function is called when a node is reached first time. |
1181 | 1181 |
void reach(const Node& node) {} |
1182 | 1182 |
/// \brief Called when a node is processed. |
1183 | 1183 |
/// |
1184 | 1184 |
/// This function is called when a node is processed. |
1185 | 1185 |
void process(const Node& node) {} |
1186 | 1186 |
/// \brief Called when an arc reaches a new node. |
1187 | 1187 |
/// |
1188 | 1188 |
/// This function is called when the BFS finds an arc whose target node |
1189 | 1189 |
/// is not reached yet. |
1190 | 1190 |
void discover(const Arc& arc) {} |
1191 | 1191 |
/// \brief Called when an arc is examined but its target node is |
1192 | 1192 |
/// already discovered. |
1193 | 1193 |
/// |
1194 | 1194 |
/// This function is called when an arc is examined but its target node is |
1195 | 1195 |
/// already discovered. |
1196 | 1196 |
void examine(const Arc& arc) {} |
1197 | 1197 |
}; |
1198 | 1198 |
#else |
1199 | 1199 |
template <typename _Digraph> |
1200 | 1200 |
struct BfsVisitor { |
1201 | 1201 |
typedef _Digraph Digraph; |
1202 | 1202 |
typedef typename Digraph::Arc Arc; |
1203 | 1203 |
typedef typename Digraph::Node Node; |
1204 | 1204 |
void start(const Node&) {} |
1205 | 1205 |
void reach(const Node&) {} |
1206 | 1206 |
void process(const Node&) {} |
1207 | 1207 |
void discover(const Arc&) {} |
1208 | 1208 |
void examine(const Arc&) {} |
1209 | 1209 |
|
1210 | 1210 |
template <typename _Visitor> |
1211 | 1211 |
struct Constraints { |
1212 | 1212 |
void constraints() { |
1213 | 1213 |
Arc arc; |
1214 | 1214 |
Node node; |
1215 | 1215 |
visitor.start(node); |
1216 | 1216 |
visitor.reach(node); |
1217 | 1217 |
visitor.process(node); |
1218 | 1218 |
visitor.discover(arc); |
1219 | 1219 |
visitor.examine(arc); |
1220 | 1220 |
} |
1221 | 1221 |
_Visitor& visitor; |
1222 | 1222 |
}; |
1223 | 1223 |
}; |
1224 | 1224 |
#endif |
1225 | 1225 |
|
1226 | 1226 |
/// \brief Default traits class of BfsVisit class. |
1227 | 1227 |
/// |
1228 | 1228 |
/// Default traits class of BfsVisit class. |
1229 | 1229 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1230 | 1230 |
template<class _Digraph> |
1231 | 1231 |
struct BfsVisitDefaultTraits { |
1232 | 1232 |
|
1233 | 1233 |
/// \brief The type of the digraph the algorithm runs on. |
1234 | 1234 |
typedef _Digraph Digraph; |
1235 | 1235 |
|
1236 | 1236 |
/// \brief The type of the map that indicates which nodes are reached. |
1237 | 1237 |
/// |
1238 | 1238 |
/// The type of the map that indicates which nodes are reached. |
1239 | 1239 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1240 | 1240 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1241 | 1241 |
|
1242 | 1242 |
/// \brief Instantiates a \ref ReachedMap. |
1243 | 1243 |
/// |
1244 | 1244 |
/// This function instantiates a \ref ReachedMap. |
1245 | 1245 |
/// \param digraph is the digraph, to which |
1246 | 1246 |
/// we would like to define the \ref ReachedMap. |
1247 | 1247 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1248 | 1248 |
return new ReachedMap(digraph); |
1249 | 1249 |
} |
1250 | 1250 |
|
1251 | 1251 |
}; |
1252 | 1252 |
|
1253 | 1253 |
/// \ingroup search |
1254 | 1254 |
/// |
1255 | 1255 |
/// \brief %BFS algorithm class with visitor interface. |
1256 | 1256 |
/// |
1257 | 1257 |
/// This class provides an efficient implementation of the %BFS algorithm |
1258 | 1258 |
/// with visitor interface. |
1259 | 1259 |
/// |
1260 | 1260 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1261 | 1261 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1262 | 1262 |
/// the member functions of the \c Visitor class on every BFS event. |
1263 | 1263 |
/// |
1264 |
/// This interface of the BFS algorithm should be used in special cases |
|
1265 |
/// when extra actions have to be performed in connection with certain |
|
1266 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
|
1267 |
/// instead. |
|
1268 |
/// |
|
1264 | 1269 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1265 | 1270 |
/// The default value is |
1266 | 1271 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1267 | 1272 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
1268 | 1273 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1269 | 1274 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
1270 | 1275 |
/// does not observe the BFS events. If you want to observe the BFS |
1271 | 1276 |
/// events, you should implement your own visitor class. |
1272 | 1277 |
/// \tparam _Traits Traits class to set various data types used by the |
1273 | 1278 |
/// algorithm. The default traits class is |
1274 | 1279 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1275 | 1280 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1276 | 1281 |
/// a BFS visit traits class. |
1277 | 1282 |
#ifdef DOXYGEN |
1278 | 1283 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1279 | 1284 |
#else |
1280 | 1285 |
template <typename _Digraph = ListDigraph, |
1281 | 1286 |
typename _Visitor = BfsVisitor<_Digraph>, |
1282 | 1287 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1283 | 1288 |
#endif |
1284 | 1289 |
class BfsVisit { |
1285 | 1290 |
public: |
1286 | 1291 |
|
1287 | 1292 |
/// \brief \ref Exception for uninitialized parameters. |
1288 | 1293 |
/// |
1289 | 1294 |
/// This error represents problems in the initialization |
1290 | 1295 |
/// of the parameters of the algorithm. |
1291 | 1296 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1292 | 1297 |
public: |
1293 | 1298 |
virtual const char* what() const throw() |
1294 | 1299 |
{ |
1295 | 1300 |
return "lemon::BfsVisit::UninitializedParameter"; |
1296 | 1301 |
} |
1297 | 1302 |
}; |
1298 | 1303 |
|
1299 | 1304 |
///The traits class. |
1300 | 1305 |
typedef _Traits Traits; |
1301 | 1306 |
|
1302 | 1307 |
///The type of the digraph the algorithm runs on. |
1303 | 1308 |
typedef typename Traits::Digraph Digraph; |
1304 | 1309 |
|
1305 | 1310 |
///The visitor type used by the algorithm. |
1306 | 1311 |
typedef _Visitor Visitor; |
1307 | 1312 |
|
1308 | 1313 |
///The type of the map that indicates which nodes are reached. |
1309 | 1314 |
typedef typename Traits::ReachedMap ReachedMap; |
1310 | 1315 |
|
1311 | 1316 |
private: |
1312 | 1317 |
|
1313 | 1318 |
typedef typename Digraph::Node Node; |
1314 | 1319 |
typedef typename Digraph::NodeIt NodeIt; |
1315 | 1320 |
typedef typename Digraph::Arc Arc; |
1316 | 1321 |
typedef typename Digraph::OutArcIt OutArcIt; |
1317 | 1322 |
|
1318 | 1323 |
//Pointer to the underlying digraph. |
1319 | 1324 |
const Digraph *_digraph; |
1320 | 1325 |
//Pointer to the visitor object. |
1321 | 1326 |
Visitor *_visitor; |
1322 | 1327 |
//Pointer to the map of reached status of the nodes. |
1323 | 1328 |
ReachedMap *_reached; |
1324 | 1329 |
//Indicates if _reached is locally allocated (true) or not. |
1325 | 1330 |
bool local_reached; |
1326 | 1331 |
|
1327 | 1332 |
std::vector<typename Digraph::Node> _list; |
1328 | 1333 |
int _list_front, _list_back; |
1329 | 1334 |
|
1330 | 1335 |
///Creates the maps if necessary. |
1331 | 1336 |
///\todo Better memory allocation (instead of new). |
1332 | 1337 |
void create_maps() { |
1333 | 1338 |
if(!_reached) { |
1334 | 1339 |
local_reached = true; |
1335 | 1340 |
_reached = Traits::createReachedMap(*_digraph); |
1336 | 1341 |
} |
1337 | 1342 |
} |
1338 | 1343 |
|
1339 | 1344 |
protected: |
1340 | 1345 |
|
1341 | 1346 |
BfsVisit() {} |
1342 | 1347 |
|
1343 | 1348 |
public: |
1344 | 1349 |
|
1345 | 1350 |
typedef BfsVisit Create; |
1346 | 1351 |
|
1347 | 1352 |
/// \name Named template parameters |
1348 | 1353 |
|
1349 | 1354 |
///@{ |
1350 | 1355 |
template <class T> |
1351 | 1356 |
struct SetReachedMapTraits : public Traits { |
1352 | 1357 |
typedef T ReachedMap; |
1353 | 1358 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1354 | 1359 |
throw UninitializedParameter(); |
1355 | 1360 |
} |
1356 | 1361 |
}; |
1357 | 1362 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1358 | 1363 |
/// ReachedMap type. |
1359 | 1364 |
/// |
1360 | 1365 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1361 | 1366 |
template <class T> |
1362 | 1367 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1363 | 1368 |
SetReachedMapTraits<T> > { |
1364 | 1369 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1365 | 1370 |
}; |
1366 | 1371 |
///@} |
1367 | 1372 |
|
1368 | 1373 |
public: |
1369 | 1374 |
|
1370 | 1375 |
/// \brief Constructor. |
1371 | 1376 |
/// |
1372 | 1377 |
/// Constructor. |
1373 | 1378 |
/// |
1374 | 1379 |
/// \param digraph The digraph the algorithm runs on. |
1375 | 1380 |
/// \param visitor The visitor object of the algorithm. |
1376 | 1381 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1377 | 1382 |
: _digraph(&digraph), _visitor(&visitor), |
1378 | 1383 |
_reached(0), local_reached(false) {} |
1379 | 1384 |
|
1380 | 1385 |
/// \brief Destructor. |
1381 | 1386 |
~BfsVisit() { |
1382 | 1387 |
if(local_reached) delete _reached; |
1383 | 1388 |
} |
1384 | 1389 |
|
1385 | 1390 |
/// \brief Sets the map that indicates which nodes are reached. |
1386 | 1391 |
/// |
1387 | 1392 |
/// Sets the map that indicates which nodes are reached. |
1388 | 1393 |
/// If you don't use this function before calling \ref run(), |
1389 | 1394 |
/// it will allocate one. The destructor deallocates this |
1390 | 1395 |
/// automatically allocated map, of course. |
1391 | 1396 |
/// \return <tt> (*this) </tt> |
1392 | 1397 |
BfsVisit &reachedMap(ReachedMap &m) { |
1393 | 1398 |
if(local_reached) { |
1394 | 1399 |
delete _reached; |
1395 | 1400 |
local_reached = false; |
1396 | 1401 |
} |
1397 | 1402 |
_reached = &m; |
1398 | 1403 |
return *this; |
1399 | 1404 |
} |
1400 | 1405 |
|
1401 | 1406 |
public: |
1402 | 1407 |
|
1403 | 1408 |
/// \name Execution control |
1404 | 1409 |
/// The simplest way to execute the algorithm is to use |
1405 | 1410 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1406 | 1411 |
/// "run()". |
1407 | 1412 |
/// \n |
1408 | 1413 |
/// If you need more control on the execution, first you must call |
1409 | 1414 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1410 | 1415 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1411 | 1416 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1412 | 1417 |
/// actual path computation. |
1413 | 1418 |
|
1414 | 1419 |
/// @{ |
1415 | 1420 |
|
1416 | 1421 |
/// \brief Initializes the internal data structures. |
1417 | 1422 |
/// |
1418 | 1423 |
/// Initializes the internal data structures. |
1419 | 1424 |
void init() { |
1420 | 1425 |
create_maps(); |
1421 | 1426 |
_list.resize(countNodes(*_digraph)); |
1422 | 1427 |
_list_front = _list_back = -1; |
1423 | 1428 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1424 | 1429 |
_reached->set(u, false); |
1425 | 1430 |
} |
1426 | 1431 |
} |
1427 | 1432 |
|
1428 | 1433 |
/// \brief Adds a new source node. |
1429 | 1434 |
/// |
1430 | 1435 |
/// Adds a new source node to the set of nodes to be processed. |
1431 | 1436 |
void addSource(Node s) { |
1432 | 1437 |
if(!(*_reached)[s]) { |
1433 | 1438 |
_reached->set(s,true); |
1434 | 1439 |
_visitor->start(s); |
1435 | 1440 |
_visitor->reach(s); |
1436 | 1441 |
_list[++_list_back] = s; |
1437 | 1442 |
} |
1438 | 1443 |
} |
1439 | 1444 |
|
1440 | 1445 |
/// \brief Processes the next node. |
1441 | 1446 |
/// |
1442 | 1447 |
/// Processes the next node. |
1443 | 1448 |
/// |
1444 | 1449 |
/// \return The processed node. |
1445 | 1450 |
/// |
1446 | 1451 |
/// \pre The queue must not be empty. |
1447 | 1452 |
Node processNextNode() { |
1448 | 1453 |
Node n = _list[++_list_front]; |
1449 | 1454 |
_visitor->process(n); |
1450 | 1455 |
Arc e; |
1451 | 1456 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1452 | 1457 |
Node m = _digraph->target(e); |
1453 | 1458 |
if (!(*_reached)[m]) { |
1454 | 1459 |
_visitor->discover(e); |
1455 | 1460 |
_visitor->reach(m); |
1456 | 1461 |
_reached->set(m, true); |
1457 | 1462 |
_list[++_list_back] = m; |
1458 | 1463 |
} else { |
1459 | 1464 |
_visitor->examine(e); |
1460 | 1465 |
} |
1461 | 1466 |
} |
1462 | 1467 |
return n; |
1463 | 1468 |
} |
1464 | 1469 |
|
1465 | 1470 |
/// \brief Processes the next node. |
1466 | 1471 |
/// |
1467 | 1472 |
/// Processes the next node and checks if the given target node |
1468 | 1473 |
/// is reached. If the target node is reachable from the processed |
1469 | 1474 |
/// node, then the \c reach parameter will be set to \c true. |
1470 | 1475 |
/// |
1471 | 1476 |
/// \param target The target node. |
1472 | 1477 |
/// \retval reach Indicates if the target node is reached. |
1473 | 1478 |
/// It should be initially \c false. |
1474 | 1479 |
/// |
1475 | 1480 |
/// \return The processed node. |
1476 | 1481 |
/// |
1477 | 1482 |
/// \pre The queue must not be empty. |
1478 | 1483 |
Node processNextNode(Node target, bool& reach) { |
1479 | 1484 |
Node n = _list[++_list_front]; |
1480 | 1485 |
_visitor->process(n); |
1481 | 1486 |
Arc e; |
1482 | 1487 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1483 | 1488 |
Node m = _digraph->target(e); |
1484 | 1489 |
if (!(*_reached)[m]) { |
1485 | 1490 |
_visitor->discover(e); |
1486 | 1491 |
_visitor->reach(m); |
1487 | 1492 |
_reached->set(m, true); |
1488 | 1493 |
_list[++_list_back] = m; |
1489 | 1494 |
reach = reach || (target == m); |
1490 | 1495 |
} else { |
1491 | 1496 |
_visitor->examine(e); |
1492 | 1497 |
} |
1493 | 1498 |
} |
1494 | 1499 |
return n; |
1495 | 1500 |
} |
1496 | 1501 |
|
1497 | 1502 |
/// \brief Processes the next node. |
1498 | 1503 |
/// |
1499 | 1504 |
/// Processes the next node and checks if at least one of reached |
1500 | 1505 |
/// nodes has \c true value in the \c nm node map. If one node |
1501 | 1506 |
/// with \c true value is reachable from the processed node, then the |
1502 | 1507 |
/// \c rnode parameter will be set to the first of such nodes. |
1503 | 1508 |
/// |
1504 | 1509 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1505 | 1510 |
/// possible targets. |
1506 | 1511 |
/// \retval rnode The reached target node. |
1507 | 1512 |
/// It should be initially \c INVALID. |
1508 | 1513 |
/// |
1509 | 1514 |
/// \return The processed node. |
1510 | 1515 |
/// |
1511 | 1516 |
/// \pre The queue must not be empty. |
1512 | 1517 |
template <typename NM> |
1513 | 1518 |
Node processNextNode(const NM& nm, Node& rnode) { |
1514 | 1519 |
Node n = _list[++_list_front]; |
1515 | 1520 |
_visitor->process(n); |
1516 | 1521 |
Arc e; |
1517 | 1522 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1518 | 1523 |
Node m = _digraph->target(e); |
1519 | 1524 |
if (!(*_reached)[m]) { |
1520 | 1525 |
_visitor->discover(e); |
1521 | 1526 |
_visitor->reach(m); |
1522 | 1527 |
_reached->set(m, true); |
1523 | 1528 |
_list[++_list_back] = m; |
1524 | 1529 |
if (nm[m] && rnode == INVALID) rnode = m; |
1525 | 1530 |
} else { |
1526 | 1531 |
_visitor->examine(e); |
1527 | 1532 |
} |
1528 | 1533 |
} |
1529 | 1534 |
return n; |
1530 | 1535 |
} |
1531 | 1536 |
|
1532 | 1537 |
/// \brief The next node to be processed. |
1533 | 1538 |
/// |
1534 | 1539 |
/// Returns the next node to be processed or \c INVALID if the queue |
1535 | 1540 |
/// is empty. |
1536 | 1541 |
Node nextNode() const { |
1537 | 1542 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1538 | 1543 |
} |
1539 | 1544 |
|
1540 | 1545 |
/// \brief Returns \c false if there are nodes |
1541 | 1546 |
/// to be processed. |
1542 | 1547 |
/// |
1543 | 1548 |
/// Returns \c false if there are nodes |
1544 | 1549 |
/// to be processed in the queue. |
1545 | 1550 |
bool emptyQueue() const { return _list_front == _list_back; } |
1546 | 1551 |
|
1547 | 1552 |
/// \brief Returns the number of the nodes to be processed. |
1548 | 1553 |
/// |
1549 | 1554 |
/// Returns the number of the nodes to be processed in the queue. |
1550 | 1555 |
int queueSize() const { return _list_back - _list_front; } |
1551 | 1556 |
|
1552 | 1557 |
/// \brief Executes the algorithm. |
1553 | 1558 |
/// |
1554 | 1559 |
/// Executes the algorithm. |
1555 | 1560 |
/// |
1556 | 1561 |
/// This method runs the %BFS algorithm from the root node(s) |
1557 | 1562 |
/// in order to compute the shortest path to each node. |
1558 | 1563 |
/// |
1559 | 1564 |
/// The algorithm computes |
1560 | 1565 |
/// - the shortest path tree (forest), |
1561 | 1566 |
/// - the distance of each node from the root(s). |
1562 | 1567 |
/// |
1563 | 1568 |
/// \pre init() must be called and at least one root node should be added |
1564 | 1569 |
/// with addSource() before using this function. |
1565 | 1570 |
/// |
1566 | 1571 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1567 | 1572 |
/// \code |
1568 | 1573 |
/// while ( !b.emptyQueue() ) { |
1569 | 1574 |
/// b.processNextNode(); |
1570 | 1575 |
/// } |
1571 | 1576 |
/// \endcode |
1572 | 1577 |
void start() { |
1573 | 1578 |
while ( !emptyQueue() ) processNextNode(); |
1574 | 1579 |
} |
1575 | 1580 |
|
1576 | 1581 |
/// \brief Executes the algorithm until the given target node is reached. |
1577 | 1582 |
/// |
1578 | 1583 |
/// Executes the algorithm until the given target node is reached. |
1579 | 1584 |
/// |
1580 | 1585 |
/// This method runs the %BFS algorithm from the root node(s) |
1581 | 1586 |
/// in order to compute the shortest path to \c dest. |
1582 | 1587 |
/// |
1583 | 1588 |
/// The algorithm computes |
1584 | 1589 |
/// - the shortest path to \c dest, |
1585 | 1590 |
/// - the distance of \c dest from the root(s). |
1586 | 1591 |
/// |
1587 | 1592 |
/// \pre init() must be called and at least one root node should be |
1588 | 1593 |
/// added with addSource() before using this function. |
1589 | 1594 |
/// |
1590 | 1595 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1591 | 1596 |
/// \code |
1592 | 1597 |
/// bool reach = false; |
1593 | 1598 |
/// while ( !b.emptyQueue() && !reach ) { |
1594 | 1599 |
/// b.processNextNode(t, reach); |
1595 | 1600 |
/// } |
1596 | 1601 |
/// \endcode |
1597 | 1602 |
void start(Node dest) { |
1598 | 1603 |
bool reach = false; |
1599 | 1604 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1600 | 1605 |
} |
1601 | 1606 |
|
1602 | 1607 |
/// \brief Executes the algorithm until a condition is met. |
1603 | 1608 |
/// |
1604 | 1609 |
/// Executes the algorithm until a condition is met. |
1605 | 1610 |
/// |
1606 | 1611 |
/// This method runs the %BFS algorithm from the root node(s) in |
1607 | 1612 |
/// order to compute the shortest path to a node \c v with |
1608 | 1613 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1609 | 1614 |
/// |
1610 | 1615 |
/// \param nm must be a bool (or convertible) node map. The |
1611 | 1616 |
/// algorithm will stop when it reaches a node \c v with |
1612 | 1617 |
/// <tt>nm[v]</tt> true. |
1613 | 1618 |
/// |
1614 | 1619 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1615 | 1620 |
/// \c INVALID if no such node was found. |
1616 | 1621 |
/// |
1617 | 1622 |
/// \pre init() must be called and at least one root node should be |
1618 | 1623 |
/// added with addSource() before using this function. |
1619 | 1624 |
/// |
1620 | 1625 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1621 | 1626 |
/// \code |
1622 | 1627 |
/// Node rnode = INVALID; |
1623 | 1628 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1624 | 1629 |
/// b.processNextNode(nm, rnode); |
1625 | 1630 |
/// } |
1626 | 1631 |
/// return rnode; |
1627 | 1632 |
/// \endcode |
1628 | 1633 |
template <typename NM> |
1629 | 1634 |
Node start(const NM &nm) { |
1630 | 1635 |
Node rnode = INVALID; |
1631 | 1636 |
while ( !emptyQueue() && rnode == INVALID ) { |
1632 | 1637 |
processNextNode(nm, rnode); |
1633 | 1638 |
} |
1634 | 1639 |
return rnode; |
1635 | 1640 |
} |
1636 | 1641 |
|
1637 | 1642 |
/// \brief Runs the algorithm from the given node. |
1638 | 1643 |
/// |
1639 | 1644 |
/// This method runs the %BFS algorithm from node \c s |
1640 | 1645 |
/// in order to compute the shortest path to each node. |
1641 | 1646 |
/// |
1642 | 1647 |
/// The algorithm computes |
1643 | 1648 |
/// - the shortest path tree, |
1644 | 1649 |
/// - the distance of each node from the root. |
1645 | 1650 |
/// |
1646 | 1651 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1647 | 1652 |
///\code |
1648 | 1653 |
/// b.init(); |
1649 | 1654 |
/// b.addSource(s); |
1650 | 1655 |
/// b.start(); |
1651 | 1656 |
///\endcode |
1652 | 1657 |
void run(Node s) { |
1653 | 1658 |
init(); |
1654 | 1659 |
addSource(s); |
1655 | 1660 |
start(); |
1656 | 1661 |
} |
1657 | 1662 |
|
1658 | 1663 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1659 | 1664 |
/// |
1660 | 1665 |
/// This method runs the %BFS algorithm in order to |
1661 | 1666 |
/// compute the shortest path to each node. |
1662 | 1667 |
/// |
1663 | 1668 |
/// The algorithm computes |
1664 | 1669 |
/// - the shortest path tree (forest), |
1665 | 1670 |
/// - the distance of each node from the root(s). |
1666 | 1671 |
/// |
1667 | 1672 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1668 | 1673 |
///\code |
1669 | 1674 |
/// b.init(); |
1670 | 1675 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1671 | 1676 |
/// if (!b.reached(n)) { |
1672 | 1677 |
/// b.addSource(n); |
1673 | 1678 |
/// b.start(); |
1674 | 1679 |
/// } |
1675 | 1680 |
/// } |
1676 | 1681 |
///\endcode |
1677 | 1682 |
void run() { |
1678 | 1683 |
init(); |
1679 | 1684 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1680 | 1685 |
if (!reached(it)) { |
1681 | 1686 |
addSource(it); |
1682 | 1687 |
start(); |
1683 | 1688 |
} |
1684 | 1689 |
} |
1685 | 1690 |
} |
1686 | 1691 |
|
1687 | 1692 |
///@} |
1688 | 1693 |
|
1689 | 1694 |
/// \name Query Functions |
1690 | 1695 |
/// The result of the %BFS algorithm can be obtained using these |
1691 | 1696 |
/// functions.\n |
1692 | 1697 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1693 | 1698 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1694 | 1699 |
/// using them. |
1695 | 1700 |
///@{ |
1696 | 1701 |
|
1697 | 1702 |
/// \brief Checks if a node is reachable from the root(s). |
1698 | 1703 |
/// |
1699 | 1704 |
/// Returns \c true if \c v is reachable from the root(s). |
1700 | 1705 |
/// \pre Either \ref run() or \ref start() |
1701 | 1706 |
/// must be called before using this function. |
1702 | 1707 |
bool reached(Node v) { return (*_reached)[v]; } |
1703 | 1708 |
|
1704 | 1709 |
///@} |
1705 | 1710 |
|
1706 | 1711 |
}; |
1707 | 1712 |
|
1708 | 1713 |
} //END OF NAMESPACE LEMON |
1709 | 1714 |
|
1710 | 1715 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
#include <lemon/bits/default_map.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup digraphbits |
32 | 32 |
///\file |
33 | 33 |
///\brief Extenders for the digraph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \ingroup digraphbits |
37 | 37 |
/// |
38 | 38 |
/// \brief BaseDigraph to BaseGraph extender |
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 |
// |
|
62 |
// Invalid arc constructor |
|
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 |
/// First node of the edge |
|
78 |
Node u(const Edge &e) const { |
|
79 |
return Parent::source(e); |
|
80 |
} |
|
77 | 81 |
|
78 |
|
|
79 |
using Parent::source; |
|
80 |
|
|
81 |
/// Source of the given Arc. |
|
82 |
/// Source of the given arc |
|
82 | 83 |
Node source(const Arc &e) const { |
83 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
84 | 85 |
} |
85 | 86 |
|
86 |
|
|
87 |
/// Second node of the edge |
|
88 |
Node v(const Edge &e) const { |
|
89 |
return Parent::target(e); |
|
90 |
} |
|
87 | 91 |
|
88 |
/// Target of the given |
|
92 |
/// Target of the given arc |
|
89 | 93 |
Node target(const Arc &e) const { |
90 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
91 | 95 |
} |
92 | 96 |
|
93 | 97 |
/// \brief Directed arc from an edge. |
94 | 98 |
/// |
95 |
/// Returns a directed arc corresponding to the specified Edge. |
|
96 |
/// If the given bool is true the given edge and the |
|
97 |
/// returned arc have the same source node. |
|
98 |
static Arc direct(const Edge &ue, bool d) { |
|
99 |
|
|
99 |
/// Returns a directed arc corresponding to the specified edge. |
|
100 |
/// If the given bool is true, the first node of the given edge and |
|
101 |
/// the source node of the returned arc are the same. |
|
102 |
static Arc direct(const Edge &e, bool d) { |
|
103 |
return Arc(e, d); |
|
100 | 104 |
} |
101 | 105 |
|
102 |
/// Returns whether the given directed arc is same orientation as the |
|
103 |
/// corresponding edge. |
|
106 |
/// Returns whether the given directed arc has the same orientation |
|
107 |
/// as the corresponding edge. |
|
104 | 108 |
/// |
105 | 109 |
/// \todo reference to the corresponding point of the undirected digraph |
106 | 110 |
/// concept. "What does the direction of an edge mean?" |
107 |
static bool direction(const Arc &e) { return e.forward; } |
|
108 |
|
|
111 |
static bool direction(const Arc &a) { return a.forward; } |
|
109 | 112 |
|
110 | 113 |
using Parent::first; |
111 | 114 |
using Parent::next; |
112 | 115 |
|
113 | 116 |
void first(Arc &e) const { |
114 | 117 |
Parent::first(e); |
115 | 118 |
e.forward=true; |
116 | 119 |
} |
117 | 120 |
|
118 | 121 |
void next(Arc &e) const { |
119 | 122 |
if( e.forward ) { |
120 | 123 |
e.forward = false; |
121 | 124 |
} |
122 | 125 |
else { |
123 | 126 |
Parent::next(e); |
124 | 127 |
e.forward = true; |
125 | 128 |
} |
126 | 129 |
} |
127 | 130 |
|
128 | 131 |
void firstOut(Arc &e, const Node &n) const { |
129 | 132 |
Parent::firstIn(e,n); |
130 | 133 |
if( Edge(e) != INVALID ) { |
131 | 134 |
e.forward = false; |
132 | 135 |
} |
133 | 136 |
else { |
134 | 137 |
Parent::firstOut(e,n); |
135 | 138 |
e.forward = true; |
136 | 139 |
} |
137 | 140 |
} |
138 | 141 |
void nextOut(Arc &e) const { |
139 | 142 |
if( ! e.forward ) { |
140 | 143 |
Node n = Parent::target(e); |
141 | 144 |
Parent::nextIn(e); |
142 | 145 |
if( Edge(e) == INVALID ) { |
143 | 146 |
Parent::firstOut(e, n); |
144 | 147 |
e.forward = true; |
145 | 148 |
} |
146 | 149 |
} |
147 | 150 |
else { |
148 | 151 |
Parent::nextOut(e); |
149 | 152 |
} |
150 | 153 |
} |
151 | 154 |
|
152 | 155 |
void firstIn(Arc &e, const Node &n) const { |
153 | 156 |
Parent::firstOut(e,n); |
154 | 157 |
if( Edge(e) != INVALID ) { |
155 | 158 |
e.forward = false; |
156 | 159 |
} |
157 | 160 |
else { |
158 | 161 |
Parent::firstIn(e,n); |
159 | 162 |
e.forward = true; |
160 | 163 |
} |
161 | 164 |
} |
162 | 165 |
void nextIn(Arc &e) const { |
163 | 166 |
if( ! e.forward ) { |
164 | 167 |
Node n = Parent::source(e); |
165 | 168 |
Parent::nextOut(e); |
166 | 169 |
if( Edge(e) == INVALID ) { |
167 | 170 |
Parent::firstIn(e, n); |
168 | 171 |
e.forward = true; |
169 | 172 |
} |
170 | 173 |
} |
171 | 174 |
else { |
172 | 175 |
Parent::nextIn(e); |
173 | 176 |
} |
174 | 177 |
} |
175 | 178 |
|
176 | 179 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
177 | 180 |
d = true; |
178 | 181 |
Parent::firstOut(e, n); |
179 | 182 |
if (e != INVALID) return; |
180 | 183 |
d = false; |
181 | 184 |
Parent::firstIn(e, n); |
182 | 185 |
} |
183 | 186 |
|
184 | 187 |
void nextInc(Edge &e, bool &d) const { |
185 | 188 |
if (d) { |
186 | 189 |
Node s = Parent::source(e); |
187 | 190 |
Parent::nextOut(e); |
188 | 191 |
if (e != INVALID) return; |
189 | 192 |
d = false; |
190 | 193 |
Parent::firstIn(e, s); |
191 | 194 |
} else { |
192 | 195 |
Parent::nextIn(e); |
193 | 196 |
} |
194 | 197 |
} |
195 | 198 |
|
196 | 199 |
Node nodeFromId(int ix) const { |
197 | 200 |
return Parent::nodeFromId(ix); |
198 | 201 |
} |
199 | 202 |
|
200 | 203 |
Arc arcFromId(int ix) const { |
201 | 204 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
202 | 205 |
} |
203 | 206 |
|
204 | 207 |
Edge edgeFromId(int ix) const { |
205 | 208 |
return Parent::arcFromId(ix); |
206 | 209 |
} |
207 | 210 |
|
208 | 211 |
int id(const Node &n) const { |
209 | 212 |
return Parent::id(n); |
210 | 213 |
} |
211 | 214 |
|
212 | 215 |
int id(const Edge &e) const { |
213 | 216 |
return Parent::id(e); |
214 | 217 |
} |
215 | 218 |
|
216 | 219 |
int id(const Arc &e) const { |
217 | 220 |
return 2 * Parent::id(e) + int(e.forward); |
218 | 221 |
} |
219 | 222 |
|
220 | 223 |
int maxNodeId() const { |
221 | 224 |
return Parent::maxNodeId(); |
222 | 225 |
} |
223 | 226 |
|
224 | 227 |
int maxArcId() const { |
225 | 228 |
return 2 * Parent::maxArcId() + 1; |
226 | 229 |
} |
227 | 230 |
|
228 | 231 |
int maxEdgeId() const { |
229 | 232 |
return Parent::maxArcId(); |
230 | 233 |
} |
231 | 234 |
|
232 |
|
|
233 | 235 |
int arcNum() const { |
234 | 236 |
return 2 * Parent::arcNum(); |
235 | 237 |
} |
236 | 238 |
|
237 | 239 |
int edgeNum() const { |
238 | 240 |
return Parent::arcNum(); |
239 | 241 |
} |
240 | 242 |
|
241 | 243 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
242 | 244 |
if (p == INVALID) { |
243 | 245 |
Edge arc = Parent::findArc(s, t); |
244 | 246 |
if (arc != INVALID) return direct(arc, true); |
245 | 247 |
arc = Parent::findArc(t, s); |
246 | 248 |
if (arc != INVALID) return direct(arc, false); |
247 | 249 |
} else if (direction(p)) { |
248 | 250 |
Edge arc = Parent::findArc(s, t, p); |
249 | 251 |
if (arc != INVALID) return direct(arc, true); |
250 | 252 |
arc = Parent::findArc(t, s); |
251 | 253 |
if (arc != INVALID) return direct(arc, false); |
252 | 254 |
} else { |
253 | 255 |
Edge arc = Parent::findArc(t, s, p); |
254 | 256 |
if (arc != INVALID) return direct(arc, false); |
255 | 257 |
} |
256 | 258 |
return INVALID; |
257 | 259 |
} |
258 | 260 |
|
259 | 261 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
260 | 262 |
if (s != t) { |
261 | 263 |
if (p == INVALID) { |
262 | 264 |
Edge arc = Parent::findArc(s, t); |
263 | 265 |
if (arc != INVALID) return arc; |
264 | 266 |
arc = Parent::findArc(t, s); |
265 | 267 |
if (arc != INVALID) return arc; |
266 | 268 |
} else if (Parent::s(p) == s) { |
267 | 269 |
Edge arc = Parent::findArc(s, t, p); |
268 | 270 |
if (arc != INVALID) return arc; |
269 | 271 |
arc = Parent::findArc(t, s); |
270 | 272 |
if (arc != INVALID) return arc; |
271 | 273 |
} else { |
272 | 274 |
Edge arc = Parent::findArc(t, s, p); |
273 | 275 |
if (arc != INVALID) return arc; |
274 | 276 |
} |
275 | 277 |
} else { |
276 | 278 |
return Parent::findArc(s, t, p); |
277 | 279 |
} |
278 | 280 |
return INVALID; |
279 | 281 |
} |
280 | 282 |
}; |
281 | 283 |
|
282 | 284 |
template <typename Base> |
283 | 285 |
class BidirBpGraphExtender : public Base { |
284 | 286 |
public: |
285 | 287 |
typedef Base Parent; |
286 | 288 |
typedef BidirBpGraphExtender Digraph; |
287 | 289 |
|
288 | 290 |
typedef typename Parent::Node Node; |
289 | 291 |
typedef typename Parent::Edge Edge; |
290 | 292 |
|
291 | 293 |
|
292 | 294 |
using Parent::first; |
293 | 295 |
using Parent::next; |
294 | 296 |
|
295 | 297 |
using Parent::id; |
296 | 298 |
|
297 | 299 |
class Red : public Node { |
298 | 300 |
friend class BidirBpGraphExtender; |
299 | 301 |
public: |
300 | 302 |
Red() {} |
301 | 303 |
Red(const Node& node) : Node(node) { |
302 | 304 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
303 | 305 |
typename Parent::NodeSetError()); |
304 | 306 |
} |
305 | 307 |
Red& operator=(const Node& node) { |
306 | 308 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
307 | 309 |
typename Parent::NodeSetError()); |
308 | 310 |
Node::operator=(node); |
309 | 311 |
return *this; |
310 | 312 |
} |
311 | 313 |
Red(Invalid) : Node(INVALID) {} |
312 | 314 |
Red& operator=(Invalid) { |
313 | 315 |
Node::operator=(INVALID); |
314 | 316 |
return *this; |
315 | 317 |
} |
316 | 318 |
}; |
317 | 319 |
|
318 | 320 |
void first(Red& node) const { |
319 | 321 |
Parent::firstRed(static_cast<Node&>(node)); |
320 | 322 |
} |
321 | 323 |
void next(Red& node) const { |
322 | 324 |
Parent::nextRed(static_cast<Node&>(node)); |
323 | 325 |
} |
324 | 326 |
|
325 | 327 |
int id(const Red& node) const { |
326 | 328 |
return Parent::redId(node); |
327 | 329 |
} |
328 | 330 |
|
329 | 331 |
class Blue : public Node { |
330 | 332 |
friend class BidirBpGraphExtender; |
331 | 333 |
public: |
332 | 334 |
Blue() {} |
333 | 335 |
Blue(const Node& node) : Node(node) { |
334 | 336 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
335 | 337 |
typename Parent::NodeSetError()); |
336 | 338 |
} |
337 | 339 |
Blue& operator=(const Node& node) { |
338 | 340 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
339 | 341 |
typename Parent::NodeSetError()); |
340 | 342 |
Node::operator=(node); |
341 | 343 |
return *this; |
342 | 344 |
} |
343 | 345 |
Blue(Invalid) : Node(INVALID) {} |
344 | 346 |
Blue& operator=(Invalid) { |
345 | 347 |
Node::operator=(INVALID); |
346 | 348 |
return *this; |
347 | 349 |
} |
348 | 350 |
}; |
349 | 351 |
|
350 | 352 |
void first(Blue& node) const { |
351 | 353 |
Parent::firstBlue(static_cast<Node&>(node)); |
352 | 354 |
} |
353 | 355 |
void next(Blue& node) const { |
354 | 356 |
Parent::nextBlue(static_cast<Node&>(node)); |
355 | 357 |
} |
356 | 358 |
|
357 | 359 |
int id(const Blue& node) const { |
358 | 360 |
return Parent::redId(node); |
359 | 361 |
} |
360 | 362 |
|
361 | 363 |
Node source(const Edge& arc) const { |
362 | 364 |
return red(arc); |
363 | 365 |
} |
364 | 366 |
Node target(const Edge& arc) const { |
365 | 367 |
return blue(arc); |
366 | 368 |
} |
367 | 369 |
|
368 | 370 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
369 | 371 |
if (Parent::red(node)) { |
370 | 372 |
Parent::firstFromRed(arc, node); |
371 | 373 |
dir = true; |
372 | 374 |
} else { |
373 | 375 |
Parent::firstFromBlue(arc, node); |
374 | 376 |
dir = static_cast<Edge&>(arc) == INVALID; |
375 | 377 |
} |
376 | 378 |
} |
377 | 379 |
void nextInc(Edge& arc, bool& dir) const { |
378 | 380 |
if (dir) { |
379 | 381 |
Parent::nextFromRed(arc); |
380 | 382 |
} else { |
381 | 383 |
Parent::nextFromBlue(arc); |
382 | 384 |
if (arc == INVALID) dir = true; |
383 | 385 |
} |
384 | 386 |
} |
385 | 387 |
|
386 | 388 |
class Arc : public Edge { |
387 | 389 |
friend class BidirBpGraphExtender; |
388 | 390 |
protected: |
389 | 391 |
bool forward; |
390 | 392 |
|
391 | 393 |
Arc(const Edge& arc, bool _forward) |
392 | 394 |
: Edge(arc), forward(_forward) {} |
393 | 395 |
|
394 | 396 |
public: |
395 | 397 |
Arc() {} |
396 | 398 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
397 | 399 |
bool operator==(const Arc& i) const { |
398 | 400 |
return Edge::operator==(i) && forward == i.forward; |
399 | 401 |
} |
400 | 402 |
bool operator!=(const Arc& i) const { |
401 | 403 |
return Edge::operator!=(i) || forward != i.forward; |
402 | 404 |
} |
403 | 405 |
bool operator<(const Arc& i) const { |
404 | 406 |
return Edge::operator<(i) || |
405 | 407 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
406 | 408 |
} |
407 | 409 |
}; |
408 | 410 |
|
409 | 411 |
void first(Arc& arc) const { |
410 | 412 |
Parent::first(static_cast<Edge&>(arc)); |
411 | 413 |
arc.forward = true; |
412 | 414 |
} |
413 | 415 |
|
414 | 416 |
void next(Arc& arc) const { |
415 | 417 |
if (!arc.forward) { |
416 | 418 |
Parent::next(static_cast<Edge&>(arc)); |
417 | 419 |
} |
418 | 420 |
arc.forward = !arc.forward; |
419 | 421 |
} |
420 | 422 |
|
421 | 423 |
void firstOut(Arc& arc, const Node& node) const { |
422 | 424 |
if (Parent::red(node)) { |
423 | 425 |
Parent::firstFromRed(arc, node); |
424 | 426 |
arc.forward = true; |
425 | 427 |
} else { |
426 | 428 |
Parent::firstFromBlue(arc, node); |
427 | 429 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
428 | 430 |
} |
429 | 431 |
} |
430 | 432 |
void nextOut(Arc& arc) const { |
431 | 433 |
if (arc.forward) { |
432 | 434 |
Parent::nextFromRed(arc); |
433 | 435 |
} else { |
434 | 436 |
Parent::nextFromBlue(arc); |
435 | 437 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
436 | 438 |
} |
437 | 439 |
} |
438 | 440 |
|
439 | 441 |
void firstIn(Arc& arc, const Node& node) const { |
440 | 442 |
if (Parent::blue(node)) { |
441 | 443 |
Parent::firstFromBlue(arc, node); |
442 | 444 |
arc.forward = true; |
443 | 445 |
} else { |
444 | 446 |
Parent::firstFromRed(arc, node); |
445 | 447 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
446 | 448 |
} |
447 | 449 |
} |
448 | 450 |
void nextIn(Arc& arc) const { |
449 | 451 |
if (arc.forward) { |
450 | 452 |
Parent::nextFromBlue(arc); |
451 | 453 |
} else { |
452 | 454 |
Parent::nextFromRed(arc); |
453 | 455 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
454 | 456 |
} |
455 | 457 |
} |
456 | 458 |
|
457 | 459 |
Node source(const Arc& arc) const { |
458 | 460 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
459 | 461 |
} |
460 | 462 |
Node target(const Arc& arc) const { |
461 | 463 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
462 | 464 |
} |
463 | 465 |
|
464 | 466 |
int id(const Arc& arc) const { |
465 | 467 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
466 | 468 |
(arc.forward ? 0 : 1); |
467 | 469 |
} |
468 | 470 |
Arc arcFromId(int ix) const { |
469 | 471 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
470 | 472 |
} |
471 | 473 |
int maxArcId() const { |
472 | 474 |
return (Parent::maxEdgeId() << 1) + 1; |
473 | 475 |
} |
474 | 476 |
|
475 | 477 |
bool direction(const Arc& arc) const { |
476 | 478 |
return arc.forward; |
477 | 479 |
} |
478 | 480 |
|
479 | 481 |
Arc direct(const Edge& arc, bool dir) const { |
480 | 482 |
return Arc(arc, dir); |
481 | 483 |
} |
482 | 484 |
|
483 | 485 |
int arcNum() const { |
484 | 486 |
return 2 * Parent::edgeNum(); |
485 | 487 |
} |
486 | 488 |
|
487 | 489 |
int edgeNum() const { |
488 | 490 |
return Parent::edgeNum(); |
489 | 491 |
} |
490 | 492 |
|
491 | 493 |
|
492 | 494 |
}; |
493 | 495 |
} |
494 | 496 |
|
495 | 497 |
#endif |
... | ... |
@@ -443,1177 +443,1182 @@ |
443 | 443 |
} |
444 | 444 |
} |
445 | 445 |
|
446 | 446 |
///Adds a new source node. |
447 | 447 |
|
448 | 448 |
///Adds a new source node to the set of nodes to be processed. |
449 | 449 |
/// |
450 | 450 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
451 | 451 |
///false results.) |
452 | 452 |
/// |
453 | 453 |
///\warning Distances will be wrong (or at least strange) in case of |
454 | 454 |
///multiple sources. |
455 | 455 |
void addSource(Node s) |
456 | 456 |
{ |
457 | 457 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
458 | 458 |
if(!(*_reached)[s]) |
459 | 459 |
{ |
460 | 460 |
_reached->set(s,true); |
461 | 461 |
_pred->set(s,INVALID); |
462 | 462 |
OutArcIt e(*G,s); |
463 | 463 |
if(e!=INVALID) { |
464 | 464 |
_stack[++_stack_head]=e; |
465 | 465 |
_dist->set(s,_stack_head); |
466 | 466 |
} |
467 | 467 |
else { |
468 | 468 |
_processed->set(s,true); |
469 | 469 |
_dist->set(s,0); |
470 | 470 |
} |
471 | 471 |
} |
472 | 472 |
} |
473 | 473 |
|
474 | 474 |
///Processes the next arc. |
475 | 475 |
|
476 | 476 |
///Processes the next arc. |
477 | 477 |
/// |
478 | 478 |
///\return The processed arc. |
479 | 479 |
/// |
480 | 480 |
///\pre The stack must not be empty. |
481 | 481 |
Arc processNextArc() |
482 | 482 |
{ |
483 | 483 |
Node m; |
484 | 484 |
Arc e=_stack[_stack_head]; |
485 | 485 |
if(!(*_reached)[m=G->target(e)]) { |
486 | 486 |
_pred->set(m,e); |
487 | 487 |
_reached->set(m,true); |
488 | 488 |
++_stack_head; |
489 | 489 |
_stack[_stack_head] = OutArcIt(*G, m); |
490 | 490 |
_dist->set(m,_stack_head); |
491 | 491 |
} |
492 | 492 |
else { |
493 | 493 |
m=G->source(e); |
494 | 494 |
++_stack[_stack_head]; |
495 | 495 |
} |
496 | 496 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
497 | 497 |
_processed->set(m,true); |
498 | 498 |
--_stack_head; |
499 | 499 |
if(_stack_head>=0) { |
500 | 500 |
m=G->source(_stack[_stack_head]); |
501 | 501 |
++_stack[_stack_head]; |
502 | 502 |
} |
503 | 503 |
} |
504 | 504 |
return e; |
505 | 505 |
} |
506 | 506 |
|
507 | 507 |
///Next arc to be processed. |
508 | 508 |
|
509 | 509 |
///Next arc to be processed. |
510 | 510 |
/// |
511 | 511 |
///\return The next arc to be processed or \c INVALID if the stack |
512 | 512 |
///is empty. |
513 | 513 |
OutArcIt nextArc() const |
514 | 514 |
{ |
515 | 515 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
516 | 516 |
} |
517 | 517 |
|
518 | 518 |
///\brief Returns \c false if there are nodes |
519 | 519 |
///to be processed. |
520 | 520 |
/// |
521 | 521 |
///Returns \c false if there are nodes |
522 | 522 |
///to be processed in the queue (stack). |
523 | 523 |
bool emptyQueue() const { return _stack_head<0; } |
524 | 524 |
|
525 | 525 |
///Returns the number of the nodes to be processed. |
526 | 526 |
|
527 | 527 |
///Returns the number of the nodes to be processed in the queue (stack). |
528 | 528 |
int queueSize() const { return _stack_head+1; } |
529 | 529 |
|
530 | 530 |
///Executes the algorithm. |
531 | 531 |
|
532 | 532 |
///Executes the algorithm. |
533 | 533 |
/// |
534 | 534 |
///This method runs the %DFS algorithm from the root node |
535 | 535 |
///in order to compute the DFS path to each node. |
536 | 536 |
/// |
537 | 537 |
/// The algorithm computes |
538 | 538 |
///- the %DFS tree, |
539 | 539 |
///- the distance of each node from the root in the %DFS tree. |
540 | 540 |
/// |
541 | 541 |
///\pre init() must be called and a root node should be |
542 | 542 |
///added with addSource() before using this function. |
543 | 543 |
/// |
544 | 544 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
545 | 545 |
///\code |
546 | 546 |
/// while ( !d.emptyQueue() ) { |
547 | 547 |
/// d.processNextArc(); |
548 | 548 |
/// } |
549 | 549 |
///\endcode |
550 | 550 |
void start() |
551 | 551 |
{ |
552 | 552 |
while ( !emptyQueue() ) processNextArc(); |
553 | 553 |
} |
554 | 554 |
|
555 | 555 |
///Executes the algorithm until the given target node is reached. |
556 | 556 |
|
557 | 557 |
///Executes the algorithm until the given target node is reached. |
558 | 558 |
/// |
559 | 559 |
///This method runs the %DFS algorithm from the root node |
560 | 560 |
///in order to compute the DFS path to \c dest. |
561 | 561 |
/// |
562 | 562 |
///The algorithm computes |
563 | 563 |
///- the %DFS path to \c dest, |
564 | 564 |
///- the distance of \c dest from the root in the %DFS tree. |
565 | 565 |
/// |
566 | 566 |
///\pre init() must be called and a root node should be |
567 | 567 |
///added with addSource() before using this function. |
568 | 568 |
void start(Node dest) |
569 | 569 |
{ |
570 | 570 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) |
571 | 571 |
processNextArc(); |
572 | 572 |
} |
573 | 573 |
|
574 | 574 |
///Executes the algorithm until a condition is met. |
575 | 575 |
|
576 | 576 |
///Executes the algorithm until a condition is met. |
577 | 577 |
/// |
578 | 578 |
///This method runs the %DFS algorithm from the root node |
579 | 579 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
580 | 580 |
/// |
581 | 581 |
///\param am A \c bool (or convertible) arc map. The algorithm |
582 | 582 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
583 | 583 |
/// |
584 | 584 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
585 | 585 |
///\c INVALID if no such arc was found. |
586 | 586 |
/// |
587 | 587 |
///\pre init() must be called and a root node should be |
588 | 588 |
///added with addSource() before using this function. |
589 | 589 |
/// |
590 | 590 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
591 | 591 |
///not a node map. |
592 | 592 |
template<class ArcBoolMap> |
593 | 593 |
Arc start(const ArcBoolMap &am) |
594 | 594 |
{ |
595 | 595 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
596 | 596 |
processNextArc(); |
597 | 597 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
598 | 598 |
} |
599 | 599 |
|
600 | 600 |
///Runs the algorithm from the given node. |
601 | 601 |
|
602 | 602 |
///This method runs the %DFS algorithm from node \c s |
603 | 603 |
///in order to compute the DFS path to each node. |
604 | 604 |
/// |
605 | 605 |
///The algorithm computes |
606 | 606 |
///- the %DFS tree, |
607 | 607 |
///- the distance of each node from the root in the %DFS tree. |
608 | 608 |
/// |
609 | 609 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
610 | 610 |
///\code |
611 | 611 |
/// d.init(); |
612 | 612 |
/// d.addSource(s); |
613 | 613 |
/// d.start(); |
614 | 614 |
///\endcode |
615 | 615 |
void run(Node s) { |
616 | 616 |
init(); |
617 | 617 |
addSource(s); |
618 | 618 |
start(); |
619 | 619 |
} |
620 | 620 |
|
621 | 621 |
///Finds the %DFS path between \c s and \c t. |
622 | 622 |
|
623 | 623 |
///This method runs the %DFS algorithm from node \c s |
624 | 624 |
///in order to compute the DFS path to \c t. |
625 | 625 |
/// |
626 | 626 |
///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
627 | 627 |
///if \c t is reachable form \c s, \c 0 otherwise. |
628 | 628 |
/// |
629 | 629 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
630 | 630 |
///just a shortcut of the following code. |
631 | 631 |
///\code |
632 | 632 |
/// d.init(); |
633 | 633 |
/// d.addSource(s); |
634 | 634 |
/// d.start(t); |
635 | 635 |
///\endcode |
636 | 636 |
int run(Node s,Node t) { |
637 | 637 |
init(); |
638 | 638 |
addSource(s); |
639 | 639 |
start(t); |
640 | 640 |
return reached(t)?_stack_head+1:0; |
641 | 641 |
} |
642 | 642 |
|
643 | 643 |
///Runs the algorithm to visit all nodes in the digraph. |
644 | 644 |
|
645 | 645 |
///This method runs the %DFS algorithm in order to compute the |
646 | 646 |
///%DFS path to each node. |
647 | 647 |
/// |
648 | 648 |
///The algorithm computes |
649 | 649 |
///- the %DFS tree, |
650 | 650 |
///- the distance of each node from the root in the %DFS tree. |
651 | 651 |
/// |
652 | 652 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
653 | 653 |
///\code |
654 | 654 |
/// d.init(); |
655 | 655 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
656 | 656 |
/// if (!d.reached(n)) { |
657 | 657 |
/// d.addSource(n); |
658 | 658 |
/// d.start(); |
659 | 659 |
/// } |
660 | 660 |
/// } |
661 | 661 |
///\endcode |
662 | 662 |
void run() { |
663 | 663 |
init(); |
664 | 664 |
for (NodeIt it(*G); it != INVALID; ++it) { |
665 | 665 |
if (!reached(it)) { |
666 | 666 |
addSource(it); |
667 | 667 |
start(); |
668 | 668 |
} |
669 | 669 |
} |
670 | 670 |
} |
671 | 671 |
|
672 | 672 |
///@} |
673 | 673 |
|
674 | 674 |
///\name Query Functions |
675 | 675 |
///The result of the %DFS algorithm can be obtained using these |
676 | 676 |
///functions.\n |
677 | 677 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
678 | 678 |
///"start()" must be called before using them. |
679 | 679 |
|
680 | 680 |
///@{ |
681 | 681 |
|
682 | 682 |
///The DFS path to a node. |
683 | 683 |
|
684 | 684 |
///Returns the DFS path to a node. |
685 | 685 |
/// |
686 | 686 |
///\warning \c t should be reachable from the root. |
687 | 687 |
/// |
688 | 688 |
///\pre Either \ref run() or \ref start() must be called before |
689 | 689 |
///using this function. |
690 | 690 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
691 | 691 |
|
692 | 692 |
///The distance of a node from the root. |
693 | 693 |
|
694 | 694 |
///Returns the distance of a node from the root. |
695 | 695 |
/// |
696 | 696 |
///\warning If node \c v is not reachable from the root, then |
697 | 697 |
///the return value of this function is undefined. |
698 | 698 |
/// |
699 | 699 |
///\pre Either \ref run() or \ref start() must be called before |
700 | 700 |
///using this function. |
701 | 701 |
int dist(Node v) const { return (*_dist)[v]; } |
702 | 702 |
|
703 | 703 |
///Returns the 'previous arc' of the %DFS tree for a node. |
704 | 704 |
|
705 | 705 |
///This function returns the 'previous arc' of the %DFS tree for the |
706 | 706 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
707 | 707 |
///root to \c v. It is \c INVALID |
708 | 708 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
709 | 709 |
/// |
710 | 710 |
///The %DFS tree used here is equal to the %DFS tree used in |
711 | 711 |
///\ref predNode(). |
712 | 712 |
/// |
713 | 713 |
///\pre Either \ref run() or \ref start() must be called before using |
714 | 714 |
///this function. |
715 | 715 |
Arc predArc(Node v) const { return (*_pred)[v];} |
716 | 716 |
|
717 | 717 |
///Returns the 'previous node' of the %DFS tree. |
718 | 718 |
|
719 | 719 |
///This function returns the 'previous node' of the %DFS |
720 | 720 |
///tree for the node \c v, i.e. it returns the last but one node |
721 | 721 |
///from a %DFS path from the root to \c v. It is \c INVALID |
722 | 722 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
723 | 723 |
/// |
724 | 724 |
///The %DFS tree used here is equal to the %DFS tree used in |
725 | 725 |
///\ref predArc(). |
726 | 726 |
/// |
727 | 727 |
///\pre Either \ref run() or \ref start() must be called before |
728 | 728 |
///using this function. |
729 | 729 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
730 | 730 |
G->source((*_pred)[v]); } |
731 | 731 |
|
732 | 732 |
///\brief Returns a const reference to the node map that stores the |
733 | 733 |
///distances of the nodes. |
734 | 734 |
/// |
735 | 735 |
///Returns a const reference to the node map that stores the |
736 | 736 |
///distances of the nodes calculated by the algorithm. |
737 | 737 |
/// |
738 | 738 |
///\pre Either \ref run() or \ref init() |
739 | 739 |
///must be called before using this function. |
740 | 740 |
const DistMap &distMap() const { return *_dist;} |
741 | 741 |
|
742 | 742 |
///\brief Returns a const reference to the node map that stores the |
743 | 743 |
///predecessor arcs. |
744 | 744 |
/// |
745 | 745 |
///Returns a const reference to the node map that stores the predecessor |
746 | 746 |
///arcs, which form the DFS tree. |
747 | 747 |
/// |
748 | 748 |
///\pre Either \ref run() or \ref init() |
749 | 749 |
///must be called before using this function. |
750 | 750 |
const PredMap &predMap() const { return *_pred;} |
751 | 751 |
|
752 | 752 |
///Checks if a node is reachable from the root(s). |
753 | 753 |
|
754 | 754 |
///Returns \c true if \c v is reachable from the root(s). |
755 | 755 |
///\pre Either \ref run() or \ref start() |
756 | 756 |
///must be called before using this function. |
757 | 757 |
bool reached(Node v) const { return (*_reached)[v]; } |
758 | 758 |
|
759 | 759 |
///@} |
760 | 760 |
}; |
761 | 761 |
|
762 | 762 |
///Default traits class of dfs() function. |
763 | 763 |
|
764 | 764 |
///Default traits class of dfs() function. |
765 | 765 |
///\tparam GR Digraph type. |
766 | 766 |
template<class GR> |
767 | 767 |
struct DfsWizardDefaultTraits |
768 | 768 |
{ |
769 | 769 |
///The type of the digraph the algorithm runs on. |
770 | 770 |
typedef GR Digraph; |
771 | 771 |
|
772 | 772 |
///\brief The type of the map that stores the predecessor |
773 | 773 |
///arcs of the %DFS paths. |
774 | 774 |
/// |
775 | 775 |
///The type of the map that stores the predecessor |
776 | 776 |
///arcs of the %DFS paths. |
777 | 777 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
778 | 778 |
/// |
779 | 779 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
780 | 780 |
///Instantiates a \ref PredMap. |
781 | 781 |
|
782 | 782 |
///This function instantiates a \ref PredMap. |
783 | 783 |
///\param g is the digraph, to which we would like to define the |
784 | 784 |
///\ref PredMap. |
785 | 785 |
///\todo The digraph alone may be insufficient to initialize |
786 | 786 |
#ifdef DOXYGEN |
787 | 787 |
static PredMap *createPredMap(const Digraph &g) |
788 | 788 |
#else |
789 | 789 |
static PredMap *createPredMap(const Digraph &) |
790 | 790 |
#endif |
791 | 791 |
{ |
792 | 792 |
return new PredMap(); |
793 | 793 |
} |
794 | 794 |
|
795 | 795 |
///The type of the map that indicates which nodes are processed. |
796 | 796 |
|
797 | 797 |
///The type of the map that indicates which nodes are processed. |
798 | 798 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
799 | 799 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
800 | 800 |
///Instantiates a \ref ProcessedMap. |
801 | 801 |
|
802 | 802 |
///This function instantiates a \ref ProcessedMap. |
803 | 803 |
///\param g is the digraph, to which |
804 | 804 |
///we would like to define the \ref ProcessedMap. |
805 | 805 |
#ifdef DOXYGEN |
806 | 806 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
807 | 807 |
#else |
808 | 808 |
static ProcessedMap *createProcessedMap(const Digraph &) |
809 | 809 |
#endif |
810 | 810 |
{ |
811 | 811 |
return new ProcessedMap(); |
812 | 812 |
} |
813 | 813 |
|
814 | 814 |
///The type of the map that indicates which nodes are reached. |
815 | 815 |
|
816 | 816 |
///The type of the map that indicates which nodes are reached. |
817 | 817 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
818 | 818 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
819 | 819 |
///Instantiates a \ref ReachedMap. |
820 | 820 |
|
821 | 821 |
///This function instantiates a \ref ReachedMap. |
822 | 822 |
///\param g is the digraph, to which |
823 | 823 |
///we would like to define the \ref ReachedMap. |
824 | 824 |
static ReachedMap *createReachedMap(const Digraph &g) |
825 | 825 |
{ |
826 | 826 |
return new ReachedMap(g); |
827 | 827 |
} |
828 | 828 |
|
829 | 829 |
///The type of the map that stores the distances of the nodes. |
830 | 830 |
|
831 | 831 |
///The type of the map that stores the distances of the nodes. |
832 | 832 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
833 | 833 |
/// |
834 | 834 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
835 | 835 |
///Instantiates a \ref DistMap. |
836 | 836 |
|
837 | 837 |
///This function instantiates a \ref DistMap. |
838 | 838 |
///\param g is the digraph, to which we would like to define |
839 | 839 |
///the \ref DistMap |
840 | 840 |
#ifdef DOXYGEN |
841 | 841 |
static DistMap *createDistMap(const Digraph &g) |
842 | 842 |
#else |
843 | 843 |
static DistMap *createDistMap(const Digraph &) |
844 | 844 |
#endif |
845 | 845 |
{ |
846 | 846 |
return new DistMap(); |
847 | 847 |
} |
848 | 848 |
}; |
849 | 849 |
|
850 | 850 |
/// Default traits class used by \ref DfsWizard |
851 | 851 |
|
852 | 852 |
/// To make it easier to use Dfs algorithm |
853 | 853 |
/// we have created a wizard class. |
854 | 854 |
/// This \ref DfsWizard class needs default traits, |
855 | 855 |
/// as well as the \ref Dfs class. |
856 | 856 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
857 | 857 |
/// \ref DfsWizard class. |
858 | 858 |
template<class GR> |
859 | 859 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
860 | 860 |
{ |
861 | 861 |
|
862 | 862 |
typedef DfsWizardDefaultTraits<GR> Base; |
863 | 863 |
protected: |
864 | 864 |
//The type of the nodes in the digraph. |
865 | 865 |
typedef typename Base::Digraph::Node Node; |
866 | 866 |
|
867 | 867 |
//Pointer to the digraph the algorithm runs on. |
868 | 868 |
void *_g; |
869 | 869 |
//Pointer to the map of reached nodes. |
870 | 870 |
void *_reached; |
871 | 871 |
//Pointer to the map of processed nodes. |
872 | 872 |
void *_processed; |
873 | 873 |
//Pointer to the map of predecessors arcs. |
874 | 874 |
void *_pred; |
875 | 875 |
//Pointer to the map of distances. |
876 | 876 |
void *_dist; |
877 | 877 |
//Pointer to the source node. |
878 | 878 |
Node _source; |
879 | 879 |
|
880 | 880 |
public: |
881 | 881 |
/// Constructor. |
882 | 882 |
|
883 | 883 |
/// This constructor does not require parameters, therefore it initiates |
884 | 884 |
/// all of the attributes to default values (0, INVALID). |
885 | 885 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
886 | 886 |
_dist(0), _source(INVALID) {} |
887 | 887 |
|
888 | 888 |
/// Constructor. |
889 | 889 |
|
890 | 890 |
/// This constructor requires some parameters, |
891 | 891 |
/// listed in the parameters list. |
892 | 892 |
/// Others are initiated to 0. |
893 | 893 |
/// \param g The digraph the algorithm runs on. |
894 | 894 |
/// \param s The source node. |
895 | 895 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
896 | 896 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
897 | 897 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
898 | 898 |
|
899 | 899 |
}; |
900 | 900 |
|
901 | 901 |
/// Auxiliary class for the function type interface of DFS algorithm. |
902 | 902 |
|
903 | 903 |
/// This auxiliary class is created to implement the function type |
904 | 904 |
/// interface of \ref Dfs algorithm. It uses the functions and features |
905 | 905 |
/// of the plain \ref Dfs, but it is much simpler to use it. |
906 | 906 |
/// It should only be used through the \ref dfs() function, which makes |
907 | 907 |
/// it easier to use the algorithm. |
908 | 908 |
/// |
909 | 909 |
/// Simplicity means that the way to change the types defined |
910 | 910 |
/// in the traits class is based on functions that returns the new class |
911 | 911 |
/// and not on templatable built-in classes. |
912 | 912 |
/// When using the plain \ref Dfs |
913 | 913 |
/// the new class with the modified type comes from |
914 | 914 |
/// the original class by using the :: |
915 | 915 |
/// operator. In the case of \ref DfsWizard only |
916 | 916 |
/// a function have to be called, and it will |
917 | 917 |
/// return the needed class. |
918 | 918 |
/// |
919 | 919 |
/// It does not have own \ref run() method. When its \ref run() method |
920 | 920 |
/// is called, it initiates a plain \ref Dfs object, and calls the |
921 | 921 |
/// \ref Dfs::run() method of it. |
922 | 922 |
template<class TR> |
923 | 923 |
class DfsWizard : public TR |
924 | 924 |
{ |
925 | 925 |
typedef TR Base; |
926 | 926 |
|
927 | 927 |
///The type of the digraph the algorithm runs on. |
928 | 928 |
typedef typename TR::Digraph Digraph; |
929 | 929 |
|
930 | 930 |
typedef typename Digraph::Node Node; |
931 | 931 |
typedef typename Digraph::NodeIt NodeIt; |
932 | 932 |
typedef typename Digraph::Arc Arc; |
933 | 933 |
typedef typename Digraph::OutArcIt OutArcIt; |
934 | 934 |
|
935 | 935 |
///\brief The type of the map that stores the predecessor |
936 | 936 |
///arcs of the shortest paths. |
937 | 937 |
typedef typename TR::PredMap PredMap; |
938 | 938 |
///\brief The type of the map that stores the distances of the nodes. |
939 | 939 |
typedef typename TR::DistMap DistMap; |
940 | 940 |
///\brief The type of the map that indicates which nodes are reached. |
941 | 941 |
typedef typename TR::ReachedMap ReachedMap; |
942 | 942 |
///\brief The type of the map that indicates which nodes are processed. |
943 | 943 |
typedef typename TR::ProcessedMap ProcessedMap; |
944 | 944 |
|
945 | 945 |
public: |
946 | 946 |
|
947 | 947 |
/// Constructor. |
948 | 948 |
DfsWizard() : TR() {} |
949 | 949 |
|
950 | 950 |
/// Constructor that requires parameters. |
951 | 951 |
|
952 | 952 |
/// Constructor that requires parameters. |
953 | 953 |
/// These parameters will be the default values for the traits class. |
954 | 954 |
DfsWizard(const Digraph &g, Node s=INVALID) : |
955 | 955 |
TR(g,s) {} |
956 | 956 |
|
957 | 957 |
///Copy constructor |
958 | 958 |
DfsWizard(const TR &b) : TR(b) {} |
959 | 959 |
|
960 | 960 |
~DfsWizard() {} |
961 | 961 |
|
962 | 962 |
///Runs DFS algorithm from a source node. |
963 | 963 |
|
964 | 964 |
///Runs DFS algorithm from a source node. |
965 | 965 |
///The node can be given with the \ref source() function. |
966 | 966 |
void run() |
967 | 967 |
{ |
968 | 968 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
969 | 969 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
970 | 970 |
if(Base::_reached) |
971 | 971 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
972 | 972 |
if(Base::_processed) |
973 | 973 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
974 | 974 |
if(Base::_pred) |
975 | 975 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
976 | 976 |
if(Base::_dist) |
977 | 977 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
978 | 978 |
alg.run(Base::_source); |
979 | 979 |
} |
980 | 980 |
|
981 | 981 |
///Runs DFS algorithm from the given node. |
982 | 982 |
|
983 | 983 |
///Runs DFS algorithm from the given node. |
984 | 984 |
///\param s is the given source. |
985 | 985 |
void run(Node s) |
986 | 986 |
{ |
987 | 987 |
Base::_source=s; |
988 | 988 |
run(); |
989 | 989 |
} |
990 | 990 |
|
991 | 991 |
/// Sets the source node, from which the Dfs algorithm runs. |
992 | 992 |
|
993 | 993 |
/// Sets the source node, from which the Dfs algorithm runs. |
994 | 994 |
/// \param s is the source node. |
995 | 995 |
DfsWizard<TR> &source(Node s) |
996 | 996 |
{ |
997 | 997 |
Base::_source=s; |
998 | 998 |
return *this; |
999 | 999 |
} |
1000 | 1000 |
|
1001 | 1001 |
template<class T> |
1002 | 1002 |
struct SetPredMapBase : public Base { |
1003 | 1003 |
typedef T PredMap; |
1004 | 1004 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1005 | 1005 |
SetPredMapBase(const TR &b) : TR(b) {} |
1006 | 1006 |
}; |
1007 | 1007 |
///\brief \ref named-templ-param "Named parameter" |
1008 | 1008 |
///for setting \ref PredMap object. |
1009 | 1009 |
/// |
1010 | 1010 |
///\ref named-templ-param "Named parameter" |
1011 | 1011 |
///for setting \ref PredMap object. |
1012 | 1012 |
template<class T> |
1013 | 1013 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1014 | 1014 |
{ |
1015 | 1015 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1016 | 1016 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1017 | 1017 |
} |
1018 | 1018 |
|
1019 | 1019 |
template<class T> |
1020 | 1020 |
struct SetReachedMapBase : public Base { |
1021 | 1021 |
typedef T ReachedMap; |
1022 | 1022 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1023 | 1023 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1024 | 1024 |
}; |
1025 | 1025 |
///\brief \ref named-templ-param "Named parameter" |
1026 | 1026 |
///for setting \ref ReachedMap object. |
1027 | 1027 |
/// |
1028 | 1028 |
/// \ref named-templ-param "Named parameter" |
1029 | 1029 |
///for setting \ref ReachedMap object. |
1030 | 1030 |
template<class T> |
1031 | 1031 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1032 | 1032 |
{ |
1033 | 1033 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1034 | 1034 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1035 | 1035 |
} |
1036 | 1036 |
|
1037 | 1037 |
template<class T> |
1038 | 1038 |
struct SetProcessedMapBase : public Base { |
1039 | 1039 |
typedef T ProcessedMap; |
1040 | 1040 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1041 | 1041 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1042 | 1042 |
}; |
1043 | 1043 |
///\brief \ref named-templ-param "Named parameter" |
1044 | 1044 |
///for setting \ref ProcessedMap object. |
1045 | 1045 |
/// |
1046 | 1046 |
/// \ref named-templ-param "Named parameter" |
1047 | 1047 |
///for setting \ref ProcessedMap object. |
1048 | 1048 |
template<class T> |
1049 | 1049 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1050 | 1050 |
{ |
1051 | 1051 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1052 | 1052 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1053 | 1053 |
} |
1054 | 1054 |
|
1055 | 1055 |
template<class T> |
1056 | 1056 |
struct SetDistMapBase : public Base { |
1057 | 1057 |
typedef T DistMap; |
1058 | 1058 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1059 | 1059 |
SetDistMapBase(const TR &b) : TR(b) {} |
1060 | 1060 |
}; |
1061 | 1061 |
///\brief \ref named-templ-param "Named parameter" |
1062 | 1062 |
///for setting \ref DistMap object. |
1063 | 1063 |
/// |
1064 | 1064 |
///\ref named-templ-param "Named parameter" |
1065 | 1065 |
///for setting \ref DistMap object. |
1066 | 1066 |
template<class T> |
1067 | 1067 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1068 | 1068 |
{ |
1069 | 1069 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1070 | 1070 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1071 | 1071 |
} |
1072 | 1072 |
|
1073 | 1073 |
}; |
1074 | 1074 |
|
1075 | 1075 |
///Function type interface for Dfs algorithm. |
1076 | 1076 |
|
1077 | 1077 |
///\ingroup search |
1078 | 1078 |
///Function type interface for Dfs algorithm. |
1079 | 1079 |
/// |
1080 | 1080 |
///This function also has several |
1081 | 1081 |
///\ref named-templ-func-param "named parameters", |
1082 | 1082 |
///they are declared as the members of class \ref DfsWizard. |
1083 | 1083 |
///The following |
1084 | 1084 |
///example shows how to use these parameters. |
1085 | 1085 |
///\code |
1086 | 1086 |
/// dfs(g,source).predMap(preds).run(); |
1087 | 1087 |
///\endcode |
1088 | 1088 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1089 | 1089 |
///to the end of the parameter list. |
1090 | 1090 |
///\sa DfsWizard |
1091 | 1091 |
///\sa Dfs |
1092 | 1092 |
template<class GR> |
1093 | 1093 |
DfsWizard<DfsWizardBase<GR> > |
1094 | 1094 |
dfs(const GR &g,typename GR::Node s=INVALID) |
1095 | 1095 |
{ |
1096 | 1096 |
return DfsWizard<DfsWizardBase<GR> >(g,s); |
1097 | 1097 |
} |
1098 | 1098 |
|
1099 | 1099 |
#ifdef DOXYGEN |
1100 | 1100 |
/// \brief Visitor class for DFS. |
1101 | 1101 |
/// |
1102 | 1102 |
/// This class defines the interface of the DfsVisit events, and |
1103 | 1103 |
/// it could be the base of a real visitor class. |
1104 | 1104 |
template <typename _Digraph> |
1105 | 1105 |
struct DfsVisitor { |
1106 | 1106 |
typedef _Digraph Digraph; |
1107 | 1107 |
typedef typename Digraph::Arc Arc; |
1108 | 1108 |
typedef typename Digraph::Node Node; |
1109 | 1109 |
/// \brief Called for the source node of the DFS. |
1110 | 1110 |
/// |
1111 | 1111 |
/// This function is called for the source node of the DFS. |
1112 | 1112 |
void start(const Node& node) {} |
1113 | 1113 |
/// \brief Called when the source node is leaved. |
1114 | 1114 |
/// |
1115 | 1115 |
/// This function is called when the source node is leaved. |
1116 | 1116 |
void stop(const Node& node) {} |
1117 | 1117 |
/// \brief Called when a node is reached first time. |
1118 | 1118 |
/// |
1119 | 1119 |
/// This function is called when a node is reached first time. |
1120 | 1120 |
void reach(const Node& node) {} |
1121 | 1121 |
/// \brief Called when an arc reaches a new node. |
1122 | 1122 |
/// |
1123 | 1123 |
/// This function is called when the DFS finds an arc whose target node |
1124 | 1124 |
/// is not reached yet. |
1125 | 1125 |
void discover(const Arc& arc) {} |
1126 | 1126 |
/// \brief Called when an arc is examined but its target node is |
1127 | 1127 |
/// already discovered. |
1128 | 1128 |
/// |
1129 | 1129 |
/// This function is called when an arc is examined but its target node is |
1130 | 1130 |
/// already discovered. |
1131 | 1131 |
void examine(const Arc& arc) {} |
1132 | 1132 |
/// \brief Called when the DFS steps back from a node. |
1133 | 1133 |
/// |
1134 | 1134 |
/// This function is called when the DFS steps back from a node. |
1135 | 1135 |
void leave(const Node& node) {} |
1136 | 1136 |
/// \brief Called when the DFS steps back on an arc. |
1137 | 1137 |
/// |
1138 | 1138 |
/// This function is called when the DFS steps back on an arc. |
1139 | 1139 |
void backtrack(const Arc& arc) {} |
1140 | 1140 |
}; |
1141 | 1141 |
#else |
1142 | 1142 |
template <typename _Digraph> |
1143 | 1143 |
struct DfsVisitor { |
1144 | 1144 |
typedef _Digraph Digraph; |
1145 | 1145 |
typedef typename Digraph::Arc Arc; |
1146 | 1146 |
typedef typename Digraph::Node Node; |
1147 | 1147 |
void start(const Node&) {} |
1148 | 1148 |
void stop(const Node&) {} |
1149 | 1149 |
void reach(const Node&) {} |
1150 | 1150 |
void discover(const Arc&) {} |
1151 | 1151 |
void examine(const Arc&) {} |
1152 | 1152 |
void leave(const Node&) {} |
1153 | 1153 |
void backtrack(const Arc&) {} |
1154 | 1154 |
|
1155 | 1155 |
template <typename _Visitor> |
1156 | 1156 |
struct Constraints { |
1157 | 1157 |
void constraints() { |
1158 | 1158 |
Arc arc; |
1159 | 1159 |
Node node; |
1160 | 1160 |
visitor.start(node); |
1161 | 1161 |
visitor.stop(arc); |
1162 | 1162 |
visitor.reach(node); |
1163 | 1163 |
visitor.discover(arc); |
1164 | 1164 |
visitor.examine(arc); |
1165 | 1165 |
visitor.leave(node); |
1166 | 1166 |
visitor.backtrack(arc); |
1167 | 1167 |
} |
1168 | 1168 |
_Visitor& visitor; |
1169 | 1169 |
}; |
1170 | 1170 |
}; |
1171 | 1171 |
#endif |
1172 | 1172 |
|
1173 | 1173 |
/// \brief Default traits class of DfsVisit class. |
1174 | 1174 |
/// |
1175 | 1175 |
/// Default traits class of DfsVisit class. |
1176 | 1176 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1177 | 1177 |
template<class _Digraph> |
1178 | 1178 |
struct DfsVisitDefaultTraits { |
1179 | 1179 |
|
1180 | 1180 |
/// \brief The type of the digraph the algorithm runs on. |
1181 | 1181 |
typedef _Digraph Digraph; |
1182 | 1182 |
|
1183 | 1183 |
/// \brief The type of the map that indicates which nodes are reached. |
1184 | 1184 |
/// |
1185 | 1185 |
/// The type of the map that indicates which nodes are reached. |
1186 | 1186 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1187 | 1187 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1188 | 1188 |
|
1189 | 1189 |
/// \brief Instantiates a \ref ReachedMap. |
1190 | 1190 |
/// |
1191 | 1191 |
/// This function instantiates a \ref ReachedMap. |
1192 | 1192 |
/// \param digraph is the digraph, to which |
1193 | 1193 |
/// we would like to define the \ref ReachedMap. |
1194 | 1194 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1195 | 1195 |
return new ReachedMap(digraph); |
1196 | 1196 |
} |
1197 | 1197 |
|
1198 | 1198 |
}; |
1199 | 1199 |
|
1200 | 1200 |
/// \ingroup search |
1201 | 1201 |
/// |
1202 | 1202 |
/// \brief %DFS algorithm class with visitor interface. |
1203 | 1203 |
/// |
1204 | 1204 |
/// This class provides an efficient implementation of the %DFS algorithm |
1205 | 1205 |
/// with visitor interface. |
1206 | 1206 |
/// |
1207 | 1207 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1208 | 1208 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1209 | 1209 |
/// the member functions of the \c Visitor class on every DFS event. |
1210 | 1210 |
/// |
1211 |
/// This interface of the DFS algorithm should be used in special cases |
|
1212 |
/// when extra actions have to be performed in connection with certain |
|
1213 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
|
1214 |
/// instead. |
|
1215 |
/// |
|
1211 | 1216 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1212 | 1217 |
/// The default value is |
1213 | 1218 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1214 | 1219 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
1215 | 1220 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1216 | 1221 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
1217 | 1222 |
/// does not observe the DFS events. If you want to observe the DFS |
1218 | 1223 |
/// events, you should implement your own visitor class. |
1219 | 1224 |
/// \tparam _Traits Traits class to set various data types used by the |
1220 | 1225 |
/// algorithm. The default traits class is |
1221 | 1226 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1222 | 1227 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1223 | 1228 |
/// a DFS visit traits class. |
1224 | 1229 |
#ifdef DOXYGEN |
1225 | 1230 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1226 | 1231 |
#else |
1227 | 1232 |
template <typename _Digraph = ListDigraph, |
1228 | 1233 |
typename _Visitor = DfsVisitor<_Digraph>, |
1229 | 1234 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1230 | 1235 |
#endif |
1231 | 1236 |
class DfsVisit { |
1232 | 1237 |
public: |
1233 | 1238 |
|
1234 | 1239 |
/// \brief \ref Exception for uninitialized parameters. |
1235 | 1240 |
/// |
1236 | 1241 |
/// This error represents problems in the initialization |
1237 | 1242 |
/// of the parameters of the algorithm. |
1238 | 1243 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1239 | 1244 |
public: |
1240 | 1245 |
virtual const char* what() const throw() |
1241 | 1246 |
{ |
1242 | 1247 |
return "lemon::DfsVisit::UninitializedParameter"; |
1243 | 1248 |
} |
1244 | 1249 |
}; |
1245 | 1250 |
|
1246 | 1251 |
///The traits class. |
1247 | 1252 |
typedef _Traits Traits; |
1248 | 1253 |
|
1249 | 1254 |
///The type of the digraph the algorithm runs on. |
1250 | 1255 |
typedef typename Traits::Digraph Digraph; |
1251 | 1256 |
|
1252 | 1257 |
///The visitor type used by the algorithm. |
1253 | 1258 |
typedef _Visitor Visitor; |
1254 | 1259 |
|
1255 | 1260 |
///The type of the map that indicates which nodes are reached. |
1256 | 1261 |
typedef typename Traits::ReachedMap ReachedMap; |
1257 | 1262 |
|
1258 | 1263 |
private: |
1259 | 1264 |
|
1260 | 1265 |
typedef typename Digraph::Node Node; |
1261 | 1266 |
typedef typename Digraph::NodeIt NodeIt; |
1262 | 1267 |
typedef typename Digraph::Arc Arc; |
1263 | 1268 |
typedef typename Digraph::OutArcIt OutArcIt; |
1264 | 1269 |
|
1265 | 1270 |
//Pointer to the underlying digraph. |
1266 | 1271 |
const Digraph *_digraph; |
1267 | 1272 |
//Pointer to the visitor object. |
1268 | 1273 |
Visitor *_visitor; |
1269 | 1274 |
//Pointer to the map of reached status of the nodes. |
1270 | 1275 |
ReachedMap *_reached; |
1271 | 1276 |
//Indicates if _reached is locally allocated (true) or not. |
1272 | 1277 |
bool local_reached; |
1273 | 1278 |
|
1274 | 1279 |
std::vector<typename Digraph::Arc> _stack; |
1275 | 1280 |
int _stack_head; |
1276 | 1281 |
|
1277 | 1282 |
///Creates the maps if necessary. |
1278 | 1283 |
///\todo Better memory allocation (instead of new). |
1279 | 1284 |
void create_maps() { |
1280 | 1285 |
if(!_reached) { |
1281 | 1286 |
local_reached = true; |
1282 | 1287 |
_reached = Traits::createReachedMap(*_digraph); |
1283 | 1288 |
} |
1284 | 1289 |
} |
1285 | 1290 |
|
1286 | 1291 |
protected: |
1287 | 1292 |
|
1288 | 1293 |
DfsVisit() {} |
1289 | 1294 |
|
1290 | 1295 |
public: |
1291 | 1296 |
|
1292 | 1297 |
typedef DfsVisit Create; |
1293 | 1298 |
|
1294 | 1299 |
/// \name Named template parameters |
1295 | 1300 |
|
1296 | 1301 |
///@{ |
1297 | 1302 |
template <class T> |
1298 | 1303 |
struct SetReachedMapTraits : public Traits { |
1299 | 1304 |
typedef T ReachedMap; |
1300 | 1305 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1301 | 1306 |
throw UninitializedParameter(); |
1302 | 1307 |
} |
1303 | 1308 |
}; |
1304 | 1309 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1305 | 1310 |
/// ReachedMap type. |
1306 | 1311 |
/// |
1307 | 1312 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1308 | 1313 |
template <class T> |
1309 | 1314 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1310 | 1315 |
SetReachedMapTraits<T> > { |
1311 | 1316 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1312 | 1317 |
}; |
1313 | 1318 |
///@} |
1314 | 1319 |
|
1315 | 1320 |
public: |
1316 | 1321 |
|
1317 | 1322 |
/// \brief Constructor. |
1318 | 1323 |
/// |
1319 | 1324 |
/// Constructor. |
1320 | 1325 |
/// |
1321 | 1326 |
/// \param digraph The digraph the algorithm runs on. |
1322 | 1327 |
/// \param visitor The visitor object of the algorithm. |
1323 | 1328 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1324 | 1329 |
: _digraph(&digraph), _visitor(&visitor), |
1325 | 1330 |
_reached(0), local_reached(false) {} |
1326 | 1331 |
|
1327 | 1332 |
/// \brief Destructor. |
1328 | 1333 |
~DfsVisit() { |
1329 | 1334 |
if(local_reached) delete _reached; |
1330 | 1335 |
} |
1331 | 1336 |
|
1332 | 1337 |
/// \brief Sets the map that indicates which nodes are reached. |
1333 | 1338 |
/// |
1334 | 1339 |
/// Sets the map that indicates which nodes are reached. |
1335 | 1340 |
/// If you don't use this function before calling \ref run(), |
1336 | 1341 |
/// it will allocate one. The destructor deallocates this |
1337 | 1342 |
/// automatically allocated map, of course. |
1338 | 1343 |
/// \return <tt> (*this) </tt> |
1339 | 1344 |
DfsVisit &reachedMap(ReachedMap &m) { |
1340 | 1345 |
if(local_reached) { |
1341 | 1346 |
delete _reached; |
1342 | 1347 |
local_reached=false; |
1343 | 1348 |
} |
1344 | 1349 |
_reached = &m; |
1345 | 1350 |
return *this; |
1346 | 1351 |
} |
1347 | 1352 |
|
1348 | 1353 |
public: |
1349 | 1354 |
|
1350 | 1355 |
/// \name Execution control |
1351 | 1356 |
/// The simplest way to execute the algorithm is to use |
1352 | 1357 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1353 | 1358 |
/// "run()". |
1354 | 1359 |
/// \n |
1355 | 1360 |
/// If you need more control on the execution, first you must call |
1356 | 1361 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1357 | 1362 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1358 | 1363 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1359 | 1364 |
/// actual path computation. |
1360 | 1365 |
|
1361 | 1366 |
/// @{ |
1362 | 1367 |
|
1363 | 1368 |
/// \brief Initializes the internal data structures. |
1364 | 1369 |
/// |
1365 | 1370 |
/// Initializes the internal data structures. |
1366 | 1371 |
void init() { |
1367 | 1372 |
create_maps(); |
1368 | 1373 |
_stack.resize(countNodes(*_digraph)); |
1369 | 1374 |
_stack_head = -1; |
1370 | 1375 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1371 | 1376 |
_reached->set(u, false); |
1372 | 1377 |
} |
1373 | 1378 |
} |
1374 | 1379 |
|
1375 | 1380 |
///Adds a new source node. |
1376 | 1381 |
|
1377 | 1382 |
///Adds a new source node to the set of nodes to be processed. |
1378 | 1383 |
/// |
1379 | 1384 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1380 | 1385 |
///false results.) |
1381 | 1386 |
/// |
1382 | 1387 |
///\warning Distances will be wrong (or at least strange) in case of |
1383 | 1388 |
///multiple sources. |
1384 | 1389 |
void addSource(Node s) |
1385 | 1390 |
{ |
1386 | 1391 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1387 | 1392 |
if(!(*_reached)[s]) { |
1388 | 1393 |
_reached->set(s,true); |
1389 | 1394 |
_visitor->start(s); |
1390 | 1395 |
_visitor->reach(s); |
1391 | 1396 |
Arc e; |
1392 | 1397 |
_digraph->firstOut(e, s); |
1393 | 1398 |
if (e != INVALID) { |
1394 | 1399 |
_stack[++_stack_head] = e; |
1395 | 1400 |
} else { |
1396 | 1401 |
_visitor->leave(s); |
1397 | 1402 |
} |
1398 | 1403 |
} |
1399 | 1404 |
} |
1400 | 1405 |
|
1401 | 1406 |
/// \brief Processes the next arc. |
1402 | 1407 |
/// |
1403 | 1408 |
/// Processes the next arc. |
1404 | 1409 |
/// |
1405 | 1410 |
/// \return The processed arc. |
1406 | 1411 |
/// |
1407 | 1412 |
/// \pre The stack must not be empty. |
1408 | 1413 |
Arc processNextArc() { |
1409 | 1414 |
Arc e = _stack[_stack_head]; |
1410 | 1415 |
Node m = _digraph->target(e); |
1411 | 1416 |
if(!(*_reached)[m]) { |
1412 | 1417 |
_visitor->discover(e); |
1413 | 1418 |
_visitor->reach(m); |
1414 | 1419 |
_reached->set(m, true); |
1415 | 1420 |
_digraph->firstOut(_stack[++_stack_head], m); |
1416 | 1421 |
} else { |
1417 | 1422 |
_visitor->examine(e); |
1418 | 1423 |
m = _digraph->source(e); |
1419 | 1424 |
_digraph->nextOut(_stack[_stack_head]); |
1420 | 1425 |
} |
1421 | 1426 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1422 | 1427 |
_visitor->leave(m); |
1423 | 1428 |
--_stack_head; |
1424 | 1429 |
if (_stack_head >= 0) { |
1425 | 1430 |
_visitor->backtrack(_stack[_stack_head]); |
1426 | 1431 |
m = _digraph->source(_stack[_stack_head]); |
1427 | 1432 |
_digraph->nextOut(_stack[_stack_head]); |
1428 | 1433 |
} else { |
1429 | 1434 |
_visitor->stop(m); |
1430 | 1435 |
} |
1431 | 1436 |
} |
1432 | 1437 |
return e; |
1433 | 1438 |
} |
1434 | 1439 |
|
1435 | 1440 |
/// \brief Next arc to be processed. |
1436 | 1441 |
/// |
1437 | 1442 |
/// Next arc to be processed. |
1438 | 1443 |
/// |
1439 | 1444 |
/// \return The next arc to be processed or INVALID if the stack is |
1440 | 1445 |
/// empty. |
1441 | 1446 |
Arc nextArc() const { |
1442 | 1447 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1443 | 1448 |
} |
1444 | 1449 |
|
1445 | 1450 |
/// \brief Returns \c false if there are nodes |
1446 | 1451 |
/// to be processed. |
1447 | 1452 |
/// |
1448 | 1453 |
/// Returns \c false if there are nodes |
1449 | 1454 |
/// to be processed in the queue (stack). |
1450 | 1455 |
bool emptyQueue() const { return _stack_head < 0; } |
1451 | 1456 |
|
1452 | 1457 |
/// \brief Returns the number of the nodes to be processed. |
1453 | 1458 |
/// |
1454 | 1459 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1455 | 1460 |
int queueSize() const { return _stack_head + 1; } |
1456 | 1461 |
|
1457 | 1462 |
/// \brief Executes the algorithm. |
1458 | 1463 |
/// |
1459 | 1464 |
/// Executes the algorithm. |
1460 | 1465 |
/// |
1461 | 1466 |
/// This method runs the %DFS algorithm from the root node |
1462 | 1467 |
/// in order to compute the %DFS path to each node. |
1463 | 1468 |
/// |
1464 | 1469 |
/// The algorithm computes |
1465 | 1470 |
/// - the %DFS tree, |
1466 | 1471 |
/// - the distance of each node from the root in the %DFS tree. |
1467 | 1472 |
/// |
1468 | 1473 |
/// \pre init() must be called and a root node should be |
1469 | 1474 |
/// added with addSource() before using this function. |
1470 | 1475 |
/// |
1471 | 1476 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1472 | 1477 |
/// \code |
1473 | 1478 |
/// while ( !d.emptyQueue() ) { |
1474 | 1479 |
/// d.processNextArc(); |
1475 | 1480 |
/// } |
1476 | 1481 |
/// \endcode |
1477 | 1482 |
void start() { |
1478 | 1483 |
while ( !emptyQueue() ) processNextArc(); |
1479 | 1484 |
} |
1480 | 1485 |
|
1481 | 1486 |
/// \brief Executes the algorithm until the given target node is reached. |
1482 | 1487 |
/// |
1483 | 1488 |
/// Executes the algorithm until the given target node is reached. |
1484 | 1489 |
/// |
1485 | 1490 |
/// This method runs the %DFS algorithm from the root node |
1486 | 1491 |
/// in order to compute the DFS path to \c dest. |
1487 | 1492 |
/// |
1488 | 1493 |
/// The algorithm computes |
1489 | 1494 |
/// - the %DFS path to \c dest, |
1490 | 1495 |
/// - the distance of \c dest from the root in the %DFS tree. |
1491 | 1496 |
/// |
1492 | 1497 |
/// \pre init() must be called and a root node should be added |
1493 | 1498 |
/// with addSource() before using this function. |
1494 | 1499 |
void start(Node dest) { |
1495 | 1500 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
1496 | 1501 |
processNextArc(); |
1497 | 1502 |
} |
1498 | 1503 |
|
1499 | 1504 |
/// \brief Executes the algorithm until a condition is met. |
1500 | 1505 |
/// |
1501 | 1506 |
/// Executes the algorithm until a condition is met. |
1502 | 1507 |
/// |
1503 | 1508 |
/// This method runs the %DFS algorithm from the root node |
1504 | 1509 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1505 | 1510 |
/// |
1506 | 1511 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1507 | 1512 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1508 | 1513 |
/// |
1509 | 1514 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1510 | 1515 |
/// \c INVALID if no such arc was found. |
1511 | 1516 |
/// |
1512 | 1517 |
/// \pre init() must be called and a root node should be added |
1513 | 1518 |
/// with addSource() before using this function. |
1514 | 1519 |
/// |
1515 | 1520 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1516 | 1521 |
/// not a node map. |
1517 | 1522 |
template <typename AM> |
1518 | 1523 |
Arc start(const AM &am) { |
1519 | 1524 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1520 | 1525 |
processNextArc(); |
1521 | 1526 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1522 | 1527 |
} |
1523 | 1528 |
|
1524 | 1529 |
/// \brief Runs the algorithm from the given node. |
1525 | 1530 |
/// |
1526 | 1531 |
/// This method runs the %DFS algorithm from node \c s. |
1527 | 1532 |
/// in order to compute the DFS path to each node. |
1528 | 1533 |
/// |
1529 | 1534 |
/// The algorithm computes |
1530 | 1535 |
/// - the %DFS tree, |
1531 | 1536 |
/// - the distance of each node from the root in the %DFS tree. |
1532 | 1537 |
/// |
1533 | 1538 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1534 | 1539 |
///\code |
1535 | 1540 |
/// d.init(); |
1536 | 1541 |
/// d.addSource(s); |
1537 | 1542 |
/// d.start(); |
1538 | 1543 |
///\endcode |
1539 | 1544 |
void run(Node s) { |
1540 | 1545 |
init(); |
1541 | 1546 |
addSource(s); |
1542 | 1547 |
start(); |
1543 | 1548 |
} |
1544 | 1549 |
|
1545 | 1550 |
/// \brief Finds the %DFS path between \c s and \c t. |
1546 | 1551 |
|
1547 | 1552 |
/// This method runs the %DFS algorithm from node \c s |
1548 | 1553 |
/// in order to compute the DFS path to \c t. |
1549 | 1554 |
/// |
1550 | 1555 |
/// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
1551 | 1556 |
/// if \c t is reachable form \c s, \c 0 otherwise. |
1552 | 1557 |
/// |
1553 | 1558 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1554 | 1559 |
/// just a shortcut of the following code. |
1555 | 1560 |
///\code |
1556 | 1561 |
/// d.init(); |
1557 | 1562 |
/// d.addSource(s); |
1558 | 1563 |
/// d.start(t); |
1559 | 1564 |
///\endcode |
1560 | 1565 |
int run(Node s,Node t) { |
1561 | 1566 |
init(); |
1562 | 1567 |
addSource(s); |
1563 | 1568 |
start(t); |
1564 | 1569 |
return reached(t)?_stack_head+1:0; |
1565 | 1570 |
} |
1566 | 1571 |
|
1567 | 1572 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1568 | 1573 |
|
1569 | 1574 |
/// This method runs the %DFS algorithm in order to |
1570 | 1575 |
/// compute the %DFS path to each node. |
1571 | 1576 |
/// |
1572 | 1577 |
/// The algorithm computes |
1573 | 1578 |
/// - the %DFS tree, |
1574 | 1579 |
/// - the distance of each node from the root in the %DFS tree. |
1575 | 1580 |
/// |
1576 | 1581 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1577 | 1582 |
///\code |
1578 | 1583 |
/// d.init(); |
1579 | 1584 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1580 | 1585 |
/// if (!d.reached(n)) { |
1581 | 1586 |
/// d.addSource(n); |
1582 | 1587 |
/// d.start(); |
1583 | 1588 |
/// } |
1584 | 1589 |
/// } |
1585 | 1590 |
///\endcode |
1586 | 1591 |
void run() { |
1587 | 1592 |
init(); |
1588 | 1593 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1589 | 1594 |
if (!reached(it)) { |
1590 | 1595 |
addSource(it); |
1591 | 1596 |
start(); |
1592 | 1597 |
} |
1593 | 1598 |
} |
1594 | 1599 |
} |
1595 | 1600 |
|
1596 | 1601 |
///@} |
1597 | 1602 |
|
1598 | 1603 |
/// \name Query Functions |
1599 | 1604 |
/// The result of the %DFS algorithm can be obtained using these |
1600 | 1605 |
/// functions.\n |
1601 | 1606 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1602 | 1607 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1603 | 1608 |
/// using them. |
1604 | 1609 |
///@{ |
1605 | 1610 |
|
1606 | 1611 |
/// \brief Checks if a node is reachable from the root(s). |
1607 | 1612 |
/// |
1608 | 1613 |
/// Returns \c true if \c v is reachable from the root(s). |
1609 | 1614 |
/// \pre Either \ref run() or \ref start() |
1610 | 1615 |
/// must be called before using this function. |
1611 | 1616 |
bool reached(Node v) { return (*_reached)[v]; } |
1612 | 1617 |
|
1613 | 1618 |
///@} |
1614 | 1619 |
|
1615 | 1620 |
}; |
1616 | 1621 |
|
1617 | 1622 |
} //END OF NAMESPACE LEMON |
1618 | 1623 |
|
1619 | 1624 |
#endif |
... | ... |
@@ -302,981 +302,987 @@ |
302 | 302 |
{ |
303 | 303 |
if(!_pred) { |
304 | 304 |
local_pred = true; |
305 | 305 |
_pred = Traits::createPredMap(*G); |
306 | 306 |
} |
307 | 307 |
if(!_dist) { |
308 | 308 |
local_dist = true; |
309 | 309 |
_dist = Traits::createDistMap(*G); |
310 | 310 |
} |
311 | 311 |
if(!_processed) { |
312 | 312 |
local_processed = true; |
313 | 313 |
_processed = Traits::createProcessedMap(*G); |
314 | 314 |
} |
315 | 315 |
if (!_heap_cross_ref) { |
316 | 316 |
local_heap_cross_ref = true; |
317 | 317 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
318 | 318 |
} |
319 | 319 |
if (!_heap) { |
320 | 320 |
local_heap = true; |
321 | 321 |
_heap = Traits::createHeap(*_heap_cross_ref); |
322 | 322 |
} |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
public: |
326 | 326 |
|
327 | 327 |
typedef Dijkstra Create; |
328 | 328 |
|
329 | 329 |
///\name Named template parameters |
330 | 330 |
|
331 | 331 |
///@{ |
332 | 332 |
|
333 | 333 |
template <class T> |
334 | 334 |
struct SetPredMapTraits : public Traits { |
335 | 335 |
typedef T PredMap; |
336 | 336 |
static PredMap *createPredMap(const Digraph &) |
337 | 337 |
{ |
338 | 338 |
throw UninitializedParameter(); |
339 | 339 |
} |
340 | 340 |
}; |
341 | 341 |
///\brief \ref named-templ-param "Named parameter" for setting |
342 | 342 |
///\ref PredMap type. |
343 | 343 |
/// |
344 | 344 |
///\ref named-templ-param "Named parameter" for setting |
345 | 345 |
///\ref PredMap type. |
346 | 346 |
template <class T> |
347 | 347 |
struct SetPredMap |
348 | 348 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
349 | 349 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
350 | 350 |
}; |
351 | 351 |
|
352 | 352 |
template <class T> |
353 | 353 |
struct SetDistMapTraits : public Traits { |
354 | 354 |
typedef T DistMap; |
355 | 355 |
static DistMap *createDistMap(const Digraph &) |
356 | 356 |
{ |
357 | 357 |
throw UninitializedParameter(); |
358 | 358 |
} |
359 | 359 |
}; |
360 | 360 |
///\brief \ref named-templ-param "Named parameter" for setting |
361 | 361 |
///\ref DistMap type. |
362 | 362 |
/// |
363 | 363 |
///\ref named-templ-param "Named parameter" for setting |
364 | 364 |
///\ref DistMap type. |
365 | 365 |
template <class T> |
366 | 366 |
struct SetDistMap |
367 | 367 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
368 | 368 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
369 | 369 |
}; |
370 | 370 |
|
371 | 371 |
template <class T> |
372 | 372 |
struct SetProcessedMapTraits : public Traits { |
373 | 373 |
typedef T ProcessedMap; |
374 | 374 |
static ProcessedMap *createProcessedMap(const Digraph &) |
375 | 375 |
{ |
376 | 376 |
throw UninitializedParameter(); |
377 | 377 |
} |
378 | 378 |
}; |
379 | 379 |
///\brief \ref named-templ-param "Named parameter" for setting |
380 | 380 |
///\ref ProcessedMap type. |
381 | 381 |
/// |
382 | 382 |
///\ref named-templ-param "Named parameter" for setting |
383 | 383 |
///\ref ProcessedMap type. |
384 | 384 |
template <class T> |
385 | 385 |
struct SetProcessedMap |
386 | 386 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
387 | 387 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
388 | 388 |
}; |
389 | 389 |
|
390 | 390 |
struct SetStandardProcessedMapTraits : public Traits { |
391 | 391 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
392 | 392 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
393 | 393 |
{ |
394 | 394 |
return new ProcessedMap(g); |
395 | 395 |
} |
396 | 396 |
}; |
397 | 397 |
///\brief \ref named-templ-param "Named parameter" for setting |
398 | 398 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
399 | 399 |
/// |
400 | 400 |
///\ref named-templ-param "Named parameter" for setting |
401 | 401 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
402 | 402 |
///If you don't set it explicitly, it will be automatically allocated. |
403 | 403 |
struct SetStandardProcessedMap |
404 | 404 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
405 | 405 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
406 | 406 |
Create; |
407 | 407 |
}; |
408 | 408 |
|
409 | 409 |
template <class H, class CR> |
410 | 410 |
struct SetHeapTraits : public Traits { |
411 | 411 |
typedef CR HeapCrossRef; |
412 | 412 |
typedef H Heap; |
413 | 413 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
414 | 414 |
throw UninitializedParameter(); |
415 | 415 |
} |
416 | 416 |
static Heap *createHeap(HeapCrossRef &) |
417 | 417 |
{ |
418 | 418 |
throw UninitializedParameter(); |
419 | 419 |
} |
420 | 420 |
}; |
421 | 421 |
///\brief \ref named-templ-param "Named parameter" for setting |
422 | 422 |
///heap and cross reference type |
423 | 423 |
/// |
424 | 424 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
425 | 425 |
///reference type. |
426 | 426 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
427 | 427 |
struct SetHeap |
428 | 428 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
429 | 429 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
430 | 430 |
}; |
431 | 431 |
|
432 | 432 |
template <class H, class CR> |
433 | 433 |
struct SetStandardHeapTraits : public Traits { |
434 | 434 |
typedef CR HeapCrossRef; |
435 | 435 |
typedef H Heap; |
436 | 436 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
437 | 437 |
return new HeapCrossRef(G); |
438 | 438 |
} |
439 | 439 |
static Heap *createHeap(HeapCrossRef &R) |
440 | 440 |
{ |
441 | 441 |
return new Heap(R); |
442 | 442 |
} |
443 | 443 |
}; |
444 | 444 |
///\brief \ref named-templ-param "Named parameter" for setting |
445 | 445 |
///heap and cross reference type with automatic allocation |
446 | 446 |
/// |
447 | 447 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
448 | 448 |
///reference type. It can allocate the heap and the cross reference |
449 | 449 |
///object if the cross reference's constructor waits for the digraph as |
450 | 450 |
///parameter and the heap's constructor waits for the cross reference. |
451 | 451 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
452 | 452 |
struct SetStandardHeap |
453 | 453 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
454 | 454 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
455 | 455 |
Create; |
456 | 456 |
}; |
457 | 457 |
|
458 | 458 |
template <class T> |
459 | 459 |
struct SetOperationTraitsTraits : public Traits { |
460 | 460 |
typedef T OperationTraits; |
461 | 461 |
}; |
462 | 462 |
|
463 | 463 |
/// \brief \ref named-templ-param "Named parameter" for setting |
464 | 464 |
///\ref OperationTraits type |
465 | 465 |
/// |
466 | 466 |
///\ref named-templ-param "Named parameter" for setting |
467 | 467 |
///\ref OperationTraits type. |
468 | 468 |
template <class T> |
469 | 469 |
struct SetOperationTraits |
470 | 470 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
471 | 471 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
472 | 472 |
Create; |
473 | 473 |
}; |
474 | 474 |
|
475 | 475 |
///@} |
476 | 476 |
|
477 | 477 |
protected: |
478 | 478 |
|
479 | 479 |
Dijkstra() {} |
480 | 480 |
|
481 | 481 |
public: |
482 | 482 |
|
483 | 483 |
///Constructor. |
484 | 484 |
|
485 | 485 |
///Constructor. |
486 | 486 |
///\param _g The digraph the algorithm runs on. |
487 | 487 |
///\param _length The length map used by the algorithm. |
488 | 488 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
489 | 489 |
G(&_g), length(&_length), |
490 | 490 |
_pred(NULL), local_pred(false), |
491 | 491 |
_dist(NULL), local_dist(false), |
492 | 492 |
_processed(NULL), local_processed(false), |
493 | 493 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
494 | 494 |
_heap(NULL), local_heap(false) |
495 | 495 |
{ } |
496 | 496 |
|
497 | 497 |
///Destructor. |
498 | 498 |
~Dijkstra() |
499 | 499 |
{ |
500 | 500 |
if(local_pred) delete _pred; |
501 | 501 |
if(local_dist) delete _dist; |
502 | 502 |
if(local_processed) delete _processed; |
503 | 503 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
504 | 504 |
if(local_heap) delete _heap; |
505 | 505 |
} |
506 | 506 |
|
507 | 507 |
///Sets the length map. |
508 | 508 |
|
509 | 509 |
///Sets the length map. |
510 | 510 |
///\return <tt> (*this) </tt> |
511 | 511 |
Dijkstra &lengthMap(const LengthMap &m) |
512 | 512 |
{ |
513 | 513 |
length = &m; |
514 | 514 |
return *this; |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
///Sets the map that stores the predecessor arcs. |
518 | 518 |
|
519 | 519 |
///Sets the map that stores the predecessor arcs. |
520 | 520 |
///If you don't use this function before calling \ref run(), |
521 | 521 |
///it will allocate one. The destructor deallocates this |
522 | 522 |
///automatically allocated map, of course. |
523 | 523 |
///\return <tt> (*this) </tt> |
524 | 524 |
Dijkstra &predMap(PredMap &m) |
525 | 525 |
{ |
526 | 526 |
if(local_pred) { |
527 | 527 |
delete _pred; |
528 | 528 |
local_pred=false; |
529 | 529 |
} |
530 | 530 |
_pred = &m; |
531 | 531 |
return *this; |
532 | 532 |
} |
533 | 533 |
|
534 | 534 |
///Sets the map that indicates which nodes are processed. |
535 | 535 |
|
536 | 536 |
///Sets the map that indicates which nodes are processed. |
537 | 537 |
///If you don't use this function before calling \ref run(), |
538 | 538 |
///it will allocate one. The destructor deallocates this |
539 | 539 |
///automatically allocated map, of course. |
540 | 540 |
///\return <tt> (*this) </tt> |
541 | 541 |
Dijkstra &processedMap(ProcessedMap &m) |
542 | 542 |
{ |
543 | 543 |
if(local_processed) { |
544 | 544 |
delete _processed; |
545 | 545 |
local_processed=false; |
546 | 546 |
} |
547 | 547 |
_processed = &m; |
548 | 548 |
return *this; |
549 | 549 |
} |
550 | 550 |
|
551 | 551 |
///Sets the map that stores the distances of the nodes. |
552 | 552 |
|
553 | 553 |
///Sets the map that stores the distances of the nodes calculated by the |
554 | 554 |
///algorithm. |
555 | 555 |
///If you don't use this function before calling \ref run(), |
556 | 556 |
///it will allocate one. The destructor deallocates this |
557 | 557 |
///automatically allocated map, of course. |
558 | 558 |
///\return <tt> (*this) </tt> |
559 | 559 |
Dijkstra &distMap(DistMap &m) |
560 | 560 |
{ |
561 | 561 |
if(local_dist) { |
562 | 562 |
delete _dist; |
563 | 563 |
local_dist=false; |
564 | 564 |
} |
565 | 565 |
_dist = &m; |
566 | 566 |
return *this; |
567 | 567 |
} |
568 | 568 |
|
569 | 569 |
///Sets the heap and the cross reference used by algorithm. |
570 | 570 |
|
571 | 571 |
///Sets the heap and the cross reference used by algorithm. |
572 | 572 |
///If you don't use this function before calling \ref run(), |
573 | 573 |
///it will allocate one. The destructor deallocates this |
574 | 574 |
///automatically allocated heap and cross reference, of course. |
575 | 575 |
///\return <tt> (*this) </tt> |
576 | 576 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
577 | 577 |
{ |
578 | 578 |
if(local_heap_cross_ref) { |
579 | 579 |
delete _heap_cross_ref; |
580 | 580 |
local_heap_cross_ref=false; |
581 | 581 |
} |
582 | 582 |
_heap_cross_ref = &cr; |
583 | 583 |
if(local_heap) { |
584 | 584 |
delete _heap; |
585 | 585 |
local_heap=false; |
586 | 586 |
} |
587 | 587 |
_heap = &hp; |
588 | 588 |
return *this; |
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
private: |
592 | 592 |
|
593 | 593 |
void finalizeNodeData(Node v,Value dst) |
594 | 594 |
{ |
595 | 595 |
_processed->set(v,true); |
596 | 596 |
_dist->set(v, dst); |
597 | 597 |
} |
598 | 598 |
|
599 | 599 |
public: |
600 | 600 |
|
601 | 601 |
///\name Execution control |
602 | 602 |
///The simplest way to execute the algorithm is to use one of the |
603 | 603 |
///member functions called \ref lemon::Dijkstra::run() "run()". |
604 | 604 |
///\n |
605 | 605 |
///If you need more control on the execution, first you must call |
606 | 606 |
///\ref lemon::Dijkstra::init() "init()", then you can add several |
607 | 607 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
608 | 608 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
609 | 609 |
///actual path computation. |
610 | 610 |
|
611 | 611 |
///@{ |
612 | 612 |
|
613 | 613 |
///Initializes the internal data structures. |
614 | 614 |
|
615 | 615 |
///Initializes the internal data structures. |
616 | 616 |
/// |
617 | 617 |
void init() |
618 | 618 |
{ |
619 | 619 |
create_maps(); |
620 | 620 |
_heap->clear(); |
621 | 621 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
622 | 622 |
_pred->set(u,INVALID); |
623 | 623 |
_processed->set(u,false); |
624 | 624 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
625 | 625 |
} |
626 | 626 |
} |
627 | 627 |
|
628 | 628 |
///Adds a new source node. |
629 | 629 |
|
630 | 630 |
///Adds a new source node to the priority heap. |
631 | 631 |
///The optional second parameter is the initial distance of the node. |
632 | 632 |
/// |
633 | 633 |
///The function checks if the node has already been added to the heap and |
634 | 634 |
///it is pushed to the heap only if either it was not in the heap |
635 | 635 |
///or the shortest path found till then is shorter than \c dst. |
636 | 636 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
637 | 637 |
{ |
638 | 638 |
if(_heap->state(s) != Heap::IN_HEAP) { |
639 | 639 |
_heap->push(s,dst); |
640 | 640 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
641 | 641 |
_heap->set(s,dst); |
642 | 642 |
_pred->set(s,INVALID); |
643 | 643 |
} |
644 | 644 |
} |
645 | 645 |
|
646 | 646 |
///Processes the next node in the priority heap |
647 | 647 |
|
648 | 648 |
///Processes the next node in the priority heap. |
649 | 649 |
/// |
650 | 650 |
///\return The processed node. |
651 | 651 |
/// |
652 | 652 |
///\warning The priority heap must not be empty. |
653 | 653 |
Node processNextNode() |
654 | 654 |
{ |
655 | 655 |
Node v=_heap->top(); |
656 | 656 |
Value oldvalue=_heap->prio(); |
657 | 657 |
_heap->pop(); |
658 | 658 |
finalizeNodeData(v,oldvalue); |
659 | 659 |
|
660 | 660 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
661 | 661 |
Node w=G->target(e); |
662 | 662 |
switch(_heap->state(w)) { |
663 | 663 |
case Heap::PRE_HEAP: |
664 | 664 |
_heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); |
665 | 665 |
_pred->set(w,e); |
666 | 666 |
break; |
667 | 667 |
case Heap::IN_HEAP: |
668 | 668 |
{ |
669 | 669 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
670 | 670 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
671 | 671 |
_heap->decrease(w, newvalue); |
672 | 672 |
_pred->set(w,e); |
673 | 673 |
} |
674 | 674 |
} |
675 | 675 |
break; |
676 | 676 |
case Heap::POST_HEAP: |
677 | 677 |
break; |
678 | 678 |
} |
679 | 679 |
} |
680 | 680 |
return v; |
681 | 681 |
} |
682 | 682 |
|
683 | 683 |
///The next node to be processed. |
684 | 684 |
|
685 | 685 |
///Returns the next node to be processed or \c INVALID if the |
686 | 686 |
///priority heap is empty. |
687 | 687 |
Node nextNode() const |
688 | 688 |
{ |
689 | 689 |
return !_heap->empty()?_heap->top():INVALID; |
690 | 690 |
} |
691 | 691 |
|
692 | 692 |
///\brief Returns \c false if there are nodes |
693 | 693 |
///to be processed. |
694 | 694 |
/// |
695 | 695 |
///Returns \c false if there are nodes |
696 | 696 |
///to be processed in the priority heap. |
697 | 697 |
bool emptyQueue() const { return _heap->empty(); } |
698 | 698 |
|
699 | 699 |
///Returns the number of the nodes to be processed in the priority heap |
700 | 700 |
|
701 | 701 |
///Returns the number of the nodes to be processed in the priority heap. |
702 | 702 |
/// |
703 | 703 |
int queueSize() const { return _heap->size(); } |
704 | 704 |
|
705 | 705 |
///Executes the algorithm. |
706 | 706 |
|
707 | 707 |
///Executes the algorithm. |
708 | 708 |
/// |
709 | 709 |
///This method runs the %Dijkstra algorithm from the root node(s) |
710 | 710 |
///in order to compute the shortest path to each node. |
711 | 711 |
/// |
712 | 712 |
///The algorithm computes |
713 | 713 |
///- the shortest path tree (forest), |
714 | 714 |
///- the distance of each node from the root(s). |
715 | 715 |
/// |
716 | 716 |
///\pre init() must be called and at least one root node should be |
717 | 717 |
///added with addSource() before using this function. |
718 | 718 |
/// |
719 | 719 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
720 | 720 |
///\code |
721 | 721 |
/// while ( !d.emptyQueue() ) { |
722 | 722 |
/// d.processNextNode(); |
723 | 723 |
/// } |
724 | 724 |
///\endcode |
725 | 725 |
void start() |
726 | 726 |
{ |
727 | 727 |
while ( !emptyQueue() ) processNextNode(); |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
///Executes the algorithm until the given target node is reached. |
731 | 731 |
|
732 | 732 |
///Executes the algorithm until the given target node is reached. |
733 | 733 |
/// |
734 | 734 |
///This method runs the %Dijkstra algorithm from the root node(s) |
735 | 735 |
///in order to compute the shortest path to \c dest. |
736 | 736 |
/// |
737 | 737 |
///The algorithm computes |
738 | 738 |
///- the shortest path to \c dest, |
739 | 739 |
///- the distance of \c dest from the root(s). |
740 | 740 |
/// |
741 | 741 |
///\pre init() must be called and at least one root node should be |
742 | 742 |
///added with addSource() before using this function. |
743 | 743 |
void start(Node dest) |
744 | 744 |
{ |
745 | 745 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
746 | 746 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
///Executes the algorithm until a condition is met. |
750 | 750 |
|
751 | 751 |
///Executes the algorithm until a condition is met. |
752 | 752 |
/// |
753 | 753 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
754 | 754 |
///order to compute the shortest path to a node \c v with |
755 | 755 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
756 | 756 |
/// |
757 | 757 |
///\param nm A \c bool (or convertible) node map. The algorithm |
758 | 758 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
759 | 759 |
/// |
760 | 760 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
761 | 761 |
///\c INVALID if no such node was found. |
762 | 762 |
/// |
763 | 763 |
///\pre init() must be called and at least one root node should be |
764 | 764 |
///added with addSource() before using this function. |
765 | 765 |
template<class NodeBoolMap> |
766 | 766 |
Node start(const NodeBoolMap &nm) |
767 | 767 |
{ |
768 | 768 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
769 | 769 |
if ( _heap->empty() ) return INVALID; |
770 | 770 |
finalizeNodeData(_heap->top(),_heap->prio()); |
771 | 771 |
return _heap->top(); |
772 | 772 |
} |
773 | 773 |
|
774 | 774 |
///Runs the algorithm from the given node. |
775 | 775 |
|
776 | 776 |
///This method runs the %Dijkstra algorithm from node \c s |
777 | 777 |
///in order to compute the shortest path to each node. |
778 | 778 |
/// |
779 | 779 |
///The algorithm computes |
780 | 780 |
///- the shortest path tree, |
781 | 781 |
///- the distance of each node from the root. |
782 | 782 |
/// |
783 | 783 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
784 | 784 |
///\code |
785 | 785 |
/// d.init(); |
786 | 786 |
/// d.addSource(s); |
787 | 787 |
/// d.start(); |
788 | 788 |
///\endcode |
789 | 789 |
void run(Node s) { |
790 | 790 |
init(); |
791 | 791 |
addSource(s); |
792 | 792 |
start(); |
793 | 793 |
} |
794 | 794 |
|
795 | 795 |
///Finds the shortest path between \c s and \c t. |
796 | 796 |
|
797 | 797 |
///This method runs the %Dijkstra algorithm from node \c s |
798 | 798 |
///in order to compute the shortest path to \c t. |
799 | 799 |
/// |
800 | 800 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
801 | 801 |
///if \c t is reachable form \c s, \c 0 otherwise. |
802 | 802 |
/// |
803 | 803 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
804 | 804 |
///shortcut of the following code. |
805 | 805 |
///\code |
806 | 806 |
/// d.init(); |
807 | 807 |
/// d.addSource(s); |
808 | 808 |
/// d.start(t); |
809 | 809 |
///\endcode |
810 | 810 |
Value run(Node s,Node t) { |
811 | 811 |
init(); |
812 | 812 |
addSource(s); |
813 | 813 |
start(t); |
814 | 814 |
return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t]; |
815 | 815 |
} |
816 | 816 |
|
817 | 817 |
///@} |
818 | 818 |
|
819 | 819 |
///\name Query Functions |
820 | 820 |
///The result of the %Dijkstra algorithm can be obtained using these |
821 | 821 |
///functions.\n |
822 | 822 |
///Either \ref lemon::Dijkstra::run() "run()" or |
823 | 823 |
///\ref lemon::Dijkstra::start() "start()" must be called before |
824 | 824 |
///using them. |
825 | 825 |
|
826 | 826 |
///@{ |
827 | 827 |
|
828 | 828 |
///The shortest path to a node. |
829 | 829 |
|
830 | 830 |
///Returns the shortest path to a node. |
831 | 831 |
/// |
832 | 832 |
///\warning \c t should be reachable from the root(s). |
833 | 833 |
/// |
834 | 834 |
///\pre Either \ref run() or \ref start() must be called before |
835 | 835 |
///using this function. |
836 | 836 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
837 | 837 |
|
838 | 838 |
///The distance of a node from the root(s). |
839 | 839 |
|
840 | 840 |
///Returns the distance of a node from the root(s). |
841 | 841 |
/// |
842 | 842 |
///\warning If node \c v is not reachable from the root(s), then |
843 | 843 |
///the return value of this function is undefined. |
844 | 844 |
/// |
845 | 845 |
///\pre Either \ref run() or \ref start() must be called before |
846 | 846 |
///using this function. |
847 | 847 |
Value dist(Node v) const { return (*_dist)[v]; } |
848 | 848 |
|
849 | 849 |
///Returns the 'previous arc' of the shortest path tree for a node. |
850 | 850 |
|
851 | 851 |
///This function returns the 'previous arc' of the shortest path |
852 | 852 |
///tree for the node \c v, i.e. it returns the last arc of a |
853 | 853 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
854 | 854 |
///is not reachable from the root(s) or if \c v is a root. |
855 | 855 |
/// |
856 | 856 |
///The shortest path tree used here is equal to the shortest path |
857 | 857 |
///tree used in \ref predNode(). |
858 | 858 |
/// |
859 | 859 |
///\pre Either \ref run() or \ref start() must be called before |
860 | 860 |
///using this function. |
861 | 861 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
862 | 862 |
|
863 | 863 |
///Returns the 'previous node' of the shortest path tree for a node. |
864 | 864 |
|
865 | 865 |
///This function returns the 'previous node' of the shortest path |
866 | 866 |
///tree for the node \c v, i.e. it returns the last but one node |
867 | 867 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
868 | 868 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
869 | 869 |
/// |
870 | 870 |
///The shortest path tree used here is equal to the shortest path |
871 | 871 |
///tree used in \ref predArc(). |
872 | 872 |
/// |
873 | 873 |
///\pre Either \ref run() or \ref start() must be called before |
874 | 874 |
///using this function. |
875 | 875 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
876 | 876 |
G->source((*_pred)[v]); } |
877 | 877 |
|
878 | 878 |
///\brief Returns a const reference to the node map that stores the |
879 | 879 |
///distances of the nodes. |
880 | 880 |
/// |
881 | 881 |
///Returns a const reference to the node map that stores the distances |
882 | 882 |
///of the nodes calculated by the algorithm. |
883 | 883 |
/// |
884 | 884 |
///\pre Either \ref run() or \ref init() |
885 | 885 |
///must be called before using this function. |
886 | 886 |
const DistMap &distMap() const { return *_dist;} |
887 | 887 |
|
888 | 888 |
///\brief Returns a const reference to the node map that stores the |
889 | 889 |
///predecessor arcs. |
890 | 890 |
/// |
891 | 891 |
///Returns a const reference to the node map that stores the predecessor |
892 | 892 |
///arcs, which form the shortest path tree. |
893 | 893 |
/// |
894 | 894 |
///\pre Either \ref run() or \ref init() |
895 | 895 |
///must be called before using this function. |
896 | 896 |
const PredMap &predMap() const { return *_pred;} |
897 | 897 |
|
898 | 898 |
///Checks if a node is reachable from the root(s). |
899 | 899 |
|
900 | 900 |
///Returns \c true if \c v is reachable from the root(s). |
901 | 901 |
///\pre Either \ref run() or \ref start() |
902 | 902 |
///must be called before using this function. |
903 | 903 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
904 | 904 |
Heap::PRE_HEAP; } |
905 | 905 |
|
906 | 906 |
///Checks if a node is processed. |
907 | 907 |
|
908 | 908 |
///Returns \c true if \c v is processed, i.e. the shortest |
909 | 909 |
///path to \c v has already found. |
910 | 910 |
///\pre Either \ref run() or \ref start() |
911 | 911 |
///must be called before using this function. |
912 | 912 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
913 | 913 |
Heap::POST_HEAP; } |
914 | 914 |
|
915 | 915 |
///The current distance of a node from the root(s). |
916 | 916 |
|
917 | 917 |
///Returns the current distance of a node from the root(s). |
918 | 918 |
///It may be decreased in the following processes. |
919 | 919 |
///\pre \c v should be reached but not processed. |
920 | 920 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
921 | 921 |
|
922 | 922 |
///@} |
923 | 923 |
}; |
924 | 924 |
|
925 | 925 |
|
926 | 926 |
///Default traits class of dijkstra() function. |
927 | 927 |
|
928 | 928 |
///Default traits class of dijkstra() function. |
929 | 929 |
///\tparam GR The type of the digraph. |
930 | 930 |
///\tparam LM The type of the length map. |
931 | 931 |
template<class GR, class LM> |
932 | 932 |
struct DijkstraWizardDefaultTraits |
933 | 933 |
{ |
934 | 934 |
///The type of the digraph the algorithm runs on. |
935 | 935 |
typedef GR Digraph; |
936 | 936 |
///The type of the map that stores the arc lengths. |
937 | 937 |
|
938 | 938 |
///The type of the map that stores the arc lengths. |
939 | 939 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
940 | 940 |
typedef LM LengthMap; |
941 | 941 |
///The type of the length of the arcs. |
942 | 942 |
typedef typename LM::Value Value; |
943 | 943 |
|
944 | 944 |
/// Operation traits for Dijkstra algorithm. |
945 | 945 |
|
946 | 946 |
/// This class defines the operations that are used in the algorithm. |
947 | 947 |
/// \see DijkstraDefaultOperationTraits |
948 | 948 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
949 | 949 |
|
950 | 950 |
/// The cross reference type used by the heap. |
951 | 951 |
|
952 | 952 |
/// The cross reference type used by the heap. |
953 | 953 |
/// Usually it is \c Digraph::NodeMap<int>. |
954 | 954 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
955 | 955 |
///Instantiates a \ref HeapCrossRef. |
956 | 956 |
|
957 | 957 |
///This function instantiates a \ref HeapCrossRef. |
958 | 958 |
/// \param g is the digraph, to which we would like to define the |
959 | 959 |
/// HeapCrossRef. |
960 | 960 |
/// \todo The digraph alone may be insufficient for the initialization |
961 | 961 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
962 | 962 |
{ |
963 | 963 |
return new HeapCrossRef(g); |
964 | 964 |
} |
965 | 965 |
|
966 | 966 |
///The heap type used by the Dijkstra algorithm. |
967 | 967 |
|
968 | 968 |
///The heap type used by the Dijkstra algorithm. |
969 | 969 |
/// |
970 | 970 |
///\sa BinHeap |
971 | 971 |
///\sa Dijkstra |
972 | 972 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
973 | 973 |
std::less<Value> > Heap; |
974 | 974 |
|
975 | 975 |
///Instantiates a \ref Heap. |
976 | 976 |
|
977 | 977 |
///This function instantiates a \ref Heap. |
978 | 978 |
/// \param r is the HeapCrossRef which is used. |
979 | 979 |
static Heap *createHeap(HeapCrossRef& r) |
980 | 980 |
{ |
981 | 981 |
return new Heap(r); |
982 | 982 |
} |
983 | 983 |
|
984 | 984 |
///\brief The type of the map that stores the predecessor |
985 | 985 |
///arcs of the shortest paths. |
986 | 986 |
/// |
987 | 987 |
///The type of the map that stores the predecessor |
988 | 988 |
///arcs of the shortest paths. |
989 | 989 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
990 | 990 |
typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
991 | 991 |
///Instantiates a \ref PredMap. |
992 | 992 |
|
993 | 993 |
///This function instantiates a \ref PredMap. |
994 | 994 |
///\param g is the digraph, to which we would like to define the |
995 | 995 |
///\ref PredMap. |
996 | 996 |
///\todo The digraph alone may be insufficient to initialize |
997 | 997 |
#ifdef DOXYGEN |
998 | 998 |
static PredMap *createPredMap(const Digraph &g) |
999 | 999 |
#else |
1000 | 1000 |
static PredMap *createPredMap(const Digraph &) |
1001 | 1001 |
#endif |
1002 | 1002 |
{ |
1003 | 1003 |
return new PredMap(); |
1004 | 1004 |
} |
1005 | 1005 |
|
1006 | 1006 |
///The type of the map that indicates which nodes are processed. |
1007 | 1007 |
|
1008 | 1008 |
///The type of the map that indicates which nodes are processed. |
1009 | 1009 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1010 | 1010 |
///By default it is a NullMap. |
1011 | 1011 |
///\todo If it is set to a real map, |
1012 | 1012 |
///Dijkstra::processed() should read this. |
1013 | 1013 |
///\todo named parameter to set this type, function to read and write. |
1014 | 1014 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1015 | 1015 |
///Instantiates a \ref ProcessedMap. |
1016 | 1016 |
|
1017 | 1017 |
///This function instantiates a \ref ProcessedMap. |
1018 | 1018 |
///\param g is the digraph, to which |
1019 | 1019 |
///we would like to define the \ref ProcessedMap. |
1020 | 1020 |
#ifdef DOXYGEN |
1021 | 1021 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1022 | 1022 |
#else |
1023 | 1023 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1024 | 1024 |
#endif |
1025 | 1025 |
{ |
1026 | 1026 |
return new ProcessedMap(); |
1027 | 1027 |
} |
1028 | 1028 |
|
1029 | 1029 |
///The type of the map that stores the distances of the nodes. |
1030 | 1030 |
|
1031 | 1031 |
///The type of the map that stores the distances of the nodes. |
1032 | 1032 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1033 | 1033 |
typedef NullMap<typename Digraph::Node,Value> DistMap; |
1034 | 1034 |
///Instantiates a \ref DistMap. |
1035 | 1035 |
|
1036 | 1036 |
///This function instantiates a \ref DistMap. |
1037 | 1037 |
///\param g is the digraph, to which we would like to define |
1038 | 1038 |
///the \ref DistMap |
1039 | 1039 |
#ifdef DOXYGEN |
1040 | 1040 |
static DistMap *createDistMap(const Digraph &g) |
1041 | 1041 |
#else |
1042 | 1042 |
static DistMap *createDistMap(const Digraph &) |
1043 | 1043 |
#endif |
1044 | 1044 |
{ |
1045 | 1045 |
return new DistMap(); |
1046 | 1046 |
} |
1047 | 1047 |
}; |
1048 | 1048 |
|
1049 | 1049 |
/// Default traits class used by \ref DijkstraWizard |
1050 | 1050 |
|
1051 | 1051 |
/// To make it easier to use Dijkstra algorithm |
1052 | 1052 |
/// we have created a wizard class. |
1053 | 1053 |
/// This \ref DijkstraWizard class needs default traits, |
1054 | 1054 |
/// as well as the \ref Dijkstra class. |
1055 | 1055 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1056 | 1056 |
/// \ref DijkstraWizard class. |
1057 | 1057 |
/// \todo More named parameters are required... |
1058 | 1058 |
template<class GR,class LM> |
1059 | 1059 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1060 | 1060 |
{ |
1061 | 1061 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1062 | 1062 |
protected: |
1063 | 1063 |
//The type of the nodes in the digraph. |
1064 | 1064 |
typedef typename Base::Digraph::Node Node; |
1065 | 1065 |
|
1066 | 1066 |
//Pointer to the digraph the algorithm runs on. |
1067 | 1067 |
void *_g; |
1068 | 1068 |
//Pointer to the length map |
1069 | 1069 |
void *_length; |
1070 |
//Pointer to the map of processed nodes. |
|
1071 |
void *_processed; |
|
1070 | 1072 |
//Pointer to the map of predecessors arcs. |
1071 | 1073 |
void *_pred; |
1072 | 1074 |
//Pointer to the map of distances. |
1073 | 1075 |
void *_dist; |
1074 | 1076 |
//Pointer to the source node. |
1075 | 1077 |
Node _source; |
1076 | 1078 |
|
1077 | 1079 |
public: |
1078 | 1080 |
/// Constructor. |
1079 | 1081 |
|
1080 | 1082 |
/// This constructor does not require parameters, therefore it initiates |
1081 | 1083 |
/// all of the attributes to default values (0, INVALID). |
1082 |
DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
|
1084 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
|
1083 | 1085 |
_dist(0), _source(INVALID) {} |
1084 | 1086 |
|
1085 | 1087 |
/// Constructor. |
1086 | 1088 |
|
1087 | 1089 |
/// This constructor requires some parameters, |
1088 | 1090 |
/// listed in the parameters list. |
1089 | 1091 |
/// Others are initiated to 0. |
1090 | 1092 |
/// \param g The digraph the algorithm runs on. |
1091 | 1093 |
/// \param l The length map. |
1092 | 1094 |
/// \param s The source node. |
1093 | 1095 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1094 | 1096 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1095 | 1097 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1096 |
_pred(0), _dist(0), _source(s) {} |
|
1098 |
_processed(0), _pred(0), _dist(0), _source(s) {} |
|
1097 | 1099 |
|
1098 | 1100 |
}; |
1099 | 1101 |
|
1100 | 1102 |
/// Auxiliary class for the function type interface of Dijkstra algorithm. |
1101 | 1103 |
|
1102 | 1104 |
/// This auxiliary class is created to implement the function type |
1103 | 1105 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features |
1104 | 1106 |
/// of the plain \ref Dijkstra, but it is much simpler to use it. |
1105 | 1107 |
/// It should only be used through the \ref dijkstra() function, which makes |
1106 | 1108 |
/// it easier to use the algorithm. |
1107 | 1109 |
/// |
1108 | 1110 |
/// Simplicity means that the way to change the types defined |
1109 | 1111 |
/// in the traits class is based on functions that returns the new class |
1110 | 1112 |
/// and not on templatable built-in classes. |
1111 | 1113 |
/// When using the plain \ref Dijkstra |
1112 | 1114 |
/// the new class with the modified type comes from |
1113 | 1115 |
/// the original class by using the :: |
1114 | 1116 |
/// operator. In the case of \ref DijkstraWizard only |
1115 | 1117 |
/// a function have to be called, and it will |
1116 | 1118 |
/// return the needed class. |
1117 | 1119 |
/// |
1118 | 1120 |
/// It does not have own \ref run() method. When its \ref run() method |
1119 | 1121 |
/// is called, it initiates a plain \ref Dijkstra object, and calls the |
1120 | 1122 |
/// \ref Dijkstra::run() method of it. |
1121 | 1123 |
template<class TR> |
1122 | 1124 |
class DijkstraWizard : public TR |
1123 | 1125 |
{ |
1124 | 1126 |
typedef TR Base; |
1125 | 1127 |
|
1126 | 1128 |
///The type of the digraph the algorithm runs on. |
1127 | 1129 |
typedef typename TR::Digraph Digraph; |
1128 | 1130 |
|
1129 | 1131 |
typedef typename Digraph::Node Node; |
1130 | 1132 |
typedef typename Digraph::NodeIt NodeIt; |
1131 | 1133 |
typedef typename Digraph::Arc Arc; |
1132 | 1134 |
typedef typename Digraph::OutArcIt OutArcIt; |
1133 | 1135 |
|
1134 | 1136 |
///The type of the map that stores the arc lengths. |
1135 | 1137 |
typedef typename TR::LengthMap LengthMap; |
1136 | 1138 |
///The type of the length of the arcs. |
1137 | 1139 |
typedef typename LengthMap::Value Value; |
1138 | 1140 |
///\brief The type of the map that stores the predecessor |
1139 | 1141 |
///arcs of the shortest paths. |
1140 | 1142 |
typedef typename TR::PredMap PredMap; |
1141 | 1143 |
///The type of the map that stores the distances of the nodes. |
1142 | 1144 |
typedef typename TR::DistMap DistMap; |
1143 | 1145 |
///The type of the map that indicates which nodes are processed. |
1144 | 1146 |
typedef typename TR::ProcessedMap ProcessedMap; |
1145 | 1147 |
///The heap type used by the dijkstra algorithm. |
1146 | 1148 |
typedef typename TR::Heap Heap; |
1147 | 1149 |
|
1148 | 1150 |
public: |
1149 | 1151 |
|
1150 | 1152 |
/// Constructor. |
1151 | 1153 |
DijkstraWizard() : TR() {} |
1152 | 1154 |
|
1153 | 1155 |
/// Constructor that requires parameters. |
1154 | 1156 |
|
1155 | 1157 |
/// Constructor that requires parameters. |
1156 | 1158 |
/// These parameters will be the default values for the traits class. |
1157 | 1159 |
DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
1158 | 1160 |
TR(g,l,s) {} |
1159 | 1161 |
|
1160 | 1162 |
///Copy constructor |
1161 | 1163 |
DijkstraWizard(const TR &b) : TR(b) {} |
1162 | 1164 |
|
1163 | 1165 |
~DijkstraWizard() {} |
1164 | 1166 |
|
1165 | 1167 |
///Runs Dijkstra algorithm from a source node. |
1166 | 1168 |
|
1167 | 1169 |
///Runs Dijkstra algorithm from a source node. |
1168 | 1170 |
///The node can be given with the \ref source() function. |
1169 | 1171 |
void run() |
1170 | 1172 |
{ |
1171 | 1173 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1172 | 1174 |
Dijkstra<Digraph,LengthMap,TR> |
1173 | 1175 |
dij(*reinterpret_cast<const Digraph*>(Base::_g), |
1174 | 1176 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1175 |
if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1176 |
if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1177 |
if(Base::_processed) |
|
1178 |
dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1179 |
if(Base::_pred) |
|
1180 |
dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1181 |
if(Base::_dist) |
|
1182 |
dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1177 | 1183 |
dij.run(Base::_source); |
1178 | 1184 |
} |
1179 | 1185 |
|
1180 | 1186 |
///Runs Dijkstra algorithm from the given node. |
1181 | 1187 |
|
1182 | 1188 |
///Runs Dijkstra algorithm from the given node. |
1183 | 1189 |
///\param s is the given source. |
1184 | 1190 |
void run(Node s) |
1185 | 1191 |
{ |
1186 | 1192 |
Base::_source=s; |
1187 | 1193 |
run(); |
1188 | 1194 |
} |
1189 | 1195 |
|
1190 | 1196 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1191 | 1197 |
|
1192 | 1198 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1193 | 1199 |
/// \param s is the source node. |
1194 | 1200 |
DijkstraWizard<TR> &source(Node s) |
1195 | 1201 |
{ |
1196 | 1202 |
Base::_source=s; |
1197 | 1203 |
return *this; |
1198 | 1204 |
} |
1199 | 1205 |
|
1200 | 1206 |
template<class T> |
1201 | 1207 |
struct SetPredMapBase : public Base { |
1202 | 1208 |
typedef T PredMap; |
1203 | 1209 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1204 | 1210 |
SetPredMapBase(const TR &b) : TR(b) {} |
1205 | 1211 |
}; |
1206 | 1212 |
///\brief \ref named-templ-param "Named parameter" |
1207 | 1213 |
///for setting \ref PredMap object. |
1208 | 1214 |
/// |
1209 | 1215 |
///\ref named-templ-param "Named parameter" |
1210 | 1216 |
///for setting \ref PredMap object. |
1211 | 1217 |
template<class T> |
1212 | 1218 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1213 | 1219 |
{ |
1214 | 1220 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1215 | 1221 |
return DijkstraWizard<SetPredMapBase<T> >(*this); |
1216 | 1222 |
} |
1217 | 1223 |
|
1218 | 1224 |
template<class T> |
1219 | 1225 |
struct SetProcessedMapBase : public Base { |
1220 | 1226 |
typedef T ProcessedMap; |
1221 | 1227 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1222 | 1228 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1223 | 1229 |
}; |
1224 | 1230 |
///\brief \ref named-templ-param "Named parameter" |
1225 | 1231 |
///for setting \ref ProcessedMap object. |
1226 | 1232 |
/// |
1227 | 1233 |
/// \ref named-templ-param "Named parameter" |
1228 | 1234 |
///for setting \ref ProcessedMap object. |
1229 | 1235 |
template<class T> |
1230 | 1236 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1231 | 1237 |
{ |
1232 | 1238 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1233 | 1239 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1234 | 1240 |
} |
1235 | 1241 |
|
1236 | 1242 |
template<class T> |
1237 | 1243 |
struct SetDistMapBase : public Base { |
1238 | 1244 |
typedef T DistMap; |
1239 | 1245 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1240 | 1246 |
SetDistMapBase(const TR &b) : TR(b) {} |
1241 | 1247 |
}; |
1242 | 1248 |
///\brief \ref named-templ-param "Named parameter" |
1243 | 1249 |
///for setting \ref DistMap object. |
1244 | 1250 |
/// |
1245 | 1251 |
///\ref named-templ-param "Named parameter" |
1246 | 1252 |
///for setting \ref DistMap object. |
1247 | 1253 |
template<class T> |
1248 | 1254 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
1249 | 1255 |
{ |
1250 | 1256 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1251 | 1257 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
1252 | 1258 |
} |
1253 | 1259 |
|
1254 | 1260 |
}; |
1255 | 1261 |
|
1256 | 1262 |
///Function type interface for Dijkstra algorithm. |
1257 | 1263 |
|
1258 | 1264 |
/// \ingroup shortest_path |
1259 | 1265 |
///Function type interface for Dijkstra algorithm. |
1260 | 1266 |
/// |
1261 | 1267 |
///This function also has several |
1262 | 1268 |
///\ref named-templ-func-param "named parameters", |
1263 | 1269 |
///they are declared as the members of class \ref DijkstraWizard. |
1264 | 1270 |
///The following |
1265 | 1271 |
///example shows how to use these parameters. |
1266 | 1272 |
///\code |
1267 | 1273 |
/// dijkstra(g,length,source).predMap(preds).run(); |
1268 | 1274 |
///\endcode |
1269 | 1275 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1270 | 1276 |
///to the end of the parameter list. |
1271 | 1277 |
///\sa DijkstraWizard |
1272 | 1278 |
///\sa Dijkstra |
1273 | 1279 |
template<class GR, class LM> |
1274 | 1280 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1275 | 1281 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
1276 | 1282 |
{ |
1277 | 1283 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
1278 | 1284 |
} |
1279 | 1285 |
|
1280 | 1286 |
} //END OF NAMESPACE LEMON |
1281 | 1287 |
|
1282 | 1288 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIM2_H |
20 | 20 |
#define LEMON_DIM2_H |
21 | 21 |
|
22 | 22 |
#include <iostream> |
23 | 23 |
|
24 | 24 |
///\ingroup misc |
25 | 25 |
///\file |
26 | 26 |
///\brief A simple two dimensional vector and a bounding box implementation |
27 | 27 |
/// |
28 | 28 |
/// The class \ref lemon::dim2::Point "dim2::Point" implements |
29 | 29 |
/// a two dimensional vector with the usual operations. |
30 | 30 |
/// |
31 |
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox" |
|
32 |
/// can be used to determine |
|
31 |
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine |
|
33 | 32 |
/// the rectangular bounding box of a set of |
34 | 33 |
/// \ref lemon::dim2::Point "dim2::Point"'s. |
35 | 34 |
|
36 | 35 |
namespace lemon { |
37 | 36 |
|
38 | 37 |
///Tools for handling two dimensional coordinates |
39 | 38 |
|
40 | 39 |
///This namespace is a storage of several |
41 | 40 |
///tools for handling two dimensional coordinates |
42 | 41 |
namespace dim2 { |
43 | 42 |
|
44 | 43 |
/// \addtogroup misc |
45 | 44 |
/// @{ |
46 | 45 |
|
47 |
/// |
|
46 |
/// Two dimensional vector (plain vector) |
|
48 | 47 |
|
49 | 48 |
/// A simple two dimensional vector (plain vector) implementation |
50 | 49 |
/// with the usual vector operations. |
51 | 50 |
template<typename T> |
52 | 51 |
class Point { |
53 | 52 |
|
54 | 53 |
public: |
55 | 54 |
|
56 | 55 |
typedef T Value; |
57 | 56 |
|
58 | 57 |
///First coordinate |
59 | 58 |
T x; |
60 | 59 |
///Second coordinate |
61 | 60 |
T y; |
62 | 61 |
|
63 | 62 |
///Default constructor |
64 | 63 |
Point() {} |
65 | 64 |
|
66 | 65 |
///Construct an instance from coordinates |
67 | 66 |
Point(T a, T b) : x(a), y(b) { } |
68 | 67 |
|
69 | 68 |
///Returns the dimension of the vector (i.e. returns 2). |
70 | 69 |
|
71 | 70 |
///The dimension of the vector. |
72 | 71 |
///This function always returns 2. |
73 | 72 |
int size() const { return 2; } |
74 | 73 |
|
75 | 74 |
///Subscripting operator |
76 | 75 |
|
77 | 76 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
78 | 77 |
/// |
79 | 78 |
T& operator[](int idx) { return idx == 0 ? x : y; } |
80 | 79 |
|
81 | 80 |
///Const subscripting operator |
82 | 81 |
|
83 | 82 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
84 | 83 |
/// |
85 | 84 |
const T& operator[](int idx) const { return idx == 0 ? x : y; } |
86 | 85 |
|
87 | 86 |
///Conversion constructor |
88 | 87 |
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {} |
89 | 88 |
|
90 | 89 |
///Give back the square of the norm of the vector |
91 | 90 |
T normSquare() const { |
92 | 91 |
return x*x+y*y; |
93 | 92 |
} |
94 | 93 |
|
95 | 94 |
///Increment the left hand side by \c u |
96 | 95 |
Point<T>& operator +=(const Point<T>& u) { |
97 | 96 |
x += u.x; |
98 | 97 |
y += u.y; |
99 | 98 |
return *this; |
100 | 99 |
} |
101 | 100 |
|
102 | 101 |
///Decrement the left hand side by \c u |
103 | 102 |
Point<T>& operator -=(const Point<T>& u) { |
104 | 103 |
x -= u.x; |
105 | 104 |
y -= u.y; |
106 | 105 |
return *this; |
107 | 106 |
} |
108 | 107 |
|
109 | 108 |
///Multiply the left hand side with a scalar |
110 | 109 |
Point<T>& operator *=(const T &u) { |
111 | 110 |
x *= u; |
112 | 111 |
y *= u; |
113 | 112 |
return *this; |
114 | 113 |
} |
115 | 114 |
|
116 | 115 |
///Divide the left hand side by a scalar |
117 | 116 |
Point<T>& operator /=(const T &u) { |
118 | 117 |
x /= u; |
119 | 118 |
y /= u; |
120 | 119 |
return *this; |
121 | 120 |
} |
122 | 121 |
|
123 | 122 |
///Return the scalar product of two vectors |
124 | 123 |
T operator *(const Point<T>& u) const { |
125 | 124 |
return x*u.x+y*u.y; |
126 | 125 |
} |
127 | 126 |
|
128 | 127 |
///Return the sum of two vectors |
129 | 128 |
Point<T> operator+(const Point<T> &u) const { |
130 | 129 |
Point<T> b=*this; |
131 | 130 |
return b+=u; |
132 | 131 |
} |
133 | 132 |
|
134 | 133 |
///Return the negative of the vector |
135 | 134 |
Point<T> operator-() const { |
136 | 135 |
Point<T> b=*this; |
137 | 136 |
b.x=-b.x; b.y=-b.y; |
138 | 137 |
return b; |
139 | 138 |
} |
140 | 139 |
|
141 | 140 |
///Return the difference of two vectors |
142 | 141 |
Point<T> operator-(const Point<T> &u) const { |
143 | 142 |
Point<T> b=*this; |
144 | 143 |
return b-=u; |
145 | 144 |
} |
146 | 145 |
|
147 | 146 |
///Return a vector multiplied by a scalar |
148 | 147 |
Point<T> operator*(const T &u) const { |
149 | 148 |
Point<T> b=*this; |
150 | 149 |
return b*=u; |
151 | 150 |
} |
152 | 151 |
|
153 | 152 |
///Return a vector divided by a scalar |
154 | 153 |
Point<T> operator/(const T &u) const { |
155 | 154 |
Point<T> b=*this; |
156 | 155 |
return b/=u; |
157 | 156 |
} |
158 | 157 |
|
159 | 158 |
///Test equality |
160 | 159 |
bool operator==(const Point<T> &u) const { |
161 | 160 |
return (x==u.x) && (y==u.y); |
162 | 161 |
} |
163 | 162 |
|
164 | 163 |
///Test inequality |
165 | 164 |
bool operator!=(Point u) const { |
166 | 165 |
return (x!=u.x) || (y!=u.y); |
167 | 166 |
} |
168 | 167 |
|
169 | 168 |
}; |
170 | 169 |
|
171 | 170 |
///Return a Point |
172 | 171 |
|
173 | 172 |
///Return a Point. |
174 | 173 |
///\relates Point |
175 | 174 |
template <typename T> |
176 | 175 |
inline Point<T> makePoint(const T& x, const T& y) { |
177 | 176 |
return Point<T>(x, y); |
178 | 177 |
} |
179 | 178 |
|
180 | 179 |
///Return a vector multiplied by a scalar |
181 | 180 |
|
182 | 181 |
///Return a vector multiplied by a scalar. |
183 | 182 |
///\relates Point |
184 | 183 |
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) { |
185 | 184 |
return x*u; |
186 | 185 |
} |
187 | 186 |
|
188 | 187 |
///Read a plain vector from a stream |
189 | 188 |
|
190 | 189 |
///Read a plain vector from a stream. |
191 | 190 |
///\relates Point |
192 | 191 |
/// |
193 | 192 |
template<typename T> |
194 | 193 |
inline std::istream& operator>>(std::istream &is, Point<T> &z) { |
195 | 194 |
char c; |
196 | 195 |
if (is >> c) { |
197 | 196 |
if (c != '(') is.putback(c); |
198 | 197 |
} else { |
199 | 198 |
is.clear(); |
200 | 199 |
} |
201 | 200 |
if (!(is >> z.x)) return is; |
202 | 201 |
if (is >> c) { |
203 | 202 |
if (c != ',') is.putback(c); |
204 | 203 |
} else { |
205 | 204 |
is.clear(); |
206 | 205 |
} |
207 | 206 |
if (!(is >> z.y)) return is; |
208 | 207 |
if (is >> c) { |
209 | 208 |
if (c != ')') is.putback(c); |
210 | 209 |
} else { |
211 | 210 |
is.clear(); |
212 | 211 |
} |
213 | 212 |
return is; |
214 | 213 |
} |
215 | 214 |
|
216 | 215 |
///Write a plain vector to a stream |
217 | 216 |
|
218 | 217 |
///Write a plain vector to a stream. |
219 | 218 |
///\relates Point |
220 | 219 |
/// |
221 | 220 |
template<typename T> |
222 | 221 |
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z) |
223 | 222 |
{ |
224 |
os << "(" << z.x << ", |
|
223 |
os << "(" << z.x << "," << z.y << ")"; |
|
225 | 224 |
return os; |
226 | 225 |
} |
227 | 226 |
|
228 | 227 |
///Rotate by 90 degrees |
229 | 228 |
|
230 | 229 |
///Returns the parameter rotated by 90 degrees in positive direction. |
231 | 230 |
///\relates Point |
232 | 231 |
/// |
233 | 232 |
template<typename T> |
234 | 233 |
inline Point<T> rot90(const Point<T> &z) |
235 | 234 |
{ |
236 | 235 |
return Point<T>(-z.y,z.x); |
237 | 236 |
} |
238 | 237 |
|
239 | 238 |
///Rotate by 180 degrees |
240 | 239 |
|
241 | 240 |
///Returns the parameter rotated by 180 degrees. |
242 | 241 |
///\relates Point |
243 | 242 |
/// |
244 | 243 |
template<typename T> |
245 | 244 |
inline Point<T> rot180(const Point<T> &z) |
246 | 245 |
{ |
247 | 246 |
return Point<T>(-z.x,-z.y); |
248 | 247 |
} |
249 | 248 |
|
250 | 249 |
///Rotate by 270 degrees |
251 | 250 |
|
252 | 251 |
///Returns the parameter rotated by 90 degrees in negative direction. |
253 | 252 |
///\relates Point |
254 | 253 |
/// |
255 | 254 |
template<typename T> |
256 | 255 |
inline Point<T> rot270(const Point<T> &z) |
257 | 256 |
{ |
258 | 257 |
return Point<T>(z.y,-z.x); |
259 | 258 |
} |
260 | 259 |
|
261 | 260 |
|
262 | 261 |
|
263 |
|
|
262 |
/// Bounding box of plain vectors (\ref Point points). |
|
264 | 263 |
|
265 |
/// A class to calculate or store the bounding box of plain vectors. |
|
266 |
/// |
|
267 |
template<typename T> |
|
268 |
class BoundingBox { |
|
264 |
/// A class to calculate or store the bounding box of plain vectors |
|
265 |
/// (\ref Point points). |
|
266 |
template<typename T> |
|
267 |
class Box { |
|
269 | 268 |
Point<T> _bottom_left, _top_right; |
270 | 269 |
bool _empty; |
271 | 270 |
public: |
272 | 271 |
|
273 |
///Default constructor: creates an empty bounding box |
|
274 |
BoundingBox() { _empty = true; } |
|
272 |
///Default constructor: creates an empty box |
|
273 |
Box() { _empty = true; } |
|
275 | 274 |
|
276 |
///Construct an instance from one point |
|
277 |
BoundingBox(Point<T> a) { |
|
275 |
///Construct a box from one point |
|
276 |
Box(Point<T> a) { |
|
278 | 277 |
_bottom_left = _top_right = a; |
279 | 278 |
_empty = false; |
280 | 279 |
} |
281 | 280 |
|
282 |
///Construct |
|
281 |
///Construct a box from two points |
|
283 | 282 |
|
284 |
///Construct |
|
283 |
///Construct a box from two points. |
|
285 | 284 |
///\param a The bottom left corner. |
286 | 285 |
///\param b The top right corner. |
287 | 286 |
///\warning The coordinates of the bottom left corner must be no more |
288 | 287 |
///than those of the top right one. |
289 |
|
|
288 |
Box(Point<T> a,Point<T> b) |
|
290 | 289 |
{ |
291 | 290 |
_bottom_left = a; |
292 | 291 |
_top_right = b; |
293 | 292 |
_empty = false; |
294 | 293 |
} |
295 | 294 |
|
296 |
///Construct |
|
295 |
///Construct a box from four numbers |
|
297 | 296 |
|
298 |
///Construct |
|
297 |
///Construct a box from four numbers. |
|
299 | 298 |
///\param l The left side of the box. |
300 | 299 |
///\param b The bottom of the box. |
301 | 300 |
///\param r The right side of the box. |
302 | 301 |
///\param t The top of the box. |
303 | 302 |
///\warning The left side must be no more than the right side and |
304 | 303 |
///bottom must be no more than the top. |
305 |
|
|
304 |
Box(T l,T b,T r,T t) |
|
306 | 305 |
{ |
307 | 306 |
_bottom_left=Point<T>(l,b); |
308 | 307 |
_top_right=Point<T>(r,t); |
309 | 308 |
_empty = false; |
310 | 309 |
} |
311 | 310 |
|
312 |
///Return \c true if the |
|
311 |
///Return \c true if the box is empty. |
|
313 | 312 |
|
314 |
///Return \c true if the |
|
313 |
///Return \c true if the box is empty (i.e. return \c false |
|
315 | 314 |
///if at least one point was added to the box or the coordinates of |
316 | 315 |
///the box were set). |
317 | 316 |
/// |
318 |
///The coordinates of an empty |
|
317 |
///The coordinates of an empty box are not defined. |
|
319 | 318 |
bool empty() const { |
320 | 319 |
return _empty; |
321 | 320 |
} |
322 | 321 |
|
323 |
///Make the |
|
322 |
///Make the box empty |
|
324 | 323 |
void clear() { |
325 | 324 |
_empty = true; |
326 | 325 |
} |
327 | 326 |
|
328 | 327 |
///Give back the bottom left corner of the box |
329 | 328 |
|
330 | 329 |
///Give back the bottom left corner of the box. |
331 |
///If the |
|
330 |
///If the box is empty, then the return value is not defined. |
|
332 | 331 |
Point<T> bottomLeft() const { |
333 | 332 |
return _bottom_left; |
334 | 333 |
} |
335 | 334 |
|
336 | 335 |
///Set the bottom left corner of the box |
337 | 336 |
|
338 | 337 |
///Set the bottom left corner of the box. |
339 | 338 |
///\pre The box must not be empty. |
340 | 339 |
void bottomLeft(Point<T> p) { |
341 | 340 |
_bottom_left = p; |
342 | 341 |
} |
343 | 342 |
|
344 | 343 |
///Give back the top right corner of the box |
345 | 344 |
|
346 | 345 |
///Give back the top right corner of the box. |
347 |
///If the |
|
346 |
///If the box is empty, then the return value is not defined. |
|
348 | 347 |
Point<T> topRight() const { |
349 | 348 |
return _top_right; |
350 | 349 |
} |
351 | 350 |
|
352 | 351 |
///Set the top right corner of the box |
353 | 352 |
|
354 | 353 |
///Set the top right corner of the box. |
355 | 354 |
///\pre The box must not be empty. |
356 | 355 |
void topRight(Point<T> p) { |
357 | 356 |
_top_right = p; |
358 | 357 |
} |
359 | 358 |
|
360 | 359 |
///Give back the bottom right corner of the box |
361 | 360 |
|
362 | 361 |
///Give back the bottom right corner of the box. |
363 |
///If the |
|
362 |
///If the box is empty, then the return value is not defined. |
|
364 | 363 |
Point<T> bottomRight() const { |
365 | 364 |
return Point<T>(_top_right.x,_bottom_left.y); |
366 | 365 |
} |
367 | 366 |
|
368 | 367 |
///Set the bottom right corner of the box |
369 | 368 |
|
370 | 369 |
///Set the bottom right corner of the box. |
371 | 370 |
///\pre The box must not be empty. |
372 | 371 |
void bottomRight(Point<T> p) { |
373 | 372 |
_top_right.x = p.x; |
374 | 373 |
_bottom_left.y = p.y; |
375 | 374 |
} |
376 | 375 |
|
377 | 376 |
///Give back the top left corner of the box |
378 | 377 |
|
379 | 378 |
///Give back the top left corner of the box. |
380 |
///If the |
|
379 |
///If the box is empty, then the return value is not defined. |
|
381 | 380 |
Point<T> topLeft() const { |
382 | 381 |
return Point<T>(_bottom_left.x,_top_right.y); |
383 | 382 |
} |
384 | 383 |
|
385 | 384 |
///Set the top left corner of the box |
386 | 385 |
|
387 | 386 |
///Set the top left corner of the box. |
388 | 387 |
///\pre The box must not be empty. |
389 | 388 |
void topLeft(Point<T> p) { |
390 | 389 |
_top_right.y = p.y; |
391 | 390 |
_bottom_left.x = p.x; |
392 | 391 |
} |
393 | 392 |
|
394 | 393 |
///Give back the bottom of the box |
395 | 394 |
|
396 | 395 |
///Give back the bottom of the box. |
397 |
///If the |
|
396 |
///If the box is empty, then the return value is not defined. |
|
398 | 397 |
T bottom() const { |
399 | 398 |
return _bottom_left.y; |
400 | 399 |
} |
401 | 400 |
|
402 | 401 |
///Set the bottom of the box |
403 | 402 |
|
404 | 403 |
///Set the bottom of the box. |
405 | 404 |
///\pre The box must not be empty. |
406 | 405 |
void bottom(T t) { |
407 | 406 |
_bottom_left.y = t; |
408 | 407 |
} |
409 | 408 |
|
410 | 409 |
///Give back the top of the box |
411 | 410 |
|
412 | 411 |
///Give back the top of the box. |
413 |
///If the |
|
412 |
///If the box is empty, then the return value is not defined. |
|
414 | 413 |
T top() const { |
415 | 414 |
return _top_right.y; |
416 | 415 |
} |
417 | 416 |
|
418 | 417 |
///Set the top of the box |
419 | 418 |
|
420 | 419 |
///Set the top of the box. |
421 | 420 |
///\pre The box must not be empty. |
422 | 421 |
void top(T t) { |
423 | 422 |
_top_right.y = t; |
424 | 423 |
} |
425 | 424 |
|
426 | 425 |
///Give back the left side of the box |
427 | 426 |
|
428 | 427 |
///Give back the left side of the box. |
429 |
///If the |
|
428 |
///If the box is empty, then the return value is not defined. |
|
430 | 429 |
T left() const { |
431 | 430 |
return _bottom_left.x; |
432 | 431 |
} |
433 | 432 |
|
434 | 433 |
///Set the left side of the box |
435 | 434 |
|
436 | 435 |
///Set the left side of the box. |
437 | 436 |
///\pre The box must not be empty. |
438 | 437 |
void left(T t) { |
439 | 438 |
_bottom_left.x = t; |
440 | 439 |
} |
441 | 440 |
|
442 | 441 |
/// Give back the right side of the box |
443 | 442 |
|
444 | 443 |
/// Give back the right side of the box. |
445 |
///If the |
|
444 |
///If the box is empty, then the return value is not defined. |
|
446 | 445 |
T right() const { |
447 | 446 |
return _top_right.x; |
448 | 447 |
} |
449 | 448 |
|
450 | 449 |
///Set the right side of the box |
451 | 450 |
|
452 | 451 |
///Set the right side of the box. |
453 | 452 |
///\pre The box must not be empty. |
454 | 453 |
void right(T t) { |
455 | 454 |
_top_right.x = t; |
456 | 455 |
} |
457 | 456 |
|
458 | 457 |
///Give back the height of the box |
459 | 458 |
|
460 | 459 |
///Give back the height of the box. |
461 |
///If the |
|
460 |
///If the box is empty, then the return value is not defined. |
|
462 | 461 |
T height() const { |
463 | 462 |
return _top_right.y-_bottom_left.y; |
464 | 463 |
} |
465 | 464 |
|
466 | 465 |
///Give back the width of the box |
467 | 466 |
|
468 | 467 |
///Give back the width of the box. |
469 |
///If the |
|
468 |
///If the box is empty, then the return value is not defined. |
|
470 | 469 |
T width() const { |
471 | 470 |
return _top_right.x-_bottom_left.x; |
472 | 471 |
} |
473 | 472 |
|
474 |
///Checks whether a point is inside |
|
473 |
///Checks whether a point is inside the box |
|
475 | 474 |
bool inside(const Point<T>& u) const { |
476 | 475 |
if (_empty) |
477 | 476 |
return false; |
478 | 477 |
else { |
479 | 478 |
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 && |
480 | 479 |
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 ); |
481 | 480 |
} |
482 | 481 |
} |
483 | 482 |
|
484 |
///Increments |
|
483 |
///Increments the box with a point |
|
485 | 484 |
|
486 |
///Increments |
|
485 |
///Increments the box with a point. |
|
487 | 486 |
/// |
488 |
|
|
487 |
Box& add(const Point<T>& u){ |
|
489 | 488 |
if (_empty) { |
490 | 489 |
_bottom_left = _top_right = u; |
491 | 490 |
_empty = false; |
492 | 491 |
} |
493 | 492 |
else { |
494 | 493 |
if (_bottom_left.x > u.x) _bottom_left.x = u.x; |
495 | 494 |
if (_bottom_left.y > u.y) _bottom_left.y = u.y; |
496 | 495 |
if (_top_right.x < u.x) _top_right.x = u.x; |
497 | 496 |
if (_top_right.y < u.y) _top_right.y = u.y; |
498 | 497 |
} |
499 | 498 |
return *this; |
500 | 499 |
} |
501 | 500 |
|
502 |
///Increments |
|
501 |
///Increments the box to contain another box |
|
503 | 502 |
|
504 |
///Increments |
|
503 |
///Increments the box to contain another box. |
|
505 | 504 |
/// |
506 |
|
|
505 |
Box& add(const Box &u){ |
|
507 | 506 |
if ( !u.empty() ){ |
508 | 507 |
add(u._bottom_left); |
509 | 508 |
add(u._top_right); |
510 | 509 |
} |
511 | 510 |
return *this; |
512 | 511 |
} |
513 | 512 |
|
514 |
///Intersection of two |
|
513 |
///Intersection of two boxes |
|
515 | 514 |
|
516 |
///Intersection of two |
|
515 |
///Intersection of two boxes. |
|
517 | 516 |
/// |
518 |
BoundingBox operator&(const BoundingBox& u) const { |
|
519 |
BoundingBox b; |
|
517 |
Box operator&(const Box& u) const { |
|
518 |
Box b; |
|
520 | 519 |
if (_empty || u._empty) { |
521 | 520 |
b._empty = true; |
522 | 521 |
} else { |
523 | 522 |
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x); |
524 | 523 |
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y); |
525 | 524 |
b._top_right.x = std::min(_top_right.x, u._top_right.x); |
526 | 525 |
b._top_right.y = std::min(_top_right.y, u._top_right.y); |
527 | 526 |
b._empty = b._bottom_left.x > b._top_right.x || |
528 | 527 |
b._bottom_left.y > b._top_right.y; |
529 | 528 |
} |
530 | 529 |
return b; |
531 | 530 |
} |
532 | 531 |
|
533 |
|
|
532 |
};//class Box |
|
534 | 533 |
|
535 | 534 |
|
535 |
///Read a box from a stream |
|
536 |
|
|
537 |
///Read a box from a stream. |
|
538 |
///\relates Box |
|
539 |
template<typename T> |
|
540 |
inline std::istream& operator>>(std::istream &is, Box<T>& b) { |
|
541 |
char c; |
|
542 |
Point<T> p; |
|
543 |
if (is >> c) { |
|
544 |
if (c != '(') is.putback(c); |
|
545 |
} else { |
|
546 |
is.clear(); |
|
547 |
} |
|
548 |
if (!(is >> p)) return is; |
|
549 |
b.bottomLeft(p); |
|
550 |
if (is >> c) { |
|
551 |
if (c != ',') is.putback(c); |
|
552 |
} else { |
|
553 |
is.clear(); |
|
554 |
} |
|
555 |
if (!(is >> p)) return is; |
|
556 |
b.topRight(p); |
|
557 |
if (is >> c) { |
|
558 |
if (c != ')') is.putback(c); |
|
559 |
} else { |
|
560 |
is.clear(); |
|
561 |
} |
|
562 |
return is; |
|
563 |
} |
|
564 |
|
|
565 |
///Write a box to a stream |
|
566 |
|
|
567 |
///Write a box to a stream. |
|
568 |
///\relates Box |
|
569 |
template<typename T> |
|
570 |
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b) |
|
571 |
{ |
|
572 |
os << "(" << b.bottomLeft() << "," << b.topRight() << ")"; |
|
573 |
return os; |
|
574 |
} |
|
575 |
|
|
536 | 576 |
///Map of x-coordinates of a \ref Point "Point"-map |
537 | 577 |
|
538 | 578 |
///\ingroup maps |
539 | 579 |
///Map of x-coordinates of a \ref Point "Point"-map. |
540 | 580 |
/// |
541 | 581 |
template<class M> |
542 | 582 |
class XMap |
543 | 583 |
{ |
544 | 584 |
M& _map; |
545 | 585 |
public: |
546 | 586 |
|
547 | 587 |
typedef typename M::Value::Value Value; |
548 | 588 |
typedef typename M::Key Key; |
549 | 589 |
///\e |
550 | 590 |
XMap(M& map) : _map(map) {} |
551 | 591 |
Value operator[](Key k) const {return _map[k].x;} |
552 | 592 |
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
553 | 593 |
}; |
554 | 594 |
|
555 | 595 |
///Returns an \ref XMap class |
556 | 596 |
|
557 | 597 |
///This function just returns an \ref XMap class. |
558 | 598 |
/// |
559 | 599 |
///\ingroup maps |
560 | 600 |
///\relates XMap |
561 | 601 |
template<class M> |
562 | 602 |
inline XMap<M> xMap(M &m) |
563 | 603 |
{ |
564 | 604 |
return XMap<M>(m); |
565 | 605 |
} |
566 | 606 |
|
567 | 607 |
template<class M> |
568 | 608 |
inline XMap<M> xMap(const M &m) |
569 | 609 |
{ |
570 | 610 |
return XMap<M>(m); |
571 | 611 |
} |
572 | 612 |
|
573 | 613 |
///Constant (read only) version of \ref XMap |
574 | 614 |
|
575 | 615 |
///\ingroup maps |
576 | 616 |
///Constant (read only) version of \ref XMap |
577 | 617 |
/// |
578 | 618 |
template<class M> |
579 | 619 |
class ConstXMap |
580 | 620 |
{ |
581 | 621 |
const M& _map; |
582 | 622 |
public: |
583 | 623 |
|
584 | 624 |
typedef typename M::Value::Value Value; |
585 | 625 |
typedef typename M::Key Key; |
586 | 626 |
///\e |
587 | 627 |
ConstXMap(const M &map) : _map(map) {} |
588 | 628 |
Value operator[](Key k) const {return _map[k].x;} |
589 | 629 |
}; |
590 | 630 |
|
591 | 631 |
///Returns a \ref ConstXMap class |
592 | 632 |
|
593 | 633 |
///This function just returns a \ref ConstXMap class. |
594 | 634 |
/// |
595 | 635 |
///\ingroup maps |
596 | 636 |
///\relates ConstXMap |
597 | 637 |
template<class M> |
598 | 638 |
inline ConstXMap<M> xMap(const M &m) |
599 | 639 |
{ |
600 | 640 |
return ConstXMap<M>(m); |
601 | 641 |
} |
602 | 642 |
|
603 | 643 |
///Map of y-coordinates of a \ref Point "Point"-map |
604 | 644 |
|
605 | 645 |
///\ingroup maps |
606 | 646 |
///Map of y-coordinates of a \ref Point "Point"-map. |
607 | 647 |
/// |
608 | 648 |
template<class M> |
609 | 649 |
class YMap |
610 | 650 |
{ |
611 | 651 |
M& _map; |
612 | 652 |
public: |
613 | 653 |
|
614 | 654 |
typedef typename M::Value::Value Value; |
615 | 655 |
typedef typename M::Key Key; |
616 | 656 |
///\e |
617 | 657 |
YMap(M& map) : _map(map) {} |
618 | 658 |
Value operator[](Key k) const {return _map[k].y;} |
619 | 659 |
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
620 | 660 |
}; |
621 | 661 |
|
622 | 662 |
///Returns a \ref YMap class |
623 | 663 |
|
624 | 664 |
///This function just returns a \ref YMap class. |
625 | 665 |
/// |
626 | 666 |
///\ingroup maps |
627 | 667 |
///\relates YMap |
628 | 668 |
template<class M> |
629 | 669 |
inline YMap<M> yMap(M &m) |
630 | 670 |
{ |
631 | 671 |
return YMap<M>(m); |
632 | 672 |
} |
633 | 673 |
|
634 | 674 |
template<class M> |
635 | 675 |
inline YMap<M> yMap(const M &m) |
636 | 676 |
{ |
637 | 677 |
return YMap<M>(m); |
638 | 678 |
} |
639 | 679 |
|
640 | 680 |
///Constant (read only) version of \ref YMap |
641 | 681 |
|
642 | 682 |
///\ingroup maps |
643 | 683 |
///Constant (read only) version of \ref YMap |
644 | 684 |
/// |
645 | 685 |
template<class M> |
646 | 686 |
class ConstYMap |
647 | 687 |
{ |
648 | 688 |
const M& _map; |
649 | 689 |
public: |
650 | 690 |
|
651 | 691 |
typedef typename M::Value::Value Value; |
652 | 692 |
typedef typename M::Key Key; |
653 | 693 |
///\e |
654 | 694 |
ConstYMap(const M &map) : _map(map) {} |
655 | 695 |
Value operator[](Key k) const {return _map[k].y;} |
656 | 696 |
}; |
657 | 697 |
|
658 | 698 |
///Returns a \ref ConstYMap class |
659 | 699 |
|
660 | 700 |
///This function just returns a \ref ConstYMap class. |
661 | 701 |
/// |
662 | 702 |
///\ingroup maps |
663 | 703 |
///\relates ConstYMap |
664 | 704 |
template<class M> |
665 | 705 |
inline ConstYMap<M> yMap(const M &m) |
666 | 706 |
{ |
667 | 707 |
return ConstYMap<M>(m); |
668 | 708 |
} |
669 | 709 |
|
670 | 710 |
|
671 | 711 |
///\brief Map of the \ref Point::normSquare() "normSquare()" |
672 | 712 |
///of a \ref Point "Point"-map |
673 | 713 |
/// |
674 | 714 |
///Map of the \ref Point::normSquare() "normSquare()" |
675 | 715 |
///of a \ref Point "Point"-map. |
676 | 716 |
///\ingroup maps |
677 | 717 |
template<class M> |
678 | 718 |
class NormSquareMap |
679 | 719 |
{ |
680 | 720 |
const M& _map; |
681 | 721 |
public: |
682 | 722 |
|
683 | 723 |
typedef typename M::Value::Value Value; |
684 | 724 |
typedef typename M::Key Key; |
685 | 725 |
///\e |
686 | 726 |
NormSquareMap(const M &map) : _map(map) {} |
687 | 727 |
Value operator[](Key k) const {return _map[k].normSquare();} |
688 | 728 |
}; |
689 | 729 |
|
690 | 730 |
///Returns a \ref NormSquareMap class |
691 | 731 |
|
692 | 732 |
///This function just returns a \ref NormSquareMap class. |
693 | 733 |
/// |
694 | 734 |
///\ingroup maps |
695 | 735 |
///\relates NormSquareMap |
696 | 736 |
template<class M> |
697 | 737 |
inline NormSquareMap<M> normSquareMap(const M &m) |
698 | 738 |
{ |
699 | 739 |
return NormSquareMap<M>(m); |
700 | 740 |
} |
701 | 741 |
|
702 | 742 |
/// @} |
703 | 743 |
|
704 | 744 |
} //namespce dim2 |
705 | 745 |
|
706 | 746 |
} //namespace lemon |
707 | 747 |
|
708 | 748 |
#endif //LEMON_DIM2_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#define WIN32_LEAN_AND_MEAN |
33 | 33 |
#define NOMINMAX |
34 | 34 |
#include<windows.h> |
35 | 35 |
#endif |
36 | 36 |
|
37 | 37 |
#include<lemon/math.h> |
38 | 38 |
#include<lemon/core.h> |
39 | 39 |
#include<lemon/dim2.h> |
40 | 40 |
#include<lemon/maps.h> |
41 | 41 |
#include<lemon/color.h> |
42 | 42 |
#include<lemon/bits/bezier.h> |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
///\ingroup eps_io |
46 | 46 |
///\file |
47 | 47 |
///\brief A well configurable tool for visualizing graphs |
48 | 48 |
|
49 | 49 |
namespace lemon { |
50 | 50 |
|
51 | 51 |
namespace _graph_to_eps_bits { |
52 | 52 |
template<class MT> |
53 | 53 |
class _NegY { |
54 | 54 |
public: |
55 | 55 |
typedef typename MT::Key Key; |
56 | 56 |
typedef typename MT::Value Value; |
57 | 57 |
const MT ↦ |
58 | 58 |
int yscale; |
59 | 59 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
60 | 60 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
61 | 61 |
}; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
///Default traits class of \ref GraphToEps |
65 | 65 |
|
66 | 66 |
///Default traits class of \ref GraphToEps. |
67 | 67 |
/// |
68 | 68 |
///\c G is the type of the underlying graph. |
69 | 69 |
template<class G> |
70 | 70 |
struct DefaultGraphToEpsTraits |
71 | 71 |
{ |
72 | 72 |
typedef G Graph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
|
102 | 102 |
bool _drawArrows; |
103 | 103 |
double _arrowLength, _arrowWidth; |
104 | 104 |
|
105 | 105 |
bool _showNodes, _showArcs; |
106 | 106 |
|
107 | 107 |
bool _enableParallel; |
108 | 108 |
double _parArcDist; |
109 | 109 |
|
110 | 110 |
bool _showNodeText; |
111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
112 | 112 |
double _nodeTextSize; |
113 | 113 |
|
114 | 114 |
bool _showNodePsText; |
115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
116 | 116 |
char *_nodePsTextsPreamble; |
117 | 117 |
|
118 | 118 |
bool _undirected; |
119 | 119 |
|
120 | 120 |
bool _pleaseRemoveOsStream; |
121 | 121 |
|
122 | 122 |
bool _scaleToA4; |
123 | 123 |
|
124 | 124 |
std::string _title; |
125 | 125 |
std::string _copyright; |
126 | 126 |
|
127 | 127 |
enum NodeTextColorType |
128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
130 | 130 |
|
131 | 131 |
bool _autoNodeScale; |
132 | 132 |
bool _autoArcWidthScale; |
133 | 133 |
|
134 | 134 |
bool _absoluteNodeSizes; |
135 | 135 |
bool _absoluteArcWidths; |
136 | 136 |
|
137 | 137 |
bool _negY; |
138 | 138 |
|
139 | 139 |
bool _preScale; |
140 | 140 |
///Constructor |
141 | 141 |
|
142 | 142 |
///Constructor |
143 | 143 |
///\param _g Reference to the graph to be printed. |
144 | 144 |
///\param _os Reference to the output stream. |
145 | 145 |
///\param _os Reference to the output stream. |
146 | 146 |
///By default it is <tt>std::cout</tt>. |
147 | 147 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
148 | 148 |
///will be explicitly deallocated by the destructor. |
149 | 149 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
150 | 150 |
bool _pros=false) : |
151 | 151 |
g(_g), os(_os), |
152 | 152 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
153 | 153 |
_nodeColors(WHITE), _arcColors(BLACK), |
154 | 154 |
_arcWidths(1.0), _arcWidthScale(0.003), |
155 | 155 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
156 | 156 |
_nodeBorderQuotient(.1), |
157 | 157 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
158 | 158 |
_showNodes(true), _showArcs(true), |
159 | 159 |
_enableParallel(false), _parArcDist(1), |
160 | 160 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
161 | 161 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
162 | 162 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
163 | 163 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
164 | 164 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
165 | 165 |
_autoNodeScale(false), |
166 | 166 |
_autoArcWidthScale(false), |
167 | 167 |
_absoluteNodeSizes(false), |
168 | 168 |
_absoluteArcWidths(false), |
169 | 169 |
_negY(false), |
170 | 170 |
_preScale(true) |
171 | 171 |
{} |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
175 | 175 |
|
176 | 176 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
177 | 177 |
/// |
178 | 178 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
179 | 179 |
template<class T> class GraphToEps : public T |
180 | 180 |
{ |
181 | 181 |
// Can't believe it is required by the C++ standard |
182 | 182 |
using T::g; |
183 | 183 |
using T::os; |
184 | 184 |
|
185 | 185 |
using T::_coords; |
186 | 186 |
using T::_nodeSizes; |
187 | 187 |
using T::_nodeShapes; |
188 | 188 |
using T::_nodeColors; |
189 | 189 |
using T::_arcColors; |
190 | 190 |
using T::_arcWidths; |
191 | 191 |
|
192 | 192 |
using T::_arcWidthScale; |
193 | 193 |
using T::_nodeScale; |
194 | 194 |
using T::_xBorder; |
195 | 195 |
using T::_yBorder; |
196 | 196 |
using T::_scale; |
197 | 197 |
using T::_nodeBorderQuotient; |
198 | 198 |
|
199 | 199 |
using T::_drawArrows; |
200 | 200 |
using T::_arrowLength; |
201 | 201 |
using T::_arrowWidth; |
202 | 202 |
|
203 | 203 |
using T::_showNodes; |
204 | 204 |
using T::_showArcs; |
205 | 205 |
|
206 | 206 |
using T::_enableParallel; |
207 | 207 |
using T::_parArcDist; |
208 | 208 |
|
209 | 209 |
using T::_showNodeText; |
210 | 210 |
using T::_nodeTexts; |
211 | 211 |
using T::_nodeTextSize; |
212 | 212 |
|
213 | 213 |
using T::_showNodePsText; |
214 | 214 |
using T::_nodePsTexts; |
215 | 215 |
using T::_nodePsTextsPreamble; |
216 | 216 |
|
217 | 217 |
using T::_undirected; |
218 | 218 |
|
219 | 219 |
using T::_pleaseRemoveOsStream; |
220 | 220 |
|
221 | 221 |
using T::_scaleToA4; |
222 | 222 |
|
223 | 223 |
using T::_title; |
224 | 224 |
using T::_copyright; |
225 | 225 |
|
226 | 226 |
using T::NodeTextColorType; |
227 | 227 |
using T::CUST_COL; |
228 | 228 |
using T::DIST_COL; |
229 | 229 |
using T::DIST_BW; |
230 | 230 |
using T::_nodeTextColorType; |
231 | 231 |
using T::_nodeTextColors; |
232 | 232 |
|
233 | 233 |
using T::_autoNodeScale; |
234 | 234 |
using T::_autoArcWidthScale; |
235 | 235 |
|
236 | 236 |
using T::_absoluteNodeSizes; |
237 | 237 |
using T::_absoluteArcWidths; |
238 | 238 |
|
239 | 239 |
|
240 | 240 |
using T::_negY; |
241 | 241 |
using T::_preScale; |
242 | 242 |
|
243 | 243 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
244 | 244 |
|
245 | 245 |
typedef typename T::Graph Graph; |
246 | 246 |
typedef typename Graph::Node Node; |
247 | 247 |
typedef typename Graph::NodeIt NodeIt; |
248 | 248 |
typedef typename Graph::Arc Arc; |
249 | 249 |
typedef typename Graph::ArcIt ArcIt; |
250 | 250 |
typedef typename Graph::InArcIt InArcIt; |
251 | 251 |
typedef typename Graph::OutArcIt OutArcIt; |
252 | 252 |
|
253 | 253 |
static const int INTERPOL_PREC; |
254 | 254 |
static const double A4HEIGHT; |
255 | 255 |
static const double A4WIDTH; |
256 | 256 |
static const double A4BORDER; |
257 | 257 |
|
258 | 258 |
bool dontPrint; |
259 | 259 |
|
260 | 260 |
public: |
261 | 261 |
///Node shapes |
262 | 262 |
|
263 | 263 |
///Node shapes. |
264 | 264 |
/// |
265 | 265 |
enum NodeShapes { |
266 | 266 |
/// = 0 |
267 | 267 |
///\image html nodeshape_0.png |
268 | 268 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
269 | 269 |
CIRCLE=0, |
270 | 270 |
/// = 1 |
271 | 271 |
///\image html nodeshape_1.png |
272 | 272 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
273 | 273 |
/// |
274 | 274 |
SQUARE=1, |
275 | 275 |
/// = 2 |
276 | 276 |
///\image html nodeshape_2.png |
277 | 277 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
278 | 278 |
/// |
279 | 279 |
DIAMOND=2, |
280 | 280 |
/// = 3 |
281 | 281 |
///\image html nodeshape_3.png |
282 | 282 |
///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
283 | 283 |
/// |
284 | 284 |
MALE=3, |
285 | 285 |
/// = 4 |
286 | 286 |
///\image html nodeshape_4.png |
287 | 287 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
288 | 288 |
/// |
289 | 289 |
FEMALE=4 |
290 | 290 |
}; |
291 | 291 |
|
292 | 292 |
private: |
293 | 293 |
class arcLess { |
294 | 294 |
const Graph &g; |
295 | 295 |
public: |
296 | 296 |
arcLess(const Graph &_g) : g(_g) {} |
297 | 297 |
bool operator()(Arc a,Arc b) const |
298 | 298 |
{ |
299 | 299 |
Node ai=std::min(g.source(a),g.target(a)); |
300 | 300 |
Node aa=std::max(g.source(a),g.target(a)); |
301 | 301 |
Node bi=std::min(g.source(b),g.target(b)); |
302 | 302 |
Node ba=std::max(g.source(b),g.target(b)); |
303 | 303 |
return ai<bi || |
304 | 304 |
(ai==bi && (aa < ba || |
305 | 305 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
306 | 306 |
} |
307 | 307 |
}; |
308 | 308 |
bool isParallel(Arc e,Arc f) const |
309 | 309 |
{ |
310 | 310 |
return (g.source(e)==g.source(f)&& |
311 | 311 |
g.target(e)==g.target(f)) || |
312 | 312 |
(g.source(e)==g.target(f)&& |
313 | 313 |
g.target(e)==g.source(f)); |
314 | 314 |
} |
315 | 315 |
template<class TT> |
316 | 316 |
static std::string psOut(const dim2::Point<TT> &p) |
317 | 317 |
{ |
318 | 318 |
std::ostringstream os; |
319 | 319 |
os << p.x << ' ' << p.y; |
320 | 320 |
return os.str(); |
321 | 321 |
} |
322 | 322 |
static std::string psOut(const Color &c) |
323 | 323 |
{ |
324 | 324 |
std::ostringstream os; |
325 | 325 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
326 | 326 |
return os.str(); |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
public: |
330 | 330 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
331 | 331 |
|
332 | 332 |
template<class X> struct CoordsTraits : public T { |
333 | 333 |
typedef X CoordsMapType; |
334 | 334 |
const X &_coords; |
335 | 335 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
336 | 336 |
}; |
337 | 337 |
///Sets the map of the node coordinates |
338 | 338 |
|
339 | 339 |
///Sets the map of the node coordinates. |
340 | 340 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
341 | 341 |
///\ref dim2::Point "dim2::Point<int>" values. |
342 | 342 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
343 | 343 |
dontPrint=true; |
344 | 344 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
345 | 345 |
} |
346 | 346 |
template<class X> struct NodeSizesTraits : public T { |
347 | 347 |
const X &_nodeSizes; |
348 | 348 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
349 | 349 |
}; |
350 | 350 |
///Sets the map of the node sizes |
351 | 351 |
|
352 | 352 |
///Sets the map of the node sizes. |
353 | 353 |
///\param x must be a node map with \c double (or convertible) values. |
354 | 354 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
355 | 355 |
{ |
356 | 356 |
dontPrint=true; |
357 | 357 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
358 | 358 |
} |
359 | 359 |
template<class X> struct NodeShapesTraits : public T { |
360 | 360 |
const X &_nodeShapes; |
361 | 361 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
362 | 362 |
}; |
363 | 363 |
///Sets the map of the node shapes |
364 | 364 |
|
365 | 365 |
///Sets the map of the node shapes. |
366 | 366 |
///The available shape values |
367 | 367 |
///can be found in \ref NodeShapes "enum NodeShapes". |
368 | 368 |
///\param x must be a node map with \c int (or convertible) values. |
369 | 369 |
///\sa NodeShapes |
370 | 370 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
371 | 371 |
{ |
372 | 372 |
dontPrint=true; |
373 | 373 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
374 | 374 |
} |
375 | 375 |
template<class X> struct NodeTextsTraits : public T { |
376 | 376 |
const X &_nodeTexts; |
377 | 377 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
378 | 378 |
}; |
379 | 379 |
///Sets the text printed on the nodes |
380 | 380 |
|
381 | 381 |
///Sets the text printed on the nodes. |
382 | 382 |
///\param x must be a node map with type that can be pushed to a standard |
383 | 383 |
///\c ostream. |
384 | 384 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
385 | 385 |
{ |
386 | 386 |
dontPrint=true; |
387 | 387 |
_showNodeText=true; |
388 | 388 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
389 | 389 |
} |
390 | 390 |
template<class X> struct NodePsTextsTraits : public T { |
391 | 391 |
const X &_nodePsTexts; |
392 | 392 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
393 | 393 |
}; |
394 | 394 |
///Inserts a PostScript block to the nodes |
395 | 395 |
|
396 | 396 |
///With this command it is possible to insert a verbatim PostScript |
397 | 397 |
///block to the nodes. |
398 | 398 |
///The PS current point will be moved to the center of the node before |
399 | 399 |
///the PostScript block inserted. |
400 | 400 |
/// |
401 | 401 |
///Before and after the block a newline character is inserted so you |
402 | 402 |
///don't have to bother with the separators. |
403 | 403 |
/// |
404 | 404 |
///\param x must be a node map with type that can be pushed to a standard |
405 | 405 |
///\c ostream. |
406 | 406 |
/// |
407 | 407 |
///\sa nodePsTextsPreamble() |
408 | 408 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
409 | 409 |
{ |
410 | 410 |
dontPrint=true; |
411 | 411 |
_showNodePsText=true; |
412 | 412 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
413 | 413 |
} |
414 | 414 |
template<class X> struct ArcWidthsTraits : public T { |
415 | 415 |
const X &_arcWidths; |
416 | 416 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
417 | 417 |
}; |
418 | 418 |
///Sets the map of the arc widths |
419 | 419 |
|
420 | 420 |
///Sets the map of the arc widths. |
421 | 421 |
///\param x must be an arc map with \c double (or convertible) values. |
422 | 422 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
423 | 423 |
{ |
424 | 424 |
dontPrint=true; |
425 | 425 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
426 | 426 |
} |
427 | 427 |
|
428 | 428 |
template<class X> struct NodeColorsTraits : public T { |
429 | 429 |
const X &_nodeColors; |
430 | 430 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
431 | 431 |
}; |
432 | 432 |
///Sets the map of the node colors |
433 | 433 |
|
434 | 434 |
///Sets the map of the node colors. |
435 | 435 |
///\param x must be a node map with \ref Color values. |
436 | 436 |
/// |
437 | 437 |
///\sa Palette |
438 | 438 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
439 | 439 |
nodeColors(const X &x) |
440 | 440 |
{ |
441 | 441 |
dontPrint=true; |
442 | 442 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
443 | 443 |
} |
444 | 444 |
template<class X> struct NodeTextColorsTraits : public T { |
445 | 445 |
const X &_nodeTextColors; |
446 | 446 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
447 | 447 |
}; |
448 | 448 |
///Sets the map of the node text colors |
449 | 449 |
|
450 | 450 |
///Sets the map of the node text colors. |
451 | 451 |
///\param x must be a node map with \ref Color values. |
452 | 452 |
/// |
453 | 453 |
///\sa Palette |
454 | 454 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
455 | 455 |
nodeTextColors(const X &x) |
456 | 456 |
{ |
457 | 457 |
dontPrint=true; |
458 | 458 |
_nodeTextColorType=CUST_COL; |
459 | 459 |
return GraphToEps<NodeTextColorsTraits<X> > |
460 | 460 |
(NodeTextColorsTraits<X>(*this,x)); |
461 | 461 |
} |
462 | 462 |
template<class X> struct ArcColorsTraits : public T { |
463 | 463 |
const X &_arcColors; |
464 | 464 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
465 | 465 |
}; |
466 | 466 |
///Sets the map of the arc colors |
467 | 467 |
|
468 | 468 |
///Sets the map of the arc colors. |
469 | 469 |
///\param x must be an arc map with \ref Color values. |
470 | 470 |
/// |
471 | 471 |
///\sa Palette |
472 | 472 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
473 | 473 |
arcColors(const X &x) |
474 | 474 |
{ |
475 | 475 |
dontPrint=true; |
476 | 476 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
477 | 477 |
} |
478 | 478 |
///Sets a global scale factor for node sizes |
479 | 479 |
|
480 | 480 |
///Sets a global scale factor for node sizes. |
481 | 481 |
/// |
482 | 482 |
/// If nodeSizes() is not given, this function simply sets the node |
483 | 483 |
/// sizes to \c d. If nodeSizes() is given, but |
484 | 484 |
/// autoNodeScale() is not, then the node size given by |
485 | 485 |
/// nodeSizes() will be multiplied by the value \c d. |
486 | 486 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
487 | 487 |
/// node sizes will be scaled in such a way that the greatest size will be |
488 | 488 |
/// equal to \c d. |
489 | 489 |
/// \sa nodeSizes() |
490 | 490 |
/// \sa autoNodeScale() |
491 | 491 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
492 | 492 |
///Turns on/off the automatic node size scaling. |
493 | 493 |
|
494 | 494 |
///Turns on/off the automatic node size scaling. |
495 | 495 |
/// |
496 | 496 |
///\sa nodeScale() |
497 | 497 |
/// |
498 | 498 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
499 | 499 |
_autoNodeScale=b;return *this; |
500 | 500 |
} |
501 | 501 |
|
502 | 502 |
///Turns on/off the absolutematic node size scaling. |
503 | 503 |
|
504 | 504 |
///Turns on/off the absolutematic node size scaling. |
505 | 505 |
/// |
506 | 506 |
///\sa nodeScale() |
507 | 507 |
/// |
508 | 508 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
509 | 509 |
_absoluteNodeSizes=b;return *this; |
510 | 510 |
} |
511 | 511 |
|
512 | 512 |
///Negates the Y coordinates. |
513 | 513 |
GraphToEps<T> &negateY(bool b=true) { |
514 | 514 |
_negY=b;return *this; |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
///Turn on/off pre-scaling |
518 | 518 |
|
519 | 519 |
///By default graphToEps() rescales the whole image in order to avoid |
520 | 520 |
///very big or very small bounding boxes. |
521 | 521 |
/// |
522 | 522 |
///This (p)rescaling can be turned off with this function. |
523 | 523 |
/// |
524 | 524 |
GraphToEps<T> &preScale(bool b=true) { |
525 | 525 |
_preScale=b;return *this; |
526 | 526 |
} |
527 | 527 |
|
528 | 528 |
///Sets a global scale factor for arc widths |
529 | 529 |
|
530 | 530 |
/// Sets a global scale factor for arc widths. |
531 | 531 |
/// |
532 | 532 |
/// If arcWidths() is not given, this function simply sets the arc |
533 | 533 |
/// widths to \c d. If arcWidths() is given, but |
534 | 534 |
/// autoArcWidthScale() is not, then the arc withs given by |
535 | 535 |
/// arcWidths() will be multiplied by the value \c d. |
536 | 536 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
537 | 537 |
/// arc withs will be scaled in such a way that the greatest width will be |
538 | 538 |
/// equal to \c d. |
539 | 539 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
540 | 540 |
///Turns on/off the automatic arc width scaling. |
541 | 541 |
|
542 | 542 |
///Turns on/off the automatic arc width scaling. |
543 | 543 |
/// |
544 | 544 |
///\sa arcWidthScale() |
545 | 545 |
/// |
546 | 546 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
547 | 547 |
_autoArcWidthScale=b;return *this; |
548 | 548 |
} |
549 | 549 |
///Turns on/off the absolutematic arc width scaling. |
550 | 550 |
|
551 | 551 |
///Turns on/off the absolutematic arc width scaling. |
552 | 552 |
/// |
553 | 553 |
///\sa arcWidthScale() |
554 | 554 |
/// |
555 | 555 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
556 | 556 |
_absoluteArcWidths=b;return *this; |
557 | 557 |
} |
558 | 558 |
///Sets a global scale factor for the whole picture |
559 | 559 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
560 | 560 |
///Sets the width of the border around the picture |
561 | 561 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
562 | 562 |
///Sets the width of the border around the picture |
563 | 563 |
GraphToEps<T> &border(double x, double y) { |
564 | 564 |
_xBorder=x;_yBorder=y;return *this; |
565 | 565 |
} |
566 | 566 |
///Sets whether to draw arrows |
567 | 567 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
568 | 568 |
///Sets the length of the arrowheads |
569 | 569 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
570 | 570 |
///Sets the width of the arrowheads |
571 | 571 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
572 | 572 |
|
573 | 573 |
///Scales the drawing to fit to A4 page |
574 | 574 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
575 | 575 |
|
576 | 576 |
///Enables parallel arcs |
577 | 577 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
578 | 578 |
|
579 | 579 |
///Sets the distance between parallel arcs |
580 | 580 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
581 | 581 |
|
582 | 582 |
///Hides the arcs |
583 | 583 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
584 | 584 |
///Hides the nodes |
585 | 585 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
586 | 586 |
|
587 | 587 |
///Sets the size of the node texts |
588 | 588 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
589 | 589 |
|
590 | 590 |
///Sets the color of the node texts to be different from the node color |
591 | 591 |
|
592 | 592 |
///Sets the color of the node texts to be as different from the node color |
593 | 593 |
///as it is possible. |
594 | 594 |
GraphToEps<T> &distantColorNodeTexts() |
595 | 595 |
{_nodeTextColorType=DIST_COL;return *this;} |
596 | 596 |
///Sets the color of the node texts to be black or white and always visible. |
597 | 597 |
|
598 | 598 |
///Sets the color of the node texts to be black or white according to |
599 | 599 |
///which is more different from the node color. |
600 | 600 |
GraphToEps<T> &distantBWNodeTexts() |
601 | 601 |
{_nodeTextColorType=DIST_BW;return *this;} |
602 | 602 |
|
603 | 603 |
///Gives a preamble block for node Postscript block. |
604 | 604 |
|
605 | 605 |
///Gives a preamble block for node Postscript block. |
606 | 606 |
/// |
607 | 607 |
///\sa nodePsTexts() |
608 | 608 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
609 | 609 |
_nodePsTextsPreamble=str ;return *this; |
610 | 610 |
} |
611 | 611 |
///Sets whether the graph is undirected |
612 | 612 |
|
613 | 613 |
///Sets whether the graph is undirected. |
614 | 614 |
/// |
615 | 615 |
///This setting is the default for undirected graphs. |
616 | 616 |
/// |
617 | 617 |
///\sa directed() |
618 | 618 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
619 | 619 |
|
620 | 620 |
///Sets whether the graph is directed |
621 | 621 |
|
622 | 622 |
///Sets whether the graph is directed. |
623 | 623 |
///Use it to show the edges as a pair of directed ones. |
624 | 624 |
/// |
625 | 625 |
///This setting is the default for digraphs. |
626 | 626 |
/// |
627 | 627 |
///\sa undirected() |
628 | 628 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
629 | 629 |
|
630 | 630 |
///Sets the title. |
631 | 631 |
|
632 | 632 |
///Sets the title of the generated image, |
633 | 633 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
634 | 634 |
///the EPS file. |
635 | 635 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
636 | 636 |
///Sets the copyright statement. |
637 | 637 |
|
638 | 638 |
///Sets the copyright statement of the generated image, |
639 | 639 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
640 | 640 |
///the EPS file. |
641 | 641 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
642 | 642 |
|
643 | 643 |
protected: |
644 | 644 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
645 | 645 |
{ |
646 | 646 |
switch(t) { |
647 | 647 |
case CIRCLE: |
648 | 648 |
case MALE: |
649 | 649 |
case FEMALE: |
650 | 650 |
return p.normSquare()<=r*r; |
651 | 651 |
case SQUARE: |
652 | 652 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
653 | 653 |
case DIAMOND: |
654 | 654 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
655 | 655 |
} |
656 | 656 |
return false; |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
public: |
660 | 660 |
~GraphToEps() { } |
661 | 661 |
|
662 | 662 |
///Draws the graph. |
663 | 663 |
|
664 | 664 |
///Like other functions using |
665 | 665 |
///\ref named-templ-func-param "named template parameters", |
666 | 666 |
///this function calls the algorithm itself, i.e. in this case |
667 | 667 |
///it draws the graph. |
668 | 668 |
void run() { |
669 | 669 |
//\todo better 'epsilon' would be nice here. |
670 | 670 |
const double EPSILON=1e-9; |
671 | 671 |
if(dontPrint) return; |
672 | 672 |
|
673 | 673 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
674 | 674 |
mycoords(_coords,_negY); |
675 | 675 |
|
676 | 676 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
677 | 677 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
678 | 678 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
679 | 679 |
os << "%%Creator: LEMON, graphToEps()\n"; |
680 | 680 |
|
681 | 681 |
{ |
682 | 682 |
#ifndef WIN32 |
683 | 683 |
timeval tv; |
684 | 684 |
gettimeofday(&tv, 0); |
685 | 685 |
|
686 | 686 |
char cbuf[26]; |
687 | 687 |
ctime_r(&tv.tv_sec,cbuf); |
688 | 688 |
os << "%%CreationDate: " << cbuf; |
689 | 689 |
#else |
690 | 690 |
SYSTEMTIME time; |
691 | 691 |
char buf1[11], buf2[9], buf3[5]; |
692 | 692 |
|
693 | 693 |
GetSystemTime(&time); |
694 | 694 |
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
695 | 695 |
"ddd MMM dd", buf1, 11) && |
696 | 696 |
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, |
697 | 697 |
"HH':'mm':'ss", buf2, 9) && |
698 | 698 |
GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
699 | 699 |
"yyyy", buf3, 5)) { |
700 | 700 |
os << "%%CreationDate: " << buf1 << ' ' |
701 | 701 |
<< buf2 << ' ' << buf3 << std::endl; |
702 | 702 |
} |
703 | 703 |
#endif |
704 | 704 |
} |
705 | 705 |
|
706 | 706 |
if (_autoArcWidthScale) { |
707 | 707 |
double max_w=0; |
708 | 708 |
for(ArcIt e(g);e!=INVALID;++e) |
709 | 709 |
max_w=std::max(double(_arcWidths[e]),max_w); |
710 | 710 |
//\todo better 'epsilon' would be nice here. |
711 | 711 |
if(max_w>EPSILON) { |
712 | 712 |
_arcWidthScale/=max_w; |
713 | 713 |
} |
714 | 714 |
} |
715 | 715 |
|
716 | 716 |
if (_autoNodeScale) { |
717 | 717 |
double max_s=0; |
718 | 718 |
for(NodeIt n(g);n!=INVALID;++n) |
719 | 719 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
720 | 720 |
//\todo better 'epsilon' would be nice here. |
721 | 721 |
if(max_s>EPSILON) { |
722 | 722 |
_nodeScale/=max_s; |
723 | 723 |
} |
724 | 724 |
} |
725 | 725 |
|
726 | 726 |
double diag_len = 1; |
727 | 727 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
728 |
dim2:: |
|
728 |
dim2::Box<double> bb; |
|
729 | 729 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
730 | 730 |
if (bb.empty()) { |
731 |
bb = dim2:: |
|
731 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
|
732 | 732 |
} |
733 | 733 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
734 | 734 |
if(diag_len<EPSILON) diag_len = 1; |
735 | 735 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
736 | 736 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
737 | 737 |
} |
738 | 738 |
|
739 |
dim2:: |
|
739 |
dim2::Box<double> bb; |
|
740 | 740 |
for(NodeIt n(g);n!=INVALID;++n) { |
741 | 741 |
double ns=_nodeSizes[n]*_nodeScale; |
742 | 742 |
dim2::Point<double> p(ns,ns); |
743 | 743 |
switch(_nodeShapes[n]) { |
744 | 744 |
case CIRCLE: |
745 | 745 |
case SQUARE: |
746 | 746 |
case DIAMOND: |
747 | 747 |
bb.add(p+mycoords[n]); |
748 | 748 |
bb.add(-p+mycoords[n]); |
749 | 749 |
break; |
750 | 750 |
case MALE: |
751 | 751 |
bb.add(-p+mycoords[n]); |
752 | 752 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
753 | 753 |
break; |
754 | 754 |
case FEMALE: |
755 | 755 |
bb.add(p+mycoords[n]); |
756 | 756 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
757 | 757 |
break; |
758 | 758 |
} |
759 | 759 |
} |
760 | 760 |
if (bb.empty()) { |
761 |
bb = dim2:: |
|
761 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
|
762 | 762 |
} |
763 | 763 |
|
764 | 764 |
if(_scaleToA4) |
765 | 765 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
766 | 766 |
else { |
767 | 767 |
if(_preScale) { |
768 | 768 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
769 | 769 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
770 | 770 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
771 | 771 |
} |
772 | 772 |
|
773 | 773 |
os << "%%BoundingBox: " |
774 | 774 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
775 | 775 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
776 | 776 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
777 | 777 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
os << "%%EndComments\n"; |
781 | 781 |
|
782 | 782 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
783 | 783 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
784 | 784 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
785 | 785 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }" |
786 | 786 |
<< " bind def\n"; |
787 | 787 |
//x y r |
788 | 788 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }" |
789 | 789 |
<< " bind def\n"; |
790 | 790 |
//x y r |
791 | 791 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
792 | 792 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
793 | 793 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
794 | 794 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
795 | 795 |
<< " closepath pop pop pop} bind def\n"; |
796 | 796 |
//x y r |
797 | 797 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
798 | 798 |
<< " 2 index 2 index 2 index add lineto\n" |
799 | 799 |
<< " 2 index 1 index sub 2 index lineto\n" |
800 | 800 |
<< " 2 index 2 index 2 index sub lineto\n" |
801 | 801 |
<< " closepath pop pop pop} bind def\n"; |
802 | 802 |
// x y r cr cg cb |
803 | 803 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
804 | 804 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
805 | 805 |
<< " } bind def\n"; |
806 | 806 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
807 | 807 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
808 | 808 |
<< " } bind def\n"; |
809 | 809 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
810 | 810 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
811 | 811 |
<< " } bind def\n"; |
812 | 812 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
813 | 813 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
814 | 814 |
<< " 1.5 mul mul setlinewidth\n" |
815 | 815 |
<< " newpath 5 index 5 index moveto " |
816 | 816 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
817 | 817 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
818 | 818 |
<< " moveto\n" |
819 | 819 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
820 | 820 |
<< "stroke\n" |
821 | 821 |
<< " 5 index 5 index 5 index c fill\n" |
822 | 822 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
823 | 823 |
<< " } bind def\n"; |
824 | 824 |
os << "/nmale {\n" |
825 | 825 |
<< " 0 0 0 setrgbcolor 3 index " |
826 | 826 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
827 | 827 |
<<" 1.5 mul mul setlinewidth\n" |
828 | 828 |
<< " newpath 5 index 5 index moveto\n" |
829 | 829 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
830 | 830 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
831 | 831 |
<< " 1 index 1 index lineto\n" |
832 | 832 |
<< " 1 index 1 index 7 index sub moveto\n" |
833 | 833 |
<< " 1 index 1 index lineto\n" |
834 | 834 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
835 | 835 |
<< " lineto\n" |
836 | 836 |
<< " stroke\n" |
837 | 837 |
<< " 5 index 5 index 5 index c fill\n" |
838 | 838 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
839 | 839 |
<< " } bind def\n"; |
840 | 840 |
|
841 | 841 |
|
842 | 842 |
os << "/arrl " << _arrowLength << " def\n"; |
843 | 843 |
os << "/arrw " << _arrowWidth << " def\n"; |
844 | 844 |
// l dx_norm dy_norm |
845 | 845 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
846 | 846 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
847 | 847 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
848 | 848 |
<< "exch def\n" |
849 | 849 |
<< " /w exch def /len exch def\n" |
850 | 850 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
851 | 851 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
852 | 852 |
<< " len w sub arrl sub dx dy lrl\n" |
853 | 853 |
<< " arrw dy dx neg lrl\n" |
854 | 854 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
855 | 855 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
856 | 856 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
857 | 857 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
858 | 858 |
<< " arrw dy dx neg lrl\n" |
859 | 859 |
<< " len w sub arrl sub neg dx dy lrl\n" |
860 | 860 |
<< " closepath fill } bind def\n"; |
861 | 861 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
862 | 862 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
863 | 863 |
|
864 | 864 |
os << "\ngsave\n"; |
865 | 865 |
if(_scaleToA4) |
866 | 866 |
if(bb.height()>bb.width()) { |
867 | 867 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
868 | 868 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
869 | 869 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
870 | 870 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
871 | 871 |
<< " translate\n" |
872 | 872 |
<< sc << " dup scale\n" |
873 | 873 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
874 | 874 |
} |
875 | 875 |
else { |
876 | 876 |
//\todo Verify centering |
877 | 877 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
878 | 878 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
879 | 879 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
880 | 880 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
881 | 881 |
<< " translate\n" |
882 | 882 |
<< sc << " dup scale\n90 rotate\n" |
883 | 883 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
884 | 884 |
} |
885 | 885 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
886 | 886 |
|
887 | 887 |
if(_showArcs) { |
888 | 888 |
os << "%Arcs:\ngsave\n"; |
889 | 889 |
if(_enableParallel) { |
890 | 890 |
std::vector<Arc> el; |
891 | 891 |
for(ArcIt e(g);e!=INVALID;++e) |
892 | 892 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
893 | 893 |
&&g.source(e)!=g.target(e)) |
894 | 894 |
el.push_back(e); |
895 | 895 |
std::sort(el.begin(),el.end(),arcLess(g)); |
896 | 896 |
|
897 | 897 |
typename std::vector<Arc>::iterator j; |
898 | 898 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
899 | 899 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
900 | 900 |
|
901 | 901 |
double sw=0; |
902 | 902 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
903 | 903 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
904 | 904 |
sw-=_parArcDist; |
905 | 905 |
sw/=-2.0; |
906 | 906 |
dim2::Point<double> |
907 | 907 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
908 | 908 |
double l=std::sqrt(dvec.normSquare()); |
909 | 909 |
//\todo better 'epsilon' would be nice here. |
910 | 910 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
911 | 911 |
dim2::Point<double> m; |
912 | 912 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
913 | 913 |
// mycoords[g.source(*i)])/2.0; |
914 | 914 |
|
915 | 915 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
916 | 916 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
917 | 917 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
918 | 918 |
|
919 | 919 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
920 | 920 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
921 | 921 |
|
922 | 922 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
923 | 923 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
924 | 924 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
925 | 925 |
if(_drawArrows) { |
926 | 926 |
int node_shape; |
927 | 927 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
928 | 928 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
929 | 929 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
930 | 930 |
node_shape=_nodeShapes[g.target(*e)]; |
931 | 931 |
dim2::Bezier3 bez(s,mm,mm,t); |
932 | 932 |
double t1=0,t2=1; |
933 | 933 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
934 | 934 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
935 | 935 |
else t1=(t1+t2)/2; |
936 | 936 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
937 | 937 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
938 | 938 |
rn*=rn; |
939 | 939 |
t2=(t1+t2)/2;t1=0; |
940 | 940 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
941 | 941 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
942 | 942 |
else t2=(t1+t2)/2; |
943 | 943 |
dim2::Point<double> linend=bez((t1+t2)/2); |
944 | 944 |
bez=bez.before((t1+t2)/2); |
945 | 945 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
946 | 946 |
// node_shape=_nodeShapes[g.source(*e)]; |
947 | 947 |
// t1=0;t2=1; |
948 | 948 |
// for(int i=0;i<INTERPOL_PREC;++i) |
949 | 949 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
950 | 950 |
// t1=(t1+t2)/2; |
951 | 951 |
// else t2=(t1+t2)/2; |
952 | 952 |
// bez=bez.after((t1+t2)/2); |
953 | 953 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
954 | 954 |
<< _arcColors[*e].red() << ' ' |
955 | 955 |
<< _arcColors[*e].green() << ' ' |
956 | 956 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
957 | 957 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
958 | 958 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
959 | 959 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
960 | 960 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
961 | 961 |
dim2::Point<double> dd(rot90(linend-apoint)); |
962 | 962 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
963 | 963 |
std::sqrt(dd.normSquare()); |
964 | 964 |
os << "newpath " << psOut(apoint) << " moveto " |
965 | 965 |
<< psOut(linend+dd) << " lineto " |
966 | 966 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
967 | 967 |
} |
968 | 968 |
else { |
969 | 969 |
os << mycoords[g.source(*e)].x << ' ' |
970 | 970 |
<< mycoords[g.source(*e)].y << ' ' |
971 | 971 |
<< mm.x << ' ' << mm.y << ' ' |
972 | 972 |
<< mycoords[g.target(*e)].x << ' ' |
973 | 973 |
<< mycoords[g.target(*e)].y << ' ' |
974 | 974 |
<< _arcColors[*e].red() << ' ' |
975 | 975 |
<< _arcColors[*e].green() << ' ' |
976 | 976 |
<< _arcColors[*e].blue() << ' ' |
977 | 977 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
978 | 978 |
} |
979 | 979 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
980 | 980 |
} |
981 | 981 |
} |
982 | 982 |
} |
983 | 983 |
else for(ArcIt e(g);e!=INVALID;++e) |
984 | 984 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
985 | 985 |
&&g.source(e)!=g.target(e)) { |
986 | 986 |
if(_drawArrows) { |
987 | 987 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
988 | 988 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
989 | 989 |
int node_shape=_nodeShapes[g.target(e)]; |
990 | 990 |
double t1=0,t2=1; |
991 | 991 |
for(int i=0;i<INTERPOL_PREC;++i) |
992 | 992 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
993 | 993 |
else t2=(t1+t2)/2; |
994 | 994 |
double l=std::sqrt(d.normSquare()); |
995 | 995 |
d/=l; |
996 | 996 |
|
997 | 997 |
os << l*(1-(t1+t2)/2) << ' ' |
998 | 998 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
999 | 999 |
<< d.x << ' ' << d.y << ' ' |
1000 | 1000 |
<< mycoords[g.source(e)].x << ' ' |
1001 | 1001 |
<< mycoords[g.source(e)].y << ' ' |
1002 | 1002 |
<< _arcColors[e].red() << ' ' |
1003 | 1003 |
<< _arcColors[e].green() << ' ' |
1004 | 1004 |
<< _arcColors[e].blue() << " arr\n"; |
1005 | 1005 |
} |
1006 | 1006 |
else os << mycoords[g.source(e)].x << ' ' |
1007 | 1007 |
<< mycoords[g.source(e)].y << ' ' |
1008 | 1008 |
<< mycoords[g.target(e)].x << ' ' |
1009 | 1009 |
<< mycoords[g.target(e)].y << ' ' |
1010 | 1010 |
<< _arcColors[e].red() << ' ' |
1011 | 1011 |
<< _arcColors[e].green() << ' ' |
1012 | 1012 |
<< _arcColors[e].blue() << ' ' |
1013 | 1013 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
1014 | 1014 |
} |
1015 | 1015 |
os << "grestore\n"; |
1016 | 1016 |
} |
1017 | 1017 |
if(_showNodes) { |
1018 | 1018 |
os << "%Nodes:\ngsave\n"; |
1019 | 1019 |
for(NodeIt n(g);n!=INVALID;++n) { |
1020 | 1020 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1021 | 1021 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1022 | 1022 |
<< _nodeColors[n].red() << ' ' |
1023 | 1023 |
<< _nodeColors[n].green() << ' ' |
1024 | 1024 |
<< _nodeColors[n].blue() << ' '; |
1025 | 1025 |
switch(_nodeShapes[n]) { |
1026 | 1026 |
case CIRCLE: |
1027 | 1027 |
os<< "nc";break; |
1028 | 1028 |
case SQUARE: |
1029 | 1029 |
os<< "nsq";break; |
1030 | 1030 |
case DIAMOND: |
1031 | 1031 |
os<< "ndi";break; |
1032 | 1032 |
case MALE: |
1033 | 1033 |
os<< "nmale";break; |
1034 | 1034 |
case FEMALE: |
1035 | 1035 |
os<< "nfemale";break; |
1036 | 1036 |
} |
1037 | 1037 |
os<<'\n'; |
1038 | 1038 |
} |
1039 | 1039 |
os << "grestore\n"; |
1040 | 1040 |
} |
1041 | 1041 |
if(_showNodeText) { |
1042 | 1042 |
os << "%Node texts:\ngsave\n"; |
1043 | 1043 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1044 | 1044 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1045 | 1045 |
for(NodeIt n(g);n!=INVALID;++n) { |
1046 | 1046 |
switch(_nodeTextColorType) { |
1047 | 1047 |
case DIST_COL: |
1048 | 1048 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1049 | 1049 |
break; |
1050 | 1050 |
case DIST_BW: |
1051 | 1051 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1052 | 1052 |
break; |
1053 | 1053 |
case CUST_COL: |
1054 | 1054 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1055 | 1055 |
break; |
1056 | 1056 |
default: |
1057 | 1057 |
os << "0 0 0 setrgbcolor\n"; |
1058 | 1058 |
} |
1059 | 1059 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1060 | 1060 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1061 | 1061 |
} |
1062 | 1062 |
os << "grestore\n"; |
1063 | 1063 |
} |
1064 | 1064 |
if(_showNodePsText) { |
1065 | 1065 |
os << "%Node PS blocks:\ngsave\n"; |
1066 | 1066 |
for(NodeIt n(g);n!=INVALID;++n) |
1067 | 1067 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1068 | 1068 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1069 | 1069 |
os << "grestore\n"; |
1070 | 1070 |
} |
1071 | 1071 |
|
1072 | 1072 |
os << "grestore\nshowpage\n"; |
1073 | 1073 |
|
1074 | 1074 |
//CleanUp: |
1075 | 1075 |
if(_pleaseRemoveOsStream) {delete &os;} |
1076 | 1076 |
} |
1077 | 1077 |
|
1078 | 1078 |
///\name Aliases |
1079 | 1079 |
///These are just some aliases to other parameter setting functions. |
1080 | 1080 |
|
1081 | 1081 |
///@{ |
1082 | 1082 |
|
1083 | 1083 |
///An alias for arcWidths() |
1084 | 1084 |
template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
1085 | 1085 |
{ |
1086 | 1086 |
return arcWidths(x); |
1087 | 1087 |
} |
1088 | 1088 |
|
1089 | 1089 |
///An alias for arcColors() |
1090 | 1090 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
1091 | 1091 |
edgeColors(const X &x) |
1092 | 1092 |
{ |
1093 | 1093 |
return arcColors(x); |
1094 | 1094 |
} |
1095 | 1095 |
|
1096 | 1096 |
///An alias for arcWidthScale() |
1097 | 1097 |
GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
1098 | 1098 |
|
1099 | 1099 |
///An alias for autoArcWidthScale() |
1100 | 1100 |
GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
1101 | 1101 |
{ |
1102 | 1102 |
return autoArcWidthScale(b); |
1103 | 1103 |
} |
1104 | 1104 |
|
1105 | 1105 |
///An alias for absoluteArcWidths() |
1106 | 1106 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1107 | 1107 |
{ |
1108 | 1108 |
return absoluteArcWidths(b); |
1109 | 1109 |
} |
1110 | 1110 |
|
1111 | 1111 |
///An alias for parArcDist() |
1112 | 1112 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1113 | 1113 |
|
1114 | 1114 |
///An alias for hideArcs() |
1115 | 1115 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1116 | 1116 |
|
1117 | 1117 |
///@} |
1118 | 1118 |
}; |
1119 | 1119 |
|
1120 | 1120 |
template<class T> |
1121 | 1121 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1122 | 1122 |
template<class T> |
1123 | 1123 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1124 | 1124 |
template<class T> |
1125 | 1125 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1126 | 1126 |
template<class T> |
1127 | 1127 |
const double GraphToEps<T>::A4BORDER = 15; |
1128 | 1128 |
|
1129 | 1129 |
|
1130 | 1130 |
///Generates an EPS file from a graph |
1131 | 1131 |
|
1132 | 1132 |
///\ingroup eps_io |
1133 | 1133 |
///Generates an EPS file from a graph. |
1134 | 1134 |
///\param g Reference to the graph to be printed. |
1135 | 1135 |
///\param os Reference to the output stream. |
1136 | 1136 |
///By default it is <tt>std::cout</tt>. |
1137 | 1137 |
/// |
1138 | 1138 |
///This function also has a lot of |
1139 | 1139 |
///\ref named-templ-func-param "named parameters", |
1140 | 1140 |
///they are declared as the members of class \ref GraphToEps. The following |
1141 | 1141 |
///example shows how to use these parameters. |
1142 | 1142 |
///\code |
1143 | 1143 |
/// graphToEps(g,os).scale(10).coords(coords) |
1144 | 1144 |
/// .nodeScale(2).nodeSizes(sizes) |
1145 | 1145 |
/// .arcWidthScale(.4).run(); |
1146 | 1146 |
///\endcode |
1147 | 1147 |
/// |
1148 | 1148 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
1149 | 1149 |
/// |
1150 | 1150 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1151 | 1151 |
///to the end of the parameter list. |
1152 | 1152 |
///\sa GraphToEps |
1153 | 1153 |
///\sa graphToEps(G &g, const char *file_name) |
1154 | 1154 |
template<class G> |
1155 | 1155 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1156 | 1156 |
graphToEps(G &g, std::ostream& os=std::cout) |
1157 | 1157 |
{ |
1158 | 1158 |
return |
1159 | 1159 |
GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
1160 | 1160 |
} |
1161 | 1161 |
|
1162 | 1162 |
///Generates an EPS file from a graph |
1163 | 1163 |
|
1164 | 1164 |
///\ingroup eps_io |
1165 | 1165 |
///This function does the same as |
1166 | 1166 |
///\ref graphToEps(G &g,std::ostream& os) |
1167 | 1167 |
///but it writes its output into the file \c file_name |
1168 | 1168 |
///instead of a stream. |
1169 | 1169 |
///\sa graphToEps(G &g, std::ostream& os) |
1170 | 1170 |
template<class G> |
1171 | 1171 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1172 | 1172 |
graphToEps(G &g,const char *file_name) |
1173 | 1173 |
{ |
1174 | 1174 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1175 | 1175 |
(DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true)); |
1176 | 1176 |
} |
1177 | 1177 |
|
1178 | 1178 |
///Generates an EPS file from a graph |
1179 | 1179 |
|
1180 | 1180 |
///\ingroup eps_io |
1181 | 1181 |
///This function does the same as |
1182 | 1182 |
///\ref graphToEps(G &g,std::ostream& os) |
1183 | 1183 |
///but it writes its output into the file \c file_name |
1184 | 1184 |
///instead of a stream. |
1185 | 1185 |
///\sa graphToEps(G &g, std::ostream& os) |
1186 | 1186 |
template<class G> |
1187 | 1187 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1188 | 1188 |
graphToEps(G &g,const std::string& file_name) |
1189 | 1189 |
{ |
1190 | 1190 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1191 | 1191 |
(DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true)); |
1192 | 1192 |
} |
1193 | 1193 |
|
1194 | 1194 |
} //END OF NAMESPACE LEMON |
1195 | 1195 |
|
1196 | 1196 |
#endif // LEMON_GRAPH_TO_EPS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/dim2.h> |
20 | 20 |
#include <iostream> |
21 | 21 |
#include "test_tools.h" |
22 | 22 |
|
23 | 23 |
using namespace std; |
24 | 24 |
using namespace lemon; |
25 | 25 |
|
26 | 26 |
int main() |
27 | 27 |
{ |
28 | 28 |
typedef dim2::Point<int> Point; |
29 | 29 |
|
30 | 30 |
Point p; |
31 | 31 |
check(p.size()==2, "Wrong dim2::Point initialization."); |
32 | 32 |
|
33 | 33 |
Point a(1,2); |
34 | 34 |
Point b(3,4); |
35 | 35 |
check(a[0]==1 && a[1]==2, "Wrong dim2::Point initialization."); |
36 | 36 |
|
37 | 37 |
p = a+b; |
38 | 38 |
check(p.x==4 && p.y==6, "Wrong dim2::Point addition."); |
39 | 39 |
|
40 | 40 |
p = a-b; |
41 | 41 |
check(p.x==-2 && p.y==-2, "Wrong dim2::Point subtraction."); |
42 | 42 |
|
43 | 43 |
check(a.normSquare()==5,"Wrong dim2::Point norm calculation."); |
44 | 44 |
check(a*b==11, "Wrong dim2::Point scalar product."); |
45 | 45 |
|
46 | 46 |
int l=2; |
47 | 47 |
p = a*l; |
48 | 48 |
check(p.x==2 && p.y==4, "Wrong dim2::Point multiplication by a scalar."); |
49 | 49 |
|
50 | 50 |
p = b/l; |
51 | 51 |
check(p.x==1 && p.y==2, "Wrong dim2::Point division by a scalar."); |
52 | 52 |
|
53 |
typedef dim2::BoundingBox<int> BB; |
|
54 |
BB box1; |
|
55 |
|
|
53 |
typedef dim2::Box<int> Box; |
|
54 |
Box box1; |
|
55 |
check(box1.empty(), "Wrong empty() in dim2::Box."); |
|
56 | 56 |
|
57 | 57 |
box1.add(a); |
58 |
check(!box1.empty(), "Wrong empty() in dim2:: |
|
58 |
check(!box1.empty(), "Wrong empty() in dim2::Box."); |
|
59 | 59 |
box1.add(b); |
60 | 60 |
|
61 | 61 |
check(box1.left()==1 && box1.bottom()==2 && |
62 | 62 |
box1.right()==3 && box1.top()==4, |
63 |
"Wrong addition of points to dim2:: |
|
63 |
"Wrong addition of points to dim2::Box."); |
|
64 | 64 |
|
65 |
check(box1.inside(Point(2,3)), "Wrong inside() in dim2::BoundingBox."); |
|
66 |
check(box1.inside(Point(1,3)), "Wrong inside() in dim2::BoundingBox."); |
|
67 |
check( |
|
65 |
check(box1.inside(Point(2,3)), "Wrong inside() in dim2::Box."); |
|
66 |
check(box1.inside(Point(1,3)), "Wrong inside() in dim2::Box."); |
|
67 |
check(!box1.inside(Point(0,3)), "Wrong inside() in dim2::Box."); |
|
68 | 68 |
|
69 |
BB box2(Point(2,2)); |
|
70 |
check(!box2.empty(), "Wrong empty() in dim2::BoundingBox."); |
|
71 |
|
|
69 |
Box box2(Point(2,2)); |
|
70 |
check(!box2.empty(), "Wrong empty() in dim2::Box."); |
|
71 |
|
|
72 | 72 |
box2.bottomLeft(Point(2,0)); |
73 | 73 |
box2.topRight(Point(5,3)); |
74 |
|
|
74 |
Box box3 = box1 & box2; |
|
75 | 75 |
check(!box3.empty() && |
76 |
box3.left()==2 && box3.bottom()==2 && |
|
76 |
box3.left()==2 && box3.bottom()==2 && |
|
77 | 77 |
box3.right()==3 && box3.top()==3, |
78 |
"Wrong intersection of two dim2::BoundingBox objects."); |
|
79 |
|
|
78 |
"Wrong intersection of two dim2::Box objects."); |
|
79 |
|
|
80 | 80 |
box1.add(box2); |
81 | 81 |
check(!box1.empty() && |
82 | 82 |
box1.left()==1 && box1.bottom()==0 && |
83 | 83 |
box1.right()==5 && box1.top()==4, |
84 |
"Wrong addition of two dim2:: |
|
84 |
"Wrong addition of two dim2::Box objects."); |
|
85 | 85 |
|
86 | 86 |
return 0; |
87 | 87 |
} |
0 comments (0 inline)