lemon/min_cost_max_flow.h
author hegyi
Tue, 08 Apr 2008 11:39:40 +0000
changeset 2602 1c7790d9e025
parent 2582 4f1ac622bb7a
child 2620 8f41a3129746
permissions -rw-r--r--
Rel.07 NEWS - 3. round
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
deba@2440
    61
kpeter@2533
    62
  template < typename Graph,
kpeter@2556
    63
             typename CapacityMap = typename Graph::template EdgeMap<int>,
kpeter@2556
    64
             typename CostMap = typename Graph::template EdgeMap<int> >
deba@2440
    65
  class MinCostMaxFlow
deba@2440
    66
  {
kpeter@2587
    67
    GRAPH_TYPEDEFS(typename Graph);
deba@2440
    68
deba@2440
    69
    typedef typename CapacityMap::Value Capacity;
kpeter@2556
    70
    typedef typename CostMap::Value Cost;
kpeter@2576
    71
    typedef typename Graph::template NodeMap<Cost> SupplyMap;
kpeter@2576
    72
kpeter@2576
    73
    typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
deba@2440
    74
    typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
kpeter@2576
    75
                            CostMap, SupplyMap > MinCostFlowImpl;
deba@2440
    76
deba@2440
    77
  public:
deba@2440
    78
kpeter@2556
    79
    /// The type of the flow map.
deba@2440
    80
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
kpeter@2576
    81
    /// The type of the potential map.
kpeter@2576
    82
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
    83
deba@2440
    84
  private:
deba@2440
    85
kpeter@2576
    86
    // The directed graph the algorithm runs on
kpeter@2576
    87
    const Graph &_graph;
kpeter@2587
    88
    // The capacity map
kpeter@2576
    89
    const CapacityMap &_capacity;
kpeter@2576
    90
    // The cost map
kpeter@2576
    91
    const CostMap &_cost;
deba@2440
    92
kpeter@2576
    93
    // Edge map of the found flow
kpeter@2587
    94
    FlowMap *_flow;
kpeter@2587
    95
    bool _local_flow;
kpeter@2587
    96
    // Node map of the current potentials
kpeter@2587
    97
    PotentialMap *_potential;
kpeter@2587
    98
    bool _local_potential;
kpeter@2576
    99
kpeter@2576
   100
    // The source node
kpeter@2576
   101
    Node _source;
kpeter@2576
   102
    // The target node
kpeter@2576
   103
    Node _target;
deba@2440
   104
deba@2440
   105
  public:
deba@2440
   106
kpeter@2587
   107
    /// \brief Constructor.
deba@2440
   108
    ///
kpeter@2587
   109
    /// Constructor.
deba@2440
   110
    ///
kpeter@2587
   111
    /// \param graph The directed graph the algorithm runs on.
kpeter@2587
   112
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2587
   113
    /// \param cost The cost (length) values of the edges.
kpeter@2587
   114
    /// \param s The source node.
kpeter@2587
   115
    /// \param t The target node.
kpeter@2576
   116
    MinCostMaxFlow( const Graph &graph,
kpeter@2576
   117
                    const CapacityMap &capacity,
kpeter@2576
   118
                    const CostMap &cost,
kpeter@2576
   119
                    Node s, Node t ) :
kpeter@2587
   120
      _graph(graph), _capacity(capacity), _cost(cost), _flow(0),
kpeter@2587
   121
      _local_flow(false), _potential(0), _local_potential(false),
kpeter@2587
   122
      _source(s), _target(t) {}
kpeter@2587
   123
kpeter@2587
   124
    /// Destructor.
kpeter@2587
   125
    ~MinCostMaxFlow() {
kpeter@2587
   126
      if (_local_flow) delete _flow;
kpeter@2587
   127
      if (_local_potential) delete _potential;
kpeter@2587
   128
    }
kpeter@2587
   129
kpeter@2587
   130
    /// \brief Sets the flow map.
kpeter@2587
   131
    ///
kpeter@2587
   132
    /// Sets the flow map.
kpeter@2587
   133
    ///
kpeter@2587
   134
    /// \return \c (*this)
kpeter@2587
   135
    MinCostMaxFlow& flowMap(FlowMap &map) {
kpeter@2587
   136
      if (_local_flow) {
kpeter@2587
   137
        delete _flow;
kpeter@2587
   138
        _local_flow = false;
kpeter@2587
   139
      }
kpeter@2587
   140
      _flow = &map;
kpeter@2587
   141
      return *this;
kpeter@2587
   142
    }
kpeter@2587
   143
kpeter@2587
   144
    /// \brief Sets the potential map.
kpeter@2587
   145
    ///
kpeter@2587
   146
    /// Sets the potential map.
kpeter@2587
   147
    ///
kpeter@2587
   148
    /// \return \c (*this)
kpeter@2587
   149
    MinCostMaxFlow& potentialMap(PotentialMap &map) {
kpeter@2587
   150
      if (_local_potential) {
kpeter@2587
   151
        delete _potential;
kpeter@2587
   152
        _local_potential = false;
kpeter@2587
   153
      }
kpeter@2587
   154
      _potential = &map;
kpeter@2587
   155
      return *this;
kpeter@2587
   156
    }
kpeter@2587
   157
kpeter@2587
   158
    /// \name Execution control
kpeter@2587
   159
    /// The only way to execute the algorithm is to call the run()
kpeter@2587
   160
    /// function.
kpeter@2587
   161
kpeter@2587
   162
    /// @{
deba@2440
   163
kpeter@2576
   164
    /// \brief Runs the algorithm.
deba@2440
   165
    ///
kpeter@2576
   166
    /// Runs the algorithm.
kpeter@2576
   167
    void run() {
kpeter@2587
   168
      // Initializing maps
kpeter@2587
   169
      if (!_flow) {
kpeter@2587
   170
        _flow = new FlowMap(_graph);
kpeter@2587
   171
        _local_flow = true;
kpeter@2587
   172
      }
kpeter@2587
   173
      if (!_potential) {
kpeter@2587
   174
        _potential = new PotentialMap(_graph);
kpeter@2587
   175
        _local_potential = true;
kpeter@2587
   176
      }
kpeter@2587
   177
      // Running Preflow
kpeter@2576
   178
      MaxFlowImpl preflow(_graph, _capacity, _source, _target);
kpeter@2587
   179
      preflow.flowMap(*_flow).runMinCut();
kpeter@2587
   180
      // Running NetworkSimplex
kpeter@2576
   181
      MinCostFlowImpl mcf( _graph, _capacity, _cost,
kpeter@2576
   182
                           _source, _target, preflow.flowValue() );
kpeter@2587
   183
      mcf.flowMap(*_flow).potentialMap(*_potential).run();
kpeter@2576
   184
    }
kpeter@2576
   185
kpeter@2587
   186
    /// @}
kpeter@2587
   187
kpeter@2587
   188
    /// \name Query Functions
kpeter@2587
   189
    /// The result of the algorithm can be obtained using these
kpeter@2587
   190
    /// functions.
kpeter@2587
   191
    /// \n run() must be called before using them.
kpeter@2587
   192
kpeter@2587
   193
    /// @{
kpeter@2587
   194
kpeter@2576
   195
    /// \brief Returns a const reference to the edge map storing the
kpeter@2576
   196
    /// found flow.
kpeter@2576
   197
    ///
kpeter@2576
   198
    /// Returns a const reference to the edge map storing the found flow.
deba@2440
   199
    ///
deba@2440
   200
    /// \pre \ref run() must be called before using this function.
deba@2440
   201
    const FlowMap& flowMap() const {
kpeter@2587
   202
      return *_flow;
kpeter@2576
   203
    }
kpeter@2576
   204
kpeter@2576
   205
    /// \brief Returns a const reference to the node map storing the
kpeter@2576
   206
    /// found potentials (the dual solution).
kpeter@2576
   207
    ///
kpeter@2576
   208
    /// Returns a const reference to the node map storing the found
kpeter@2576
   209
    /// potentials (the dual solution).
kpeter@2576
   210
    ///
kpeter@2576
   211
    /// \pre \ref run() must be called before using this function.
kpeter@2576
   212
    const PotentialMap& potentialMap() const {
kpeter@2587
   213
      return *_potential;
kpeter@2587
   214
    }
kpeter@2587
   215
kpeter@2587
   216
    /// \brief Returns the flow on the given edge.
kpeter@2587
   217
    ///
kpeter@2587
   218
    /// Returns the flow on the given edge.
kpeter@2587
   219
    ///
kpeter@2587
   220
    /// \pre \ref run() must be called before using this function.
kpeter@2587
   221
    Capacity flow(const Edge& edge) const {
kpeter@2587
   222
      return (*_flow)[edge];
kpeter@2587
   223
    }
kpeter@2587
   224
kpeter@2587
   225
    /// \brief Returns the potential of the given node.
kpeter@2587
   226
    ///
kpeter@2587
   227
    /// Returns the potential of the given node.
kpeter@2587
   228
    ///
kpeter@2587
   229
    /// \pre \ref run() must be called before using this function.
kpeter@2587
   230
    Cost potential(const Node& node) const {
kpeter@2587
   231
      return (*_potential)[node];
deba@2440
   232
    }
deba@2440
   233
deba@2440
   234
    /// \brief Returns the total cost of the found flow.
deba@2440
   235
    ///
deba@2440
   236
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   237
    /// function is \f$ O(e) \f$.
deba@2440
   238
    ///
deba@2440
   239
    /// \pre \ref run() must be called before using this function.
deba@2440
   240
    Cost totalCost() const {
deba@2440
   241
      Cost c = 0;
kpeter@2587
   242
      for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2587
   243
        c += (*_flow)[e] * _cost[e];
deba@2440
   244
      return c;
deba@2440
   245
    }
deba@2440
   246
kpeter@2587
   247
    /// @}
kpeter@2587
   248
deba@2440
   249
  }; //class MinCostMaxFlow
deba@2440
   250
deba@2440
   251
  ///@}
deba@2440
   252
deba@2440
   253
} //namespace lemon
deba@2440
   254
deba@2440
   255
#endif //LEMON_MIN_COST_MAX_FLOW_H