lemon/pr_bipartite_matching.h
author deba
Tue, 21 Aug 2007 13:22:21 +0000
changeset 2463 19651a04d056
parent 2462 7a096a6bf53a
child 2466 feb7974cf4ec
permissions -rw-r--r--
Query functions: aMatching and bMatching
Modified algorithm function interfaces
ANodeMap<UEdge> matching map
BNodeMap<bool> barrier map

Consistency between augmenting path and push-relabel algorithm
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@2463
   318
    ///Set true all matching uedge in the map.
deba@2463
   319
deba@2463
   320
    ///Set true all matching uedge in the map. It does not change the
deba@2463
   321
    ///value mapped to the other uedges.
deba@2463
   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@2463
   331
    ///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@2463
   343
    ///Gives back the matching in an ANodeMap.
deba@2463
   344
deba@2463
   345
    ///Gives back the matching in an ANodeMap. The parameter should
deba@2463
   346
    ///be a write ANodeMap of UEdge values.
deba@2463
   347
    ///\return The number of the matching edges.
deba@2463
   348
    template<class MatchingMap>
deba@2463
   349
    int aMatching(MatchingMap& mm) const {
deba@2463
   350
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2463
   351
        mm.set(n,_matching[n]);
deba@2463
   352
      }
deba@2463
   353
      return _matching_size;
deba@2463
   354
    }
deba@2463
   355
deba@2463
   356
    ///Gives back the matching in a BNodeMap.
deba@2463
   357
deba@2463
   358
    ///Gives back the matching in a BNodeMap. The parameter should
deba@2463
   359
    ///be a write BNodeMap of UEdge values.
deba@2463
   360
    ///\return The number of the matching edges.
deba@2463
   361
    template<class MatchingMap>
deba@2463
   362
    int bMatching(MatchingMap& mm) const {
deba@2463
   363
      for (BNodeIt n(_g);n!=INVALID;++n) {
deba@2463
   364
        mm.set(n,INVALID);
deba@2463
   365
      }
deba@2463
   366
      for (ANodeIt n(_g);n!=INVALID;++n) {
deba@2463
   367
        if (_matching[n]!=INVALID)
deba@2463
   368
	  mm.set(_g.bNode(_matching[n]),_matching[n]);
deba@2463
   369
      }
deba@2463
   370
      return _matching_size;
deba@2463
   371
    }
deba@2463
   372
deba@2462
   373
deba@2462
   374
    ///Returns true if the given uedge is in the matching.
deba@2462
   375
deba@2462
   376
    ///It returns true if the given uedge is in the matching.
deba@2462
   377
    ///
deba@2462
   378
    bool matchingEdge(const UEdge& e) const {
deba@2462
   379
      return _matching[_g.aNode(e)]==e;
deba@2462
   380
    }
deba@2462
   381
deba@2462
   382
    ///Returns the matching edge from the node.
deba@2462
   383
deba@2462
   384
    ///Returns the matching edge from the node. If there is not such
deba@2462
   385
    ///edge it gives back \c INVALID.  
deba@2462
   386
    ///\note If the parameter node is a B-node then the running time is
deba@2462
   387
    ///propotional to the degree of the node.
deba@2462
   388
    UEdge matchingEdge(const Node& n) const {
deba@2462
   389
      if (_g.aNode(n)) {
deba@2462
   390
        return _matching[n];
deba@2462
   391
      } else {
deba@2462
   392
	for (IncEdgeIt e(_g,n);e!=INVALID;++e)
deba@2462
   393
	  if (e==_matching[_g.aNode(e)]) return e;
deba@2462
   394
	return INVALID;
deba@2462
   395
      }
deba@2462
   396
    }
deba@2462
   397
deba@2462
   398
    ///Gives back the number of the matching edges.
deba@2462
   399
deba@2462
   400
    ///Gives back the number of the matching edges.
deba@2462
   401
    int matchingSize() const {
deba@2462
   402
      return _matching_size;
deba@2462
   403
    }
deba@2462
   404
deba@2462
   405
    ///Gives back a barrier on the A-nodes
deba@2462
   406
    
deba@2462
   407
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   408
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   409
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   410
    ///the maximum matching searched then the barrier size minus its
deba@2462
   411
    ///neighbours will be exactly the unmatched nodes on the A-side.
deba@2462
   412
    ///\retval bar A WriteMap on the ANodes with bool value.
deba@2462
   413
    template<class BarrierMap>
deba@2462
   414
    void aBarrier(BarrierMap &bar) const 
alpar@2353
   415
    {
alpar@2353
   416
      for(ANodeIt n(_g);n!=INVALID;++n)
deba@2462
   417
	bar.set(n,_matching[n]==INVALID ||
deba@2462
   418
	  _levels[_g.bNode(_matching[n])]<_empty_level);  
alpar@2353
   419
    }  
deba@2462
   420
deba@2462
   421
    ///Gives back a barrier on the B-nodes
deba@2462
   422
    
deba@2462
   423
    ///The barrier is s subset of the nodes on the same side of the
deba@2462
   424
    ///graph. If we tried to find a perfect matching and it failed
deba@2462
   425
    ///then the barrier size will be greater than its neighbours. If
deba@2462
   426
    ///the maximum matching searched then the barrier size minus its
deba@2462
   427
    ///neighbours will be exactly the unmatched nodes on the B-side.
deba@2462
   428
    ///\retval bar A WriteMap on the BNodes with bool value.
deba@2462
   429
    template<class BarrierMap>
deba@2462
   430
    void bBarrier(BarrierMap &bar) const
alpar@2353
   431
    {
deba@2462
   432
      for(BNodeIt n(_g);n!=INVALID;++n) bar.set(n,_levels[n]>=_empty_level);  
deba@2462
   433
    }
deba@2462
   434
deba@2462
   435
    ///Returns a minimum covering of the nodes.
deba@2462
   436
deba@2462
   437
    ///The minimum covering set problem is the dual solution of the
deba@2462
   438
    ///maximum bipartite matching. It provides a solution for this
deba@2462
   439
    ///problem what is proof of the optimality of the matching.
deba@2462
   440
    ///\param covering NodeMap of bool values, the nodes of the cover
deba@2462
   441
    ///set will set true while the others false.  
deba@2462
   442
    ///\return The size of the cover set.
deba@2462
   443
    ///\note This function can be called just after the algorithm have
deba@2462
   444
    ///already found a matching. 
deba@2462
   445
    template<class CoverMap>
deba@2462
   446
    int coverSet(CoverMap& covering) const {
deba@2462
   447
      int ret=0;
deba@2462
   448
      for(BNodeIt n(_g);n!=INVALID;++n) {
deba@2462
   449
	if (_levels[n]<_empty_level) { covering.set(n,true); ++ret; }
deba@2462
   450
	else covering.set(n,false);
deba@2462
   451
      }
deba@2462
   452
      for(ANodeIt n(_g);n!=INVALID;++n) {
deba@2462
   453
	if (_matching[n]!=INVALID &&
deba@2462
   454
	    _levels[_g.bNode(_matching[n])]>=_empty_level) 
deba@2462
   455
	  { covering.set(n,true); ++ret; }
deba@2462
   456
	else covering.set(n,false);
deba@2462
   457
      }
deba@2462
   458
      return ret;
deba@2462
   459
    }
deba@2462
   460
deba@2462
   461
deba@2462
   462
    /// @}
deba@2462
   463
    
alpar@2353
   464
  };
alpar@2353
   465
  
alpar@2353
   466
  
alpar@2353
   467
  ///Maximum cardinality of the matchings in a bipartite graph
alpar@2353
   468
alpar@2353
   469
  ///\ingroup matching
alpar@2353
   470
  ///This function finds the maximum cardinality of the matchings
alpar@2353
   471
  ///in a bipartite graph \c g.
alpar@2353
   472
  ///\param g An undirected bipartite graph.
alpar@2353
   473
  ///\return The cardinality of the maximum matching.
alpar@2353
   474
  ///
alpar@2353
   475
  ///\note The the implementation is based
alpar@2353
   476
  ///on the push-relabel principle.
alpar@2353
   477
  template<class Graph>
deba@2462
   478
  int prBipartiteMatching(const Graph &g)
alpar@2353
   479
  {
deba@2462
   480
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   481
    return bpm.matchingSize();
alpar@2353
   482
  }
alpar@2353
   483
alpar@2353
   484
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   485
alpar@2353
   486
  ///\ingroup matching
alpar@2353
   487
  ///This function finds a maximum cardinality matching
alpar@2353
   488
  ///in a bipartite graph \c g.
alpar@2353
   489
  ///\param g An undirected bipartite graph.
deba@2463
   490
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   491
  /// The found edges will be returned in this map,
deba@2463
   492
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   493
  /// that covers the node \c n.
alpar@2353
   494
  ///\return The cardinality of the maximum matching.
alpar@2353
   495
  ///
alpar@2353
   496
  ///\note The the implementation is based
alpar@2353
   497
  ///on the push-relabel principle.
alpar@2353
   498
  template<class Graph,class MT>
deba@2462
   499
  int prBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   500
  {
deba@2462
   501
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   502
    bpm.run();
deba@2463
   503
    bpm.aMatching(matching);
deba@2462
   504
    return bpm.matchingSize();
alpar@2353
   505
  }
alpar@2353
   506
alpar@2353
   507
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   508
alpar@2353
   509
  ///\ingroup matching
alpar@2353
   510
  ///This function finds a maximum cardinality matching
alpar@2353
   511
  ///in a bipartite graph \c g.
alpar@2353
   512
  ///\param g An undirected bipartite graph.
deba@2463
   513
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   514
  /// The found edges will be returned in this map,
deba@2463
   515
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   516
  /// that covers the node \c n.
alpar@2353
   517
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will be set
alpar@2353
   518
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   519
  /// a barrier \e B, i.e. the cardinality of \e B minus the number of its
alpar@2353
   520
  /// neighbor is equal to the number of the <tt>BNode</tt>s minus the
alpar@2353
   521
  /// cardinality of the maximum matching.
alpar@2353
   522
  ///\return The cardinality of the maximum matching.
alpar@2353
   523
  ///
alpar@2353
   524
  ///\note The the implementation is based
alpar@2353
   525
  ///on the push-relabel principle.
alpar@2353
   526
  template<class Graph,class MT, class GT>
deba@2462
   527
  int prBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   528
  {
deba@2462
   529
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   530
    bpm.run();
deba@2463
   531
    bpm.aMatching(matching);
deba@2462
   532
    bpm.bBarrier(barrier);
deba@2462
   533
    return bpm.matchingSize();
alpar@2353
   534
  }  
alpar@2353
   535
alpar@2353
   536
  ///Perfect matching in a bipartite graph
alpar@2353
   537
alpar@2353
   538
  ///\ingroup matching
alpar@2353
   539
  ///This function checks whether the bipartite graph \c g
alpar@2353
   540
  ///has a perfect matching.
alpar@2353
   541
  ///\param g An undirected bipartite graph.
alpar@2353
   542
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   543
  ///
alpar@2353
   544
  ///\note The the implementation is based
alpar@2353
   545
  ///on the push-relabel principle.
alpar@2353
   546
  template<class Graph>
deba@2462
   547
  bool prPerfectBipartiteMatching(const Graph &g)
alpar@2353
   548
  {
deba@2462
   549
    PrBipartiteMatching<Graph> bpm(g);
deba@2462
   550
    return bpm.runPerfect();
alpar@2353
   551
  }
alpar@2353
   552
alpar@2353
   553
  ///Perfect matching in a bipartite graph
alpar@2353
   554
alpar@2353
   555
  ///\ingroup matching
alpar@2353
   556
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   557
  ///\param g An undirected bipartite graph.
deba@2463
   558
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   559
  /// The found edges will be returned in this map,
deba@2463
   560
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   561
  /// that covers the node \c n.
deba@2462
   562
  /// The values are unchanged if the graph
alpar@2353
   563
  /// has no perfect matching.
alpar@2353
   564
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   565
  ///
alpar@2353
   566
  ///\note The the implementation is based
alpar@2353
   567
  ///on the push-relabel principle.
alpar@2353
   568
  template<class Graph,class MT>
deba@2462
   569
  bool prPerfectBipartiteMatching(const Graph &g,MT &matching) 
alpar@2353
   570
  {
deba@2462
   571
    PrBipartiteMatching<Graph> bpm(g);
deba@2463
   572
    bool ret = bpm.checkedRunPerfect();
deba@2463
   573
    if (ret) bpm.aMatching(matching);
deba@2462
   574
    return ret;
alpar@2353
   575
  }
alpar@2353
   576
alpar@2353
   577
  ///Perfect matching in a bipartite graph
alpar@2353
   578
alpar@2353
   579
  ///\ingroup matching
alpar@2353
   580
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   581
  ///\param g An undirected bipartite graph.
deba@2463
   582
  ///\retval matching A write ANodeMap of value type \c UEdge.
deba@2463
   583
  /// The found edges will be returned in this map,
deba@2463
   584
  /// i.e. for an \c ANode \c n the edge <tt>matching[n]</tt> is the one
deba@2463
   585
  /// that covers the node \c n.
deba@2462
   586
  /// The values are unchanged if the graph
alpar@2353
   587
  /// has no perfect matching.
alpar@2353
   588
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will only
alpar@2353
   589
  /// be set if \c g has no perfect matching. In this case it is set 
alpar@2353
   590
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   591
  /// a barrier, i.e. a subset \e B a of BNodes with the property that
deba@2462
   592
  /// the cardinality of \e B is greater than the number of its neighbors.
alpar@2353
   593
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   594
  ///
alpar@2353
   595
  ///\note The the implementation is based
alpar@2353
   596
  ///on the push-relabel principle.
alpar@2353
   597
  template<class Graph,class MT, class GT>
deba@2463
   598
  bool prPerfectBipartiteMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   599
  {
deba@2462
   600
    PrBipartiteMatching<Graph> bpm(g);
deba@2463
   601
    bool ret=bpm.checkedRunPerfect();
deba@2462
   602
    if(ret)
deba@2463
   603
      bpm.aMatching(matching);
deba@2462
   604
    else
deba@2462
   605
      bpm.bBarrier(barrier);
deba@2462
   606
    return ret;
alpar@2353
   607
  }  
alpar@2353
   608
}
alpar@2353
   609
alpar@2353
   610
#endif