jacint@1077: /* -*- C++ -*- jacint@1077: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2391: * Copyright (C) 2003-2007 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: jacint@1077: #include deba@1993: #include jacint@1093: #include jacint@1077: #include jacint@1077: deba@2042: ///\ingroup matching jacint@1077: ///\file deba@2042: ///\brief Maximum matching algorithm in undirected graph. jacint@1077: jacint@1077: namespace lemon { jacint@1077: deba@2505: ///\ingroup matching jacint@1077: 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: }; jacint@1077: jacint@1077: } //END OF NAMESPACE LEMON jacint@1077: jacint@1165: #endif //LEMON_MAX_MATCHING_H