2 #ifndef HUGO_MINLENGTHPATHS_H
3 #define HUGO_MINLENGTHPATHS_H
6 ///\brief An algorithm for finding k paths of minimal total length.
10 #include <graph_wrapper.h>
16 ///\brief Implementation of an algorithm for finding k paths between 2 nodes
17 /// of minimal total length
19 /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
20 /// an algorithm which finds k edge-disjoint paths
21 /// from a given source node to a given target node in an
22 /// edge-weighted directed graph having minimal total weigth (length).
24 template <typename Graph, typename LengthMap>
25 class MinLengthPaths {
27 typedef typename LengthMap::ValueType Length;
29 typedef typename Graph::Node Node;
30 typedef typename Graph::NodeIt NodeIt;
31 typedef typename Graph::Edge Edge;
32 typedef typename Graph::OutEdgeIt OutEdgeIt;
33 typedef typename Graph::EdgeMap<int> EdgeIntMap;
35 typedef ConstMap<Edge,int> ConstMap;
37 typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
41 typedef typename ResGraphType::NodeMap<Length> NodeMap;
42 const ResGraphType& G;
43 const EdgeIntMap& rev;
47 typedef typename LengthMap::KeyType KeyType;
48 typedef typename LengthMap::ValueType ValueType;
50 ValueType operator[](typename ResGraphType::Edge e) const {
51 if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
52 ///\TODO This has to be removed
53 std::cout<<"Negative length!!"<<std::endl;
55 return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
58 ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
59 const LengthMap &o, const NodeMap &p) :
60 G(_G), rev(_rev), ol(o), pot(p){};
65 const LengthMap& length;
68 //The value is 1 iff the edge is reversed
75 MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
76 length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
81 ///Returns k if there are at least k edge-disjoint paths from s to t.
82 ///Otherwise it returns the number of edge-disjoint paths from s to t.
83 int run(Node s, Node t, int k) {
84 ConstMap const1map(1);
86 ResGraphType res_graph(G, reversed, const1map);
88 //Initialize the copy of the Dijkstra potential to zero
89 typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
90 ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
92 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
94 for (int i=0; i<k; ++i){
96 if (!dijkstra.reached(t)){
97 //There is no k path from s to t
98 /// \TODO mit keresett itt ez a ++?
103 //We have to copy the potential
104 typename ResGraphType::NodeIt n;
105 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
106 dijkstra_dist[n] += dijkstra.distMap()[n];
111 //Reversing the sortest path
115 e = dijkstra.pred(n);
116 n = dijkstra.predNode(n);
117 reversed[e] = 1-reversed[e];
129 }; //class MinLengthPaths
134 #endif //HUGO_MINLENGTHPATHS_H