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.
34 /// \tparam _Diraph Digraph type.
35 /// \tparam _LCapMap Lower bound capacity map type.
36 /// \tparam _UCapMap Upper bound capacity map type.
37 /// \tparam _DeltaMap Delta map type.
38 template <typename _Diraph, typename _LCapMap,
39 typename _UCapMap, typename _DeltaMap>
40 struct CirculationDefaultTraits {
42 /// \brief The type of the digraph the algorithm runs on.
43 typedef _Diraph Digraph;
45 /// \brief The type of the map that stores the circulation lower
48 /// The type of the map that stores the circulation lower bound.
49 /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
50 typedef _LCapMap LCapMap;
52 /// \brief The type of the map that stores the circulation upper
55 /// The type of the map that stores the circulation upper bound.
56 /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
57 typedef _UCapMap UCapMap;
59 /// \brief The type of the map that stores the lower bound for
60 /// the supply of the nodes.
62 /// The type of the map that stores the lower bound for the supply
63 /// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap"
65 typedef _DeltaMap DeltaMap;
67 /// \brief The type of the flow values.
68 typedef typename DeltaMap::Value Value;
70 /// \brief The type of the map that stores the flow values.
72 /// The type of the map that stores the flow values.
73 /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
74 typedef typename Digraph::template ArcMap<Value> FlowMap;
76 /// \brief Instantiates a FlowMap.
78 /// This function instantiates a \ref FlowMap.
79 /// \param digraph The digraph, to 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, to 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<Value> Tolerance;
111 \brief Push-relabel algorithm for the network circulation problem.
114 This class implements a push-relabel algorithm for the network
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
118 are given for the supply values of the nodes.
120 The exact formulation of this problem is the following.
121 Let \f$G=(V,A)\f$ be a digraph,
122 \f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$,
123 \f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation
124 \f$f: A\rightarrow\mathbf{R}^+_0\f$ so that
125 \f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a)
126 \geq delta(v) \quad \forall v\in V, \f]
127 \f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f]
128 \note \f$delta(v)\f$ specifies a lower bound for the supply of node
129 \f$v\f$. It can be either positive or negative, however note that
130 \f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to
131 have a feasible solution.
133 \note A special case of this problem is when
134 \f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$
135 will be \e equal \e to \f$delta(v)\f$, if a circulation can be found.
136 Thus a feasible solution for the
137 \ref min_cost_flow "minimum cost flow" problem can be calculated
140 \tparam _Digraph The type of the digraph the algorithm runs on.
141 \tparam _LCapMap The type of the lower bound capacity map. The default
142 map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>".
143 \tparam _UCapMap The type of the upper bound capacity map. The default
144 map type is \c _LCapMap.
145 \tparam _DeltaMap The type of the map that stores the lower bound
146 for the supply of the nodes. The default map type is
147 \c _Digraph::ArcMap<_UCapMap::Value>.
150 template< typename _Digraph,
156 template< typename _Digraph,
157 typename _LCapMap = typename _Digraph::template ArcMap<int>,
158 typename _UCapMap = _LCapMap,
159 typename _DeltaMap = typename _Digraph::
160 template NodeMap<typename _UCapMap::Value>,
161 typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap,
162 _UCapMap, _DeltaMap> >
167 ///The \ref CirculationDefaultTraits "traits class" of the algorithm.
168 typedef _Traits Traits;
169 ///The type of the digraph the algorithm runs on.
170 typedef typename Traits::Digraph Digraph;
171 ///The type of the flow values.
172 typedef typename Traits::Value Value;
174 /// The type of the lower bound capacity map.
175 typedef typename Traits::LCapMap LCapMap;
176 /// The type of the upper bound capacity map.
177 typedef typename Traits::UCapMap UCapMap;
178 /// \brief The type of the map that stores the lower bound for
179 /// the supply of the nodes.
180 typedef typename Traits::DeltaMap DeltaMap;
181 ///The type of the flow map.
182 typedef typename Traits::FlowMap FlowMap;
184 ///The type of the elevator.
185 typedef typename Traits::Elevator Elevator;
186 ///The type of the tolerance.
187 typedef typename Traits::Tolerance Tolerance;
191 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
198 const DeltaMap *_delta;
206 typedef typename Digraph::template NodeMap<Value> ExcessMap;
214 typedef Circulation Create;
216 ///\name Named Template Parameters
220 template <typename _FlowMap>
221 struct SetFlowMapTraits : public Traits {
222 typedef _FlowMap FlowMap;
223 static FlowMap *createFlowMap(const Digraph&) {
224 LEMON_ASSERT(false, "FlowMap is not initialized");
225 return 0; // ignore warnings
229 /// \brief \ref named-templ-param "Named parameter" for setting
232 /// \ref named-templ-param "Named parameter" for setting FlowMap
234 template <typename _FlowMap>
236 : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
237 SetFlowMapTraits<_FlowMap> > {
238 typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
239 SetFlowMapTraits<_FlowMap> > Create;
242 template <typename _Elevator>
243 struct SetElevatorTraits : public Traits {
244 typedef _Elevator Elevator;
245 static Elevator *createElevator(const Digraph&, int) {
246 LEMON_ASSERT(false, "Elevator is not initialized");
247 return 0; // ignore warnings
251 /// \brief \ref named-templ-param "Named parameter" for setting
254 /// \ref named-templ-param "Named parameter" for setting Elevator
255 /// type. If this named parameter is used, then an external
256 /// elevator object must be passed to the algorithm using the
257 /// \ref elevator(Elevator&) "elevator()" function before calling
258 /// \ref run() or \ref init().
259 /// \sa SetStandardElevator
260 template <typename _Elevator>
262 : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
263 SetElevatorTraits<_Elevator> > {
264 typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
265 SetElevatorTraits<_Elevator> > Create;
268 template <typename _Elevator>
269 struct SetStandardElevatorTraits : public Traits {
270 typedef _Elevator Elevator;
271 static Elevator *createElevator(const Digraph& digraph, int max_level) {
272 return new Elevator(digraph, max_level);
276 /// \brief \ref named-templ-param "Named parameter" for setting
277 /// Elevator type with automatic allocation
279 /// \ref named-templ-param "Named parameter" for setting Elevator
280 /// type with automatic allocation.
281 /// The Elevator should have standard constructor interface to be
282 /// able to automatically created by the algorithm (i.e. the
283 /// digraph and the maximum level should be passed to it).
284 /// However an external elevator object could also be passed to the
285 /// algorithm with the \ref elevator(Elevator&) "elevator()" function
286 /// before calling \ref run() or \ref init().
288 template <typename _Elevator>
289 struct SetStandardElevator
290 : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
291 SetStandardElevatorTraits<_Elevator> > {
292 typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
293 SetStandardElevatorTraits<_Elevator> > Create;
304 /// The constructor of the class.
306 /// The constructor of the class.
307 /// \param g The digraph the algorithm runs on.
308 /// \param lo The lower bound capacity of the arcs.
309 /// \param up The upper bound capacity of the arcs.
310 /// \param delta The lower bound for the supply of the nodes.
311 Circulation(const Digraph &g,const LCapMap &lo,
312 const UCapMap &up,const DeltaMap &delta)
313 : _g(g), _node_num(),
314 _lo(&lo),_up(&up),_delta(&delta),_flow(0),_local_flow(false),
315 _level(0), _local_level(false), _excess(0), _el() {}
325 void createStructures() {
326 _node_num = _el = countNodes(_g);
329 _flow = Traits::createFlowMap(_g);
333 _level = Traits::createElevator(_g, _node_num);
337 _excess = new ExcessMap(_g);
341 void destroyStructures() {
355 /// Sets the lower bound capacity map.
357 /// Sets the lower bound capacity map.
358 /// \return <tt>(*this)</tt>
359 Circulation& lowerCapMap(const LCapMap& map) {
364 /// Sets the upper bound capacity map.
366 /// Sets the upper bound capacity map.
367 /// \return <tt>(*this)</tt>
368 Circulation& upperCapMap(const LCapMap& map) {
373 /// Sets the lower bound map for the supply of the nodes.
375 /// Sets the lower bound map for the supply of the nodes.
376 /// \return <tt>(*this)</tt>
377 Circulation& deltaMap(const DeltaMap& map) {
382 /// \brief Sets the flow map.
384 /// Sets the flow map.
385 /// If you don't use this function before calling \ref run() or
386 /// \ref init(), an instance will be allocated automatically.
387 /// The destructor deallocates this automatically allocated map,
389 /// \return <tt>(*this)</tt>
390 Circulation& flowMap(FlowMap& map) {
399 /// \brief Sets the elevator used by algorithm.
401 /// Sets the elevator used by algorithm.
402 /// If you don't use this function before calling \ref run() or
403 /// \ref init(), an instance will be allocated automatically.
404 /// The destructor deallocates this automatically allocated elevator,
406 /// \return <tt>(*this)</tt>
407 Circulation& elevator(Elevator& elevator) {
410 _local_level = false;
416 /// \brief Returns a const reference to the elevator.
418 /// Returns a const reference to the elevator.
420 /// \pre Either \ref run() or \ref init() must be called before
421 /// using this function.
422 const Elevator& elevator() const {
426 /// \brief Sets the tolerance used by algorithm.
428 /// Sets the tolerance used by algorithm.
429 Circulation& tolerance(const Tolerance& tolerance) const {
434 /// \brief Returns a const reference to the tolerance.
436 /// Returns a const reference to the tolerance.
437 const Tolerance& tolerance() const {
441 /// \name Execution Control
442 /// The simplest way to execute the algorithm is to call \ref run().\n
443 /// If you need more control on the initial solution or the execution,
444 /// first you have to call one of the \ref init() functions, then
445 /// the \ref start() function.
449 /// Initializes the internal data structures.
451 /// Initializes the internal data structures and sets all flow values
452 /// to the lower bound.
457 for(NodeIt n(_g);n!=INVALID;++n) {
458 _excess->set(n, (*_delta)[n]);
461 for (ArcIt e(_g);e!=INVALID;++e) {
462 _flow->set(e, (*_lo)[e]);
463 _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]);
464 _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]);
467 // global relabeling tested, but in general case it provides
468 // worse performance for random digraphs
470 for(NodeIt n(_g);n!=INVALID;++n)
471 _level->initAddItem(n);
472 _level->initFinish();
473 for(NodeIt n(_g);n!=INVALID;++n)
474 if(_tol.positive((*_excess)[n]))
478 /// Initializes the internal data structures using a greedy approach.
480 /// Initializes the internal data structures using a greedy approach
481 /// to construct the initial solution.
486 for(NodeIt n(_g);n!=INVALID;++n) {
487 _excess->set(n, (*_delta)[n]);
490 for (ArcIt e(_g);e!=INVALID;++e) {
491 if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) {
492 _flow->set(e, (*_up)[e]);
493 _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]);
494 _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]);
495 } else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) {
496 _flow->set(e, (*_lo)[e]);
497 _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]);
498 _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]);
500 Value fc = -(*_excess)[_g.target(e)];
502 _excess->set(_g.target(e), 0);
503 _excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc);
508 for(NodeIt n(_g);n!=INVALID;++n)
509 _level->initAddItem(n);
510 _level->initFinish();
511 for(NodeIt n(_g);n!=INVALID;++n)
512 if(_tol.positive((*_excess)[n]))
516 ///Executes the algorithm
518 ///This function executes the algorithm.
520 ///\return \c true if a feasible circulation is found.
529 Node last_activated=INVALID;
530 while((act=_level->highestActive())!=INVALID) {
531 int actlevel=(*_level)[act];
532 int mlevel=_node_num;
533 Value exc=(*_excess)[act];
535 for(OutArcIt e(_g,act);e!=INVALID; ++e) {
536 Node v = _g.target(e);
537 Value fc=(*_up)[e]-(*_flow)[e];
538 if(!_tol.positive(fc)) continue;
539 if((*_level)[v]<actlevel) {
540 if(!_tol.less(fc, exc)) {
541 _flow->set(e, (*_flow)[e] + exc);
542 _excess->set(v, (*_excess)[v] + exc);
543 if(!_level->active(v) && _tol.positive((*_excess)[v]))
546 _level->deactivate(act);
550 _flow->set(e, (*_up)[e]);
551 _excess->set(v, (*_excess)[v] + fc);
552 if(!_level->active(v) && _tol.positive((*_excess)[v]))
557 else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
559 for(InArcIt e(_g,act);e!=INVALID; ++e) {
560 Node v = _g.source(e);
561 Value fc=(*_flow)[e]-(*_lo)[e];
562 if(!_tol.positive(fc)) continue;
563 if((*_level)[v]<actlevel) {
564 if(!_tol.less(fc, exc)) {
565 _flow->set(e, (*_flow)[e] - exc);
566 _excess->set(v, (*_excess)[v] + exc);
567 if(!_level->active(v) && _tol.positive((*_excess)[v]))
570 _level->deactivate(act);
574 _flow->set(e, (*_lo)[e]);
575 _excess->set(v, (*_excess)[v] + fc);
576 if(!_level->active(v) && _tol.positive((*_excess)[v]))
581 else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
584 _excess->set(act, exc);
585 if(!_tol.positive(exc)) _level->deactivate(act);
586 else if(mlevel==_node_num) {
587 _level->liftHighestActiveToTop();
592 _level->liftHighestActive(mlevel+1);
593 if(_level->onLevel(actlevel)==0) {
604 /// Runs the algorithm.
606 /// This function runs the algorithm.
608 /// \return \c true if a feasible circulation is found.
610 /// \note Apart from the return value, c.run() is just a shortcut of
611 /// the following code.
623 /// \name Query Functions
624 /// The results of the circulation algorithm can be obtained using
625 /// these functions.\n
626 /// Either \ref run() or \ref start() should be called before
631 /// \brief Returns the flow on the given arc.
633 /// Returns the flow on the given arc.
635 /// \pre Either \ref run() or \ref init() must be called before
636 /// using this function.
637 Value flow(const Arc& arc) const {
638 return (*_flow)[arc];
641 /// \brief Returns a const reference to the flow map.
643 /// Returns a const reference to the arc map storing the found flow.
645 /// \pre Either \ref run() or \ref init() must be called before
646 /// using this function.
647 const FlowMap& flowMap() const {
652 \brief Returns \c true if the given node is in a barrier.
654 Barrier is a set \e B of nodes for which
656 \f[ \sum_{a\in\delta_{out}(B)} upper(a) -
657 \sum_{a\in\delta_{in}(B)} lower(a) < \sum_{v\in B}delta(v) \f]
659 holds. The existence of a set with this property prooves that a
660 feasible circualtion cannot exist.
662 This function returns \c true if the given node is in the found
663 barrier. If a feasible circulation is found, the function
664 gives back \c false for every node.
666 \pre Either \ref run() or \ref init() must be called before
672 bool barrier(const Node& node) const
674 return (*_level)[node] >= _el;
677 /// \brief Gives back a barrier.
679 /// This function sets \c bar to the characteristic vector of the
680 /// found barrier. \c bar should be a \ref concepts::WriteMap "writable"
681 /// node map with \c bool (or convertible) value type.
683 /// If a feasible circulation is found, the function gives back an
684 /// empty set, so \c bar[v] will be \c false for all nodes \c v.
686 /// \note This function calls \ref barrier() for each node,
687 /// so it runs in \f$O(n)\f$ time.
689 /// \pre Either \ref run() or \ref init() must be called before
690 /// using this function.
693 /// \sa checkBarrier()
694 template<class BarrierMap>
695 void barrierMap(BarrierMap &bar) const
697 for(NodeIt n(_g);n!=INVALID;++n)
698 bar.set(n, (*_level)[n] >= _el);
703 /// \name Checker Functions
704 /// The feasibility of the results can be checked using
705 /// these functions.\n
706 /// Either \ref run() or \ref start() should be called before
711 ///Check if the found flow is a feasible circulation
713 ///Check if the found flow is a feasible circulation,
715 bool checkFlow() const {
716 for(ArcIt e(_g);e!=INVALID;++e)
717 if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false;
718 for(NodeIt n(_g);n!=INVALID;++n)
720 Value dif=-(*_delta)[n];
721 for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e];
722 for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e];
723 if(_tol.negative(dif)) return false;
728 ///Check whether or not the last execution provides a barrier
730 ///Check whether or not the last execution provides a barrier.
733 bool checkBarrier() const
736 for(NodeIt n(_g);n!=INVALID;++n)
739 for(ArcIt e(_g);e!=INVALID;++e)
743 if(barrier(s)&&!barrier(t)) delta+=(*_up)[e];
744 else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e];
746 return _tol.negative(delta);