lemon/suurballe.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 16 Oct 2009 01:06:16 +0200
changeset 926 ec0b1b423b8b
parent 925 30c77d1c0cba
child 927 9a7e4e606f83
permissions -rw-r--r--
Rework and improve Suurballe (#323)

- Improve the implementation: use a specific, faster variant of
residual Dijkstra for the first search.
- Some reorganizatiopn to make the code simpler.
- Small doc improvements.
alpar@463
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@357
     2
 *
alpar@463
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@357
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
alpar@357
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@357
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@357
     8
 *
alpar@357
     9
 * Permission to use, modify and distribute this software is granted
alpar@357
    10
 * provided that this copyright notice appears in all copies. For
alpar@357
    11
 * precise terms see the accompanying LICENSE file.
alpar@357
    12
 *
alpar@357
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@357
    14
 * express or implied, and with no claim as to its suitability for any
alpar@357
    15
 * purpose.
alpar@357
    16
 *
alpar@357
    17
 */
alpar@357
    18
alpar@357
    19
#ifndef LEMON_SUURBALLE_H
alpar@357
    20
#define LEMON_SUURBALLE_H
alpar@357
    21
alpar@357
    22
///\ingroup shortest_path
alpar@357
    23
///\file
alpar@357
    24
///\brief An algorithm for finding arc-disjoint paths between two
alpar@357
    25
/// nodes having minimum total length.
alpar@357
    26
alpar@357
    27
#include <vector>
kpeter@670
    28
#include <limits>
alpar@357
    29
#include <lemon/bin_heap.h>
alpar@357
    30
#include <lemon/path.h>
deba@566
    31
#include <lemon/list_graph.h>
deba@566
    32
#include <lemon/maps.h>
alpar@357
    33
alpar@357
    34
namespace lemon {
alpar@357
    35
alpar@357
    36
  /// \addtogroup shortest_path
alpar@357
    37
  /// @{
alpar@357
    38
kpeter@358
    39
  /// \brief Algorithm for finding arc-disjoint paths between two nodes
kpeter@358
    40
  /// having minimum total length.
alpar@357
    41
  ///
alpar@357
    42
  /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
alpar@357
    43
  /// finding arc-disjoint paths having minimum total length (cost)
kpeter@358
    44
  /// from a given source node to a given target node in a digraph.
alpar@357
    45
  ///
kpeter@670
    46
  /// Note that this problem is a special case of the \ref min_cost_flow
kpeter@670
    47
  /// "minimum cost flow problem". This implementation is actually an
kpeter@670
    48
  /// efficient specialized version of the \ref CapacityScaling
kpeter@926
    49
  /// "successive shortest path" algorithm directly for this problem.
kpeter@670
    50
  /// Therefore this class provides query functions for flow values and
kpeter@670
    51
  /// node potentials (the dual solution) just like the minimum cost flow
kpeter@670
    52
  /// algorithms.
alpar@357
    53
  ///
kpeter@606
    54
  /// \tparam GR The digraph type the algorithm runs on.
kpeter@670
    55
  /// \tparam LEN The type of the length map.
kpeter@670
    56
  /// The default value is <tt>GR::ArcMap<int></tt>.
alpar@357
    57
  ///
kpeter@925
    58
  /// \warning Length values should be \e non-negative.
alpar@357
    59
  ///
kpeter@926
    60
  /// \note For finding \e node-disjoint paths, this algorithm can be used
kpeter@670
    61
  /// along with the \ref SplitNodes adaptor.
kpeter@358
    62
#ifdef DOXYGEN
kpeter@606
    63
  template <typename GR, typename LEN>
kpeter@358
    64
#else
kpeter@670
    65
  template < typename GR,
kpeter@606
    66
             typename LEN = typename GR::template ArcMap<int> >
kpeter@358
    67
#endif
alpar@357
    68
  class Suurballe
alpar@357
    69
  {
kpeter@606
    70
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
alpar@357
    71
alpar@357
    72
    typedef ConstMap<Arc, int> ConstArcMap;
kpeter@606
    73
    typedef typename GR::template NodeMap<Arc> PredMap;
alpar@357
    74
alpar@357
    75
  public:
alpar@357
    76
kpeter@606
    77
    /// The type of the digraph the algorithm runs on.
kpeter@606
    78
    typedef GR Digraph;
kpeter@606
    79
    /// The type of the length map.
kpeter@606
    80
    typedef LEN LengthMap;
kpeter@606
    81
    /// The type of the lengths.
kpeter@606
    82
    typedef typename LengthMap::Value Length;
kpeter@670
    83
#ifdef DOXYGEN
kpeter@670
    84
    /// The type of the flow map.
kpeter@670
    85
    typedef GR::ArcMap<int> FlowMap;
kpeter@670
    86
    /// The type of the potential map.
kpeter@670
    87
    typedef GR::NodeMap<Length> PotentialMap;
kpeter@670
    88
#else
alpar@357
    89
    /// The type of the flow map.
alpar@357
    90
    typedef typename Digraph::template ArcMap<int> FlowMap;
alpar@357
    91
    /// The type of the potential map.
alpar@357
    92
    typedef typename Digraph::template NodeMap<Length> PotentialMap;
kpeter@670
    93
#endif
kpeter@670
    94
alpar@357
    95
    /// The type of the path structures.
kpeter@670
    96
    typedef SimplePath<GR> Path;
alpar@357
    97
alpar@357
    98
  private:
alpar@463
    99
kpeter@670
   100
    // ResidualDijkstra is a special implementation of the
kpeter@670
   101
    // Dijkstra algorithm for finding shortest paths in the
kpeter@670
   102
    // residual network with respect to the reduced arc lengths
kpeter@670
   103
    // and modifying the node potentials according to the
kpeter@670
   104
    // distance of the nodes.
alpar@357
   105
    class ResidualDijkstra
alpar@357
   106
    {
alpar@357
   107
      typedef typename Digraph::template NodeMap<int> HeapCrossRef;
alpar@357
   108
      typedef BinHeap<Length, HeapCrossRef> Heap;
alpar@357
   109
alpar@357
   110
    private:
alpar@357
   111
alpar@357
   112
      const Digraph &_graph;
kpeter@926
   113
      const LengthMap &_length;
alpar@357
   114
      const FlowMap &_flow;
kpeter@926
   115
      PotentialMap &_pi;
alpar@357
   116
      PredMap &_pred;
alpar@357
   117
      Node _s;
alpar@357
   118
      Node _t;
kpeter@926
   119
      
kpeter@926
   120
      PotentialMap _dist;
kpeter@926
   121
      std::vector<Node> _proc_nodes;
alpar@357
   122
alpar@357
   123
    public:
alpar@357
   124
kpeter@926
   125
      // Constructor
kpeter@926
   126
      ResidualDijkstra(Suurballe &srb) :
kpeter@926
   127
        _graph(srb._graph), _length(srb._length),
kpeter@926
   128
        _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), 
kpeter@926
   129
        _s(srb._s), _t(srb._t), _dist(_graph) {}
kpeter@926
   130
        
kpeter@926
   131
      // Run the algorithm and return true if a path is found
kpeter@926
   132
      // from the source node to the target node.
kpeter@926
   133
      bool run(int cnt) {
kpeter@926
   134
        return cnt == 0 ? startFirst() : start();
kpeter@926
   135
      }
alpar@357
   136
kpeter@926
   137
    private:
kpeter@926
   138
    
kpeter@926
   139
      // Execute the algorithm for the first time (the flow and potential
kpeter@926
   140
      // functions have to be identically zero).
kpeter@926
   141
      bool startFirst() {
alpar@357
   142
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
alpar@357
   143
        Heap heap(heap_cross_ref);
alpar@357
   144
        heap.push(_s, 0);
alpar@357
   145
        _pred[_s] = INVALID;
alpar@357
   146
        _proc_nodes.clear();
alpar@357
   147
kpeter@358
   148
        // Process nodes
alpar@357
   149
        while (!heap.empty() && heap.top() != _t) {
alpar@357
   150
          Node u = heap.top(), v;
kpeter@926
   151
          Length d = heap.prio(), dn;
alpar@357
   152
          _dist[u] = heap.prio();
kpeter@926
   153
          _proc_nodes.push_back(u);
alpar@357
   154
          heap.pop();
kpeter@926
   155
kpeter@926
   156
          // Traverse outgoing arcs
kpeter@926
   157
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
kpeter@926
   158
            v = _graph.target(e);
kpeter@926
   159
            switch(heap.state(v)) {
kpeter@926
   160
              case Heap::PRE_HEAP:
kpeter@926
   161
                heap.push(v, d + _length[e]);
kpeter@926
   162
                _pred[v] = e;
kpeter@926
   163
                break;
kpeter@926
   164
              case Heap::IN_HEAP:
kpeter@926
   165
                dn = d + _length[e];
kpeter@926
   166
                if (dn < heap[v]) {
kpeter@926
   167
                  heap.decrease(v, dn);
kpeter@926
   168
                  _pred[v] = e;
kpeter@926
   169
                }
kpeter@926
   170
                break;
kpeter@926
   171
              case Heap::POST_HEAP:
kpeter@926
   172
                break;
kpeter@926
   173
            }
kpeter@926
   174
          }
kpeter@926
   175
        }
kpeter@926
   176
        if (heap.empty()) return false;
kpeter@926
   177
kpeter@926
   178
        // Update potentials of processed nodes
kpeter@926
   179
        Length t_dist = heap.prio();
kpeter@926
   180
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
kpeter@926
   181
          _pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist;
kpeter@926
   182
        return true;
kpeter@926
   183
      }
kpeter@926
   184
kpeter@926
   185
      // Execute the algorithm.
kpeter@926
   186
      bool start() {
kpeter@926
   187
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
kpeter@926
   188
        Heap heap(heap_cross_ref);
kpeter@926
   189
        heap.push(_s, 0);
kpeter@926
   190
        _pred[_s] = INVALID;
kpeter@926
   191
        _proc_nodes.clear();
kpeter@926
   192
kpeter@926
   193
        // Process nodes
kpeter@926
   194
        while (!heap.empty() && heap.top() != _t) {
kpeter@926
   195
          Node u = heap.top(), v;
kpeter@926
   196
          Length d = heap.prio() + _pi[u], dn;
kpeter@926
   197
          _dist[u] = heap.prio();
alpar@357
   198
          _proc_nodes.push_back(u);
kpeter@926
   199
          heap.pop();
alpar@357
   200
kpeter@358
   201
          // Traverse outgoing arcs
alpar@357
   202
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
alpar@357
   203
            if (_flow[e] == 0) {
alpar@357
   204
              v = _graph.target(e);
alpar@357
   205
              switch(heap.state(v)) {
kpeter@926
   206
                case Heap::PRE_HEAP:
kpeter@926
   207
                  heap.push(v, d + _length[e] - _pi[v]);
alpar@357
   208
                  _pred[v] = e;
kpeter@926
   209
                  break;
kpeter@926
   210
                case Heap::IN_HEAP:
kpeter@926
   211
                  dn = d + _length[e] - _pi[v];
kpeter@926
   212
                  if (dn < heap[v]) {
kpeter@926
   213
                    heap.decrease(v, dn);
kpeter@926
   214
                    _pred[v] = e;
kpeter@926
   215
                  }
kpeter@926
   216
                  break;
kpeter@926
   217
                case Heap::POST_HEAP:
kpeter@926
   218
                  break;
alpar@357
   219
              }
alpar@357
   220
            }
alpar@357
   221
          }
alpar@357
   222
kpeter@358
   223
          // Traverse incoming arcs
alpar@357
   224
          for (InArcIt e(_graph, u); e != INVALID; ++e) {
alpar@357
   225
            if (_flow[e] == 1) {
alpar@357
   226
              v = _graph.source(e);
alpar@357
   227
              switch(heap.state(v)) {
kpeter@926
   228
                case Heap::PRE_HEAP:
kpeter@926
   229
                  heap.push(v, d - _length[e] - _pi[v]);
alpar@357
   230
                  _pred[v] = e;
kpeter@926
   231
                  break;
kpeter@926
   232
                case Heap::IN_HEAP:
kpeter@926
   233
                  dn = d - _length[e] - _pi[v];
kpeter@926
   234
                  if (dn < heap[v]) {
kpeter@926
   235
                    heap.decrease(v, dn);
kpeter@926
   236
                    _pred[v] = e;
kpeter@926
   237
                  }
kpeter@926
   238
                  break;
kpeter@926
   239
                case Heap::POST_HEAP:
kpeter@926
   240
                  break;
alpar@357
   241
              }
alpar@357
   242
            }
alpar@357
   243
          }
alpar@357
   244
        }
alpar@357
   245
        if (heap.empty()) return false;
alpar@357
   246
kpeter@358
   247
        // Update potentials of processed nodes
alpar@357
   248
        Length t_dist = heap.prio();
alpar@357
   249
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
kpeter@926
   250
          _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
alpar@357
   251
        return true;
alpar@357
   252
      }
alpar@357
   253
alpar@357
   254
    }; //class ResidualDijkstra
alpar@357
   255
alpar@357
   256
  private:
alpar@357
   257
kpeter@358
   258
    // The digraph the algorithm runs on
alpar@357
   259
    const Digraph &_graph;
alpar@357
   260
    // The length map
alpar@357
   261
    const LengthMap &_length;
alpar@463
   262
alpar@357
   263
    // Arc map of the current flow
alpar@357
   264
    FlowMap *_flow;
alpar@357
   265
    bool _local_flow;
alpar@357
   266
    // Node map of the current potentials
alpar@357
   267
    PotentialMap *_potential;
alpar@357
   268
    bool _local_potential;
alpar@357
   269
alpar@357
   270
    // The source node
kpeter@926
   271
    Node _s;
alpar@357
   272
    // The target node
kpeter@926
   273
    Node _t;
alpar@357
   274
alpar@357
   275
    // Container to store the found paths
kpeter@926
   276
    std::vector<Path> _paths;
alpar@357
   277
    int _path_num;
alpar@357
   278
alpar@357
   279
    // The pred arc map
alpar@357
   280
    PredMap _pred;
alpar@357
   281
alpar@357
   282
  public:
alpar@357
   283
alpar@357
   284
    /// \brief Constructor.
alpar@357
   285
    ///
alpar@357
   286
    /// Constructor.
alpar@357
   287
    ///
kpeter@670
   288
    /// \param graph The digraph the algorithm runs on.
alpar@357
   289
    /// \param length The length (cost) values of the arcs.
kpeter@670
   290
    Suurballe( const Digraph &graph,
kpeter@670
   291
               const LengthMap &length ) :
kpeter@670
   292
      _graph(graph), _length(length), _flow(0), _local_flow(false),
kpeter@670
   293
      _potential(0), _local_potential(false), _pred(graph)
kpeter@925
   294
    {}
alpar@357
   295
alpar@357
   296
    /// Destructor.
alpar@357
   297
    ~Suurballe() {
alpar@357
   298
      if (_local_flow) delete _flow;
alpar@357
   299
      if (_local_potential) delete _potential;
alpar@357
   300
    }
alpar@357
   301
kpeter@358
   302
    /// \brief Set the flow map.
alpar@357
   303
    ///
kpeter@358
   304
    /// This function sets the flow map.
kpeter@670
   305
    /// If it is not used before calling \ref run() or \ref init(),
kpeter@670
   306
    /// an instance will be allocated automatically. The destructor
kpeter@670
   307
    /// deallocates this automatically allocated map, of course.
alpar@357
   308
    ///
kpeter@670
   309
    /// The found flow contains only 0 and 1 values, since it is the
kpeter@670
   310
    /// union of the found arc-disjoint paths.
alpar@357
   311
    ///
kpeter@606
   312
    /// \return <tt>(*this)</tt>
alpar@357
   313
    Suurballe& flowMap(FlowMap &map) {
alpar@357
   314
      if (_local_flow) {
alpar@357
   315
        delete _flow;
alpar@357
   316
        _local_flow = false;
alpar@357
   317
      }
alpar@357
   318
      _flow = &map;
alpar@357
   319
      return *this;
alpar@357
   320
    }
alpar@357
   321
kpeter@358
   322
    /// \brief Set the potential map.
alpar@357
   323
    ///
kpeter@358
   324
    /// This function sets the potential map.
kpeter@670
   325
    /// If it is not used before calling \ref run() or \ref init(),
kpeter@670
   326
    /// an instance will be allocated automatically. The destructor
kpeter@670
   327
    /// deallocates this automatically allocated map, of course.
alpar@357
   328
    ///
kpeter@670
   329
    /// The node potentials provide the dual solution of the underlying
kpeter@670
   330
    /// \ref min_cost_flow "minimum cost flow problem".
alpar@357
   331
    ///
kpeter@606
   332
    /// \return <tt>(*this)</tt>
alpar@357
   333
    Suurballe& potentialMap(PotentialMap &map) {
alpar@357
   334
      if (_local_potential) {
alpar@357
   335
        delete _potential;
alpar@357
   336
        _local_potential = false;
alpar@357
   337
      }
alpar@357
   338
      _potential = &map;
alpar@357
   339
      return *this;
alpar@357
   340
    }
alpar@357
   341
kpeter@631
   342
    /// \name Execution Control
alpar@357
   343
    /// The simplest way to execute the algorithm is to call the run()
alpar@357
   344
    /// function.
alpar@357
   345
    /// \n
alpar@357
   346
    /// If you only need the flow that is the union of the found
alpar@357
   347
    /// arc-disjoint paths, you may call init() and findFlow().
alpar@357
   348
alpar@357
   349
    /// @{
alpar@357
   350
kpeter@358
   351
    /// \brief Run the algorithm.
alpar@357
   352
    ///
kpeter@358
   353
    /// This function runs the algorithm.
alpar@357
   354
    ///
kpeter@670
   355
    /// \param s The source node.
kpeter@670
   356
    /// \param t The target node.
alpar@357
   357
    /// \param k The number of paths to be found.
alpar@357
   358
    ///
kpeter@358
   359
    /// \return \c k if there are at least \c k arc-disjoint paths from
kpeter@358
   360
    /// \c s to \c t in the digraph. Otherwise it returns the number of
alpar@357
   361
    /// arc-disjoint paths found.
alpar@357
   362
    ///
kpeter@670
   363
    /// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
kpeter@670
   364
    /// just a shortcut of the following code.
alpar@357
   365
    /// \code
kpeter@670
   366
    ///   s.init(s);
kpeter@670
   367
    ///   s.findFlow(t, k);
alpar@357
   368
    ///   s.findPaths();
alpar@357
   369
    /// \endcode
kpeter@670
   370
    int run(const Node& s, const Node& t, int k = 2) {
kpeter@670
   371
      init(s);
kpeter@670
   372
      findFlow(t, k);
alpar@357
   373
      findPaths();
alpar@357
   374
      return _path_num;
alpar@357
   375
    }
alpar@357
   376
kpeter@358
   377
    /// \brief Initialize the algorithm.
alpar@357
   378
    ///
kpeter@358
   379
    /// This function initializes the algorithm.
kpeter@670
   380
    ///
kpeter@670
   381
    /// \param s The source node.
kpeter@670
   382
    void init(const Node& s) {
kpeter@926
   383
      _s = s;
kpeter@670
   384
kpeter@358
   385
      // Initialize maps
alpar@357
   386
      if (!_flow) {
alpar@357
   387
        _flow = new FlowMap(_graph);
alpar@357
   388
        _local_flow = true;
alpar@357
   389
      }
alpar@357
   390
      if (!_potential) {
alpar@357
   391
        _potential = new PotentialMap(_graph);
alpar@357
   392
        _local_potential = true;
alpar@357
   393
      }
alpar@357
   394
      for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
alpar@357
   395
      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
alpar@357
   396
    }
alpar@357
   397
kpeter@670
   398
    /// \brief Execute the algorithm to find an optimal flow.
alpar@357
   399
    ///
kpeter@358
   400
    /// This function executes the successive shortest path algorithm to
kpeter@670
   401
    /// find a minimum cost flow, which is the union of \c k (or less)
alpar@357
   402
    /// arc-disjoint paths.
alpar@357
   403
    ///
kpeter@670
   404
    /// \param t The target node.
kpeter@670
   405
    /// \param k The number of paths to be found.
kpeter@670
   406
    ///
kpeter@358
   407
    /// \return \c k if there are at least \c k arc-disjoint paths from
kpeter@670
   408
    /// the source node to the given node \c t in the digraph.
kpeter@670
   409
    /// Otherwise it returns the number of arc-disjoint paths found.
alpar@357
   410
    ///
alpar@357
   411
    /// \pre \ref init() must be called before using this function.
kpeter@670
   412
    int findFlow(const Node& t, int k = 2) {
kpeter@926
   413
      _t = t;
kpeter@926
   414
      ResidualDijkstra dijkstra(*this);
kpeter@670
   415
kpeter@358
   416
      // Find shortest paths
alpar@357
   417
      _path_num = 0;
alpar@357
   418
      while (_path_num < k) {
kpeter@358
   419
        // Run Dijkstra
kpeter@926
   420
        if (!dijkstra.run(_path_num)) break;
alpar@357
   421
        ++_path_num;
alpar@357
   422
kpeter@358
   423
        // Set the flow along the found shortest path
kpeter@926
   424
        Node u = _t;
alpar@357
   425
        Arc e;
alpar@357
   426
        while ((e = _pred[u]) != INVALID) {
alpar@357
   427
          if (u == _graph.target(e)) {
alpar@357
   428
            (*_flow)[e] = 1;
alpar@357
   429
            u = _graph.source(e);
alpar@357
   430
          } else {
alpar@357
   431
            (*_flow)[e] = 0;
alpar@357
   432
            u = _graph.target(e);
alpar@357
   433
          }
alpar@357
   434
        }
alpar@357
   435
      }
alpar@357
   436
      return _path_num;
alpar@357
   437
    }
alpar@463
   438
kpeter@358
   439
    /// \brief Compute the paths from the flow.
alpar@357
   440
    ///
kpeter@926
   441
    /// This function computes arc-disjoint paths from the found minimum
kpeter@926
   442
    /// cost flow, which is the union of them.
alpar@357
   443
    ///
alpar@357
   444
    /// \pre \ref init() and \ref findFlow() must be called before using
alpar@357
   445
    /// this function.
alpar@357
   446
    void findPaths() {
alpar@357
   447
      FlowMap res_flow(_graph);
kpeter@358
   448
      for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
alpar@357
   449
kpeter@926
   450
      _paths.clear();
kpeter@926
   451
      _paths.resize(_path_num);
alpar@357
   452
      for (int i = 0; i < _path_num; ++i) {
kpeter@926
   453
        Node n = _s;
kpeter@926
   454
        while (n != _t) {
alpar@357
   455
          OutArcIt e(_graph, n);
alpar@357
   456
          for ( ; res_flow[e] == 0; ++e) ;
alpar@357
   457
          n = _graph.target(e);
kpeter@926
   458
          _paths[i].addBack(e);
alpar@357
   459
          res_flow[e] = 0;
alpar@357
   460
        }
alpar@357
   461
      }
alpar@357
   462
    }
alpar@357
   463
alpar@357
   464
    /// @}
alpar@357
   465
alpar@357
   466
    /// \name Query Functions
kpeter@358
   467
    /// The results of the algorithm can be obtained using these
alpar@357
   468
    /// functions.
alpar@357
   469
    /// \n The algorithm should be executed before using them.
alpar@357
   470
alpar@357
   471
    /// @{
alpar@357
   472
kpeter@670
   473
    /// \brief Return the total length of the found paths.
kpeter@670
   474
    ///
kpeter@670
   475
    /// This function returns the total length of the found paths, i.e.
kpeter@670
   476
    /// the total cost of the found flow.
kpeter@670
   477
    /// The complexity of the function is O(e).
kpeter@670
   478
    ///
kpeter@670
   479
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@670
   480
    /// this function.
kpeter@670
   481
    Length totalLength() const {
kpeter@670
   482
      Length c = 0;
kpeter@670
   483
      for (ArcIt e(_graph); e != INVALID; ++e)
kpeter@670
   484
        c += (*_flow)[e] * _length[e];
kpeter@670
   485
      return c;
kpeter@670
   486
    }
kpeter@670
   487
kpeter@670
   488
    /// \brief Return the flow value on the given arc.
kpeter@670
   489
    ///
kpeter@670
   490
    /// This function returns the flow value on the given arc.
kpeter@670
   491
    /// It is \c 1 if the arc is involved in one of the found arc-disjoint
kpeter@670
   492
    /// paths, otherwise it is \c 0.
kpeter@670
   493
    ///
kpeter@670
   494
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@670
   495
    /// this function.
kpeter@670
   496
    int flow(const Arc& arc) const {
kpeter@670
   497
      return (*_flow)[arc];
kpeter@670
   498
    }
kpeter@670
   499
kpeter@670
   500
    /// \brief Return a const reference to an arc map storing the
alpar@357
   501
    /// found flow.
alpar@357
   502
    ///
kpeter@670
   503
    /// This function returns a const reference to an arc map storing
kpeter@358
   504
    /// the flow that is the union of the found arc-disjoint paths.
alpar@357
   505
    ///
kpeter@358
   506
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@358
   507
    /// this function.
alpar@357
   508
    const FlowMap& flowMap() const {
alpar@357
   509
      return *_flow;
alpar@357
   510
    }
alpar@357
   511
kpeter@358
   512
    /// \brief Return the potential of the given node.
alpar@357
   513
    ///
kpeter@358
   514
    /// This function returns the potential of the given node.
kpeter@670
   515
    /// The node potentials provide the dual solution of the
kpeter@670
   516
    /// underlying \ref min_cost_flow "minimum cost flow problem".
alpar@357
   517
    ///
kpeter@358
   518
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@358
   519
    /// this function.
alpar@357
   520
    Length potential(const Node& node) const {
alpar@357
   521
      return (*_potential)[node];
alpar@357
   522
    }
alpar@357
   523
kpeter@670
   524
    /// \brief Return a const reference to a node map storing the
kpeter@670
   525
    /// found potentials (the dual solution).
alpar@357
   526
    ///
kpeter@670
   527
    /// This function returns a const reference to a node map storing
kpeter@670
   528
    /// the found potentials that provide the dual solution of the
kpeter@670
   529
    /// underlying \ref min_cost_flow "minimum cost flow problem".
alpar@357
   530
    ///
kpeter@358
   531
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@358
   532
    /// this function.
kpeter@670
   533
    const PotentialMap& potentialMap() const {
kpeter@670
   534
      return *_potential;
alpar@357
   535
    }
alpar@357
   536
kpeter@358
   537
    /// \brief Return the number of the found paths.
alpar@357
   538
    ///
kpeter@358
   539
    /// This function returns the number of the found paths.
alpar@357
   540
    ///
kpeter@358
   541
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@358
   542
    /// this function.
alpar@357
   543
    int pathNum() const {
alpar@357
   544
      return _path_num;
alpar@357
   545
    }
alpar@357
   546
kpeter@358
   547
    /// \brief Return a const reference to the specified path.
alpar@357
   548
    ///
kpeter@358
   549
    /// This function returns a const reference to the specified path.
alpar@357
   550
    ///
kpeter@670
   551
    /// \param i The function returns the <tt>i</tt>-th path.
alpar@357
   552
    /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
alpar@357
   553
    ///
kpeter@358
   554
    /// \pre \ref run() or \ref findPaths() must be called before using
kpeter@358
   555
    /// this function.
kpeter@924
   556
    const Path& path(int i) const {
kpeter@926
   557
      return _paths[i];
alpar@357
   558
    }
alpar@357
   559
alpar@357
   560
    /// @}
alpar@357
   561
alpar@357
   562
  }; //class Suurballe
alpar@357
   563
alpar@357
   564
  ///@}
alpar@357
   565
alpar@357
   566
} //namespace lemon
alpar@357
   567
alpar@357
   568
#endif //LEMON_SUURBALLE_H