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 alpar@921: #include alpar@921: #include alpar@921: #include athos@607: #include 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 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 EdgeIntMap; athos@601: athos@601: typedef ConstMap ConstMap; athos@601: athos@601: typedef ResGraphWrapper ResGraphType; athos@601: athos@601: class ModLengthMap { athos@601: typedef typename ResGraphType::template NodeMap 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!!"< > paths; athos@601: //typedef DirPath 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 dijkstra_dist(res_graph); athos@601: ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist); athos@601: athos@601: Dijkstra dijkstra(res_graph, mod_length); athos@601: athos@601: int i; athos@601: for (i=0; i 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::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