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