0
2
0
12
12
... | ... |
@@ -100,253 +100,253 @@ |
100 | 100 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
101 | 101 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
102 | 102 |
/// |
103 | 103 |
/// The algorithm consists from two phases. After the first phase |
104 | 104 |
/// the maximal flow value and the minimum cut can be obtained. The |
105 | 105 |
/// second phase constructs the feasible maximum flow on each arc. |
106 | 106 |
/// |
107 | 107 |
/// \param _Graph The digraph type the algorithm runs on. |
108 | 108 |
/// \param _CapacityMap The flow map type. |
109 | 109 |
/// \param _Traits Traits class to set various data types used by |
110 | 110 |
/// the algorithm. The default traits class is \ref |
111 | 111 |
/// PreflowDefaultTraits. See \ref PreflowDefaultTraits for the |
112 | 112 |
/// documentation of a %Preflow traits class. |
113 | 113 |
/// |
114 | 114 |
///\author Jacint Szabo and Balazs Dezso |
115 | 115 |
#ifdef DOXYGEN |
116 | 116 |
template <typename _Graph, typename _CapacityMap, typename _Traits> |
117 | 117 |
#else |
118 | 118 |
template <typename _Graph, |
119 | 119 |
typename _CapacityMap = typename _Graph::template ArcMap<int>, |
120 | 120 |
typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> > |
121 | 121 |
#endif |
122 | 122 |
class Preflow { |
123 | 123 |
public: |
124 | 124 |
|
125 | 125 |
typedef _Traits Traits; |
126 | 126 |
typedef typename Traits::Digraph Digraph; |
127 | 127 |
typedef typename Traits::CapacityMap CapacityMap; |
128 | 128 |
typedef typename Traits::Value Value; |
129 | 129 |
|
130 | 130 |
typedef typename Traits::FlowMap FlowMap; |
131 | 131 |
typedef typename Traits::Elevator Elevator; |
132 | 132 |
typedef typename Traits::Tolerance Tolerance; |
133 | 133 |
|
134 | 134 |
private: |
135 | 135 |
|
136 | 136 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
137 | 137 |
|
138 | 138 |
const Digraph& _graph; |
139 | 139 |
const CapacityMap* _capacity; |
140 | 140 |
|
141 | 141 |
int _node_num; |
142 | 142 |
|
143 | 143 |
Node _source, _target; |
144 | 144 |
|
145 | 145 |
FlowMap* _flow; |
146 | 146 |
bool _local_flow; |
147 | 147 |
|
148 | 148 |
Elevator* _level; |
149 | 149 |
bool _local_level; |
150 | 150 |
|
151 | 151 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
152 | 152 |
ExcessMap* _excess; |
153 | 153 |
|
154 | 154 |
Tolerance _tolerance; |
155 | 155 |
|
156 | 156 |
bool _phase; |
157 | 157 |
|
158 | 158 |
|
159 | 159 |
void createStructures() { |
160 | 160 |
_node_num = countNodes(_graph); |
161 | 161 |
|
162 | 162 |
if (!_flow) { |
163 | 163 |
_flow = Traits::createFlowMap(_graph); |
164 | 164 |
_local_flow = true; |
165 | 165 |
} |
166 | 166 |
if (!_level) { |
167 | 167 |
_level = Traits::createElevator(_graph, _node_num); |
168 | 168 |
_local_level = true; |
169 | 169 |
} |
170 | 170 |
if (!_excess) { |
171 | 171 |
_excess = new ExcessMap(_graph); |
172 | 172 |
} |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
void destroyStructures() { |
176 | 176 |
if (_local_flow) { |
177 | 177 |
delete _flow; |
178 | 178 |
} |
179 | 179 |
if (_local_level) { |
180 | 180 |
delete _level; |
181 | 181 |
} |
182 | 182 |
if (_excess) { |
183 | 183 |
delete _excess; |
184 | 184 |
} |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
public: |
188 | 188 |
|
189 | 189 |
typedef Preflow Create; |
190 | 190 |
|
191 | 191 |
///\name Named template parameters |
192 | 192 |
|
193 | 193 |
///@{ |
194 | 194 |
|
195 | 195 |
template <typename _FlowMap> |
196 |
struct |
|
196 |
struct SetFlowMapTraits : public Traits { |
|
197 | 197 |
typedef _FlowMap FlowMap; |
198 | 198 |
static FlowMap *createFlowMap(const Digraph&) { |
199 | 199 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
200 | 200 |
return 0; // ignore warnings |
201 | 201 |
} |
202 | 202 |
}; |
203 | 203 |
|
204 | 204 |
/// \brief \ref named-templ-param "Named parameter" for setting |
205 | 205 |
/// FlowMap type |
206 | 206 |
/// |
207 | 207 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
208 | 208 |
/// type |
209 | 209 |
template <typename _FlowMap> |
210 |
struct DefFlowMap |
|
211 |
: public Preflow<Digraph, CapacityMap, DefFlowMapTraits<_FlowMap> > { |
|
210 |
struct SetFlowMap |
|
211 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > { |
|
212 | 212 |
typedef Preflow<Digraph, CapacityMap, |
213 |
|
|
213 |
SetFlowMapTraits<_FlowMap> > Create; |
|
214 | 214 |
}; |
215 | 215 |
|
216 | 216 |
template <typename _Elevator> |
217 |
struct |
|
217 |
struct SetElevatorTraits : public Traits { |
|
218 | 218 |
typedef _Elevator Elevator; |
219 | 219 |
static Elevator *createElevator(const Digraph&, int) { |
220 | 220 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
221 | 221 |
return 0; // ignore warnings |
222 | 222 |
} |
223 | 223 |
}; |
224 | 224 |
|
225 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
226 | 226 |
/// Elevator type |
227 | 227 |
/// |
228 | 228 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
229 | 229 |
/// type |
230 | 230 |
template <typename _Elevator> |
231 |
struct DefElevator |
|
232 |
: public Preflow<Digraph, CapacityMap, DefElevatorTraits<_Elevator> > { |
|
231 |
struct SetElevator |
|
232 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > { |
|
233 | 233 |
typedef Preflow<Digraph, CapacityMap, |
234 |
|
|
234 |
SetElevatorTraits<_Elevator> > Create; |
|
235 | 235 |
}; |
236 | 236 |
|
237 | 237 |
template <typename _Elevator> |
238 |
struct |
|
238 |
struct SetStandardElevatorTraits : public Traits { |
|
239 | 239 |
typedef _Elevator Elevator; |
240 | 240 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
241 | 241 |
return new Elevator(digraph, max_level); |
242 | 242 |
} |
243 | 243 |
}; |
244 | 244 |
|
245 | 245 |
/// \brief \ref named-templ-param "Named parameter" for setting |
246 | 246 |
/// Elevator type |
247 | 247 |
/// |
248 | 248 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
249 | 249 |
/// type. The Elevator should be standard constructor interface, ie. |
250 | 250 |
/// the digraph and the maximum level should be passed to it. |
251 | 251 |
template <typename _Elevator> |
252 |
struct |
|
252 |
struct SetStandardElevator |
|
253 | 253 |
: public Preflow<Digraph, CapacityMap, |
254 |
|
|
254 |
SetStandardElevatorTraits<_Elevator> > { |
|
255 | 255 |
typedef Preflow<Digraph, CapacityMap, |
256 |
|
|
256 |
SetStandardElevatorTraits<_Elevator> > Create; |
|
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
/// @} |
260 | 260 |
|
261 | 261 |
protected: |
262 | 262 |
|
263 | 263 |
Preflow() {} |
264 | 264 |
|
265 | 265 |
public: |
266 | 266 |
|
267 | 267 |
|
268 | 268 |
/// \brief The constructor of the class. |
269 | 269 |
/// |
270 | 270 |
/// The constructor of the class. |
271 | 271 |
/// \param digraph The digraph the algorithm runs on. |
272 | 272 |
/// \param capacity The capacity of the arcs. |
273 | 273 |
/// \param source The source node. |
274 | 274 |
/// \param target The target node. |
275 | 275 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
276 | 276 |
Node source, Node target) |
277 | 277 |
: _graph(digraph), _capacity(&capacity), |
278 | 278 |
_node_num(0), _source(source), _target(target), |
279 | 279 |
_flow(0), _local_flow(false), |
280 | 280 |
_level(0), _local_level(false), |
281 | 281 |
_excess(0), _tolerance(), _phase() {} |
282 | 282 |
|
283 | 283 |
/// \brief Destrcutor. |
284 | 284 |
/// |
285 | 285 |
/// Destructor. |
286 | 286 |
~Preflow() { |
287 | 287 |
destroyStructures(); |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
/// \brief Sets the capacity map. |
291 | 291 |
/// |
292 | 292 |
/// Sets the capacity map. |
293 | 293 |
/// \return \c (*this) |
294 | 294 |
Preflow& capacityMap(const CapacityMap& map) { |
295 | 295 |
_capacity = ↦ |
296 | 296 |
return *this; |
297 | 297 |
} |
298 | 298 |
|
299 | 299 |
/// \brief Sets the flow map. |
300 | 300 |
/// |
301 | 301 |
/// Sets the flow map. |
302 | 302 |
/// \return \c (*this) |
303 | 303 |
Preflow& flowMap(FlowMap& map) { |
304 | 304 |
if (_local_flow) { |
305 | 305 |
delete _flow; |
306 | 306 |
_local_flow = false; |
307 | 307 |
} |
308 | 308 |
_flow = ↦ |
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
/// \brief Returns the flow map. |
313 | 313 |
/// |
314 | 314 |
/// \return The flow map. |
315 | 315 |
const FlowMap& flowMap() { |
316 | 316 |
return *_flow; |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
/// \brief Sets the elevator. |
320 | 320 |
/// |
321 | 321 |
/// Sets the elevator. |
322 | 322 |
/// \return \c (*this) |
323 | 323 |
Preflow& elevator(Elevator& elevator) { |
324 | 324 |
if (_local_level) { |
325 | 325 |
delete _level; |
326 | 326 |
_local_level = false; |
327 | 327 |
} |
328 | 328 |
_level = &elevator; |
329 | 329 |
return *this; |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
/// \brief Returns the elevator. |
333 | 333 |
/// |
334 | 334 |
/// \return The elevator. |
335 | 335 |
const Elevator& elevator() { |
336 | 336 |
return *_level; |
337 | 337 |
} |
338 | 338 |
|
339 | 339 |
/// \brief Sets the source node. |
340 | 340 |
/// |
341 | 341 |
/// Sets the source node. |
342 | 342 |
/// \return \c (*this) |
343 | 343 |
Preflow& source(const Node& node) { |
344 | 344 |
_source = node; |
345 | 345 |
return *this; |
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
/// \brief Sets the target node. |
349 | 349 |
/// |
350 | 350 |
/// Sets the target node. |
351 | 351 |
/// \return \c (*this) |
352 | 352 |
Preflow& target(const Node& node) { |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <fstream> |
20 | 20 |
#include <string> |
21 | 21 |
|
22 | 22 |
#include "test_tools.h" |
23 | 23 |
#include <lemon/smart_graph.h> |
24 | 24 |
#include <lemon/preflow.h> |
25 | 25 |
#include <lemon/concepts/digraph.h> |
26 | 26 |
#include <lemon/concepts/maps.h> |
27 | 27 |
#include <lemon/lgf_reader.h> |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
void checkPreflow() |
32 | 32 |
{ |
33 | 33 |
typedef int VType; |
34 | 34 |
typedef concepts::Digraph Digraph; |
35 | 35 |
|
36 | 36 |
typedef Digraph::Node Node; |
37 | 37 |
typedef Digraph::Arc Arc; |
38 | 38 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
39 | 39 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
40 | 40 |
typedef concepts::WriteMap<Node,bool> CutMap; |
41 | 41 |
|
42 | 42 |
Digraph g; |
43 | 43 |
Node n; |
44 | 44 |
Arc e; |
45 | 45 |
CapMap cap; |
46 | 46 |
FlowMap flow; |
47 | 47 |
CutMap cut; |
48 | 48 |
|
49 |
Preflow<Digraph, CapMap>:: |
|
49 |
Preflow<Digraph, CapMap>::SetFlowMap<FlowMap>::Create preflow_test(g,cap,n,n); |
|
50 | 50 |
|
51 | 51 |
preflow_test.capacityMap(cap); |
52 | 52 |
flow = preflow_test.flowMap(); |
53 | 53 |
preflow_test.flowMap(flow); |
54 | 54 |
preflow_test.source(n); |
55 | 55 |
preflow_test.target(n); |
56 | 56 |
|
57 | 57 |
preflow_test.init(); |
58 | 58 |
preflow_test.flowInit(cap); |
59 | 59 |
preflow_test.startFirstPhase(); |
60 | 60 |
preflow_test.startSecondPhase(); |
61 | 61 |
preflow_test.run(); |
62 | 62 |
preflow_test.runMinCut(); |
63 | 63 |
|
64 | 64 |
preflow_test.flowValue(); |
65 | 65 |
preflow_test.minCut(n); |
66 | 66 |
preflow_test.minCutMap(cut); |
67 | 67 |
preflow_test.flow(e); |
68 | 68 |
|
69 | 69 |
} |
70 | 70 |
|
71 | 71 |
int cutValue (const SmartDigraph& g, |
72 | 72 |
const SmartDigraph::NodeMap<bool>& cut, |
73 | 73 |
const SmartDigraph::ArcMap<int>& cap) { |
74 | 74 |
|
75 | 75 |
int c=0; |
76 | 76 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) { |
77 | 77 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
78 | 78 |
} |
79 | 79 |
return c; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
bool checkFlow(const SmartDigraph& g, |
83 | 83 |
const SmartDigraph::ArcMap<int>& flow, |
84 | 84 |
const SmartDigraph::ArcMap<int>& cap, |
85 | 85 |
SmartDigraph::Node s, SmartDigraph::Node t) { |
86 | 86 |
|
87 | 87 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) { |
88 | 88 |
if (flow[e] < 0 || flow[e] > cap[e]) return false; |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) { |
92 | 92 |
if (n == s || n == t) continue; |
93 | 93 |
int sum = 0; |
94 | 94 |
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) { |
95 | 95 |
sum += flow[e]; |
96 | 96 |
} |
97 | 97 |
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) { |
98 | 98 |
sum -= flow[e]; |
99 | 99 |
} |
100 | 100 |
if (sum != 0) return false; |
101 | 101 |
} |
102 | 102 |
return true; |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
int main() { |
106 | 106 |
|
107 | 107 |
typedef SmartDigraph Digraph; |
108 | 108 |
|
109 | 109 |
typedef Digraph::Node Node; |
110 | 110 |
typedef Digraph::NodeIt NodeIt; |
111 | 111 |
typedef Digraph::ArcIt ArcIt; |
112 | 112 |
typedef Digraph::ArcMap<int> CapMap; |
113 | 113 |
typedef Digraph::ArcMap<int> FlowMap; |
114 | 114 |
typedef Digraph::NodeMap<bool> CutMap; |
115 | 115 |
|
116 | 116 |
typedef Preflow<Digraph, CapMap> PType; |
117 | 117 |
|
118 | 118 |
std::string f_name; |
119 | 119 |
if( getenv("srcdir") ) |
120 | 120 |
f_name = std::string(getenv("srcdir")); |
121 | 121 |
else f_name = "."; |
122 | 122 |
f_name += "/test/preflow_graph.lgf"; |
123 | 123 |
|
124 | 124 |
std::ifstream file(f_name.c_str()); |
125 | 125 |
|
126 | 126 |
check(file, "Input file '" << f_name << "' not found."); |
127 | 127 |
|
128 | 128 |
Digraph g; |
129 | 129 |
Node s, t; |
130 | 130 |
CapMap cap(g); |
131 | 131 |
DigraphReader<Digraph>(g,file). |
132 | 132 |
arcMap("capacity", cap). |
133 | 133 |
node("source",s). |
134 | 134 |
node("target",t). |
135 | 135 |
run(); |
136 | 136 |
|
137 | 137 |
PType preflow_test(g, cap, s, t); |
138 | 138 |
preflow_test.run(); |
139 | 139 |
|
140 | 140 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t), |
141 | 141 |
"The flow is not feasible."); |
142 | 142 |
|
143 | 143 |
CutMap min_cut(g); |
144 | 144 |
preflow_test.minCutMap(min_cut); |
145 | 145 |
int min_cut_value=cutValue(g,min_cut,cap); |
0 comments (0 inline)