.
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).
26 template <typename Graph, typename T,
27 typename LengthMap=typename Graph::EdgeMap<T> >
28 class MinLengthPaths {
33 // typedef int ValueType;
34 // typedef typename Graph::Edge KeyType;
36 // int operator[](typename Graph::Edge e) const {
42 typedef typename Graph::Node Node;
43 typedef typename Graph::NodeIt NodeIt;
44 typedef typename Graph::Edge Edge;
45 typedef typename Graph::OutEdgeIt OutEdgeIt;
46 typedef typename Graph::EdgeMap<int> EdgeIntMap;
48 typedef ConstMap<Edge,int> ConstMap;
50 typedef TrivGraphWrapper<const Graph> TrivGraphType;
51 typedef ResGraphWrapper<TrivGraphType,int,EdgeIntMap,
52 ConstMap> ResGraphType;
55 //template <typename Graph, typename T>
57 typedef typename ResGraphType::NodeMap<T> NodeMap;
58 const ResGraphType& G;
59 const EdgeIntMap& rev;
63 typedef typename LengthMap::KeyType KeyType;
64 typedef typename LengthMap::ValueType ValueType;
66 ValueType operator[](typename ResGraphType::Edge e) const {
67 if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
68 ///\TODO This has to be removed
69 std::cout<<"Negative length!!"<<std::endl;
71 return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
74 ModLengthMap( const ResGraphType& _G, const EdgeIntMap& _rev,
75 const LengthMap &o, const NodeMap &p) :
76 G(_G), rev(_rev), ol(o), pot(p){};
81 const LengthMap& length;
84 //The value is 1 iff the edge is reversed
91 MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
92 length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
97 ///Returns k if there are at least k edge-disjoint paths from s to t.
98 ///Otherwise it returns the number of edge-disjoint paths from s to t.
99 int run(Node s, Node t, int k) {
100 ConstMap const1map(1);
102 ResGraphType res_graph(G, reversed, const1map);
104 //Initialize the copy of the Dijkstra potential to zero
105 typename ResGraphType::NodeMap<T> dijkstra_dist(G);
106 ModLengthMap mod_length( res_graph, reversed, length, dijkstra_dist);
108 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
110 for (int i=0; i<k; ++i){
112 if (!dijkstra.reached(t)){
113 //There is no k path from s to t
118 //We have to copy the potential
119 typename ResGraphType::NodeIt n;
120 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
121 dijkstra_dist[n] += dijkstra.distMap()[n];
128 //We have to copy the potential
129 typename ResGraphType::EdgeIt e;
130 for ( res_graph.first(e) ; res_graph.valid(e) ; res_graph.next(e) ) {
131 //dijkstra_dist[e] = dijkstra.distMap()[e];
132 mod_length_c[Edge(e)] = mod_length_c[Edge(e)] -
133 dijkstra.distMap()[res_graph.head(e)] +
134 dijkstra.distMap()[res_graph.tail(e)];
139 //Reversing the sortest path
143 e = dijkstra.pred(n);
144 n = dijkstra.predNode(n);
145 reversed[e] = 1-reversed[e];
157 };//class MinLengthPaths
164 #endif //HUGO_MINLENGTHPATHS_H