lemon/pr_bipartite_matching.h
author deba
Sat, 11 Aug 2007 16:34:41 +0000
changeset 2462 7a096a6bf53a
parent 2391 lemon/bp_matching.h@14a343be7a5a
child 2463 19651a04d056
permissions -rw-r--r--
Common interface for bipartite matchings
Some useful query function for push-relabel based matching

The naming should be rethink for these classes
for example: pr-ap prefix for push-relabel and augmenting path
algorithms
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@2462
    62
    PrBipartiteMatching(const Graph &g) :
alpar@2353
    63
      _g(g),
alpar@2353
    64
      _node_num(countBNodes(g)),
deba@2462
    65
      _matching(g),
alpar@2353
    66
      _levels(g,_node_num),
alpar@2353
    67
      _cov(g,0)
alpar@2353
    68
    {
alpar@2353
    69
    }
alpar@2353
    70
    
deba@2462
    71
    /// \name Execution control 
deba@2462
    72
    /// The simplest way to execute the algorithm is to use one of the
deba@2462
    73
    /// member functions called \c run(). \n
deba@2462
    74
    /// If you need more control on the execution, first
deba@2462
    75
    /// you must call \ref init() and then one variant of the start()
deba@2462
    76
    /// member. 
deba@2462
    77
deba@2462
    78
    /// @{
deba@2462
    79
deba@2462
    80
    ///Initialize the data structures
deba@2462
    81
deba@2462
    82
    ///This function constructs a prematching first, which is a
deba@2462
    83
    ///regular matching on the A-side of the graph, but on the B-side
deba@2462
    84
    ///each node could cover more matching edges. After that, the
deba@2462
    85
    ///B-nodes which multiple matched, will be pushed into the lowest
deba@2462
    86
    ///level of the Elevator. The remaning B-nodes will be pushed to
deba@2462
    87
    ///the consequent levels respect to a Bfs on following graph: the
deba@2462
    88
    ///nodes are the B-nodes of the original bipartite graph and two
deba@2462
    89
    ///nodes are adjacent if a node can pass over a matching edge to
deba@2462
    90
    ///an other node. The source of the Bfs are the lowest level
deba@2462
    91
    ///nodes. Last, the reached B-nodes without covered matching edge
deba@2462
    92
    ///becomes active.
deba@2462
    93
    void init() {
deba@2462
    94
      _matching_size=0;
deba@2462
    95
      _empty_level=_node_num;
alpar@2353
    96
      for(ANodeIt n(_g);n!=INVALID;++n)
alpar@2353
    97
	if((_matching[n]=IncEdgeIt(_g,n))!=INVALID)
deba@2462
    98
	  ++_cov[_g.bNode(_matching[n])];
alpar@2353
    99
alpar@2353
   100
      std::queue<Node> q;
alpar@2353
   101
      _levels.initStart();
alpar@2353
   102
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
   103
	if(_cov[n]>1) {
alpar@2353
   104
	  _levels.initAddItem(n);
alpar@2353
   105
	  q.push(n);
alpar@2353
   106
	}
alpar@2353
   107
      int hlev=0;
alpar@2353
   108
      while(!q.empty()) {
alpar@2353
   109
	Node n=q.front();
alpar@2353
   110
	q.pop();
alpar@2353
   111
	int nlev=_levels[n]+1;
alpar@2353
   112
	for(IncEdgeIt e(_g,n);e!=INVALID;++e) {
alpar@2353
   113
	  Node m=_g.runningNode(e);
alpar@2353
   114
	  if(e==_matching[m]) {
alpar@2353
   115
	    for(IncEdgeIt f(_g,m);f!=INVALID;++f) {
alpar@2353
   116
	      Node r=_g.runningNode(f);
alpar@2353
   117
	      if(_levels[r]>nlev) {
alpar@2353
   118
		for(;nlev>hlev;hlev++)
alpar@2353
   119
		  _levels.initNewLevel();
alpar@2353
   120
		_levels.initAddItem(r);
alpar@2353
   121
		q.push(r);
alpar@2353
   122
	      }
alpar@2353
   123
	    }
alpar@2353
   124
	  }
alpar@2353
   125
	}
alpar@2353
   126
      }
alpar@2353
   127
      _levels.initFinish();
alpar@2353
   128
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
   129
	if(_cov[n]<1&&_levels[n]<_node_num)
alpar@2353
   130
	  _levels.activate(n);
alpar@2353
   131
    }
alpar@2353
   132
deba@2462
   133
    ///Start the main phase of the algorithm
alpar@2353
   134
    
deba@2462
   135
    ///This algorithm calculates the maximum matching with the
deba@2462
   136
    ///push-relabel principle. This function should be called just
deba@2462
   137
    ///after the init() function which already set the initial
deba@2462
   138
    ///prematching, the level function on the B-nodes and the active,
deba@2462
   139
    ///ie. unmatched B-nodes.
deba@2462
   140
    ///
deba@2462
   141
    ///The algorithm always takes highest active B-node, and it try to
deba@2462
   142
    ///find a B-node which is eligible to pass over one of it's
deba@2462
   143
    ///matching edge. This condition holds when the B-node is one
deba@2462
   144
    ///level lower, and the opposite node of it's matching edge is
deba@2462
   145
    ///adjacent to the highest active node. In this case the current
deba@2462
   146
    ///node steals the matching edge and becomes inactive. If there is
deba@2462
   147
    ///not eligible node then the highest active node should be lift
deba@2462
   148
    ///to the next proper level.
deba@2462
   149
    ///
deba@2462
   150
    ///The nodes should not lift higher than the number of the
deba@2462
   151
    ///B-nodes, if a node reach this level it remains unmatched. If
deba@2462
   152
    ///during the execution one level becomes empty the nodes above it
deba@2462
   153
    ///can be deactivated and lift to the highest level.
deba@2462
   154
    void start() {
alpar@2353
   155
      Node act;
alpar@2353
   156
      Node bact=INVALID;
alpar@2353
   157
      Node last_activated=INVALID;
alpar@2353
   158
      while((act=_levels.highestActive())!=INVALID) {
alpar@2353
   159
	last_activated=INVALID;
alpar@2353
   160
	int actlevel=_levels[act];
alpar@2353
   161
	
alpar@2353
   162
	UEdge bedge=INVALID;
alpar@2353
   163
	int nlevel=_node_num;
alpar@2353
   164
	{
alpar@2353
   165
	  int nnlevel;
alpar@2353
   166
	  for(IncEdgeIt tbedge(_g,act);
alpar@2353
   167
	      tbedge!=INVALID && nlevel>=actlevel;
alpar@2353
   168
	      ++tbedge)
alpar@2353
   169
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
alpar@2353
   170
	       nlevel)
alpar@2353
   171
	      {
alpar@2353
   172
		nlevel=nnlevel;
alpar@2353
   173
		bedge=tbedge;
alpar@2353
   174
	      }
alpar@2353
   175
	}
alpar@2353
   176
	if(nlevel<_node_num) {
alpar@2353
   177
	  if(nlevel>=actlevel)
alpar@2353
   178
	    _levels.liftHighestActiveTo(nlevel+1);
alpar@2353
   179
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
alpar@2353
   180
	  if(--_cov[bact]<1) {
alpar@2353
   181
	    _levels.activate(bact);
alpar@2353
   182
	    last_activated=bact;
alpar@2353
   183
	  }
alpar@2353
   184
	  _matching[_g.aNode(bedge)]=bedge;
alpar@2353
   185
	  _cov[act]=1;
alpar@2353
   186
	  _levels.deactivate(act);
alpar@2353
   187
	}
alpar@2353
   188
	else {
alpar@2353
   189
	  if(_node_num>actlevel) 
alpar@2353
   190
	    _levels.liftHighestActiveTo(_node_num);
alpar@2353
   191
	  _levels.deactivate(act); 
alpar@2353
   192
	}
alpar@2353
   193
alpar@2353
   194
	if(_levels.onLevel(actlevel)==0)
deba@2462
   195
	  _levels.liftToTop(actlevel);
alpar@2353
   196
      }
deba@2462
   197
      
deba@2462
   198
      _matching_size = _node_num;
deba@2462
   199
      for(ANodeIt n(_g);n!=INVALID;++n)
deba@2462
   200
	if(_matching[n]==INVALID) _matching_size--;
deba@2462
   201
	else if (_cov[_g.bNode(_matching[n])]>1) {
deba@2462
   202
	  _cov[_g.bNode(_matching[n])]--;
deba@2462
   203
	  _matching_size--;
deba@2462
   204
	  _matching[n]=INVALID;
deba@2462
   205
	}
alpar@2353
   206
    }
deba@2462
   207
deba@2462
   208
    ///Start the algorithm to find a perfect matching
deba@2462
   209
deba@2462
   210
    ///This function is close to identical to the simple start()
deba@2462
   211
    ///member function but it calculates just perfect matching.
deba@2462
   212
    ///However, the perfect property is only checked on the B-side of
deba@2462
   213
    ///the graph
deba@2462
   214
    ///
deba@2462
   215
    ///The main difference between the two function is the handling of
deba@2462
   216
    ///the empty levels. The simple start() function let the nodes
deba@2462
   217
    ///above the empty levels unmatched while this variant if it find
deba@2462
   218
    ///an empty level immediately terminates and gives back false
deba@2462
   219
    ///return value.
deba@2462
   220
    bool startPerfect() {
deba@2462
   221
      Node act;
deba@2462
   222
      Node bact=INVALID;
deba@2462
   223
      Node last_activated=INVALID;
deba@2462
   224
      while((act=_levels.highestActive())!=INVALID) {
deba@2462
   225
	last_activated=INVALID;
deba@2462
   226
	int actlevel=_levels[act];
deba@2462
   227
	
deba@2462
   228
	UEdge bedge=INVALID;
deba@2462
   229
	int nlevel=_node_num;
deba@2462
   230
	{
deba@2462
   231
	  int nnlevel;
deba@2462
   232
	  for(IncEdgeIt tbedge(_g,act);
deba@2462
   233
	      tbedge!=INVALID && nlevel>=actlevel;
deba@2462
   234
	      ++tbedge)
deba@2462
   235
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
deba@2462
   236
	       nlevel)
deba@2462
   237
	      {
deba@2462
   238
		nlevel=nnlevel;
deba@2462
   239
		bedge=tbedge;
deba@2462
   240
	      }
deba@2462
   241
	}
deba@2462
   242
	if(nlevel<_node_num) {
deba@2462
   243
	  if(nlevel>=actlevel)
deba@2462
   244
	    _levels.liftHighestActiveTo(nlevel+1);
deba@2462
   245
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
deba@2462
   246
	  if(--_cov[bact]<1) {
deba@2462
   247
	    _levels.activate(bact);
deba@2462
   248
	    last_activated=bact;
deba@2462
   249
	  }
deba@2462
   250
	  _matching[_g.aNode(bedge)]=bedge;
deba@2462
   251
	  _cov[act]=1;
deba@2462
   252
	  _levels.deactivate(act);
deba@2462
   253
	}
deba@2462
   254
	else {
deba@2462
   255
	  if(_node_num>actlevel) 
deba@2462
   256
	    _levels.liftHighestActiveTo(_node_num);
deba@2462
   257
	  _levels.deactivate(act); 
deba@2462
   258
	}
deba@2462
   259
deba@2462
   260
	if(_levels.onLevel(actlevel)==0)
deba@2462
   261
	  _empty_level=actlevel;
deba@2462
   262
	  return false;
deba@2462
   263
      }
deba@2462
   264
      return true;
deba@2462
   265
    }
deba@2462
   266
  
deba@2462
   267
    ///Runs the algorithm
deba@2462
   268
    
deba@2462
   269
    ///Just a shortcut for the next code:
deba@2462
   270
    ///\code
deba@2462
   271
    /// init();
deba@2462
   272
    /// start();
deba@2462
   273
    ///\endcode
deba@2462
   274
    void run() {
deba@2462
   275
      init();
deba@2462
   276
      start();
deba@2462
   277
    }
deba@2462
   278
    
deba@2462
   279
    ///Runs the algorithm to find a perfect matching
deba@2462
   280
    
deba@2462
   281
    ///Just a shortcut for the next code:
deba@2462
   282
    ///\code
deba@2462
   283
    /// init();
deba@2462
   284
    /// startPerfect();
deba@2462
   285
    ///\endcode
deba@2462
   286
    ///
deba@2462
   287
    ///\note If the two nodesets of the graph have different size then
deba@2462
   288
    ///this algorithm checks the perfect property on the B-side.
deba@2462
   289
    bool runPerfect() {
deba@2462
   290
      init();
deba@2462
   291
      return startPerfect();
deba@2462
   292
    }
deba@2462
   293
deba@2462
   294
    ///Runs the algorithm to find a perfect matching
deba@2462
   295
    
deba@2462
   296
    ///Just a shortcut for the next code:
deba@2462
   297
    ///\code
deba@2462
   298
    /// init();
deba@2462
   299
    /// startPerfect();
deba@2462
   300
    ///\endcode
deba@2462
   301
    ///
deba@2462
   302
    ///\note It checks that the size of the two nodesets are equal.
deba@2462
   303
    bool checkedRunPerfect() {
deba@2462
   304
      if (countANodes(_g) != _node_num) return false;
deba@2462
   305
      init();
deba@2462
   306
      return startPerfect();
deba@2462
   307
    }
deba@2462
   308
deba@2462
   309
    ///@}
deba@2462
   310
deba@2462
   311
    /// \name Query Functions
deba@2462
   312
    /// The result of the %Matching algorithm can be obtained using these
deba@2462
   313
    /// functions.\n
deba@2462
   314
    /// Before the use of these functions,
deba@2462
   315
    /// either run() or start() must be called.
deba@2462
   316
    ///@{
deba@2462
   317
deba@2462
   318
    /// \brief Set true all matching uedge in the map.
deba@2462
   319
    /// 
deba@2462
   320
    /// Set true all matching uedge in the map. It does not change the
deba@2462
   321
    /// value mapped to the other uedges.
deba@2462
   322
    /// \return The number of the matching edges.
deba@2462
   323
    template <typename MatchingMap>
deba@2462
   324
    int quickMatching(MatchingMap& mm) const {
deba@2462
   325
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2462
   326
        if (_matching[n]!=INVALID) mm.set(_matching[n],true);
deba@2462
   327
      }
deba@2462
   328
      return _matching_size;
deba@2462
   329
    }
deba@2462
   330
deba@2462
   331
    ///\brief Set true all matching uedge in the map and the others to false.
deba@2462
   332
deba@2462
   333
    ///Set true all matching uedge in the map and the others to false.
deba@2462
   334
    ///\return The number of the matching edges.
deba@2462
   335
    template<class MatchingMap>
deba@2462
   336
    int matching(MatchingMap& mm) const {
deba@2462
   337
      for (UEdgeIt e(_g);e!=INVALID;++e) {
deba@2462
   338
        mm.set(e,e==_matching[_g.aNode(e)]);
deba@2462
   339
      }
deba@2462
   340
      return _matching_size;
deba@2462
   341
    }
deba@2462
   342
deba@2462
   343
deba@2462
   344
    ///Returns true if the given uedge is in the matching.
deba@2462
   345
deba@2462
   346
    ///It returns true if the given uedge is in the matching.
deba@2462
   347
    ///
deba@2462
   348
    bool matchingEdge(const UEdge& e) const {
deba@2462
   349
      return _matching[_g.aNode(e)]==e;
deba@2462
   350
    }
deba@2462
   351
deba@2462
   352
    ///Returns the matching edge from the node.
deba@2462
   353
deba@2462
   354
    ///Returns the matching edge from the node. If there is not such
deba@2462
   355
    ///edge it gives back \c INVALID.  
deba@2462
   356
    ///\note If the parameter node is a B-node then the running time is
deba@2462
   357
    ///propotional to the degree of the node.
deba@2462
   358
    UEdge matchingEdge(const Node& n) const {
deba@2462
   359
      if (_g.aNode(n)) {
deba@2462
   360
        return _matching[n];
deba@2462
   361
      } else {
deba@2462
   362
	for (IncEdgeIt e(_g,n);e!=INVALID;++e)
deba@2462
   363
	  if (e==_matching[_g.aNode(e)]) return e;
deba@2462
   364
	return INVALID;
deba@2462
   365
      }
deba@2462
   366
    }
deba@2462
   367
deba@2462
   368
    ///Gives back the number of the matching edges.
deba@2462
   369
deba@2462
   370
    ///Gives back the number of the matching edges.
deba@2462
   371
    int matchingSize() const {
deba@2462
   372
      return _matching_size;
deba@2462
   373
    }
deba@2462
   374
deba@2462
   375
    ///Gives back a barrier on the A-nodes
deba@2462
   376
    
deba@2462
   377
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   378
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   379
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   380
    ///the maximum matching searched then the barrier size minus its
deba@2462
   381
    ///neighbours will be exactly the unmatched nodes on the A-side.
deba@2462
   382
    ///\retval bar A WriteMap on the ANodes with bool value.
deba@2462
   383
    template<class BarrierMap>
deba@2462
   384
    void aBarrier(BarrierMap &bar) const 
alpar@2353
   385
    {
alpar@2353
   386
      for(ANodeIt n(_g);n!=INVALID;++n)
deba@2462
   387
	bar.set(n,_matching[n]==INVALID ||
deba@2462
   388
	  _levels[_g.bNode(_matching[n])]<_empty_level);  
alpar@2353
   389
    }  
deba@2462
   390
deba@2462
   391
    ///Gives back a barrier on the B-nodes
deba@2462
   392
    
deba@2462
   393
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   394
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   395
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   396
    ///the maximum matching searched then the barrier size minus its
deba@2462
   397
    ///neighbours will be exactly the unmatched nodes on the B-side.
deba@2462
   398
    ///\retval bar A WriteMap on the BNodes with bool value.
deba@2462
   399
    template<class BarrierMap>
deba@2462
   400
    void bBarrier(BarrierMap &bar) const
alpar@2353
   401
    {
deba@2462
   402
      for(BNodeIt n(_g);n!=INVALID;++n) bar.set(n,_levels[n]>=_empty_level);  
deba@2462
   403
    }
deba@2462
   404
deba@2462
   405
    ///Returns a minimum covering of the nodes.
deba@2462
   406
deba@2462
   407
    ///The minimum covering set problem is the dual solution of the
deba@2462
   408
    ///maximum bipartite matching. It provides a solution for this
deba@2462
   409
    ///problem what is proof of the optimality of the matching.
deba@2462
   410
    ///\param covering NodeMap of bool values, the nodes of the cover
deba@2462
   411
    ///set will set true while the others false.  
deba@2462
   412
    ///\return The size of the cover set.
deba@2462
   413
    ///\note This function can be called just after the algorithm have
deba@2462
   414
    ///already found a matching. 
deba@2462
   415
    template<class CoverMap>
deba@2462
   416
    int coverSet(CoverMap& covering) const {
deba@2462
   417
      int ret=0;
deba@2462
   418
      for(BNodeIt n(_g);n!=INVALID;++n) {
deba@2462
   419
	if (_levels[n]<_empty_level) { covering.set(n,true); ++ret; }
deba@2462
   420
	else covering.set(n,false);
deba@2462
   421
      }
deba@2462
   422
      for(ANodeIt n(_g);n!=INVALID;++n) {
deba@2462
   423
	if (_matching[n]!=INVALID &&
deba@2462
   424
	    _levels[_g.bNode(_matching[n])]>=_empty_level) 
deba@2462
   425
	  { covering.set(n,true); ++ret; }
deba@2462
   426
	else covering.set(n,false);
deba@2462
   427
      }
deba@2462
   428
      return ret;
deba@2462
   429
    }
deba@2462
   430
deba@2462
   431
deba@2462
   432
    /// @}
deba@2462
   433
    
alpar@2353
   434
  };
alpar@2353
   435
  
alpar@2353
   436
  
alpar@2353
   437
  ///Maximum cardinality of the matchings in a bipartite graph
alpar@2353
   438
alpar@2353
   439
  ///\ingroup matching
alpar@2353
   440
  ///This function finds the maximum cardinality of the matchings
alpar@2353
   441
  ///in a bipartite graph \c g.
alpar@2353
   442
  ///\param g An undirected bipartite graph.
alpar@2353
   443
  ///\return The cardinality of the maximum matching.
alpar@2353
   444
  ///
alpar@2353
   445
  ///\note The the implementation is based
alpar@2353
   446
  ///on the push-relabel principle.
alpar@2353
   447
  template<class Graph>
deba@2462
   448
  int prBipartiteMatching(const Graph &g)
alpar@2353
   449
  {
deba@2462
   450
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   451
    return bpm.matchingSize();
alpar@2353
   452
  }
alpar@2353
   453
alpar@2353
   454
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   455
alpar@2353
   456
  ///\ingroup matching
alpar@2353
   457
  ///This function finds a maximum cardinality matching
alpar@2353
   458
  ///in a bipartite graph \c g.
alpar@2353
   459
  ///\param g An undirected bipartite graph.
deba@2462
   460
  ///\retval matching A write UEdgeMap of value type \c bool.
deba@2462
   461
  /// The found edges will be returned in this map.
alpar@2353
   462
  ///\return The cardinality of the maximum matching.
alpar@2353
   463
  ///
alpar@2353
   464
  ///\note The the implementation is based
alpar@2353
   465
  ///on the push-relabel principle.
alpar@2353
   466
  template<class Graph,class MT>
deba@2462
   467
  int prBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   468
  {
deba@2462
   469
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   470
    bpm.run();
deba@2462
   471
    bpm.matching(matching);
deba@2462
   472
    return bpm.matchingSize();
alpar@2353
   473
  }
alpar@2353
   474
alpar@2353
   475
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   476
alpar@2353
   477
  ///\ingroup matching
alpar@2353
   478
  ///This function finds a maximum cardinality matching
alpar@2353
   479
  ///in a bipartite graph \c g.
alpar@2353
   480
  ///\param g An undirected bipartite graph.
deba@2462
   481
  ///\retval matching A write UEdgeMap of value type \c bool.
deba@2462
   482
  /// The found edges will be returned in this map.
alpar@2353
   483
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will be set
alpar@2353
   484
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   485
  /// a barrier \e B, i.e. the cardinality of \e B minus the number of its
alpar@2353
   486
  /// neighbor is equal to the number of the <tt>BNode</tt>s minus the
alpar@2353
   487
  /// cardinality of the maximum matching.
alpar@2353
   488
  ///\return The cardinality of the maximum matching.
alpar@2353
   489
  ///
alpar@2353
   490
  ///\note The the implementation is based
alpar@2353
   491
  ///on the push-relabel principle.
alpar@2353
   492
  template<class Graph,class MT, class GT>
deba@2462
   493
  int prBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   494
  {
deba@2462
   495
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   496
    bpm.run();
deba@2462
   497
    bpm.matching(matching);
deba@2462
   498
    bpm.bBarrier(barrier);
deba@2462
   499
    return bpm.matchingSize();
alpar@2353
   500
  }  
alpar@2353
   501
alpar@2353
   502
  ///Perfect matching in a bipartite graph
alpar@2353
   503
alpar@2353
   504
  ///\ingroup matching
alpar@2353
   505
  ///This function checks whether the bipartite graph \c g
alpar@2353
   506
  ///has a perfect matching.
alpar@2353
   507
  ///\param g An undirected bipartite graph.
alpar@2353
   508
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   509
  ///
alpar@2353
   510
  ///\note The the implementation is based
alpar@2353
   511
  ///on the push-relabel principle.
alpar@2353
   512
  template<class Graph>
deba@2462
   513
  bool prPerfectBipartiteMatching(const Graph &g)
alpar@2353
   514
  {
deba@2462
   515
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   516
    return bpm.runPerfect();
alpar@2353
   517
  }
alpar@2353
   518
alpar@2353
   519
  ///Perfect matching in a bipartite graph
alpar@2353
   520
alpar@2353
   521
  ///\ingroup matching
alpar@2353
   522
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   523
  ///\param g An undirected bipartite graph.
deba@2462
   524
  ///\retval matching A write UEdgeMap of value type \c bool.
deba@2462
   525
  /// The found edges will be returned in this map.
deba@2462
   526
  /// The values are unchanged if the graph
alpar@2353
   527
  /// has no perfect matching.
alpar@2353
   528
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   529
  ///
alpar@2353
   530
  ///\note The the implementation is based
alpar@2353
   531
  ///on the push-relabel principle.
alpar@2353
   532
  template<class Graph,class MT>
deba@2462
   533
  bool prPerfectBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   534
  {
deba@2462
   535
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   536
    bool ret = bpm.runPerfect();
deba@2462
   537
    if (ret) bpm.matching(matching);
deba@2462
   538
    return ret;
alpar@2353
   539
  }
alpar@2353
   540
alpar@2353
   541
  ///Perfect matching in a bipartite graph
alpar@2353
   542
alpar@2353
   543
  ///\ingroup matching
alpar@2353
   544
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   545
  ///\param g An undirected bipartite graph.
deba@2462
   546
  ///\retval matching A readwrite UEdgeMap of value type \c bool.
deba@2462
   547
  /// The found edges will be returned in this map.
deba@2462
   548
  /// The values are unchanged if the graph
alpar@2353
   549
  /// has no perfect matching.
alpar@2353
   550
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will only
alpar@2353
   551
  /// be set if \c g has no perfect matching. In this case it is set 
alpar@2353
   552
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   553
  /// a barrier, i.e. a subset \e B a of BNodes with the property that
deba@2462
   554
  /// the cardinality of \e B is greater than the number of its neighbors.
alpar@2353
   555
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   556
  ///
alpar@2353
   557
  ///\note The the implementation is based
alpar@2353
   558
  ///on the push-relabel principle.
alpar@2353
   559
  template<class Graph,class MT, class GT>
deba@2462
   560
  int prPerfectBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   561
  {
deba@2462
   562
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   563
    bool ret=bpm.runPerfect();
deba@2462
   564
    if(ret)
deba@2462
   565
      bpm.matching(matching);
deba@2462
   566
    else
deba@2462
   567
      bpm.bBarrier(barrier);
deba@2462
   568
    return ret;
alpar@2353
   569
  }  
alpar@2353
   570
}
alpar@2353
   571
alpar@2353
   572
#endif