lemon/capacity_scaling.h
author ladanyi
Tue, 27 Nov 2007 16:26:58 +0000
changeset 2523 ceb7f3c704b7
parent 2507 6520edb2c3f3
child 2533 aea952a1af99
permissions -rw-r--r--
Fix warning when compiling with -Wall.
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>
kpeter@2509
    29
#include <lemon/graph_adaptor.h>
deba@2440
    30
#include <lemon/dijkstra.h>
deba@2440
    31
deba@2440
    32
#define WITH_SCALING
deba@2440
    33
kpeter@2471
    34
#ifdef WITH_SCALING
kpeter@2471
    35
#define SCALING_FACTOR	  2
kpeter@2471
    36
#endif
kpeter@2471
    37
deba@2457
    38
//#define _DEBUG_ITER_
deba@2457
    39
deba@2440
    40
namespace lemon {
deba@2440
    41
deba@2440
    42
  /// \addtogroup min_cost_flow
deba@2440
    43
  /// @{
deba@2440
    44
deba@2440
    45
  /// \brief Implementation of the capacity scaling version of the
kpeter@2509
    46
  /// successive shortest path algorithm for finding a minimum cost
kpeter@2509
    47
  /// flow.
deba@2440
    48
  ///
kpeter@2509
    49
  /// \ref lemon::CapacityScaling "CapacityScaling" implements the
kpeter@2509
    50
  /// capacity scaling version of the successive shortest path
kpeter@2509
    51
  /// algorithm for finding a minimum cost flow.
deba@2440
    52
  ///
deba@2440
    53
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    54
  /// \param LowerMap The type of the lower bound map.
deba@2440
    55
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    56
  /// \param CostMap The type of the cost (length) map.
deba@2440
    57
  /// \param SupplyMap The type of the supply map.
deba@2440
    58
  ///
deba@2440
    59
  /// \warning
deba@2440
    60
  /// - Edge capacities and costs should be nonnegative integers.
deba@2440
    61
  ///	However \c CostMap::Value should be signed type.
kpeter@2509
    62
  /// - Supply values should be signed integers.
deba@2440
    63
  /// - \c LowerMap::Value must be convertible to
deba@2440
    64
  ///	\c CapacityMap::Value and \c CapacityMap::Value must be
deba@2440
    65
  ///	convertible to \c SupplyMap::Value.
deba@2440
    66
  ///
deba@2440
    67
  /// \author Peter Kovacs
deba@2440
    68
deba@2440
    69
template < typename Graph,
deba@2440
    70
	   typename LowerMap = typename Graph::template EdgeMap<int>,
deba@2440
    71
	   typename CapacityMap = LowerMap,
deba@2440
    72
	   typename CostMap = typename Graph::template EdgeMap<int>,
deba@2440
    73
	   typename SupplyMap = typename Graph::template NodeMap
deba@2440
    74
				<typename CapacityMap::Value> >
deba@2440
    75
  class CapacityScaling
deba@2440
    76
  {
deba@2440
    77
    typedef typename Graph::Node Node;
deba@2440
    78
    typedef typename Graph::NodeIt NodeIt;
deba@2440
    79
    typedef typename Graph::Edge Edge;
deba@2440
    80
    typedef typename Graph::EdgeIt EdgeIt;
deba@2440
    81
    typedef typename Graph::InEdgeIt InEdgeIt;
deba@2440
    82
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2440
    83
deba@2440
    84
    typedef typename LowerMap::Value Lower;
deba@2440
    85
    typedef typename CapacityMap::Value Capacity;
deba@2440
    86
    typedef typename CostMap::Value Cost;
deba@2440
    87
    typedef typename SupplyMap::Value Supply;
deba@2440
    88
    typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
deba@2440
    89
    typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
deba@2440
    90
deba@2440
    91
    typedef ResGraphAdaptor< const Graph, Capacity,
deba@2440
    92
			     CapacityRefMap, CapacityRefMap > ResGraph;
deba@2440
    93
    typedef typename ResGraph::Node ResNode;
deba@2440
    94
    typedef typename ResGraph::NodeIt ResNodeIt;
deba@2440
    95
    typedef typename ResGraph::Edge ResEdge;
deba@2440
    96
    typedef typename ResGraph::EdgeIt ResEdgeIt;
deba@2440
    97
deba@2440
    98
  public:
deba@2440
    99
deba@2440
   100
    /// \brief The type of the flow map.
deba@2440
   101
    typedef CapacityRefMap FlowMap;
deba@2440
   102
    /// \brief The type of the potential map.
deba@2440
   103
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
   104
deba@2440
   105
  protected:
deba@2440
   106
deba@2440
   107
    /// \brief Map adaptor class for handling reduced edge costs.
deba@2440
   108
    class ReducedCostMap : public MapBase<ResEdge, Cost>
deba@2440
   109
    {
deba@2440
   110
    private:
deba@2440
   111
deba@2440
   112
      const ResGraph &gr;
deba@2440
   113
      const CostMap &cost_map;
deba@2440
   114
      const PotentialMap &pot_map;
deba@2440
   115
deba@2440
   116
    public:
deba@2440
   117
deba@2440
   118
      ReducedCostMap( const ResGraph &_gr,
deba@2440
   119
		      const CostMap &_cost,
deba@2440
   120
		      const PotentialMap &_pot ) :
deba@2440
   121
	gr(_gr), cost_map(_cost), pot_map(_pot) {}
deba@2440
   122
kpeter@2509
   123
      Cost operator[](const ResEdge &e) const {
deba@2440
   124
	return ResGraph::forward(e) ?
deba@2440
   125
	   cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)] :
deba@2440
   126
	  -cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
deba@2440
   127
      }
deba@2440
   128
deba@2440
   129
    }; //class ReducedCostMap
deba@2440
   130
kpeter@2509
   131
    /// \brief Map class for the \ref lemon::Dijkstra "Dijkstra"
kpeter@2509
   132
    /// algorithm to update node potentials.
deba@2440
   133
    class PotentialUpdateMap : public MapBase<ResNode, Cost>
deba@2440
   134
    {
deba@2440
   135
    private:
deba@2440
   136
deba@2440
   137
      PotentialMap *pot;
deba@2440
   138
      typedef std::pair<ResNode, Cost> Pair;
deba@2440
   139
      std::vector<Pair> data;
deba@2440
   140
deba@2440
   141
    public:
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
kpeter@2509
   151
      void set(const ResNode &n, const Cost &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
kpeter@2509
   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
      DeltaFilterMap(const ResGraph &_gr, const Capacity &_delta) :
deba@2440
   195
	gr(_gr), delta(_delta) {}
deba@2440
   196
kpeter@2509
   197
      bool operator[](const ResEdge &e) const {
deba@2440
   198
	return gr.rescap(e) >= delta;
deba@2440
   199
      }
deba@2440
   200
deba@2440
   201
    }; //class DeltaFilterMap
deba@2440
   202
deba@2440
   203
    typedef EdgeSubGraphAdaptor<const ResGraph, const DeltaFilterMap>
deba@2440
   204
      DeltaResGraph;
deba@2440
   205
deba@2440
   206
    /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
deba@2440
   207
    class ResDijkstraTraits :
deba@2440
   208
      public DijkstraDefaultTraits<DeltaResGraph, ReducedCostMap>
deba@2440
   209
    {
deba@2440
   210
    public:
deba@2440
   211
deba@2440
   212
      typedef PotentialUpdateMap DistMap;
deba@2440
   213
deba@2440
   214
      static DistMap *createDistMap(const DeltaResGraph&) {
deba@2440
   215
	return new DistMap();
deba@2440
   216
      }
deba@2440
   217
deba@2440
   218
    }; //class ResDijkstraTraits
deba@2440
   219
deba@2440
   220
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   221
    /// \brief Map adaptor class for identifing deficit nodes.
deba@2440
   222
    class DeficitBoolMap : public MapBase<ResNode, bool>
deba@2440
   223
    {
deba@2440
   224
    private:
deba@2440
   225
deba@2440
   226
      const SupplyRefMap &imb;
deba@2440
   227
deba@2440
   228
    public:
deba@2440
   229
deba@2440
   230
      DeficitBoolMap(const SupplyRefMap &_imb) : imb(_imb) {}
deba@2440
   231
deba@2440
   232
      bool operator[](const ResNode &n) const {
deba@2440
   233
	return imb[n] < 0;
deba@2440
   234
      }
deba@2440
   235
deba@2440
   236
    }; //class DeficitBoolMap
deba@2440
   237
deba@2440
   238
    /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
deba@2440
   239
    class ResDijkstraTraits :
deba@2440
   240
      public DijkstraDefaultTraits<ResGraph, ReducedCostMap>
deba@2440
   241
    {
deba@2440
   242
    public:
deba@2440
   243
deba@2440
   244
      typedef PotentialUpdateMap DistMap;
deba@2440
   245
deba@2440
   246
      static DistMap *createDistMap(const ResGraph&) {
deba@2440
   247
	return new DistMap();
deba@2440
   248
      }
deba@2440
   249
deba@2440
   250
    }; //class ResDijkstraTraits
deba@2440
   251
#endif
deba@2440
   252
deba@2440
   253
  protected:
deba@2440
   254
deba@2440
   255
    /// \brief The directed graph the algorithm runs on.
deba@2440
   256
    const Graph &graph;
deba@2440
   257
    /// \brief The original lower bound map.
deba@2440
   258
    const LowerMap *lower;
deba@2440
   259
    /// \brief The modified capacity map.
deba@2440
   260
    CapacityRefMap capacity;
deba@2440
   261
    /// \brief The cost map.
deba@2440
   262
    const CostMap &cost;
deba@2440
   263
    /// \brief The modified supply map.
deba@2440
   264
    SupplyRefMap supply;
deba@2440
   265
    /// \brief The sum of supply values equals zero.
deba@2440
   266
    bool valid_supply;
deba@2440
   267
deba@2440
   268
    /// \brief The edge map of the current flow.
deba@2440
   269
    FlowMap flow;
deba@2440
   270
    /// \brief The potential node map.
deba@2440
   271
    PotentialMap potential;
deba@2440
   272
    /// \brief The residual graph.
deba@2440
   273
    ResGraph res_graph;
deba@2440
   274
    /// \brief The reduced cost map.
deba@2440
   275
    ReducedCostMap red_cost;
deba@2440
   276
deba@2440
   277
    /// \brief The imbalance map.
deba@2440
   278
    SupplyRefMap imbalance;
deba@2440
   279
    /// \brief The excess nodes.
deba@2440
   280
    std::vector<ResNode> excess_nodes;
deba@2440
   281
    /// \brief The index of the next excess node.
deba@2440
   282
    int next_node;
deba@2440
   283
deba@2440
   284
#ifdef WITH_SCALING
deba@2440
   285
    typedef Dijkstra<DeltaResGraph, ReducedCostMap, ResDijkstraTraits>
deba@2440
   286
      ResDijkstra;
deba@2440
   287
    /// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
deba@2440
   288
    /// shortest paths in the residual graph with respect to the
deba@2440
   289
    /// reduced edge costs.
deba@2440
   290
    ResDijkstra dijkstra;
deba@2440
   291
deba@2440
   292
    /// \brief The delta parameter used for capacity scaling.
deba@2440
   293
    Capacity delta;
kpeter@2509
   294
    /// \brief Edge filter map for filtering edges with at least
kpeter@2509
   295
    /// \c delta residual capacity.
deba@2440
   296
    DeltaFilterMap delta_filter;
kpeter@2509
   297
    /// \brief The delta residual graph (i.e. the subgraph of the
kpeter@2509
   298
    /// residual graph consisting of edges with at least \c delta
kpeter@2509
   299
    /// residual capacity).
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];
kpeter@2507
   363
	supply[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];
kpeter@2507
   444
	supply[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;
kpeter@2507
   532
      imbalance = supply;
deba@2440
   533
deba@2440
   534
      // Initalizing Dijkstra class
deba@2440
   535
      updater.potentialMap(potential);
deba@2440
   536
      dijkstra.distMap(updater).predMap(pred);
deba@2440
   537
deba@2440
   538
#ifdef WITH_SCALING
deba@2440
   539
      // Initilaizing delta value
deba@2457
   540
      Supply max_sup = 0, max_dem = 0;
deba@2457
   541
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2457
   542
	if (supply[n] >  max_sup) max_sup =  supply[n];
deba@2457
   543
	if (supply[n] < -max_dem) max_dem = -supply[n];
deba@2440
   544
      }
deba@2457
   545
      if (max_dem < max_sup) max_sup = max_dem;
kpeter@2471
   546
      for ( delta = 1; SCALING_FACTOR * delta < max_sup;
kpeter@2471
   547
	    delta *= SCALING_FACTOR ) ;
deba@2440
   548
#endif
deba@2440
   549
      return true;
deba@2440
   550
    }
deba@2440
   551
deba@2440
   552
#ifdef WITH_SCALING
deba@2440
   553
    /// \brief Executes the capacity scaling version of the successive
deba@2440
   554
    /// shortest path algorithm.
deba@2440
   555
    bool start() {
deba@2440
   556
      typedef typename DeltaResGraph::EdgeIt DeltaResEdgeIt;
deba@2440
   557
      typedef typename DeltaResGraph::Edge DeltaResEdge;
deba@2457
   558
#ifdef _DEBUG_ITER_
deba@2457
   559
      int dijk_num = 0;
deba@2457
   560
#endif
deba@2440
   561
deba@2440
   562
      // Processing capacity scaling phases
deba@2440
   563
      ResNode s, t;
kpeter@2471
   564
      for ( ; delta >= 1; delta = delta < SCALING_FACTOR && delta > 1 ?
kpeter@2471
   565
				  1 : delta / SCALING_FACTOR )
deba@2440
   566
      {
deba@2440
   567
	// Saturating edges not satisfying the optimality condition
deba@2440
   568
	Capacity r;
deba@2440
   569
	for (DeltaResEdgeIt e(dres_graph); e != INVALID; ++e) {
deba@2440
   570
	  if (red_cost[e] < 0) {
deba@2440
   571
	    res_graph.augment(e, r = res_graph.rescap(e));
kpeter@2509
   572
	    imbalance[dres_graph.source(e)] -= r;
deba@2440
   573
	    imbalance[dres_graph.target(e)] += r;
deba@2440
   574
	  }
deba@2440
   575
	}
deba@2440
   576
deba@2440
   577
	// Finding excess nodes
deba@2440
   578
	excess_nodes.clear();
deba@2457
   579
	deficit_nodes.clear();
deba@2440
   580
	for (ResNodeIt n(res_graph); n != INVALID; ++n) {
deba@2457
   581
	  if (imbalance[n] >=  delta) excess_nodes.push_back(n);
deba@2457
   582
	  if (imbalance[n] <= -delta) deficit_nodes.push_back(n);
deba@2440
   583
	}
deba@2440
   584
	next_node = 0;
deba@2440
   585
deba@2457
   586
	// Finding shortest paths
deba@2440
   587
	while (next_node < excess_nodes.size()) {
deba@2457
   588
	  // Checking deficit nodes
deba@2457
   589
	  if (delta > 1) {
deba@2457
   590
	    bool delta_def = false;
deba@2457
   591
	    for (int i = 0; i < deficit_nodes.size(); ++i) {
deba@2457
   592
	      if (imbalance[deficit_nodes[i]] <= -delta) {
deba@2457
   593
		delta_def = true;
deba@2457
   594
		break;
deba@2457
   595
	      }
deba@2457
   596
	    }
deba@2457
   597
	    if (!delta_def) break;
deba@2457
   598
	  }
deba@2457
   599
deba@2440
   600
	  // Running Dijkstra
deba@2440
   601
	  s = excess_nodes[next_node];
deba@2440
   602
	  updater.init();
deba@2440
   603
	  dijkstra.init();
deba@2440
   604
	  dijkstra.addSource(s);
deba@2457
   605
#ifdef _DEBUG_ITER_
deba@2457
   606
	  ++dijk_num;
deba@2457
   607
#endif
deba@2440
   608
	  if ((t = dijkstra.start(delta_deficit)) == INVALID) {
deba@2440
   609
	    if (delta > 1) {
deba@2440
   610
	      ++next_node;
deba@2440
   611
	      continue;
deba@2440
   612
	    }
deba@2440
   613
	    return false;
deba@2440
   614
	  }
deba@2440
   615
deba@2440
   616
	  // Updating node potentials
deba@2440
   617
	  updater.update();
deba@2440
   618
deba@2440
   619
	  // Augment along a shortest path from s to t
deba@2440
   620
	  Capacity d = imbalance[s] < -imbalance[t] ?
deba@2440
   621
	    imbalance[s] : -imbalance[t];
deba@2440
   622
	  ResNode u = t;
deba@2440
   623
	  ResEdge e;
deba@2440
   624
	  if (d > delta) {
deba@2440
   625
	    while ((e = pred[u]) != INVALID) {
deba@2440
   626
	      if (res_graph.rescap(e) < d) d = res_graph.rescap(e);
deba@2440
   627
	      u = dres_graph.source(e);
deba@2440
   628
	    }
deba@2440
   629
	  }
deba@2440
   630
	  u = t;
deba@2440
   631
	  while ((e = pred[u]) != INVALID) {
deba@2440
   632
	    res_graph.augment(e, d);
deba@2440
   633
	    u = dres_graph.source(e);
deba@2440
   634
	  }
deba@2440
   635
	  imbalance[s] -= d;
deba@2440
   636
	  imbalance[t] += d;
deba@2440
   637
	  if (imbalance[s] < delta) ++next_node;
deba@2440
   638
	}
deba@2440
   639
      }
deba@2457
   640
#ifdef _DEBUG_ITER_
deba@2457
   641
      std::cout << "Cost Scaling algorithm finished with running Dijkstra algorithm "
deba@2457
   642
	<< dijk_num << " times." << std::endl;
deba@2457
   643
#endif
deba@2440
   644
deba@2440
   645
      // Handling nonzero lower bounds
deba@2440
   646
      if (lower) {
deba@2440
   647
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   648
	  flow[e] += (*lower)[e];
deba@2440
   649
      }
deba@2440
   650
      return true;
deba@2440
   651
    }
deba@2440
   652
deba@2440
   653
#else //WITHOUT_CAPACITY_SCALING
deba@2440
   654
    /// \brief Executes the successive shortest path algorithm without
deba@2440
   655
    /// capacity scaling.
deba@2440
   656
    bool start() {
deba@2440
   657
      // Finding excess nodes
deba@2440
   658
      for (ResNodeIt n(res_graph); n != INVALID; ++n) {
deba@2440
   659
	if (imbalance[n] > 0) excess_nodes.push_back(n);
deba@2440
   660
      }
deba@2440
   661
      if (excess_nodes.size() == 0) return true;
deba@2440
   662
      next_node = 0;
deba@2440
   663
deba@2457
   664
      // Finding shortest paths
deba@2440
   665
      ResNode s, t;
deba@2440
   666
      while ( imbalance[excess_nodes[next_node]] > 0 ||
deba@2440
   667
	      ++next_node < excess_nodes.size() )
deba@2440
   668
      {
deba@2440
   669
	// Running Dijkstra
deba@2440
   670
	s = excess_nodes[next_node];
deba@2440
   671
	updater.init();
deba@2440
   672
	dijkstra.init();
deba@2440
   673
	dijkstra.addSource(s);
deba@2440
   674
	if ((t = dijkstra.start(has_deficit)) == INVALID)
deba@2440
   675
	  return false;
deba@2440
   676
deba@2440
   677
	// Updating node potentials
deba@2440
   678
	updater.update();
deba@2440
   679
deba@2440
   680
	// Augmenting along a shortest path from s to t
deba@2440
   681
	Capacity delta = imbalance[s] < -imbalance[t] ?
deba@2440
   682
	  imbalance[s] : -imbalance[t];
deba@2440
   683
	ResNode u = t;
deba@2440
   684
	ResEdge e;
deba@2440
   685
	while ((e = pred[u]) != INVALID) {
deba@2440
   686
	  if (res_graph.rescap(e) < delta) delta = res_graph.rescap(e);
deba@2440
   687
	  u = res_graph.source(e);
deba@2440
   688
	}
deba@2440
   689
	u = t;
deba@2440
   690
	while ((e = pred[u]) != INVALID) {
deba@2440
   691
	  res_graph.augment(e, delta);
deba@2440
   692
	  u = res_graph.source(e);
deba@2440
   693
	}
deba@2440
   694
	imbalance[s] -= delta;
deba@2440
   695
	imbalance[t] += delta;
deba@2440
   696
      }
deba@2440
   697
deba@2440
   698
      // Handling nonzero lower bounds
deba@2440
   699
      if (lower) {
deba@2440
   700
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   701
	  flow[e] += (*lower)[e];
deba@2440
   702
      }
deba@2440
   703
      return true;
deba@2440
   704
    }
deba@2440
   705
#endif
deba@2440
   706
deba@2440
   707
  }; //class CapacityScaling
deba@2440
   708
deba@2440
   709
  ///@}
deba@2440
   710
deba@2440
   711
} //namespace lemon
deba@2440
   712
deba@2440
   713
#endif //LEMON_CAPACITY_SCALING_H