Some day this file will contain an erasable version of SmartGraph.
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>
19 ///\brief Implementation of an algorithm for finding k paths between 2 nodes
20 /// of minimal total length
22 /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
23 /// an algorithm which finds k edge-disjoint paths
24 /// from a given source node to a given target node in an
25 /// edge-weighted directed graph having minimal total weigth (length).
27 template <typename Graph, typename LengthMap>
28 class MinLengthPaths {
30 typedef typename LengthMap::ValueType Length;
32 typedef typename Graph::Node Node;
33 typedef typename Graph::NodeIt NodeIt;
34 typedef typename Graph::Edge Edge;
35 typedef typename Graph::OutEdgeIt OutEdgeIt;
36 typedef typename Graph::EdgeMap<int> EdgeIntMap;
38 typedef ConstMap<Edge,int> ConstMap;
40 typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
44 typedef typename ResGraphType::NodeMap<Length> NodeMap;
45 const ResGraphType& G;
46 const EdgeIntMap& rev;
50 typedef typename LengthMap::KeyType KeyType;
51 typedef typename LengthMap::ValueType ValueType;
53 ValueType operator[](typename ResGraphType::Edge e) const {
54 //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
55 // std::cout<<"Negative length!!"<<std::endl;
57 return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
60 ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
61 const LengthMap &o, const NodeMap &p) :
62 G(_G), rev(_rev), ol(o), pot(p){};
67 const LengthMap& length;
71 //The value is 1 iff the edge is reversed.
72 //If the algorithm has finished, the edges of the seeked paths are
73 //exactly those that are reversed
76 //Container to store found paths
77 std::vector< std::vector<Edge> > paths;
82 MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
83 length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
86 ///Runs the algorithm.
88 ///Runs the algorithm.
89 ///Returns k if there are at least k edge-disjoint paths from s to t.
90 ///Otherwise it returns the number of found edge-disjoint paths from s to t.
91 int run(Node s, Node t, int k) {
92 ConstMap const1map(1);
94 //We need a residual graph, in which some of the edges are reversed
95 ResGraphType res_graph(G, const1map, reversed);
97 //Initialize the copy of the Dijkstra potential to zero
98 typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
99 ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
101 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
106 if (!dijkstra.reached(t)){
107 //There are no k paths from s to t
112 //We have to copy the potential
113 typename ResGraphType::NodeIt n;
114 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
115 dijkstra_dist[n] += dijkstra.distMap()[n];
120 //Reversing the sortest path
124 e = dijkstra.pred(n);
125 n = dijkstra.predNode(n);
126 reversed[e] = 1-reversed[e];
132 //Let's find the paths
133 //We put the paths into vectors (just for now). In the meantime we lose
134 //the information stored in 'reversed'
135 //We suppose the lengths to be positive now.
138 for (int j=0; j<i; ++j){
147 while (!reversed[e]){
151 paths[j].push_back(e);
152 reversed[e] = 1-reversed[e];
161 }; //class MinLengthPaths
166 #endif //HUGO_MINLENGTHPATHS_H