lemon/preflow.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 21 Nov 2008 14:26:58 +0000
changeset 391 624e673efa76
parent 390 53c5277ba294
child 392 db3251947eba
permissions -rw-r--r--
Def -> Set renaming in Preflow
alpar@389
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@389
     2
 *
alpar@389
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@389
     4
 *
alpar@389
     5
 * Copyright (C) 2003-2008
alpar@389
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@389
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@389
     8
 *
alpar@389
     9
 * Permission to use, modify and distribute this software is granted
alpar@389
    10
 * provided that this copyright notice appears in all copies. For
alpar@389
    11
 * precise terms see the accompanying LICENSE file.
alpar@389
    12
 *
alpar@389
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@389
    14
 * express or implied, and with no claim as to its suitability for any
alpar@389
    15
 * purpose.
alpar@389
    16
 *
alpar@389
    17
 */
alpar@389
    18
alpar@389
    19
#ifndef LEMON_PREFLOW_H
alpar@389
    20
#define LEMON_PREFLOW_H
alpar@389
    21
alpar@389
    22
#include <lemon/tolerance.h>
alpar@389
    23
#include <lemon/elevator.h>
alpar@389
    24
alpar@389
    25
/// \file
alpar@389
    26
/// \ingroup max_flow
alpar@389
    27
/// \brief Implementation of the preflow algorithm.
alpar@389
    28
alpar@389
    29
namespace lemon {
alpar@389
    30
alpar@389
    31
  /// \brief Default traits class of Preflow class.
alpar@389
    32
  ///
alpar@389
    33
  /// Default traits class of Preflow class.
alpar@389
    34
  /// \param _Graph Digraph type.
alpar@389
    35
  /// \param _CapacityMap Type of capacity map.
alpar@389
    36
  template <typename _Graph, typename _CapacityMap>
alpar@389
    37
  struct PreflowDefaultTraits {
alpar@389
    38
alpar@389
    39
    /// \brief The digraph type the algorithm runs on.
alpar@389
    40
    typedef _Graph Digraph;
alpar@389
    41
alpar@389
    42
    /// \brief The type of the map that stores the arc capacities.
alpar@389
    43
    ///
alpar@389
    44
    /// The type of the map that stores the arc capacities.
alpar@389
    45
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@389
    46
    typedef _CapacityMap CapacityMap;
alpar@389
    47
alpar@389
    48
    /// \brief The type of the length of the arcs.
alpar@389
    49
    typedef typename CapacityMap::Value Value;
alpar@389
    50
alpar@389
    51
    /// \brief The map type that stores the flow values.
alpar@389
    52
    ///
alpar@389
    53
    /// The map type that stores the flow values.
alpar@389
    54
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@389
    55
    typedef typename Digraph::template ArcMap<Value> FlowMap;
alpar@389
    56
alpar@389
    57
    /// \brief Instantiates a FlowMap.
alpar@389
    58
    ///
alpar@389
    59
    /// This function instantiates a \ref FlowMap.
alpar@389
    60
    /// \param digraph The digraph, to which we would like to define
alpar@389
    61
    /// the flow map.
alpar@389
    62
    static FlowMap* createFlowMap(const Digraph& digraph) {
alpar@389
    63
      return new FlowMap(digraph);
alpar@389
    64
    }
alpar@389
    65
alpar@389
    66
    /// \brief The eleavator type used by Preflow algorithm.
alpar@389
    67
    ///
alpar@389
    68
    /// The elevator type used by Preflow algorithm.
alpar@389
    69
    ///
alpar@389
    70
    /// \sa Elevator
alpar@389
    71
    /// \sa LinkedElevator
alpar@389
    72
    typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator;
alpar@389
    73
alpar@389
    74
    /// \brief Instantiates an Elevator.
alpar@389
    75
    ///
alpar@389
    76
    /// This function instantiates a \ref Elevator.
alpar@389
    77
    /// \param digraph The digraph, to which we would like to define
alpar@389
    78
    /// the elevator.
alpar@389
    79
    /// \param max_level The maximum level of the elevator.
alpar@389
    80
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
alpar@389
    81
      return new Elevator(digraph, max_level);
alpar@389
    82
    }
alpar@389
    83
alpar@389
    84
    /// \brief The tolerance used by the algorithm
alpar@389
    85
    ///
alpar@389
    86
    /// The tolerance used by the algorithm to handle inexact computation.
alpar@389
    87
    typedef lemon::Tolerance<Value> Tolerance;
alpar@389
    88
alpar@389
    89
  };
alpar@389
    90
alpar@389
    91
alpar@389
    92
  /// \ingroup max_flow
alpar@389
    93
  ///
alpar@389
    94
  /// \brief %Preflow algorithms class.
alpar@389
    95
  ///
alpar@389
    96
  /// This class provides an implementation of the Goldberg's \e
alpar@389
    97
  /// preflow \e algorithm producing a flow of maximum value in a
alpar@389
    98
  /// digraph. The preflow algorithms are the fastest known max
alpar@389
    99
  /// flow algorithms. The current implementation use a mixture of the
alpar@389
   100
  /// \e "highest label" and the \e "bound decrease" heuristics.
alpar@389
   101
  /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
alpar@389
   102
  ///
alpar@389
   103
  /// The algorithm consists from two phases. After the first phase
alpar@389
   104
  /// the maximal flow value and the minimum cut can be obtained. The
alpar@389
   105
  /// second phase constructs the feasible maximum flow on each arc.
alpar@389
   106
  ///
alpar@389
   107
  /// \param _Graph The digraph type the algorithm runs on.
alpar@389
   108
  /// \param _CapacityMap The flow map type.
alpar@389
   109
  /// \param _Traits Traits class to set various data types used by
alpar@389
   110
  /// the algorithm.  The default traits class is \ref
alpar@389
   111
  /// PreflowDefaultTraits.  See \ref PreflowDefaultTraits for the
alpar@389
   112
  /// documentation of a %Preflow traits class.
alpar@389
   113
  ///
alpar@389
   114
  ///\author Jacint Szabo and Balazs Dezso
alpar@389
   115
#ifdef DOXYGEN
alpar@389
   116
  template <typename _Graph, typename _CapacityMap, typename _Traits>
alpar@389
   117
#else
alpar@389
   118
  template <typename _Graph,
alpar@389
   119
            typename _CapacityMap = typename _Graph::template ArcMap<int>,
alpar@389
   120
            typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> >
alpar@389
   121
#endif
alpar@389
   122
  class Preflow {
alpar@389
   123
  public:
alpar@389
   124
alpar@389
   125
    typedef _Traits Traits;
alpar@389
   126
    typedef typename Traits::Digraph Digraph;
alpar@389
   127
    typedef typename Traits::CapacityMap CapacityMap;
alpar@389
   128
    typedef typename Traits::Value Value;
alpar@389
   129
alpar@389
   130
    typedef typename Traits::FlowMap FlowMap;
alpar@389
   131
    typedef typename Traits::Elevator Elevator;
alpar@389
   132
    typedef typename Traits::Tolerance Tolerance;
alpar@389
   133
alpar@389
   134
  private:
alpar@389
   135
alpar@389
   136
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@389
   137
alpar@389
   138
    const Digraph& _graph;
alpar@389
   139
    const CapacityMap* _capacity;
alpar@389
   140
alpar@389
   141
    int _node_num;
alpar@389
   142
alpar@389
   143
    Node _source, _target;
alpar@389
   144
alpar@389
   145
    FlowMap* _flow;
alpar@389
   146
    bool _local_flow;
alpar@389
   147
alpar@389
   148
    Elevator* _level;
alpar@389
   149
    bool _local_level;
alpar@389
   150
alpar@389
   151
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
alpar@389
   152
    ExcessMap* _excess;
alpar@389
   153
alpar@389
   154
    Tolerance _tolerance;
alpar@389
   155
alpar@389
   156
    bool _phase;
alpar@389
   157
alpar@389
   158
alpar@389
   159
    void createStructures() {
alpar@389
   160
      _node_num = countNodes(_graph);
alpar@389
   161
alpar@389
   162
      if (!_flow) {
alpar@389
   163
        _flow = Traits::createFlowMap(_graph);
alpar@389
   164
        _local_flow = true;
alpar@389
   165
      }
alpar@389
   166
      if (!_level) {
alpar@389
   167
        _level = Traits::createElevator(_graph, _node_num);
alpar@389
   168
        _local_level = true;
alpar@389
   169
      }
alpar@389
   170
      if (!_excess) {
alpar@389
   171
        _excess = new ExcessMap(_graph);
alpar@389
   172
      }
alpar@389
   173
    }
alpar@389
   174
alpar@389
   175
    void destroyStructures() {
alpar@389
   176
      if (_local_flow) {
alpar@389
   177
        delete _flow;
alpar@389
   178
      }
alpar@389
   179
      if (_local_level) {
alpar@389
   180
        delete _level;
alpar@389
   181
      }
alpar@389
   182
      if (_excess) {
alpar@389
   183
        delete _excess;
alpar@389
   184
      }
alpar@389
   185
    }
alpar@389
   186
alpar@389
   187
  public:
alpar@389
   188
alpar@389
   189
    typedef Preflow Create;
alpar@389
   190
alpar@389
   191
    ///\name Named template parameters
alpar@389
   192
alpar@389
   193
    ///@{
alpar@389
   194
alpar@389
   195
    template <typename _FlowMap>
alpar@391
   196
    struct SetFlowMapTraits : public Traits {
alpar@389
   197
      typedef _FlowMap FlowMap;
alpar@389
   198
      static FlowMap *createFlowMap(const Digraph&) {
alpar@390
   199
        LEMON_ASSERT(false, "FlowMap is not initialized");
alpar@390
   200
        return 0; // ignore warnings
alpar@389
   201
      }
alpar@389
   202
    };
alpar@389
   203
alpar@389
   204
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@389
   205
    /// FlowMap type
alpar@389
   206
    ///
alpar@389
   207
    /// \ref named-templ-param "Named parameter" for setting FlowMap
alpar@389
   208
    /// type
alpar@389
   209
    template <typename _FlowMap>
alpar@391
   210
    struct SetFlowMap
alpar@391
   211
      : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > {
alpar@389
   212
      typedef Preflow<Digraph, CapacityMap,
alpar@391
   213
                      SetFlowMapTraits<_FlowMap> > Create;
alpar@389
   214
    };
alpar@389
   215
alpar@389
   216
    template <typename _Elevator>
alpar@391
   217
    struct SetElevatorTraits : public Traits {
alpar@389
   218
      typedef _Elevator Elevator;
alpar@389
   219
      static Elevator *createElevator(const Digraph&, int) {
alpar@390
   220
        LEMON_ASSERT(false, "Elevator is not initialized");
alpar@390
   221
        return 0; // ignore warnings
alpar@389
   222
      }
alpar@389
   223
    };
alpar@389
   224
alpar@389
   225
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@389
   226
    /// Elevator type
alpar@389
   227
    ///
alpar@389
   228
    /// \ref named-templ-param "Named parameter" for setting Elevator
alpar@389
   229
    /// type
alpar@389
   230
    template <typename _Elevator>
alpar@391
   231
    struct SetElevator
alpar@391
   232
      : public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > {
alpar@389
   233
      typedef Preflow<Digraph, CapacityMap,
alpar@391
   234
                      SetElevatorTraits<_Elevator> > Create;
alpar@389
   235
    };
alpar@389
   236
alpar@389
   237
    template <typename _Elevator>
alpar@391
   238
    struct SetStandardElevatorTraits : public Traits {
alpar@389
   239
      typedef _Elevator Elevator;
alpar@389
   240
      static Elevator *createElevator(const Digraph& digraph, int max_level) {
alpar@389
   241
        return new Elevator(digraph, max_level);
alpar@389
   242
      }
alpar@389
   243
    };
alpar@389
   244
alpar@389
   245
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@389
   246
    /// Elevator type
alpar@389
   247
    ///
alpar@389
   248
    /// \ref named-templ-param "Named parameter" for setting Elevator
alpar@389
   249
    /// type. The Elevator should be standard constructor interface, ie.
alpar@389
   250
    /// the digraph and the maximum level should be passed to it.
alpar@389
   251
    template <typename _Elevator>
alpar@391
   252
    struct SetStandardElevator
alpar@389
   253
      : public Preflow<Digraph, CapacityMap,
alpar@391
   254
                       SetStandardElevatorTraits<_Elevator> > {
alpar@389
   255
      typedef Preflow<Digraph, CapacityMap,
alpar@391
   256
                      SetStandardElevatorTraits<_Elevator> > Create;
alpar@389
   257
    };
alpar@389
   258
alpar@389
   259
    /// @}
alpar@389
   260
alpar@389
   261
  protected:
alpar@389
   262
alpar@389
   263
    Preflow() {}
alpar@389
   264
alpar@389
   265
  public:
alpar@389
   266
alpar@389
   267
alpar@389
   268
    /// \brief The constructor of the class.
alpar@389
   269
    ///
alpar@389
   270
    /// The constructor of the class.
alpar@389
   271
    /// \param digraph The digraph the algorithm runs on.
alpar@389
   272
    /// \param capacity The capacity of the arcs.
alpar@389
   273
    /// \param source The source node.
alpar@389
   274
    /// \param target The target node.
alpar@389
   275
    Preflow(const Digraph& digraph, const CapacityMap& capacity,
alpar@389
   276
               Node source, Node target)
alpar@389
   277
      : _graph(digraph), _capacity(&capacity),
alpar@389
   278
        _node_num(0), _source(source), _target(target),
alpar@389
   279
        _flow(0), _local_flow(false),
alpar@389
   280
        _level(0), _local_level(false),
alpar@389
   281
        _excess(0), _tolerance(), _phase() {}
alpar@389
   282
alpar@389
   283
    /// \brief Destrcutor.
alpar@389
   284
    ///
alpar@389
   285
    /// Destructor.
alpar@389
   286
    ~Preflow() {
alpar@389
   287
      destroyStructures();
alpar@389
   288
    }
alpar@389
   289
alpar@389
   290
    /// \brief Sets the capacity map.
alpar@389
   291
    ///
alpar@389
   292
    /// Sets the capacity map.
alpar@389
   293
    /// \return \c (*this)
alpar@389
   294
    Preflow& capacityMap(const CapacityMap& map) {
alpar@389
   295
      _capacity = &map;
alpar@389
   296
      return *this;
alpar@389
   297
    }
alpar@389
   298
alpar@389
   299
    /// \brief Sets the flow map.
alpar@389
   300
    ///
alpar@389
   301
    /// Sets the flow map.
alpar@389
   302
    /// \return \c (*this)
alpar@389
   303
    Preflow& flowMap(FlowMap& map) {
alpar@389
   304
      if (_local_flow) {
alpar@389
   305
        delete _flow;
alpar@389
   306
        _local_flow = false;
alpar@389
   307
      }
alpar@389
   308
      _flow = &map;
alpar@389
   309
      return *this;
alpar@389
   310
    }
alpar@389
   311
alpar@389
   312
    /// \brief Returns the flow map.
alpar@389
   313
    ///
alpar@389
   314
    /// \return The flow map.
alpar@389
   315
    const FlowMap& flowMap() {
alpar@389
   316
      return *_flow;
alpar@389
   317
    }
alpar@389
   318
alpar@389
   319
    /// \brief Sets the elevator.
alpar@389
   320
    ///
alpar@389
   321
    /// Sets the elevator.
alpar@389
   322
    /// \return \c (*this)
alpar@389
   323
    Preflow& elevator(Elevator& elevator) {
alpar@389
   324
      if (_local_level) {
alpar@389
   325
        delete _level;
alpar@389
   326
        _local_level = false;
alpar@389
   327
      }
alpar@389
   328
      _level = &elevator;
alpar@389
   329
      return *this;
alpar@389
   330
    }
alpar@389
   331
alpar@389
   332
    /// \brief Returns the elevator.
alpar@389
   333
    ///
alpar@389
   334
    /// \return The elevator.
alpar@389
   335
    const Elevator& elevator() {
alpar@389
   336
      return *_level;
alpar@389
   337
    }
alpar@389
   338
alpar@389
   339
    /// \brief Sets the source node.
alpar@389
   340
    ///
alpar@389
   341
    /// Sets the source node.
alpar@389
   342
    /// \return \c (*this)
alpar@389
   343
    Preflow& source(const Node& node) {
alpar@389
   344
      _source = node;
alpar@389
   345
      return *this;
alpar@389
   346
    }
alpar@389
   347
alpar@389
   348
    /// \brief Sets the target node.
alpar@389
   349
    ///
alpar@389
   350
    /// Sets the target node.
alpar@389
   351
    /// \return \c (*this)
alpar@389
   352
    Preflow& target(const Node& node) {
alpar@389
   353
      _target = node;
alpar@389
   354
      return *this;
alpar@389
   355
    }
alpar@389
   356
alpar@389
   357
    /// \brief Sets the tolerance used by algorithm.
alpar@389
   358
    ///
alpar@389
   359
    /// Sets the tolerance used by algorithm.
alpar@389
   360
    Preflow& tolerance(const Tolerance& tolerance) const {
alpar@389
   361
      _tolerance = tolerance;
alpar@389
   362
      return *this;
alpar@389
   363
    }
alpar@389
   364
alpar@389
   365
    /// \brief Returns the tolerance used by algorithm.
alpar@389
   366
    ///
alpar@389
   367
    /// Returns the tolerance used by algorithm.
alpar@389
   368
    const Tolerance& tolerance() const {
alpar@389
   369
      return tolerance;
alpar@389
   370
    }
alpar@389
   371
alpar@389
   372
    /// \name Execution control The simplest way to execute the
alpar@389
   373
    /// algorithm is to use one of the member functions called \c
alpar@389
   374
    /// run().
alpar@389
   375
    /// \n
alpar@389
   376
    /// If you need more control on initial solution or
alpar@389
   377
    /// execution then you have to call one \ref init() function and then
alpar@389
   378
    /// the startFirstPhase() and if you need the startSecondPhase().
alpar@389
   379
alpar@389
   380
    ///@{
alpar@389
   381
alpar@389
   382
    /// \brief Initializes the internal data structures.
alpar@389
   383
    ///
alpar@389
   384
    /// Initializes the internal data structures.
alpar@389
   385
    ///
alpar@389
   386
    void init() {
alpar@389
   387
      createStructures();
alpar@389
   388
alpar@389
   389
      _phase = true;
alpar@389
   390
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@389
   391
        _excess->set(n, 0);
alpar@389
   392
      }
alpar@389
   393
alpar@389
   394
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@389
   395
        _flow->set(e, 0);
alpar@389
   396
      }
alpar@389
   397
alpar@389
   398
      typename Digraph::template NodeMap<bool> reached(_graph, false);
alpar@389
   399
alpar@389
   400
      _level->initStart();
alpar@389
   401
      _level->initAddItem(_target);
alpar@389
   402
alpar@389
   403
      std::vector<Node> queue;
alpar@389
   404
      reached.set(_source, true);
alpar@389
   405
alpar@389
   406
      queue.push_back(_target);
alpar@389
   407
      reached.set(_target, true);
alpar@389
   408
      while (!queue.empty()) {
alpar@389
   409
        _level->initNewLevel();
alpar@389
   410
        std::vector<Node> nqueue;
alpar@389
   411
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@389
   412
          Node n = queue[i];
alpar@389
   413
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   414
            Node u = _graph.source(e);
alpar@389
   415
            if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
alpar@389
   416
              reached.set(u, true);
alpar@389
   417
              _level->initAddItem(u);
alpar@389
   418
              nqueue.push_back(u);
alpar@389
   419
            }
alpar@389
   420
          }
alpar@389
   421
        }
alpar@389
   422
        queue.swap(nqueue);
alpar@389
   423
      }
alpar@389
   424
      _level->initFinish();
alpar@389
   425
alpar@389
   426
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@389
   427
        if (_tolerance.positive((*_capacity)[e])) {
alpar@389
   428
          Node u = _graph.target(e);
alpar@389
   429
          if ((*_level)[u] == _level->maxLevel()) continue;
alpar@389
   430
          _flow->set(e, (*_capacity)[e]);
alpar@389
   431
          _excess->set(u, (*_excess)[u] + (*_capacity)[e]);
alpar@389
   432
          if (u != _target && !_level->active(u)) {
alpar@389
   433
            _level->activate(u);
alpar@389
   434
          }
alpar@389
   435
        }
alpar@389
   436
      }
alpar@389
   437
    }
alpar@389
   438
alpar@389
   439
    /// \brief Initializes the internal data structures.
alpar@389
   440
    ///
alpar@389
   441
    /// Initializes the internal data structures and sets the initial
alpar@389
   442
    /// flow to the given \c flowMap. The \c flowMap should contain a
alpar@389
   443
    /// flow or at least a preflow, ie. in each node excluding the
alpar@389
   444
    /// target the incoming flow should greater or equal to the
alpar@389
   445
    /// outgoing flow.
alpar@389
   446
    /// \return %False when the given \c flowMap is not a preflow.
alpar@389
   447
    template <typename FlowMap>
alpar@389
   448
    bool flowInit(const FlowMap& flowMap) {
alpar@389
   449
      createStructures();
alpar@389
   450
alpar@389
   451
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@389
   452
        _flow->set(e, flowMap[e]);
alpar@389
   453
      }
alpar@389
   454
alpar@389
   455
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@389
   456
        Value excess = 0;
alpar@389
   457
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   458
          excess += (*_flow)[e];
alpar@389
   459
        }
alpar@389
   460
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   461
          excess -= (*_flow)[e];
alpar@389
   462
        }
alpar@389
   463
        if (excess < 0 && n != _source) return false;
alpar@389
   464
        _excess->set(n, excess);
alpar@389
   465
      }
alpar@389
   466
alpar@389
   467
      typename Digraph::template NodeMap<bool> reached(_graph, false);
alpar@389
   468
alpar@389
   469
      _level->initStart();
alpar@389
   470
      _level->initAddItem(_target);
alpar@389
   471
alpar@389
   472
      std::vector<Node> queue;
alpar@389
   473
      reached.set(_source, true);
alpar@389
   474
alpar@389
   475
      queue.push_back(_target);
alpar@389
   476
      reached.set(_target, true);
alpar@389
   477
      while (!queue.empty()) {
alpar@389
   478
        _level->initNewLevel();
alpar@389
   479
        std::vector<Node> nqueue;
alpar@389
   480
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@389
   481
          Node n = queue[i];
alpar@389
   482
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   483
            Node u = _graph.source(e);
alpar@389
   484
            if (!reached[u] &&
alpar@389
   485
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
alpar@389
   486
              reached.set(u, true);
alpar@389
   487
              _level->initAddItem(u);
alpar@389
   488
              nqueue.push_back(u);
alpar@389
   489
            }
alpar@389
   490
          }
alpar@389
   491
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   492
            Node v = _graph.target(e);
alpar@389
   493
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
alpar@389
   494
              reached.set(v, true);
alpar@389
   495
              _level->initAddItem(v);
alpar@389
   496
              nqueue.push_back(v);
alpar@389
   497
            }
alpar@389
   498
          }
alpar@389
   499
        }
alpar@389
   500
        queue.swap(nqueue);
alpar@389
   501
      }
alpar@389
   502
      _level->initFinish();
alpar@389
   503
alpar@389
   504
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@389
   505
        Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@389
   506
        if (_tolerance.positive(rem)) {
alpar@389
   507
          Node u = _graph.target(e);
alpar@389
   508
          if ((*_level)[u] == _level->maxLevel()) continue;
alpar@389
   509
          _flow->set(e, (*_capacity)[e]);
alpar@389
   510
          _excess->set(u, (*_excess)[u] + rem);
alpar@389
   511
          if (u != _target && !_level->active(u)) {
alpar@389
   512
            _level->activate(u);
alpar@389
   513
          }
alpar@389
   514
        }
alpar@389
   515
      }
alpar@389
   516
      for (InArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@389
   517
        Value rem = (*_flow)[e];
alpar@389
   518
        if (_tolerance.positive(rem)) {
alpar@389
   519
          Node v = _graph.source(e);
alpar@389
   520
          if ((*_level)[v] == _level->maxLevel()) continue;
alpar@389
   521
          _flow->set(e, 0);
alpar@389
   522
          _excess->set(v, (*_excess)[v] + rem);
alpar@389
   523
          if (v != _target && !_level->active(v)) {
alpar@389
   524
            _level->activate(v);
alpar@389
   525
          }
alpar@389
   526
        }
alpar@389
   527
      }
alpar@389
   528
      return true;
alpar@389
   529
    }
alpar@389
   530
alpar@389
   531
    /// \brief Starts the first phase of the preflow algorithm.
alpar@389
   532
    ///
alpar@389
   533
    /// The preflow algorithm consists of two phases, this method runs
alpar@389
   534
    /// the first phase. After the first phase the maximum flow value
alpar@389
   535
    /// and a minimum value cut can already be computed, although a
alpar@389
   536
    /// maximum flow is not yet obtained. So after calling this method
alpar@389
   537
    /// \ref flowValue() returns the value of a maximum flow and \ref
alpar@389
   538
    /// minCut() returns a minimum cut.
alpar@389
   539
    /// \pre One of the \ref init() functions should be called.
alpar@389
   540
    void startFirstPhase() {
alpar@389
   541
      _phase = true;
alpar@389
   542
alpar@389
   543
      Node n = _level->highestActive();
alpar@389
   544
      int level = _level->highestActiveLevel();
alpar@389
   545
      while (n != INVALID) {
alpar@389
   546
        int num = _node_num;
alpar@389
   547
alpar@389
   548
        while (num > 0 && n != INVALID) {
alpar@389
   549
          Value excess = (*_excess)[n];
alpar@389
   550
          int new_level = _level->maxLevel();
alpar@389
   551
alpar@389
   552
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   553
            Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@389
   554
            if (!_tolerance.positive(rem)) continue;
alpar@389
   555
            Node v = _graph.target(e);
alpar@389
   556
            if ((*_level)[v] < level) {
alpar@389
   557
              if (!_level->active(v) && v != _target) {
alpar@389
   558
                _level->activate(v);
alpar@389
   559
              }
alpar@389
   560
              if (!_tolerance.less(rem, excess)) {
alpar@389
   561
                _flow->set(e, (*_flow)[e] + excess);
alpar@389
   562
                _excess->set(v, (*_excess)[v] + excess);
alpar@389
   563
                excess = 0;
alpar@389
   564
                goto no_more_push_1;
alpar@389
   565
              } else {
alpar@389
   566
                excess -= rem;
alpar@389
   567
                _excess->set(v, (*_excess)[v] + rem);
alpar@389
   568
                _flow->set(e, (*_capacity)[e]);
alpar@389
   569
              }
alpar@389
   570
            } else if (new_level > (*_level)[v]) {
alpar@389
   571
              new_level = (*_level)[v];
alpar@389
   572
            }
alpar@389
   573
          }
alpar@389
   574
alpar@389
   575
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   576
            Value rem = (*_flow)[e];
alpar@389
   577
            if (!_tolerance.positive(rem)) continue;
alpar@389
   578
            Node v = _graph.source(e);
alpar@389
   579
            if ((*_level)[v] < level) {
alpar@389
   580
              if (!_level->active(v) && v != _target) {
alpar@389
   581
                _level->activate(v);
alpar@389
   582
              }
alpar@389
   583
              if (!_tolerance.less(rem, excess)) {
alpar@389
   584
                _flow->set(e, (*_flow)[e] - excess);
alpar@389
   585
                _excess->set(v, (*_excess)[v] + excess);
alpar@389
   586
                excess = 0;
alpar@389
   587
                goto no_more_push_1;
alpar@389
   588
              } else {
alpar@389
   589
                excess -= rem;
alpar@389
   590
                _excess->set(v, (*_excess)[v] + rem);
alpar@389
   591
                _flow->set(e, 0);
alpar@389
   592
              }
alpar@389
   593
            } else if (new_level > (*_level)[v]) {
alpar@389
   594
              new_level = (*_level)[v];
alpar@389
   595
            }
alpar@389
   596
          }
alpar@389
   597
alpar@389
   598
        no_more_push_1:
alpar@389
   599
alpar@389
   600
          _excess->set(n, excess);
alpar@389
   601
alpar@389
   602
          if (excess != 0) {
alpar@389
   603
            if (new_level + 1 < _level->maxLevel()) {
alpar@389
   604
              _level->liftHighestActive(new_level + 1);
alpar@389
   605
            } else {
alpar@389
   606
              _level->liftHighestActiveToTop();
alpar@389
   607
            }
alpar@389
   608
            if (_level->emptyLevel(level)) {
alpar@389
   609
              _level->liftToTop(level);
alpar@389
   610
            }
alpar@389
   611
          } else {
alpar@389
   612
            _level->deactivate(n);
alpar@389
   613
          }
alpar@389
   614
alpar@389
   615
          n = _level->highestActive();
alpar@389
   616
          level = _level->highestActiveLevel();
alpar@389
   617
          --num;
alpar@389
   618
        }
alpar@389
   619
alpar@389
   620
        num = _node_num * 20;
alpar@389
   621
        while (num > 0 && n != INVALID) {
alpar@389
   622
          Value excess = (*_excess)[n];
alpar@389
   623
          int new_level = _level->maxLevel();
alpar@389
   624
alpar@389
   625
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   626
            Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@389
   627
            if (!_tolerance.positive(rem)) continue;
alpar@389
   628
            Node v = _graph.target(e);
alpar@389
   629
            if ((*_level)[v] < level) {
alpar@389
   630
              if (!_level->active(v) && v != _target) {
alpar@389
   631
                _level->activate(v);
alpar@389
   632
              }
alpar@389
   633
              if (!_tolerance.less(rem, excess)) {
alpar@389
   634
                _flow->set(e, (*_flow)[e] + excess);
alpar@389
   635
                _excess->set(v, (*_excess)[v] + excess);
alpar@389
   636
                excess = 0;
alpar@389
   637
                goto no_more_push_2;
alpar@389
   638
              } else {
alpar@389
   639
                excess -= rem;
alpar@389
   640
                _excess->set(v, (*_excess)[v] + rem);
alpar@389
   641
                _flow->set(e, (*_capacity)[e]);
alpar@389
   642
              }
alpar@389
   643
            } else if (new_level > (*_level)[v]) {
alpar@389
   644
              new_level = (*_level)[v];
alpar@389
   645
            }
alpar@389
   646
          }
alpar@389
   647
alpar@389
   648
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   649
            Value rem = (*_flow)[e];
alpar@389
   650
            if (!_tolerance.positive(rem)) continue;
alpar@389
   651
            Node v = _graph.source(e);
alpar@389
   652
            if ((*_level)[v] < level) {
alpar@389
   653
              if (!_level->active(v) && v != _target) {
alpar@389
   654
                _level->activate(v);
alpar@389
   655
              }
alpar@389
   656
              if (!_tolerance.less(rem, excess)) {
alpar@389
   657
                _flow->set(e, (*_flow)[e] - excess);
alpar@389
   658
                _excess->set(v, (*_excess)[v] + excess);
alpar@389
   659
                excess = 0;
alpar@389
   660
                goto no_more_push_2;
alpar@389
   661
              } else {
alpar@389
   662
                excess -= rem;
alpar@389
   663
                _excess->set(v, (*_excess)[v] + rem);
alpar@389
   664
                _flow->set(e, 0);
alpar@389
   665
              }
alpar@389
   666
            } else if (new_level > (*_level)[v]) {
alpar@389
   667
              new_level = (*_level)[v];
alpar@389
   668
            }
alpar@389
   669
          }
alpar@389
   670
alpar@389
   671
        no_more_push_2:
alpar@389
   672
alpar@389
   673
          _excess->set(n, excess);
alpar@389
   674
alpar@389
   675
          if (excess != 0) {
alpar@389
   676
            if (new_level + 1 < _level->maxLevel()) {
alpar@389
   677
              _level->liftActiveOn(level, new_level + 1);
alpar@389
   678
            } else {
alpar@389
   679
              _level->liftActiveToTop(level);
alpar@389
   680
            }
alpar@389
   681
            if (_level->emptyLevel(level)) {
alpar@389
   682
              _level->liftToTop(level);
alpar@389
   683
            }
alpar@389
   684
          } else {
alpar@389
   685
            _level->deactivate(n);
alpar@389
   686
          }
alpar@389
   687
alpar@389
   688
          while (level >= 0 && _level->activeFree(level)) {
alpar@389
   689
            --level;
alpar@389
   690
          }
alpar@389
   691
          if (level == -1) {
alpar@389
   692
            n = _level->highestActive();
alpar@389
   693
            level = _level->highestActiveLevel();
alpar@389
   694
          } else {
alpar@389
   695
            n = _level->activeOn(level);
alpar@389
   696
          }
alpar@389
   697
          --num;
alpar@389
   698
        }
alpar@389
   699
      }
alpar@389
   700
    }
alpar@389
   701
alpar@389
   702
    /// \brief Starts the second phase of the preflow algorithm.
alpar@389
   703
    ///
alpar@389
   704
    /// The preflow algorithm consists of two phases, this method runs
alpar@389
   705
    /// the second phase. After calling \ref init() and \ref
alpar@389
   706
    /// startFirstPhase() and then \ref startSecondPhase(), \ref
alpar@389
   707
    /// flowMap() return a maximum flow, \ref flowValue() returns the
alpar@389
   708
    /// value of a maximum flow, \ref minCut() returns a minimum cut
alpar@389
   709
    /// \pre The \ref init() and startFirstPhase() functions should be
alpar@389
   710
    /// called before.
alpar@389
   711
    void startSecondPhase() {
alpar@389
   712
      _phase = false;
alpar@389
   713
alpar@389
   714
      typename Digraph::template NodeMap<bool> reached(_graph);
alpar@389
   715
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@389
   716
        reached.set(n, (*_level)[n] < _level->maxLevel());
alpar@389
   717
      }
alpar@389
   718
alpar@389
   719
      _level->initStart();
alpar@389
   720
      _level->initAddItem(_source);
alpar@389
   721
alpar@389
   722
      std::vector<Node> queue;
alpar@389
   723
      queue.push_back(_source);
alpar@389
   724
      reached.set(_source, true);
alpar@389
   725
alpar@389
   726
      while (!queue.empty()) {
alpar@389
   727
        _level->initNewLevel();
alpar@389
   728
        std::vector<Node> nqueue;
alpar@389
   729
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@389
   730
          Node n = queue[i];
alpar@389
   731
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   732
            Node v = _graph.target(e);
alpar@389
   733
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
alpar@389
   734
              reached.set(v, true);
alpar@389
   735
              _level->initAddItem(v);
alpar@389
   736
              nqueue.push_back(v);
alpar@389
   737
            }
alpar@389
   738
          }
alpar@389
   739
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   740
            Node u = _graph.source(e);
alpar@389
   741
            if (!reached[u] &&
alpar@389
   742
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
alpar@389
   743
              reached.set(u, true);
alpar@389
   744
              _level->initAddItem(u);
alpar@389
   745
              nqueue.push_back(u);
alpar@389
   746
            }
alpar@389
   747
          }
alpar@389
   748
        }
alpar@389
   749
        queue.swap(nqueue);
alpar@389
   750
      }
alpar@389
   751
      _level->initFinish();
alpar@389
   752
alpar@389
   753
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@389
   754
        if (!reached[n]) {
alpar@389
   755
          _level->dirtyTopButOne(n);
alpar@389
   756
        } else if ((*_excess)[n] > 0 && _target != n) {
alpar@389
   757
          _level->activate(n);
alpar@389
   758
        }
alpar@389
   759
      }
alpar@389
   760
alpar@389
   761
      Node n;
alpar@389
   762
      while ((n = _level->highestActive()) != INVALID) {
alpar@389
   763
        Value excess = (*_excess)[n];
alpar@389
   764
        int level = _level->highestActiveLevel();
alpar@389
   765
        int new_level = _level->maxLevel();
alpar@389
   766
alpar@389
   767
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   768
          Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@389
   769
          if (!_tolerance.positive(rem)) continue;
alpar@389
   770
          Node v = _graph.target(e);
alpar@389
   771
          if ((*_level)[v] < level) {
alpar@389
   772
            if (!_level->active(v) && v != _source) {
alpar@389
   773
              _level->activate(v);
alpar@389
   774
            }
alpar@389
   775
            if (!_tolerance.less(rem, excess)) {
alpar@389
   776
              _flow->set(e, (*_flow)[e] + excess);
alpar@389
   777
              _excess->set(v, (*_excess)[v] + excess);
alpar@389
   778
              excess = 0;
alpar@389
   779
              goto no_more_push;
alpar@389
   780
            } else {
alpar@389
   781
              excess -= rem;
alpar@389
   782
              _excess->set(v, (*_excess)[v] + rem);
alpar@389
   783
              _flow->set(e, (*_capacity)[e]);
alpar@389
   784
            }
alpar@389
   785
          } else if (new_level > (*_level)[v]) {
alpar@389
   786
            new_level = (*_level)[v];
alpar@389
   787
          }
alpar@389
   788
        }
alpar@389
   789
alpar@389
   790
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@389
   791
          Value rem = (*_flow)[e];
alpar@389
   792
          if (!_tolerance.positive(rem)) continue;
alpar@389
   793
          Node v = _graph.source(e);
alpar@389
   794
          if ((*_level)[v] < level) {
alpar@389
   795
            if (!_level->active(v) && v != _source) {
alpar@389
   796
              _level->activate(v);
alpar@389
   797
            }
alpar@389
   798
            if (!_tolerance.less(rem, excess)) {
alpar@389
   799
              _flow->set(e, (*_flow)[e] - excess);
alpar@389
   800
              _excess->set(v, (*_excess)[v] + excess);
alpar@389
   801
              excess = 0;
alpar@389
   802
              goto no_more_push;
alpar@389
   803
            } else {
alpar@389
   804
              excess -= rem;
alpar@389
   805
              _excess->set(v, (*_excess)[v] + rem);
alpar@389
   806
              _flow->set(e, 0);
alpar@389
   807
            }
alpar@389
   808
          } else if (new_level > (*_level)[v]) {
alpar@389
   809
            new_level = (*_level)[v];
alpar@389
   810
          }
alpar@389
   811
        }
alpar@389
   812
alpar@389
   813
      no_more_push:
alpar@389
   814
alpar@389
   815
        _excess->set(n, excess);
alpar@389
   816
alpar@389
   817
        if (excess != 0) {
alpar@389
   818
          if (new_level + 1 < _level->maxLevel()) {
alpar@389
   819
            _level->liftHighestActive(new_level + 1);
alpar@389
   820
          } else {
alpar@389
   821
            // Calculation error
alpar@389
   822
            _level->liftHighestActiveToTop();
alpar@389
   823
          }
alpar@389
   824
          if (_level->emptyLevel(level)) {
alpar@389
   825
            // Calculation error
alpar@389
   826
            _level->liftToTop(level);
alpar@389
   827
          }
alpar@389
   828
        } else {
alpar@389
   829
          _level->deactivate(n);
alpar@389
   830
        }
alpar@389
   831
alpar@389
   832
      }
alpar@389
   833
    }
alpar@389
   834
alpar@389
   835
    /// \brief Runs the preflow algorithm.
alpar@389
   836
    ///
alpar@389
   837
    /// Runs the preflow algorithm.
alpar@389
   838
    /// \note pf.run() is just a shortcut of the following code.
alpar@389
   839
    /// \code
alpar@389
   840
    ///   pf.init();
alpar@389
   841
    ///   pf.startFirstPhase();
alpar@389
   842
    ///   pf.startSecondPhase();
alpar@389
   843
    /// \endcode
alpar@389
   844
    void run() {
alpar@389
   845
      init();
alpar@389
   846
      startFirstPhase();
alpar@389
   847
      startSecondPhase();
alpar@389
   848
    }
alpar@389
   849
alpar@389
   850
    /// \brief Runs the preflow algorithm to compute the minimum cut.
alpar@389
   851
    ///
alpar@389
   852
    /// Runs the preflow algorithm to compute the minimum cut.
alpar@389
   853
    /// \note pf.runMinCut() is just a shortcut of the following code.
alpar@389
   854
    /// \code
alpar@389
   855
    ///   pf.init();
alpar@389
   856
    ///   pf.startFirstPhase();
alpar@389
   857
    /// \endcode
alpar@389
   858
    void runMinCut() {
alpar@389
   859
      init();
alpar@389
   860
      startFirstPhase();
alpar@389
   861
    }
alpar@389
   862
alpar@389
   863
    /// @}
alpar@389
   864
alpar@389
   865
    /// \name Query Functions
alpar@389
   866
    /// The result of the %Preflow algorithm can be obtained using these
alpar@389
   867
    /// functions.\n
alpar@389
   868
    /// Before the use of these functions,
alpar@389
   869
    /// either run() or start() must be called.
alpar@389
   870
alpar@389
   871
    ///@{
alpar@389
   872
alpar@389
   873
    /// \brief Returns the value of the maximum flow.
alpar@389
   874
    ///
alpar@389
   875
    /// Returns the value of the maximum flow by returning the excess
alpar@389
   876
    /// of the target node \c t. This value equals to the value of
alpar@389
   877
    /// the maximum flow already after the first phase.
alpar@389
   878
    Value flowValue() const {
alpar@389
   879
      return (*_excess)[_target];
alpar@389
   880
    }
alpar@389
   881
alpar@389
   882
    /// \brief Returns true when the node is on the source side of minimum cut.
alpar@389
   883
    ///
alpar@389
   884
    /// Returns true when the node is on the source side of minimum
alpar@389
   885
    /// cut. This method can be called both after running \ref
alpar@389
   886
    /// startFirstPhase() and \ref startSecondPhase().
alpar@389
   887
    bool minCut(const Node& node) const {
alpar@389
   888
      return ((*_level)[node] == _level->maxLevel()) == _phase;
alpar@389
   889
    }
alpar@389
   890
alpar@389
   891
    /// \brief Returns a minimum value cut.
alpar@389
   892
    ///
alpar@389
   893
    /// Sets the \c cutMap to the characteristic vector of a minimum value
alpar@389
   894
    /// cut. This method can be called both after running \ref
alpar@389
   895
    /// startFirstPhase() and \ref startSecondPhase(). The result after second
alpar@389
   896
    /// phase could be changed slightly if inexact computation is used.
alpar@389
   897
    /// \pre The \c cutMap should be a bool-valued node-map.
alpar@389
   898
    template <typename CutMap>
alpar@389
   899
    void minCutMap(CutMap& cutMap) const {
alpar@389
   900
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@389
   901
        cutMap.set(n, minCut(n));
alpar@389
   902
      }
alpar@389
   903
    }
alpar@389
   904
alpar@389
   905
    /// \brief Returns the flow on the arc.
alpar@389
   906
    ///
alpar@389
   907
    /// Sets the \c flowMap to the flow on the arcs. This method can
alpar@389
   908
    /// be called after the second phase of algorithm.
alpar@389
   909
    Value flow(const Arc& arc) const {
alpar@389
   910
      return (*_flow)[arc];
alpar@389
   911
    }
alpar@389
   912
alpar@389
   913
    /// @}
alpar@389
   914
  };
alpar@389
   915
}
alpar@389
   916
alpar@389
   917
#endif