src/hugo/mincostflows.h
author marci
Fri, 14 May 2004 15:01:21 +0000
changeset 638 2153dd45937a
parent 633 305bd9c56f10
child 661 d306e777117e
permissions -rw-r--r--
for_eachmacros.h in include
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@610
    14
#include <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@610
    93
    typename Graph::template NodeMap<Length> potential;
athos@610
    94
    
athos@610
    95
athos@610
    96
    Length total_length;
athos@610
    97
athos@610
    98
athos@610
    99
  public :
athos@610
   100
athos@610
   101
athos@610
   102
    MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
athos@610
   103
      length(_length), capacity(_cap), flow(_G), potential(_G){ }
athos@610
   104
athos@610
   105
    
athos@610
   106
    ///Runs the algorithm.
athos@610
   107
athos@610
   108
    ///Runs the algorithm.
athos@610
   109
    ///Returns k if there are at least k edge-disjoint paths from s to t.
athos@610
   110
    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
athos@610
   111
    ///\todo May be it does make sense to be able to start with a nonzero 
athos@610
   112
    /// feasible primal-dual solution pair as well.
athos@610
   113
    int run(Node s, Node t, int k) {
athos@610
   114
athos@610
   115
      //Resetting variables from previous runs
athos@610
   116
      total_length = 0;
athos@610
   117
      
athos@610
   118
      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
athos@610
   119
	flow.set(e,0);
athos@610
   120
      }
athos@634
   121
athos@634
   122
      //Initialize the potential to zero
athos@610
   123
      FOR_EACH_LOC(typename Graph::NodeIt, n, G){
athos@610
   124
	potential.set(n,0);
athos@610
   125
      }
athos@610
   126
      
athos@610
   127
athos@610
   128
      
athos@610
   129
      //We need a residual graph
athos@610
   130
      ResGraphType res_graph(G, capacity, flow);
athos@610
   131
athos@610
   132
athos@610
   133
      ModLengthMap mod_length(res_graph, length, potential);
athos@610
   134
athos@610
   135
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@610
   136
athos@610
   137
      int i;
athos@610
   138
      for (i=0; i<k; ++i){
athos@610
   139
	dijkstra.run(s);
athos@610
   140
	if (!dijkstra.reached(t)){
athos@610
   141
	  //There are no k paths from s to t
athos@610
   142
	  break;
athos@610
   143
	};
athos@610
   144
	
athos@634
   145
	//We have to change the potential
athos@633
   146
	FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
athos@633
   147
	  potential[n] += dijkstra.distMap()[n];
athos@633
   148
	}
athos@634
   149
athos@610
   150
athos@610
   151
	//Augmenting on the sortest path
athos@610
   152
	Node n=t;
athos@610
   153
	ResGraphEdge e;
athos@610
   154
	while (n!=s){
athos@610
   155
	  e = dijkstra.pred(n);
athos@610
   156
	  n = dijkstra.predNode(n);
athos@610
   157
	  res_graph.augment(e,1);
athos@610
   158
	  //Let's update the total length
athos@610
   159
	  if (res_graph.forward(e))
athos@610
   160
	    total_length += length[e];
athos@610
   161
	  else 
athos@610
   162
	    total_length -= length[e];	    
athos@610
   163
	}
athos@610
   164
athos@610
   165
	  
athos@610
   166
      }
athos@610
   167
      
athos@610
   168
athos@610
   169
      return i;
athos@610
   170
    }
athos@610
   171
athos@610
   172
athos@610
   173
athos@610
   174
athos@610
   175
    ///This function gives back the total length of the found paths.
athos@610
   176
    ///Assumes that \c run() has been run and nothing changed since then.
athos@610
   177
    Length totalLength(){
athos@610
   178
      return total_length;
athos@610
   179
    }
athos@610
   180
athos@610
   181
    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
athos@610
   182
    ///be called before using this function.
athos@610
   183
    const EdgeIntMap &getFlow() const { return flow;}
athos@610
   184
athos@610
   185
  ///Returns a const reference to the NodeMap \c potential (the dual solution).
athos@610
   186
    /// \pre \ref run() must be called before using this function.
athos@610
   187
    const EdgeIntMap &getPotential() const { return potential;}
athos@610
   188
athos@610
   189
    ///This function checks, whether the given solution is optimal
athos@610
   190
    ///Running after a \c run() should return with true
athos@610
   191
    ///In this "state of the art" this only check optimality, doesn't bother with feasibility
athos@610
   192
    ///
athos@610
   193
    ///\todo Is this OK here?
athos@610
   194
    bool checkComplementarySlackness(){
athos@610
   195
      Length mod_pot;
athos@610
   196
      Length fl_e;
athos@610
   197
      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
athos@610
   198
	//C^{\Pi}_{i,j}
athos@610
   199
	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
athos@610
   200
	fl_e = flow[e];
athos@610
   201
	//	std::cout << fl_e << std::endl;
athos@610
   202
	if (0<fl_e && fl_e<capacity[e]){
athos@610
   203
	  if (mod_pot != 0)
athos@610
   204
	    return false;
athos@610
   205
	}
athos@610
   206
	else{
athos@610
   207
	  if (mod_pot > 0 && fl_e != 0)
athos@610
   208
	    return false;
athos@610
   209
	  if (mod_pot < 0 && fl_e != capacity[e])
athos@610
   210
	    return false;
athos@610
   211
	}
athos@610
   212
      }
athos@610
   213
      return true;
athos@610
   214
    }
athos@610
   215
    
athos@610
   216
athos@610
   217
  }; //class MinCostFlows
athos@610
   218
athos@610
   219
  ///@}
athos@610
   220
athos@610
   221
} //namespace hugo
athos@610
   222
athos@633
   223
#endif //HUGO_MINCOSTFLOWS_H