... |
... |
@@ -27,2 +27,3 @@
|
27 |
27 |
#include <vector>
|
|
28 |
#include <limits>
|
28 |
29 |
#include <lemon/bin_heap.h>
|
... |
... |
@@ -44,9 +45,13 @@
|
44 |
45 |
///
|
45 |
|
/// In fact, this implementation is the specialization of the
|
46 |
|
/// \ref CapacityScaling "successive shortest path" algorithm.
|
|
46 |
/// Note that this problem is a special case of the \ref min_cost_flow
|
|
47 |
/// "minimum cost flow problem". This implementation is actually an
|
|
48 |
/// efficient specialized version of the \ref CapacityScaling
|
|
49 |
/// "Successive Shortest Path" algorithm directly for this problem.
|
|
50 |
/// Therefore this class provides query functions for flow values and
|
|
51 |
/// node potentials (the dual solution) just like the minimum cost flow
|
|
52 |
/// algorithms.
|
47 |
53 |
///
|
48 |
54 |
/// \tparam GR The digraph type the algorithm runs on.
|
49 |
|
/// The default value is \c ListDigraph.
|
50 |
|
/// \tparam LEN The type of the length (cost) map.
|
51 |
|
/// The default value is <tt>Digraph::ArcMap<int></tt>.
|
|
55 |
/// \tparam LEN The type of the length map.
|
|
56 |
/// The default value is <tt>GR::ArcMap<int></tt>.
|
52 |
57 |
///
|
... |
... |
@@ -55,3 +60,3 @@
|
55 |
60 |
/// \note For finding node-disjoint paths this algorithm can be used
|
56 |
|
/// with \ref SplitNodes.
|
|
61 |
/// along with the \ref SplitNodes adaptor.
|
57 |
62 |
#ifdef DOXYGEN
|
... |
... |
@@ -59,3 +64,3 @@
|
59 |
64 |
#else
|
60 |
|
template < typename GR = ListDigraph,
|
|
65 |
template < typename GR,
|
61 |
66 |
typename LEN = typename GR::template ArcMap<int> >
|
... |
... |
@@ -77,2 +82,8 @@
|
77 |
82 |
typedef typename LengthMap::Value Length;
|
|
83 |
#ifdef DOXYGEN
|
|
84 |
/// The type of the flow map.
|
|
85 |
typedef GR::ArcMap<int> FlowMap;
|
|
86 |
/// The type of the potential map.
|
|
87 |
typedef GR::NodeMap<Length> PotentialMap;
|
|
88 |
#else
|
78 |
89 |
/// The type of the flow map.
|
... |
... |
@@ -81,4 +92,6 @@
|
81 |
92 |
typedef typename Digraph::template NodeMap<Length> PotentialMap;
|
|
93 |
#endif
|
|
94 |
|
82 |
95 |
/// The type of the path structures.
|
83 |
|
typedef SimplePath<Digraph> Path;
|
|
96 |
typedef SimplePath<GR> Path;
|
84 |
97 |
|
... |
... |
@@ -86,10 +99,7 @@
|
86 |
99 |
|
87 |
|
/// \brief Special implementation of the Dijkstra algorithm
|
88 |
|
/// for finding shortest paths in the residual network.
|
89 |
|
///
|
90 |
|
/// \ref ResidualDijkstra is a special implementation of the
|
91 |
|
/// \ref Dijkstra algorithm for finding shortest paths in the
|
92 |
|
/// residual network of the digraph with respect to the reduced arc
|
93 |
|
/// lengths and modifying the node potentials according to the
|
94 |
|
/// distance of the nodes.
|
|
100 |
// ResidualDijkstra is a special implementation of the
|
|
101 |
// Dijkstra algorithm for finding shortest paths in the
|
|
102 |
// residual network with respect to the reduced arc lengths
|
|
103 |
// and modifying the node potentials according to the
|
|
104 |
// distance of the nodes.
|
95 |
105 |
class ResidualDijkstra
|
... |
... |
@@ -122,3 +132,3 @@
|
122 |
132 |
/// Constructor.
|
123 |
|
ResidualDijkstra( const Digraph &digraph,
|
|
133 |
ResidualDijkstra( const Digraph &graph,
|
124 |
134 |
const FlowMap &flow,
|
... |
... |
@@ -128,4 +138,4 @@
|
128 |
138 |
Node s, Node t ) :
|
129 |
|
_graph(digraph), _flow(flow), _length(length), _potential(potential),
|
130 |
|
_dist(digraph), _pred(pred), _s(s), _t(t) {}
|
|
139 |
_graph(graph), _flow(flow), _length(length), _potential(potential),
|
|
140 |
_dist(graph), _pred(pred), _s(s), _t(t) {}
|
131 |
141 |
|
... |
... |
@@ -238,12 +248,12 @@
|
238 |
248 |
///
|
239 |
|
/// \param digraph The digraph the algorithm runs on.
|
|
249 |
/// \param graph The digraph the algorithm runs on.
|
240 |
250 |
/// \param length The length (cost) values of the arcs.
|
241 |
|
/// \param s The source node.
|
242 |
|
/// \param t The target node.
|
243 |
|
Suurballe( const Digraph &digraph,
|
244 |
|
const LengthMap &length,
|
245 |
|
Node s, Node t ) :
|
246 |
|
_graph(digraph), _length(length), _flow(0), _local_flow(false),
|
247 |
|
_potential(0), _local_potential(false), _source(s), _target(t),
|
248 |
|
_pred(digraph) {}
|
|
251 |
Suurballe( const Digraph &graph,
|
|
252 |
const LengthMap &length ) :
|
|
253 |
_graph(graph), _length(length), _flow(0), _local_flow(false),
|
|
254 |
_potential(0), _local_potential(false), _pred(graph)
|
|
255 |
{
|
|
256 |
LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
|
|
257 |
"The length type of Suurballe must be integer");
|
|
258 |
}
|
249 |
259 |
|
... |
... |
@@ -259,5 +269,8 @@
|
259 |
269 |
/// This function sets the flow map.
|
|
270 |
/// If it is not used before calling \ref run() or \ref init(),
|
|
271 |
/// an instance will be allocated automatically. The destructor
|
|
272 |
/// deallocates this automatically allocated map, of course.
|
260 |
273 |
///
|
261 |
|
/// The found flow contains only 0 and 1 values. It is the union of
|
262 |
|
/// the found arc-disjoint paths.
|
|
274 |
/// The found flow contains only 0 and 1 values, since it is the
|
|
275 |
/// union of the found arc-disjoint paths.
|
263 |
276 |
///
|
... |
... |
@@ -276,5 +289,8 @@
|
276 |
289 |
/// This function sets the potential map.
|
|
290 |
/// If it is not used before calling \ref run() or \ref init(),
|
|
291 |
/// an instance will be allocated automatically. The destructor
|
|
292 |
/// deallocates this automatically allocated map, of course.
|
277 |
293 |
///
|
278 |
|
/// The potentials provide the dual solution of the underlying
|
279 |
|
/// minimum cost flow problem.
|
|
294 |
/// The node potentials provide the dual solution of the underlying
|
|
295 |
/// \ref min_cost_flow "minimum cost flow problem".
|
280 |
296 |
///
|
... |
... |
@@ -303,2 +319,4 @@
|
303 |
319 |
///
|
|
320 |
/// \param s The source node.
|
|
321 |
/// \param t The target node.
|
304 |
322 |
/// \param k The number of paths to be found.
|
... |
... |
@@ -309,12 +327,12 @@
|
309 |
327 |
///
|
310 |
|
/// \note Apart from the return value, <tt>s.run(k)</tt> is just a
|
311 |
|
/// shortcut of the following code.
|
|
328 |
/// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
|
|
329 |
/// just a shortcut of the following code.
|
312 |
330 |
/// \code
|
313 |
|
/// s.init();
|
314 |
|
/// s.findFlow(k);
|
|
331 |
/// s.init(s);
|
|
332 |
/// s.findFlow(t, k);
|
315 |
333 |
/// s.findPaths();
|
316 |
334 |
/// \endcode
|
317 |
|
int run(int k = 2) {
|
318 |
|
init();
|
319 |
|
findFlow(k);
|
|
335 |
int run(const Node& s, const Node& t, int k = 2) {
|
|
336 |
init(s);
|
|
337 |
findFlow(t, k);
|
320 |
338 |
findPaths();
|
... |
... |
@@ -326,3 +344,7 @@
|
326 |
344 |
/// This function initializes the algorithm.
|
327 |
|
void init() {
|
|
345 |
///
|
|
346 |
/// \param s The source node.
|
|
347 |
void init(const Node& s) {
|
|
348 |
_source = s;
|
|
349 |
|
328 |
350 |
// Initialize maps
|
... |
... |
@@ -338,21 +360,24 @@
|
338 |
360 |
for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
|
339 |
|
|
340 |
|
_dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
|
341 |
|
*_potential, _pred,
|
342 |
|
_source, _target );
|
343 |
361 |
}
|
344 |
362 |
|
345 |
|
/// \brief Execute the successive shortest path algorithm to find
|
346 |
|
/// an optimal flow.
|
|
363 |
/// \brief Execute the algorithm to find an optimal flow.
|
347 |
364 |
///
|
348 |
365 |
/// This function executes the successive shortest path algorithm to
|
349 |
|
/// find a minimum cost flow, which is the union of \c k or less
|
|
366 |
/// find a minimum cost flow, which is the union of \c k (or less)
|
350 |
367 |
/// arc-disjoint paths.
|
351 |
368 |
///
|
|
369 |
/// \param t The target node.
|
|
370 |
/// \param k The number of paths to be found.
|
|
371 |
///
|
352 |
372 |
/// \return \c k if there are at least \c k arc-disjoint paths from
|
353 |
|
/// \c s to \c t in the digraph. Otherwise it returns the number of
|
354 |
|
/// arc-disjoint paths found.
|
|
373 |
/// the source node to the given node \c t in the digraph.
|
|
374 |
/// Otherwise it returns the number of arc-disjoint paths found.
|
355 |
375 |
///
|
356 |
376 |
/// \pre \ref init() must be called before using this function.
|
357 |
|
int findFlow(int k = 2) {
|
|
377 |
int findFlow(const Node& t, int k = 2) {
|
|
378 |
_target = t;
|
|
379 |
_dijkstra =
|
|
380 |
new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
|
|
381 |
_source, _target );
|
|
382 |
|
358 |
383 |
// Find shortest paths
|
... |
... |
@@ -382,3 +407,4 @@
|
382 |
407 |
///
|
383 |
|
/// This function computes the paths from the flow.
|
|
408 |
/// This function computes the paths from the found minimum cost flow,
|
|
409 |
/// which is the union of some arc-disjoint paths.
|
384 |
410 |
///
|
... |
... |
@@ -387,4 +413,2 @@
|
387 |
413 |
void findPaths() {
|
388 |
|
// Create the residual flow map (the union of the paths not found
|
389 |
|
// so far)
|
390 |
414 |
FlowMap res_flow(_graph);
|
... |
... |
@@ -415,6 +439,33 @@
|
415 |
439 |
|
416 |
|
/// \brief Return a const reference to the arc map storing the
|
|
440 |
/// \brief Return the total length of the found paths.
|
|
441 |
///
|
|
442 |
/// This function returns the total length of the found paths, i.e.
|
|
443 |
/// the total cost of the found flow.
|
|
444 |
/// The complexity of the function is O(e).
|
|
445 |
///
|
|
446 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
|
447 |
/// this function.
|
|
448 |
Length totalLength() const {
|
|
449 |
Length c = 0;
|
|
450 |
for (ArcIt e(_graph); e != INVALID; ++e)
|
|
451 |
c += (*_flow)[e] * _length[e];
|
|
452 |
return c;
|
|
453 |
}
|
|
454 |
|
|
455 |
/// \brief Return the flow value on the given arc.
|
|
456 |
///
|
|
457 |
/// This function returns the flow value on the given arc.
|
|
458 |
/// It is \c 1 if the arc is involved in one of the found arc-disjoint
|
|
459 |
/// paths, otherwise it is \c 0.
|
|
460 |
///
|
|
461 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
|
462 |
/// this function.
|
|
463 |
int flow(const Arc& arc) const {
|
|
464 |
return (*_flow)[arc];
|
|
465 |
}
|
|
466 |
|
|
467 |
/// \brief Return a const reference to an arc map storing the
|
417 |
468 |
/// found flow.
|
418 |
469 |
///
|
419 |
|
/// This function returns a const reference to the arc map storing
|
|
470 |
/// This function returns a const reference to an arc map storing
|
420 |
471 |
/// the flow that is the union of the found arc-disjoint paths.
|
... |
... |
@@ -427,27 +478,2 @@
|
427 |
478 |
|
428 |
|
/// \brief Return a const reference to the node map storing the
|
429 |
|
/// found potentials (the dual solution).
|
430 |
|
///
|
431 |
|
/// This function returns a const reference to the node map storing
|
432 |
|
/// the found potentials that provide the dual solution of the
|
433 |
|
/// underlying minimum cost flow problem.
|
434 |
|
///
|
435 |
|
/// \pre \ref run() or \ref findFlow() must be called before using
|
436 |
|
/// this function.
|
437 |
|
const PotentialMap& potentialMap() const {
|
438 |
|
return *_potential;
|
439 |
|
}
|
440 |
|
|
441 |
|
/// \brief Return the flow on the given arc.
|
442 |
|
///
|
443 |
|
/// This function returns the flow on the given arc.
|
444 |
|
/// It is \c 1 if the arc is involved in one of the found paths,
|
445 |
|
/// otherwise it is \c 0.
|
446 |
|
///
|
447 |
|
/// \pre \ref run() or \ref findFlow() must be called before using
|
448 |
|
/// this function.
|
449 |
|
int flow(const Arc& arc) const {
|
450 |
|
return (*_flow)[arc];
|
451 |
|
}
|
452 |
|
|
453 |
479 |
/// \brief Return the potential of the given node.
|
... |
... |
@@ -455,2 +481,4 @@
|
455 |
481 |
/// This function returns the potential of the given node.
|
|
482 |
/// The node potentials provide the dual solution of the
|
|
483 |
/// underlying \ref min_cost_flow "minimum cost flow problem".
|
456 |
484 |
///
|
... |
... |
@@ -462,6 +490,8 @@
|
462 |
490 |
|
463 |
|
/// \brief Return the total length (cost) of the found paths (flow).
|
|
491 |
/// \brief Return a const reference to a node map storing the
|
|
492 |
/// found potentials (the dual solution).
|
464 |
493 |
///
|
465 |
|
/// This function returns the total length (cost) of the found paths
|
466 |
|
/// (flow). The complexity of the function is O(e).
|
|
494 |
/// This function returns a const reference to a node map storing
|
|
495 |
/// the found potentials that provide the dual solution of the
|
|
496 |
/// underlying \ref min_cost_flow "minimum cost flow problem".
|
467 |
497 |
///
|
... |
... |
@@ -469,7 +499,4 @@
|
469 |
499 |
/// this function.
|
470 |
|
Length totalLength() const {
|
471 |
|
Length c = 0;
|
472 |
|
for (ArcIt e(_graph); e != INVALID; ++e)
|
473 |
|
c += (*_flow)[e] * _length[e];
|
474 |
|
return c;
|
|
500 |
const PotentialMap& potentialMap() const {
|
|
501 |
return *_potential;
|
475 |
502 |
}
|
... |
... |
@@ -490,3 +517,3 @@
|
490 |
517 |
///
|
491 |
|
/// \param i The function returns the \c i-th path.
|
|
518 |
/// \param i The function returns the <tt>i</tt>-th path.
|
492 |
519 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
|