93 /// worst case. Always try the Preflow algorithm instead of this if |
93 /// worst case. Always try the Preflow algorithm instead of this if |
94 /// you just want to compute the optimal flow. |
94 /// you just want to compute the optimal flow. |
95 /// |
95 /// |
96 /// \tparam GR The type of the digraph the algorithm runs on. |
96 /// \tparam GR The type of the digraph the algorithm runs on. |
97 /// \tparam CAP The type of the capacity map. The default map |
97 /// \tparam CAP The type of the capacity map. The default map |
98 /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
98 /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
99 /// \tparam TR The traits class that defines various types used by the |
99 /// \tparam TR The traits class that defines various types used by the |
100 /// algorithm. By default, it is \ref EdmondsKarpDefaultTraits |
100 /// algorithm. By default, it is \ref EdmondsKarpDefaultTraits |
101 /// "EdmondsKarpDefaultTraits<GR, CAP>". |
101 /// "EdmondsKarpDefaultTraits<GR, CAP>". |
102 /// In most cases, this parameter should not be set directly, |
102 /// In most cases, this parameter should not be set directly, |
103 /// consider to use the named template parameters instead. |
103 /// consider to use the named template parameters instead. |
104 |
104 |
105 #ifdef DOXYGEN |
105 #ifdef DOXYGEN |
106 template <typename GR, typename CAP, typename TR> |
106 template <typename GR, typename CAP, typename TR> |
107 #else |
107 #else |
108 template <typename GR, |
108 template <typename GR, |
109 typename CAP = typename GR::template ArcMap<int>, |
109 typename CAP = typename GR::template ArcMap<int>, |
110 typename TR = EdmondsKarpDefaultTraits<GR, CAP> > |
110 typename TR = EdmondsKarpDefaultTraits<GR, CAP> > |
111 #endif |
111 #endif |
112 class EdmondsKarp { |
112 class EdmondsKarp { |
113 public: |
113 public: |
114 |
114 |
118 /// The type of the digraph the algorithm runs on. |
118 /// The type of the digraph the algorithm runs on. |
119 typedef typename Traits::Digraph Digraph; |
119 typedef typename Traits::Digraph Digraph; |
120 /// The type of the capacity map. |
120 /// The type of the capacity map. |
121 typedef typename Traits::CapacityMap CapacityMap; |
121 typedef typename Traits::CapacityMap CapacityMap; |
122 /// The type of the flow values. |
122 /// The type of the flow values. |
123 typedef typename Traits::Value Value; |
123 typedef typename Traits::Value Value; |
124 |
124 |
125 /// The type of the flow map. |
125 /// The type of the flow map. |
126 typedef typename Traits::FlowMap FlowMap; |
126 typedef typename Traits::FlowMap FlowMap; |
127 /// The type of the tolerance. |
127 /// The type of the tolerance. |
128 typedef typename Traits::Tolerance Tolerance; |
128 typedef typename Traits::Tolerance Tolerance; |
129 |
129 |
130 private: |
130 private: |
131 |
131 |
132 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
132 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
133 typedef typename Digraph::template NodeMap<Arc> PredMap; |
133 typedef typename Digraph::template NodeMap<Arc> PredMap; |
134 |
134 |
135 const Digraph& _graph; |
135 const Digraph& _graph; |
136 const CapacityMap* _capacity; |
136 const CapacityMap* _capacity; |
137 |
137 |
138 Node _source, _target; |
138 Node _source, _target; |
139 |
139 |
140 FlowMap* _flow; |
140 FlowMap* _flow; |
141 bool _local_flow; |
141 bool _local_flow; |
142 |
142 |
143 PredMap* _pred; |
143 PredMap* _pred; |
144 std::vector<Node> _queue; |
144 std::vector<Node> _queue; |
145 |
145 |
146 Tolerance _tolerance; |
146 Tolerance _tolerance; |
147 Value _flow_value; |
147 Value _flow_value; |
148 |
148 |
149 void createStructures() { |
149 void createStructures() { |
150 if (!_flow) { |
150 if (!_flow) { |
151 _flow = Traits::createFlowMap(_graph); |
151 _flow = Traits::createFlowMap(_graph); |
152 _local_flow = true; |
152 _local_flow = true; |
153 } |
153 } |
154 if (!_pred) { |
154 if (!_pred) { |
155 _pred = new PredMap(_graph); |
155 _pred = new PredMap(_graph); |
156 } |
156 } |
157 _queue.resize(countNodes(_graph)); |
157 _queue.resize(countNodes(_graph)); |
158 } |
158 } |
159 |
159 |
160 void destroyStructures() { |
160 void destroyStructures() { |
161 if (_local_flow) { |
161 if (_local_flow) { |
162 delete _flow; |
162 delete _flow; |
163 } |
163 } |
164 if (_pred) { |
164 if (_pred) { |
165 delete _pred; |
165 delete _pred; |
166 } |
166 } |
167 } |
167 } |
168 |
168 |
169 public: |
169 public: |
170 |
170 |
171 typedef EdmondsKarp Create; |
171 typedef EdmondsKarp Create; |
172 |
172 |
173 ///\name Named template parameters |
173 ///\name Named template parameters |
176 |
176 |
177 template <typename T> |
177 template <typename T> |
178 struct SetFlowMapTraits : public Traits { |
178 struct SetFlowMapTraits : public Traits { |
179 typedef T FlowMap; |
179 typedef T FlowMap; |
180 static FlowMap *createFlowMap(const Digraph&) { |
180 static FlowMap *createFlowMap(const Digraph&) { |
181 LEMON_ASSERT(false, "FlowMap is not initialized"); |
181 LEMON_ASSERT(false, "FlowMap is not initialized"); |
182 return 0; |
182 return 0; |
183 } |
183 } |
184 }; |
184 }; |
185 |
185 |
186 /// \brief \ref named-templ-param "Named parameter" for setting |
186 /// \brief \ref named-templ-param "Named parameter" for setting |
187 /// FlowMap type |
187 /// FlowMap type |
188 /// |
188 /// |
189 /// \ref named-templ-param "Named parameter" for setting FlowMap |
189 /// \ref named-templ-param "Named parameter" for setting FlowMap |
190 /// type |
190 /// type |
191 template <typename T> |
191 template <typename T> |
192 struct SetFlowMap |
192 struct SetFlowMap |
193 : public EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
193 : public EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
194 typedef EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > Create; |
194 typedef EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > Create; |
195 }; |
195 }; |
196 |
196 |
197 /// @} |
197 /// @} |
198 |
198 |
199 protected: |
199 protected: |
200 |
200 |
201 EdmondsKarp() {} |
201 EdmondsKarp() {} |
202 |
202 |
203 public: |
203 public: |
204 |
204 |
205 /// \brief The constructor of the class. |
205 /// \brief The constructor of the class. |
206 /// |
206 /// |
207 /// The constructor of the class. |
207 /// The constructor of the class. |
208 /// \param digraph The digraph the algorithm runs on. |
208 /// \param digraph The digraph the algorithm runs on. |
209 /// \param capacity The capacity of the arcs. |
209 /// \param capacity The capacity of the arcs. |
210 /// \param source The source node. |
210 /// \param source The source node. |
211 /// \param target The target node. |
211 /// \param target The target node. |
212 EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity, |
212 EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity, |
213 Node source, Node target) |
213 Node source, Node target) |
214 : _graph(digraph), _capacity(&capacity), _source(source), _target(target), |
214 : _graph(digraph), _capacity(&capacity), _source(source), _target(target), |
215 _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value() |
215 _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value() |
216 { |
216 { |
217 LEMON_ASSERT(_source != _target, |
217 LEMON_ASSERT(_source != _target, |
218 "Flow source and target are the same nodes."); |
218 "Flow source and target are the same nodes."); |
219 } |
219 } |
220 |
220 |
274 /// Sets the tolerance used by algorithm. |
274 /// Sets the tolerance used by algorithm. |
275 /// \return <tt>(*this)</tt> |
275 /// \return <tt>(*this)</tt> |
276 EdmondsKarp& tolerance(const Tolerance& tolerance) { |
276 EdmondsKarp& tolerance(const Tolerance& tolerance) { |
277 _tolerance = tolerance; |
277 _tolerance = tolerance; |
278 return *this; |
278 return *this; |
279 } |
279 } |
280 |
280 |
281 /// \brief Returns a const reference to the tolerance. |
281 /// \brief Returns a const reference to the tolerance. |
282 /// |
282 /// |
283 /// Returns a const reference to the tolerance object used by |
283 /// Returns a const reference to the tolerance object used by |
284 /// the algorithm. |
284 /// the algorithm. |
285 const Tolerance& tolerance() const { |
285 const Tolerance& tolerance() const { |
286 return _tolerance; |
286 return _tolerance; |
287 } |
287 } |
288 |
288 |
289 /// \name Execution control |
289 /// \name Execution control |
290 /// The simplest way to execute the algorithm is to use \ref run().\n |
290 /// The simplest way to execute the algorithm is to use \ref run().\n |
291 /// If you need better control on the initial solution or the execution, |
291 /// If you need better control on the initial solution or the execution, |
292 /// you have to call one of the \ref init() functions first, then |
292 /// you have to call one of the \ref init() functions first, then |
293 /// \ref start() or multiple times the \ref augment() function. |
293 /// \ref start() or multiple times the \ref augment() function. |
294 |
294 |
295 ///@{ |
295 ///@{ |
296 |
296 |
297 /// \brief Initializes the algorithm. |
297 /// \brief Initializes the algorithm. |
298 /// |
298 /// |
299 /// Initializes the internal data structures and sets the initial |
299 /// Initializes the internal data structures and sets the initial |
332 /// |
332 /// |
333 /// Initializes the internal data structures and sets the initial |
333 /// Initializes the internal data structures and sets the initial |
334 /// flow to the given \c flowMap. The \c flowMap should |
334 /// flow to the given \c flowMap. The \c flowMap should |
335 /// contain a feasible flow, i.e. at each node excluding the source |
335 /// contain a feasible flow, i.e. at each node excluding the source |
336 /// and the target, the incoming flow should be equal to the |
336 /// and the target, the incoming flow should be equal to the |
337 /// outgoing flow. |
337 /// outgoing flow. |
338 /// \return \c false when the given \c flowMap does not contain a |
338 /// \return \c false when the given \c flowMap does not contain a |
339 /// feasible flow. |
339 /// feasible flow. |
340 template <typename FlowMap> |
340 template <typename FlowMap> |
341 bool checkedInit(const FlowMap& flowMap) { |
341 bool checkedInit(const FlowMap& flowMap) { |
342 createStructures(); |
342 createStructures(); |
343 for (ArcIt e(_graph); e != INVALID; ++e) { |
343 for (ArcIt e(_graph); e != INVALID; ++e) { |
344 _flow->set(e, flowMap[e]); |
344 _flow->set(e, flowMap[e]); |
345 } |
345 } |
346 for (NodeIt it(_graph); it != INVALID; ++it) { |
346 for (NodeIt it(_graph); it != INVALID; ++it) { |
347 if (it == _source || it == _target) continue; |
347 if (it == _source || it == _target) continue; |
348 Value outFlow = 0; |
348 Value outFlow = 0; |
349 for (OutArcIt jt(_graph, it); jt != INVALID; ++jt) { |
349 for (OutArcIt jt(_graph, it); jt != INVALID; ++jt) { |
370 } |
370 } |
371 return true; |
371 return true; |
372 } |
372 } |
373 |
373 |
374 /// \brief Augments the solution along a shortest path. |
374 /// \brief Augments the solution along a shortest path. |
375 /// |
375 /// |
376 /// Augments the solution along a shortest path. This function searches a |
376 /// Augments the solution along a shortest path. This function searches a |
377 /// shortest path between the source and the target |
377 /// shortest path between the source and the target |
378 /// in the residual digraph by the Bfs algoritm. |
378 /// in the residual digraph by the Bfs algoritm. |
379 /// Then it increases the flow on this path with the minimal residual |
379 /// Then it increases the flow on this path with the minimal residual |
380 /// capacity on the path. If there is no such path, it gives back |
380 /// capacity on the path. If there is no such path, it gives back |
381 /// false. |
381 /// false. |
382 /// \return \c false when the augmenting did not success, i.e. the |
382 /// \return \c false when the augmenting did not success, i.e. the |
383 /// current flow is a feasible and optimal solution. |
383 /// current flow is a feasible and optimal solution. |
384 bool augment() { |
384 bool augment() { |
385 for (NodeIt n(_graph); n != INVALID; ++n) { |
385 for (NodeIt n(_graph); n != INVALID; ++n) { |
386 _pred->set(n, INVALID); |
386 _pred->set(n, INVALID); |
387 } |
387 } |
388 |
388 |
389 int first = 0, last = 1; |
389 int first = 0, last = 1; |
390 |
390 |
391 _queue[0] = _source; |
391 _queue[0] = _source; |
392 _pred->set(_source, OutArcIt(_graph, _source)); |
392 _pred->set(_source, OutArcIt(_graph, _source)); |
393 |
393 |
394 while (first != last && (*_pred)[_target] == INVALID) { |
394 while (first != last && (*_pred)[_target] == INVALID) { |
395 Node n = _queue[first++]; |
395 Node n = _queue[first++]; |
396 |
396 |
397 for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
397 for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
398 Value rem = (*_capacity)[e] - (*_flow)[e]; |
398 Value rem = (*_capacity)[e] - (*_flow)[e]; |
399 Node t = _graph.target(e); |
399 Node t = _graph.target(e); |
400 if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { |
400 if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { |
401 _pred->set(t, e); |
401 _pred->set(t, e); |
402 _queue[last++] = t; |
402 _queue[last++] = t; |
403 } |
403 } |
404 } |
404 } |
405 for (InArcIt e(_graph, n); e != INVALID; ++e) { |
405 for (InArcIt e(_graph, n); e != INVALID; ++e) { |
406 Value rem = (*_flow)[e]; |
406 Value rem = (*_flow)[e]; |
407 Node t = _graph.source(e); |
407 Node t = _graph.source(e); |
408 if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { |
408 if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { |
409 _pred->set(t, e); |
409 _pred->set(t, e); |
410 _queue[last++] = t; |
410 _queue[last++] = t; |
411 } |
411 } |
412 } |
412 } |
413 } |
413 } |
414 |
414 |
415 if ((*_pred)[_target] != INVALID) { |
415 if ((*_pred)[_target] != INVALID) { |
416 Node n = _target; |
416 Node n = _target; |
417 Arc e = (*_pred)[n]; |
417 Arc e = (*_pred)[n]; |
418 |
418 |
419 Value prem = (*_capacity)[e] - (*_flow)[e]; |
419 Value prem = (*_capacity)[e] - (*_flow)[e]; |
420 n = _graph.source(e); |
420 n = _graph.source(e); |
421 while (n != _source) { |
421 while (n != _source) { |
422 e = (*_pred)[n]; |
422 e = (*_pred)[n]; |
423 if (_graph.target(e) == n) { |
423 if (_graph.target(e) == n) { |
424 Value rem = (*_capacity)[e] - (*_flow)[e]; |
424 Value rem = (*_capacity)[e] - (*_flow)[e]; |
425 if (rem < prem) prem = rem; |
425 if (rem < prem) prem = rem; |
426 n = _graph.source(e); |
426 n = _graph.source(e); |
427 } else { |
427 } else { |
428 Value rem = (*_flow)[e]; |
428 Value rem = (*_flow)[e]; |
429 if (rem < prem) prem = rem; |
429 if (rem < prem) prem = rem; |
430 n = _graph.target(e); |
430 n = _graph.target(e); |
431 } |
431 } |
432 } |
432 } |
433 |
433 |
434 n = _target; |
434 n = _target; |
435 e = (*_pred)[n]; |
435 e = (*_pred)[n]; |
436 |
436 |
437 _flow->set(e, (*_flow)[e] + prem); |
437 _flow->set(e, (*_flow)[e] + prem); |
438 n = _graph.source(e); |
438 n = _graph.source(e); |
439 while (n != _source) { |
439 while (n != _source) { |
440 e = (*_pred)[n]; |
440 e = (*_pred)[n]; |
441 if (_graph.target(e) == n) { |
441 if (_graph.target(e) == n) { |
442 _flow->set(e, (*_flow)[e] + prem); |
442 _flow->set(e, (*_flow)[e] + prem); |
443 n = _graph.source(e); |
443 n = _graph.source(e); |
444 } else { |
444 } else { |
445 _flow->set(e, (*_flow)[e] - prem); |
445 _flow->set(e, (*_flow)[e] - prem); |
446 n = _graph.target(e); |
446 n = _graph.target(e); |
447 } |
447 } |
448 } |
448 } |
449 |
449 |
450 _flow_value += prem; |
450 _flow_value += prem; |
451 return true; |
451 return true; |
452 } else { |
452 } else { |
453 return false; |
453 return false; |
454 } |
454 } |
455 } |
455 } |
456 |
456 |
457 /// \brief Executes the algorithm |
457 /// \brief Executes the algorithm |
458 /// |
458 /// |
459 /// Executes the algorithm by performing augmenting phases until the |
459 /// Executes the algorithm by performing augmenting phases until the |
460 /// optimal solution is reached. |
460 /// optimal solution is reached. |
461 /// \pre One of the \ref init() functions must be called before |
461 /// \pre One of the \ref init() functions must be called before |
462 /// using this function. |
462 /// using this function. |
463 void start() { |
463 void start() { |
464 while (augment()) {} |
464 while (augment()) {} |
465 } |
465 } |
466 |
466 |
467 /// \brief Runs the algorithm. |
467 /// \brief Runs the algorithm. |
468 /// |
468 /// |
469 /// Runs the Edmonds-Karp algorithm. |
469 /// Runs the Edmonds-Karp algorithm. |
470 /// \note ek.run() is just a shortcut of the following code. |
470 /// \note ek.run() is just a shortcut of the following code. |
471 ///\code |
471 ///\code |
472 /// ek.init(); |
472 /// ek.init(); |
473 /// ek.start(); |
473 /// ek.start(); |
474 ///\endcode |
474 ///\endcode |
475 void run() { |
475 void run() { |
476 init(); |
476 init(); |