lemon/max_matching.h
author alpar
Fri, 14 Jul 2006 10:50:05 +0000
changeset 2138 a683f63b54e2
parent 2023 f34f044a043c
child 2205 c20b0eb92a33
permissions -rwxr-xr-x
glemon is in a separate repository.
jacint@1077
     1
/* -*- C++ -*-
jacint@1077
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
jacint@1077
     8
 *
jacint@1077
     9
 * Permission to use, modify and distribute this software is granted
jacint@1077
    10
 * provided that this copyright notice appears in all copies. For
jacint@1077
    11
 * precise terms see the accompanying LICENSE file.
jacint@1077
    12
 *
jacint@1077
    13
 * This software is provided "AS IS" with no warranty of any kind,
jacint@1077
    14
 * express or implied, and with no claim as to its suitability for any
jacint@1077
    15
 * purpose.
jacint@1077
    16
 *
jacint@1077
    17
 */
jacint@1077
    18
jacint@1077
    19
#ifndef LEMON_MAX_MATCHING_H
jacint@1077
    20
#define LEMON_MAX_MATCHING_H
jacint@1077
    21
jacint@1077
    22
#include <queue>
deba@1993
    23
#include <lemon/bits/invalid.h>
jacint@1093
    24
#include <lemon/unionfind.h>
jacint@1077
    25
#include <lemon/graph_utils.h>
jacint@1077
    26
deba@2042
    27
///\ingroup matching
jacint@1077
    28
///\file
deba@2042
    29
///\brief Maximum matching algorithm in undirected graph.
jacint@1077
    30
jacint@1077
    31
namespace lemon {
jacint@1077
    32
deba@2042
    33
  /// \ingroup matching
jacint@1077
    34
jacint@1077
    35
  ///Edmonds' alternating forest maximum matching algorithm.
jacint@1077
    36
jacint@1077
    37
  ///This class provides Edmonds' alternating forest matching
jacint@1077
    38
  ///algorithm. The starting matching (if any) can be passed to the
jacint@1077
    39
  ///algorithm using read-in functions \ref readNMapNode, \ref
jacint@1077
    40
  ///readNMapEdge or \ref readEMapBool depending on the container. The
jacint@1077
    41
  ///resulting maximum matching can be attained by write-out functions
jacint@1077
    42
  ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
jacint@1077
    43
  ///depending on the preferred container. 
jacint@1077
    44
  ///
jacint@1077
    45
  ///The dual side of a matching is a map of the nodes to
jacint@1077
    46
  ///MaxMatching::pos_enum, having values D, A and C showing the
jacint@1077
    47
  ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
jacint@1077
    48
  ///a graph with factor-critical components, the nodes in A form the
jacint@1077
    49
  ///barrier, and the nodes in C induce a graph having a perfect
jacint@1077
    50
  ///matching. This decomposition can be attained by calling \ref
jacint@1090
    51
  ///writePos after running the algorithm. 
jacint@1077
    52
  ///
jacint@1077
    53
  ///\param Graph The undirected graph type the algorithm runs on.
jacint@1077
    54
  ///
jacint@1077
    55
  ///\author Jacint Szabo  
jacint@1077
    56
  template <typename Graph>
jacint@1077
    57
  class MaxMatching {
jacint@1165
    58
jacint@1165
    59
  protected:
jacint@1165
    60
jacint@1077
    61
    typedef typename Graph::Node Node;
jacint@1077
    62
    typedef typename Graph::Edge Edge;
klao@1909
    63
    typedef typename Graph::UEdge UEdge;
klao@1909
    64
    typedef typename Graph::UEdgeIt UEdgeIt;
jacint@1077
    65
    typedef typename Graph::NodeIt NodeIt;
jacint@1077
    66
    typedef typename Graph::IncEdgeIt IncEdgeIt;
jacint@1077
    67
jacint@1077
    68
    typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
jacint@1077
    69
jacint@1077
    70
  public:
jacint@1077
    71
    
jacint@1077
    72
    ///Indicates the Gallai-Edmonds decomposition of the graph.
jacint@1077
    73
jacint@1077
    74
    ///Indicates the Gallai-Edmonds decomposition of the graph, which
jacint@1077
    75
    ///shows an upper bound on the size of a maximum matching. The
jacint@1077
    76
    ///nodes with pos_enum \c D induce a graph with factor-critical
jacint@1077
    77
    ///components, the nodes in \c A form the canonical barrier, and the
jacint@1077
    78
    ///nodes in \c C induce a graph having a perfect matching. 
jacint@1077
    79
    enum pos_enum {
jacint@1077
    80
      D=0,
jacint@1077
    81
      A=1,
jacint@1077
    82
      C=2
jacint@1077
    83
    }; 
jacint@1077
    84
jacint@1165
    85
  protected:
jacint@1077
    86
jacint@1077
    87
    static const int HEUR_density=2;
jacint@1077
    88
    const Graph& g;
jacint@1093
    89
    typename Graph::template NodeMap<Node> _mate;
jacint@1077
    90
    typename Graph::template NodeMap<pos_enum> position;
jacint@1077
    91
     
jacint@1077
    92
  public:
jacint@1077
    93
    
jacint@1093
    94
    MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
jacint@1077
    95
jacint@1077
    96
    ///Runs Edmonds' algorithm.
jacint@1077
    97
jacint@1077
    98
    ///Runs Edmonds' algorithm for sparse graphs (number of edges <
jacint@1077
    99
    ///2*number of nodes), and a heuristical Edmonds' algorithm with a
jacint@1090
   100
    ///heuristic of postponing shrinks for dense graphs. 
alpar@1587
   101
    void run() {
klao@1909
   102
      if ( countUEdges(g) < HEUR_density*countNodes(g) ) {
alpar@1587
   103
	greedyMatching();
alpar@1587
   104
	runEdmonds(0);
alpar@1587
   105
      } else runEdmonds(1);
alpar@1587
   106
    }
alpar@1587
   107
jacint@1077
   108
jacint@1077
   109
    ///Runs Edmonds' algorithm.
jacint@1077
   110
    
jacint@1077
   111
    ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
jacint@1077
   112
    ///Edmonds' algorithm with a heuristic of postponing shrinks,
jacint@1090
   113
    ///giving a faster algorithm for dense graphs.  
alpar@1587
   114
    void runEdmonds( int heur = 1 ) {
alpar@1587
   115
      
jacint@2023
   116
      //each vertex is put to C
alpar@1587
   117
      for(NodeIt v(g); v!=INVALID; ++v)
alpar@1587
   118
	position.set(v,C);      
alpar@1587
   119
      
alpar@1587
   120
      typename Graph::template NodeMap<Node> ear(g,INVALID); 
alpar@1587
   121
      //undefined for the base nodes of the blossoms (i.e. for the
alpar@1587
   122
      //representative elements of UFE blossom) and for the nodes in C 
alpar@1587
   123
      
alpar@1587
   124
      typename UFE::MapType blossom_base(g);
alpar@1587
   125
      UFE blossom(blossom_base);
alpar@1587
   126
      typename UFE::MapType tree_base(g);
alpar@1587
   127
      UFE tree(tree_base);
alpar@1587
   128
      //If these UFE's would be members of the class then also
alpar@1587
   129
      //blossom_base and tree_base should be a member.
alpar@1587
   130
      
jacint@2023
   131
      //We build only one tree and the other vertices uncovered by the
jacint@2023
   132
      //matching belong to C. (They can be considered as singleton
jacint@2023
   133
      //trees.) If this tree can be augmented or no more
jacint@2023
   134
      //grow/augmentation/shrink is possible then we return to this
jacint@2023
   135
      //"for" cycle.
alpar@1587
   136
      for(NodeIt v(g); v!=INVALID; ++v) {
alpar@1587
   137
	if ( position[v]==C && _mate[v]==INVALID ) {
alpar@1587
   138
	  blossom.insert(v);
alpar@1587
   139
	  tree.insert(v); 
alpar@1587
   140
	  position.set(v,D);
alpar@1587
   141
	  if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
alpar@1587
   142
	  else normShrink( v, ear, blossom, tree );
alpar@1587
   143
	}
alpar@1587
   144
      }
alpar@1587
   145
    }
alpar@1587
   146
jacint@1077
   147
jacint@1077
   148
    ///Finds a greedy matching starting from the actual matching.
jacint@1077
   149
    
jacint@1077
   150
    ///Starting form the actual matching stored, it finds a maximal
jacint@1077
   151
    ///greedy matching.
alpar@1587
   152
    void greedyMatching() {
alpar@1587
   153
      for(NodeIt v(g); v!=INVALID; ++v)
alpar@1587
   154
	if ( _mate[v]==INVALID ) {
alpar@1587
   155
	  for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
alpar@1587
   156
	    Node y=g.runningNode(e);
alpar@1587
   157
	    if ( _mate[y]==INVALID && y!=v ) {
alpar@1587
   158
	      _mate.set(v,y);
alpar@1587
   159
	      _mate.set(y,v);
alpar@1587
   160
	      break;
alpar@1587
   161
	    }
alpar@1587
   162
	  }
alpar@1587
   163
	} 
alpar@1587
   164
    }
jacint@1077
   165
jacint@1077
   166
    ///Returns the size of the actual matching stored.
jacint@1077
   167
jacint@1077
   168
    ///Returns the size of the actual matching stored. After \ref
jacint@1077
   169
    ///run() it returns the size of a maximum matching in the graph.
alpar@1587
   170
    int size() const {
alpar@1587
   171
      int s=0;
alpar@1587
   172
      for(NodeIt v(g); v!=INVALID; ++v) {
alpar@1587
   173
	if ( _mate[v]!=INVALID ) {
alpar@1587
   174
	  ++s;
alpar@1587
   175
	}
alpar@1587
   176
      }
alpar@1587
   177
      return s/2;
alpar@1587
   178
    }
alpar@1587
   179
jacint@1077
   180
jacint@1077
   181
    ///Resets the actual matching to the empty matching.
jacint@1077
   182
jacint@1077
   183
    ///Resets the actual matching to the empty matching.  
jacint@1077
   184
    ///
alpar@1587
   185
    void resetMatching() {
alpar@1587
   186
      for(NodeIt v(g); v!=INVALID; ++v)
alpar@1587
   187
	_mate.set(v,INVALID);      
alpar@1587
   188
    }
jacint@1077
   189
jacint@1093
   190
    ///Returns the mate of a node in the actual matching.
jacint@1093
   191
jacint@1093
   192
    ///Returns the mate of a \c node in the actual matching. 
jacint@1093
   193
    ///Returns INVALID if the \c node is not covered by the actual matching. 
jacint@1093
   194
    Node mate(Node& node) const {
jacint@1093
   195
      return _mate[node];
jacint@1093
   196
    } 
jacint@1093
   197
jacint@1165
   198
    ///Reads a matching from a \c Node valued \c Node map.
jacint@1077
   199
jacint@1165
   200
    ///Reads a matching from a \c Node valued \c Node map. This map
jacint@1165
   201
    ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
jacint@1165
   202
    ///must hold, and \c uv will be an edge of the matching.
jacint@1077
   203
    template<typename NMapN>
jacint@1077
   204
    void readNMapNode(NMapN& map) {
jacint@1077
   205
      for(NodeIt v(g); v!=INVALID; ++v) {
jacint@1093
   206
	_mate.set(v,map[v]);   
jacint@1077
   207
      } 
jacint@1077
   208
    } 
jacint@1077
   209
    
jacint@1165
   210
    ///Writes the stored matching to a \c Node valued \c Node map.
jacint@1077
   211
jacint@1165
   212
    ///Writes the stored matching to a \c Node valued \c Node map. The
jacint@1077
   213
    ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
jacint@1077
   214
    ///map[v]==u will hold, and now \c uv is an edge of the matching.
jacint@1077
   215
    template<typename NMapN>
jacint@1077
   216
    void writeNMapNode (NMapN& map) const {
jacint@1077
   217
      for(NodeIt v(g); v!=INVALID; ++v) {
jacint@1093
   218
	map.set(v,_mate[v]);   
jacint@1077
   219
      } 
jacint@1077
   220
    } 
jacint@1077
   221
klao@1909
   222
    ///Reads a matching from an \c UEdge valued \c Node map.
jacint@1077
   223
klao@1909
   224
    ///Reads a matching from an \c UEdge valued \c Node map. \c
klao@1909
   225
    ///map[v] must be an \c UEdge incident to \c v. This map must
jacint@1165
   226
    ///have the property that if \c g.oppositeNode(u,map[u])==v then
jacint@1165
   227
    ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
marci@1172
   228
    ///joining \c u to \c v will be an edge of the matching.
jacint@1077
   229
    template<typename NMapE>
jacint@1077
   230
    void readNMapEdge(NMapE& map) {
jacint@2023
   231
      for(NodeIt v(g); v!=INVALID; ++v) {
jacint@2023
   232
	UEdge e=map[v];
jacint@1165
   233
	if ( e!=INVALID )
jacint@1166
   234
	  _mate.set(v,g.oppositeNode(v,e));
jacint@1077
   235
      } 
jacint@1077
   236
    } 
jacint@1077
   237
    
klao@1909
   238
    ///Writes the matching stored to an \c UEdge valued \c Node map.
jacint@1077
   239
klao@1909
   240
    ///Writes the stored matching to an \c UEdge valued \c Node
klao@1909
   241
    ///map. \c map[v] will be an \c UEdge incident to \c v. This
jacint@1165
   242
    ///map will have the property that if \c g.oppositeNode(u,map[u])
jacint@1165
   243
    ///== v then \c map[u]==map[v] holds, and now this edge is an edge
jacint@1165
   244
    ///of the matching.
jacint@1077
   245
    template<typename NMapE>
jacint@1077
   246
    void writeNMapEdge (NMapE& map)  const {
jacint@1077
   247
      typename Graph::template NodeMap<bool> todo(g,true); 
jacint@1077
   248
      for(NodeIt v(g); v!=INVALID; ++v) {
jacint@1093
   249
	if ( todo[v] && _mate[v]!=INVALID ) {
jacint@1093
   250
	  Node u=_mate[v];
jacint@1077
   251
	  for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
klao@1158
   252
	    if ( g.runningNode(e) == u ) {
jacint@1077
   253
	      map.set(u,e);
jacint@1077
   254
	      map.set(v,e);
jacint@1077
   255
	      todo.set(u,false);
jacint@1077
   256
	      todo.set(v,false);
jacint@1077
   257
	      break;
jacint@1077
   258
	    }
jacint@1077
   259
	  }
jacint@1077
   260
	}
jacint@1077
   261
      } 
jacint@1077
   262
    }
jacint@1077
   263
jacint@1077
   264
jacint@1165
   265
    ///Reads a matching from a \c bool valued \c Edge map.
jacint@1077
   266
    
jacint@1165
   267
    ///Reads a matching from a \c bool valued \c Edge map. This map
jacint@1165
   268
    ///must have the property that there are no two incident edges \c
jacint@1165
   269
    ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
jacint@1077
   270
    ///map[e]==true form the matching.
jacint@1077
   271
    template<typename EMapB>
jacint@1077
   272
    void readEMapBool(EMapB& map) {
klao@1909
   273
      for(UEdgeIt e(g); e!=INVALID; ++e) {
jacint@1077
   274
	if ( map[e] ) {
jacint@1077
   275
	  Node u=g.source(e);	  
jacint@1077
   276
	  Node v=g.target(e);
jacint@1093
   277
	  _mate.set(u,v);
jacint@1093
   278
	  _mate.set(v,u);
jacint@1077
   279
	} 
jacint@1077
   280
      } 
jacint@1077
   281
    }
jacint@1077
   282
jacint@1077
   283
jacint@1165
   284
    ///Writes the matching stored to a \c bool valued \c Edge map.
jacint@1077
   285
jacint@1165
   286
    ///Writes the matching stored to a \c bool valued \c Edge
jacint@1165
   287
    ///map. This map will have the property that there are no two
jacint@1165
   288
    ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
jacint@1165
   289
    ///edges \c e with \c map[e]==true form the matching.
jacint@1077
   290
    template<typename EMapB>
jacint@1077
   291
    void writeEMapBool (EMapB& map) const {
klao@1909
   292
      for(UEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
jacint@1077
   293
jacint@1077
   294
      typename Graph::template NodeMap<bool> todo(g,true); 
jacint@1077
   295
      for(NodeIt v(g); v!=INVALID; ++v) {
jacint@1093
   296
	if ( todo[v] && _mate[v]!=INVALID ) {
jacint@1093
   297
	  Node u=_mate[v];
jacint@1077
   298
	  for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
klao@1158
   299
	    if ( g.runningNode(e) == u ) {
jacint@1077
   300
	      map.set(e,true);
jacint@1077
   301
	      todo.set(u,false);
jacint@1077
   302
	      todo.set(v,false);
jacint@1077
   303
	      break;
jacint@1077
   304
	    }
jacint@1077
   305
	  }
jacint@1077
   306
	}
jacint@1077
   307
      } 
jacint@1077
   308
    }
jacint@1077
   309
jacint@1077
   310
jacint@1077
   311
    ///Writes the canonical decomposition of the graph after running
jacint@1077
   312
    ///the algorithm.
jacint@1077
   313
jacint@1090
   314
    ///After calling any run methods of the class, it writes the
jacint@1090
   315
    ///Gallai-Edmonds canonical decomposition of the graph. \c map
jacint@1090
   316
    ///must be a node map of \ref pos_enum 's.
jacint@1077
   317
    template<typename NMapEnum>
jacint@1077
   318
    void writePos (NMapEnum& map) const {
jacint@1077
   319
      for(NodeIt v(g); v!=INVALID; ++v)  map.set(v,position[v]);
jacint@1077
   320
    }
jacint@1077
   321
jacint@1077
   322
  private: 
jacint@1077
   323
jacint@1165
   324
 
jacint@1077
   325
    void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
jacint@1077
   326
		    UFE& blossom, UFE& tree);
jacint@1077
   327
alpar@1234
   328
    void normShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
jacint@1077
   329
		    UFE& blossom, UFE& tree);
jacint@1077
   330
jacint@2023
   331
    void shrink(Node x,Node y, typename Graph::template NodeMap<Node>& ear,  
jacint@2023
   332
		UFE& blossom, UFE& tree,std::queue<Node>& Q);
jacint@1077
   333
alpar@1234
   334
    void shrinkStep(Node& top, Node& middle, Node& bottom,
alpar@1234
   335
		    typename Graph::template NodeMap<Node>& ear,  
jacint@1077
   336
		    UFE& blossom, UFE& tree, std::queue<Node>& Q);
jacint@1077
   337
jacint@2023
   338
    bool growOrAugment(Node& y, Node& x, typename Graph::template 
jacint@2023
   339
		       NodeMap<Node>& ear, UFE& blossom, UFE& tree, 
jacint@2023
   340
		       std::queue<Node>& Q);
jacint@2023
   341
alpar@1234
   342
    void augment(Node x, typename Graph::template NodeMap<Node>& ear,  
jacint@1077
   343
		 UFE& blossom, UFE& tree);
jacint@1077
   344
jacint@1077
   345
  };
jacint@1077
   346
jacint@1077
   347
jacint@1077
   348
  // **********************************************************************
jacint@1077
   349
  //  IMPLEMENTATIONS
jacint@1077
   350
  // **********************************************************************
jacint@1077
   351
jacint@1077
   352
jacint@1077
   353
  template <typename Graph>
jacint@2023
   354
  void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template 
jacint@2023
   355
				      NodeMap<Node>& ear, UFE& blossom, UFE& tree) {
jacint@2023
   356
    //We have one tree which we grow, and also shrink but only if it cannot be
jacint@2023
   357
    //postponed. If we augment then we return to the "for" cycle of
jacint@2023
   358
    //runEdmonds().
jacint@1077
   359
jacint@1077
   360
    std::queue<Node> Q;   //queue of the totally unscanned nodes
jacint@1077
   361
    Q.push(v);  
jacint@1077
   362
    std::queue<Node> R;   
jacint@1077
   363
    //queue of the nodes which must be scanned for a possible shrink
jacint@1077
   364
      
jacint@1077
   365
    while ( !Q.empty() ) {
jacint@1077
   366
      Node x=Q.front();
jacint@1077
   367
      Q.pop();
jacint@2023
   368
      for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
jacint@2023
   369
	Node y=g.runningNode(e);
jacint@2023
   370
	//growOrAugment grows if y is covered by the matching and
jacint@2023
   371
	//augments if not. In this latter case it returns 1.
jacint@2023
   372
	if ( position[y]==C && growOrAugment(y, x, ear, blossom, tree, Q) ) return;
jacint@2023
   373
      }
jacint@2023
   374
      R.push(x);
jacint@1077
   375
    }
jacint@1077
   376
      
jacint@1077
   377
    while ( !R.empty() ) {
jacint@1077
   378
      Node x=R.front();
jacint@1077
   379
      R.pop();
jacint@1077
   380
	
jacint@1077
   381
      for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
klao@1158
   382
	Node y=g.runningNode(e);
jacint@1077
   383
jacint@2023
   384
	if ( position[y] == D && blossom.find(x) != blossom.find(y) )  
jacint@2023
   385
	  //Recall that we have only one tree.
jacint@2023
   386
	  shrink( x, y, ear, blossom, tree, Q);	
jacint@1077
   387
	
jacint@1077
   388
	while ( !Q.empty() ) {
jacint@1077
   389
	  Node x=Q.front();
jacint@1077
   390
	  Q.pop();
jacint@2023
   391
	  for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
jacint@2023
   392
	    Node y=g.runningNode(e);
jacint@2023
   393
	    //growOrAugment grows if y is covered by the matching and
jacint@2023
   394
	    //augments if not. In this latter case it returns 1.
jacint@2023
   395
	    if ( position[y]==C && growOrAugment(y, x, ear, blossom, tree, Q) ) return;
jacint@2023
   396
	  }
jacint@2023
   397
	  R.push(x);
jacint@1077
   398
	}
jacint@1077
   399
      } //for e
jacint@1077
   400
    } // while ( !R.empty() )
jacint@1077
   401
  }
jacint@1077
   402
jacint@1077
   403
jacint@1077
   404
  template <typename Graph>
alpar@1234
   405
  void MaxMatching<Graph>::normShrink(Node v,
alpar@1234
   406
				      typename Graph::template
alpar@1234
   407
				      NodeMap<Node>& ear,  
jacint@1077
   408
				      UFE& blossom, UFE& tree) {
jacint@2023
   409
    //We have one tree, which we grow and shrink. If we augment then we
jacint@2023
   410
    //return to the "for" cycle of runEdmonds().
jacint@2023
   411
    
jacint@1077
   412
    std::queue<Node> Q;   //queue of the unscanned nodes
jacint@1077
   413
    Q.push(v);  
jacint@1077
   414
    while ( !Q.empty() ) {
jacint@1077
   415
jacint@1077
   416
      Node x=Q.front();
jacint@1077
   417
      Q.pop();
jacint@1077
   418
	
jacint@1077
   419
      for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
klao@1158
   420
	Node y=g.runningNode(e);
jacint@1077
   421
	      
jacint@1077
   422
	switch ( position[y] ) {
jacint@1077
   423
	case D:          //x and y must be in the same tree
jacint@2023
   424
	  if ( blossom.find(x) != blossom.find(y) )
jacint@2023
   425
	    //x and y are in the same tree
jacint@2023
   426
	    shrink( x, y, ear, blossom, tree, Q);
jacint@1077
   427
	  break;
jacint@1077
   428
	case C:
jacint@2023
   429
	  //growOrAugment grows if y is covered by the matching and
jacint@2023
   430
	  //augments if not. In this latter case it returns 1.
jacint@2023
   431
	  if ( growOrAugment(y, x, ear, blossom, tree, Q) ) return;
jacint@1077
   432
	  break;
jacint@1077
   433
	default: break;
jacint@2023
   434
       	}
jacint@1077
   435
      }
jacint@1077
   436
    }
jacint@1077
   437
  }
jacint@2023
   438
  
jacint@1077
   439
jacint@1077
   440
  template <typename Graph>
jacint@2023
   441
    void MaxMatching<Graph>::shrink(Node x,Node y, typename 
jacint@2023
   442
				    Graph::template NodeMap<Node>& ear,  
jacint@2023
   443
				    UFE& blossom, UFE& tree, std::queue<Node>& Q) {
jacint@2023
   444
    //x and y are the two adjacent vertices in two blossoms.
jacint@2023
   445
   
jacint@2023
   446
    typename Graph::template NodeMap<bool> path(g,false);
jacint@2023
   447
    
jacint@2023
   448
    Node b=blossom.find(x);
jacint@2023
   449
    path.set(b,true);
jacint@2023
   450
    b=_mate[b];
jacint@2023
   451
    while ( b!=INVALID ) { 
jacint@2023
   452
      b=blossom.find(ear[b]);
jacint@2023
   453
      path.set(b,true);
jacint@2023
   454
      b=_mate[b];
jacint@2023
   455
    } //we go until the root through bases of blossoms and odd vertices
jacint@2023
   456
    
jacint@2023
   457
    Node top=y;
jacint@2023
   458
    Node middle=blossom.find(top);
jacint@2023
   459
    Node bottom=x;
jacint@2023
   460
    while ( !path[middle] )
jacint@2023
   461
      shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
jacint@2023
   462
    //Until we arrive to a node on the path, we update blossom, tree
jacint@2023
   463
    //and the positions of the odd nodes.
jacint@2023
   464
    
jacint@2023
   465
    Node base=middle;
jacint@2023
   466
    top=x;
jacint@2023
   467
    middle=blossom.find(top);
jacint@2023
   468
    bottom=y;
jacint@2023
   469
    Node blossom_base=blossom.find(base);
jacint@2023
   470
    while ( middle!=blossom_base )
jacint@2023
   471
      shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
jacint@2023
   472
    //Until we arrive to a node on the path, we update blossom, tree
jacint@2023
   473
    //and the positions of the odd nodes.
jacint@2023
   474
    
jacint@2023
   475
    blossom.makeRep(base);
jacint@1077
   476
  }
jacint@1077
   477
jacint@2023
   478
jacint@2023
   479
jacint@1077
   480
  template <typename Graph>
alpar@1234
   481
  void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom,
alpar@1234
   482
				      typename Graph::template
alpar@1234
   483
				      NodeMap<Node>& ear,  
alpar@1234
   484
				      UFE& blossom, UFE& tree,
alpar@1234
   485
				      std::queue<Node>& Q) {
jacint@2023
   486
    //We traverse a blossom and update everything.
jacint@2023
   487
    
jacint@1077
   488
    ear.set(top,bottom);
jacint@1077
   489
    Node t=top;
jacint@1077
   490
    while ( t!=middle ) {
jacint@1093
   491
      Node u=_mate[t];
jacint@1077
   492
      t=ear[u];
jacint@1077
   493
      ear.set(t,u);
jacint@1077
   494
    } 
jacint@1093
   495
    bottom=_mate[middle];
jacint@1077
   496
    position.set(bottom,D);
jacint@1077
   497
    Q.push(bottom);
jacint@1077
   498
    top=ear[bottom];		
jacint@1077
   499
    Node oldmiddle=middle;
jacint@1077
   500
    middle=blossom.find(top);
jacint@1077
   501
    tree.erase(bottom);
jacint@1077
   502
    tree.erase(oldmiddle);
jacint@1077
   503
    blossom.insert(bottom);
jacint@1077
   504
    blossom.join(bottom, oldmiddle);
jacint@1077
   505
    blossom.join(top, oldmiddle);
jacint@1077
   506
  }
jacint@1077
   507
jacint@2023
   508
jacint@2023
   509
  template <typename Graph>
jacint@2023
   510
  bool MaxMatching<Graph>::growOrAugment(Node& y, Node& x, typename Graph::template
jacint@2023
   511
					 NodeMap<Node>& ear, UFE& blossom, UFE& tree,
jacint@2023
   512
					 std::queue<Node>& Q) {
jacint@2023
   513
    //x is in a blossom in the tree, y is outside. If y is covered by
jacint@2023
   514
    //the matching we grow, otherwise we augment. In this case we
jacint@2023
   515
    //return 1.
jacint@2023
   516
    
jacint@2023
   517
    if ( _mate[y]!=INVALID ) {       //grow
jacint@2023
   518
      ear.set(y,x);
jacint@2023
   519
      Node w=_mate[y];
jacint@2023
   520
      blossom.insert(w);
jacint@2023
   521
      position.set(y,A);
jacint@2023
   522
      position.set(w,D);
jacint@2023
   523
      tree.insert(y);
jacint@2023
   524
      tree.insert(w);
jacint@2023
   525
      tree.join(y,blossom.find(x));  
jacint@2023
   526
      tree.join(w,y);  
jacint@2023
   527
      Q.push(w);
jacint@2023
   528
    } else {                      //augment 
jacint@2023
   529
      augment(x, ear, blossom, tree);
jacint@2023
   530
      _mate.set(x,y);
jacint@2023
   531
      _mate.set(y,x);
jacint@2023
   532
      return true;
jacint@2023
   533
    }
jacint@2023
   534
    return false;
jacint@2023
   535
  }
jacint@2023
   536
  
jacint@2023
   537
jacint@1077
   538
  template <typename Graph>
alpar@1234
   539
  void MaxMatching<Graph>::augment(Node x,
alpar@1234
   540
				   typename Graph::template NodeMap<Node>& ear,  
jacint@1077
   541
				   UFE& blossom, UFE& tree) { 
jacint@1093
   542
    Node v=_mate[x];
jacint@1077
   543
    while ( v!=INVALID ) {
jacint@1077
   544
	
jacint@1077
   545
      Node u=ear[v];
jacint@1093
   546
      _mate.set(v,u);
jacint@1077
   547
      Node tmp=v;
jacint@1093
   548
      v=_mate[u];
jacint@1093
   549
      _mate.set(u,tmp);
jacint@1077
   550
    }
jacint@2023
   551
    Node y=blossom.find(x);
jacint@1077
   552
    typename UFE::ItemIt it;
jacint@1077
   553
    for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {   
jacint@1077
   554
      if ( position[it] == D ) {
jacint@1077
   555
	typename UFE::ItemIt b_it;
jacint@1077
   556
	for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {  
jacint@1077
   557
	  position.set( b_it ,C);
jacint@1077
   558
	}
jacint@1077
   559
	blossom.eraseClass(it);
jacint@1077
   560
      } else position.set( it ,C);
jacint@1077
   561
    }
jacint@2023
   562
    tree.eraseClass(y);
jacint@1077
   563
jacint@1077
   564
  }
jacint@1077
   565
jacint@1077
   566
  
jacint@1077
   567
} //END OF NAMESPACE LEMON
jacint@1077
   568
jacint@1165
   569
#endif //LEMON_MAX_MATCHING_H