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_PREFLOW_H
20 #define LEMON_PREFLOW_H
22 #include <lemon/tolerance.h>
23 #include <lemon/elevator.h>
27 /// \brief Implementation of the preflow algorithm.
31 /// \brief Default traits class of Preflow class.
33 /// Default traits class of Preflow class.
34 /// \tparam GR Digraph type.
35 /// \tparam CAP Capacity map type.
36 template <typename GR, typename CAP>
37 struct PreflowDefaultTraits {
39 /// \brief The type of the digraph the algorithm runs on.
42 /// \brief 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.
46 typedef CAP CapacityMap;
48 /// \brief The type of the flow values.
49 typedef typename CapacityMap::Value Value;
51 /// \brief The type of the map 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.
56 typedef GR::ArcMap<Value> FlowMap;
58 typedef typename Digraph::template ArcMap<Value> FlowMap;
61 /// \brief Instantiates a FlowMap.
63 /// This function instantiates a \ref FlowMap.
64 /// \param digraph The digraph for which we would like to define
66 static FlowMap* createFlowMap(const Digraph& digraph) {
67 return new FlowMap(digraph);
70 /// \brief The elevator type used by Preflow algorithm.
72 /// The elevator type used by Preflow algorithm.
74 /// \sa Elevator, LinkedElevator
76 typedef lemon::Elevator<GR, GR::Node> Elevator;
78 typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
81 /// \brief Instantiates an Elevator.
83 /// This function instantiates an \ref Elevator.
84 /// \param digraph The digraph for which we would like to define
86 /// \param max_level The maximum level of the elevator.
87 static Elevator* createElevator(const Digraph& digraph, int max_level) {
88 return new Elevator(digraph, max_level);
91 /// \brief The tolerance used by the algorithm
93 /// The tolerance used by the algorithm to handle inexact computation.
94 typedef lemon::Tolerance<Value> Tolerance;
101 /// \brief %Preflow algorithm class.
103 /// This class provides an implementation of Goldberg-Tarjan's \e preflow
104 /// \e push-relabel algorithm producing a \ref max_flow
105 /// "flow of maximum value" in a digraph \ref clrs01algorithms,
106 /// \ref amo93networkflows, \ref goldberg88newapproach.
107 /// The preflow algorithms are the fastest known maximum
108 /// flow algorithms. The current implementation uses a mixture of the
109 /// \e "highest label" and the \e "bound decrease" heuristics.
110 /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
112 /// The algorithm consists of two phases. After the first phase
113 /// the maximum flow value and the minimum cut is obtained. The
114 /// second phase constructs a feasible maximum flow on each arc.
116 /// \warning This implementation cannot handle infinite or very large
117 /// capacities (e.g. the maximum value of \c CAP::Value).
119 /// \tparam GR The type of the digraph the algorithm runs on.
120 /// \tparam CAP The type of the capacity map. The default map
121 /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
123 template <typename GR, typename CAP, typename TR>
125 template <typename GR,
126 typename CAP = typename GR::template ArcMap<int>,
127 typename TR = PreflowDefaultTraits<GR, CAP> >
132 ///The \ref PreflowDefaultTraits "traits class" of the algorithm.
134 ///The type of the digraph the algorithm runs on.
135 typedef typename Traits::Digraph Digraph;
136 ///The type of the capacity map.
137 typedef typename Traits::CapacityMap CapacityMap;
138 ///The type of the flow values.
139 typedef typename Traits::Value Value;
141 ///The type of the flow map.
142 typedef typename Traits::FlowMap FlowMap;
143 ///The type of the elevator.
144 typedef typename Traits::Elevator Elevator;
145 ///The type of the tolerance.
146 typedef typename Traits::Tolerance Tolerance;
150 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
152 const Digraph& _graph;
153 const CapacityMap* _capacity;
157 Node _source, _target;
165 typedef typename Digraph::template NodeMap<Value> ExcessMap;
168 Tolerance _tolerance;
173 void createStructures() {
174 _node_num = countNodes(_graph);
177 _flow = Traits::createFlowMap(_graph);
181 _level = Traits::createElevator(_graph, _node_num);
185 _excess = new ExcessMap(_graph);
189 void destroyStructures() {
203 typedef Preflow Create;
205 ///\name Named Template Parameters
209 template <typename T>
210 struct SetFlowMapTraits : public Traits {
212 static FlowMap *createFlowMap(const Digraph&) {
213 LEMON_ASSERT(false, "FlowMap is not initialized");
214 return 0; // ignore warnings
218 /// \brief \ref named-templ-param "Named parameter" for setting
221 /// \ref named-templ-param "Named parameter" for setting FlowMap
223 template <typename T>
225 : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > {
226 typedef Preflow<Digraph, CapacityMap,
227 SetFlowMapTraits<T> > Create;
230 template <typename T>
231 struct SetElevatorTraits : public Traits {
233 static Elevator *createElevator(const Digraph&, int) {
234 LEMON_ASSERT(false, "Elevator is not initialized");
235 return 0; // ignore warnings
239 /// \brief \ref named-templ-param "Named parameter" for setting
242 /// \ref named-templ-param "Named parameter" for setting Elevator
243 /// type. If this named parameter is used, then an external
244 /// elevator object must be passed to the algorithm using the
245 /// \ref elevator(Elevator&) "elevator()" function before calling
246 /// \ref run() or \ref init().
247 /// \sa SetStandardElevator
248 template <typename T>
250 : public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > {
251 typedef Preflow<Digraph, CapacityMap,
252 SetElevatorTraits<T> > Create;
255 template <typename T>
256 struct SetStandardElevatorTraits : public Traits {
258 static Elevator *createElevator(const Digraph& digraph, int max_level) {
259 return new Elevator(digraph, max_level);
263 /// \brief \ref named-templ-param "Named parameter" for setting
264 /// Elevator type with automatic allocation
266 /// \ref named-templ-param "Named parameter" for setting Elevator
267 /// type with automatic allocation.
268 /// The Elevator should have standard constructor interface to be
269 /// able to automatically created by the algorithm (i.e. the
270 /// digraph and the maximum level should be passed to it).
271 /// However, an external elevator object could also be passed to the
272 /// algorithm with the \ref elevator(Elevator&) "elevator()" function
273 /// before calling \ref run() or \ref init().
275 template <typename T>
276 struct SetStandardElevator
277 : public Preflow<Digraph, CapacityMap,
278 SetStandardElevatorTraits<T> > {
279 typedef Preflow<Digraph, CapacityMap,
280 SetStandardElevatorTraits<T> > Create;
292 /// \brief The constructor of the class.
294 /// The constructor of the class.
295 /// \param digraph The digraph the algorithm runs on.
296 /// \param capacity The capacity of the arcs.
297 /// \param source The source node.
298 /// \param target The target node.
299 Preflow(const Digraph& digraph, const CapacityMap& capacity,
300 Node source, Node target)
301 : _graph(digraph), _capacity(&capacity),
302 _node_num(0), _source(source), _target(target),
303 _flow(0), _local_flow(false),
304 _level(0), _local_level(false),
305 _excess(0), _tolerance(), _phase() {}
307 /// \brief Destructor.
314 /// \brief Sets the capacity map.
316 /// Sets the capacity map.
317 /// \return <tt>(*this)</tt>
318 Preflow& capacityMap(const CapacityMap& map) {
323 /// \brief Sets the flow map.
325 /// Sets the flow map.
326 /// If you don't use this function before calling \ref run() or
327 /// \ref init(), an instance will be allocated automatically.
328 /// The destructor deallocates this automatically allocated map,
330 /// \return <tt>(*this)</tt>
331 Preflow& flowMap(FlowMap& map) {
340 /// \brief Sets the source node.
342 /// Sets the source node.
343 /// \return <tt>(*this)</tt>
344 Preflow& source(const Node& node) {
349 /// \brief Sets the target node.
351 /// Sets the target node.
352 /// \return <tt>(*this)</tt>
353 Preflow& target(const Node& node) {
358 /// \brief Sets the elevator used by algorithm.
360 /// Sets the elevator used by algorithm.
361 /// If you don't use this function before calling \ref run() or
362 /// \ref init(), an instance will be allocated automatically.
363 /// The destructor deallocates this automatically allocated elevator,
365 /// \return <tt>(*this)</tt>
366 Preflow& elevator(Elevator& elevator) {
369 _local_level = false;
375 /// \brief Returns a const reference to the elevator.
377 /// Returns a const reference to the elevator.
379 /// \pre Either \ref run() or \ref init() must be called before
380 /// using this function.
381 const Elevator& elevator() const {
385 /// \brief Sets the tolerance used by the algorithm.
387 /// Sets the tolerance object used by the algorithm.
388 /// \return <tt>(*this)</tt>
389 Preflow& tolerance(const Tolerance& tolerance) {
390 _tolerance = tolerance;
394 /// \brief Returns a const reference to the tolerance.
396 /// Returns a const reference to the tolerance object used by
398 const Tolerance& tolerance() const {
402 /// \name Execution Control
403 /// The simplest way to execute the preflow algorithm is to use
404 /// \ref run() or \ref runMinCut().\n
405 /// If you need better control on the initial solution or the execution,
406 /// you have to call one of the \ref init() functions first, then
407 /// \ref startFirstPhase() and if you need it \ref startSecondPhase().
411 /// \brief Initializes the internal data structures.
413 /// Initializes the internal data structures and sets the initial
414 /// flow to zero on each arc.
419 for (NodeIt n(_graph); n != INVALID; ++n) {
423 for (ArcIt e(_graph); e != INVALID; ++e) {
427 typename Digraph::template NodeMap<bool> reached(_graph, false);
430 _level->initAddItem(_target);
432 std::vector<Node> queue;
433 reached[_source] = true;
435 queue.push_back(_target);
436 reached[_target] = true;
437 while (!queue.empty()) {
438 _level->initNewLevel();
439 std::vector<Node> nqueue;
440 for (int i = 0; i < int(queue.size()); ++i) {
442 for (InArcIt e(_graph, n); e != INVALID; ++e) {
443 Node u = _graph.source(e);
444 if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
446 _level->initAddItem(u);
453 _level->initFinish();
455 for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
456 if (_tolerance.positive((*_capacity)[e])) {
457 Node u = _graph.target(e);
458 if ((*_level)[u] == _level->maxLevel()) continue;
459 _flow->set(e, (*_capacity)[e]);
460 (*_excess)[u] += (*_capacity)[e];
461 if (u != _target && !_level->active(u)) {
468 /// \brief Initializes the internal data structures using the
471 /// Initializes the internal data structures and sets the initial
472 /// flow to the given \c flowMap. The \c flowMap should contain a
473 /// flow or at least a preflow, i.e. at each node excluding the
474 /// source node the incoming flow should greater or equal to the
476 /// \return \c false if the given \c flowMap is not a preflow.
477 template <typename FlowMap>
478 bool init(const FlowMap& flowMap) {
481 for (ArcIt e(_graph); e != INVALID; ++e) {
482 _flow->set(e, flowMap[e]);
485 for (NodeIt n(_graph); n != INVALID; ++n) {
487 for (InArcIt e(_graph, n); e != INVALID; ++e) {
488 excess += (*_flow)[e];
490 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
491 excess -= (*_flow)[e];
493 if (excess < 0 && n != _source) return false;
494 (*_excess)[n] = excess;
497 typename Digraph::template NodeMap<bool> reached(_graph, false);
500 _level->initAddItem(_target);
502 std::vector<Node> queue;
503 reached[_source] = true;
505 queue.push_back(_target);
506 reached[_target] = true;
507 while (!queue.empty()) {
508 _level->initNewLevel();
509 std::vector<Node> nqueue;
510 for (int i = 0; i < int(queue.size()); ++i) {
512 for (InArcIt e(_graph, n); e != INVALID; ++e) {
513 Node u = _graph.source(e);
515 _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
517 _level->initAddItem(u);
521 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
522 Node v = _graph.target(e);
523 if (!reached[v] && _tolerance.positive((*_flow)[e])) {
525 _level->initAddItem(v);
532 _level->initFinish();
534 for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
535 Value rem = (*_capacity)[e] - (*_flow)[e];
536 if (_tolerance.positive(rem)) {
537 Node u = _graph.target(e);
538 if ((*_level)[u] == _level->maxLevel()) continue;
539 _flow->set(e, (*_capacity)[e]);
540 (*_excess)[u] += rem;
541 if (u != _target && !_level->active(u)) {
546 for (InArcIt e(_graph, _source); e != INVALID; ++e) {
547 Value rem = (*_flow)[e];
548 if (_tolerance.positive(rem)) {
549 Node v = _graph.source(e);
550 if ((*_level)[v] == _level->maxLevel()) continue;
552 (*_excess)[v] += rem;
553 if (v != _target && !_level->active(v)) {
561 /// \brief Starts the first phase of the preflow algorithm.
563 /// The preflow algorithm consists of two phases, this method runs
564 /// the first phase. After the first phase the maximum flow value
565 /// and a minimum value cut can already be computed, although a
566 /// maximum flow is not yet obtained. So after calling this method
567 /// \ref flowValue() returns the value of a maximum flow and \ref
568 /// minCut() returns a minimum cut.
569 /// \pre One of the \ref init() functions must be called before
570 /// using this function.
571 void startFirstPhase() {
574 Node n = _level->highestActive();
575 int level = _level->highestActiveLevel();
576 while (n != INVALID) {
579 while (num > 0 && n != INVALID) {
580 Value excess = (*_excess)[n];
581 int new_level = _level->maxLevel();
583 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
584 Value rem = (*_capacity)[e] - (*_flow)[e];
585 if (!_tolerance.positive(rem)) continue;
586 Node v = _graph.target(e);
587 if ((*_level)[v] < level) {
588 if (!_level->active(v) && v != _target) {
591 if (!_tolerance.less(rem, excess)) {
592 _flow->set(e, (*_flow)[e] + excess);
593 (*_excess)[v] += excess;
598 (*_excess)[v] += rem;
599 _flow->set(e, (*_capacity)[e]);
601 } else if (new_level > (*_level)[v]) {
602 new_level = (*_level)[v];
606 for (InArcIt e(_graph, n); e != INVALID; ++e) {
607 Value rem = (*_flow)[e];
608 if (!_tolerance.positive(rem)) continue;
609 Node v = _graph.source(e);
610 if ((*_level)[v] < level) {
611 if (!_level->active(v) && v != _target) {
614 if (!_tolerance.less(rem, excess)) {
615 _flow->set(e, (*_flow)[e] - excess);
616 (*_excess)[v] += excess;
621 (*_excess)[v] += rem;
624 } else if (new_level > (*_level)[v]) {
625 new_level = (*_level)[v];
631 (*_excess)[n] = excess;
634 if (new_level + 1 < _level->maxLevel()) {
635 _level->liftHighestActive(new_level + 1);
637 _level->liftHighestActiveToTop();
639 if (_level->emptyLevel(level)) {
640 _level->liftToTop(level);
643 _level->deactivate(n);
646 n = _level->highestActive();
647 level = _level->highestActiveLevel();
651 num = _node_num * 20;
652 while (num > 0 && n != INVALID) {
653 Value excess = (*_excess)[n];
654 int new_level = _level->maxLevel();
656 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
657 Value rem = (*_capacity)[e] - (*_flow)[e];
658 if (!_tolerance.positive(rem)) continue;
659 Node v = _graph.target(e);
660 if ((*_level)[v] < level) {
661 if (!_level->active(v) && v != _target) {
664 if (!_tolerance.less(rem, excess)) {
665 _flow->set(e, (*_flow)[e] + excess);
666 (*_excess)[v] += excess;
671 (*_excess)[v] += rem;
672 _flow->set(e, (*_capacity)[e]);
674 } else if (new_level > (*_level)[v]) {
675 new_level = (*_level)[v];
679 for (InArcIt e(_graph, n); e != INVALID; ++e) {
680 Value rem = (*_flow)[e];
681 if (!_tolerance.positive(rem)) continue;
682 Node v = _graph.source(e);
683 if ((*_level)[v] < level) {
684 if (!_level->active(v) && v != _target) {
687 if (!_tolerance.less(rem, excess)) {
688 _flow->set(e, (*_flow)[e] - excess);
689 (*_excess)[v] += excess;
694 (*_excess)[v] += rem;
697 } else if (new_level > (*_level)[v]) {
698 new_level = (*_level)[v];
704 (*_excess)[n] = excess;
707 if (new_level + 1 < _level->maxLevel()) {
708 _level->liftActiveOn(level, new_level + 1);
710 _level->liftActiveToTop(level);
712 if (_level->emptyLevel(level)) {
713 _level->liftToTop(level);
716 _level->deactivate(n);
719 while (level >= 0 && _level->activeFree(level)) {
723 n = _level->highestActive();
724 level = _level->highestActiveLevel();
726 n = _level->activeOn(level);
733 /// \brief Starts the second phase of the preflow algorithm.
735 /// The preflow algorithm consists of two phases, this method runs
736 /// the second phase. After calling one of the \ref init() functions
737 /// and \ref startFirstPhase() and then \ref startSecondPhase(),
738 /// \ref flowMap() returns a maximum flow, \ref flowValue() returns the
739 /// value of a maximum flow, \ref minCut() returns a minimum cut
740 /// \pre One of the \ref init() functions and \ref startFirstPhase()
741 /// must be called before using this function.
742 void startSecondPhase() {
745 typename Digraph::template NodeMap<bool> reached(_graph);
746 for (NodeIt n(_graph); n != INVALID; ++n) {
747 reached[n] = (*_level)[n] < _level->maxLevel();
751 _level->initAddItem(_source);
753 std::vector<Node> queue;
754 queue.push_back(_source);
755 reached[_source] = true;
757 while (!queue.empty()) {
758 _level->initNewLevel();
759 std::vector<Node> nqueue;
760 for (int i = 0; i < int(queue.size()); ++i) {
762 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
763 Node v = _graph.target(e);
764 if (!reached[v] && _tolerance.positive((*_flow)[e])) {
766 _level->initAddItem(v);
770 for (InArcIt e(_graph, n); e != INVALID; ++e) {
771 Node u = _graph.source(e);
773 _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
775 _level->initAddItem(u);
782 _level->initFinish();
784 for (NodeIt n(_graph); n != INVALID; ++n) {
786 _level->dirtyTopButOne(n);
787 } else if ((*_excess)[n] > 0 && _target != n) {
793 while ((n = _level->highestActive()) != INVALID) {
794 Value excess = (*_excess)[n];
795 int level = _level->highestActiveLevel();
796 int new_level = _level->maxLevel();
798 for (OutArcIt e(_graph, n); e != INVALID; ++e) {
799 Value rem = (*_capacity)[e] - (*_flow)[e];
800 if (!_tolerance.positive(rem)) continue;
801 Node v = _graph.target(e);
802 if ((*_level)[v] < level) {
803 if (!_level->active(v) && v != _source) {
806 if (!_tolerance.less(rem, excess)) {
807 _flow->set(e, (*_flow)[e] + excess);
808 (*_excess)[v] += excess;
813 (*_excess)[v] += rem;
814 _flow->set(e, (*_capacity)[e]);
816 } else if (new_level > (*_level)[v]) {
817 new_level = (*_level)[v];
821 for (InArcIt e(_graph, n); e != INVALID; ++e) {
822 Value rem = (*_flow)[e];
823 if (!_tolerance.positive(rem)) continue;
824 Node v = _graph.source(e);
825 if ((*_level)[v] < level) {
826 if (!_level->active(v) && v != _source) {
829 if (!_tolerance.less(rem, excess)) {
830 _flow->set(e, (*_flow)[e] - excess);
831 (*_excess)[v] += excess;
836 (*_excess)[v] += rem;
839 } else if (new_level > (*_level)[v]) {
840 new_level = (*_level)[v];
846 (*_excess)[n] = excess;
849 if (new_level + 1 < _level->maxLevel()) {
850 _level->liftHighestActive(new_level + 1);
853 _level->liftHighestActiveToTop();
855 if (_level->emptyLevel(level)) {
857 _level->liftToTop(level);
860 _level->deactivate(n);
866 /// \brief Runs the preflow algorithm.
868 /// Runs the preflow algorithm.
869 /// \note pf.run() is just a shortcut of the following code.
872 /// pf.startFirstPhase();
873 /// pf.startSecondPhase();
881 /// \brief Runs the preflow algorithm to compute the minimum cut.
883 /// Runs the preflow algorithm to compute the minimum cut.
884 /// \note pf.runMinCut() is just a shortcut of the following code.
887 /// pf.startFirstPhase();
896 /// \name Query Functions
897 /// The results of the preflow algorithm can be obtained using these
899 /// Either one of the \ref run() "run*()" functions or one of the
900 /// \ref startFirstPhase() "start*()" functions should be called
901 /// before using them.
905 /// \brief Returns the value of the maximum flow.
907 /// Returns the value of the maximum flow by returning the excess
908 /// of the target node. This value equals to the value of
909 /// the maximum flow already after the first phase of the algorithm.
911 /// \pre Either \ref run() or \ref init() must be called before
912 /// using this function.
913 Value flowValue() const {
914 return (*_excess)[_target];
917 /// \brief Returns the flow value on the given arc.
919 /// Returns the flow value on the given arc. This method can
920 /// be called after the second phase of the algorithm.
922 /// \pre Either \ref run() or \ref init() must be called before
923 /// using this function.
924 Value flow(const Arc& arc) const {
925 return (*_flow)[arc];
928 /// \brief Returns a const reference to the flow map.
930 /// Returns a const reference to the arc map storing the found flow.
931 /// This method can be called after the second phase of the algorithm.
933 /// \pre Either \ref run() or \ref init() must be called before
934 /// using this function.
935 const FlowMap& flowMap() const {
939 /// \brief Returns \c true when the node is on the source side of the
942 /// Returns true when the node is on the source side of the found
943 /// minimum cut. This method can be called both after running \ref
944 /// startFirstPhase() and \ref startSecondPhase().
946 /// \pre Either \ref run() or \ref init() must be called before
947 /// using this function.
948 bool minCut(const Node& node) const {
949 return ((*_level)[node] == _level->maxLevel()) == _phase;
952 /// \brief Gives back a minimum value cut.
954 /// Sets \c cutMap to the characteristic vector of a minimum value
955 /// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
956 /// node map with \c bool (or convertible) value type.
958 /// This method can be called both after running \ref startFirstPhase()
959 /// and \ref startSecondPhase(). The result after the second phase
960 /// could be slightly different if inexact computation is used.
962 /// \note This function calls \ref minCut() for each node, so it runs in
965 /// \pre Either \ref run() or \ref init() must be called before
966 /// using this function.
967 template <typename CutMap>
968 void minCutMap(CutMap& cutMap) const {
969 for (NodeIt n(_graph); n != INVALID; ++n) {
970 cutMap.set(n, minCut(n));