athos@276: // -*- c++ -*- athos@306: #ifndef HUGO_MINLENGTHPATHS_H athos@306: #define HUGO_MINLENGTHPATHS_H athos@276: alpar@430: ///ingroup galgs alpar@294: ///\file athos@306: ///\brief An algorithm for finding k paths of minimal total length. alpar@294: athos@276: #include <iostream> athos@276: #include <dijkstra.h> athos@276: #include <graph_wrapper.h> athos@306: #include <maps.h> athos@322: #include <vector> athos@322: athos@306: athos@276: namespace hugo { athos@276: alpar@430: /// \addtogroup galgs alpar@430: /// @{ 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 <typename Graph, typename LengthMap> 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<int> EdgeIntMap; athos@306: athos@306: typedef ConstMap<Edge,int> ConstMap; athos@306: marci@330: typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType; athos@276: athos@306: athos@306: class ModLengthMap { klao@310: typedef typename ResGraphType::NodeMap<Length> 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!!"<<std::endl; athos@322: //} athos@306: return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); athos@306: } athos@306: klao@310: ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, klao@310: const LengthMap &o, const NodeMap &p) : athos@306: G(_G), rev(_rev), ol(o), pot(p){}; athos@306: }; athos@306: athos@306: athos@276: const Graph& G; athos@276: const LengthMap& length; athos@276: alpar@328: //auxiliary variables athos@322: athos@314: //The value is 1 iff the edge is reversed. athos@314: //If the algorithm has finished, the edges of the seeked paths are athos@314: //exactly those that are reversed athos@306: EdgeIntMap reversed; athos@276: athos@322: //Container to store found paths athos@322: std::vector< std::vector<Edge> > 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 marci@330: ResGraphType res_graph(G, const1map, reversed); athos@306: athos@306: //Initialize the copy of the Dijkstra potential to zero klao@310: typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph); klao@310: ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist); athos@306: athos@306: Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length); athos@322: athos@322: int i; athos@322: for (i=0; i<k; ++i){ athos@276: dijkstra.run(s); athos@276: if (!dijkstra.reached(t)){ athos@314: //There are no k paths from s to t athos@322: break; athos@276: }; athos@306: athos@306: { athos@306: //We have to copy the potential athos@306: typename ResGraphType::NodeIt n; athos@306: for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) { athos@306: dijkstra_dist[n] += dijkstra.distMap()[n]; athos@306: } athos@306: } athos@306: athos@306: athos@276: //Reversing the sortest path athos@276: Node n=t; athos@276: Edge e; athos@276: while (n!=s){ athos@291: e = dijkstra.pred(n); athos@291: n = dijkstra.predNode(n); athos@276: reversed[e] = 1-reversed[e]; athos@276: } athos@276: athos@276: athos@276: } athos@322: athos@322: //Let's find the paths athos@322: //We put the paths into vectors (just for now). In the meantime we lose athos@322: //the information stored in 'reversed' athos@322: //We suppose the lengths to be positive now. athos@322: paths.clear(); athos@322: paths.resize(k); athos@322: for (int j=0; j<i; ++j){ athos@322: Node n=s; athos@322: OutEdgeIt e; athos@322: athos@322: while (n!=t){ athos@322: athos@322: athos@322: G.first(e,n); athos@322: athos@322: while (!reversed[e]){ athos@322: G.next(e); athos@322: } athos@322: n = G.head(e); athos@322: paths[j].push_back(e); athos@322: reversed[e] = 1-reversed[e]; athos@322: } athos@322: athos@322: } athos@322: athos@322: return i; athos@276: } athos@276: athos@276: klao@310: }; //class MinLengthPaths athos@276: alpar@430: ///@} athos@276: athos@276: } //namespace hugo athos@276: athos@306: #endif //HUGO_MINLENGTHPATHS_H