43 /// |
43 /// |
44 /// The type of the map that stores the arc capacities. |
44 /// The type of the map that stores the arc capacities. |
45 /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
45 /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 typedef CAP CapacityMap; |
46 typedef CAP CapacityMap; |
47 |
47 |
48 /// \brief The type of the length of the arcs. |
48 /// \brief The type of the flow values. |
49 typedef typename CapacityMap::Value Value; |
49 typedef typename CapacityMap::Value Value; |
50 |
50 |
51 /// \brief The map type that stores the flow values. |
51 /// \brief The type of the map that stores the flow values. |
52 /// |
52 /// |
53 /// The map type that stores the flow values. |
53 /// The type of the map that stores the flow values. |
54 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
54 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
55 #ifdef DOXYGEN |
|
56 typedef GR::ArcMap<Value> FlowMap; |
|
57 #else |
55 typedef typename Digraph::template ArcMap<Value> FlowMap; |
58 typedef typename Digraph::template ArcMap<Value> FlowMap; |
|
59 #endif |
56 |
60 |
57 /// \brief Instantiates a FlowMap. |
61 /// \brief Instantiates a FlowMap. |
58 /// |
62 /// |
59 /// This function instantiates a \ref FlowMap. |
63 /// This function instantiates a \ref FlowMap. |
60 /// \param digraph The digraph, to which we would like to define the flow map. |
64 /// \param digraph The digraph for which we would like to define |
|
65 /// the flow map. |
61 static FlowMap* createFlowMap(const Digraph& digraph) { |
66 static FlowMap* createFlowMap(const Digraph& digraph) { |
62 return new FlowMap(digraph); |
67 return new FlowMap(digraph); |
63 } |
68 } |
64 |
69 |
65 /// \brief The tolerance used by the algorithm |
70 /// \brief The tolerance used by the algorithm |
72 /// \ingroup max_flow |
77 /// \ingroup max_flow |
73 /// |
78 /// |
74 /// \brief Edmonds-Karp algorithms class. |
79 /// \brief Edmonds-Karp algorithms class. |
75 /// |
80 /// |
76 /// This class provides an implementation of the \e Edmonds-Karp \e |
81 /// This class provides an implementation of the \e Edmonds-Karp \e |
77 /// algorithm producing a flow of maximum value in directed |
82 /// algorithm producing a \ref max_flow "flow of maximum value" in a |
78 /// digraphs. The Edmonds-Karp algorithm is slower than the Preflow |
83 /// digraph \ref clrs01algorithms, \ref amo93networkflows, |
79 /// algorithm but it has an advantage of the step-by-step execution |
84 /// \ref edmondskarp72theoretical. |
|
85 /// The Edmonds-Karp algorithm is slower than the Preflow |
|
86 /// algorithm, but it has an advantage of the step-by-step execution |
80 /// control with feasible flow solutions. The \e source node, the \e |
87 /// control with feasible flow solutions. The \e source node, the \e |
81 /// target node, the \e capacity of the arcs and the \e starting \e |
88 /// target node, the \e capacity of the arcs and the \e starting \e |
82 /// flow value of the arcs should be passed to the algorithm |
89 /// flow value of the arcs should be passed to the algorithm |
83 /// through the constructor. |
90 /// through the constructor. |
84 /// |
91 /// |
85 /// The time complexity of the algorithm is \f$ O(nm^2) \f$ in |
92 /// The time complexity of the algorithm is \f$ O(nm^2) \f$ in |
86 /// worst case. Always try the preflow algorithm instead of this if |
93 /// worst case. Always try the Preflow algorithm instead of this if |
87 /// you just want to compute the optimal flow. |
94 /// you just want to compute the optimal flow. |
88 /// |
95 /// |
89 /// \param GR The digraph type the algorithm runs on. |
96 /// \tparam GR The type of the digraph the algorithm runs on. |
90 /// \param CAP The capacity map type. |
97 /// \tparam CAP The type of the capacity map. The default map |
91 /// \param TR Traits class to set various data types used by |
98 /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
92 /// the algorithm. The default traits class is \ref |
99 /// \tparam TR The traits class that defines various types used by the |
93 /// EdmondsKarpDefaultTraits. See \ref EdmondsKarpDefaultTraits for the |
100 /// algorithm. By default, it is \ref EdmondsKarpDefaultTraits |
94 /// documentation of a Edmonds-Karp traits class. |
101 /// "EdmondsKarpDefaultTraits<GR, CAP>". |
|
102 /// In most cases, this parameter should not be set directly, |
|
103 /// consider to use the named template parameters instead. |
95 |
104 |
96 #ifdef DOXYGEN |
105 #ifdef DOXYGEN |
97 template <typename GR, typename CAP, typename TR> |
106 template <typename GR, typename CAP, typename TR> |
98 #else |
107 #else |
99 template <typename GR, |
108 template <typename GR, |
101 typename TR = EdmondsKarpDefaultTraits<GR, CAP> > |
110 typename TR = EdmondsKarpDefaultTraits<GR, CAP> > |
102 #endif |
111 #endif |
103 class EdmondsKarp { |
112 class EdmondsKarp { |
104 public: |
113 public: |
105 |
114 |
|
115 /// The \ref EdmondsKarpDefaultTraits "traits class" of the algorithm. |
106 typedef TR Traits; |
116 typedef TR Traits; |
|
117 /// The type of the digraph the algorithm runs on. |
107 typedef typename Traits::Digraph Digraph; |
118 typedef typename Traits::Digraph Digraph; |
|
119 /// The type of the capacity map. |
108 typedef typename Traits::CapacityMap CapacityMap; |
120 typedef typename Traits::CapacityMap CapacityMap; |
|
121 /// The type of the flow values. |
109 typedef typename Traits::Value Value; |
122 typedef typename Traits::Value Value; |
110 |
123 |
|
124 /// The type of the flow map. |
111 typedef typename Traits::FlowMap FlowMap; |
125 typedef typename Traits::FlowMap FlowMap; |
|
126 /// The type of the tolerance. |
112 typedef typename Traits::Tolerance Tolerance; |
127 typedef typename Traits::Tolerance Tolerance; |
113 |
128 |
114 private: |
129 private: |
115 |
130 |
116 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
131 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
196 EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity, |
210 EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity, |
197 Node source, Node target) |
211 Node source, Node target) |
198 : _graph(digraph), _capacity(&capacity), _source(source), _target(target), |
212 : _graph(digraph), _capacity(&capacity), _source(source), _target(target), |
199 _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value() |
213 _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value() |
200 { |
214 { |
201 LEMON_ASSERT(_source != _target,"Flow source and target are the same nodes."); |
215 LEMON_ASSERT(_source != _target, |
|
216 "Flow source and target are the same nodes."); |
202 } |
217 } |
203 |
218 |
204 /// \brief Destructor. |
219 /// \brief Destructor. |
205 /// |
220 /// |
206 /// Destructor. |
221 /// Destructor. |
209 } |
224 } |
210 |
225 |
211 /// \brief Sets the capacity map. |
226 /// \brief Sets the capacity map. |
212 /// |
227 /// |
213 /// Sets the capacity map. |
228 /// Sets the capacity map. |
214 /// \return \c (*this) |
229 /// \return <tt>(*this)</tt> |
215 EdmondsKarp& capacityMap(const CapacityMap& map) { |
230 EdmondsKarp& capacityMap(const CapacityMap& map) { |
216 _capacity = ↦ |
231 _capacity = ↦ |
217 return *this; |
232 return *this; |
218 } |
233 } |
219 |
234 |
220 /// \brief Sets the flow map. |
235 /// \brief Sets the flow map. |
221 /// |
236 /// |
222 /// Sets the flow map. |
237 /// Sets the flow map. |
223 /// \return \c (*this) |
238 /// If you don't use this function before calling \ref run() or |
|
239 /// \ref init(), an instance will be allocated automatically. |
|
240 /// The destructor deallocates this automatically allocated map, |
|
241 /// of course. |
|
242 /// \return <tt>(*this)</tt> |
224 EdmondsKarp& flowMap(FlowMap& map) { |
243 EdmondsKarp& flowMap(FlowMap& map) { |
225 if (_local_flow) { |
244 if (_local_flow) { |
226 delete _flow; |
245 delete _flow; |
227 _local_flow = false; |
246 _local_flow = false; |
228 } |
247 } |
229 _flow = ↦ |
248 _flow = ↦ |
230 return *this; |
249 return *this; |
231 } |
250 } |
232 |
251 |
233 /// \brief Returns the flow map. |
|
234 /// |
|
235 /// \return The flow map. |
|
236 const FlowMap& flowMap() const { |
|
237 return *_flow; |
|
238 } |
|
239 |
|
240 /// \brief Sets the source node. |
252 /// \brief Sets the source node. |
241 /// |
253 /// |
242 /// Sets the source node. |
254 /// Sets the source node. |
243 /// \return \c (*this) |
255 /// \return <tt>(*this)</tt> |
244 EdmondsKarp& source(const Node& node) { |
256 EdmondsKarp& source(const Node& node) { |
245 _source = node; |
257 _source = node; |
246 return *this; |
258 return *this; |
247 } |
259 } |
248 |
260 |
249 /// \brief Sets the target node. |
261 /// \brief Sets the target node. |
250 /// |
262 /// |
251 /// Sets the target node. |
263 /// Sets the target node. |
252 /// \return \c (*this) |
264 /// \return <tt>(*this)</tt> |
253 EdmondsKarp& target(const Node& node) { |
265 EdmondsKarp& target(const Node& node) { |
254 _target = node; |
266 _target = node; |
255 return *this; |
267 return *this; |
256 } |
268 } |
257 |
269 |
258 /// \brief Sets the tolerance used by algorithm. |
270 /// \brief Sets the tolerance used by algorithm. |
259 /// |
271 /// |
260 /// Sets the tolerance used by algorithm. |
272 /// Sets the tolerance used by algorithm. |
|
273 /// \return <tt>(*this)</tt> |
261 EdmondsKarp& tolerance(const Tolerance& tolerance) { |
274 EdmondsKarp& tolerance(const Tolerance& tolerance) { |
262 _tolerance = tolerance; |
275 _tolerance = tolerance; |
263 return *this; |
276 return *this; |
264 } |
277 } |
265 |
278 |
266 /// \brief Returns the tolerance used by algorithm. |
279 /// \brief Returns a const reference to the tolerance. |
267 /// |
280 /// |
268 /// Returns the tolerance used by algorithm. |
281 /// Returns a const reference to the tolerance object used by |
|
282 /// the algorithm. |
269 const Tolerance& tolerance() const { |
283 const Tolerance& tolerance() const { |
270 return _tolerance; |
284 return _tolerance; |
271 } |
285 } |
272 |
286 |
273 /// \name Execution control |
287 /// \name Execution control |
274 /// The simplest way to execute the |
288 /// The simplest way to execute the algorithm is to use \ref run().\n |
275 /// algorithm is to use the \c run() member functions. |
289 /// If you need better control on the initial solution or the execution, |
276 /// \n |
290 /// you have to call one of the \ref init() functions first, then |
277 /// If you need more control on initial solution or |
291 /// \ref start() or multiple times the \ref augment() function. |
278 /// execution then you have to call one \ref init() function and then |
|
279 /// the start() or multiple times the \c augment() member function. |
|
280 |
292 |
281 ///@{ |
293 ///@{ |
282 |
294 |
283 /// \brief Initializes the algorithm |
295 /// \brief Initializes the algorithm. |
284 /// |
296 /// |
285 /// Sets the flow to empty flow. |
297 /// Initializes the internal data structures and sets the initial |
|
298 /// flow to zero on each arc. |
286 void init() { |
299 void init() { |
287 createStructures(); |
300 createStructures(); |
288 for (ArcIt it(_graph); it != INVALID; ++it) { |
301 for (ArcIt it(_graph); it != INVALID; ++it) { |
289 _flow->set(it, 0); |
302 _flow->set(it, 0); |
290 } |
303 } |
291 _flow_value = 0; |
304 _flow_value = 0; |
292 } |
305 } |
293 |
306 |
294 /// \brief Initializes the algorithm |
307 /// \brief Initializes the algorithm using the given flow map. |
295 /// |
308 /// |
296 /// Initializes the flow to the \c flowMap. The \c flowMap should |
309 /// Initializes the internal data structures and sets the initial |
297 /// contain a feasible flow, ie. in each node excluding the source |
310 /// flow to the given \c flowMap. The \c flowMap should |
298 /// and the target the incoming flow should be equal to the |
311 /// contain a feasible flow, i.e. at each node excluding the source |
|
312 /// and the target, the incoming flow should be equal to the |
299 /// outgoing flow. |
313 /// outgoing flow. |
300 template <typename FlowMap> |
314 template <typename FlowMap> |
301 void flowInit(const FlowMap& flowMap) { |
315 void flowInit(const FlowMap& flowMap) { |
302 createStructures(); |
316 createStructures(); |
303 for (ArcIt e(_graph); e != INVALID; ++e) { |
317 for (ArcIt e(_graph); e != INVALID; ++e) { |
310 for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) { |
324 for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) { |
311 _flow_value -= (*_flow)[jt]; |
325 _flow_value -= (*_flow)[jt]; |
312 } |
326 } |
313 } |
327 } |
314 |
328 |
315 /// \brief Initializes the algorithm |
329 /// \brief Initializes the algorithm using the given flow map. |
316 /// |
330 /// |
317 /// Initializes the flow to the \c flowMap. The \c flowMap should |
331 /// Initializes the internal data structures and sets the initial |
318 /// contain a feasible flow, ie. in each node excluding the source |
332 /// flow to the given \c flowMap. The \c flowMap should |
319 /// and the target the incoming flow should be equal to the |
333 /// contain a feasible flow, i.e. at each node excluding the source |
320 /// outgoing flow. |
334 /// and the target, the incoming flow should be equal to the |
321 /// \return %False when the given flowMap does not contain |
335 /// outgoing flow. |
|
336 /// \return \c false when the given \c flowMap does not contain a |
322 /// feasible flow. |
337 /// feasible flow. |
323 template <typename FlowMap> |
338 template <typename FlowMap> |
324 bool checkedFlowInit(const FlowMap& flowMap) { |
339 bool checkedFlowInit(const FlowMap& flowMap) { |
325 createStructures(); |
340 createStructures(); |
326 for (ArcIt e(_graph); e != INVALID; ++e) { |
341 for (ArcIt e(_graph); e != INVALID; ++e) { |
352 _flow_value -= (*_flow)[jt]; |
367 _flow_value -= (*_flow)[jt]; |
353 } |
368 } |
354 return true; |
369 return true; |
355 } |
370 } |
356 |
371 |
357 /// \brief Augment the solution on an arc shortest path. |
372 /// \brief Augments the solution along a shortest path. |
358 /// |
373 /// |
359 /// Augment the solution on an arc shortest path. It searches an |
374 /// Augments the solution along a shortest path. This function searches a |
360 /// arc shortest path between the source and the target |
375 /// shortest path between the source and the target |
361 /// in the residual digraph by the bfs algoritm. |
376 /// in the residual digraph by the Bfs algoritm. |
362 /// Then it increases the flow on this path with the minimal residual |
377 /// Then it increases the flow on this path with the minimal residual |
363 /// capacity on the path. If there is no such path it gives back |
378 /// capacity on the path. If there is no such path, it gives back |
364 /// false. |
379 /// false. |
365 /// \return %False when the augmenting didn't success so the |
380 /// \return \c false when the augmenting did not success, i.e. the |
366 /// current flow is a feasible and optimal solution. |
381 /// current flow is a feasible and optimal solution. |
367 bool augment() { |
382 bool augment() { |
368 for (NodeIt n(_graph); n != INVALID; ++n) { |
383 for (NodeIt n(_graph); n != INVALID; ++n) { |
369 _pred->set(n, INVALID); |
384 _pred->set(n, INVALID); |
370 } |
385 } |
460 /// @} |
478 /// @} |
461 |
479 |
462 /// \name Query Functions |
480 /// \name Query Functions |
463 /// The result of the Edmonds-Karp algorithm can be obtained using these |
481 /// The result of the Edmonds-Karp algorithm can be obtained using these |
464 /// functions.\n |
482 /// functions.\n |
465 /// Before the use of these functions, |
483 /// Either \ref run() or \ref start() should be called before using them. |
466 /// either run() or start() must be called. |
|
467 |
484 |
468 ///@{ |
485 ///@{ |
469 |
486 |
470 /// \brief Returns the value of the maximum flow. |
487 /// \brief Returns the value of the maximum flow. |
471 /// |
488 /// |
472 /// Returns the value of the maximum flow by returning the excess |
489 /// Returns the value of the maximum flow found by the algorithm. |
473 /// of the target node \c t. |
490 /// |
474 |
491 /// \pre Either \ref run() or \ref init() must be called before |
|
492 /// using this function. |
475 Value flowValue() const { |
493 Value flowValue() const { |
476 return _flow_value; |
494 return _flow_value; |
477 } |
495 } |
478 |
496 |
479 |
497 /// \brief Returns the flow value on the given arc. |
480 /// \brief Returns the flow on the arc. |
498 /// |
481 /// |
499 /// Returns the flow value on the given arc. |
482 /// Sets the \c flowMap to the flow on the arcs. |
500 /// |
|
501 /// \pre Either \ref run() or \ref init() must be called before |
|
502 /// using this function. |
483 Value flow(const Arc& arc) const { |
503 Value flow(const Arc& arc) const { |
484 return (*_flow)[arc]; |
504 return (*_flow)[arc]; |
485 } |
505 } |
486 |
506 |
487 /// \brief Returns true when the node is on the source side of minimum cut. |
507 /// \brief Returns a const reference to the flow map. |
488 /// |
508 /// |
489 |
509 /// Returns a const reference to the arc map storing the found flow. |
490 /// Returns true when the node is on the source side of minimum |
510 /// |
491 /// cut. |
511 /// \pre Either \ref run() or \ref init() must be called before |
492 |
512 /// using this function. |
|
513 const FlowMap& flowMap() const { |
|
514 return *_flow; |
|
515 } |
|
516 |
|
517 /// \brief Returns \c true when the node is on the source side of the |
|
518 /// minimum cut. |
|
519 /// |
|
520 /// Returns true when the node is on the source side of the found |
|
521 /// minimum cut. |
|
522 /// |
|
523 /// \pre Either \ref run() or \ref init() must be called before |
|
524 /// using this function. |
493 bool minCut(const Node& node) const { |
525 bool minCut(const Node& node) const { |
494 return ((*_pred)[node] != INVALID) or node == _source; |
526 return ((*_pred)[node] != INVALID) or node == _source; |
495 } |
527 } |
496 |
528 |
497 /// \brief Returns a minimum value cut. |
529 /// \brief Gives back a minimum value cut. |
498 /// |
530 /// |
499 /// Sets \c cutMap to the characteristic vector of a minimum value cut. |
531 /// Sets \c cutMap to the characteristic vector of a minimum value |
500 |
532 /// cut. \c cutMap should be a \ref concepts::WriteMap "writable" |
|
533 /// node map with \c bool (or convertible) value type. |
|
534 /// |
|
535 /// \note This function calls \ref minCut() for each node, so it runs in |
|
536 /// O(n) time. |
|
537 /// |
|
538 /// \pre Either \ref run() or \ref init() must be called before |
|
539 /// using this function. |
501 template <typename CutMap> |
540 template <typename CutMap> |
502 void minCutMap(CutMap& cutMap) const { |
541 void minCutMap(CutMap& cutMap) const { |
503 for (NodeIt n(_graph); n != INVALID; ++n) { |
542 for (NodeIt n(_graph); n != INVALID; ++n) { |
504 cutMap.set(n, (*_pred)[n] != INVALID); |
543 cutMap.set(n, (*_pred)[n] != INVALID); |
505 } |
544 } |