deba@409: /* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@409:  *
deba@409:  * This file is a part of LEMON, a generic C++ optimization library.
deba@409:  *
alpar@440:  * Copyright (C) 2003-2009
deba@409:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@409:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@409:  *
deba@409:  * Permission to use, modify and distribute this software is granted
deba@409:  * provided that this copyright notice appears in all copies. For
deba@409:  * precise terms see the accompanying LICENSE file.
deba@409:  *
deba@409:  * This software is provided "AS IS" with no warranty of any kind,
deba@409:  * express or implied, and with no claim as to its suitability for any
deba@409:  * purpose.
deba@409:  *
deba@409:  */
deba@409: 
deba@409: #ifndef LEMON_HAO_ORLIN_H
deba@409: #define LEMON_HAO_ORLIN_H
deba@409: 
deba@409: #include <vector>
deba@409: #include <list>
deba@409: #include <limits>
deba@409: 
deba@409: #include <lemon/maps.h>
deba@409: #include <lemon/core.h>
deba@409: #include <lemon/tolerance.h>
deba@409: 
deba@409: /// \file
deba@409: /// \ingroup min_cut
deba@409: /// \brief Implementation of the Hao-Orlin algorithm.
deba@409: ///
kpeter@596: /// Implementation of the Hao-Orlin algorithm for finding a minimum cut 
kpeter@596: /// in a digraph.
deba@409: 
deba@409: namespace lemon {
deba@409: 
deba@409:   /// \ingroup min_cut
deba@409:   ///
kpeter@596:   /// \brief Hao-Orlin algorithm for finding a minimum cut in a digraph.
deba@409:   ///
kpeter@596:   /// This class implements the Hao-Orlin algorithm for finding a minimum
kpeter@596:   /// value cut in a directed graph \f$D=(V,A)\f$. 
kpeter@596:   /// It takes a fixed node \f$ source \in V \f$ and
deba@409:   /// consists of two phases: in the first phase it determines a
deba@409:   /// minimum cut with \f$ source \f$ on the source-side (i.e. a set
kpeter@596:   /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal outgoing
kpeter@596:   /// capacity) and in the second phase it determines a minimum cut
deba@409:   /// with \f$ source \f$ on the sink-side (i.e. a set
kpeter@596:   /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal outgoing
kpeter@596:   /// capacity). Obviously, the smaller of these two cuts will be a
deba@409:   /// minimum cut of \f$ D \f$. The algorithm is a modified
kpeter@596:   /// preflow push-relabel algorithm. Our implementation calculates
deba@409:   /// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
deba@409:   /// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The
kpeter@596:   /// purpose of such algorithm is e.g. testing network reliability.
deba@409:   ///
kpeter@596:   /// For an undirected graph you can run just the first phase of the
kpeter@596:   /// algorithm or you can use the algorithm of Nagamochi and Ibaraki,
kpeter@596:   /// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ 
kpeter@596:   /// time. It is implemented in the NagamochiIbaraki algorithm class.
kpeter@596:   ///
kpeter@596:   /// \tparam GR The type of the digraph the algorithm runs on.
kpeter@596:   /// \tparam CAP The type of the arc map containing the capacities,
kpeter@596:   /// which can be any numreric type. The default map type is
kpeter@596:   /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
kpeter@596:   /// \tparam TOL Tolerance class for handling inexact computations. The
kpeter@559:   /// default tolerance type is \ref Tolerance "Tolerance<CAP::Value>".
deba@409: #ifdef DOXYGEN
kpeter@559:   template <typename GR, typename CAP, typename TOL>
deba@409: #else
kpeter@559:   template <typename GR,
kpeter@559:             typename CAP = typename GR::template ArcMap<int>,
kpeter@559:             typename TOL = Tolerance<typename CAP::Value> >
deba@409: #endif
deba@409:   class HaoOrlin {
kpeter@596:   public:
kpeter@596:    
kpeter@596:     /// The digraph type of the algorithm
kpeter@596:     typedef GR Digraph;
kpeter@596:     /// The capacity map type of the algorithm
kpeter@596:     typedef CAP CapacityMap;
kpeter@596:     /// The tolerance type of the algorithm
kpeter@596:     typedef TOL Tolerance;
kpeter@596: 
deba@409:   private:
deba@409: 
deba@409:     typedef typename CapacityMap::Value Value;
deba@409: 
kpeter@596:     TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
deba@409: 
deba@409:     const Digraph& _graph;
deba@409:     const CapacityMap* _capacity;
deba@409: 
deba@409:     typedef typename Digraph::template ArcMap<Value> FlowMap;
deba@409:     FlowMap* _flow;
deba@409: 
deba@409:     Node _source;
deba@409: 
deba@409:     int _node_num;
deba@409: 
deba@409:     // Bucketing structure
deba@409:     std::vector<Node> _first, _last;
deba@409:     typename Digraph::template NodeMap<Node>* _next;
deba@409:     typename Digraph::template NodeMap<Node>* _prev;
deba@409:     typename Digraph::template NodeMap<bool>* _active;
deba@409:     typename Digraph::template NodeMap<int>* _bucket;
deba@409: 
deba@409:     std::vector<bool> _dormant;
deba@409: 
deba@409:     std::list<std::list<int> > _sets;
deba@409:     std::list<int>::iterator _highest;
deba@409: 
deba@409:     typedef typename Digraph::template NodeMap<Value> ExcessMap;
deba@409:     ExcessMap* _excess;
deba@409: 
deba@409:     typedef typename Digraph::template NodeMap<bool> SourceSetMap;
deba@409:     SourceSetMap* _source_set;
deba@409: 
deba@409:     Value _min_cut;
deba@409: 
deba@409:     typedef typename Digraph::template NodeMap<bool> MinCutMap;
deba@409:     MinCutMap* _min_cut_map;
deba@409: 
deba@409:     Tolerance _tolerance;
deba@409: 
deba@409:   public:
deba@409: 
deba@409:     /// \brief Constructor
deba@409:     ///
deba@409:     /// Constructor of the algorithm class.
deba@409:     HaoOrlin(const Digraph& graph, const CapacityMap& capacity,
deba@409:              const Tolerance& tolerance = Tolerance()) :
deba@409:       _graph(graph), _capacity(&capacity), _flow(0), _source(),
deba@409:       _node_num(), _first(), _last(), _next(0), _prev(0),
deba@409:       _active(0), _bucket(0), _dormant(), _sets(), _highest(),
deba@409:       _excess(0), _source_set(0), _min_cut(), _min_cut_map(0),
deba@409:       _tolerance(tolerance) {}
deba@409: 
deba@409:     ~HaoOrlin() {
deba@409:       if (_min_cut_map) {
deba@409:         delete _min_cut_map;
deba@409:       }
deba@409:       if (_source_set) {
deba@409:         delete _source_set;
deba@409:       }
deba@409:       if (_excess) {
deba@409:         delete _excess;
deba@409:       }
deba@409:       if (_next) {
deba@409:         delete _next;
deba@409:       }
deba@409:       if (_prev) {
deba@409:         delete _prev;
deba@409:       }
deba@409:       if (_active) {
deba@409:         delete _active;
deba@409:       }
deba@409:       if (_bucket) {
deba@409:         delete _bucket;
deba@409:       }
deba@409:       if (_flow) {
deba@409:         delete _flow;
deba@409:       }
deba@409:     }
deba@409: 
deba@409:   private:
deba@409: 
deba@409:     void activate(const Node& i) {
kpeter@581:       (*_active)[i] = true;
deba@409: 
deba@409:       int bucket = (*_bucket)[i];
deba@409: 
deba@409:       if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return;
deba@409:       //unlace
kpeter@581:       (*_next)[(*_prev)[i]] = (*_next)[i];
deba@409:       if ((*_next)[i] != INVALID) {
kpeter@581:         (*_prev)[(*_next)[i]] = (*_prev)[i];
deba@409:       } else {
deba@409:         _last[bucket] = (*_prev)[i];
deba@409:       }
deba@409:       //lace
kpeter@581:       (*_next)[i] = _first[bucket];
kpeter@581:       (*_prev)[_first[bucket]] = i;
kpeter@581:       (*_prev)[i] = INVALID;
deba@409:       _first[bucket] = i;
deba@409:     }
deba@409: 
deba@409:     void deactivate(const Node& i) {
kpeter@581:       (*_active)[i] = false;
deba@409:       int bucket = (*_bucket)[i];
deba@409: 
deba@409:       if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return;
deba@409: 
deba@409:       //unlace
kpeter@581:       (*_prev)[(*_next)[i]] = (*_prev)[i];
deba@409:       if ((*_prev)[i] != INVALID) {
kpeter@581:         (*_next)[(*_prev)[i]] = (*_next)[i];
deba@409:       } else {
deba@409:         _first[bucket] = (*_next)[i];
deba@409:       }
deba@409:       //lace
kpeter@581:       (*_prev)[i] = _last[bucket];
kpeter@581:       (*_next)[_last[bucket]] = i;
kpeter@581:       (*_next)[i] = INVALID;
deba@409:       _last[bucket] = i;
deba@409:     }
deba@409: 
deba@409:     void addItem(const Node& i, int bucket) {
deba@409:       (*_bucket)[i] = bucket;
deba@409:       if (_last[bucket] != INVALID) {
kpeter@581:         (*_prev)[i] = _last[bucket];
kpeter@581:         (*_next)[_last[bucket]] = i;
kpeter@581:         (*_next)[i] = INVALID;
deba@409:         _last[bucket] = i;
deba@409:       } else {
kpeter@581:         (*_prev)[i] = INVALID;
deba@409:         _first[bucket] = i;
kpeter@581:         (*_next)[i] = INVALID;
deba@409:         _last[bucket] = i;
deba@409:       }
deba@409:     }
deba@409: 
deba@409:     void findMinCutOut() {
deba@409: 
deba@409:       for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581:         (*_excess)[n] = 0;
deba@597:         (*_source_set)[n] = false;
deba@409:       }
deba@409: 
deba@409:       for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@581:         (*_flow)[a] = 0;
deba@409:       }
deba@409: 
deba@411:       int bucket_num = 0;
deba@411:       std::vector<Node> queue(_node_num);
deba@411:       int qfirst = 0, qlast = 0, qsep = 0;
deba@409: 
deba@409:       {
deba@409:         typename Digraph::template NodeMap<bool> reached(_graph, false);
deba@409: 
kpeter@581:         reached[_source] = true;
deba@409:         bool first_set = true;
deba@409: 
deba@409:         for (NodeIt t(_graph); t != INVALID; ++t) {
deba@409:           if (reached[t]) continue;
deba@409:           _sets.push_front(std::list<int>());
alpar@440: 
deba@411:           queue[qlast++] = t;
kpeter@581:           reached[t] = true;
deba@409: 
deba@411:           while (qfirst != qlast) {
deba@411:             if (qsep == qfirst) {
deba@411:               ++bucket_num;
deba@411:               _sets.front().push_front(bucket_num);
deba@411:               _dormant[bucket_num] = !first_set;
deba@411:               _first[bucket_num] = _last[bucket_num] = INVALID;
deba@411:               qsep = qlast;
deba@411:             }
deba@409: 
deba@411:             Node n = queue[qfirst++];
deba@411:             addItem(n, bucket_num);
deba@411: 
deba@411:             for (InArcIt a(_graph, n); a != INVALID; ++a) {
deba@411:               Node u = _graph.source(a);
deba@411:               if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
kpeter@581:                 reached[u] = true;
deba@411:                 queue[qlast++] = u;
deba@409:               }
deba@409:             }
deba@409:           }
deba@409:           first_set = false;
deba@409:         }
deba@409: 
deba@411:         ++bucket_num;
kpeter@581:         (*_bucket)[_source] = 0;
deba@409:         _dormant[0] = true;
deba@409:       }
kpeter@581:       (*_source_set)[_source] = true;
deba@409: 
deba@409:       Node target = _last[_sets.back().back()];
deba@409:       {
deba@409:         for (OutArcIt a(_graph, _source); a != INVALID; ++a) {
deba@409:           if (_tolerance.positive((*_capacity)[a])) {
deba@409:             Node u = _graph.target(a);
kpeter@581:             (*_flow)[a] = (*_capacity)[a];
kpeter@581:             (*_excess)[u] += (*_capacity)[a];
deba@409:             if (!(*_active)[u] && u != _source) {
deba@409:               activate(u);
deba@409:             }
deba@409:           }
deba@409:         }
deba@409: 
deba@409:         if ((*_active)[target]) {
deba@409:           deactivate(target);
deba@409:         }
deba@409: 
deba@409:         _highest = _sets.back().begin();
deba@409:         while (_highest != _sets.back().end() &&
deba@409:                !(*_active)[_first[*_highest]]) {
deba@409:           ++_highest;
deba@409:         }
deba@409:       }
deba@409: 
deba@409:       while (true) {
deba@409:         while (_highest != _sets.back().end()) {
deba@409:           Node n = _first[*_highest];
deba@409:           Value excess = (*_excess)[n];
deba@409:           int next_bucket = _node_num;
deba@409: 
deba@409:           int under_bucket;
deba@409:           if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
deba@409:             under_bucket = -1;
deba@409:           } else {
deba@409:             under_bucket = *(++std::list<int>::iterator(_highest));
deba@409:           }
deba@409: 
deba@409:           for (OutArcIt a(_graph, n); a != INVALID; ++a) {
deba@409:             Node v = _graph.target(a);
deba@409:             if (_dormant[(*_bucket)[v]]) continue;
deba@409:             Value rem = (*_capacity)[a] - (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             if ((*_bucket)[v] == under_bucket) {
deba@409:               if (!(*_active)[v] && v != target) {
deba@409:                 activate(v);
deba@409:               }
deba@409:               if (!_tolerance.less(rem, excess)) {
kpeter@581:                 (*_flow)[a] += excess;
kpeter@581:                 (*_excess)[v] += excess;
deba@409:                 excess = 0;
deba@409:                 goto no_more_push;
deba@409:               } else {
deba@409:                 excess -= rem;
kpeter@581:                 (*_excess)[v] += rem;
kpeter@581:                 (*_flow)[a] = (*_capacity)[a];
deba@409:               }
deba@409:             } else if (next_bucket > (*_bucket)[v]) {
deba@409:               next_bucket = (*_bucket)[v];
deba@409:             }
deba@409:           }
deba@409: 
deba@409:           for (InArcIt a(_graph, n); a != INVALID; ++a) {
deba@409:             Node v = _graph.source(a);
deba@409:             if (_dormant[(*_bucket)[v]]) continue;
deba@409:             Value rem = (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             if ((*_bucket)[v] == under_bucket) {
deba@409:               if (!(*_active)[v] && v != target) {
deba@409:                 activate(v);
deba@409:               }
deba@409:               if (!_tolerance.less(rem, excess)) {
kpeter@581:                 (*_flow)[a] -= excess;
kpeter@581:                 (*_excess)[v] += excess;
deba@409:                 excess = 0;
deba@409:                 goto no_more_push;
deba@409:               } else {
deba@409:                 excess -= rem;
kpeter@581:                 (*_excess)[v] += rem;
kpeter@581:                 (*_flow)[a] = 0;
deba@409:               }
deba@409:             } else if (next_bucket > (*_bucket)[v]) {
deba@409:               next_bucket = (*_bucket)[v];
deba@409:             }
deba@409:           }
deba@409: 
deba@409:         no_more_push:
deba@409: 
kpeter@581:           (*_excess)[n] = excess;
deba@409: 
deba@409:           if (excess != 0) {
deba@409:             if ((*_next)[n] == INVALID) {
deba@409:               typename std::list<std::list<int> >::iterator new_set =
deba@409:                 _sets.insert(--_sets.end(), std::list<int>());
deba@409:               new_set->splice(new_set->end(), _sets.back(),
deba@409:                               _sets.back().begin(), ++_highest);
deba@409:               for (std::list<int>::iterator it = new_set->begin();
deba@409:                    it != new_set->end(); ++it) {
deba@409:                 _dormant[*it] = true;
deba@409:               }
deba@409:               while (_highest != _sets.back().end() &&
deba@409:                      !(*_active)[_first[*_highest]]) {
deba@409:                 ++_highest;
deba@409:               }
deba@409:             } else if (next_bucket == _node_num) {
deba@409:               _first[(*_bucket)[n]] = (*_next)[n];
kpeter@581:               (*_prev)[(*_next)[n]] = INVALID;
deba@409: 
deba@409:               std::list<std::list<int> >::iterator new_set =
deba@409:                 _sets.insert(--_sets.end(), std::list<int>());
deba@409: 
deba@409:               new_set->push_front(bucket_num);
kpeter@581:               (*_bucket)[n] = bucket_num;
deba@409:               _first[bucket_num] = _last[bucket_num] = n;
kpeter@581:               (*_next)[n] = INVALID;
kpeter@581:               (*_prev)[n] = INVALID;
deba@409:               _dormant[bucket_num] = true;
deba@409:               ++bucket_num;
deba@409: 
deba@409:               while (_highest != _sets.back().end() &&
deba@409:                      !(*_active)[_first[*_highest]]) {
deba@409:                 ++_highest;
deba@409:               }
deba@409:             } else {
deba@409:               _first[*_highest] = (*_next)[n];
kpeter@581:               (*_prev)[(*_next)[n]] = INVALID;
deba@409: 
deba@409:               while (next_bucket != *_highest) {
deba@409:                 --_highest;
deba@409:               }
deba@409: 
deba@409:               if (_highest == _sets.back().begin()) {
deba@409:                 _sets.back().push_front(bucket_num);
deba@409:                 _dormant[bucket_num] = false;
deba@409:                 _first[bucket_num] = _last[bucket_num] = INVALID;
deba@409:                 ++bucket_num;
deba@409:               }
deba@409:               --_highest;
deba@409: 
kpeter@581:               (*_bucket)[n] = *_highest;
kpeter@581:               (*_next)[n] = _first[*_highest];
deba@409:               if (_first[*_highest] != INVALID) {
kpeter@581:                 (*_prev)[_first[*_highest]] = n;
deba@409:               } else {
deba@409:                 _last[*_highest] = n;
deba@409:               }
deba@409:               _first[*_highest] = n;
deba@409:             }
deba@409:           } else {
deba@409: 
deba@409:             deactivate(n);
deba@409:             if (!(*_active)[_first[*_highest]]) {
deba@409:               ++_highest;
deba@409:               if (_highest != _sets.back().end() &&
deba@409:                   !(*_active)[_first[*_highest]]) {
deba@409:                 _highest = _sets.back().end();
deba@409:               }
deba@409:             }
deba@409:           }
deba@409:         }
deba@409: 
deba@409:         if ((*_excess)[target] < _min_cut) {
deba@409:           _min_cut = (*_excess)[target];
deba@409:           for (NodeIt i(_graph); i != INVALID; ++i) {
kpeter@581:             (*_min_cut_map)[i] = true;
deba@409:           }
deba@409:           for (std::list<int>::iterator it = _sets.back().begin();
deba@409:                it != _sets.back().end(); ++it) {
deba@409:             Node n = _first[*it];
deba@409:             while (n != INVALID) {
kpeter@581:               (*_min_cut_map)[n] = false;
deba@409:               n = (*_next)[n];
deba@409:             }
deba@409:           }
deba@409:         }
deba@409: 
deba@409:         {
deba@409:           Node new_target;
deba@409:           if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
deba@409:             if ((*_next)[target] == INVALID) {
deba@409:               _last[(*_bucket)[target]] = (*_prev)[target];
deba@409:               new_target = (*_prev)[target];
deba@409:             } else {
kpeter@581:               (*_prev)[(*_next)[target]] = (*_prev)[target];
deba@409:               new_target = (*_next)[target];
deba@409:             }
deba@409:             if ((*_prev)[target] == INVALID) {
deba@409:               _first[(*_bucket)[target]] = (*_next)[target];
deba@409:             } else {
kpeter@581:               (*_next)[(*_prev)[target]] = (*_next)[target];
deba@409:             }
deba@409:           } else {
deba@409:             _sets.back().pop_back();
deba@409:             if (_sets.back().empty()) {
deba@409:               _sets.pop_back();
deba@409:               if (_sets.empty())
deba@409:                 break;
deba@409:               for (std::list<int>::iterator it = _sets.back().begin();
deba@409:                    it != _sets.back().end(); ++it) {
deba@409:                 _dormant[*it] = false;
deba@409:               }
deba@409:             }
deba@409:             new_target = _last[_sets.back().back()];
deba@409:           }
deba@409: 
kpeter@581:           (*_bucket)[target] = 0;
deba@409: 
kpeter@581:           (*_source_set)[target] = true;
deba@409:           for (OutArcIt a(_graph, target); a != INVALID; ++a) {
deba@409:             Value rem = (*_capacity)[a] - (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             Node v = _graph.target(a);
deba@409:             if (!(*_active)[v] && !(*_source_set)[v]) {
deba@409:               activate(v);
deba@409:             }
kpeter@581:             (*_excess)[v] += rem;
kpeter@581:             (*_flow)[a] = (*_capacity)[a];
deba@409:           }
deba@409: 
deba@409:           for (InArcIt a(_graph, target); a != INVALID; ++a) {
deba@409:             Value rem = (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             Node v = _graph.source(a);
deba@409:             if (!(*_active)[v] && !(*_source_set)[v]) {
deba@409:               activate(v);
deba@409:             }
kpeter@581:             (*_excess)[v] += rem;
kpeter@581:             (*_flow)[a] = 0;
deba@409:           }
deba@409: 
deba@409:           target = new_target;
deba@409:           if ((*_active)[target]) {
deba@409:             deactivate(target);
deba@409:           }
deba@409: 
deba@409:           _highest = _sets.back().begin();
deba@409:           while (_highest != _sets.back().end() &&
deba@409:                  !(*_active)[_first[*_highest]]) {
deba@409:             ++_highest;
deba@409:           }
deba@409:         }
deba@409:       }
deba@409:     }
deba@409: 
deba@409:     void findMinCutIn() {
deba@409: 
deba@409:       for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581:         (*_excess)[n] = 0;
deba@597:         (*_source_set)[n] = false;
deba@409:       }
deba@409: 
deba@409:       for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@581:         (*_flow)[a] = 0;
deba@409:       }
deba@409: 
deba@411:       int bucket_num = 0;
deba@411:       std::vector<Node> queue(_node_num);
deba@411:       int qfirst = 0, qlast = 0, qsep = 0;
deba@409: 
deba@409:       {
deba@409:         typename Digraph::template NodeMap<bool> reached(_graph, false);
deba@409: 
kpeter@581:         reached[_source] = true;
deba@409: 
deba@409:         bool first_set = true;
deba@409: 
deba@409:         for (NodeIt t(_graph); t != INVALID; ++t) {
deba@409:           if (reached[t]) continue;
deba@409:           _sets.push_front(std::list<int>());
alpar@440: 
deba@411:           queue[qlast++] = t;
kpeter@581:           reached[t] = true;
deba@409: 
deba@411:           while (qfirst != qlast) {
deba@411:             if (qsep == qfirst) {
deba@411:               ++bucket_num;
deba@411:               _sets.front().push_front(bucket_num);
deba@411:               _dormant[bucket_num] = !first_set;
deba@411:               _first[bucket_num] = _last[bucket_num] = INVALID;
deba@411:               qsep = qlast;
deba@411:             }
deba@409: 
deba@411:             Node n = queue[qfirst++];
deba@411:             addItem(n, bucket_num);
deba@411: 
deba@411:             for (OutArcIt a(_graph, n); a != INVALID; ++a) {
deba@411:               Node u = _graph.target(a);
deba@411:               if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
kpeter@581:                 reached[u] = true;
deba@411:                 queue[qlast++] = u;
deba@409:               }
deba@409:             }
deba@409:           }
deba@409:           first_set = false;
deba@409:         }
deba@409: 
deba@411:         ++bucket_num;
kpeter@581:         (*_bucket)[_source] = 0;
deba@409:         _dormant[0] = true;
deba@409:       }
kpeter@581:       (*_source_set)[_source] = true;
deba@409: 
deba@409:       Node target = _last[_sets.back().back()];
deba@409:       {
deba@409:         for (InArcIt a(_graph, _source); a != INVALID; ++a) {
deba@409:           if (_tolerance.positive((*_capacity)[a])) {
deba@409:             Node u = _graph.source(a);
kpeter@581:             (*_flow)[a] = (*_capacity)[a];
kpeter@581:             (*_excess)[u] += (*_capacity)[a];
deba@409:             if (!(*_active)[u] && u != _source) {
deba@409:               activate(u);
deba@409:             }
deba@409:           }
deba@409:         }
deba@409:         if ((*_active)[target]) {
deba@409:           deactivate(target);
deba@409:         }
deba@409: 
deba@409:         _highest = _sets.back().begin();
deba@409:         while (_highest != _sets.back().end() &&
deba@409:                !(*_active)[_first[*_highest]]) {
deba@409:           ++_highest;
deba@409:         }
deba@409:       }
deba@409: 
deba@409: 
deba@409:       while (true) {
deba@409:         while (_highest != _sets.back().end()) {
deba@409:           Node n = _first[*_highest];
deba@409:           Value excess = (*_excess)[n];
deba@409:           int next_bucket = _node_num;
deba@409: 
deba@409:           int under_bucket;
deba@409:           if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
deba@409:             under_bucket = -1;
deba@409:           } else {
deba@409:             under_bucket = *(++std::list<int>::iterator(_highest));
deba@409:           }
deba@409: 
deba@409:           for (InArcIt a(_graph, n); a != INVALID; ++a) {
deba@409:             Node v = _graph.source(a);
deba@409:             if (_dormant[(*_bucket)[v]]) continue;
deba@409:             Value rem = (*_capacity)[a] - (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             if ((*_bucket)[v] == under_bucket) {
deba@409:               if (!(*_active)[v] && v != target) {
deba@409:                 activate(v);
deba@409:               }
deba@409:               if (!_tolerance.less(rem, excess)) {
kpeter@581:                 (*_flow)[a] += excess;
kpeter@581:                 (*_excess)[v] += excess;
deba@409:                 excess = 0;
deba@409:                 goto no_more_push;
deba@409:               } else {
deba@409:                 excess -= rem;
kpeter@581:                 (*_excess)[v] += rem;
kpeter@581:                 (*_flow)[a] = (*_capacity)[a];
deba@409:               }
deba@409:             } else if (next_bucket > (*_bucket)[v]) {
deba@409:               next_bucket = (*_bucket)[v];
deba@409:             }
deba@409:           }
deba@409: 
deba@409:           for (OutArcIt a(_graph, n); a != INVALID; ++a) {
deba@409:             Node v = _graph.target(a);
deba@409:             if (_dormant[(*_bucket)[v]]) continue;
deba@409:             Value rem = (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             if ((*_bucket)[v] == under_bucket) {
deba@409:               if (!(*_active)[v] && v != target) {
deba@409:                 activate(v);
deba@409:               }
deba@409:               if (!_tolerance.less(rem, excess)) {
kpeter@581:                 (*_flow)[a] -= excess;
kpeter@581:                 (*_excess)[v] += excess;
deba@409:                 excess = 0;
deba@409:                 goto no_more_push;
deba@409:               } else {
deba@409:                 excess -= rem;
kpeter@581:                 (*_excess)[v] += rem;
kpeter@581:                 (*_flow)[a] = 0;
deba@409:               }
deba@409:             } else if (next_bucket > (*_bucket)[v]) {
deba@409:               next_bucket = (*_bucket)[v];
deba@409:             }
deba@409:           }
deba@409: 
deba@409:         no_more_push:
deba@409: 
kpeter@581:           (*_excess)[n] = excess;
deba@409: 
deba@409:           if (excess != 0) {
deba@409:             if ((*_next)[n] == INVALID) {
deba@409:               typename std::list<std::list<int> >::iterator new_set =
deba@409:                 _sets.insert(--_sets.end(), std::list<int>());
deba@409:               new_set->splice(new_set->end(), _sets.back(),
deba@409:                               _sets.back().begin(), ++_highest);
deba@409:               for (std::list<int>::iterator it = new_set->begin();
deba@409:                    it != new_set->end(); ++it) {
deba@409:                 _dormant[*it] = true;
deba@409:               }
deba@409:               while (_highest != _sets.back().end() &&
deba@409:                      !(*_active)[_first[*_highest]]) {
deba@409:                 ++_highest;
deba@409:               }
deba@409:             } else if (next_bucket == _node_num) {
deba@409:               _first[(*_bucket)[n]] = (*_next)[n];
kpeter@581:               (*_prev)[(*_next)[n]] = INVALID;
deba@409: 
deba@409:               std::list<std::list<int> >::iterator new_set =
deba@409:                 _sets.insert(--_sets.end(), std::list<int>());
deba@409: 
deba@409:               new_set->push_front(bucket_num);
kpeter@581:               (*_bucket)[n] = bucket_num;
deba@409:               _first[bucket_num] = _last[bucket_num] = n;
kpeter@581:               (*_next)[n] = INVALID;
kpeter@581:               (*_prev)[n] = INVALID;
deba@409:               _dormant[bucket_num] = true;
deba@409:               ++bucket_num;
deba@409: 
deba@409:               while (_highest != _sets.back().end() &&
deba@409:                      !(*_active)[_first[*_highest]]) {
deba@409:                 ++_highest;
deba@409:               }
deba@409:             } else {
deba@409:               _first[*_highest] = (*_next)[n];
kpeter@581:               (*_prev)[(*_next)[n]] = INVALID;
deba@409: 
deba@409:               while (next_bucket != *_highest) {
deba@409:                 --_highest;
deba@409:               }
deba@409:               if (_highest == _sets.back().begin()) {
deba@409:                 _sets.back().push_front(bucket_num);
deba@409:                 _dormant[bucket_num] = false;
deba@409:                 _first[bucket_num] = _last[bucket_num] = INVALID;
deba@409:                 ++bucket_num;
deba@409:               }
deba@409:               --_highest;
deba@409: 
kpeter@581:               (*_bucket)[n] = *_highest;
kpeter@581:               (*_next)[n] = _first[*_highest];
deba@409:               if (_first[*_highest] != INVALID) {
kpeter@581:                 (*_prev)[_first[*_highest]] = n;
deba@409:               } else {
deba@409:                 _last[*_highest] = n;
deba@409:               }
deba@409:               _first[*_highest] = n;
deba@409:             }
deba@409:           } else {
deba@409: 
deba@409:             deactivate(n);
deba@409:             if (!(*_active)[_first[*_highest]]) {
deba@409:               ++_highest;
deba@409:               if (_highest != _sets.back().end() &&
deba@409:                   !(*_active)[_first[*_highest]]) {
deba@409:                 _highest = _sets.back().end();
deba@409:               }
deba@409:             }
deba@409:           }
deba@409:         }
deba@409: 
deba@409:         if ((*_excess)[target] < _min_cut) {
deba@409:           _min_cut = (*_excess)[target];
deba@409:           for (NodeIt i(_graph); i != INVALID; ++i) {
kpeter@581:             (*_min_cut_map)[i] = false;
deba@409:           }
deba@409:           for (std::list<int>::iterator it = _sets.back().begin();
deba@409:                it != _sets.back().end(); ++it) {
deba@409:             Node n = _first[*it];
deba@409:             while (n != INVALID) {
kpeter@581:               (*_min_cut_map)[n] = true;
deba@409:               n = (*_next)[n];
deba@409:             }
deba@409:           }
deba@409:         }
deba@409: 
deba@409:         {
deba@409:           Node new_target;
deba@409:           if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
deba@409:             if ((*_next)[target] == INVALID) {
deba@409:               _last[(*_bucket)[target]] = (*_prev)[target];
deba@409:               new_target = (*_prev)[target];
deba@409:             } else {
kpeter@581:               (*_prev)[(*_next)[target]] = (*_prev)[target];
deba@409:               new_target = (*_next)[target];
deba@409:             }
deba@409:             if ((*_prev)[target] == INVALID) {
deba@409:               _first[(*_bucket)[target]] = (*_next)[target];
deba@409:             } else {
kpeter@581:               (*_next)[(*_prev)[target]] = (*_next)[target];
deba@409:             }
deba@409:           } else {
deba@409:             _sets.back().pop_back();
deba@409:             if (_sets.back().empty()) {
deba@409:               _sets.pop_back();
deba@409:               if (_sets.empty())
deba@409:                 break;
deba@409:               for (std::list<int>::iterator it = _sets.back().begin();
deba@409:                    it != _sets.back().end(); ++it) {
deba@409:                 _dormant[*it] = false;
deba@409:               }
deba@409:             }
deba@409:             new_target = _last[_sets.back().back()];
deba@409:           }
deba@409: 
kpeter@581:           (*_bucket)[target] = 0;
deba@409: 
kpeter@581:           (*_source_set)[target] = true;
deba@409:           for (InArcIt a(_graph, target); a != INVALID; ++a) {
deba@409:             Value rem = (*_capacity)[a] - (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             Node v = _graph.source(a);
deba@409:             if (!(*_active)[v] && !(*_source_set)[v]) {
deba@409:               activate(v);
deba@409:             }
kpeter@581:             (*_excess)[v] += rem;
kpeter@581:             (*_flow)[a] = (*_capacity)[a];
deba@409:           }
deba@409: 
deba@409:           for (OutArcIt a(_graph, target); a != INVALID; ++a) {
deba@409:             Value rem = (*_flow)[a];
deba@409:             if (!_tolerance.positive(rem)) continue;
deba@409:             Node v = _graph.target(a);
deba@409:             if (!(*_active)[v] && !(*_source_set)[v]) {
deba@409:               activate(v);
deba@409:             }
kpeter@581:             (*_excess)[v] += rem;
kpeter@581:             (*_flow)[a] = 0;
deba@409:           }
deba@409: 
deba@409:           target = new_target;
deba@409:           if ((*_active)[target]) {
deba@409:             deactivate(target);
deba@409:           }
deba@409: 
deba@409:           _highest = _sets.back().begin();
deba@409:           while (_highest != _sets.back().end() &&
deba@409:                  !(*_active)[_first[*_highest]]) {
deba@409:             ++_highest;
deba@409:           }
deba@409:         }
deba@409:       }
deba@409:     }
deba@409: 
deba@409:   public:
deba@409: 
kpeter@596:     /// \name Execution Control
deba@409:     /// The simplest way to execute the algorithm is to use
kpeter@559:     /// one of the member functions called \ref run().
deba@409:     /// \n
kpeter@596:     /// If you need better control on the execution,
kpeter@596:     /// you have to call one of the \ref init() functions first, then
kpeter@596:     /// \ref calculateOut() and/or \ref calculateIn().
deba@409: 
deba@409:     /// @{
deba@409: 
kpeter@596:     /// \brief Initialize the internal data structures.
deba@409:     ///
kpeter@596:     /// This function initializes the internal data structures. It creates
kpeter@596:     /// the maps and some bucket structures for the algorithm.
kpeter@596:     /// The first node is used as the source node for the push-relabel
kpeter@596:     /// algorithm.
deba@409:     void init() {
deba@409:       init(NodeIt(_graph));
deba@409:     }
deba@409: 
kpeter@596:     /// \brief Initialize the internal data structures.
deba@409:     ///
kpeter@596:     /// This function initializes the internal data structures. It creates
kpeter@596:     /// the maps and some bucket structures for the algorithm. 
kpeter@596:     /// The given node is used as the source node for the push-relabel
kpeter@596:     /// algorithm.
deba@409:     void init(const Node& source) {
deba@409:       _source = source;
deba@409: 
deba@409:       _node_num = countNodes(_graph);
deba@409: 
deba@411:       _first.resize(_node_num);
deba@411:       _last.resize(_node_num);
deba@409: 
deba@411:       _dormant.resize(_node_num);
deba@409: 
deba@409:       if (!_flow) {
deba@409:         _flow = new FlowMap(_graph);
deba@409:       }
deba@409:       if (!_next) {
deba@409:         _next = new typename Digraph::template NodeMap<Node>(_graph);
deba@409:       }
deba@409:       if (!_prev) {
deba@409:         _prev = new typename Digraph::template NodeMap<Node>(_graph);
deba@409:       }
deba@409:       if (!_active) {
deba@409:         _active = new typename Digraph::template NodeMap<bool>(_graph);
deba@409:       }
deba@409:       if (!_bucket) {
deba@409:         _bucket = new typename Digraph::template NodeMap<int>(_graph);
deba@409:       }
deba@409:       if (!_excess) {
deba@409:         _excess = new ExcessMap(_graph);
deba@409:       }
deba@409:       if (!_source_set) {
deba@409:         _source_set = new SourceSetMap(_graph);
deba@409:       }
deba@409:       if (!_min_cut_map) {
deba@409:         _min_cut_map = new MinCutMap(_graph);
deba@409:       }
deba@409: 
deba@409:       _min_cut = std::numeric_limits<Value>::max();
deba@409:     }
deba@409: 
deba@409: 
kpeter@596:     /// \brief Calculate a minimum cut with \f$ source \f$ on the
deba@409:     /// source-side.
deba@409:     ///
kpeter@596:     /// This function calculates a minimum cut with \f$ source \f$ on the
alpar@412:     /// source-side (i.e. a set \f$ X\subsetneq V \f$ with
kpeter@596:     /// \f$ source \in X \f$ and minimal outgoing capacity).
kpeter@596:     ///
kpeter@596:     /// \pre \ref init() must be called before using this function.
deba@409:     void calculateOut() {
deba@409:       findMinCutOut();
deba@409:     }
deba@409: 
kpeter@596:     /// \brief Calculate a minimum cut with \f$ source \f$ on the
kpeter@596:     /// sink-side.
deba@409:     ///
kpeter@596:     /// This function calculates a minimum cut with \f$ source \f$ on the
kpeter@596:     /// sink-side (i.e. a set \f$ X\subsetneq V \f$ with
kpeter@596:     /// \f$ source \notin X \f$ and minimal outgoing capacity).
kpeter@596:     ///
kpeter@596:     /// \pre \ref init() must be called before using this function.
deba@409:     void calculateIn() {
deba@409:       findMinCutIn();
deba@409:     }
deba@409: 
deba@409: 
kpeter@596:     /// \brief Run the algorithm.
deba@409:     ///
kpeter@596:     /// This function runs the algorithm. It finds nodes \c source and
kpeter@596:     /// \c target arbitrarily and then calls \ref init(), \ref calculateOut()
deba@409:     /// and \ref calculateIn().
deba@409:     void run() {
deba@409:       init();
deba@409:       calculateOut();
deba@409:       calculateIn();
deba@409:     }
deba@409: 
kpeter@596:     /// \brief Run the algorithm.
deba@409:     ///
kpeter@596:     /// This function runs the algorithm. It uses the given \c source node, 
kpeter@596:     /// finds a proper \c target node and then calls the \ref init(),
kpeter@596:     /// \ref calculateOut() and \ref calculateIn().
deba@409:     void run(const Node& s) {
deba@409:       init(s);
deba@409:       calculateOut();
deba@409:       calculateIn();
deba@409:     }
deba@409: 
deba@409:     /// @}
deba@409: 
deba@409:     /// \name Query Functions
deba@409:     /// The result of the %HaoOrlin algorithm
kpeter@596:     /// can be obtained using these functions.\n
kpeter@596:     /// \ref run(), \ref calculateOut() or \ref calculateIn() 
kpeter@596:     /// should be called before using them.
deba@409: 
deba@409:     /// @{
deba@409: 
kpeter@596:     /// \brief Return the value of the minimum cut.
deba@409:     ///
kpeter@596:     /// This function returns the value of the minimum cut.
kpeter@596:     ///
kpeter@596:     /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
kpeter@596:     /// must be called before using this function.
deba@409:     Value minCutValue() const {
deba@409:       return _min_cut;
deba@409:     }
deba@409: 
deba@409: 
kpeter@596:     /// \brief Return a minimum cut.
deba@409:     ///
kpeter@596:     /// This function sets \c cutMap to the characteristic vector of a
kpeter@596:     /// minimum value cut: it will give a non-empty set \f$ X\subsetneq V \f$
kpeter@596:     /// with minimal outgoing capacity (i.e. \c cutMap will be \c true exactly
kpeter@596:     /// for the nodes of \f$ X \f$).
kpeter@596:     ///
kpeter@596:     /// \param cutMap A \ref concepts::WriteMap "writable" node map with
kpeter@596:     /// \c bool (or convertible) value type.
kpeter@596:     ///
kpeter@596:     /// \return The value of the minimum cut.
kpeter@596:     ///
kpeter@596:     /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
kpeter@596:     /// must be called before using this function.
kpeter@596:     template <typename CutMap>
kpeter@596:     Value minCutMap(CutMap& cutMap) const {
deba@409:       for (NodeIt it(_graph); it != INVALID; ++it) {
kpeter@596:         cutMap.set(it, (*_min_cut_map)[it]);
deba@409:       }
deba@409:       return _min_cut;
deba@409:     }
deba@409: 
deba@409:     /// @}
deba@409: 
deba@409:   }; //class HaoOrlin
deba@409: 
deba@409: } //namespace lemon
deba@409: 
deba@409: #endif //LEMON_HAO_ORLIN_H