Sorry, the other half of the move comes here.
authorathos
Tue, 11 May 2004 16:15:18 +0000
changeset 6104ce8c695e748
parent 609 0566ac97809b
child 611 83530dad618a
Sorry, the other half of the move comes here.
src/hugo/mincostflows.h
src/hugo/minlengthpaths.h
src/test/minlengthpaths_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/hugo/mincostflows.h	Tue May 11 16:15:18 2004 +0000
     1.3 @@ -0,0 +1,254 @@
     1.4 +// -*- c++ -*-
     1.5 +#ifndef HUGO_MINCOSTFLOWS_H
     1.6 +#define HUGO_MINCOSTFLOWS_H
     1.7 +
     1.8 +///\ingroup galgs
     1.9 +///\file
    1.10 +///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
    1.11 +
    1.12 +#include <iostream>
    1.13 +#include <hugo/dijkstra.h>
    1.14 +#include <hugo/graph_wrapper.h>
    1.15 +#include <hugo/maps.h>
    1.16 +#include <vector>
    1.17 +#include <for_each_macros.h>
    1.18 +
    1.19 +namespace hugo {
    1.20 +
    1.21 +/// \addtogroup galgs
    1.22 +/// @{
    1.23 +
    1.24 +  ///\brief Implementation of an algorithm for finding a flow of value \c k 
    1.25 +  ///(for small values of \c k) having minimal total cost between 2 nodes 
    1.26 +  /// 
    1.27 +  ///
    1.28 +  /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
    1.29 +  /// an algorithm for finding a flow of value \c k 
    1.30 +  ///(for small values of \c k) having minimal total cost  
    1.31 +  /// from a given source node to a given target node in an
    1.32 +  /// edge-weighted directed graph having nonnegative integer capacities.
    1.33 +  /// The range of the length (weight) function is nonnegative reals but 
    1.34 +  /// the range of capacity function is the set of nonnegative integers. 
    1.35 +  /// It is not a polinomial time algorithm for counting the minimum cost
    1.36 +  /// maximal flow, since it counts the minimum cost flow for every value 0..M
    1.37 +  /// where \c M is the value of the maximal flow.
    1.38 +  ///
    1.39 +  ///\author Attila Bernath
    1.40 +  template <typename Graph, typename LengthMap, typename CapacityMap>
    1.41 +  class MinCostFlows {
    1.42 +
    1.43 +    typedef typename LengthMap::ValueType Length;
    1.44 +
    1.45 +    //Warning: this should be integer type
    1.46 +    typedef typename CapacityMap::ValueType Capacity;
    1.47 +    
    1.48 +    typedef typename Graph::Node Node;
    1.49 +    typedef typename Graph::NodeIt NodeIt;
    1.50 +    typedef typename Graph::Edge Edge;
    1.51 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.52 +    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    1.53 +
    1.54 +    //    typedef ConstMap<Edge,int> ConstMap;
    1.55 +
    1.56 +    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
    1.57 +    typedef typename ResGraphType::Edge ResGraphEdge;
    1.58 +
    1.59 +    class ModLengthMap {   
    1.60 +      //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
    1.61 +      typedef typename Graph::template NodeMap<Length> NodeMap;
    1.62 +      const ResGraphType& G;
    1.63 +      //      const EdgeIntMap& rev;
    1.64 +      const LengthMap &ol;
    1.65 +      const NodeMap &pot;
    1.66 +    public :
    1.67 +      typedef typename LengthMap::KeyType KeyType;
    1.68 +      typedef typename LengthMap::ValueType ValueType;
    1.69 +	
    1.70 +      ValueType operator[](typename ResGraphType::Edge e) const {     
    1.71 +	if (G.forward(e))
    1.72 +	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    1.73 +	else
    1.74 +	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    1.75 +      }     
    1.76 +	
    1.77 +      ModLengthMap(const ResGraphType& _G,
    1.78 +		   const LengthMap &o,  const NodeMap &p) : 
    1.79 +	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
    1.80 +    };//ModLengthMap
    1.81 +
    1.82 +
    1.83 +  protected:
    1.84 +    
    1.85 +    //Input
    1.86 +    const Graph& G;
    1.87 +    const LengthMap& length;
    1.88 +    const CapacityMap& capacity;
    1.89 +
    1.90 +
    1.91 +    //auxiliary variables
    1.92 +
    1.93 +    //To store the flow
    1.94 +    EdgeIntMap flow; 
    1.95 +    //To store the potentila (dual variables)
    1.96 +    typename Graph::template NodeMap<Length> potential;
    1.97 +    
    1.98 +    //Container to store found paths
    1.99 +    //std::vector< std::vector<Edge> > paths;
   1.100 +    //typedef DirPath<Graph> DPath;
   1.101 +    //DPath paths;
   1.102 +
   1.103 +
   1.104 +    Length total_length;
   1.105 +
   1.106 +
   1.107 +  public :
   1.108 +
   1.109 +
   1.110 +    MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
   1.111 +      length(_length), capacity(_cap), flow(_G), potential(_G){ }
   1.112 +
   1.113 +    
   1.114 +    ///Runs the algorithm.
   1.115 +
   1.116 +    ///Runs the algorithm.
   1.117 +    ///Returns k if there are at least k edge-disjoint paths from s to t.
   1.118 +    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
   1.119 +    ///\todo May be it does make sense to be able to start with a nonzero 
   1.120 +    /// feasible primal-dual solution pair as well.
   1.121 +    int run(Node s, Node t, int k) {
   1.122 +
   1.123 +      //Resetting variables from previous runs
   1.124 +      total_length = 0;
   1.125 +      
   1.126 +      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
   1.127 +	flow.set(e,0);
   1.128 +      }
   1.129 +      
   1.130 +      FOR_EACH_LOC(typename Graph::NodeIt, n, G){
   1.131 +	//cout << potential[n]<<endl;
   1.132 +	potential.set(n,0);
   1.133 +      }
   1.134 +      
   1.135 +
   1.136 +      
   1.137 +      //We need a residual graph
   1.138 +      ResGraphType res_graph(G, capacity, flow);
   1.139 +
   1.140 +      //Initialize the copy of the Dijkstra potential to zero
   1.141 +      
   1.142 +      //typename ResGraphType::template NodeMap<Length> potential(res_graph);
   1.143 +
   1.144 +
   1.145 +      ModLengthMap mod_length(res_graph, length, potential);
   1.146 +
   1.147 +      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
   1.148 +
   1.149 +      int i;
   1.150 +      for (i=0; i<k; ++i){
   1.151 +	dijkstra.run(s);
   1.152 +	if (!dijkstra.reached(t)){
   1.153 +	  //There are no k paths from s to t
   1.154 +	  break;
   1.155 +	};
   1.156 +	
   1.157 +	{
   1.158 +	  //We have to copy the potential
   1.159 +	  typename ResGraphType::NodeIt n;
   1.160 +	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
   1.161 +	      potential[n] += dijkstra.distMap()[n];
   1.162 +	  }
   1.163 +	}
   1.164 +
   1.165 +
   1.166 +	//Augmenting on the sortest path
   1.167 +	Node n=t;
   1.168 +	ResGraphEdge e;
   1.169 +	while (n!=s){
   1.170 +	  e = dijkstra.pred(n);
   1.171 +	  n = dijkstra.predNode(n);
   1.172 +	  res_graph.augment(e,1);
   1.173 +	  //Let's update the total length
   1.174 +	  if (res_graph.forward(e))
   1.175 +	    total_length += length[e];
   1.176 +	  else 
   1.177 +	    total_length -= length[e];	    
   1.178 +	}
   1.179 +
   1.180 +	  
   1.181 +      }
   1.182 +      
   1.183 +
   1.184 +      return i;
   1.185 +    }
   1.186 +
   1.187 +
   1.188 +
   1.189 +
   1.190 +    ///This function gives back the total length of the found paths.
   1.191 +    ///Assumes that \c run() has been run and nothing changed since then.
   1.192 +    Length totalLength(){
   1.193 +      return total_length;
   1.194 +    }
   1.195 +
   1.196 +    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   1.197 +    ///be called before using this function.
   1.198 +    const EdgeIntMap &getFlow() const { return flow;}
   1.199 +
   1.200 +  ///Returns a const reference to the NodeMap \c potential (the dual solution).
   1.201 +    /// \pre \ref run() must be called before using this function.
   1.202 +    const EdgeIntMap &getPotential() const { return potential;}
   1.203 +
   1.204 +    ///This function checks, whether the given solution is optimal
   1.205 +    ///Running after a \c run() should return with true
   1.206 +    ///In this "state of the art" this only check optimality, doesn't bother with feasibility
   1.207 +    ///
   1.208 +    ///\todo Is this OK here?
   1.209 +    bool checkComplementarySlackness(){
   1.210 +      Length mod_pot;
   1.211 +      Length fl_e;
   1.212 +      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
   1.213 +	//C^{\Pi}_{i,j}
   1.214 +	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
   1.215 +	fl_e = flow[e];
   1.216 +	//	std::cout << fl_e << std::endl;
   1.217 +	if (0<fl_e && fl_e<capacity[e]){
   1.218 +	  if (mod_pot != 0)
   1.219 +	    return false;
   1.220 +	}
   1.221 +	else{
   1.222 +	  if (mod_pot > 0 && fl_e != 0)
   1.223 +	    return false;
   1.224 +	  if (mod_pot < 0 && fl_e != capacity[e])
   1.225 +	    return false;
   1.226 +	}
   1.227 +      }
   1.228 +      return true;
   1.229 +    }
   1.230 +    
   1.231 +    /*
   1.232 +      ///\todo To be implemented later
   1.233 +
   1.234 +    ///This function gives back the \c j-th path in argument p.
   1.235 +    ///Assumes that \c run() has been run and nothing changed since then.
   1.236 +    /// \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.
   1.237 +    template<typename DirPath>
   1.238 +    void getPath(DirPath& p, int j){
   1.239 +      p.clear();
   1.240 +      typename DirPath::Builder B(p);
   1.241 +      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
   1.242 +	  i!=paths[j].end(); ++i ){
   1.243 +	B.pushBack(*i);
   1.244 +      }
   1.245 +
   1.246 +      B.commit();
   1.247 +    }
   1.248 +
   1.249 +    */
   1.250 +
   1.251 +  }; //class MinCostFlows
   1.252 +
   1.253 +  ///@}
   1.254 +
   1.255 +} //namespace hugo
   1.256 +
   1.257 +#endif //HUGO_MINCOSTFLOW_H
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/hugo/minlengthpaths.h	Tue May 11 16:15:18 2004 +0000
     2.3 @@ -0,0 +1,164 @@
     2.4 +// -*- c++ -*-
     2.5 +#ifndef HUGO_MINLENGTHPATHS_H
     2.6 +#define HUGO_MINLENGTHPATHS_H
     2.7 +
     2.8 +///\ingroup galgs
     2.9 +///\file
    2.10 +///\brief An algorithm for finding k paths of minimal total length.
    2.11 +
    2.12 +#include <iostream>
    2.13 +//#include <hugo/dijkstra.h>
    2.14 +//#include <hugo/graph_wrapper.h>
    2.15 +#include <hugo/maps.h>
    2.16 +#include <vector>
    2.17 +#include <hugo/mincostflows.h>
    2.18 +#include <for_each_macros.h>
    2.19 +
    2.20 +namespace hugo {
    2.21 +
    2.22 +/// \addtogroup galgs
    2.23 +/// @{
    2.24 +
    2.25 +  ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
    2.26 +  /// of minimal total length 
    2.27 +  ///
    2.28 +  /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
    2.29 +  /// an algorithm for finding k edge-disjoint paths
    2.30 +  /// from a given source node to a given target node in an
    2.31 +  /// edge-weighted directed graph having minimal total weigth (length).
    2.32 +  ///
    2.33 +  ///\warning It is assumed that the lengths are positive, since the
    2.34 +  /// general flow-decomposition is not implemented yet.
    2.35 +  ///
    2.36 +  ///\author Attila Bernath
    2.37 +  template <typename Graph, typename LengthMap>
    2.38 +  class MinLengthPaths{
    2.39 +
    2.40 +
    2.41 +    typedef typename LengthMap::ValueType Length;
    2.42 +    
    2.43 +    typedef typename Graph::Node Node;
    2.44 +    typedef typename Graph::NodeIt NodeIt;
    2.45 +    typedef typename Graph::Edge Edge;
    2.46 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
    2.47 +    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    2.48 +
    2.49 +    typedef ConstMap<Edge,int> ConstMap;
    2.50 +
    2.51 +    //Input
    2.52 +    const Graph& G;
    2.53 +
    2.54 +    //Auxiliary variables
    2.55 +    //This is the capacity map for the mincostflow problem
    2.56 +    ConstMap const1map;
    2.57 +    //This MinCostFlows instance will actually solve the problem
    2.58 +    MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
    2.59 +
    2.60 +    //Container to store found paths
    2.61 +    std::vector< std::vector<Edge> > paths;
    2.62 +
    2.63 +  public :
    2.64 +
    2.65 +
    2.66 +    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
    2.67 +      const1map(1), mincost_flow(_G, _length, const1map){}
    2.68 +
    2.69 +    ///Runs the algorithm.
    2.70 +
    2.71 +    ///Runs the algorithm.
    2.72 +    ///Returns k if there are at least k edge-disjoint paths from s to t.
    2.73 +   ///Otherwise it returns the number of found edge-disjoint paths from s to t.
    2.74 +    int run(Node s, Node t, int k) {
    2.75 +
    2.76 +      int i = mincost_flow.run(s,t,k);
    2.77 +      
    2.78 +
    2.79 +
    2.80 +      //Let's find the paths
    2.81 +      //We put the paths into stl vectors (as an inner representation). 
    2.82 +      //In the meantime we lose the information stored in 'reversed'.
    2.83 +      //We suppose the lengths to be positive now.
    2.84 +
    2.85 +      //We don't want to change the flow of mincost_flow, so we make a copy
    2.86 +      //The name here suggests that the flow has only 0/1 values.
    2.87 +      EdgeIntMap reversed(G); 
    2.88 +
    2.89 +      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
    2.90 +	reversed[e] = mincost_flow.getFlow()[e];
    2.91 +      }
    2.92 +      
    2.93 +      paths.clear();
    2.94 +      //total_length=0;
    2.95 +      paths.resize(k);
    2.96 +      for (int j=0; j<i; ++j){
    2.97 +	Node n=s;
    2.98 +	OutEdgeIt e;
    2.99 +
   2.100 +	while (n!=t){
   2.101 +
   2.102 +
   2.103 +	  G.first(e,n);
   2.104 +	  
   2.105 +	  while (!reversed[e]){
   2.106 +	    G.next(e);
   2.107 +	  }
   2.108 +	  n = G.head(e);
   2.109 +	  paths[j].push_back(e);
   2.110 +	  //total_length += length[e];
   2.111 +	  reversed[e] = 1-reversed[e];
   2.112 +	}
   2.113 +	
   2.114 +      }
   2.115 +      return i;
   2.116 +    }
   2.117 +
   2.118 +    
   2.119 +    ///This function gives back the total length of the found paths.
   2.120 +    ///Assumes that \c run() has been run and nothing changed since then.
   2.121 +    Length totalLength(){
   2.122 +      return mincost_flow.totalLength();
   2.123 +    }
   2.124 +
   2.125 +    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   2.126 +    ///be called before using this function.
   2.127 +    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
   2.128 +
   2.129 +  ///Returns a const reference to the NodeMap \c potential (the dual solution).
   2.130 +    /// \pre \ref run() must be called before using this function.
   2.131 +    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
   2.132 +
   2.133 +    ///This function checks, whether the given solution is optimal
   2.134 +    ///Running after a \c run() should return with true
   2.135 +    ///In this "state of the art" this only checks optimality, doesn't bother with feasibility
   2.136 +    ///
   2.137 +    ///\todo Is this OK here?
   2.138 +    bool checkComplementarySlackness(){
   2.139 +      return mincost_flow.checkComplementarySlackness();
   2.140 +    }
   2.141 +
   2.142 +    ///This function gives back the \c j-th path in argument p.
   2.143 +    ///Assumes that \c run() has been run and nothing changed since then.
   2.144 +    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is not less than the result of previous \c run, then the result here will be an empty path (\c j can be 0 as well).
   2.145 +    template<typename DirPath>
   2.146 +    void getPath(DirPath& p, size_t j){
   2.147 +      
   2.148 +      p.clear();
   2.149 +      if (j>paths.size()-1){
   2.150 +	return;
   2.151 +      }
   2.152 +      typename DirPath::Builder B(p);
   2.153 +      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
   2.154 +	  i!=paths[j].end(); ++i ){
   2.155 +	B.pushBack(*i);
   2.156 +      }
   2.157 +
   2.158 +      B.commit();
   2.159 +    }
   2.160 +
   2.161 +  }; //class MinLengthPaths
   2.162 +
   2.163 +  ///@}
   2.164 +
   2.165 +} //namespace hugo
   2.166 +
   2.167 +#endif //HUGO_MINLENGTHPATHS_H
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/test/minlengthpaths_test.cc	Tue May 11 16:15:18 2004 +0000
     3.3 @@ -0,0 +1,99 @@
     3.4 +#include <iostream>
     3.5 +#include <hugo/list_graph.h>
     3.6 +#include <hugo/minlengthpaths.h>
     3.7 +#include <path.h>
     3.8 +
     3.9 +using namespace std;
    3.10 +using namespace hugo;
    3.11 +
    3.12 +
    3.13 +
    3.14 +bool passed = true;
    3.15 +
    3.16 +void check(bool rc, char *msg="") {
    3.17 +  passed = passed && rc;
    3.18 +  if(!rc) {
    3.19 +    std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
    3.20 + 
    3.21 +
    3.22 +  }
    3.23 +}
    3.24 +
    3.25 +
    3.26 +
    3.27 +int main()
    3.28 +{
    3.29 +
    3.30 +  typedef ListGraph::Node Node;
    3.31 +  typedef ListGraph::Edge Edge;
    3.32 +
    3.33 +  ListGraph graph;
    3.34 +
    3.35 +  //Ahuja könyv példája
    3.36 +
    3.37 +  Node s=graph.addNode();
    3.38 +  Node v1=graph.addNode();  
    3.39 +  Node v2=graph.addNode();
    3.40 +  Node v3=graph.addNode();
    3.41 +  Node v4=graph.addNode();
    3.42 +  Node v5=graph.addNode();
    3.43 +  Node t=graph.addNode();
    3.44 +
    3.45 +  Edge s_v1=graph.addEdge(s, v1);
    3.46 +  Edge v1_v2=graph.addEdge(v1, v2);
    3.47 +  Edge s_v3=graph.addEdge(s, v3);
    3.48 +  Edge v2_v4=graph.addEdge(v2, v4);
    3.49 +  Edge v2_v5=graph.addEdge(v2, v5);
    3.50 +  Edge v3_v5=graph.addEdge(v3, v5);
    3.51 +  Edge v4_t=graph.addEdge(v4, t);
    3.52 +  Edge v5_t=graph.addEdge(v5, t);
    3.53 +  
    3.54 +
    3.55 +  ListGraph::EdgeMap<int> length(graph);
    3.56 +
    3.57 +  length.set(s_v1, 6);
    3.58 +  length.set(v1_v2, 4);
    3.59 +  length.set(s_v3, 10);
    3.60 +  length.set(v2_v4, 5);
    3.61 +  length.set(v2_v5, 1);
    3.62 +  length.set(v3_v5, 5);
    3.63 +  length.set(v4_t, 8);
    3.64 +  length.set(v5_t, 8);
    3.65 +
    3.66 +  std::cout << "Minlengthpaths algorithm test..." << std::endl;
    3.67 +
    3.68 +  
    3.69 +  int k=3;
    3.70 +  MinLengthPaths< ListGraph, ListGraph::EdgeMap<int> >
    3.71 +    surb_test(graph, length);
    3.72 +
    3.73 +  check(  surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 46,"Two paths, total length should be 46");
    3.74 +
    3.75 +  check(  surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met.");
    3.76 +
    3.77 +  typedef DirPath<ListGraph> DPath;
    3.78 +  DPath P(graph);
    3.79 +
    3.80 +  /*
    3.81 +  surb_test.getPath(P,0);
    3.82 +  check(P.length() == 4, "First path should contain 4 edges.");  
    3.83 +  cout<<P.length()<<endl;
    3.84 +  surb_test.getPath(P,1);
    3.85 +  check(P.length() == 3, "Second path: 3 edges.");
    3.86 +  cout<<P.length()<<endl;
    3.87 +  */  
    3.88 +
    3.89 +  k=1;
    3.90 +  check(  surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
    3.91 +
    3.92 +  check(  surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met.");
    3.93 + 
    3.94 +  surb_test.getPath(P,0);
    3.95 +  check(P.length() == 4, "First path should contain 4 edges.");  
    3.96 +
    3.97 +  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
    3.98 +       << endl;
    3.99 +
   3.100 +  return passed ? 0 : 1;
   3.101 +
   3.102 +}