1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_CIRCULATION_H
20 #define LEMON_CIRCULATION_H
22 #include <lemon/tolerance.h>
23 #include <lemon/elevator.h>
27 ///\brief Push-relabel algorithm for finding a feasible circulation.
31 /// \brief Default traits class of Circulation class.
33 /// Default traits class of Circulation class.
35 /// \tparam GR Type of the digraph the algorithm runs on.
36 /// \tparam LM The type of the lower bound map.
37 /// \tparam UM The type of the upper bound (capacity) map.
38 /// \tparam SM The type of the supply map.
39 template <typename GR, typename LM,
40 typename UM, typename SM>
41 struct CirculationDefaultTraits {
43 /// \brief The type of the digraph the algorithm runs on.
46 /// \brief The type of the lower bound map.
48 /// The type of the map that stores the lower bounds on the arcs.
49 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
52 /// \brief The type of the upper bound (capacity) map.
54 /// The type of the map that stores the upper bounds (capacities)
56 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
59 /// \brief The type of supply map.
61 /// The type of the map that stores the signed supply values of the
63 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
66 /// \brief The type of the flow values.
67 typedef typename SupplyMap::Value Flow;
69 /// \brief The type of the map that stores the flow values.
71 /// The type of the map that stores the flow values.
72 /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
74 typedef typename Digraph::template ArcMap<Flow> FlowMap;
76 /// \brief Instantiates a FlowMap.
78 /// This function instantiates a \ref FlowMap.
79 /// \param digraph The digraph for which we would like to define
81 static FlowMap* createFlowMap(const Digraph& digraph) {
82 return new FlowMap(digraph);
85 /// \brief The elevator type used by the algorithm.
87 /// The elevator type used by the algorithm.
90 /// \sa LinkedElevator
91 typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
93 /// \brief Instantiates an Elevator.
95 /// This function instantiates an \ref Elevator.
96 /// \param digraph The digraph for which we would like to define
98 /// \param max_level The maximum level of the elevator.
99 static Elevator* createElevator(const Digraph& digraph, int max_level) {
100 return new Elevator(digraph, max_level);
103 /// \brief The tolerance used by the algorithm
105 /// The tolerance used by the algorithm to handle inexact computation.
106 typedef lemon::Tolerance<Flow> Tolerance;
111 \brief Push-relabel algorithm for the network circulation problem.
114 This class implements a push-relabel algorithm for the \e network
115 \e circulation problem.
116 It is to find a feasible circulation when lower and upper bounds
117 are given for the flow values on the arcs and lower bounds are
118 given for the difference between the outgoing and incoming flow
121 The exact formulation of this problem is the following.
122 Let \f$G=(V,A)\f$ be a digraph,
123 \f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$ denote the lower and
124 upper bounds on the arcs, for which \f$0 \leq lower(uv) \leq upper(uv)\f$
125 holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
126 denotes the signed supply values of the nodes.
127 If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$
128 supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with
129 \f$-sup(u)\f$ demand.
130 A feasible circulation is an \f$f: A\rightarrow\mathbf{R}^+_0\f$
131 solution of the following problem.
133 \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
134 \geq sup(u) \quad \forall u\in V, \f]
135 \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f]
137 The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
138 zero or negative in order to have a feasible solution (since the sum
139 of the expressions on the left-hand side of the inequalities is zero).
140 It means that the total demand must be greater or equal to the total
141 supply and all the supplies have to be carried out from the supply nodes,
142 but there could be demands that are not satisfied.
143 If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
144 constraints have to be satisfied with equality, i.e. all demands
145 have to be satisfied and all supplies have to be used.
147 If you need the opposite inequalities in the supply/demand constraints
148 (i.e. the total demand is less than the total supply and all the demands
149 have to be satisfied while there could be supplies that are not used),
150 then you could easily transform the problem to the above form by reversing
151 the direction of the arcs and taking the negative of the supply values
152 (e.g. using \ref ReverseDigraph and \ref NegMap adaptors).
154 Note that this algorithm also provides a feasible solution for the
155 \ref min_cost_flow "minimum cost flow problem".
157 \tparam GR The type of the digraph the algorithm runs on.
158 \tparam LM The type of the lower bound map. The default
159 map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
160 \tparam UM The type of the upper bound (capacity) map.
161 The default map type is \c LM.
162 \tparam SM The type of the supply map. The default map type is
163 \ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>".
166 template< typename GR,
172 template< typename GR,
173 typename LM = typename GR::template ArcMap<int>,
175 typename SM = typename GR::template NodeMap<typename UM::Value>,
176 typename TR = CirculationDefaultTraits<GR, LM, UM, SM> >
181 ///The \ref CirculationDefaultTraits "traits class" of the algorithm.
183 ///The type of the digraph the algorithm runs on.
184 typedef typename Traits::Digraph Digraph;
185 ///The type of the flow values.
186 typedef typename Traits::Flow Flow;
188 ///The type of the lower bound map.
189 typedef typename Traits::LowerMap LowerMap;
190 ///The type of the upper bound (capacity) map.
191 typedef typename Traits::UpperMap UpperMap;
192 ///The type of the supply map.
193 typedef typename Traits::SupplyMap SupplyMap;
194 ///The type of the flow map.
195 typedef typename Traits::FlowMap FlowMap;
197 ///The type of the elevator.
198 typedef typename Traits::Elevator Elevator;
199 ///The type of the tolerance.
200 typedef typename Traits::Tolerance Tolerance;
204 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
211 const SupplyMap *_supply;
219 typedef typename Digraph::template NodeMap<Flow> ExcessMap;
227 typedef Circulation Create;
229 ///\name Named Template Parameters
233 template <typename T>
234 struct SetFlowMapTraits : public Traits {
236 static FlowMap *createFlowMap(const Digraph&) {
237 LEMON_ASSERT(false, "FlowMap is not initialized");
238 return 0; // ignore warnings
242 /// \brief \ref named-templ-param "Named parameter" for setting
245 /// \ref named-templ-param "Named parameter" for setting FlowMap
247 template <typename T>
249 : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
250 SetFlowMapTraits<T> > {
251 typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
252 SetFlowMapTraits<T> > Create;
255 template <typename T>
256 struct SetElevatorTraits : public Traits {
258 static Elevator *createElevator(const Digraph&, int) {
259 LEMON_ASSERT(false, "Elevator is not initialized");
260 return 0; // ignore warnings
264 /// \brief \ref named-templ-param "Named parameter" for setting
267 /// \ref named-templ-param "Named parameter" for setting Elevator
268 /// type. If this named parameter is used, then an external
269 /// elevator object must be passed to the algorithm using the
270 /// \ref elevator(Elevator&) "elevator()" function before calling
271 /// \ref run() or \ref init().
272 /// \sa SetStandardElevator
273 template <typename T>
275 : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
276 SetElevatorTraits<T> > {
277 typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
278 SetElevatorTraits<T> > Create;
281 template <typename T>
282 struct SetStandardElevatorTraits : public Traits {
284 static Elevator *createElevator(const Digraph& digraph, int max_level) {
285 return new Elevator(digraph, max_level);
289 /// \brief \ref named-templ-param "Named parameter" for setting
290 /// Elevator type with automatic allocation
292 /// \ref named-templ-param "Named parameter" for setting Elevator
293 /// type with automatic allocation.
294 /// The Elevator should have standard constructor interface to be
295 /// able to automatically created by the algorithm (i.e. the
296 /// digraph and the maximum level should be passed to it).
297 /// However an external elevator object could also be passed to the
298 /// algorithm with the \ref elevator(Elevator&) "elevator()" function
299 /// before calling \ref run() or \ref init().
301 template <typename T>
302 struct SetStandardElevator
303 : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
304 SetStandardElevatorTraits<T> > {
305 typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
306 SetStandardElevatorTraits<T> > Create;
319 /// The constructor of the class.
321 /// \param graph The digraph the algorithm runs on.
322 /// \param lower The lower bounds for the flow values on the arcs.
323 /// \param upper The upper bounds (capacities) for the flow values
325 /// \param supply The signed supply values of the nodes.
326 Circulation(const Digraph &graph, const LowerMap &lower,
327 const UpperMap &upper, const SupplyMap &supply)
328 : _g(graph), _lo(&lower), _up(&upper), _supply(&supply),
329 _flow(NULL), _local_flow(false), _level(NULL), _local_level(false),
340 void createStructures() {
341 _node_num = _el = countNodes(_g);
344 _flow = Traits::createFlowMap(_g);
348 _level = Traits::createElevator(_g, _node_num);
352 _excess = new ExcessMap(_g);
356 void destroyStructures() {
370 /// Sets the lower bound map.
372 /// Sets the lower bound map.
373 /// \return <tt>(*this)</tt>
374 Circulation& lowerMap(const LowerMap& map) {
379 /// Sets the upper bound (capacity) map.
381 /// Sets the upper bound (capacity) map.
382 /// \return <tt>(*this)</tt>
383 Circulation& upperMap(const LowerMap& map) {
388 /// Sets the supply map.
390 /// Sets the supply map.
391 /// \return <tt>(*this)</tt>
392 Circulation& supplyMap(const SupplyMap& map) {
397 /// \brief Sets the flow map.
399 /// Sets the flow map.
400 /// If you don't use this function before calling \ref run() or
401 /// \ref init(), an instance will be allocated automatically.
402 /// The destructor deallocates this automatically allocated map,
404 /// \return <tt>(*this)</tt>
405 Circulation& flowMap(FlowMap& map) {
414 /// \brief Sets the elevator used by algorithm.
416 /// Sets the elevator used by algorithm.
417 /// If you don't use this function before calling \ref run() or
418 /// \ref init(), an instance will be allocated automatically.
419 /// The destructor deallocates this automatically allocated elevator,
421 /// \return <tt>(*this)</tt>
422 Circulation& elevator(Elevator& elevator) {
425 _local_level = false;
431 /// \brief Returns a const reference to the elevator.
433 /// Returns a const reference to the elevator.
435 /// \pre Either \ref run() or \ref init() must be called before
436 /// using this function.
437 const Elevator& elevator() const {
441 /// \brief Sets the tolerance used by algorithm.
443 /// Sets the tolerance used by algorithm.
444 Circulation& tolerance(const Tolerance& tolerance) const {
449 /// \brief Returns a const reference to the tolerance.
451 /// Returns a const reference to the tolerance.
452 const Tolerance& tolerance() const {
456 /// \name Execution Control
457 /// The simplest way to execute the algorithm is to call \ref run().\n
458 /// If you need more control on the initial solution or the execution,
459 /// first you have to call one of the \ref init() functions, then
460 /// the \ref start() function.
464 /// Initializes the internal data structures.
466 /// Initializes the internal data structures and sets all flow values
467 /// to the lower bound.
472 for(NodeIt n(_g);n!=INVALID;++n) {
473 (*_excess)[n] = (*_supply)[n];
476 for (ArcIt e(_g);e!=INVALID;++e) {
477 _flow->set(e, (*_lo)[e]);
478 (*_excess)[_g.target(e)] += (*_flow)[e];
479 (*_excess)[_g.source(e)] -= (*_flow)[e];
482 // global relabeling tested, but in general case it provides
483 // worse performance for random digraphs
485 for(NodeIt n(_g);n!=INVALID;++n)
486 _level->initAddItem(n);
487 _level->initFinish();
488 for(NodeIt n(_g);n!=INVALID;++n)
489 if(_tol.positive((*_excess)[n]))
493 /// Initializes the internal data structures using a greedy approach.
495 /// Initializes the internal data structures using a greedy approach
496 /// to construct the initial solution.
501 for(NodeIt n(_g);n!=INVALID;++n) {
502 (*_excess)[n] = (*_supply)[n];
505 for (ArcIt e(_g);e!=INVALID;++e) {
506 if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) {
507 _flow->set(e, (*_up)[e]);
508 (*_excess)[_g.target(e)] += (*_up)[e];
509 (*_excess)[_g.source(e)] -= (*_up)[e];
510 } else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) {
511 _flow->set(e, (*_lo)[e]);
512 (*_excess)[_g.target(e)] += (*_lo)[e];
513 (*_excess)[_g.source(e)] -= (*_lo)[e];
515 Flow fc = -(*_excess)[_g.target(e)];
517 (*_excess)[_g.target(e)] = 0;
518 (*_excess)[_g.source(e)] -= fc;
523 for(NodeIt n(_g);n!=INVALID;++n)
524 _level->initAddItem(n);
525 _level->initFinish();
526 for(NodeIt n(_g);n!=INVALID;++n)
527 if(_tol.positive((*_excess)[n]))
531 ///Executes the algorithm
533 ///This function executes the algorithm.
535 ///\return \c true if a feasible circulation is found.
544 Node last_activated=INVALID;
545 while((act=_level->highestActive())!=INVALID) {
546 int actlevel=(*_level)[act];
547 int mlevel=_node_num;
548 Flow exc=(*_excess)[act];
550 for(OutArcIt e(_g,act);e!=INVALID; ++e) {
551 Node v = _g.target(e);
552 Flow fc=(*_up)[e]-(*_flow)[e];
553 if(!_tol.positive(fc)) continue;
554 if((*_level)[v]<actlevel) {
555 if(!_tol.less(fc, exc)) {
556 _flow->set(e, (*_flow)[e] + exc);
557 (*_excess)[v] += exc;
558 if(!_level->active(v) && _tol.positive((*_excess)[v]))
561 _level->deactivate(act);
565 _flow->set(e, (*_up)[e]);
567 if(!_level->active(v) && _tol.positive((*_excess)[v]))
572 else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
574 for(InArcIt e(_g,act);e!=INVALID; ++e) {
575 Node v = _g.source(e);
576 Flow fc=(*_flow)[e]-(*_lo)[e];
577 if(!_tol.positive(fc)) continue;
578 if((*_level)[v]<actlevel) {
579 if(!_tol.less(fc, exc)) {
580 _flow->set(e, (*_flow)[e] - exc);
581 (*_excess)[v] += exc;
582 if(!_level->active(v) && _tol.positive((*_excess)[v]))
585 _level->deactivate(act);
589 _flow->set(e, (*_lo)[e]);
591 if(!_level->active(v) && _tol.positive((*_excess)[v]))
596 else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
599 (*_excess)[act] = exc;
600 if(!_tol.positive(exc)) _level->deactivate(act);
601 else if(mlevel==_node_num) {
602 _level->liftHighestActiveToTop();
607 _level->liftHighestActive(mlevel+1);
608 if(_level->onLevel(actlevel)==0) {
619 /// Runs the algorithm.
621 /// This function runs the algorithm.
623 /// \return \c true if a feasible circulation is found.
625 /// \note Apart from the return value, c.run() is just a shortcut of
626 /// the following code.
638 /// \name Query Functions
639 /// The results of the circulation algorithm can be obtained using
640 /// these functions.\n
641 /// Either \ref run() or \ref start() should be called before
646 /// \brief Returns the flow on the given arc.
648 /// Returns the flow on the given arc.
650 /// \pre Either \ref run() or \ref init() must be called before
651 /// using this function.
652 Flow flow(const Arc& arc) const {
653 return (*_flow)[arc];
656 /// \brief Returns a const reference to the flow map.
658 /// Returns a const reference to the arc map storing the found flow.
660 /// \pre Either \ref run() or \ref init() must be called before
661 /// using this function.
662 const FlowMap& flowMap() const {
667 \brief Returns \c true if the given node is in a barrier.
669 Barrier is a set \e B of nodes for which
671 \f[ \sum_{uv\in A: u\in B} upper(uv) -
672 \sum_{uv\in A: v\in B} lower(uv) < \sum_{v\in B} sup(v) \f]
674 holds. The existence of a set with this property prooves that a
675 feasible circualtion cannot exist.
677 This function returns \c true if the given node is in the found
678 barrier. If a feasible circulation is found, the function
679 gives back \c false for every node.
681 \pre Either \ref run() or \ref init() must be called before
687 bool barrier(const Node& node) const
689 return (*_level)[node] >= _el;
692 /// \brief Gives back a barrier.
694 /// This function sets \c bar to the characteristic vector of the
695 /// found barrier. \c bar should be a \ref concepts::WriteMap "writable"
696 /// node map with \c bool (or convertible) value type.
698 /// If a feasible circulation is found, the function gives back an
699 /// empty set, so \c bar[v] will be \c false for all nodes \c v.
701 /// \note This function calls \ref barrier() for each node,
702 /// so it runs in O(n) time.
704 /// \pre Either \ref run() or \ref init() must be called before
705 /// using this function.
708 /// \sa checkBarrier()
709 template<class BarrierMap>
710 void barrierMap(BarrierMap &bar) const
712 for(NodeIt n(_g);n!=INVALID;++n)
713 bar.set(n, (*_level)[n] >= _el);
718 /// \name Checker Functions
719 /// The feasibility of the results can be checked using
720 /// these functions.\n
721 /// Either \ref run() or \ref start() should be called before
726 ///Check if the found flow is a feasible circulation
728 ///Check if the found flow is a feasible circulation,
730 bool checkFlow() const {
731 for(ArcIt e(_g);e!=INVALID;++e)
732 if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false;
733 for(NodeIt n(_g);n!=INVALID;++n)
735 Flow dif=-(*_supply)[n];
736 for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e];
737 for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e];
738 if(_tol.negative(dif)) return false;
743 ///Check whether or not the last execution provides a barrier
745 ///Check whether or not the last execution provides a barrier.
748 bool checkBarrier() const
751 for(NodeIt n(_g);n!=INVALID;++n)
753 delta-=(*_supply)[n];
754 for(ArcIt e(_g);e!=INVALID;++e)
758 if(barrier(s)&&!barrier(t)) delta+=(*_up)[e];
759 else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e];
761 return _tol.negative(delta);