lemon/min_cost_max_flow.h
author kpeter
Tue, 05 Feb 2008 11:03:55 +0000
changeset 2560 635e7985be46
parent 2553 bfced05fa852
child 2576 ae092c63d3ba
permissions -rw-r--r--
Fix the rename script.
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@2556
    43
  /// \note \ref MinCostMaxFlow uses \ref Preflow algorithm for finding
kpeter@2556
    44
  /// the maximum flow value and \ref NetworkSimplex algorithm for
kpeter@2556
    45
  /// finding a minimum cost flow of that value.
deba@2440
    46
  ///
deba@2440
    47
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    48
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    49
  /// \param CostMap The type of the cost (length) map.
deba@2440
    50
  ///
deba@2440
    51
  /// \warning
kpeter@2556
    52
  /// - Edge capacities and costs should be non-negative integers.
kpeter@2556
    53
  ///   However \c CostMap::Value should be signed type.
deba@2440
    54
  ///
deba@2440
    55
  /// \author Peter Kovacs
deba@2440
    56
kpeter@2533
    57
  template < typename Graph,
kpeter@2556
    58
             typename CapacityMap = typename Graph::template EdgeMap<int>,
kpeter@2556
    59
             typename CostMap = typename Graph::template EdgeMap<int> >
deba@2440
    60
  class MinCostMaxFlow
deba@2440
    61
  {
deba@2440
    62
    typedef typename Graph::Node Node;
deba@2440
    63
    typedef typename Graph::Edge Edge;
deba@2440
    64
deba@2440
    65
    typedef typename CapacityMap::Value Capacity;
kpeter@2556
    66
    typedef typename CostMap::Value Cost;
deba@2440
    67
    typedef typename Graph::template NodeMap<Capacity> SupplyMap;
deba@2440
    68
    typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
kpeter@2556
    69
                            CostMap, SupplyMap >
deba@2440
    70
      MinCostFlowImpl;
deba@2440
    71
deba@2440
    72
  public:
deba@2440
    73
kpeter@2556
    74
    /// The type of the flow map.
deba@2440
    75
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
deba@2440
    76
deba@2440
    77
  private:
deba@2440
    78
kpeter@2556
    79
    /// The directed graph the algorithm runs on.
deba@2440
    80
    const Graph &graph;
kpeter@2556
    81
    /// The modified capacity map.
deba@2440
    82
    const CapacityMap &capacity;
kpeter@2556
    83
    /// The cost map.
deba@2440
    84
    const CostMap &cost;
kpeter@2556
    85
    /// The edge map of the found flow.
kpeter@2556
    86
    FlowMap flow;
kpeter@2556
    87
    /// The source node.
deba@2440
    88
    Node source;
kpeter@2556
    89
    /// The target node.
deba@2440
    90
    Node target;
deba@2440
    91
kpeter@2556
    92
    typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
kpeter@2556
    93
    /// \brief \ref Preflow class for finding the maximum flow value.
kpeter@2556
    94
    MaxFlowImpl preflow;
deba@2440
    95
deba@2440
    96
  public:
deba@2440
    97
deba@2440
    98
    /// \brief The constructor of the class.
deba@2440
    99
    ///
deba@2440
   100
    /// The constructor of the class.
deba@2440
   101
    ///
deba@2440
   102
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   103
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   104
    /// \param _cost The cost (length) values of the edges.
deba@2440
   105
    /// \param _s The source node.
deba@2440
   106
    /// \param _t The target node.
deba@2440
   107
    MinCostMaxFlow( const Graph &_graph,
kpeter@2556
   108
                    const CapacityMap &_capacity,
kpeter@2556
   109
                    const CostMap &_cost,
kpeter@2556
   110
                    Node _s, Node _t ) :
deba@2440
   111
      graph(_graph), capacity(_capacity), cost(_cost),
deba@2440
   112
      source(_s), target(_t), flow(_graph),
deba@2515
   113
      preflow(_graph, _capacity, _s, _t)
deba@2440
   114
    {}
deba@2440
   115
deba@2440
   116
    /// \brief Returns a const reference to the flow map.
deba@2440
   117
    ///
deba@2440
   118
    /// Returns a const reference to the flow map.
deba@2440
   119
    ///
deba@2440
   120
    /// \pre \ref run() must be called before using this function.
deba@2440
   121
    const FlowMap& flowMap() const {
deba@2440
   122
      return flow;
deba@2440
   123
    }
deba@2440
   124
deba@2440
   125
    /// \brief Returns the total cost of the found flow.
deba@2440
   126
    ///
deba@2440
   127
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   128
    /// function is \f$ O(e) \f$.
deba@2440
   129
    ///
deba@2440
   130
    /// \pre \ref run() must be called before using this function.
deba@2440
   131
    Cost totalCost() const {
deba@2440
   132
      Cost c = 0;
kpeter@2507
   133
      for (typename Graph::EdgeIt e(graph); e != INVALID; ++e)
kpeter@2556
   134
        c += flow[e] * cost[e];
deba@2440
   135
      return c;
deba@2440
   136
    }
deba@2440
   137
deba@2440
   138
    /// \brief Runs the algorithm.
deba@2440
   139
    void run() {
deba@2515
   140
      preflow.flowMap(flow);
deba@2515
   141
      preflow.runMinCut();
deba@2440
   142
      MinCostFlowImpl mcf_impl( graph, capacity, cost,
kpeter@2556
   143
                                source, target, preflow.flowValue() );
deba@2440
   144
      mcf_impl.run();
deba@2440
   145
      flow = mcf_impl.flowMap();
deba@2440
   146
    }
deba@2440
   147
deba@2440
   148
  }; //class MinCostMaxFlow
deba@2440
   149
deba@2440
   150
  ///@}
deba@2440
   151
deba@2440
   152
} //namespace lemon
deba@2440
   153
deba@2440
   154
#endif //LEMON_MIN_COST_MAX_FLOW_H