* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* 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_GOMORY_HU_TREE_H
#define LEMON_GOMORY_HU_TREE_H
#include <lemon/preflow.h>
#include <lemon/concept_check.h>
#include <lemon/concepts/maps.h>
/// \brief Gomory-Hu cut tree in graphs.
/// \brief Gomory-Hu cut tree algorithm
/// The Gomory-Hu tree is a tree on the nodeset of the digraph, but it
/// may contain arcs which are not in the original digraph. It helps
/// to calculate the minimum cut between all pairs of nodes, because
/// the minimum capacity arc on the tree path between two nodes has
/// the same weight as the minimum cut in the digraph between these
/// nodes. Moreover this arc separates the nodes to two parts which
/// determine this minimum cut.
/// The algorithm calculates \e n-1 distinict minimum cuts with
/// preflow algorithm, therefore the algorithm has
/// \f$(O(n^3\sqrt{e})\f$ overall time complexity. It calculates a
/// rooted Gomory-Hu tree, the structure of the tree and the weights
/// can be obtained with \c predNode() and \c predValue()
/// functions. The \c minCutValue() and \c minCutMap() calculates
/// the minimum cut and the minimum cut value between any two node
template <typename _Graph,
typename _Capacity = typename _Graph::template EdgeMap<int> >
/// The capacity on edges
typedef _Capacity Capacity;
/// The value type of capacities
typedef typename Capacity::Value Value;
TEMPLATE_GRAPH_TYPEDEFS(Graph);
const Capacity& _capacity;
typename Graph::template NodeMap<Node>* _pred;
typename Graph::template NodeMap<Value>* _weight;
typename Graph::template NodeMap<int>* _order;
void createStructures() {
_pred = new typename Graph::template NodeMap<Node>(_graph);
_weight = new typename Graph::template NodeMap<Value>(_graph);
_order = new typename Graph::template NodeMap<int>(_graph);
void destroyStructures() {
/// \param graph The graph type.
/// \param capacity The capacity map.
GomoryHuTree(const Graph& graph, const Capacity& capacity)
: _graph(graph), _capacity(capacity),
_pred(0), _weight(0), _order(0)
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
/// \brief Initializes the internal data structures.
/// Initializes the internal data structures.
for (NodeIt n(_graph); n != INVALID; ++n) {
_pred->set(_root, INVALID);
_weight->set(_root, std::numeric_limits<Value>::max());
/// \brief Starts the algorithm
/// Starts the algorithm.
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
for (NodeIt n(_graph); n != INVALID; ++n) {
if (n == _root) continue;
_weight->set(n, fa.flowValue());
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
_pred->set(n, (*_pred)[pn]);
_weight->set(n, (*_weight)[pn]);
_weight->set(pn, fa.flowValue());
for (NodeIt n(_graph); n != INVALID; ++n) {
while ((*_order)[nn] == -1) {
_order->set(st.back(), index++);
/// \brief Runs the Gomory-Hu algorithm.
/// Runs the Gomory-Hu algorithm.
/// \note gh.run() is just a shortcut of the following code.
/// \brief Returns the predecessor node in the Gomory-Hu tree.
/// Returns the predecessor node in the Gomory-Hu tree. If the node is
/// the root of the Gomory-Hu tree, then it returns \c INVALID.
Node predNode(const Node& node) {
/// \brief Returns the weight of the predecessor arc in the
/// Returns the weight of the predecessor arc in the Gomory-Hu
/// tree. If the node is the root of the Gomory-Hu tree, the
Value predValue(const Node& node) {
/// \brief Returns the minimum cut value between two nodes
/// Returns the minimum cut value between two nodes. The
/// algorithm finds the nearest common ancestor in the Gomory-Hu
/// tree and calculates the minimum weight arc on the paths to
Value minCutValue(const Node& s, const Node& t) const {
Value value = std::numeric_limits<Value>::max();
if ((*_order)[sn] < (*_order)[tn]) {
if ((*_weight)[tn] < value) value = (*_weight)[tn];
if ((*_weight)[sn] < value) value = (*_weight)[sn];
/// \brief Returns the minimum cut between two nodes
/// Returns the minimum cut value between two nodes. The
/// algorithm finds the nearest common ancestor in the Gomory-Hu
/// tree and calculates the minimum weight arc on the paths to
/// the ancestor. Then it sets all nodes to the cut determined by
/// this arc. The \c cutMap should be \ref concepts::ReadWriteMap
template <typename CutMap>
Value minCutMap(const Node& s, const Node& t, CutMap& cutMap) const {
Value value = std::numeric_limits<Value>::max();
if ((*_order)[sn] < (*_order)[tn]) {
if ((*_weight)[tn] < value) {
if ((*_weight)[sn] < value) {
typename Graph::template NodeMap<bool> reached(_graph, false);
reached.set(_root, true);
cutMap.set(_root, false);
for (NodeIt n(_graph); n != INVALID; ++n) {
cutMap.set(st.back(), cutMap[nn]);