jacint@1077: /* -*- C++ -*- jacint@1077: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2553: * Copyright (C) 2003-2008 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). jacint@1077: * jacint@1077: * Permission to use, modify and distribute this software is granted jacint@1077: * provided that this copyright notice appears in all copies. For jacint@1077: * precise terms see the accompanying LICENSE file. jacint@1077: * jacint@1077: * This software is provided "AS IS" with no warranty of any kind, jacint@1077: * express or implied, and with no claim as to its suitability for any jacint@1077: * purpose. jacint@1077: * jacint@1077: */ jacint@1077: jacint@1077: #ifndef LEMON_MAX_MATCHING_H jacint@1077: #define LEMON_MAX_MATCHING_H jacint@1077: deba@2548: #include jacint@1077: #include deba@2548: #include deba@2548: deba@1993: #include jacint@1093: #include jacint@1077: #include deba@2548: #include jacint@1077: deba@2042: ///\ingroup matching jacint@1077: ///\file deba@2549: ///\brief Maximum matching algorithms in undirected graph. jacint@1077: jacint@1077: namespace lemon { jacint@1077: deba@2505: ///\ingroup matching deba@2548: /// deba@2505: ///\brief Edmonds' alternating forest maximum matching algorithm. deba@2505: /// jacint@1077: ///This class provides Edmonds' alternating forest matching jacint@1077: ///algorithm. The starting matching (if any) can be passed to the deba@2505: ///algorithm using some of init functions. jacint@1077: /// jacint@1077: ///The dual side of a matching is a map of the nodes to deba@2505: ///MaxMatching::DecompType, having values \c D, \c A and \c C deba@2505: ///showing the Gallai-Edmonds decomposition of the graph. The nodes deba@2505: ///in \c D induce a graph with factor-critical components, the nodes deba@2505: ///in \c A form the barrier, and the nodes in \c C induce a graph deba@2505: ///having a perfect matching. This decomposition can be attained by deba@2505: ///calling \c decomposition() after running the algorithm. jacint@1077: /// jacint@1077: ///\param Graph The undirected graph type the algorithm runs on. jacint@1077: /// jacint@1077: ///\author Jacint Szabo jacint@1077: template jacint@1077: class MaxMatching { jacint@1165: jacint@1165: protected: jacint@1165: jacint@1077: typedef typename Graph::Node Node; jacint@1077: typedef typename Graph::Edge Edge; klao@1909: typedef typename Graph::UEdge UEdge; klao@1909: typedef typename Graph::UEdgeIt UEdgeIt; jacint@1077: typedef typename Graph::NodeIt NodeIt; jacint@1077: typedef typename Graph::IncEdgeIt IncEdgeIt; jacint@1077: deba@2205: typedef typename Graph::template NodeMap UFECrossRef; deba@2308: typedef UnionFindEnum UFE; deba@2505: typedef std::vector NV; deba@2505: deba@2505: typedef typename Graph::template NodeMap EFECrossRef; deba@2505: typedef ExtendFindEnum EFE; jacint@1077: jacint@1077: public: jacint@1077: deba@2505: ///\brief Indicates the Gallai-Edmonds decomposition of the graph. deba@2505: /// jacint@1077: ///Indicates the Gallai-Edmonds decomposition of the graph, which jacint@1077: ///shows an upper bound on the size of a maximum matching. The deba@2505: ///nodes with DecompType \c D induce a graph with factor-critical jacint@1077: ///components, the nodes in \c A form the canonical barrier, and the jacint@1077: ///nodes in \c C induce a graph having a perfect matching. deba@2505: enum DecompType { jacint@1077: D=0, jacint@1077: A=1, jacint@1077: C=2 jacint@1077: }; jacint@1077: jacint@1165: protected: jacint@1077: jacint@1077: static const int HEUR_density=2; jacint@1077: const Graph& g; jacint@1093: typename Graph::template NodeMap _mate; deba@2505: typename Graph::template NodeMap position; jacint@1077: jacint@1077: public: jacint@1077: deba@2505: MaxMatching(const Graph& _g) deba@2505: : g(_g), _mate(_g), position(_g) {} jacint@1077: deba@2505: ///\brief Sets the actual matching to the empty matching. deba@2505: /// deba@2505: ///Sets the actual matching to the empty matching. deba@2505: /// deba@2505: void init() { alpar@1587: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: _mate.set(v,INVALID); deba@2505: position.set(v,C); alpar@1587: } alpar@1587: } alpar@1587: deba@2505: ///\brief Finds a greedy matching for initial matching. deba@2505: /// deba@2505: ///For initial matchig it finds a maximal greedy matching. deba@2505: void greedyInit() { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: _mate.set(v,INVALID); deba@2505: position.set(v,C); deba@2505: } alpar@1587: for(NodeIt v(g); v!=INVALID; ++v) alpar@1587: if ( _mate[v]==INVALID ) { alpar@1587: for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) { alpar@1587: Node y=g.runningNode(e); alpar@1587: if ( _mate[y]==INVALID && y!=v ) { alpar@1587: _mate.set(v,y); alpar@1587: _mate.set(y,v); alpar@1587: break; alpar@1587: } alpar@1587: } alpar@1587: } alpar@1587: } jacint@1077: deba@2505: ///\brief Initialize the matching from each nodes' mate. deba@2505: /// deba@2505: ///Initialize the matching from a \c Node valued \c Node map. This deba@2505: ///map must be \e symmetric, i.e. if \c map[u]==v then \c deba@2505: ///map[v]==u must hold, and \c uv will be an edge of the initial deba@2505: ///matching. deba@2505: template deba@2505: void mateMapInit(MateMap& map) { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: _mate.set(v,map[v]); deba@2505: position.set(v,C); deba@2505: } deba@2505: } jacint@1077: deba@2505: ///\brief Initialize the matching from a node map with the deba@2505: ///incident matching edges. deba@2505: /// deba@2505: ///Initialize the matching from an \c UEdge valued \c Node map. \c deba@2505: ///map[v] must be an \c UEdge incident to \c v. This map must have deba@2505: ///the property that if \c g.oppositeNode(u,map[u])==v then \c \c deba@2505: ///g.oppositeNode(v,map[v])==u holds, and now some edge joining \c deba@2505: ///u to \c v will be an edge of the matching. deba@2505: template deba@2505: void matchingMapInit(MatchingMap& map) { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: position.set(v,C); deba@2505: UEdge e=map[v]; deba@2505: if ( e!=INVALID ) deba@2505: _mate.set(v,g.oppositeNode(v,e)); deba@2505: else deba@2505: _mate.set(v,INVALID); deba@2505: } deba@2505: } deba@2505: deba@2505: ///\brief Initialize the matching from the map containing the deba@2505: ///undirected matching edges. deba@2505: /// deba@2505: ///Initialize the matching from a \c bool valued \c UEdge map. This deba@2505: ///map must have the property that there are no two incident edges deba@2505: ///\c e, \c f with \c map[e]==map[f]==true. The edges \c e with \c deba@2505: ///map[e]==true form the matching. deba@2505: template deba@2505: void matchingInit(MatchingMap& map) { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: _mate.set(v,INVALID); deba@2505: position.set(v,C); deba@2505: } deba@2505: for(UEdgeIt e(g); e!=INVALID; ++e) { deba@2505: if ( map[e] ) { deba@2505: Node u=g.source(e); deba@2505: Node v=g.target(e); deba@2505: _mate.set(u,v); deba@2505: _mate.set(v,u); deba@2505: } deba@2505: } deba@2505: } deba@2505: deba@2505: deba@2505: ///\brief Runs Edmonds' algorithm. deba@2505: /// deba@2505: ///Runs Edmonds' algorithm for sparse graphs (number of edges < deba@2505: ///2*number of nodes), and a heuristical Edmonds' algorithm with a deba@2505: ///heuristic of postponing shrinks for dense graphs. deba@2505: void run() { deba@2505: if (countUEdges(g) < HEUR_density * countNodes(g)) { deba@2505: greedyInit(); deba@2505: startSparse(); deba@2505: } else { deba@2505: init(); deba@2505: startDense(); deba@2505: } deba@2505: } deba@2505: deba@2505: deba@2505: ///\brief Starts Edmonds' algorithm. deba@2505: /// deba@2505: ///If runs the original Edmonds' algorithm. deba@2505: void startSparse() { deba@2505: deba@2505: typename Graph::template NodeMap ear(g,INVALID); deba@2505: //undefined for the base nodes of the blossoms (i.e. for the deba@2505: //representative elements of UFE blossom) and for the nodes in C deba@2505: deba@2505: UFECrossRef blossom_base(g); deba@2505: UFE blossom(blossom_base); deba@2505: NV rep(countNodes(g)); deba@2505: deba@2505: EFECrossRef tree_base(g); deba@2505: EFE tree(tree_base); deba@2505: deba@2505: //If these UFE's would be members of the class then also deba@2505: //blossom_base and tree_base should be a member. deba@2505: deba@2505: //We build only one tree and the other vertices uncovered by the deba@2505: //matching belong to C. (They can be considered as singleton deba@2505: //trees.) If this tree can be augmented or no more deba@2505: //grow/augmentation/shrink is possible then we return to this deba@2505: //"for" cycle. deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: if (position[v]==C && _mate[v]==INVALID) { deba@2505: rep[blossom.insert(v)] = v; deba@2505: tree.insert(v); deba@2505: position.set(v,D); deba@2505: normShrink(v, ear, blossom, rep, tree); deba@2505: } deba@2505: } deba@2505: } deba@2505: deba@2505: ///\brief Starts Edmonds' algorithm. deba@2505: /// deba@2505: ///It runs Edmonds' algorithm with a heuristic of postponing deba@2505: ///shrinks, giving a faster algorithm for dense graphs. deba@2505: void startDense() { deba@2505: deba@2505: typename Graph::template NodeMap ear(g,INVALID); deba@2505: //undefined for the base nodes of the blossoms (i.e. for the deba@2505: //representative elements of UFE blossom) and for the nodes in C deba@2505: deba@2505: UFECrossRef blossom_base(g); deba@2505: UFE blossom(blossom_base); deba@2505: NV rep(countNodes(g)); deba@2505: deba@2505: EFECrossRef tree_base(g); deba@2505: EFE tree(tree_base); deba@2505: deba@2505: //If these UFE's would be members of the class then also deba@2505: //blossom_base and tree_base should be a member. deba@2505: deba@2505: //We build only one tree and the other vertices uncovered by the deba@2505: //matching belong to C. (They can be considered as singleton deba@2505: //trees.) If this tree can be augmented or no more deba@2505: //grow/augmentation/shrink is possible then we return to this deba@2505: //"for" cycle. deba@2505: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: if ( position[v]==C && _mate[v]==INVALID ) { deba@2505: rep[blossom.insert(v)] = v; deba@2505: tree.insert(v); deba@2505: position.set(v,D); deba@2505: lateShrink(v, ear, blossom, rep, tree); deba@2505: } deba@2505: } deba@2505: } deba@2505: deba@2505: deba@2505: deba@2505: ///\brief Returns the size of the actual matching stored. deba@2505: /// jacint@1077: ///Returns the size of the actual matching stored. After \ref jacint@1077: ///run() it returns the size of a maximum matching in the graph. alpar@1587: int size() const { alpar@1587: int s=0; alpar@1587: for(NodeIt v(g); v!=INVALID; ++v) { alpar@1587: if ( _mate[v]!=INVALID ) { alpar@1587: ++s; alpar@1587: } alpar@1587: } alpar@1587: return s/2; alpar@1587: } alpar@1587: jacint@1077: deba@2505: ///\brief Returns the mate of a node in the actual matching. jacint@1077: /// jacint@1093: ///Returns the mate of a \c node in the actual matching. jacint@1093: ///Returns INVALID if the \c node is not covered by the actual matching. deba@2505: Node mate(const Node& node) const { jacint@1093: return _mate[node]; jacint@1093: } jacint@1093: deba@2505: ///\brief Returns the matching edge incident to the given node. deba@2505: /// deba@2505: ///Returns the matching edge of a \c node in the actual matching. deba@2505: ///Returns INVALID if the \c node is not covered by the actual matching. deba@2505: UEdge matchingEdge(const Node& node) const { deba@2505: if (_mate[node] == INVALID) return INVALID; deba@2505: Node n = node < _mate[node] ? node : _mate[node]; deba@2505: for (IncEdgeIt e(g, n); e != INVALID; ++e) { deba@2505: if (g.oppositeNode(n, e) == _mate[n]) { deba@2505: return e; deba@2505: } deba@2505: } deba@2505: return INVALID; deba@2505: } jacint@1077: deba@2505: /// \brief Returns the class of the node in the Edmonds-Gallai deba@2505: /// decomposition. deba@2505: /// deba@2505: /// Returns the class of the node in the Edmonds-Gallai deba@2505: /// decomposition. deba@2505: DecompType decomposition(const Node& n) { deba@2505: return position[n] == A; deba@2505: } deba@2505: deba@2505: /// \brief Returns true when the node is in the barrier. deba@2505: /// deba@2505: /// Returns true when the node is in the barrier. deba@2505: bool barrier(const Node& n) { deba@2505: return position[n] == A; deba@2505: } jacint@1077: deba@2505: ///\brief Gives back the matching in a \c Node of mates. deba@2505: /// jacint@1165: ///Writes the stored matching to a \c Node valued \c Node map. The jacint@1077: ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c jacint@1077: ///map[v]==u will hold, and now \c uv is an edge of the matching. deba@2505: template deba@2505: void mateMap(MateMap& map) const { jacint@1077: for(NodeIt v(g); v!=INVALID; ++v) { jacint@1093: map.set(v,_mate[v]); jacint@1077: } jacint@1077: } jacint@1077: deba@2505: ///\brief Gives back the matching in an \c UEdge valued \c Node deba@2505: ///map. deba@2505: /// klao@1909: ///Writes the stored matching to an \c UEdge valued \c Node klao@1909: ///map. \c map[v] will be an \c UEdge incident to \c v. This jacint@1165: ///map will have the property that if \c g.oppositeNode(u,map[u]) jacint@1165: ///== v then \c map[u]==map[v] holds, and now this edge is an edge jacint@1165: ///of the matching. deba@2505: template deba@2505: void matchingMap(MatchingMap& map) const { jacint@1077: typename Graph::template NodeMap todo(g,true); jacint@1077: for(NodeIt v(g); v!=INVALID; ++v) { deba@2505: if (_mate[v]!=INVALID && v < _mate[v]) { jacint@1093: Node u=_mate[v]; jacint@1077: for(IncEdgeIt e(g,v); e!=INVALID; ++e) { klao@1158: if ( g.runningNode(e) == u ) { jacint@1077: map.set(u,e); jacint@1077: map.set(v,e); jacint@1077: todo.set(u,false); jacint@1077: todo.set(v,false); jacint@1077: break; jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: jacint@1077: deba@2505: ///\brief Gives back the matching in a \c bool valued \c UEdge deba@2505: ///map. deba@2505: /// jacint@1165: ///Writes the matching stored to a \c bool valued \c Edge jacint@1165: ///map. This map will have the property that there are no two jacint@1165: ///incident edges \c e, \c f with \c map[e]==map[f]==true. The jacint@1165: ///edges \c e with \c map[e]==true form the matching. deba@2505: template deba@2505: void matching(MatchingMap& map) const { klao@1909: for(UEdgeIt e(g); e!=INVALID; ++e) map.set(e,false); jacint@1077: jacint@1077: typename Graph::template NodeMap todo(g,true); jacint@1077: for(NodeIt v(g); v!=INVALID; ++v) { jacint@1093: if ( todo[v] && _mate[v]!=INVALID ) { jacint@1093: Node u=_mate[v]; jacint@1077: for(IncEdgeIt e(g,v); e!=INVALID; ++e) { klao@1158: if ( g.runningNode(e) == u ) { jacint@1077: map.set(e,true); jacint@1077: todo.set(u,false); jacint@1077: todo.set(v,false); jacint@1077: break; jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: } jacint@1077: jacint@1077: deba@2505: ///\brief Returns the canonical decomposition of the graph after running jacint@1077: ///the algorithm. deba@2505: /// jacint@1090: ///After calling any run methods of the class, it writes the jacint@1090: ///Gallai-Edmonds canonical decomposition of the graph. \c map deba@2505: ///must be a node map of \ref DecompType 's. deba@2505: template deba@2505: void decomposition(DecompositionMap& map) const { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]); deba@2505: } deba@2505: deba@2505: ///\brief Returns a barrier on the nodes. deba@2505: /// deba@2505: ///After calling any run methods of the class, it writes a deba@2505: ///canonical barrier on the nodes. The odd component number of the deba@2505: ///remaining graph minus the barrier size is a lower bound for the deba@2505: ///uncovered nodes in the graph. The \c map must be a node map of deba@2505: ///bools. deba@2505: template deba@2505: void barrier(BarrierMap& barrier) { deba@2505: for(NodeIt v(g); v!=INVALID; ++v) barrier.set(v,position[v] == A); jacint@1077: } jacint@1077: jacint@1077: private: jacint@1077: jacint@1165: jacint@1077: void lateShrink(Node v, typename Graph::template NodeMap& ear, deba@2505: UFE& blossom, NV& rep, EFE& tree) { deba@2505: //We have one tree which we grow, and also shrink but only if it deba@2505: //cannot be postponed. If we augment then we return to the "for" deba@2505: //cycle of runEdmonds(). deba@2505: deba@2505: std::queue Q; //queue of the totally unscanned nodes deba@2505: Q.push(v); deba@2505: std::queue R; deba@2505: //queue of the nodes which must be scanned for a possible shrink deba@2505: deba@2505: while ( !Q.empty() ) { deba@2505: Node x=Q.front(); deba@2505: Q.pop(); deba@2505: for( IncEdgeIt e(g,x); e!= INVALID; ++e ) { deba@2505: Node y=g.runningNode(e); deba@2505: //growOrAugment grows if y is covered by the matching and deba@2505: //augments if not. In this latter case it returns 1. deba@2505: if (position[y]==C && deba@2505: growOrAugment(y, x, ear, blossom, rep, tree, Q)) return; deba@2505: } deba@2505: R.push(x); deba@2505: } deba@2505: deba@2505: while ( !R.empty() ) { deba@2505: Node x=R.front(); deba@2505: R.pop(); deba@2505: deba@2505: for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) { deba@2505: Node y=g.runningNode(e); deba@2505: deba@2505: if ( position[y] == D && blossom.find(x) != blossom.find(y) ) deba@2505: //Recall that we have only one tree. deba@2505: shrink( x, y, ear, blossom, rep, tree, Q); deba@2505: deba@2505: while ( !Q.empty() ) { deba@2505: Node z=Q.front(); deba@2505: Q.pop(); deba@2505: for( IncEdgeIt f(g,z); f!= INVALID; ++f ) { deba@2505: Node w=g.runningNode(f); deba@2505: //growOrAugment grows if y is covered by the matching and deba@2505: //augments if not. In this latter case it returns 1. deba@2505: if (position[w]==C && deba@2505: growOrAugment(w, z, ear, blossom, rep, tree, Q)) return; deba@2505: } deba@2505: R.push(z); deba@2505: } deba@2505: } //for e deba@2505: } // while ( !R.empty() ) deba@2505: } jacint@1077: alpar@1234: void normShrink(Node v, typename Graph::template NodeMap& ear, deba@2505: UFE& blossom, NV& rep, EFE& tree) { deba@2505: //We have one tree, which we grow and shrink. If we augment then we deba@2505: //return to the "for" cycle of runEdmonds(). deba@2505: deba@2505: std::queue Q; //queue of the unscanned nodes deba@2505: Q.push(v); deba@2505: while ( !Q.empty() ) { deba@2505: deba@2505: Node x=Q.front(); deba@2505: Q.pop(); deba@2505: deba@2505: for( IncEdgeIt e(g,x); e!=INVALID; ++e ) { deba@2505: Node y=g.runningNode(e); deba@2505: deba@2505: switch ( position[y] ) { deba@2505: case D: //x and y must be in the same tree deba@2505: if ( blossom.find(x) != blossom.find(y)) deba@2505: //x and y are in the same tree deba@2505: shrink(x, y, ear, blossom, rep, tree, Q); deba@2505: break; deba@2505: case C: deba@2505: //growOrAugment grows if y is covered by the matching and deba@2505: //augments if not. In this latter case it returns 1. deba@2505: if (growOrAugment(y, x, ear, blossom, rep, tree, Q)) return; deba@2505: break; deba@2505: default: break; deba@2505: } deba@2505: } deba@2505: } deba@2505: } jacint@1077: jacint@2023: void shrink(Node x,Node y, typename Graph::template NodeMap& ear, deba@2505: UFE& blossom, NV& rep, EFE& tree,std::queue& Q) { deba@2505: //x and y are the two adjacent vertices in two blossoms. deba@2505: deba@2505: typename Graph::template NodeMap path(g,false); deba@2505: deba@2505: Node b=rep[blossom.find(x)]; deba@2505: path.set(b,true); deba@2505: b=_mate[b]; deba@2505: while ( b!=INVALID ) { deba@2505: b=rep[blossom.find(ear[b])]; deba@2505: path.set(b,true); deba@2505: b=_mate[b]; deba@2505: } //we go until the root through bases of blossoms and odd vertices deba@2505: deba@2505: Node top=y; deba@2505: Node middle=rep[blossom.find(top)]; deba@2505: Node bottom=x; deba@2505: while ( !path[middle] ) deba@2505: shrinkStep(top, middle, bottom, ear, blossom, rep, tree, Q); deba@2505: //Until we arrive to a node on the path, we update blossom, tree deba@2505: //and the positions of the odd nodes. deba@2505: deba@2505: Node base=middle; deba@2505: top=x; deba@2505: middle=rep[blossom.find(top)]; deba@2505: bottom=y; deba@2505: Node blossom_base=rep[blossom.find(base)]; deba@2505: while ( middle!=blossom_base ) deba@2505: shrinkStep(top, middle, bottom, ear, blossom, rep, tree, Q); deba@2505: //Until we arrive to a node on the path, we update blossom, tree deba@2505: //and the positions of the odd nodes. deba@2505: deba@2505: rep[blossom.find(base)] = base; deba@2505: } jacint@1077: alpar@1234: void shrinkStep(Node& top, Node& middle, Node& bottom, alpar@1234: typename Graph::template NodeMap& ear, deba@2505: UFE& blossom, NV& rep, EFE& tree, std::queue& Q) { deba@2505: //We traverse a blossom and update everything. deba@2505: deba@2505: ear.set(top,bottom); deba@2505: Node t=top; deba@2505: while ( t!=middle ) { deba@2505: Node u=_mate[t]; deba@2505: t=ear[u]; deba@2505: ear.set(t,u); deba@2505: } deba@2505: bottom=_mate[middle]; deba@2505: position.set(bottom,D); deba@2505: Q.push(bottom); deba@2505: top=ear[bottom]; deba@2505: Node oldmiddle=middle; deba@2505: middle=rep[blossom.find(top)]; deba@2505: tree.erase(bottom); deba@2505: tree.erase(oldmiddle); deba@2505: blossom.insert(bottom); deba@2505: blossom.join(bottom, oldmiddle); deba@2505: blossom.join(top, oldmiddle); deba@2505: } deba@2505: deba@2505: jacint@1077: jacint@2023: bool growOrAugment(Node& y, Node& x, typename Graph::template deba@2505: NodeMap& ear, UFE& blossom, NV& rep, EFE& tree, deba@2505: std::queue& Q) { deba@2505: //x is in a blossom in the tree, y is outside. If y is covered by deba@2505: //the matching we grow, otherwise we augment. In this case we deba@2505: //return 1. deba@2505: deba@2505: if ( _mate[y]!=INVALID ) { //grow deba@2505: ear.set(y,x); deba@2505: Node w=_mate[y]; deba@2505: rep[blossom.insert(w)] = w; deba@2505: position.set(y,A); deba@2505: position.set(w,D); deba@2505: int t = tree.find(rep[blossom.find(x)]); deba@2505: tree.insert(y,t); deba@2505: tree.insert(w,t); deba@2505: Q.push(w); deba@2505: } else { //augment deba@2505: augment(x, ear, blossom, rep, tree); deba@2505: _mate.set(x,y); deba@2505: _mate.set(y,x); deba@2505: return true; deba@2505: } deba@2505: return false; deba@2505: } jacint@2023: alpar@1234: void augment(Node x, typename Graph::template NodeMap& ear, deba@2505: UFE& blossom, NV& rep, EFE& tree) { deba@2505: Node v=_mate[x]; deba@2505: while ( v!=INVALID ) { deba@2505: deba@2505: Node u=ear[v]; deba@2505: _mate.set(v,u); deba@2505: Node tmp=v; deba@2505: v=_mate[u]; deba@2505: _mate.set(u,tmp); deba@2505: } deba@2505: int y = tree.find(rep[blossom.find(x)]); deba@2505: for (typename EFE::ItemIt tit(tree, y); tit != INVALID; ++tit) { deba@2505: if ( position[tit] == D ) { deba@2505: int b = blossom.find(tit); deba@2505: for (typename UFE::ItemIt bit(blossom, b); bit != INVALID; ++bit) { deba@2505: position.set(bit, C); deba@2505: } deba@2505: blossom.eraseClass(b); deba@2505: } else position.set(tit, C); deba@2505: } deba@2505: tree.eraseClass(y); deba@2505: deba@2505: } jacint@1077: jacint@1077: }; deba@2548: deba@2548: /// \ingroup matching deba@2548: /// deba@2548: /// \brief Weighted matching in general undirected graphs deba@2548: /// deba@2548: /// This class provides an efficient implementation of Edmond's deba@2548: /// maximum weighted matching algorithm. The implementation is based deba@2548: /// on extensive use of priority queues and provides deba@2548: /// \f$O(nm\log(n))\f$ time complexity. deba@2548: /// deba@2548: /// The maximum weighted matching problem is to find undirected deba@2548: /// edges in the graph with maximum overall weight and no two of deba@2548: /// them shares their endpoints. The problem can be formulated with deba@2548: /// the next linear program: deba@2548: /// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f] deba@2548: ///\f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} \quad \forall B\in\mathcal{O}\f] deba@2548: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@2548: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@2548: /// where \f$\delta(X)\f$ is the set of edges incident to a node in deba@2548: /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both endpoints in deba@2548: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality subsets of deba@2548: /// the nodes. deba@2548: /// deba@2548: /// The algorithm calculates an optimal matching and a proof of the deba@2548: /// optimality. The solution of the dual problem can be used to check deba@2548: /// the result of the algorithm. The dual linear problem is the next: deba@2548: /// \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge w_{uv} \quad \forall uv\in E\f] deba@2548: /// \f[y_u \ge 0 \quad \forall u \in V\f] deba@2548: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@2548: /// \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}\frac{\vert B \vert - 1}{2}z_B\f] deba@2548: /// deba@2548: /// The algorithm can be executed with \c run() or the \c init() and deba@2548: /// then the \c start() member functions. After it the matching can deba@2548: /// be asked with \c matching() or mate() functions. The dual deba@2548: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@2548: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt deba@2548: /// "BlossomIt" nested class which is able to iterate on the nodes deba@2548: /// of a blossom. If the value type is integral then the dual deba@2548: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". deba@2548: /// deba@2548: /// \author Balazs Dezso deba@2548: template > deba@2548: class MaxWeightedMatching { deba@2548: public: deba@2548: deba@2548: typedef _UGraph UGraph; deba@2548: typedef _WeightMap WeightMap; deba@2548: typedef typename WeightMap::Value Value; deba@2548: deba@2548: /// \brief Scaling factor for dual solution deba@2548: /// deba@2548: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@2548: /// according to the value type. deba@2548: static const int dualScale = deba@2548: std::numeric_limits::is_integer ? 4 : 1; deba@2548: deba@2548: typedef typename UGraph::template NodeMap deba@2548: MatchingMap; deba@2548: deba@2548: private: deba@2548: deba@2548: UGRAPH_TYPEDEFS(typename UGraph); deba@2548: deba@2548: typedef typename UGraph::template NodeMap NodePotential; deba@2548: typedef std::vector BlossomNodeList; deba@2548: deba@2548: struct BlossomVariable { deba@2548: int begin, end; deba@2548: Value value; deba@2548: deba@2548: BlossomVariable(int _begin, int _end, Value _value) deba@2548: : begin(_begin), end(_end), value(_value) {} deba@2548: deba@2548: }; deba@2548: deba@2548: typedef std::vector BlossomPotential; deba@2548: deba@2548: const UGraph& _ugraph; deba@2548: const WeightMap& _weight; deba@2548: deba@2548: MatchingMap* _matching; deba@2548: deba@2548: NodePotential* _node_potential; deba@2548: deba@2548: BlossomPotential _blossom_potential; deba@2548: BlossomNodeList _blossom_node_list; deba@2548: deba@2548: int _node_num; deba@2548: int _blossom_num; deba@2548: deba@2548: typedef typename UGraph::template NodeMap NodeIntMap; deba@2548: typedef typename UGraph::template EdgeMap EdgeIntMap; deba@2548: typedef typename UGraph::template UEdgeMap UEdgeIntMap; deba@2548: typedef IntegerMap IntIntMap; deba@2548: deba@2548: enum Status { deba@2548: EVEN = -1, MATCHED = 0, ODD = 1, UNMATCHED = -2 deba@2548: }; deba@2548: deba@2548: typedef HeapUnionFind BlossomSet; deba@2548: struct BlossomData { deba@2548: int tree; deba@2548: Status status; deba@2548: Edge pred, next; deba@2548: Value pot, offset; deba@2548: Node base; deba@2548: }; deba@2548: deba@2548: NodeIntMap *_blossom_index; deba@2548: BlossomSet *_blossom_set; deba@2548: IntegerMap* _blossom_data; deba@2548: deba@2548: NodeIntMap *_node_index; deba@2548: EdgeIntMap *_node_heap_index; deba@2548: deba@2548: struct NodeData { deba@2548: deba@2548: NodeData(EdgeIntMap& node_heap_index) deba@2548: : heap(node_heap_index) {} deba@2548: deba@2548: int blossom; deba@2548: Value pot; deba@2548: BinHeap heap; deba@2548: std::map heap_index; deba@2548: deba@2548: int tree; deba@2548: }; deba@2548: deba@2548: IntegerMap* _node_data; deba@2548: deba@2548: typedef ExtendFindEnum TreeSet; deba@2548: deba@2548: IntIntMap *_tree_set_index; deba@2548: TreeSet *_tree_set; deba@2548: deba@2548: NodeIntMap *_delta1_index; deba@2548: BinHeap *_delta1; deba@2548: deba@2548: IntIntMap *_delta2_index; deba@2548: BinHeap *_delta2; deba@2548: deba@2548: UEdgeIntMap *_delta3_index; deba@2548: BinHeap *_delta3; deba@2548: deba@2548: IntIntMap *_delta4_index; deba@2548: BinHeap *_delta4; deba@2548: deba@2548: Value _delta_sum; deba@2548: deba@2548: void createStructures() { deba@2548: _node_num = countNodes(_ugraph); deba@2548: _blossom_num = _node_num * 3 / 2; deba@2548: deba@2548: if (!_matching) { deba@2548: _matching = new MatchingMap(_ugraph); deba@2548: } deba@2548: if (!_node_potential) { deba@2548: _node_potential = new NodePotential(_ugraph); deba@2548: } deba@2548: if (!_blossom_set) { deba@2548: _blossom_index = new NodeIntMap(_ugraph); deba@2548: _blossom_set = new BlossomSet(*_blossom_index); deba@2548: _blossom_data = new IntegerMap(_blossom_num); deba@2548: } deba@2548: deba@2548: if (!_node_index) { deba@2548: _node_index = new NodeIntMap(_ugraph); deba@2548: _node_heap_index = new EdgeIntMap(_ugraph); deba@2548: _node_data = new IntegerMap(_node_num, deba@2548: NodeData(*_node_heap_index)); deba@2548: } deba@2548: deba@2548: if (!_tree_set) { deba@2548: _tree_set_index = new IntIntMap(_blossom_num); deba@2548: _tree_set = new TreeSet(*_tree_set_index); deba@2548: } deba@2548: if (!_delta1) { deba@2548: _delta1_index = new NodeIntMap(_ugraph); deba@2548: _delta1 = new BinHeap(*_delta1_index); deba@2548: } deba@2548: if (!_delta2) { deba@2548: _delta2_index = new IntIntMap(_blossom_num); deba@2548: _delta2 = new BinHeap(*_delta2_index); deba@2548: } deba@2548: if (!_delta3) { deba@2548: _delta3_index = new UEdgeIntMap(_ugraph); deba@2548: _delta3 = new BinHeap(*_delta3_index); deba@2548: } deba@2548: if (!_delta4) { deba@2548: _delta4_index = new IntIntMap(_blossom_num); deba@2548: _delta4 = new BinHeap(*_delta4_index); deba@2548: } deba@2548: } deba@2548: deba@2548: void destroyStructures() { deba@2548: _node_num = countNodes(_ugraph); deba@2548: _blossom_num = _node_num * 3 / 2; deba@2548: deba@2548: if (_matching) { deba@2548: delete _matching; deba@2548: } deba@2548: if (_node_potential) { deba@2548: delete _node_potential; deba@2548: } deba@2548: if (_blossom_set) { deba@2548: delete _blossom_index; deba@2548: delete _blossom_set; deba@2548: delete _blossom_data; deba@2548: } deba@2548: deba@2548: if (_node_index) { deba@2548: delete _node_index; deba@2548: delete _node_heap_index; deba@2548: delete _node_data; deba@2548: } deba@2548: deba@2548: if (_tree_set) { deba@2548: delete _tree_set_index; deba@2548: delete _tree_set; deba@2548: } deba@2548: if (_delta1) { deba@2548: delete _delta1_index; deba@2548: delete _delta1; deba@2548: } deba@2548: if (_delta2) { deba@2548: delete _delta2_index; deba@2548: delete _delta2; deba@2548: } deba@2548: if (_delta3) { deba@2548: delete _delta3_index; deba@2548: delete _delta3; deba@2548: } deba@2548: if (_delta4) { deba@2548: delete _delta4_index; deba@2548: delete _delta4; deba@2548: } deba@2548: } deba@2548: deba@2548: void matchedToEven(int blossom, int tree) { deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: (*_blossom_data)[blossom].pot -= deba@2548: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: deba@2548: _blossom_set->increase(n, std::numeric_limits::max()); deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: (*_node_data)[ni].heap.clear(); deba@2548: (*_node_data)[ni].heap_index.clear(); deba@2548: deba@2548: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@2548: deba@2548: _delta1->push(n, (*_node_data)[ni].pot); deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if ((*_blossom_data)[vb].status == EVEN) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@2548: _delta3->push(e, rw / 2); deba@2548: } deba@2548: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@2548: _delta3->push(e, rw); deba@2548: } deba@2548: } else { deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: if ((*_node_data)[vi].heap[it->second] > rw) { deba@2548: (*_node_data)[vi].heap.replace(it->second, e); deba@2548: (*_node_data)[vi].heap.decrease(e, rw); deba@2548: it->second = e; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[vi].heap.push(e, rw); deba@2548: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@2548: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset){ deba@2548: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: } deba@2548: deba@2548: void matchedToOdd(int blossom) { deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: (*_blossom_data)[blossom].offset += _delta_sum; deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: } deba@2548: deba@2548: void evenToMatched(int blossom, int tree) { deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: (*_node_data)[ni].pot -= _delta_sum; deba@2548: deba@2548: _delta1->erase(n); deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if (vb == blossom) { deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: } else if ((*_blossom_data)[vb].status == EVEN) { deba@2548: deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: deba@2548: int vt = _tree_set->find(vb); deba@2548: deba@2548: if (vt != tree) { deba@2548: deba@2548: Edge r = _ugraph.oppositeEdge(e); deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[ni].heap_index.find(vt); deba@2548: deba@2548: if (it != (*_node_data)[ni].heap_index.end()) { deba@2548: if ((*_node_data)[ni].heap[it->second] > rw) { deba@2548: (*_node_data)[ni].heap.replace(it->second, r); deba@2548: (*_node_data)[ni].heap.decrease(r, rw); deba@2548: it->second = r; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[ni].heap.push(r, rw); deba@2548: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@2548: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@2548: deba@2548: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@2548: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } else if ((*_delta2)[blossom] > deba@2548: _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset){ deba@2548: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: } else { deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: (*_node_data)[vi].heap.erase(it->second); deba@2548: (*_node_data)[vi].heap_index.erase(it); deba@2548: if ((*_node_data)[vi].heap.empty()) { deba@2548: _blossom_set->increase(v, std::numeric_limits::max()); deba@2548: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@2548: } deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_blossom_set->classPrio(vb) == deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->erase(vb); deba@2548: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset) { deba@2548: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: void oddToMatched(int blossom) { deba@2548: (*_blossom_data)[blossom].offset -= _delta_sum; deba@2548: deba@2548: if (_blossom_set->classPrio(blossom) != deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->erase(blossom); deba@2548: } deba@2548: } deba@2548: deba@2548: void oddToEven(int blossom, int tree) { deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->erase(blossom); deba@2548: (*_blossom_data)[blossom].pot -= deba@2548: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: _blossom_set->increase(n, std::numeric_limits::max()); deba@2548: deba@2548: (*_node_data)[ni].heap.clear(); deba@2548: (*_node_data)[ni].heap_index.clear(); deba@2548: (*_node_data)[ni].pot += deba@2548: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@2548: deba@2548: _delta1->push(n, (*_node_data)[ni].pot); deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if ((*_blossom_data)[vb].status == EVEN) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@2548: _delta3->push(e, rw / 2); deba@2548: } deba@2548: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@2548: _delta3->push(e, rw); deba@2548: } deba@2548: } else { deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: if ((*_node_data)[vi].heap[it->second] > rw) { deba@2548: (*_node_data)[vi].heap.replace(it->second, e); deba@2548: (*_node_data)[vi].heap.decrease(e, rw); deba@2548: it->second = e; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[vi].heap.push(e, rw); deba@2548: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@2548: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset) { deba@2548: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: } deba@2548: deba@2548: deba@2548: void matchedToUnmatched(int blossom) { deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: _blossom_set->increase(n, std::numeric_limits::max()); deba@2548: deba@2548: (*_node_data)[ni].heap.clear(); deba@2548: (*_node_data)[ni].heap_index.clear(); deba@2548: deba@2548: for (OutEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.target(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if ((*_blossom_data)[vb].status == EVEN) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@2548: _delta3->push(e, rw); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: void unmatchedToMatched(int blossom) { deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if (vb == blossom) { deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: } else if ((*_blossom_data)[vb].status == EVEN) { deba@2548: deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: deba@2548: int vt = _tree_set->find(vb); deba@2548: deba@2548: Edge r = _ugraph.oppositeEdge(e); deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[ni].heap_index.find(vt); deba@2548: deba@2548: if (it != (*_node_data)[ni].heap_index.end()) { deba@2548: if ((*_node_data)[ni].heap[it->second] > rw) { deba@2548: (*_node_data)[ni].heap.replace(it->second, r); deba@2548: (*_node_data)[ni].heap.decrease(r, rw); deba@2548: it->second = r; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[ni].heap.push(r, rw); deba@2548: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@2548: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@2548: deba@2548: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@2548: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } else if ((*_delta2)[blossom] > _blossom_set->classPrio(blossom)- deba@2548: (*_blossom_data)[blossom].offset){ deba@2548: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: } deba@2548: deba@2548: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: void alternatePath(int even, int tree) { deba@2548: int odd; deba@2548: deba@2548: evenToMatched(even, tree); deba@2548: (*_blossom_data)[even].status = MATCHED; deba@2548: deba@2548: while ((*_blossom_data)[even].pred != INVALID) { deba@2548: odd = _blossom_set->find(_ugraph.target((*_blossom_data)[even].pred)); deba@2548: (*_blossom_data)[odd].status = MATCHED; deba@2548: oddToMatched(odd); deba@2548: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@2548: deba@2548: even = _blossom_set->find(_ugraph.target((*_blossom_data)[odd].pred)); deba@2548: (*_blossom_data)[even].status = MATCHED; deba@2548: evenToMatched(even, tree); deba@2548: (*_blossom_data)[even].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[odd].pred); deba@2548: } deba@2548: deba@2548: } deba@2548: deba@2548: void destroyTree(int tree) { deba@2548: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@2548: if ((*_blossom_data)[b].status == EVEN) { deba@2548: (*_blossom_data)[b].status = MATCHED; deba@2548: evenToMatched(b, tree); deba@2548: } else if ((*_blossom_data)[b].status == ODD) { deba@2548: (*_blossom_data)[b].status = MATCHED; deba@2548: oddToMatched(b); deba@2548: } deba@2548: } deba@2548: _tree_set->eraseClass(tree); deba@2548: } deba@2548: deba@2548: deba@2548: void unmatchNode(const Node& node) { deba@2548: int blossom = _blossom_set->find(node); deba@2548: int tree = _tree_set->find(blossom); deba@2548: deba@2548: alternatePath(blossom, tree); deba@2548: destroyTree(tree); deba@2548: deba@2548: (*_blossom_data)[blossom].status = UNMATCHED; deba@2548: (*_blossom_data)[blossom].base = node; deba@2548: matchedToUnmatched(blossom); deba@2548: } deba@2548: deba@2548: deba@2548: void augmentOnEdge(const UEdge& edge) { deba@2548: deba@2548: int left = _blossom_set->find(_ugraph.source(edge)); deba@2548: int right = _blossom_set->find(_ugraph.target(edge)); deba@2548: deba@2548: if ((*_blossom_data)[left].status == EVEN) { deba@2548: int left_tree = _tree_set->find(left); deba@2548: alternatePath(left, left_tree); deba@2548: destroyTree(left_tree); deba@2548: } else { deba@2548: (*_blossom_data)[left].status = MATCHED; deba@2548: unmatchedToMatched(left); deba@2548: } deba@2548: deba@2548: if ((*_blossom_data)[right].status == EVEN) { deba@2548: int right_tree = _tree_set->find(right); deba@2548: alternatePath(right, right_tree); deba@2548: destroyTree(right_tree); deba@2548: } else { deba@2548: (*_blossom_data)[right].status = MATCHED; deba@2548: unmatchedToMatched(right); deba@2548: } deba@2548: deba@2548: (*_blossom_data)[left].next = _ugraph.direct(edge, true); deba@2548: (*_blossom_data)[right].next = _ugraph.direct(edge, false); deba@2548: } deba@2548: deba@2548: void extendOnEdge(const Edge& edge) { deba@2548: int base = _blossom_set->find(_ugraph.target(edge)); deba@2548: int tree = _tree_set->find(base); deba@2548: deba@2548: int odd = _blossom_set->find(_ugraph.source(edge)); deba@2548: _tree_set->insert(odd, tree); deba@2548: (*_blossom_data)[odd].status = ODD; deba@2548: matchedToOdd(odd); deba@2548: (*_blossom_data)[odd].pred = edge; deba@2548: deba@2548: int even = _blossom_set->find(_ugraph.target((*_blossom_data)[odd].next)); deba@2548: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@2548: _tree_set->insert(even, tree); deba@2548: (*_blossom_data)[even].status = EVEN; deba@2548: matchedToEven(even, tree); deba@2548: } deba@2548: deba@2548: void shrinkOnEdge(const UEdge& uedge, int tree) { deba@2548: int nca = -1; deba@2548: std::vector left_path, right_path; deba@2548: deba@2548: { deba@2548: std::set left_set, right_set; deba@2548: int left = _blossom_set->find(_ugraph.source(uedge)); deba@2548: left_path.push_back(left); deba@2548: left_set.insert(left); deba@2548: deba@2548: int right = _blossom_set->find(_ugraph.target(uedge)); deba@2548: right_path.push_back(right); deba@2548: right_set.insert(right); deba@2548: deba@2548: while (true) { deba@2548: deba@2548: if ((*_blossom_data)[left].pred == INVALID) break; deba@2548: deba@2548: left = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[left].pred)); deba@2548: left_path.push_back(left); deba@2548: left = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[left].pred)); deba@2548: left_path.push_back(left); deba@2548: deba@2548: left_set.insert(left); deba@2548: deba@2548: if (right_set.find(left) != right_set.end()) { deba@2548: nca = left; deba@2548: break; deba@2548: } deba@2548: deba@2548: if ((*_blossom_data)[right].pred == INVALID) break; deba@2548: deba@2548: right = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[right].pred)); deba@2548: right_path.push_back(right); deba@2548: right = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[right].pred)); deba@2548: right_path.push_back(right); deba@2548: deba@2548: right_set.insert(right); deba@2548: deba@2548: if (left_set.find(right) != left_set.end()) { deba@2548: nca = right; deba@2548: break; deba@2548: } deba@2548: deba@2548: } deba@2548: deba@2548: if (nca == -1) { deba@2548: if ((*_blossom_data)[left].pred == INVALID) { deba@2548: nca = right; deba@2548: while (left_set.find(nca) == left_set.end()) { deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: right_path.push_back(nca); deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: right_path.push_back(nca); deba@2548: } deba@2548: } else { deba@2548: nca = left; deba@2548: while (right_set.find(nca) == right_set.end()) { deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: left_path.push_back(nca); deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: left_path.push_back(nca); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: std::vector subblossoms; deba@2548: Edge prev; deba@2548: deba@2548: prev = _ugraph.direct(uedge, true); deba@2548: for (int i = 0; left_path[i] != nca; i += 2) { deba@2548: subblossoms.push_back(left_path[i]); deba@2548: (*_blossom_data)[left_path[i]].next = prev; deba@2548: _tree_set->erase(left_path[i]); deba@2548: deba@2548: subblossoms.push_back(left_path[i + 1]); deba@2548: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@2548: oddToEven(left_path[i + 1], tree); deba@2548: _tree_set->erase(left_path[i + 1]); deba@2548: prev = _ugraph.oppositeEdge((*_blossom_data)[left_path[i + 1]].pred); deba@2548: } deba@2548: deba@2548: int k = 0; deba@2548: while (right_path[k] != nca) ++k; deba@2548: deba@2548: subblossoms.push_back(nca); deba@2548: (*_blossom_data)[nca].next = prev; deba@2548: deba@2548: for (int i = k - 2; i >= 0; i -= 2) { deba@2548: subblossoms.push_back(right_path[i + 1]); deba@2548: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@2548: oddToEven(right_path[i + 1], tree); deba@2548: _tree_set->erase(right_path[i + 1]); deba@2548: deba@2548: (*_blossom_data)[right_path[i + 1]].next = deba@2548: (*_blossom_data)[right_path[i + 1]].pred; deba@2548: deba@2548: subblossoms.push_back(right_path[i]); deba@2548: _tree_set->erase(right_path[i]); deba@2548: } deba@2548: deba@2548: int surface = deba@2548: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@2548: deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (!_blossom_set->trivial(subblossoms[i])) { deba@2548: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@2548: } deba@2548: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@2548: (*_blossom_data)[surface].offset = 0; deba@2548: (*_blossom_data)[surface].status = EVEN; deba@2548: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@2548: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@2548: deba@2548: _tree_set->insert(surface, tree); deba@2548: _tree_set->erase(nca); deba@2548: } deba@2548: deba@2548: void splitBlossom(int blossom) { deba@2548: Edge next = (*_blossom_data)[blossom].next; deba@2548: Edge pred = (*_blossom_data)[blossom].pred; deba@2548: deba@2548: int tree = _tree_set->find(blossom); deba@2548: deba@2548: (*_blossom_data)[blossom].status = MATCHED; deba@2548: oddToMatched(blossom); deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: deba@2548: std::vector subblossoms; deba@2548: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@2548: deba@2548: Value offset = (*_blossom_data)[blossom].offset; deba@2548: int b = _blossom_set->find(_ugraph.source(pred)); deba@2548: int d = _blossom_set->find(_ugraph.source(next)); deba@2548: deba@2549: int ib = -1, id = -1; deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (subblossoms[i] == b) ib = i; deba@2548: if (subblossoms[i] == d) id = i; deba@2548: deba@2548: (*_blossom_data)[subblossoms[i]].offset = offset; deba@2548: if (!_blossom_set->trivial(subblossoms[i])) { deba@2548: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@2548: } deba@2548: if (_blossom_set->classPrio(subblossoms[i]) != deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->push(subblossoms[i], deba@2548: _blossom_set->classPrio(subblossoms[i]) - deba@2548: (*_blossom_data)[subblossoms[i]].offset); deba@2548: } deba@2548: } deba@2548: deba@2548: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@2548: for (int i = (id + 1) % subblossoms.size(); deba@2548: i != ib; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: } deba@2548: deba@2548: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@2548: deba@2548: (*_blossom_data)[sb].status = ODD; deba@2548: matchedToOdd(sb); deba@2548: _tree_set->insert(sb, tree); deba@2548: (*_blossom_data)[sb].pred = pred; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: deba@2548: pred = (*_blossom_data)[ub].next; deba@2548: deba@2548: (*_blossom_data)[tb].status = EVEN; deba@2548: matchedToEven(tb, tree); deba@2548: _tree_set->insert(tb, tree); deba@2548: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[subblossoms[id]].status = ODD; deba@2548: matchedToOdd(subblossoms[id]); deba@2548: _tree_set->insert(subblossoms[id], tree); deba@2548: (*_blossom_data)[subblossoms[id]].next = next; deba@2548: (*_blossom_data)[subblossoms[id]].pred = pred; deba@2548: deba@2548: } else { deba@2548: deba@2548: for (int i = (ib + 1) % subblossoms.size(); deba@2548: i != id; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: } deba@2548: deba@2548: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@2548: deba@2548: (*_blossom_data)[sb].status = ODD; deba@2548: matchedToOdd(sb); deba@2548: _tree_set->insert(sb, tree); deba@2548: (*_blossom_data)[sb].next = next; deba@2548: (*_blossom_data)[sb].pred = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: deba@2548: (*_blossom_data)[tb].status = EVEN; deba@2548: matchedToEven(tb, tree); deba@2548: _tree_set->insert(tb, tree); deba@2548: (*_blossom_data)[tb].pred = deba@2548: (*_blossom_data)[tb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[ub].next); deba@2548: next = (*_blossom_data)[ub].next; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@2548: matchedToOdd(subblossoms[ib]); deba@2548: _tree_set->insert(subblossoms[ib], tree); deba@2548: (*_blossom_data)[subblossoms[ib]].next = next; deba@2548: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@2548: } deba@2548: _tree_set->erase(blossom); deba@2548: } deba@2548: deba@2548: void extractBlossom(int blossom, const Node& base, const Edge& matching) { deba@2548: if (_blossom_set->trivial(blossom)) { deba@2548: int bi = (*_node_index)[base]; deba@2548: Value pot = (*_node_data)[bi].pot; deba@2548: deba@2548: _matching->set(base, matching); deba@2548: _blossom_node_list.push_back(base); deba@2548: _node_potential->set(base, pot); deba@2548: } else { deba@2548: deba@2548: Value pot = (*_blossom_data)[blossom].pot; deba@2548: int bn = _blossom_node_list.size(); deba@2548: deba@2548: std::vector subblossoms; deba@2548: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@2548: int b = _blossom_set->find(base); deba@2548: int ib = -1; deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (subblossoms[i] == b) { ib = i; break; } deba@2548: } deba@2548: deba@2548: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@2548: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@2548: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@2548: deba@2548: Edge m = (*_blossom_data)[tb].next; deba@2548: extractBlossom(sb, _ugraph.target(m), _ugraph.oppositeEdge(m)); deba@2548: extractBlossom(tb, _ugraph.source(m), m); deba@2548: } deba@2548: extractBlossom(subblossoms[ib], base, matching); deba@2548: deba@2548: int en = _blossom_node_list.size(); deba@2548: deba@2548: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@2548: } deba@2548: } deba@2548: deba@2548: void extractMatching() { deba@2548: std::vector blossoms; deba@2548: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@2548: blossoms.push_back(c); deba@2548: } deba@2548: deba@2548: for (int i = 0; i < int(blossoms.size()); ++i) { deba@2548: if ((*_blossom_data)[blossoms[i]].status == MATCHED) { deba@2548: deba@2548: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@2548: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@2548: n != INVALID; ++n) { deba@2548: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@2548: } deba@2548: deba@2548: Edge matching = (*_blossom_data)[blossoms[i]].next; deba@2548: Node base = _ugraph.source(matching); deba@2548: extractBlossom(blossoms[i], base, matching); deba@2548: } else { deba@2548: Node base = (*_blossom_data)[blossoms[i]].base; deba@2548: extractBlossom(blossoms[i], base, INVALID); deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: public: deba@2548: deba@2548: /// \brief Constructor deba@2548: /// deba@2548: /// Constructor. deba@2548: MaxWeightedMatching(const UGraph& ugraph, const WeightMap& weight) deba@2548: : _ugraph(ugraph), _weight(weight), _matching(0), deba@2548: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@2548: _node_num(0), _blossom_num(0), deba@2548: deba@2548: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@2548: _node_index(0), _node_heap_index(0), _node_data(0), deba@2548: _tree_set_index(0), _tree_set(0), deba@2548: deba@2548: _delta1_index(0), _delta1(0), deba@2548: _delta2_index(0), _delta2(0), deba@2548: _delta3_index(0), _delta3(0), deba@2548: _delta4_index(0), _delta4(0), deba@2548: deba@2548: _delta_sum() {} deba@2548: deba@2548: ~MaxWeightedMatching() { deba@2548: destroyStructures(); deba@2548: } deba@2548: deba@2548: /// \name Execution control deba@2548: /// The simplest way to execute the algorithm is to use the member deba@2548: /// \c run() member function. deba@2548: deba@2548: ///@{ deba@2548: deba@2548: /// \brief Initialize the algorithm deba@2548: /// deba@2548: /// Initialize the algorithm deba@2548: void init() { deba@2548: createStructures(); deba@2548: deba@2548: for (EdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: _node_heap_index->set(e, BinHeap::PRE_HEAP); deba@2548: } deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: _delta1_index->set(n, _delta1->PRE_HEAP); deba@2548: } deba@2548: for (UEdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: _delta3_index->set(e, _delta3->PRE_HEAP); deba@2548: } deba@2548: for (int i = 0; i < _blossom_num; ++i) { deba@2548: _delta2_index->set(i, _delta2->PRE_HEAP); deba@2548: _delta4_index->set(i, _delta4->PRE_HEAP); deba@2548: } deba@2548: deba@2548: int index = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: Value max = 0; deba@2548: for (OutEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: if (_ugraph.target(e) == n) continue; deba@2548: if ((dualScale * _weight[e]) / 2 > max) { deba@2548: max = (dualScale * _weight[e]) / 2; deba@2548: } deba@2548: } deba@2548: _node_index->set(n, index); deba@2548: (*_node_data)[index].pot = max; deba@2548: _delta1->push(n, max); deba@2548: int blossom = deba@2548: _blossom_set->insert(n, std::numeric_limits::max()); deba@2548: deba@2548: _tree_set->insert(blossom); deba@2548: deba@2548: (*_blossom_data)[blossom].status = EVEN; deba@2548: (*_blossom_data)[blossom].pred = INVALID; deba@2548: (*_blossom_data)[blossom].next = INVALID; deba@2548: (*_blossom_data)[blossom].pot = 0; deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: ++index; deba@2548: } deba@2548: for (UEdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: int si = (*_node_index)[_ugraph.source(e)]; deba@2548: int ti = (*_node_index)[_ugraph.target(e)]; deba@2548: if (_ugraph.source(e) != _ugraph.target(e)) { deba@2548: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@2548: dualScale * _weight[e]) / 2); deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: /// \brief Starts the algorithm deba@2548: /// deba@2548: /// Starts the algorithm deba@2548: void start() { deba@2548: enum OpType { deba@2548: D1, D2, D3, D4 deba@2548: }; deba@2548: deba@2548: int unmatched = _node_num; deba@2548: while (unmatched > 0) { deba@2548: Value d1 = !_delta1->empty() ? deba@2548: _delta1->prio() : std::numeric_limits::max(); deba@2548: deba@2548: Value d2 = !_delta2->empty() ? deba@2548: _delta2->prio() : std::numeric_limits::max(); deba@2548: deba@2548: Value d3 = !_delta3->empty() ? deba@2548: _delta3->prio() : std::numeric_limits::max(); deba@2548: deba@2548: Value d4 = !_delta4->empty() ? deba@2548: _delta4->prio() : std::numeric_limits::max(); deba@2548: deba@2548: _delta_sum = d1; OpType ot = D1; deba@2548: if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; } deba@2548: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@2548: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@2548: deba@2548: deba@2548: switch (ot) { deba@2548: case D1: deba@2548: { deba@2548: Node n = _delta1->top(); deba@2548: unmatchNode(n); deba@2548: --unmatched; deba@2548: } deba@2548: break; deba@2548: case D2: deba@2548: { deba@2548: int blossom = _delta2->top(); deba@2548: Node n = _blossom_set->classTop(blossom); deba@2548: Edge e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@2548: extendOnEdge(e); deba@2548: } deba@2548: break; deba@2548: case D3: deba@2548: { deba@2548: UEdge e = _delta3->top(); deba@2548: deba@2548: int left_blossom = _blossom_set->find(_ugraph.source(e)); deba@2548: int right_blossom = _blossom_set->find(_ugraph.target(e)); deba@2548: deba@2548: if (left_blossom == right_blossom) { deba@2548: _delta3->pop(); deba@2548: } else { deba@2548: int left_tree; deba@2548: if ((*_blossom_data)[left_blossom].status == EVEN) { deba@2548: left_tree = _tree_set->find(left_blossom); deba@2548: } else { deba@2548: left_tree = -1; deba@2548: ++unmatched; deba@2548: } deba@2548: int right_tree; deba@2548: if ((*_blossom_data)[right_blossom].status == EVEN) { deba@2548: right_tree = _tree_set->find(right_blossom); deba@2548: } else { deba@2548: right_tree = -1; deba@2548: ++unmatched; deba@2548: } deba@2548: deba@2548: if (left_tree == right_tree) { deba@2548: shrinkOnEdge(e, left_tree); deba@2548: } else { deba@2548: augmentOnEdge(e); deba@2548: unmatched -= 2; deba@2548: } deba@2548: } deba@2548: } break; deba@2548: case D4: deba@2548: splitBlossom(_delta4->top()); deba@2548: break; deba@2548: } deba@2548: } deba@2548: extractMatching(); deba@2548: } deba@2548: deba@2548: /// \brief Runs %MaxWeightedMatching algorithm. deba@2548: /// deba@2548: /// This method runs the %MaxWeightedMatching algorithm. deba@2548: /// deba@2548: /// \note mwm.run() is just a shortcut of the following code. deba@2548: /// \code deba@2548: /// mwm.init(); deba@2548: /// mwm.start(); deba@2548: /// \endcode deba@2548: void run() { deba@2548: init(); deba@2548: start(); deba@2548: } deba@2548: deba@2548: /// @} deba@2548: deba@2548: /// \name Primal solution deba@2548: /// Functions for get the primal solution, ie. the matching. deba@2548: deba@2548: /// @{ deba@2548: deba@2548: /// \brief Returns the matching value. deba@2548: /// deba@2548: /// Returns the matching value. deba@2548: Value matchingValue() const { deba@2548: Value sum = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: if ((*_matching)[n] != INVALID) { deba@2548: sum += _weight[(*_matching)[n]]; deba@2548: } deba@2548: } deba@2548: return sum /= 2; deba@2548: } deba@2548: deba@2548: /// \brief Returns true when the edge is in the matching. deba@2548: /// deba@2548: /// Returns true when the edge is in the matching. deba@2548: bool matching(const UEdge& edge) const { deba@2548: return (*_matching)[_ugraph.source(edge)] == _ugraph.direct(edge, true); deba@2548: } deba@2548: deba@2548: /// \brief Returns the incident matching edge. deba@2548: /// deba@2548: /// Returns the incident matching edge from given node. If the deba@2548: /// node is not matched then it gives back \c INVALID. deba@2548: Edge matching(const Node& node) const { deba@2548: return (*_matching)[node]; deba@2548: } deba@2548: deba@2548: /// \brief Returns the mate of the node. deba@2548: /// deba@2548: /// Returns the adjancent node in a mathcing edge. If the node is deba@2548: /// not matched then it gives back \c INVALID. deba@2548: Node mate(const Node& node) const { deba@2548: return (*_matching)[node] != INVALID ? deba@2548: _ugraph.target((*_matching)[node]) : INVALID; deba@2548: } deba@2548: deba@2548: /// @} deba@2548: deba@2548: /// \name Dual solution deba@2548: /// Functions for get the dual solution. deba@2548: deba@2548: /// @{ deba@2548: deba@2548: /// \brief Returns the value of the dual solution. deba@2548: /// deba@2548: /// Returns the value of the dual solution. It should be equal to deba@2548: /// the primal value scaled by \ref dualScale "dual scale". deba@2548: Value dualValue() const { deba@2548: Value sum = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: sum += nodeValue(n); deba@2548: } deba@2548: for (int i = 0; i < blossomNum(); ++i) { deba@2548: sum += blossomValue(i) * (blossomSize(i) / 2); deba@2548: } deba@2548: return sum; deba@2548: } deba@2548: deba@2548: /// \brief Returns the value of the node. deba@2548: /// deba@2548: /// Returns the the value of the node. deba@2548: Value nodeValue(const Node& n) const { deba@2548: return (*_node_potential)[n]; deba@2548: } deba@2548: deba@2548: /// \brief Returns the number of the blossoms in the basis. deba@2548: /// deba@2548: /// Returns the number of the blossoms in the basis. deba@2548: /// \see BlossomIt deba@2548: int blossomNum() const { deba@2548: return _blossom_potential.size(); deba@2548: } deba@2548: deba@2548: deba@2548: /// \brief Returns the number of the nodes in the blossom. deba@2548: /// deba@2548: /// Returns the number of the nodes in the blossom. deba@2548: int blossomSize(int k) const { deba@2548: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@2548: } deba@2548: deba@2548: /// \brief Returns the value of the blossom. deba@2548: /// deba@2548: /// Returns the the value of the blossom. deba@2548: /// \see BlossomIt deba@2548: Value blossomValue(int k) const { deba@2548: return _blossom_potential[k].value; deba@2548: } deba@2548: deba@2548: /// \brief Lemon iterator for get the items of the blossom. deba@2548: /// deba@2548: /// Lemon iterator for get the nodes of the blossom. This class deba@2548: /// provides a common style lemon iterator which gives back a deba@2548: /// subset of the nodes. deba@2548: class BlossomIt { deba@2548: public: deba@2548: deba@2548: /// \brief Constructor. deba@2548: /// deba@2548: /// Constructor for get the nodes of the variable. deba@2548: BlossomIt(const MaxWeightedMatching& algorithm, int variable) deba@2548: : _algorithm(&algorithm) deba@2548: { deba@2548: _index = _algorithm->_blossom_potential[variable].begin; deba@2548: _last = _algorithm->_blossom_potential[variable].end; deba@2548: } deba@2548: deba@2548: /// \brief Invalid constructor. deba@2548: /// deba@2548: /// Invalid constructor. deba@2548: BlossomIt(Invalid) : _index(-1) {} deba@2548: deba@2548: /// \brief Conversion to node. deba@2548: /// deba@2548: /// Conversion to node. deba@2548: operator Node() const { deba@2548: return _algorithm ? _algorithm->_blossom_node_list[_index] : INVALID; deba@2548: } deba@2548: deba@2548: /// \brief Increment operator. deba@2548: /// deba@2548: /// Increment operator. deba@2548: BlossomIt& operator++() { deba@2548: ++_index; deba@2548: if (_index == _last) { deba@2548: _index = -1; deba@2548: } deba@2548: return *this; deba@2548: } deba@2548: deba@2548: bool operator==(const BlossomIt& it) const { deba@2548: return _index == it._index; deba@2548: } deba@2548: bool operator!=(const BlossomIt& it) const { deba@2548: return _index != it._index; deba@2548: } deba@2548: deba@2548: private: deba@2548: const MaxWeightedMatching* _algorithm; deba@2548: int _last; deba@2548: int _index; deba@2548: }; deba@2548: deba@2548: /// @} deba@2548: deba@2548: }; deba@2548: deba@2548: /// \ingroup matching deba@2548: /// deba@2548: /// \brief Weighted perfect matching in general undirected graphs deba@2548: /// deba@2548: /// This class provides an efficient implementation of Edmond's deba@2548: /// maximum weighted perfecr matching algorithm. The implementation deba@2548: /// is based on extensive use of priority queues and provides deba@2548: /// \f$O(nm\log(n))\f$ time complexity. deba@2548: /// deba@2548: /// The maximum weighted matching problem is to find undirected deba@2548: /// edges in the graph with maximum overall weight and no two of deba@2548: /// them shares their endpoints and covers all nodes. The problem deba@2548: /// can be formulated with the next linear program: deba@2548: /// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f] deba@2548: ///\f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} \quad \forall B\in\mathcal{O}\f] deba@2548: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@2548: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@2548: /// where \f$\delta(X)\f$ is the set of edges incident to a node in deba@2548: /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both endpoints in deba@2548: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality subsets of deba@2548: /// the nodes. deba@2548: /// deba@2548: /// The algorithm calculates an optimal matching and a proof of the deba@2548: /// optimality. The solution of the dual problem can be used to check deba@2548: /// the result of the algorithm. The dual linear problem is the next: deba@2548: /// \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge w_{uv} \quad \forall uv\in E\f] deba@2548: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@2548: /// \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}\frac{\vert B \vert - 1}{2}z_B\f] deba@2548: /// deba@2548: /// The algorithm can be executed with \c run() or the \c init() and deba@2548: /// then the \c start() member functions. After it the matching can deba@2548: /// be asked with \c matching() or mate() functions. The dual deba@2548: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@2548: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt deba@2548: /// "BlossomIt" nested class which is able to iterate on the nodes deba@2548: /// of a blossom. If the value type is integral then the dual deba@2548: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". deba@2548: /// deba@2548: /// \author Balazs Dezso deba@2548: template > deba@2548: class MaxWeightedPerfectMatching { deba@2548: public: deba@2548: deba@2548: typedef _UGraph UGraph; deba@2548: typedef _WeightMap WeightMap; deba@2548: typedef typename WeightMap::Value Value; deba@2548: deba@2548: /// \brief Scaling factor for dual solution deba@2548: /// deba@2548: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@2548: /// according to the value type. deba@2548: static const int dualScale = deba@2548: std::numeric_limits::is_integer ? 4 : 1; deba@2548: deba@2548: typedef typename UGraph::template NodeMap deba@2548: MatchingMap; deba@2548: deba@2548: private: deba@2548: deba@2548: UGRAPH_TYPEDEFS(typename UGraph); deba@2548: deba@2548: typedef typename UGraph::template NodeMap NodePotential; deba@2548: typedef std::vector BlossomNodeList; deba@2548: deba@2548: struct BlossomVariable { deba@2548: int begin, end; deba@2548: Value value; deba@2548: deba@2548: BlossomVariable(int _begin, int _end, Value _value) deba@2548: : begin(_begin), end(_end), value(_value) {} deba@2548: deba@2548: }; deba@2548: deba@2548: typedef std::vector BlossomPotential; deba@2548: deba@2548: const UGraph& _ugraph; deba@2548: const WeightMap& _weight; deba@2548: deba@2548: MatchingMap* _matching; deba@2548: deba@2548: NodePotential* _node_potential; deba@2548: deba@2548: BlossomPotential _blossom_potential; deba@2548: BlossomNodeList _blossom_node_list; deba@2548: deba@2548: int _node_num; deba@2548: int _blossom_num; deba@2548: deba@2548: typedef typename UGraph::template NodeMap NodeIntMap; deba@2548: typedef typename UGraph::template EdgeMap EdgeIntMap; deba@2548: typedef typename UGraph::template UEdgeMap UEdgeIntMap; deba@2548: typedef IntegerMap IntIntMap; deba@2548: deba@2548: enum Status { deba@2548: EVEN = -1, MATCHED = 0, ODD = 1 deba@2548: }; deba@2548: deba@2548: typedef HeapUnionFind BlossomSet; deba@2548: struct BlossomData { deba@2548: int tree; deba@2548: Status status; deba@2548: Edge pred, next; deba@2548: Value pot, offset; deba@2548: }; deba@2548: deba@2548: NodeIntMap *_blossom_index; deba@2548: BlossomSet *_blossom_set; deba@2548: IntegerMap* _blossom_data; deba@2548: deba@2548: NodeIntMap *_node_index; deba@2548: EdgeIntMap *_node_heap_index; deba@2548: deba@2548: struct NodeData { deba@2548: deba@2548: NodeData(EdgeIntMap& node_heap_index) deba@2548: : heap(node_heap_index) {} deba@2548: deba@2548: int blossom; deba@2548: Value pot; deba@2548: BinHeap heap; deba@2548: std::map heap_index; deba@2548: deba@2548: int tree; deba@2548: }; deba@2548: deba@2548: IntegerMap* _node_data; deba@2548: deba@2548: typedef ExtendFindEnum TreeSet; deba@2548: deba@2548: IntIntMap *_tree_set_index; deba@2548: TreeSet *_tree_set; deba@2548: deba@2548: IntIntMap *_delta2_index; deba@2548: BinHeap *_delta2; deba@2548: deba@2548: UEdgeIntMap *_delta3_index; deba@2548: BinHeap *_delta3; deba@2548: deba@2548: IntIntMap *_delta4_index; deba@2548: BinHeap *_delta4; deba@2548: deba@2548: Value _delta_sum; deba@2548: deba@2548: void createStructures() { deba@2548: _node_num = countNodes(_ugraph); deba@2548: _blossom_num = _node_num * 3 / 2; deba@2548: deba@2548: if (!_matching) { deba@2548: _matching = new MatchingMap(_ugraph); deba@2548: } deba@2548: if (!_node_potential) { deba@2548: _node_potential = new NodePotential(_ugraph); deba@2548: } deba@2548: if (!_blossom_set) { deba@2548: _blossom_index = new NodeIntMap(_ugraph); deba@2548: _blossom_set = new BlossomSet(*_blossom_index); deba@2548: _blossom_data = new IntegerMap(_blossom_num); deba@2548: } deba@2548: deba@2548: if (!_node_index) { deba@2548: _node_index = new NodeIntMap(_ugraph); deba@2548: _node_heap_index = new EdgeIntMap(_ugraph); deba@2548: _node_data = new IntegerMap(_node_num, deba@2548: NodeData(*_node_heap_index)); deba@2548: } deba@2548: deba@2548: if (!_tree_set) { deba@2548: _tree_set_index = new IntIntMap(_blossom_num); deba@2548: _tree_set = new TreeSet(*_tree_set_index); deba@2548: } deba@2548: if (!_delta2) { deba@2548: _delta2_index = new IntIntMap(_blossom_num); deba@2548: _delta2 = new BinHeap(*_delta2_index); deba@2548: } deba@2548: if (!_delta3) { deba@2548: _delta3_index = new UEdgeIntMap(_ugraph); deba@2548: _delta3 = new BinHeap(*_delta3_index); deba@2548: } deba@2548: if (!_delta4) { deba@2548: _delta4_index = new IntIntMap(_blossom_num); deba@2548: _delta4 = new BinHeap(*_delta4_index); deba@2548: } deba@2548: } deba@2548: deba@2548: void destroyStructures() { deba@2548: _node_num = countNodes(_ugraph); deba@2548: _blossom_num = _node_num * 3 / 2; deba@2548: deba@2548: if (_matching) { deba@2548: delete _matching; deba@2548: } deba@2548: if (_node_potential) { deba@2548: delete _node_potential; deba@2548: } deba@2548: if (_blossom_set) { deba@2548: delete _blossom_index; deba@2548: delete _blossom_set; deba@2548: delete _blossom_data; deba@2548: } deba@2548: deba@2548: if (_node_index) { deba@2548: delete _node_index; deba@2548: delete _node_heap_index; deba@2548: delete _node_data; deba@2548: } deba@2548: deba@2548: if (_tree_set) { deba@2548: delete _tree_set_index; deba@2548: delete _tree_set; deba@2548: } deba@2548: if (_delta2) { deba@2548: delete _delta2_index; deba@2548: delete _delta2; deba@2548: } deba@2548: if (_delta3) { deba@2548: delete _delta3_index; deba@2548: delete _delta3; deba@2548: } deba@2548: if (_delta4) { deba@2548: delete _delta4_index; deba@2548: delete _delta4; deba@2548: } deba@2548: } deba@2548: deba@2548: void matchedToEven(int blossom, int tree) { deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: (*_blossom_data)[blossom].pot -= deba@2548: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: deba@2548: _blossom_set->increase(n, std::numeric_limits::max()); deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: (*_node_data)[ni].heap.clear(); deba@2548: (*_node_data)[ni].heap_index.clear(); deba@2548: deba@2548: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if ((*_blossom_data)[vb].status == EVEN) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@2548: _delta3->push(e, rw / 2); deba@2548: } deba@2548: } else { deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: if ((*_node_data)[vi].heap[it->second] > rw) { deba@2548: (*_node_data)[vi].heap.replace(it->second, e); deba@2548: (*_node_data)[vi].heap.decrease(e, rw); deba@2548: it->second = e; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[vi].heap.push(e, rw); deba@2548: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@2548: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset){ deba@2548: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: } deba@2548: deba@2548: void matchedToOdd(int blossom) { deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: (*_blossom_data)[blossom].offset += _delta_sum; deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: } deba@2548: deba@2548: void evenToMatched(int blossom, int tree) { deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: (*_node_data)[ni].pot -= _delta_sum; deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if (vb == blossom) { deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: } else if ((*_blossom_data)[vb].status == EVEN) { deba@2548: deba@2548: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@2548: _delta3->erase(e); deba@2548: } deba@2548: deba@2548: int vt = _tree_set->find(vb); deba@2548: deba@2548: if (vt != tree) { deba@2548: deba@2548: Edge r = _ugraph.oppositeEdge(e); deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[ni].heap_index.find(vt); deba@2548: deba@2548: if (it != (*_node_data)[ni].heap_index.end()) { deba@2548: if ((*_node_data)[ni].heap[it->second] > rw) { deba@2548: (*_node_data)[ni].heap.replace(it->second, r); deba@2548: (*_node_data)[ni].heap.decrease(r, rw); deba@2548: it->second = r; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[ni].heap.push(r, rw); deba@2548: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@2548: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@2548: deba@2548: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@2548: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } else if ((*_delta2)[blossom] > deba@2548: _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset){ deba@2548: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } else { deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: (*_node_data)[vi].heap.erase(it->second); deba@2548: (*_node_data)[vi].heap_index.erase(it); deba@2548: if ((*_node_data)[vi].heap.empty()) { deba@2548: _blossom_set->increase(v, std::numeric_limits::max()); deba@2548: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@2548: } deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_blossom_set->classPrio(vb) == deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->erase(vb); deba@2548: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset) { deba@2548: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: void oddToMatched(int blossom) { deba@2548: (*_blossom_data)[blossom].offset -= _delta_sum; deba@2548: deba@2548: if (_blossom_set->classPrio(blossom) != deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@2548: (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->erase(blossom); deba@2548: } deba@2548: } deba@2548: deba@2548: void oddToEven(int blossom, int tree) { deba@2548: if (!_blossom_set->trivial(blossom)) { deba@2548: _delta4->erase(blossom); deba@2548: (*_blossom_data)[blossom].pot -= deba@2548: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@2548: } deba@2548: deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@2548: n != INVALID; ++n) { deba@2548: int ni = (*_node_index)[n]; deba@2548: deba@2548: _blossom_set->increase(n, std::numeric_limits::max()); deba@2548: deba@2548: (*_node_data)[ni].heap.clear(); deba@2548: (*_node_data)[ni].heap_index.clear(); deba@2548: (*_node_data)[ni].pot += deba@2548: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@2548: deba@2548: for (InEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: Node v = _ugraph.source(e); deba@2548: int vb = _blossom_set->find(v); deba@2548: int vi = (*_node_index)[v]; deba@2548: deba@2548: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@2548: dualScale * _weight[e]; deba@2548: deba@2548: if ((*_blossom_data)[vb].status == EVEN) { deba@2548: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@2548: _delta3->push(e, rw / 2); deba@2548: } deba@2548: } else { deba@2548: deba@2548: typename std::map::iterator it = deba@2548: (*_node_data)[vi].heap_index.find(tree); deba@2548: deba@2548: if (it != (*_node_data)[vi].heap_index.end()) { deba@2548: if ((*_node_data)[vi].heap[it->second] > rw) { deba@2548: (*_node_data)[vi].heap.replace(it->second, e); deba@2548: (*_node_data)[vi].heap.decrease(e, rw); deba@2548: it->second = e; deba@2548: } deba@2548: } else { deba@2548: (*_node_data)[vi].heap.push(e, rw); deba@2548: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@2548: } deba@2548: deba@2548: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@2548: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@2548: deba@2548: if ((*_blossom_data)[vb].status == MATCHED) { deba@2548: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@2548: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset) { deba@2548: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@2548: (*_blossom_data)[vb].offset); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: } deba@2548: deba@2548: void alternatePath(int even, int tree) { deba@2548: int odd; deba@2548: deba@2548: evenToMatched(even, tree); deba@2548: (*_blossom_data)[even].status = MATCHED; deba@2548: deba@2548: while ((*_blossom_data)[even].pred != INVALID) { deba@2548: odd = _blossom_set->find(_ugraph.target((*_blossom_data)[even].pred)); deba@2548: (*_blossom_data)[odd].status = MATCHED; deba@2548: oddToMatched(odd); deba@2548: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@2548: deba@2548: even = _blossom_set->find(_ugraph.target((*_blossom_data)[odd].pred)); deba@2548: (*_blossom_data)[even].status = MATCHED; deba@2548: evenToMatched(even, tree); deba@2548: (*_blossom_data)[even].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[odd].pred); deba@2548: } deba@2548: deba@2548: } deba@2548: deba@2548: void destroyTree(int tree) { deba@2548: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@2548: if ((*_blossom_data)[b].status == EVEN) { deba@2548: (*_blossom_data)[b].status = MATCHED; deba@2548: evenToMatched(b, tree); deba@2548: } else if ((*_blossom_data)[b].status == ODD) { deba@2548: (*_blossom_data)[b].status = MATCHED; deba@2548: oddToMatched(b); deba@2548: } deba@2548: } deba@2548: _tree_set->eraseClass(tree); deba@2548: } deba@2548: deba@2548: void augmentOnEdge(const UEdge& edge) { deba@2548: deba@2548: int left = _blossom_set->find(_ugraph.source(edge)); deba@2548: int right = _blossom_set->find(_ugraph.target(edge)); deba@2548: deba@2548: int left_tree = _tree_set->find(left); deba@2548: alternatePath(left, left_tree); deba@2548: destroyTree(left_tree); deba@2548: deba@2548: int right_tree = _tree_set->find(right); deba@2548: alternatePath(right, right_tree); deba@2548: destroyTree(right_tree); deba@2548: deba@2548: (*_blossom_data)[left].next = _ugraph.direct(edge, true); deba@2548: (*_blossom_data)[right].next = _ugraph.direct(edge, false); deba@2548: } deba@2548: deba@2548: void extendOnEdge(const Edge& edge) { deba@2548: int base = _blossom_set->find(_ugraph.target(edge)); deba@2548: int tree = _tree_set->find(base); deba@2548: deba@2548: int odd = _blossom_set->find(_ugraph.source(edge)); deba@2548: _tree_set->insert(odd, tree); deba@2548: (*_blossom_data)[odd].status = ODD; deba@2548: matchedToOdd(odd); deba@2548: (*_blossom_data)[odd].pred = edge; deba@2548: deba@2548: int even = _blossom_set->find(_ugraph.target((*_blossom_data)[odd].next)); deba@2548: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@2548: _tree_set->insert(even, tree); deba@2548: (*_blossom_data)[even].status = EVEN; deba@2548: matchedToEven(even, tree); deba@2548: } deba@2548: deba@2548: void shrinkOnEdge(const UEdge& uedge, int tree) { deba@2548: int nca = -1; deba@2548: std::vector left_path, right_path; deba@2548: deba@2548: { deba@2548: std::set left_set, right_set; deba@2548: int left = _blossom_set->find(_ugraph.source(uedge)); deba@2548: left_path.push_back(left); deba@2548: left_set.insert(left); deba@2548: deba@2548: int right = _blossom_set->find(_ugraph.target(uedge)); deba@2548: right_path.push_back(right); deba@2548: right_set.insert(right); deba@2548: deba@2548: while (true) { deba@2548: deba@2548: if ((*_blossom_data)[left].pred == INVALID) break; deba@2548: deba@2548: left = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[left].pred)); deba@2548: left_path.push_back(left); deba@2548: left = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[left].pred)); deba@2548: left_path.push_back(left); deba@2548: deba@2548: left_set.insert(left); deba@2548: deba@2548: if (right_set.find(left) != right_set.end()) { deba@2548: nca = left; deba@2548: break; deba@2548: } deba@2548: deba@2548: if ((*_blossom_data)[right].pred == INVALID) break; deba@2548: deba@2548: right = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[right].pred)); deba@2548: right_path.push_back(right); deba@2548: right = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[right].pred)); deba@2548: right_path.push_back(right); deba@2548: deba@2548: right_set.insert(right); deba@2548: deba@2548: if (left_set.find(right) != left_set.end()) { deba@2548: nca = right; deba@2548: break; deba@2548: } deba@2548: deba@2548: } deba@2548: deba@2548: if (nca == -1) { deba@2548: if ((*_blossom_data)[left].pred == INVALID) { deba@2548: nca = right; deba@2548: while (left_set.find(nca) == left_set.end()) { deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: right_path.push_back(nca); deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: right_path.push_back(nca); deba@2548: } deba@2548: } else { deba@2548: nca = left; deba@2548: while (right_set.find(nca) == right_set.end()) { deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: left_path.push_back(nca); deba@2548: nca = deba@2548: _blossom_set->find(_ugraph.target((*_blossom_data)[nca].pred)); deba@2548: left_path.push_back(nca); deba@2548: } deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: std::vector subblossoms; deba@2548: Edge prev; deba@2548: deba@2548: prev = _ugraph.direct(uedge, true); deba@2548: for (int i = 0; left_path[i] != nca; i += 2) { deba@2548: subblossoms.push_back(left_path[i]); deba@2548: (*_blossom_data)[left_path[i]].next = prev; deba@2548: _tree_set->erase(left_path[i]); deba@2548: deba@2548: subblossoms.push_back(left_path[i + 1]); deba@2548: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@2548: oddToEven(left_path[i + 1], tree); deba@2548: _tree_set->erase(left_path[i + 1]); deba@2548: prev = _ugraph.oppositeEdge((*_blossom_data)[left_path[i + 1]].pred); deba@2548: } deba@2548: deba@2548: int k = 0; deba@2548: while (right_path[k] != nca) ++k; deba@2548: deba@2548: subblossoms.push_back(nca); deba@2548: (*_blossom_data)[nca].next = prev; deba@2548: deba@2548: for (int i = k - 2; i >= 0; i -= 2) { deba@2548: subblossoms.push_back(right_path[i + 1]); deba@2548: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@2548: oddToEven(right_path[i + 1], tree); deba@2548: _tree_set->erase(right_path[i + 1]); deba@2548: deba@2548: (*_blossom_data)[right_path[i + 1]].next = deba@2548: (*_blossom_data)[right_path[i + 1]].pred; deba@2548: deba@2548: subblossoms.push_back(right_path[i]); deba@2548: _tree_set->erase(right_path[i]); deba@2548: } deba@2548: deba@2548: int surface = deba@2548: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@2548: deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (!_blossom_set->trivial(subblossoms[i])) { deba@2548: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@2548: } deba@2548: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@2548: (*_blossom_data)[surface].offset = 0; deba@2548: (*_blossom_data)[surface].status = EVEN; deba@2548: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@2548: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@2548: deba@2548: _tree_set->insert(surface, tree); deba@2548: _tree_set->erase(nca); deba@2548: } deba@2548: deba@2548: void splitBlossom(int blossom) { deba@2548: Edge next = (*_blossom_data)[blossom].next; deba@2548: Edge pred = (*_blossom_data)[blossom].pred; deba@2548: deba@2548: int tree = _tree_set->find(blossom); deba@2548: deba@2548: (*_blossom_data)[blossom].status = MATCHED; deba@2548: oddToMatched(blossom); deba@2548: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@2548: _delta2->erase(blossom); deba@2548: } deba@2548: deba@2548: std::vector subblossoms; deba@2548: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@2548: deba@2548: Value offset = (*_blossom_data)[blossom].offset; deba@2548: int b = _blossom_set->find(_ugraph.source(pred)); deba@2548: int d = _blossom_set->find(_ugraph.source(next)); deba@2548: deba@2549: int ib = -1, id = -1; deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (subblossoms[i] == b) ib = i; deba@2548: if (subblossoms[i] == d) id = i; deba@2548: deba@2548: (*_blossom_data)[subblossoms[i]].offset = offset; deba@2548: if (!_blossom_set->trivial(subblossoms[i])) { deba@2548: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@2548: } deba@2548: if (_blossom_set->classPrio(subblossoms[i]) != deba@2548: std::numeric_limits::max()) { deba@2548: _delta2->push(subblossoms[i], deba@2548: _blossom_set->classPrio(subblossoms[i]) - deba@2548: (*_blossom_data)[subblossoms[i]].offset); deba@2548: } deba@2548: } deba@2548: deba@2548: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@2548: for (int i = (id + 1) % subblossoms.size(); deba@2548: i != ib; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: } deba@2548: deba@2548: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@2548: deba@2548: (*_blossom_data)[sb].status = ODD; deba@2548: matchedToOdd(sb); deba@2548: _tree_set->insert(sb, tree); deba@2548: (*_blossom_data)[sb].pred = pred; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: deba@2548: pred = (*_blossom_data)[ub].next; deba@2548: deba@2548: (*_blossom_data)[tb].status = EVEN; deba@2548: matchedToEven(tb, tree); deba@2548: _tree_set->insert(tb, tree); deba@2548: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[subblossoms[id]].status = ODD; deba@2548: matchedToOdd(subblossoms[id]); deba@2548: _tree_set->insert(subblossoms[id], tree); deba@2548: (*_blossom_data)[subblossoms[id]].next = next; deba@2548: (*_blossom_data)[subblossoms[id]].pred = pred; deba@2548: deba@2548: } else { deba@2548: deba@2548: for (int i = (ib + 1) % subblossoms.size(); deba@2548: i != id; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: (*_blossom_data)[sb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: } deba@2548: deba@2548: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@2548: int sb = subblossoms[i]; deba@2548: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@2548: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@2548: deba@2548: (*_blossom_data)[sb].status = ODD; deba@2548: matchedToOdd(sb); deba@2548: _tree_set->insert(sb, tree); deba@2548: (*_blossom_data)[sb].next = next; deba@2548: (*_blossom_data)[sb].pred = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[tb].next); deba@2548: deba@2548: (*_blossom_data)[tb].status = EVEN; deba@2548: matchedToEven(tb, tree); deba@2548: _tree_set->insert(tb, tree); deba@2548: (*_blossom_data)[tb].pred = deba@2548: (*_blossom_data)[tb].next = deba@2548: _ugraph.oppositeEdge((*_blossom_data)[ub].next); deba@2548: next = (*_blossom_data)[ub].next; deba@2548: } deba@2548: deba@2548: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@2548: matchedToOdd(subblossoms[ib]); deba@2548: _tree_set->insert(subblossoms[ib], tree); deba@2548: (*_blossom_data)[subblossoms[ib]].next = next; deba@2548: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@2548: } deba@2548: _tree_set->erase(blossom); deba@2548: } deba@2548: deba@2548: void extractBlossom(int blossom, const Node& base, const Edge& matching) { deba@2548: if (_blossom_set->trivial(blossom)) { deba@2548: int bi = (*_node_index)[base]; deba@2548: Value pot = (*_node_data)[bi].pot; deba@2548: deba@2548: _matching->set(base, matching); deba@2548: _blossom_node_list.push_back(base); deba@2548: _node_potential->set(base, pot); deba@2548: } else { deba@2548: deba@2548: Value pot = (*_blossom_data)[blossom].pot; deba@2548: int bn = _blossom_node_list.size(); deba@2548: deba@2548: std::vector subblossoms; deba@2548: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@2548: int b = _blossom_set->find(base); deba@2548: int ib = -1; deba@2548: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@2548: if (subblossoms[i] == b) { ib = i; break; } deba@2548: } deba@2548: deba@2548: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@2548: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@2548: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@2548: deba@2548: Edge m = (*_blossom_data)[tb].next; deba@2548: extractBlossom(sb, _ugraph.target(m), _ugraph.oppositeEdge(m)); deba@2548: extractBlossom(tb, _ugraph.source(m), m); deba@2548: } deba@2548: extractBlossom(subblossoms[ib], base, matching); deba@2548: deba@2548: int en = _blossom_node_list.size(); deba@2548: deba@2548: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@2548: } deba@2548: } deba@2548: deba@2548: void extractMatching() { deba@2548: std::vector blossoms; deba@2548: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@2548: blossoms.push_back(c); deba@2548: } deba@2548: deba@2548: for (int i = 0; i < int(blossoms.size()); ++i) { deba@2548: deba@2548: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@2548: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@2548: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@2548: n != INVALID; ++n) { deba@2548: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@2548: } deba@2548: deba@2548: Edge matching = (*_blossom_data)[blossoms[i]].next; deba@2548: Node base = _ugraph.source(matching); deba@2548: extractBlossom(blossoms[i], base, matching); deba@2548: } deba@2548: } deba@2548: deba@2548: public: deba@2548: deba@2548: /// \brief Constructor deba@2548: /// deba@2548: /// Constructor. deba@2548: MaxWeightedPerfectMatching(const UGraph& ugraph, const WeightMap& weight) deba@2548: : _ugraph(ugraph), _weight(weight), _matching(0), deba@2548: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@2548: _node_num(0), _blossom_num(0), deba@2548: deba@2548: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@2548: _node_index(0), _node_heap_index(0), _node_data(0), deba@2548: _tree_set_index(0), _tree_set(0), deba@2548: deba@2548: _delta2_index(0), _delta2(0), deba@2548: _delta3_index(0), _delta3(0), deba@2548: _delta4_index(0), _delta4(0), deba@2548: deba@2548: _delta_sum() {} deba@2548: deba@2548: ~MaxWeightedPerfectMatching() { deba@2548: destroyStructures(); deba@2548: } deba@2548: deba@2548: /// \name Execution control deba@2548: /// The simplest way to execute the algorithm is to use the member deba@2548: /// \c run() member function. deba@2548: deba@2548: ///@{ deba@2548: deba@2548: /// \brief Initialize the algorithm deba@2548: /// deba@2548: /// Initialize the algorithm deba@2548: void init() { deba@2548: createStructures(); deba@2548: deba@2548: for (EdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: _node_heap_index->set(e, BinHeap::PRE_HEAP); deba@2548: } deba@2548: for (UEdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: _delta3_index->set(e, _delta3->PRE_HEAP); deba@2548: } deba@2548: for (int i = 0; i < _blossom_num; ++i) { deba@2548: _delta2_index->set(i, _delta2->PRE_HEAP); deba@2548: _delta4_index->set(i, _delta4->PRE_HEAP); deba@2548: } deba@2548: deba@2548: int index = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2612: Value max = - std::numeric_limits::max(); deba@2548: for (OutEdgeIt e(_ugraph, n); e != INVALID; ++e) { deba@2548: if (_ugraph.target(e) == n) continue; deba@2548: if ((dualScale * _weight[e]) / 2 > max) { deba@2548: max = (dualScale * _weight[e]) / 2; deba@2548: } deba@2548: } deba@2548: _node_index->set(n, index); deba@2548: (*_node_data)[index].pot = max; deba@2548: int blossom = deba@2548: _blossom_set->insert(n, std::numeric_limits::max()); deba@2548: deba@2548: _tree_set->insert(blossom); deba@2548: deba@2548: (*_blossom_data)[blossom].status = EVEN; deba@2548: (*_blossom_data)[blossom].pred = INVALID; deba@2548: (*_blossom_data)[blossom].next = INVALID; deba@2548: (*_blossom_data)[blossom].pot = 0; deba@2548: (*_blossom_data)[blossom].offset = 0; deba@2548: ++index; deba@2548: } deba@2548: for (UEdgeIt e(_ugraph); e != INVALID; ++e) { deba@2548: int si = (*_node_index)[_ugraph.source(e)]; deba@2548: int ti = (*_node_index)[_ugraph.target(e)]; deba@2548: if (_ugraph.source(e) != _ugraph.target(e)) { deba@2548: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@2548: dualScale * _weight[e]) / 2); deba@2548: } deba@2548: } deba@2548: } deba@2548: deba@2548: /// \brief Starts the algorithm deba@2548: /// deba@2548: /// Starts the algorithm deba@2548: bool start() { deba@2548: enum OpType { deba@2548: D2, D3, D4 deba@2548: }; deba@2548: deba@2548: int unmatched = _node_num; deba@2548: while (unmatched > 0) { deba@2548: Value d2 = !_delta2->empty() ? deba@2548: _delta2->prio() : std::numeric_limits::max(); deba@2548: deba@2548: Value d3 = !_delta3->empty() ? deba@2548: _delta3->prio() : std::numeric_limits::max(); deba@2548: deba@2548: Value d4 = !_delta4->empty() ? deba@2548: _delta4->prio() : std::numeric_limits::max(); deba@2548: deba@2548: _delta_sum = d2; OpType ot = D2; deba@2548: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@2548: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@2548: deba@2548: if (_delta_sum == std::numeric_limits::max()) { deba@2548: return false; deba@2548: } deba@2548: deba@2548: switch (ot) { deba@2548: case D2: deba@2548: { deba@2548: int blossom = _delta2->top(); deba@2548: Node n = _blossom_set->classTop(blossom); deba@2548: Edge e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@2548: extendOnEdge(e); deba@2548: } deba@2548: break; deba@2548: case D3: deba@2548: { deba@2548: UEdge e = _delta3->top(); deba@2548: deba@2548: int left_blossom = _blossom_set->find(_ugraph.source(e)); deba@2548: int right_blossom = _blossom_set->find(_ugraph.target(e)); deba@2548: deba@2548: if (left_blossom == right_blossom) { deba@2548: _delta3->pop(); deba@2548: } else { deba@2548: int left_tree = _tree_set->find(left_blossom); deba@2548: int right_tree = _tree_set->find(right_blossom); deba@2548: deba@2548: if (left_tree == right_tree) { deba@2548: shrinkOnEdge(e, left_tree); deba@2548: } else { deba@2548: augmentOnEdge(e); deba@2548: unmatched -= 2; deba@2548: } deba@2548: } deba@2548: } break; deba@2548: case D4: deba@2548: splitBlossom(_delta4->top()); deba@2548: break; deba@2548: } deba@2548: } deba@2548: extractMatching(); deba@2548: return true; deba@2548: } deba@2548: deba@2548: /// \brief Runs %MaxWeightedPerfectMatching algorithm. deba@2548: /// deba@2548: /// This method runs the %MaxWeightedPerfectMatching algorithm. deba@2548: /// deba@2548: /// \note mwm.run() is just a shortcut of the following code. deba@2548: /// \code deba@2548: /// mwm.init(); deba@2548: /// mwm.start(); deba@2548: /// \endcode deba@2548: bool run() { deba@2548: init(); deba@2548: return start(); deba@2548: } deba@2548: deba@2548: /// @} deba@2548: deba@2548: /// \name Primal solution deba@2548: /// Functions for get the primal solution, ie. the matching. deba@2548: deba@2548: /// @{ deba@2548: deba@2548: /// \brief Returns the matching value. deba@2548: /// deba@2548: /// Returns the matching value. deba@2548: Value matchingValue() const { deba@2548: Value sum = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: if ((*_matching)[n] != INVALID) { deba@2548: sum += _weight[(*_matching)[n]]; deba@2548: } deba@2548: } deba@2548: return sum /= 2; deba@2548: } deba@2548: deba@2548: /// \brief Returns true when the edge is in the matching. deba@2548: /// deba@2548: /// Returns true when the edge is in the matching. deba@2548: bool matching(const UEdge& edge) const { deba@2548: return (*_matching)[_ugraph.source(edge)] == _ugraph.direct(edge, true); deba@2548: } deba@2548: deba@2548: /// \brief Returns the incident matching edge. deba@2548: /// deba@2548: /// Returns the incident matching edge from given node. deba@2548: Edge matching(const Node& node) const { deba@2548: return (*_matching)[node]; deba@2548: } deba@2548: deba@2548: /// \brief Returns the mate of the node. deba@2548: /// deba@2548: /// Returns the adjancent node in a mathcing edge. deba@2548: Node mate(const Node& node) const { deba@2548: return _ugraph.target((*_matching)[node]); deba@2548: } deba@2548: deba@2548: /// @} deba@2548: deba@2548: /// \name Dual solution deba@2548: /// Functions for get the dual solution. deba@2548: deba@2548: /// @{ deba@2548: deba@2548: /// \brief Returns the value of the dual solution. deba@2548: /// deba@2548: /// Returns the value of the dual solution. It should be equal to deba@2548: /// the primal value scaled by \ref dualScale "dual scale". deba@2548: Value dualValue() const { deba@2548: Value sum = 0; deba@2548: for (NodeIt n(_ugraph); n != INVALID; ++n) { deba@2548: sum += nodeValue(n); deba@2548: } deba@2548: for (int i = 0; i < blossomNum(); ++i) { deba@2548: sum += blossomValue(i) * (blossomSize(i) / 2); deba@2548: } deba@2548: return sum; deba@2548: } deba@2548: deba@2548: /// \brief Returns the value of the node. deba@2548: /// deba@2548: /// Returns the the value of the node. deba@2548: Value nodeValue(const Node& n) const { deba@2548: return (*_node_potential)[n]; deba@2548: } deba@2548: deba@2548: /// \brief Returns the number of the blossoms in the basis. deba@2548: /// deba@2548: /// Returns the number of the blossoms in the basis. deba@2548: /// \see BlossomIt deba@2548: int blossomNum() const { deba@2548: return _blossom_potential.size(); deba@2548: } deba@2548: deba@2548: deba@2548: /// \brief Returns the number of the nodes in the blossom. deba@2548: /// deba@2548: /// Returns the number of the nodes in the blossom. deba@2548: int blossomSize(int k) const { deba@2548: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@2548: } deba@2548: deba@2548: /// \brief Returns the value of the blossom. deba@2548: /// deba@2548: /// Returns the the value of the blossom. deba@2548: /// \see BlossomIt deba@2548: Value blossomValue(int k) const { deba@2548: return _blossom_potential[k].value; deba@2548: } deba@2548: deba@2548: /// \brief Lemon iterator for get the items of the blossom. deba@2548: /// deba@2548: /// Lemon iterator for get the nodes of the blossom. This class deba@2548: /// provides a common style lemon iterator which gives back a deba@2548: /// subset of the nodes. deba@2548: class BlossomIt { deba@2548: public: deba@2548: deba@2548: /// \brief Constructor. deba@2548: /// deba@2548: /// Constructor for get the nodes of the variable. deba@2548: BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable) deba@2548: : _algorithm(&algorithm) deba@2548: { deba@2548: _index = _algorithm->_blossom_potential[variable].begin; deba@2548: _last = _algorithm->_blossom_potential[variable].end; deba@2548: } deba@2548: deba@2548: /// \brief Invalid constructor. deba@2548: /// deba@2548: /// Invalid constructor. deba@2548: BlossomIt(Invalid) : _index(-1) {} deba@2548: deba@2548: /// \brief Conversion to node. deba@2548: /// deba@2548: /// Conversion to node. deba@2548: operator Node() const { deba@2548: return _algorithm ? _algorithm->_blossom_node_list[_index] : INVALID; deba@2548: } deba@2548: deba@2548: /// \brief Increment operator. deba@2548: /// deba@2548: /// Increment operator. deba@2548: BlossomIt& operator++() { deba@2548: ++_index; deba@2548: if (_index == _last) { deba@2548: _index = -1; deba@2548: } deba@2548: return *this; deba@2548: } deba@2548: deba@2548: bool operator==(const BlossomIt& it) const { deba@2548: return _index == it._index; deba@2548: } deba@2548: bool operator!=(const BlossomIt& it) const { deba@2548: return _index != it._index; deba@2548: } deba@2548: deba@2548: private: deba@2548: const MaxWeightedPerfectMatching* _algorithm; deba@2548: int _last; deba@2548: int _index; deba@2548: }; deba@2548: deba@2548: /// @} deba@2548: deba@2548: }; deba@2548: jacint@1077: jacint@1077: } //END OF NAMESPACE LEMON jacint@1077: jacint@1165: #endif //LEMON_MAX_MATCHING_H