lemon/hao_orlin.h
changeset 409 b8ce15103485
child 411 01c443515ad2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/hao_orlin.h	Mon Dec 01 23:12:16 2008 +0100
     1.3 @@ -0,0 +1,985 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_HAO_ORLIN_H
    1.23 +#define LEMON_HAO_ORLIN_H
    1.24 +
    1.25 +#include <vector>
    1.26 +#include <list>
    1.27 +#include <limits>
    1.28 +
    1.29 +#include <lemon/maps.h>
    1.30 +#include <lemon/core.h>
    1.31 +#include <lemon/tolerance.h>
    1.32 +
    1.33 +/// \file
    1.34 +/// \ingroup min_cut
    1.35 +/// \brief Implementation of the Hao-Orlin algorithm.
    1.36 +///
    1.37 +/// Implementation of the Hao-Orlin algorithm class for testing network
    1.38 +/// reliability.
    1.39 +
    1.40 +namespace lemon {
    1.41 +
    1.42 +  /// \ingroup min_cut
    1.43 +  ///
    1.44 +  /// \brief %Hao-Orlin algorithm to find a minimum cut in directed graphs.
    1.45 +  ///
    1.46 +  /// Hao-Orlin calculates a minimum cut in a directed graph
    1.47 +  /// \f$D=(V,A)\f$. It takes a fixed node \f$ source \in V \f$ and
    1.48 +  /// consists of two phases: in the first phase it determines a
    1.49 +  /// minimum cut with \f$ source \f$ on the source-side (i.e. a set
    1.50 +  /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal
    1.51 +  /// out-degree) and in the second phase it determines a minimum cut
    1.52 +  /// with \f$ source \f$ on the sink-side (i.e. a set
    1.53 +  /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal
    1.54 +  /// out-degree). Obviously, the smaller of these two cuts will be a
    1.55 +  /// minimum cut of \f$ D \f$. The algorithm is a modified
    1.56 +  /// push-relabel preflow algorithm and our implementation calculates
    1.57 +  /// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
    1.58 +  /// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The
    1.59 +  /// purpose of such algorithm is testing network reliability. For an
    1.60 +  /// undirected graph you can run just the first phase of the
    1.61 +  /// algorithm or you can use the algorithm of Nagamochi and Ibaraki
    1.62 +  /// which solves the undirected problem in
    1.63 +  /// \f$ O(nm + n^2 \log(n)) \f$ time: it is implemented in the
    1.64 +  /// NagamochiIbaraki algorithm class.
    1.65 +  ///
    1.66 +  /// \param _Digraph is the graph type of the algorithm.
    1.67 +  /// \param _CapacityMap is an edge map of capacities which should
    1.68 +  /// be any numreric type. The default type is _Digraph::ArcMap<int>.
    1.69 +  /// \param _Tolerance is the handler of the inexact computation. The
    1.70 +  /// default type for this is Tolerance<CapacityMap::Value>.
    1.71 +#ifdef DOXYGEN
    1.72 +  template <typename _Digraph, typename _CapacityMap, typename _Tolerance>
    1.73 +#else
    1.74 +  template <typename _Digraph,
    1.75 +            typename _CapacityMap = typename _Digraph::template ArcMap<int>,
    1.76 +            typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
    1.77 +#endif
    1.78 +  class HaoOrlin {
    1.79 +  private:
    1.80 +
    1.81 +    typedef _Digraph Digraph;
    1.82 +    typedef _CapacityMap CapacityMap;
    1.83 +    typedef _Tolerance Tolerance;
    1.84 +
    1.85 +    typedef typename CapacityMap::Value Value;
    1.86 +
    1.87 +    TEMPLATE_GRAPH_TYPEDEFS(Digraph);
    1.88 +
    1.89 +    const Digraph& _graph;
    1.90 +    const CapacityMap* _capacity;
    1.91 +
    1.92 +    typedef typename Digraph::template ArcMap<Value> FlowMap;
    1.93 +    FlowMap* _flow;
    1.94 +
    1.95 +    Node _source;
    1.96 +
    1.97 +    int _node_num;
    1.98 +
    1.99 +    // Bucketing structure
   1.100 +    std::vector<Node> _first, _last;
   1.101 +    typename Digraph::template NodeMap<Node>* _next;
   1.102 +    typename Digraph::template NodeMap<Node>* _prev;
   1.103 +    typename Digraph::template NodeMap<bool>* _active;
   1.104 +    typename Digraph::template NodeMap<int>* _bucket;
   1.105 +
   1.106 +    std::vector<bool> _dormant;
   1.107 +
   1.108 +    std::list<std::list<int> > _sets;
   1.109 +    std::list<int>::iterator _highest;
   1.110 +
   1.111 +    typedef typename Digraph::template NodeMap<Value> ExcessMap;
   1.112 +    ExcessMap* _excess;
   1.113 +
   1.114 +    typedef typename Digraph::template NodeMap<bool> SourceSetMap;
   1.115 +    SourceSetMap* _source_set;
   1.116 +
   1.117 +    Value _min_cut;
   1.118 +
   1.119 +    typedef typename Digraph::template NodeMap<bool> MinCutMap;
   1.120 +    MinCutMap* _min_cut_map;
   1.121 +
   1.122 +    Tolerance _tolerance;
   1.123 +
   1.124 +  public:
   1.125 +
   1.126 +    /// \brief Constructor
   1.127 +    ///
   1.128 +    /// Constructor of the algorithm class.
   1.129 +    HaoOrlin(const Digraph& graph, const CapacityMap& capacity,
   1.130 +             const Tolerance& tolerance = Tolerance()) :
   1.131 +      _graph(graph), _capacity(&capacity), _flow(0), _source(),
   1.132 +      _node_num(), _first(), _last(), _next(0), _prev(0),
   1.133 +      _active(0), _bucket(0), _dormant(), _sets(), _highest(),
   1.134 +      _excess(0), _source_set(0), _min_cut(), _min_cut_map(0),
   1.135 +      _tolerance(tolerance) {}
   1.136 +
   1.137 +    ~HaoOrlin() {
   1.138 +      if (_min_cut_map) {
   1.139 +        delete _min_cut_map;
   1.140 +      }
   1.141 +      if (_source_set) {
   1.142 +        delete _source_set;
   1.143 +      }
   1.144 +      if (_excess) {
   1.145 +        delete _excess;
   1.146 +      }
   1.147 +      if (_next) {
   1.148 +        delete _next;
   1.149 +      }
   1.150 +      if (_prev) {
   1.151 +        delete _prev;
   1.152 +      }
   1.153 +      if (_active) {
   1.154 +        delete _active;
   1.155 +      }
   1.156 +      if (_bucket) {
   1.157 +        delete _bucket;
   1.158 +      }
   1.159 +      if (_flow) {
   1.160 +        delete _flow;
   1.161 +      }
   1.162 +    }
   1.163 +
   1.164 +  private:
   1.165 +
   1.166 +    void activate(const Node& i) {
   1.167 +      _active->set(i, true);
   1.168 +
   1.169 +      int bucket = (*_bucket)[i];
   1.170 +
   1.171 +      if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return;
   1.172 +      //unlace
   1.173 +      _next->set((*_prev)[i], (*_next)[i]);
   1.174 +      if ((*_next)[i] != INVALID) {
   1.175 +        _prev->set((*_next)[i], (*_prev)[i]);
   1.176 +      } else {
   1.177 +        _last[bucket] = (*_prev)[i];
   1.178 +      }
   1.179 +      //lace
   1.180 +      _next->set(i, _first[bucket]);
   1.181 +      _prev->set(_first[bucket], i);
   1.182 +      _prev->set(i, INVALID);
   1.183 +      _first[bucket] = i;
   1.184 +    }
   1.185 +
   1.186 +    void deactivate(const Node& i) {
   1.187 +      _active->set(i, false);
   1.188 +      int bucket = (*_bucket)[i];
   1.189 +
   1.190 +      if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return;
   1.191 +
   1.192 +      //unlace
   1.193 +      _prev->set((*_next)[i], (*_prev)[i]);
   1.194 +      if ((*_prev)[i] != INVALID) {
   1.195 +        _next->set((*_prev)[i], (*_next)[i]);
   1.196 +      } else {
   1.197 +        _first[bucket] = (*_next)[i];
   1.198 +      }
   1.199 +      //lace
   1.200 +      _prev->set(i, _last[bucket]);
   1.201 +      _next->set(_last[bucket], i);
   1.202 +      _next->set(i, INVALID);
   1.203 +      _last[bucket] = i;
   1.204 +    }
   1.205 +
   1.206 +    void addItem(const Node& i, int bucket) {
   1.207 +      (*_bucket)[i] = bucket;
   1.208 +      if (_last[bucket] != INVALID) {
   1.209 +        _prev->set(i, _last[bucket]);
   1.210 +        _next->set(_last[bucket], i);
   1.211 +        _next->set(i, INVALID);
   1.212 +        _last[bucket] = i;
   1.213 +      } else {
   1.214 +        _prev->set(i, INVALID);
   1.215 +        _first[bucket] = i;
   1.216 +        _next->set(i, INVALID);
   1.217 +        _last[bucket] = i;
   1.218 +      }
   1.219 +    }
   1.220 +
   1.221 +    void findMinCutOut() {
   1.222 +
   1.223 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   1.224 +        _excess->set(n, 0);
   1.225 +      }
   1.226 +
   1.227 +      for (ArcIt a(_graph); a != INVALID; ++a) {
   1.228 +        _flow->set(a, 0);
   1.229 +      }
   1.230 +
   1.231 +      int bucket_num = 1;
   1.232 +
   1.233 +      {
   1.234 +        typename Digraph::template NodeMap<bool> reached(_graph, false);
   1.235 +
   1.236 +        reached.set(_source, true);
   1.237 +
   1.238 +        bool first_set = true;
   1.239 +
   1.240 +        for (NodeIt t(_graph); t != INVALID; ++t) {
   1.241 +          if (reached[t]) continue;
   1.242 +          _sets.push_front(std::list<int>());
   1.243 +          _sets.front().push_front(bucket_num);
   1.244 +          _dormant[bucket_num] = !first_set;
   1.245 +
   1.246 +          _bucket->set(t, bucket_num);
   1.247 +          _first[bucket_num] = _last[bucket_num] = t;
   1.248 +          _next->set(t, INVALID);
   1.249 +          _prev->set(t, INVALID);
   1.250 +
   1.251 +          ++bucket_num;
   1.252 +
   1.253 +          std::vector<Node> queue;
   1.254 +          queue.push_back(t);
   1.255 +          reached.set(t, true);
   1.256 +
   1.257 +          while (!queue.empty()) {
   1.258 +            _sets.front().push_front(bucket_num);
   1.259 +            _dormant[bucket_num] = !first_set;
   1.260 +            _first[bucket_num] = _last[bucket_num] = INVALID;
   1.261 +
   1.262 +            std::vector<Node> nqueue;
   1.263 +            for (int i = 0; i < int(queue.size()); ++i) {
   1.264 +              Node n = queue[i];
   1.265 +              for (InArcIt a(_graph, n); a != INVALID; ++a) {
   1.266 +                Node u = _graph.source(a);
   1.267 +                if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
   1.268 +                  reached.set(u, true);
   1.269 +                  addItem(u, bucket_num);
   1.270 +                  nqueue.push_back(u);
   1.271 +                }
   1.272 +              }
   1.273 +            }
   1.274 +            queue.swap(nqueue);
   1.275 +            ++bucket_num;
   1.276 +          }
   1.277 +          _sets.front().pop_front();
   1.278 +          --bucket_num;
   1.279 +          first_set = false;
   1.280 +        }
   1.281 +
   1.282 +        _bucket->set(_source, 0);
   1.283 +        _dormant[0] = true;
   1.284 +      }
   1.285 +      _source_set->set(_source, true);
   1.286 +
   1.287 +      Node target = _last[_sets.back().back()];
   1.288 +      {
   1.289 +        for (OutArcIt a(_graph, _source); a != INVALID; ++a) {
   1.290 +          if (_tolerance.positive((*_capacity)[a])) {
   1.291 +            Node u = _graph.target(a);
   1.292 +            _flow->set(a, (*_capacity)[a]);
   1.293 +            _excess->set(u, (*_excess)[u] + (*_capacity)[a]);
   1.294 +            if (!(*_active)[u] && u != _source) {
   1.295 +              activate(u);
   1.296 +            }
   1.297 +          }
   1.298 +        }
   1.299 +
   1.300 +        if ((*_active)[target]) {
   1.301 +          deactivate(target);
   1.302 +        }
   1.303 +
   1.304 +        _highest = _sets.back().begin();
   1.305 +        while (_highest != _sets.back().end() &&
   1.306 +               !(*_active)[_first[*_highest]]) {
   1.307 +          ++_highest;
   1.308 +        }
   1.309 +      }
   1.310 +
   1.311 +      while (true) {
   1.312 +        while (_highest != _sets.back().end()) {
   1.313 +          Node n = _first[*_highest];
   1.314 +          Value excess = (*_excess)[n];
   1.315 +          int next_bucket = _node_num;
   1.316 +
   1.317 +          int under_bucket;
   1.318 +          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
   1.319 +            under_bucket = -1;
   1.320 +          } else {
   1.321 +            under_bucket = *(++std::list<int>::iterator(_highest));
   1.322 +          }
   1.323 +
   1.324 +          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
   1.325 +            Node v = _graph.target(a);
   1.326 +            if (_dormant[(*_bucket)[v]]) continue;
   1.327 +            Value rem = (*_capacity)[a] - (*_flow)[a];
   1.328 +            if (!_tolerance.positive(rem)) continue;
   1.329 +            if ((*_bucket)[v] == under_bucket) {
   1.330 +              if (!(*_active)[v] && v != target) {
   1.331 +                activate(v);
   1.332 +              }
   1.333 +              if (!_tolerance.less(rem, excess)) {
   1.334 +                _flow->set(a, (*_flow)[a] + excess);
   1.335 +                _excess->set(v, (*_excess)[v] + excess);
   1.336 +                excess = 0;
   1.337 +                goto no_more_push;
   1.338 +              } else {
   1.339 +                excess -= rem;
   1.340 +                _excess->set(v, (*_excess)[v] + rem);
   1.341 +                _flow->set(a, (*_capacity)[a]);
   1.342 +              }
   1.343 +            } else if (next_bucket > (*_bucket)[v]) {
   1.344 +              next_bucket = (*_bucket)[v];
   1.345 +            }
   1.346 +          }
   1.347 +
   1.348 +          for (InArcIt a(_graph, n); a != INVALID; ++a) {
   1.349 +            Node v = _graph.source(a);
   1.350 +            if (_dormant[(*_bucket)[v]]) continue;
   1.351 +            Value rem = (*_flow)[a];
   1.352 +            if (!_tolerance.positive(rem)) continue;
   1.353 +            if ((*_bucket)[v] == under_bucket) {
   1.354 +              if (!(*_active)[v] && v != target) {
   1.355 +                activate(v);
   1.356 +              }
   1.357 +              if (!_tolerance.less(rem, excess)) {
   1.358 +                _flow->set(a, (*_flow)[a] - excess);
   1.359 +                _excess->set(v, (*_excess)[v] + excess);
   1.360 +                excess = 0;
   1.361 +                goto no_more_push;
   1.362 +              } else {
   1.363 +                excess -= rem;
   1.364 +                _excess->set(v, (*_excess)[v] + rem);
   1.365 +                _flow->set(a, 0);
   1.366 +              }
   1.367 +            } else if (next_bucket > (*_bucket)[v]) {
   1.368 +              next_bucket = (*_bucket)[v];
   1.369 +            }
   1.370 +          }
   1.371 +
   1.372 +        no_more_push:
   1.373 +
   1.374 +          _excess->set(n, excess);
   1.375 +
   1.376 +          if (excess != 0) {
   1.377 +            if ((*_next)[n] == INVALID) {
   1.378 +              typename std::list<std::list<int> >::iterator new_set =
   1.379 +                _sets.insert(--_sets.end(), std::list<int>());
   1.380 +              new_set->splice(new_set->end(), _sets.back(),
   1.381 +                              _sets.back().begin(), ++_highest);
   1.382 +              for (std::list<int>::iterator it = new_set->begin();
   1.383 +                   it != new_set->end(); ++it) {
   1.384 +                _dormant[*it] = true;
   1.385 +              }
   1.386 +              while (_highest != _sets.back().end() &&
   1.387 +                     !(*_active)[_first[*_highest]]) {
   1.388 +                ++_highest;
   1.389 +              }
   1.390 +            } else if (next_bucket == _node_num) {
   1.391 +              _first[(*_bucket)[n]] = (*_next)[n];
   1.392 +              _prev->set((*_next)[n], INVALID);
   1.393 +
   1.394 +              std::list<std::list<int> >::iterator new_set =
   1.395 +                _sets.insert(--_sets.end(), std::list<int>());
   1.396 +
   1.397 +              new_set->push_front(bucket_num);
   1.398 +              _bucket->set(n, bucket_num);
   1.399 +              _first[bucket_num] = _last[bucket_num] = n;
   1.400 +              _next->set(n, INVALID);
   1.401 +              _prev->set(n, INVALID);
   1.402 +              _dormant[bucket_num] = true;
   1.403 +              ++bucket_num;
   1.404 +
   1.405 +              while (_highest != _sets.back().end() &&
   1.406 +                     !(*_active)[_first[*_highest]]) {
   1.407 +                ++_highest;
   1.408 +              }
   1.409 +            } else {
   1.410 +              _first[*_highest] = (*_next)[n];
   1.411 +              _prev->set((*_next)[n], INVALID);
   1.412 +
   1.413 +              while (next_bucket != *_highest) {
   1.414 +                --_highest;
   1.415 +              }
   1.416 +
   1.417 +              if (_highest == _sets.back().begin()) {
   1.418 +                _sets.back().push_front(bucket_num);
   1.419 +                _dormant[bucket_num] = false;
   1.420 +                _first[bucket_num] = _last[bucket_num] = INVALID;
   1.421 +                ++bucket_num;
   1.422 +              }
   1.423 +              --_highest;
   1.424 +
   1.425 +              _bucket->set(n, *_highest);
   1.426 +              _next->set(n, _first[*_highest]);
   1.427 +              if (_first[*_highest] != INVALID) {
   1.428 +                _prev->set(_first[*_highest], n);
   1.429 +              } else {
   1.430 +                _last[*_highest] = n;
   1.431 +              }
   1.432 +              _first[*_highest] = n;
   1.433 +            }
   1.434 +          } else {
   1.435 +
   1.436 +            deactivate(n);
   1.437 +            if (!(*_active)[_first[*_highest]]) {
   1.438 +              ++_highest;
   1.439 +              if (_highest != _sets.back().end() &&
   1.440 +                  !(*_active)[_first[*_highest]]) {
   1.441 +                _highest = _sets.back().end();
   1.442 +              }
   1.443 +            }
   1.444 +          }
   1.445 +        }
   1.446 +
   1.447 +        if ((*_excess)[target] < _min_cut) {
   1.448 +          _min_cut = (*_excess)[target];
   1.449 +          for (NodeIt i(_graph); i != INVALID; ++i) {
   1.450 +            _min_cut_map->set(i, true);
   1.451 +          }
   1.452 +          for (std::list<int>::iterator it = _sets.back().begin();
   1.453 +               it != _sets.back().end(); ++it) {
   1.454 +            Node n = _first[*it];
   1.455 +            while (n != INVALID) {
   1.456 +              _min_cut_map->set(n, false);
   1.457 +              n = (*_next)[n];
   1.458 +            }
   1.459 +          }
   1.460 +        }
   1.461 +
   1.462 +        {
   1.463 +          Node new_target;
   1.464 +          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
   1.465 +            if ((*_next)[target] == INVALID) {
   1.466 +              _last[(*_bucket)[target]] = (*_prev)[target];
   1.467 +              new_target = (*_prev)[target];
   1.468 +            } else {
   1.469 +              _prev->set((*_next)[target], (*_prev)[target]);
   1.470 +              new_target = (*_next)[target];
   1.471 +            }
   1.472 +            if ((*_prev)[target] == INVALID) {
   1.473 +              _first[(*_bucket)[target]] = (*_next)[target];
   1.474 +            } else {
   1.475 +              _next->set((*_prev)[target], (*_next)[target]);
   1.476 +            }
   1.477 +          } else {
   1.478 +            _sets.back().pop_back();
   1.479 +            if (_sets.back().empty()) {
   1.480 +              _sets.pop_back();
   1.481 +              if (_sets.empty())
   1.482 +                break;
   1.483 +              for (std::list<int>::iterator it = _sets.back().begin();
   1.484 +                   it != _sets.back().end(); ++it) {
   1.485 +                _dormant[*it] = false;
   1.486 +              }
   1.487 +            }
   1.488 +            new_target = _last[_sets.back().back()];
   1.489 +          }
   1.490 +
   1.491 +          _bucket->set(target, 0);
   1.492 +
   1.493 +          _source_set->set(target, true);
   1.494 +          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
   1.495 +            Value rem = (*_capacity)[a] - (*_flow)[a];
   1.496 +            if (!_tolerance.positive(rem)) continue;
   1.497 +            Node v = _graph.target(a);
   1.498 +            if (!(*_active)[v] && !(*_source_set)[v]) {
   1.499 +              activate(v);
   1.500 +            }
   1.501 +            _excess->set(v, (*_excess)[v] + rem);
   1.502 +            _flow->set(a, (*_capacity)[a]);
   1.503 +          }
   1.504 +
   1.505 +          for (InArcIt a(_graph, target); a != INVALID; ++a) {
   1.506 +            Value rem = (*_flow)[a];
   1.507 +            if (!_tolerance.positive(rem)) continue;
   1.508 +            Node v = _graph.source(a);
   1.509 +            if (!(*_active)[v] && !(*_source_set)[v]) {
   1.510 +              activate(v);
   1.511 +            }
   1.512 +            _excess->set(v, (*_excess)[v] + rem);
   1.513 +            _flow->set(a, 0);
   1.514 +          }
   1.515 +
   1.516 +          target = new_target;
   1.517 +          if ((*_active)[target]) {
   1.518 +            deactivate(target);
   1.519 +          }
   1.520 +
   1.521 +          _highest = _sets.back().begin();
   1.522 +          while (_highest != _sets.back().end() &&
   1.523 +                 !(*_active)[_first[*_highest]]) {
   1.524 +            ++_highest;
   1.525 +          }
   1.526 +        }
   1.527 +      }
   1.528 +    }
   1.529 +
   1.530 +    void findMinCutIn() {
   1.531 +
   1.532 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   1.533 +        _excess->set(n, 0);
   1.534 +      }
   1.535 +
   1.536 +      for (ArcIt a(_graph); a != INVALID; ++a) {
   1.537 +        _flow->set(a, 0);
   1.538 +      }
   1.539 +
   1.540 +      int bucket_num = 1;
   1.541 +
   1.542 +      {
   1.543 +        typename Digraph::template NodeMap<bool> reached(_graph, false);
   1.544 +
   1.545 +        reached.set(_source, true);
   1.546 +
   1.547 +        bool first_set = true;
   1.548 +
   1.549 +        for (NodeIt t(_graph); t != INVALID; ++t) {
   1.550 +          if (reached[t]) continue;
   1.551 +          _sets.push_front(std::list<int>());
   1.552 +          _sets.front().push_front(bucket_num);
   1.553 +          _dormant[bucket_num] = !first_set;
   1.554 +
   1.555 +          _bucket->set(t, bucket_num);
   1.556 +          _first[bucket_num] = _last[bucket_num] = t;
   1.557 +          _next->set(t, INVALID);
   1.558 +          _prev->set(t, INVALID);
   1.559 +
   1.560 +          ++bucket_num;
   1.561 +
   1.562 +          std::vector<Node> queue;
   1.563 +          queue.push_back(t);
   1.564 +          reached.set(t, true);
   1.565 +
   1.566 +          while (!queue.empty()) {
   1.567 +            _sets.front().push_front(bucket_num);
   1.568 +            _dormant[bucket_num] = !first_set;
   1.569 +            _first[bucket_num] = _last[bucket_num] = INVALID;
   1.570 +
   1.571 +            std::vector<Node> nqueue;
   1.572 +            for (int i = 0; i < int(queue.size()); ++i) {
   1.573 +              Node n = queue[i];
   1.574 +              for (OutArcIt a(_graph, n); a != INVALID; ++a) {
   1.575 +                Node u = _graph.target(a);
   1.576 +                if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
   1.577 +                  reached.set(u, true);
   1.578 +                  addItem(u, bucket_num);
   1.579 +                  nqueue.push_back(u);
   1.580 +                }
   1.581 +              }
   1.582 +            }
   1.583 +            queue.swap(nqueue);
   1.584 +            ++bucket_num;
   1.585 +          }
   1.586 +          _sets.front().pop_front();
   1.587 +          --bucket_num;
   1.588 +          first_set = false;
   1.589 +        }
   1.590 +
   1.591 +        _bucket->set(_source, 0);
   1.592 +        _dormant[0] = true;
   1.593 +      }
   1.594 +      _source_set->set(_source, true);
   1.595 +
   1.596 +      Node target = _last[_sets.back().back()];
   1.597 +      {
   1.598 +        for (InArcIt a(_graph, _source); a != INVALID; ++a) {
   1.599 +          if (_tolerance.positive((*_capacity)[a])) {
   1.600 +            Node u = _graph.source(a);
   1.601 +            _flow->set(a, (*_capacity)[a]);
   1.602 +            _excess->set(u, (*_excess)[u] + (*_capacity)[a]);
   1.603 +            if (!(*_active)[u] && u != _source) {
   1.604 +              activate(u);
   1.605 +            }
   1.606 +          }
   1.607 +        }
   1.608 +        if ((*_active)[target]) {
   1.609 +          deactivate(target);
   1.610 +        }
   1.611 +
   1.612 +        _highest = _sets.back().begin();
   1.613 +        while (_highest != _sets.back().end() &&
   1.614 +               !(*_active)[_first[*_highest]]) {
   1.615 +          ++_highest;
   1.616 +        }
   1.617 +      }
   1.618 +
   1.619 +
   1.620 +      while (true) {
   1.621 +        while (_highest != _sets.back().end()) {
   1.622 +          Node n = _first[*_highest];
   1.623 +          Value excess = (*_excess)[n];
   1.624 +          int next_bucket = _node_num;
   1.625 +
   1.626 +          int under_bucket;
   1.627 +          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
   1.628 +            under_bucket = -1;
   1.629 +          } else {
   1.630 +            under_bucket = *(++std::list<int>::iterator(_highest));
   1.631 +          }
   1.632 +
   1.633 +          for (InArcIt a(_graph, n); a != INVALID; ++a) {
   1.634 +            Node v = _graph.source(a);
   1.635 +            if (_dormant[(*_bucket)[v]]) continue;
   1.636 +            Value rem = (*_capacity)[a] - (*_flow)[a];
   1.637 +            if (!_tolerance.positive(rem)) continue;
   1.638 +            if ((*_bucket)[v] == under_bucket) {
   1.639 +              if (!(*_active)[v] && v != target) {
   1.640 +                activate(v);
   1.641 +              }
   1.642 +              if (!_tolerance.less(rem, excess)) {
   1.643 +                _flow->set(a, (*_flow)[a] + excess);
   1.644 +                _excess->set(v, (*_excess)[v] + excess);
   1.645 +                excess = 0;
   1.646 +                goto no_more_push;
   1.647 +              } else {
   1.648 +                excess -= rem;
   1.649 +                _excess->set(v, (*_excess)[v] + rem);
   1.650 +                _flow->set(a, (*_capacity)[a]);
   1.651 +              }
   1.652 +            } else if (next_bucket > (*_bucket)[v]) {
   1.653 +              next_bucket = (*_bucket)[v];
   1.654 +            }
   1.655 +          }
   1.656 +
   1.657 +          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
   1.658 +            Node v = _graph.target(a);
   1.659 +            if (_dormant[(*_bucket)[v]]) continue;
   1.660 +            Value rem = (*_flow)[a];
   1.661 +            if (!_tolerance.positive(rem)) continue;
   1.662 +            if ((*_bucket)[v] == under_bucket) {
   1.663 +              if (!(*_active)[v] && v != target) {
   1.664 +                activate(v);
   1.665 +              }
   1.666 +              if (!_tolerance.less(rem, excess)) {
   1.667 +                _flow->set(a, (*_flow)[a] - excess);
   1.668 +                _excess->set(v, (*_excess)[v] + excess);
   1.669 +                excess = 0;
   1.670 +                goto no_more_push;
   1.671 +              } else {
   1.672 +                excess -= rem;
   1.673 +                _excess->set(v, (*_excess)[v] + rem);
   1.674 +                _flow->set(a, 0);
   1.675 +              }
   1.676 +            } else if (next_bucket > (*_bucket)[v]) {
   1.677 +              next_bucket = (*_bucket)[v];
   1.678 +            }
   1.679 +          }
   1.680 +
   1.681 +        no_more_push:
   1.682 +
   1.683 +          _excess->set(n, excess);
   1.684 +
   1.685 +          if (excess != 0) {
   1.686 +            if ((*_next)[n] == INVALID) {
   1.687 +              typename std::list<std::list<int> >::iterator new_set =
   1.688 +                _sets.insert(--_sets.end(), std::list<int>());
   1.689 +              new_set->splice(new_set->end(), _sets.back(),
   1.690 +                              _sets.back().begin(), ++_highest);
   1.691 +              for (std::list<int>::iterator it = new_set->begin();
   1.692 +                   it != new_set->end(); ++it) {
   1.693 +                _dormant[*it] = true;
   1.694 +              }
   1.695 +              while (_highest != _sets.back().end() &&
   1.696 +                     !(*_active)[_first[*_highest]]) {
   1.697 +                ++_highest;
   1.698 +              }
   1.699 +            } else if (next_bucket == _node_num) {
   1.700 +              _first[(*_bucket)[n]] = (*_next)[n];
   1.701 +              _prev->set((*_next)[n], INVALID);
   1.702 +
   1.703 +              std::list<std::list<int> >::iterator new_set =
   1.704 +                _sets.insert(--_sets.end(), std::list<int>());
   1.705 +
   1.706 +              new_set->push_front(bucket_num);
   1.707 +              _bucket->set(n, bucket_num);
   1.708 +              _first[bucket_num] = _last[bucket_num] = n;
   1.709 +              _next->set(n, INVALID);
   1.710 +              _prev->set(n, INVALID);
   1.711 +              _dormant[bucket_num] = true;
   1.712 +              ++bucket_num;
   1.713 +
   1.714 +              while (_highest != _sets.back().end() &&
   1.715 +                     !(*_active)[_first[*_highest]]) {
   1.716 +                ++_highest;
   1.717 +              }
   1.718 +            } else {
   1.719 +              _first[*_highest] = (*_next)[n];
   1.720 +              _prev->set((*_next)[n], INVALID);
   1.721 +
   1.722 +              while (next_bucket != *_highest) {
   1.723 +                --_highest;
   1.724 +              }
   1.725 +              if (_highest == _sets.back().begin()) {
   1.726 +                _sets.back().push_front(bucket_num);
   1.727 +                _dormant[bucket_num] = false;
   1.728 +                _first[bucket_num] = _last[bucket_num] = INVALID;
   1.729 +                ++bucket_num;
   1.730 +              }
   1.731 +              --_highest;
   1.732 +
   1.733 +              _bucket->set(n, *_highest);
   1.734 +              _next->set(n, _first[*_highest]);
   1.735 +              if (_first[*_highest] != INVALID) {
   1.736 +                _prev->set(_first[*_highest], n);
   1.737 +              } else {
   1.738 +                _last[*_highest] = n;
   1.739 +              }
   1.740 +              _first[*_highest] = n;
   1.741 +            }
   1.742 +          } else {
   1.743 +
   1.744 +            deactivate(n);
   1.745 +            if (!(*_active)[_first[*_highest]]) {
   1.746 +              ++_highest;
   1.747 +              if (_highest != _sets.back().end() &&
   1.748 +                  !(*_active)[_first[*_highest]]) {
   1.749 +                _highest = _sets.back().end();
   1.750 +              }
   1.751 +            }
   1.752 +          }
   1.753 +        }
   1.754 +
   1.755 +        if ((*_excess)[target] < _min_cut) {
   1.756 +          _min_cut = (*_excess)[target];
   1.757 +          for (NodeIt i(_graph); i != INVALID; ++i) {
   1.758 +            _min_cut_map->set(i, false);
   1.759 +          }
   1.760 +          for (std::list<int>::iterator it = _sets.back().begin();
   1.761 +               it != _sets.back().end(); ++it) {
   1.762 +            Node n = _first[*it];
   1.763 +            while (n != INVALID) {
   1.764 +              _min_cut_map->set(n, true);
   1.765 +              n = (*_next)[n];
   1.766 +            }
   1.767 +          }
   1.768 +        }
   1.769 +
   1.770 +        {
   1.771 +          Node new_target;
   1.772 +          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
   1.773 +            if ((*_next)[target] == INVALID) {
   1.774 +              _last[(*_bucket)[target]] = (*_prev)[target];
   1.775 +              new_target = (*_prev)[target];
   1.776 +            } else {
   1.777 +              _prev->set((*_next)[target], (*_prev)[target]);
   1.778 +              new_target = (*_next)[target];
   1.779 +            }
   1.780 +            if ((*_prev)[target] == INVALID) {
   1.781 +              _first[(*_bucket)[target]] = (*_next)[target];
   1.782 +            } else {
   1.783 +              _next->set((*_prev)[target], (*_next)[target]);
   1.784 +            }
   1.785 +          } else {
   1.786 +            _sets.back().pop_back();
   1.787 +            if (_sets.back().empty()) {
   1.788 +              _sets.pop_back();
   1.789 +              if (_sets.empty())
   1.790 +                break;
   1.791 +              for (std::list<int>::iterator it = _sets.back().begin();
   1.792 +                   it != _sets.back().end(); ++it) {
   1.793 +                _dormant[*it] = false;
   1.794 +              }
   1.795 +            }
   1.796 +            new_target = _last[_sets.back().back()];
   1.797 +          }
   1.798 +
   1.799 +          _bucket->set(target, 0);
   1.800 +
   1.801 +          _source_set->set(target, true);
   1.802 +          for (InArcIt a(_graph, target); a != INVALID; ++a) {
   1.803 +            Value rem = (*_capacity)[a] - (*_flow)[a];
   1.804 +            if (!_tolerance.positive(rem)) continue;
   1.805 +            Node v = _graph.source(a);
   1.806 +            if (!(*_active)[v] && !(*_source_set)[v]) {
   1.807 +              activate(v);
   1.808 +            }
   1.809 +            _excess->set(v, (*_excess)[v] + rem);
   1.810 +            _flow->set(a, (*_capacity)[a]);
   1.811 +          }
   1.812 +
   1.813 +          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
   1.814 +            Value rem = (*_flow)[a];
   1.815 +            if (!_tolerance.positive(rem)) continue;
   1.816 +            Node v = _graph.target(a);
   1.817 +            if (!(*_active)[v] && !(*_source_set)[v]) {
   1.818 +              activate(v);
   1.819 +            }
   1.820 +            _excess->set(v, (*_excess)[v] + rem);
   1.821 +            _flow->set(a, 0);
   1.822 +          }
   1.823 +
   1.824 +          target = new_target;
   1.825 +          if ((*_active)[target]) {
   1.826 +            deactivate(target);
   1.827 +          }
   1.828 +
   1.829 +          _highest = _sets.back().begin();
   1.830 +          while (_highest != _sets.back().end() &&
   1.831 +                 !(*_active)[_first[*_highest]]) {
   1.832 +            ++_highest;
   1.833 +          }
   1.834 +        }
   1.835 +      }
   1.836 +    }
   1.837 +
   1.838 +  public:
   1.839 +
   1.840 +    /// \name Execution control
   1.841 +    /// The simplest way to execute the algorithm is to use
   1.842 +    /// one of the member functions called \c run(...).
   1.843 +    /// \n
   1.844 +    /// If you need more control on the execution,
   1.845 +    /// first you must call \ref init(), then the \ref calculateIn() or
   1.846 +    /// \ref calculateIn() functions.
   1.847 +
   1.848 +    /// @{
   1.849 +
   1.850 +    /// \brief Initializes the internal data structures.
   1.851 +    ///
   1.852 +    /// Initializes the internal data structures. It creates
   1.853 +    /// the maps, residual graph adaptors and some bucket structures
   1.854 +    /// for the algorithm.
   1.855 +    void init() {
   1.856 +      init(NodeIt(_graph));
   1.857 +    }
   1.858 +
   1.859 +    /// \brief Initializes the internal data structures.
   1.860 +    ///
   1.861 +    /// Initializes the internal data structures. It creates
   1.862 +    /// the maps, residual graph adaptor and some bucket structures
   1.863 +    /// for the algorithm. Node \c source  is used as the push-relabel
   1.864 +    /// algorithm's source.
   1.865 +    void init(const Node& source) {
   1.866 +      _source = source;
   1.867 +
   1.868 +      _node_num = countNodes(_graph);
   1.869 +
   1.870 +      _first.resize(_node_num + 1);
   1.871 +      _last.resize(_node_num + 1);
   1.872 +
   1.873 +      _dormant.resize(_node_num + 1);
   1.874 +
   1.875 +      if (!_flow) {
   1.876 +        _flow = new FlowMap(_graph);
   1.877 +      }
   1.878 +      if (!_next) {
   1.879 +        _next = new typename Digraph::template NodeMap<Node>(_graph);
   1.880 +      }
   1.881 +      if (!_prev) {
   1.882 +        _prev = new typename Digraph::template NodeMap<Node>(_graph);
   1.883 +      }
   1.884 +      if (!_active) {
   1.885 +        _active = new typename Digraph::template NodeMap<bool>(_graph);
   1.886 +      }
   1.887 +      if (!_bucket) {
   1.888 +        _bucket = new typename Digraph::template NodeMap<int>(_graph);
   1.889 +      }
   1.890 +      if (!_excess) {
   1.891 +        _excess = new ExcessMap(_graph);
   1.892 +      }
   1.893 +      if (!_source_set) {
   1.894 +        _source_set = new SourceSetMap(_graph);
   1.895 +      }
   1.896 +      if (!_min_cut_map) {
   1.897 +        _min_cut_map = new MinCutMap(_graph);
   1.898 +      }
   1.899 +
   1.900 +      _min_cut = std::numeric_limits<Value>::max();
   1.901 +    }
   1.902 +
   1.903 +
   1.904 +    /// \brief Calculates a minimum cut with \f$ source \f$ on the
   1.905 +    /// source-side.
   1.906 +    ///
   1.907 +    /// Calculates a minimum cut with \f$ source \f$ on the
   1.908 +    /// source-side (i.e. a set \f$ X\subsetneq V \f$ with \f$ source
   1.909 +    /// \in X \f$ and minimal out-degree).
   1.910 +    void calculateOut() {
   1.911 +      findMinCutOut();
   1.912 +    }
   1.913 +
   1.914 +    /// \brief Calculates a minimum cut with \f$ source \f$ on the
   1.915 +    /// target-side.
   1.916 +    ///
   1.917 +    /// Calculates a minimum cut with \f$ source \f$ on the
   1.918 +    /// target-side (i.e. a set \f$ X\subsetneq V \f$ with \f$ source
   1.919 +    /// \in X \f$ and minimal out-degree).
   1.920 +    void calculateIn() {
   1.921 +      findMinCutIn();
   1.922 +    }
   1.923 +
   1.924 +
   1.925 +    /// \brief Runs the algorithm.
   1.926 +    ///
   1.927 +    /// Runs the algorithm. It finds nodes \c source and \c target
   1.928 +    /// arbitrarily and then calls \ref init(), \ref calculateOut()
   1.929 +    /// and \ref calculateIn().
   1.930 +    void run() {
   1.931 +      init();
   1.932 +      calculateOut();
   1.933 +      calculateIn();
   1.934 +    }
   1.935 +
   1.936 +    /// \brief Runs the algorithm.
   1.937 +    ///
   1.938 +    /// Runs the algorithm. It uses the given \c source node, finds a
   1.939 +    /// proper \c target and then calls the \ref init(), \ref
   1.940 +    /// calculateOut() and \ref calculateIn().
   1.941 +    void run(const Node& s) {
   1.942 +      init(s);
   1.943 +      calculateOut();
   1.944 +      calculateIn();
   1.945 +    }
   1.946 +
   1.947 +    /// @}
   1.948 +
   1.949 +    /// \name Query Functions
   1.950 +    /// The result of the %HaoOrlin algorithm
   1.951 +    /// can be obtained using these functions.
   1.952 +    /// \n
   1.953 +    /// Before using these functions, either \ref run(), \ref
   1.954 +    /// calculateOut() or \ref calculateIn() must be called.
   1.955 +
   1.956 +    /// @{
   1.957 +
   1.958 +    /// \brief Returns the value of the minimum value cut.
   1.959 +    ///
   1.960 +    /// Returns the value of the minimum value cut.
   1.961 +    Value minCutValue() const {
   1.962 +      return _min_cut;
   1.963 +    }
   1.964 +
   1.965 +
   1.966 +    /// \brief Returns a minimum cut.
   1.967 +    ///
   1.968 +    /// Sets \c nodeMap to the characteristic vector of a minimum
   1.969 +    /// value cut: it will give a nonempty set \f$ X\subsetneq V \f$
   1.970 +    /// with minimal out-degree (i.e. \c nodeMap will be true exactly
   1.971 +    /// for the nodes of \f$ X \f$).  \pre nodeMap should be a
   1.972 +    /// bool-valued node-map.
   1.973 +    template <typename NodeMap>
   1.974 +    Value minCutMap(NodeMap& nodeMap) const {
   1.975 +      for (NodeIt it(_graph); it != INVALID; ++it) {
   1.976 +        nodeMap.set(it, (*_min_cut_map)[it]);
   1.977 +      }
   1.978 +      return _min_cut;
   1.979 +    }
   1.980 +
   1.981 +    /// @}
   1.982 +
   1.983 +  }; //class HaoOrlin
   1.984 +
   1.985 +
   1.986 +} //namespace lemon
   1.987 +
   1.988 +#endif //LEMON_HAO_ORLIN_H