1.1 --- a/lemon/suurballe.h Mon Mar 01 07:51:45 2010 +0100
1.2 +++ b/lemon/suurballe.h Wed Mar 03 17:14:17 2010 +0000
1.3 @@ -29,6 +29,7 @@
1.4 #include <lemon/bin_heap.h>
1.5 #include <lemon/path.h>
1.6 #include <lemon/list_graph.h>
1.7 +#include <lemon/dijkstra.h>
1.8 #include <lemon/maps.h>
1.9
1.10 namespace lemon {
1.11 @@ -46,7 +47,7 @@
1.12 /// Note that this problem is a special case of the \ref min_cost_flow
1.13 /// "minimum cost flow problem". This implementation is actually an
1.14 /// efficient specialized version of the \ref CapacityScaling
1.15 - /// "Successive Shortest Path" algorithm directly for this problem.
1.16 + /// "successive shortest path" algorithm directly for this problem.
1.17 /// Therefore this class provides query functions for flow values and
1.18 /// node potentials (the dual solution) just like the minimum cost flow
1.19 /// algorithms.
1.20 @@ -55,9 +56,9 @@
1.21 /// \tparam LEN The type of the length map.
1.22 /// The default value is <tt>GR::ArcMap<int></tt>.
1.23 ///
1.24 - /// \warning Length values should be \e non-negative \e integers.
1.25 + /// \warning Length values should be \e non-negative.
1.26 ///
1.27 - /// \note For finding node-disjoint paths this algorithm can be used
1.28 + /// \note For finding \e node-disjoint paths, this algorithm can be used
1.29 /// along with the \ref SplitNodes adaptor.
1.30 #ifdef DOXYGEN
1.31 template <typename GR, typename LEN>
1.32 @@ -97,6 +98,9 @@
1.33
1.34 private:
1.35
1.36 + typedef typename Digraph::template NodeMap<int> HeapCrossRef;
1.37 + typedef BinHeap<Length, HeapCrossRef> Heap;
1.38 +
1.39 // ResidualDijkstra is a special implementation of the
1.40 // Dijkstra algorithm for finding shortest paths in the
1.41 // residual network with respect to the reduced arc lengths
1.42 @@ -104,44 +108,38 @@
1.43 // distance of the nodes.
1.44 class ResidualDijkstra
1.45 {
1.46 - typedef typename Digraph::template NodeMap<int> HeapCrossRef;
1.47 - typedef BinHeap<Length, HeapCrossRef> Heap;
1.48 -
1.49 private:
1.50
1.51 - // The digraph the algorithm runs on
1.52 const Digraph &_graph;
1.53 -
1.54 - // The main maps
1.55 + const LengthMap &_length;
1.56 const FlowMap &_flow;
1.57 - const LengthMap &_length;
1.58 - PotentialMap &_potential;
1.59 -
1.60 - // The distance map
1.61 - PotentialMap _dist;
1.62 - // The pred arc map
1.63 + PotentialMap &_pi;
1.64 PredMap &_pred;
1.65 - // The processed (i.e. permanently labeled) nodes
1.66 - std::vector<Node> _proc_nodes;
1.67 -
1.68 Node _s;
1.69 Node _t;
1.70 +
1.71 + PotentialMap _dist;
1.72 + std::vector<Node> _proc_nodes;
1.73
1.74 public:
1.75
1.76 - /// Constructor.
1.77 - ResidualDijkstra( const Digraph &graph,
1.78 - const FlowMap &flow,
1.79 - const LengthMap &length,
1.80 - PotentialMap &potential,
1.81 - PredMap &pred,
1.82 - Node s, Node t ) :
1.83 - _graph(graph), _flow(flow), _length(length), _potential(potential),
1.84 - _dist(graph), _pred(pred), _s(s), _t(t) {}
1.85 + // Constructor
1.86 + ResidualDijkstra(Suurballe &srb) :
1.87 + _graph(srb._graph), _length(srb._length),
1.88 + _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred),
1.89 + _s(srb._s), _t(srb._t), _dist(_graph) {}
1.90 +
1.91 + // Run the algorithm and return true if a path is found
1.92 + // from the source node to the target node.
1.93 + bool run(int cnt) {
1.94 + return cnt == 0 ? startFirst() : start();
1.95 + }
1.96
1.97 - /// \brief Run the algorithm. It returns \c true if a path is found
1.98 - /// from the source node to the target node.
1.99 - bool run() {
1.100 + private:
1.101 +
1.102 + // Execute the algorithm for the first time (the flow and potential
1.103 + // functions have to be identically zero).
1.104 + bool startFirst() {
1.105 HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
1.106 Heap heap(heap_cross_ref);
1.107 heap.push(_s, 0);
1.108 @@ -151,29 +149,74 @@
1.109 // Process nodes
1.110 while (!heap.empty() && heap.top() != _t) {
1.111 Node u = heap.top(), v;
1.112 - Length d = heap.prio() + _potential[u], nd;
1.113 + Length d = heap.prio(), dn;
1.114 _dist[u] = heap.prio();
1.115 + _proc_nodes.push_back(u);
1.116 heap.pop();
1.117 +
1.118 + // Traverse outgoing arcs
1.119 + for (OutArcIt e(_graph, u); e != INVALID; ++e) {
1.120 + v = _graph.target(e);
1.121 + switch(heap.state(v)) {
1.122 + case Heap::PRE_HEAP:
1.123 + heap.push(v, d + _length[e]);
1.124 + _pred[v] = e;
1.125 + break;
1.126 + case Heap::IN_HEAP:
1.127 + dn = d + _length[e];
1.128 + if (dn < heap[v]) {
1.129 + heap.decrease(v, dn);
1.130 + _pred[v] = e;
1.131 + }
1.132 + break;
1.133 + case Heap::POST_HEAP:
1.134 + break;
1.135 + }
1.136 + }
1.137 + }
1.138 + if (heap.empty()) return false;
1.139 +
1.140 + // Update potentials of processed nodes
1.141 + Length t_dist = heap.prio();
1.142 + for (int i = 0; i < int(_proc_nodes.size()); ++i)
1.143 + _pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist;
1.144 + return true;
1.145 + }
1.146 +
1.147 + // Execute the algorithm.
1.148 + bool start() {
1.149 + HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
1.150 + Heap heap(heap_cross_ref);
1.151 + heap.push(_s, 0);
1.152 + _pred[_s] = INVALID;
1.153 + _proc_nodes.clear();
1.154 +
1.155 + // Process nodes
1.156 + while (!heap.empty() && heap.top() != _t) {
1.157 + Node u = heap.top(), v;
1.158 + Length d = heap.prio() + _pi[u], dn;
1.159 + _dist[u] = heap.prio();
1.160 _proc_nodes.push_back(u);
1.161 + heap.pop();
1.162
1.163 // Traverse outgoing arcs
1.164 for (OutArcIt e(_graph, u); e != INVALID; ++e) {
1.165 if (_flow[e] == 0) {
1.166 v = _graph.target(e);
1.167 switch(heap.state(v)) {
1.168 - case Heap::PRE_HEAP:
1.169 - heap.push(v, d + _length[e] - _potential[v]);
1.170 - _pred[v] = e;
1.171 - break;
1.172 - case Heap::IN_HEAP:
1.173 - nd = d + _length[e] - _potential[v];
1.174 - if (nd < heap[v]) {
1.175 - heap.decrease(v, nd);
1.176 + case Heap::PRE_HEAP:
1.177 + heap.push(v, d + _length[e] - _pi[v]);
1.178 _pred[v] = e;
1.179 - }
1.180 - break;
1.181 - case Heap::POST_HEAP:
1.182 - break;
1.183 + break;
1.184 + case Heap::IN_HEAP:
1.185 + dn = d + _length[e] - _pi[v];
1.186 + if (dn < heap[v]) {
1.187 + heap.decrease(v, dn);
1.188 + _pred[v] = e;
1.189 + }
1.190 + break;
1.191 + case Heap::POST_HEAP:
1.192 + break;
1.193 }
1.194 }
1.195 }
1.196 @@ -183,19 +226,19 @@
1.197 if (_flow[e] == 1) {
1.198 v = _graph.source(e);
1.199 switch(heap.state(v)) {
1.200 - case Heap::PRE_HEAP:
1.201 - heap.push(v, d - _length[e] - _potential[v]);
1.202 - _pred[v] = e;
1.203 - break;
1.204 - case Heap::IN_HEAP:
1.205 - nd = d - _length[e] - _potential[v];
1.206 - if (nd < heap[v]) {
1.207 - heap.decrease(v, nd);
1.208 + case Heap::PRE_HEAP:
1.209 + heap.push(v, d - _length[e] - _pi[v]);
1.210 _pred[v] = e;
1.211 - }
1.212 - break;
1.213 - case Heap::POST_HEAP:
1.214 - break;
1.215 + break;
1.216 + case Heap::IN_HEAP:
1.217 + dn = d - _length[e] - _pi[v];
1.218 + if (dn < heap[v]) {
1.219 + heap.decrease(v, dn);
1.220 + _pred[v] = e;
1.221 + }
1.222 + break;
1.223 + case Heap::POST_HEAP:
1.224 + break;
1.225 }
1.226 }
1.227 }
1.228 @@ -205,7 +248,7 @@
1.229 // Update potentials of processed nodes
1.230 Length t_dist = heap.prio();
1.231 for (int i = 0; i < int(_proc_nodes.size()); ++i)
1.232 - _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
1.233 + _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
1.234 return true;
1.235 }
1.236
1.237 @@ -226,19 +269,21 @@
1.238 bool _local_potential;
1.239
1.240 // The source node
1.241 - Node _source;
1.242 + Node _s;
1.243 // The target node
1.244 - Node _target;
1.245 + Node _t;
1.246
1.247 // Container to store the found paths
1.248 - std::vector< SimplePath<Digraph> > paths;
1.249 + std::vector<Path> _paths;
1.250 int _path_num;
1.251
1.252 // The pred arc map
1.253 PredMap _pred;
1.254 - // Implementation of the Dijkstra algorithm for finding augmenting
1.255 - // shortest paths in the residual network
1.256 - ResidualDijkstra *_dijkstra;
1.257 +
1.258 + // Data for full init
1.259 + PotentialMap *_init_dist;
1.260 + PredMap *_init_pred;
1.261 + bool _full_init;
1.262
1.263 public:
1.264
1.265 @@ -251,17 +296,16 @@
1.266 Suurballe( const Digraph &graph,
1.267 const LengthMap &length ) :
1.268 _graph(graph), _length(length), _flow(0), _local_flow(false),
1.269 - _potential(0), _local_potential(false), _pred(graph)
1.270 - {
1.271 - LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
1.272 - "The length type of Suurballe must be integer");
1.273 - }
1.274 + _potential(0), _local_potential(false), _pred(graph),
1.275 + _init_dist(0), _init_pred(0)
1.276 + {}
1.277
1.278 /// Destructor.
1.279 ~Suurballe() {
1.280 if (_local_flow) delete _flow;
1.281 if (_local_potential) delete _potential;
1.282 - delete _dijkstra;
1.283 + delete _init_dist;
1.284 + delete _init_pred;
1.285 }
1.286
1.287 /// \brief Set the flow map.
1.288 @@ -306,10 +350,13 @@
1.289
1.290 /// \name Execution Control
1.291 /// The simplest way to execute the algorithm is to call the run()
1.292 - /// function.
1.293 - /// \n
1.294 + /// function.\n
1.295 + /// If you need to execute the algorithm many times using the same
1.296 + /// source node, then you may call fullInit() once and start()
1.297 + /// for each target node.\n
1.298 /// If you only need the flow that is the union of the found
1.299 - /// arc-disjoint paths, you may call init() and findFlow().
1.300 + /// arc-disjoint paths, then you may call findFlow() instead of
1.301 + /// start().
1.302
1.303 /// @{
1.304
1.305 @@ -329,23 +376,21 @@
1.306 /// just a shortcut of the following code.
1.307 /// \code
1.308 /// s.init(s);
1.309 - /// s.findFlow(t, k);
1.310 - /// s.findPaths();
1.311 + /// s.start(t, k);
1.312 /// \endcode
1.313 int run(const Node& s, const Node& t, int k = 2) {
1.314 init(s);
1.315 - findFlow(t, k);
1.316 - findPaths();
1.317 + start(t, k);
1.318 return _path_num;
1.319 }
1.320
1.321 /// \brief Initialize the algorithm.
1.322 ///
1.323 - /// This function initializes the algorithm.
1.324 + /// This function initializes the algorithm with the given source node.
1.325 ///
1.326 /// \param s The source node.
1.327 void init(const Node& s) {
1.328 - _source = s;
1.329 + _s = s;
1.330
1.331 // Initialize maps
1.332 if (!_flow) {
1.333 @@ -356,8 +401,63 @@
1.334 _potential = new PotentialMap(_graph);
1.335 _local_potential = true;
1.336 }
1.337 - for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
1.338 - for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
1.339 + _full_init = false;
1.340 + }
1.341 +
1.342 + /// \brief Initialize the algorithm and perform Dijkstra.
1.343 + ///
1.344 + /// This function initializes the algorithm and performs a full
1.345 + /// Dijkstra search from the given source node. It makes consecutive
1.346 + /// executions of \ref start() "start(t, k)" faster, since they
1.347 + /// have to perform %Dijkstra only k-1 times.
1.348 + ///
1.349 + /// This initialization is usually worth using instead of \ref init()
1.350 + /// if the algorithm is executed many times using the same source node.
1.351 + ///
1.352 + /// \param s The source node.
1.353 + void fullInit(const Node& s) {
1.354 + // Initialize maps
1.355 + init(s);
1.356 + if (!_init_dist) {
1.357 + _init_dist = new PotentialMap(_graph);
1.358 + }
1.359 + if (!_init_pred) {
1.360 + _init_pred = new PredMap(_graph);
1.361 + }
1.362 +
1.363 + // Run a full Dijkstra
1.364 + typename Dijkstra<Digraph, LengthMap>
1.365 + ::template SetStandardHeap<Heap>
1.366 + ::template SetDistMap<PotentialMap>
1.367 + ::template SetPredMap<PredMap>
1.368 + ::Create dijk(_graph, _length);
1.369 + dijk.distMap(*_init_dist).predMap(*_init_pred);
1.370 + dijk.run(s);
1.371 +
1.372 + _full_init = true;
1.373 + }
1.374 +
1.375 + /// \brief Execute the algorithm.
1.376 + ///
1.377 + /// This function executes the algorithm.
1.378 + ///
1.379 + /// \param t The target node.
1.380 + /// \param k The number of paths to be found.
1.381 + ///
1.382 + /// \return \c k if there are at least \c k arc-disjoint paths from
1.383 + /// \c s to \c t in the digraph. Otherwise it returns the number of
1.384 + /// arc-disjoint paths found.
1.385 + ///
1.386 + /// \note Apart from the return value, <tt>s.start(t, k)</tt> is
1.387 + /// just a shortcut of the following code.
1.388 + /// \code
1.389 + /// s.findFlow(t, k);
1.390 + /// s.findPaths();
1.391 + /// \endcode
1.392 + int start(const Node& t, int k = 2) {
1.393 + findFlow(t, k);
1.394 + findPaths();
1.395 + return _path_num;
1.396 }
1.397
1.398 /// \brief Execute the algorithm to find an optimal flow.
1.399 @@ -375,20 +475,39 @@
1.400 ///
1.401 /// \pre \ref init() must be called before using this function.
1.402 int findFlow(const Node& t, int k = 2) {
1.403 - _target = t;
1.404 - _dijkstra =
1.405 - new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
1.406 - _source, _target );
1.407 + _t = t;
1.408 + ResidualDijkstra dijkstra(*this);
1.409 +
1.410 + // Initialization
1.411 + for (ArcIt e(_graph); e != INVALID; ++e) {
1.412 + (*_flow)[e] = 0;
1.413 + }
1.414 + if (_full_init) {
1.415 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.416 + (*_potential)[n] = (*_init_dist)[n];
1.417 + }
1.418 + Node u = _t;
1.419 + Arc e;
1.420 + while ((e = (*_init_pred)[u]) != INVALID) {
1.421 + (*_flow)[e] = 1;
1.422 + u = _graph.source(e);
1.423 + }
1.424 + _path_num = 1;
1.425 + } else {
1.426 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.427 + (*_potential)[n] = 0;
1.428 + }
1.429 + _path_num = 0;
1.430 + }
1.431
1.432 // Find shortest paths
1.433 - _path_num = 0;
1.434 while (_path_num < k) {
1.435 // Run Dijkstra
1.436 - if (!_dijkstra->run()) break;
1.437 + if (!dijkstra.run(_path_num)) break;
1.438 ++_path_num;
1.439
1.440 // Set the flow along the found shortest path
1.441 - Node u = _target;
1.442 + Node u = _t;
1.443 Arc e;
1.444 while ((e = _pred[u]) != INVALID) {
1.445 if (u == _graph.target(e)) {
1.446 @@ -405,8 +524,8 @@
1.447
1.448 /// \brief Compute the paths from the flow.
1.449 ///
1.450 - /// This function computes the paths from the found minimum cost flow,
1.451 - /// which is the union of some arc-disjoint paths.
1.452 + /// This function computes arc-disjoint paths from the found minimum
1.453 + /// cost flow, which is the union of them.
1.454 ///
1.455 /// \pre \ref init() and \ref findFlow() must be called before using
1.456 /// this function.
1.457 @@ -414,15 +533,15 @@
1.458 FlowMap res_flow(_graph);
1.459 for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
1.460
1.461 - paths.clear();
1.462 - paths.resize(_path_num);
1.463 + _paths.clear();
1.464 + _paths.resize(_path_num);
1.465 for (int i = 0; i < _path_num; ++i) {
1.466 - Node n = _source;
1.467 - while (n != _target) {
1.468 + Node n = _s;
1.469 + while (n != _t) {
1.470 OutArcIt e(_graph, n);
1.471 for ( ; res_flow[e] == 0; ++e) ;
1.472 n = _graph.target(e);
1.473 - paths[i].addBack(e);
1.474 + _paths[i].addBack(e);
1.475 res_flow[e] = 0;
1.476 }
1.477 }
1.478 @@ -520,8 +639,8 @@
1.479 ///
1.480 /// \pre \ref run() or \ref findPaths() must be called before using
1.481 /// this function.
1.482 - Path path(int i) const {
1.483 - return paths[i];
1.484 + const Path& path(int i) const {
1.485 + return _paths[i];
1.486 }
1.487
1.488 /// @}
2.1 --- a/test/suurballe_test.cc Mon Mar 01 07:51:45 2010 +0100
2.2 +++ b/test/suurballe_test.cc Wed Mar 03 17:14:17 2010 +0000
2.3 @@ -101,6 +101,9 @@
2.4 k = suurb_test.run(n, n);
2.5 k = suurb_test.run(n, n, k);
2.6 suurb_test.init(n);
2.7 + suurb_test.fullInit(n);
2.8 + suurb_test.start(n);
2.9 + suurb_test.start(n, k);
2.10 k = suurb_test.findFlow(n);
2.11 k = suurb_test.findFlow(n, k);
2.12 suurb_test.findPaths();