Update INSTALL to use CMAKE. Also update AUTHORS and LICENSE
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_GOMORY_HU_TREE_H
20 #define LEMON_GOMORY_HU_TREE_H
24 #include <lemon/core.h>
25 #include <lemon/preflow.h>
26 #include <lemon/concept_check.h>
27 #include <lemon/concepts/maps.h>
31 /// \brief Gomory-Hu cut tree in graphs.
37 /// \brief Gomory-Hu cut tree algorithm
39 /// The Gomory-Hu tree is a tree on the node set of a given graph, but it
40 /// may contain edges which are not in the original graph. It has the
41 /// property that the minimum capacity edge of the path between two nodes
42 /// in this tree has the same weight as the minimum cut in the graph
43 /// between these nodes. Moreover the components obtained by removing
44 /// this edge from the tree determine the corresponding minimum cut.
45 /// Therefore once this tree is computed, the minimum cut between any pair
46 /// of nodes can easily be obtained.
48 /// The algorithm calculates \e n-1 distinct minimum cuts (currently with
49 /// the \ref Preflow algorithm), thus it has \f$O(n^3\sqrt{e})\f$ overall
50 /// time complexity. It calculates a rooted Gomory-Hu tree.
51 /// The structure of the tree and the edge weights can be
52 /// obtained using \c predNode(), \c predValue() and \c rootDist().
53 /// The functions \c minCutMap() and \c minCutValue() calculate
54 /// the minimum cut and the minimum cut value between any two nodes
55 /// in the graph. You can also list (iterate on) the nodes and the
56 /// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
58 /// \tparam GR The type of the undirected graph the algorithm runs on.
59 /// \tparam CAP The type of the edge map containing the capacities.
60 /// The default map type is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
62 template <typename GR,
65 template <typename GR,
66 typename CAP = typename GR::template EdgeMap<int> >
71 /// The graph type of the algorithm
73 /// The capacity map type of the algorithm
75 /// The value type of capacities
76 typedef typename Capacity::Value Value;
80 TEMPLATE_GRAPH_TYPEDEFS(Graph);
83 const Capacity& _capacity;
86 typename Graph::template NodeMap<Node>* _pred;
87 typename Graph::template NodeMap<Value>* _weight;
88 typename Graph::template NodeMap<int>* _order;
90 void createStructures() {
92 _pred = new typename Graph::template NodeMap<Node>(_graph);
95 _weight = new typename Graph::template NodeMap<Value>(_graph);
98 _order = new typename Graph::template NodeMap<int>(_graph);
102 void destroyStructures() {
116 /// \brief Constructor
119 /// \param graph The undirected graph the algorithm runs on.
120 /// \param capacity The edge capacity map.
121 GomoryHu(const Graph& graph, const Capacity& capacity)
122 : _graph(graph), _capacity(capacity),
123 _pred(0), _weight(0), _order(0)
125 checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
129 /// \brief Destructor
138 // Initialize the internal data structures
142 _root = NodeIt(_graph);
143 for (NodeIt n(_graph); n != INVALID; ++n) {
147 (*_pred)[_root] = INVALID;
148 (*_weight)[_root] = std::numeric_limits<Value>::max();
152 // Start the algorithm
154 Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
156 for (NodeIt n(_graph); n != INVALID; ++n) {
157 if (n == _root) continue;
159 Node pn = (*_pred)[n];
165 (*_weight)[n] = fa.flowValue();
167 for (NodeIt nn(_graph); nn != INVALID; ++nn) {
168 if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
172 if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
173 (*_pred)[n] = (*_pred)[pn];
175 (*_weight)[n] = (*_weight)[pn];
176 (*_weight)[pn] = fa.flowValue();
180 (*_order)[_root] = 0;
183 for (NodeIt n(_graph); n != INVALID; ++n) {
184 std::vector<Node> st;
186 while ((*_order)[nn] == -1) {
190 while (!st.empty()) {
191 (*_order)[st.back()] = index++;
199 ///\name Execution Control
203 /// \brief Run the Gomory-Hu algorithm.
205 /// This function runs the Gomory-Hu algorithm.
213 ///\name Query Functions
214 ///The results of the algorithm can be obtained using these
216 ///\ref run() should be called before using them.\n
217 ///See also \ref MinCutNodeIt and \ref MinCutEdgeIt.
221 /// \brief Return the predecessor node in the Gomory-Hu tree.
223 /// This function returns the predecessor node of the given node
224 /// in the Gomory-Hu tree.
225 /// If \c node is the root of the tree, then it returns \c INVALID.
227 /// \pre \ref run() must be called before using this function.
228 Node predNode(const Node& node) const {
229 return (*_pred)[node];
232 /// \brief Return the weight of the predecessor edge in the
235 /// This function returns the weight of the predecessor edge of the
236 /// given node in the Gomory-Hu tree.
237 /// If \c node is the root of the tree, the result is undefined.
239 /// \pre \ref run() must be called before using this function.
240 Value predValue(const Node& node) const {
241 return (*_weight)[node];
244 /// \brief Return the distance from the root node in the Gomory-Hu tree.
246 /// This function returns the distance of the given node from the root
247 /// node in the Gomory-Hu tree.
249 /// \pre \ref run() must be called before using this function.
250 int rootDist(const Node& node) const {
251 return (*_order)[node];
254 /// \brief Return the minimum cut value between two nodes
256 /// This function returns the minimum cut value between the nodes
258 /// It finds the nearest common ancestor of the given nodes in the
259 /// Gomory-Hu tree and calculates the minimum weight edge on the
260 /// paths to the ancestor.
262 /// \pre \ref run() must be called before using this function.
263 Value minCutValue(const Node& s, const Node& t) const {
265 Value value = std::numeric_limits<Value>::max();
268 if ((*_order)[sn] < (*_order)[tn]) {
269 if ((*_weight)[tn] <= value) value = (*_weight)[tn];
272 if ((*_weight)[sn] <= value) value = (*_weight)[sn];
279 /// \brief Return the minimum cut between two nodes
281 /// This function returns the minimum cut between the nodes \c s and \c t
282 /// in the \c cutMap parameter by setting the nodes in the component of
283 /// \c s to \c true and the other nodes to \c false.
285 /// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt.
287 /// \param s The base node.
288 /// \param t The node you want to separate from node \c s.
289 /// \param cutMap The cut will be returned in this map.
290 /// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap
291 /// "ReadWriteMap" on the graph nodes.
293 /// \return The value of the minimum cut between \c s and \c t.
295 /// \pre \ref run() must be called before using this function.
296 template <typename CutMap>
297 Value minCutMap(const Node& s,
304 Value value = std::numeric_limits<Value>::max();
307 if ((*_order)[sn] < (*_order)[tn]) {
308 if ((*_weight)[tn] <= value) {
311 value = (*_weight)[tn];
315 if ((*_weight)[sn] <= value) {
318 value = (*_weight)[sn];
324 typename Graph::template NodeMap<bool> reached(_graph, false);
325 reached[_root] = true;
326 cutMap.set(_root, !s_root);
328 cutMap.set(rn, s_root);
330 std::vector<Node> st;
331 for (NodeIt n(_graph); n != INVALID; ++n) {
334 while (!reached[nn]) {
338 while (!st.empty()) {
339 cutMap.set(st.back(), cutMap[nn]);
349 friend class MinCutNodeIt;
351 /// Iterate on the nodes of a minimum cut
353 /// This iterator class lists the nodes of a minimum cut found by
354 /// GomoryHu. Before using it, you must allocate a GomoryHu class
355 /// and call its \ref GomoryHu::run() "run()" method.
357 /// This example counts the nodes in the minimum cut separating \c s from
360 /// GomoryHu<Graph> gom(g, capacities);
363 /// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
368 typename Graph::NodeIt _node_it;
369 typename Graph::template NodeMap<bool> _cut;
375 MinCutNodeIt(GomoryHu const &gomory,
376 ///< The GomoryHu class. You must call its
378 /// before initializing this iterator.
379 const Node& s, ///< The base node.
381 ///< The node you want to separate from node \c s.
383 ///< If it is \c true (default) then the iterator lists
384 /// the nodes of the component containing \c s,
385 /// otherwise it lists the other component.
386 /// \note As the minimum cut is not always unique,
388 /// MinCutNodeIt(gomory, s, t, true);
392 /// MinCutNodeIt(gomory, t, s, false);
394 /// does not necessarily give the same set of nodes.
395 /// However, it is ensured that
397 /// MinCutNodeIt(gomory, s, t, true);
401 /// MinCutNodeIt(gomory, s, t, false);
403 /// together list each node exactly once.
405 : _side(side), _cut(gomory._graph)
407 gomory.minCutMap(s,t,_cut);
408 for(_node_it=typename Graph::NodeIt(gomory._graph);
409 _node_it!=INVALID && _cut[_node_it]!=_side;
412 /// Conversion to \c Node
414 /// Conversion to \c Node.
416 operator typename Graph::Node() const
420 bool operator==(Invalid) { return _node_it==INVALID; }
421 bool operator!=(Invalid) { return _node_it!=INVALID; }
426 MinCutNodeIt &operator++()
428 for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
431 /// Postfix incrementation
433 /// Postfix incrementation.
435 /// \warning This incrementation
436 /// returns a \c Node, not a \c MinCutNodeIt, as one may
438 typename Graph::Node operator++(int)
440 typename Graph::Node n=*this;
446 friend class MinCutEdgeIt;
448 /// Iterate on the edges of a minimum cut
450 /// This iterator class lists the edges of a minimum cut found by
451 /// GomoryHu. Before using it, you must allocate a GomoryHu class
452 /// and call its \ref GomoryHu::run() "run()" method.
454 /// This example computes the value of the minimum cut separating \c s from
457 /// GomoryHu<Graph> gom(g, capacities);
460 /// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
461 /// value+=capacities[e];
463 /// The result will be the same as the value returned by
464 /// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)".
469 typename Graph::NodeIt _node_it;
470 typename Graph::OutArcIt _arc_it;
471 typename Graph::template NodeMap<bool> _cut;
475 while(_node_it!=INVALID && _arc_it==INVALID)
477 for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
478 if(_node_it!=INVALID)
479 _arc_it=typename Graph::OutArcIt(_graph,_node_it);
488 MinCutEdgeIt(GomoryHu const &gomory,
489 ///< The GomoryHu class. You must call its
491 /// before initializing this iterator.
492 const Node& s, ///< The base node.
494 ///< The node you want to separate from node \c s.
496 ///< If it is \c true (default) then the listed arcs
497 /// will be oriented from the
498 /// nodes of the component containing \c s,
499 /// otherwise they will be oriented in the opposite
502 : _graph(gomory._graph), _cut(_graph)
504 gomory.minCutMap(s,t,_cut);
506 for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
509 for(_node_it=typename Graph::NodeIt(_graph);
510 _node_it!=INVALID && !_cut[_node_it];
512 _arc_it = _node_it!=INVALID ?
513 typename Graph::OutArcIt(_graph,_node_it) : INVALID;
514 while(_node_it!=INVALID && _arc_it == INVALID)
516 for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
517 if(_node_it!=INVALID)
518 _arc_it= typename Graph::OutArcIt(_graph,_node_it);
520 while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
522 /// Conversion to \c Arc
524 /// Conversion to \c Arc.
526 operator typename Graph::Arc() const
530 /// Conversion to \c Edge
532 /// Conversion to \c Edge.
534 operator typename Graph::Edge() const
538 bool operator==(Invalid) { return _node_it==INVALID; }
539 bool operator!=(Invalid) { return _node_it!=INVALID; }
544 MinCutEdgeIt &operator++()
547 while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
550 /// Postfix incrementation
552 /// Postfix incrementation.
554 /// \warning This incrementation
555 /// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
556 typename Graph::Arc operator++(int)
558 typename Graph::Arc e=*this;