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