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