athos@276: // -*- c++ -*- athos@306: #ifndef HUGO_MINLENGTHPATHS_H athos@306: #define HUGO_MINLENGTHPATHS_H athos@276: alpar@294: ///\file athos@306: ///\brief An algorithm for finding k paths of minimal total length. alpar@294: athos@276: #include athos@276: #include athos@276: #include athos@306: #include athos@322: #include athos@322: athos@306: athos@276: namespace hugo { athos@276: athos@276: athos@322: klao@310: ///\brief Implementation of an algorithm for finding k paths between 2 nodes athos@306: /// of minimal total length klao@310: /// klao@310: /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements klao@310: /// an algorithm which finds k edge-disjoint paths klao@310: /// from a given source node to a given target node in an klao@310: /// edge-weighted directed graph having minimal total weigth (length). athos@276: klao@310: template athos@306: class MinLengthPaths { athos@276: klao@310: typedef typename LengthMap::ValueType Length; athos@276: athos@276: typedef typename Graph::Node Node; athos@276: typedef typename Graph::NodeIt NodeIt; athos@276: typedef typename Graph::Edge Edge; athos@276: typedef typename Graph::OutEdgeIt OutEdgeIt; athos@306: typedef typename Graph::EdgeMap EdgeIntMap; athos@306: athos@306: typedef ConstMap ConstMap; athos@306: klao@310: typedef ResGraphWrapper ResGraphType; athos@276: athos@306: athos@306: class ModLengthMap { klao@310: typedef typename ResGraphType::NodeMap NodeMap; athos@306: const ResGraphType& G; klao@310: const EdgeIntMap& rev; klao@310: const LengthMap &ol; klao@310: const NodeMap &pot; athos@306: public : athos@306: typedef typename LengthMap::KeyType KeyType; athos@306: typedef typename LengthMap::ValueType ValueType; athos@306: athos@306: ValueType operator[](typename ResGraphType::Edge e) const { athos@322: //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){ athos@322: // std::cout<<"Negative length!!"< > paths; athos@322: athos@276: public : klao@310: athos@276: athos@306: MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), athos@306: length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ } athos@276: alpar@294: alpar@329: ///Runs the algorithm. alpar@329: alpar@329: ///Runs the algorithm. athos@306: ///Returns k if there are at least k edge-disjoint paths from s to t. alpar@329: ///Otherwise it returns the number of found edge-disjoint paths from s to t. athos@306: int run(Node s, Node t, int k) { athos@306: ConstMap const1map(1); athos@276: athos@314: //We need a residual graph, in which some of the edges are reversed athos@306: ResGraphType res_graph(G, reversed, const1map); athos@306: athos@306: //Initialize the copy of the Dijkstra potential to zero klao@310: typename ResGraphType::NodeMap dijkstra_dist(res_graph); klao@310: ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist); athos@306: athos@306: Dijkstra dijkstra(res_graph, mod_length); athos@322: athos@322: int i; athos@322: for (i=0; i