lemon/capacity_scaling.h
author kpeter
Thu, 13 Sep 2007 22:05:32 +0000
changeset 2470 46818ce27a60
parent 2440 c9218405595b
child 2471 ed70b226cc48
permissions -rw-r--r--
Small bug fixes.
deba@2440
     1
/* -*- C++ -*-
deba@2440
     2
 *
deba@2440
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2440
     4
 *
deba@2440
     5
 * Copyright (C) 2003-2007
deba@2440
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2440
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2440
     8
 *
deba@2440
     9
 * Permission to use, modify and distribute this software is granted
deba@2440
    10
 * provided that this copyright notice appears in all copies. For
deba@2440
    11
 * precise terms see the accompanying LICENSE file.
deba@2440
    12
 *
deba@2440
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2440
    14
 * express or implied, and with no claim as to its suitability for any
deba@2440
    15
 * purpose.
deba@2440
    16
 *
deba@2440
    17
 */
deba@2440
    18
deba@2440
    19
#ifndef LEMON_CAPACITY_SCALING_H
deba@2440
    20
#define LEMON_CAPACITY_SCALING_H
deba@2440
    21
deba@2440
    22
/// \ingroup min_cost_flow
deba@2440
    23
///
deba@2440
    24
/// \file
deba@2440
    25
/// \brief The capacity scaling algorithm for finding a minimum cost
deba@2440
    26
/// flow.
deba@2440
    27
deba@2440
    28
#include <vector>
deba@2440
    29
#include <lemon/dijkstra.h>
deba@2440
    30
#include <lemon/graph_adaptor.h>
deba@2440
    31
deba@2440
    32
#define WITH_SCALING
deba@2440
    33
deba@2457
    34
//#define _DEBUG_ITER_
deba@2457
    35
deba@2440
    36
namespace lemon {
deba@2440
    37
deba@2440
    38
  /// \addtogroup min_cost_flow
deba@2440
    39
  /// @{
deba@2440
    40
deba@2440
    41
  /// \brief Implementation of the capacity scaling version of the
deba@2457
    42
  /// successive shortest path algorithm for finding a minimum cost flow.
deba@2440
    43
  ///
deba@2440
    44
  /// \ref lemon::CapacityScaling "CapacityScaling" implements a
deba@2440
    45
  /// capacity scaling algorithm for finding a minimum cost flow.
deba@2440
    46
  ///
deba@2440
    47
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    48
  /// \param LowerMap The type of the lower bound map.
deba@2440
    49
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    50
  /// \param CostMap The type of the cost (length) map.
deba@2440
    51
  /// \param SupplyMap The type of the supply map.
deba@2440
    52
  ///
deba@2440
    53
  /// \warning
deba@2440
    54
  /// - Edge capacities and costs should be nonnegative integers.
deba@2440
    55
  ///	However \c CostMap::Value should be signed type.
deba@2440
    56
  /// - Supply values should be integers.
deba@2440
    57
  /// - \c LowerMap::Value must be convertible to
deba@2440
    58
  ///	\c CapacityMap::Value and \c CapacityMap::Value must be
deba@2440
    59
  ///	convertible to \c SupplyMap::Value.
deba@2440
    60
  ///
deba@2440
    61
  /// \author Peter Kovacs
deba@2440
    62
deba@2440
    63
template < typename Graph,
deba@2440
    64
	   typename LowerMap = typename Graph::template EdgeMap<int>,
deba@2440
    65
	   typename CapacityMap = LowerMap,
deba@2440
    66
	   typename CostMap = typename Graph::template EdgeMap<int>,
deba@2440
    67
	   typename SupplyMap = typename Graph::template NodeMap
deba@2440
    68
				<typename CapacityMap::Value> >
deba@2440
    69
  class CapacityScaling
deba@2440
    70
  {
deba@2440
    71
    typedef typename Graph::Node Node;
deba@2440
    72
    typedef typename Graph::NodeIt NodeIt;
deba@2440
    73
    typedef typename Graph::Edge Edge;
deba@2440
    74
    typedef typename Graph::EdgeIt EdgeIt;
deba@2440
    75
    typedef typename Graph::InEdgeIt InEdgeIt;
deba@2440
    76
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2440
    77
deba@2440
    78
    typedef typename LowerMap::Value Lower;
deba@2440
    79
    typedef typename CapacityMap::Value Capacity;
deba@2440
    80
    typedef typename CostMap::Value Cost;
deba@2440
    81
    typedef typename SupplyMap::Value Supply;
deba@2440
    82
    typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
deba@2440
    83
    typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
deba@2440
    84
deba@2440
    85
    typedef ResGraphAdaptor< const Graph, Capacity,
deba@2440
    86
			     CapacityRefMap, CapacityRefMap > ResGraph;
deba@2440
    87
    typedef typename ResGraph::Node ResNode;
deba@2440
    88
    typedef typename ResGraph::NodeIt ResNodeIt;
deba@2440
    89
    typedef typename ResGraph::Edge ResEdge;
deba@2440
    90
    typedef typename ResGraph::EdgeIt ResEdgeIt;
deba@2440
    91
deba@2440
    92
  public:
deba@2440
    93
deba@2440
    94
    /// \brief The type of the flow map.
deba@2440
    95
    typedef CapacityRefMap FlowMap;
deba@2440
    96
    /// \brief The type of the potential map.
deba@2440
    97
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
    98
deba@2440
    99
  protected:
deba@2440
   100
deba@2440
   101
    /// \brief Map adaptor class for handling reduced edge costs.
deba@2440
   102
    class ReducedCostMap : public MapBase<ResEdge, Cost>
deba@2440
   103
    {
deba@2440
   104
    private:
deba@2440
   105
deba@2440
   106
      const ResGraph &gr;
deba@2440
   107
      const CostMap &cost_map;
deba@2440
   108
      const PotentialMap &pot_map;
deba@2440
   109
deba@2440
   110
    public:
deba@2440
   111
deba@2440
   112
      typedef typename MapBase<ResEdge, Cost>::Value Value;
deba@2440
   113
      typedef typename MapBase<ResEdge, Cost>::Key Key;
deba@2440
   114
deba@2440
   115
      ReducedCostMap( const ResGraph &_gr,
deba@2440
   116
		      const CostMap &_cost,
deba@2440
   117
		      const PotentialMap &_pot ) :
deba@2440
   118
	gr(_gr), cost_map(_cost), pot_map(_pot) {}
deba@2440
   119
deba@2440
   120
      Value operator[](const Key &e) const {
deba@2440
   121
	return ResGraph::forward(e) ?
deba@2440
   122
	   cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)] :
deba@2440
   123
	  -cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
deba@2440
   124
      }
deba@2440
   125
deba@2440
   126
    }; //class ReducedCostMap
deba@2440
   127
deba@2440
   128
    /// \brief Map adaptor for \ref lemon::Dijkstra "Dijkstra" class to
deba@2440
   129
    /// update node potentials.
deba@2440
   130
    class PotentialUpdateMap : public MapBase<ResNode, Cost>
deba@2440
   131
    {
deba@2440
   132
    private:
deba@2440
   133
deba@2440
   134
      PotentialMap *pot;
deba@2440
   135
      typedef std::pair<ResNode, Cost> Pair;
deba@2440
   136
      std::vector<Pair> data;
deba@2440
   137
deba@2440
   138
    public:
deba@2440
   139
deba@2440
   140
      typedef typename MapBase<ResNode, Cost>::Value Value;
deba@2440
   141
      typedef typename MapBase<ResNode, Cost>::Key Key;
deba@2440
   142
deba@2440
   143
      void potentialMap(PotentialMap &_pot) {
deba@2440
   144
	pot = &_pot;
deba@2440
   145
      }
deba@2440
   146
deba@2440
   147
      void init() {
deba@2440
   148
	data.clear();
deba@2440
   149
      }
deba@2440
   150
deba@2440
   151
      void set(const Key &n, const Value &v) {
deba@2440
   152
	data.push_back(Pair(n, v));
deba@2440
   153
      }
deba@2440
   154
deba@2440
   155
      void update() {
deba@2440
   156
	Cost val = data[data.size()-1].second;
deba@2440
   157
	for (int i = 0; i < data.size()-1; ++i)
deba@2440
   158
	  (*pot)[data[i].first] += val - data[i].second;
deba@2440
   159
      }
deba@2440
   160
deba@2440
   161
    }; //class PotentialUpdateMap
deba@2440
   162
deba@2440
   163
#ifdef WITH_SCALING
deba@2440
   164
    /// \brief Map adaptor class for identifing deficit nodes.
deba@2440
   165
    class DeficitBoolMap : public MapBase<ResNode, bool>
deba@2440
   166
    {
deba@2440
   167
    private:
deba@2440
   168
deba@2440
   169
      const SupplyRefMap &imb;
deba@2440
   170
      const Capacity &delta;
deba@2440
   171
deba@2440
   172
    public:
deba@2440
   173
deba@2440
   174
      DeficitBoolMap(const SupplyRefMap &_imb, const Capacity &_delta) :
deba@2440
   175
	imb(_imb), delta(_delta) {}
deba@2440
   176
deba@2440
   177
      bool operator[](const ResNode &n) const {
deba@2440
   178
	return imb[n] <= -delta;
deba@2440
   179
      }
deba@2440
   180
deba@2440
   181
    }; //class DeficitBoolMap
deba@2440
   182
deba@2440
   183
    /// \brief Map adaptor class for filtering edges with at least
deba@2440
   184
    /// \c delta residual capacity
deba@2440
   185
    class DeltaFilterMap : public MapBase<ResEdge, bool>
deba@2440
   186
    {
deba@2440
   187
    private:
deba@2440
   188
deba@2440
   189
      const ResGraph &gr;
deba@2440
   190
      const Capacity &delta;
deba@2440
   191
deba@2440
   192
    public:
deba@2440
   193
deba@2440
   194
      typedef typename MapBase<ResEdge, Cost>::Value Value;
deba@2440
   195
      typedef typename MapBase<ResEdge, Cost>::Key Key;
deba@2440
   196
deba@2440
   197
      DeltaFilterMap(const ResGraph &_gr, const Capacity &_delta) :
deba@2440
   198
	gr(_gr), delta(_delta) {}
deba@2440
   199
deba@2440
   200
      Value operator[](const Key &e) const {
deba@2440
   201
	return gr.rescap(e) >= delta;
deba@2440
   202
      }
deba@2440
   203
deba@2440
   204
    }; //class DeltaFilterMap
deba@2440
   205
deba@2440
   206
    typedef EdgeSubGraphAdaptor<const ResGraph, const DeltaFilterMap>
deba@2440
   207
      DeltaResGraph;
deba@2440
   208
deba@2440
   209
    /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
deba@2440
   210
    class ResDijkstraTraits :
deba@2440
   211
      public DijkstraDefaultTraits<DeltaResGraph, ReducedCostMap>
deba@2440
   212
    {
deba@2440
   213
    public:
deba@2440
   214
deba@2440
   215
      typedef PotentialUpdateMap DistMap;
deba@2440
   216
deba@2440
   217
      static DistMap *createDistMap(const DeltaResGraph&) {
deba@2440
   218
	return new DistMap();
deba@2440
   219
      }
deba@2440
   220
deba@2440
   221
    }; //class ResDijkstraTraits
deba@2440
   222
deba@2440
   223
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   224
    /// \brief Map adaptor class for identifing deficit nodes.
deba@2440
   225
    class DeficitBoolMap : public MapBase<ResNode, bool>
deba@2440
   226
    {
deba@2440
   227
    private:
deba@2440
   228
deba@2440
   229
      const SupplyRefMap &imb;
deba@2440
   230
deba@2440
   231
    public:
deba@2440
   232
deba@2440
   233
      DeficitBoolMap(const SupplyRefMap &_imb) : imb(_imb) {}
deba@2440
   234
deba@2440
   235
      bool operator[](const ResNode &n) const {
deba@2440
   236
	return imb[n] < 0;
deba@2440
   237
      }
deba@2440
   238
deba@2440
   239
    }; //class DeficitBoolMap
deba@2440
   240
deba@2440
   241
    /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
deba@2440
   242
    class ResDijkstraTraits :
deba@2440
   243
      public DijkstraDefaultTraits<ResGraph, ReducedCostMap>
deba@2440
   244
    {
deba@2440
   245
    public:
deba@2440
   246
deba@2440
   247
      typedef PotentialUpdateMap DistMap;
deba@2440
   248
deba@2440
   249
      static DistMap *createDistMap(const ResGraph&) {
deba@2440
   250
	return new DistMap();
deba@2440
   251
      }
deba@2440
   252
deba@2440
   253
    }; //class ResDijkstraTraits
deba@2440
   254
#endif
deba@2440
   255
deba@2440
   256
  protected:
deba@2440
   257
deba@2440
   258
    /// \brief The directed graph the algorithm runs on.
deba@2440
   259
    const Graph &graph;
deba@2440
   260
    /// \brief The original lower bound map.
deba@2440
   261
    const LowerMap *lower;
deba@2440
   262
    /// \brief The modified capacity map.
deba@2440
   263
    CapacityRefMap capacity;
deba@2440
   264
    /// \brief The cost map.
deba@2440
   265
    const CostMap &cost;
deba@2440
   266
    /// \brief The modified supply map.
deba@2440
   267
    SupplyRefMap supply;
deba@2440
   268
    /// \brief The sum of supply values equals zero.
deba@2440
   269
    bool valid_supply;
deba@2440
   270
deba@2440
   271
    /// \brief The edge map of the current flow.
deba@2440
   272
    FlowMap flow;
deba@2440
   273
    /// \brief The potential node map.
deba@2440
   274
    PotentialMap potential;
deba@2440
   275
    /// \brief The residual graph.
deba@2440
   276
    ResGraph res_graph;
deba@2440
   277
    /// \brief The reduced cost map.
deba@2440
   278
    ReducedCostMap red_cost;
deba@2440
   279
deba@2440
   280
    /// \brief The imbalance map.
deba@2440
   281
    SupplyRefMap imbalance;
deba@2440
   282
    /// \brief The excess nodes.
deba@2440
   283
    std::vector<ResNode> excess_nodes;
deba@2440
   284
    /// \brief The index of the next excess node.
deba@2440
   285
    int next_node;
deba@2440
   286
deba@2440
   287
#ifdef WITH_SCALING
deba@2440
   288
    typedef Dijkstra<DeltaResGraph, ReducedCostMap, ResDijkstraTraits>
deba@2440
   289
      ResDijkstra;
deba@2440
   290
    /// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
deba@2440
   291
    /// shortest paths in the residual graph with respect to the
deba@2440
   292
    /// reduced edge costs.
deba@2440
   293
    ResDijkstra dijkstra;
deba@2440
   294
deba@2440
   295
    /// \brief The delta parameter used for capacity scaling.
deba@2440
   296
    Capacity delta;
deba@2440
   297
    /// \brief Edge filter map.
deba@2440
   298
    DeltaFilterMap delta_filter;
deba@2440
   299
    /// \brief The delta residual graph.
deba@2440
   300
    DeltaResGraph dres_graph;
deba@2440
   301
    /// \brief Map for identifing deficit nodes.
deba@2440
   302
    DeficitBoolMap delta_deficit;
deba@2440
   303
deba@2457
   304
    /// \brief The deficit nodes.
deba@2457
   305
    std::vector<ResNode> deficit_nodes;
deba@2457
   306
deba@2440
   307
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   308
    typedef Dijkstra<ResGraph, ReducedCostMap, ResDijkstraTraits>
deba@2440
   309
      ResDijkstra;
deba@2440
   310
    /// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
deba@2440
   311
    /// shortest paths in the residual graph with respect to the
deba@2440
   312
    /// reduced edge costs.
deba@2440
   313
    ResDijkstra dijkstra;
deba@2440
   314
    /// \brief Map for identifing deficit nodes.
deba@2440
   315
    DeficitBoolMap has_deficit;
deba@2440
   316
#endif
deba@2440
   317
deba@2440
   318
    /// \brief Pred map for the \ref lemon::Dijkstra "Dijkstra" class.
deba@2440
   319
    typename ResDijkstra::PredMap pred;
deba@2440
   320
    /// \brief Dist map for the \ref lemon::Dijkstra "Dijkstra" class to
deba@2440
   321
    /// update node potentials.
deba@2440
   322
    PotentialUpdateMap updater;
deba@2440
   323
deba@2440
   324
  public :
deba@2440
   325
deba@2440
   326
    /// \brief General constructor of the class (with lower bounds).
deba@2440
   327
    ///
deba@2440
   328
    /// General constructor of the class (with lower bounds).
deba@2440
   329
    ///
deba@2440
   330
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   331
    /// \param _lower The lower bounds of the edges.
deba@2440
   332
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   333
    /// \param _cost The cost (length) values of the edges.
deba@2440
   334
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   335
    CapacityScaling( const Graph &_graph,
deba@2440
   336
		     const LowerMap &_lower,
deba@2440
   337
		     const CapacityMap &_capacity,
deba@2440
   338
		     const CostMap &_cost,
deba@2440
   339
		     const SupplyMap &_supply ) :
deba@2440
   340
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   341
      supply(_graph), flow(_graph, 0), potential(_graph, 0),
deba@2440
   342
      res_graph(_graph, capacity, flow),
deba@2440
   343
      red_cost(res_graph, cost, potential), imbalance(_graph),
deba@2440
   344
#ifdef WITH_SCALING
deba@2440
   345
      delta(0), delta_filter(res_graph, delta),
deba@2440
   346
      dres_graph(res_graph, delta_filter),
deba@2440
   347
      dijkstra(dres_graph, red_cost), pred(dres_graph),
deba@2440
   348
      delta_deficit(imbalance, delta)
deba@2440
   349
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   350
      dijkstra(res_graph, red_cost), pred(res_graph),
deba@2440
   351
      has_deficit(imbalance)
deba@2440
   352
#endif
deba@2440
   353
    {
deba@2440
   354
      // Removing nonzero lower bounds
deba@2440
   355
      capacity = subMap(_capacity, _lower);
deba@2440
   356
      Supply sum = 0;
deba@2440
   357
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2440
   358
	Supply s = _supply[n];
deba@2440
   359
	for (InEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   360
	  s += _lower[e];
deba@2440
   361
	for (OutEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   362
	  s -= _lower[e];
deba@2440
   363
	supply[n] = imbalance[n] = s;
deba@2440
   364
	sum += s;
deba@2440
   365
      }
deba@2440
   366
      valid_supply = sum == 0;
deba@2440
   367
    }
deba@2440
   368
deba@2440
   369
    /// \brief General constructor of the class (without lower bounds).
deba@2440
   370
    ///
deba@2440
   371
    /// General constructor of the class (without lower bounds).
deba@2440
   372
    ///
deba@2440
   373
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   374
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   375
    /// \param _cost The cost (length) values of the edges.
deba@2440
   376
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   377
    CapacityScaling( const Graph &_graph,
deba@2440
   378
		     const CapacityMap &_capacity,
deba@2440
   379
		     const CostMap &_cost,
deba@2440
   380
		     const SupplyMap &_supply ) :
deba@2440
   381
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   382
      supply(_supply), flow(_graph, 0), potential(_graph, 0),
deba@2440
   383
      res_graph(_graph, capacity, flow),
deba@2440
   384
      red_cost(res_graph, cost, potential), imbalance(_graph),
deba@2440
   385
#ifdef WITH_SCALING
deba@2440
   386
      delta(0), delta_filter(res_graph, delta),
deba@2440
   387
      dres_graph(res_graph, delta_filter),
deba@2440
   388
      dijkstra(dres_graph, red_cost), pred(dres_graph),
deba@2440
   389
      delta_deficit(imbalance, delta)
deba@2440
   390
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   391
      dijkstra(res_graph, red_cost), pred(res_graph),
deba@2440
   392
      has_deficit(imbalance)
deba@2440
   393
#endif
deba@2440
   394
    {
deba@2440
   395
      // Checking the sum of supply values
deba@2440
   396
      Supply sum = 0;
deba@2440
   397
      for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
deba@2440
   398
      valid_supply = sum == 0;
deba@2440
   399
    }
deba@2440
   400
deba@2440
   401
    /// \brief Simple constructor of the class (with lower bounds).
deba@2440
   402
    ///
deba@2440
   403
    /// Simple constructor of the class (with lower bounds).
deba@2440
   404
    ///
deba@2440
   405
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   406
    /// \param _lower The lower bounds of the edges.
deba@2440
   407
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   408
    /// \param _cost The cost (length) values of the edges.
deba@2440
   409
    /// \param _s The source node.
deba@2440
   410
    /// \param _t The target node.
deba@2440
   411
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   412
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   413
    /// \c _t).
deba@2440
   414
    CapacityScaling( const Graph &_graph,
deba@2440
   415
		     const LowerMap &_lower,
deba@2440
   416
		     const CapacityMap &_capacity,
deba@2440
   417
		     const CostMap &_cost,
deba@2440
   418
		     Node _s, Node _t,
deba@2440
   419
		     Supply _flow_value ) :
deba@2440
   420
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   421
      supply(_graph), flow(_graph, 0), potential(_graph, 0),
deba@2440
   422
      res_graph(_graph, capacity, flow),
deba@2440
   423
      red_cost(res_graph, cost, potential), imbalance(_graph),
deba@2440
   424
#ifdef WITH_SCALING
deba@2440
   425
      delta(0), delta_filter(res_graph, delta),
deba@2440
   426
      dres_graph(res_graph, delta_filter),
deba@2440
   427
      dijkstra(dres_graph, red_cost), pred(dres_graph),
deba@2440
   428
      delta_deficit(imbalance, delta)
deba@2440
   429
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   430
      dijkstra(res_graph, red_cost), pred(res_graph),
deba@2440
   431
      has_deficit(imbalance)
deba@2440
   432
#endif
deba@2440
   433
    {
deba@2440
   434
      // Removing nonzero lower bounds
deba@2440
   435
      capacity = subMap(_capacity, _lower);
deba@2440
   436
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2440
   437
	Supply s = 0;
deba@2440
   438
	if (n == _s) s =  _flow_value;
deba@2440
   439
	if (n == _t) s = -_flow_value;
deba@2440
   440
	for (InEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   441
	  s += _lower[e];
deba@2440
   442
	for (OutEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   443
	  s -= _lower[e];
deba@2440
   444
	supply[n] = imbalance[n] = s;
deba@2440
   445
      }
deba@2440
   446
      valid_supply = true;
deba@2440
   447
    }
deba@2440
   448
deba@2440
   449
    /// \brief Simple constructor of the class (without lower bounds).
deba@2440
   450
    ///
deba@2440
   451
    /// Simple constructor of the class (without lower bounds).
deba@2440
   452
    ///
deba@2440
   453
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   454
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   455
    /// \param _cost The cost (length) values of the edges.
deba@2440
   456
    /// \param _s The source node.
deba@2440
   457
    /// \param _t The target node.
deba@2440
   458
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   459
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   460
    /// \c _t).
deba@2440
   461
    CapacityScaling( const Graph &_graph,
deba@2440
   462
		     const CapacityMap &_capacity,
deba@2440
   463
		     const CostMap &_cost,
deba@2440
   464
		     Node _s, Node _t,
deba@2440
   465
		     Supply _flow_value ) :
deba@2440
   466
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   467
      supply(_graph, 0), flow(_graph, 0), potential(_graph, 0),
deba@2440
   468
      res_graph(_graph, capacity, flow),
deba@2440
   469
      red_cost(res_graph, cost, potential), imbalance(_graph),
deba@2440
   470
#ifdef WITH_SCALING
deba@2440
   471
      delta(0), delta_filter(res_graph, delta),
deba@2440
   472
      dres_graph(res_graph, delta_filter),
deba@2440
   473
      dijkstra(dres_graph, red_cost), pred(dres_graph),
deba@2440
   474
      delta_deficit(imbalance, delta)
deba@2440
   475
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   476
      dijkstra(res_graph, red_cost), pred(res_graph),
deba@2440
   477
      has_deficit(imbalance)
deba@2440
   478
#endif
deba@2440
   479
    {
deba@2440
   480
      supply[_s] =  _flow_value;
deba@2440
   481
      supply[_t] = -_flow_value;
deba@2440
   482
      valid_supply = true;
deba@2440
   483
    }
deba@2440
   484
deba@2440
   485
    /// \brief Returns a const reference to the flow map.
deba@2440
   486
    ///
deba@2440
   487
    /// Returns a const reference to the flow map.
deba@2440
   488
    ///
deba@2440
   489
    /// \pre \ref run() must be called before using this function.
deba@2440
   490
    const FlowMap& flowMap() const {
deba@2440
   491
      return flow;
deba@2440
   492
    }
deba@2440
   493
deba@2440
   494
    /// \brief Returns a const reference to the potential map (the dual
deba@2440
   495
    /// solution).
deba@2440
   496
    ///
deba@2440
   497
    /// Returns a const reference to the potential map (the dual
deba@2440
   498
    /// solution).
deba@2440
   499
    ///
deba@2440
   500
    /// \pre \ref run() must be called before using this function.
deba@2440
   501
    const PotentialMap& potentialMap() const {
deba@2440
   502
      return potential;
deba@2440
   503
    }
deba@2440
   504
deba@2440
   505
    /// \brief Returns the total cost of the found flow.
deba@2440
   506
    ///
deba@2440
   507
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   508
    /// function is \f$ O(e) \f$.
deba@2440
   509
    ///
deba@2440
   510
    /// \pre \ref run() must be called before using this function.
deba@2440
   511
    Cost totalCost() const {
deba@2440
   512
      Cost c = 0;
deba@2440
   513
      for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   514
	c += flow[e] * cost[e];
deba@2440
   515
      return c;
deba@2440
   516
    }
deba@2440
   517
deba@2457
   518
    /// \brief Runs the algorithm.
deba@2440
   519
    ///
deba@2457
   520
    /// Runs the algorithm.
deba@2440
   521
    ///
deba@2440
   522
    /// \return \c true if a feasible flow can be found.
deba@2440
   523
    bool run() {
deba@2440
   524
      return init() && start();
deba@2440
   525
    }
deba@2440
   526
deba@2440
   527
  protected:
deba@2440
   528
deba@2440
   529
    /// \brief Initializes the algorithm.
deba@2440
   530
    bool init() {
deba@2440
   531
      if (!valid_supply) return false;
deba@2440
   532
deba@2440
   533
      // Initalizing Dijkstra class
deba@2440
   534
      updater.potentialMap(potential);
deba@2440
   535
      dijkstra.distMap(updater).predMap(pred);
deba@2440
   536
deba@2440
   537
#ifdef WITH_SCALING
deba@2440
   538
      // Initilaizing delta value
deba@2457
   539
      Supply max_sup = 0, max_dem = 0;
deba@2457
   540
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2457
   541
	if (supply[n] >  max_sup) max_sup =  supply[n];
deba@2457
   542
	if (supply[n] < -max_dem) max_dem = -supply[n];
deba@2440
   543
      }
deba@2457
   544
      if (max_dem < max_sup) max_sup = max_dem;
deba@2457
   545
      for (delta = 1; 2 * delta < max_sup; delta *= 2) ;
deba@2440
   546
#endif
deba@2440
   547
      return true;
deba@2440
   548
    }
deba@2440
   549
deba@2440
   550
#ifdef WITH_SCALING
deba@2440
   551
    /// \brief Executes the capacity scaling version of the successive
deba@2440
   552
    /// shortest path algorithm.
deba@2440
   553
    bool start() {
deba@2440
   554
      typedef typename DeltaResGraph::EdgeIt DeltaResEdgeIt;
deba@2440
   555
      typedef typename DeltaResGraph::Edge DeltaResEdge;
deba@2457
   556
#ifdef _DEBUG_ITER_
deba@2457
   557
      int dijk_num = 0;
deba@2457
   558
#endif
deba@2440
   559
deba@2440
   560
      // Processing capacity scaling phases
deba@2440
   561
      ResNode s, t;
deba@2440
   562
      for ( ; delta >= 1; delta = delta < 4 && delta > 1 ?
deba@2440
   563
				  1 : delta / 4 )
deba@2440
   564
      {
deba@2440
   565
	// Saturating edges not satisfying the optimality condition
deba@2440
   566
	Capacity r;
deba@2440
   567
	for (DeltaResEdgeIt e(dres_graph); e != INVALID; ++e) {
deba@2440
   568
	  if (red_cost[e] < 0) {
deba@2440
   569
	    res_graph.augment(e, r = res_graph.rescap(e));
deba@2440
   570
	    imbalance[dres_graph.target(e)] += r;
deba@2440
   571
	    imbalance[dres_graph.source(e)] -= r;
deba@2440
   572
	  }
deba@2440
   573
	}
deba@2440
   574
deba@2440
   575
	// Finding excess nodes
deba@2440
   576
	excess_nodes.clear();
deba@2457
   577
	deficit_nodes.clear();
deba@2440
   578
	for (ResNodeIt n(res_graph); n != INVALID; ++n) {
deba@2457
   579
	  if (imbalance[n] >=  delta) excess_nodes.push_back(n);
deba@2457
   580
	  if (imbalance[n] <= -delta) deficit_nodes.push_back(n);
deba@2440
   581
	}
deba@2440
   582
	next_node = 0;
deba@2440
   583
deba@2457
   584
	// Finding shortest paths
deba@2440
   585
	while (next_node < excess_nodes.size()) {
deba@2457
   586
	  // Checking deficit nodes
deba@2457
   587
	  if (delta > 1) {
deba@2457
   588
	    bool delta_def = false;
deba@2457
   589
	    for (int i = 0; i < deficit_nodes.size(); ++i) {
deba@2457
   590
	      if (imbalance[deficit_nodes[i]] <= -delta) {
deba@2457
   591
		delta_def = true;
deba@2457
   592
		break;
deba@2457
   593
	      }
deba@2457
   594
	    }
deba@2457
   595
	    if (!delta_def) break;
deba@2457
   596
	  }
deba@2457
   597
deba@2440
   598
	  // Running Dijkstra
deba@2440
   599
	  s = excess_nodes[next_node];
deba@2440
   600
	  updater.init();
deba@2440
   601
	  dijkstra.init();
deba@2440
   602
	  dijkstra.addSource(s);
deba@2457
   603
#ifdef _DEBUG_ITER_
deba@2457
   604
	  ++dijk_num;
deba@2457
   605
#endif
deba@2440
   606
	  if ((t = dijkstra.start(delta_deficit)) == INVALID) {
deba@2440
   607
	    if (delta > 1) {
deba@2440
   608
	      ++next_node;
deba@2440
   609
	      continue;
deba@2440
   610
	    }
deba@2440
   611
	    return false;
deba@2440
   612
	  }
deba@2440
   613
deba@2440
   614
	  // Updating node potentials
deba@2440
   615
	  updater.update();
deba@2440
   616
deba@2440
   617
	  // Augment along a shortest path from s to t
deba@2440
   618
	  Capacity d = imbalance[s] < -imbalance[t] ?
deba@2440
   619
	    imbalance[s] : -imbalance[t];
deba@2440
   620
	  ResNode u = t;
deba@2440
   621
	  ResEdge e;
deba@2440
   622
	  if (d > delta) {
deba@2440
   623
	    while ((e = pred[u]) != INVALID) {
deba@2440
   624
	      if (res_graph.rescap(e) < d) d = res_graph.rescap(e);
deba@2440
   625
	      u = dres_graph.source(e);
deba@2440
   626
	    }
deba@2440
   627
	  }
deba@2440
   628
	  u = t;
deba@2440
   629
	  while ((e = pred[u]) != INVALID) {
deba@2440
   630
	    res_graph.augment(e, d);
deba@2440
   631
	    u = dres_graph.source(e);
deba@2440
   632
	  }
deba@2440
   633
	  imbalance[s] -= d;
deba@2440
   634
	  imbalance[t] += d;
deba@2440
   635
	  if (imbalance[s] < delta) ++next_node;
deba@2440
   636
	}
deba@2440
   637
      }
deba@2457
   638
#ifdef _DEBUG_ITER_
deba@2457
   639
      std::cout << "Cost Scaling algorithm finished with running Dijkstra algorithm "
deba@2457
   640
	<< dijk_num << " times." << std::endl;
deba@2457
   641
#endif
deba@2440
   642
deba@2440
   643
      // Handling nonzero lower bounds
deba@2440
   644
      if (lower) {
deba@2440
   645
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   646
	  flow[e] += (*lower)[e];
deba@2440
   647
      }
deba@2440
   648
      return true;
deba@2440
   649
    }
deba@2440
   650
deba@2440
   651
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   652
    /// \brief Executes the successive shortest path algorithm without
deba@2440
   653
    /// capacity scaling.
deba@2440
   654
    bool start() {
deba@2440
   655
      // Finding excess nodes
deba@2440
   656
      for (ResNodeIt n(res_graph); n != INVALID; ++n) {
deba@2440
   657
	if (imbalance[n] > 0) excess_nodes.push_back(n);
deba@2440
   658
      }
deba@2440
   659
      if (excess_nodes.size() == 0) return true;
deba@2440
   660
      next_node = 0;
deba@2440
   661
deba@2457
   662
      // Finding shortest paths
deba@2440
   663
      ResNode s, t;
deba@2440
   664
      while ( imbalance[excess_nodes[next_node]] > 0 ||
deba@2440
   665
	      ++next_node < excess_nodes.size() )
deba@2440
   666
      {
deba@2440
   667
	// Running Dijkstra
deba@2440
   668
	s = excess_nodes[next_node];
deba@2440
   669
	updater.init();
deba@2440
   670
	dijkstra.init();
deba@2440
   671
	dijkstra.addSource(s);
deba@2440
   672
	if ((t = dijkstra.start(has_deficit)) == INVALID)
deba@2440
   673
	  return false;
deba@2440
   674
deba@2440
   675
	// Updating node potentials
deba@2440
   676
	updater.update();
deba@2440
   677
deba@2440
   678
	// Augmenting along a shortest path from s to t
deba@2440
   679
	Capacity delta = imbalance[s] < -imbalance[t] ?
deba@2440
   680
	  imbalance[s] : -imbalance[t];
deba@2440
   681
	ResNode u = t;
deba@2440
   682
	ResEdge e;
deba@2440
   683
	while ((e = pred[u]) != INVALID) {
deba@2440
   684
	  if (res_graph.rescap(e) < delta) delta = res_graph.rescap(e);
deba@2440
   685
	  u = res_graph.source(e);
deba@2440
   686
	}
deba@2440
   687
	u = t;
deba@2440
   688
	while ((e = pred[u]) != INVALID) {
deba@2440
   689
	  res_graph.augment(e, delta);
deba@2440
   690
	  u = res_graph.source(e);
deba@2440
   691
	}
deba@2440
   692
	imbalance[s] -= delta;
deba@2440
   693
	imbalance[t] += delta;
deba@2440
   694
      }
deba@2440
   695
deba@2440
   696
      // Handling nonzero lower bounds
deba@2440
   697
      if (lower) {
deba@2440
   698
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   699
	  flow[e] += (*lower)[e];
deba@2440
   700
      }
deba@2440
   701
      return true;
deba@2440
   702
    }
deba@2440
   703
#endif
deba@2440
   704
deba@2440
   705
  }; //class CapacityScaling
deba@2440
   706
deba@2440
   707
  ///@}
deba@2440
   708
deba@2440
   709
} //namespace lemon
deba@2440
   710
deba@2440
   711
#endif //LEMON_CAPACITY_SCALING_H