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