Ticket #96: dijk_4a3bfa4577a5.patch
File dijk_4a3bfa4577a5.patch, 37.9 KB (added by , 16 years ago) |
---|
-
lemon/bfs.h
# HG changeset patch # User Peter Kovacs <kpeter@inf.elte.hu> # Date 1221731815 -7200 # Node ID 4a3bfa4577a506eae6bdafa3017458a2a6a328ce # Parent c691064dfd4f2e3d4e230a1fd9845a1d96de2a5d Modify the function interface of bfs, dfs, and dijkstra (ticket #96) - Make the source node a mandatory argument of bfs()/dfs()/dijkstra(). - Support s-t searches with function interface. - Set NodeMap<T> instead of NullMap as PredMap and DistMap in the default traits classes. - Modify the related test files. - Doc improvements. - Bug fix in concepts/path.h. diff --git a/lemon/bfs.h b/lemon/bfs.h
a b 28 28 #include <lemon/core.h> 29 29 #include <lemon/error.h> 30 30 #include <lemon/maps.h> 31 #include <lemon/path.h> 31 32 32 33 namespace lemon { 33 34 … … 841 842 ///The type of the map that stores the predecessor 842 843 ///arcs of the shortest paths. 843 844 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 844 typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;845 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 845 846 ///Instantiates a \ref PredMap. 846 847 847 848 ///This function instantiates a \ref PredMap. 848 849 ///\param g is the digraph, to which we would like to define the 849 850 ///\ref PredMap. 850 851 ///\todo The digraph alone may be insufficient to initialize 851 #ifdef DOXYGEN852 852 static PredMap *createPredMap(const Digraph &g) 853 #else854 static PredMap *createPredMap(const Digraph &)855 #endif856 853 { 857 return new PredMap( );854 return new PredMap(g); 858 855 } 859 856 860 857 ///The type of the map that indicates which nodes are processed. 861 858 862 859 ///The type of the map that indicates which nodes are processed. 863 860 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 861 ///By default it is a NullMap. 864 862 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 865 863 ///Instantiates a \ref ProcessedMap. 866 864 … … 895 893 896 894 ///The type of the map that stores the distances of the nodes. 897 895 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 898 /// 899 typedef NullMap<typename Digraph::Node,int> DistMap; 896 typedef typename Digraph::template NodeMap<int> DistMap; 900 897 ///Instantiates a \ref DistMap. 901 898 902 899 ///This function instantiates a \ref DistMap. 903 900 ///\param g is the digraph, to which we would like to define 904 901 ///the \ref DistMap 905 #ifdef DOXYGEN906 902 static DistMap *createDistMap(const Digraph &g) 907 #else908 static DistMap *createDistMap(const Digraph &)909 #endif910 903 { 911 return new DistMap( );904 return new DistMap(g); 912 905 } 906 907 ///The type of the shortest paths. 908 909 ///The type of the shortest paths. 910 ///It must meet the \ref concepts::Path "Path" concept. 911 typedef lemon::Path<Digraph> Path; 913 912 }; 914 913 915 914 /// Default traits class used by \ref BfsWizard … … 939 938 void *_pred; 940 939 //Pointer to the map of distances. 941 940 void *_dist; 942 //Pointer to the source node. 941 //Pointer to the shortest path to the target node. 942 void *_path; 943 //Pointer to the distance of the target node. 944 int *_di; 945 //The source node. 943 946 Node _source; 947 //The the target node. 948 Node _target; 944 949 945 950 public: 946 951 /// Constructor. … … 948 953 /// This constructor does not require parameters, therefore it initiates 949 954 /// all of the attributes to default values (0, INVALID). 950 955 BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), 951 _dist(0), _source(INVALID) {} 956 _dist(0), _path(0), _di(0), 957 _source(INVALID), _target(INVALID) {} 952 958 953 959 /// Constructor. 954 960 … … 957 963 /// Others are initiated to 0. 958 964 /// \param g The digraph the algorithm runs on. 959 965 /// \param s The source node. 960 BfsWizardBase(const GR &g, Node s=INVALID) : 966 /// \param t The optional target node. 967 BfsWizardBase(const GR &g, Node s, Node t=INVALID) : 961 968 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 962 _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} 969 _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0), 970 _source(s), _target(t) {} 963 971 964 972 }; 965 973 … … 1006 1014 typedef typename TR::ReachedMap ReachedMap; 1007 1015 ///\brief The type of the map that indicates which nodes are processed. 1008 1016 typedef typename TR::ProcessedMap ProcessedMap; 1017 ///The type of the shortest paths 1018 typedef typename TR::Path Path; 1009 1019 1010 1020 public: 1011 1021 … … 1016 1026 1017 1027 /// Constructor that requires parameters. 1018 1028 /// These parameters will be the default values for the traits class. 1019 BfsWizard(const Digraph &g, Node s=INVALID) : 1020 TR(g,s) {} 1029 /// \param g The digraph the algorithm runs on. 1030 /// \param s The source node. 1031 /// \param t The optional target node. 1032 BfsWizard(const Digraph &g, Node s, Node t=INVALID) : 1033 TR(g,s,t) {} 1021 1034 1022 1035 ///Copy constructor 1023 1036 BfsWizard(const TR &b) : TR(b) {} 1024 1037 1025 1038 ~BfsWizard() {} 1026 1039 1027 ///Runs BFS algorithm from asource node.1040 ///Runs BFS algorithm from the source node. 1028 1041 1029 ///Runs BFS algorithm from a source node. 1030 ///The node can be given with the \ref source() function. 1031 void run() 1042 ///This function runs BFS algorithm from the source node that 1043 ///was given in the constructor. 1044 ///If a target node is given, the function computes the shortest 1045 ///path to that node (it stops the search when it reaches that 1046 ///node), otherwise it computes the shortest path to each node. 1047 ///\param t The optional target node. 1048 ///\return \c true if no target node was given (so the function 1049 ///computed the shortest path to each reachable node) or if the 1050 ///given target node was reached. 1051 bool run(Node t=INVALID) 1032 1052 { 1053 if(t!=INVALID) Base::_target=t; 1033 1054 if(Base::_source==INVALID) throw UninitializedParameter(); 1034 1055 Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 1035 1056 if(Base::_reached) … … 1040 1061 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 1041 1062 if(Base::_dist) 1042 1063 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 1043 alg.run(Base::_source); 1044 } 1045 1046 ///Runs BFS algorithm from the given node. 1047 1048 ///Runs BFS algorithm from the given node. 1049 ///\param s is the given source. 1050 void run(Node s) 1051 { 1052 Base::_source=s; 1053 run(); 1054 } 1055 1056 /// Sets the source node, from which the Bfs algorithm runs. 1057 1058 /// Sets the source node, from which the Bfs algorithm runs. 1059 /// \param s is the source node. 1060 BfsWizard<TR> &source(Node s) 1061 { 1062 Base::_source=s; 1063 return *this; 1064 if(Base::_target!=INVALID) { 1065 alg.run(Base::_source, Base::_target); 1066 if (Base::_path) 1067 *reinterpret_cast<Path*>(Base::_path) = alg.path(Base::_target); 1068 if (Base::_di) 1069 *Base::_di = alg.dist(Base::_target); 1070 return alg.reached(Base::_target); 1071 } else { 1072 alg.run(Base::_source); 1073 return true; 1074 } 1064 1075 } 1065 1076 1066 1077 template<class T> … … 1100 1111 } 1101 1112 1102 1113 template<class T> 1114 struct SetDistMapBase : public Base { 1115 typedef T DistMap; 1116 static DistMap *createDistMap(const Digraph &) { return 0; }; 1117 SetDistMapBase(const TR &b) : TR(b) {} 1118 }; 1119 ///\brief \ref named-templ-param "Named parameter" 1120 ///for setting \ref DistMap object. 1121 /// 1122 /// \ref named-templ-param "Named parameter" 1123 ///for setting \ref DistMap object. 1124 template<class T> 1125 BfsWizard<SetDistMapBase<T> > distMap(const T &t) 1126 { 1127 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1128 return BfsWizard<SetDistMapBase<T> >(*this); 1129 } 1130 1131 template<class T> 1103 1132 struct SetProcessedMapBase : public Base { 1104 1133 typedef T ProcessedMap; 1105 1134 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; … … 1118 1147 } 1119 1148 1120 1149 template<class T> 1121 struct SetDistMapBase : public Base { 1122 typedef T DistMap; 1123 static DistMap *createDistMap(const Digraph &) { return 0; }; 1124 SetDistMapBase(const TR &b) : TR(b) {} 1150 struct SetPathBase : public Base { 1151 typedef T Path; 1152 SetPathBase(const TR &b) : TR(b) {} 1125 1153 }; 1126 1154 ///\brief \ref named-templ-param "Named parameter" 1127 ///for setting \ref DistMap object.1155 ///for getting the shortest path to the target node. 1128 1156 /// 1129 /// 1130 ///for setting \ref DistMap object.1157 ///\ref named-templ-param "Named parameter" 1158 ///for getting the shortest path to the target node. 1131 1159 template<class T> 1132 BfsWizard<Set DistMapBase<T> > distMap(const T &t)1160 BfsWizard<SetPathBase<T> > path(const T &t) 1133 1161 { 1134 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1135 return BfsWizard<SetDistMapBase<T> >(*this); 1162 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); 1163 return BfsWizard<SetPathBase<T> >(*this); 1164 } 1165 1166 ///\brief \ref named-templ-param "Named parameter" 1167 ///for getting the distance of the target node. 1168 /// 1169 ///\ref named-templ-param "Named parameter" 1170 ///for getting the distance of the target node. 1171 BfsWizard dist(const int &d) 1172 { 1173 Base::_di=const_cast<int*>(&d); 1174 return *this; 1136 1175 } 1137 1176 1138 1177 }; 1139 1178 1140 ///Function type interface for B fsalgorithm.1179 ///Function type interface for BFS algorithm. 1141 1180 1142 1181 /// \ingroup search 1143 ///Function type interface for B fsalgorithm.1182 ///Function type interface for BFS algorithm. 1144 1183 /// 1145 1184 ///This function also has several 1146 1185 ///\ref named-templ-func-param "named parameters", 1147 1186 ///they are declared as the members of class \ref BfsWizard. 1148 ///The following 1149 ///example shows how to use these parameters. 1187 ///The following examples show how to use these parameters. 1150 1188 ///\code 1151 /// bfs(g,source).predMap(preds).run(); 1189 /// // Compute shortest path to each node 1190 /// bfs(g,source).predMap(preds).distMap(dists).run(); 1191 /// 1192 /// // Compute shortest path to one node 1193 /// bool reached = bfs(g,source,target).path(p).dist(d).run(); 1152 1194 ///\endcode 1153 1195 ///\warning Don't forget to put the \ref BfsWizard::run() "run()" 1154 1196 ///to the end of the parameter list. … … 1156 1198 ///\sa Bfs 1157 1199 template<class GR> 1158 1200 BfsWizard<BfsWizardBase<GR> > 1159 bfs(const GR &g,typename GR::Node s=INVALID) 1201 bfs(const GR &digraph, 1202 typename GR::Node source, typename GR::Node target=INVALID) 1160 1203 { 1161 return BfsWizard<BfsWizardBase<GR> >( g,s);1204 return BfsWizard<BfsWizardBase<GR> >(digraph,source,target); 1162 1205 } 1163 1206 1164 1207 #ifdef DOXYGEN -
lemon/concepts/path.h
diff --git a/lemon/concepts/path.h b/lemon/concepts/path.h
a b 66 66 67 67 /// \brief Template assigment 68 68 template <typename CPath> 69 Path& operator=(const CPath& cpath) {} 69 Path& operator=(const CPath& cpath) { 70 ignore_unused_variable_warning(cpath); 71 return *this; 72 } 70 73 71 74 /// Length of the path ie. the number of arcs in the path. 72 75 int length() const { return 0;} -
lemon/dfs.h
diff --git a/lemon/dfs.h b/lemon/dfs.h
a b 29 29 #include <lemon/error.h> 30 30 #include <lemon/assert.h> 31 31 #include <lemon/maps.h> 32 #include <lemon/path.h> 32 33 33 34 namespace lemon { 34 35 … … 775 776 ///The type of the map that stores the predecessor 776 777 ///arcs of the %DFS paths. 777 778 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 778 /// 779 typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; 779 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 780 780 ///Instantiates a \ref PredMap. 781 781 782 782 ///This function instantiates a \ref PredMap. 783 783 ///\param g is the digraph, to which we would like to define the 784 784 ///\ref PredMap. 785 785 ///\todo The digraph alone may be insufficient to initialize 786 #ifdef DOXYGEN787 786 static PredMap *createPredMap(const Digraph &g) 788 #else789 static PredMap *createPredMap(const Digraph &)790 #endif791 787 { 792 return new PredMap( );788 return new PredMap(g); 793 789 } 794 790 795 791 ///The type of the map that indicates which nodes are processed. 796 792 797 793 ///The type of the map that indicates which nodes are processed. 798 794 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 795 ///By default it is a NullMap. 799 796 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 800 797 ///Instantiates a \ref ProcessedMap. 801 798 … … 830 827 831 828 ///The type of the map that stores the distances of the nodes. 832 829 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 833 /// 834 typedef NullMap<typename Digraph::Node,int> DistMap; 830 typedef typename Digraph::template NodeMap<int> DistMap; 835 831 ///Instantiates a \ref DistMap. 836 832 837 833 ///This function instantiates a \ref DistMap. 838 834 ///\param g is the digraph, to which we would like to define 839 835 ///the \ref DistMap 840 #ifdef DOXYGEN841 836 static DistMap *createDistMap(const Digraph &g) 842 #else843 static DistMap *createDistMap(const Digraph &)844 #endif845 837 { 846 return new DistMap( );838 return new DistMap(g); 847 839 } 840 841 ///The type of the DFS paths. 842 843 ///The type of the DFS paths. 844 ///It must meet the \ref concepts::Path "Path" concept. 845 typedef lemon::Path<Digraph> Path; 848 846 }; 849 847 850 848 /// Default traits class used by \ref DfsWizard … … 874 872 void *_pred; 875 873 //Pointer to the map of distances. 876 874 void *_dist; 877 //Pointer to the source node. 875 //Pointer to the DFS path to the target node. 876 void *_path; 877 //Pointer to the distance of the target node. 878 int *_di; 879 //The source node. 878 880 Node _source; 881 //The the target node. 882 Node _target; 879 883 880 884 public: 881 885 /// Constructor. … … 883 887 /// This constructor does not require parameters, therefore it initiates 884 888 /// all of the attributes to default values (0, INVALID). 885 889 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), 886 _dist(0), _source(INVALID) {} 890 _dist(0), _path(0), _di(0), 891 _source(INVALID), _target(INVALID) {} 887 892 888 893 /// Constructor. 889 894 … … 892 897 /// Others are initiated to 0. 893 898 /// \param g The digraph the algorithm runs on. 894 899 /// \param s The source node. 895 DfsWizardBase(const GR &g, Node s=INVALID) : 900 /// \param t The optional target node. 901 DfsWizardBase(const GR &g, Node s, Node t=INVALID) : 896 902 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 897 _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} 903 _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0), 904 _source(s), _target(t) {} 898 905 899 906 }; 900 907 … … 933 940 typedef typename Digraph::OutArcIt OutArcIt; 934 941 935 942 ///\brief The type of the map that stores the predecessor 936 ///arcs of the shortestpaths.943 ///arcs of the DFS paths. 937 944 typedef typename TR::PredMap PredMap; 938 945 ///\brief The type of the map that stores the distances of the nodes. 939 946 typedef typename TR::DistMap DistMap; … … 941 948 typedef typename TR::ReachedMap ReachedMap; 942 949 ///\brief The type of the map that indicates which nodes are processed. 943 950 typedef typename TR::ProcessedMap ProcessedMap; 951 ///The type of the DFS paths 952 typedef typename TR::Path Path; 944 953 945 954 public: 946 955 … … 951 960 952 961 /// Constructor that requires parameters. 953 962 /// These parameters will be the default values for the traits class. 954 DfsWizard(const Digraph &g, Node s=INVALID) : 955 TR(g,s) {} 963 /// \param g The digraph the algorithm runs on. 964 /// \param s The source node. 965 /// \param t The optional target node. 966 DfsWizard(const Digraph &g, Node s, Node t=INVALID) : 967 TR(g,s,t) {} 956 968 957 969 ///Copy constructor 958 970 DfsWizard(const TR &b) : TR(b) {} 959 971 960 972 ~DfsWizard() {} 961 973 962 ///Runs DFS algorithm from asource node.974 ///Runs DFS algorithm from the source node. 963 975 964 ///Runs DFS algorithm from a source node. 965 ///The node can be given with the \ref source() function. 966 void run() 976 ///This function runs DFS algorithm from the source node that 977 ///was given in the constructor. 978 ///If a target node is given, the function computes the DFS 979 ///path to that node (it stops the search when it reaches that 980 ///node), otherwise it computes the DFS path to each node. 981 ///\param t The optional target node. 982 ///\return \c true if no target node was given (so the function 983 ///computed the DFS path to each reachable node) or if the 984 ///given target node was reached. 985 bool run(Node t=INVALID) 967 986 { 987 if(t!=INVALID) Base::_target=t; 968 988 if(Base::_source==INVALID) throw UninitializedParameter(); 969 989 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 970 990 if(Base::_reached) … … 975 995 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 976 996 if(Base::_dist) 977 997 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 978 alg.run(Base::_source); 979 } 980 981 ///Runs DFS algorithm from the given node. 982 983 ///Runs DFS algorithm from the given node. 984 ///\param s is the given source. 985 void run(Node s) 986 { 987 Base::_source=s; 988 run(); 989 } 990 991 /// Sets the source node, from which the Dfs algorithm runs. 992 993 /// Sets the source node, from which the Dfs algorithm runs. 994 /// \param s is the source node. 995 DfsWizard<TR> &source(Node s) 996 { 997 Base::_source=s; 998 return *this; 998 if(Base::_target!=INVALID) { 999 alg.run(Base::_source, Base::_target); 1000 if (Base::_path) 1001 *reinterpret_cast<Path*>(Base::_path) = alg.path(Base::_target); 1002 if (Base::_di) 1003 *Base::_di = alg.dist(Base::_target); 1004 return alg.reached(Base::_target); 1005 } else { 1006 alg.run(Base::_source); 1007 return true; 1008 } 999 1009 } 1000 1010 1001 1011 template<class T> … … 1035 1045 } 1036 1046 1037 1047 template<class T> 1048 struct SetDistMapBase : public Base { 1049 typedef T DistMap; 1050 static DistMap *createDistMap(const Digraph &) { return 0; }; 1051 SetDistMapBase(const TR &b) : TR(b) {} 1052 }; 1053 ///\brief \ref named-templ-param "Named parameter" 1054 ///for setting \ref DistMap object. 1055 /// 1056 ///\ref named-templ-param "Named parameter" 1057 ///for setting \ref DistMap object. 1058 template<class T> 1059 DfsWizard<SetDistMapBase<T> > distMap(const T &t) 1060 { 1061 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1062 return DfsWizard<SetDistMapBase<T> >(*this); 1063 } 1064 1065 template<class T> 1038 1066 struct SetProcessedMapBase : public Base { 1039 1067 typedef T ProcessedMap; 1040 1068 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; … … 1053 1081 } 1054 1082 1055 1083 template<class T> 1056 struct SetDistMapBase : public Base { 1057 typedef T DistMap; 1058 static DistMap *createDistMap(const Digraph &) { return 0; }; 1059 SetDistMapBase(const TR &b) : TR(b) {} 1084 struct SetPathBase : public Base { 1085 typedef T Path; 1086 SetPathBase(const TR &b) : TR(b) {} 1060 1087 }; 1061 1088 ///\brief \ref named-templ-param "Named parameter" 1062 ///for setting \ref DistMap object.1089 ///for getting the DFS path to the target node. 1063 1090 /// 1064 1091 ///\ref named-templ-param "Named parameter" 1065 ///for setting \ref DistMap object.1092 ///for getting the DFS path to the target node. 1066 1093 template<class T> 1067 DfsWizard<Set DistMapBase<T> > distMap(const T &t)1094 DfsWizard<SetPathBase<T> > path(const T &t) 1068 1095 { 1069 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1070 return DfsWizard<SetDistMapBase<T> >(*this); 1096 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); 1097 return DfsWizard<SetPathBase<T> >(*this); 1098 } 1099 1100 ///\brief \ref named-templ-param "Named parameter" 1101 ///for getting the distance of the target node. 1102 /// 1103 ///\ref named-templ-param "Named parameter" 1104 ///for getting the distance of the target node. 1105 DfsWizard dist(const int &d) 1106 { 1107 Base::_di=const_cast<int*>(&d); 1108 return *this; 1071 1109 } 1072 1110 1073 1111 }; 1074 1112 1075 ///Function type interface for D fsalgorithm.1113 ///Function type interface for DFS algorithm. 1076 1114 1077 1115 ///\ingroup search 1078 ///Function type interface for D fsalgorithm.1116 ///Function type interface for DFS algorithm. 1079 1117 /// 1080 1118 ///This function also has several 1081 1119 ///\ref named-templ-func-param "named parameters", 1082 1120 ///they are declared as the members of class \ref DfsWizard. 1083 ///The following 1084 ///example shows how to use these parameters. 1121 ///The following examples show how to use these parameters. 1085 1122 ///\code 1086 /// dfs(g,source).predMap(preds).run(); 1123 /// // Compute the DFS tree 1124 /// dfs(g,source).predMap(preds).distMap(dists).run(); 1125 /// 1126 /// // Compute the DFS path to one node 1127 /// bool reached = dfs(g,source,target).path(p).dist(d).run(); 1087 1128 ///\endcode 1129 1088 1130 ///\warning Don't forget to put the \ref DfsWizard::run() "run()" 1089 1131 ///to the end of the parameter list. 1090 1132 ///\sa DfsWizard 1091 1133 ///\sa Dfs 1092 1134 template<class GR> 1093 1135 DfsWizard<DfsWizardBase<GR> > 1094 dfs(const GR &g,typename GR::Node s=INVALID) 1136 dfs(const GR &digraph, 1137 typename GR::Node source, typename GR::Node target=INVALID) 1095 1138 { 1096 return DfsWizard<DfsWizardBase<GR> >( g,s);1139 return DfsWizard<DfsWizardBase<GR> >(digraph,source,target); 1097 1140 } 1098 1141 1099 1142 #ifdef DOXYGEN -
lemon/dijkstra.h
diff --git a/lemon/dijkstra.h b/lemon/dijkstra.h
a b 30 30 #include <lemon/core.h> 31 31 #include <lemon/error.h> 32 32 #include <lemon/maps.h> 33 #include <lemon/path.h> 33 34 34 35 namespace lemon { 35 36 … … 987 988 ///The type of the map that stores the predecessor 988 989 ///arcs of the shortest paths. 989 990 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 990 typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;991 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 991 992 ///Instantiates a \ref PredMap. 992 993 993 994 ///This function instantiates a \ref PredMap. 994 995 ///\param g is the digraph, to which we would like to define the 995 996 ///\ref PredMap. 996 997 ///\todo The digraph alone may be insufficient to initialize 997 #ifdef DOXYGEN998 998 static PredMap *createPredMap(const Digraph &g) 999 #else1000 static PredMap *createPredMap(const Digraph &)1001 #endif1002 999 { 1003 return new PredMap( );1000 return new PredMap(g); 1004 1001 } 1005 1002 1006 1003 ///The type of the map that indicates which nodes are processed. … … 1030 1027 1031 1028 ///The type of the map that stores the distances of the nodes. 1032 1029 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 1033 typedef NullMap<typename Digraph::Node,Value> DistMap;1030 typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; 1034 1031 ///Instantiates a \ref DistMap. 1035 1032 1036 1033 ///This function instantiates a \ref DistMap. 1037 1034 ///\param g is the digraph, to which we would like to define 1038 1035 ///the \ref DistMap 1039 #ifdef DOXYGEN1040 1036 static DistMap *createDistMap(const Digraph &g) 1041 #else1042 static DistMap *createDistMap(const Digraph &)1043 #endif1044 1037 { 1045 return new DistMap( );1038 return new DistMap(g); 1046 1039 } 1040 1041 ///The type of the shortest paths. 1042 1043 ///The type of the shortest paths. 1044 ///It must meet the \ref concepts::Path "Path" concept. 1045 typedef lemon::Path<Digraph> Path; 1047 1046 }; 1048 1047 1049 1048 /// Default traits class used by \ref DijkstraWizard … … 1065 1064 1066 1065 //Pointer to the digraph the algorithm runs on. 1067 1066 void *_g; 1068 //Pointer to the length map 1067 //Pointer to the length map. 1069 1068 void *_length; 1070 1069 //Pointer to the map of processed nodes. 1071 1070 void *_processed; … … 1073 1072 void *_pred; 1074 1073 //Pointer to the map of distances. 1075 1074 void *_dist; 1076 //Pointer to the source node. 1075 //Pointer to the shortest path to the target node. 1076 void *_path; 1077 //Pointer to the distance of the target node. 1078 void *_di; 1079 //The source node. 1077 1080 Node _source; 1081 //The target node. 1082 Node _target; 1078 1083 1079 1084 public: 1080 1085 /// Constructor. … … 1082 1087 /// This constructor does not require parameters, therefore it initiates 1083 1088 /// all of the attributes to default values (0, INVALID). 1084 1089 DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), 1085 _dist(0), _source(INVALID) {} 1090 _dist(0), _path(0), _di(0), 1091 _source(INVALID), _target(INVALID) {} 1086 1092 1087 1093 /// Constructor. 1088 1094 … … 1092 1098 /// \param g The digraph the algorithm runs on. 1093 1099 /// \param l The length map. 1094 1100 /// \param s The source node. 1095 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : 1101 /// \param t The optional target node. 1102 DijkstraWizardBase(const GR &g,const LM &l, Node s, Node t=INVALID) : 1096 1103 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 1097 1104 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), 1098 _processed(0), _pred(0), _dist(0), _source(s) {} 1105 _processed(0), _pred(0), _dist(0), _path(0), _di(0), 1106 _source(s), _target(t) {} 1099 1107 1100 1108 }; 1101 1109 … … 1144 1152 typedef typename TR::DistMap DistMap; 1145 1153 ///The type of the map that indicates which nodes are processed. 1146 1154 typedef typename TR::ProcessedMap ProcessedMap; 1155 ///The type of the shortest paths 1156 typedef typename TR::Path Path; 1147 1157 ///The heap type used by the dijkstra algorithm. 1148 1158 typedef typename TR::Heap Heap; 1149 1159 … … 1156 1166 1157 1167 /// Constructor that requires parameters. 1158 1168 /// These parameters will be the default values for the traits class. 1159 DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : 1160 TR(g,l,s) {} 1169 /// \param g The digraph the algorithm runs on. 1170 /// \param l The length map. 1171 /// \param s The source node. 1172 /// \param t The optional target node. 1173 DijkstraWizard(const Digraph &g, const LengthMap &l, 1174 Node s, Node t=INVALID) : 1175 TR(g,l,s,t) {} 1161 1176 1162 1177 ///Copy constructor 1163 1178 DijkstraWizard(const TR &b) : TR(b) {} 1164 1179 1165 1180 ~DijkstraWizard() {} 1166 1181 1167 ///Runs Dijkstra algorithm from asource node.1182 ///Runs Dijkstra algorithm from the source node. 1168 1183 1169 ///Runs Dijkstra algorithm from a source node. 1170 ///The node can be given with the \ref source() function. 1171 void run() 1184 ///This function runs Dijkstra algorithm from the source node that 1185 ///was given in the constructor. 1186 ///If a target node is given, the function computes the shortest 1187 ///path to that node (it stops the search when it processes that 1188 ///node), otherwise it computes the shortest path to each node. 1189 ///\param t The optional target node. 1190 ///\return \c true if no target node was given (so the function 1191 ///computed the shortest path to each reachable node) or if the 1192 ///given target node was reached. 1193 bool run(Node t=INVALID) 1172 1194 { 1195 if(t!=INVALID) Base::_target=t; 1173 1196 if(Base::_source==INVALID) throw UninitializedParameter(); 1174 1197 Dijkstra<Digraph,LengthMap,TR> 1175 dij (*reinterpret_cast<const Digraph*>(Base::_g),1176 *reinterpret_cast<const LengthMap*>(Base::_length));1198 dijk(*reinterpret_cast<const Digraph*>(Base::_g), 1199 *reinterpret_cast<const LengthMap*>(Base::_length)); 1177 1200 if(Base::_processed) 1178 dij .processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));1201 dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); 1179 1202 if(Base::_pred) 1180 dij .predMap(*reinterpret_cast<PredMap*>(Base::_pred));1203 dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 1181 1204 if(Base::_dist) 1182 dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 1183 dij.run(Base::_source); 1184 } 1185 1186 ///Runs Dijkstra algorithm from the given node. 1187 1188 ///Runs Dijkstra algorithm from the given node. 1189 ///\param s is the given source. 1190 void run(Node s) 1191 { 1192 Base::_source=s; 1193 run(); 1194 } 1195 1196 /// Sets the source node, from which the Dijkstra algorithm runs. 1197 1198 /// Sets the source node, from which the Dijkstra algorithm runs. 1199 /// \param s is the source node. 1200 DijkstraWizard<TR> &source(Node s) 1201 { 1202 Base::_source=s; 1203 return *this; 1205 dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 1206 if(Base::_target!=INVALID) { 1207 dijk.run(Base::_source, Base::_target); 1208 if (Base::_path) 1209 *reinterpret_cast<Path*>(Base::_path) = dijk.path(Base::_target); 1210 if (Base::_di) 1211 *reinterpret_cast<Value*>(Base::_di) = dijk.dist(Base::_target); 1212 return dijk.reached(Base::_target); 1213 } else { 1214 dijk.run(Base::_source); 1215 return true; 1216 } 1204 1217 } 1205 1218 1206 1219 template<class T> … … 1222 1235 } 1223 1236 1224 1237 template<class T> 1238 struct SetDistMapBase : public Base { 1239 typedef T DistMap; 1240 static DistMap *createDistMap(const Digraph &) { return 0; }; 1241 SetDistMapBase(const TR &b) : TR(b) {} 1242 }; 1243 ///\brief \ref named-templ-param "Named parameter" 1244 ///for setting \ref DistMap object. 1245 /// 1246 ///\ref named-templ-param "Named parameter" 1247 ///for setting \ref DistMap object. 1248 template<class T> 1249 DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) 1250 { 1251 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1252 return DijkstraWizard<SetDistMapBase<T> >(*this); 1253 } 1254 1255 template<class T> 1225 1256 struct SetProcessedMapBase : public Base { 1226 1257 typedef T ProcessedMap; 1227 1258 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; … … 1240 1271 } 1241 1272 1242 1273 template<class T> 1243 struct SetDistMapBase : public Base { 1244 typedef T DistMap; 1245 static DistMap *createDistMap(const Digraph &) { return 0; }; 1246 SetDistMapBase(const TR &b) : TR(b) {} 1274 struct SetPathBase : public Base { 1275 typedef T Path; 1276 SetPathBase(const TR &b) : TR(b) {} 1247 1277 }; 1248 1278 ///\brief \ref named-templ-param "Named parameter" 1249 ///for setting \ref DistMap object.1279 ///for getting the shortest path to the target node. 1250 1280 /// 1251 1281 ///\ref named-templ-param "Named parameter" 1252 ///for setting \ref DistMap object.1282 ///for getting the shortest path to the target node. 1253 1283 template<class T> 1254 DijkstraWizard<Set DistMapBase<T> > distMap(const T &t)1284 DijkstraWizard<SetPathBase<T> > path(const T &t) 1255 1285 { 1256 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1257 return DijkstraWizard<SetDistMapBase<T> >(*this); 1286 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); 1287 return DijkstraWizard<SetPathBase<T> >(*this); 1288 } 1289 1290 ///\brief \ref named-templ-param "Named parameter" 1291 ///for getting the distance of the target node. 1292 /// 1293 ///\ref named-templ-param "Named parameter" 1294 ///for getting the distance of the target node. 1295 DijkstraWizard dist(const Value &d) 1296 { 1297 Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); 1298 return *this; 1258 1299 } 1259 1300 1260 1301 }; … … 1267 1308 ///This function also has several 1268 1309 ///\ref named-templ-func-param "named parameters", 1269 1310 ///they are declared as the members of class \ref DijkstraWizard. 1270 ///The following 1271 ///example shows how to use these parameters. 1311 ///The following examples show how to use these parameters. 1272 1312 ///\code 1273 /// dijkstra(g,length,source).predMap(preds).run(); 1313 /// // Compute shortest path to each node 1314 /// dijkstra(g,length,source).predMap(preds).distMap(dists).run(); 1315 /// 1316 /// // Compute shortest path to one node 1317 /// bool reached = dijkstra(g,length,source,target).path(p).dist(d).run(); 1274 1318 ///\endcode 1275 1319 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" 1276 1320 ///to the end of the parameter list. … … 1278 1322 ///\sa Dijkstra 1279 1323 template<class GR, class LM> 1280 1324 DijkstraWizard<DijkstraWizardBase<GR,LM> > 1281 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) 1325 dijkstra(const GR &digraph, const LM &length, 1326 typename GR::Node source, typename GR::Node target=INVALID) 1282 1327 { 1283 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); 1328 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length, 1329 source,target); 1284 1330 } 1285 1331 1286 1332 } //END OF NAMESPACE LEMON -
test/bfs_test.cc
diff --git a/test/bfs_test.cc b/test/bfs_test.cc
a b 62 62 bool b; 63 63 BType::DistMap d(G); 64 64 BType::PredMap p(G); 65 // BType::PredNodeMap pn(G);66 65 67 66 BType bfs_test(G); 68 67 … … 72 71 e = bfs_test.predArc(n); 73 72 n = bfs_test.predNode(n); 74 73 d = bfs_test.distMap(); 75 76 74 p = bfs_test.predMap(); 77 // pn = bfs_test.predNodeMap();78 75 b = bfs_test.reached(n); 79 76 80 77 Path<Digraph> pp = bfs_test.path(n); … … 89 86 90 87 Digraph g; 91 88 bfs(g,Node()).run(); 92 bfs(g).source(Node()).run(); 93 bfs(g) 94 .predMap(concepts::WriteMap<Node,Arc>()) 95 .distMap(concepts::WriteMap<Node,VType>()) 96 .reachedMap(concepts::ReadWriteMap<Node,bool>()) 89 bfs(g,Node()) 90 .predMap(concepts::ReadWriteMap<Node,Arc>()) 91 .distMap(concepts::ReadWriteMap<Node,VType>()) 97 92 .processedMap(concepts::WriteMap<Node,bool>()) 93 .run(); 94 bfs(g,Node(),Node()) 95 .predMap(concepts::ReadWriteMap<Node,Arc>()) 96 .distMap(concepts::ReadWriteMap<Node,VType>()) 97 .processedMap(concepts::WriteMap<Node,bool>()) 98 .path(concepts::Path<Digraph>()).dist(VType()) 99 .run(); 100 bfs(g,Node()) 101 .predMap(concepts::ReadWriteMap<Node,Arc>()) 102 .distMap(concepts::ReadWriteMap<Node,VType>()) 103 .processedMap(concepts::WriteMap<Node,bool>()) 104 .path(concepts::Path<Digraph>()).dist(VType()) 98 105 .run(Node()); 99 106 } 100 107 … … 114 121 Bfs<Digraph> bfs_test(G); 115 122 bfs_test.run(s); 116 123 117 check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));124 check(bfs_test.dist(t)==2,"Bfs found a wrong path."); 118 125 119 126 Path<Digraph> p = bfs_test.path(t); 120 127 check(p.length()==2,"path() found a wrong path."); … … 128 135 Node v=G.target(a); 129 136 check( !bfs_test.reached(u) || 130 137 (bfs_test.dist(v) <= bfs_test.dist(u)+1), 131 "Wrong output. " << G.id(v) << ' ' << G.id(u));138 "Wrong output. " << G.id(u) << "->" << G.id(v)); 132 139 } 133 140 134 141 for(NodeIt v(G); v!=INVALID; ++v) { … … 140 147 check(u==bfs_test.predNode(v),"Wrong tree."); 141 148 check(bfs_test.dist(v) - bfs_test.dist(u) == 1, 142 149 "Wrong distance. Difference: " 143 << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 144 - 1)); 150 << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); 145 151 } 146 152 } 153 } 154 155 { 156 NullMap<Node,Arc> myPredMap; 157 bfs(G,s).predMap(myPredMap).run(); 147 158 } 148 159 } 149 160 -
test/dfs_test.cc
diff --git a/test/dfs_test.cc b/test/dfs_test.cc
a b 89 89 90 90 Digraph g; 91 91 dfs(g,Node()).run(); 92 dfs(g).source(Node()).run(); 93 dfs(g) 94 .predMap(concepts::WriteMap<Node,Arc>()) 95 .distMap(concepts::WriteMap<Node,VType>()) 96 .reachedMap(concepts::ReadWriteMap<Node,bool>()) 92 dfs(g,Node()) 93 .predMap(concepts::ReadWriteMap<Node,Arc>()) 94 .distMap(concepts::ReadWriteMap<Node,VType>()) 97 95 .processedMap(concepts::WriteMap<Node,bool>()) 96 .run(); 97 dfs(g,Node(),Node()) 98 .predMap(concepts::ReadWriteMap<Node,Arc>()) 99 .distMap(concepts::ReadWriteMap<Node,VType>()) 100 .processedMap(concepts::WriteMap<Node,bool>()) 101 .path(concepts::Path<Digraph>()).dist(VType()) 102 .run(); 103 dfs(g,Node()) 104 .predMap(concepts::ReadWriteMap<Node,Arc>()) 105 .distMap(concepts::ReadWriteMap<Node,VType>()) 106 .processedMap(concepts::WriteMap<Node,bool>()) 107 .path(concepts::Path<Digraph>()).dist(VType()) 98 108 .run(Node()); 99 109 } 100 110 … … 129 139 check(u==dfs_test.predNode(v),"Wrong tree."); 130 140 check(dfs_test.dist(v) - dfs_test.dist(u) == 1, 131 141 "Wrong distance. (" << dfs_test.dist(u) << "->" 132 << dfs_test.dist(v) << ')');142 << dfs_test.dist(v) << ")"); 133 143 } 134 144 } 145 } 146 147 { 148 NullMap<Node,Arc> myPredMap; 149 dfs(G,s).predMap(myPredMap).run(); 135 150 } 136 151 } 137 152 -
test/dijkstra_test.cc
diff --git a/test/dijkstra_test.cc b/test/dijkstra_test.cc
a b 64 64 bool b; 65 65 DType::DistMap d(G); 66 66 DType::PredMap p(G); 67 // DType::PredNodeMap pn(G);68 67 LengthMap length; 69 68 70 69 DType dijkstra_test(G,length); … … 76 75 n = dijkstra_test.predNode(n); 77 76 d = dijkstra_test.distMap(); 78 77 p = dijkstra_test.predMap(); 79 // pn = dijkstra_test.predNodeMap();80 78 b = dijkstra_test.reached(n); 81 79 82 80 Path<Digraph> pp = dijkstra_test.path(n); … … 92 90 93 91 Digraph g; 94 92 dijkstra(g,LengthMap(),Node()).run(); 95 dijkstra(g,LengthMap()).source(Node()).run(); 96 dijkstra(g,LengthMap()) 97 .predMap(concepts::WriteMap<Node,Arc>()) 98 .distMap(concepts::WriteMap<Node,VType>()) 93 dijkstra(g,LengthMap(),Node()) 94 .predMap(concepts::ReadWriteMap<Node,Arc>()) 95 .distMap(concepts::ReadWriteMap<Node,VType>()) 96 .processedMap(concepts::WriteMap<Node,bool>()) 97 .run(); 98 dijkstra(g,LengthMap(),Node(),Node()) 99 .predMap(concepts::ReadWriteMap<Node,Arc>()) 100 .distMap(concepts::ReadWriteMap<Node,VType>()) 101 .processedMap(concepts::WriteMap<Node,bool>()) 102 .path(concepts::Path<Digraph>()).dist(VType()) 103 .run(); 104 dijkstra(g,LengthMap(),Node()) 105 .predMap(concepts::ReadWriteMap<Node,Arc>()) 106 .distMap(concepts::ReadWriteMap<Node,VType>()) 107 .processedMap(concepts::WriteMap<Node,bool>()) 108 .path(concepts::Path<Digraph>()).dist(VType()) 99 109 .run(Node()); 100 110 } 101 111 … … 122 132 check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path."); 123 133 124 134 Path<Digraph> p = dijkstra_test.path(t); 125 check(p.length()==3," getPath() found a wrong path.");135 check(p.length()==3,"path() found a wrong path."); 126 136 check(checkPath(G, p),"path() found a wrong path."); 127 137 check(pathSource(G, p) == s,"path() found a wrong path."); 128 138 check(pathTarget(G, p) == t,"path() found a wrong path."); … … 132 142 Node v=G.target(e); 133 143 check( !dijkstra_test.reached(u) || 134 144 (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), 135 " dist(target)-dist(source)-arc_length=" <<145 "Wrong output. dist(target)-dist(source)-arc_length=" << 136 146 dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); 137 147 } 138 148 … … 152 162 153 163 { 154 164 NullMap<Node,Arc> myPredMap; 155 dijkstra(G,length ).predMap(myPredMap).run(s);165 dijkstra(G,length,s).predMap(myPredMap).run(); 156 166 } 157 167 } 158 168