athos@601: // -*- c++ -*-
alpar@921: #ifndef LEMON_MINLENGTHPATHS_H
alpar@921: #define LEMON_MINLENGTHPATHS_H
athos@601: 
athos@601: ///\ingroup galgs
athos@601: ///\file
athos@601: ///\brief An algorithm for finding k paths of minimal total length.
athos@601: 
athos@601: #include <iostream>
alpar@921: #include <lemon/dijkstra.h>
alpar@921: #include <lemon/graph_wrapper.h>
alpar@921: #include <lemon/maps.h>
athos@607: #include <vector>
athos@601: 
athos@601: 
alpar@921: namespace lemon {
athos@601: 
athos@601: /// \addtogroup galgs
athos@601: /// @{
athos@601: 
athos@601:   ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
athos@601:   /// of minimal total length 
athos@601:   ///
alpar@921:   /// The class \ref lemon::MinLengthPaths "MinLengthPaths" implements
athos@601:   /// an algorithm for finding k edge-disjoint paths
athos@601:   /// from a given source node to a given target node in an
athos@601:   /// edge-weighted directed graph having minimal total weigth (length).
athos@601:   ///
athos@601:   ///\author Attila Bernath
athos@601:   template <typename Graph, typename LengthMap>
athos@601:   class MinLengthPaths {
athos@601: 
alpar@987:     typedef typename LengthMap::Value Length;
athos@601:     
athos@601:     typedef typename Graph::Node Node;
athos@601:     typedef typename Graph::NodeIt NodeIt;
athos@601:     typedef typename Graph::Edge Edge;
athos@601:     typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@601:     typedef typename Graph::template EdgeMap<int> EdgeIntMap;
athos@601: 
athos@601:     typedef ConstMap<Edge,int> ConstMap;
athos@601: 
athos@601:     typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
athos@601: 
athos@601:     class ModLengthMap {   
athos@601:       typedef typename ResGraphType::template NodeMap<Length> NodeMap;
athos@601:       const ResGraphType& G;
athos@601:       const EdgeIntMap& rev;
athos@601:       const LengthMap &ol;
athos@601:       const NodeMap &pot;
athos@601:     public :
alpar@987:       typedef typename LengthMap::Key Key;
alpar@987:       typedef typename LengthMap::Value Value;
athos@601: 	
alpar@987:       Value operator[](typename ResGraphType::Edge e) const {     
alpar@986: 	//if ( (1-2*rev[e])*ol[e]-(pot[G.target(e)]-pot[G.source(e)] ) <0 ){
athos@601: 	//  std::cout<<"Negative length!!"<<std::endl;
athos@601: 	//}
alpar@986: 	return (1-2*rev[e])*ol[e]-(pot[G.target(e)]-pot[G.source(e)]);   
athos@601:       }     
athos@601: 	
athos@601:       ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
athos@601: 		   const LengthMap &o,  const NodeMap &p) : 
athos@601: 	G(_G), rev(_rev), ol(o), pot(p){}; 
athos@601:     };//ModLengthMap
athos@601: 
athos@601: 
athos@601:     
athos@601: 
athos@601:     const Graph& G;
athos@601:     const LengthMap& length;
athos@601: 
athos@601:     //auxiliary variables
athos@601: 
athos@601:     //The value is 1 iff the edge is reversed. 
athos@601:     //If the algorithm has finished, the edges of the seeked paths are 
athos@601:     //exactly those that are reversed 
athos@601:     EdgeIntMap reversed; 
athos@601:     
athos@601:     //Container to store found paths
athos@601:     std::vector< std::vector<Edge> > paths;
athos@601:     //typedef DirPath<Graph> DPath;
athos@601:     //DPath paths;
athos@601: 
athos@601: 
athos@601:     Length total_length;
athos@601: 
athos@601:   public :
athos@601: 
athos@601: 
athos@601:     MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
athos@601:       length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
athos@601: 
athos@601:     
athos@601:     ///Runs the algorithm.
athos@601: 
athos@601:     ///Runs the algorithm.
athos@601:     ///Returns k if there are at least k edge-disjoint paths from s to t.
athos@601:     ///Otherwise it returns the number of found edge-disjoint paths from s to t.
athos@601:     int run(Node s, Node t, int k) {
athos@601:       ConstMap const1map(1);
athos@601: 
athos@601: 
athos@601:       //We need a residual graph, in which some of the edges are reversed
athos@601:       ResGraphType res_graph(G, const1map, reversed);
athos@601: 
athos@601:       //Initialize the copy of the Dijkstra potential to zero
athos@601:       typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
athos@601:       ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
athos@601: 
athos@601:       Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@601: 
athos@601:       int i;
athos@601:       for (i=0; i<k; ++i){
athos@601: 	dijkstra.run(s);
athos@601: 	if (!dijkstra.reached(t)){
athos@601: 	  //There are no k paths from s to t
athos@601: 	  break;
athos@601: 	};
athos@601: 	
athos@601: 	{
athos@601: 	  //We have to copy the potential
athos@601: 	  typename ResGraphType::NodeIt n;
athos@601: 	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
athos@601: 	      dijkstra_dist[n] += dijkstra.distMap()[n];
athos@601: 	  }
athos@601: 	}
athos@601: 
athos@601: 
athos@601: 	//Reversing the sortest path
athos@601: 	Node n=t;
athos@601: 	Edge e;
athos@601: 	while (n!=s){
athos@601: 	  e = dijkstra.pred(n);
athos@601: 	  n = dijkstra.predNode(n);
athos@601: 	  reversed[e] = 1-reversed[e];
athos@601: 	}
athos@601: 
athos@601: 	  
athos@601:       }
athos@601:       
athos@601:       //Let's find the paths
athos@601:       //We put the paths into stl vectors (as an inner representation). 
athos@601:       //In the meantime we lose the information stored in 'reversed'.
athos@601:       //We suppose the lengths to be positive now.
athos@601: 
athos@601:       //Meanwhile we put the total length of the found paths 
athos@601:       //in the member variable total_length
athos@601:       paths.clear();
athos@601:       total_length=0;
athos@601:       paths.resize(k);
athos@601:       for (int j=0; j<i; ++j){
athos@601: 	Node n=s;
athos@601: 	OutEdgeIt e;
athos@601: 
athos@601: 	while (n!=t){
athos@601: 
athos@601: 
athos@601: 	  G.first(e,n);
athos@601: 	  
athos@601: 	  while (!reversed[e]){
athos@601: 	    G.next(e);
athos@601: 	  }
alpar@986: 	  n = G.target(e);
athos@601: 	  paths[j].push_back(e);
athos@601: 	  total_length += length[e];
athos@601: 	  reversed[e] = 1-reversed[e];
athos@601: 	}
athos@601: 	
athos@601:       }
athos@601: 
athos@601:       return i;
athos@601:     }
athos@601: 
athos@601:     ///This function gives back the total length of the found paths.
athos@601:     ///Assumes that \c run() has been run and nothing changed since then.
athos@601:     Length totalLength(){
athos@601:       return total_length;
athos@601:     }
athos@601: 
athos@601:     ///This function gives back the \c j-th path in argument p.
athos@601:     ///Assumes that \c run() has been run and nothing changed since then.
athos@601:     /// \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@601:     template<typename DirPath>
athos@601:     void getPath(DirPath& p, int j){
athos@601:       p.clear();
athos@601:       typename DirPath::Builder B(p);
athos@601:       for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
athos@601: 	  i!=paths[j].end(); ++i ){
athos@601: 	B.pushBack(*i);
athos@601:       }
athos@601: 
athos@601:       B.commit();
athos@601:     }
athos@601: 
athos@601:   }; //class MinLengthPaths
athos@601: 
athos@601:   ///@}
athos@601: 
alpar@921: } //namespace lemon
athos@601: 
alpar@921: #endif //LEMON_MINLENGTHPATHS_H