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.
69 //If the algorithm has finished, the edges of the seeked paths are
70 //exactly those that are reversed
76 MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
77 length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
82 ///Returns k if there are at least k edge-disjoint paths from s to t.
83 ///Otherwise it returns the number of edge-disjoint paths from s to t.
84 int run(Node s, Node t, int k) {
85 ConstMap const1map(1);
87 //We need a residual graph, in which some of the edges are reversed
88 ResGraphType res_graph(G, reversed, const1map);
90 //Initialize the copy of the Dijkstra potential to zero
91 typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
92 ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
94 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
96 for (int i=0; i<k; ++i){
98 if (!dijkstra.reached(t)){
99 //There are no k paths from s to t
104 //We have to copy the potential
105 typename ResGraphType::NodeIt n;
106 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
107 dijkstra_dist[n] += dijkstra.distMap()[n];
112 //Reversing the sortest path
116 e = dijkstra.pred(n);
117 n = dijkstra.predNode(n);
118 reversed[e] = 1-reversed[e];
130 }; //class MinLengthPaths
135 #endif //HUGO_MINLENGTHPATHS_H