lemon/min_cost_max_flow.h
author kpeter
Mon, 25 Feb 2008 12:35:06 +0000
changeset 2579 691ce54544c5
parent 2576 ae092c63d3ba
child 2582 4f1ac622bb7a
permissions -rw-r--r--
Bug fixes in min cost flow files.
Use enum type instead of static constants in NetworkSimplex to avoid
linker errors.
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
  ///   However \c CostMap::Value must be signed type.
kpeter@2576
    58
  /// - \c CapacityMap::Value must be convertible to \c CostMap::Value.
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
  {
deba@2440
    67
    typedef typename Graph::Node Node;
deba@2440
    68
    typedef typename Graph::Edge Edge;
deba@2440
    69
deba@2440
    70
    typedef typename CapacityMap::Value Capacity;
kpeter@2556
    71
    typedef typename CostMap::Value Cost;
kpeter@2576
    72
    typedef typename Graph::template NodeMap<Cost> SupplyMap;
kpeter@2576
    73
kpeter@2576
    74
    typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
deba@2440
    75
    typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
kpeter@2576
    76
                            CostMap, SupplyMap > MinCostFlowImpl;
deba@2440
    77
deba@2440
    78
  public:
deba@2440
    79
kpeter@2556
    80
    /// The type of the flow map.
deba@2440
    81
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
kpeter@2576
    82
    /// The type of the potential map.
kpeter@2576
    83
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
    84
deba@2440
    85
  private:
deba@2440
    86
kpeter@2576
    87
    // The directed graph the algorithm runs on
kpeter@2576
    88
    const Graph &_graph;
kpeter@2576
    89
    // The modified capacity map
kpeter@2576
    90
    const CapacityMap &_capacity;
kpeter@2576
    91
    // The cost map
kpeter@2576
    92
    const CostMap &_cost;
deba@2440
    93
kpeter@2576
    94
    // Edge map of the found flow
kpeter@2576
    95
    FlowMap _flow;
kpeter@2576
    96
    // Node map of the found potentials
kpeter@2576
    97
    PotentialMap _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
deba@2440
   106
    /// \brief The constructor of the class.
deba@2440
   107
    ///
deba@2440
   108
    /// The constructor of the class.
deba@2440
   109
    ///
deba@2440
   110
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   111
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   112
    /// \param _cost The cost (length) values of the edges.
deba@2440
   113
    /// \param _s The source node.
deba@2440
   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@2576
   119
      _graph(graph), _capacity(capacity), _cost(cost), _flow(graph),
kpeter@2576
   120
      _potential(graph), _source(s), _target(t)
deba@2440
   121
    {}
deba@2440
   122
kpeter@2576
   123
    /// \brief Runs the algorithm.
deba@2440
   124
    ///
kpeter@2576
   125
    /// Runs the algorithm.
kpeter@2576
   126
    void run() {
kpeter@2576
   127
      MaxFlowImpl preflow(_graph, _capacity, _source, _target);
kpeter@2576
   128
      preflow.flowMap(_flow).runMinCut();
kpeter@2576
   129
      MinCostFlowImpl mcf( _graph, _capacity, _cost,
kpeter@2576
   130
                           _source, _target, preflow.flowValue() );
kpeter@2576
   131
      mcf.run();
kpeter@2576
   132
      _flow = mcf.flowMap();
kpeter@2576
   133
      _potential = mcf.potentialMap();
kpeter@2576
   134
    }
kpeter@2576
   135
kpeter@2576
   136
    /// \brief Returns a const reference to the edge map storing the
kpeter@2576
   137
    /// found flow.
kpeter@2576
   138
    ///
kpeter@2576
   139
    /// Returns a const reference to the edge map storing the found flow.
deba@2440
   140
    ///
deba@2440
   141
    /// \pre \ref run() must be called before using this function.
deba@2440
   142
    const FlowMap& flowMap() const {
kpeter@2579
   143
      return _flow;
kpeter@2576
   144
    }
kpeter@2576
   145
kpeter@2576
   146
    /// \brief Returns a const reference to the node map storing the
kpeter@2576
   147
    /// found potentials (the dual solution).
kpeter@2576
   148
    ///
kpeter@2576
   149
    /// Returns a const reference to the node map storing the found
kpeter@2576
   150
    /// potentials (the dual solution).
kpeter@2576
   151
    ///
kpeter@2576
   152
    /// \pre \ref run() must be called before using this function.
kpeter@2576
   153
    const PotentialMap& potentialMap() const {
kpeter@2579
   154
      return _potential;
deba@2440
   155
    }
deba@2440
   156
deba@2440
   157
    /// \brief Returns the total cost of the found flow.
deba@2440
   158
    ///
deba@2440
   159
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   160
    /// function is \f$ O(e) \f$.
deba@2440
   161
    ///
deba@2440
   162
    /// \pre \ref run() must be called before using this function.
deba@2440
   163
    Cost totalCost() const {
deba@2440
   164
      Cost c = 0;
kpeter@2579
   165
      for (typename Graph::EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2576
   166
        c += _flow[e] * _cost[e];
deba@2440
   167
      return c;
deba@2440
   168
    }
deba@2440
   169
deba@2440
   170
  }; //class MinCostMaxFlow
deba@2440
   171
deba@2440
   172
  ///@}
deba@2440
   173
deba@2440
   174
} //namespace lemon
deba@2440
   175
deba@2440
   176
#endif //LEMON_MIN_COST_MAX_FLOW_H