deba@326: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@326: * deba@326: * This file is a part of LEMON, a generic C++ optimization library. deba@326: * alpar@440: * Copyright (C) 2003-2009 deba@326: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@326: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@326: * deba@326: * Permission to use, modify and distribute this software is granted deba@326: * provided that this copyright notice appears in all copies. For deba@326: * precise terms see the accompanying LICENSE file. deba@326: * deba@326: * This software is provided "AS IS" with no warranty of any kind, deba@326: * express or implied, and with no claim as to its suitability for any deba@326: * purpose. deba@326: * deba@326: */ deba@326: deba@326: #ifndef LEMON_MAX_MATCHING_H deba@326: #define LEMON_MAX_MATCHING_H deba@326: deba@326: #include deba@326: #include deba@326: #include deba@326: #include deba@326: deba@326: #include deba@326: #include deba@326: #include deba@326: #include deba@326: deba@326: ///\ingroup matching deba@326: ///\file deba@327: ///\brief Maximum matching algorithms in general graphs. deba@326: deba@326: namespace lemon { deba@326: deba@327: /// \ingroup matching deba@326: /// deba@327: /// \brief Edmonds' alternating forest maximum matching algorithm. deba@326: /// alpar@330: /// This class implements Edmonds' alternating forest matching alpar@330: /// algorithm. The algorithm can be started from an arbitrary initial alpar@330: /// matching (the default is the empty one) deba@326: /// alpar@330: /// The dual solution of the problem is a map of the nodes to deba@327: /// MaxMatching::Status, having values \c EVEN/D, \c ODD/A and \c deba@327: /// MATCHED/C showing the Gallai-Edmonds decomposition of the deba@327: /// graph. The nodes in \c EVEN/D induce a graph with deba@327: /// factor-critical components, the nodes in \c ODD/A form the deba@327: /// barrier, and the nodes in \c MATCHED/C induce a graph having a alpar@330: /// perfect matching. The number of the factor-critical components deba@327: /// minus the number of barrier nodes is a lower bound on the alpar@330: /// unmatched nodes, and the matching is optimal if and only if this bound is deba@327: /// tight. This decomposition can be attained by calling \c deba@327: /// decomposition() after running the algorithm. deba@326: /// kpeter@559: /// \param GR The graph type the algorithm runs on. kpeter@559: template deba@326: class MaxMatching { deba@327: public: deba@327: kpeter@559: typedef GR Graph; deba@327: typedef typename Graph::template NodeMap deba@327: MatchingMap; deba@327: deba@327: ///\brief Indicates the Gallai-Edmonds decomposition of the graph. deba@327: /// alpar@330: ///Indicates the Gallai-Edmonds decomposition of the graph. The deba@327: ///nodes with Status \c EVEN/D induce a graph with factor-critical deba@327: ///components, the nodes in \c ODD/A form the canonical barrier, deba@327: ///and the nodes in \c MATCHED/C induce a graph having a perfect deba@327: ///matching. deba@327: enum Status { deba@327: EVEN = 1, D = 1, MATCHED = 0, C = 0, ODD = -1, A = -1, UNMATCHED = -2 deba@327: }; deba@327: deba@327: typedef typename Graph::template NodeMap StatusMap; deba@327: deba@327: private: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@327: typedef UnionFindEnum BlossomSet; deba@327: typedef ExtendFindEnum TreeSet; deba@327: typedef RangeMap NodeIntMap; deba@327: typedef MatchingMap EarMap; deba@327: typedef std::vector NodeQueue; deba@327: deba@327: const Graph& _graph; deba@327: MatchingMap* _matching; deba@327: StatusMap* _status; deba@327: deba@327: EarMap* _ear; deba@327: deba@327: IntNodeMap* _blossom_set_index; deba@327: BlossomSet* _blossom_set; deba@327: NodeIntMap* _blossom_rep; deba@327: deba@327: IntNodeMap* _tree_set_index; deba@327: TreeSet* _tree_set; deba@327: deba@327: NodeQueue _node_queue; deba@327: int _process, _postpone, _last; deba@327: deba@327: int _node_num; deba@327: deba@327: private: deba@327: deba@327: void createStructures() { deba@327: _node_num = countNodes(_graph); deba@327: if (!_matching) { deba@327: _matching = new MatchingMap(_graph); deba@327: } deba@327: if (!_status) { deba@327: _status = new StatusMap(_graph); deba@327: } deba@327: if (!_ear) { deba@327: _ear = new EarMap(_graph); deba@327: } deba@327: if (!_blossom_set) { deba@327: _blossom_set_index = new IntNodeMap(_graph); deba@327: _blossom_set = new BlossomSet(*_blossom_set_index); deba@327: } deba@327: if (!_blossom_rep) { deba@327: _blossom_rep = new NodeIntMap(_node_num); deba@327: } deba@327: if (!_tree_set) { deba@327: _tree_set_index = new IntNodeMap(_graph); deba@327: _tree_set = new TreeSet(*_tree_set_index); deba@327: } deba@327: _node_queue.resize(_node_num); deba@327: } deba@327: deba@327: void destroyStructures() { deba@327: if (_matching) { deba@327: delete _matching; deba@327: } deba@327: if (_status) { deba@327: delete _status; deba@327: } deba@327: if (_ear) { deba@327: delete _ear; deba@327: } deba@327: if (_blossom_set) { deba@327: delete _blossom_set; deba@327: delete _blossom_set_index; deba@327: } deba@327: if (_blossom_rep) { deba@327: delete _blossom_rep; deba@327: } deba@327: if (_tree_set) { deba@327: delete _tree_set_index; deba@327: delete _tree_set; deba@327: } deba@327: } deba@327: deba@327: void processDense(const Node& n) { deba@327: _process = _postpone = _last = 0; deba@327: _node_queue[_last++] = n; deba@327: deba@327: while (_process != _last) { deba@327: Node u = _node_queue[_process++]; deba@327: for (OutArcIt a(_graph, u); a != INVALID; ++a) { deba@327: Node v = _graph.target(a); deba@327: if ((*_status)[v] == MATCHED) { deba@327: extendOnArc(a); deba@327: } else if ((*_status)[v] == UNMATCHED) { deba@327: augmentOnArc(a); deba@327: return; deba@327: } deba@327: } deba@327: } deba@327: deba@327: while (_postpone != _last) { deba@327: Node u = _node_queue[_postpone++]; deba@327: deba@327: for (OutArcIt a(_graph, u); a != INVALID ; ++a) { deba@327: Node v = _graph.target(a); deba@327: deba@327: if ((*_status)[v] == EVEN) { deba@327: if (_blossom_set->find(u) != _blossom_set->find(v)) { deba@327: shrinkOnEdge(a); deba@327: } deba@327: } deba@327: deba@327: while (_process != _last) { deba@327: Node w = _node_queue[_process++]; deba@327: for (OutArcIt b(_graph, w); b != INVALID; ++b) { deba@327: Node x = _graph.target(b); deba@327: if ((*_status)[x] == MATCHED) { deba@327: extendOnArc(b); deba@327: } else if ((*_status)[x] == UNMATCHED) { deba@327: augmentOnArc(b); deba@327: return; deba@327: } deba@327: } deba@327: } deba@327: } deba@327: } deba@327: } deba@327: deba@327: void processSparse(const Node& n) { deba@327: _process = _last = 0; deba@327: _node_queue[_last++] = n; deba@327: while (_process != _last) { deba@327: Node u = _node_queue[_process++]; deba@327: for (OutArcIt a(_graph, u); a != INVALID; ++a) { deba@327: Node v = _graph.target(a); deba@327: deba@327: if ((*_status)[v] == EVEN) { deba@327: if (_blossom_set->find(u) != _blossom_set->find(v)) { deba@327: shrinkOnEdge(a); deba@327: } deba@327: } else if ((*_status)[v] == MATCHED) { deba@327: extendOnArc(a); deba@327: } else if ((*_status)[v] == UNMATCHED) { deba@327: augmentOnArc(a); deba@327: return; deba@327: } deba@327: } deba@327: } deba@327: } deba@327: deba@327: void shrinkOnEdge(const Edge& e) { deba@327: Node nca = INVALID; deba@327: deba@327: { deba@327: std::set left_set, right_set; deba@327: deba@327: Node left = (*_blossom_rep)[_blossom_set->find(_graph.u(e))]; deba@327: left_set.insert(left); deba@327: deba@327: Node right = (*_blossom_rep)[_blossom_set->find(_graph.v(e))]; deba@327: right_set.insert(right); deba@327: deba@327: while (true) { deba@327: if ((*_matching)[left] == INVALID) break; deba@327: left = _graph.target((*_matching)[left]); deba@327: left = (*_blossom_rep)[_blossom_set-> deba@327: find(_graph.target((*_ear)[left]))]; deba@327: if (right_set.find(left) != right_set.end()) { deba@327: nca = left; deba@327: break; deba@327: } deba@327: left_set.insert(left); deba@327: deba@327: if ((*_matching)[right] == INVALID) break; deba@327: right = _graph.target((*_matching)[right]); deba@327: right = (*_blossom_rep)[_blossom_set-> deba@327: find(_graph.target((*_ear)[right]))]; deba@327: if (left_set.find(right) != left_set.end()) { deba@327: nca = right; deba@327: break; deba@327: } deba@327: right_set.insert(right); deba@327: } deba@327: deba@327: if (nca == INVALID) { deba@327: if ((*_matching)[left] == INVALID) { deba@327: nca = right; deba@327: while (left_set.find(nca) == left_set.end()) { deba@327: nca = _graph.target((*_matching)[nca]); deba@327: nca =(*_blossom_rep)[_blossom_set-> deba@327: find(_graph.target((*_ear)[nca]))]; deba@327: } deba@327: } else { deba@327: nca = left; deba@327: while (right_set.find(nca) == right_set.end()) { deba@327: nca = _graph.target((*_matching)[nca]); deba@327: nca = (*_blossom_rep)[_blossom_set-> deba@327: find(_graph.target((*_ear)[nca]))]; deba@327: } deba@327: } deba@327: } deba@327: } deba@327: deba@327: { deba@327: deba@327: Node node = _graph.u(e); deba@327: Arc arc = _graph.direct(e, true); deba@327: Node base = (*_blossom_rep)[_blossom_set->find(node)]; deba@327: deba@327: while (base != nca) { kpeter@581: (*_ear)[node] = arc; deba@327: deba@327: Node n = node; deba@327: while (n != base) { deba@327: n = _graph.target((*_matching)[n]); deba@327: Arc a = (*_ear)[n]; deba@327: n = _graph.target(a); kpeter@581: (*_ear)[n] = _graph.oppositeArc(a); deba@327: } deba@327: node = _graph.target((*_matching)[base]); deba@327: _tree_set->erase(base); deba@327: _tree_set->erase(node); deba@327: _blossom_set->insert(node, _blossom_set->find(base)); kpeter@581: (*_status)[node] = EVEN; deba@327: _node_queue[_last++] = node; deba@327: arc = _graph.oppositeArc((*_ear)[node]); deba@327: node = _graph.target((*_ear)[node]); deba@327: base = (*_blossom_rep)[_blossom_set->find(node)]; deba@327: _blossom_set->join(_graph.target(arc), base); deba@327: } deba@327: } deba@327: kpeter@581: (*_blossom_rep)[_blossom_set->find(nca)] = nca; deba@327: deba@327: { deba@327: deba@327: Node node = _graph.v(e); deba@327: Arc arc = _graph.direct(e, false); deba@327: Node base = (*_blossom_rep)[_blossom_set->find(node)]; deba@327: deba@327: while (base != nca) { kpeter@581: (*_ear)[node] = arc; deba@327: deba@327: Node n = node; deba@327: while (n != base) { deba@327: n = _graph.target((*_matching)[n]); deba@327: Arc a = (*_ear)[n]; deba@327: n = _graph.target(a); kpeter@581: (*_ear)[n] = _graph.oppositeArc(a); deba@327: } deba@327: node = _graph.target((*_matching)[base]); deba@327: _tree_set->erase(base); deba@327: _tree_set->erase(node); deba@327: _blossom_set->insert(node, _blossom_set->find(base)); kpeter@581: (*_status)[node] = EVEN; deba@327: _node_queue[_last++] = node; deba@327: arc = _graph.oppositeArc((*_ear)[node]); deba@327: node = _graph.target((*_ear)[node]); deba@327: base = (*_blossom_rep)[_blossom_set->find(node)]; deba@327: _blossom_set->join(_graph.target(arc), base); deba@327: } deba@327: } deba@327: kpeter@581: (*_blossom_rep)[_blossom_set->find(nca)] = nca; deba@327: } deba@327: deba@327: deba@327: deba@327: void extendOnArc(const Arc& a) { deba@327: Node base = _graph.source(a); deba@327: Node odd = _graph.target(a); deba@327: kpeter@581: (*_ear)[odd] = _graph.oppositeArc(a); deba@327: Node even = _graph.target((*_matching)[odd]); kpeter@581: (*_blossom_rep)[_blossom_set->insert(even)] = even; kpeter@581: (*_status)[odd] = ODD; kpeter@581: (*_status)[even] = EVEN; deba@327: int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]); deba@327: _tree_set->insert(odd, tree); deba@327: _tree_set->insert(even, tree); deba@327: _node_queue[_last++] = even; deba@327: deba@327: } deba@327: deba@327: void augmentOnArc(const Arc& a) { deba@327: Node even = _graph.source(a); deba@327: Node odd = _graph.target(a); deba@327: deba@327: int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]); deba@327: kpeter@581: (*_matching)[odd] = _graph.oppositeArc(a); kpeter@581: (*_status)[odd] = MATCHED; deba@327: deba@327: Arc arc = (*_matching)[even]; kpeter@581: (*_matching)[even] = a; deba@327: deba@327: while (arc != INVALID) { deba@327: odd = _graph.target(arc); deba@327: arc = (*_ear)[odd]; deba@327: even = _graph.target(arc); kpeter@581: (*_matching)[odd] = arc; deba@327: arc = (*_matching)[even]; kpeter@581: (*_matching)[even] = _graph.oppositeArc((*_matching)[odd]); deba@327: } deba@327: deba@327: for (typename TreeSet::ItemIt it(*_tree_set, tree); deba@327: it != INVALID; ++it) { deba@327: if ((*_status)[it] == ODD) { kpeter@581: (*_status)[it] = MATCHED; deba@327: } else { deba@327: int blossom = _blossom_set->find(it); deba@327: for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom); deba@327: jt != INVALID; ++jt) { kpeter@581: (*_status)[jt] = MATCHED; deba@327: } deba@327: _blossom_set->eraseClass(blossom); deba@327: } deba@327: } deba@327: _tree_set->eraseClass(tree); deba@327: deba@327: } deba@326: deba@326: public: deba@326: deba@327: /// \brief Constructor deba@326: /// deba@327: /// Constructor. deba@327: MaxMatching(const Graph& graph) deba@327: : _graph(graph), _matching(0), _status(0), _ear(0), deba@327: _blossom_set_index(0), _blossom_set(0), _blossom_rep(0), deba@327: _tree_set_index(0), _tree_set(0) {} deba@327: deba@327: ~MaxMatching() { deba@327: destroyStructures(); deba@327: } deba@327: deba@327: /// \name Execution control alpar@330: /// The simplest way to execute the algorithm is to use the deba@327: /// \c run() member function. deba@327: /// \n deba@327: alpar@330: /// If you need better control on the execution, you must call deba@327: /// \ref init(), \ref greedyInit() or \ref matchingInit() alpar@330: /// functions first, then you can start the algorithm with the \ref deba@425: /// startSparse() or startDense() functions. deba@327: deba@327: ///@{ deba@327: deba@327: /// \brief Sets the actual matching to the empty matching. deba@326: /// deba@327: /// Sets the actual matching to the empty matching. deba@326: /// deba@326: void init() { deba@327: createStructures(); deba@327: for(NodeIt n(_graph); n != INVALID; ++n) { kpeter@581: (*_matching)[n] = INVALID; kpeter@581: (*_status)[n] = UNMATCHED; deba@326: } deba@326: } deba@326: alpar@330: ///\brief Finds an initial matching in a greedy way deba@326: /// alpar@330: ///It finds an initial matching in a greedy way. deba@326: void greedyInit() { deba@327: createStructures(); deba@327: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@581: (*_matching)[n] = INVALID; kpeter@581: (*_status)[n] = UNMATCHED; deba@326: } deba@327: for (NodeIt n(_graph); n != INVALID; ++n) { deba@327: if ((*_matching)[n] == INVALID) { deba@327: for (OutArcIt a(_graph, n); a != INVALID ; ++a) { deba@327: Node v = _graph.target(a); deba@327: if ((*_matching)[v] == INVALID && v != n) { kpeter@581: (*_matching)[n] = a; kpeter@581: (*_status)[n] = MATCHED; kpeter@581: (*_matching)[v] = _graph.oppositeArc(a); kpeter@581: (*_status)[v] = MATCHED; deba@326: break; deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@327: alpar@330: /// \brief Initialize the matching from a map containing. deba@326: /// deba@327: /// Initialize the matching from a \c bool valued \c Edge map. This deba@327: /// map must have the property that there are no two incident edges deba@327: /// with true value, ie. it contains a matching. kpeter@559: /// \return \c true if the map contains a matching. deba@327: template deba@327: bool matchingInit(const MatchingMap& matching) { deba@327: createStructures(); deba@327: deba@327: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@581: (*_matching)[n] = INVALID; kpeter@581: (*_status)[n] = UNMATCHED; deba@326: } deba@327: for(EdgeIt e(_graph); e!=INVALID; ++e) { deba@327: if (matching[e]) { deba@327: deba@327: Node u = _graph.u(e); deba@327: if ((*_matching)[u] != INVALID) return false; kpeter@581: (*_matching)[u] = _graph.direct(e, true); kpeter@581: (*_status)[u] = MATCHED; deba@327: deba@327: Node v = _graph.v(e); deba@327: if ((*_matching)[v] != INVALID) return false; kpeter@581: (*_matching)[v] = _graph.direct(e, false); kpeter@581: (*_status)[v] = MATCHED; deba@327: } deba@327: } deba@327: return true; deba@326: } deba@326: deba@327: /// \brief Starts Edmonds' algorithm deba@326: /// deba@327: /// If runs the original Edmonds' algorithm. deba@327: void startSparse() { deba@327: for(NodeIt n(_graph); n != INVALID; ++n) { deba@327: if ((*_status)[n] == UNMATCHED) { deba@327: (*_blossom_rep)[_blossom_set->insert(n)] = n; deba@327: _tree_set->insert(n); kpeter@581: (*_status)[n] = EVEN; deba@327: processSparse(n); deba@326: } deba@326: } deba@326: } deba@326: deba@327: /// \brief Starts Edmonds' algorithm. deba@326: /// deba@327: /// It runs Edmonds' algorithm with a heuristic of postponing alpar@330: /// shrinks, therefore resulting in a faster algorithm for dense graphs. deba@327: void startDense() { deba@327: for(NodeIt n(_graph); n != INVALID; ++n) { deba@327: if ((*_status)[n] == UNMATCHED) { deba@327: (*_blossom_rep)[_blossom_set->insert(n)] = n; deba@327: _tree_set->insert(n); kpeter@581: (*_status)[n] = EVEN; deba@327: processDense(n); deba@327: } deba@327: } deba@327: } deba@327: deba@327: deba@327: /// \brief Runs Edmonds' algorithm deba@327: /// deba@327: /// Runs Edmonds' algorithm for sparse graphs (m<2*n) deba@327: /// or Edmonds' algorithm with a heuristic of deba@327: /// postponing shrinks for dense graphs. deba@326: void run() { deba@327: if (countEdges(_graph) < 2 * countNodes(_graph)) { deba@326: greedyInit(); deba@326: startSparse(); deba@326: } else { deba@326: init(); deba@326: startDense(); deba@326: } deba@326: } deba@326: deba@327: /// @} deba@327: deba@327: /// \name Primal solution alpar@330: /// Functions to get the primal solution, ie. the matching. deba@327: deba@327: /// @{ deba@326: alpar@330: ///\brief Returns the size of the current matching. deba@326: /// alpar@330: ///Returns the size of the current matching. After \ref deba@327: ///run() it returns the size of the maximum matching in the graph. deba@327: int matchingSize() const { deba@327: int size = 0; deba@327: for (NodeIt n(_graph); n != INVALID; ++n) { deba@327: if ((*_matching)[n] != INVALID) { deba@327: ++size; deba@326: } deba@326: } deba@327: return size / 2; deba@326: } deba@326: deba@327: /// \brief Returns true when the edge is in the matching. deba@327: /// deba@327: /// Returns true when the edge is in the matching. deba@327: bool matching(const Edge& edge) const { deba@327: return edge == (*_matching)[_graph.u(edge)]; deba@327: } deba@327: deba@327: /// \brief Returns the matching edge incident to the given node. deba@327: /// deba@327: /// Returns the matching edge of a \c node in the actual matching or deba@327: /// INVALID if the \c node is not covered by the actual matching. deba@327: Arc matching(const Node& n) const { deba@327: return (*_matching)[n]; deba@327: } deba@326: deba@326: ///\brief Returns the mate of a node in the actual matching. deba@326: /// deba@327: ///Returns the mate of a \c node in the actual matching or deba@327: ///INVALID if the \c node is not covered by the actual matching. deba@327: Node mate(const Node& n) const { deba@327: return (*_matching)[n] != INVALID ? deba@327: _graph.target((*_matching)[n]) : INVALID; deba@326: } deba@326: deba@327: /// @} deba@327: deba@327: /// \name Dual solution alpar@330: /// Functions to get the dual solution, ie. the decomposition. deba@327: deba@327: /// @{ deba@326: deba@326: /// \brief Returns the class of the node in the Edmonds-Gallai deba@326: /// decomposition. deba@326: /// deba@326: /// Returns the class of the node in the Edmonds-Gallai deba@326: /// decomposition. deba@327: Status decomposition(const Node& n) const { deba@327: return (*_status)[n]; deba@326: } deba@326: deba@326: /// \brief Returns true when the node is in the barrier. deba@326: /// deba@326: /// Returns true when the node is in the barrier. deba@327: bool barrier(const Node& n) const { deba@327: return (*_status)[n] == ODD; deba@326: } deba@326: deba@327: /// @} deba@326: deba@326: }; deba@326: deba@326: /// \ingroup matching deba@326: /// deba@326: /// \brief Weighted matching in general graphs deba@326: /// deba@326: /// This class provides an efficient implementation of Edmond's deba@326: /// maximum weighted matching algorithm. The implementation is based deba@326: /// on extensive use of priority queues and provides kpeter@559: /// \f$O(nm\log n)\f$ time complexity. deba@326: /// deba@326: /// The maximum weighted matching problem is to find undirected deba@327: /// edges in the graph with maximum overall weight and no two of deba@327: /// them shares their ends. The problem can be formulated with the deba@327: /// following linear program. deba@326: /// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f] deba@327: /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} deba@327: \quad \forall B\in\mathcal{O}\f] */ deba@326: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@326: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@327: /// where \f$\delta(X)\f$ is the set of edges incident to a node in deba@327: /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in deba@327: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality deba@327: /// subsets of the nodes. deba@326: /// deba@326: /// The algorithm calculates an optimal matching and a proof of the deba@326: /// optimality. The solution of the dual problem can be used to check deba@327: /// the result of the algorithm. The dual linear problem is the deba@327: /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)} deba@327: z_B \ge w_{uv} \quad \forall uv\in E\f] */ deba@326: /// \f[y_u \ge 0 \quad \forall u \in V\f] deba@326: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@327: /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}} deba@327: \frac{\vert B \vert - 1}{2}z_B\f] */ deba@326: /// deba@326: /// The algorithm can be executed with \c run() or the \c init() and deba@326: /// then the \c start() member functions. After it the matching can deba@326: /// be asked with \c matching() or mate() functions. The dual deba@326: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@326: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt alpar@330: /// "BlossomIt" nested class, which is able to iterate on the nodes deba@326: /// of a blossom. If the value type is integral then the dual deba@326: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". kpeter@559: template > deba@326: class MaxWeightedMatching { deba@326: public: deba@326: kpeter@559: ///\e kpeter@559: typedef GR Graph; kpeter@559: ///\e kpeter@559: typedef WM WeightMap; kpeter@559: ///\e deba@326: typedef typename WeightMap::Value Value; deba@326: deba@326: /// \brief Scaling factor for dual solution deba@326: /// deba@326: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@326: /// according to the value type. deba@326: static const int dualScale = deba@326: std::numeric_limits::is_integer ? 4 : 1; deba@326: deba@326: typedef typename Graph::template NodeMap deba@326: MatchingMap; deba@326: deba@326: private: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@326: typedef typename Graph::template NodeMap NodePotential; deba@326: typedef std::vector BlossomNodeList; deba@326: deba@326: struct BlossomVariable { deba@326: int begin, end; deba@326: Value value; deba@326: deba@326: BlossomVariable(int _begin, int _end, Value _value) deba@326: : begin(_begin), end(_end), value(_value) {} deba@326: deba@326: }; deba@326: deba@326: typedef std::vector BlossomPotential; deba@326: deba@326: const Graph& _graph; deba@326: const WeightMap& _weight; deba@326: deba@326: MatchingMap* _matching; deba@326: deba@326: NodePotential* _node_potential; deba@326: deba@326: BlossomPotential _blossom_potential; deba@326: BlossomNodeList _blossom_node_list; deba@326: deba@326: int _node_num; deba@326: int _blossom_num; deba@326: deba@326: typedef RangeMap IntIntMap; deba@326: deba@326: enum Status { deba@326: EVEN = -1, MATCHED = 0, ODD = 1, UNMATCHED = -2 deba@326: }; deba@326: deba@327: typedef HeapUnionFind BlossomSet; deba@326: struct BlossomData { deba@326: int tree; deba@326: Status status; deba@326: Arc pred, next; deba@326: Value pot, offset; deba@326: Node base; deba@326: }; deba@326: deba@327: IntNodeMap *_blossom_index; deba@326: BlossomSet *_blossom_set; deba@326: RangeMap* _blossom_data; deba@326: deba@327: IntNodeMap *_node_index; deba@327: IntArcMap *_node_heap_index; deba@326: deba@326: struct NodeData { deba@326: deba@327: NodeData(IntArcMap& node_heap_index) deba@326: : heap(node_heap_index) {} deba@326: deba@326: int blossom; deba@326: Value pot; deba@327: BinHeap heap; deba@326: std::map heap_index; deba@326: deba@326: int tree; deba@326: }; deba@326: deba@326: RangeMap* _node_data; deba@326: deba@326: typedef ExtendFindEnum TreeSet; deba@326: deba@326: IntIntMap *_tree_set_index; deba@326: TreeSet *_tree_set; deba@326: deba@327: IntNodeMap *_delta1_index; deba@327: BinHeap *_delta1; deba@326: deba@326: IntIntMap *_delta2_index; deba@326: BinHeap *_delta2; deba@326: deba@327: IntEdgeMap *_delta3_index; deba@327: BinHeap *_delta3; deba@326: deba@326: IntIntMap *_delta4_index; deba@326: BinHeap *_delta4; deba@326: deba@326: Value _delta_sum; deba@326: deba@326: void createStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (!_matching) { deba@326: _matching = new MatchingMap(_graph); deba@326: } deba@326: if (!_node_potential) { deba@326: _node_potential = new NodePotential(_graph); deba@326: } deba@326: if (!_blossom_set) { deba@327: _blossom_index = new IntNodeMap(_graph); deba@326: _blossom_set = new BlossomSet(*_blossom_index); deba@326: _blossom_data = new RangeMap(_blossom_num); deba@326: } deba@326: deba@326: if (!_node_index) { deba@327: _node_index = new IntNodeMap(_graph); deba@327: _node_heap_index = new IntArcMap(_graph); deba@326: _node_data = new RangeMap(_node_num, deba@326: NodeData(*_node_heap_index)); deba@326: } deba@326: deba@326: if (!_tree_set) { deba@326: _tree_set_index = new IntIntMap(_blossom_num); deba@326: _tree_set = new TreeSet(*_tree_set_index); deba@326: } deba@326: if (!_delta1) { deba@327: _delta1_index = new IntNodeMap(_graph); deba@327: _delta1 = new BinHeap(*_delta1_index); deba@326: } deba@326: if (!_delta2) { deba@326: _delta2_index = new IntIntMap(_blossom_num); deba@326: _delta2 = new BinHeap(*_delta2_index); deba@326: } deba@326: if (!_delta3) { deba@327: _delta3_index = new IntEdgeMap(_graph); deba@327: _delta3 = new BinHeap(*_delta3_index); deba@326: } deba@326: if (!_delta4) { deba@326: _delta4_index = new IntIntMap(_blossom_num); deba@326: _delta4 = new BinHeap(*_delta4_index); deba@326: } deba@326: } deba@326: deba@326: void destroyStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (_matching) { deba@326: delete _matching; deba@326: } deba@326: if (_node_potential) { deba@326: delete _node_potential; deba@326: } deba@326: if (_blossom_set) { deba@326: delete _blossom_index; deba@326: delete _blossom_set; deba@326: delete _blossom_data; deba@326: } deba@326: deba@326: if (_node_index) { deba@326: delete _node_index; deba@326: delete _node_heap_index; deba@326: delete _node_data; deba@326: } deba@326: deba@326: if (_tree_set) { deba@326: delete _tree_set_index; deba@326: delete _tree_set; deba@326: } deba@326: if (_delta1) { deba@326: delete _delta1_index; deba@326: delete _delta1; deba@326: } deba@326: if (_delta2) { deba@326: delete _delta2_index; deba@326: delete _delta2; deba@326: } deba@326: if (_delta3) { deba@326: delete _delta3_index; deba@326: delete _delta3; deba@326: } deba@326: if (_delta4) { deba@326: delete _delta4_index; deba@326: delete _delta4; deba@326: } deba@326: } deba@326: deba@326: void matchedToEven(int blossom, int tree) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: _delta1->push(n, (*_node_data)[ni].pot); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } else { deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset){ deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void matchedToOdd(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: (*_blossom_data)[blossom].offset += _delta_sum; deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: void evenToMatched(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: (*_node_data)[ni].pot -= _delta_sum; deba@326: deba@326: _delta1->erase(n); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: if (vt != tree) { deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > deba@326: _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: } deba@326: deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: (*_node_data)[vi].heap.erase(it->second); deba@326: (*_node_data)[vi].heap_index.erase(it); deba@326: if ((*_node_data)[vi].heap.empty()) { deba@326: _blossom_set->increase(v, std::numeric_limits::max()); deba@326: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@326: } deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_blossom_set->classPrio(vb) == deba@326: std::numeric_limits::max()) { deba@326: _delta2->erase(vb); deba@326: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void oddToMatched(int blossom) { deba@326: (*_blossom_data)[blossom].offset -= _delta_sum; deba@326: deba@326: if (_blossom_set->classPrio(blossom) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: } deba@326: } deba@326: deba@326: void oddToEven(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: (*_node_data)[ni].pot += deba@326: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: _delta1->push(n, (*_node_data)[ni].pot); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: deba@326: void matchedToUnmatched(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.target(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void unmatchedToMatched(int blossom) { deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > _blossom_set->classPrio(blossom)- deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void alternatePath(int even, int tree) { deba@326: int odd; deba@326: deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: deba@326: while ((*_blossom_data)[even].pred != INVALID) { deba@326: odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred)); deba@326: (*_blossom_data)[odd].status = MATCHED; deba@326: oddToMatched(odd); deba@326: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@326: deba@326: even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred)); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].next = deba@326: _graph.oppositeArc((*_blossom_data)[odd].pred); deba@326: } deba@326: deba@326: } deba@326: deba@326: void destroyTree(int tree) { deba@326: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@326: if ((*_blossom_data)[b].status == EVEN) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: evenToMatched(b, tree); deba@326: } else if ((*_blossom_data)[b].status == ODD) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: oddToMatched(b); deba@326: } deba@326: } deba@326: _tree_set->eraseClass(tree); deba@326: } deba@326: deba@326: deba@326: void unmatchNode(const Node& node) { deba@326: int blossom = _blossom_set->find(node); deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: alternatePath(blossom, tree); deba@326: destroyTree(tree); deba@326: deba@326: (*_blossom_data)[blossom].status = UNMATCHED; deba@326: (*_blossom_data)[blossom].base = node; deba@326: matchedToUnmatched(blossom); deba@326: } deba@326: deba@326: deba@327: void augmentOnEdge(const Edge& edge) { deba@327: deba@327: int left = _blossom_set->find(_graph.u(edge)); deba@327: int right = _blossom_set->find(_graph.v(edge)); deba@326: deba@326: if ((*_blossom_data)[left].status == EVEN) { deba@326: int left_tree = _tree_set->find(left); deba@326: alternatePath(left, left_tree); deba@326: destroyTree(left_tree); deba@326: } else { deba@326: (*_blossom_data)[left].status = MATCHED; deba@326: unmatchedToMatched(left); deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].status == EVEN) { deba@326: int right_tree = _tree_set->find(right); deba@326: alternatePath(right, right_tree); deba@326: destroyTree(right_tree); deba@326: } else { deba@326: (*_blossom_data)[right].status = MATCHED; deba@326: unmatchedToMatched(right); deba@326: } deba@326: deba@327: (*_blossom_data)[left].next = _graph.direct(edge, true); deba@327: (*_blossom_data)[right].next = _graph.direct(edge, false); deba@326: } deba@326: deba@326: void extendOnArc(const Arc& arc) { deba@326: int base = _blossom_set->find(_graph.target(arc)); deba@326: int tree = _tree_set->find(base); deba@326: deba@326: int odd = _blossom_set->find(_graph.source(arc)); deba@326: _tree_set->insert(odd, tree); deba@326: (*_blossom_data)[odd].status = ODD; deba@326: matchedToOdd(odd); deba@326: (*_blossom_data)[odd].pred = arc; deba@326: deba@326: int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next)); deba@326: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@326: _tree_set->insert(even, tree); deba@326: (*_blossom_data)[even].status = EVEN; deba@326: matchedToEven(even, tree); deba@326: } deba@326: deba@327: void shrinkOnEdge(const Edge& edge, int tree) { deba@326: int nca = -1; deba@326: std::vector left_path, right_path; deba@326: deba@326: { deba@326: std::set left_set, right_set; deba@326: int left = _blossom_set->find(_graph.u(edge)); deba@326: left_path.push_back(left); deba@326: left_set.insert(left); deba@326: deba@326: int right = _blossom_set->find(_graph.v(edge)); deba@326: right_path.push_back(right); deba@326: right_set.insert(right); deba@326: deba@326: while (true) { deba@326: deba@326: if ((*_blossom_data)[left].pred == INVALID) break; deba@326: deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: deba@326: left_set.insert(left); deba@326: deba@326: if (right_set.find(left) != right_set.end()) { deba@326: nca = left; deba@326: break; deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].pred == INVALID) break; deba@326: deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: deba@326: right_set.insert(right); deba@326: deba@326: if (left_set.find(right) != left_set.end()) { deba@326: nca = right; deba@326: break; deba@326: } deba@326: deba@326: } deba@326: deba@326: if (nca == -1) { deba@326: if ((*_blossom_data)[left].pred == INVALID) { deba@326: nca = right; deba@326: while (left_set.find(nca) == left_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: } deba@326: } else { deba@326: nca = left; deba@326: while (right_set.find(nca) == right_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: Arc prev; deba@326: deba@326: prev = _graph.direct(edge, true); deba@326: for (int i = 0; left_path[i] != nca; i += 2) { deba@326: subblossoms.push_back(left_path[i]); deba@326: (*_blossom_data)[left_path[i]].next = prev; deba@326: _tree_set->erase(left_path[i]); deba@326: deba@326: subblossoms.push_back(left_path[i + 1]); deba@326: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@326: oddToEven(left_path[i + 1], tree); deba@326: _tree_set->erase(left_path[i + 1]); deba@326: prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred); deba@326: } deba@326: deba@326: int k = 0; deba@326: while (right_path[k] != nca) ++k; deba@326: deba@326: subblossoms.push_back(nca); deba@326: (*_blossom_data)[nca].next = prev; deba@326: deba@326: for (int i = k - 2; i >= 0; i -= 2) { deba@326: subblossoms.push_back(right_path[i + 1]); deba@326: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@326: oddToEven(right_path[i + 1], tree); deba@326: _tree_set->erase(right_path[i + 1]); deba@326: deba@326: (*_blossom_data)[right_path[i + 1]].next = deba@326: (*_blossom_data)[right_path[i + 1]].pred; deba@326: deba@326: subblossoms.push_back(right_path[i]); deba@326: _tree_set->erase(right_path[i]); deba@326: } deba@326: deba@326: int surface = deba@326: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@326: deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@326: } deba@326: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@326: } deba@326: deba@326: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@326: (*_blossom_data)[surface].offset = 0; deba@326: (*_blossom_data)[surface].status = EVEN; deba@326: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@326: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@326: deba@326: _tree_set->insert(surface, tree); deba@326: _tree_set->erase(nca); deba@326: } deba@326: deba@326: void splitBlossom(int blossom) { deba@326: Arc next = (*_blossom_data)[blossom].next; deba@326: Arc pred = (*_blossom_data)[blossom].pred; deba@326: deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = MATCHED; deba@326: oddToMatched(blossom); deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: deba@326: Value offset = (*_blossom_data)[blossom].offset; deba@326: int b = _blossom_set->find(_graph.source(pred)); deba@326: int d = _blossom_set->find(_graph.source(next)); deba@326: deba@326: int ib = -1, id = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) ib = i; deba@326: if (subblossoms[i] == d) id = i; deba@326: deba@326: (*_blossom_data)[subblossoms[i]].offset = offset; deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@326: } deba@326: if (_blossom_set->classPrio(subblossoms[i]) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(subblossoms[i], deba@326: _blossom_set->classPrio(subblossoms[i]) - deba@326: (*_blossom_data)[subblossoms[i]].offset); deba@326: } deba@326: } deba@326: deba@326: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@326: for (int i = (id + 1) % subblossoms.size(); deba@326: i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].pred = pred; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: pred = (*_blossom_data)[ub].next; deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[id]].status = ODD; deba@326: matchedToOdd(subblossoms[id]); deba@326: _tree_set->insert(subblossoms[id], tree); deba@326: (*_blossom_data)[subblossoms[id]].next = next; deba@326: (*_blossom_data)[subblossoms[id]].pred = pred; deba@326: deba@326: } else { deba@326: deba@326: for (int i = (ib + 1) % subblossoms.size(); deba@326: i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].next = next; deba@326: (*_blossom_data)[sb].pred = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = deba@326: (*_blossom_data)[tb].next = deba@326: _graph.oppositeArc((*_blossom_data)[ub].next); deba@326: next = (*_blossom_data)[ub].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@326: matchedToOdd(subblossoms[ib]); deba@326: _tree_set->insert(subblossoms[ib], tree); deba@326: (*_blossom_data)[subblossoms[ib]].next = next; deba@326: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@326: } deba@326: _tree_set->erase(blossom); deba@326: } deba@326: deba@326: void extractBlossom(int blossom, const Node& base, const Arc& matching) { deba@326: if (_blossom_set->trivial(blossom)) { deba@326: int bi = (*_node_index)[base]; deba@326: Value pot = (*_node_data)[bi].pot; deba@326: kpeter@581: (*_matching)[base] = matching; deba@326: _blossom_node_list.push_back(base); kpeter@581: (*_node_potential)[base] = pot; deba@326: } else { deba@326: deba@326: Value pot = (*_blossom_data)[blossom].pot; deba@326: int bn = _blossom_node_list.size(); deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: int b = _blossom_set->find(base); deba@326: int ib = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) { ib = i; break; } deba@326: } deba@326: deba@326: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@326: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@326: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@326: deba@326: Arc m = (*_blossom_data)[tb].next; deba@326: extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m)); deba@326: extractBlossom(tb, _graph.source(m), m); deba@326: } deba@326: extractBlossom(subblossoms[ib], base, matching); deba@326: deba@326: int en = _blossom_node_list.size(); deba@326: deba@326: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@326: } deba@326: } deba@326: deba@326: void extractMatching() { deba@326: std::vector blossoms; deba@326: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@326: blossoms.push_back(c); deba@326: } deba@326: deba@326: for (int i = 0; i < int(blossoms.size()); ++i) { deba@326: if ((*_blossom_data)[blossoms[i]].status == MATCHED) { deba@326: deba@326: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@326: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@326: n != INVALID; ++n) { deba@326: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@326: } deba@326: deba@326: Arc matching = (*_blossom_data)[blossoms[i]].next; deba@326: Node base = _graph.source(matching); deba@326: extractBlossom(blossoms[i], base, matching); deba@326: } else { deba@326: Node base = (*_blossom_data)[blossoms[i]].base; deba@326: extractBlossom(blossoms[i], base, INVALID); deba@326: } deba@326: } deba@326: } deba@326: deba@326: public: deba@326: deba@326: /// \brief Constructor deba@326: /// deba@326: /// Constructor. deba@326: MaxWeightedMatching(const Graph& graph, const WeightMap& weight) deba@326: : _graph(graph), _weight(weight), _matching(0), deba@326: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@326: _node_num(0), _blossom_num(0), deba@326: deba@326: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@326: _node_index(0), _node_heap_index(0), _node_data(0), deba@326: _tree_set_index(0), _tree_set(0), deba@326: deba@326: _delta1_index(0), _delta1(0), deba@326: _delta2_index(0), _delta2(0), deba@326: _delta3_index(0), _delta3(0), deba@326: _delta4_index(0), _delta4(0), deba@326: deba@326: _delta_sum() {} deba@326: deba@326: ~MaxWeightedMatching() { deba@326: destroyStructures(); deba@326: } deba@326: deba@326: /// \name Execution control alpar@330: /// The simplest way to execute the algorithm is to use the deba@326: /// \c run() member function. deba@326: deba@326: ///@{ deba@326: deba@326: /// \brief Initialize the algorithm deba@326: /// deba@326: /// Initialize the algorithm deba@326: void init() { deba@326: createStructures(); deba@326: deba@326: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@581: (*_node_heap_index)[e] = BinHeap::PRE_HEAP; deba@326: } deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@581: (*_delta1_index)[n] = _delta1->PRE_HEAP; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { kpeter@581: (*_delta3_index)[e] = _delta3->PRE_HEAP; deba@326: } deba@326: for (int i = 0; i < _blossom_num; ++i) { kpeter@581: (*_delta2_index)[i] = _delta2->PRE_HEAP; kpeter@581: (*_delta4_index)[i] = _delta4->PRE_HEAP; deba@326: } deba@326: deba@326: int index = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: Value max = 0; deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: if (_graph.target(e) == n) continue; deba@326: if ((dualScale * _weight[e]) / 2 > max) { deba@326: max = (dualScale * _weight[e]) / 2; deba@326: } deba@326: } kpeter@581: (*_node_index)[n] = index; deba@326: (*_node_data)[index].pot = max; deba@326: _delta1->push(n, max); deba@326: int blossom = deba@326: _blossom_set->insert(n, std::numeric_limits::max()); deba@326: deba@326: _tree_set->insert(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = EVEN; deba@326: (*_blossom_data)[blossom].pred = INVALID; deba@326: (*_blossom_data)[blossom].next = INVALID; deba@326: (*_blossom_data)[blossom].pot = 0; deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: ++index; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: int si = (*_node_index)[_graph.u(e)]; deba@326: int ti = (*_node_index)[_graph.v(e)]; deba@326: if (_graph.u(e) != _graph.v(e)) { deba@326: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@326: dualScale * _weight[e]) / 2); deba@326: } deba@326: } deba@326: } deba@326: deba@326: /// \brief Starts the algorithm deba@326: /// deba@326: /// Starts the algorithm deba@326: void start() { deba@326: enum OpType { deba@326: D1, D2, D3, D4 deba@326: }; deba@326: deba@326: int unmatched = _node_num; deba@326: while (unmatched > 0) { deba@326: Value d1 = !_delta1->empty() ? deba@326: _delta1->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d2 = !_delta2->empty() ? deba@326: _delta2->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d3 = !_delta3->empty() ? deba@326: _delta3->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d4 = !_delta4->empty() ? deba@326: _delta4->prio() : std::numeric_limits::max(); deba@326: deba@326: _delta_sum = d1; OpType ot = D1; deba@326: if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; } deba@326: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@326: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@326: deba@326: deba@326: switch (ot) { deba@326: case D1: deba@326: { deba@326: Node n = _delta1->top(); deba@326: unmatchNode(n); deba@326: --unmatched; deba@326: } deba@326: break; deba@326: case D2: deba@326: { deba@326: int blossom = _delta2->top(); deba@326: Node n = _blossom_set->classTop(blossom); deba@326: Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@326: extendOnArc(e); deba@326: } deba@326: break; deba@326: case D3: deba@326: { deba@326: Edge e = _delta3->top(); deba@326: deba@326: int left_blossom = _blossom_set->find(_graph.u(e)); deba@326: int right_blossom = _blossom_set->find(_graph.v(e)); deba@326: deba@326: if (left_blossom == right_blossom) { deba@326: _delta3->pop(); deba@326: } else { deba@326: int left_tree; deba@326: if ((*_blossom_data)[left_blossom].status == EVEN) { deba@326: left_tree = _tree_set->find(left_blossom); deba@326: } else { deba@326: left_tree = -1; deba@326: ++unmatched; deba@326: } deba@326: int right_tree; deba@326: if ((*_blossom_data)[right_blossom].status == EVEN) { deba@326: right_tree = _tree_set->find(right_blossom); deba@326: } else { deba@326: right_tree = -1; deba@326: ++unmatched; deba@326: } deba@326: deba@326: if (left_tree == right_tree) { deba@327: shrinkOnEdge(e, left_tree); deba@326: } else { deba@327: augmentOnEdge(e); deba@326: unmatched -= 2; deba@326: } deba@326: } deba@326: } break; deba@326: case D4: deba@326: splitBlossom(_delta4->top()); deba@326: break; deba@326: } deba@326: } deba@326: extractMatching(); deba@326: } deba@326: deba@326: /// \brief Runs %MaxWeightedMatching algorithm. deba@326: /// deba@326: /// This method runs the %MaxWeightedMatching algorithm. deba@326: /// deba@326: /// \note mwm.run() is just a shortcut of the following code. deba@326: /// \code deba@326: /// mwm.init(); deba@326: /// mwm.start(); deba@326: /// \endcode deba@326: void run() { deba@326: init(); deba@326: start(); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Primal solution alpar@330: /// Functions to get the primal solution, ie. the matching. deba@326: deba@326: /// @{ deba@326: alpar@330: /// \brief Returns the weight of the matching. deba@326: /// alpar@330: /// Returns the weight of the matching. deba@326: Value matchingValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: if ((*_matching)[n] != INVALID) { deba@326: sum += _weight[(*_matching)[n]]; deba@326: } deba@326: } deba@326: return sum /= 2; deba@326: } deba@326: deba@327: /// \brief Returns the cardinality of the matching. deba@326: /// deba@327: /// Returns the cardinality of the matching. deba@327: int matchingSize() const { deba@327: int num = 0; deba@327: for (NodeIt n(_graph); n != INVALID; ++n) { deba@327: if ((*_matching)[n] != INVALID) { deba@327: ++num; deba@327: } deba@327: } deba@327: return num /= 2; deba@327: } deba@327: deba@327: /// \brief Returns true when the edge is in the matching. deba@327: /// deba@327: /// Returns true when the edge is in the matching. deba@327: bool matching(const Edge& edge) const { deba@327: return edge == (*_matching)[_graph.u(edge)]; deba@326: } deba@326: deba@326: /// \brief Returns the incident matching arc. deba@326: /// deba@326: /// Returns the incident matching arc from given node. If the deba@326: /// node is not matched then it gives back \c INVALID. deba@326: Arc matching(const Node& node) const { deba@326: return (*_matching)[node]; deba@326: } deba@326: deba@326: /// \brief Returns the mate of the node. deba@326: /// deba@326: /// Returns the adjancent node in a mathcing arc. If the node is deba@326: /// not matched then it gives back \c INVALID. deba@326: Node mate(const Node& node) const { deba@326: return (*_matching)[node] != INVALID ? deba@326: _graph.target((*_matching)[node]) : INVALID; deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Dual solution alpar@330: /// Functions to get the dual solution. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the value of the dual solution. deba@326: /// deba@326: /// Returns the value of the dual solution. It should be equal to deba@326: /// the primal value scaled by \ref dualScale "dual scale". deba@326: Value dualValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: sum += nodeValue(n); deba@326: } deba@326: for (int i = 0; i < blossomNum(); ++i) { deba@326: sum += blossomValue(i) * (blossomSize(i) / 2); deba@326: } deba@326: return sum; deba@326: } deba@326: deba@326: /// \brief Returns the value of the node. deba@326: /// deba@326: /// Returns the the value of the node. deba@326: Value nodeValue(const Node& n) const { deba@326: return (*_node_potential)[n]; deba@326: } deba@326: deba@326: /// \brief Returns the number of the blossoms in the basis. deba@326: /// deba@326: /// Returns the number of the blossoms in the basis. deba@326: /// \see BlossomIt deba@326: int blossomNum() const { deba@326: return _blossom_potential.size(); deba@326: } deba@326: deba@326: deba@326: /// \brief Returns the number of the nodes in the blossom. deba@326: /// deba@326: /// Returns the number of the nodes in the blossom. deba@326: int blossomSize(int k) const { deba@326: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@326: } deba@326: deba@326: /// \brief Returns the value of the blossom. deba@326: /// deba@326: /// Returns the the value of the blossom. deba@326: /// \see BlossomIt deba@326: Value blossomValue(int k) const { deba@326: return _blossom_potential[k].value; deba@326: } deba@326: alpar@330: /// \brief Iterator for obtaining the nodes of the blossom. deba@326: /// alpar@330: /// Iterator for obtaining the nodes of the blossom. This class alpar@330: /// provides a common lemon style iterator for listing a deba@326: /// subset of the nodes. deba@326: class BlossomIt { deba@326: public: deba@326: deba@326: /// \brief Constructor. deba@326: /// alpar@330: /// Constructor to get the nodes of the variable. deba@326: BlossomIt(const MaxWeightedMatching& algorithm, int variable) deba@326: : _algorithm(&algorithm) deba@326: { deba@326: _index = _algorithm->_blossom_potential[variable].begin; deba@326: _last = _algorithm->_blossom_potential[variable].end; deba@326: } deba@326: deba@326: /// \brief Conversion to node. deba@326: /// deba@326: /// Conversion to node. deba@326: operator Node() const { deba@327: return _algorithm->_blossom_node_list[_index]; deba@326: } deba@326: deba@326: /// \brief Increment operator. deba@326: /// deba@326: /// Increment operator. deba@326: BlossomIt& operator++() { deba@326: ++_index; deba@326: return *this; deba@326: } deba@326: deba@327: /// \brief Validity checking deba@327: /// deba@327: /// Checks whether the iterator is invalid. deba@327: bool operator==(Invalid) const { return _index == _last; } deba@327: deba@327: /// \brief Validity checking deba@327: /// deba@327: /// Checks whether the iterator is valid. deba@327: bool operator!=(Invalid) const { return _index != _last; } deba@326: deba@326: private: deba@326: const MaxWeightedMatching* _algorithm; deba@326: int _last; deba@326: int _index; deba@326: }; deba@326: deba@326: /// @} deba@326: deba@326: }; deba@326: deba@326: /// \ingroup matching deba@326: /// deba@326: /// \brief Weighted perfect matching in general graphs deba@326: /// deba@326: /// This class provides an efficient implementation of Edmond's deba@327: /// maximum weighted perfect matching algorithm. The implementation deba@326: /// is based on extensive use of priority queues and provides kpeter@559: /// \f$O(nm\log n)\f$ time complexity. deba@326: /// deba@326: /// The maximum weighted matching problem is to find undirected deba@327: /// edges in the graph with maximum overall weight and no two of deba@327: /// them shares their ends and covers all nodes. The problem can be deba@327: /// formulated with the following linear program. deba@326: /// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f] deba@327: /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} deba@327: \quad \forall B\in\mathcal{O}\f] */ deba@326: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@326: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@327: /// where \f$\delta(X)\f$ is the set of edges incident to a node in deba@327: /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in deba@327: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality deba@327: /// subsets of the nodes. deba@326: /// deba@326: /// The algorithm calculates an optimal matching and a proof of the deba@326: /// optimality. The solution of the dual problem can be used to check deba@327: /// the result of the algorithm. The dual linear problem is the deba@327: /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge deba@327: w_{uv} \quad \forall uv\in E\f] */ deba@326: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@327: /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}} deba@327: \frac{\vert B \vert - 1}{2}z_B\f] */ deba@326: /// deba@326: /// The algorithm can be executed with \c run() or the \c init() and deba@326: /// then the \c start() member functions. After it the matching can deba@326: /// be asked with \c matching() or mate() functions. The dual deba@326: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@326: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt deba@326: /// "BlossomIt" nested class which is able to iterate on the nodes deba@326: /// of a blossom. If the value type is integral then the dual deba@326: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". kpeter@559: template > deba@326: class MaxWeightedPerfectMatching { deba@326: public: deba@326: kpeter@559: typedef GR Graph; kpeter@559: typedef WM WeightMap; deba@326: typedef typename WeightMap::Value Value; deba@326: deba@326: /// \brief Scaling factor for dual solution deba@326: /// deba@326: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@326: /// according to the value type. deba@326: static const int dualScale = deba@326: std::numeric_limits::is_integer ? 4 : 1; deba@326: deba@326: typedef typename Graph::template NodeMap deba@326: MatchingMap; deba@326: deba@326: private: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@326: typedef typename Graph::template NodeMap NodePotential; deba@326: typedef std::vector BlossomNodeList; deba@326: deba@326: struct BlossomVariable { deba@326: int begin, end; deba@326: Value value; deba@326: deba@326: BlossomVariable(int _begin, int _end, Value _value) deba@326: : begin(_begin), end(_end), value(_value) {} deba@326: deba@326: }; deba@326: deba@326: typedef std::vector BlossomPotential; deba@326: deba@326: const Graph& _graph; deba@326: const WeightMap& _weight; deba@326: deba@326: MatchingMap* _matching; deba@326: deba@326: NodePotential* _node_potential; deba@326: deba@326: BlossomPotential _blossom_potential; deba@326: BlossomNodeList _blossom_node_list; deba@326: deba@326: int _node_num; deba@326: int _blossom_num; deba@326: deba@326: typedef RangeMap IntIntMap; deba@326: deba@326: enum Status { deba@326: EVEN = -1, MATCHED = 0, ODD = 1 deba@326: }; deba@326: deba@327: typedef HeapUnionFind BlossomSet; deba@326: struct BlossomData { deba@326: int tree; deba@326: Status status; deba@326: Arc pred, next; deba@326: Value pot, offset; deba@326: }; deba@326: deba@327: IntNodeMap *_blossom_index; deba@326: BlossomSet *_blossom_set; deba@326: RangeMap* _blossom_data; deba@326: deba@327: IntNodeMap *_node_index; deba@327: IntArcMap *_node_heap_index; deba@326: deba@326: struct NodeData { deba@326: deba@327: NodeData(IntArcMap& node_heap_index) deba@326: : heap(node_heap_index) {} deba@326: deba@326: int blossom; deba@326: Value pot; deba@327: BinHeap heap; deba@326: std::map heap_index; deba@326: deba@326: int tree; deba@326: }; deba@326: deba@326: RangeMap* _node_data; deba@326: deba@326: typedef ExtendFindEnum TreeSet; deba@326: deba@326: IntIntMap *_tree_set_index; deba@326: TreeSet *_tree_set; deba@326: deba@326: IntIntMap *_delta2_index; deba@326: BinHeap *_delta2; deba@326: deba@327: IntEdgeMap *_delta3_index; deba@327: BinHeap *_delta3; deba@326: deba@326: IntIntMap *_delta4_index; deba@326: BinHeap *_delta4; deba@326: deba@326: Value _delta_sum; deba@326: deba@326: void createStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (!_matching) { deba@326: _matching = new MatchingMap(_graph); deba@326: } deba@326: if (!_node_potential) { deba@326: _node_potential = new NodePotential(_graph); deba@326: } deba@326: if (!_blossom_set) { deba@327: _blossom_index = new IntNodeMap(_graph); deba@326: _blossom_set = new BlossomSet(*_blossom_index); deba@326: _blossom_data = new RangeMap(_blossom_num); deba@326: } deba@326: deba@326: if (!_node_index) { deba@327: _node_index = new IntNodeMap(_graph); deba@327: _node_heap_index = new IntArcMap(_graph); deba@326: _node_data = new RangeMap(_node_num, deba@327: NodeData(*_node_heap_index)); deba@326: } deba@326: deba@326: if (!_tree_set) { deba@326: _tree_set_index = new IntIntMap(_blossom_num); deba@326: _tree_set = new TreeSet(*_tree_set_index); deba@326: } deba@326: if (!_delta2) { deba@326: _delta2_index = new IntIntMap(_blossom_num); deba@326: _delta2 = new BinHeap(*_delta2_index); deba@326: } deba@326: if (!_delta3) { deba@327: _delta3_index = new IntEdgeMap(_graph); deba@327: _delta3 = new BinHeap(*_delta3_index); deba@326: } deba@326: if (!_delta4) { deba@326: _delta4_index = new IntIntMap(_blossom_num); deba@326: _delta4 = new BinHeap(*_delta4_index); deba@326: } deba@326: } deba@326: deba@326: void destroyStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (_matching) { deba@326: delete _matching; deba@326: } deba@326: if (_node_potential) { deba@326: delete _node_potential; deba@326: } deba@326: if (_blossom_set) { deba@326: delete _blossom_index; deba@326: delete _blossom_set; deba@326: delete _blossom_data; deba@326: } deba@326: deba@326: if (_node_index) { deba@326: delete _node_index; deba@326: delete _node_heap_index; deba@326: delete _node_data; deba@326: } deba@326: deba@326: if (_tree_set) { deba@326: delete _tree_set_index; deba@326: delete _tree_set; deba@326: } deba@326: if (_delta2) { deba@326: delete _delta2_index; deba@326: delete _delta2; deba@326: } deba@326: if (_delta3) { deba@326: delete _delta3_index; deba@326: delete _delta3; deba@326: } deba@326: if (_delta4) { deba@326: delete _delta4_index; deba@326: delete _delta4; deba@326: } deba@326: } deba@326: deba@326: void matchedToEven(int blossom, int tree) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else { deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset){ deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void matchedToOdd(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: (*_blossom_data)[blossom].offset += _delta_sum; deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: void evenToMatched(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: (*_node_data)[ni].pot -= _delta_sum; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: if (vt != tree) { deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > deba@326: _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: (*_node_data)[vi].heap.erase(it->second); deba@326: (*_node_data)[vi].heap_index.erase(it); deba@326: if ((*_node_data)[vi].heap.empty()) { deba@326: _blossom_set->increase(v, std::numeric_limits::max()); deba@326: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@326: } deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_blossom_set->classPrio(vb) == deba@326: std::numeric_limits::max()) { deba@326: _delta2->erase(vb); deba@326: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void oddToMatched(int blossom) { deba@326: (*_blossom_data)[blossom].offset -= _delta_sum; deba@326: deba@326: if (_blossom_set->classPrio(blossom) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: } deba@326: } deba@326: deba@326: void oddToEven(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: (*_node_data)[ni].pot += deba@326: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void alternatePath(int even, int tree) { deba@326: int odd; deba@326: deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: deba@326: while ((*_blossom_data)[even].pred != INVALID) { deba@326: odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred)); deba@326: (*_blossom_data)[odd].status = MATCHED; deba@326: oddToMatched(odd); deba@326: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@326: deba@326: even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred)); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].next = deba@326: _graph.oppositeArc((*_blossom_data)[odd].pred); deba@326: } deba@326: deba@326: } deba@326: deba@326: void destroyTree(int tree) { deba@326: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@326: if ((*_blossom_data)[b].status == EVEN) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: evenToMatched(b, tree); deba@326: } else if ((*_blossom_data)[b].status == ODD) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: oddToMatched(b); deba@326: } deba@326: } deba@326: _tree_set->eraseClass(tree); deba@326: } deba@326: deba@327: void augmentOnEdge(const Edge& edge) { deba@327: deba@327: int left = _blossom_set->find(_graph.u(edge)); deba@327: int right = _blossom_set->find(_graph.v(edge)); deba@326: deba@326: int left_tree = _tree_set->find(left); deba@326: alternatePath(left, left_tree); deba@326: destroyTree(left_tree); deba@326: deba@326: int right_tree = _tree_set->find(right); deba@326: alternatePath(right, right_tree); deba@326: destroyTree(right_tree); deba@326: deba@327: (*_blossom_data)[left].next = _graph.direct(edge, true); deba@327: (*_blossom_data)[right].next = _graph.direct(edge, false); deba@326: } deba@326: deba@326: void extendOnArc(const Arc& arc) { deba@326: int base = _blossom_set->find(_graph.target(arc)); deba@326: int tree = _tree_set->find(base); deba@326: deba@326: int odd = _blossom_set->find(_graph.source(arc)); deba@326: _tree_set->insert(odd, tree); deba@326: (*_blossom_data)[odd].status = ODD; deba@326: matchedToOdd(odd); deba@326: (*_blossom_data)[odd].pred = arc; deba@326: deba@326: int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next)); deba@326: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@326: _tree_set->insert(even, tree); deba@326: (*_blossom_data)[even].status = EVEN; deba@326: matchedToEven(even, tree); deba@326: } deba@326: deba@327: void shrinkOnEdge(const Edge& edge, int tree) { deba@326: int nca = -1; deba@326: std::vector left_path, right_path; deba@326: deba@326: { deba@326: std::set left_set, right_set; deba@326: int left = _blossom_set->find(_graph.u(edge)); deba@326: left_path.push_back(left); deba@326: left_set.insert(left); deba@326: deba@326: int right = _blossom_set->find(_graph.v(edge)); deba@326: right_path.push_back(right); deba@326: right_set.insert(right); deba@326: deba@326: while (true) { deba@326: deba@326: if ((*_blossom_data)[left].pred == INVALID) break; deba@326: deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: deba@326: left_set.insert(left); deba@326: deba@326: if (right_set.find(left) != right_set.end()) { deba@326: nca = left; deba@326: break; deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].pred == INVALID) break; deba@326: deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: deba@326: right_set.insert(right); deba@326: deba@326: if (left_set.find(right) != left_set.end()) { deba@326: nca = right; deba@326: break; deba@326: } deba@326: deba@326: } deba@326: deba@326: if (nca == -1) { deba@326: if ((*_blossom_data)[left].pred == INVALID) { deba@326: nca = right; deba@326: while (left_set.find(nca) == left_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: } deba@326: } else { deba@326: nca = left; deba@326: while (right_set.find(nca) == right_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: Arc prev; deba@326: deba@326: prev = _graph.direct(edge, true); deba@326: for (int i = 0; left_path[i] != nca; i += 2) { deba@326: subblossoms.push_back(left_path[i]); deba@326: (*_blossom_data)[left_path[i]].next = prev; deba@326: _tree_set->erase(left_path[i]); deba@326: deba@326: subblossoms.push_back(left_path[i + 1]); deba@326: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@326: oddToEven(left_path[i + 1], tree); deba@326: _tree_set->erase(left_path[i + 1]); deba@326: prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred); deba@326: } deba@326: deba@326: int k = 0; deba@326: while (right_path[k] != nca) ++k; deba@326: deba@326: subblossoms.push_back(nca); deba@326: (*_blossom_data)[nca].next = prev; deba@326: deba@326: for (int i = k - 2; i >= 0; i -= 2) { deba@326: subblossoms.push_back(right_path[i + 1]); deba@326: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@326: oddToEven(right_path[i + 1], tree); deba@326: _tree_set->erase(right_path[i + 1]); deba@326: deba@326: (*_blossom_data)[right_path[i + 1]].next = deba@326: (*_blossom_data)[right_path[i + 1]].pred; deba@326: deba@326: subblossoms.push_back(right_path[i]); deba@326: _tree_set->erase(right_path[i]); deba@326: } deba@326: deba@326: int surface = deba@326: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@326: deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@326: } deba@326: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@326: } deba@326: deba@326: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@326: (*_blossom_data)[surface].offset = 0; deba@326: (*_blossom_data)[surface].status = EVEN; deba@326: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@326: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@326: deba@326: _tree_set->insert(surface, tree); deba@326: _tree_set->erase(nca); deba@326: } deba@326: deba@326: void splitBlossom(int blossom) { deba@326: Arc next = (*_blossom_data)[blossom].next; deba@326: Arc pred = (*_blossom_data)[blossom].pred; deba@326: deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = MATCHED; deba@326: oddToMatched(blossom); deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: deba@326: Value offset = (*_blossom_data)[blossom].offset; deba@326: int b = _blossom_set->find(_graph.source(pred)); deba@326: int d = _blossom_set->find(_graph.source(next)); deba@326: deba@326: int ib = -1, id = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) ib = i; deba@326: if (subblossoms[i] == d) id = i; deba@326: deba@326: (*_blossom_data)[subblossoms[i]].offset = offset; deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@326: } deba@326: if (_blossom_set->classPrio(subblossoms[i]) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(subblossoms[i], deba@326: _blossom_set->classPrio(subblossoms[i]) - deba@326: (*_blossom_data)[subblossoms[i]].offset); deba@326: } deba@326: } deba@326: deba@326: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@326: for (int i = (id + 1) % subblossoms.size(); deba@326: i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].pred = pred; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: pred = (*_blossom_data)[ub].next; deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[id]].status = ODD; deba@326: matchedToOdd(subblossoms[id]); deba@326: _tree_set->insert(subblossoms[id], tree); deba@326: (*_blossom_data)[subblossoms[id]].next = next; deba@326: (*_blossom_data)[subblossoms[id]].pred = pred; deba@326: deba@326: } else { deba@326: deba@326: for (int i = (ib + 1) % subblossoms.size(); deba@326: i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].next = next; deba@326: (*_blossom_data)[sb].pred = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = deba@326: (*_blossom_data)[tb].next = deba@326: _graph.oppositeArc((*_blossom_data)[ub].next); deba@326: next = (*_blossom_data)[ub].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@326: matchedToOdd(subblossoms[ib]); deba@326: _tree_set->insert(subblossoms[ib], tree); deba@326: (*_blossom_data)[subblossoms[ib]].next = next; deba@326: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@326: } deba@326: _tree_set->erase(blossom); deba@326: } deba@326: deba@326: void extractBlossom(int blossom, const Node& base, const Arc& matching) { deba@326: if (_blossom_set->trivial(blossom)) { deba@326: int bi = (*_node_index)[base]; deba@326: Value pot = (*_node_data)[bi].pot; deba@326: kpeter@581: (*_matching)[base] = matching; deba@326: _blossom_node_list.push_back(base); kpeter@581: (*_node_potential)[base] = pot; deba@326: } else { deba@326: deba@326: Value pot = (*_blossom_data)[blossom].pot; deba@326: int bn = _blossom_node_list.size(); deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: int b = _blossom_set->find(base); deba@326: int ib = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) { ib = i; break; } deba@326: } deba@326: deba@326: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@326: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@326: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@326: deba@326: Arc m = (*_blossom_data)[tb].next; deba@326: extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m)); deba@326: extractBlossom(tb, _graph.source(m), m); deba@326: } deba@326: extractBlossom(subblossoms[ib], base, matching); deba@326: deba@326: int en = _blossom_node_list.size(); deba@326: deba@326: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@326: } deba@326: } deba@326: deba@326: void extractMatching() { deba@326: std::vector blossoms; deba@326: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@326: blossoms.push_back(c); deba@326: } deba@326: deba@326: for (int i = 0; i < int(blossoms.size()); ++i) { deba@326: deba@326: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@326: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@326: n != INVALID; ++n) { deba@326: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@326: } deba@326: deba@326: Arc matching = (*_blossom_data)[blossoms[i]].next; deba@326: Node base = _graph.source(matching); deba@326: extractBlossom(blossoms[i], base, matching); deba@326: } deba@326: } deba@326: deba@326: public: deba@326: deba@326: /// \brief Constructor deba@326: /// deba@326: /// Constructor. deba@326: MaxWeightedPerfectMatching(const Graph& graph, const WeightMap& weight) deba@326: : _graph(graph), _weight(weight), _matching(0), deba@326: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@326: _node_num(0), _blossom_num(0), deba@326: deba@326: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@326: _node_index(0), _node_heap_index(0), _node_data(0), deba@326: _tree_set_index(0), _tree_set(0), deba@326: deba@326: _delta2_index(0), _delta2(0), deba@326: _delta3_index(0), _delta3(0), deba@326: _delta4_index(0), _delta4(0), deba@326: deba@326: _delta_sum() {} deba@326: deba@326: ~MaxWeightedPerfectMatching() { deba@326: destroyStructures(); deba@326: } deba@326: deba@326: /// \name Execution control alpar@330: /// The simplest way to execute the algorithm is to use the deba@326: /// \c run() member function. deba@326: deba@326: ///@{ deba@326: deba@326: /// \brief Initialize the algorithm deba@326: /// deba@326: /// Initialize the algorithm deba@326: void init() { deba@326: createStructures(); deba@326: deba@326: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@581: (*_node_heap_index)[e] = BinHeap::PRE_HEAP; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { kpeter@581: (*_delta3_index)[e] = _delta3->PRE_HEAP; deba@326: } deba@326: for (int i = 0; i < _blossom_num; ++i) { kpeter@581: (*_delta2_index)[i] = _delta2->PRE_HEAP; kpeter@581: (*_delta4_index)[i] = _delta4->PRE_HEAP; deba@326: } deba@326: deba@326: int index = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: Value max = - std::numeric_limits::max(); deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: if (_graph.target(e) == n) continue; deba@326: if ((dualScale * _weight[e]) / 2 > max) { deba@326: max = (dualScale * _weight[e]) / 2; deba@326: } deba@326: } kpeter@581: (*_node_index)[n] = index; deba@326: (*_node_data)[index].pot = max; deba@326: int blossom = deba@326: _blossom_set->insert(n, std::numeric_limits::max()); deba@326: deba@326: _tree_set->insert(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = EVEN; deba@326: (*_blossom_data)[blossom].pred = INVALID; deba@326: (*_blossom_data)[blossom].next = INVALID; deba@326: (*_blossom_data)[blossom].pot = 0; deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: ++index; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: int si = (*_node_index)[_graph.u(e)]; deba@326: int ti = (*_node_index)[_graph.v(e)]; deba@326: if (_graph.u(e) != _graph.v(e)) { deba@326: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@326: dualScale * _weight[e]) / 2); deba@326: } deba@326: } deba@326: } deba@326: deba@326: /// \brief Starts the algorithm deba@326: /// deba@326: /// Starts the algorithm deba@326: bool start() { deba@326: enum OpType { deba@326: D2, D3, D4 deba@326: }; deba@326: deba@326: int unmatched = _node_num; deba@326: while (unmatched > 0) { deba@326: Value d2 = !_delta2->empty() ? deba@326: _delta2->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d3 = !_delta3->empty() ? deba@326: _delta3->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d4 = !_delta4->empty() ? deba@326: _delta4->prio() : std::numeric_limits::max(); deba@326: deba@326: _delta_sum = d2; OpType ot = D2; deba@326: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@326: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@326: deba@326: if (_delta_sum == std::numeric_limits::max()) { deba@326: return false; deba@326: } deba@326: deba@326: switch (ot) { deba@326: case D2: deba@326: { deba@326: int blossom = _delta2->top(); deba@326: Node n = _blossom_set->classTop(blossom); deba@326: Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@326: extendOnArc(e); deba@326: } deba@326: break; deba@326: case D3: deba@326: { deba@326: Edge e = _delta3->top(); deba@326: deba@326: int left_blossom = _blossom_set->find(_graph.u(e)); deba@326: int right_blossom = _blossom_set->find(_graph.v(e)); deba@326: deba@326: if (left_blossom == right_blossom) { deba@326: _delta3->pop(); deba@326: } else { deba@326: int left_tree = _tree_set->find(left_blossom); deba@326: int right_tree = _tree_set->find(right_blossom); deba@326: deba@326: if (left_tree == right_tree) { deba@327: shrinkOnEdge(e, left_tree); deba@326: } else { deba@327: augmentOnEdge(e); deba@326: unmatched -= 2; deba@326: } deba@326: } deba@326: } break; deba@326: case D4: deba@326: splitBlossom(_delta4->top()); deba@326: break; deba@326: } deba@326: } deba@326: extractMatching(); deba@326: return true; deba@326: } deba@326: deba@326: /// \brief Runs %MaxWeightedPerfectMatching algorithm. deba@326: /// deba@326: /// This method runs the %MaxWeightedPerfectMatching algorithm. deba@326: /// deba@326: /// \note mwm.run() is just a shortcut of the following code. deba@326: /// \code deba@326: /// mwm.init(); deba@326: /// mwm.start(); deba@326: /// \endcode deba@326: bool run() { deba@326: init(); deba@326: return start(); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Primal solution alpar@330: /// Functions to get the primal solution, ie. the matching. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the matching value. deba@326: /// deba@326: /// Returns the matching value. deba@326: Value matchingValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: if ((*_matching)[n] != INVALID) { deba@326: sum += _weight[(*_matching)[n]]; deba@326: } deba@326: } deba@326: return sum /= 2; deba@326: } deba@326: deba@327: /// \brief Returns true when the edge is in the matching. deba@326: /// deba@327: /// Returns true when the edge is in the matching. deba@327: bool matching(const Edge& edge) const { deba@327: return static_cast((*_matching)[_graph.u(edge)]) == edge; deba@326: } deba@326: deba@327: /// \brief Returns the incident matching edge. deba@326: /// deba@327: /// Returns the incident matching arc from given edge. deba@326: Arc matching(const Node& node) const { deba@326: return (*_matching)[node]; deba@326: } deba@326: deba@326: /// \brief Returns the mate of the node. deba@326: /// deba@326: /// Returns the adjancent node in a mathcing arc. deba@326: Node mate(const Node& node) const { deba@326: return _graph.target((*_matching)[node]); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Dual solution alpar@330: /// Functions to get the dual solution. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the value of the dual solution. deba@326: /// deba@326: /// Returns the value of the dual solution. It should be equal to deba@326: /// the primal value scaled by \ref dualScale "dual scale". deba@326: Value dualValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: sum += nodeValue(n); deba@326: } deba@326: for (int i = 0; i < blossomNum(); ++i) { deba@326: sum += blossomValue(i) * (blossomSize(i) / 2); deba@326: } deba@326: return sum; deba@326: } deba@326: deba@326: /// \brief Returns the value of the node. deba@326: /// deba@326: /// Returns the the value of the node. deba@326: Value nodeValue(const Node& n) const { deba@326: return (*_node_potential)[n]; deba@326: } deba@326: deba@326: /// \brief Returns the number of the blossoms in the basis. deba@326: /// deba@326: /// Returns the number of the blossoms in the basis. deba@326: /// \see BlossomIt deba@326: int blossomNum() const { deba@326: return _blossom_potential.size(); deba@326: } deba@326: deba@326: deba@326: /// \brief Returns the number of the nodes in the blossom. deba@326: /// deba@326: /// Returns the number of the nodes in the blossom. deba@326: int blossomSize(int k) const { deba@326: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@326: } deba@326: deba@326: /// \brief Returns the value of the blossom. deba@326: /// deba@326: /// Returns the the value of the blossom. deba@326: /// \see BlossomIt deba@326: Value blossomValue(int k) const { deba@326: return _blossom_potential[k].value; deba@326: } deba@326: alpar@330: /// \brief Iterator for obtaining the nodes of the blossom. deba@326: /// alpar@330: /// Iterator for obtaining the nodes of the blossom. This class alpar@330: /// provides a common lemon style iterator for listing a deba@326: /// subset of the nodes. deba@326: class BlossomIt { deba@326: public: deba@326: deba@326: /// \brief Constructor. deba@326: /// alpar@330: /// Constructor to get the nodes of the variable. deba@326: BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable) deba@326: : _algorithm(&algorithm) deba@326: { deba@326: _index = _algorithm->_blossom_potential[variable].begin; deba@326: _last = _algorithm->_blossom_potential[variable].end; deba@326: } deba@326: deba@326: /// \brief Conversion to node. deba@326: /// deba@326: /// Conversion to node. deba@326: operator Node() const { deba@327: return _algorithm->_blossom_node_list[_index]; deba@326: } deba@326: deba@326: /// \brief Increment operator. deba@326: /// deba@326: /// Increment operator. deba@326: BlossomIt& operator++() { deba@326: ++_index; deba@326: return *this; deba@326: } deba@326: deba@327: /// \brief Validity checking deba@327: /// deba@327: /// Checks whether the iterator is invalid. deba@327: bool operator==(Invalid) const { return _index == _last; } deba@327: deba@327: /// \brief Validity checking deba@327: /// deba@327: /// Checks whether the iterator is valid. deba@327: bool operator!=(Invalid) const { return _index != _last; } deba@326: deba@326: private: deba@326: const MaxWeightedPerfectMatching* _algorithm; deba@326: int _last; deba@326: int _index; deba@326: }; deba@326: deba@326: /// @} deba@326: deba@326: }; deba@326: deba@326: deba@326: } //END OF NAMESPACE LEMON deba@326: deba@326: #endif //LEMON_MAX_MATCHING_H