lemon/bp_matching.h
author deba
Wed, 07 Mar 2007 12:00:59 +0000
changeset 2400 b199ded24c19
parent 2353 c43f8802c90a
permissions -rw-r--r--
Steiner 2-approximation demo
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
alpar@2353
    19
#ifndef LEMON_BP_MATCHING
alpar@2353
    20
#define LEMON_BP_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/counter.h>
alpar@2353
    27
#include <lemon/elevator.h>
alpar@2353
    28
alpar@2353
    29
///\ingroup matching
alpar@2353
    30
///\file
alpar@2353
    31
///\brief Push-prelabel maximum matching algorithms in bipartite graphs.
alpar@2353
    32
///
alpar@2353
    33
///\todo This file slightly conflicts with \ref lemon/bipartite_matching.h
alpar@2353
    34
///\todo (Re)move the XYZ_TYPEDEFS macros
alpar@2353
    35
namespace lemon {
alpar@2353
    36
alpar@2353
    37
#define BIPARTITE_TYPEDEFS(Graph)		\
alpar@2353
    38
  GRAPH_TYPEDEFS(Graph)				\
alpar@2353
    39
    typedef Graph::ANodeIt ANodeIt;	\
alpar@2353
    40
    typedef Graph::BNodeIt BNodeIt;
alpar@2353
    41
alpar@2353
    42
#define UNDIRBIPARTITE_TYPEDEFS(Graph)		\
alpar@2353
    43
  UNDIRGRAPH_TYPEDEFS(Graph)			\
alpar@2353
    44
    typedef Graph::ANodeIt ANodeIt;	\
alpar@2353
    45
    typedef Graph::BNodeIt BNodeIt;
alpar@2353
    46
alpar@2353
    47
  template<class Graph,
alpar@2353
    48
	   class MT=typename Graph::template ANodeMap<typename Graph::UEdge> >
alpar@2353
    49
  class BpMatching {
alpar@2353
    50
    typedef typename Graph::Node Node;
alpar@2353
    51
    typedef typename Graph::ANodeIt ANodeIt;
alpar@2353
    52
    typedef typename Graph::BNodeIt BNodeIt;
alpar@2353
    53
    typedef typename Graph::UEdge UEdge;
alpar@2353
    54
    typedef typename Graph::IncEdgeIt IncEdgeIt;
alpar@2353
    55
    
alpar@2353
    56
    const Graph &_g;
alpar@2353
    57
    int _node_num;
alpar@2353
    58
    MT &_matching;
alpar@2353
    59
    Elevator<Graph,typename Graph::BNode> _levels;
alpar@2353
    60
    typename Graph::template BNodeMap<int> _cov;
alpar@2353
    61
alpar@2353
    62
  public:
alpar@2353
    63
    BpMatching(const Graph &g, MT &matching) :
alpar@2353
    64
      _g(g),
alpar@2353
    65
      _node_num(countBNodes(g)),
alpar@2353
    66
      _matching(matching),
alpar@2353
    67
      _levels(g,_node_num),
alpar@2353
    68
      _cov(g,0)
alpar@2353
    69
    {
alpar@2353
    70
    }
alpar@2353
    71
    
alpar@2353
    72
  private:
alpar@2353
    73
    void init() 
alpar@2353
    74
    {
alpar@2353
    75
//     for(BNodeIt n(g);n!=INVALID;++n) cov[n]=0;
alpar@2353
    76
      for(ANodeIt n(_g);n!=INVALID;++n)
alpar@2353
    77
	if((_matching[n]=IncEdgeIt(_g,n))!=INVALID)
alpar@2353
    78
	  ++_cov[_g.oppositeNode(n,_matching[n])];
alpar@2353
    79
alpar@2353
    80
      std::queue<Node> q;
alpar@2353
    81
      _levels.initStart();
alpar@2353
    82
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
    83
	if(_cov[n]>1) {
alpar@2353
    84
	  _levels.initAddItem(n);
alpar@2353
    85
	  q.push(n);
alpar@2353
    86
	}
alpar@2353
    87
      int hlev=0;
alpar@2353
    88
      while(!q.empty()) {
alpar@2353
    89
	Node n=q.front();
alpar@2353
    90
	q.pop();
alpar@2353
    91
	int nlev=_levels[n]+1;
alpar@2353
    92
	for(IncEdgeIt e(_g,n);e!=INVALID;++e) {
alpar@2353
    93
	  Node m=_g.runningNode(e);
alpar@2353
    94
	  if(e==_matching[m]) {
alpar@2353
    95
	    for(IncEdgeIt f(_g,m);f!=INVALID;++f) {
alpar@2353
    96
	      Node r=_g.runningNode(f);
alpar@2353
    97
	      if(_levels[r]>nlev) {
alpar@2353
    98
		for(;nlev>hlev;hlev++)
alpar@2353
    99
		  _levels.initNewLevel();
alpar@2353
   100
		_levels.initAddItem(r);
alpar@2353
   101
		q.push(r);
alpar@2353
   102
	      }
alpar@2353
   103
	    }
alpar@2353
   104
	  }
alpar@2353
   105
	}
alpar@2353
   106
      }
alpar@2353
   107
      _levels.initFinish();
alpar@2353
   108
      for(BNodeIt n(_g);n!=INVALID;++n)
alpar@2353
   109
	if(_cov[n]<1&&_levels[n]<_node_num)
alpar@2353
   110
	  _levels.activate(n);
alpar@2353
   111
    }
alpar@2353
   112
  public:
alpar@2353
   113
    int run() 
alpar@2353
   114
    {
alpar@2353
   115
      init();
alpar@2353
   116
alpar@2353
   117
      Node act;
alpar@2353
   118
      Node bact=INVALID;
alpar@2353
   119
      Node last_activated=INVALID;
alpar@2353
   120
//       while((act=last_activated!=INVALID?
alpar@2353
   121
// 	     last_activated:_levels.highestActive())
alpar@2353
   122
// 	    !=INVALID)
alpar@2353
   123
      while((act=_levels.highestActive())!=INVALID) {
alpar@2353
   124
	last_activated=INVALID;
alpar@2353
   125
	int actlevel=_levels[act];
alpar@2353
   126
	
alpar@2353
   127
	UEdge bedge=INVALID;
alpar@2353
   128
	int nlevel=_node_num;
alpar@2353
   129
	{
alpar@2353
   130
	  int nnlevel;
alpar@2353
   131
	  for(IncEdgeIt tbedge(_g,act);
alpar@2353
   132
	      tbedge!=INVALID && nlevel>=actlevel;
alpar@2353
   133
	      ++tbedge)
alpar@2353
   134
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
alpar@2353
   135
	       nlevel)
alpar@2353
   136
	      {
alpar@2353
   137
		nlevel=nnlevel;
alpar@2353
   138
		bedge=tbedge;
alpar@2353
   139
	      }
alpar@2353
   140
	}
alpar@2353
   141
	if(nlevel<_node_num) {
alpar@2353
   142
	  if(nlevel>=actlevel)
alpar@2353
   143
	    _levels.liftHighestActiveTo(nlevel+1);
alpar@2353
   144
// 	    _levels.liftTo(act,nlevel+1);
alpar@2353
   145
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
alpar@2353
   146
	  if(--_cov[bact]<1) {
alpar@2353
   147
	    _levels.activate(bact);
alpar@2353
   148
	    last_activated=bact;
alpar@2353
   149
	  }
alpar@2353
   150
	  _matching[_g.aNode(bedge)]=bedge;
alpar@2353
   151
	  _cov[act]=1;
alpar@2353
   152
	  _levels.deactivate(act);
alpar@2353
   153
	}
alpar@2353
   154
	else {
alpar@2353
   155
	  if(_node_num>actlevel) 
alpar@2353
   156
	    _levels.liftHighestActiveTo(_node_num);
alpar@2353
   157
//  	    _levels.liftTo(act,_node_num);
alpar@2353
   158
	  _levels.deactivate(act); 
alpar@2353
   159
	}
alpar@2353
   160
alpar@2353
   161
	if(_levels.onLevel(actlevel)==0)
alpar@2353
   162
	  _levels.liftToTop(actlevel);
alpar@2353
   163
      }
alpar@2353
   164
      
alpar@2353
   165
      int ret=_node_num;
alpar@2353
   166
      for(ANodeIt n(_g);n!=INVALID;++n)
alpar@2353
   167
	if(_matching[n]==INVALID) ret--;
alpar@2353
   168
	else if (_cov[_g.bNode(_matching[n])]>1) {
alpar@2353
   169
	  _cov[_g.bNode(_matching[n])]--;
alpar@2353
   170
	  ret--;
alpar@2353
   171
	  _matching[n]=INVALID;
alpar@2353
   172
	}
alpar@2353
   173
      return ret;
alpar@2353
   174
    }
alpar@2353
   175
    
alpar@2353
   176
    ///\returns -1 if there is a perfect matching, or an empty level
alpar@2353
   177
    ///if it doesn't exists
alpar@2353
   178
    int runPerfect() 
alpar@2353
   179
    {
alpar@2353
   180
      init();
alpar@2353
   181
alpar@2353
   182
      Node act;
alpar@2353
   183
      Node bact=INVALID;
alpar@2353
   184
      Node last_activated=INVALID;
alpar@2353
   185
      while((act=_levels.highestActive())!=INVALID) {
alpar@2353
   186
	last_activated=INVALID;
alpar@2353
   187
	int actlevel=_levels[act];
alpar@2353
   188
	
alpar@2353
   189
	UEdge bedge=INVALID;
alpar@2353
   190
	int nlevel=_node_num;
alpar@2353
   191
	{
alpar@2353
   192
	  int nnlevel;
alpar@2353
   193
	  for(IncEdgeIt tbedge(_g,act);
alpar@2353
   194
	      tbedge!=INVALID && nlevel>=actlevel;
alpar@2353
   195
	      ++tbedge)
alpar@2353
   196
	    if((nnlevel=_levels[_g.bNode(_matching[_g.runningNode(tbedge)])])<
alpar@2353
   197
	       nlevel)
alpar@2353
   198
	      {
alpar@2353
   199
		nlevel=nnlevel;
alpar@2353
   200
		bedge=tbedge;
alpar@2353
   201
	      }
alpar@2353
   202
	}
alpar@2353
   203
	if(nlevel<_node_num) {
alpar@2353
   204
	  if(nlevel>=actlevel)
alpar@2353
   205
	    _levels.liftHighestActiveTo(nlevel+1);
alpar@2353
   206
	  bact=_g.bNode(_matching[_g.aNode(bedge)]);
alpar@2353
   207
	  if(--_cov[bact]<1) {
alpar@2353
   208
	    _levels.activate(bact);
alpar@2353
   209
	    last_activated=bact;
alpar@2353
   210
	  }
alpar@2353
   211
	  _matching[_g.aNode(bedge)]=bedge;
alpar@2353
   212
	  _cov[act]=1;
alpar@2353
   213
	  _levels.deactivate(act);
alpar@2353
   214
	}
alpar@2353
   215
	else {
alpar@2353
   216
	  if(_node_num>actlevel) 
alpar@2353
   217
	    _levels.liftHighestActiveTo(_node_num);
alpar@2353
   218
	  _levels.deactivate(act); 
alpar@2353
   219
	}
alpar@2353
   220
alpar@2353
   221
	if(_levels.onLevel(actlevel)==0)
alpar@2353
   222
	  return actlevel;
alpar@2353
   223
      }
alpar@2353
   224
      return -1;
alpar@2353
   225
    }
alpar@2353
   226
 
alpar@2353
   227
    template<class GT>
alpar@2353
   228
    void aBarrier(GT &bar,int empty_level=-1) 
alpar@2353
   229
    {
alpar@2353
   230
      if(empty_level==-1)
alpar@2353
   231
	for(empty_level=0;_levels.onLevel(empty_level);empty_level++) ;
alpar@2353
   232
      for(ANodeIt n(_g);n!=INVALID;++n)
alpar@2353
   233
	bar[n] = _matching[n]==INVALID ||
alpar@2353
   234
	  _levels[_g.bNode(_matching[n])]<empty_level;  
alpar@2353
   235
    }  
alpar@2353
   236
    template<class GT>
alpar@2353
   237
    void bBarrier(GT &bar, int empty_level=-1) 
alpar@2353
   238
    {
alpar@2353
   239
      if(empty_level==-1)
alpar@2353
   240
	for(empty_level=0;_levels.onLevel(empty_level);empty_level++) ;
alpar@2353
   241
      for(BNodeIt n(_g);n!=INVALID;++n) bar[n]=(_levels[n]>empty_level);  
alpar@2353
   242
    }  
alpar@2353
   243
  
alpar@2353
   244
  };
alpar@2353
   245
  
alpar@2353
   246
  
alpar@2353
   247
  ///Maximum cardinality of the matchings in a bipartite graph
alpar@2353
   248
alpar@2353
   249
  ///\ingroup matching
alpar@2353
   250
  ///This function finds the maximum cardinality of the matchings
alpar@2353
   251
  ///in a bipartite graph \c g.
alpar@2353
   252
  ///\param g An undirected bipartite graph.
alpar@2353
   253
  ///\return The cardinality of the maximum matching.
alpar@2353
   254
  ///
alpar@2353
   255
  ///\note The the implementation is based
alpar@2353
   256
  ///on the push-relabel principle.
alpar@2353
   257
  template<class Graph>
alpar@2353
   258
  int maxBpMatching(const Graph &g)
alpar@2353
   259
  {
alpar@2353
   260
    typename Graph::template ANodeMap<typename Graph::UEdge> matching(g);
alpar@2353
   261
    return maxBpMatching(g,matching);
alpar@2353
   262
  }
alpar@2353
   263
alpar@2353
   264
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   265
alpar@2353
   266
  ///\ingroup matching
alpar@2353
   267
  ///This function finds a maximum cardinality matching
alpar@2353
   268
  ///in a bipartite graph \c g.
alpar@2353
   269
  ///\param g An undirected bipartite graph.
alpar@2353
   270
  ///\retval matching A readwrite ANodeMap of value type \c Edge.
alpar@2353
   271
  /// The found edges will be returned in this map,
alpar@2353
   272
  /// i.e. for an \c ANode \c n,
alpar@2353
   273
  /// the edge <tt>matching[n]</tt> is the one that covers the node \c n, or
alpar@2353
   274
  /// \ref INVALID if it is uncovered.
alpar@2353
   275
  ///\return The cardinality of the maximum matching.
alpar@2353
   276
  ///
alpar@2353
   277
  ///\note The the implementation is based
alpar@2353
   278
  ///on the push-relabel principle.
alpar@2353
   279
  template<class Graph,class MT>
alpar@2353
   280
  int maxBpMatching(const Graph &g,MT &matching) 
alpar@2353
   281
  {
alpar@2353
   282
    return BpMatching<Graph,MT>(g,matching).run();
alpar@2353
   283
  }
alpar@2353
   284
alpar@2353
   285
  ///Maximum cardinality matching in a bipartite graph
alpar@2353
   286
alpar@2353
   287
  ///\ingroup matching
alpar@2353
   288
  ///This function finds a maximum cardinality matching
alpar@2353
   289
  ///in a bipartite graph \c g.
alpar@2353
   290
  ///\param g An undirected bipartite graph.
alpar@2353
   291
  ///\retval matching A readwrite ANodeMap of value type \c Edge.
alpar@2353
   292
  /// The found edges will be returned in this map,
alpar@2353
   293
  /// i.e. for an \c ANode \c n,
alpar@2353
   294
  /// the edge <tt>matching[n]</tt> is the one that covers the node \c n, or
alpar@2353
   295
  /// \ref INVALID if it is uncovered.
alpar@2353
   296
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will be set
alpar@2353
   297
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   298
  /// a barrier \e B, i.e. the cardinality of \e B minus the number of its
alpar@2353
   299
  /// neighbor is equal to the number of the <tt>BNode</tt>s minus the
alpar@2353
   300
  /// cardinality of the maximum matching.
alpar@2353
   301
  ///\return The cardinality of the maximum matching.
alpar@2353
   302
  ///
alpar@2353
   303
  ///\note The the implementation is based
alpar@2353
   304
  ///on the push-relabel principle.
alpar@2353
   305
  template<class Graph,class MT, class GT>
alpar@2353
   306
  int maxBpMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   307
  {
alpar@2353
   308
    BpMatching<Graph,MT> bpm(g,matching);
alpar@2353
   309
    int ret=bpm.run();
alpar@2353
   310
    bpm.barrier(barrier);
alpar@2353
   311
    return ret;
alpar@2353
   312
  }  
alpar@2353
   313
alpar@2353
   314
  ///Perfect matching in a bipartite graph
alpar@2353
   315
alpar@2353
   316
  ///\ingroup matching
alpar@2353
   317
  ///This function checks whether the bipartite graph \c g
alpar@2353
   318
  ///has a perfect matching.
alpar@2353
   319
  ///\param g An undirected bipartite graph.
alpar@2353
   320
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   321
  ///
alpar@2353
   322
  ///\note The the implementation is based
alpar@2353
   323
  ///on the push-relabel principle.
alpar@2353
   324
  template<class Graph>
alpar@2353
   325
  bool perfectBpMatching(const Graph &g)
alpar@2353
   326
  {
alpar@2353
   327
    typename Graph::template ANodeMap<typename Graph::UEdge> matching(g);
alpar@2353
   328
    return perfectBpMatching(g,matching);
alpar@2353
   329
  }
alpar@2353
   330
alpar@2353
   331
  ///Perfect matching in a bipartite graph
alpar@2353
   332
alpar@2353
   333
  ///\ingroup matching
alpar@2353
   334
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   335
  ///\param g An undirected bipartite graph.
alpar@2353
   336
  ///\retval matching A readwrite ANodeMap of value type \c Edge.
alpar@2353
   337
  /// The found edges will be returned in this map,
alpar@2353
   338
  /// i.e. for an \c ANode \c n,
alpar@2353
   339
  /// the edge <tt>matching[n]</tt> is the one that covers the node \c n.
alpar@2353
   340
  /// The values are unspecified if the graph
alpar@2353
   341
  /// has no perfect matching.
alpar@2353
   342
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   343
  ///
alpar@2353
   344
  ///\note The the implementation is based
alpar@2353
   345
  ///on the push-relabel principle.
alpar@2353
   346
  template<class Graph,class MT>
alpar@2353
   347
  bool perfectBpMatching(const Graph &g,MT &matching) 
alpar@2353
   348
  {
alpar@2353
   349
    return BpMatching<Graph,MT>(g,matching).runPerfect()<0;
alpar@2353
   350
  }
alpar@2353
   351
alpar@2353
   352
  ///Perfect matching in a bipartite graph
alpar@2353
   353
alpar@2353
   354
  ///\ingroup matching
alpar@2353
   355
  ///This function finds a perfect matching in a bipartite graph \c g.
alpar@2353
   356
  ///\param g An undirected bipartite graph.
alpar@2353
   357
  ///\retval matching A readwrite ANodeMap of value type \c Edge.
alpar@2353
   358
  /// The found edges will be returned in this map,
alpar@2353
   359
  /// i.e. for an \c ANode \c n,
alpar@2353
   360
  /// the edge <tt>matching[n]</tt> is the one that covers the node \c n.
alpar@2353
   361
  /// The values are unspecified if the graph
alpar@2353
   362
  /// has no perfect matching.
alpar@2353
   363
  ///\retval barrier A \c bool WriteMap on the BNodes. The map will only
alpar@2353
   364
  /// be set if \c g has no perfect matching. In this case it is set 
alpar@2353
   365
  /// exactly once for each BNode. The nodes with \c true value represent
alpar@2353
   366
  /// a barrier, i.e. a subset \e B a of BNodes with the property that
alpar@2353
   367
  /// the cardinality of \e B is greater than the numner of its neighbors.
alpar@2353
   368
  ///\return \c true iff \c g has a perfect matching.
alpar@2353
   369
  ///
alpar@2353
   370
  ///\note The the implementation is based
alpar@2353
   371
  ///on the push-relabel principle.
alpar@2353
   372
  template<class Graph,class MT, class GT>
alpar@2353
   373
  int perfectBpMatching(const Graph &g,MT &matching,GT &barrier) 
alpar@2353
   374
  {
alpar@2353
   375
    BpMatching<Graph,MT> bpm(g,matching);
alpar@2353
   376
    int ret=bpm.run();
alpar@2353
   377
    if(ret>=0)
alpar@2353
   378
      bpm.barrier(barrier,ret);
alpar@2353
   379
    return ret<0;
alpar@2353
   380
  }  
alpar@2353
   381
}
alpar@2353
   382
alpar@2353
   383
#endif