[Lemon-commits] Alpar Juttner: Merge
Lemon HG
hg at lemon.cs.elte.hu
Wed Mar 4 16:35:20 CET 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/d59dcc933e59
changeset: 569:d59dcc933e59
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Wed Mar 04 14:09:45 2009 +0000
description:
Merge
diffstat:
5 files changed, 647 insertions(+)
lemon/Makefile.am | 1
lemon/gomory_hu.h | 551 ++++++++++++++++++++++++++++++++++++++++++++++++
test/CMakeLists.txt | 1
test/Makefile.am | 2
test/gomory_hu_test.cc | 92 ++++++++
diffs (truncated from 694 to 300 lines):
diff --git a/lemon/Makefile.am b/lemon/Makefile.am
--- a/lemon/Makefile.am
+++ b/lemon/Makefile.am
@@ -68,6 +68,7 @@
lemon/euler.h \
lemon/full_graph.h \
lemon/glpk.h \
+ lemon/gomory_hu.h \
lemon/graph_to_eps.h \
lemon/grid_graph.h \
lemon/hypercube_graph.h \
diff --git a/lemon/gomory_hu.h b/lemon/gomory_hu.h
new file mode 100644
--- /dev/null
+++ b/lemon/gomory_hu.h
@@ -0,0 +1,551 @@
+/* -*- C++ -*-
+ *
+ * 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
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_GOMORY_HU_TREE_H
+#define LEMON_GOMORY_HU_TREE_H
+
+#include <limits>
+
+#include <lemon/core.h>
+#include <lemon/preflow.h>
+#include <lemon/concept_check.h>
+#include <lemon/concepts/maps.h>
+
+/// \ingroup min_cut
+/// \file
+/// \brief Gomory-Hu cut tree in graphs.
+
+namespace lemon {
+
+ /// \ingroup min_cut
+ ///
+ /// \brief Gomory-Hu cut tree algorithm
+ ///
+ /// The Gomory-Hu tree is a tree on the node set of a given graph, but it
+ /// may contain edges which are not in the original graph. It has the
+ /// property that the minimum capacity edge of the path between two nodes
+ /// in this tree has the same weight as the minimum cut in the graph
+ /// between these nodes. Moreover the components obtained by removing
+ /// this edge from the tree determine the corresponding minimum cut.
+ ///
+ /// Therefore once this tree is computed, the minimum cut between any pair
+ /// of nodes can easily be obtained.
+ ///
+ /// The algorithm calculates \e n-1 distinct minimum cuts (currently with
+ /// the \ref Preflow algorithm), therefore the algorithm has
+ /// \f$(O(n^3\sqrt{e})\f$ overall time complexity. It calculates a
+ /// rooted Gomory-Hu tree, its structure and the weights can be obtained
+ /// by \c predNode(), \c predValue() and \c rootDist().
+ ///
+ /// The members \c minCutMap() and \c minCutValue() calculate
+ /// the minimum cut and the minimum cut value between any two nodes
+ /// in the graph. You can also list (iterate on) the nodes and the
+ /// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
+ ///
+ /// \tparam GR The type of the undirected graph the algorithm runs on.
+ /// \tparam CAP The type of the edge map describing the edge capacities.
+ /// It is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>" by default.
+#ifdef DOXYGEN
+ template <typename GR,
+ typename CAP>
+#else
+ template <typename GR,
+ typename CAP = typename GR::template EdgeMap<int> >
+#endif
+ class GomoryHu {
+ public:
+
+ /// The graph type
+ typedef GR Graph;
+ /// The type of the edge capacity map
+ typedef CAP Capacity;
+ /// The value type of capacities
+ typedef typename Capacity::Value Value;
+
+ private:
+
+ TEMPLATE_GRAPH_TYPEDEFS(Graph);
+
+ const Graph& _graph;
+ const Capacity& _capacity;
+
+ Node _root;
+ typename Graph::template NodeMap<Node>* _pred;
+ typename Graph::template NodeMap<Value>* _weight;
+ typename Graph::template NodeMap<int>* _order;
+
+ void createStructures() {
+ if (!_pred) {
+ _pred = new typename Graph::template NodeMap<Node>(_graph);
+ }
+ if (!_weight) {
+ _weight = new typename Graph::template NodeMap<Value>(_graph);
+ }
+ if (!_order) {
+ _order = new typename Graph::template NodeMap<int>(_graph);
+ }
+ }
+
+ void destroyStructures() {
+ if (_pred) {
+ delete _pred;
+ }
+ if (_weight) {
+ delete _weight;
+ }
+ if (_order) {
+ delete _order;
+ }
+ }
+
+ public:
+
+ /// \brief Constructor
+ ///
+ /// Constructor
+ /// \param graph The undirected graph the algorithm runs on.
+ /// \param capacity The edge capacity map.
+ GomoryHu(const Graph& graph, const Capacity& capacity)
+ : _graph(graph), _capacity(capacity),
+ _pred(0), _weight(0), _order(0)
+ {
+ checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
+ }
+
+
+ /// \brief Destructor
+ ///
+ /// Destructor
+ ~GomoryHu() {
+ destroyStructures();
+ }
+
+ private:
+
+ // Initialize the internal data structures
+ void init() {
+ createStructures();
+
+ _root = NodeIt(_graph);
+ for (NodeIt n(_graph); n != INVALID; ++n) {
+ _pred->set(n, _root);
+ _order->set(n, -1);
+ }
+ _pred->set(_root, INVALID);
+ _weight->set(_root, std::numeric_limits<Value>::max());
+ }
+
+
+ // Start the algorithm
+ void start() {
+ Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
+
+ for (NodeIt n(_graph); n != INVALID; ++n) {
+ if (n == _root) continue;
+
+ Node pn = (*_pred)[n];
+ fa.source(n);
+ fa.target(pn);
+
+ fa.runMinCut();
+
+ _weight->set(n, fa.flowValue());
+
+ for (NodeIt nn(_graph); nn != INVALID; ++nn) {
+ if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
+ _pred->set(nn, n);
+ }
+ }
+ if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
+ _pred->set(n, (*_pred)[pn]);
+ _pred->set(pn, n);
+ _weight->set(n, (*_weight)[pn]);
+ _weight->set(pn, fa.flowValue());
+ }
+ }
+
+ _order->set(_root, 0);
+ int index = 1;
+
+ for (NodeIt n(_graph); n != INVALID; ++n) {
+ std::vector<Node> st;
+ Node nn = n;
+ while ((*_order)[nn] == -1) {
+ st.push_back(nn);
+ nn = (*_pred)[nn];
+ }
+ while (!st.empty()) {
+ _order->set(st.back(), index++);
+ st.pop_back();
+ }
+ }
+ }
+
+ public:
+
+ ///\name Execution Control
+
+ ///@{
+
+ /// \brief Run the Gomory-Hu algorithm.
+ ///
+ /// This function runs the Gomory-Hu algorithm.
+ void run() {
+ init();
+ start();
+ }
+
+ /// @}
+
+ ///\name Query Functions
+ ///The results of the algorithm can be obtained using these
+ ///functions.\n
+ ///\ref run() "run()" should be called before using them.\n
+ ///See also \ref MinCutNodeIt and \ref MinCutEdgeIt.
+
+ ///@{
+
+ /// \brief Return the predecessor node in the Gomory-Hu tree.
+ ///
+ /// This function 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) {
+ return (*_pred)[node];
+ }
+
+ /// \brief Return the distance from the root node in the Gomory-Hu tree.
+ ///
+ /// This function returns the distance of \c node from the root node
+ /// in the Gomory-Hu tree.
+ int rootDist(const Node& node) {
+ return (*_order)[node];
+ }
+
+ /// \brief Return the weight of the predecessor edge in the
+ /// Gomory-Hu tree.
+ ///
+ /// This function returns the weight of the predecessor edge in the
+ /// Gomory-Hu tree. If the node is the root, the result is undefined.
+ Value predValue(const Node& node) {
+ return (*_weight)[node];
+ }
+
+ /// \brief Return the minimum cut value between two nodes
+ ///
+ /// This function 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 edge on the paths to
+ /// the ancestor.
+ Value minCutValue(const Node& s, const Node& t) const {
+ Node sn = s, tn = t;
+ Value value = std::numeric_limits<Value>::max();
+
+ while (sn != tn) {
+ if ((*_order)[sn] < (*_order)[tn]) {
+ if ((*_weight)[tn] <= value) value = (*_weight)[tn];
+ tn = (*_pred)[tn];
+ } else {
+ if ((*_weight)[sn] <= value) value = (*_weight)[sn];
+ sn = (*_pred)[sn];
+ }
+ }
+ return value;
+ }
+
+ /// \brief Return the minimum cut between two nodes
+ ///
+ /// This function returns the minimum cut between the nodes \c s and \c t
+ /// in the \c cutMap parameter by setting the nodes in the component of
+ /// \c s to \c true and the other nodes to \c false.
+ ///
+ /// For higher level interfaces, see MinCutNodeIt and MinCutEdgeIt.
+ template <typename CutMap>
+ Value minCutMap(const Node& s, ///< The base node.
+ const Node& t,
+ ///< The node you want to separate from node \c s.
+ CutMap& cutMap
+ ///< The cut will be returned in this map.
+ /// It must be a \c bool (or convertible)
More information about the Lemon-commits
mailing list