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