0
8
0
46
46
28
30
45
45
32
31
13
13
... | ... |
@@ -28,102 +28,102 @@ |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 |
///Instantiates a PredMap. |
|
52 |
///Instantiates a \c PredMap. |
|
53 | 53 |
|
54 |
///This function instantiates a PredMap. |
|
54 |
///This function instantiates a \ref PredMap. |
|
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 |
///PredMap. |
|
56 |
///\ref PredMap. |
|
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 |
///Instantiates a ProcessedMap. |
|
67 |
///Instantiates a \c ProcessedMap. |
|
68 | 68 |
|
69 |
///This function instantiates a ProcessedMap. |
|
69 |
///This function instantiates a \ref ProcessedMap. |
|
70 | 70 |
///\param g is the digraph, to which |
71 |
///we would like to define the ProcessedMap |
|
71 |
///we would like to define the \ref ProcessedMap |
|
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 |
///Instantiates a ReachedMap. |
|
86 |
///Instantiates a \c ReachedMap. |
|
87 | 87 |
|
88 |
///This function instantiates a ReachedMap. |
|
88 |
///This function instantiates a \ref ReachedMap. |
|
89 | 89 |
///\param g is the digraph, to which |
90 |
///we would like to define the ReachedMap. |
|
90 |
///we would like to define the \ref ReachedMap. |
|
91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 92 |
{ |
93 | 93 |
return new ReachedMap(g); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///The type of the map that stores the distances of the nodes. |
97 | 97 |
|
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 |
///Instantiates a DistMap. |
|
101 |
///Instantiates a \c DistMap. |
|
102 | 102 |
|
103 |
///This function instantiates a DistMap. |
|
103 |
///This function instantiates a \ref DistMap. |
|
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 |
///DistMap. |
|
105 |
///\ref DistMap. |
|
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%BFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 122 |
///The default type is \ref ListDigraph. |
123 | 123 |
#ifdef DOXYGEN |
124 | 124 |
template <typename GR, |
125 | 125 |
typename TR> |
126 | 126 |
#else |
127 | 127 |
template <typename GR=ListDigraph, |
128 | 128 |
typename TR=BfsDefaultTraits<GR> > |
129 | 129 |
#endif |
... | ... |
@@ -200,131 +200,131 @@ |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
protected: |
203 | 203 |
|
204 | 204 |
Bfs() {} |
205 | 205 |
|
206 | 206 |
public: |
207 | 207 |
|
208 | 208 |
typedef Bfs Create; |
209 | 209 |
|
210 | 210 |
///\name Named Template Parameters |
211 | 211 |
|
212 | 212 |
///@{ |
213 | 213 |
|
214 | 214 |
template <class T> |
215 | 215 |
struct SetPredMapTraits : public Traits { |
216 | 216 |
typedef T PredMap; |
217 | 217 |
static PredMap *createPredMap(const Digraph &) |
218 | 218 |
{ |
219 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 220 |
return 0; // ignore warnings |
221 | 221 |
} |
222 | 222 |
}; |
223 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 |
///PredMap type. |
|
224 |
///\c PredMap type. |
|
225 | 225 |
/// |
226 | 226 |
///\ref named-templ-param "Named parameter" for setting |
227 |
///PredMap type. |
|
227 |
///\c PredMap type. |
|
228 | 228 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
229 | 229 |
template <class T> |
230 | 230 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
231 | 231 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
232 | 232 |
}; |
233 | 233 |
|
234 | 234 |
template <class T> |
235 | 235 |
struct SetDistMapTraits : public Traits { |
236 | 236 |
typedef T DistMap; |
237 | 237 |
static DistMap *createDistMap(const Digraph &) |
238 | 238 |
{ |
239 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 240 |
return 0; // ignore warnings |
241 | 241 |
} |
242 | 242 |
}; |
243 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 |
///DistMap type. |
|
244 |
///\c DistMap type. |
|
245 | 245 |
/// |
246 | 246 |
///\ref named-templ-param "Named parameter" for setting |
247 |
///DistMap type. |
|
247 |
///\c DistMap type. |
|
248 | 248 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
249 | 249 |
template <class T> |
250 | 250 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
251 | 251 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
252 | 252 |
}; |
253 | 253 |
|
254 | 254 |
template <class T> |
255 | 255 |
struct SetReachedMapTraits : public Traits { |
256 | 256 |
typedef T ReachedMap; |
257 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 258 |
{ |
259 | 259 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
260 | 260 |
return 0; // ignore warnings |
261 | 261 |
} |
262 | 262 |
}; |
263 | 263 |
///\brief \ref named-templ-param "Named parameter" for setting |
264 |
///ReachedMap type. |
|
264 |
///\c ReachedMap type. |
|
265 | 265 |
/// |
266 | 266 |
///\ref named-templ-param "Named parameter" for setting |
267 |
///ReachedMap type. |
|
267 |
///\c ReachedMap type. |
|
268 | 268 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
269 | 269 |
template <class T> |
270 | 270 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
271 | 271 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
272 | 272 |
}; |
273 | 273 |
|
274 | 274 |
template <class T> |
275 | 275 |
struct SetProcessedMapTraits : public Traits { |
276 | 276 |
typedef T ProcessedMap; |
277 | 277 |
static ProcessedMap *createProcessedMap(const Digraph &) |
278 | 278 |
{ |
279 | 279 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
280 | 280 |
return 0; // ignore warnings |
281 | 281 |
} |
282 | 282 |
}; |
283 | 283 |
///\brief \ref named-templ-param "Named parameter" for setting |
284 |
///ProcessedMap type. |
|
284 |
///\c ProcessedMap type. |
|
285 | 285 |
/// |
286 | 286 |
///\ref named-templ-param "Named parameter" for setting |
287 |
///ProcessedMap type. |
|
287 |
///\c ProcessedMap type. |
|
288 | 288 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
289 | 289 |
template <class T> |
290 | 290 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
291 | 291 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
292 | 292 |
}; |
293 | 293 |
|
294 | 294 |
struct SetStandardProcessedMapTraits : public Traits { |
295 | 295 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
296 | 296 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
297 | 297 |
{ |
298 | 298 |
return new ProcessedMap(g); |
299 | 299 |
return 0; // ignore warnings |
300 | 300 |
} |
301 | 301 |
}; |
302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
303 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
304 | 304 |
/// |
305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
306 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
307 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
308 | 308 |
struct SetStandardProcessedMap : |
309 | 309 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
310 | 310 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
311 | 311 |
}; |
312 | 312 |
|
313 | 313 |
///@} |
314 | 314 |
|
315 | 315 |
public: |
316 | 316 |
|
317 | 317 |
///Constructor. |
318 | 318 |
|
319 | 319 |
///Constructor. |
320 | 320 |
///\param g The digraph the algorithm runs on. |
321 | 321 |
Bfs(const Digraph &g) : |
322 | 322 |
G(&g), |
323 | 323 |
_pred(NULL), local_pred(false), |
324 | 324 |
_dist(NULL), local_dist(false), |
325 | 325 |
_reached(NULL), local_reached(false), |
326 | 326 |
_processed(NULL), local_processed(false) |
327 | 327 |
{ } |
328 | 328 |
|
329 | 329 |
///Destructor. |
330 | 330 |
~Bfs() |
... | ... |
@@ -1173,179 +1173,179 @@ |
1173 | 1173 |
///The following examples show how to use these parameters. |
1174 | 1174 |
///\code |
1175 | 1175 |
/// // Compute shortest path from node s to each node |
1176 | 1176 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
1177 | 1177 |
/// |
1178 | 1178 |
/// // Compute shortest path from s to t |
1179 | 1179 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
1180 | 1180 |
///\endcode |
1181 | 1181 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
1182 | 1182 |
///to the end of the parameter list. |
1183 | 1183 |
///\sa BfsWizard |
1184 | 1184 |
///\sa Bfs |
1185 | 1185 |
template<class GR> |
1186 | 1186 |
BfsWizard<BfsWizardBase<GR> > |
1187 | 1187 |
bfs(const GR &digraph) |
1188 | 1188 |
{ |
1189 | 1189 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
1190 | 1190 |
} |
1191 | 1191 |
|
1192 | 1192 |
#ifdef DOXYGEN |
1193 | 1193 |
/// \brief Visitor class for BFS. |
1194 | 1194 |
/// |
1195 | 1195 |
/// This class defines the interface of the BfsVisit events, and |
1196 | 1196 |
/// it could be the base of a real visitor class. |
1197 |
template <typename |
|
1197 |
template <typename GR> |
|
1198 | 1198 |
struct BfsVisitor { |
1199 |
typedef |
|
1199 |
typedef GR Digraph; |
|
1200 | 1200 |
typedef typename Digraph::Arc Arc; |
1201 | 1201 |
typedef typename Digraph::Node Node; |
1202 | 1202 |
/// \brief Called for the source node(s) of the BFS. |
1203 | 1203 |
/// |
1204 | 1204 |
/// This function is called for the source node(s) of the BFS. |
1205 | 1205 |
void start(const Node& node) {} |
1206 | 1206 |
/// \brief Called when a node is reached first time. |
1207 | 1207 |
/// |
1208 | 1208 |
/// This function is called when a node is reached first time. |
1209 | 1209 |
void reach(const Node& node) {} |
1210 | 1210 |
/// \brief Called when a node is processed. |
1211 | 1211 |
/// |
1212 | 1212 |
/// This function is called when a node is processed. |
1213 | 1213 |
void process(const Node& node) {} |
1214 | 1214 |
/// \brief Called when an arc reaches a new node. |
1215 | 1215 |
/// |
1216 | 1216 |
/// This function is called when the BFS finds an arc whose target node |
1217 | 1217 |
/// is not reached yet. |
1218 | 1218 |
void discover(const Arc& arc) {} |
1219 | 1219 |
/// \brief Called when an arc is examined but its target node is |
1220 | 1220 |
/// already discovered. |
1221 | 1221 |
/// |
1222 | 1222 |
/// This function is called when an arc is examined but its target node is |
1223 | 1223 |
/// already discovered. |
1224 | 1224 |
void examine(const Arc& arc) {} |
1225 | 1225 |
}; |
1226 | 1226 |
#else |
1227 |
template <typename |
|
1227 |
template <typename GR> |
|
1228 | 1228 |
struct BfsVisitor { |
1229 |
typedef |
|
1229 |
typedef GR Digraph; |
|
1230 | 1230 |
typedef typename Digraph::Arc Arc; |
1231 | 1231 |
typedef typename Digraph::Node Node; |
1232 | 1232 |
void start(const Node&) {} |
1233 | 1233 |
void reach(const Node&) {} |
1234 | 1234 |
void process(const Node&) {} |
1235 | 1235 |
void discover(const Arc&) {} |
1236 | 1236 |
void examine(const Arc&) {} |
1237 | 1237 |
|
1238 | 1238 |
template <typename _Visitor> |
1239 | 1239 |
struct Constraints { |
1240 | 1240 |
void constraints() { |
1241 | 1241 |
Arc arc; |
1242 | 1242 |
Node node; |
1243 | 1243 |
visitor.start(node); |
1244 | 1244 |
visitor.reach(node); |
1245 | 1245 |
visitor.process(node); |
1246 | 1246 |
visitor.discover(arc); |
1247 | 1247 |
visitor.examine(arc); |
1248 | 1248 |
} |
1249 | 1249 |
_Visitor& visitor; |
1250 | 1250 |
}; |
1251 | 1251 |
}; |
1252 | 1252 |
#endif |
1253 | 1253 |
|
1254 | 1254 |
/// \brief Default traits class of BfsVisit class. |
1255 | 1255 |
/// |
1256 | 1256 |
/// Default traits class of BfsVisit class. |
1257 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1258 |
template<class _Digraph> |
|
1257 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
1258 |
template<class GR> |
|
1259 | 1259 |
struct BfsVisitDefaultTraits { |
1260 | 1260 |
|
1261 | 1261 |
/// \brief The type of the digraph the algorithm runs on. |
1262 |
typedef |
|
1262 |
typedef GR Digraph; |
|
1263 | 1263 |
|
1264 | 1264 |
/// \brief The type of the map that indicates which nodes are reached. |
1265 | 1265 |
/// |
1266 | 1266 |
/// The type of the map that indicates which nodes are reached. |
1267 | 1267 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1268 | 1268 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1269 | 1269 |
|
1270 | 1270 |
/// \brief Instantiates a ReachedMap. |
1271 | 1271 |
/// |
1272 | 1272 |
/// This function instantiates a ReachedMap. |
1273 | 1273 |
/// \param digraph is the digraph, to which |
1274 | 1274 |
/// we would like to define the ReachedMap. |
1275 | 1275 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1276 | 1276 |
return new ReachedMap(digraph); |
1277 | 1277 |
} |
1278 | 1278 |
|
1279 | 1279 |
}; |
1280 | 1280 |
|
1281 | 1281 |
/// \ingroup search |
1282 | 1282 |
/// |
1283 |
/// \brief |
|
1283 |
/// \brief BFS algorithm class with visitor interface. |
|
1284 | 1284 |
/// |
1285 |
/// This class provides an efficient implementation of the |
|
1285 |
/// This class provides an efficient implementation of the BFS algorithm |
|
1286 | 1286 |
/// with visitor interface. |
1287 | 1287 |
/// |
1288 |
/// The |
|
1288 |
/// The BfsVisit class provides an alternative interface to the Bfs |
|
1289 | 1289 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1290 | 1290 |
/// the member functions of the \c Visitor class on every BFS event. |
1291 | 1291 |
/// |
1292 | 1292 |
/// This interface of the BFS algorithm should be used in special cases |
1293 | 1293 |
/// when extra actions have to be performed in connection with certain |
1294 | 1294 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1295 | 1295 |
/// instead. |
1296 | 1296 |
/// |
1297 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1298 |
/// The default value is |
|
1299 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
1300 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
|
1301 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
1302 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
|
1297 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
1298 |
/// The default type is \ref ListDigraph. |
|
1299 |
/// The value of GR is not used directly by \ref BfsVisit, |
|
1300 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
|
1301 |
/// \tparam VS The Visitor type that is used by the algorithm. |
|
1302 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
|
1303 | 1303 |
/// does not observe the BFS events. If you want to observe the BFS |
1304 | 1304 |
/// events, you should implement your own visitor class. |
1305 |
/// \tparam |
|
1305 |
/// \tparam TR Traits class to set various data types used by the |
|
1306 | 1306 |
/// algorithm. The default traits class is |
1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits< |
|
1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
1308 | 1308 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1309 | 1309 |
/// a BFS visit traits class. |
1310 | 1310 |
#ifdef DOXYGEN |
1311 |
template <typename |
|
1311 |
template <typename GR, typename VS, typename TR> |
|
1312 | 1312 |
#else |
1313 |
template <typename _Digraph = ListDigraph, |
|
1314 |
typename _Visitor = BfsVisitor<_Digraph>, |
|
1315 |
|
|
1313 |
template <typename GR = ListDigraph, |
|
1314 |
typename VS = BfsVisitor<GR>, |
|
1315 |
typename TR = BfsVisitDefaultTraits<GR> > |
|
1316 | 1316 |
#endif |
1317 | 1317 |
class BfsVisit { |
1318 | 1318 |
public: |
1319 | 1319 |
|
1320 | 1320 |
///The traits class. |
1321 |
typedef |
|
1321 |
typedef TR Traits; |
|
1322 | 1322 |
|
1323 | 1323 |
///The type of the digraph the algorithm runs on. |
1324 | 1324 |
typedef typename Traits::Digraph Digraph; |
1325 | 1325 |
|
1326 | 1326 |
///The visitor type used by the algorithm. |
1327 |
typedef |
|
1327 |
typedef VS Visitor; |
|
1328 | 1328 |
|
1329 | 1329 |
///The type of the map that indicates which nodes are reached. |
1330 | 1330 |
typedef typename Traits::ReachedMap ReachedMap; |
1331 | 1331 |
|
1332 | 1332 |
private: |
1333 | 1333 |
|
1334 | 1334 |
typedef typename Digraph::Node Node; |
1335 | 1335 |
typedef typename Digraph::NodeIt NodeIt; |
1336 | 1336 |
typedef typename Digraph::Arc Arc; |
1337 | 1337 |
typedef typename Digraph::OutArcIt OutArcIt; |
1338 | 1338 |
|
1339 | 1339 |
//Pointer to the underlying digraph. |
1340 | 1340 |
const Digraph *_digraph; |
1341 | 1341 |
//Pointer to the visitor object. |
1342 | 1342 |
Visitor *_visitor; |
1343 | 1343 |
//Pointer to the map of reached status of the nodes. |
1344 | 1344 |
ReachedMap *_reached; |
1345 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
1346 | 1346 |
bool local_reached; |
1347 | 1347 |
|
1348 | 1348 |
std::vector<typename Digraph::Node> _list; |
1349 | 1349 |
int _list_front, _list_back; |
1350 | 1350 |
|
1351 | 1351 |
//Creates the maps if necessary. |
... | ... |
@@ -114,49 +114,49 @@ |
114 | 114 |
if (capacity == 0) return; |
115 | 115 |
values = allocator.allocate(capacity); |
116 | 116 |
Notifier* nf = Parent::notifier(); |
117 | 117 |
Item it; |
118 | 118 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
119 | 119 |
int id = nf->id(it);; |
120 | 120 |
allocator.construct(&(values[id]), copy.values[id]); |
121 | 121 |
} |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
// \brief Assign operator. |
125 | 125 |
// |
126 | 126 |
// This operator assigns for each item in the map the |
127 | 127 |
// value mapped to the same item in the copied map. |
128 | 128 |
// The parameter map should be indiced with the same |
129 | 129 |
// itemset because this assign operator does not change |
130 | 130 |
// the container of the map. |
131 | 131 |
ArrayMap& operator=(const ArrayMap& cmap) { |
132 | 132 |
return operator=<ArrayMap>(cmap); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
// \brief Template assign operator. |
137 | 137 |
// |
138 |
// The given parameter should |
|
138 |
// The given parameter should conform to the ReadMap |
|
139 | 139 |
// concecpt and could be indiced by the current item set of |
140 | 140 |
// the NodeMap. In this case the value for each item |
141 | 141 |
// is assigned by the value of the given ReadMap. |
142 | 142 |
template <typename CMap> |
143 | 143 |
ArrayMap& operator=(const CMap& cmap) { |
144 | 144 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
145 | 145 |
const typename Parent::Notifier* nf = Parent::notifier(); |
146 | 146 |
Item it; |
147 | 147 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
148 | 148 |
set(it, cmap[it]); |
149 | 149 |
} |
150 | 150 |
return *this; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
public: |
154 | 154 |
// \brief The destructor of the map. |
155 | 155 |
// |
156 | 156 |
// The destructor of the map. |
157 | 157 |
virtual ~ArrayMap() { |
158 | 158 |
if (attached()) { |
159 | 159 |
clear(); |
160 | 160 |
detach(); |
161 | 161 |
} |
162 | 162 |
} |
... | ... |
@@ -103,49 +103,49 @@ |
103 | 103 |
// \brief Copy constructor |
104 | 104 |
// |
105 | 105 |
// Copy constructor. |
106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
107 | 107 |
if (_copy.attached()) { |
108 | 108 |
Parent::attach(*_copy.notifier()); |
109 | 109 |
container = _copy.container; |
110 | 110 |
} |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
// \brief Assign operator. |
114 | 114 |
// |
115 | 115 |
// This operator assigns for each item in the map the |
116 | 116 |
// value mapped to the same item in the copied map. |
117 | 117 |
// The parameter map should be indiced with the same |
118 | 118 |
// itemset because this assign operator does not change |
119 | 119 |
// the container of the map. |
120 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
121 | 121 |
return operator=<VectorMap>(cmap); |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
|
125 | 125 |
// \brief Template assign operator. |
126 | 126 |
// |
127 |
// The given parameter should |
|
127 |
// The given parameter should conform to the ReadMap |
|
128 | 128 |
// concecpt and could be indiced by the current item set of |
129 | 129 |
// the NodeMap. In this case the value for each item |
130 | 130 |
// is assigned by the value of the given ReadMap. |
131 | 131 |
template <typename CMap> |
132 | 132 |
VectorMap& operator=(const CMap& cmap) { |
133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
135 | 135 |
Item it; |
136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
137 | 137 |
set(it, cmap[it]); |
138 | 138 |
} |
139 | 139 |
return *this; |
140 | 140 |
} |
141 | 141 |
|
142 | 142 |
public: |
143 | 143 |
|
144 | 144 |
// \brief The subcript operator. |
145 | 145 |
// |
146 | 146 |
// The subscript operator. The map can be subscripted by the |
147 | 147 |
// actual items of the graph. |
148 | 148 |
Reference operator[](const Key& key) { |
149 | 149 |
return container[Parent::notifier()->id(key)]; |
150 | 150 |
} |
151 | 151 |
... | ... |
@@ -10,80 +10,80 @@ |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CIRCULATION_H |
20 | 20 |
#define LEMON_CIRCULATION_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
///\ingroup max_flow |
26 | 26 |
///\file |
27 | 27 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
28 | 28 |
/// |
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Circulation class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Circulation class. |
34 |
/// \tparam _Diraph Digraph type. |
|
35 |
/// \tparam _LCapMap Lower bound capacity map type. |
|
36 |
/// \tparam _UCapMap Upper bound capacity map type. |
|
37 |
/// \tparam _DeltaMap Delta map type. |
|
38 |
template <typename _Diraph, typename _LCapMap, |
|
39 |
typename _UCapMap, typename _DeltaMap> |
|
34 |
/// \tparam GR Digraph type. |
|
35 |
/// \tparam LM Lower bound capacity map type. |
|
36 |
/// \tparam UM Upper bound capacity map type. |
|
37 |
/// \tparam DM Delta map type. |
|
38 |
template <typename GR, typename LM, |
|
39 |
typename UM, typename DM> |
|
40 | 40 |
struct CirculationDefaultTraits { |
41 | 41 |
|
42 | 42 |
/// \brief The type of the digraph the algorithm runs on. |
43 |
typedef |
|
43 |
typedef GR Digraph; |
|
44 | 44 |
|
45 | 45 |
/// \brief The type of the map that stores the circulation lower |
46 | 46 |
/// bound. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the circulation lower bound. |
49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
50 |
typedef |
|
50 |
typedef LM LCapMap; |
|
51 | 51 |
|
52 | 52 |
/// \brief The type of the map that stores the circulation upper |
53 | 53 |
/// bound. |
54 | 54 |
/// |
55 | 55 |
/// The type of the map that stores the circulation upper bound. |
56 | 56 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
57 |
typedef |
|
57 |
typedef UM UCapMap; |
|
58 | 58 |
|
59 | 59 |
/// \brief The type of the map that stores the lower bound for |
60 | 60 |
/// the supply of the nodes. |
61 | 61 |
/// |
62 | 62 |
/// The type of the map that stores the lower bound for the supply |
63 | 63 |
/// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" |
64 | 64 |
/// concept. |
65 |
typedef |
|
65 |
typedef DM DeltaMap; |
|
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow values. |
68 | 68 |
typedef typename DeltaMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
74 | 74 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
75 | 75 |
|
76 | 76 |
/// \brief Instantiates a FlowMap. |
77 | 77 |
/// |
78 | 78 |
/// This function instantiates a \ref FlowMap. |
79 | 79 |
/// \param digraph The digraph, to which we would like to define |
80 | 80 |
/// the flow map. |
81 | 81 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
82 | 82 |
return new FlowMap(digraph); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief The elevator type used by the algorithm. |
86 | 86 |
/// |
87 | 87 |
/// The elevator type used by the algorithm. |
88 | 88 |
/// |
89 | 89 |
/// \sa Elevator |
... | ... |
@@ -116,77 +116,75 @@ |
116 | 116 |
It is to find a feasible circulation when lower and upper bounds |
117 | 117 |
are given for the flow values on the arcs and lower bounds |
118 | 118 |
are given for the supply values of the nodes. |
119 | 119 |
|
120 | 120 |
The exact formulation of this problem is the following. |
121 | 121 |
Let \f$G=(V,A)\f$ be a digraph, |
122 | 122 |
\f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$, |
123 | 123 |
\f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation |
124 | 124 |
\f$f: A\rightarrow\mathbf{R}^+_0\f$ so that |
125 | 125 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) |
126 | 126 |
\geq delta(v) \quad \forall v\in V, \f] |
127 | 127 |
\f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] |
128 | 128 |
\note \f$delta(v)\f$ specifies a lower bound for the supply of node |
129 | 129 |
\f$v\f$. It can be either positive or negative, however note that |
130 | 130 |
\f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to |
131 | 131 |
have a feasible solution. |
132 | 132 |
|
133 | 133 |
\note A special case of this problem is when |
134 | 134 |
\f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$ |
135 | 135 |
will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. |
136 | 136 |
Thus a feasible solution for the |
137 | 137 |
\ref min_cost_flow "minimum cost flow" problem can be calculated |
138 | 138 |
in this way. |
139 | 139 |
|
140 |
\tparam _Digraph The type of the digraph the algorithm runs on. |
|
141 |
\tparam _LCapMap The type of the lower bound capacity map. The default |
|
142 |
map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
|
143 |
\tparam _UCapMap The type of the upper bound capacity map. The default |
|
144 |
map type is \c _LCapMap. |
|
145 |
\tparam _DeltaMap The type of the map that stores the lower bound |
|
140 |
\tparam GR The type of the digraph the algorithm runs on. |
|
141 |
\tparam LM The type of the lower bound capacity map. The default |
|
142 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
143 |
\tparam UM The type of the upper bound capacity map. The default |
|
144 |
map type is \c LM. |
|
145 |
\tparam DM The type of the map that stores the lower bound |
|
146 | 146 |
for the supply of the nodes. The default map type is |
147 |
\ |
|
147 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
|
148 | 148 |
*/ |
149 | 149 |
#ifdef DOXYGEN |
150 |
template< typename _Digraph, |
|
151 |
typename _LCapMap, |
|
152 |
typename _UCapMap, |
|
153 |
typename _DeltaMap, |
|
154 |
|
|
150 |
template< typename GR, |
|
151 |
typename LM, |
|
152 |
typename UM, |
|
153 |
typename DM, |
|
154 |
typename TR > |
|
155 | 155 |
#else |
156 |
template< typename _Digraph, |
|
157 |
typename _LCapMap = typename _Digraph::template ArcMap<int>, |
|
158 |
typename _UCapMap = _LCapMap, |
|
159 |
typename _DeltaMap = typename _Digraph:: |
|
160 |
template NodeMap<typename _UCapMap::Value>, |
|
161 |
typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap, |
|
162 |
|
|
156 |
template< typename GR, |
|
157 |
typename LM = typename GR::template ArcMap<int>, |
|
158 |
typename UM = LM, |
|
159 |
typename DM = typename GR::template NodeMap<typename UM::Value>, |
|
160 |
typename TR = CirculationDefaultTraits<GR, LM, UM, DM> > |
|
163 | 161 |
#endif |
164 | 162 |
class Circulation { |
165 | 163 |
public: |
166 | 164 |
|
167 | 165 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
168 |
typedef |
|
166 |
typedef TR Traits; |
|
169 | 167 |
///The type of the digraph the algorithm runs on. |
170 | 168 |
typedef typename Traits::Digraph Digraph; |
171 | 169 |
///The type of the flow values. |
172 | 170 |
typedef typename Traits::Value Value; |
173 | 171 |
|
174 | 172 |
/// The type of the lower bound capacity map. |
175 | 173 |
typedef typename Traits::LCapMap LCapMap; |
176 | 174 |
/// The type of the upper bound capacity map. |
177 | 175 |
typedef typename Traits::UCapMap UCapMap; |
178 | 176 |
/// \brief The type of the map that stores the lower bound for |
179 | 177 |
/// the supply of the nodes. |
180 | 178 |
typedef typename Traits::DeltaMap DeltaMap; |
181 | 179 |
///The type of the flow map. |
182 | 180 |
typedef typename Traits::FlowMap FlowMap; |
183 | 181 |
|
184 | 182 |
///The type of the elevator. |
185 | 183 |
typedef typename Traits::Elevator Elevator; |
186 | 184 |
///The type of the tolerance. |
187 | 185 |
typedef typename Traits::Tolerance Tolerance; |
188 | 186 |
|
189 | 187 |
private: |
190 | 188 |
|
191 | 189 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
192 | 190 |
... | ... |
@@ -93,49 +93,49 @@ |
93 | 93 |
template<typename _GraphItem> |
94 | 94 |
struct Constraints { |
95 | 95 |
void constraints() { |
96 | 96 |
_GraphItem i1; |
97 | 97 |
_GraphItem i2 = i1; |
98 | 98 |
_GraphItem i3 = INVALID; |
99 | 99 |
|
100 | 100 |
i1 = i2 = i3; |
101 | 101 |
|
102 | 102 |
bool b; |
103 | 103 |
// b = (ia == ib) && (ia != ib) && (ia < ib); |
104 | 104 |
b = (ia == ib) && (ia != ib); |
105 | 105 |
b = (ia == INVALID) && (ib != INVALID); |
106 | 106 |
b = (ia < ib); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
const _GraphItem &ia; |
110 | 110 |
const _GraphItem &ib; |
111 | 111 |
}; |
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
/// \brief An empty base directed graph class. |
115 | 115 |
/// |
116 | 116 |
/// This class provides the minimal set of features needed for a |
117 |
/// directed graph structure. All digraph concepts have to |
|
117 |
/// directed graph structure. All digraph concepts have to |
|
118 | 118 |
/// conform to this base directed graph. It just provides types |
119 | 119 |
/// for nodes and arcs and functions to get the source and the |
120 | 120 |
/// target of the arcs. |
121 | 121 |
class BaseDigraphComponent { |
122 | 122 |
public: |
123 | 123 |
|
124 | 124 |
typedef BaseDigraphComponent Digraph; |
125 | 125 |
|
126 | 126 |
/// \brief Node class of the digraph. |
127 | 127 |
/// |
128 | 128 |
/// This class represents the Nodes of the digraph. |
129 | 129 |
/// |
130 | 130 |
typedef GraphItem<'n'> Node; |
131 | 131 |
|
132 | 132 |
/// \brief Arc class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the Arcs of the digraph. |
135 | 135 |
/// |
136 | 136 |
typedef GraphItem<'e'> Arc; |
137 | 137 |
|
138 | 138 |
/// \brief Gives back the target node of an arc. |
139 | 139 |
/// |
140 | 140 |
/// Gives back the target node of an arc. |
141 | 141 |
/// |
... | ... |
@@ -158,49 +158,49 @@ |
158 | 158 |
struct Constraints { |
159 | 159 |
typedef typename _Digraph::Node Node; |
160 | 160 |
typedef typename _Digraph::Arc Arc; |
161 | 161 |
|
162 | 162 |
void constraints() { |
163 | 163 |
checkConcept<GraphItem<'n'>, Node>(); |
164 | 164 |
checkConcept<GraphItem<'a'>, Arc>(); |
165 | 165 |
{ |
166 | 166 |
Node n; |
167 | 167 |
Arc e(INVALID); |
168 | 168 |
n = digraph.source(e); |
169 | 169 |
n = digraph.target(e); |
170 | 170 |
n = digraph.oppositeNode(n, e); |
171 | 171 |
} |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
const _Digraph& digraph; |
175 | 175 |
}; |
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
/// \brief An empty base undirected graph class. |
179 | 179 |
/// |
180 | 180 |
/// This class provides the minimal set of features needed for an |
181 | 181 |
/// undirected graph structure. All undirected graph concepts have |
182 |
/// to |
|
182 |
/// to conform to this base graph. It just provides types for |
|
183 | 183 |
/// nodes, arcs and edges and functions to get the |
184 | 184 |
/// source and the target of the arcs and edges, |
185 | 185 |
/// conversion from arcs to edges and function to get |
186 | 186 |
/// both direction of the edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
typedef BaseDigraphComponent::Node Node; |
190 | 190 |
typedef BaseDigraphComponent::Arc Arc; |
191 | 191 |
/// \brief Undirected arc class of the graph. |
192 | 192 |
/// |
193 | 193 |
/// This class represents the edges of the graph. |
194 | 194 |
/// The undirected graphs can be used as a directed graph which |
195 | 195 |
/// for each arc contains the opposite arc too so the graph is |
196 | 196 |
/// bidirected. The edge represents two opposite |
197 | 197 |
/// directed arcs. |
198 | 198 |
class Edge : public GraphItem<'u'> { |
199 | 199 |
public: |
200 | 200 |
typedef GraphItem<'u'> Parent; |
201 | 201 |
/// \brief Default constructor. |
202 | 202 |
/// |
203 | 203 |
/// \warning The default constructor is not required to set |
204 | 204 |
/// the item to some well-defined value. So you should consider it |
205 | 205 |
/// as uninitialized. |
206 | 206 |
Edge() {} |
... | ... |
@@ -273,49 +273,49 @@ |
273 | 273 |
{ |
274 | 274 |
Node n; |
275 | 275 |
Edge ue(INVALID); |
276 | 276 |
Arc e; |
277 | 277 |
n = graph.u(ue); |
278 | 278 |
n = graph.v(ue); |
279 | 279 |
e = graph.direct(ue, true); |
280 | 280 |
e = graph.direct(ue, n); |
281 | 281 |
e = graph.oppositeArc(e); |
282 | 282 |
ue = e; |
283 | 283 |
bool d = graph.direction(e); |
284 | 284 |
ignore_unused_variable_warning(d); |
285 | 285 |
} |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
const _Graph& graph; |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
/// \brief An empty idable base digraph class. |
294 | 294 |
/// |
295 | 295 |
/// This class provides beside the core digraph features |
296 | 296 |
/// core id functions for the digraph structure. |
297 |
/// The most of the base digraphs should |
|
297 |
/// The most of the base digraphs should conform to this concept. |
|
298 | 298 |
/// The id's are unique and immutable. |
299 | 299 |
template <typename _Base = BaseDigraphComponent> |
300 | 300 |
class IDableDigraphComponent : public _Base { |
301 | 301 |
public: |
302 | 302 |
|
303 | 303 |
typedef _Base Base; |
304 | 304 |
typedef typename Base::Node Node; |
305 | 305 |
typedef typename Base::Arc Arc; |
306 | 306 |
|
307 | 307 |
/// \brief Gives back an unique integer id for the Node. |
308 | 308 |
/// |
309 | 309 |
/// Gives back an unique integer id for the Node. |
310 | 310 |
/// |
311 | 311 |
int id(const Node&) const { return -1;} |
312 | 312 |
|
313 | 313 |
/// \brief Gives back the node by the unique id. |
314 | 314 |
/// |
315 | 315 |
/// Gives back the node by the unique id. |
316 | 316 |
/// If the digraph does not contain node with the given id |
317 | 317 |
/// then the result of the function is undetermined. |
318 | 318 |
Node nodeFromId(int) const { return INVALID;} |
319 | 319 |
|
320 | 320 |
/// \brief Gives back an unique integer id for the Arc. |
321 | 321 |
/// |
... | ... |
@@ -351,49 +351,49 @@ |
351 | 351 |
checkConcept<Base, _Digraph >(); |
352 | 352 |
typename _Digraph::Node node; |
353 | 353 |
int nid = digraph.id(node); |
354 | 354 |
nid = digraph.id(node); |
355 | 355 |
node = digraph.nodeFromId(nid); |
356 | 356 |
typename _Digraph::Arc arc; |
357 | 357 |
int eid = digraph.id(arc); |
358 | 358 |
eid = digraph.id(arc); |
359 | 359 |
arc = digraph.arcFromId(eid); |
360 | 360 |
|
361 | 361 |
nid = digraph.maxNodeId(); |
362 | 362 |
ignore_unused_variable_warning(nid); |
363 | 363 |
eid = digraph.maxArcId(); |
364 | 364 |
ignore_unused_variable_warning(eid); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
const _Digraph& digraph; |
368 | 368 |
}; |
369 | 369 |
}; |
370 | 370 |
|
371 | 371 |
/// \brief An empty idable base undirected graph class. |
372 | 372 |
/// |
373 | 373 |
/// This class provides beside the core undirected graph features |
374 | 374 |
/// core id functions for the undirected graph structure. The |
375 |
/// most of the base undirected graphs should |
|
375 |
/// most of the base undirected graphs should conform to this |
|
376 | 376 |
/// concept. The id's are unique and immutable. |
377 | 377 |
template <typename _Base = BaseGraphComponent> |
378 | 378 |
class IDableGraphComponent : public IDableDigraphComponent<_Base> { |
379 | 379 |
public: |
380 | 380 |
|
381 | 381 |
typedef _Base Base; |
382 | 382 |
typedef typename Base::Edge Edge; |
383 | 383 |
|
384 | 384 |
using IDableDigraphComponent<_Base>::id; |
385 | 385 |
|
386 | 386 |
/// \brief Gives back an unique integer id for the Edge. |
387 | 387 |
/// |
388 | 388 |
/// Gives back an unique integer id for the Edge. |
389 | 389 |
/// |
390 | 390 |
int id(const Edge&) const { return -1;} |
391 | 391 |
|
392 | 392 |
/// \brief Gives back the edge by the unique id. |
393 | 393 |
/// |
394 | 394 |
/// Gives back the edge by the unique id. If the |
395 | 395 |
/// graph does not contain arc with the given id then the |
396 | 396 |
/// result of the function is undetermined. |
397 | 397 |
Edge edgeFromId(int) const { return INVALID;} |
398 | 398 |
|
399 | 399 |
/// \brief Gives back an integer greater or equal to the maximum |
... | ... |
@@ -28,102 +28,102 @@ |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 |
///Instantiates a PredMap. |
|
52 |
///Instantiates a \c PredMap. |
|
53 | 53 |
|
54 |
///This function instantiates a PredMap. |
|
54 |
///This function instantiates a \ref PredMap. |
|
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 |
///PredMap. |
|
56 |
///\ref PredMap. |
|
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 |
///Instantiates a ProcessedMap. |
|
67 |
///Instantiates a \c ProcessedMap. |
|
68 | 68 |
|
69 |
///This function instantiates a ProcessedMap. |
|
69 |
///This function instantiates a \ref ProcessedMap. |
|
70 | 70 |
///\param g is the digraph, to which |
71 |
///we would like to define the ProcessedMap |
|
71 |
///we would like to define the \ref ProcessedMap. |
|
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 |
///Instantiates a ReachedMap. |
|
86 |
///Instantiates a \c ReachedMap. |
|
87 | 87 |
|
88 |
///This function instantiates a ReachedMap. |
|
88 |
///This function instantiates a \ref ReachedMap. |
|
89 | 89 |
///\param g is the digraph, to which |
90 |
///we would like to define the ReachedMap. |
|
90 |
///we would like to define the \ref ReachedMap. |
|
91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 92 |
{ |
93 | 93 |
return new ReachedMap(g); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///The type of the map that stores the distances of the nodes. |
97 | 97 |
|
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 |
///Instantiates a DistMap. |
|
101 |
///Instantiates a \c DistMap. |
|
102 | 102 |
|
103 |
///This function instantiates a DistMap. |
|
103 |
///This function instantiates a \ref DistMap. |
|
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 |
///DistMap. |
|
105 |
///\ref DistMap. |
|
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%DFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 122 |
///The default type is \ref ListDigraph. |
123 | 123 |
#ifdef DOXYGEN |
124 | 124 |
template <typename GR, |
125 | 125 |
typename TR> |
126 | 126 |
#else |
127 | 127 |
template <typename GR=ListDigraph, |
128 | 128 |
typename TR=DfsDefaultTraits<GR> > |
129 | 129 |
#endif |
... | ... |
@@ -199,130 +199,130 @@ |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
protected: |
202 | 202 |
|
203 | 203 |
Dfs() {} |
204 | 204 |
|
205 | 205 |
public: |
206 | 206 |
|
207 | 207 |
typedef Dfs Create; |
208 | 208 |
|
209 | 209 |
///\name Named template parameters |
210 | 210 |
|
211 | 211 |
///@{ |
212 | 212 |
|
213 | 213 |
template <class T> |
214 | 214 |
struct SetPredMapTraits : public Traits { |
215 | 215 |
typedef T PredMap; |
216 | 216 |
static PredMap *createPredMap(const Digraph &) |
217 | 217 |
{ |
218 | 218 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
219 | 219 |
return 0; // ignore warnings |
220 | 220 |
} |
221 | 221 |
}; |
222 | 222 |
///\brief \ref named-templ-param "Named parameter" for setting |
223 |
///PredMap type. |
|
223 |
///\c PredMap type. |
|
224 | 224 |
/// |
225 | 225 |
///\ref named-templ-param "Named parameter" for setting |
226 |
///PredMap type. |
|
226 |
///\c PredMap type. |
|
227 | 227 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
228 | 228 |
template <class T> |
229 | 229 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
230 | 230 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <class T> |
234 | 234 |
struct SetDistMapTraits : public Traits { |
235 | 235 |
typedef T DistMap; |
236 | 236 |
static DistMap *createDistMap(const Digraph &) |
237 | 237 |
{ |
238 | 238 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
239 | 239 |
return 0; // ignore warnings |
240 | 240 |
} |
241 | 241 |
}; |
242 | 242 |
///\brief \ref named-templ-param "Named parameter" for setting |
243 |
///DistMap type. |
|
243 |
///\c DistMap type. |
|
244 | 244 |
/// |
245 | 245 |
///\ref named-templ-param "Named parameter" for setting |
246 |
///DistMap type. |
|
246 |
///\c DistMap type. |
|
247 | 247 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
248 | 248 |
template <class T> |
249 | 249 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
250 | 250 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
251 | 251 |
}; |
252 | 252 |
|
253 | 253 |
template <class T> |
254 | 254 |
struct SetReachedMapTraits : public Traits { |
255 | 255 |
typedef T ReachedMap; |
256 | 256 |
static ReachedMap *createReachedMap(const Digraph &) |
257 | 257 |
{ |
258 | 258 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
259 | 259 |
return 0; // ignore warnings |
260 | 260 |
} |
261 | 261 |
}; |
262 | 262 |
///\brief \ref named-templ-param "Named parameter" for setting |
263 |
///ReachedMap type. |
|
263 |
///\c ReachedMap type. |
|
264 | 264 |
/// |
265 | 265 |
///\ref named-templ-param "Named parameter" for setting |
266 |
///ReachedMap type. |
|
266 |
///\c ReachedMap type. |
|
267 | 267 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
268 | 268 |
template <class T> |
269 | 269 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
270 | 270 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
271 | 271 |
}; |
272 | 272 |
|
273 | 273 |
template <class T> |
274 | 274 |
struct SetProcessedMapTraits : public Traits { |
275 | 275 |
typedef T ProcessedMap; |
276 | 276 |
static ProcessedMap *createProcessedMap(const Digraph &) |
277 | 277 |
{ |
278 | 278 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
279 | 279 |
return 0; // ignore warnings |
280 | 280 |
} |
281 | 281 |
}; |
282 | 282 |
///\brief \ref named-templ-param "Named parameter" for setting |
283 |
///ProcessedMap type. |
|
283 |
///\c ProcessedMap type. |
|
284 | 284 |
/// |
285 | 285 |
///\ref named-templ-param "Named parameter" for setting |
286 |
///ProcessedMap type. |
|
286 |
///\c ProcessedMap type. |
|
287 | 287 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
288 | 288 |
template <class T> |
289 | 289 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
290 | 290 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
struct SetStandardProcessedMapTraits : public Traits { |
294 | 294 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
295 | 295 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
296 | 296 |
{ |
297 | 297 |
return new ProcessedMap(g); |
298 | 298 |
} |
299 | 299 |
}; |
300 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
301 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
301 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
302 | 302 |
/// |
303 | 303 |
///\ref named-templ-param "Named parameter" for setting |
304 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
304 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
305 | 305 |
///If you don't set it explicitly, it will be automatically allocated. |
306 | 306 |
struct SetStandardProcessedMap : |
307 | 307 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
308 | 308 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
309 | 309 |
}; |
310 | 310 |
|
311 | 311 |
///@} |
312 | 312 |
|
313 | 313 |
public: |
314 | 314 |
|
315 | 315 |
///Constructor. |
316 | 316 |
|
317 | 317 |
///Constructor. |
318 | 318 |
///\param g The digraph the algorithm runs on. |
319 | 319 |
Dfs(const Digraph &g) : |
320 | 320 |
G(&g), |
321 | 321 |
_pred(NULL), local_pred(false), |
322 | 322 |
_dist(NULL), local_dist(false), |
323 | 323 |
_reached(NULL), local_reached(false), |
324 | 324 |
_processed(NULL), local_processed(false) |
325 | 325 |
{ } |
326 | 326 |
|
327 | 327 |
///Destructor. |
328 | 328 |
~Dfs() |
... | ... |
@@ -1105,191 +1105,191 @@ |
1105 | 1105 |
///The following examples show how to use these parameters. |
1106 | 1106 |
///\code |
1107 | 1107 |
/// // Compute the DFS tree |
1108 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1109 | 1109 |
/// |
1110 | 1110 |
/// // Compute the DFS path from s to t |
1111 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1112 | 1112 |
///\endcode |
1113 | 1113 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
1114 | 1114 |
///to the end of the parameter list. |
1115 | 1115 |
///\sa DfsWizard |
1116 | 1116 |
///\sa Dfs |
1117 | 1117 |
template<class GR> |
1118 | 1118 |
DfsWizard<DfsWizardBase<GR> > |
1119 | 1119 |
dfs(const GR &digraph) |
1120 | 1120 |
{ |
1121 | 1121 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1122 | 1122 |
} |
1123 | 1123 |
|
1124 | 1124 |
#ifdef DOXYGEN |
1125 | 1125 |
/// \brief Visitor class for DFS. |
1126 | 1126 |
/// |
1127 | 1127 |
/// This class defines the interface of the DfsVisit events, and |
1128 | 1128 |
/// it could be the base of a real visitor class. |
1129 |
template <typename |
|
1129 |
template <typename GR> |
|
1130 | 1130 |
struct DfsVisitor { |
1131 |
typedef |
|
1131 |
typedef GR Digraph; |
|
1132 | 1132 |
typedef typename Digraph::Arc Arc; |
1133 | 1133 |
typedef typename Digraph::Node Node; |
1134 | 1134 |
/// \brief Called for the source node of the DFS. |
1135 | 1135 |
/// |
1136 | 1136 |
/// This function is called for the source node of the DFS. |
1137 | 1137 |
void start(const Node& node) {} |
1138 | 1138 |
/// \brief Called when the source node is leaved. |
1139 | 1139 |
/// |
1140 | 1140 |
/// This function is called when the source node is leaved. |
1141 | 1141 |
void stop(const Node& node) {} |
1142 | 1142 |
/// \brief Called when a node is reached first time. |
1143 | 1143 |
/// |
1144 | 1144 |
/// This function is called when a node is reached first time. |
1145 | 1145 |
void reach(const Node& node) {} |
1146 | 1146 |
/// \brief Called when an arc reaches a new node. |
1147 | 1147 |
/// |
1148 | 1148 |
/// This function is called when the DFS finds an arc whose target node |
1149 | 1149 |
/// is not reached yet. |
1150 | 1150 |
void discover(const Arc& arc) {} |
1151 | 1151 |
/// \brief Called when an arc is examined but its target node is |
1152 | 1152 |
/// already discovered. |
1153 | 1153 |
/// |
1154 | 1154 |
/// This function is called when an arc is examined but its target node is |
1155 | 1155 |
/// already discovered. |
1156 | 1156 |
void examine(const Arc& arc) {} |
1157 | 1157 |
/// \brief Called when the DFS steps back from a node. |
1158 | 1158 |
/// |
1159 | 1159 |
/// This function is called when the DFS steps back from a node. |
1160 | 1160 |
void leave(const Node& node) {} |
1161 | 1161 |
/// \brief Called when the DFS steps back on an arc. |
1162 | 1162 |
/// |
1163 | 1163 |
/// This function is called when the DFS steps back on an arc. |
1164 | 1164 |
void backtrack(const Arc& arc) {} |
1165 | 1165 |
}; |
1166 | 1166 |
#else |
1167 |
template <typename |
|
1167 |
template <typename GR> |
|
1168 | 1168 |
struct DfsVisitor { |
1169 |
typedef |
|
1169 |
typedef GR Digraph; |
|
1170 | 1170 |
typedef typename Digraph::Arc Arc; |
1171 | 1171 |
typedef typename Digraph::Node Node; |
1172 | 1172 |
void start(const Node&) {} |
1173 | 1173 |
void stop(const Node&) {} |
1174 | 1174 |
void reach(const Node&) {} |
1175 | 1175 |
void discover(const Arc&) {} |
1176 | 1176 |
void examine(const Arc&) {} |
1177 | 1177 |
void leave(const Node&) {} |
1178 | 1178 |
void backtrack(const Arc&) {} |
1179 | 1179 |
|
1180 | 1180 |
template <typename _Visitor> |
1181 | 1181 |
struct Constraints { |
1182 | 1182 |
void constraints() { |
1183 | 1183 |
Arc arc; |
1184 | 1184 |
Node node; |
1185 | 1185 |
visitor.start(node); |
1186 | 1186 |
visitor.stop(arc); |
1187 | 1187 |
visitor.reach(node); |
1188 | 1188 |
visitor.discover(arc); |
1189 | 1189 |
visitor.examine(arc); |
1190 | 1190 |
visitor.leave(node); |
1191 | 1191 |
visitor.backtrack(arc); |
1192 | 1192 |
} |
1193 | 1193 |
_Visitor& visitor; |
1194 | 1194 |
}; |
1195 | 1195 |
}; |
1196 | 1196 |
#endif |
1197 | 1197 |
|
1198 | 1198 |
/// \brief Default traits class of DfsVisit class. |
1199 | 1199 |
/// |
1200 | 1200 |
/// Default traits class of DfsVisit class. |
1201 | 1201 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1202 |
template<class |
|
1202 |
template<class GR> |
|
1203 | 1203 |
struct DfsVisitDefaultTraits { |
1204 | 1204 |
|
1205 | 1205 |
/// \brief The type of the digraph the algorithm runs on. |
1206 |
typedef |
|
1206 |
typedef GR Digraph; |
|
1207 | 1207 |
|
1208 | 1208 |
/// \brief The type of the map that indicates which nodes are reached. |
1209 | 1209 |
/// |
1210 | 1210 |
/// The type of the map that indicates which nodes are reached. |
1211 | 1211 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1212 | 1212 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1213 | 1213 |
|
1214 | 1214 |
/// \brief Instantiates a ReachedMap. |
1215 | 1215 |
/// |
1216 | 1216 |
/// This function instantiates a ReachedMap. |
1217 | 1217 |
/// \param digraph is the digraph, to which |
1218 | 1218 |
/// we would like to define the ReachedMap. |
1219 | 1219 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1220 | 1220 |
return new ReachedMap(digraph); |
1221 | 1221 |
} |
1222 | 1222 |
|
1223 | 1223 |
}; |
1224 | 1224 |
|
1225 | 1225 |
/// \ingroup search |
1226 | 1226 |
/// |
1227 |
/// \brief |
|
1227 |
/// \brief DFS algorithm class with visitor interface. |
|
1228 | 1228 |
/// |
1229 |
/// This class provides an efficient implementation of the |
|
1229 |
/// This class provides an efficient implementation of the DFS algorithm |
|
1230 | 1230 |
/// with visitor interface. |
1231 | 1231 |
/// |
1232 |
/// The |
|
1232 |
/// The DfsVisit class provides an alternative interface to the Dfs |
|
1233 | 1233 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1234 | 1234 |
/// the member functions of the \c Visitor class on every DFS event. |
1235 | 1235 |
/// |
1236 | 1236 |
/// This interface of the DFS algorithm should be used in special cases |
1237 | 1237 |
/// when extra actions have to be performed in connection with certain |
1238 | 1238 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1239 | 1239 |
/// instead. |
1240 | 1240 |
/// |
1241 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1242 |
/// The default value is |
|
1243 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
1244 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
|
1245 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
1246 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
|
1241 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
1242 |
/// The default type is \ref ListDigraph. |
|
1243 |
/// The value of GR is not used directly by \ref DfsVisit, |
|
1244 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
|
1245 |
/// \tparam VS The Visitor type that is used by the algorithm. |
|
1246 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
|
1247 | 1247 |
/// does not observe the DFS events. If you want to observe the DFS |
1248 | 1248 |
/// events, you should implement your own visitor class. |
1249 |
/// \tparam |
|
1249 |
/// \tparam TR Traits class to set various data types used by the |
|
1250 | 1250 |
/// algorithm. The default traits class is |
1251 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits< |
|
1251 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
|
1252 | 1252 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1253 | 1253 |
/// a DFS visit traits class. |
1254 | 1254 |
#ifdef DOXYGEN |
1255 |
template <typename |
|
1255 |
template <typename GR, typename VS, typename TR> |
|
1256 | 1256 |
#else |
1257 |
template <typename _Digraph = ListDigraph, |
|
1258 |
typename _Visitor = DfsVisitor<_Digraph>, |
|
1259 |
|
|
1257 |
template <typename GR = ListDigraph, |
|
1258 |
typename VS = DfsVisitor<GR>, |
|
1259 |
typename TR = DfsVisitDefaultTraits<GR> > |
|
1260 | 1260 |
#endif |
1261 | 1261 |
class DfsVisit { |
1262 | 1262 |
public: |
1263 | 1263 |
|
1264 | 1264 |
///The traits class. |
1265 |
typedef |
|
1265 |
typedef TR Traits; |
|
1266 | 1266 |
|
1267 | 1267 |
///The type of the digraph the algorithm runs on. |
1268 | 1268 |
typedef typename Traits::Digraph Digraph; |
1269 | 1269 |
|
1270 | 1270 |
///The visitor type used by the algorithm. |
1271 |
typedef |
|
1271 |
typedef VS Visitor; |
|
1272 | 1272 |
|
1273 | 1273 |
///The type of the map that indicates which nodes are reached. |
1274 | 1274 |
typedef typename Traits::ReachedMap ReachedMap; |
1275 | 1275 |
|
1276 | 1276 |
private: |
1277 | 1277 |
|
1278 | 1278 |
typedef typename Digraph::Node Node; |
1279 | 1279 |
typedef typename Digraph::NodeIt NodeIt; |
1280 | 1280 |
typedef typename Digraph::Arc Arc; |
1281 | 1281 |
typedef typename Digraph::OutArcIt OutArcIt; |
1282 | 1282 |
|
1283 | 1283 |
//Pointer to the underlying digraph. |
1284 | 1284 |
const Digraph *_digraph; |
1285 | 1285 |
//Pointer to the visitor object. |
1286 | 1286 |
Visitor *_visitor; |
1287 | 1287 |
//Pointer to the map of reached status of the nodes. |
1288 | 1288 |
ReachedMap *_reached; |
1289 | 1289 |
//Indicates if _reached is locally allocated (true) or not. |
1290 | 1290 |
bool local_reached; |
1291 | 1291 |
|
1292 | 1292 |
std::vector<typename Digraph::Arc> _stack; |
1293 | 1293 |
int _stack_head; |
1294 | 1294 |
|
1295 | 1295 |
//Creates the maps if necessary. |
... | ... |
@@ -52,131 +52,131 @@ |
52 | 52 |
static bool less(const Value& left, const Value& right) { |
53 | 53 |
return left < right; |
54 | 54 |
} |
55 | 55 |
}; |
56 | 56 |
|
57 | 57 |
///Default traits class of Dijkstra class. |
58 | 58 |
|
59 | 59 |
///Default traits class of Dijkstra class. |
60 | 60 |
///\tparam GR The type of the digraph. |
61 | 61 |
///\tparam LM The type of the length map. |
62 | 62 |
template<class GR, class LM> |
63 | 63 |
struct DijkstraDefaultTraits |
64 | 64 |
{ |
65 | 65 |
///The type of the digraph the algorithm runs on. |
66 | 66 |
typedef GR Digraph; |
67 | 67 |
|
68 | 68 |
///The type of the map that stores the arc lengths. |
69 | 69 |
|
70 | 70 |
///The type of the map that stores the arc lengths. |
71 | 71 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
72 | 72 |
typedef LM LengthMap; |
73 | 73 |
///The type of the length of the arcs. |
74 | 74 |
typedef typename LM::Value Value; |
75 | 75 |
|
76 |
/// Operation traits for Dijkstra algorithm. |
|
76 |
/// Operation traits for %Dijkstra algorithm. |
|
77 | 77 |
|
78 | 78 |
/// This class defines the operations that are used in the algorithm. |
79 | 79 |
/// \see DijkstraDefaultOperationTraits |
80 | 80 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
81 | 81 |
|
82 | 82 |
/// The cross reference type used by the heap. |
83 | 83 |
|
84 | 84 |
/// The cross reference type used by the heap. |
85 | 85 |
/// Usually it is \c Digraph::NodeMap<int>. |
86 | 86 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
87 |
///Instantiates a \ |
|
87 |
///Instantiates a \c HeapCrossRef. |
|
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref HeapCrossRef. |
90 | 90 |
/// \param g is the digraph, to which we would like to define the |
91 | 91 |
/// \ref HeapCrossRef. |
92 | 92 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new HeapCrossRef(g); |
95 | 95 |
} |
96 | 96 |
|
97 |
///The heap type used by the Dijkstra algorithm. |
|
97 |
///The heap type used by the %Dijkstra algorithm. |
|
98 | 98 |
|
99 | 99 |
///The heap type used by the Dijkstra algorithm. |
100 | 100 |
/// |
101 | 101 |
///\sa BinHeap |
102 | 102 |
///\sa Dijkstra |
103 | 103 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
104 |
///Instantiates a \ |
|
104 |
///Instantiates a \c Heap. |
|
105 | 105 |
|
106 | 106 |
///This function instantiates a \ref Heap. |
107 | 107 |
static Heap *createHeap(HeapCrossRef& r) |
108 | 108 |
{ |
109 | 109 |
return new Heap(r); |
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
///\brief The type of the map that stores the predecessor |
113 | 113 |
///arcs of the shortest paths. |
114 | 114 |
/// |
115 | 115 |
///The type of the map that stores the predecessor |
116 | 116 |
///arcs of the shortest paths. |
117 | 117 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
118 | 118 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
119 |
///Instantiates a PredMap. |
|
119 |
///Instantiates a \c PredMap. |
|
120 | 120 |
|
121 |
///This function instantiates a PredMap. |
|
121 |
///This function instantiates a \ref PredMap. |
|
122 | 122 |
///\param g is the digraph, to which we would like to define the |
123 |
///PredMap. |
|
123 |
///\ref PredMap. |
|
124 | 124 |
static PredMap *createPredMap(const Digraph &g) |
125 | 125 |
{ |
126 | 126 |
return new PredMap(g); |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
///The type of the map that indicates which nodes are processed. |
130 | 130 |
|
131 | 131 |
///The type of the map that indicates which nodes are processed. |
132 | 132 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
133 | 133 |
///By default it is a NullMap. |
134 | 134 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
135 |
///Instantiates a ProcessedMap. |
|
135 |
///Instantiates a \c ProcessedMap. |
|
136 | 136 |
|
137 |
///This function instantiates a ProcessedMap. |
|
137 |
///This function instantiates a \ref ProcessedMap. |
|
138 | 138 |
///\param g is the digraph, to which |
139 |
///we would like to define the ProcessedMap |
|
139 |
///we would like to define the \ref ProcessedMap. |
|
140 | 140 |
#ifdef DOXYGEN |
141 | 141 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
142 | 142 |
#else |
143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &) |
144 | 144 |
#endif |
145 | 145 |
{ |
146 | 146 |
return new ProcessedMap(); |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
///The type of the map that stores the distances of the nodes. |
150 | 150 |
|
151 | 151 |
///The type of the map that stores the distances of the nodes. |
152 | 152 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
153 | 153 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
154 |
///Instantiates a DistMap. |
|
154 |
///Instantiates a \c DistMap. |
|
155 | 155 |
|
156 |
///This function instantiates a DistMap. |
|
156 |
///This function instantiates a \ref DistMap. |
|
157 | 157 |
///\param g is the digraph, to which we would like to define |
158 |
///the DistMap |
|
158 |
///the \ref DistMap. |
|
159 | 159 |
static DistMap *createDistMap(const Digraph &g) |
160 | 160 |
{ |
161 | 161 |
return new DistMap(g); |
162 | 162 |
} |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
///%Dijkstra algorithm class. |
166 | 166 |
|
167 | 167 |
/// \ingroup shortest_path |
168 | 168 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
169 | 169 |
/// |
170 | 170 |
///The arc lengths are passed to the algorithm using a |
171 | 171 |
///\ref concepts::ReadMap "ReadMap", |
172 | 172 |
///so it is easy to change it to any kind of length. |
173 | 173 |
///The type of the length is determined by the |
174 | 174 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
175 | 175 |
///It is also possible to change the underlying priority heap. |
176 | 176 |
/// |
177 | 177 |
///There is also a \ref dijkstra() "function-type interface" for the |
178 | 178 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
179 | 179 |
///it can be used easier. |
180 | 180 |
/// |
181 | 181 |
///\tparam GR The type of the digraph the algorithm runs on. |
182 | 182 |
///The default type is \ref ListDigraph. |
... | ... |
@@ -195,65 +195,66 @@ |
195 | 195 |
#endif |
196 | 196 |
class Dijkstra { |
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
///The type of the digraph the algorithm runs on. |
200 | 200 |
typedef typename TR::Digraph Digraph; |
201 | 201 |
|
202 | 202 |
///The type of the length of the arcs. |
203 | 203 |
typedef typename TR::LengthMap::Value Value; |
204 | 204 |
///The type of the map that stores the arc lengths. |
205 | 205 |
typedef typename TR::LengthMap LengthMap; |
206 | 206 |
///\brief The type of the map that stores the predecessor arcs of the |
207 | 207 |
///shortest paths. |
208 | 208 |
typedef typename TR::PredMap PredMap; |
209 | 209 |
///The type of the map that stores the distances of the nodes. |
210 | 210 |
typedef typename TR::DistMap DistMap; |
211 | 211 |
///The type of the map that indicates which nodes are processed. |
212 | 212 |
typedef typename TR::ProcessedMap ProcessedMap; |
213 | 213 |
///The type of the paths. |
214 | 214 |
typedef PredMapPath<Digraph, PredMap> Path; |
215 | 215 |
///The cross reference type used for the current heap. |
216 | 216 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
217 | 217 |
///The heap type used by the algorithm. |
218 | 218 |
typedef typename TR::Heap Heap; |
219 |
///The operation traits class |
|
219 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
|
220 |
///of the algorithm. |
|
220 | 221 |
typedef typename TR::OperationTraits OperationTraits; |
221 | 222 |
|
222 | 223 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
223 | 224 |
typedef TR Traits; |
224 | 225 |
|
225 | 226 |
private: |
226 | 227 |
|
227 | 228 |
typedef typename Digraph::Node Node; |
228 | 229 |
typedef typename Digraph::NodeIt NodeIt; |
229 | 230 |
typedef typename Digraph::Arc Arc; |
230 | 231 |
typedef typename Digraph::OutArcIt OutArcIt; |
231 | 232 |
|
232 | 233 |
//Pointer to the underlying digraph. |
233 | 234 |
const Digraph *G; |
234 | 235 |
//Pointer to the length map. |
235 |
const LengthMap * |
|
236 |
const LengthMap *_length; |
|
236 | 237 |
//Pointer to the map of predecessors arcs. |
237 | 238 |
PredMap *_pred; |
238 | 239 |
//Indicates if _pred is locally allocated (true) or not. |
239 | 240 |
bool local_pred; |
240 | 241 |
//Pointer to the map of distances. |
241 | 242 |
DistMap *_dist; |
242 | 243 |
//Indicates if _dist is locally allocated (true) or not. |
243 | 244 |
bool local_dist; |
244 | 245 |
//Pointer to the map of processed status of the nodes. |
245 | 246 |
ProcessedMap *_processed; |
246 | 247 |
//Indicates if _processed is locally allocated (true) or not. |
247 | 248 |
bool local_processed; |
248 | 249 |
//Pointer to the heap cross references. |
249 | 250 |
HeapCrossRef *_heap_cross_ref; |
250 | 251 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
251 | 252 |
bool local_heap_cross_ref; |
252 | 253 |
//Pointer to the heap. |
253 | 254 |
Heap *_heap; |
254 | 255 |
//Indicates if _heap is locally allocated (true) or not. |
255 | 256 |
bool local_heap; |
256 | 257 |
|
257 | 258 |
//Creates the maps if necessary. |
258 | 259 |
void create_maps() |
259 | 260 |
{ |
... | ... |
@@ -276,113 +277,113 @@ |
276 | 277 |
if (!_heap) { |
277 | 278 |
local_heap = true; |
278 | 279 |
_heap = Traits::createHeap(*_heap_cross_ref); |
279 | 280 |
} |
280 | 281 |
} |
281 | 282 |
|
282 | 283 |
public: |
283 | 284 |
|
284 | 285 |
typedef Dijkstra Create; |
285 | 286 |
|
286 | 287 |
///\name Named template parameters |
287 | 288 |
|
288 | 289 |
///@{ |
289 | 290 |
|
290 | 291 |
template <class T> |
291 | 292 |
struct SetPredMapTraits : public Traits { |
292 | 293 |
typedef T PredMap; |
293 | 294 |
static PredMap *createPredMap(const Digraph &) |
294 | 295 |
{ |
295 | 296 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
296 | 297 |
return 0; // ignore warnings |
297 | 298 |
} |
298 | 299 |
}; |
299 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
300 |
///PredMap type. |
|
301 |
///\c PredMap type. |
|
301 | 302 |
/// |
302 | 303 |
///\ref named-templ-param "Named parameter" for setting |
303 |
///PredMap type. |
|
304 |
///\c PredMap type. |
|
304 | 305 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
305 | 306 |
template <class T> |
306 | 307 |
struct SetPredMap |
307 | 308 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
308 | 309 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
309 | 310 |
}; |
310 | 311 |
|
311 | 312 |
template <class T> |
312 | 313 |
struct SetDistMapTraits : public Traits { |
313 | 314 |
typedef T DistMap; |
314 | 315 |
static DistMap *createDistMap(const Digraph &) |
315 | 316 |
{ |
316 | 317 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
317 | 318 |
return 0; // ignore warnings |
318 | 319 |
} |
319 | 320 |
}; |
320 | 321 |
///\brief \ref named-templ-param "Named parameter" for setting |
321 |
///DistMap type. |
|
322 |
///\c DistMap type. |
|
322 | 323 |
/// |
323 | 324 |
///\ref named-templ-param "Named parameter" for setting |
324 |
///DistMap type. |
|
325 |
///\c DistMap type. |
|
325 | 326 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
326 | 327 |
template <class T> |
327 | 328 |
struct SetDistMap |
328 | 329 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
329 | 330 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
330 | 331 |
}; |
331 | 332 |
|
332 | 333 |
template <class T> |
333 | 334 |
struct SetProcessedMapTraits : public Traits { |
334 | 335 |
typedef T ProcessedMap; |
335 | 336 |
static ProcessedMap *createProcessedMap(const Digraph &) |
336 | 337 |
{ |
337 | 338 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
338 | 339 |
return 0; // ignore warnings |
339 | 340 |
} |
340 | 341 |
}; |
341 | 342 |
///\brief \ref named-templ-param "Named parameter" for setting |
342 |
///ProcessedMap type. |
|
343 |
///\c ProcessedMap type. |
|
343 | 344 |
/// |
344 | 345 |
///\ref named-templ-param "Named parameter" for setting |
345 |
///ProcessedMap type. |
|
346 |
///\c ProcessedMap type. |
|
346 | 347 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
347 | 348 |
template <class T> |
348 | 349 |
struct SetProcessedMap |
349 | 350 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
350 | 351 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
351 | 352 |
}; |
352 | 353 |
|
353 | 354 |
struct SetStandardProcessedMapTraits : public Traits { |
354 | 355 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
355 | 356 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
356 | 357 |
{ |
357 | 358 |
return new ProcessedMap(g); |
358 | 359 |
} |
359 | 360 |
}; |
360 | 361 |
///\brief \ref named-templ-param "Named parameter" for setting |
361 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
362 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
362 | 363 |
/// |
363 | 364 |
///\ref named-templ-param "Named parameter" for setting |
364 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
365 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
365 | 366 |
///If you don't set it explicitly, it will be automatically allocated. |
366 | 367 |
struct SetStandardProcessedMap |
367 | 368 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
368 | 369 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
369 | 370 |
Create; |
370 | 371 |
}; |
371 | 372 |
|
372 | 373 |
template <class H, class CR> |
373 | 374 |
struct SetHeapTraits : public Traits { |
374 | 375 |
typedef CR HeapCrossRef; |
375 | 376 |
typedef H Heap; |
376 | 377 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
377 | 378 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
378 | 379 |
return 0; // ignore warnings |
379 | 380 |
} |
380 | 381 |
static Heap *createHeap(HeapCrossRef &) |
381 | 382 |
{ |
382 | 383 |
LEMON_ASSERT(false, "Heap is not initialized"); |
383 | 384 |
return 0; // ignore warnings |
384 | 385 |
} |
385 | 386 |
}; |
386 | 387 |
///\brief \ref named-templ-param "Named parameter" for setting |
387 | 388 |
///heap and cross reference types |
388 | 389 |
/// |
... | ... |
@@ -418,95 +419,95 @@ |
418 | 419 |
///They should have standard constructor interfaces to be able to |
419 | 420 |
///automatically created by the algorithm (i.e. the digraph should be |
420 | 421 |
///passed to the constructor of the cross reference and the cross |
421 | 422 |
///reference should be passed to the constructor of the heap). |
422 | 423 |
///However external heap and cross reference objects could also be |
423 | 424 |
///passed to the algorithm using the \ref heap() function before |
424 | 425 |
///calling \ref run(Node) "run()" or \ref init(). |
425 | 426 |
///\sa SetHeap |
426 | 427 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
427 | 428 |
struct SetStandardHeap |
428 | 429 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
429 | 430 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
430 | 431 |
Create; |
431 | 432 |
}; |
432 | 433 |
|
433 | 434 |
template <class T> |
434 | 435 |
struct SetOperationTraitsTraits : public Traits { |
435 | 436 |
typedef T OperationTraits; |
436 | 437 |
}; |
437 | 438 |
|
438 | 439 |
/// \brief \ref named-templ-param "Named parameter" for setting |
439 | 440 |
///\c OperationTraits type |
440 | 441 |
/// |
441 | 442 |
///\ref named-templ-param "Named parameter" for setting |
442 |
///\ |
|
443 |
///\c OperationTraits type. |
|
443 | 444 |
template <class T> |
444 | 445 |
struct SetOperationTraits |
445 | 446 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
446 | 447 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
447 | 448 |
Create; |
448 | 449 |
}; |
449 | 450 |
|
450 | 451 |
///@} |
451 | 452 |
|
452 | 453 |
protected: |
453 | 454 |
|
454 | 455 |
Dijkstra() {} |
455 | 456 |
|
456 | 457 |
public: |
457 | 458 |
|
458 | 459 |
///Constructor. |
459 | 460 |
|
460 | 461 |
///Constructor. |
461 |
///\param _g The digraph the algorithm runs on. |
|
462 |
///\param _length The length map used by the algorithm. |
|
463 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
|
464 |
G(&_g), length(&_length), |
|
462 |
///\param g The digraph the algorithm runs on. |
|
463 |
///\param length The length map used by the algorithm. |
|
464 |
Dijkstra(const Digraph& g, const LengthMap& length) : |
|
465 |
G(&g), _length(&length), |
|
465 | 466 |
_pred(NULL), local_pred(false), |
466 | 467 |
_dist(NULL), local_dist(false), |
467 | 468 |
_processed(NULL), local_processed(false), |
468 | 469 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
469 | 470 |
_heap(NULL), local_heap(false) |
470 | 471 |
{ } |
471 | 472 |
|
472 | 473 |
///Destructor. |
473 | 474 |
~Dijkstra() |
474 | 475 |
{ |
475 | 476 |
if(local_pred) delete _pred; |
476 | 477 |
if(local_dist) delete _dist; |
477 | 478 |
if(local_processed) delete _processed; |
478 | 479 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
479 | 480 |
if(local_heap) delete _heap; |
480 | 481 |
} |
481 | 482 |
|
482 | 483 |
///Sets the length map. |
483 | 484 |
|
484 | 485 |
///Sets the length map. |
485 | 486 |
///\return <tt> (*this) </tt> |
486 | 487 |
Dijkstra &lengthMap(const LengthMap &m) |
487 | 488 |
{ |
488 |
|
|
489 |
_length = &m; |
|
489 | 490 |
return *this; |
490 | 491 |
} |
491 | 492 |
|
492 | 493 |
///Sets the map that stores the predecessor arcs. |
493 | 494 |
|
494 | 495 |
///Sets the map that stores the predecessor arcs. |
495 | 496 |
///If you don't use this function before calling \ref run(Node) "run()" |
496 | 497 |
///or \ref init(), an instance will be allocated automatically. |
497 | 498 |
///The destructor deallocates this automatically allocated map, |
498 | 499 |
///of course. |
499 | 500 |
///\return <tt> (*this) </tt> |
500 | 501 |
Dijkstra &predMap(PredMap &m) |
501 | 502 |
{ |
502 | 503 |
if(local_pred) { |
503 | 504 |
delete _pred; |
504 | 505 |
local_pred=false; |
505 | 506 |
} |
506 | 507 |
_pred = &m; |
507 | 508 |
return *this; |
508 | 509 |
} |
509 | 510 |
|
510 | 511 |
///Sets the map that indicates which nodes are processed. |
511 | 512 |
|
512 | 513 |
///Sets the map that indicates which nodes are processed. |
... | ... |
@@ -617,54 +618,54 @@ |
617 | 618 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
618 | 619 |
_heap->set(s,dst); |
619 | 620 |
_pred->set(s,INVALID); |
620 | 621 |
} |
621 | 622 |
} |
622 | 623 |
|
623 | 624 |
///Processes the next node in the priority heap |
624 | 625 |
|
625 | 626 |
///Processes the next node in the priority heap. |
626 | 627 |
/// |
627 | 628 |
///\return The processed node. |
628 | 629 |
/// |
629 | 630 |
///\warning The priority heap must not be empty. |
630 | 631 |
Node processNextNode() |
631 | 632 |
{ |
632 | 633 |
Node v=_heap->top(); |
633 | 634 |
Value oldvalue=_heap->prio(); |
634 | 635 |
_heap->pop(); |
635 | 636 |
finalizeNodeData(v,oldvalue); |
636 | 637 |
|
637 | 638 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
638 | 639 |
Node w=G->target(e); |
639 | 640 |
switch(_heap->state(w)) { |
640 | 641 |
case Heap::PRE_HEAP: |
641 |
_heap->push(w,OperationTraits::plus(oldvalue, (* |
|
642 |
_heap->push(w,OperationTraits::plus(oldvalue, (*_length)[e])); |
|
642 | 643 |
_pred->set(w,e); |
643 | 644 |
break; |
644 | 645 |
case Heap::IN_HEAP: |
645 | 646 |
{ |
646 |
Value newvalue = OperationTraits::plus(oldvalue, (* |
|
647 |
Value newvalue = OperationTraits::plus(oldvalue, (*_length)[e]); |
|
647 | 648 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
648 | 649 |
_heap->decrease(w, newvalue); |
649 | 650 |
_pred->set(w,e); |
650 | 651 |
} |
651 | 652 |
} |
652 | 653 |
break; |
653 | 654 |
case Heap::POST_HEAP: |
654 | 655 |
break; |
655 | 656 |
} |
656 | 657 |
} |
657 | 658 |
return v; |
658 | 659 |
} |
659 | 660 |
|
660 | 661 |
///The next node to be processed. |
661 | 662 |
|
662 | 663 |
///Returns the next node to be processed or \c INVALID if the |
663 | 664 |
///priority heap is empty. |
664 | 665 |
Node nextNode() const |
665 | 666 |
{ |
666 | 667 |
return !_heap->empty()?_heap->top():INVALID; |
667 | 668 |
} |
668 | 669 |
|
669 | 670 |
///Returns \c false if there are nodes to be processed. |
670 | 671 |
... | ... |
@@ -10,61 +10,61 @@ |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_PREFLOW_H |
20 | 20 |
#define LEMON_PREFLOW_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
/// \file |
26 | 26 |
/// \ingroup max_flow |
27 | 27 |
/// \brief Implementation of the preflow algorithm. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Preflow class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Preflow class. |
34 |
/// \tparam _Digraph Digraph type. |
|
35 |
/// \tparam _CapacityMap Capacity map type. |
|
36 |
|
|
34 |
/// \tparam GR Digraph type. |
|
35 |
/// \tparam CM Capacity map type. |
|
36 |
template <typename GR, typename CM> |
|
37 | 37 |
struct PreflowDefaultTraits { |
38 | 38 |
|
39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
40 |
typedef |
|
40 |
typedef GR Digraph; |
|
41 | 41 |
|
42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
43 | 43 |
/// |
44 | 44 |
/// The type of the map that stores the arc capacities. |
45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 |
typedef |
|
46 |
typedef CM CapacityMap; |
|
47 | 47 |
|
48 | 48 |
/// \brief The type of the flow values. |
49 | 49 |
typedef typename CapacityMap::Value Value; |
50 | 50 |
|
51 | 51 |
/// \brief The type of the map that stores the flow values. |
52 | 52 |
/// |
53 | 53 |
/// The type of the map that stores the flow values. |
54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
56 | 56 |
|
57 | 57 |
/// \brief Instantiates a FlowMap. |
58 | 58 |
/// |
59 | 59 |
/// This function instantiates a \ref FlowMap. |
60 | 60 |
/// \param digraph The digraph, to which we would like to define |
61 | 61 |
/// the flow map. |
62 | 62 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
63 | 63 |
return new FlowMap(digraph); |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
/// \brief The elevator type used by Preflow algorithm. |
67 | 67 |
/// |
68 | 68 |
/// The elevator type used by Preflow algorithm. |
69 | 69 |
/// |
70 | 70 |
/// \sa Elevator |
... | ... |
@@ -83,63 +83,63 @@ |
83 | 83 |
|
84 | 84 |
/// \brief The tolerance used by the algorithm |
85 | 85 |
/// |
86 | 86 |
/// The tolerance used by the algorithm to handle inexact computation. |
87 | 87 |
typedef lemon::Tolerance<Value> Tolerance; |
88 | 88 |
|
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
|
92 | 92 |
/// \ingroup max_flow |
93 | 93 |
/// |
94 | 94 |
/// \brief %Preflow algorithm class. |
95 | 95 |
/// |
96 | 96 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
97 | 97 |
/// \e push-relabel algorithm producing a flow of maximum value in a |
98 | 98 |
/// digraph. The preflow algorithms are the fastest known maximum |
99 | 99 |
/// flow algorithms. The current implementation use a mixture of the |
100 | 100 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
101 | 101 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
102 | 102 |
/// |
103 | 103 |
/// The algorithm consists of two phases. After the first phase |
104 | 104 |
/// the maximum flow value and the minimum cut is obtained. The |
105 | 105 |
/// second phase constructs a feasible maximum flow on each arc. |
106 | 106 |
/// |
107 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
108 |
/// \tparam _CapacityMap The type of the capacity map. The default map |
|
109 |
/// type |
|
107 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
108 |
/// \tparam CM The type of the capacity map. The default map |
|
109 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
110 | 110 |
#ifdef DOXYGEN |
111 |
template <typename |
|
111 |
template <typename GR, typename CM, typename TR> |
|
112 | 112 |
#else |
113 |
template <typename _Digraph, |
|
114 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
115 |
|
|
113 |
template <typename GR, |
|
114 |
typename CM = typename GR::template ArcMap<int>, |
|
115 |
typename TR = PreflowDefaultTraits<GR, CM> > |
|
116 | 116 |
#endif |
117 | 117 |
class Preflow { |
118 | 118 |
public: |
119 | 119 |
|
120 | 120 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
121 |
typedef |
|
121 |
typedef TR Traits; |
|
122 | 122 |
///The type of the digraph the algorithm runs on. |
123 | 123 |
typedef typename Traits::Digraph Digraph; |
124 | 124 |
///The type of the capacity map. |
125 | 125 |
typedef typename Traits::CapacityMap CapacityMap; |
126 | 126 |
///The type of the flow values. |
127 | 127 |
typedef typename Traits::Value Value; |
128 | 128 |
|
129 | 129 |
///The type of the flow map. |
130 | 130 |
typedef typename Traits::FlowMap FlowMap; |
131 | 131 |
///The type of the elevator. |
132 | 132 |
typedef typename Traits::Elevator Elevator; |
133 | 133 |
///The type of the tolerance. |
134 | 134 |
typedef typename Traits::Tolerance Tolerance; |
135 | 135 |
|
136 | 136 |
private: |
137 | 137 |
|
138 | 138 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
139 | 139 |
|
140 | 140 |
const Digraph& _graph; |
141 | 141 |
const CapacityMap* _capacity; |
142 | 142 |
|
143 | 143 |
int _node_num; |
144 | 144 |
|
145 | 145 |
Node _source, _target; |
0 comments (0 inline)