lemon/pr_bipartite_matching.h
author deba
Sun, 30 Dec 2007 18:23:32 +0000
changeset 2550 f26368148b9c
parent 2466 feb7974cf4ec
child 2553 bfced05fa852
permissions -rw-r--r--
Changing degree of tournament tree
Bug fix in union find
Small efficiency improvment in bipartite matchings
alpar@2353
     1
/* -*- C++ -*-
alpar@2353
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2353
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2353
     8
 *
alpar@2353
     9
 * Permission to use, modify and distribute this software is granted
alpar@2353
    10
 * provided that this copyright notice appears in all copies. For
alpar@2353
    11
 * precise terms see the accompanying LICENSE file.
alpar@2353
    12
 *
alpar@2353
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2353
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2353
    15
 * purpose.
alpar@2353
    16
 *
alpar@2353
    17
 */
alpar@2353
    18
deba@2462
    19
#ifndef LEMON_PR_BIPARTITE_MATCHING
deba@2462
    20
#define LEMON_PR_BIPARTITE_MATCHING
alpar@2353
    21
alpar@2353
    22
#include <lemon/graph_utils.h>
alpar@2353
    23
#include <lemon/iterable_maps.h>
alpar@2353
    24
#include <iostream>
alpar@2353
    25
#include <queue>
alpar@2353
    26
#include <lemon/elevator.h>
alpar@2353
    27
alpar@2353
    28
///\ingroup matching
alpar@2353
    29
///\file
alpar@2353
    30
///\brief Push-prelabel maximum matching algorithms in bipartite graphs.
alpar@2353
    31
///
alpar@2353
    32
namespace lemon {
alpar@2353
    33
deba@2462
    34
  ///Max cardinality matching algorithm based on push-relabel principle
alpar@2353
    35
deba@2462
    36
  ///\ingroup matching
deba@2462
    37
  ///Bipartite Max Cardinality Matching algorithm. This class uses the
deba@2462
    38
  ///push-relabel principle which in several cases has better runtime
deba@2462
    39
  ///performance than the augmenting path solutions.
deba@2462
    40
  ///
deba@2462
    41
  ///\author Alpar Juttner
deba@2462
    42
  template<class Graph>
deba@2462
    43
  class PrBipartiteMatching {
alpar@2353
    44
    typedef typename Graph::Node Node;
alpar@2353
    45
    typedef typename Graph::ANodeIt ANodeIt;
alpar@2353
    46
    typedef typename Graph::BNodeIt BNodeIt;
alpar@2353
    47
    typedef typename Graph::UEdge UEdge;
deba@2462
    48
    typedef typename Graph::UEdgeIt UEdgeIt;
alpar@2353
    49
    typedef typename Graph::IncEdgeIt IncEdgeIt;
alpar@2353
    50
    
alpar@2353
    51
    const Graph &_g;
alpar@2353
    52
    int _node_num;
deba@2462
    53
    int _matching_size;
deba@2462
    54
    int _empty_level;
deba@2462
    55
    
deba@2462
    56
    typename Graph::template ANodeMap<typename Graph::UEdge> _matching;
alpar@2353
    57
    Elevator<Graph,typename Graph::BNode> _levels;
alpar@2353
    58
    typename Graph::template BNodeMap<int> _cov;
alpar@2353
    59
alpar@2353
    60
  public:
deba@2462
    61
deba@2466
    62
    /// Constructor
deba@2466
    63
deba@2466
    64
    /// Constructor
deba@2466
    65
    ///
deba@2462
    66
    PrBipartiteMatching(const Graph &g) :
alpar@2353
    67
      _g(g),
alpar@2353
    68
      _node_num(countBNodes(g)),
deba@2462
    69
      _matching(g),
alpar@2353
    70
      _levels(g,_node_num),
alpar@2353
    71
      _cov(g,0)
alpar@2353
    72
    {
alpar@2353
    73
    }
alpar@2353
    74
    
deba@2462
    75
    /// \name Execution control 
deba@2462
    76
    /// The simplest way to execute the algorithm is to use one of the
deba@2462
    77
    /// member functions called \c run(). \n
deba@2462
    78
    /// If you need more control on the execution, first
deba@2462
    79
    /// you must call \ref init() and then one variant of the start()
deba@2462
    80
    /// member. 
deba@2462
    81
deba@2462
    82
    /// @{
deba@2462
    83
deba@2462
    84
    ///Initialize the data structures
deba@2462
    85
deba@2462
    86
    ///This function constructs a prematching first, which is a
deba@2462
    87
    ///regular matching on the A-side of the graph, but on the B-side
deba@2462
    88
    ///each node could cover more matching edges. After that, the
deba@2462
    89
    ///B-nodes which multiple matched, will be pushed into the lowest
deba@2462
    90
    ///level of the Elevator. The remaning B-nodes will be pushed to
deba@2462
    91
    ///the consequent levels respect to a Bfs on following graph: the
deba@2462
    92
    ///nodes are the B-nodes of the original bipartite graph and two
deba@2462
    93
    ///nodes are adjacent if a node can pass over a matching edge to
deba@2462
    94
    ///an other node. The source of the Bfs are the lowest level
deba@2462
    95
    ///nodes. Last, the reached B-nodes without covered matching edge
deba@2462
    96
    ///becomes active.
deba@2462
    97
    void init() {
deba@2462
    98
      _matching_size=0;
deba@2462
    99
      _empty_level=_node_num;
alpar@2353
   100
      for(ANodeIt n(_g);n!=INVALID;++n)
alpar@2353
   101
	if((_matching[n]=IncEdgeIt(_g,n))!=INVALID)
deba@2462
   102
	  ++_cov[_g.bNode(_matching[n])];
alpar@2353
   103
alpar@2353
   104
      std::queue<Node> q;
alpar@2353
   105
      _levels.initStart();
alpar@2353
   106
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
   107
	if(_cov[n]>1) {
alpar@2353
   108
	  _levels.initAddItem(n);
alpar@2353
   109
	  q.push(n);
alpar@2353
   110
	}
alpar@2353
   111
      int hlev=0;
alpar@2353
   112
      while(!q.empty()) {
alpar@2353
   113
	Node n=q.front();
alpar@2353
   114
	q.pop();
alpar@2353
   115
	int nlev=_levels[n]+1;
alpar@2353
   116
	for(IncEdgeIt e(_g,n);e!=INVALID;++e) {
alpar@2353
   117
	  Node m=_g.runningNode(e);
alpar@2353
   118
	  if(e==_matching[m]) {
alpar@2353
   119
	    for(IncEdgeIt f(_g,m);f!=INVALID;++f) {
alpar@2353
   120
	      Node r=_g.runningNode(f);
alpar@2353
   121
	      if(_levels[r]>nlev) {
alpar@2353
   122
		for(;nlev>hlev;hlev++)
alpar@2353
   123
		  _levels.initNewLevel();
alpar@2353
   124
		_levels.initAddItem(r);
alpar@2353
   125
		q.push(r);
alpar@2353
   126
	      }
alpar@2353
   127
	    }
alpar@2353
   128
	  }
alpar@2353
   129
	}
alpar@2353
   130
      }
alpar@2353
   131
      _levels.initFinish();
alpar@2353
   132
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
   133
	if(_cov[n]<1&&_levels[n]<_node_num)
alpar@2353
   134
	  _levels.activate(n);
alpar@2353
   135
    }
alpar@2353
   136
deba@2462
   137
    ///Start the main phase of the algorithm
alpar@2353
   138
    
deba@2462
   139
    ///This algorithm calculates the maximum matching with the
deba@2462
   140
    ///push-relabel principle. This function should be called just
deba@2462
   141
    ///after the init() function which already set the initial
deba@2462
   142
    ///prematching, the level function on the B-nodes and the active,
deba@2462
   143
    ///ie. unmatched B-nodes.
deba@2462
   144
    ///
deba@2462
   145
    ///The algorithm always takes highest active B-node, and it try to
deba@2462
   146
    ///find a B-node which is eligible to pass over one of it's
deba@2462
   147
    ///matching edge. This condition holds when the B-node is one
deba@2462
   148
    ///level lower, and the opposite node of it's matching edge is
deba@2462
   149
    ///adjacent to the highest active node. In this case the current
deba@2462
   150
    ///node steals the matching edge and becomes inactive. If there is
deba@2462
   151
    ///not eligible node then the highest active node should be lift
deba@2462
   152
    ///to the next proper level.
deba@2462
   153
    ///
deba@2462
   154
    ///The nodes should not lift higher than the number of the
deba@2462
   155
    ///B-nodes, if a node reach this level it remains unmatched. If
deba@2462
   156
    ///during the execution one level becomes empty the nodes above it
deba@2462
   157
    ///can be deactivated and lift to the highest level.
deba@2462
   158
    void start() {
alpar@2353
   159
      Node act;
alpar@2353
   160
      Node bact=INVALID;
alpar@2353
   161
      Node last_activated=INVALID;
alpar@2353
   162
      while((act=_levels.highestActive())!=INVALID) {
alpar@2353
   163
	last_activated=INVALID;
alpar@2353
   164
	int actlevel=_levels[act];
alpar@2353
   165
	
alpar@2353
   166
	UEdge bedge=INVALID;
alpar@2353
   167
	int nlevel=_node_num;
alpar@2353
   168
	{
alpar@2353
   169
	  int nnlevel;
alpar@2353
   170
	  for(IncEdgeIt tbedge(_g,act);
alpar@2353
   171
	      tbedge!=INVALID && nlevel>=actlevel;
alpar@2353
   172
	      ++tbedge)
alpar@2353
   173
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
alpar@2353
   174
	       nlevel)
alpar@2353
   175
	      {
alpar@2353
   176
		nlevel=nnlevel;
alpar@2353
   177
		bedge=tbedge;
alpar@2353
   178
	      }
alpar@2353
   179
	}
alpar@2353
   180
	if(nlevel<_node_num) {
alpar@2353
   181
	  if(nlevel>=actlevel)
deba@2512
   182
	    _levels.liftHighestActive(nlevel+1);
alpar@2353
   183
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
alpar@2353
   184
	  if(--_cov[bact]<1) {
alpar@2353
   185
	    _levels.activate(bact);
alpar@2353
   186
	    last_activated=bact;
alpar@2353
   187
	  }
alpar@2353
   188
	  _matching[_g.aNode(bedge)]=bedge;
alpar@2353
   189
	  _cov[act]=1;
alpar@2353
   190
	  _levels.deactivate(act);
alpar@2353
   191
	}
alpar@2353
   192
	else {
deba@2512
   193
	  _levels.liftHighestActiveToTop();
alpar@2353
   194
	}
alpar@2353
   195
deba@2512
   196
	if(_levels.emptyLevel(actlevel))
deba@2462
   197
	  _levels.liftToTop(actlevel);
alpar@2353
   198
      }
deba@2462
   199
      
deba@2466
   200
      for(ANodeIt n(_g);n!=INVALID;++n) {
deba@2466
   201
	if (_matching[n]==INVALID)continue;
deba@2466
   202
	if (_cov[_g.bNode(_matching[n])]>1) {
deba@2462
   203
	  _cov[_g.bNode(_matching[n])]--;
deba@2462
   204
	  _matching[n]=INVALID;
deba@2466
   205
	} else {
deba@2466
   206
	  ++_matching_size;
deba@2462
   207
	}
deba@2466
   208
      }
alpar@2353
   209
    }
deba@2462
   210
deba@2462
   211
    ///Start the algorithm to find a perfect matching
deba@2462
   212
deba@2462
   213
    ///This function is close to identical to the simple start()
deba@2462
   214
    ///member function but it calculates just perfect matching.
deba@2462
   215
    ///However, the perfect property is only checked on the B-side of
deba@2462
   216
    ///the graph
deba@2462
   217
    ///
deba@2462
   218
    ///The main difference between the two function is the handling of
deba@2462
   219
    ///the empty levels. The simple start() function let the nodes
deba@2462
   220
    ///above the empty levels unmatched while this variant if it find
deba@2462
   221
    ///an empty level immediately terminates and gives back false
deba@2462
   222
    ///return value.
deba@2462
   223
    bool startPerfect() {
deba@2462
   224
      Node act;
deba@2462
   225
      Node bact=INVALID;
deba@2462
   226
      Node last_activated=INVALID;
deba@2462
   227
      while((act=_levels.highestActive())!=INVALID) {
deba@2462
   228
	last_activated=INVALID;
deba@2462
   229
	int actlevel=_levels[act];
deba@2462
   230
	
deba@2462
   231
	UEdge bedge=INVALID;
deba@2462
   232
	int nlevel=_node_num;
deba@2462
   233
	{
deba@2462
   234
	  int nnlevel;
deba@2462
   235
	  for(IncEdgeIt tbedge(_g,act);
deba@2462
   236
	      tbedge!=INVALID && nlevel>=actlevel;
deba@2462
   237
	      ++tbedge)
deba@2462
   238
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
deba@2462
   239
	       nlevel)
deba@2462
   240
	      {
deba@2462
   241
		nlevel=nnlevel;
deba@2462
   242
		bedge=tbedge;
deba@2462
   243
	      }
deba@2462
   244
	}
deba@2462
   245
	if(nlevel<_node_num) {
deba@2462
   246
	  if(nlevel>=actlevel)
deba@2512
   247
	    _levels.liftHighestActive(nlevel+1);
deba@2462
   248
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
deba@2462
   249
	  if(--_cov[bact]<1) {
deba@2462
   250
	    _levels.activate(bact);
deba@2462
   251
	    last_activated=bact;
deba@2462
   252
	  }
deba@2462
   253
	  _matching[_g.aNode(bedge)]=bedge;
deba@2462
   254
	  _cov[act]=1;
deba@2462
   255
	  _levels.deactivate(act);
deba@2462
   256
	}
deba@2462
   257
	else {
deba@2512
   258
	  _levels.liftHighestActiveToTop();
deba@2462
   259
	}
deba@2462
   260
deba@2512
   261
	if(_levels.emptyLevel(actlevel))
deba@2462
   262
	  _empty_level=actlevel;
deba@2462
   263
	  return false;
deba@2462
   264
      }
deba@2466
   265
      _matching_size = _node_num;
deba@2462
   266
      return true;
deba@2462
   267
    }
deba@2462
   268
  
deba@2462
   269
    ///Runs the algorithm
deba@2462
   270
    
deba@2462
   271
    ///Just a shortcut for the next code:
deba@2462
   272
    ///\code
deba@2462
   273
    /// init();
deba@2462
   274
    /// start();
deba@2462
   275
    ///\endcode
deba@2462
   276
    void run() {
deba@2462
   277
      init();
deba@2462
   278
      start();
deba@2462
   279
    }
deba@2462
   280
    
deba@2462
   281
    ///Runs the algorithm to find a perfect matching
deba@2462
   282
    
deba@2462
   283
    ///Just a shortcut for the next code:
deba@2462
   284
    ///\code
deba@2462
   285
    /// init();
deba@2462
   286
    /// startPerfect();
deba@2462
   287
    ///\endcode
deba@2462
   288
    ///
deba@2462
   289
    ///\note If the two nodesets of the graph have different size then
deba@2462
   290
    ///this algorithm checks the perfect property on the B-side.
deba@2462
   291
    bool runPerfect() {
deba@2462
   292
      init();
deba@2462
   293
      return startPerfect();
deba@2462
   294
    }
deba@2462
   295
deba@2462
   296
    ///Runs the algorithm to find a perfect matching
deba@2462
   297
    
deba@2462
   298
    ///Just a shortcut for the next code:
deba@2462
   299
    ///\code
deba@2462
   300
    /// init();
deba@2462
   301
    /// startPerfect();
deba@2462
   302
    ///\endcode
deba@2462
   303
    ///
deba@2462
   304
    ///\note It checks that the size of the two nodesets are equal.
deba@2462
   305
    bool checkedRunPerfect() {
deba@2462
   306
      if (countANodes(_g) != _node_num) return false;
deba@2462
   307
      init();
deba@2462
   308
      return startPerfect();
deba@2462
   309
    }
deba@2462
   310
deba@2462
   311
    ///@}
deba@2462
   312
deba@2462
   313
    /// \name Query Functions
deba@2462
   314
    /// The result of the %Matching algorithm can be obtained using these
deba@2462
   315
    /// functions.\n
deba@2462
   316
    /// Before the use of these functions,
deba@2462
   317
    /// either run() or start() must be called.
deba@2462
   318
    ///@{
deba@2462
   319
deba@2463
   320
    ///Set true all matching uedge in the map.
deba@2463
   321
deba@2463
   322
    ///Set true all matching uedge in the map. It does not change the
deba@2463
   323
    ///value mapped to the other uedges.
deba@2463
   324
    ///\return The number of the matching edges.
deba@2462
   325
    template <typename MatchingMap>
deba@2462
   326
    int quickMatching(MatchingMap& mm) const {
deba@2462
   327
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2462
   328
        if (_matching[n]!=INVALID) mm.set(_matching[n],true);
deba@2462
   329
      }
deba@2462
   330
      return _matching_size;
deba@2462
   331
    }
deba@2462
   332
deba@2463
   333
    ///Set true all matching uedge in the map and the others to false.
deba@2462
   334
deba@2462
   335
    ///Set true all matching uedge in the map and the others to false.
deba@2462
   336
    ///\return The number of the matching edges.
deba@2462
   337
    template<class MatchingMap>
deba@2462
   338
    int matching(MatchingMap& mm) const {
deba@2462
   339
      for (UEdgeIt e(_g);e!=INVALID;++e) {
deba@2462
   340
        mm.set(e,e==_matching[_g.aNode(e)]);
deba@2462
   341
      }
deba@2462
   342
      return _matching_size;
deba@2462
   343
    }
deba@2462
   344
deba@2463
   345
    ///Gives back the matching in an ANodeMap.
deba@2463
   346
deba@2463
   347
    ///Gives back the matching in an ANodeMap. The parameter should
deba@2463
   348
    ///be a write ANodeMap of UEdge values.
deba@2463
   349
    ///\return The number of the matching edges.
deba@2463
   350
    template<class MatchingMap>
deba@2463
   351
    int aMatching(MatchingMap& mm) const {
deba@2463
   352
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2463
   353
        mm.set(n,_matching[n]);
deba@2463
   354
      }
deba@2463
   355
      return _matching_size;
deba@2463
   356
    }
deba@2463
   357
deba@2463
   358
    ///Gives back the matching in a BNodeMap.
deba@2463
   359
deba@2463
   360
    ///Gives back the matching in a BNodeMap. The parameter should
deba@2463
   361
    ///be a write BNodeMap of UEdge values.
deba@2463
   362
    ///\return The number of the matching edges.
deba@2463
   363
    template<class MatchingMap>
deba@2463
   364
    int bMatching(MatchingMap& mm) const {
deba@2463
   365
      for (BNodeIt n(_g);n!=INVALID;++n) {
deba@2463
   366
        mm.set(n,INVALID);
deba@2463
   367
      }
deba@2463
   368
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2463
   369
        if (_matching[n]!=INVALID)
deba@2463
   370
	  mm.set(_g.bNode(_matching[n]),_matching[n]);
deba@2463
   371
      }
deba@2463
   372
      return _matching_size;
deba@2463
   373
    }
deba@2463
   374
deba@2462
   375
deba@2462
   376
    ///Returns true if the given uedge is in the matching.
deba@2462
   377
deba@2462
   378
    ///It returns true if the given uedge is in the matching.
deba@2462
   379
    ///
deba@2462
   380
    bool matchingEdge(const UEdge& e) const {
deba@2462
   381
      return _matching[_g.aNode(e)]==e;
deba@2462
   382
    }
deba@2462
   383
deba@2462
   384
    ///Returns the matching edge from the node.
deba@2462
   385
deba@2462
   386
    ///Returns the matching edge from the node. If there is not such
deba@2462
   387
    ///edge it gives back \c INVALID.  
deba@2462
   388
    ///\note If the parameter node is a B-node then the running time is
deba@2462
   389
    ///propotional to the degree of the node.
deba@2462
   390
    UEdge matchingEdge(const Node& n) const {
deba@2462
   391
      if (_g.aNode(n)) {
deba@2462
   392
        return _matching[n];
deba@2462
   393
      } else {
deba@2462
   394
	for (IncEdgeIt e(_g,n);e!=INVALID;++e)
deba@2462
   395
	  if (e==_matching[_g.aNode(e)]) return e;
deba@2462
   396
	return INVALID;
deba@2462
   397
      }
deba@2462
   398
    }
deba@2462
   399
deba@2462
   400
    ///Gives back the number of the matching edges.
deba@2462
   401
deba@2462
   402
    ///Gives back the number of the matching edges.
deba@2462
   403
    int matchingSize() const {
deba@2462
   404
      return _matching_size;
deba@2462
   405
    }
deba@2462
   406
deba@2462
   407
    ///Gives back a barrier on the A-nodes
deba@2462
   408
    
deba@2462
   409
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   410
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   411
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   412
    ///the maximum matching searched then the barrier size minus its
deba@2462
   413
    ///neighbours will be exactly the unmatched nodes on the A-side.
deba@2462
   414
    ///\retval bar A WriteMap on the ANodes with bool value.
deba@2462
   415
    template<class BarrierMap>
deba@2462
   416
    void aBarrier(BarrierMap &bar) const 
alpar@2353
   417
    {
alpar@2353
   418
      for(ANodeIt n(_g);n!=INVALID;++n)
deba@2462
   419
	bar.set(n,_matching[n]==INVALID ||
deba@2462
   420
	  _levels[_g.bNode(_matching[n])]<_empty_level);  
alpar@2353
   421
    }  
deba@2462
   422
deba@2462
   423
    ///Gives back a barrier on the B-nodes
deba@2462
   424
    
deba@2462
   425
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   426
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   427
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   428
    ///the maximum matching searched then the barrier size minus its
deba@2462
   429
    ///neighbours will be exactly the unmatched nodes on the B-side.
deba@2462
   430
    ///\retval bar A WriteMap on the BNodes with bool value.
deba@2462
   431
    template<class BarrierMap>
deba@2462
   432
    void bBarrier(BarrierMap &bar) const
alpar@2353
   433
    {
deba@2462
   434
      for(BNodeIt n(_g);n!=INVALID;++n) bar.set(n,_levels[n]>=_empty_level);  
deba@2462
   435
    }
deba@2462
   436
deba@2462
   437
    ///Returns a minimum covering of the nodes.
deba@2462
   438
deba@2462
   439
    ///The minimum covering set problem is the dual solution of the
deba@2462
   440
    ///maximum bipartite matching. It provides a solution for this
deba@2462
   441
    ///problem what is proof of the optimality of the matching.
deba@2462
   442
    ///\param covering NodeMap of bool values, the nodes of the cover
deba@2462
   443
    ///set will set true while the others false.  
deba@2462
   444
    ///\return The size of the cover set.
deba@2462
   445
    ///\note This function can be called just after the algorithm have
deba@2462
   446
    ///already found a matching. 
deba@2462
   447
    template<class CoverMap>
deba@2462
   448
    int coverSet(CoverMap& covering) const {
deba@2462
   449
      int ret=0;
deba@2462
   450
      for(BNodeIt n(_g);n!=INVALID;++n) {
deba@2462
   451
	if (_levels[n]<_empty_level) { covering.set(n,true); ++ret; }
deba@2462
   452
	else covering.set(n,false);
deba@2462
   453
      }
deba@2462
   454
      for(ANodeIt n(_g);n!=INVALID;++n) {
deba@2462
   455
	if (_matching[n]!=INVALID &&
deba@2462
   456
	    _levels[_g.bNode(_matching[n])]>=_empty_level) 
deba@2462
   457
	  { covering.set(n,true); ++ret; }
deba@2462
   458
	else covering.set(n,false);
deba@2462
   459
      }
deba@2462
   460
      return ret;
deba@2462
   461
    }
deba@2462
   462
deba@2462
   463
deba@2462
   464
    /// @}
deba@2462
   465
    
alpar@2353
   466
  };
alpar@2353
   467
  
alpar@2353
   468
  
alpar@2353
   469
  ///Maximum cardinality of the matchings in a bipartite graph
alpar@2353
   470
alpar@2353
   471
  ///\ingroup matching
alpar@2353
   472
  ///This function finds the maximum cardinality of the matchings
alpar@2353
   473
  ///in a bipartite graph \c g.
alpar@2353
   474
  ///\param g An undirected bipartite graph.
alpar@2353
   475
  ///\return The cardinality of the maximum matching.
alpar@2353
   476
  ///
alpar@2353
   477
  ///\note The the implementation is based
alpar@2353
   478
  ///on the push-relabel principle.
alpar@2353
   479
  template<class Graph>
deba@2462
   480
  int prBipartiteMatching(const Graph &g)
alpar@2353
   481
  {
deba@2462
   482
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   483
    return bpm.matchingSize();
alpar@2353
   484
  }
alpar@2353
   485
alpar@2353
   486
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   487
alpar@2353
   488
  ///\ingroup matching
alpar@2353
   489
  ///This function finds a maximum cardinality matching
alpar@2353
   490
  ///in a bipartite graph \c g.
alpar@2353
   491
  ///\param g An undirected bipartite graph.
deba@2463
   492
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   493
  /// The found edges will be returned in this map,
deba@2463
   494
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   495
  /// that covers the node \c n.
alpar@2353
   496
  ///\return The cardinality of the maximum matching.
alpar@2353
   497
  ///
alpar@2353
   498
  ///\note The the implementation is based
alpar@2353
   499
  ///on the push-relabel principle.
alpar@2353
   500
  template<class Graph,class MT>
deba@2462
   501
  int prBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   502
  {
deba@2462
   503
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   504
    bpm.run();
deba@2463
   505
    bpm.aMatching(matching);
deba@2462
   506
    return bpm.matchingSize();
alpar@2353
   507
  }
alpar@2353
   508
alpar@2353
   509
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   510
alpar@2353
   511
  ///\ingroup matching
alpar@2353
   512
  ///This function finds a maximum cardinality matching
alpar@2353
   513
  ///in a bipartite graph \c g.
alpar@2353
   514
  ///\param g An undirected bipartite graph.
deba@2463
   515
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   516
  /// The found edges will be returned in this map,
deba@2463
   517
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   518
  /// that covers the node \c n.
alpar@2353
   519
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will be set
alpar@2353
   520
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   521
  /// a barrier \e B, i.e. the cardinality of \e B minus the number of its
alpar@2353
   522
  /// neighbor is equal to the number of the <tt>BNode</tt>s minus the
alpar@2353
   523
  /// cardinality of the maximum matching.
alpar@2353
   524
  ///\return The cardinality of the maximum matching.
alpar@2353
   525
  ///
alpar@2353
   526
  ///\note The the implementation is based
alpar@2353
   527
  ///on the push-relabel principle.
alpar@2353
   528
  template<class Graph,class MT, class GT>
deba@2462
   529
  int prBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   530
  {
deba@2462
   531
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   532
    bpm.run();
deba@2463
   533
    bpm.aMatching(matching);
deba@2462
   534
    bpm.bBarrier(barrier);
deba@2462
   535
    return bpm.matchingSize();
alpar@2353
   536
  }  
alpar@2353
   537
alpar@2353
   538
  ///Perfect matching in a bipartite graph
alpar@2353
   539
alpar@2353
   540
  ///\ingroup matching
alpar@2353
   541
  ///This function checks whether the bipartite graph \c g
alpar@2353
   542
  ///has a perfect matching.
alpar@2353
   543
  ///\param g An undirected bipartite graph.
alpar@2353
   544
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   545
  ///
alpar@2353
   546
  ///\note The the implementation is based
alpar@2353
   547
  ///on the push-relabel principle.
alpar@2353
   548
  template<class Graph>
deba@2462
   549
  bool prPerfectBipartiteMatching(const Graph &g)
alpar@2353
   550
  {
deba@2462
   551
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   552
    return bpm.runPerfect();
alpar@2353
   553
  }
alpar@2353
   554
alpar@2353
   555
  ///Perfect matching in a bipartite graph
alpar@2353
   556
alpar@2353
   557
  ///\ingroup matching
alpar@2353
   558
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   559
  ///\param g An undirected bipartite graph.
deba@2463
   560
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   561
  /// The found edges will be returned in this map,
deba@2463
   562
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   563
  /// that covers the node \c n.
deba@2462
   564
  /// The values are unchanged if the graph
alpar@2353
   565
  /// has no perfect matching.
alpar@2353
   566
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   567
  ///
alpar@2353
   568
  ///\note The the implementation is based
alpar@2353
   569
  ///on the push-relabel principle.
alpar@2353
   570
  template<class Graph,class MT>
deba@2462
   571
  bool prPerfectBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   572
  {
deba@2462
   573
    PrBipartiteMatching<Graph> bpm(g);
deba@2463
   574
    bool ret = bpm.checkedRunPerfect();
deba@2463
   575
    if (ret) bpm.aMatching(matching);
deba@2462
   576
    return ret;
alpar@2353
   577
  }
alpar@2353
   578
alpar@2353
   579
  ///Perfect matching in a bipartite graph
alpar@2353
   580
alpar@2353
   581
  ///\ingroup matching
alpar@2353
   582
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   583
  ///\param g An undirected bipartite graph.
deba@2463
   584
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   585
  /// The found edges will be returned in this map,
deba@2463
   586
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   587
  /// that covers the node \c n.
deba@2462
   588
  /// The values are unchanged if the graph
alpar@2353
   589
  /// has no perfect matching.
alpar@2353
   590
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will only
alpar@2353
   591
  /// be set if \c g has no perfect matching. In this case it is set 
alpar@2353
   592
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   593
  /// a barrier, i.e. a subset \e B a of BNodes with the property that
deba@2462
   594
  /// the cardinality of \e B is greater than the number of its neighbors.
alpar@2353
   595
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   596
  ///
alpar@2353
   597
  ///\note The the implementation is based
alpar@2353
   598
  ///on the push-relabel principle.
alpar@2353
   599
  template<class Graph,class MT, class GT>
deba@2463
   600
  bool prPerfectBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   601
  {
deba@2462
   602
    PrBipartiteMatching<Graph> bpm(g);
deba@2463
   603
    bool ret=bpm.checkedRunPerfect();
deba@2462
   604
    if(ret)
deba@2463
   605
      bpm.aMatching(matching);
deba@2462
   606
    else
deba@2462
   607
      bpm.bBarrier(barrier);
deba@2462
   608
    return ret;
alpar@2353
   609
  }  
alpar@2353
   610
}
alpar@2353
   611
alpar@2353
   612
#endif