src/hugo/mincostflows.h
changeset 879 5e284075b193
parent 788 c3187cafcabf
child 893 89d5c283a485
equal deleted inserted replaced
8:46c9b06aad62 9:256be900925e
    21   ///(for small values of \c k) having minimal total cost between 2 nodes 
    21   ///(for small values of \c k) having minimal total cost between 2 nodes 
    22   /// 
    22   /// 
    23   ///
    23   ///
    24   /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
    24   /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
    25   /// an algorithm for finding a flow of value \c k 
    25   /// an algorithm for finding a flow of value \c k 
    26   ///(for small values of \c k) having minimal total cost  
    26   /// having minimal total cost  
    27   /// from a given source node to a given target node in an
    27   /// from a given source node to a given target node in an
    28   /// edge-weighted directed graph having nonnegative integer capacities.
    28   /// edge-weighted directed graph having nonnegative integer capacities.
    29   /// The range of the length (weight) function is nonnegative reals but 
    29   /// The range of the length (weight or cost) function can be nonnegative reals but 
    30   /// the range of capacity function is the set of nonnegative integers. 
    30   /// the range of the capacity function has to be the set of nonnegative integers.
    31   /// It is not a polinomial time algorithm for counting the minimum cost
    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
    32   /// maximal flow, since it counts the minimum cost flow for every value 0..M
    32   /// maximal flow (in order to find the minimum cost flow of value \c k it 
    33   /// where \c M is the value of the maximal flow.
    33   /// finds the minimum cost flow of value \c i for every 
       
    34   /// \c i between 0 and \c k). 
       
    35   ///
       
    36   ///\param Graph The directed graph type the algorithm runs on.
       
    37   ///\param LengthMap The type of the length map.
       
    38   ///\param CapacityMap The capacity map type.
    34   ///
    39   ///
    35   ///\author Attila Bernath
    40   ///\author Attila Bernath
    36   template <typename Graph, typename LengthMap, typename CapacityMap>
    41   template <typename Graph, typename LengthMap, typename CapacityMap>
    37   class MinCostFlows {
    42   class MinCostFlows {
       
    43 
       
    44 
    38 
    45 
    39     typedef typename LengthMap::ValueType Length;
    46     typedef typename LengthMap::ValueType Length;
    40 
    47 
    41     //Warning: this should be integer type
    48     //Warning: this should be integer type
    42     typedef typename CapacityMap::ValueType Capacity;
    49     typedef typename CapacityMap::ValueType Capacity;
    45     typedef typename Graph::NodeIt NodeIt;
    52     typedef typename Graph::NodeIt NodeIt;
    46     typedef typename Graph::Edge Edge;
    53     typedef typename Graph::Edge Edge;
    47     typedef typename Graph::OutEdgeIt OutEdgeIt;
    54     typedef typename Graph::OutEdgeIt OutEdgeIt;
    48     typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    55     typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    49 
    56 
    50     //    typedef ConstMap<Edge,int> ConstMap;
       
    51 
    57 
    52     typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
    58     typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
    53     typedef typename ResGraphType::Edge ResGraphEdge;
    59     typedef typename ResGraphType::Edge ResGraphEdge;
    54 
    60 
    55     class ModLengthMap {   
    61     class ModLengthMap {   
    56       //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
       
    57       typedef typename Graph::template NodeMap<Length> NodeMap;
    62       typedef typename Graph::template NodeMap<Length> NodeMap;
    58       const ResGraphType& G;
    63       const ResGraphType& G;
    59       //      const EdgeIntMap& rev;
       
    60       const LengthMap &ol;
    64       const LengthMap &ol;
    61       const NodeMap &pot;
    65       const NodeMap &pot;
    62     public :
    66     public :
    63       typedef typename LengthMap::KeyType KeyType;
    67       typedef typename LengthMap::KeyType KeyType;
    64       typedef typename LengthMap::ValueType ValueType;
    68       typedef typename LengthMap::ValueType ValueType;
    96     Length total_length;
   100     Length total_length;
    97 
   101 
    98 
   102 
    99   public :
   103   public :
   100 
   104 
   101 
   105     /// The constructor of the class.
       
   106     
       
   107     ///\param _G The directed graph the algorithm runs on. 
       
   108     ///\param _length The length (weight or cost) of the edges. 
       
   109     ///\param _cap The capacity of the edges. 
   102     MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
   110     MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
   103       length(_length), capacity(_cap), flow(_G), potential(_G){ }
   111       length(_length), capacity(_cap), flow(_G), potential(_G){ }
   104 
   112 
   105     
   113     
   106     ///Runs the algorithm.
   114     ///Runs the algorithm.
   107 
   115     
   108     ///Runs the algorithm.
   116     ///Runs the algorithm.
   109     ///Returns k if there are at least k edge-disjoint paths from s to t.
   117     ///Returns k if there is a flow of value at least k edge-disjoint 
   110     ///Otherwise it returns the number of found edge-disjoint paths from s to t.
   118     ///from s to t.
       
   119     ///Otherwise it returns the maximum value of a flow from s to t.
       
   120     ///
       
   121     ///\param s The source node.
       
   122     ///\param t The target node.
       
   123     ///\param k The value of the flow we are looking for.
       
   124     ///
   111     ///\todo May be it does make sense to be able to start with a nonzero 
   125     ///\todo May be it does make sense to be able to start with a nonzero 
   112     /// feasible primal-dual solution pair as well.
   126     /// feasible primal-dual solution pair as well.
   113     int run(Node s, Node t, int k) {
   127     int run(Node s, Node t, int k) {
   114 
   128 
   115       //Resetting variables from previous runs
   129       //Resetting variables from previous runs
   131 
   145 
   132       int i;
   146       int i;
   133       for (i=0; i<k; ++i){
   147       for (i=0; i<k; ++i){
   134 	dijkstra.run(s);
   148 	dijkstra.run(s);
   135 	if (!dijkstra.reached(t)){
   149 	if (!dijkstra.reached(t)){
   136 	  //There are no k paths from s to t
   150 	  //There are no flow of value k from s to t
   137 	  break;
   151 	  break;
   138 	};
   152 	};
   139 	
   153 	
   140 	//We have to change the potential
   154 	//We have to change the potential
   141         for(typename ResGraphType::NodeIt n(res_graph); n!=INVALID; ++n)
   155         for(typename ResGraphType::NodeIt n(res_graph); n!=INVALID; ++n)
   163       return i;
   177       return i;
   164     }
   178     }
   165 
   179 
   166 
   180 
   167 
   181 
   168 
   182     /// Gives back the total weight of the found flow.
   169     ///This function gives back the total length of the found paths.
   183 
       
   184     ///This function gives back the total weight of the found flow.
   170     ///Assumes that \c run() has been run and nothing changed since then.
   185     ///Assumes that \c run() has been run and nothing changed since then.
   171     Length totalLength(){
   186     Length totalLength(){
   172       return total_length;
   187       return total_length;
   173     }
   188     }
   174 
   189 
   175     ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   190     ///Returns a const reference to the EdgeMap \c flow. 
       
   191 
       
   192     ///Returns a const reference to the EdgeMap \c flow. 
       
   193     ///\pre \ref run() must
   176     ///be called before using this function.
   194     ///be called before using this function.
   177     const EdgeIntMap &getFlow() const { return flow;}
   195     const EdgeIntMap &getFlow() const { return flow;}
   178 
   196 
   179   ///Returns a const reference to the NodeMap \c potential (the dual solution).
   197     ///Returns a const reference to the NodeMap \c potential (the dual solution).
       
   198 
       
   199     ///Returns a const reference to the NodeMap \c potential (the dual solution).
   180     /// \pre \ref run() must be called before using this function.
   200     /// \pre \ref run() must be called before using this function.
   181     const PotentialMap &getPotential() const { return potential;}
   201     const PotentialMap &getPotential() const { return potential;}
   182 
   202 
       
   203     /// Checking the complementary slackness optimality criteria
       
   204 
   183     ///This function checks, whether the given solution is optimal
   205     ///This function checks, whether the given solution is optimal
   184     ///Running after a \c run() should return with true
   206     ///If executed after the call of \c run() then it should return with true.
   185     ///In this "state of the art" this only check optimality, doesn't bother with feasibility
   207     ///This function only checks optimality, doesn't bother with feasibility.
       
   208     ///It is meant for testing purposes.
   186     ///
   209     ///
   187     ///\todo Is this OK here?
       
   188     bool checkComplementarySlackness(){
   210     bool checkComplementarySlackness(){
   189       Length mod_pot;
   211       Length mod_pot;
   190       Length fl_e;
   212       Length fl_e;
   191         for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) {
   213         for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) {
   192 	//C^{\Pi}_{i,j}
   214 	//C^{\Pi}_{i,j}