undirgrafbug
authorjacint
Fri, 07 Jan 2005 08:39:53 +0000
changeset 10574588f97ad91f
parent 1056 cbc27743e17a
child 1058 dddb01f98796
undirgrafbug
src/work/jacint/matching.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/jacint/matching.h	Fri Jan 07 08:39:53 2005 +0000
     1.3 @@ -0,0 +1,608 @@
     1.4 +// -*- C++ -*-
     1.5 +#ifndef LEMON_MAX_MATCHING_H
     1.6 +#define LEMON_MAX_MATCHING_H
     1.7 +
     1.8 +///\ingroup galgs
     1.9 +///\file
    1.10 +///\brief Maximum matching algorithm.
    1.11 +
    1.12 +#include <queue>
    1.13 +
    1.14 +
    1.15 +#include <iostream>
    1.16 +
    1.17 +
    1.18 +
    1.19 +#include <invalid.h>
    1.20 +#include <unionfind.h>
    1.21 +#include <lemon/graph_utils.h>
    1.22 +
    1.23 +namespace lemon {
    1.24 +
    1.25 +  /// \addtogroup galgs
    1.26 +  /// @{
    1.27 +
    1.28 +  ///Maximum matching algorithms class.
    1.29 +
    1.30 +  ///This class provides Edmonds' alternating forest matching
    1.31 +  ///algorithm. The starting matching (if any) can be passed to the
    1.32 +  ///algorithm using read-in functions \ref readNMapNode, \ref
    1.33 +  ///readNMapEdge or \ref readEMapBool depending on the container. The
    1.34 +  ///resulting maximum matching can be attained by write-out functions
    1.35 +  ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
    1.36 +  ///depending on the preferred container. 
    1.37 +  ///
    1.38 +  ///The dual side of a mathcing is a map of the nodes to
    1.39 +  ///MaxMatching::pos_enum, having values D, A and C showing the
    1.40 +  ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
    1.41 +  ///a graph with factor-critical components, the nodes in A form the
    1.42 +  ///barrier, and the nodes in C induce a graph having a perfect
    1.43 +  ///matching. This decomposition can be attained by calling \ref
    1.44 +  ///writePos after running the algorithm. Before subsequent runs,
    1.45 +  ///the function \ref resetPos() must be called.
    1.46 +  ///
    1.47 +  ///\param Graph The undirected graph type the algorithm runs on.
    1.48 +  ///
    1.49 +  ///\author Jacint Szabo  
    1.50 +  template <typename Graph>
    1.51 +  class MaxMatching {
    1.52 +    typedef typename Graph::Node Node;
    1.53 +    typedef typename Graph::Edge Edge;
    1.54 +    typedef typename Graph::UndirEdgeIt UndirEdgeIt;
    1.55 +    typedef typename Graph::NodeIt NodeIt;
    1.56 +    typedef typename Graph::IncEdgeIt IncEdgeIt;
    1.57 +
    1.58 +    typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
    1.59 +
    1.60 +  public:
    1.61 +    
    1.62 +    ///Indicates the Gallai-Edmonds decomposition of the graph.
    1.63 +
    1.64 +    ///Indicates the Gallai-Edmonds decomposition of the graph, which
    1.65 +    ///shows an upper bound on the size of a maximum matching. The
    1.66 +    ///nodes with pos_enum \c D induce a graph with factor-critical
    1.67 +    ///components, the nodes in \c A form the canonical barrier, and the
    1.68 +    ///nodes in \c C induce a graph having a perfect matching. 
    1.69 +    enum pos_enum {
    1.70 +      D=0,
    1.71 +      A=1,
    1.72 +      C=2
    1.73 +    }; 
    1.74 +
    1.75 +  private:
    1.76 +
    1.77 +    static const int HEUR_density=2;
    1.78 +    const Graph& g;
    1.79 +    typename Graph::template NodeMap<Node> mate;
    1.80 +    typename Graph::template NodeMap<pos_enum> position;
    1.81 +     
    1.82 +  public:
    1.83 +    
    1.84 +    MaxMatching(const Graph& _g) : g(_g), mate(_g,INVALID), position(_g,C) {}
    1.85 +
    1.86 +    ///Runs Edmonds' algorithm.
    1.87 +
    1.88 +    ///Runs Edmonds' algorithm for sparse graphs (countEdges <=
    1.89 +    ///2*countNodes), and a heuristical Edmonds' algorithm with a
    1.90 +    ///heuristic of postponing shrinks for dense graphs. \pre Before
    1.91 +    ///the subsequent calls \ref resetPos must be called.
    1.92 +    inline void run();
    1.93 +
    1.94 +    ///Runs Edmonds' algorithm.
    1.95 +    
    1.96 +    ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
    1.97 +    ///Edmonds' algorithm with a heuristic of postponing shrinks,
    1.98 +    ///giving a faster algorithm for dense graphs.  \pre Before the
    1.99 +    ///subsequent calls \ref resetPos must be called.
   1.100 +    void runEdmonds( int heur );
   1.101 +
   1.102 +    ///Finds a greedy matching starting from the actual matching.
   1.103 +    
   1.104 +    ///Starting form the actual matching stored, it finds a maximal
   1.105 +    ///greedy matching.
   1.106 +    void greedyMatching();
   1.107 +
   1.108 +    ///Returns the size of the actual matching stored.
   1.109 +
   1.110 +    ///Returns the size of the actual matching stored. After \ref
   1.111 +    ///run() it returns the size of a maximum matching in the graph.
   1.112 +    int size () const;
   1.113 +
   1.114 +    ///Resets the map storing the Gallai-Edmonds decomposition.
   1.115 +    
   1.116 +    ///Resets the map storing the Gallai-Edmonds decomposition of the
   1.117 +    ///graph, making it possible to run the algorithm. Must be called
   1.118 +    ///before all runs of the Edmonds algorithm, except for the first
   1.119 +    ///run.
   1.120 +    void resetPos();
   1.121 +
   1.122 +    ///Resets the actual matching to the empty matching.
   1.123 +
   1.124 +    ///Resets the actual matching to the empty matching.  
   1.125 +    ///
   1.126 +    void resetMatching();
   1.127 +
   1.128 +    ///Reads a matching from a \c Node map of \c Nodes.
   1.129 +
   1.130 +    ///Reads a matching from a \c Node map of \c Nodes. This map must be \e
   1.131 +    ///symmetric, i.e. if \c map[u]=v then \c map[v]=u must hold, and
   1.132 +    ///\c uv will be an edge of the matching.
   1.133 +    template<typename NMapN>
   1.134 +    void readNMapNode(NMapN& map) {
   1.135 +      for(NodeIt v(g); v!=INVALID; ++v) {
   1.136 +	mate.set(v,map[v]);   
   1.137 +      } 
   1.138 +    } 
   1.139 +    
   1.140 +    ///Writes the stored matching to a \c Node map of \c Nodes.
   1.141 +
   1.142 +    ///Writes the stored matching to a \c Node map of \c Nodes. The
   1.143 +    ///resulting map will be \e symmetric, i.e. if \c map[u]=v then \c
   1.144 +    ///map[v]=u will hold, and now \c uv is an edge of the matching.
   1.145 +    template<typename NMapN>
   1.146 +    void writeNMapNode (NMapN& map) const {
   1.147 +      for(NodeIt v(g); v!=INVALID; ++v) {
   1.148 +	map.set(v,mate[v]);   
   1.149 +      } 
   1.150 +    } 
   1.151 +
   1.152 +    ///Reads a matching from a \c Node map of \c Edges.
   1.153 +
   1.154 +    ///Reads a matching from a \c Node map of incident \c Edges. This
   1.155 +    ///map must have the property that if \c G.target(map[u])=v then \c
   1.156 +    ///G.target(map[v])=u must hold, and now this edge is an edge of
   1.157 +    ///the matching.
   1.158 +    template<typename NMapE>
   1.159 +    void readNMapEdge(NMapE& map) {
   1.160 +     for(NodeIt v(g); v!=INVALID; ++v) {
   1.161 +	Edge e=map[v];
   1.162 +	if ( g.valid(e) )
   1.163 +	  g.source(e) == v ? mate.set(v,g.target(e)) : mate.set(v,g.source(e)); 
   1.164 +      } 
   1.165 +    } 
   1.166 +    
   1.167 +    ///Writes the matching stored to a \c Node map of \c Edges.
   1.168 +
   1.169 +    ///Writes the stored matching to a \c Node map of incident \c
   1.170 +    ///Edges. This map will have the property that if \c
   1.171 +    ///g.target(map[u])=v then \c g.target(map[v])=u holds, and now this
   1.172 +    ///edge is an edge of the matching.
   1.173 +    template<typename NMapE>
   1.174 +    void writeNMapEdge (NMapE& map)  const {
   1.175 +      typename Graph::template NodeMap<bool> todo(g,true); 
   1.176 +      for(NodeIt v(g); v!=INVALID; ++v) {
   1.177 +	if ( todo[v] && mate[v]!=INVALID ) {
   1.178 +	  Node u=mate[v];
   1.179 +	  for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
   1.180 +	    if ( g.target(e) == u ) {
   1.181 +	      map.set(u,e);
   1.182 +	      map.set(v,e);
   1.183 +	      todo.set(u,false);
   1.184 +	      todo.set(v,false);
   1.185 +	      break;
   1.186 +	    }
   1.187 +	  }
   1.188 +	}
   1.189 +      } 
   1.190 +    }
   1.191 +
   1.192 +
   1.193 +    ///Reads a matching from an \c Edge map of \c bools.
   1.194 +    
   1.195 +    ///Reads a matching from an \c Edge map of \c bools. This map must
   1.196 +    ///have the property that there are no two adjacent edges \c e, \c
   1.197 +    ///f with \c map[e]=map[f]=true. The edges \c e with \c
   1.198 +    ///map[e]=true form the matching.
   1.199 +    template<typename EMapB>
   1.200 +    void readEMapBool(EMapB& map) {
   1.201 +      for(UndirEdgeIt e(g); e!=INVALID; ++e) {
   1.202 +	if ( map[e] ) {
   1.203 +	  Node u=g.source(e);	  
   1.204 +	  Node v=g.target(e);
   1.205 +	  mate.set(u,v);
   1.206 +	  mate.set(v,u);
   1.207 +	} 
   1.208 +      } 
   1.209 +    }
   1.210 +    //iterable boolmap?
   1.211 +
   1.212 +
   1.213 +    ///Writes the matching stored to an \c Edge map of \c bools.
   1.214 +
   1.215 +    ///Writes the matching stored to an \c Edge map of \c bools. This
   1.216 +    ///map will have the property that there are no two adjacent edges
   1.217 +    ///\c e, \c f with \c map[e]=map[f]=true. The edges \c e with \c
   1.218 +    ///map[e]=true form the matching.
   1.219 +    template<typename EMapB>
   1.220 +    void writeEMapBool (EMapB& map) const {
   1.221 +      for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
   1.222 +
   1.223 +      typename Graph::template NodeMap<bool> todo(g,true); 
   1.224 +      for(NodeIt v(g); v!=INVALID; ++v) {
   1.225 +	if ( todo[v] && mate[v]!=INVALID ) {
   1.226 +	  Node u=mate[v];
   1.227 +	  for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
   1.228 +	    if ( g.target(e) == u ) {
   1.229 +	      map.set(e,true);
   1.230 +	      todo.set(u,false);
   1.231 +	      todo.set(v,false);
   1.232 +	      break;
   1.233 +	    }
   1.234 +	  }
   1.235 +	}
   1.236 +      } 
   1.237 +    }
   1.238 +
   1.239 +
   1.240 +    ///Writes the canonical decomposition of the graph after running
   1.241 +    ///the algorithm.
   1.242 +
   1.243 +    ///After calling any run methods of the class, and before calling
   1.244 +    ///\ref resetPos(), it writes the Gallai-Edmonds canonical
   1.245 +    ///decomposition of the graph. \c map must be a node map
   1.246 +    ///of \ref pos_enum 's.
   1.247 +    template<typename NMapEnum>
   1.248 +    void writePos (NMapEnum& map) const {
   1.249 +      for(NodeIt v(g); v!=INVALID; ++v)  map.set(v,position[v]);
   1.250 +    }
   1.251 +
   1.252 +  private: 
   1.253 +
   1.254 +    void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
   1.255 +		    UFE& blossom, UFE& tree);
   1.256 +
   1.257 +    void normShrink(Node v, typename Graph::NodeMap<Node>& ear,  
   1.258 +		    UFE& blossom, UFE& tree);
   1.259 +
   1.260 +    bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,  
   1.261 +		      UFE& blossom, UFE& tree, std::queue<Node>& Q);
   1.262 +
   1.263 +    void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,  
   1.264 +		    UFE& blossom, UFE& tree, std::queue<Node>& Q);
   1.265 +
   1.266 +    void augment(Node x, typename Graph::NodeMap<Node>& ear,  
   1.267 +		 UFE& blossom, UFE& tree);
   1.268 +  };
   1.269 +
   1.270 +
   1.271 +  // **********************************************************************
   1.272 +  //  IMPLEMENTATIONS
   1.273 +  // **********************************************************************
   1.274 +
   1.275 +
   1.276 +  template <typename Graph>
   1.277 +  void MaxMatching<Graph>::run() {
   1.278 +    if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
   1.279 +      greedyMatching();
   1.280 +      runEdmonds(1);
   1.281 +    } else runEdmonds(0);
   1.282 +  }
   1.283 +
   1.284 +
   1.285 +  template <typename Graph>
   1.286 +  void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
   1.287 +      
   1.288 +    std::cout<<"Entering runEdmonds"<<std::endl;
   1.289 +
   1.290 +    typename Graph::template NodeMap<Node> ear(g,INVALID); 
   1.291 +    //undefined for the base nodes of the blossoms (i.e. for the
   1.292 +    //representative elements of UFE blossom) and for the nodes in C
   1.293 + 
   1.294 +    typename UFE::MapType blossom_base(g);
   1.295 +    UFE blossom(blossom_base);
   1.296 +    typename UFE::MapType tree_base(g);
   1.297 +    UFE tree(tree_base);
   1.298 +
   1.299 +    for(NodeIt v(g); v!=INVALID; ++v) {
   1.300 +      if ( position[v]==C && mate[v]==INVALID ) {
   1.301 +	blossom.insert(v);
   1.302 +	tree.insert(v); 
   1.303 +	position.set(v,D);
   1.304 +	if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
   1.305 +	else normShrink( v, ear, blossom, tree );
   1.306 +      }
   1.307 +    }
   1.308 +
   1.309 +
   1.310 +    std::cout<<" runEdmonds end"<<std::endl;
   1.311 +
   1.312 +
   1.313 +  }
   1.314 +    
   1.315 +  template <typename Graph>
   1.316 +  void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
   1.317 +				      UFE& blossom, UFE& tree) {
   1.318 +     
   1.319 +
   1.320 +    std::cout<<"Entering lateShrink"<<std::endl;
   1.321 +
   1.322 +
   1.323 +    std::queue<Node> Q;   //queue of the totally unscanned nodes
   1.324 +    Q.push(v);  
   1.325 +    std::queue<Node> R;   
   1.326 +    //queue of the nodes which must be scanned for a possible shrink
   1.327 +      
   1.328 +    while ( !Q.empty() ) {
   1.329 +      Node x=Q.front();
   1.330 +      Q.pop();
   1.331 +      if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
   1.332 +      else R.push(x);
   1.333 +    }
   1.334 +      
   1.335 +    while ( !R.empty() ) {
   1.336 +      Node x=R.front();
   1.337 +      R.pop();
   1.338 +	
   1.339 +      for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
   1.340 +	Node y=g.target(e);
   1.341 +
   1.342 +	if ( position[y] == D && blossom.find(x) != blossom.find(y) ) { 
   1.343 +	  //x and y must be in the same tree//biztos? az oddbol d-belive lettek is?
   1.344 +	
   1.345 +	  typename Graph::template NodeMap<bool> path(g,false);
   1.346 +
   1.347 +	  Node b=blossom.find(x);
   1.348 +	  path.set(b,true);
   1.349 +	  b=mate[b];
   1.350 +	  while ( b!=INVALID ) { 
   1.351 +	    b=blossom.find(ear[b]);
   1.352 +	    path.set(b,true);
   1.353 +	    b=mate[b];
   1.354 +	  } //going till the root
   1.355 +	
   1.356 +	  Node top=y;
   1.357 +	  Node middle=blossom.find(top);
   1.358 +	  Node bottom=x;
   1.359 +	  while ( !path[middle] )
   1.360 +	    shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
   1.361 +		  
   1.362 +	  Node base=middle;
   1.363 +	  top=x;
   1.364 +	  middle=blossom.find(top);
   1.365 +	  bottom=y;
   1.366 +	  Node blossom_base=blossom.find(base);
   1.367 +	  while ( middle!=blossom_base )
   1.368 +	    shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
   1.369 +		  
   1.370 +	  blossom.makeRep(base);
   1.371 +	} // if shrink is needed
   1.372 +
   1.373 +	//most nehany odd node is d-beli lett, es rajuk az is megnezendo hogy mely d-beliekkel szonszedosak mas faban
   1.374 +
   1.375 +	while ( !Q.empty() ) {
   1.376 +	  Node x=Q.front();
   1.377 +	  Q.pop();
   1.378 +	  if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
   1.379 +	  else R.push(x);
   1.380 +	}
   1.381 +      } //for e
   1.382 +    } // while ( !R.empty() )
   1.383 +  }
   1.384 +
   1.385 +
   1.386 +  template <typename Graph>
   1.387 +  void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,  
   1.388 +				      UFE& blossom, UFE& tree) {
   1.389 +
   1.390 +
   1.391 +    std::cout<<"Entering normShrink with node "<<g.id(v)<<std::endl;
   1.392 +
   1.393 +
   1.394 +    std::queue<Node> Q;   //queue of the unscanned nodes
   1.395 +    Q.push(v);  
   1.396 +    while ( !Q.empty() ) {
   1.397 +
   1.398 +      std::cout<<"beginning of norm while"<<std::endl;
   1.399 +
   1.400 +      Node x=Q.front();
   1.401 +      Q.pop();
   1.402 +	
   1.403 +      for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
   1.404 +
   1.405 +
   1.406 +	for( IncEdgeIt f(g,x); f!=INVALID; ++f ) {
   1.407 +	  std::cout<<"Starting for." <<std::endl;
   1.408 +	  std::cout<<"edges " << g.id(f)<< " : " << g.id(g.target(f))<<std::endl;
   1.409 +	  std::cout<<"Ending for." <<std::endl;
   1.410 +	}
   1.411 +
   1.412 +	std::cout<<"Ending the whole for." <<std::endl;
   1.413 +	std::cout<<"for (In normShrink) with edge " << g.id(e)<< " : " << g.id(x);
   1.414 +
   1.415 +	Node y=g.target(e);
   1.416 +	
   1.417 +	std::cout<<" "<<g.id(y)<<std::endl;
   1.418 +	      
   1.419 +	switch ( position[y] ) {
   1.420 +	case D:          //x and y must be in the same tree //asszem nem!!!
   1.421 +
   1.422 +	  std::cout<<" pos[y] " << position[y]<<std::endl;
   1.423 +	  std::cout<<" blossom.find(x) ="<< g.id(blossom.find(x))<<std::endl;
   1.424 +	  std::cout<<" blossom.find(y) ="<< g.id(blossom.find(y))<<std::endl;
   1.425 +
   1.426 +
   1.427 +	  if ( blossom.find(x) != blossom.find(y) ) { //shrink
   1.428 +	    typename Graph::template NodeMap<bool> path(g,false);
   1.429 +	      
   1.430 +	    Node b=blossom.find(x);
   1.431 +	    path.set(b,true);
   1.432 +	    b=mate[b];
   1.433 +	    while ( b!=INVALID ) { 
   1.434 +	      b=blossom.find(ear[b]);
   1.435 +	      path.set(b,true);
   1.436 +	      b=mate[b];
   1.437 +	    } //going till the root
   1.438 +	
   1.439 +	    Node top=y;
   1.440 +	    Node middle=blossom.find(top);
   1.441 +	    Node bottom=x;
   1.442 +	    while ( !path[middle] )
   1.443 +	      shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
   1.444 +		
   1.445 +	    Node base=middle;
   1.446 +	    top=x;
   1.447 +	    middle=blossom.find(top);
   1.448 +	    bottom=y;
   1.449 +	    Node blossom_base=blossom.find(base);
   1.450 +	    while ( middle!=blossom_base )
   1.451 +	      shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
   1.452 +		
   1.453 +	    blossom.makeRep(base);
   1.454 +	  }
   1.455 +	  break;
   1.456 +	case C:
   1.457 +	  if ( mate[y]!=INVALID ) {   //grow
   1.458 +	    
   1.459 +	    std::cout<<"grow"<<std::endl;
   1.460 +
   1.461 +	    ear.set(y,x);
   1.462 +	    Node w=mate[y];
   1.463 +	    blossom.insert(w);
   1.464 +	    position.set(y,A); 
   1.465 +	    position.set(w,D); 
   1.466 +	    tree.insert(y);
   1.467 +	    tree.insert(w);
   1.468 +	    tree.join(y,blossom.find(x));  
   1.469 +	    tree.join(w,y);  
   1.470 +	    Q.push(w);
   1.471 +
   1.472 +	  } else {                 //augment  
   1.473 +
   1.474 +	    std::cout<<"augment"<<std::endl;
   1.475 +
   1.476 +	    augment(x, ear, blossom, tree);
   1.477 +	    mate.set(x,y);
   1.478 +	    mate.set(y,x);
   1.479 +	    return;
   1.480 +	  } //if 
   1.481 +
   1.482 +	  std::cout<<"end c eset"<<std::endl;
   1.483 +	  break;
   1.484 +	default: break;
   1.485 +	}
   1.486 +	std::cout<<"end switch"<<std::endl;
   1.487 +      }
   1.488 +    }
   1.489 +  }
   1.490 +
   1.491 +  template <typename Graph>
   1.492 +  void MaxMatching<Graph>::greedyMatching() {
   1.493 +    for(NodeIt v(g); v!=INVALID; ++v)
   1.494 +      if ( mate[v]==INVALID ) {
   1.495 +	for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
   1.496 +	  Node y=g.target(e);
   1.497 +	  if ( mate[y]==INVALID && y!=v ) {
   1.498 +	    mate.set(v,y);
   1.499 +	    mate.set(y,v);
   1.500 +	    break;
   1.501 +	  }
   1.502 +	}
   1.503 +      } 
   1.504 +  }
   1.505 +   
   1.506 +  template <typename Graph>
   1.507 +  int MaxMatching<Graph>::size() const {
   1.508 +    int s=0;
   1.509 +    for(NodeIt v(g); v!=INVALID; ++v) {
   1.510 +      if ( mate[v]!=INVALID ) {
   1.511 +	++s;
   1.512 +      }
   1.513 +    }
   1.514 +    return (int)s/2;
   1.515 +  }
   1.516 +
   1.517 +  template <typename Graph>
   1.518 +  void MaxMatching<Graph>::resetPos() {
   1.519 +    for(NodeIt v(g); v!=INVALID; ++v)
   1.520 +      position.set(v,C);      
   1.521 +  }
   1.522 +
   1.523 +  template <typename Graph>
   1.524 +  void MaxMatching<Graph>::resetMatching() {
   1.525 +    for(NodeIt v(g); v!=INVALID; ++v)
   1.526 +      mate.set(v,INVALID);      
   1.527 +  }
   1.528 +
   1.529 +  template <typename Graph>
   1.530 +  bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,  
   1.531 +					UFE& blossom, UFE& tree, std::queue<Node>& Q) {
   1.532 +    for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
   1.533 +      Node y=g.target(e);
   1.534 +	
   1.535 +      if ( position[y]==C ) {
   1.536 +	if ( mate[y]!=INVALID ) {       //grow
   1.537 +	  ear.set(y,x);
   1.538 +	  Node w=mate[y];
   1.539 +	  blossom.insert(w);
   1.540 +	  position.set(y,A);
   1.541 +	  position.set(w,D);
   1.542 +	  tree.insert(y);
   1.543 +	  tree.insert(w);
   1.544 +	  tree.join(y,blossom.find(x));  
   1.545 +	  tree.join(w,y);  
   1.546 +	  Q.push(w);
   1.547 +	} else {                      //augment 
   1.548 +	  augment(x, ear, blossom, tree);
   1.549 +	  mate.set(x,y);
   1.550 +	  mate.set(y,x);
   1.551 +	  return true;
   1.552 +	}
   1.553 +      }
   1.554 +    }
   1.555 +    return false;
   1.556 +  }
   1.557 +
   1.558 +  template <typename Graph>
   1.559 +  void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,  
   1.560 +				      UFE& blossom, UFE& tree, std::queue<Node>& Q) {
   1.561 +    ear.set(top,bottom);
   1.562 +    Node t=top;
   1.563 +    while ( t!=middle ) {
   1.564 +      Node u=mate[t];
   1.565 +      t=ear[u];
   1.566 +      ear.set(t,u);
   1.567 +    } 
   1.568 +    bottom=mate[middle];
   1.569 +    position.set(bottom,D);
   1.570 +    Q.push(bottom);
   1.571 +    top=ear[bottom];		
   1.572 +    Node oldmiddle=middle;
   1.573 +    middle=blossom.find(top);
   1.574 +    tree.erase(bottom);
   1.575 +    tree.erase(oldmiddle);
   1.576 +    blossom.insert(bottom);
   1.577 +    blossom.join(bottom, oldmiddle);
   1.578 +    blossom.join(top, oldmiddle);
   1.579 +  }
   1.580 +
   1.581 +  template <typename Graph>
   1.582 +  void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,  
   1.583 +				   UFE& blossom, UFE& tree) { 
   1.584 +    Node v=mate[x];
   1.585 +    while ( v!=INVALID ) {
   1.586 +	
   1.587 +      Node u=ear[v];
   1.588 +      mate.set(v,u);
   1.589 +      Node tmp=v;
   1.590 +      v=mate[u];
   1.591 +      mate.set(u,tmp);
   1.592 +    }
   1.593 +    typename UFE::ItemIt it;
   1.594 +    for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {   
   1.595 +      if ( position[it] == D ) {
   1.596 +	typename UFE::ItemIt b_it;
   1.597 +	for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {  
   1.598 +	  position.set( b_it ,C);
   1.599 +	}
   1.600 +	blossom.eraseClass(it);
   1.601 +      } else position.set( it ,C);
   1.602 +    }
   1.603 +    tree.eraseClass(x);
   1.604 +
   1.605 +  }
   1.606 +
   1.607 +  /// @}
   1.608 +  
   1.609 +} //END OF NAMESPACE LEMON
   1.610 +
   1.611 +#endif //EDMONDS_H