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