athos@610
|
1 |
// -*- c++ -*-
|
athos@610
|
2 |
#ifndef HUGO_MINCOSTFLOWS_H
|
athos@610
|
3 |
#define HUGO_MINCOSTFLOWS_H
|
athos@610
|
4 |
|
athos@610
|
5 |
///\ingroup galgs
|
athos@610
|
6 |
///\file
|
athos@610
|
7 |
///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost
|
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 <for_each_macros.h>
|
athos@610
|
15 |
|
athos@610
|
16 |
namespace hugo {
|
athos@610
|
17 |
|
athos@610
|
18 |
/// \addtogroup galgs
|
athos@610
|
19 |
/// @{
|
athos@610
|
20 |
|
athos@610
|
21 |
///\brief Implementation of an algorithm for finding a flow of value \c k
|
athos@610
|
22 |
///(for small values of \c k) having minimal total cost between 2 nodes
|
athos@610
|
23 |
///
|
athos@610
|
24 |
///
|
athos@610
|
25 |
/// The class \ref hugo::MinCostFlows "MinCostFlows" implements
|
athos@610
|
26 |
/// an algorithm for finding a flow of value \c k
|
athos@610
|
27 |
///(for small values of \c k) having minimal total cost
|
athos@610
|
28 |
/// from a given source node to a given target node in an
|
athos@610
|
29 |
/// edge-weighted directed graph having nonnegative integer capacities.
|
athos@610
|
30 |
/// The range of the length (weight) function is nonnegative reals but
|
athos@610
|
31 |
/// the range of capacity function is the set of nonnegative integers.
|
athos@610
|
32 |
/// It is not a polinomial time algorithm for counting the minimum cost
|
athos@610
|
33 |
/// maximal flow, since it counts the minimum cost flow for every value 0..M
|
athos@610
|
34 |
/// where \c M is the value of the maximal flow.
|
athos@610
|
35 |
///
|
athos@610
|
36 |
///\author Attila Bernath
|
athos@610
|
37 |
template <typename Graph, typename LengthMap, typename CapacityMap>
|
athos@610
|
38 |
class MinCostFlows {
|
athos@610
|
39 |
|
athos@610
|
40 |
typedef typename LengthMap::ValueType Length;
|
athos@610
|
41 |
|
athos@610
|
42 |
//Warning: this should be integer type
|
athos@610
|
43 |
typedef typename CapacityMap::ValueType Capacity;
|
athos@610
|
44 |
|
athos@610
|
45 |
typedef typename Graph::Node Node;
|
athos@610
|
46 |
typedef typename Graph::NodeIt NodeIt;
|
athos@610
|
47 |
typedef typename Graph::Edge Edge;
|
athos@610
|
48 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
athos@610
|
49 |
typedef typename Graph::template EdgeMap<int> EdgeIntMap;
|
athos@610
|
50 |
|
athos@610
|
51 |
// typedef ConstMap<Edge,int> ConstMap;
|
athos@610
|
52 |
|
athos@610
|
53 |
typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
|
athos@610
|
54 |
typedef typename ResGraphType::Edge ResGraphEdge;
|
athos@610
|
55 |
|
athos@610
|
56 |
class ModLengthMap {
|
athos@610
|
57 |
//typedef typename ResGraphType::template NodeMap<Length> NodeMap;
|
athos@610
|
58 |
typedef typename Graph::template NodeMap<Length> NodeMap;
|
athos@610
|
59 |
const ResGraphType& G;
|
athos@610
|
60 |
// const EdgeIntMap& rev;
|
athos@610
|
61 |
const LengthMap &ol;
|
athos@610
|
62 |
const NodeMap &pot;
|
athos@610
|
63 |
public :
|
athos@610
|
64 |
typedef typename LengthMap::KeyType KeyType;
|
athos@610
|
65 |
typedef typename LengthMap::ValueType ValueType;
|
athos@610
|
66 |
|
athos@610
|
67 |
ValueType operator[](typename ResGraphType::Edge e) const {
|
athos@610
|
68 |
if (G.forward(e))
|
athos@610
|
69 |
return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
|
athos@610
|
70 |
else
|
athos@610
|
71 |
return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
|
athos@610
|
72 |
}
|
athos@610
|
73 |
|
athos@610
|
74 |
ModLengthMap(const ResGraphType& _G,
|
athos@610
|
75 |
const LengthMap &o, const NodeMap &p) :
|
athos@610
|
76 |
G(_G), /*rev(_rev),*/ ol(o), pot(p){};
|
athos@610
|
77 |
};//ModLengthMap
|
athos@610
|
78 |
|
athos@610
|
79 |
|
athos@610
|
80 |
protected:
|
athos@610
|
81 |
|
athos@610
|
82 |
//Input
|
athos@610
|
83 |
const Graph& G;
|
athos@610
|
84 |
const LengthMap& length;
|
athos@610
|
85 |
const CapacityMap& capacity;
|
athos@610
|
86 |
|
athos@610
|
87 |
|
athos@610
|
88 |
//auxiliary variables
|
athos@610
|
89 |
|
athos@610
|
90 |
//To store the flow
|
athos@610
|
91 |
EdgeIntMap flow;
|
athos@610
|
92 |
//To store the potentila (dual variables)
|
athos@610
|
93 |
typename Graph::template NodeMap<Length> potential;
|
athos@610
|
94 |
|
athos@610
|
95 |
//Container to store found paths
|
athos@610
|
96 |
//std::vector< std::vector<Edge> > paths;
|
athos@610
|
97 |
//typedef DirPath<Graph> DPath;
|
athos@610
|
98 |
//DPath paths;
|
athos@610
|
99 |
|
athos@610
|
100 |
|
athos@610
|
101 |
Length total_length;
|
athos@610
|
102 |
|
athos@610
|
103 |
|
athos@610
|
104 |
public :
|
athos@610
|
105 |
|
athos@610
|
106 |
|
athos@610
|
107 |
MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G),
|
athos@610
|
108 |
length(_length), capacity(_cap), flow(_G), potential(_G){ }
|
athos@610
|
109 |
|
athos@610
|
110 |
|
athos@610
|
111 |
///Runs the algorithm.
|
athos@610
|
112 |
|
athos@610
|
113 |
///Runs the algorithm.
|
athos@610
|
114 |
///Returns k if there are at least k edge-disjoint paths from s to t.
|
athos@610
|
115 |
///Otherwise it returns the number of found edge-disjoint paths from s to t.
|
athos@610
|
116 |
///\todo May be it does make sense to be able to start with a nonzero
|
athos@610
|
117 |
/// feasible primal-dual solution pair as well.
|
athos@610
|
118 |
int run(Node s, Node t, int k) {
|
athos@610
|
119 |
|
athos@610
|
120 |
//Resetting variables from previous runs
|
athos@610
|
121 |
total_length = 0;
|
athos@610
|
122 |
|
athos@610
|
123 |
FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
|
athos@610
|
124 |
flow.set(e,0);
|
athos@610
|
125 |
}
|
athos@610
|
126 |
|
athos@610
|
127 |
FOR_EACH_LOC(typename Graph::NodeIt, n, G){
|
athos@610
|
128 |
//cout << potential[n]<<endl;
|
athos@610
|
129 |
potential.set(n,0);
|
athos@610
|
130 |
}
|
athos@610
|
131 |
|
athos@610
|
132 |
|
athos@610
|
133 |
|
athos@610
|
134 |
//We need a residual graph
|
athos@610
|
135 |
ResGraphType res_graph(G, capacity, flow);
|
athos@610
|
136 |
|
athos@610
|
137 |
//Initialize the copy of the Dijkstra potential to zero
|
athos@610
|
138 |
|
athos@610
|
139 |
//typename ResGraphType::template NodeMap<Length> potential(res_graph);
|
athos@610
|
140 |
|
athos@610
|
141 |
|
athos@610
|
142 |
ModLengthMap mod_length(res_graph, length, potential);
|
athos@610
|
143 |
|
athos@610
|
144 |
Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
|
athos@610
|
145 |
|
athos@610
|
146 |
int i;
|
athos@610
|
147 |
for (i=0; i<k; ++i){
|
athos@610
|
148 |
dijkstra.run(s);
|
athos@610
|
149 |
if (!dijkstra.reached(t)){
|
athos@610
|
150 |
//There are no k paths from s to t
|
athos@610
|
151 |
break;
|
athos@610
|
152 |
};
|
athos@610
|
153 |
|
athos@610
|
154 |
{
|
athos@610
|
155 |
//We have to copy the potential
|
athos@610
|
156 |
typename ResGraphType::NodeIt n;
|
athos@610
|
157 |
for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
|
athos@610
|
158 |
potential[n] += dijkstra.distMap()[n];
|
athos@610
|
159 |
}
|
athos@610
|
160 |
}
|
athos@610
|
161 |
|
athos@610
|
162 |
|
athos@610
|
163 |
//Augmenting on the sortest path
|
athos@610
|
164 |
Node n=t;
|
athos@610
|
165 |
ResGraphEdge e;
|
athos@610
|
166 |
while (n!=s){
|
athos@610
|
167 |
e = dijkstra.pred(n);
|
athos@610
|
168 |
n = dijkstra.predNode(n);
|
athos@610
|
169 |
res_graph.augment(e,1);
|
athos@610
|
170 |
//Let's update the total length
|
athos@610
|
171 |
if (res_graph.forward(e))
|
athos@610
|
172 |
total_length += length[e];
|
athos@610
|
173 |
else
|
athos@610
|
174 |
total_length -= length[e];
|
athos@610
|
175 |
}
|
athos@610
|
176 |
|
athos@610
|
177 |
|
athos@610
|
178 |
}
|
athos@610
|
179 |
|
athos@610
|
180 |
|
athos@610
|
181 |
return i;
|
athos@610
|
182 |
}
|
athos@610
|
183 |
|
athos@610
|
184 |
|
athos@610
|
185 |
|
athos@610
|
186 |
|
athos@610
|
187 |
///This function gives back the total length of the found paths.
|
athos@610
|
188 |
///Assumes that \c run() has been run and nothing changed since then.
|
athos@610
|
189 |
Length totalLength(){
|
athos@610
|
190 |
return total_length;
|
athos@610
|
191 |
}
|
athos@610
|
192 |
|
athos@610
|
193 |
///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
|
athos@610
|
194 |
///be called before using this function.
|
athos@610
|
195 |
const EdgeIntMap &getFlow() const { return flow;}
|
athos@610
|
196 |
|
athos@610
|
197 |
///Returns a const reference to the NodeMap \c potential (the dual solution).
|
athos@610
|
198 |
/// \pre \ref run() must be called before using this function.
|
athos@610
|
199 |
const EdgeIntMap &getPotential() const { return potential;}
|
athos@610
|
200 |
|
athos@610
|
201 |
///This function checks, whether the given solution is optimal
|
athos@610
|
202 |
///Running after a \c run() should return with true
|
athos@610
|
203 |
///In this "state of the art" this only check optimality, doesn't bother with feasibility
|
athos@610
|
204 |
///
|
athos@610
|
205 |
///\todo Is this OK here?
|
athos@610
|
206 |
bool checkComplementarySlackness(){
|
athos@610
|
207 |
Length mod_pot;
|
athos@610
|
208 |
Length fl_e;
|
athos@610
|
209 |
FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
|
athos@610
|
210 |
//C^{\Pi}_{i,j}
|
athos@610
|
211 |
mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
|
athos@610
|
212 |
fl_e = flow[e];
|
athos@610
|
213 |
// std::cout << fl_e << std::endl;
|
athos@610
|
214 |
if (0<fl_e && fl_e<capacity[e]){
|
athos@610
|
215 |
if (mod_pot != 0)
|
athos@610
|
216 |
return false;
|
athos@610
|
217 |
}
|
athos@610
|
218 |
else{
|
athos@610
|
219 |
if (mod_pot > 0 && fl_e != 0)
|
athos@610
|
220 |
return false;
|
athos@610
|
221 |
if (mod_pot < 0 && fl_e != capacity[e])
|
athos@610
|
222 |
return false;
|
athos@610
|
223 |
}
|
athos@610
|
224 |
}
|
athos@610
|
225 |
return true;
|
athos@610
|
226 |
}
|
athos@610
|
227 |
|
athos@610
|
228 |
/*
|
athos@610
|
229 |
///\todo To be implemented later
|
athos@610
|
230 |
|
athos@610
|
231 |
///This function gives back the \c j-th path in argument p.
|
athos@610
|
232 |
///Assumes that \c run() has been run and nothing changed since then.
|
athos@610
|
233 |
/// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
|
athos@610
|
234 |
template<typename DirPath>
|
athos@610
|
235 |
void getPath(DirPath& p, int j){
|
athos@610
|
236 |
p.clear();
|
athos@610
|
237 |
typename DirPath::Builder B(p);
|
athos@610
|
238 |
for(typename std::vector<Edge>::iterator i=paths[j].begin();
|
athos@610
|
239 |
i!=paths[j].end(); ++i ){
|
athos@610
|
240 |
B.pushBack(*i);
|
athos@610
|
241 |
}
|
athos@610
|
242 |
|
athos@610
|
243 |
B.commit();
|
athos@610
|
244 |
}
|
athos@610
|
245 |
|
athos@610
|
246 |
*/
|
athos@610
|
247 |
|
athos@610
|
248 |
}; //class MinCostFlows
|
athos@610
|
249 |
|
athos@610
|
250 |
///@}
|
athos@610
|
251 |
|
athos@610
|
252 |
} //namespace hugo
|
athos@610
|
253 |
|
athos@610
|
254 |
#endif //HUGO_MINCOSTFLOW_H
|