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