src/lemon/min_cost_flow.h
author marci
Wed, 29 Sep 2004 19:02:26 +0000
changeset 923 acbef5dd0e65
parent 910 5a89cacf17f1
child 941 186aa53d2802
permissions -rw-r--r--
more docs
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/min_cost_flow.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_MIN_COST_FLOW_H
alpar@921
    18
#define LEMON_MIN_COST_FLOW_H
alpar@899
    19
alpar@899
    20
///\ingroup flowalgs
alpar@899
    21
///\file
alpar@899
    22
///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
alpar@899
    23
alpar@899
    24
alpar@921
    25
#include <lemon/dijkstra.h>
alpar@921
    26
#include <lemon/graph_wrapper.h>
alpar@921
    27
#include <lemon/maps.h>
alpar@899
    28
#include <vector>
alpar@899
    29
alpar@921
    30
namespace lemon {
alpar@899
    31
alpar@899
    32
/// \addtogroup flowalgs
alpar@899
    33
/// @{
alpar@899
    34
alpar@899
    35
  ///\brief Implementation of an algorithm for finding a flow of value \c k 
alpar@899
    36
  ///(for small values of \c k) having minimal total cost between 2 nodes 
alpar@899
    37
  /// 
alpar@899
    38
  ///
alpar@921
    39
  /// The class \ref lemon::MinCostFlow "MinCostFlow" implements
alpar@899
    40
  /// an algorithm for finding a flow of value \c k 
alpar@899
    41
  /// having minimal total cost 
alpar@899
    42
  /// from a given source node to a given target node in an
alpar@899
    43
  /// edge-weighted directed graph. To this end, 
alpar@899
    44
  /// the edge-capacities and edge-weitghs have to be nonnegative. 
alpar@899
    45
  /// The edge-capacities should be integers, but the edge-weights can be 
alpar@899
    46
  /// integers, reals or of other comparable numeric type.
alpar@899
    47
  /// This algorithm is intended to use only for small values of \c k, 
alpar@899
    48
  /// since it is only polynomial in k, 
alpar@899
    49
  /// not in the length of k (which is log k). 
alpar@899
    50
  /// In order to find the minimum cost flow of value \c k it 
alpar@899
    51
  /// finds the minimum cost flow of value \c i for every 
alpar@899
    52
  /// \c i between 0 and \c k. 
alpar@899
    53
  ///
alpar@899
    54
  ///\param Graph The directed graph type the algorithm runs on.
alpar@899
    55
  ///\param LengthMap The type of the length map.
alpar@899
    56
  ///\param CapacityMap The capacity map type.
alpar@899
    57
  ///
alpar@899
    58
  ///\author Attila Bernath
alpar@899
    59
  template <typename Graph, typename LengthMap, typename CapacityMap>
alpar@899
    60
  class MinCostFlow {
alpar@899
    61
alpar@899
    62
    typedef typename LengthMap::ValueType Length;
alpar@899
    63
alpar@899
    64
    //Warning: this should be integer type
alpar@899
    65
    typedef typename CapacityMap::ValueType Capacity;
alpar@899
    66
    
alpar@899
    67
    typedef typename Graph::Node Node;
alpar@899
    68
    typedef typename Graph::NodeIt NodeIt;
alpar@899
    69
    typedef typename Graph::Edge Edge;
alpar@899
    70
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@899
    71
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
alpar@899
    72
alpar@899
    73
marci@910
    74
    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGW;
marci@910
    75
    typedef typename ResGW::Edge ResGraphEdge;
alpar@899
    76
alpar@899
    77
    class ModLengthMap {   
alpar@899
    78
      typedef typename Graph::template NodeMap<Length> NodeMap;
marci@910
    79
      const ResGW& G;
alpar@899
    80
      const LengthMap &ol;
alpar@899
    81
      const NodeMap &pot;
alpar@899
    82
    public :
alpar@899
    83
      typedef typename LengthMap::KeyType KeyType;
alpar@899
    84
      typedef typename LengthMap::ValueType ValueType;
alpar@899
    85
	
marci@910
    86
      ValueType operator[](typename ResGW::Edge e) const {     
alpar@899
    87
	if (G.forward(e))
alpar@899
    88
	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
alpar@899
    89
	else
alpar@899
    90
	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
alpar@899
    91
      }     
alpar@899
    92
	
marci@910
    93
      ModLengthMap(const ResGW& _G,
alpar@899
    94
		   const LengthMap &o,  const NodeMap &p) : 
alpar@899
    95
	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
alpar@899
    96
    };//ModLengthMap
alpar@899
    97
alpar@899
    98
alpar@899
    99
  protected:
alpar@899
   100
    
alpar@899
   101
    //Input
alpar@899
   102
    const Graph& G;
alpar@899
   103
    const LengthMap& length;
alpar@899
   104
    const CapacityMap& capacity;
alpar@899
   105
alpar@899
   106
alpar@899
   107
    //auxiliary variables
alpar@899
   108
alpar@899
   109
    //To store the flow
alpar@899
   110
    EdgeIntMap flow; 
alpar@899
   111
    //To store the potential (dual variables)
alpar@899
   112
    typedef typename Graph::template NodeMap<Length> PotentialMap;
alpar@899
   113
    PotentialMap potential;
alpar@899
   114
    
alpar@899
   115
alpar@899
   116
    Length total_length;
alpar@899
   117
alpar@899
   118
alpar@899
   119
  public :
alpar@899
   120
alpar@899
   121
    /// The constructor of the class.
alpar@899
   122
    
alpar@899
   123
    ///\param _G The directed graph the algorithm runs on. 
alpar@899
   124
    ///\param _length The length (weight or cost) of the edges. 
alpar@899
   125
    ///\param _cap The capacity of the edges. 
alpar@899
   126
    MinCostFlow(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
alpar@899
   127
      length(_length), capacity(_cap), flow(_G), potential(_G){ }
alpar@899
   128
alpar@899
   129
    
alpar@899
   130
    ///Runs the algorithm.
alpar@899
   131
    
alpar@899
   132
    ///Runs the algorithm.
alpar@899
   133
    ///Returns k if there is a flow of value at least k edge-disjoint 
alpar@899
   134
    ///from s to t.
alpar@899
   135
    ///Otherwise it returns the maximum value of a flow from s to t.
alpar@899
   136
    ///
alpar@899
   137
    ///\param s The source node.
alpar@899
   138
    ///\param t The target node.
alpar@899
   139
    ///\param k The value of the flow we are looking for.
alpar@899
   140
    ///
alpar@899
   141
    ///\todo May be it does make sense to be able to start with a nonzero 
alpar@899
   142
    /// feasible primal-dual solution pair as well.
alpar@899
   143
    int run(Node s, Node t, int k) {
alpar@899
   144
alpar@899
   145
      //Resetting variables from previous runs
alpar@899
   146
      total_length = 0;
alpar@899
   147
      
alpar@899
   148
      for (typename Graph::EdgeIt e(G); e!=INVALID; ++e) flow.set(e, 0);
alpar@899
   149
alpar@899
   150
      //Initialize the potential to zero
alpar@899
   151
      for (typename Graph::NodeIt n(G); n!=INVALID; ++n) potential.set(n, 0);
alpar@899
   152
      
alpar@899
   153
      
alpar@899
   154
      //We need a residual graph
marci@910
   155
      ResGW res_graph(G, capacity, flow);
alpar@899
   156
alpar@899
   157
alpar@899
   158
      ModLengthMap mod_length(res_graph, length, potential);
alpar@899
   159
marci@910
   160
      Dijkstra<ResGW, ModLengthMap> dijkstra(res_graph, mod_length);
alpar@899
   161
alpar@899
   162
      int i;
alpar@899
   163
      for (i=0; i<k; ++i){
alpar@899
   164
	dijkstra.run(s);
alpar@899
   165
	if (!dijkstra.reached(t)){
alpar@899
   166
	  //There are no flow of value k from s to t
alpar@899
   167
	  break;
alpar@899
   168
	};
alpar@899
   169
	
alpar@899
   170
	//We have to change the potential
marci@910
   171
        for(typename ResGW::NodeIt n(res_graph); n!=INVALID; ++n)
alpar@899
   172
	  potential[n] += dijkstra.distMap()[n];
alpar@899
   173
alpar@899
   174
alpar@899
   175
	//Augmenting on the sortest path
alpar@899
   176
	Node n=t;
alpar@899
   177
	ResGraphEdge e;
alpar@899
   178
	while (n!=s){
alpar@899
   179
	  e = dijkstra.pred(n);
alpar@899
   180
	  n = dijkstra.predNode(n);
alpar@899
   181
	  res_graph.augment(e,1);
alpar@899
   182
	  //Let's update the total length
alpar@899
   183
	  if (res_graph.forward(e))
alpar@899
   184
	    total_length += length[e];
alpar@899
   185
	  else 
alpar@899
   186
	    total_length -= length[e];	    
alpar@899
   187
	}
alpar@899
   188
alpar@899
   189
	  
alpar@899
   190
      }
alpar@899
   191
      
alpar@899
   192
alpar@899
   193
      return i;
alpar@899
   194
    }
alpar@899
   195
alpar@899
   196
alpar@899
   197
alpar@899
   198
    /// Gives back the total weight of the found flow.
alpar@899
   199
alpar@899
   200
    ///This function gives back the total weight of the found flow.
alpar@899
   201
    ///Assumes that \c run() has been run and nothing changed since then.
alpar@899
   202
    Length totalLength(){
alpar@899
   203
      return total_length;
alpar@899
   204
    }
alpar@899
   205
alpar@899
   206
    ///Returns a const reference to the EdgeMap \c flow. 
alpar@899
   207
alpar@899
   208
    ///Returns a const reference to the EdgeMap \c flow. 
alpar@899
   209
    ///\pre \ref run() must
alpar@899
   210
    ///be called before using this function.
alpar@899
   211
    const EdgeIntMap &getFlow() const { return flow;}
alpar@899
   212
alpar@899
   213
    ///Returns a const reference to the NodeMap \c potential (the dual solution).
alpar@899
   214
alpar@899
   215
    ///Returns a const reference to the NodeMap \c potential (the dual solution).
alpar@899
   216
    /// \pre \ref run() must be called before using this function.
alpar@899
   217
    const PotentialMap &getPotential() const { return potential;}
alpar@899
   218
alpar@899
   219
    /// Checking the complementary slackness optimality criteria
alpar@899
   220
alpar@899
   221
    ///This function checks, whether the given solution is optimal
alpar@899
   222
    ///If executed after the call of \c run() then it should return with true.
alpar@899
   223
    ///This function only checks optimality, doesn't bother with feasibility.
alpar@899
   224
    ///It is meant for testing purposes.
alpar@899
   225
    ///
alpar@899
   226
    bool checkComplementarySlackness(){
alpar@899
   227
      Length mod_pot;
alpar@899
   228
      Length fl_e;
alpar@899
   229
        for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) {
alpar@899
   230
	//C^{\Pi}_{i,j}
alpar@899
   231
	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
alpar@899
   232
	fl_e = flow[e];
alpar@899
   233
	if (0<fl_e && fl_e<capacity[e]) {
alpar@899
   234
	  /// \todo better comparison is needed for real types, moreover, 
alpar@899
   235
	  /// this comparison here is superfluous.
alpar@899
   236
	  if (mod_pot != 0)
alpar@899
   237
	    return false;
alpar@899
   238
	} 
alpar@899
   239
	else {
alpar@899
   240
	  if (mod_pot > 0 && fl_e != 0)
alpar@899
   241
	    return false;
alpar@899
   242
	  if (mod_pot < 0 && fl_e != capacity[e])
alpar@899
   243
	    return false;
alpar@899
   244
	}
alpar@899
   245
      }
alpar@899
   246
      return true;
alpar@899
   247
    }
alpar@899
   248
    
alpar@899
   249
alpar@899
   250
  }; //class MinCostFlow
alpar@899
   251
alpar@899
   252
  ///@}
alpar@899
   253
alpar@921
   254
} //namespace lemon
alpar@899
   255
alpar@921
   256
#endif //LEMON_MIN_COST_FLOW_H