lemon/min_cost_max_flow.h
author kpeter
Thu, 13 Sep 2007 22:06:54 +0000
changeset 2471 ed70b226cc48
child 2507 6520edb2c3f3
permissions -rw-r--r--
Small changes in min. cost flow algorithms.
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_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
deba@2440
    25
/// \brief An efficient algorithm for finding a minimum cost maximum
deba@2440
    26
/// flow.
deba@2440
    27
deba@2440
    28
#include <lemon/preflow.h>
deba@2440
    29
#include <lemon/network_simplex.h>
deba@2440
    30
#include <lemon/maps.h>
deba@2440
    31
deba@2440
    32
namespace lemon {
deba@2440
    33
deba@2440
    34
  /// \addtogroup min_cost_flow
deba@2440
    35
  /// @{
deba@2440
    36
deba@2440
    37
  /// \brief An efficient algorithm for finding a minimum cost maximum
deba@2440
    38
  /// flow.
deba@2440
    39
  ///
deba@2440
    40
  /// \ref lemon::MinCostFlow "MinCostMaxFlow" implements an efficient
deba@2440
    41
  /// algorithm for finding a maximum flow having minimal total cost
deba@2440
    42
  /// from a given source node to a given target node in a directed
deba@2440
    43
  /// graph.
deba@2440
    44
  ///
deba@2440
    45
  /// \note \ref lemon::MinCostMaxFlow "MinCostMaxFlow" uses
deba@2440
    46
  /// \ref lemon::Preflow "Preflow" algorithm for finding the maximum
deba@2440
    47
  /// flow value and \ref lemon::NetworkSimplex "NetworkSimplex" 
deba@2440
    48
  /// algorithm for finding a minimum cost flow of that value.
deba@2440
    49
  ///
deba@2440
    50
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    51
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    52
  /// \param CostMap The type of the cost (length) map.
deba@2440
    53
  ///
deba@2440
    54
  /// \warning
deba@2440
    55
  /// - Edge capacities and costs should be nonnegative integers.
deba@2440
    56
  ///	However \c CostMap::Value should be signed type.
deba@2440
    57
  ///
deba@2440
    58
  /// \author Peter Kovacs
deba@2440
    59
deba@2440
    60
template < typename Graph,
deba@2440
    61
	   typename CapacityMap = typename Graph::template EdgeMap<int>,
deba@2440
    62
	   typename CostMap = typename Graph::template EdgeMap<int> >
deba@2440
    63
  class MinCostMaxFlow
deba@2440
    64
  {
deba@2440
    65
    typedef typename Graph::Node Node;
deba@2440
    66
    typedef typename Graph::Edge Edge;
deba@2440
    67
deba@2440
    68
    typedef typename CapacityMap::Value Capacity;
deba@2440
    69
    typedef typename Graph::template NodeMap<Capacity> SupplyMap;
deba@2440
    70
    typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
deba@2440
    71
			    CostMap, SupplyMap >
deba@2440
    72
      MinCostFlowImpl;
deba@2440
    73
deba@2440
    74
  public:
deba@2440
    75
deba@2440
    76
    /// \brief The type of the flow map.
deba@2440
    77
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
deba@2440
    78
deba@2440
    79
  private:
deba@2440
    80
deba@2440
    81
    /// \brief The directed graph the algorithm runs on.
deba@2440
    82
    const Graph &graph;
deba@2440
    83
    /// \brief The modified capacity map.
deba@2440
    84
    const CapacityMap &capacity;
deba@2440
    85
    /// \brief The cost map.
deba@2440
    86
    const CostMap &cost;
deba@2440
    87
    /// \brief The source node.
deba@2440
    88
    Node source;
deba@2440
    89
    /// \brief The target node.
deba@2440
    90
    Node target;
deba@2440
    91
    /// \brief The edge map of the found flow.
deba@2440
    92
    FlowMap flow;
deba@2440
    93
deba@2440
    94
    typedef Preflow<Graph, Capacity, CapacityMap, FlowMap> PreflowImpl;
deba@2440
    95
    /// \brief \ref lemon::Preflow "Preflow" class for finding the
deba@2440
    96
    /// maximum flow value.
deba@2440
    97
    PreflowImpl preflow;
deba@2440
    98
deba@2440
    99
  public:
deba@2440
   100
deba@2440
   101
    /// \brief The constructor of the class.
deba@2440
   102
    ///
deba@2440
   103
    /// The constructor of the class.
deba@2440
   104
    ///
deba@2440
   105
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   106
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   107
    /// \param _cost The cost (length) values of the edges.
deba@2440
   108
    /// \param _s The source node.
deba@2440
   109
    /// \param _t The target node.
deba@2440
   110
    MinCostMaxFlow( const Graph &_graph,
deba@2440
   111
		    const CapacityMap &_capacity,
deba@2440
   112
		    const CostMap &_cost,
deba@2440
   113
		    Node _s, Node _t ) :
deba@2440
   114
      graph(_graph), capacity(_capacity), cost(_cost),
deba@2440
   115
      source(_s), target(_t), flow(_graph),
deba@2440
   116
      preflow(_graph, _s, _t, _capacity, flow)
deba@2440
   117
    {}
deba@2440
   118
deba@2440
   119
    /// \brief Returns a const reference to the flow map.
deba@2440
   120
    ///
deba@2440
   121
    /// Returns a const reference to the flow map.
deba@2440
   122
    ///
deba@2440
   123
    /// \pre \ref run() must be called before using this function.
deba@2440
   124
    const FlowMap& flowMap() const {
deba@2440
   125
      return flow;
deba@2440
   126
    }
deba@2440
   127
deba@2440
   128
    /// \brief Returns the total cost of the found flow.
deba@2440
   129
    ///
deba@2440
   130
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   131
    /// function is \f$ O(e) \f$.
deba@2440
   132
    ///
deba@2440
   133
    /// \pre \ref run() must be called before using this function.
deba@2440
   134
    Cost totalCost() const {
deba@2440
   135
      Cost c = 0;
deba@2440
   136
      for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   137
	c += flow[e] * cost[e];
deba@2440
   138
      return c;
deba@2440
   139
    }
deba@2440
   140
deba@2440
   141
    /// \brief Runs the algorithm.
deba@2440
   142
    void run() {
deba@2440
   143
      preflow.phase1();
deba@2440
   144
      MinCostFlowImpl mcf_impl( graph, capacity, cost,
deba@2440
   145
				source, target, preflow.flowValue() );
deba@2440
   146
      mcf_impl.run();
deba@2440
   147
      flow = mcf_impl.flowMap();
deba@2440
   148
    }
deba@2440
   149
deba@2440
   150
  }; //class MinCostMaxFlow
deba@2440
   151
deba@2440
   152
  ///@}
deba@2440
   153
deba@2440
   154
} //namespace lemon
deba@2440
   155
deba@2440
   156
#endif //LEMON_MIN_COST_MAX_FLOW_H