... | ... |
@@ -763,534 +763,534 @@ |
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:: |
|
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:: |
|
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> |
... | ... |
@@ -746,534 +746,534 @@ |
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:: |
|
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:: |
|
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 |
|
0 comments (0 inline)