/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* 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 class for testing network
/// \brief %Hao-Orlin algorithm to find a minimum cut in directed graphs.
/// Hao-Orlin calculates a minimum 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
/// out-degree) 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
/// out-degree). Obviously, the smaller of these two cuts will be a
/// minimum cut of \f$ D \f$. The algorithm is a modified
/// push-relabel preflow algorithm and 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 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.
/// \param GR The digraph class the algorithm runs on.
/// \param CAP An arc map of capacities which can be any numreric type.
/// The default type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
/// \param 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> >
typedef typename CapacityMap::Value Value;
TEMPLATE_GRAPH_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),
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) {
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) {
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 more control on the execution,
/// first you must call \ref init(), then the \ref calculateIn() or
/// \ref calculateOut() functions.
/// \brief Initializes the internal data structures.
/// Initializes the internal data structures. It creates
/// the maps, residual graph adaptors and some bucket structures
/// \brief Initializes the internal data structures.
/// Initializes the internal data structures. It creates
/// the maps, residual graph adaptor and some bucket structures
/// for the algorithm. Node \c source is used as 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 Calculates a minimum cut with \f$ source \f$ on the
/// 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 out-degree).
/// \brief Calculates a minimum cut with \f$ source \f$ on the
/// Calculates a minimum cut with \f$ source \f$ on the
/// target-side (i.e. a set \f$ X\subsetneq V \f$ with
/// \f$ source \in X \f$ and minimal out-degree).
/// \brief Runs the algorithm.
/// Runs the algorithm. It finds nodes \c source and \c target
/// arbitrarily and then calls \ref init(), \ref calculateOut()
/// and \ref calculateIn().
/// \brief Runs the algorithm.
/// Runs the algorithm. It uses the given \c source node, finds a
/// proper \c target 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.
/// Before using these functions, either \ref run(), \ref
/// calculateOut() or \ref calculateIn() must be called.
/// \brief Returns the value of the minimum value cut.
/// Returns the value of the minimum value cut.
Value minCutValue() const {
/// \brief Returns a minimum cut.
/// Sets \c nodeMap to the characteristic vector of a minimum
/// value cut: it will give a nonempty set \f$ X\subsetneq V \f$
/// with minimal out-degree (i.e. \c nodeMap will be true exactly
/// for the nodes of \f$ X \f$). \pre nodeMap should be a
/// bool-valued node-map.
template <typename NodeMap>
Value minCutMap(NodeMap& nodeMap) const {
for (NodeIt it(_graph); it != INVALID; ++it) {
nodeMap.set(it, (*_min_cut_map)[it]);
#endif //LEMON_HAO_ORLIN_H