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