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@761: * Copyright (C) 2003-2011 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 deba@409: #include deba@409: #include deba@409: deba@409: #include deba@409: #include deba@409: #include deba@409: deba@409: /// \file deba@409: /// \ingroup min_cut deba@409: /// \brief Implementation of the Hao-Orlin algorithm. deba@409: /// alpar@761: /// Implementation of the Hao-Orlin algorithm for finding a minimum cut kpeter@588: /// in a digraph. deba@409: deba@409: namespace lemon { deba@409: deba@409: /// \ingroup min_cut deba@409: /// kpeter@588: /// \brief Hao-Orlin algorithm for finding a minimum cut in a digraph. deba@409: /// kpeter@588: /// This class implements the Hao-Orlin algorithm for finding a minimum alpar@761: /// value cut in a directed graph \f$D=(V,A)\f$. kpeter@588: /// 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@588: /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal outgoing kpeter@588: /// 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@588: /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal outgoing kpeter@588: /// 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@588: /// 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@588: /// purpose of such algorithm is e.g. testing network reliability. deba@409: /// kpeter@588: /// For an undirected graph you can run just the first phase of the kpeter@588: /// algorithm or you can use the algorithm of Nagamochi and Ibaraki, alpar@761: /// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ kpeter@588: /// time. It is implemented in the NagamochiIbaraki algorithm class. kpeter@588: /// kpeter@588: /// \tparam GR The type of the digraph the algorithm runs on. kpeter@588: /// \tparam CAP The type of the arc map containing the capacities, kpeter@588: /// which can be any numreric type. The default map type is kpeter@588: /// \ref concepts::Digraph::ArcMap "GR::ArcMap". kpeter@588: /// \tparam TOL Tolerance class for handling inexact computations. The kpeter@550: /// default tolerance type is \ref Tolerance "Tolerance". deba@409: #ifdef DOXYGEN kpeter@550: template deba@409: #else kpeter@550: template , kpeter@550: typename TOL = Tolerance > deba@409: #endif deba@409: class HaoOrlin { kpeter@588: public: alpar@761: kpeter@588: /// The digraph type of the algorithm kpeter@588: typedef GR Digraph; kpeter@588: /// The capacity map type of the algorithm kpeter@588: typedef CAP CapacityMap; kpeter@588: /// The tolerance type of the algorithm kpeter@588: typedef TOL Tolerance; kpeter@588: deba@409: private: deba@409: deba@409: typedef typename CapacityMap::Value Value; deba@409: kpeter@588: 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 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 _first, _last; deba@409: typename Digraph::template NodeMap* _next; deba@409: typename Digraph::template NodeMap* _prev; deba@409: typename Digraph::template NodeMap* _active; deba@409: typename Digraph::template NodeMap* _bucket; deba@409: deba@409: std::vector _dormant; deba@409: deba@409: std::list > _sets; deba@409: std::list::iterator _highest; deba@409: deba@409: typedef typename Digraph::template NodeMap ExcessMap; deba@409: ExcessMap* _excess; deba@409: deba@409: typedef typename Digraph::template NodeMap SourceSetMap; deba@409: SourceSetMap* _source_set; deba@409: deba@409: Value _min_cut; deba@409: deba@409: typedef typename Digraph::template NodeMap 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@573: (*_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@573: (*_next)[(*_prev)[i]] = (*_next)[i]; deba@409: if ((*_next)[i] != INVALID) { kpeter@573: (*_prev)[(*_next)[i]] = (*_prev)[i]; deba@409: } else { deba@409: _last[bucket] = (*_prev)[i]; deba@409: } deba@409: //lace kpeter@573: (*_next)[i] = _first[bucket]; kpeter@573: (*_prev)[_first[bucket]] = i; kpeter@573: (*_prev)[i] = INVALID; deba@409: _first[bucket] = i; deba@409: } deba@409: deba@409: void deactivate(const Node& i) { kpeter@573: (*_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@573: (*_prev)[(*_next)[i]] = (*_prev)[i]; deba@409: if ((*_prev)[i] != INVALID) { kpeter@573: (*_next)[(*_prev)[i]] = (*_next)[i]; deba@409: } else { deba@409: _first[bucket] = (*_next)[i]; deba@409: } deba@409: //lace kpeter@573: (*_prev)[i] = _last[bucket]; kpeter@573: (*_next)[_last[bucket]] = i; kpeter@573: (*_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@573: (*_prev)[i] = _last[bucket]; kpeter@573: (*_next)[_last[bucket]] = i; kpeter@573: (*_next)[i] = INVALID; deba@409: _last[bucket] = i; deba@409: } else { kpeter@573: (*_prev)[i] = INVALID; deba@409: _first[bucket] = i; kpeter@573: (*_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@573: (*_excess)[n] = 0; deba@589: (*_source_set)[n] = false; deba@409: } deba@409: deba@409: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@573: (*_flow)[a] = 0; deba@409: } deba@409: deba@411: int bucket_num = 0; deba@411: std::vector queue(_node_num); deba@411: int qfirst = 0, qlast = 0, qsep = 0; deba@409: deba@409: { deba@409: typename Digraph::template NodeMap reached(_graph, false); deba@409: kpeter@573: 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()); alpar@440: deba@411: queue[qlast++] = t; kpeter@573: 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@573: 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@573: (*_bucket)[_source] = 0; deba@409: _dormant[0] = true; deba@409: } kpeter@573: (*_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@573: (*_flow)[a] = (*_capacity)[a]; kpeter@573: (*_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::iterator(_highest) == _sets.back().end()) { deba@409: under_bucket = -1; deba@409: } else { deba@409: under_bucket = *(++std::list::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@573: (*_flow)[a] += excess; kpeter@573: (*_excess)[v] += excess; deba@409: excess = 0; deba@409: goto no_more_push; deba@409: } else { deba@409: excess -= rem; kpeter@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_flow)[a] -= excess; kpeter@573: (*_excess)[v] += excess; deba@409: excess = 0; deba@409: goto no_more_push; deba@409: } else { deba@409: excess -= rem; kpeter@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_excess)[n] = excess; deba@409: deba@409: if (excess != 0) { deba@409: if ((*_next)[n] == INVALID) { deba@409: typename std::list >::iterator new_set = deba@409: _sets.insert(--_sets.end(), std::list()); deba@409: new_set->splice(new_set->end(), _sets.back(), deba@409: _sets.back().begin(), ++_highest); deba@409: for (std::list::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@573: (*_prev)[(*_next)[n]] = INVALID; deba@409: deba@409: std::list >::iterator new_set = deba@409: _sets.insert(--_sets.end(), std::list()); deba@409: deba@409: new_set->push_front(bucket_num); kpeter@573: (*_bucket)[n] = bucket_num; deba@409: _first[bucket_num] = _last[bucket_num] = n; kpeter@573: (*_next)[n] = INVALID; kpeter@573: (*_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@573: (*_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@573: (*_bucket)[n] = *_highest; kpeter@573: (*_next)[n] = _first[*_highest]; deba@409: if (_first[*_highest] != INVALID) { kpeter@573: (*_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@573: (*_min_cut_map)[i] = true; deba@409: } deba@409: for (std::list::iterator it = _sets.back().begin(); deba@409: it != _sets.back().end(); ++it) { deba@409: Node n = _first[*it]; deba@409: while (n != INVALID) { kpeter@573: (*_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@573: (*_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@573: (*_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::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@573: (*_bucket)[target] = 0; deba@409: kpeter@573: (*_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@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_excess)[n] = 0; deba@589: (*_source_set)[n] = false; deba@409: } deba@409: deba@409: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@573: (*_flow)[a] = 0; deba@409: } deba@409: deba@411: int bucket_num = 0; deba@411: std::vector queue(_node_num); deba@411: int qfirst = 0, qlast = 0, qsep = 0; deba@409: deba@409: { deba@409: typename Digraph::template NodeMap reached(_graph, false); deba@409: kpeter@573: 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()); alpar@440: deba@411: queue[qlast++] = t; kpeter@573: 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@573: 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@573: (*_bucket)[_source] = 0; deba@409: _dormant[0] = true; deba@409: } kpeter@573: (*_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@573: (*_flow)[a] = (*_capacity)[a]; kpeter@573: (*_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::iterator(_highest) == _sets.back().end()) { deba@409: under_bucket = -1; deba@409: } else { deba@409: under_bucket = *(++std::list::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@573: (*_flow)[a] += excess; kpeter@573: (*_excess)[v] += excess; deba@409: excess = 0; deba@409: goto no_more_push; deba@409: } else { deba@409: excess -= rem; kpeter@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_flow)[a] -= excess; kpeter@573: (*_excess)[v] += excess; deba@409: excess = 0; deba@409: goto no_more_push; deba@409: } else { deba@409: excess -= rem; kpeter@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_excess)[n] = excess; deba@409: deba@409: if (excess != 0) { deba@409: if ((*_next)[n] == INVALID) { deba@409: typename std::list >::iterator new_set = deba@409: _sets.insert(--_sets.end(), std::list()); deba@409: new_set->splice(new_set->end(), _sets.back(), deba@409: _sets.back().begin(), ++_highest); deba@409: for (std::list::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@573: (*_prev)[(*_next)[n]] = INVALID; deba@409: deba@409: std::list >::iterator new_set = deba@409: _sets.insert(--_sets.end(), std::list()); deba@409: deba@409: new_set->push_front(bucket_num); kpeter@573: (*_bucket)[n] = bucket_num; deba@409: _first[bucket_num] = _last[bucket_num] = n; kpeter@573: (*_next)[n] = INVALID; kpeter@573: (*_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@573: (*_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@573: (*_bucket)[n] = *_highest; kpeter@573: (*_next)[n] = _first[*_highest]; deba@409: if (_first[*_highest] != INVALID) { kpeter@573: (*_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@573: (*_min_cut_map)[i] = false; deba@409: } deba@409: for (std::list::iterator it = _sets.back().begin(); deba@409: it != _sets.back().end(); ++it) { deba@409: Node n = _first[*it]; deba@409: while (n != INVALID) { kpeter@573: (*_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@573: (*_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@573: (*_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::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@573: (*_bucket)[target] = 0; deba@409: kpeter@573: (*_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@573: (*_excess)[v] += rem; kpeter@573: (*_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@573: (*_excess)[v] += rem; kpeter@573: (*_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@588: /// \name Execution Control deba@409: /// The simplest way to execute the algorithm is to use kpeter@550: /// one of the member functions called \ref run(). deba@409: /// \n kpeter@588: /// If you need better control on the execution, kpeter@588: /// you have to call one of the \ref init() functions first, then kpeter@588: /// \ref calculateOut() and/or \ref calculateIn(). deba@409: deba@409: /// @{ deba@409: kpeter@588: /// \brief Initialize the internal data structures. deba@409: /// kpeter@588: /// This function initializes the internal data structures. It creates kpeter@588: /// the maps and some bucket structures for the algorithm. kpeter@588: /// The first node is used as the source node for the push-relabel kpeter@588: /// algorithm. deba@409: void init() { deba@409: init(NodeIt(_graph)); deba@409: } deba@409: kpeter@588: /// \brief Initialize the internal data structures. deba@409: /// kpeter@588: /// This function initializes the internal data structures. It creates alpar@761: /// the maps and some bucket structures for the algorithm. kpeter@588: /// The given node is used as the source node for the push-relabel kpeter@588: /// 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(_graph); deba@409: } deba@409: if (!_prev) { deba@409: _prev = new typename Digraph::template NodeMap(_graph); deba@409: } deba@409: if (!_active) { deba@409: _active = new typename Digraph::template NodeMap(_graph); deba@409: } deba@409: if (!_bucket) { deba@409: _bucket = new typename Digraph::template NodeMap(_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::max(); deba@409: } deba@409: deba@409: kpeter@588: /// \brief Calculate a minimum cut with \f$ source \f$ on the deba@409: /// source-side. deba@409: /// kpeter@588: /// 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@588: /// \f$ source \in X \f$ and minimal outgoing capacity). kpeter@588: /// kpeter@588: /// \pre \ref init() must be called before using this function. deba@409: void calculateOut() { deba@409: findMinCutOut(); deba@409: } deba@409: kpeter@588: /// \brief Calculate a minimum cut with \f$ source \f$ on the kpeter@588: /// sink-side. deba@409: /// kpeter@588: /// This function calculates a minimum cut with \f$ source \f$ on the kpeter@588: /// sink-side (i.e. a set \f$ X\subsetneq V \f$ with kpeter@588: /// \f$ source \notin X \f$ and minimal outgoing capacity). kpeter@588: /// kpeter@588: /// \pre \ref init() must be called before using this function. deba@409: void calculateIn() { deba@409: findMinCutIn(); deba@409: } deba@409: deba@409: kpeter@588: /// \brief Run the algorithm. deba@409: /// kpeter@588: /// This function runs the algorithm. It finds nodes \c source and kpeter@588: /// \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@588: /// \brief Run the algorithm. deba@409: /// alpar@761: /// This function runs the algorithm. It uses the given \c source node, kpeter@588: /// finds a proper \c target node and then calls the \ref init(), kpeter@588: /// \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@588: /// can be obtained using these functions.\n alpar@761: /// \ref run(), \ref calculateOut() or \ref calculateIn() kpeter@588: /// should be called before using them. deba@409: deba@409: /// @{ deba@409: kpeter@588: /// \brief Return the value of the minimum cut. deba@409: /// kpeter@588: /// This function returns the value of the minimum cut. kpeter@588: /// alpar@761: /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() kpeter@588: /// must be called before using this function. deba@409: Value minCutValue() const { deba@409: return _min_cut; deba@409: } deba@409: deba@409: kpeter@588: /// \brief Return a minimum cut. deba@409: /// kpeter@588: /// This function sets \c cutMap to the characteristic vector of a kpeter@588: /// minimum value cut: it will give a non-empty set \f$ X\subsetneq V \f$ kpeter@588: /// with minimal outgoing capacity (i.e. \c cutMap will be \c true exactly kpeter@588: /// for the nodes of \f$ X \f$). kpeter@588: /// kpeter@588: /// \param cutMap A \ref concepts::WriteMap "writable" node map with kpeter@588: /// \c bool (or convertible) value type. kpeter@588: /// kpeter@588: /// \return The value of the minimum cut. kpeter@588: /// alpar@761: /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() kpeter@588: /// must be called before using this function. kpeter@588: template kpeter@588: Value minCutMap(CutMap& cutMap) const { deba@409: for (NodeIt it(_graph); it != INVALID; ++it) { kpeter@588: 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