athos@610
|
1 |
// -*- c++ -*-
|
athos@610
|
2 |
#ifndef HUGO_MINLENGTHPATHS_H
|
athos@610
|
3 |
#define HUGO_MINLENGTHPATHS_H
|
athos@610
|
4 |
|
alpar@759
|
5 |
///\ingroup flowalgs
|
athos@610
|
6 |
///\file
|
athos@610
|
7 |
///\brief An algorithm for finding k paths of minimal total length.
|
athos@610
|
8 |
|
athos@611
|
9 |
|
athos@610
|
10 |
//#include <hugo/dijkstra.h>
|
athos@610
|
11 |
//#include <hugo/graph_wrapper.h>
|
athos@610
|
12 |
#include <hugo/maps.h>
|
athos@610
|
13 |
#include <vector>
|
athos@610
|
14 |
#include <hugo/mincostflows.h>
|
athos@610
|
15 |
#include <for_each_macros.h>
|
athos@610
|
16 |
|
athos@610
|
17 |
namespace hugo {
|
athos@610
|
18 |
|
alpar@759
|
19 |
/// \addtogroup flowalgs
|
athos@610
|
20 |
/// @{
|
athos@610
|
21 |
|
athos@610
|
22 |
///\brief Implementation of an algorithm for finding k paths between 2 nodes
|
athos@610
|
23 |
/// of minimal total length
|
athos@610
|
24 |
///
|
athos@610
|
25 |
/// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
|
athos@610
|
26 |
/// an algorithm for finding k edge-disjoint paths
|
athos@610
|
27 |
/// from a given source node to a given target node in an
|
athos@610
|
28 |
/// edge-weighted directed graph having minimal total weigth (length).
|
athos@610
|
29 |
///
|
athos@610
|
30 |
///\warning It is assumed that the lengths are positive, since the
|
athos@610
|
31 |
/// general flow-decomposition is not implemented yet.
|
athos@610
|
32 |
///
|
athos@610
|
33 |
///\author Attila Bernath
|
athos@610
|
34 |
template <typename Graph, typename LengthMap>
|
athos@610
|
35 |
class MinLengthPaths{
|
athos@610
|
36 |
|
athos@610
|
37 |
|
athos@610
|
38 |
typedef typename LengthMap::ValueType Length;
|
athos@610
|
39 |
|
athos@610
|
40 |
typedef typename Graph::Node Node;
|
athos@610
|
41 |
typedef typename Graph::NodeIt NodeIt;
|
athos@610
|
42 |
typedef typename Graph::Edge Edge;
|
athos@610
|
43 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
athos@610
|
44 |
typedef typename Graph::template EdgeMap<int> EdgeIntMap;
|
athos@610
|
45 |
|
athos@610
|
46 |
typedef ConstMap<Edge,int> ConstMap;
|
athos@610
|
47 |
|
athos@610
|
48 |
//Input
|
athos@610
|
49 |
const Graph& G;
|
athos@610
|
50 |
|
athos@610
|
51 |
//Auxiliary variables
|
athos@610
|
52 |
//This is the capacity map for the mincostflow problem
|
athos@610
|
53 |
ConstMap const1map;
|
athos@610
|
54 |
//This MinCostFlows instance will actually solve the problem
|
athos@610
|
55 |
MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
|
athos@610
|
56 |
|
athos@610
|
57 |
//Container to store found paths
|
athos@610
|
58 |
std::vector< std::vector<Edge> > paths;
|
athos@610
|
59 |
|
athos@610
|
60 |
public :
|
athos@610
|
61 |
|
athos@610
|
62 |
|
athos@610
|
63 |
MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
|
athos@610
|
64 |
const1map(1), mincost_flow(_G, _length, const1map){}
|
athos@610
|
65 |
|
athos@610
|
66 |
///Runs the algorithm.
|
athos@610
|
67 |
|
athos@610
|
68 |
///Runs the algorithm.
|
athos@610
|
69 |
///Returns k if there are at least k edge-disjoint paths from s to t.
|
athos@610
|
70 |
///Otherwise it returns the number of found edge-disjoint paths from s to t.
|
athos@610
|
71 |
int run(Node s, Node t, int k) {
|
athos@610
|
72 |
|
athos@610
|
73 |
int i = mincost_flow.run(s,t,k);
|
athos@610
|
74 |
|
athos@610
|
75 |
|
athos@610
|
76 |
|
athos@610
|
77 |
//Let's find the paths
|
athos@610
|
78 |
//We put the paths into stl vectors (as an inner representation).
|
athos@610
|
79 |
//In the meantime we lose the information stored in 'reversed'.
|
athos@610
|
80 |
//We suppose the lengths to be positive now.
|
athos@610
|
81 |
|
athos@610
|
82 |
//We don't want to change the flow of mincost_flow, so we make a copy
|
athos@610
|
83 |
//The name here suggests that the flow has only 0/1 values.
|
athos@610
|
84 |
EdgeIntMap reversed(G);
|
athos@610
|
85 |
|
hegyi@776
|
86 |
//FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
|
hegyi@776
|
87 |
for(typename Graph::EdgeIt e=loopFirst(typename Graph::EdgeIt(), (G)); e!=INVALID; ++e){
|
athos@610
|
88 |
reversed[e] = mincost_flow.getFlow()[e];
|
athos@610
|
89 |
}
|
athos@610
|
90 |
|
athos@610
|
91 |
paths.clear();
|
athos@610
|
92 |
//total_length=0;
|
athos@610
|
93 |
paths.resize(k);
|
athos@610
|
94 |
for (int j=0; j<i; ++j){
|
athos@610
|
95 |
Node n=s;
|
athos@610
|
96 |
OutEdgeIt e;
|
athos@610
|
97 |
|
athos@610
|
98 |
while (n!=t){
|
athos@610
|
99 |
|
athos@610
|
100 |
|
athos@610
|
101 |
G.first(e,n);
|
athos@610
|
102 |
|
athos@610
|
103 |
while (!reversed[e]){
|
hegyi@776
|
104 |
++e;
|
athos@610
|
105 |
}
|
athos@610
|
106 |
n = G.head(e);
|
athos@610
|
107 |
paths[j].push_back(e);
|
athos@610
|
108 |
//total_length += length[e];
|
athos@610
|
109 |
reversed[e] = 1-reversed[e];
|
athos@610
|
110 |
}
|
athos@610
|
111 |
|
athos@610
|
112 |
}
|
athos@610
|
113 |
return i;
|
athos@610
|
114 |
}
|
athos@610
|
115 |
|
athos@610
|
116 |
|
athos@610
|
117 |
///This function gives back the total length of the found paths.
|
athos@610
|
118 |
///Assumes that \c run() has been run and nothing changed since then.
|
athos@610
|
119 |
Length totalLength(){
|
athos@610
|
120 |
return mincost_flow.totalLength();
|
athos@610
|
121 |
}
|
athos@610
|
122 |
|
athos@610
|
123 |
///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
|
athos@610
|
124 |
///be called before using this function.
|
athos@610
|
125 |
const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
|
athos@610
|
126 |
|
athos@610
|
127 |
///Returns a const reference to the NodeMap \c potential (the dual solution).
|
athos@610
|
128 |
/// \pre \ref run() must be called before using this function.
|
athos@610
|
129 |
const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
|
athos@610
|
130 |
|
athos@610
|
131 |
///This function checks, whether the given solution is optimal
|
athos@610
|
132 |
///Running after a \c run() should return with true
|
athos@610
|
133 |
///In this "state of the art" this only checks optimality, doesn't bother with feasibility
|
athos@610
|
134 |
///
|
athos@610
|
135 |
///\todo Is this OK here?
|
athos@610
|
136 |
bool checkComplementarySlackness(){
|
athos@610
|
137 |
return mincost_flow.checkComplementarySlackness();
|
athos@610
|
138 |
}
|
athos@610
|
139 |
|
athos@610
|
140 |
///This function gives back the \c j-th path in argument p.
|
athos@610
|
141 |
///Assumes that \c run() has been run and nothing changed since then.
|
athos@610
|
142 |
/// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is not less than the result of previous \c run, then the result here will be an empty path (\c j can be 0 as well).
|
athos@610
|
143 |
template<typename DirPath>
|
athos@610
|
144 |
void getPath(DirPath& p, size_t j){
|
athos@610
|
145 |
|
athos@610
|
146 |
p.clear();
|
athos@610
|
147 |
if (j>paths.size()-1){
|
athos@610
|
148 |
return;
|
athos@610
|
149 |
}
|
athos@610
|
150 |
typename DirPath::Builder B(p);
|
athos@610
|
151 |
for(typename std::vector<Edge>::iterator i=paths[j].begin();
|
athos@610
|
152 |
i!=paths[j].end(); ++i ){
|
athos@610
|
153 |
B.pushBack(*i);
|
athos@610
|
154 |
}
|
athos@610
|
155 |
|
athos@610
|
156 |
B.commit();
|
athos@610
|
157 |
}
|
athos@610
|
158 |
|
athos@610
|
159 |
}; //class MinLengthPaths
|
athos@610
|
160 |
|
athos@610
|
161 |
///@}
|
athos@610
|
162 |
|
athos@610
|
163 |
} //namespace hugo
|
athos@610
|
164 |
|
athos@610
|
165 |
#endif //HUGO_MINLENGTHPATHS_H
|