lemon/suurballe.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 15 May 2015 10:16:48 +0200
changeset 1354 1de908281369
parent 1254 c5cd8960df74
permissions -rw-r--r--
Update Doxyfile.in

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