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