1.1 --- a/lemon/suurballe.h Mon Jan 12 23:11:39 2009 +0100
1.2 +++ b/lemon/suurballe.h Thu Nov 05 15:48:01 2009 +0100
1.3 @@ -25,8 +25,11 @@
1.4 /// nodes having minimum total length.
1.5
1.6 #include <vector>
1.7 +#include <limits>
1.8 #include <lemon/bin_heap.h>
1.9 #include <lemon/path.h>
1.10 +#include <lemon/list_graph.h>
1.11 +#include <lemon/maps.h>
1.12
1.13 namespace lemon {
1.14
1.15 @@ -40,51 +43,65 @@
1.16 /// finding arc-disjoint paths having minimum total length (cost)
1.17 /// from a given source node to a given target node in a digraph.
1.18 ///
1.19 - /// In fact, this implementation is the specialization of the
1.20 - /// \ref CapacityScaling "successive shortest path" algorithm.
1.21 + /// Note that this problem is a special case of the \ref min_cost_flow
1.22 + /// "minimum cost flow problem". This implementation is actually an
1.23 + /// efficient specialized version of the \ref CapacityScaling
1.24 + /// "Successive Shortest Path" algorithm directly for this problem.
1.25 + /// Therefore this class provides query functions for flow values and
1.26 + /// node potentials (the dual solution) just like the minimum cost flow
1.27 + /// algorithms.
1.28 ///
1.29 - /// \tparam Digraph The digraph type the algorithm runs on.
1.30 - /// The default value is \c ListDigraph.
1.31 - /// \tparam LengthMap The type of the length (cost) map.
1.32 - /// The default value is <tt>Digraph::ArcMap<int></tt>.
1.33 + /// \tparam GR The digraph type the algorithm runs on.
1.34 + /// \tparam LEN The type of the length map.
1.35 + /// The default value is <tt>GR::ArcMap<int></tt>.
1.36 ///
1.37 /// \warning Length values should be \e non-negative \e integers.
1.38 ///
1.39 /// \note For finding node-disjoint paths this algorithm can be used
1.40 - /// with \ref SplitNodes.
1.41 + /// along with the \ref SplitNodes adaptor.
1.42 #ifdef DOXYGEN
1.43 - template <typename Digraph, typename LengthMap>
1.44 + template <typename GR, typename LEN>
1.45 #else
1.46 - template < typename Digraph = ListDigraph,
1.47 - typename LengthMap = typename Digraph::template ArcMap<int> >
1.48 + template < typename GR,
1.49 + typename LEN = typename GR::template ArcMap<int> >
1.50 #endif
1.51 class Suurballe
1.52 {
1.53 - TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
1.54 + TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1.55
1.56 - typedef typename LengthMap::Value Length;
1.57 typedef ConstMap<Arc, int> ConstArcMap;
1.58 - typedef typename Digraph::template NodeMap<Arc> PredMap;
1.59 + typedef typename GR::template NodeMap<Arc> PredMap;
1.60
1.61 public:
1.62
1.63 + /// The type of the digraph the algorithm runs on.
1.64 + typedef GR Digraph;
1.65 + /// The type of the length map.
1.66 + typedef LEN LengthMap;
1.67 + /// The type of the lengths.
1.68 + typedef typename LengthMap::Value Length;
1.69 +#ifdef DOXYGEN
1.70 + /// The type of the flow map.
1.71 + typedef GR::ArcMap<int> FlowMap;
1.72 + /// The type of the potential map.
1.73 + typedef GR::NodeMap<Length> PotentialMap;
1.74 +#else
1.75 /// The type of the flow map.
1.76 typedef typename Digraph::template ArcMap<int> FlowMap;
1.77 /// The type of the potential map.
1.78 typedef typename Digraph::template NodeMap<Length> PotentialMap;
1.79 +#endif
1.80 +
1.81 /// The type of the path structures.
1.82 - typedef SimplePath<Digraph> Path;
1.83 + typedef SimplePath<GR> Path;
1.84
1.85 private:
1.86
1.87 - /// \brief Special implementation of the Dijkstra algorithm
1.88 - /// for finding shortest paths in the residual network.
1.89 - ///
1.90 - /// \ref ResidualDijkstra is a special implementation of the
1.91 - /// \ref Dijkstra algorithm for finding shortest paths in the
1.92 - /// residual network of the digraph with respect to the reduced arc
1.93 - /// lengths and modifying the node potentials according to the
1.94 - /// distance of the nodes.
1.95 + // ResidualDijkstra is a special implementation of the
1.96 + // Dijkstra algorithm for finding shortest paths in the
1.97 + // residual network with respect to the reduced arc lengths
1.98 + // and modifying the node potentials according to the
1.99 + // distance of the nodes.
1.100 class ResidualDijkstra
1.101 {
1.102 typedef typename Digraph::template NodeMap<int> HeapCrossRef;
1.103 @@ -113,14 +130,14 @@
1.104 public:
1.105
1.106 /// Constructor.
1.107 - ResidualDijkstra( const Digraph &digraph,
1.108 + ResidualDijkstra( const Digraph &graph,
1.109 const FlowMap &flow,
1.110 const LengthMap &length,
1.111 PotentialMap &potential,
1.112 PredMap &pred,
1.113 Node s, Node t ) :
1.114 - _graph(digraph), _flow(flow), _length(length), _potential(potential),
1.115 - _dist(digraph), _pred(pred), _s(s), _t(t) {}
1.116 + _graph(graph), _flow(flow), _length(length), _potential(potential),
1.117 + _dist(graph), _pred(pred), _s(s), _t(t) {}
1.118
1.119 /// \brief Run the algorithm. It returns \c true if a path is found
1.120 /// from the source node to the target node.
1.121 @@ -229,16 +246,16 @@
1.122 ///
1.123 /// Constructor.
1.124 ///
1.125 - /// \param digraph The digraph the algorithm runs on.
1.126 + /// \param graph The digraph the algorithm runs on.
1.127 /// \param length The length (cost) values of the arcs.
1.128 - /// \param s The source node.
1.129 - /// \param t The target node.
1.130 - Suurballe( const Digraph &digraph,
1.131 - const LengthMap &length,
1.132 - Node s, Node t ) :
1.133 - _graph(digraph), _length(length), _flow(0), _local_flow(false),
1.134 - _potential(0), _local_potential(false), _source(s), _target(t),
1.135 - _pred(digraph) {}
1.136 + Suurballe( const Digraph &graph,
1.137 + const LengthMap &length ) :
1.138 + _graph(graph), _length(length), _flow(0), _local_flow(false),
1.139 + _potential(0), _local_potential(false), _pred(graph)
1.140 + {
1.141 + LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
1.142 + "The length type of Suurballe must be integer");
1.143 + }
1.144
1.145 /// Destructor.
1.146 ~Suurballe() {
1.147 @@ -250,11 +267,14 @@
1.148 /// \brief Set the flow map.
1.149 ///
1.150 /// This function sets the flow map.
1.151 + /// If it is not used before calling \ref run() or \ref init(),
1.152 + /// an instance will be allocated automatically. The destructor
1.153 + /// deallocates this automatically allocated map, of course.
1.154 ///
1.155 - /// The found flow contains only 0 and 1 values. It is the union of
1.156 - /// the found arc-disjoint paths.
1.157 + /// The found flow contains only 0 and 1 values, since it is the
1.158 + /// union of the found arc-disjoint paths.
1.159 ///
1.160 - /// \return \c (*this)
1.161 + /// \return <tt>(*this)</tt>
1.162 Suurballe& flowMap(FlowMap &map) {
1.163 if (_local_flow) {
1.164 delete _flow;
1.165 @@ -267,11 +287,14 @@
1.166 /// \brief Set the potential map.
1.167 ///
1.168 /// This function sets the potential map.
1.169 + /// If it is not used before calling \ref run() or \ref init(),
1.170 + /// an instance will be allocated automatically. The destructor
1.171 + /// deallocates this automatically allocated map, of course.
1.172 ///
1.173 - /// The potentials provide the dual solution of the underlying
1.174 - /// minimum cost flow problem.
1.175 + /// The node potentials provide the dual solution of the underlying
1.176 + /// \ref min_cost_flow "minimum cost flow problem".
1.177 ///
1.178 - /// \return \c (*this)
1.179 + /// \return <tt>(*this)</tt>
1.180 Suurballe& potentialMap(PotentialMap &map) {
1.181 if (_local_potential) {
1.182 delete _potential;
1.183 @@ -281,7 +304,7 @@
1.184 return *this;
1.185 }
1.186
1.187 - /// \name Execution control
1.188 + /// \name Execution Control
1.189 /// The simplest way to execute the algorithm is to call the run()
1.190 /// function.
1.191 /// \n
1.192 @@ -294,22 +317,24 @@
1.193 ///
1.194 /// This function runs the algorithm.
1.195 ///
1.196 + /// \param s The source node.
1.197 + /// \param t The target node.
1.198 /// \param k The number of paths to be found.
1.199 ///
1.200 /// \return \c k if there are at least \c k arc-disjoint paths from
1.201 /// \c s to \c t in the digraph. Otherwise it returns the number of
1.202 /// arc-disjoint paths found.
1.203 ///
1.204 - /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
1.205 - /// shortcut of the following code.
1.206 + /// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
1.207 + /// just a shortcut of the following code.
1.208 /// \code
1.209 - /// s.init();
1.210 - /// s.findFlow(k);
1.211 + /// s.init(s);
1.212 + /// s.findFlow(t, k);
1.213 /// s.findPaths();
1.214 /// \endcode
1.215 - int run(int k = 2) {
1.216 - init();
1.217 - findFlow(k);
1.218 + int run(const Node& s, const Node& t, int k = 2) {
1.219 + init(s);
1.220 + findFlow(t, k);
1.221 findPaths();
1.222 return _path_num;
1.223 }
1.224 @@ -317,7 +342,11 @@
1.225 /// \brief Initialize the algorithm.
1.226 ///
1.227 /// This function initializes the algorithm.
1.228 - void init() {
1.229 + ///
1.230 + /// \param s The source node.
1.231 + void init(const Node& s) {
1.232 + _source = s;
1.233 +
1.234 // Initialize maps
1.235 if (!_flow) {
1.236 _flow = new FlowMap(_graph);
1.237 @@ -329,25 +358,28 @@
1.238 }
1.239 for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
1.240 for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
1.241 -
1.242 - _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
1.243 - *_potential, _pred,
1.244 - _source, _target );
1.245 }
1.246
1.247 - /// \brief Execute the successive shortest path algorithm to find
1.248 - /// an optimal flow.
1.249 + /// \brief Execute the algorithm to find an optimal flow.
1.250 ///
1.251 /// This function executes the successive shortest path algorithm to
1.252 - /// find a minimum cost flow, which is the union of \c k or less
1.253 + /// find a minimum cost flow, which is the union of \c k (or less)
1.254 /// arc-disjoint paths.
1.255 ///
1.256 + /// \param t The target node.
1.257 + /// \param k The number of paths to be found.
1.258 + ///
1.259 /// \return \c k if there are at least \c k arc-disjoint paths from
1.260 - /// \c s to \c t in the digraph. Otherwise it returns the number of
1.261 - /// arc-disjoint paths found.
1.262 + /// the source node to the given node \c t in the digraph.
1.263 + /// Otherwise it returns the number of arc-disjoint paths found.
1.264 ///
1.265 /// \pre \ref init() must be called before using this function.
1.266 - int findFlow(int k = 2) {
1.267 + int findFlow(const Node& t, int k = 2) {
1.268 + _target = t;
1.269 + _dijkstra =
1.270 + new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
1.271 + _source, _target );
1.272 +
1.273 // Find shortest paths
1.274 _path_num = 0;
1.275 while (_path_num < k) {
1.276 @@ -373,13 +405,12 @@
1.277
1.278 /// \brief Compute the paths from the flow.
1.279 ///
1.280 - /// This function computes the paths from the flow.
1.281 + /// This function computes the paths from the found minimum cost flow,
1.282 + /// which is the union of some arc-disjoint paths.
1.283 ///
1.284 /// \pre \ref init() and \ref findFlow() must be called before using
1.285 /// this function.
1.286 void findPaths() {
1.287 - // Create the residual flow map (the union of the paths not found
1.288 - // so far)
1.289 FlowMap res_flow(_graph);
1.290 for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
1.291
1.292 @@ -406,10 +437,37 @@
1.293
1.294 /// @{
1.295
1.296 - /// \brief Return a const reference to the arc map storing the
1.297 + /// \brief Return the total length of the found paths.
1.298 + ///
1.299 + /// This function returns the total length of the found paths, i.e.
1.300 + /// the total cost of the found flow.
1.301 + /// The complexity of the function is O(e).
1.302 + ///
1.303 + /// \pre \ref run() or \ref findFlow() must be called before using
1.304 + /// this function.
1.305 + Length totalLength() const {
1.306 + Length c = 0;
1.307 + for (ArcIt e(_graph); e != INVALID; ++e)
1.308 + c += (*_flow)[e] * _length[e];
1.309 + return c;
1.310 + }
1.311 +
1.312 + /// \brief Return the flow value on the given arc.
1.313 + ///
1.314 + /// This function returns the flow value on the given arc.
1.315 + /// It is \c 1 if the arc is involved in one of the found arc-disjoint
1.316 + /// paths, otherwise it is \c 0.
1.317 + ///
1.318 + /// \pre \ref run() or \ref findFlow() must be called before using
1.319 + /// this function.
1.320 + int flow(const Arc& arc) const {
1.321 + return (*_flow)[arc];
1.322 + }
1.323 +
1.324 + /// \brief Return a const reference to an arc map storing the
1.325 /// found flow.
1.326 ///
1.327 - /// This function returns a const reference to the arc map storing
1.328 + /// This function returns a const reference to an arc map storing
1.329 /// the flow that is the union of the found arc-disjoint paths.
1.330 ///
1.331 /// \pre \ref run() or \ref findFlow() must be called before using
1.332 @@ -418,34 +476,11 @@
1.333 return *_flow;
1.334 }
1.335
1.336 - /// \brief Return a const reference to the node map storing the
1.337 - /// found potentials (the dual solution).
1.338 - ///
1.339 - /// This function returns a const reference to the node map storing
1.340 - /// the found potentials that provide the dual solution of the
1.341 - /// underlying minimum cost flow problem.
1.342 - ///
1.343 - /// \pre \ref run() or \ref findFlow() must be called before using
1.344 - /// this function.
1.345 - const PotentialMap& potentialMap() const {
1.346 - return *_potential;
1.347 - }
1.348 -
1.349 - /// \brief Return the flow on the given arc.
1.350 - ///
1.351 - /// This function returns the flow on the given arc.
1.352 - /// It is \c 1 if the arc is involved in one of the found paths,
1.353 - /// otherwise it is \c 0.
1.354 - ///
1.355 - /// \pre \ref run() or \ref findFlow() must be called before using
1.356 - /// this function.
1.357 - int flow(const Arc& arc) const {
1.358 - return (*_flow)[arc];
1.359 - }
1.360 -
1.361 /// \brief Return the potential of the given node.
1.362 ///
1.363 /// This function returns the potential of the given node.
1.364 + /// The node potentials provide the dual solution of the
1.365 + /// underlying \ref min_cost_flow "minimum cost flow problem".
1.366 ///
1.367 /// \pre \ref run() or \ref findFlow() must be called before using
1.368 /// this function.
1.369 @@ -453,18 +488,17 @@
1.370 return (*_potential)[node];
1.371 }
1.372
1.373 - /// \brief Return the total length (cost) of the found paths (flow).
1.374 + /// \brief Return a const reference to a node map storing the
1.375 + /// found potentials (the dual solution).
1.376 ///
1.377 - /// This function returns the total length (cost) of the found paths
1.378 - /// (flow). The complexity of the function is \f$ O(e) \f$.
1.379 + /// This function returns a const reference to a node map storing
1.380 + /// the found potentials that provide the dual solution of the
1.381 + /// underlying \ref min_cost_flow "minimum cost flow problem".
1.382 ///
1.383 /// \pre \ref run() or \ref findFlow() must be called before using
1.384 /// this function.
1.385 - Length totalLength() const {
1.386 - Length c = 0;
1.387 - for (ArcIt e(_graph); e != INVALID; ++e)
1.388 - c += (*_flow)[e] * _length[e];
1.389 - return c;
1.390 + const PotentialMap& potentialMap() const {
1.391 + return *_potential;
1.392 }
1.393
1.394 /// \brief Return the number of the found paths.
1.395 @@ -481,7 +515,7 @@
1.396 ///
1.397 /// This function returns a const reference to the specified path.
1.398 ///
1.399 - /// \param i The function returns the \c i-th path.
1.400 + /// \param i The function returns the <tt>i</tt>-th path.
1.401 /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
1.402 ///
1.403 /// \pre \ref run() or \ref findPaths() must be called before using