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