0
6
0
... | ... |
@@ -208,192 +208,196 @@ |
208 | 208 |
const IntVector &_target; |
209 | 209 |
const CostVector &_cost; |
210 | 210 |
const ValueVector &_res_cap; |
211 | 211 |
const ValueVector &_excess; |
212 | 212 |
CostVector &_pi; |
213 | 213 |
IntVector &_pred; |
214 | 214 |
|
215 | 215 |
IntVector _proc_nodes; |
216 | 216 |
CostVector _dist; |
217 | 217 |
|
218 | 218 |
public: |
219 | 219 |
|
220 | 220 |
ResidualDijkstra(CapacityScaling& cs) : |
221 | 221 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
222 | 222 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
223 | 223 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
224 | 224 |
_pred(cs._pred), _dist(cs._node_num) |
225 | 225 |
{} |
226 | 226 |
|
227 | 227 |
int run(int s, Value delta = 1) { |
228 | 228 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
229 | 229 |
Heap heap(heap_cross_ref); |
230 | 230 |
heap.push(s, 0); |
231 | 231 |
_pred[s] = -1; |
232 | 232 |
_proc_nodes.clear(); |
233 | 233 |
|
234 | 234 |
// Process nodes |
235 | 235 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
236 | 236 |
int u = heap.top(), v; |
237 | 237 |
Cost d = heap.prio() + _pi[u], dn; |
238 | 238 |
_dist[u] = heap.prio(); |
239 | 239 |
_proc_nodes.push_back(u); |
240 | 240 |
heap.pop(); |
241 | 241 |
|
242 | 242 |
// Traverse outgoing residual arcs |
243 | 243 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
244 | 244 |
for (int a = _first_out[u]; a != last_out; ++a) { |
245 | 245 |
if (_res_cap[a] < delta) continue; |
246 | 246 |
v = _target[a]; |
247 | 247 |
switch (heap.state(v)) { |
248 | 248 |
case Heap::PRE_HEAP: |
249 | 249 |
heap.push(v, d + _cost[a] - _pi[v]); |
250 | 250 |
_pred[v] = a; |
251 | 251 |
break; |
252 | 252 |
case Heap::IN_HEAP: |
253 | 253 |
dn = d + _cost[a] - _pi[v]; |
254 | 254 |
if (dn < heap[v]) { |
255 | 255 |
heap.decrease(v, dn); |
256 | 256 |
_pred[v] = a; |
257 | 257 |
} |
258 | 258 |
break; |
259 | 259 |
case Heap::POST_HEAP: |
260 | 260 |
break; |
261 | 261 |
} |
262 | 262 |
} |
263 | 263 |
} |
264 | 264 |
if (heap.empty()) return -1; |
265 | 265 |
|
266 | 266 |
// Update potentials of processed nodes |
267 | 267 |
int t = heap.top(); |
268 | 268 |
Cost dt = heap.prio(); |
269 | 269 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
270 | 270 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
return t; |
274 | 274 |
} |
275 | 275 |
|
276 | 276 |
}; //class ResidualDijkstra |
277 | 277 |
|
278 | 278 |
public: |
279 | 279 |
|
280 | 280 |
/// \name Named Template Parameters |
281 | 281 |
/// @{ |
282 | 282 |
|
283 | 283 |
template <typename T> |
284 | 284 |
struct SetHeapTraits : public Traits { |
285 | 285 |
typedef T Heap; |
286 | 286 |
}; |
287 | 287 |
|
288 | 288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
289 | 289 |
/// \c Heap type. |
290 | 290 |
/// |
291 | 291 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
292 | 292 |
/// type, which is used for internal Dijkstra computations. |
293 | 293 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
294 | 294 |
/// its priority type must be \c Cost and its cross reference type |
295 | 295 |
/// must be \ref RangeMap "RangeMap<int>". |
296 | 296 |
template <typename T> |
297 | 297 |
struct SetHeap |
298 | 298 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > { |
299 | 299 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
300 | 300 |
}; |
301 | 301 |
|
302 | 302 |
/// @} |
303 | 303 |
|
304 |
protected: |
|
305 |
|
|
306 |
CapacityScaling() {} |
|
307 |
|
|
304 | 308 |
public: |
305 | 309 |
|
306 | 310 |
/// \brief Constructor. |
307 | 311 |
/// |
308 | 312 |
/// The constructor of the class. |
309 | 313 |
/// |
310 | 314 |
/// \param graph The digraph the algorithm runs on. |
311 | 315 |
CapacityScaling(const GR& graph) : |
312 | 316 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
313 | 317 |
INF(std::numeric_limits<Value>::has_infinity ? |
314 | 318 |
std::numeric_limits<Value>::infinity() : |
315 | 319 |
std::numeric_limits<Value>::max()) |
316 | 320 |
{ |
317 | 321 |
// Check the number types |
318 | 322 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
319 | 323 |
"The flow type of CapacityScaling must be signed"); |
320 | 324 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
321 | 325 |
"The cost type of CapacityScaling must be signed"); |
322 | 326 |
|
323 | 327 |
// Reset data structures |
324 | 328 |
reset(); |
325 | 329 |
} |
326 | 330 |
|
327 | 331 |
/// \name Parameters |
328 | 332 |
/// The parameters of the algorithm can be specified using these |
329 | 333 |
/// functions. |
330 | 334 |
|
331 | 335 |
/// @{ |
332 | 336 |
|
333 | 337 |
/// \brief Set the lower bounds on the arcs. |
334 | 338 |
/// |
335 | 339 |
/// This function sets the lower bounds on the arcs. |
336 | 340 |
/// If it is not used before calling \ref run(), the lower bounds |
337 | 341 |
/// will be set to zero on all arcs. |
338 | 342 |
/// |
339 | 343 |
/// \param map An arc map storing the lower bounds. |
340 | 344 |
/// Its \c Value type must be convertible to the \c Value type |
341 | 345 |
/// of the algorithm. |
342 | 346 |
/// |
343 | 347 |
/// \return <tt>(*this)</tt> |
344 | 348 |
template <typename LowerMap> |
345 | 349 |
CapacityScaling& lowerMap(const LowerMap& map) { |
346 | 350 |
_have_lower = true; |
347 | 351 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
348 | 352 |
_lower[_arc_idf[a]] = map[a]; |
349 | 353 |
_lower[_arc_idb[a]] = map[a]; |
350 | 354 |
} |
351 | 355 |
return *this; |
352 | 356 |
} |
353 | 357 |
|
354 | 358 |
/// \brief Set the upper bounds (capacities) on the arcs. |
355 | 359 |
/// |
356 | 360 |
/// This function sets the upper bounds (capacities) on the arcs. |
357 | 361 |
/// If it is not used before calling \ref run(), the upper bounds |
358 | 362 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
359 | 363 |
/// unbounded from above). |
360 | 364 |
/// |
361 | 365 |
/// \param map An arc map storing the upper bounds. |
362 | 366 |
/// Its \c Value type must be convertible to the \c Value type |
363 | 367 |
/// of the algorithm. |
364 | 368 |
/// |
365 | 369 |
/// \return <tt>(*this)</tt> |
366 | 370 |
template<typename UpperMap> |
367 | 371 |
CapacityScaling& upperMap(const UpperMap& map) { |
368 | 372 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
369 | 373 |
_upper[_arc_idf[a]] = map[a]; |
370 | 374 |
} |
371 | 375 |
return *this; |
372 | 376 |
} |
373 | 377 |
|
374 | 378 |
/// \brief Set the costs of the arcs. |
375 | 379 |
/// |
376 | 380 |
/// This function sets the costs of the arcs. |
377 | 381 |
/// If it is not used before calling \ref run(), the costs |
378 | 382 |
/// will be set to \c 1 on all arcs. |
379 | 383 |
/// |
380 | 384 |
/// \param map An arc map storing the costs. |
381 | 385 |
/// Its \c Value type must be convertible to the \c Cost type |
382 | 386 |
/// of the algorithm. |
383 | 387 |
/// |
384 | 388 |
/// \return <tt>(*this)</tt> |
385 | 389 |
template<typename CostMap> |
386 | 390 |
CapacityScaling& costMap(const CostMap& map) { |
387 | 391 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
388 | 392 |
_cost[_arc_idf[a]] = map[a]; |
389 | 393 |
_cost[_arc_idb[a]] = -map[a]; |
390 | 394 |
} |
391 | 395 |
return *this; |
392 | 396 |
} |
393 | 397 |
|
394 | 398 |
/// \brief Set the supply values of the nodes. |
395 | 399 |
/// |
396 | 400 |
/// This function sets the supply values of the nodes. |
397 | 401 |
/// If neither this function nor \ref stSupply() is used before |
398 | 402 |
/// calling \ref run(), the supply of each node will be set to zero. |
399 | 403 |
/// |
... | ... |
@@ -232,192 +232,196 @@ |
232 | 232 |
private: |
233 | 233 |
std::vector<Value>& _v; |
234 | 234 |
}; |
235 | 235 |
|
236 | 236 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
237 | 237 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
238 | 238 |
|
239 | 239 |
private: |
240 | 240 |
|
241 | 241 |
// Data related to the underlying digraph |
242 | 242 |
const GR &_graph; |
243 | 243 |
int _node_num; |
244 | 244 |
int _arc_num; |
245 | 245 |
int _res_node_num; |
246 | 246 |
int _res_arc_num; |
247 | 247 |
int _root; |
248 | 248 |
|
249 | 249 |
// Parameters of the problem |
250 | 250 |
bool _have_lower; |
251 | 251 |
Value _sum_supply; |
252 | 252 |
int _sup_node_num; |
253 | 253 |
|
254 | 254 |
// Data structures for storing the digraph |
255 | 255 |
IntNodeMap _node_id; |
256 | 256 |
IntArcMap _arc_idf; |
257 | 257 |
IntArcMap _arc_idb; |
258 | 258 |
IntVector _first_out; |
259 | 259 |
BoolVector _forward; |
260 | 260 |
IntVector _source; |
261 | 261 |
IntVector _target; |
262 | 262 |
IntVector _reverse; |
263 | 263 |
|
264 | 264 |
// Node and arc data |
265 | 265 |
ValueVector _lower; |
266 | 266 |
ValueVector _upper; |
267 | 267 |
CostVector _scost; |
268 | 268 |
ValueVector _supply; |
269 | 269 |
|
270 | 270 |
ValueVector _res_cap; |
271 | 271 |
LargeCostVector _cost; |
272 | 272 |
LargeCostVector _pi; |
273 | 273 |
ValueVector _excess; |
274 | 274 |
IntVector _next_out; |
275 | 275 |
std::deque<int> _active_nodes; |
276 | 276 |
|
277 | 277 |
// Data for scaling |
278 | 278 |
LargeCost _epsilon; |
279 | 279 |
int _alpha; |
280 | 280 |
|
281 | 281 |
IntVector _buckets; |
282 | 282 |
IntVector _bucket_next; |
283 | 283 |
IntVector _bucket_prev; |
284 | 284 |
IntVector _rank; |
285 | 285 |
int _max_rank; |
286 | 286 |
|
287 | 287 |
// Data for a StaticDigraph structure |
288 | 288 |
typedef std::pair<int, int> IntPair; |
289 | 289 |
StaticDigraph _sgr; |
290 | 290 |
std::vector<IntPair> _arc_vec; |
291 | 291 |
std::vector<LargeCost> _cost_vec; |
292 | 292 |
LargeCostArcMap _cost_map; |
293 | 293 |
LargeCostNodeMap _pi_map; |
294 | 294 |
|
295 | 295 |
public: |
296 | 296 |
|
297 | 297 |
/// \brief Constant for infinite upper bounds (capacities). |
298 | 298 |
/// |
299 | 299 |
/// Constant for infinite upper bounds (capacities). |
300 | 300 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
301 | 301 |
/// \c std::numeric_limits<Value>::max() otherwise. |
302 | 302 |
const Value INF; |
303 | 303 |
|
304 | 304 |
public: |
305 | 305 |
|
306 | 306 |
/// \name Named Template Parameters |
307 | 307 |
/// @{ |
308 | 308 |
|
309 | 309 |
template <typename T> |
310 | 310 |
struct SetLargeCostTraits : public Traits { |
311 | 311 |
typedef T LargeCost; |
312 | 312 |
}; |
313 | 313 |
|
314 | 314 |
/// \brief \ref named-templ-param "Named parameter" for setting |
315 | 315 |
/// \c LargeCost type. |
316 | 316 |
/// |
317 | 317 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
318 | 318 |
/// type, which is used for internal computations in the algorithm. |
319 | 319 |
/// \c Cost must be convertible to \c LargeCost. |
320 | 320 |
template <typename T> |
321 | 321 |
struct SetLargeCost |
322 | 322 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > { |
323 | 323 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
324 | 324 |
}; |
325 | 325 |
|
326 | 326 |
/// @} |
327 | 327 |
|
328 |
protected: |
|
329 |
|
|
330 |
CostScaling() {} |
|
331 |
|
|
328 | 332 |
public: |
329 | 333 |
|
330 | 334 |
/// \brief Constructor. |
331 | 335 |
/// |
332 | 336 |
/// The constructor of the class. |
333 | 337 |
/// |
334 | 338 |
/// \param graph The digraph the algorithm runs on. |
335 | 339 |
CostScaling(const GR& graph) : |
336 | 340 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
337 | 341 |
_cost_map(_cost_vec), _pi_map(_pi), |
338 | 342 |
INF(std::numeric_limits<Value>::has_infinity ? |
339 | 343 |
std::numeric_limits<Value>::infinity() : |
340 | 344 |
std::numeric_limits<Value>::max()) |
341 | 345 |
{ |
342 | 346 |
// Check the number types |
343 | 347 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
344 | 348 |
"The flow type of CostScaling must be signed"); |
345 | 349 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
346 | 350 |
"The cost type of CostScaling must be signed"); |
347 | 351 |
|
348 | 352 |
// Reset data structures |
349 | 353 |
reset(); |
350 | 354 |
} |
351 | 355 |
|
352 | 356 |
/// \name Parameters |
353 | 357 |
/// The parameters of the algorithm can be specified using these |
354 | 358 |
/// functions. |
355 | 359 |
|
356 | 360 |
/// @{ |
357 | 361 |
|
358 | 362 |
/// \brief Set the lower bounds on the arcs. |
359 | 363 |
/// |
360 | 364 |
/// This function sets the lower bounds on the arcs. |
361 | 365 |
/// If it is not used before calling \ref run(), the lower bounds |
362 | 366 |
/// will be set to zero on all arcs. |
363 | 367 |
/// |
364 | 368 |
/// \param map An arc map storing the lower bounds. |
365 | 369 |
/// Its \c Value type must be convertible to the \c Value type |
366 | 370 |
/// of the algorithm. |
367 | 371 |
/// |
368 | 372 |
/// \return <tt>(*this)</tt> |
369 | 373 |
template <typename LowerMap> |
370 | 374 |
CostScaling& lowerMap(const LowerMap& map) { |
371 | 375 |
_have_lower = true; |
372 | 376 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
373 | 377 |
_lower[_arc_idf[a]] = map[a]; |
374 | 378 |
_lower[_arc_idb[a]] = map[a]; |
375 | 379 |
} |
376 | 380 |
return *this; |
377 | 381 |
} |
378 | 382 |
|
379 | 383 |
/// \brief Set the upper bounds (capacities) on the arcs. |
380 | 384 |
/// |
381 | 385 |
/// This function sets the upper bounds (capacities) on the arcs. |
382 | 386 |
/// If it is not used before calling \ref run(), the upper bounds |
383 | 387 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
384 | 388 |
/// unbounded from above). |
385 | 389 |
/// |
386 | 390 |
/// \param map An arc map storing the upper bounds. |
387 | 391 |
/// Its \c Value type must be convertible to the \c Value type |
388 | 392 |
/// of the algorithm. |
389 | 393 |
/// |
390 | 394 |
/// \return <tt>(*this)</tt> |
391 | 395 |
template<typename UpperMap> |
392 | 396 |
CostScaling& upperMap(const UpperMap& map) { |
393 | 397 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
394 | 398 |
_upper[_arc_idf[a]] = map[a]; |
395 | 399 |
} |
396 | 400 |
return *this; |
397 | 401 |
} |
398 | 402 |
|
399 | 403 |
/// \brief Set the costs of the arcs. |
400 | 404 |
/// |
401 | 405 |
/// This function sets the costs of the arcs. |
402 | 406 |
/// If it is not used before calling \ref run(), the costs |
403 | 407 |
/// will be set to \c 1 on all arcs. |
404 | 408 |
/// |
405 | 409 |
/// \param map An arc map storing the costs. |
406 | 410 |
/// Its \c Value type must be convertible to the \c Cost type |
407 | 411 |
/// of the algorithm. |
408 | 412 |
/// |
409 | 413 |
/// \return <tt>(*this)</tt> |
410 | 414 |
template<typename CostMap> |
411 | 415 |
CostScaling& costMap(const CostMap& map) { |
412 | 416 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
413 | 417 |
_scost[_arc_idf[a]] = map[a]; |
414 | 418 |
_scost[_arc_idb[a]] = -map[a]; |
415 | 419 |
} |
416 | 420 |
return *this; |
417 | 421 |
} |
418 | 422 |
|
419 | 423 |
/// \brief Set the supply values of the nodes. |
420 | 424 |
/// |
421 | 425 |
/// This function sets the supply values of the nodes. |
422 | 426 |
/// If neither this function nor \ref stSupply() is used before |
423 | 427 |
/// calling \ref run(), the supply of each node will be set to zero. |
... | ... |
@@ -148,192 +148,196 @@ |
148 | 148 |
|
149 | 149 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
152 | 152 |
private: |
153 | 153 |
|
154 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
155 | 155 |
|
156 | 156 |
// Data sturcture for path data |
157 | 157 |
struct PathData |
158 | 158 |
{ |
159 | 159 |
LargeValue dist; |
160 | 160 |
Arc pred; |
161 | 161 |
PathData(LargeValue d, Arc p = INVALID) : |
162 | 162 |
dist(d), pred(p) {} |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
166 | 166 |
PathDataNodeMap; |
167 | 167 |
|
168 | 168 |
private: |
169 | 169 |
|
170 | 170 |
// The digraph the algorithm runs on |
171 | 171 |
const Digraph &_gr; |
172 | 172 |
// The length of the arcs |
173 | 173 |
const LengthMap &_length; |
174 | 174 |
|
175 | 175 |
// Data for storing the strongly connected components |
176 | 176 |
int _comp_num; |
177 | 177 |
typename Digraph::template NodeMap<int> _comp; |
178 | 178 |
std::vector<std::vector<Node> > _comp_nodes; |
179 | 179 |
std::vector<Node>* _nodes; |
180 | 180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
181 | 181 |
|
182 | 182 |
// Data for the found cycles |
183 | 183 |
bool _curr_found, _best_found; |
184 | 184 |
LargeValue _curr_length, _best_length; |
185 | 185 |
int _curr_size, _best_size; |
186 | 186 |
Node _curr_node, _best_node; |
187 | 187 |
int _curr_level, _best_level; |
188 | 188 |
|
189 | 189 |
Path *_cycle_path; |
190 | 190 |
bool _local_path; |
191 | 191 |
|
192 | 192 |
// Node map for storing path data |
193 | 193 |
PathDataNodeMap _data; |
194 | 194 |
// The processed nodes in the last round |
195 | 195 |
std::vector<Node> _process; |
196 | 196 |
|
197 | 197 |
Tolerance _tolerance; |
198 | 198 |
|
199 | 199 |
// Infinite constant |
200 | 200 |
const LargeValue INF; |
201 | 201 |
|
202 | 202 |
public: |
203 | 203 |
|
204 | 204 |
/// \name Named Template Parameters |
205 | 205 |
/// @{ |
206 | 206 |
|
207 | 207 |
template <typename T> |
208 | 208 |
struct SetLargeValueTraits : public Traits { |
209 | 209 |
typedef T LargeValue; |
210 | 210 |
typedef lemon::Tolerance<T> Tolerance; |
211 | 211 |
}; |
212 | 212 |
|
213 | 213 |
/// \brief \ref named-templ-param "Named parameter" for setting |
214 | 214 |
/// \c LargeValue type. |
215 | 215 |
/// |
216 | 216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
217 | 217 |
/// type. It is used for internal computations in the algorithm. |
218 | 218 |
template <typename T> |
219 | 219 |
struct SetLargeValue |
220 | 220 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > { |
221 | 221 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
222 | 222 |
}; |
223 | 223 |
|
224 | 224 |
template <typename T> |
225 | 225 |
struct SetPathTraits : public Traits { |
226 | 226 |
typedef T Path; |
227 | 227 |
}; |
228 | 228 |
|
229 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
230 | 230 |
/// \c %Path type. |
231 | 231 |
/// |
232 | 232 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
233 | 233 |
/// type of the found cycles. |
234 | 234 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
235 | 235 |
/// and it must have an \c addFront() function. |
236 | 236 |
template <typename T> |
237 | 237 |
struct SetPath |
238 | 238 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > { |
239 | 239 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
/// @} |
243 | 243 |
|
244 |
protected: |
|
245 |
|
|
246 |
HartmannOrlin() {} |
|
247 |
|
|
244 | 248 |
public: |
245 | 249 |
|
246 | 250 |
/// \brief Constructor. |
247 | 251 |
/// |
248 | 252 |
/// The constructor of the class. |
249 | 253 |
/// |
250 | 254 |
/// \param digraph The digraph the algorithm runs on. |
251 | 255 |
/// \param length The lengths (costs) of the arcs. |
252 | 256 |
HartmannOrlin( const Digraph &digraph, |
253 | 257 |
const LengthMap &length ) : |
254 | 258 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
255 | 259 |
_best_found(false), _best_length(0), _best_size(1), |
256 | 260 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
257 | 261 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
258 | 262 |
std::numeric_limits<LargeValue>::infinity() : |
259 | 263 |
std::numeric_limits<LargeValue>::max()) |
260 | 264 |
{} |
261 | 265 |
|
262 | 266 |
/// Destructor. |
263 | 267 |
~HartmannOrlin() { |
264 | 268 |
if (_local_path) delete _cycle_path; |
265 | 269 |
} |
266 | 270 |
|
267 | 271 |
/// \brief Set the path structure for storing the found cycle. |
268 | 272 |
/// |
269 | 273 |
/// This function sets an external path structure for storing the |
270 | 274 |
/// found cycle. |
271 | 275 |
/// |
272 | 276 |
/// If you don't call this function before calling \ref run() or |
273 | 277 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
274 | 278 |
/// structure. The destuctor deallocates this automatically |
275 | 279 |
/// allocated object, of course. |
276 | 280 |
/// |
277 | 281 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
278 | 282 |
/// "addFront()" function of the given path structure. |
279 | 283 |
/// |
280 | 284 |
/// \return <tt>(*this)</tt> |
281 | 285 |
HartmannOrlin& cycle(Path &path) { |
282 | 286 |
if (_local_path) { |
283 | 287 |
delete _cycle_path; |
284 | 288 |
_local_path = false; |
285 | 289 |
} |
286 | 290 |
_cycle_path = &path; |
287 | 291 |
return *this; |
288 | 292 |
} |
289 | 293 |
|
290 | 294 |
/// \brief Set the tolerance used by the algorithm. |
291 | 295 |
/// |
292 | 296 |
/// This function sets the tolerance object used by the algorithm. |
293 | 297 |
/// |
294 | 298 |
/// \return <tt>(*this)</tt> |
295 | 299 |
HartmannOrlin& tolerance(const Tolerance& tolerance) { |
296 | 300 |
_tolerance = tolerance; |
297 | 301 |
return *this; |
298 | 302 |
} |
299 | 303 |
|
300 | 304 |
/// \brief Return a const reference to the tolerance. |
301 | 305 |
/// |
302 | 306 |
/// This function returns a const reference to the tolerance object |
303 | 307 |
/// used by the algorithm. |
304 | 308 |
const Tolerance& tolerance() const { |
305 | 309 |
return _tolerance; |
306 | 310 |
} |
307 | 311 |
|
308 | 312 |
/// \name Execution control |
309 | 313 |
/// The simplest way to execute the algorithm is to call the \ref run() |
310 | 314 |
/// function.\n |
311 | 315 |
/// If you only need the minimum mean length, you may call |
312 | 316 |
/// \ref findMinMean(). |
313 | 317 |
|
314 | 318 |
/// @{ |
315 | 319 |
|
316 | 320 |
/// \brief Run the algorithm. |
317 | 321 |
/// |
318 | 322 |
/// This function runs the algorithm. |
319 | 323 |
/// It can be called more than once (e.g. if the underlying digraph |
320 | 324 |
/// and/or the arc lengths have been modified). |
321 | 325 |
/// |
322 | 326 |
/// \return \c true if a directed cycle exists in the digraph. |
323 | 327 |
/// |
324 | 328 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
325 | 329 |
/// \code |
326 | 330 |
/// return mmc.findMinMean() && mmc.findCycle(); |
327 | 331 |
/// \endcode |
328 | 332 |
bool run() { |
329 | 333 |
return findMinMean() && findCycle(); |
330 | 334 |
} |
331 | 335 |
|
332 | 336 |
/// \brief Find the minimum cycle mean. |
333 | 337 |
/// |
334 | 338 |
/// This function finds the minimum mean length of the directed |
335 | 339 |
/// cycles in the digraph. |
336 | 340 |
/// |
337 | 341 |
/// \return \c true if a directed cycle exists in the digraph. |
338 | 342 |
bool findMinMean() { |
339 | 343 |
// Initialization and find strongly connected components |
... | ... |
@@ -138,192 +138,196 @@ |
138 | 138 |
|
139 | 139 |
/// The tolerance type |
140 | 140 |
typedef typename TR::Tolerance Tolerance; |
141 | 141 |
|
142 | 142 |
/// \brief The path type of the found cycles |
143 | 143 |
/// |
144 | 144 |
/// The path type of the found cycles. |
145 | 145 |
/// Using the \ref HowardDefaultTraits "default traits class", |
146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
147 | 147 |
typedef typename TR::Path Path; |
148 | 148 |
|
149 | 149 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
152 | 152 |
private: |
153 | 153 |
|
154 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
155 | 155 |
|
156 | 156 |
// The digraph the algorithm runs on |
157 | 157 |
const Digraph &_gr; |
158 | 158 |
// The length of the arcs |
159 | 159 |
const LengthMap &_length; |
160 | 160 |
|
161 | 161 |
// Data for the found cycles |
162 | 162 |
bool _curr_found, _best_found; |
163 | 163 |
LargeValue _curr_length, _best_length; |
164 | 164 |
int _curr_size, _best_size; |
165 | 165 |
Node _curr_node, _best_node; |
166 | 166 |
|
167 | 167 |
Path *_cycle_path; |
168 | 168 |
bool _local_path; |
169 | 169 |
|
170 | 170 |
// Internal data used by the algorithm |
171 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
172 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
173 | 173 |
typename Digraph::template NodeMap<int> _level; |
174 | 174 |
typename Digraph::template NodeMap<LargeValue> _dist; |
175 | 175 |
|
176 | 176 |
// Data for storing the strongly connected components |
177 | 177 |
int _comp_num; |
178 | 178 |
typename Digraph::template NodeMap<int> _comp; |
179 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
180 | 180 |
std::vector<Node>* _nodes; |
181 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
182 | 182 |
|
183 | 183 |
// Queue used for BFS search |
184 | 184 |
std::vector<Node> _queue; |
185 | 185 |
int _qfront, _qback; |
186 | 186 |
|
187 | 187 |
Tolerance _tolerance; |
188 | 188 |
|
189 | 189 |
// Infinite constant |
190 | 190 |
const LargeValue INF; |
191 | 191 |
|
192 | 192 |
public: |
193 | 193 |
|
194 | 194 |
/// \name Named Template Parameters |
195 | 195 |
/// @{ |
196 | 196 |
|
197 | 197 |
template <typename T> |
198 | 198 |
struct SetLargeValueTraits : public Traits { |
199 | 199 |
typedef T LargeValue; |
200 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
201 | 201 |
}; |
202 | 202 |
|
203 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
204 | 204 |
/// \c LargeValue type. |
205 | 205 |
/// |
206 | 206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
207 | 207 |
/// type. It is used for internal computations in the algorithm. |
208 | 208 |
template <typename T> |
209 | 209 |
struct SetLargeValue |
210 | 210 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > { |
211 | 211 |
typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create; |
212 | 212 |
}; |
213 | 213 |
|
214 | 214 |
template <typename T> |
215 | 215 |
struct SetPathTraits : public Traits { |
216 | 216 |
typedef T Path; |
217 | 217 |
}; |
218 | 218 |
|
219 | 219 |
/// \brief \ref named-templ-param "Named parameter" for setting |
220 | 220 |
/// \c %Path type. |
221 | 221 |
/// |
222 | 222 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
223 | 223 |
/// type of the found cycles. |
224 | 224 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
225 | 225 |
/// and it must have an \c addBack() function. |
226 | 226 |
template <typename T> |
227 | 227 |
struct SetPath |
228 | 228 |
: public Howard<GR, LEN, SetPathTraits<T> > { |
229 | 229 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
230 | 230 |
}; |
231 | 231 |
|
232 | 232 |
/// @} |
233 | 233 |
|
234 |
protected: |
|
235 |
|
|
236 |
Howard() {} |
|
237 |
|
|
234 | 238 |
public: |
235 | 239 |
|
236 | 240 |
/// \brief Constructor. |
237 | 241 |
/// |
238 | 242 |
/// The constructor of the class. |
239 | 243 |
/// |
240 | 244 |
/// \param digraph The digraph the algorithm runs on. |
241 | 245 |
/// \param length The lengths (costs) of the arcs. |
242 | 246 |
Howard( const Digraph &digraph, |
243 | 247 |
const LengthMap &length ) : |
244 | 248 |
_gr(digraph), _length(length), _best_found(false), |
245 | 249 |
_best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
246 | 250 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
247 | 251 |
_comp(digraph), _in_arcs(digraph), |
248 | 252 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
249 | 253 |
std::numeric_limits<LargeValue>::infinity() : |
250 | 254 |
std::numeric_limits<LargeValue>::max()) |
251 | 255 |
{} |
252 | 256 |
|
253 | 257 |
/// Destructor. |
254 | 258 |
~Howard() { |
255 | 259 |
if (_local_path) delete _cycle_path; |
256 | 260 |
} |
257 | 261 |
|
258 | 262 |
/// \brief Set the path structure for storing the found cycle. |
259 | 263 |
/// |
260 | 264 |
/// This function sets an external path structure for storing the |
261 | 265 |
/// found cycle. |
262 | 266 |
/// |
263 | 267 |
/// If you don't call this function before calling \ref run() or |
264 | 268 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
265 | 269 |
/// structure. The destuctor deallocates this automatically |
266 | 270 |
/// allocated object, of course. |
267 | 271 |
/// |
268 | 272 |
/// \note The algorithm calls only the \ref lemon::Path::addBack() |
269 | 273 |
/// "addBack()" function of the given path structure. |
270 | 274 |
/// |
271 | 275 |
/// \return <tt>(*this)</tt> |
272 | 276 |
Howard& cycle(Path &path) { |
273 | 277 |
if (_local_path) { |
274 | 278 |
delete _cycle_path; |
275 | 279 |
_local_path = false; |
276 | 280 |
} |
277 | 281 |
_cycle_path = &path; |
278 | 282 |
return *this; |
279 | 283 |
} |
280 | 284 |
|
281 | 285 |
/// \brief Set the tolerance used by the algorithm. |
282 | 286 |
/// |
283 | 287 |
/// This function sets the tolerance object used by the algorithm. |
284 | 288 |
/// |
285 | 289 |
/// \return <tt>(*this)</tt> |
286 | 290 |
Howard& tolerance(const Tolerance& tolerance) { |
287 | 291 |
_tolerance = tolerance; |
288 | 292 |
return *this; |
289 | 293 |
} |
290 | 294 |
|
291 | 295 |
/// \brief Return a const reference to the tolerance. |
292 | 296 |
/// |
293 | 297 |
/// This function returns a const reference to the tolerance object |
294 | 298 |
/// used by the algorithm. |
295 | 299 |
const Tolerance& tolerance() const { |
296 | 300 |
return _tolerance; |
297 | 301 |
} |
298 | 302 |
|
299 | 303 |
/// \name Execution control |
300 | 304 |
/// The simplest way to execute the algorithm is to call the \ref run() |
301 | 305 |
/// function.\n |
302 | 306 |
/// If you only need the minimum mean length, you may call |
303 | 307 |
/// \ref findMinMean(). |
304 | 308 |
|
305 | 309 |
/// @{ |
306 | 310 |
|
307 | 311 |
/// \brief Run the algorithm. |
308 | 312 |
/// |
309 | 313 |
/// This function runs the algorithm. |
310 | 314 |
/// It can be called more than once (e.g. if the underlying digraph |
311 | 315 |
/// and/or the arc lengths have been modified). |
312 | 316 |
/// |
313 | 317 |
/// \return \c true if a directed cycle exists in the digraph. |
314 | 318 |
/// |
315 | 319 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
316 | 320 |
/// \code |
317 | 321 |
/// return mmc.findMinMean() && mmc.findCycle(); |
318 | 322 |
/// \endcode |
319 | 323 |
bool run() { |
320 | 324 |
return findMinMean() && findCycle(); |
321 | 325 |
} |
322 | 326 |
|
323 | 327 |
/// \brief Find the minimum cycle mean. |
324 | 328 |
/// |
325 | 329 |
/// This function finds the minimum mean length of the directed |
326 | 330 |
/// cycles in the digraph. |
327 | 331 |
/// |
328 | 332 |
/// \return \c true if a directed cycle exists in the digraph. |
329 | 333 |
bool findMinMean() { |
... | ... |
@@ -144,192 +144,196 @@ |
144 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
145 | 145 |
typedef typename TR::Path Path; |
146 | 146 |
|
147 | 147 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
148 | 148 |
typedef TR Traits; |
149 | 149 |
|
150 | 150 |
private: |
151 | 151 |
|
152 | 152 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
153 | 153 |
|
154 | 154 |
// Data sturcture for path data |
155 | 155 |
struct PathData |
156 | 156 |
{ |
157 | 157 |
LargeValue dist; |
158 | 158 |
Arc pred; |
159 | 159 |
PathData(LargeValue d, Arc p = INVALID) : |
160 | 160 |
dist(d), pred(p) {} |
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
164 | 164 |
PathDataNodeMap; |
165 | 165 |
|
166 | 166 |
private: |
167 | 167 |
|
168 | 168 |
// The digraph the algorithm runs on |
169 | 169 |
const Digraph &_gr; |
170 | 170 |
// The length of the arcs |
171 | 171 |
const LengthMap &_length; |
172 | 172 |
|
173 | 173 |
// Data for storing the strongly connected components |
174 | 174 |
int _comp_num; |
175 | 175 |
typename Digraph::template NodeMap<int> _comp; |
176 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
177 | 177 |
std::vector<Node>* _nodes; |
178 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
179 | 179 |
|
180 | 180 |
// Data for the found cycle |
181 | 181 |
LargeValue _cycle_length; |
182 | 182 |
int _cycle_size; |
183 | 183 |
Node _cycle_node; |
184 | 184 |
|
185 | 185 |
Path *_cycle_path; |
186 | 186 |
bool _local_path; |
187 | 187 |
|
188 | 188 |
// Node map for storing path data |
189 | 189 |
PathDataNodeMap _data; |
190 | 190 |
// The processed nodes in the last round |
191 | 191 |
std::vector<Node> _process; |
192 | 192 |
|
193 | 193 |
Tolerance _tolerance; |
194 | 194 |
|
195 | 195 |
// Infinite constant |
196 | 196 |
const LargeValue INF; |
197 | 197 |
|
198 | 198 |
public: |
199 | 199 |
|
200 | 200 |
/// \name Named Template Parameters |
201 | 201 |
/// @{ |
202 | 202 |
|
203 | 203 |
template <typename T> |
204 | 204 |
struct SetLargeValueTraits : public Traits { |
205 | 205 |
typedef T LargeValue; |
206 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
207 | 207 |
}; |
208 | 208 |
|
209 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
210 | 210 |
/// \c LargeValue type. |
211 | 211 |
/// |
212 | 212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
213 | 213 |
/// type. It is used for internal computations in the algorithm. |
214 | 214 |
template <typename T> |
215 | 215 |
struct SetLargeValue |
216 | 216 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > { |
217 | 217 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename T> |
221 | 221 |
struct SetPathTraits : public Traits { |
222 | 222 |
typedef T Path; |
223 | 223 |
}; |
224 | 224 |
|
225 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
226 | 226 |
/// \c %Path type. |
227 | 227 |
/// |
228 | 228 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
229 | 229 |
/// type of the found cycles. |
230 | 230 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
231 | 231 |
/// and it must have an \c addFront() function. |
232 | 232 |
template <typename T> |
233 | 233 |
struct SetPath |
234 | 234 |
: public Karp<GR, LEN, SetPathTraits<T> > { |
235 | 235 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
/// @} |
239 | 239 |
|
240 |
protected: |
|
241 |
|
|
242 |
Karp() {} |
|
243 |
|
|
240 | 244 |
public: |
241 | 245 |
|
242 | 246 |
/// \brief Constructor. |
243 | 247 |
/// |
244 | 248 |
/// The constructor of the class. |
245 | 249 |
/// |
246 | 250 |
/// \param digraph The digraph the algorithm runs on. |
247 | 251 |
/// \param length The lengths (costs) of the arcs. |
248 | 252 |
Karp( const Digraph &digraph, |
249 | 253 |
const LengthMap &length ) : |
250 | 254 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
251 | 255 |
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID), |
252 | 256 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
253 | 257 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
254 | 258 |
std::numeric_limits<LargeValue>::infinity() : |
255 | 259 |
std::numeric_limits<LargeValue>::max()) |
256 | 260 |
{} |
257 | 261 |
|
258 | 262 |
/// Destructor. |
259 | 263 |
~Karp() { |
260 | 264 |
if (_local_path) delete _cycle_path; |
261 | 265 |
} |
262 | 266 |
|
263 | 267 |
/// \brief Set the path structure for storing the found cycle. |
264 | 268 |
/// |
265 | 269 |
/// This function sets an external path structure for storing the |
266 | 270 |
/// found cycle. |
267 | 271 |
/// |
268 | 272 |
/// If you don't call this function before calling \ref run() or |
269 | 273 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
270 | 274 |
/// structure. The destuctor deallocates this automatically |
271 | 275 |
/// allocated object, of course. |
272 | 276 |
/// |
273 | 277 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
274 | 278 |
/// "addFront()" function of the given path structure. |
275 | 279 |
/// |
276 | 280 |
/// \return <tt>(*this)</tt> |
277 | 281 |
Karp& cycle(Path &path) { |
278 | 282 |
if (_local_path) { |
279 | 283 |
delete _cycle_path; |
280 | 284 |
_local_path = false; |
281 | 285 |
} |
282 | 286 |
_cycle_path = &path; |
283 | 287 |
return *this; |
284 | 288 |
} |
285 | 289 |
|
286 | 290 |
/// \brief Set the tolerance used by the algorithm. |
287 | 291 |
/// |
288 | 292 |
/// This function sets the tolerance object used by the algorithm. |
289 | 293 |
/// |
290 | 294 |
/// \return <tt>(*this)</tt> |
291 | 295 |
Karp& tolerance(const Tolerance& tolerance) { |
292 | 296 |
_tolerance = tolerance; |
293 | 297 |
return *this; |
294 | 298 |
} |
295 | 299 |
|
296 | 300 |
/// \brief Return a const reference to the tolerance. |
297 | 301 |
/// |
298 | 302 |
/// This function returns a const reference to the tolerance object |
299 | 303 |
/// used by the algorithm. |
300 | 304 |
const Tolerance& tolerance() const { |
301 | 305 |
return _tolerance; |
302 | 306 |
} |
303 | 307 |
|
304 | 308 |
/// \name Execution control |
305 | 309 |
/// The simplest way to execute the algorithm is to call the \ref run() |
306 | 310 |
/// function.\n |
307 | 311 |
/// If you only need the minimum mean length, you may call |
308 | 312 |
/// \ref findMinMean(). |
309 | 313 |
|
310 | 314 |
/// @{ |
311 | 315 |
|
312 | 316 |
/// \brief Run the algorithm. |
313 | 317 |
/// |
314 | 318 |
/// This function runs the algorithm. |
315 | 319 |
/// It can be called more than once (e.g. if the underlying digraph |
316 | 320 |
/// and/or the arc lengths have been modified). |
317 | 321 |
/// |
318 | 322 |
/// \return \c true if a directed cycle exists in the digraph. |
319 | 323 |
/// |
320 | 324 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
321 | 325 |
/// \code |
322 | 326 |
/// return mmc.findMinMean() && mmc.findCycle(); |
323 | 327 |
/// \endcode |
324 | 328 |
bool run() { |
325 | 329 |
return findMinMean() && findCycle(); |
326 | 330 |
} |
327 | 331 |
|
328 | 332 |
/// \brief Find the minimum cycle mean. |
329 | 333 |
/// |
330 | 334 |
/// This function finds the minimum mean length of the directed |
331 | 335 |
/// cycles in the digraph. |
332 | 336 |
/// |
333 | 337 |
/// \return \c true if a directed cycle exists in the digraph. |
334 | 338 |
bool findMinMean() { |
335 | 339 |
// Initialization and find strongly connected components |
... | ... |
@@ -310,192 +310,196 @@ |
310 | 310 |
/// |
311 | 311 |
/// \ref named-templ-param "Named parameter" for setting |
312 | 312 |
/// \c FlowMap type. |
313 | 313 |
template <typename T> |
314 | 314 |
struct SetFlowMap |
315 | 315 |
: public Suurballe<GR, LEN, SetFlowMapTraits<T> > { |
316 | 316 |
typedef Suurballe<GR, LEN, SetFlowMapTraits<T> > Create; |
317 | 317 |
}; |
318 | 318 |
|
319 | 319 |
template <typename T> |
320 | 320 |
struct SetPotentialMapTraits : public Traits { |
321 | 321 |
typedef T PotentialMap; |
322 | 322 |
}; |
323 | 323 |
|
324 | 324 |
/// \brief \ref named-templ-param "Named parameter" for setting |
325 | 325 |
/// \c PotentialMap type. |
326 | 326 |
/// |
327 | 327 |
/// \ref named-templ-param "Named parameter" for setting |
328 | 328 |
/// \c PotentialMap type. |
329 | 329 |
template <typename T> |
330 | 330 |
struct SetPotentialMap |
331 | 331 |
: public Suurballe<GR, LEN, SetPotentialMapTraits<T> > { |
332 | 332 |
typedef Suurballe<GR, LEN, SetPotentialMapTraits<T> > Create; |
333 | 333 |
}; |
334 | 334 |
|
335 | 335 |
template <typename T> |
336 | 336 |
struct SetPathTraits : public Traits { |
337 | 337 |
typedef T Path; |
338 | 338 |
}; |
339 | 339 |
|
340 | 340 |
/// \brief \ref named-templ-param "Named parameter" for setting |
341 | 341 |
/// \c %Path type. |
342 | 342 |
/// |
343 | 343 |
/// \ref named-templ-param "Named parameter" for setting \c %Path type. |
344 | 344 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
345 | 345 |
/// and it must have an \c addBack() function. |
346 | 346 |
template <typename T> |
347 | 347 |
struct SetPath |
348 | 348 |
: public Suurballe<GR, LEN, SetPathTraits<T> > { |
349 | 349 |
typedef Suurballe<GR, LEN, SetPathTraits<T> > Create; |
350 | 350 |
}; |
351 | 351 |
|
352 | 352 |
template <typename H, typename CR> |
353 | 353 |
struct SetHeapTraits : public Traits { |
354 | 354 |
typedef H Heap; |
355 | 355 |
typedef CR HeapCrossRef; |
356 | 356 |
}; |
357 | 357 |
|
358 | 358 |
/// \brief \ref named-templ-param "Named parameter" for setting |
359 | 359 |
/// \c Heap and \c HeapCrossRef types. |
360 | 360 |
/// |
361 | 361 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
362 | 362 |
/// and \c HeapCrossRef types with automatic allocation. |
363 | 363 |
/// They will be used for internal Dijkstra computations. |
364 | 364 |
/// The heap type must conform to the \ref lemon::concepts::Heap "Heap" |
365 | 365 |
/// concept and its priority type must be \c Length. |
366 | 366 |
template <typename H, |
367 | 367 |
typename CR = typename Digraph::template NodeMap<int> > |
368 | 368 |
struct SetHeap |
369 | 369 |
: public Suurballe<GR, LEN, SetHeapTraits<H, CR> > { |
370 | 370 |
typedef Suurballe<GR, LEN, SetHeapTraits<H, CR> > Create; |
371 | 371 |
}; |
372 | 372 |
|
373 | 373 |
/// @} |
374 | 374 |
|
375 | 375 |
private: |
376 | 376 |
|
377 | 377 |
// The digraph the algorithm runs on |
378 | 378 |
const Digraph &_graph; |
379 | 379 |
// The length map |
380 | 380 |
const LengthMap &_length; |
381 | 381 |
|
382 | 382 |
// Arc map of the current flow |
383 | 383 |
FlowMap *_flow; |
384 | 384 |
bool _local_flow; |
385 | 385 |
// Node map of the current potentials |
386 | 386 |
PotentialMap *_potential; |
387 | 387 |
bool _local_potential; |
388 | 388 |
|
389 | 389 |
// The source node |
390 | 390 |
Node _s; |
391 | 391 |
// The target node |
392 | 392 |
Node _t; |
393 | 393 |
|
394 | 394 |
// Container to store the found paths |
395 | 395 |
std::vector<Path> _paths; |
396 | 396 |
int _path_num; |
397 | 397 |
|
398 | 398 |
// The pred arc map |
399 | 399 |
PredMap _pred; |
400 | 400 |
|
401 | 401 |
// Data for full init |
402 | 402 |
PotentialMap *_init_dist; |
403 | 403 |
PredMap *_init_pred; |
404 | 404 |
bool _full_init; |
405 | 405 |
|
406 |
protected: |
|
407 |
|
|
408 |
Suurballe() {} |
|
409 |
|
|
406 | 410 |
public: |
407 | 411 |
|
408 | 412 |
/// \brief Constructor. |
409 | 413 |
/// |
410 | 414 |
/// Constructor. |
411 | 415 |
/// |
412 | 416 |
/// \param graph The digraph the algorithm runs on. |
413 | 417 |
/// \param length The length (cost) values of the arcs. |
414 | 418 |
Suurballe( const Digraph &graph, |
415 | 419 |
const LengthMap &length ) : |
416 | 420 |
_graph(graph), _length(length), _flow(0), _local_flow(false), |
417 | 421 |
_potential(0), _local_potential(false), _pred(graph), |
418 | 422 |
_init_dist(0), _init_pred(0) |
419 | 423 |
{} |
420 | 424 |
|
421 | 425 |
/// Destructor. |
422 | 426 |
~Suurballe() { |
423 | 427 |
if (_local_flow) delete _flow; |
424 | 428 |
if (_local_potential) delete _potential; |
425 | 429 |
delete _init_dist; |
426 | 430 |
delete _init_pred; |
427 | 431 |
} |
428 | 432 |
|
429 | 433 |
/// \brief Set the flow map. |
430 | 434 |
/// |
431 | 435 |
/// This function sets the flow map. |
432 | 436 |
/// If it is not used before calling \ref run() or \ref init(), |
433 | 437 |
/// an instance will be allocated automatically. The destructor |
434 | 438 |
/// deallocates this automatically allocated map, of course. |
435 | 439 |
/// |
436 | 440 |
/// The found flow contains only 0 and 1 values, since it is the |
437 | 441 |
/// union of the found arc-disjoint paths. |
438 | 442 |
/// |
439 | 443 |
/// \return <tt>(*this)</tt> |
440 | 444 |
Suurballe& flowMap(FlowMap &map) { |
441 | 445 |
if (_local_flow) { |
442 | 446 |
delete _flow; |
443 | 447 |
_local_flow = false; |
444 | 448 |
} |
445 | 449 |
_flow = ↦ |
446 | 450 |
return *this; |
447 | 451 |
} |
448 | 452 |
|
449 | 453 |
/// \brief Set the potential map. |
450 | 454 |
/// |
451 | 455 |
/// This function sets the potential map. |
452 | 456 |
/// If it is not used before calling \ref run() or \ref init(), |
453 | 457 |
/// an instance will be allocated automatically. The destructor |
454 | 458 |
/// deallocates this automatically allocated map, of course. |
455 | 459 |
/// |
456 | 460 |
/// The node potentials provide the dual solution of the underlying |
457 | 461 |
/// \ref min_cost_flow "minimum cost flow problem". |
458 | 462 |
/// |
459 | 463 |
/// \return <tt>(*this)</tt> |
460 | 464 |
Suurballe& potentialMap(PotentialMap &map) { |
461 | 465 |
if (_local_potential) { |
462 | 466 |
delete _potential; |
463 | 467 |
_local_potential = false; |
464 | 468 |
} |
465 | 469 |
_potential = ↦ |
466 | 470 |
return *this; |
467 | 471 |
} |
468 | 472 |
|
469 | 473 |
/// \name Execution Control |
470 | 474 |
/// The simplest way to execute the algorithm is to call the run() |
471 | 475 |
/// function.\n |
472 | 476 |
/// If you need to execute the algorithm many times using the same |
473 | 477 |
/// source node, then you may call fullInit() once and start() |
474 | 478 |
/// for each target node.\n |
475 | 479 |
/// If you only need the flow that is the union of the found |
476 | 480 |
/// arc-disjoint paths, then you may call findFlow() instead of |
477 | 481 |
/// start(). |
478 | 482 |
|
479 | 483 |
/// @{ |
480 | 484 |
|
481 | 485 |
/// \brief Run the algorithm. |
482 | 486 |
/// |
483 | 487 |
/// This function runs the algorithm. |
484 | 488 |
/// |
485 | 489 |
/// \param s The source node. |
486 | 490 |
/// \param t The target node. |
487 | 491 |
/// \param k The number of paths to be found. |
488 | 492 |
/// |
489 | 493 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
490 | 494 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
491 | 495 |
/// arc-disjoint paths found. |
492 | 496 |
/// |
493 | 497 |
/// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is |
494 | 498 |
/// just a shortcut of the following code. |
495 | 499 |
/// \code |
496 | 500 |
/// s.init(s); |
497 | 501 |
/// s.start(t, k); |
498 | 502 |
/// \endcode |
499 | 503 |
int run(const Node& s, const Node& t, int k = 2) { |
500 | 504 |
init(s); |
501 | 505 |
start(t, k); |
0 comments (0 inline)