lemon/min_cost_max_flow.h
author kpeter
Mon, 01 Jun 2009 15:35:27 +0000
changeset 2635 23570e368afa
parent 2587 061be2e64eca
permissions -rw-r--r--
XTI data structure for NS - backport hg commit [8c3112a66878]
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
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
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_MIN_COST_MAX_FLOW_H
deba@2440
    20
#define LEMON_MIN_COST_MAX_FLOW_H
deba@2440
    21
deba@2440
    22
/// \ingroup min_cost_flow
deba@2440
    23
///
deba@2440
    24
/// \file
kpeter@2556
    25
/// \brief An efficient algorithm for finding a minimum cost maximum flow.
deba@2440
    26
deba@2440
    27
#include <lemon/preflow.h>
deba@2440
    28
#include <lemon/network_simplex.h>
deba@2440
    29
#include <lemon/maps.h>
deba@2440
    30
deba@2440
    31
namespace lemon {
deba@2440
    32
deba@2440
    33
  /// \addtogroup min_cost_flow
deba@2440
    34
  /// @{
deba@2440
    35
kpeter@2556
    36
  /// \brief An efficient algorithm for finding a minimum cost
kpeter@2556
    37
  /// maximum flow.
deba@2440
    38
  ///
kpeter@2556
    39
  /// \ref MinCostMaxFlow implements an efficient algorithm for
kpeter@2556
    40
  /// finding a maximum flow having minimal total cost from a given
kpeter@2556
    41
  /// source node to a given target node in a directed graph.
deba@2440
    42
  ///
kpeter@2576
    43
  /// \ref MinCostMaxFlow uses \ref Preflow for finding the maximum
kpeter@2576
    44
  /// flow value and \ref NetworkSimplex for finding a minimum cost
kpeter@2576
    45
  /// flow of that value.
kpeter@2576
    46
  /// According to our benchmark tests \ref Preflow is generally the
kpeter@2576
    47
  /// most efficient algorithm for the maximum flow problem and
kpeter@2576
    48
  /// \ref NetworkSimplex is the most efficient for the minimum cost
kpeter@2576
    49
  /// flow problem in LEMON.
deba@2440
    50
  ///
kpeter@2576
    51
  /// \tparam Graph The directed graph type the algorithm runs on.
kpeter@2576
    52
  /// \tparam CapacityMap The type of the capacity (upper bound) map.
kpeter@2576
    53
  /// \tparam CostMap The type of the cost (length) map.
deba@2440
    54
  ///
deba@2440
    55
  /// \warning
kpeter@2576
    56
  /// - Edge capacities and costs should be \e non-negative \e integers.
kpeter@2576
    57
  /// - \c CapacityMap::Value must be convertible to \c CostMap::Value.
kpeter@2582
    58
  /// - \c CostMap::Value must be signed type.
deba@2440
    59
  ///
deba@2440
    60
  /// \author Peter Kovacs
kpeter@2533
    61
  template < typename Graph,
kpeter@2556
    62
             typename CapacityMap = typename Graph::template EdgeMap<int>,
kpeter@2556
    63
             typename CostMap = typename Graph::template EdgeMap<int> >
deba@2440
    64
  class MinCostMaxFlow
deba@2440
    65
  {
kpeter@2587
    66
    GRAPH_TYPEDEFS(typename Graph);
deba@2440
    67
deba@2440
    68
    typedef typename CapacityMap::Value Capacity;
kpeter@2556
    69
    typedef typename CostMap::Value Cost;
kpeter@2576
    70
    typedef typename Graph::template NodeMap<Cost> SupplyMap;
kpeter@2576
    71
kpeter@2576
    72
    typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
deba@2440
    73
    typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
kpeter@2576
    74
                            CostMap, SupplyMap > MinCostFlowImpl;
deba@2440
    75
deba@2440
    76
  public:
deba@2440
    77
kpeter@2556
    78
    /// The type of the flow map.
deba@2440
    79
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
kpeter@2576
    80
    /// The type of the potential map.
kpeter@2576
    81
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
    82
deba@2440
    83
  private:
deba@2440
    84
kpeter@2576
    85
    // The directed graph the algorithm runs on
kpeter@2576
    86
    const Graph &_graph;
kpeter@2587
    87
    // The capacity map
kpeter@2576
    88
    const CapacityMap &_capacity;
kpeter@2576
    89
    // The cost map
kpeter@2576
    90
    const CostMap &_cost;
deba@2440
    91
kpeter@2576
    92
    // Edge map of the found flow
kpeter@2587
    93
    FlowMap *_flow;
kpeter@2587
    94
    bool _local_flow;
kpeter@2587
    95
    // Node map of the current potentials
kpeter@2587
    96
    PotentialMap *_potential;
kpeter@2587
    97
    bool _local_potential;
kpeter@2576
    98
kpeter@2576
    99
    // The source node
kpeter@2576
   100
    Node _source;
kpeter@2576
   101
    // The target node
kpeter@2576
   102
    Node _target;
deba@2440
   103
deba@2440
   104
  public:
deba@2440
   105
kpeter@2587
   106
    /// \brief Constructor.
deba@2440
   107
    ///
kpeter@2587
   108
    /// Constructor.
deba@2440
   109
    ///
kpeter@2587
   110
    /// \param graph The directed graph the algorithm runs on.
kpeter@2587
   111
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2587
   112
    /// \param cost The cost (length) values of the edges.
kpeter@2587
   113
    /// \param s The source node.
kpeter@2587
   114
    /// \param t The target node.
kpeter@2576
   115
    MinCostMaxFlow( const Graph &graph,
kpeter@2576
   116
                    const CapacityMap &capacity,
kpeter@2576
   117
                    const CostMap &cost,
kpeter@2576
   118
                    Node s, Node t ) :
kpeter@2587
   119
      _graph(graph), _capacity(capacity), _cost(cost), _flow(0),
kpeter@2587
   120
      _local_flow(false), _potential(0), _local_potential(false),
kpeter@2587
   121
      _source(s), _target(t) {}
kpeter@2587
   122
kpeter@2587
   123
    /// Destructor.
kpeter@2587
   124
    ~MinCostMaxFlow() {
kpeter@2587
   125
      if (_local_flow) delete _flow;
kpeter@2587
   126
      if (_local_potential) delete _potential;
kpeter@2587
   127
    }
kpeter@2587
   128
kpeter@2620
   129
    /// \brief Set the flow map.
kpeter@2587
   130
    ///
kpeter@2620
   131
    /// Set the flow map.
kpeter@2587
   132
    ///
kpeter@2587
   133
    /// \return \c (*this)
kpeter@2587
   134
    MinCostMaxFlow& flowMap(FlowMap &map) {
kpeter@2587
   135
      if (_local_flow) {
kpeter@2587
   136
        delete _flow;
kpeter@2587
   137
        _local_flow = false;
kpeter@2587
   138
      }
kpeter@2587
   139
      _flow = &map;
kpeter@2587
   140
      return *this;
kpeter@2587
   141
    }
kpeter@2587
   142
kpeter@2620
   143
    /// \brief Set the potential map.
kpeter@2587
   144
    ///
kpeter@2620
   145
    /// Set the potential map.
kpeter@2587
   146
    ///
kpeter@2587
   147
    /// \return \c (*this)
kpeter@2587
   148
    MinCostMaxFlow& potentialMap(PotentialMap &map) {
kpeter@2587
   149
      if (_local_potential) {
kpeter@2587
   150
        delete _potential;
kpeter@2587
   151
        _local_potential = false;
kpeter@2587
   152
      }
kpeter@2587
   153
      _potential = &map;
kpeter@2587
   154
      return *this;
kpeter@2587
   155
    }
kpeter@2587
   156
kpeter@2587
   157
    /// \name Execution control
kpeter@2587
   158
kpeter@2587
   159
    /// @{
deba@2440
   160
kpeter@2620
   161
    /// \brief Run the algorithm.
deba@2440
   162
    ///
kpeter@2620
   163
    /// Run the algorithm.
kpeter@2576
   164
    void run() {
kpeter@2587
   165
      // Initializing maps
kpeter@2587
   166
      if (!_flow) {
kpeter@2587
   167
        _flow = new FlowMap(_graph);
kpeter@2587
   168
        _local_flow = true;
kpeter@2587
   169
      }
kpeter@2587
   170
      if (!_potential) {
kpeter@2587
   171
        _potential = new PotentialMap(_graph);
kpeter@2587
   172
        _local_potential = true;
kpeter@2587
   173
      }
kpeter@2587
   174
      // Running Preflow
kpeter@2576
   175
      MaxFlowImpl preflow(_graph, _capacity, _source, _target);
kpeter@2587
   176
      preflow.flowMap(*_flow).runMinCut();
kpeter@2587
   177
      // Running NetworkSimplex
kpeter@2576
   178
      MinCostFlowImpl mcf( _graph, _capacity, _cost,
kpeter@2576
   179
                           _source, _target, preflow.flowValue() );
kpeter@2587
   180
      mcf.flowMap(*_flow).potentialMap(*_potential).run();
kpeter@2576
   181
    }
kpeter@2576
   182
kpeter@2587
   183
    /// @}
kpeter@2587
   184
kpeter@2587
   185
    /// \name Query Functions
kpeter@2620
   186
    /// The results of the algorithm can be obtained using these
kpeter@2620
   187
    /// functions.\n
kpeter@2620
   188
    /// \ref lemon::MinCostMaxFlow::run() "run()" must be called before
kpeter@2620
   189
    /// using them.
kpeter@2587
   190
kpeter@2587
   191
    /// @{
kpeter@2587
   192
kpeter@2620
   193
    /// \brief Return a const reference to the edge map storing the
kpeter@2576
   194
    /// found flow.
kpeter@2576
   195
    ///
kpeter@2620
   196
    /// Return a const reference to the edge map storing the found flow.
deba@2440
   197
    ///
deba@2440
   198
    /// \pre \ref run() must be called before using this function.
deba@2440
   199
    const FlowMap& flowMap() const {
kpeter@2587
   200
      return *_flow;
kpeter@2576
   201
    }
kpeter@2576
   202
kpeter@2620
   203
    /// \brief Return a const reference to the node map storing the
kpeter@2576
   204
    /// found potentials (the dual solution).
kpeter@2576
   205
    ///
kpeter@2620
   206
    /// Return a const reference to the node map storing the found
kpeter@2576
   207
    /// potentials (the dual solution).
kpeter@2576
   208
    ///
kpeter@2576
   209
    /// \pre \ref run() must be called before using this function.
kpeter@2576
   210
    const PotentialMap& potentialMap() const {
kpeter@2587
   211
      return *_potential;
kpeter@2587
   212
    }
kpeter@2587
   213
kpeter@2620
   214
    /// \brief Return the flow on the given edge.
kpeter@2587
   215
    ///
kpeter@2620
   216
    /// Return the flow on the given edge.
kpeter@2587
   217
    ///
kpeter@2587
   218
    /// \pre \ref run() must be called before using this function.
kpeter@2587
   219
    Capacity flow(const Edge& edge) const {
kpeter@2587
   220
      return (*_flow)[edge];
kpeter@2587
   221
    }
kpeter@2587
   222
kpeter@2620
   223
    /// \brief Return the potential of the given node.
kpeter@2587
   224
    ///
kpeter@2620
   225
    /// Return the potential of the given node.
kpeter@2587
   226
    ///
kpeter@2587
   227
    /// \pre \ref run() must be called before using this function.
kpeter@2587
   228
    Cost potential(const Node& node) const {
kpeter@2587
   229
      return (*_potential)[node];
deba@2440
   230
    }
deba@2440
   231
kpeter@2620
   232
    /// \brief Return the total cost of the found flow.
deba@2440
   233
    ///
kpeter@2620
   234
    /// Return the total cost of the found flow. The complexity of the
deba@2440
   235
    /// function is \f$ O(e) \f$.
deba@2440
   236
    ///
deba@2440
   237
    /// \pre \ref run() must be called before using this function.
deba@2440
   238
    Cost totalCost() const {
deba@2440
   239
      Cost c = 0;
kpeter@2587
   240
      for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2587
   241
        c += (*_flow)[e] * _cost[e];
deba@2440
   242
      return c;
deba@2440
   243
    }
deba@2440
   244
kpeter@2587
   245
    /// @}
kpeter@2587
   246
deba@2440
   247
  }; //class MinCostMaxFlow
deba@2440
   248
deba@2440
   249
  ///@}
deba@2440
   250
deba@2440
   251
} //namespace lemon
deba@2440
   252
deba@2440
   253
#endif //LEMON_MIN_COST_MAX_FLOW_H