1053 //The type of the nodes in the digraph. |
1052 //The type of the nodes in the digraph. |
1054 typedef typename Base::Digraph::Node Node; |
1053 typedef typename Base::Digraph::Node Node; |
1055 |
1054 |
1056 //Pointer to the digraph the algorithm runs on. |
1055 //Pointer to the digraph the algorithm runs on. |
1057 void *_g; |
1056 void *_g; |
1058 //Pointer to the length map |
1057 //Pointer to the length map. |
1059 void *_length; |
1058 void *_length; |
1060 //Pointer to the map of processed nodes. |
1059 //Pointer to the map of processed nodes. |
1061 void *_processed; |
1060 void *_processed; |
1062 //Pointer to the map of predecessors arcs. |
1061 //Pointer to the map of predecessors arcs. |
1063 void *_pred; |
1062 void *_pred; |
1064 //Pointer to the map of distances. |
1063 //Pointer to the map of distances. |
1065 void *_dist; |
1064 void *_dist; |
1066 //Pointer to the source node. |
1065 //Pointer to the shortest path to the target node. |
1067 Node _source; |
1066 void *_path; |
|
1067 //Pointer to the distance of the target node. |
|
1068 void *_di; |
1068 |
1069 |
1069 public: |
1070 public: |
1070 /// Constructor. |
1071 /// Constructor. |
1071 |
1072 |
1072 /// This constructor does not require parameters, therefore it initiates |
1073 /// This constructor does not require parameters, therefore it initiates |
1073 /// all of the attributes to default values (0, INVALID). |
1074 /// all of the attributes to \c 0. |
1074 DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1075 DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1075 _dist(0), _source(INVALID) {} |
1076 _dist(0), _path(0), _di(0) {} |
1076 |
1077 |
1077 /// Constructor. |
1078 /// Constructor. |
1078 |
1079 |
1079 /// This constructor requires some parameters, |
1080 /// This constructor requires two parameters, |
1080 /// listed in the parameters list. |
1081 /// others are initiated to \c 0. |
1081 /// Others are initiated to 0. |
|
1082 /// \param g The digraph the algorithm runs on. |
1082 /// \param g The digraph the algorithm runs on. |
1083 /// \param l The length map. |
1083 /// \param l The length map. |
1084 /// \param s The source node. |
1084 DijkstraWizardBase(const GR &g,const LM &l) : |
1085 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
|
1086 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1085 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1087 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1086 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1088 _processed(0), _pred(0), _dist(0), _source(s) {} |
1087 _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1089 |
1088 |
1090 }; |
1089 }; |
1091 |
1090 |
1092 /// Auxiliary class for the function type interface of Dijkstra algorithm. |
1091 /// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1093 |
1092 |
1094 /// This auxiliary class is created to implement the function type |
1093 /// This auxiliary class is created to implement the |
1095 /// interface of \ref Dijkstra algorithm. It uses the functions and features |
1094 /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1096 /// of the plain \ref Dijkstra, but it is much simpler to use it. |
1095 /// It does not have own \ref run() method, it uses the functions |
1097 /// It should only be used through the \ref dijkstra() function, which makes |
1096 /// and features of the plain \ref Dijkstra. |
1098 /// it easier to use the algorithm. |
|
1099 /// |
1097 /// |
1100 /// Simplicity means that the way to change the types defined |
1098 /// This class should only be used through the \ref dijkstra() function, |
1101 /// in the traits class is based on functions that returns the new class |
1099 /// which makes it easier to use the algorithm. |
1102 /// and not on templatable built-in classes. |
|
1103 /// When using the plain \ref Dijkstra |
|
1104 /// the new class with the modified type comes from |
|
1105 /// the original class by using the :: |
|
1106 /// operator. In the case of \ref DijkstraWizard only |
|
1107 /// a function have to be called, and it will |
|
1108 /// return the needed class. |
|
1109 /// |
|
1110 /// It does not have own \ref run() method. When its \ref run() method |
|
1111 /// is called, it initiates a plain \ref Dijkstra object, and calls the |
|
1112 /// \ref Dijkstra::run() method of it. |
|
1113 template<class TR> |
1100 template<class TR> |
1114 class DijkstraWizard : public TR |
1101 class DijkstraWizard : public TR |
1115 { |
1102 { |
1116 typedef TR Base; |
1103 typedef TR Base; |
1117 |
1104 |
1144 |
1133 |
1145 /// Constructor that requires parameters. |
1134 /// Constructor that requires parameters. |
1146 |
1135 |
1147 /// Constructor that requires parameters. |
1136 /// Constructor that requires parameters. |
1148 /// These parameters will be the default values for the traits class. |
1137 /// These parameters will be the default values for the traits class. |
1149 DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
1138 /// \param g The digraph the algorithm runs on. |
1150 TR(g,l,s) {} |
1139 /// \param l The length map. |
|
1140 DijkstraWizard(const Digraph &g, const LengthMap &l) : |
|
1141 TR(g,l) {} |
1151 |
1142 |
1152 ///Copy constructor |
1143 ///Copy constructor |
1153 DijkstraWizard(const TR &b) : TR(b) {} |
1144 DijkstraWizard(const TR &b) : TR(b) {} |
1154 |
1145 |
1155 ~DijkstraWizard() {} |
1146 ~DijkstraWizard() {} |
1156 |
1147 |
1157 ///Runs Dijkstra algorithm from a source node. |
1148 ///Runs Dijkstra algorithm from the given source node. |
1158 |
1149 |
1159 ///Runs Dijkstra algorithm from a source node. |
1150 ///This method runs %Dijkstra algorithm from the given source node |
1160 ///The node can be given with the \ref source() function. |
1151 ///in order to compute the shortest path to each node. |
1161 void run() |
1152 void run(Node s) |
1162 { |
1153 { |
1163 if(Base::_source==INVALID) throw UninitializedParameter(); |
1154 if (s==INVALID) throw UninitializedParameter(); |
1164 Dijkstra<Digraph,LengthMap,TR> |
1155 Dijkstra<Digraph,LengthMap,TR> |
1165 dij(*reinterpret_cast<const Digraph*>(Base::_g), |
1156 dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1166 *reinterpret_cast<const LengthMap*>(Base::_length)); |
1157 *reinterpret_cast<const LengthMap*>(Base::_length)); |
1167 if(Base::_processed) |
1158 if (Base::_pred) |
1168 dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1159 dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1169 if(Base::_pred) |
1160 if (Base::_dist) |
1170 dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1161 dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1171 if(Base::_dist) |
1162 if (Base::_processed) |
1172 dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1163 dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1173 dij.run(Base::_source); |
1164 dijk.run(s); |
1174 } |
1165 } |
1175 |
1166 |
1176 ///Runs Dijkstra algorithm from the given node. |
1167 ///Finds the shortest path between \c s and \c t. |
1177 |
1168 |
1178 ///Runs Dijkstra algorithm from the given node. |
1169 ///This method runs the %Dijkstra algorithm from node \c s |
1179 ///\param s is the given source. |
1170 ///in order to compute the shortest path to node \c t |
1180 void run(Node s) |
1171 ///(it stops searching when \c t is processed). |
1181 { |
1172 /// |
1182 Base::_source=s; |
1173 ///\return \c true if \c t is reachable form \c s. |
1183 run(); |
1174 bool run(Node s, Node t) |
1184 } |
1175 { |
1185 |
1176 if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
1186 /// Sets the source node, from which the Dijkstra algorithm runs. |
1177 Dijkstra<Digraph,LengthMap,TR> |
1187 |
1178 dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1188 /// Sets the source node, from which the Dijkstra algorithm runs. |
1179 *reinterpret_cast<const LengthMap*>(Base::_length)); |
1189 /// \param s is the source node. |
1180 if (Base::_pred) |
1190 DijkstraWizard<TR> &source(Node s) |
1181 dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1191 { |
1182 if (Base::_dist) |
1192 Base::_source=s; |
1183 dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1193 return *this; |
1184 if (Base::_processed) |
|
1185 dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1186 dijk.run(s,t); |
|
1187 if (Base::_path) |
|
1188 *reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
|
1189 if (Base::_di) |
|
1190 *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
|
1191 return dijk.reached(t); |
1194 } |
1192 } |
1195 |
1193 |
1196 template<class T> |
1194 template<class T> |
1197 struct SetPredMapBase : public Base { |
1195 struct SetPredMapBase : public Base { |
1198 typedef T PredMap; |
1196 typedef T PredMap; |
1199 static PredMap *createPredMap(const Digraph &) { return 0; }; |
1197 static PredMap *createPredMap(const Digraph &) { return 0; }; |
1200 SetPredMapBase(const TR &b) : TR(b) {} |
1198 SetPredMapBase(const TR &b) : TR(b) {} |
1201 }; |
1199 }; |
1202 ///\brief \ref named-templ-param "Named parameter" |
1200 ///\brief \ref named-func-param "Named parameter" |
1203 ///for setting \ref PredMap object. |
1201 ///for setting \ref PredMap object. |
1204 /// |
1202 /// |
1205 ///\ref named-templ-param "Named parameter" |
1203 ///\ref named-func-param "Named parameter" |
1206 ///for setting \ref PredMap object. |
1204 ///for setting \ref PredMap object. |
1207 template<class T> |
1205 template<class T> |
1208 DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1206 DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1209 { |
1207 { |
1210 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1208 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1211 return DijkstraWizard<SetPredMapBase<T> >(*this); |
1209 return DijkstraWizard<SetPredMapBase<T> >(*this); |
|
1210 } |
|
1211 |
|
1212 template<class T> |
|
1213 struct SetDistMapBase : public Base { |
|
1214 typedef T DistMap; |
|
1215 static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1216 SetDistMapBase(const TR &b) : TR(b) {} |
|
1217 }; |
|
1218 ///\brief \ref named-func-param "Named parameter" |
|
1219 ///for setting \ref DistMap object. |
|
1220 /// |
|
1221 ///\ref named-func-param "Named parameter" |
|
1222 ///for setting \ref DistMap object. |
|
1223 template<class T> |
|
1224 DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
|
1225 { |
|
1226 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1227 return DijkstraWizard<SetDistMapBase<T> >(*this); |
1212 } |
1228 } |
1213 |
1229 |
1214 template<class T> |
1230 template<class T> |
1215 struct SetProcessedMapBase : public Base { |
1231 struct SetProcessedMapBase : public Base { |
1216 typedef T ProcessedMap; |
1232 typedef T ProcessedMap; |
1217 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1233 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1218 SetProcessedMapBase(const TR &b) : TR(b) {} |
1234 SetProcessedMapBase(const TR &b) : TR(b) {} |
1219 }; |
1235 }; |
1220 ///\brief \ref named-templ-param "Named parameter" |
1236 ///\brief \ref named-func-param "Named parameter" |
1221 ///for setting \ref ProcessedMap object. |
1237 ///for setting \ref ProcessedMap object. |
1222 /// |
1238 /// |
1223 /// \ref named-templ-param "Named parameter" |
1239 /// \ref named-func-param "Named parameter" |
1224 ///for setting \ref ProcessedMap object. |
1240 ///for setting \ref ProcessedMap object. |
1225 template<class T> |
1241 template<class T> |
1226 DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1242 DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1227 { |
1243 { |
1228 Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1244 Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1229 return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1245 return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1230 } |
1246 } |
1231 |
1247 |
1232 template<class T> |
1248 template<class T> |
1233 struct SetDistMapBase : public Base { |
1249 struct SetPathBase : public Base { |
1234 typedef T DistMap; |
1250 typedef T Path; |
1235 static DistMap *createDistMap(const Digraph &) { return 0; }; |
1251 SetPathBase(const TR &b) : TR(b) {} |
1236 SetDistMapBase(const TR &b) : TR(b) {} |
1252 }; |
1237 }; |
1253 ///\brief \ref named-func-param "Named parameter" |
1238 ///\brief \ref named-templ-param "Named parameter" |
1254 ///for getting the shortest path to the target node. |
1239 ///for setting \ref DistMap object. |
1255 /// |
1240 /// |
1256 ///\ref named-func-param "Named parameter" |
1241 ///\ref named-templ-param "Named parameter" |
1257 ///for getting the shortest path to the target node. |
1242 ///for setting \ref DistMap object. |
|
1243 template<class T> |
1258 template<class T> |
1244 DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
1259 DijkstraWizard<SetPathBase<T> > path(const T &t) |
1245 { |
1260 { |
1246 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1261 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1247 return DijkstraWizard<SetDistMapBase<T> >(*this); |
1262 return DijkstraWizard<SetPathBase<T> >(*this); |
|
1263 } |
|
1264 |
|
1265 ///\brief \ref named-func-param "Named parameter" |
|
1266 ///for getting the distance of the target node. |
|
1267 /// |
|
1268 ///\ref named-func-param "Named parameter" |
|
1269 ///for getting the distance of the target node. |
|
1270 DijkstraWizard dist(const Value &d) |
|
1271 { |
|
1272 Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
|
1273 return *this; |
1248 } |
1274 } |
1249 |
1275 |
1250 }; |
1276 }; |
1251 |
1277 |
1252 ///Function type interface for Dijkstra algorithm. |
1278 ///Function-type interface for Dijkstra algorithm. |
1253 |
1279 |
1254 /// \ingroup shortest_path |
1280 /// \ingroup shortest_path |
1255 ///Function type interface for Dijkstra algorithm. |
1281 ///Function-type interface for Dijkstra algorithm. |
1256 /// |
1282 /// |
1257 ///This function also has several |
1283 ///This function also has several \ref named-func-param "named parameters", |
1258 ///\ref named-templ-func-param "named parameters", |
|
1259 ///they are declared as the members of class \ref DijkstraWizard. |
1284 ///they are declared as the members of class \ref DijkstraWizard. |
1260 ///The following |
1285 ///The following examples show how to use these parameters. |
1261 ///example shows how to use these parameters. |
|
1262 ///\code |
1286 ///\code |
1263 /// dijkstra(g,length,source).predMap(preds).run(); |
1287 /// // Compute shortest path from node s to each node |
|
1288 /// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
|
1289 /// |
|
1290 /// // Compute shortest path from s to t |
|
1291 /// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
1264 ///\endcode |
1292 ///\endcode |
1265 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1293 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1266 ///to the end of the parameter list. |
1294 ///to the end of the parameter list. |
1267 ///\sa DijkstraWizard |
1295 ///\sa DijkstraWizard |
1268 ///\sa Dijkstra |
1296 ///\sa Dijkstra |
1269 template<class GR, class LM> |
1297 template<class GR, class LM> |
1270 DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1298 DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1271 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
1299 dijkstra(const GR &digraph, const LM &length) |
1272 { |
1300 { |
1273 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
1301 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length); |
1274 } |
1302 } |
1275 |
1303 |
1276 } //END OF NAMESPACE LEMON |
1304 } //END OF NAMESPACE LEMON |
1277 |
1305 |
1278 #endif |
1306 #endif |