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