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