gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Wrong member variable settings bug fix. (Ticket #95)
0 2 0
default
2 files changed with 4 insertions and 4 deletions:
↑ Collapse diff ↑
Ignore white space 1024 line context
... ...
@@ -507,1046 +507,1046 @@
507 507
	  _reached->set(m,true);
508 508
	  _pred->set(m,e);
509 509
	  _dist->set(m,_curr_dist);
510 510
          reach = reach || (target == m);
511 511
	}
512 512
      return n;
513 513
    }
514 514

	
515 515
    ///Processes the next node.
516 516

	
517 517
    ///Processes the next node. And checks that at least one of
518 518
    ///reached node has true value in the \c nm node map. If one node
519 519
    ///with true value is reachable from the processed node then the
520 520
    ///rnode parameter will be set to the first of such nodes.
521 521
    ///
522 522
    ///\param nm The node map of possible targets.
523 523
    ///\retval rnode The reached target node.
524 524
    ///\return The processed node.
525 525
    ///
526 526
    ///\warning The queue must not be empty!
527 527
    template<class NM>
528 528
    Node processNextNode(const NM& nm, Node& rnode)
529 529
    {
530 530
      if(_queue_tail==_queue_next_dist) {
531 531
	_curr_dist++;
532 532
	_queue_next_dist=_queue_head;
533 533
      }
534 534
      Node n=_queue[_queue_tail++];
535 535
      _processed->set(n,true);
536 536
      Node m;
537 537
      for(OutArcIt e(*G,n);e!=INVALID;++e)
538 538
	if(!(*_reached)[m=G->target(e)]) {
539 539
	  _queue[_queue_head++]=m;
540 540
	  _reached->set(m,true);
541 541
	  _pred->set(m,e);
542 542
	  _dist->set(m,_curr_dist);
543 543
	  if (nm[m] && rnode == INVALID) rnode = m;
544 544
	}
545 545
      return n;
546 546
    }
547 547
      
548 548
    ///Next node to be processed.
549 549

	
550 550
    ///Next node to be processed.
551 551
    ///
552 552
    ///\return The next node to be processed or INVALID if the queue is
553 553
    /// empty.
554 554
    Node nextNode()
555 555
    { 
556 556
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
557 557
    }
558 558
 
559 559
    ///\brief Returns \c false if there are nodes
560 560
    ///to be processed in the queue
561 561
    ///
562 562
    ///Returns \c false if there are nodes
563 563
    ///to be processed in the queue
564 564
    bool emptyQueue() { return _queue_tail==_queue_head; }
565 565
    ///Returns the number of the nodes to be processed.
566 566
    
567 567
    ///Returns the number of the nodes to be processed in the queue.
568 568
    int queueSize() { return _queue_head-_queue_tail; }
569 569
    
570 570
    ///Executes the algorithm.
571 571

	
572 572
    ///Executes the algorithm.
573 573
    ///
574 574
    ///\pre init() must be called and at least one node should be added
575 575
    ///with addSource() before using this function.
576 576
    ///
577 577
    ///This method runs the %BFS algorithm from the root node(s)
578 578
    ///in order to
579 579
    ///compute the
580 580
    ///shortest path to each node. The algorithm computes
581 581
    ///- The shortest path tree.
582 582
    ///- The distance of each node from the root(s).
583 583
    void start()
584 584
    {
585 585
      while ( !emptyQueue() ) processNextNode();
586 586
    }
587 587
    
588 588
    ///Executes the algorithm until \c dest is reached.
589 589

	
590 590
    ///Executes the algorithm until \c dest is reached.
591 591
    ///
592 592
    ///\pre init() must be called and at least one node should be added
593 593
    ///with addSource() before using this function.
594 594
    ///
595 595
    ///This method runs the %BFS algorithm from the root node(s)
596 596
    ///in order to compute the shortest path to \c dest.
597 597
    ///The algorithm computes
598 598
    ///- The shortest path to \c  dest.
599 599
    ///- The distance of \c dest from the root(s).
600 600
    void start(Node dest)
601 601
    {
602 602
      bool reach = false;
603 603
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
604 604
    }
605 605
    
606 606
    ///Executes the algorithm until a condition is met.
607 607

	
608 608
    ///Executes the algorithm until a condition is met.
609 609
    ///
610 610
    ///\pre init() must be called and at least one node should be added
611 611
    ///with addSource() before using this function.
612 612
    ///
613 613
    ///\param nm must be a bool (or convertible) node map. The
614 614
    ///algorithm will stop when it reaches a node \c v with
615 615
    /// <tt>nm[v]</tt> true.
616 616
    ///
617 617
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
618 618
    ///\c INVALID if no such node was found.
619 619
    template<class NM>
620 620
    Node start(const NM &nm)
621 621
    {
622 622
      Node rnode = INVALID;
623 623
      while ( !emptyQueue() && rnode == INVALID ) {
624 624
	processNextNode(nm, rnode);
625 625
      }
626 626
      return rnode;
627 627
    }
628 628
    
629 629
    ///Runs %BFS algorithm from node \c s.
630 630
    
631 631
    ///This method runs the %BFS algorithm from a root node \c s
632 632
    ///in order to
633 633
    ///compute the
634 634
    ///shortest path to each node. The algorithm computes
635 635
    ///- The shortest path tree.
636 636
    ///- The distance of each node from the root.
637 637
    ///
638 638
    ///\note b.run(s) is just a shortcut of the following code.
639 639
    ///\code
640 640
    ///  b.init();
641 641
    ///  b.addSource(s);
642 642
    ///  b.start();
643 643
    ///\endcode
644 644
    void run(Node s) {
645 645
      init();
646 646
      addSource(s);
647 647
      start();
648 648
    }
649 649
    
650 650
    ///Finds the shortest path between \c s and \c t.
651 651
    
652 652
    ///Finds the shortest path between \c s and \c t.
653 653
    ///
654 654
    ///\return The length of the shortest s---t path if there exists one,
655 655
    ///0 otherwise.
656 656
    ///\note Apart from the return value, b.run(s) is
657 657
    ///just a shortcut of the following code.
658 658
    ///\code
659 659
    ///  b.init();
660 660
    ///  b.addSource(s);
661 661
    ///  b.start(t);
662 662
    ///\endcode
663 663
    int run(Node s,Node t) {
664 664
      init();
665 665
      addSource(s);
666 666
      start(t);
667 667
      return reached(t) ? _curr_dist : 0;
668 668
    }
669 669
    
670 670
    ///@}
671 671

	
672 672
    ///\name Query Functions
673 673
    ///The result of the %BFS algorithm can be obtained using these
674 674
    ///functions.\n
675 675
    ///Before the use of these functions,
676 676
    ///either run() or start() must be calleb.
677 677
    
678 678
    ///@{
679 679

	
680 680
    typedef PredMapPath<Digraph, PredMap> Path;
681 681

	
682 682
    ///Gives back the shortest path.
683 683
    
684 684
    ///Gives back the shortest path.
685 685
    ///\pre The \c t should be reachable from the source.
686 686
    Path path(Node t) 
687 687
    {
688 688
      return Path(*G, *_pred, t);
689 689
    }
690 690

	
691 691
    ///The distance of a node from the root(s).
692 692

	
693 693
    ///Returns the distance of a node from the root(s).
694 694
    ///\pre \ref run() must be called before using this function.
695 695
    ///\warning If node \c v in unreachable from the root(s) the return value
696 696
    ///of this function is undefined.
697 697
    int dist(Node v) const { return (*_dist)[v]; }
698 698

	
699 699
    ///Returns the 'previous arc' of the shortest path tree.
700 700

	
701 701
    ///For a node \c v it returns the 'previous arc'
702 702
    ///of the shortest path tree,
703 703
    ///i.e. it returns the last arc of a shortest path from the root(s) to \c
704 704
    ///v. It is \ref INVALID
705 705
    ///if \c v is unreachable from the root(s) or \c v is a root. The
706 706
    ///shortest path tree used here is equal to the shortest path tree used in
707 707
    ///\ref predNode().
708 708
    ///\pre Either \ref run() or \ref start() must be called before using
709 709
    ///this function.
710 710
    Arc predArc(Node v) const { return (*_pred)[v];}
711 711

	
712 712
    ///Returns the 'previous node' of the shortest path tree.
713 713

	
714 714
    ///For a node \c v it returns the 'previous node'
715 715
    ///of the shortest path tree,
716 716
    ///i.e. it returns the last but one node from a shortest path from the
717 717
    ///root(a) to \c /v.
718 718
    ///It is INVALID if \c v is unreachable from the root(s) or
719 719
    ///if \c v itself a root.
720 720
    ///The shortest path tree used here is equal to the shortest path
721 721
    ///tree used in \ref predArc().
722 722
    ///\pre Either \ref run() or \ref start() must be called before
723 723
    ///using this function.
724 724
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
725 725
				  G->source((*_pred)[v]); }
726 726
    
727 727
    ///Returns a reference to the NodeMap of distances.
728 728

	
729 729
    ///Returns a reference to the NodeMap of distances.
730 730
    ///\pre Either \ref run() or \ref init() must
731 731
    ///be called before using this function.
732 732
    const DistMap &distMap() const { return *_dist;}
733 733
 
734 734
    ///Returns a reference to the shortest path tree map.
735 735

	
736 736
    ///Returns a reference to the NodeMap of the arcs of the
737 737
    ///shortest path tree.
738 738
    ///\pre Either \ref run() or \ref init()
739 739
    ///must be called before using this function.
740 740
    const PredMap &predMap() const { return *_pred;}
741 741
 
742 742
    ///Checks if a node is reachable from the root.
743 743

	
744 744
    ///Returns \c true if \c v is reachable from the root.
745 745
    ///\warning The source nodes are indicated as unreached.
746 746
    ///\pre Either \ref run() or \ref start()
747 747
    ///must be called before using this function.
748 748
    ///
749 749
    bool reached(Node v) { return (*_reached)[v]; }
750 750
    
751 751
    ///@}
752 752
  };
753 753

	
754 754
  ///Default traits class of Bfs function.
755 755

	
756 756
  ///Default traits class of Bfs function.
757 757
  ///\tparam GR Digraph type.
758 758
  template<class GR>
759 759
  struct BfsWizardDefaultTraits
760 760
  {
761 761
    ///The digraph type the algorithm runs on. 
762 762
    typedef GR Digraph;
763 763
    ///\brief The type of the map that stores the last
764 764
    ///arcs of the shortest paths.
765 765
    /// 
766 766
    ///The type of the map that stores the last
767 767
    ///arcs of the shortest paths.
768 768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
769 769
    ///
770 770
    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
771 771
    ///Instantiates a PredMap.
772 772
 
773 773
    ///This function instantiates a \ref PredMap. 
774 774
    ///\param g is the digraph, to which we would like to define the PredMap.
775 775
    ///\todo The digraph alone may be insufficient to initialize
776 776
#ifdef DOXYGEN
777 777
    static PredMap *createPredMap(const GR &g) 
778 778
#else
779 779
    static PredMap *createPredMap(const GR &) 
780 780
#endif
781 781
    {
782 782
      return new PredMap();
783 783
    }
784 784

	
785 785
    ///The type of the map that indicates which nodes are processed.
786 786
 
787 787
    ///The type of the map that indicates which nodes are processed.
788 788
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
789 789
    ///\todo named parameter to set this type, function to read and write.
790 790
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
791 791
    ///Instantiates a ProcessedMap.
792 792
 
793 793
    ///This function instantiates a \ref ProcessedMap. 
794 794
    ///\param g is the digraph, to which
795 795
    ///we would like to define the \ref ProcessedMap
796 796
#ifdef DOXYGEN
797 797
    static ProcessedMap *createProcessedMap(const GR &g)
798 798
#else
799 799
    static ProcessedMap *createProcessedMap(const GR &)
800 800
#endif
801 801
    {
802 802
      return new ProcessedMap();
803 803
    }
804 804
    ///The type of the map that indicates which nodes are reached.
805 805
 
806 806
    ///The type of the map that indicates which nodes are reached.
807 807
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
808 808
    ///\todo named parameter to set this type, function to read and write.
809 809
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
810 810
    ///Instantiates a ReachedMap.
811 811
 
812 812
    ///This function instantiates a \ref ReachedMap. 
813 813
    ///\param G is the digraph, to which
814 814
    ///we would like to define the \ref ReachedMap.
815 815
    static ReachedMap *createReachedMap(const GR &G)
816 816
    {
817 817
      return new ReachedMap(G);
818 818
    }
819 819
    ///The type of the map that stores the dists of the nodes.
820 820
 
821 821
    ///The type of the map that stores the dists of the nodes.
822 822
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
823 823
    ///
824 824
    typedef NullMap<typename Digraph::Node,int> DistMap;
825 825
    ///Instantiates a DistMap.
826 826
 
827 827
    ///This function instantiates a \ref DistMap. 
828 828
    ///\param g is the digraph, to which we would like to define the \ref DistMap
829 829
#ifdef DOXYGEN
830 830
    static DistMap *createDistMap(const GR &g)
831 831
#else
832 832
    static DistMap *createDistMap(const GR &)
833 833
#endif
834 834
    {
835 835
      return new DistMap();
836 836
    }
837 837
  };
838 838
  
839 839
  /// Default traits used by \ref BfsWizard
840 840

	
841 841
  /// To make it easier to use Bfs algorithm
842 842
  ///we have created a wizard class.
843 843
  /// This \ref BfsWizard class needs default traits,
844 844
  ///as well as the \ref Bfs class.
845 845
  /// The \ref BfsWizardBase is a class to be the default traits of the
846 846
  /// \ref BfsWizard class.
847 847
  template<class GR>
848 848
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
849 849
  {
850 850

	
851 851
    typedef BfsWizardDefaultTraits<GR> Base;
852 852
  protected:
853 853
    /// Type of the nodes in the digraph.
854 854
    typedef typename Base::Digraph::Node Node;
855 855

	
856 856
    /// Pointer to the underlying digraph.
857 857
    void *_g;
858 858
    ///Pointer to the map of reached nodes.
859 859
    void *_reached;
860 860
    ///Pointer to the map of processed nodes.
861 861
    void *_processed;
862 862
    ///Pointer to the map of predecessors arcs.
863 863
    void *_pred;
864 864
    ///Pointer to the map of distances.
865 865
    void *_dist;
866 866
    ///Pointer to the source node.
867 867
    Node _source;
868 868
    
869 869
    public:
870 870
    /// Constructor.
871 871
    
872 872
    /// This constructor does not require parameters, therefore it initiates
873 873
    /// all of the attributes to default values (0, INVALID).
874 874
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
875 875
			   _dist(0), _source(INVALID) {}
876 876

	
877 877
    /// Constructor.
878 878
    
879 879
    /// This constructor requires some parameters,
880 880
    /// listed in the parameters list.
881 881
    /// Others are initiated to 0.
882 882
    /// \param g is the initial value of  \ref _g
883 883
    /// \param s is the initial value of  \ref _source
884 884
    BfsWizardBase(const GR &g, Node s=INVALID) :
885 885
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
886 886
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
887 887

	
888 888
  };
889 889
  
890 890
  /// A class to make the usage of Bfs algorithm easier
891 891

	
892 892
  /// This class is created to make it easier to use Bfs algorithm.
893 893
  /// It uses the functions and features of the plain \ref Bfs,
894 894
  /// but it is much simpler to use it.
895 895
  ///
896 896
  /// Simplicity means that the way to change the types defined
897 897
  /// in the traits class is based on functions that returns the new class
898 898
  /// and not on templatable built-in classes.
899 899
  /// When using the plain \ref Bfs
900 900
  /// the new class with the modified type comes from
901 901
  /// the original class by using the ::
902 902
  /// operator. In the case of \ref BfsWizard only
903 903
  /// a function have to be called and it will
904 904
  /// return the needed class.
905 905
  ///
906 906
  /// It does not have own \ref run method. When its \ref run method is called
907 907
  /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
908 908
  /// method of it.
909 909
  template<class TR>
910 910
  class BfsWizard : public TR
911 911
  {
912 912
    typedef TR Base;
913 913

	
914 914
    ///The type of the underlying digraph.
915 915
    typedef typename TR::Digraph Digraph;
916 916
    //\e
917 917
    typedef typename Digraph::Node Node;
918 918
    //\e
919 919
    typedef typename Digraph::NodeIt NodeIt;
920 920
    //\e
921 921
    typedef typename Digraph::Arc Arc;
922 922
    //\e
923 923
    typedef typename Digraph::OutArcIt OutArcIt;
924 924
    
925 925
    ///\brief The type of the map that stores
926 926
    ///the reached nodes
927 927
    typedef typename TR::ReachedMap ReachedMap;
928 928
    ///\brief The type of the map that stores
929 929
    ///the processed nodes
930 930
    typedef typename TR::ProcessedMap ProcessedMap;
931 931
    ///\brief The type of the map that stores the last
932 932
    ///arcs of the shortest paths.
933 933
    typedef typename TR::PredMap PredMap;
934 934
    ///The type of the map that stores the dists of the nodes.
935 935
    typedef typename TR::DistMap DistMap;
936 936

	
937 937
  public:
938 938
    /// Constructor.
939 939
    BfsWizard() : TR() {}
940 940

	
941 941
    /// Constructor that requires parameters.
942 942

	
943 943
    /// Constructor that requires parameters.
944 944
    /// These parameters will be the default values for the traits class.
945 945
    BfsWizard(const Digraph &g, Node s=INVALID) :
946 946
      TR(g,s) {}
947 947

	
948 948
    ///Copy constructor
949 949
    BfsWizard(const TR &b) : TR(b) {}
950 950

	
951 951
    ~BfsWizard() {}
952 952

	
953 953
    ///Runs Bfs algorithm from a given node.
954 954
    
955 955
    ///Runs Bfs algorithm from a given node.
956 956
    ///The node can be given by the \ref source function.
957 957
    void run()
958 958
    {
959 959
      if(Base::_source==INVALID) throw UninitializedParameter();
960 960
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
961 961
      if(Base::_reached)
962 962
	alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
963 963
      if(Base::_processed) 
964 964
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
965 965
      if(Base::_pred) 
966 966
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
967 967
      if(Base::_dist) 
968 968
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
969 969
      alg.run(Base::_source);
970 970
    }
971 971

	
972 972
    ///Runs Bfs algorithm from the given node.
973 973

	
974 974
    ///Runs Bfs algorithm from the given node.
975 975
    ///\param s is the given source.
976 976
    void run(Node s)
977 977
    {
978 978
      Base::_source=s;
979 979
      run();
980 980
    }
981 981

	
982 982
    template<class T>
983 983
    struct DefPredMapBase : public Base {
984 984
      typedef T PredMap;
985 985
      static PredMap *createPredMap(const Digraph &) { return 0; };
986 986
      DefPredMapBase(const TR &b) : TR(b) {}
987 987
    };
988 988
    
989 989
    ///\brief \ref named-templ-param "Named parameter"
990 990
    ///function for setting PredMap
991 991
    ///
992 992
    /// \ref named-templ-param "Named parameter"
993 993
    ///function for setting PredMap
994 994
    ///
995 995
    template<class T>
996 996
    BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
997 997
    {
998 998
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
999 999
      return BfsWizard<DefPredMapBase<T> >(*this);
1000 1000
    }
1001 1001
    
1002 1002
 
1003 1003
    template<class T>
1004 1004
    struct DefReachedMapBase : public Base {
1005 1005
      typedef T ReachedMap;
1006 1006
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1007 1007
      DefReachedMapBase(const TR &b) : TR(b) {}
1008 1008
    };
1009 1009
    
1010 1010
    ///\brief \ref named-templ-param "Named parameter"
1011 1011
    ///function for setting ReachedMap
1012 1012
    ///
1013 1013
    /// \ref named-templ-param "Named parameter"
1014 1014
    ///function for setting ReachedMap
1015 1015
    ///
1016 1016
    template<class T>
1017 1017
    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
1018 1018
    {
1019
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1019
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1020 1020
      return BfsWizard<DefReachedMapBase<T> >(*this);
1021 1021
    }
1022 1022
    
1023 1023

	
1024 1024
    template<class T>
1025 1025
    struct DefProcessedMapBase : public Base {
1026 1026
      typedef T ProcessedMap;
1027 1027
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1028 1028
      DefProcessedMapBase(const TR &b) : TR(b) {}
1029 1029
    };
1030 1030
    
1031 1031
    ///\brief \ref named-templ-param "Named parameter"
1032 1032
    ///function for setting ProcessedMap
1033 1033
    ///
1034 1034
    /// \ref named-templ-param "Named parameter"
1035 1035
    ///function for setting ProcessedMap
1036 1036
    ///
1037 1037
    template<class T>
1038 1038
    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
1039 1039
    {
1040
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1040
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1041 1041
      return BfsWizard<DefProcessedMapBase<T> >(*this);
1042 1042
    }
1043 1043
    
1044 1044
   
1045 1045
    template<class T>
1046 1046
    struct DefDistMapBase : public Base {
1047 1047
      typedef T DistMap;
1048 1048
      static DistMap *createDistMap(const Digraph &) { return 0; };
1049 1049
      DefDistMapBase(const TR &b) : TR(b) {}
1050 1050
    };
1051 1051
    
1052 1052
    ///\brief \ref named-templ-param "Named parameter"
1053 1053
    ///function for setting DistMap type
1054 1054
    ///
1055 1055
    /// \ref named-templ-param "Named parameter"
1056 1056
    ///function for setting DistMap type
1057 1057
    ///
1058 1058
    template<class T>
1059 1059
    BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
1060 1060
    {
1061 1061
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1062 1062
      return BfsWizard<DefDistMapBase<T> >(*this);
1063 1063
    }
1064 1064
    
1065 1065
    /// Sets the source node, from which the Bfs algorithm runs.
1066 1066

	
1067 1067
    /// Sets the source node, from which the Bfs algorithm runs.
1068 1068
    /// \param s is the source node.
1069 1069
    BfsWizard<TR> &source(Node s) 
1070 1070
    {
1071 1071
      Base::_source=s;
1072 1072
      return *this;
1073 1073
    }
1074 1074
    
1075 1075
  };
1076 1076
  
1077 1077
  ///Function type interface for Bfs algorithm.
1078 1078

	
1079 1079
  /// \ingroup search
1080 1080
  ///Function type interface for Bfs algorithm.
1081 1081
  ///
1082 1082
  ///This function also has several
1083 1083
  ///\ref named-templ-func-param "named parameters",
1084 1084
  ///they are declared as the members of class \ref BfsWizard.
1085 1085
  ///The following
1086 1086
  ///example shows how to use these parameters.
1087 1087
  ///\code
1088 1088
  ///  bfs(g,source).predMap(preds).run();
1089 1089
  ///\endcode
1090 1090
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
1091 1091
  ///to the end of the parameter list.
1092 1092
  ///\sa BfsWizard
1093 1093
  ///\sa Bfs
1094 1094
  template<class GR>
1095 1095
  BfsWizard<BfsWizardBase<GR> >
1096 1096
  bfs(const GR &g,typename GR::Node s=INVALID)
1097 1097
  {
1098 1098
    return BfsWizard<BfsWizardBase<GR> >(g,s);
1099 1099
  }
1100 1100

	
1101 1101
#ifdef DOXYGEN
1102 1102
  /// \brief Visitor class for bfs.
1103 1103
  ///  
1104 1104
  /// This class defines the interface of the BfsVisit events, and
1105 1105
  /// it could be the base of a real Visitor class.
1106 1106
  template <typename _Digraph>
1107 1107
  struct BfsVisitor {
1108 1108
    typedef _Digraph Digraph;
1109 1109
    typedef typename Digraph::Arc Arc;
1110 1110
    typedef typename Digraph::Node Node;
1111 1111
    /// \brief Called when the arc reach a node.
1112 1112
    /// 
1113 1113
    /// It is called when the bfs find an arc which target is not
1114 1114
    /// reached yet.
1115 1115
    void discover(const Arc& arc) {}
1116 1116
    /// \brief Called when the node reached first time.
1117 1117
    /// 
1118 1118
    /// It is Called when the node reached first time.
1119 1119
    void reach(const Node& node) {}
1120 1120
    /// \brief Called when the arc examined but target of the arc 
1121 1121
    /// already discovered.
1122 1122
    /// 
1123 1123
    /// It called when the arc examined but the target of the arc 
1124 1124
    /// already discovered.
1125 1125
    void examine(const Arc& arc) {}
1126 1126
    /// \brief Called for the source node of the bfs.
1127 1127
    /// 
1128 1128
    /// It is called for the source node of the bfs.
1129 1129
    void start(const Node& node) {}
1130 1130
    /// \brief Called when the node processed.
1131 1131
    /// 
1132 1132
    /// It is Called when the node processed.
1133 1133
    void process(const Node& node) {}
1134 1134
  };
1135 1135
#else
1136 1136
  template <typename _Digraph>
1137 1137
  struct BfsVisitor {
1138 1138
    typedef _Digraph Digraph;
1139 1139
    typedef typename Digraph::Arc Arc;
1140 1140
    typedef typename Digraph::Node Node;
1141 1141
    void discover(const Arc&) {}
1142 1142
    void reach(const Node&) {}
1143 1143
    void examine(const Arc&) {}
1144 1144
    void start(const Node&) {}
1145 1145
    void process(const Node&) {}
1146 1146

	
1147 1147
    template <typename _Visitor>
1148 1148
    struct Constraints {
1149 1149
      void constraints() {
1150 1150
	Arc arc;
1151 1151
	Node node;
1152 1152
	visitor.discover(arc);
1153 1153
	visitor.reach(node);
1154 1154
	visitor.examine(arc);
1155 1155
	visitor.start(node);
1156 1156
        visitor.process(node);
1157 1157
      }
1158 1158
      _Visitor& visitor;
1159 1159
    };
1160 1160
  };
1161 1161
#endif
1162 1162

	
1163 1163
  /// \brief Default traits class of BfsVisit class.
1164 1164
  ///
1165 1165
  /// Default traits class of BfsVisit class.
1166 1166
  /// \tparam _Digraph Digraph type.
1167 1167
  template<class _Digraph>
1168 1168
  struct BfsVisitDefaultTraits {
1169 1169

	
1170 1170
    /// \brief The digraph type the algorithm runs on. 
1171 1171
    typedef _Digraph Digraph;
1172 1172

	
1173 1173
    /// \brief The type of the map that indicates which nodes are reached.
1174 1174
    /// 
1175 1175
    /// The type of the map that indicates which nodes are reached.
1176 1176
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
1177 1177
    /// \todo named parameter to set this type, function to read and write.
1178 1178
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1179 1179

	
1180 1180
    /// \brief Instantiates a ReachedMap.
1181 1181
    ///
1182 1182
    /// This function instantiates a \ref ReachedMap. 
1183 1183
    /// \param digraph is the digraph, to which
1184 1184
    /// we would like to define the \ref ReachedMap.
1185 1185
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1186 1186
      return new ReachedMap(digraph);
1187 1187
    }
1188 1188

	
1189 1189
  };
1190 1190

	
1191 1191
  /// \ingroup search
1192 1192
  ///  
1193 1193
  /// \brief %BFS Visit algorithm class.
1194 1194
  ///  
1195 1195
  /// This class provides an efficient implementation of the %BFS algorithm
1196 1196
  /// with visitor interface.
1197 1197
  ///
1198 1198
  /// The %BfsVisit class provides an alternative interface to the Bfs
1199 1199
  /// class. It works with callback mechanism, the BfsVisit object calls
1200 1200
  /// on every bfs event the \c Visitor class member functions. 
1201 1201
  ///
1202 1202
  /// \tparam _Digraph The digraph type the algorithm runs on. The default value is
1203 1203
  /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
1204 1204
  /// is only passed to \ref BfsDefaultTraits.
1205 1205
  /// \tparam _Visitor The Visitor object for the algorithm. The 
1206 1206
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
1207 1207
  /// does not observe the Bfs events. If you want to observe the bfs
1208 1208
  /// events you should implement your own Visitor class.
1209 1209
  /// \tparam _Traits Traits class to set various data types used by the 
1210 1210
  /// algorithm. The default traits class is
1211 1211
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
1212 1212
  /// See \ref BfsVisitDefaultTraits for the documentation of
1213 1213
  /// a Bfs visit traits class.
1214 1214
#ifdef DOXYGEN
1215 1215
  template <typename _Digraph, typename _Visitor, typename _Traits>
1216 1216
#else
1217 1217
  template <typename _Digraph = ListDigraph,
1218 1218
	    typename _Visitor = BfsVisitor<_Digraph>,
1219 1219
	    typename _Traits = BfsDefaultTraits<_Digraph> >
1220 1220
#endif
1221 1221
  class BfsVisit {
1222 1222
  public:
1223 1223
    
1224 1224
    /// \brief \ref Exception for uninitialized parameters.
1225 1225
    ///
1226 1226
    /// This error represents problems in the initialization
1227 1227
    /// of the parameters of the algorithms.
1228 1228
    class UninitializedParameter : public lemon::UninitializedParameter {
1229 1229
    public:
1230 1230
      virtual const char* what() const throw() 
1231 1231
      {
1232 1232
	return "lemon::BfsVisit::UninitializedParameter";
1233 1233
      }
1234 1234
    };
1235 1235

	
1236 1236
    typedef _Traits Traits;
1237 1237

	
1238 1238
    typedef typename Traits::Digraph Digraph;
1239 1239

	
1240 1240
    typedef _Visitor Visitor;
1241 1241

	
1242 1242
    ///The type of the map indicating which nodes are reached.
1243 1243
    typedef typename Traits::ReachedMap ReachedMap;
1244 1244

	
1245 1245
  private:
1246 1246

	
1247 1247
    typedef typename Digraph::Node Node;
1248 1248
    typedef typename Digraph::NodeIt NodeIt;
1249 1249
    typedef typename Digraph::Arc Arc;
1250 1250
    typedef typename Digraph::OutArcIt OutArcIt;
1251 1251

	
1252 1252
    /// Pointer to the underlying digraph.
1253 1253
    const Digraph *_digraph;
1254 1254
    /// Pointer to the visitor object.
1255 1255
    Visitor *_visitor;
1256 1256
    ///Pointer to the map of reached status of the nodes.
1257 1257
    ReachedMap *_reached;
1258 1258
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
1259 1259
    bool local_reached;
1260 1260

	
1261 1261
    std::vector<typename Digraph::Node> _list;
1262 1262
    int _list_front, _list_back;
1263 1263

	
1264 1264
    /// \brief Creates the maps if necessary.
1265 1265
    ///
1266 1266
    /// Creates the maps if necessary.
1267 1267
    void create_maps() {
1268 1268
      if(!_reached) {
1269 1269
	local_reached = true;
1270 1270
	_reached = Traits::createReachedMap(*_digraph);
1271 1271
      }
1272 1272
    }
1273 1273

	
1274 1274
  protected:
1275 1275

	
1276 1276
    BfsVisit() {}
1277 1277
    
1278 1278
  public:
1279 1279

	
1280 1280
    typedef BfsVisit Create;
1281 1281

	
1282 1282
    /// \name Named template parameters
1283 1283

	
1284 1284
    ///@{
1285 1285
    template <class T>
1286 1286
    struct DefReachedMapTraits : public Traits {
1287 1287
      typedef T ReachedMap;
1288 1288
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1289 1289
	throw UninitializedParameter();
1290 1290
      }
1291 1291
    };
1292 1292
    /// \brief \ref named-templ-param "Named parameter" for setting 
1293 1293
    /// ReachedMap type
1294 1294
    ///
1295 1295
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
1296 1296
    template <class T>
1297 1297
    struct DefReachedMap : public BfsVisit< Digraph, Visitor,
1298 1298
					    DefReachedMapTraits<T> > {
1299 1299
      typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
1300 1300
    };
1301 1301
    ///@}
1302 1302

	
1303 1303
  public:      
1304 1304
    
1305 1305
    /// \brief Constructor.
1306 1306
    ///
1307 1307
    /// Constructor.
1308 1308
    ///
1309 1309
    /// \param digraph the digraph the algorithm will run on.
1310 1310
    /// \param visitor The visitor of the algorithm.
1311 1311
    ///
1312 1312
    BfsVisit(const Digraph& digraph, Visitor& visitor) 
1313 1313
      : _digraph(&digraph), _visitor(&visitor),
1314 1314
	_reached(0), local_reached(false) {}
1315 1315
    
1316 1316
    /// \brief Destructor.
1317 1317
    ///
1318 1318
    /// Destructor.
1319 1319
    ~BfsVisit() {
1320 1320
      if(local_reached) delete _reached;
1321 1321
    }
1322 1322

	
1323 1323
    /// \brief Sets the map indicating if a node is reached.
1324 1324
    ///
1325 1325
    /// Sets the map indicating if a node is reached.
1326 1326
    /// If you don't use this function before calling \ref run(),
1327 1327
    /// it will allocate one. The destuctor deallocates this
1328 1328
    /// automatically allocated map, of course.
1329 1329
    /// \return <tt> (*this) </tt>
1330 1330
    BfsVisit &reachedMap(ReachedMap &m) {
1331 1331
      if(local_reached) {
1332 1332
	delete _reached;
1333 1333
	local_reached = false;
1334 1334
      }
1335 1335
      _reached = &m;
1336 1336
      return *this;
1337 1337
    }
1338 1338

	
1339 1339
  public:
1340 1340
    /// \name Execution control
1341 1341
    /// The simplest way to execute the algorithm is to use
1342 1342
    /// one of the member functions called \c run(...).
1343 1343
    /// \n
1344 1344
    /// If you need more control on the execution,
1345 1345
    /// first you must call \ref init(), then you can adda source node
1346 1346
    /// with \ref addSource().
1347 1347
    /// Finally \ref start() will perform the actual path
1348 1348
    /// computation.
1349 1349

	
1350 1350
    /// @{
1351 1351
    /// \brief Initializes the internal data structures.
1352 1352
    ///
1353 1353
    /// Initializes the internal data structures.
1354 1354
    ///
1355 1355
    void init() {
1356 1356
      create_maps();
1357 1357
      _list.resize(countNodes(*_digraph));
1358 1358
      _list_front = _list_back = -1;
1359 1359
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1360 1360
	_reached->set(u, false);
1361 1361
      }
1362 1362
    }
1363 1363
    
1364 1364
    /// \brief Adds a new source node.
1365 1365
    ///
1366 1366
    /// Adds a new source node to the set of nodes to be processed.
1367 1367
    void addSource(Node s) {
1368 1368
      if(!(*_reached)[s]) {
1369 1369
	  _reached->set(s,true);
1370 1370
	  _visitor->start(s);
1371 1371
	  _visitor->reach(s);
1372 1372
          _list[++_list_back] = s;
1373 1373
	}
1374 1374
    }
1375 1375
    
1376 1376
    /// \brief Processes the next node.
1377 1377
    ///
1378 1378
    /// Processes the next node.
1379 1379
    ///
1380 1380
    /// \return The processed node.
1381 1381
    ///
1382 1382
    /// \pre The queue must not be empty!
1383 1383
    Node processNextNode() { 
1384 1384
      Node n = _list[++_list_front];
1385 1385
      _visitor->process(n);
1386 1386
      Arc e;
1387 1387
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1388 1388
        Node m = _digraph->target(e);
1389 1389
        if (!(*_reached)[m]) {
1390 1390
          _visitor->discover(e);
1391 1391
          _visitor->reach(m);
1392 1392
          _reached->set(m, true);
1393 1393
          _list[++_list_back] = m;
1394 1394
        } else {
1395 1395
          _visitor->examine(e);
1396 1396
        }
1397 1397
      }
1398 1398
      return n;
1399 1399
    }
1400 1400

	
1401 1401
    /// \brief Processes the next node.
1402 1402
    ///
1403 1403
    /// Processes the next node. And checks that the given target node
1404 1404
    /// is reached. If the target node is reachable from the processed
1405 1405
    /// node then the reached parameter will be set true. The reached
1406 1406
    /// parameter should be initially false.
1407 1407
    ///
1408 1408
    /// \param target The target node.
1409 1409
    /// \retval reach Indicates that the target node is reached.
1410 1410
    /// \return The processed node.
1411 1411
    ///
1412 1412
    /// \warning The queue must not be empty!
1413 1413
    Node processNextNode(Node target, bool& reach) {
1414 1414
      Node n = _list[++_list_front];
1415 1415
      _visitor->process(n);
1416 1416
      Arc e;
1417 1417
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1418 1418
        Node m = _digraph->target(e);
1419 1419
        if (!(*_reached)[m]) {
1420 1420
          _visitor->discover(e);
1421 1421
          _visitor->reach(m);
1422 1422
          _reached->set(m, true);
1423 1423
          _list[++_list_back] = m;
1424 1424
          reach = reach || (target == m);
1425 1425
        } else {
1426 1426
          _visitor->examine(e);
1427 1427
        }
1428 1428
      }
1429 1429
      return n;
1430 1430
    }
1431 1431

	
1432 1432
    /// \brief Processes the next node.
1433 1433
    ///
1434 1434
    /// Processes the next node. And checks that at least one of
1435 1435
    /// reached node has true value in the \c nm node map. If one node
1436 1436
    /// with true value is reachable from the processed node then the
1437 1437
    /// rnode parameter will be set to the first of such nodes.
1438 1438
    ///
1439 1439
    /// \param nm The node map of possible targets.
1440 1440
    /// \retval rnode The reached target node.
1441 1441
    /// \return The processed node.
1442 1442
    ///
1443 1443
    /// \warning The queue must not be empty!
1444 1444
    template <typename NM>
1445 1445
    Node processNextNode(const NM& nm, Node& rnode) {
1446 1446
      Node n = _list[++_list_front];
1447 1447
      _visitor->process(n);
1448 1448
      Arc e;
1449 1449
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1450 1450
        Node m = _digraph->target(e);
1451 1451
        if (!(*_reached)[m]) {
1452 1452
          _visitor->discover(e);
1453 1453
          _visitor->reach(m);
1454 1454
          _reached->set(m, true);
1455 1455
          _list[++_list_back] = m;
1456 1456
          if (nm[m] && rnode == INVALID) rnode = m;
1457 1457
        } else {
1458 1458
          _visitor->examine(e);
1459 1459
        }
1460 1460
      }
1461 1461
      return n;
1462 1462
    }
1463 1463

	
1464 1464
    /// \brief Next node to be processed.
1465 1465
    ///
1466 1466
    /// Next node to be processed.
1467 1467
    ///
1468 1468
    /// \return The next node to be processed or INVALID if the stack is
1469 1469
    /// empty.
1470 1470
    Node nextNode() { 
1471 1471
      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
1472 1472
    }
1473 1473

	
1474 1474
    /// \brief Returns \c false if there are nodes
1475 1475
    /// to be processed in the queue
1476 1476
    ///
1477 1477
    /// Returns \c false if there are nodes
1478 1478
    /// to be processed in the queue
1479 1479
    bool emptyQueue() { return _list_front == _list_back; }
1480 1480

	
1481 1481
    /// \brief Returns the number of the nodes to be processed.
1482 1482
    ///
1483 1483
    /// Returns the number of the nodes to be processed in the queue.
1484 1484
    int queueSize() { return _list_back - _list_front; }
1485 1485
    
1486 1486
    /// \brief Executes the algorithm.
1487 1487
    ///
1488 1488
    /// Executes the algorithm.
1489 1489
    ///
1490 1490
    /// \pre init() must be called and at least one node should be added
1491 1491
    /// with addSource() before using this function.
1492 1492
    void start() {
1493 1493
      while ( !emptyQueue() ) processNextNode();
1494 1494
    }
1495 1495
    
1496 1496
    /// \brief Executes the algorithm until \c dest is reached.
1497 1497
    ///
1498 1498
    /// Executes the algorithm until \c dest is reached.
1499 1499
    ///
1500 1500
    /// \pre init() must be called and at least one node should be added
1501 1501
    /// with addSource() before using this function.
1502 1502
    void start(Node dest) {
1503 1503
      bool reach = false;
1504 1504
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
1505 1505
    }
1506 1506
    
1507 1507
    /// \brief Executes the algorithm until a condition is met.
1508 1508
    ///
1509 1509
    /// Executes the algorithm until a condition is met.
1510 1510
    ///
1511 1511
    /// \pre init() must be called and at least one node should be added
1512 1512
    /// with addSource() before using this function.
1513 1513
    ///
1514 1514
    ///\param nm must be a bool (or convertible) node map. The
1515 1515
    ///algorithm will stop when it reaches a node \c v with
1516 1516
    /// <tt>nm[v]</tt> true.
1517 1517
    ///
1518 1518
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
1519 1519
    ///\c INVALID if no such node was found.
1520 1520
    template <typename NM>
1521 1521
    Node start(const NM &nm) {
1522 1522
      Node rnode = INVALID;
1523 1523
      while ( !emptyQueue() && rnode == INVALID ) {
1524 1524
	processNextNode(nm, rnode);
1525 1525
      }
1526 1526
      return rnode;
1527 1527
    }
1528 1528

	
1529 1529
    /// \brief Runs %BFSVisit algorithm from node \c s.
1530 1530
    ///
1531 1531
    /// This method runs the %BFS algorithm from a root node \c s.
1532 1532
    /// \note b.run(s) is just a shortcut of the following code.
1533 1533
    ///\code
1534 1534
    ///   b.init();
1535 1535
    ///   b.addSource(s);
1536 1536
    ///   b.start();
1537 1537
    ///\endcode
1538 1538
    void run(Node s) {
1539 1539
      init();
1540 1540
      addSource(s);
1541 1541
      start();
1542 1542
    }
1543 1543

	
1544 1544
    /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
1545 1545
    ///    
1546 1546
    /// This method runs the %BFS algorithm in order to
1547 1547
    /// compute the %BFS path to each node. The algorithm computes
1548 1548
    /// - The %BFS tree.
1549 1549
    /// - The distance of each node from the root in the %BFS tree.
1550 1550
    ///
1551 1551
    ///\note b.run() is just a shortcut of the following code.
1552 1552
    ///\code
Ignore white space 1024 line context
... ...
@@ -490,1046 +490,1046 @@
490 490
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
491 491
	_processed->set(m,true);
492 492
	--_stack_head;
493 493
	if(_stack_head>=0) {
494 494
	  m=G->source(_stack[_stack_head]);
495 495
	  ++_stack[_stack_head];
496 496
	}
497 497
      }
498 498
      return e;
499 499
    }
500 500
    ///Next arc to be processed.
501 501

	
502 502
    ///Next arc to be processed.
503 503
    ///
504 504
    ///\return The next arc to be processed or INVALID if the stack is
505 505
    /// empty.
506 506
    OutArcIt nextArc()
507 507
    { 
508 508
      return _stack_head>=0?_stack[_stack_head]:INVALID;
509 509
    }
510 510

	
511 511
    ///\brief Returns \c false if there are nodes
512 512
    ///to be processed in the queue
513 513
    ///
514 514
    ///Returns \c false if there are nodes
515 515
    ///to be processed in the queue
516 516
    bool emptyQueue() { return _stack_head<0; }
517 517
    ///Returns the number of the nodes to be processed.
518 518
    
519 519
    ///Returns the number of the nodes to be processed in the queue.
520 520
    int queueSize() { return _stack_head+1; }
521 521
    
522 522
    ///Executes the algorithm.
523 523

	
524 524
    ///Executes the algorithm.
525 525
    ///
526 526
    ///\pre init() must be called and at least one node should be added
527 527
    ///with addSource() before using this function.
528 528
    ///
529 529
    ///This method runs the %DFS algorithm from the root node(s)
530 530
    ///in order to
531 531
    ///compute the
532 532
    ///%DFS path to each node. The algorithm computes
533 533
    ///- The %DFS tree.
534 534
    ///- The distance of each node from the root(s) in the %DFS tree.
535 535
    ///
536 536
    void start()
537 537
    {
538 538
      while ( !emptyQueue() ) processNextArc();
539 539
    }
540 540
    
541 541
    ///Executes the algorithm until \c dest is reached.
542 542

	
543 543
    ///Executes the algorithm until \c dest is reached.
544 544
    ///
545 545
    ///\pre init() must be called and at least one node should be added
546 546
    ///with addSource() before using this function.
547 547
    ///
548 548
    ///This method runs the %DFS algorithm from the root node(s)
549 549
    ///in order to
550 550
    ///compute the
551 551
    ///%DFS path to \c dest. The algorithm computes
552 552
    ///- The %DFS path to \c  dest.
553 553
    ///- The distance of \c dest from the root(s) in the %DFS tree.
554 554
    ///
555 555
    void start(Node dest)
556 556
    {
557 557
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) 
558 558
	processNextArc();
559 559
    }
560 560
    
561 561
    ///Executes the algorithm until a condition is met.
562 562

	
563 563
    ///Executes the algorithm until a condition is met.
564 564
    ///
565 565
    ///\pre init() must be called and at least one node should be added
566 566
    ///with addSource() before using this function.
567 567
    ///
568 568
    ///\param em must be a bool (or convertible) arc map. The algorithm
569 569
    ///will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
570 570
    ///
571 571
    ///\return The reached arc \c e with <tt>em[e]</tt> true or
572 572
    ///\c INVALID if no such arc was found.
573 573
    ///
574 574
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
575 575
    ///not a node map.
576 576
    template<class EM>
577 577
    Arc start(const EM &em)
578 578
    {
579 579
      while ( !emptyQueue() && !em[_stack[_stack_head]] )
580 580
        processNextArc();
581 581
      return emptyQueue() ? INVALID : _stack[_stack_head];
582 582
    }
583 583

	
584 584
    ///Runs %DFS algorithm to visit all nodes in the digraph.
585 585
    
586 586
    ///This method runs the %DFS algorithm in order to
587 587
    ///compute the
588 588
    ///%DFS path to each node. The algorithm computes
589 589
    ///- The %DFS tree.
590 590
    ///- The distance of each node from the root in the %DFS tree.
591 591
    ///
592 592
    ///\note d.run() is just a shortcut of the following code.
593 593
    ///\code
594 594
    ///  d.init();
595 595
    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
596 596
    ///    if (!d.reached(it)) {
597 597
    ///      d.addSource(it);
598 598
    ///      d.start();
599 599
    ///    }
600 600
    ///  }
601 601
    ///\endcode
602 602
    void run() {
603 603
      init();
604 604
      for (NodeIt it(*G); it != INVALID; ++it) {
605 605
        if (!reached(it)) {
606 606
          addSource(it);
607 607
          start();
608 608
        }
609 609
      }
610 610
    }
611 611

	
612 612
    ///Runs %DFS algorithm from node \c s.
613 613
    
614 614
    ///This method runs the %DFS algorithm from a root node \c s
615 615
    ///in order to
616 616
    ///compute the
617 617
    ///%DFS path to each node. The algorithm computes
618 618
    ///- The %DFS tree.
619 619
    ///- The distance of each node from the root in the %DFS tree.
620 620
    ///
621 621
    ///\note d.run(s) is just a shortcut of the following code.
622 622
    ///\code
623 623
    ///  d.init();
624 624
    ///  d.addSource(s);
625 625
    ///  d.start();
626 626
    ///\endcode
627 627
    void run(Node s) {
628 628
      init();
629 629
      addSource(s);
630 630
      start();
631 631
    }
632 632
    
633 633
    ///Finds the %DFS path between \c s and \c t.
634 634
    
635 635
    ///Finds the %DFS path between \c s and \c t.
636 636
    ///
637 637
    ///\return The length of the %DFS s---t path if there exists one,
638 638
    ///0 otherwise.
639 639
    ///\note Apart from the return value, d.run(s,t) is
640 640
    ///just a shortcut of the following code.
641 641
    ///\code
642 642
    ///  d.init();
643 643
    ///  d.addSource(s);
644 644
    ///  d.start(t);
645 645
    ///\endcode
646 646
    int run(Node s,Node t) {
647 647
      init();
648 648
      addSource(s);
649 649
      start(t);
650 650
      return reached(t)?_stack_head+1:0;
651 651
    }
652 652
    
653 653
    ///@}
654 654

	
655 655
    ///\name Query Functions
656 656
    ///The result of the %DFS algorithm can be obtained using these
657 657
    ///functions.\n
658 658
    ///Before the use of these functions,
659 659
    ///either run() or start() must be called.
660 660
    
661 661
    ///@{
662 662

	
663 663
    typedef PredMapPath<Digraph, PredMap> Path;
664 664

	
665 665
    ///Gives back the shortest path.
666 666
    
667 667
    ///Gives back the shortest path.
668 668
    ///\pre The \c t should be reachable from the source.
669 669
    Path path(Node t) 
670 670
    {
671 671
      return Path(*G, *_pred, t);
672 672
    }
673 673

	
674 674
    ///The distance of a node from the root(s).
675 675

	
676 676
    ///Returns the distance of a node from the root(s).
677 677
    ///\pre \ref run() must be called before using this function.
678 678
    ///\warning If node \c v is unreachable from the root(s) then the return 
679 679
    ///value of this funcion is undefined.
680 680
    int dist(Node v) const { return (*_dist)[v]; }
681 681

	
682 682
    ///Returns the 'previous arc' of the %DFS tree.
683 683

	
684 684
    ///For a node \c v it returns the 'previous arc'
685 685
    ///of the %DFS path,
686 686
    ///i.e. it returns the last arc of a %DFS path from the root(s) to \c
687 687
    ///v. It is \ref INVALID
688 688
    ///if \c v is unreachable from the root(s) or \c v is a root. The
689 689
    ///%DFS tree used here is equal to the %DFS tree used in
690 690
    ///\ref predNode().
691 691
    ///\pre Either \ref run() or \ref start() must be called before using
692 692
    ///this function.
693 693
    Arc predArc(Node v) const { return (*_pred)[v];}
694 694

	
695 695
    ///Returns the 'previous node' of the %DFS tree.
696 696

	
697 697
    ///For a node \c v it returns the 'previous node'
698 698
    ///of the %DFS tree,
699 699
    ///i.e. it returns the last but one node from a %DFS path from the
700 700
    ///root(s) to \c v.
701 701
    ///It is INVALID if \c v is unreachable from the root(s) or
702 702
    ///if \c v itself a root.
703 703
    ///The %DFS tree used here is equal to the %DFS
704 704
    ///tree used in \ref predArc().
705 705
    ///\pre Either \ref run() or \ref start() must be called before
706 706
    ///using this function.
707 707
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
708 708
				  G->source((*_pred)[v]); }
709 709
    
710 710
    ///Returns a reference to the NodeMap of distances.
711 711

	
712 712
    ///Returns a reference to the NodeMap of distances.
713 713
    ///\pre Either \ref run() or \ref init() must
714 714
    ///be called before using this function.
715 715
    const DistMap &distMap() const { return *_dist;}
716 716
 
717 717
    ///Returns a reference to the %DFS arc-tree map.
718 718

	
719 719
    ///Returns a reference to the NodeMap of the arcs of the
720 720
    ///%DFS tree.
721 721
    ///\pre Either \ref run() or \ref init()
722 722
    ///must be called before using this function.
723 723
    const PredMap &predMap() const { return *_pred;}
724 724
 
725 725
    ///Checks if a node is reachable from the root.
726 726

	
727 727
    ///Returns \c true if \c v is reachable from the root(s).
728 728
    ///\warning The source nodes are inditated as unreachable.
729 729
    ///\pre Either \ref run() or \ref start()
730 730
    ///must be called before using this function.
731 731
    ///
732 732
    bool reached(Node v) { return (*_reached)[v]; }
733 733
    
734 734
    ///@}
735 735
  };
736 736

	
737 737
  ///Default traits class of Dfs function.
738 738

	
739 739
  ///Default traits class of Dfs function.
740 740
  ///\tparam GR Digraph type.
741 741
  template<class GR>
742 742
  struct DfsWizardDefaultTraits
743 743
  {
744 744
    ///The digraph type the algorithm runs on. 
745 745
    typedef GR Digraph;
746 746
    ///\brief The type of the map that stores the last
747 747
    ///arcs of the %DFS paths.
748 748
    /// 
749 749
    ///The type of the map that stores the last
750 750
    ///arcs of the %DFS paths.
751 751
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
752 752
    ///
753 753
    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
754 754
    ///Instantiates a PredMap.
755 755
 
756 756
    ///This function instantiates a \ref PredMap. 
757 757
    ///\param g is the digraph, to which we would like to define the PredMap.
758 758
    ///\todo The digraph alone may be insufficient to initialize
759 759
#ifdef DOXYGEN
760 760
    static PredMap *createPredMap(const GR &g) 
761 761
#else
762 762
    static PredMap *createPredMap(const GR &) 
763 763
#endif
764 764
    {
765 765
      return new PredMap();
766 766
    }
767 767

	
768 768
    ///The type of the map that indicates which nodes are processed.
769 769
 
770 770
    ///The type of the map that indicates which nodes are processed.
771 771
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
772 772
    ///\todo named parameter to set this type, function to read and write.
773 773
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
774 774
    ///Instantiates a ProcessedMap.
775 775
 
776 776
    ///This function instantiates a \ref ProcessedMap. 
777 777
    ///\param g is the digraph, to which
778 778
    ///we would like to define the \ref ProcessedMap
779 779
#ifdef DOXYGEN
780 780
    static ProcessedMap *createProcessedMap(const GR &g)
781 781
#else
782 782
    static ProcessedMap *createProcessedMap(const GR &)
783 783
#endif
784 784
    {
785 785
      return new ProcessedMap();
786 786
    }
787 787
    ///The type of the map that indicates which nodes are reached.
788 788
 
789 789
    ///The type of the map that indicates which nodes are reached.
790 790
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
791 791
    ///\todo named parameter to set this type, function to read and write.
792 792
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
793 793
    ///Instantiates a ReachedMap.
794 794
 
795 795
    ///This function instantiates a \ref ReachedMap. 
796 796
    ///\param G is the digraph, to which
797 797
    ///we would like to define the \ref ReachedMap.
798 798
    static ReachedMap *createReachedMap(const GR &G)
799 799
    {
800 800
      return new ReachedMap(G);
801 801
    }
802 802
    ///The type of the map that stores the dists of the nodes.
803 803
 
804 804
    ///The type of the map that stores the dists of the nodes.
805 805
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
806 806
    ///
807 807
    typedef NullMap<typename Digraph::Node,int> DistMap;
808 808
    ///Instantiates a DistMap.
809 809
 
810 810
    ///This function instantiates a \ref DistMap. 
811 811
    ///\param g is the digraph, to which we would like to define the \ref DistMap
812 812
#ifdef DOXYGEN
813 813
    static DistMap *createDistMap(const GR &g)
814 814
#else
815 815
    static DistMap *createDistMap(const GR &)
816 816
#endif
817 817
    {
818 818
      return new DistMap();
819 819
    }
820 820
  };
821 821
  
822 822
  /// Default traits used by \ref DfsWizard
823 823

	
824 824
  /// To make it easier to use Dfs algorithm
825 825
  ///we have created a wizard class.
826 826
  /// This \ref DfsWizard class needs default traits,
827 827
  ///as well as the \ref Dfs class.
828 828
  /// The \ref DfsWizardBase is a class to be the default traits of the
829 829
  /// \ref DfsWizard class.
830 830
  template<class GR>
831 831
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
832 832
  {
833 833

	
834 834
    typedef DfsWizardDefaultTraits<GR> Base;
835 835
  protected:
836 836
    /// Type of the nodes in the digraph.
837 837
    typedef typename Base::Digraph::Node Node;
838 838

	
839 839
    /// Pointer to the underlying digraph.
840 840
    void *_g;
841 841
    ///Pointer to the map of reached nodes.
842 842
    void *_reached;
843 843
    ///Pointer to the map of processed nodes.
844 844
    void *_processed;
845 845
    ///Pointer to the map of predecessors arcs.
846 846
    void *_pred;
847 847
    ///Pointer to the map of distances.
848 848
    void *_dist;
849 849
    ///Pointer to the source node.
850 850
    Node _source;
851 851
    
852 852
    public:
853 853
    /// Constructor.
854 854
    
855 855
    /// This constructor does not require parameters, therefore it initiates
856 856
    /// all of the attributes to default values (0, INVALID).
857 857
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
858 858
			   _dist(0), _source(INVALID) {}
859 859

	
860 860
    /// Constructor.
861 861
    
862 862
    /// This constructor requires some parameters,
863 863
    /// listed in the parameters list.
864 864
    /// Others are initiated to 0.
865 865
    /// \param g is the initial value of  \ref _g
866 866
    /// \param s is the initial value of  \ref _source
867 867
    DfsWizardBase(const GR &g, Node s=INVALID) :
868 868
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
869 869
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
870 870

	
871 871
  };
872 872
  
873 873
  /// A class to make the usage of the Dfs algorithm easier
874 874

	
875 875
  /// This class is created to make it easier to use the Dfs algorithm.
876 876
  /// It uses the functions and features of the plain \ref Dfs,
877 877
  /// but it is much simpler to use it.
878 878
  ///
879 879
  /// Simplicity means that the way to change the types defined
880 880
  /// in the traits class is based on functions that returns the new class
881 881
  /// and not on templatable built-in classes.
882 882
  /// When using the plain \ref Dfs
883 883
  /// the new class with the modified type comes from
884 884
  /// the original class by using the ::
885 885
  /// operator. In the case of \ref DfsWizard only
886 886
  /// a function have to be called and it will
887 887
  /// return the needed class.
888 888
  ///
889 889
  /// It does not have own \ref run method. When its \ref run method is called
890 890
  /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
891 891
  /// method of it.
892 892
  template<class TR>
893 893
  class DfsWizard : public TR
894 894
  {
895 895
    typedef TR Base;
896 896

	
897 897
    ///The type of the underlying digraph.
898 898
    typedef typename TR::Digraph Digraph;
899 899
    //\e
900 900
    typedef typename Digraph::Node Node;
901 901
    //\e
902 902
    typedef typename Digraph::NodeIt NodeIt;
903 903
    //\e
904 904
    typedef typename Digraph::Arc Arc;
905 905
    //\e
906 906
    typedef typename Digraph::OutArcIt OutArcIt;
907 907
    
908 908
    ///\brief The type of the map that stores
909 909
    ///the reached nodes
910 910
    typedef typename TR::ReachedMap ReachedMap;
911 911
    ///\brief The type of the map that stores
912 912
    ///the processed nodes
913 913
    typedef typename TR::ProcessedMap ProcessedMap;
914 914
    ///\brief The type of the map that stores the last
915 915
    ///arcs of the %DFS paths.
916 916
    typedef typename TR::PredMap PredMap;
917 917
    ///The type of the map that stores the distances of the nodes.
918 918
    typedef typename TR::DistMap DistMap;
919 919

	
920 920
  public:
921 921
    /// Constructor.
922 922
    DfsWizard() : TR() {}
923 923

	
924 924
    /// Constructor that requires parameters.
925 925

	
926 926
    /// Constructor that requires parameters.
927 927
    /// These parameters will be the default values for the traits class.
928 928
    DfsWizard(const Digraph &g, Node s=INVALID) :
929 929
      TR(g,s) {}
930 930

	
931 931
    ///Copy constructor
932 932
    DfsWizard(const TR &b) : TR(b) {}
933 933

	
934 934
    ~DfsWizard() {}
935 935

	
936 936
    ///Runs Dfs algorithm from a given node.
937 937
    
938 938
    ///Runs Dfs algorithm from a given node.
939 939
    ///The node can be given by the \ref source function.
940 940
    void run()
941 941
    {
942 942
      if(Base::_source==INVALID) throw UninitializedParameter();
943 943
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
944 944
      if(Base::_reached) 
945 945
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
946 946
      if(Base::_processed) 
947 947
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
948 948
      if(Base::_pred) 
949 949
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
950 950
      if(Base::_dist) 
951 951
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
952 952
      alg.run(Base::_source);
953 953
    }
954 954

	
955 955
    ///Runs Dfs algorithm from the given node.
956 956

	
957 957
    ///Runs Dfs algorithm from the given node.
958 958
    ///\param s is the given source.
959 959
    void run(Node s)
960 960
    {
961 961
      Base::_source=s;
962 962
      run();
963 963
    }
964 964

	
965 965
    template<class T>
966 966
    struct DefPredMapBase : public Base {
967 967
      typedef T PredMap;
968 968
      static PredMap *createPredMap(const Digraph &) { return 0; };
969 969
      DefPredMapBase(const TR &b) : TR(b) {}
970 970
    };
971 971
    
972 972
    ///\brief \ref named-templ-param "Named parameter"
973 973
    ///function for setting PredMap type
974 974
    ///
975 975
    /// \ref named-templ-param "Named parameter"
976 976
    ///function for setting PredMap type
977 977
    ///
978 978
    template<class T>
979 979
    DfsWizard<DefPredMapBase<T> > predMap(const T &t) 
980 980
    {
981 981
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
982 982
      return DfsWizard<DefPredMapBase<T> >(*this);
983 983
    }
984 984
    
985 985
 
986 986
    template<class T>
987 987
    struct DefReachedMapBase : public Base {
988 988
      typedef T ReachedMap;
989 989
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
990 990
      DefReachedMapBase(const TR &b) : TR(b) {}
991 991
    };
992 992
    
993 993
    ///\brief \ref named-templ-param "Named parameter"
994 994
    ///function for setting ReachedMap
995 995
    ///
996 996
    /// \ref named-templ-param "Named parameter"
997 997
    ///function for setting ReachedMap
998 998
    ///
999 999
    template<class T>
1000 1000
    DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
1001 1001
    {
1002
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1002
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1003 1003
      return DfsWizard<DefReachedMapBase<T> >(*this);
1004 1004
    }
1005 1005
    
1006 1006

	
1007 1007
    template<class T>
1008 1008
    struct DefProcessedMapBase : public Base {
1009 1009
      typedef T ProcessedMap;
1010 1010
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1011 1011
      DefProcessedMapBase(const TR &b) : TR(b) {}
1012 1012
    };
1013 1013
    
1014 1014
    ///\brief \ref named-templ-param "Named parameter"
1015 1015
    ///function for setting ProcessedMap
1016 1016
    ///
1017 1017
    /// \ref named-templ-param "Named parameter"
1018 1018
    ///function for setting ProcessedMap
1019 1019
    ///
1020 1020
    template<class T>
1021 1021
    DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
1022 1022
    {
1023
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1023
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1024 1024
      return DfsWizard<DefProcessedMapBase<T> >(*this);
1025 1025
    }
1026 1026
    
1027 1027
    template<class T>
1028 1028
    struct DefDistMapBase : public Base {
1029 1029
      typedef T DistMap;
1030 1030
      static DistMap *createDistMap(const Digraph &) { return 0; };
1031 1031
      DefDistMapBase(const TR &b) : TR(b) {}
1032 1032
    };
1033 1033
    
1034 1034
    ///\brief \ref named-templ-param "Named parameter"
1035 1035
    ///function for setting DistMap type
1036 1036
    ///
1037 1037
    /// \ref named-templ-param "Named parameter"
1038 1038
    ///function for setting DistMap type
1039 1039
    ///
1040 1040
    template<class T>
1041 1041
    DfsWizard<DefDistMapBase<T> > distMap(const T &t) 
1042 1042
    {
1043 1043
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1044 1044
      return DfsWizard<DefDistMapBase<T> >(*this);
1045 1045
    }
1046 1046
    
1047 1047
    /// Sets the source node, from which the Dfs algorithm runs.
1048 1048

	
1049 1049
    /// Sets the source node, from which the Dfs algorithm runs.
1050 1050
    /// \param s is the source node.
1051 1051
    DfsWizard<TR> &source(Node s) 
1052 1052
    {
1053 1053
      Base::_source=s;
1054 1054
      return *this;
1055 1055
    }
1056 1056
    
1057 1057
  };
1058 1058
  
1059 1059
  ///Function type interface for Dfs algorithm.
1060 1060

	
1061 1061
  ///\ingroup search
1062 1062
  ///Function type interface for Dfs algorithm.
1063 1063
  ///
1064 1064
  ///This function also has several
1065 1065
  ///\ref named-templ-func-param "named parameters",
1066 1066
  ///they are declared as the members of class \ref DfsWizard.
1067 1067
  ///The following
1068 1068
  ///example shows how to use these parameters.
1069 1069
  ///\code
1070 1070
  ///  dfs(g,source).predMap(preds).run();
1071 1071
  ///\endcode
1072 1072
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1073 1073
  ///to the end of the parameter list.
1074 1074
  ///\sa DfsWizard
1075 1075
  ///\sa Dfs
1076 1076
  template<class GR>
1077 1077
  DfsWizard<DfsWizardBase<GR> >
1078 1078
  dfs(const GR &g,typename GR::Node s=INVALID)
1079 1079
  {
1080 1080
    return DfsWizard<DfsWizardBase<GR> >(g,s);
1081 1081
  }
1082 1082

	
1083 1083
#ifdef DOXYGEN
1084 1084
  /// \brief Visitor class for dfs.
1085 1085
  ///  
1086 1086
  /// It gives a simple interface for a functional interface for dfs 
1087 1087
  /// traversal. The traversal on a linear data structure. 
1088 1088
  template <typename _Digraph>
1089 1089
  struct DfsVisitor {
1090 1090
    typedef _Digraph Digraph;
1091 1091
    typedef typename Digraph::Arc Arc;
1092 1092
    typedef typename Digraph::Node Node;
1093 1093
    /// \brief Called when the arc reach a node.
1094 1094
    /// 
1095 1095
    /// It is called when the dfs find an arc which target is not
1096 1096
    /// reached yet.
1097 1097
    void discover(const Arc& arc) {}
1098 1098
    /// \brief Called when the node reached first time.
1099 1099
    /// 
1100 1100
    /// It is Called when the node reached first time.
1101 1101
    void reach(const Node& node) {}
1102 1102
    /// \brief Called when we step back on an arc.
1103 1103
    /// 
1104 1104
    /// It is called when the dfs should step back on the arc.
1105 1105
    void backtrack(const Arc& arc) {}
1106 1106
    /// \brief Called when we step back from the node.
1107 1107
    /// 
1108 1108
    /// It is called when we step back from the node.
1109 1109
    void leave(const Node& node) {}
1110 1110
    /// \brief Called when the arc examined but target of the arc 
1111 1111
    /// already discovered.
1112 1112
    /// 
1113 1113
    /// It called when the arc examined but the target of the arc 
1114 1114
    /// already discovered.
1115 1115
    void examine(const Arc& arc) {}
1116 1116
    /// \brief Called for the source node of the dfs.
1117 1117
    /// 
1118 1118
    /// It is called for the source node of the dfs.
1119 1119
    void start(const Node& node) {}
1120 1120
    /// \brief Called when we leave the source node of the dfs.
1121 1121
    /// 
1122 1122
    /// It is called when we leave the source node of the dfs.
1123 1123
    void stop(const Node& node) {}
1124 1124

	
1125 1125
  };
1126 1126
#else
1127 1127
  template <typename _Digraph>
1128 1128
  struct DfsVisitor {
1129 1129
    typedef _Digraph Digraph;
1130 1130
    typedef typename Digraph::Arc Arc;
1131 1131
    typedef typename Digraph::Node Node;
1132 1132
    void discover(const Arc&) {}
1133 1133
    void reach(const Node&) {}
1134 1134
    void backtrack(const Arc&) {}
1135 1135
    void leave(const Node&) {}
1136 1136
    void examine(const Arc&) {}
1137 1137
    void start(const Node&) {}
1138 1138
    void stop(const Node&) {}
1139 1139

	
1140 1140
    template <typename _Visitor>
1141 1141
    struct Constraints {
1142 1142
      void constraints() {
1143 1143
	Arc arc;
1144 1144
	Node node;
1145 1145
	visitor.discover(arc);
1146 1146
	visitor.reach(node);
1147 1147
	visitor.backtrack(arc);
1148 1148
	visitor.leave(node);
1149 1149
	visitor.examine(arc);
1150 1150
	visitor.start(node);
1151 1151
	visitor.stop(arc);
1152 1152
      }
1153 1153
      _Visitor& visitor;
1154 1154
    };
1155 1155
  };
1156 1156
#endif
1157 1157

	
1158 1158
  /// \brief Default traits class of DfsVisit class.
1159 1159
  ///
1160 1160
  /// Default traits class of DfsVisit class.
1161 1161
  /// \tparam _Digraph Digraph type.
1162 1162
  template<class _Digraph>
1163 1163
  struct DfsVisitDefaultTraits {
1164 1164

	
1165 1165
    /// \brief The digraph type the algorithm runs on. 
1166 1166
    typedef _Digraph Digraph;
1167 1167

	
1168 1168
    /// \brief The type of the map that indicates which nodes are reached.
1169 1169
    /// 
1170 1170
    /// The type of the map that indicates which nodes are reached.
1171 1171
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
1172 1172
    /// \todo named parameter to set this type, function to read and write.
1173 1173
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1174 1174

	
1175 1175
    /// \brief Instantiates a ReachedMap.
1176 1176
    ///
1177 1177
    /// This function instantiates a \ref ReachedMap. 
1178 1178
    /// \param digraph is the digraph, to which
1179 1179
    /// we would like to define the \ref ReachedMap.
1180 1180
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1181 1181
      return new ReachedMap(digraph);
1182 1182
    }
1183 1183

	
1184 1184
  };
1185 1185
  
1186 1186
  /// %DFS Visit algorithm class.
1187 1187
  
1188 1188
  /// \ingroup search
1189 1189
  /// This class provides an efficient implementation of the %DFS algorithm
1190 1190
  /// with visitor interface.
1191 1191
  ///
1192 1192
  /// The %DfsVisit class provides an alternative interface to the Dfs
1193 1193
  /// class. It works with callback mechanism, the DfsVisit object calls
1194 1194
  /// on every dfs event the \c Visitor class member functions. 
1195 1195
  ///
1196 1196
  /// \tparam _Digraph The digraph type the algorithm runs on. The default value is
1197 1197
  /// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it
1198 1198
  /// is only passed to \ref DfsDefaultTraits.
1199 1199
  /// \tparam _Visitor The Visitor object for the algorithm. The 
1200 1200
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitor which
1201 1201
  /// does not observe the Dfs events. If you want to observe the dfs
1202 1202
  /// events you should implement your own Visitor class.
1203 1203
  /// \tparam _Traits Traits class to set various data types used by the 
1204 1204
  /// algorithm. The default traits class is
1205 1205
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
1206 1206
  /// See \ref DfsVisitDefaultTraits for the documentation of
1207 1207
  /// a Dfs visit traits class.
1208 1208
  ///
1209 1209
  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
1210 1210
#ifdef DOXYGEN
1211 1211
  template <typename _Digraph, typename _Visitor, typename _Traits>
1212 1212
#else
1213 1213
  template <typename _Digraph = ListDigraph,
1214 1214
	    typename _Visitor = DfsVisitor<_Digraph>,
1215 1215
	    typename _Traits = DfsDefaultTraits<_Digraph> >
1216 1216
#endif
1217 1217
  class DfsVisit {
1218 1218
  public:
1219 1219
    
1220 1220
    /// \brief \ref Exception for uninitialized parameters.
1221 1221
    ///
1222 1222
    /// This error represents problems in the initialization
1223 1223
    /// of the parameters of the algorithms.
1224 1224
    class UninitializedParameter : public lemon::UninitializedParameter {
1225 1225
    public:
1226 1226
      virtual const char* what() const throw() 
1227 1227
      {
1228 1228
	return "lemon::DfsVisit::UninitializedParameter";
1229 1229
      }
1230 1230
    };
1231 1231

	
1232 1232
    typedef _Traits Traits;
1233 1233

	
1234 1234
    typedef typename Traits::Digraph Digraph;
1235 1235

	
1236 1236
    typedef _Visitor Visitor;
1237 1237

	
1238 1238
    ///The type of the map indicating which nodes are reached.
1239 1239
    typedef typename Traits::ReachedMap ReachedMap;
1240 1240

	
1241 1241
  private:
1242 1242

	
1243 1243
    typedef typename Digraph::Node Node;
1244 1244
    typedef typename Digraph::NodeIt NodeIt;
1245 1245
    typedef typename Digraph::Arc Arc;
1246 1246
    typedef typename Digraph::OutArcIt OutArcIt;
1247 1247

	
1248 1248
    /// Pointer to the underlying digraph.
1249 1249
    const Digraph *_digraph;
1250 1250
    /// Pointer to the visitor object.
1251 1251
    Visitor *_visitor;
1252 1252
    ///Pointer to the map of reached status of the nodes.
1253 1253
    ReachedMap *_reached;
1254 1254
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
1255 1255
    bool local_reached;
1256 1256

	
1257 1257
    std::vector<typename Digraph::Arc> _stack;
1258 1258
    int _stack_head;
1259 1259

	
1260 1260
    /// \brief Creates the maps if necessary.
1261 1261
    ///
1262 1262
    /// Creates the maps if necessary.
1263 1263
    void create_maps() {
1264 1264
      if(!_reached) {
1265 1265
	local_reached = true;
1266 1266
	_reached = Traits::createReachedMap(*_digraph);
1267 1267
      }
1268 1268
    }
1269 1269

	
1270 1270
  protected:
1271 1271

	
1272 1272
    DfsVisit() {}
1273 1273
    
1274 1274
  public:
1275 1275

	
1276 1276
    typedef DfsVisit Create;
1277 1277

	
1278 1278
    /// \name Named template parameters
1279 1279

	
1280 1280
    ///@{
1281 1281
    template <class T>
1282 1282
    struct DefReachedMapTraits : public Traits {
1283 1283
      typedef T ReachedMap;
1284 1284
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1285 1285
	throw UninitializedParameter();
1286 1286
      }
1287 1287
    };
1288 1288
    /// \brief \ref named-templ-param "Named parameter" for setting 
1289 1289
    /// ReachedMap type
1290 1290
    ///
1291 1291
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
1292 1292
    template <class T>
1293 1293
    struct DefReachedMap : public DfsVisit< Digraph, Visitor,
1294 1294
					    DefReachedMapTraits<T> > {
1295 1295
      typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
1296 1296
    };
1297 1297
    ///@}
1298 1298

	
1299 1299
  public:      
1300 1300
    
1301 1301
    /// \brief Constructor.
1302 1302
    ///
1303 1303
    /// Constructor.
1304 1304
    ///
1305 1305
    /// \param digraph the digraph the algorithm will run on.
1306 1306
    /// \param visitor The visitor of the algorithm.
1307 1307
    ///
1308 1308
    DfsVisit(const Digraph& digraph, Visitor& visitor) 
1309 1309
      : _digraph(&digraph), _visitor(&visitor),
1310 1310
	_reached(0), local_reached(false) {}
1311 1311
    
1312 1312
    /// \brief Destructor.
1313 1313
    ///
1314 1314
    /// Destructor.
1315 1315
    ~DfsVisit() {
1316 1316
      if(local_reached) delete _reached;
1317 1317
    }
1318 1318

	
1319 1319
    /// \brief Sets the map indicating if a node is reached.
1320 1320
    ///
1321 1321
    /// Sets the map indicating if a node is reached.
1322 1322
    /// If you don't use this function before calling \ref run(),
1323 1323
    /// it will allocate one. The destuctor deallocates this
1324 1324
    /// automatically allocated map, of course.
1325 1325
    /// \return <tt> (*this) </tt>
1326 1326
    DfsVisit &reachedMap(ReachedMap &m) {
1327 1327
      if(local_reached) {
1328 1328
	delete _reached;
1329 1329
	local_reached=false;
1330 1330
      }
1331 1331
      _reached = &m;
1332 1332
      return *this;
1333 1333
    }
1334 1334

	
1335 1335
  public:
1336 1336
    /// \name Execution control
1337 1337
    /// The simplest way to execute the algorithm is to use
1338 1338
    /// one of the member functions called \c run(...).
1339 1339
    /// \n
1340 1340
    /// If you need more control on the execution,
1341 1341
    /// first you must call \ref init(), then you can adda source node
1342 1342
    /// with \ref addSource().
1343 1343
    /// Finally \ref start() will perform the actual path
1344 1344
    /// computation.
1345 1345

	
1346 1346
    /// @{
1347 1347
    /// \brief Initializes the internal data structures.
1348 1348
    ///
1349 1349
    /// Initializes the internal data structures.
1350 1350
    ///
1351 1351
    void init() {
1352 1352
      create_maps();
1353 1353
      _stack.resize(countNodes(*_digraph));
1354 1354
      _stack_head = -1;
1355 1355
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1356 1356
	_reached->set(u, false);
1357 1357
      }
1358 1358
    }
1359 1359
    
1360 1360
    /// \brief Adds a new source node.
1361 1361
    ///
1362 1362
    /// Adds a new source node to the set of nodes to be processed.
1363 1363
    void addSource(Node s) {
1364 1364
      if(!(*_reached)[s]) {
1365 1365
	  _reached->set(s,true);
1366 1366
	  _visitor->start(s);
1367 1367
	  _visitor->reach(s);
1368 1368
	  Arc e; 
1369 1369
	  _digraph->firstOut(e, s);
1370 1370
	  if (e != INVALID) {
1371 1371
	    _stack[++_stack_head] = e;
1372 1372
	  } else {
1373 1373
	    _visitor->leave(s);
1374 1374
	  }
1375 1375
	}
1376 1376
    }
1377 1377
    
1378 1378
    /// \brief Processes the next arc.
1379 1379
    ///
1380 1380
    /// Processes the next arc.
1381 1381
    ///
1382 1382
    /// \return The processed arc.
1383 1383
    ///
1384 1384
    /// \pre The stack must not be empty!
1385 1385
    Arc processNextArc() { 
1386 1386
      Arc e = _stack[_stack_head];
1387 1387
      Node m = _digraph->target(e);
1388 1388
      if(!(*_reached)[m]) {
1389 1389
	_visitor->discover(e);
1390 1390
	_visitor->reach(m);
1391 1391
	_reached->set(m, true);
1392 1392
	_digraph->firstOut(_stack[++_stack_head], m);
1393 1393
      } else {
1394 1394
	_visitor->examine(e);
1395 1395
	m = _digraph->source(e);
1396 1396
	_digraph->nextOut(_stack[_stack_head]);
1397 1397
      }
1398 1398
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1399 1399
	_visitor->leave(m);
1400 1400
	--_stack_head;
1401 1401
	if (_stack_head >= 0) {
1402 1402
	  _visitor->backtrack(_stack[_stack_head]);
1403 1403
	  m = _digraph->source(_stack[_stack_head]);
1404 1404
	  _digraph->nextOut(_stack[_stack_head]);
1405 1405
	} else {
1406 1406
	  _visitor->stop(m);	  
1407 1407
	}
1408 1408
      }
1409 1409
      return e;
1410 1410
    }
1411 1411

	
1412 1412
    /// \brief Next arc to be processed.
1413 1413
    ///
1414 1414
    /// Next arc to be processed.
1415 1415
    ///
1416 1416
    /// \return The next arc to be processed or INVALID if the stack is
1417 1417
    /// empty.
1418 1418
    Arc nextArc() { 
1419 1419
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1420 1420
    }
1421 1421

	
1422 1422
    /// \brief Returns \c false if there are nodes
1423 1423
    /// to be processed in the queue
1424 1424
    ///
1425 1425
    /// Returns \c false if there are nodes
1426 1426
    /// to be processed in the queue
1427 1427
    bool emptyQueue() { return _stack_head < 0; }
1428 1428

	
1429 1429
    /// \brief Returns the number of the nodes to be processed.
1430 1430
    ///
1431 1431
    /// Returns the number of the nodes to be processed in the queue.
1432 1432
    int queueSize() { return _stack_head + 1; }
1433 1433
    
1434 1434
    /// \brief Executes the algorithm.
1435 1435
    ///
1436 1436
    /// Executes the algorithm.
1437 1437
    ///
1438 1438
    /// \pre init() must be called and at least one node should be added
1439 1439
    /// with addSource() before using this function.
1440 1440
    void start() {
1441 1441
      while ( !emptyQueue() ) processNextArc();
1442 1442
    }
1443 1443
    
1444 1444
    /// \brief Executes the algorithm until \c dest is reached.
1445 1445
    ///
1446 1446
    /// Executes the algorithm until \c dest is reached.
1447 1447
    ///
1448 1448
    /// \pre init() must be called and at least one node should be added
1449 1449
    /// with addSource() before using this function.
1450 1450
    void start(Node dest) {
1451 1451
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) 
1452 1452
	processNextArc();
1453 1453
    }
1454 1454
    
1455 1455
    /// \brief Executes the algorithm until a condition is met.
1456 1456
    ///
1457 1457
    /// Executes the algorithm until a condition is met.
1458 1458
    ///
1459 1459
    /// \pre init() must be called and at least one node should be added
1460 1460
    /// with addSource() before using this function.
1461 1461
    ///
1462 1462
    /// \param em must be a bool (or convertible) arc map. The algorithm
1463 1463
    /// will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
1464 1464
    ///
1465 1465
    ///\return The reached arc \c e with <tt>em[e]</tt> true or
1466 1466
    ///\c INVALID if no such arc was found.
1467 1467
    ///
1468 1468
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
1469 1469
    /// not a node map.
1470 1470
    template <typename EM>
1471 1471
    Arc start(const EM &em) {
1472 1472
      while ( !emptyQueue() && !em[_stack[_stack_head]] )
1473 1473
        processNextArc();
1474 1474
      return emptyQueue() ? INVALID : _stack[_stack_head];
1475 1475
    }
1476 1476

	
1477 1477
    /// \brief Runs %DFSVisit algorithm from node \c s.
1478 1478
    ///
1479 1479
    /// This method runs the %DFS algorithm from a root node \c s.
1480 1480
    /// \note d.run(s) is just a shortcut of the following code.
1481 1481
    ///\code
1482 1482
    ///   d.init();
1483 1483
    ///   d.addSource(s);
1484 1484
    ///   d.start();
1485 1485
    ///\endcode
1486 1486
    void run(Node s) {
1487 1487
      init();
1488 1488
      addSource(s);
1489 1489
      start();
1490 1490
    }
1491 1491

	
1492 1492
    /// \brief Runs %DFSVisit algorithm to visit all nodes in the digraph.
1493 1493
    
1494 1494
    /// This method runs the %DFS algorithm in order to
1495 1495
    /// compute the %DFS path to each node. The algorithm computes
1496 1496
    /// - The %DFS tree.
1497 1497
    /// - The distance of each node from the root in the %DFS tree.
1498 1498
    ///
1499 1499
    ///\note d.run() is just a shortcut of the following code.
1500 1500
    ///\code
1501 1501
    ///  d.init();
1502 1502
    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
1503 1503
    ///    if (!d.reached(it)) {
1504 1504
    ///      d.addSource(it);
1505 1505
    ///      d.start();
1506 1506
    ///    }
1507 1507
    ///  }
1508 1508
    ///\endcode
1509 1509
    void run() {
1510 1510
      init();
1511 1511
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1512 1512
        if (!reached(it)) {
1513 1513
          addSource(it);
1514 1514
          start();
1515 1515
        }
1516 1516
      }
1517 1517
    }
1518 1518
    ///@}
1519 1519

	
1520 1520
    /// \name Query Functions
1521 1521
    /// The result of the %DFS algorithm can be obtained using these
1522 1522
    /// functions.\n
1523 1523
    /// Before the use of these functions,
1524 1524
    /// either run() or start() must be called.
1525 1525
    ///@{
1526 1526
    /// \brief Checks if a node is reachable from the root.
1527 1527
    ///
1528 1528
    /// Returns \c true if \c v is reachable from the root(s).
1529 1529
    /// \warning The source nodes are inditated as unreachable.
1530 1530
    /// \pre Either \ref run() or \ref start()
1531 1531
    /// must be called before using this function.
1532 1532
    ///
1533 1533
    bool reached(Node v) { return (*_reached)[v]; }
1534 1534
    ///@}
1535 1535
  };
0 comments (0 inline)