athos@276
|
1 |
// -*- c++ -*-
|
athos@306
|
2 |
#ifndef HUGO_MINLENGTHPATHS_H
|
athos@306
|
3 |
#define HUGO_MINLENGTHPATHS_H
|
athos@276
|
4 |
|
alpar@294
|
5 |
///\file
|
athos@306
|
6 |
///\brief An algorithm for finding k paths of minimal total length.
|
alpar@294
|
7 |
|
athos@276
|
8 |
#include <iostream>
|
athos@276
|
9 |
#include <dijkstra.h>
|
athos@276
|
10 |
#include <graph_wrapper.h>
|
athos@306
|
11 |
#include <maps.h>
|
athos@306
|
12 |
|
athos@276
|
13 |
namespace hugo {
|
athos@276
|
14 |
|
athos@276
|
15 |
|
klao@310
|
16 |
///\brief Implementation of an algorithm for finding k paths between 2 nodes
|
athos@306
|
17 |
/// of minimal total length
|
klao@310
|
18 |
///
|
klao@310
|
19 |
/// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
|
klao@310
|
20 |
/// an algorithm which finds k edge-disjoint paths
|
klao@310
|
21 |
/// from a given source node to a given target node in an
|
klao@310
|
22 |
/// edge-weighted directed graph having minimal total weigth (length).
|
athos@276
|
23 |
|
klao@310
|
24 |
template <typename Graph, typename LengthMap>
|
athos@306
|
25 |
class MinLengthPaths {
|
athos@276
|
26 |
|
klao@310
|
27 |
typedef typename LengthMap::ValueType Length;
|
athos@276
|
28 |
|
athos@276
|
29 |
typedef typename Graph::Node Node;
|
athos@276
|
30 |
typedef typename Graph::NodeIt NodeIt;
|
athos@276
|
31 |
typedef typename Graph::Edge Edge;
|
athos@276
|
32 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
athos@306
|
33 |
typedef typename Graph::EdgeMap<int> EdgeIntMap;
|
athos@306
|
34 |
|
athos@306
|
35 |
typedef ConstMap<Edge,int> ConstMap;
|
athos@306
|
36 |
|
klao@310
|
37 |
typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
|
athos@276
|
38 |
|
athos@306
|
39 |
|
athos@306
|
40 |
class ModLengthMap {
|
klao@310
|
41 |
typedef typename ResGraphType::NodeMap<Length> NodeMap;
|
athos@306
|
42 |
const ResGraphType& G;
|
klao@310
|
43 |
const EdgeIntMap& rev;
|
klao@310
|
44 |
const LengthMap &ol;
|
klao@310
|
45 |
const NodeMap &pot;
|
athos@306
|
46 |
public :
|
athos@306
|
47 |
typedef typename LengthMap::KeyType KeyType;
|
athos@306
|
48 |
typedef typename LengthMap::ValueType ValueType;
|
athos@306
|
49 |
|
athos@306
|
50 |
ValueType operator[](typename ResGraphType::Edge e) const {
|
athos@306
|
51 |
if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
|
athos@306
|
52 |
///\TODO This has to be removed
|
athos@306
|
53 |
std::cout<<"Negative length!!"<<std::endl;
|
athos@306
|
54 |
}
|
athos@306
|
55 |
return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
|
athos@306
|
56 |
}
|
athos@306
|
57 |
|
klao@310
|
58 |
ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
|
klao@310
|
59 |
const LengthMap &o, const NodeMap &p) :
|
athos@306
|
60 |
G(_G), rev(_rev), ol(o), pot(p){};
|
athos@306
|
61 |
};
|
athos@306
|
62 |
|
athos@306
|
63 |
|
athos@276
|
64 |
const Graph& G;
|
athos@276
|
65 |
const LengthMap& length;
|
athos@276
|
66 |
|
athos@306
|
67 |
//auxiliary variable
|
athos@306
|
68 |
//The value is 1 iff the edge is reversed
|
athos@306
|
69 |
EdgeIntMap reversed;
|
athos@276
|
70 |
|
athos@276
|
71 |
|
athos@276
|
72 |
public :
|
klao@310
|
73 |
|
athos@276
|
74 |
|
athos@306
|
75 |
MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
|
athos@306
|
76 |
length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
|
athos@276
|
77 |
|
athos@306
|
78 |
///Runs the algorithm
|
alpar@294
|
79 |
|
athos@306
|
80 |
///Runs the algorithm
|
athos@306
|
81 |
///Returns k if there are at least k edge-disjoint paths from s to t.
|
athos@306
|
82 |
///Otherwise it returns the number of edge-disjoint paths from s to t.
|
athos@306
|
83 |
int run(Node s, Node t, int k) {
|
athos@306
|
84 |
ConstMap const1map(1);
|
athos@276
|
85 |
|
athos@306
|
86 |
ResGraphType res_graph(G, reversed, const1map);
|
athos@306
|
87 |
|
athos@306
|
88 |
//Initialize the copy of the Dijkstra potential to zero
|
klao@310
|
89 |
typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
|
klao@310
|
90 |
ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
|
athos@306
|
91 |
|
athos@306
|
92 |
Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
|
athos@276
|
93 |
|
athos@276
|
94 |
for (int i=0; i<k; ++i){
|
athos@276
|
95 |
dijkstra.run(s);
|
athos@276
|
96 |
if (!dijkstra.reached(t)){
|
athos@276
|
97 |
//There is no k path from s to t
|
klao@310
|
98 |
/// \TODO mit keresett itt ez a ++?
|
klao@310
|
99 |
return i;
|
athos@276
|
100 |
};
|
athos@306
|
101 |
|
athos@306
|
102 |
{
|
athos@306
|
103 |
//We have to copy the potential
|
athos@306
|
104 |
typename ResGraphType::NodeIt n;
|
athos@306
|
105 |
for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
|
athos@306
|
106 |
dijkstra_dist[n] += dijkstra.distMap()[n];
|
athos@306
|
107 |
}
|
athos@306
|
108 |
}
|
athos@306
|
109 |
|
athos@306
|
110 |
|
athos@276
|
111 |
//Reversing the sortest path
|
athos@276
|
112 |
Node n=t;
|
athos@276
|
113 |
Edge e;
|
athos@276
|
114 |
while (n!=s){
|
athos@291
|
115 |
e = dijkstra.pred(n);
|
athos@291
|
116 |
n = dijkstra.predNode(n);
|
athos@276
|
117 |
reversed[e] = 1-reversed[e];
|
athos@276
|
118 |
}
|
athos@276
|
119 |
|
athos@276
|
120 |
|
athos@276
|
121 |
}
|
athos@306
|
122 |
return k;
|
athos@276
|
123 |
}
|
athos@276
|
124 |
|
athos@276
|
125 |
|
athos@276
|
126 |
|
athos@276
|
127 |
|
athos@276
|
128 |
|
klao@310
|
129 |
}; //class MinLengthPaths
|
athos@276
|
130 |
|
athos@276
|
131 |
|
athos@276
|
132 |
} //namespace hugo
|
athos@276
|
133 |
|
athos@306
|
134 |
#endif //HUGO_MINLENGTHPATHS_H
|