tapolcai@531: /* -*- C++ -*- tapolcai@531: * tapolcai@531: * This file is a part of LEMON, a generic C++ optimization library tapolcai@531: * tapolcai@531: * Copyright (C) 2003-2008 tapolcai@531: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport tapolcai@531: * (Egervary Research Group on Combinatorial Optimization, EGRES). tapolcai@531: * tapolcai@531: * Permission to use, modify and distribute this software is granted tapolcai@531: * provided that this copyright notice appears in all copies. For tapolcai@531: * precise terms see the accompanying LICENSE file. tapolcai@531: * tapolcai@531: * This software is provided "AS IS" with no warranty of any kind, tapolcai@531: * express or implied, and with no claim as to its suitability for any tapolcai@531: * purpose. tapolcai@531: * tapolcai@531: */ tapolcai@531: tapolcai@531: #ifndef LEMON_GOMORY_HU_TREE_H tapolcai@531: #define LEMON_GOMORY_HU_TREE_H tapolcai@531: tapolcai@531: #include tapolcai@531: alpar@532: #include tapolcai@531: #include tapolcai@531: #include tapolcai@531: #include tapolcai@531: tapolcai@531: /// \ingroup min_cut tapolcai@531: /// \file tapolcai@531: /// \brief Gomory-Hu cut tree in graphs. tapolcai@531: tapolcai@531: namespace lemon { tapolcai@531: tapolcai@531: /// \ingroup min_cut tapolcai@531: /// tapolcai@531: /// \brief Gomory-Hu cut tree algorithm tapolcai@531: /// kpeter@534: /// The Gomory-Hu tree is a tree on the node set of a given graph, but it kpeter@534: /// may contain edges which are not in the original graph. It has the alpar@532: /// property that the minimum capacity edge of the path between two nodes kpeter@534: /// in this tree has the same weight as the minimum cut in the graph alpar@532: /// between these nodes. Moreover the components obtained by removing alpar@532: /// this edge from the tree determine the corresponding minimum cut. alpar@532: /// alpar@532: /// Therefore once this tree is computed, the minimum cut between any pair alpar@532: /// of nodes can easily be obtained. tapolcai@531: /// alpar@532: /// The algorithm calculates \e n-1 distinct minimum cuts (currently with alpar@532: /// the \ref Preflow algorithm), therefore the algorithm has tapolcai@531: /// \f$(O(n^3\sqrt{e})\f$ overall time complexity. It calculates a alpar@532: /// rooted Gomory-Hu tree, its structure and the weights can be obtained alpar@532: /// by \c predNode(), \c predValue() and \c rootDist(). alpar@532: /// alpar@532: /// The members \c minCutMap() and \c minCutValue() calculate kpeter@534: /// the minimum cut and the minimum cut value between any two nodes kpeter@534: /// in the graph. You can also list (iterate on) the nodes and the kpeter@534: /// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt. alpar@532: /// kpeter@534: /// \tparam GR The type of the undirected graph the algorithm runs on. kpeter@534: /// \tparam CAP The type of the edge map describing the edge capacities. kpeter@534: /// It is \ref concepts::Graph::EdgeMap "GR::EdgeMap" by default. kpeter@534: #ifdef DOXYGEN alpar@532: template kpeter@534: #else kpeter@534: template > kpeter@534: #endif alpar@533: class GomoryHu { tapolcai@531: public: tapolcai@531: tapolcai@531: /// The graph type alpar@532: typedef GR Graph; kpeter@534: /// The type of the edge capacity map alpar@532: typedef CAP Capacity; tapolcai@531: /// The value type of capacities tapolcai@531: typedef typename Capacity::Value Value; tapolcai@531: tapolcai@531: private: tapolcai@531: tapolcai@531: TEMPLATE_GRAPH_TYPEDEFS(Graph); tapolcai@531: tapolcai@531: const Graph& _graph; tapolcai@531: const Capacity& _capacity; tapolcai@531: tapolcai@531: Node _root; tapolcai@531: typename Graph::template NodeMap* _pred; tapolcai@531: typename Graph::template NodeMap* _weight; tapolcai@531: typename Graph::template NodeMap* _order; tapolcai@531: tapolcai@531: void createStructures() { tapolcai@531: if (!_pred) { tapolcai@531: _pred = new typename Graph::template NodeMap(_graph); tapolcai@531: } tapolcai@531: if (!_weight) { tapolcai@531: _weight = new typename Graph::template NodeMap(_graph); tapolcai@531: } tapolcai@531: if (!_order) { tapolcai@531: _order = new typename Graph::template NodeMap(_graph); tapolcai@531: } tapolcai@531: } tapolcai@531: tapolcai@531: void destroyStructures() { tapolcai@531: if (_pred) { tapolcai@531: delete _pred; tapolcai@531: } tapolcai@531: if (_weight) { tapolcai@531: delete _weight; tapolcai@531: } tapolcai@531: if (_order) { tapolcai@531: delete _order; tapolcai@531: } tapolcai@531: } tapolcai@531: tapolcai@531: public: tapolcai@531: tapolcai@531: /// \brief Constructor tapolcai@531: /// tapolcai@531: /// Constructor kpeter@534: /// \param graph The undirected graph the algorithm runs on. kpeter@534: /// \param capacity The edge capacity map. alpar@533: GomoryHu(const Graph& graph, const Capacity& capacity) tapolcai@531: : _graph(graph), _capacity(capacity), tapolcai@531: _pred(0), _weight(0), _order(0) tapolcai@531: { tapolcai@531: checkConcept, Capacity>(); tapolcai@531: } tapolcai@531: tapolcai@531: tapolcai@531: /// \brief Destructor tapolcai@531: /// tapolcai@531: /// Destructor alpar@533: ~GomoryHu() { tapolcai@531: destroyStructures(); tapolcai@531: } tapolcai@531: kpeter@534: private: kpeter@534: kpeter@534: // Initialize the internal data structures tapolcai@531: void init() { tapolcai@531: createStructures(); tapolcai@531: tapolcai@531: _root = NodeIt(_graph); tapolcai@531: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@573: (*_pred)[n] = _root; kpeter@573: (*_order)[n] = -1; tapolcai@531: } kpeter@573: (*_pred)[_root] = INVALID; kpeter@573: (*_weight)[_root] = std::numeric_limits::max(); tapolcai@531: } tapolcai@531: tapolcai@531: kpeter@534: // Start the algorithm tapolcai@531: void start() { tapolcai@531: Preflow fa(_graph, _capacity, _root, INVALID); tapolcai@531: tapolcai@531: for (NodeIt n(_graph); n != INVALID; ++n) { tapolcai@531: if (n == _root) continue; tapolcai@531: tapolcai@531: Node pn = (*_pred)[n]; tapolcai@531: fa.source(n); tapolcai@531: fa.target(pn); tapolcai@531: tapolcai@531: fa.runMinCut(); tapolcai@531: kpeter@573: (*_weight)[n] = fa.flowValue(); tapolcai@531: tapolcai@531: for (NodeIt nn(_graph); nn != INVALID; ++nn) { tapolcai@531: if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) { kpeter@573: (*_pred)[nn] = n; tapolcai@531: } tapolcai@531: } tapolcai@531: if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) { kpeter@573: (*_pred)[n] = (*_pred)[pn]; kpeter@573: (*_pred)[pn] = n; kpeter@573: (*_weight)[n] = (*_weight)[pn]; kpeter@573: (*_weight)[pn] = fa.flowValue(); tapolcai@531: } tapolcai@531: } tapolcai@531: kpeter@573: (*_order)[_root] = 0; tapolcai@531: int index = 1; tapolcai@531: tapolcai@531: for (NodeIt n(_graph); n != INVALID; ++n) { tapolcai@531: std::vector st; tapolcai@531: Node nn = n; tapolcai@531: while ((*_order)[nn] == -1) { tapolcai@531: st.push_back(nn); tapolcai@531: nn = (*_pred)[nn]; tapolcai@531: } tapolcai@531: while (!st.empty()) { kpeter@573: (*_order)[st.back()] = index++; tapolcai@531: st.pop_back(); tapolcai@531: } tapolcai@531: } tapolcai@531: } tapolcai@531: kpeter@534: public: kpeter@534: alpar@532: ///\name Execution Control alpar@532: alpar@532: ///@{ alpar@532: alpar@532: /// \brief Run the Gomory-Hu algorithm. tapolcai@531: /// alpar@532: /// This function runs the Gomory-Hu algorithm. tapolcai@531: void run() { tapolcai@531: init(); tapolcai@531: start(); tapolcai@531: } alpar@532: alpar@532: /// @} tapolcai@531: alpar@532: ///\name Query Functions alpar@532: ///The results of the algorithm can be obtained using these alpar@532: ///functions.\n kpeter@534: ///\ref run() "run()" should be called before using them.\n kpeter@534: ///See also \ref MinCutNodeIt and \ref MinCutEdgeIt. alpar@532: alpar@532: ///@{ alpar@532: alpar@532: /// \brief Return the predecessor node in the Gomory-Hu tree. tapolcai@531: /// alpar@532: /// This function returns the predecessor node in the Gomory-Hu tree. alpar@532: /// If the node is tapolcai@531: /// the root of the Gomory-Hu tree, then it returns \c INVALID. tapolcai@531: Node predNode(const Node& node) { tapolcai@531: return (*_pred)[node]; tapolcai@531: } tapolcai@531: alpar@532: /// \brief Return the distance from the root node in the Gomory-Hu tree. alpar@532: /// alpar@532: /// This function returns the distance of \c node from the root node alpar@532: /// in the Gomory-Hu tree. alpar@532: int rootDist(const Node& node) { alpar@532: return (*_order)[node]; alpar@532: } alpar@532: alpar@532: /// \brief Return the weight of the predecessor edge in the tapolcai@531: /// Gomory-Hu tree. tapolcai@531: /// alpar@532: /// This function returns the weight of the predecessor edge in the alpar@532: /// Gomory-Hu tree. If the node is the root, the result is undefined. tapolcai@531: Value predValue(const Node& node) { tapolcai@531: return (*_weight)[node]; tapolcai@531: } tapolcai@531: alpar@532: /// \brief Return the minimum cut value between two nodes tapolcai@531: /// alpar@532: /// This function returns the minimum cut value between two nodes. The tapolcai@531: /// algorithm finds the nearest common ancestor in the Gomory-Hu kpeter@534: /// tree and calculates the minimum weight edge on the paths to tapolcai@531: /// the ancestor. tapolcai@531: Value minCutValue(const Node& s, const Node& t) const { tapolcai@531: Node sn = s, tn = t; tapolcai@531: Value value = std::numeric_limits::max(); tapolcai@531: tapolcai@531: while (sn != tn) { tapolcai@531: if ((*_order)[sn] < (*_order)[tn]) { alpar@532: if ((*_weight)[tn] <= value) value = (*_weight)[tn]; tapolcai@531: tn = (*_pred)[tn]; tapolcai@531: } else { alpar@532: if ((*_weight)[sn] <= value) value = (*_weight)[sn]; tapolcai@531: sn = (*_pred)[sn]; tapolcai@531: } tapolcai@531: } tapolcai@531: return value; tapolcai@531: } tapolcai@531: alpar@532: /// \brief Return the minimum cut between two nodes tapolcai@531: /// alpar@532: /// This function returns the minimum cut between the nodes \c s and \c t kpeter@534: /// in the \c cutMap parameter by setting the nodes in the component of kpeter@534: /// \c s to \c true and the other nodes to \c false. alpar@532: /// kpeter@534: /// For higher level interfaces, see MinCutNodeIt and MinCutEdgeIt. tapolcai@531: template kpeter@534: Value minCutMap(const Node& s, ///< The base node. alpar@532: const Node& t, kpeter@534: ///< The node you want to separate from node \c s. alpar@532: CutMap& cutMap kpeter@534: ///< The cut will be returned in this map. kpeter@534: /// It must be a \c bool (or convertible) kpeter@534: /// \ref concepts::ReadWriteMap "ReadWriteMap" kpeter@534: /// on the graph nodes. alpar@532: ) const { tapolcai@531: Node sn = s, tn = t; alpar@532: bool s_root=false; tapolcai@531: Node rn = INVALID; tapolcai@531: Value value = std::numeric_limits::max(); tapolcai@531: tapolcai@531: while (sn != tn) { tapolcai@531: if ((*_order)[sn] < (*_order)[tn]) { alpar@532: if ((*_weight)[tn] <= value) { tapolcai@531: rn = tn; alpar@532: s_root = false; tapolcai@531: value = (*_weight)[tn]; tapolcai@531: } tapolcai@531: tn = (*_pred)[tn]; tapolcai@531: } else { alpar@532: if ((*_weight)[sn] <= value) { tapolcai@531: rn = sn; alpar@532: s_root = true; tapolcai@531: value = (*_weight)[sn]; tapolcai@531: } tapolcai@531: sn = (*_pred)[sn]; tapolcai@531: } tapolcai@531: } tapolcai@531: tapolcai@531: typename Graph::template NodeMap reached(_graph, false); kpeter@573: reached[_root] = true; alpar@532: cutMap.set(_root, !s_root); kpeter@573: reached[rn] = true; alpar@532: cutMap.set(rn, s_root); tapolcai@531: alpar@532: std::vector st; tapolcai@531: for (NodeIt n(_graph); n != INVALID; ++n) { alpar@532: st.clear(); alpar@532: Node nn = n; tapolcai@531: while (!reached[nn]) { tapolcai@531: st.push_back(nn); tapolcai@531: nn = (*_pred)[nn]; tapolcai@531: } tapolcai@531: while (!st.empty()) { tapolcai@531: cutMap.set(st.back(), cutMap[nn]); tapolcai@531: st.pop_back(); tapolcai@531: } tapolcai@531: } tapolcai@531: tapolcai@531: return value; tapolcai@531: } tapolcai@531: alpar@532: ///@} alpar@532: alpar@532: friend class MinCutNodeIt; alpar@532: alpar@532: /// Iterate on the nodes of a minimum cut alpar@532: alpar@532: /// This iterator class lists the nodes of a minimum cut found by alpar@533: /// GomoryHu. Before using it, you must allocate a GomoryHu class, alpar@533: /// and call its \ref GomoryHu::run() "run()" method. alpar@532: /// alpar@532: /// This example counts the nodes in the minimum cut separating \c s from alpar@532: /// \c t. alpar@532: /// \code alpar@533: /// GomoruHu gom(g, capacities); alpar@532: /// gom.run(); kpeter@534: /// int cnt=0; kpeter@534: /// for(GomoruHu::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt; alpar@532: /// \endcode alpar@532: class MinCutNodeIt alpar@532: { alpar@532: bool _side; alpar@532: typename Graph::NodeIt _node_it; alpar@532: typename Graph::template NodeMap _cut; alpar@532: public: alpar@532: /// Constructor alpar@532: kpeter@534: /// Constructor. alpar@532: /// alpar@533: MinCutNodeIt(GomoryHu const &gomory, alpar@533: ///< The GomoryHu class. You must call its alpar@532: /// run() method kpeter@534: /// before initializing this iterator. kpeter@534: const Node& s, ///< The base node. alpar@532: const Node& t, kpeter@534: ///< The node you want to separate from node \c s. alpar@532: bool side=true alpar@532: ///< If it is \c true (default) then the iterator lists alpar@532: /// the nodes of the component containing \c s, alpar@532: /// otherwise it lists the other component. alpar@532: /// \note As the minimum cut is not always unique, alpar@532: /// \code alpar@532: /// MinCutNodeIt(gomory, s, t, true); alpar@532: /// \endcode alpar@532: /// and alpar@532: /// \code alpar@532: /// MinCutNodeIt(gomory, t, s, false); alpar@532: /// \endcode alpar@532: /// does not necessarily give the same set of nodes. alpar@532: /// However it is ensured that alpar@532: /// \code alpar@532: /// MinCutNodeIt(gomory, s, t, true); alpar@532: /// \endcode alpar@532: /// and alpar@532: /// \code alpar@532: /// MinCutNodeIt(gomory, s, t, false); alpar@532: /// \endcode alpar@532: /// together list each node exactly once. alpar@532: ) alpar@532: : _side(side), _cut(gomory._graph) alpar@532: { alpar@532: gomory.minCutMap(s,t,_cut); alpar@532: for(_node_it=typename Graph::NodeIt(gomory._graph); alpar@532: _node_it!=INVALID && _cut[_node_it]!=_side; alpar@532: ++_node_it) {} alpar@532: } kpeter@534: /// Conversion to \c Node alpar@532: kpeter@534: /// Conversion to \c Node. alpar@532: /// alpar@532: operator typename Graph::Node() const alpar@532: { alpar@532: return _node_it; alpar@532: } alpar@532: bool operator==(Invalid) { return _node_it==INVALID; } alpar@532: bool operator!=(Invalid) { return _node_it!=INVALID; } alpar@532: /// Next node alpar@532: kpeter@534: /// Next node. alpar@532: /// alpar@532: MinCutNodeIt &operator++() alpar@532: { alpar@532: for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {} alpar@532: return *this; alpar@532: } alpar@532: /// Postfix incrementation alpar@532: kpeter@534: /// Postfix incrementation. alpar@532: /// alpar@532: /// \warning This incrementation kpeter@534: /// returns a \c Node, not a \c MinCutNodeIt, as one may alpar@532: /// expect. alpar@532: typename Graph::Node operator++(int) alpar@532: { alpar@532: typename Graph::Node n=*this; alpar@532: ++(*this); alpar@532: return n; alpar@532: } alpar@532: }; alpar@532: alpar@532: friend class MinCutEdgeIt; alpar@532: alpar@532: /// Iterate on the edges of a minimum cut alpar@532: alpar@532: /// This iterator class lists the edges of a minimum cut found by alpar@533: /// GomoryHu. Before using it, you must allocate a GomoryHu class, alpar@533: /// and call its \ref GomoryHu::run() "run()" method. alpar@532: /// alpar@532: /// This example computes the value of the minimum cut separating \c s from alpar@532: /// \c t. alpar@532: /// \code alpar@533: /// GomoruHu gom(g, capacities); alpar@532: /// gom.run(); alpar@532: /// int value=0; kpeter@534: /// for(GomoruHu::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e) alpar@532: /// value+=capacities[e]; alpar@532: /// \endcode alpar@532: /// the result will be the same as it is returned by kpeter@534: /// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)" alpar@532: class MinCutEdgeIt alpar@532: { alpar@532: bool _side; alpar@532: const Graph &_graph; alpar@532: typename Graph::NodeIt _node_it; alpar@532: typename Graph::OutArcIt _arc_it; alpar@532: typename Graph::template NodeMap _cut; alpar@532: void step() alpar@532: { alpar@532: ++_arc_it; alpar@532: while(_node_it!=INVALID && _arc_it==INVALID) alpar@532: { alpar@532: for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {} alpar@532: if(_node_it!=INVALID) alpar@532: _arc_it=typename Graph::OutArcIt(_graph,_node_it); alpar@532: } alpar@532: } alpar@532: alpar@532: public: alpar@533: MinCutEdgeIt(GomoryHu const &gomory, alpar@533: ///< The GomoryHu class. You must call its alpar@532: /// run() method kpeter@534: /// before initializing this iterator. kpeter@534: const Node& s, ///< The base node. alpar@532: const Node& t, kpeter@534: ///< The node you want to separate from node \c s. alpar@532: bool side=true alpar@532: ///< If it is \c true (default) then the listed arcs alpar@532: /// will be oriented from the alpar@532: /// the nodes of the component containing \c s, alpar@532: /// otherwise they will be oriented in the opposite alpar@532: /// direction. alpar@532: ) alpar@532: : _graph(gomory._graph), _cut(_graph) alpar@532: { alpar@532: gomory.minCutMap(s,t,_cut); alpar@532: if(!side) alpar@532: for(typename Graph::NodeIt n(_graph);n!=INVALID;++n) alpar@532: _cut[n]=!_cut[n]; alpar@532: alpar@532: for(_node_it=typename Graph::NodeIt(_graph); alpar@532: _node_it!=INVALID && !_cut[_node_it]; alpar@532: ++_node_it) {} alpar@532: _arc_it = _node_it!=INVALID ? alpar@532: typename Graph::OutArcIt(_graph,_node_it) : INVALID; alpar@532: while(_node_it!=INVALID && _arc_it == INVALID) alpar@532: { alpar@532: for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {} alpar@532: if(_node_it!=INVALID) alpar@532: _arc_it= typename Graph::OutArcIt(_graph,_node_it); alpar@532: } alpar@532: while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); alpar@532: } kpeter@534: /// Conversion to \c Arc alpar@532: kpeter@534: /// Conversion to \c Arc. alpar@532: /// alpar@532: operator typename Graph::Arc() const alpar@532: { alpar@532: return _arc_it; alpar@532: } kpeter@534: /// Conversion to \c Edge alpar@532: kpeter@534: /// Conversion to \c Edge. alpar@532: /// alpar@532: operator typename Graph::Edge() const alpar@532: { alpar@532: return _arc_it; alpar@532: } alpar@532: bool operator==(Invalid) { return _node_it==INVALID; } alpar@532: bool operator!=(Invalid) { return _node_it!=INVALID; } alpar@532: /// Next edge alpar@532: kpeter@534: /// Next edge. alpar@532: /// alpar@532: MinCutEdgeIt &operator++() alpar@532: { alpar@532: step(); alpar@532: while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); alpar@532: return *this; alpar@532: } alpar@532: /// Postfix incrementation alpar@532: kpeter@534: /// Postfix incrementation. alpar@532: /// alpar@532: /// \warning This incrementation kpeter@534: /// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect. alpar@532: typename Graph::Arc operator++(int) alpar@532: { alpar@532: typename Graph::Arc e=*this; alpar@532: ++(*this); alpar@532: return e; alpar@532: } alpar@532: }; alpar@532: tapolcai@531: }; tapolcai@531: tapolcai@531: } tapolcai@531: tapolcai@531: #endif