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