lemon/suurballe.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 03 Mar 2010 17:14:17 +0000
changeset 856 5df6a8f29d5e
parent 853 ec0b1b423b8b
child 857 abb95d48e89e
permissions -rw-r--r--
Merge #181, #323
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>
kpeter@854
    32
#include <lemon/dijkstra.h>
deba@519
    33
#include <lemon/maps.h>
alpar@345
    34
alpar@345
    35
namespace lemon {
alpar@345
    36
alpar@345
    37
  /// \addtogroup shortest_path
alpar@345
    38
  /// @{
alpar@345
    39
kpeter@346
    40
  /// \brief Algorithm for finding arc-disjoint paths between two nodes
kpeter@346
    41
  /// having minimum total length.
alpar@345
    42
  ///
alpar@345
    43
  /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
alpar@345
    44
  /// finding arc-disjoint paths having minimum total length (cost)
kpeter@346
    45
  /// from a given source node to a given target node in a digraph.
alpar@345
    46
  ///
kpeter@623
    47
  /// Note that this problem is a special case of the \ref min_cost_flow
kpeter@623
    48
  /// "minimum cost flow problem". This implementation is actually an
kpeter@623
    49
  /// efficient specialized version of the \ref CapacityScaling
kpeter@853
    50
  /// "successive shortest path" algorithm directly for this problem.
kpeter@623
    51
  /// Therefore this class provides query functions for flow values and
kpeter@623
    52
  /// node potentials (the dual solution) just like the minimum cost flow
kpeter@623
    53
  /// algorithms.
alpar@345
    54
  ///
kpeter@559
    55
  /// \tparam GR The digraph type the algorithm runs on.
kpeter@623
    56
  /// \tparam LEN The type of the length map.
kpeter@623
    57
  /// The default value is <tt>GR::ArcMap<int></tt>.
alpar@345
    58
  ///
kpeter@852
    59
  /// \warning Length values should be \e non-negative.
alpar@345
    60
  ///
kpeter@853
    61
  /// \note For finding \e node-disjoint paths, this algorithm can be used
kpeter@623
    62
  /// along with the \ref SplitNodes adaptor.
kpeter@346
    63
#ifdef DOXYGEN
kpeter@559
    64
  template <typename GR, typename LEN>
kpeter@346
    65
#else
kpeter@623
    66
  template < typename GR,
kpeter@559
    67
             typename LEN = typename GR::template ArcMap<int> >
kpeter@346
    68
#endif
alpar@345
    69
  class Suurballe
alpar@345
    70
  {
kpeter@559
    71
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
alpar@345
    72
alpar@345
    73
    typedef ConstMap<Arc, int> ConstArcMap;
kpeter@559
    74
    typedef typename GR::template NodeMap<Arc> PredMap;
alpar@345
    75
alpar@345
    76
  public:
alpar@345
    77
kpeter@559
    78
    /// The type of the digraph the algorithm runs on.
kpeter@559
    79
    typedef GR Digraph;
kpeter@559
    80
    /// The type of the length map.
kpeter@559
    81
    typedef LEN LengthMap;
kpeter@559
    82
    /// The type of the lengths.
kpeter@559
    83
    typedef typename LengthMap::Value Length;
kpeter@623
    84
#ifdef DOXYGEN
kpeter@623
    85
    /// The type of the flow map.
kpeter@623
    86
    typedef GR::ArcMap<int> FlowMap;
kpeter@623
    87
    /// The type of the potential map.
kpeter@623
    88
    typedef GR::NodeMap<Length> PotentialMap;
kpeter@623
    89
#else
alpar@345
    90
    /// The type of the flow map.
alpar@345
    91
    typedef typename Digraph::template ArcMap<int> FlowMap;
alpar@345
    92
    /// The type of the potential map.
alpar@345
    93
    typedef typename Digraph::template NodeMap<Length> PotentialMap;
kpeter@623
    94
#endif
kpeter@623
    95
alpar@345
    96
    /// The type of the path structures.
kpeter@623
    97
    typedef SimplePath<GR> Path;
alpar@345
    98
alpar@345
    99
  private:
alpar@440
   100
kpeter@854
   101
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
kpeter@854
   102
    typedef BinHeap<Length, HeapCrossRef> Heap;
kpeter@854
   103
kpeter@623
   104
    // ResidualDijkstra is a special implementation of the
kpeter@623
   105
    // Dijkstra algorithm for finding shortest paths in the
kpeter@623
   106
    // residual network with respect to the reduced arc lengths
kpeter@623
   107
    // and modifying the node potentials according to the
kpeter@623
   108
    // distance of the nodes.
alpar@345
   109
    class ResidualDijkstra
alpar@345
   110
    {
alpar@345
   111
    private:
alpar@345
   112
alpar@345
   113
      const Digraph &_graph;
kpeter@853
   114
      const LengthMap &_length;
alpar@345
   115
      const FlowMap &_flow;
kpeter@853
   116
      PotentialMap &_pi;
alpar@345
   117
      PredMap &_pred;
alpar@345
   118
      Node _s;
alpar@345
   119
      Node _t;
kpeter@853
   120
      
kpeter@853
   121
      PotentialMap _dist;
kpeter@853
   122
      std::vector<Node> _proc_nodes;
alpar@345
   123
alpar@345
   124
    public:
alpar@345
   125
kpeter@853
   126
      // Constructor
kpeter@853
   127
      ResidualDijkstra(Suurballe &srb) :
kpeter@853
   128
        _graph(srb._graph), _length(srb._length),
kpeter@853
   129
        _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), 
kpeter@853
   130
        _s(srb._s), _t(srb._t), _dist(_graph) {}
kpeter@853
   131
        
kpeter@853
   132
      // Run the algorithm and return true if a path is found
kpeter@853
   133
      // from the source node to the target node.
kpeter@853
   134
      bool run(int cnt) {
kpeter@853
   135
        return cnt == 0 ? startFirst() : start();
kpeter@853
   136
      }
alpar@345
   137
kpeter@853
   138
    private:
kpeter@853
   139
    
kpeter@853
   140
      // Execute the algorithm for the first time (the flow and potential
kpeter@853
   141
      // functions have to be identically zero).
kpeter@853
   142
      bool startFirst() {
alpar@345
   143
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
alpar@345
   144
        Heap heap(heap_cross_ref);
alpar@345
   145
        heap.push(_s, 0);
alpar@345
   146
        _pred[_s] = INVALID;
alpar@345
   147
        _proc_nodes.clear();
alpar@345
   148
kpeter@346
   149
        // Process nodes
alpar@345
   150
        while (!heap.empty() && heap.top() != _t) {
alpar@345
   151
          Node u = heap.top(), v;
kpeter@853
   152
          Length d = heap.prio(), dn;
alpar@345
   153
          _dist[u] = heap.prio();
kpeter@853
   154
          _proc_nodes.push_back(u);
alpar@345
   155
          heap.pop();
kpeter@853
   156
kpeter@853
   157
          // Traverse outgoing arcs
kpeter@853
   158
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
kpeter@853
   159
            v = _graph.target(e);
kpeter@853
   160
            switch(heap.state(v)) {
kpeter@853
   161
              case Heap::PRE_HEAP:
kpeter@853
   162
                heap.push(v, d + _length[e]);
kpeter@853
   163
                _pred[v] = e;
kpeter@853
   164
                break;
kpeter@853
   165
              case Heap::IN_HEAP:
kpeter@853
   166
                dn = d + _length[e];
kpeter@853
   167
                if (dn < heap[v]) {
kpeter@853
   168
                  heap.decrease(v, dn);
kpeter@853
   169
                  _pred[v] = e;
kpeter@853
   170
                }
kpeter@853
   171
                break;
kpeter@853
   172
              case Heap::POST_HEAP:
kpeter@853
   173
                break;
kpeter@853
   174
            }
kpeter@853
   175
          }
kpeter@853
   176
        }
kpeter@853
   177
        if (heap.empty()) return false;
kpeter@853
   178
kpeter@853
   179
        // Update potentials of processed nodes
kpeter@853
   180
        Length t_dist = heap.prio();
kpeter@853
   181
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
kpeter@853
   182
          _pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist;
kpeter@853
   183
        return true;
kpeter@853
   184
      }
kpeter@853
   185
kpeter@853
   186
      // Execute the algorithm.
kpeter@853
   187
      bool start() {
kpeter@853
   188
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
kpeter@853
   189
        Heap heap(heap_cross_ref);
kpeter@853
   190
        heap.push(_s, 0);
kpeter@853
   191
        _pred[_s] = INVALID;
kpeter@853
   192
        _proc_nodes.clear();
kpeter@853
   193
kpeter@853
   194
        // Process nodes
kpeter@853
   195
        while (!heap.empty() && heap.top() != _t) {
kpeter@853
   196
          Node u = heap.top(), v;
kpeter@853
   197
          Length d = heap.prio() + _pi[u], dn;
kpeter@853
   198
          _dist[u] = heap.prio();
alpar@345
   199
          _proc_nodes.push_back(u);
kpeter@853
   200
          heap.pop();
alpar@345
   201
kpeter@346
   202
          // Traverse outgoing arcs
alpar@345
   203
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
alpar@345
   204
            if (_flow[e] == 0) {
alpar@345
   205
              v = _graph.target(e);
alpar@345
   206
              switch(heap.state(v)) {
kpeter@853
   207
                case Heap::PRE_HEAP:
kpeter@853
   208
                  heap.push(v, d + _length[e] - _pi[v]);
alpar@345
   209
                  _pred[v] = e;
kpeter@853
   210
                  break;
kpeter@853
   211
                case Heap::IN_HEAP:
kpeter@853
   212
                  dn = d + _length[e] - _pi[v];
kpeter@853
   213
                  if (dn < heap[v]) {
kpeter@853
   214
                    heap.decrease(v, dn);
kpeter@853
   215
                    _pred[v] = e;
kpeter@853
   216
                  }
kpeter@853
   217
                  break;
kpeter@853
   218
                case Heap::POST_HEAP:
kpeter@853
   219
                  break;
alpar@345
   220
              }
alpar@345
   221
            }
alpar@345
   222
          }
alpar@345
   223
kpeter@346
   224
          // Traverse incoming arcs
alpar@345
   225
          for (InArcIt e(_graph, u); e != INVALID; ++e) {
alpar@345
   226
            if (_flow[e] == 1) {
alpar@345
   227
              v = _graph.source(e);
alpar@345
   228
              switch(heap.state(v)) {
kpeter@853
   229
                case Heap::PRE_HEAP:
kpeter@853
   230
                  heap.push(v, d - _length[e] - _pi[v]);
alpar@345
   231
                  _pred[v] = e;
kpeter@853
   232
                  break;
kpeter@853
   233
                case Heap::IN_HEAP:
kpeter@853
   234
                  dn = d - _length[e] - _pi[v];
kpeter@853
   235
                  if (dn < heap[v]) {
kpeter@853
   236
                    heap.decrease(v, dn);
kpeter@853
   237
                    _pred[v] = e;
kpeter@853
   238
                  }
kpeter@853
   239
                  break;
kpeter@853
   240
                case Heap::POST_HEAP:
kpeter@853
   241
                  break;
alpar@345
   242
              }
alpar@345
   243
            }
alpar@345
   244
          }
alpar@345
   245
        }
alpar@345
   246
        if (heap.empty()) return false;
alpar@345
   247
kpeter@346
   248
        // Update potentials of processed nodes
alpar@345
   249
        Length t_dist = heap.prio();
alpar@345
   250
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
kpeter@853
   251
          _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
alpar@345
   252
        return true;
alpar@345
   253
      }
alpar@345
   254
alpar@345
   255
    }; //class ResidualDijkstra
alpar@345
   256
alpar@345
   257
  private:
alpar@345
   258
kpeter@346
   259
    // The digraph the algorithm runs on
alpar@345
   260
    const Digraph &_graph;
alpar@345
   261
    // The length map
alpar@345
   262
    const LengthMap &_length;
alpar@440
   263
alpar@345
   264
    // Arc map of the current flow
alpar@345
   265
    FlowMap *_flow;
alpar@345
   266
    bool _local_flow;
alpar@345
   267
    // Node map of the current potentials
alpar@345
   268
    PotentialMap *_potential;
alpar@345
   269
    bool _local_potential;
alpar@345
   270
alpar@345
   271
    // The source node
kpeter@853
   272
    Node _s;
alpar@345
   273
    // The target node
kpeter@853
   274
    Node _t;
alpar@345
   275
alpar@345
   276
    // Container to store the found paths
kpeter@853
   277
    std::vector<Path> _paths;
alpar@345
   278
    int _path_num;
alpar@345
   279
alpar@345
   280
    // The pred arc map
alpar@345
   281
    PredMap _pred;
kpeter@854
   282
    
kpeter@854
   283
    // Data for full init
kpeter@854
   284
    PotentialMap *_init_dist;
kpeter@854
   285
    PredMap *_init_pred;
kpeter@854
   286
    bool _full_init;
alpar@345
   287
alpar@345
   288
  public:
alpar@345
   289
alpar@345
   290
    /// \brief Constructor.
alpar@345
   291
    ///
alpar@345
   292
    /// Constructor.
alpar@345
   293
    ///
kpeter@623
   294
    /// \param graph The digraph the algorithm runs on.
alpar@345
   295
    /// \param length The length (cost) values of the arcs.
kpeter@623
   296
    Suurballe( const Digraph &graph,
kpeter@623
   297
               const LengthMap &length ) :
kpeter@623
   298
      _graph(graph), _length(length), _flow(0), _local_flow(false),
kpeter@854
   299
      _potential(0), _local_potential(false), _pred(graph),
kpeter@854
   300
      _init_dist(0), _init_pred(0)
kpeter@852
   301
    {}
alpar@345
   302
alpar@345
   303
    /// Destructor.
alpar@345
   304
    ~Suurballe() {
alpar@345
   305
      if (_local_flow) delete _flow;
alpar@345
   306
      if (_local_potential) delete _potential;
kpeter@854
   307
      delete _init_dist;
kpeter@854
   308
      delete _init_pred;
alpar@345
   309
    }
alpar@345
   310
kpeter@346
   311
    /// \brief Set the flow map.
alpar@345
   312
    ///
kpeter@346
   313
    /// This function sets the flow map.
kpeter@623
   314
    /// If it is not used before calling \ref run() or \ref init(),
kpeter@623
   315
    /// an instance will be allocated automatically. The destructor
kpeter@623
   316
    /// deallocates this automatically allocated map, of course.
alpar@345
   317
    ///
kpeter@623
   318
    /// The found flow contains only 0 and 1 values, since it is the
kpeter@623
   319
    /// union of the found arc-disjoint paths.
alpar@345
   320
    ///
kpeter@559
   321
    /// \return <tt>(*this)</tt>
alpar@345
   322
    Suurballe& flowMap(FlowMap &map) {
alpar@345
   323
      if (_local_flow) {
alpar@345
   324
        delete _flow;
alpar@345
   325
        _local_flow = false;
alpar@345
   326
      }
alpar@345
   327
      _flow = &map;
alpar@345
   328
      return *this;
alpar@345
   329
    }
alpar@345
   330
kpeter@346
   331
    /// \brief Set the potential map.
alpar@345
   332
    ///
kpeter@346
   333
    /// This function sets the potential map.
kpeter@623
   334
    /// If it is not used before calling \ref run() or \ref init(),
kpeter@623
   335
    /// an instance will be allocated automatically. The destructor
kpeter@623
   336
    /// deallocates this automatically allocated map, of course.
alpar@345
   337
    ///
kpeter@623
   338
    /// The node potentials provide the dual solution of the underlying
kpeter@623
   339
    /// \ref min_cost_flow "minimum cost flow problem".
alpar@345
   340
    ///
kpeter@559
   341
    /// \return <tt>(*this)</tt>
alpar@345
   342
    Suurballe& potentialMap(PotentialMap &map) {
alpar@345
   343
      if (_local_potential) {
alpar@345
   344
        delete _potential;
alpar@345
   345
        _local_potential = false;
alpar@345
   346
      }
alpar@345
   347
      _potential = &map;
alpar@345
   348
      return *this;
alpar@345
   349
    }
alpar@345
   350
kpeter@584
   351
    /// \name Execution Control
alpar@345
   352
    /// The simplest way to execute the algorithm is to call the run()
kpeter@854
   353
    /// function.\n
kpeter@854
   354
    /// If you need to execute the algorithm many times using the same
kpeter@854
   355
    /// source node, then you may call fullInit() once and start()
kpeter@854
   356
    /// for each target node.\n
alpar@345
   357
    /// If you only need the flow that is the union of the found
kpeter@854
   358
    /// arc-disjoint paths, then you may call findFlow() instead of
kpeter@854
   359
    /// start().
alpar@345
   360
alpar@345
   361
    /// @{
alpar@345
   362
kpeter@346
   363
    /// \brief Run the algorithm.
alpar@345
   364
    ///
kpeter@346
   365
    /// This function runs the algorithm.
alpar@345
   366
    ///
kpeter@623
   367
    /// \param s The source node.
kpeter@623
   368
    /// \param t The target node.
alpar@345
   369
    /// \param k The number of paths to be found.
alpar@345
   370
    ///
kpeter@346
   371
    /// \return \c k if there are at least \c k arc-disjoint paths from
kpeter@346
   372
    /// \c s to \c t in the digraph. Otherwise it returns the number of
alpar@345
   373
    /// arc-disjoint paths found.
alpar@345
   374
    ///
kpeter@623
   375
    /// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
kpeter@623
   376
    /// just a shortcut of the following code.
alpar@345
   377
    /// \code
kpeter@623
   378
    ///   s.init(s);
kpeter@854
   379
    ///   s.start(t, k);
alpar@345
   380
    /// \endcode
kpeter@623
   381
    int run(const Node& s, const Node& t, int k = 2) {
kpeter@623
   382
      init(s);
kpeter@854
   383
      start(t, k);
alpar@345
   384
      return _path_num;
alpar@345
   385
    }
alpar@345
   386
kpeter@346
   387
    /// \brief Initialize the algorithm.
alpar@345
   388
    ///
kpeter@854
   389
    /// This function initializes the algorithm with the given source node.
kpeter@623
   390
    ///
kpeter@623
   391
    /// \param s The source node.
kpeter@623
   392
    void init(const Node& s) {
kpeter@853
   393
      _s = s;
kpeter@623
   394
kpeter@346
   395
      // Initialize maps
alpar@345
   396
      if (!_flow) {
alpar@345
   397
        _flow = new FlowMap(_graph);
alpar@345
   398
        _local_flow = true;
alpar@345
   399
      }
alpar@345
   400
      if (!_potential) {
alpar@345
   401
        _potential = new PotentialMap(_graph);
alpar@345
   402
        _local_potential = true;
alpar@345
   403
      }
kpeter@854
   404
      _full_init = false;
kpeter@854
   405
    }
kpeter@854
   406
kpeter@854
   407
    /// \brief Initialize the algorithm and perform Dijkstra.
kpeter@854
   408
    ///
kpeter@854
   409
    /// This function initializes the algorithm and performs a full
kpeter@854
   410
    /// Dijkstra search from the given source node. It makes consecutive
kpeter@854
   411
    /// executions of \ref start() "start(t, k)" faster, since they
kpeter@854
   412
    /// have to perform %Dijkstra only k-1 times.
kpeter@854
   413
    ///
kpeter@854
   414
    /// This initialization is usually worth using instead of \ref init()
kpeter@854
   415
    /// if the algorithm is executed many times using the same source node.
kpeter@854
   416
    ///
kpeter@854
   417
    /// \param s The source node.
kpeter@854
   418
    void fullInit(const Node& s) {
kpeter@854
   419
      // Initialize maps
kpeter@854
   420
      init(s);
kpeter@854
   421
      if (!_init_dist) {
kpeter@854
   422
        _init_dist = new PotentialMap(_graph);
kpeter@854
   423
      }
kpeter@854
   424
      if (!_init_pred) {
kpeter@854
   425
        _init_pred = new PredMap(_graph);
kpeter@854
   426
      }
kpeter@854
   427
kpeter@854
   428
      // Run a full Dijkstra
kpeter@854
   429
      typename Dijkstra<Digraph, LengthMap>
kpeter@854
   430
        ::template SetStandardHeap<Heap>
kpeter@854
   431
        ::template SetDistMap<PotentialMap>
kpeter@854
   432
        ::template SetPredMap<PredMap>
kpeter@854
   433
        ::Create dijk(_graph, _length);
kpeter@854
   434
      dijk.distMap(*_init_dist).predMap(*_init_pred);
kpeter@854
   435
      dijk.run(s);
kpeter@854
   436
      
kpeter@854
   437
      _full_init = true;
kpeter@854
   438
    }
kpeter@854
   439
kpeter@854
   440
    /// \brief Execute the algorithm.
kpeter@854
   441
    ///
kpeter@854
   442
    /// This function executes the algorithm.
kpeter@854
   443
    ///
kpeter@854
   444
    /// \param t The target node.
kpeter@854
   445
    /// \param k The number of paths to be found.
kpeter@854
   446
    ///
kpeter@854
   447
    /// \return \c k if there are at least \c k arc-disjoint paths from
kpeter@854
   448
    /// \c s to \c t in the digraph. Otherwise it returns the number of
kpeter@854
   449
    /// arc-disjoint paths found.
kpeter@854
   450
    ///
kpeter@854
   451
    /// \note Apart from the return value, <tt>s.start(t, k)</tt> is
kpeter@854
   452
    /// just a shortcut of the following code.
kpeter@854
   453
    /// \code
kpeter@854
   454
    ///   s.findFlow(t, k);
kpeter@854
   455
    ///   s.findPaths();
kpeter@854
   456
    /// \endcode
kpeter@854
   457
    int start(const Node& t, int k = 2) {
kpeter@854
   458
      findFlow(t, k);
kpeter@854
   459
      findPaths();
kpeter@854
   460
      return _path_num;
alpar@345
   461
    }
alpar@345
   462
kpeter@623
   463
    /// \brief Execute the algorithm to find an optimal flow.
alpar@345
   464
    ///
kpeter@346
   465
    /// This function executes the successive shortest path algorithm to
kpeter@623
   466
    /// find a minimum cost flow, which is the union of \c k (or less)
alpar@345
   467
    /// arc-disjoint paths.
alpar@345
   468
    ///
kpeter@623
   469
    /// \param t The target node.
kpeter@623
   470
    /// \param k The number of paths to be found.
kpeter@623
   471
    ///
kpeter@346
   472
    /// \return \c k if there are at least \c k arc-disjoint paths from
kpeter@623
   473
    /// the source node to the given node \c t in the digraph.
kpeter@623
   474
    /// Otherwise it returns the number of arc-disjoint paths found.
alpar@345
   475
    ///
alpar@345
   476
    /// \pre \ref init() must be called before using this function.
kpeter@623
   477
    int findFlow(const Node& t, int k = 2) {
kpeter@853
   478
      _t = t;
kpeter@853
   479
      ResidualDijkstra dijkstra(*this);
kpeter@854
   480
      
kpeter@854
   481
      // Initialization
kpeter@854
   482
      for (ArcIt e(_graph); e != INVALID; ++e) {
kpeter@854
   483
        (*_flow)[e] = 0;
kpeter@854
   484
      }
kpeter@854
   485
      if (_full_init) {
kpeter@854
   486
        for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@854
   487
          (*_potential)[n] = (*_init_dist)[n];
kpeter@854
   488
        }
kpeter@854
   489
        Node u = _t;
kpeter@854
   490
        Arc e;
kpeter@854
   491
        while ((e = (*_init_pred)[u]) != INVALID) {
kpeter@854
   492
          (*_flow)[e] = 1;
kpeter@854
   493
          u = _graph.source(e);
kpeter@854
   494
        }        
kpeter@854
   495
        _path_num = 1;
kpeter@854
   496
      } else {
kpeter@854
   497
        for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@854
   498
          (*_potential)[n] = 0;
kpeter@854
   499
        }
kpeter@854
   500
        _path_num = 0;
kpeter@854
   501
      }
kpeter@623
   502
kpeter@346
   503
      // Find shortest paths
alpar@345
   504
      while (_path_num < k) {
kpeter@346
   505
        // Run Dijkstra
kpeter@853
   506
        if (!dijkstra.run(_path_num)) break;
alpar@345
   507
        ++_path_num;
alpar@345
   508
kpeter@346
   509
        // Set the flow along the found shortest path
kpeter@853
   510
        Node u = _t;
alpar@345
   511
        Arc e;
alpar@345
   512
        while ((e = _pred[u]) != INVALID) {
alpar@345
   513
          if (u == _graph.target(e)) {
alpar@345
   514
            (*_flow)[e] = 1;
alpar@345
   515
            u = _graph.source(e);
alpar@345
   516
          } else {
alpar@345
   517
            (*_flow)[e] = 0;
alpar@345
   518
            u = _graph.target(e);
alpar@345
   519
          }
alpar@345
   520
        }
alpar@345
   521
      }
alpar@345
   522
      return _path_num;
alpar@345
   523
    }
alpar@440
   524
kpeter@346
   525
    /// \brief Compute the paths from the flow.
alpar@345
   526
    ///
kpeter@853
   527
    /// This function computes arc-disjoint paths from the found minimum
kpeter@853
   528
    /// cost flow, which is the union of them.
alpar@345
   529
    ///
alpar@345
   530
    /// \pre \ref init() and \ref findFlow() must be called before using
alpar@345
   531
    /// this function.
alpar@345
   532
    void findPaths() {
alpar@345
   533
      FlowMap res_flow(_graph);
kpeter@346
   534
      for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
alpar@345
   535
kpeter@853
   536
      _paths.clear();
kpeter@853
   537
      _paths.resize(_path_num);
alpar@345
   538
      for (int i = 0; i < _path_num; ++i) {
kpeter@853
   539
        Node n = _s;
kpeter@853
   540
        while (n != _t) {
alpar@345
   541
          OutArcIt e(_graph, n);
alpar@345
   542
          for ( ; res_flow[e] == 0; ++e) ;
alpar@345
   543
          n = _graph.target(e);
kpeter@853
   544
          _paths[i].addBack(e);
alpar@345
   545
          res_flow[e] = 0;
alpar@345
   546
        }
alpar@345
   547
      }
alpar@345
   548
    }
alpar@345
   549
alpar@345
   550
    /// @}
alpar@345
   551
alpar@345
   552
    /// \name Query Functions
kpeter@346
   553
    /// The results of the algorithm can be obtained using these
alpar@345
   554
    /// functions.
alpar@345
   555
    /// \n The algorithm should be executed before using them.
alpar@345
   556
alpar@345
   557
    /// @{
alpar@345
   558
kpeter@623
   559
    /// \brief Return the total length of the found paths.
kpeter@623
   560
    ///
kpeter@623
   561
    /// This function returns the total length of the found paths, i.e.
kpeter@623
   562
    /// the total cost of the found flow.
kpeter@623
   563
    /// The complexity of the function is O(e).
kpeter@623
   564
    ///
kpeter@623
   565
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@623
   566
    /// this function.
kpeter@623
   567
    Length totalLength() const {
kpeter@623
   568
      Length c = 0;
kpeter@623
   569
      for (ArcIt e(_graph); e != INVALID; ++e)
kpeter@623
   570
        c += (*_flow)[e] * _length[e];
kpeter@623
   571
      return c;
kpeter@623
   572
    }
kpeter@623
   573
kpeter@623
   574
    /// \brief Return the flow value on the given arc.
kpeter@623
   575
    ///
kpeter@623
   576
    /// This function returns the flow value on the given arc.
kpeter@623
   577
    /// It is \c 1 if the arc is involved in one of the found arc-disjoint
kpeter@623
   578
    /// paths, otherwise it is \c 0.
kpeter@623
   579
    ///
kpeter@623
   580
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@623
   581
    /// this function.
kpeter@623
   582
    int flow(const Arc& arc) const {
kpeter@623
   583
      return (*_flow)[arc];
kpeter@623
   584
    }
kpeter@623
   585
kpeter@623
   586
    /// \brief Return a const reference to an arc map storing the
alpar@345
   587
    /// found flow.
alpar@345
   588
    ///
kpeter@623
   589
    /// This function returns a const reference to an arc map storing
kpeter@346
   590
    /// the flow that is the union of the found arc-disjoint paths.
alpar@345
   591
    ///
kpeter@346
   592
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@346
   593
    /// this function.
alpar@345
   594
    const FlowMap& flowMap() const {
alpar@345
   595
      return *_flow;
alpar@345
   596
    }
alpar@345
   597
kpeter@346
   598
    /// \brief Return the potential of the given node.
alpar@345
   599
    ///
kpeter@346
   600
    /// This function returns the potential of the given node.
kpeter@623
   601
    /// The node potentials provide the dual solution of the
kpeter@623
   602
    /// underlying \ref min_cost_flow "minimum cost flow problem".
alpar@345
   603
    ///
kpeter@346
   604
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@346
   605
    /// this function.
alpar@345
   606
    Length potential(const Node& node) const {
alpar@345
   607
      return (*_potential)[node];
alpar@345
   608
    }
alpar@345
   609
kpeter@623
   610
    /// \brief Return a const reference to a node map storing the
kpeter@623
   611
    /// found potentials (the dual solution).
alpar@345
   612
    ///
kpeter@623
   613
    /// This function returns a const reference to a node map storing
kpeter@623
   614
    /// the found potentials that provide the dual solution of the
kpeter@623
   615
    /// underlying \ref min_cost_flow "minimum cost flow problem".
alpar@345
   616
    ///
kpeter@346
   617
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@346
   618
    /// this function.
kpeter@623
   619
    const PotentialMap& potentialMap() const {
kpeter@623
   620
      return *_potential;
alpar@345
   621
    }
alpar@345
   622
kpeter@346
   623
    /// \brief Return the number of the found paths.
alpar@345
   624
    ///
kpeter@346
   625
    /// This function returns the number of the found paths.
alpar@345
   626
    ///
kpeter@346
   627
    /// \pre \ref run() or \ref findFlow() must be called before using
kpeter@346
   628
    /// this function.
alpar@345
   629
    int pathNum() const {
alpar@345
   630
      return _path_num;
alpar@345
   631
    }
alpar@345
   632
kpeter@346
   633
    /// \brief Return a const reference to the specified path.
alpar@345
   634
    ///
kpeter@346
   635
    /// This function returns a const reference to the specified path.
alpar@345
   636
    ///
kpeter@623
   637
    /// \param i The function returns the <tt>i</tt>-th path.
alpar@345
   638
    /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
alpar@345
   639
    ///
kpeter@346
   640
    /// \pre \ref run() or \ref findPaths() must be called before using
kpeter@346
   641
    /// this function.
kpeter@851
   642
    const Path& path(int i) const {
kpeter@853
   643
      return _paths[i];
alpar@345
   644
    }
alpar@345
   645
alpar@345
   646
    /// @}
alpar@345
   647
alpar@345
   648
  }; //class Suurballe
alpar@345
   649
alpar@345
   650
  ///@}
alpar@345
   651
alpar@345
   652
} //namespace lemon
alpar@345
   653
alpar@345
   654
#endif //LEMON_SUURBALLE_H