1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/max_matching.h Mon May 23 04:48:14 2005 +0000
1.3 @@ -0,0 +1,583 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/max_matching.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_MAX_MATCHING_H
1.21 +#define LEMON_MAX_MATCHING_H
1.22 +
1.23 +#include <queue>
1.24 +#include <lemon/invalid.h>
1.25 +#include <lemon/unionfind.h>
1.26 +#include <lemon/graph_utils.h>
1.27 +
1.28 +///\ingroup galgs
1.29 +///\file
1.30 +///\brief Maximum matching algorithm.
1.31 +
1.32 +namespace lemon {
1.33 +
1.34 + /// \addtogroup galgs
1.35 + /// @{
1.36 +
1.37 + ///Edmonds' alternating forest maximum matching algorithm.
1.38 +
1.39 + ///This class provides Edmonds' alternating forest matching
1.40 + ///algorithm. The starting matching (if any) can be passed to the
1.41 + ///algorithm using read-in functions \ref readNMapNode, \ref
1.42 + ///readNMapEdge or \ref readEMapBool depending on the container. The
1.43 + ///resulting maximum matching can be attained by write-out functions
1.44 + ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
1.45 + ///depending on the preferred container.
1.46 + ///
1.47 + ///The dual side of a matching is a map of the nodes to
1.48 + ///MaxMatching::pos_enum, having values D, A and C showing the
1.49 + ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
1.50 + ///a graph with factor-critical components, the nodes in A form the
1.51 + ///barrier, and the nodes in C induce a graph having a perfect
1.52 + ///matching. This decomposition can be attained by calling \ref
1.53 + ///writePos after running the algorithm.
1.54 + ///
1.55 + ///\param Graph The undirected graph type the algorithm runs on.
1.56 + ///
1.57 + ///\author Jacint Szabo
1.58 + template <typename Graph>
1.59 + class MaxMatching {
1.60 +
1.61 + protected:
1.62 +
1.63 + typedef typename Graph::Node Node;
1.64 + typedef typename Graph::Edge Edge;
1.65 + typedef typename Graph::UndirEdge UndirEdge;
1.66 + typedef typename Graph::UndirEdgeIt UndirEdgeIt;
1.67 + typedef typename Graph::NodeIt NodeIt;
1.68 + typedef typename Graph::IncEdgeIt IncEdgeIt;
1.69 +
1.70 + typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
1.71 +
1.72 + public:
1.73 +
1.74 + ///Indicates the Gallai-Edmonds decomposition of the graph.
1.75 +
1.76 + ///Indicates the Gallai-Edmonds decomposition of the graph, which
1.77 + ///shows an upper bound on the size of a maximum matching. The
1.78 + ///nodes with pos_enum \c D induce a graph with factor-critical
1.79 + ///components, the nodes in \c A form the canonical barrier, and the
1.80 + ///nodes in \c C induce a graph having a perfect matching.
1.81 + enum pos_enum {
1.82 + D=0,
1.83 + A=1,
1.84 + C=2
1.85 + };
1.86 +
1.87 + protected:
1.88 +
1.89 + static const int HEUR_density=2;
1.90 + const Graph& g;
1.91 + typename Graph::template NodeMap<Node> _mate;
1.92 + typename Graph::template NodeMap<pos_enum> position;
1.93 +
1.94 + public:
1.95 +
1.96 + MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
1.97 +
1.98 + ///Runs Edmonds' algorithm.
1.99 +
1.100 + ///Runs Edmonds' algorithm for sparse graphs (number of edges <
1.101 + ///2*number of nodes), and a heuristical Edmonds' algorithm with a
1.102 + ///heuristic of postponing shrinks for dense graphs.
1.103 + inline void run();
1.104 +
1.105 + ///Runs Edmonds' algorithm.
1.106 +
1.107 + ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
1.108 + ///Edmonds' algorithm with a heuristic of postponing shrinks,
1.109 + ///giving a faster algorithm for dense graphs.
1.110 + void runEdmonds( int heur );
1.111 +
1.112 + ///Finds a greedy matching starting from the actual matching.
1.113 +
1.114 + ///Starting form the actual matching stored, it finds a maximal
1.115 + ///greedy matching.
1.116 + void greedyMatching();
1.117 +
1.118 + ///Returns the size of the actual matching stored.
1.119 +
1.120 + ///Returns the size of the actual matching stored. After \ref
1.121 + ///run() it returns the size of a maximum matching in the graph.
1.122 + int size() const;
1.123 +
1.124 + ///Resets the actual matching to the empty matching.
1.125 +
1.126 + ///Resets the actual matching to the empty matching.
1.127 + ///
1.128 + void resetMatching();
1.129 +
1.130 + ///Returns the mate of a node in the actual matching.
1.131 +
1.132 + ///Returns the mate of a \c node in the actual matching.
1.133 + ///Returns INVALID if the \c node is not covered by the actual matching.
1.134 + Node mate(Node& node) const {
1.135 + return _mate[node];
1.136 + }
1.137 +
1.138 + ///Reads a matching from a \c Node valued \c Node map.
1.139 +
1.140 + ///Reads a matching from a \c Node valued \c Node map. This map
1.141 + ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
1.142 + ///must hold, and \c uv will be an edge of the matching.
1.143 + template<typename NMapN>
1.144 + void readNMapNode(NMapN& map) {
1.145 + for(NodeIt v(g); v!=INVALID; ++v) {
1.146 + _mate.set(v,map[v]);
1.147 + }
1.148 + }
1.149 +
1.150 + ///Writes the stored matching to a \c Node valued \c Node map.
1.151 +
1.152 + ///Writes the stored matching to a \c Node valued \c Node map. The
1.153 + ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
1.154 + ///map[v]==u will hold, and now \c uv is an edge of the matching.
1.155 + template<typename NMapN>
1.156 + void writeNMapNode (NMapN& map) const {
1.157 + for(NodeIt v(g); v!=INVALID; ++v) {
1.158 + map.set(v,_mate[v]);
1.159 + }
1.160 + }
1.161 +
1.162 + ///Reads a matching from an \c UndirEdge valued \c Node map.
1.163 +
1.164 + ///Reads a matching from an \c UndirEdge valued \c Node map. \c
1.165 + ///map[v] must be an \c UndirEdge incident to \c v. This map must
1.166 + ///have the property that if \c g.oppositeNode(u,map[u])==v then
1.167 + ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
1.168 + ///joining \c u to \c v will be an edge of the matching.
1.169 + template<typename NMapE>
1.170 + void readNMapEdge(NMapE& map) {
1.171 + for(NodeIt v(g); v!=INVALID; ++v) {
1.172 + UndirEdge e=map[v];
1.173 + if ( e!=INVALID )
1.174 + _mate.set(v,g.oppositeNode(v,e));
1.175 + }
1.176 + }
1.177 +
1.178 + ///Writes the matching stored to an \c UndirEdge valued \c Node map.
1.179 +
1.180 + ///Writes the stored matching to an \c UndirEdge valued \c Node
1.181 + ///map. \c map[v] will be an \c UndirEdge incident to \c v. This
1.182 + ///map will have the property that if \c g.oppositeNode(u,map[u])
1.183 + ///== v then \c map[u]==map[v] holds, and now this edge is an edge
1.184 + ///of the matching.
1.185 + template<typename NMapE>
1.186 + void writeNMapEdge (NMapE& map) const {
1.187 + typename Graph::template NodeMap<bool> todo(g,true);
1.188 + for(NodeIt v(g); v!=INVALID; ++v) {
1.189 + if ( todo[v] && _mate[v]!=INVALID ) {
1.190 + Node u=_mate[v];
1.191 + for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
1.192 + if ( g.runningNode(e) == u ) {
1.193 + map.set(u,e);
1.194 + map.set(v,e);
1.195 + todo.set(u,false);
1.196 + todo.set(v,false);
1.197 + break;
1.198 + }
1.199 + }
1.200 + }
1.201 + }
1.202 + }
1.203 +
1.204 +
1.205 + ///Reads a matching from a \c bool valued \c Edge map.
1.206 +
1.207 + ///Reads a matching from a \c bool valued \c Edge map. This map
1.208 + ///must have the property that there are no two incident edges \c
1.209 + ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
1.210 + ///map[e]==true form the matching.
1.211 + template<typename EMapB>
1.212 + void readEMapBool(EMapB& map) {
1.213 + for(UndirEdgeIt e(g); e!=INVALID; ++e) {
1.214 + if ( map[e] ) {
1.215 + Node u=g.source(e);
1.216 + Node v=g.target(e);
1.217 + _mate.set(u,v);
1.218 + _mate.set(v,u);
1.219 + }
1.220 + }
1.221 + }
1.222 +
1.223 +
1.224 + ///Writes the matching stored to a \c bool valued \c Edge map.
1.225 +
1.226 + ///Writes the matching stored to a \c bool valued \c Edge
1.227 + ///map. This map will have the property that there are no two
1.228 + ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
1.229 + ///edges \c e with \c map[e]==true form the matching.
1.230 + template<typename EMapB>
1.231 + void writeEMapBool (EMapB& map) const {
1.232 + for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
1.233 +
1.234 + typename Graph::template NodeMap<bool> todo(g,true);
1.235 + for(NodeIt v(g); v!=INVALID; ++v) {
1.236 + if ( todo[v] && _mate[v]!=INVALID ) {
1.237 + Node u=_mate[v];
1.238 + for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
1.239 + if ( g.runningNode(e) == u ) {
1.240 + map.set(e,true);
1.241 + todo.set(u,false);
1.242 + todo.set(v,false);
1.243 + break;
1.244 + }
1.245 + }
1.246 + }
1.247 + }
1.248 + }
1.249 +
1.250 +
1.251 + ///Writes the canonical decomposition of the graph after running
1.252 + ///the algorithm.
1.253 +
1.254 + ///After calling any run methods of the class, it writes the
1.255 + ///Gallai-Edmonds canonical decomposition of the graph. \c map
1.256 + ///must be a node map of \ref pos_enum 's.
1.257 + template<typename NMapEnum>
1.258 + void writePos (NMapEnum& map) const {
1.259 + for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
1.260 + }
1.261 +
1.262 + private:
1.263 +
1.264 +
1.265 + void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
1.266 + UFE& blossom, UFE& tree);
1.267 +
1.268 + void normShrink(Node v, typename Graph::template NodeMap<Node>& ear,
1.269 + UFE& blossom, UFE& tree);
1.270 +
1.271 + bool noShrinkStep(Node x, typename Graph::template NodeMap<Node>& ear,
1.272 + UFE& blossom, UFE& tree, std::queue<Node>& Q);
1.273 +
1.274 + void shrinkStep(Node& top, Node& middle, Node& bottom,
1.275 + typename Graph::template NodeMap<Node>& ear,
1.276 + UFE& blossom, UFE& tree, std::queue<Node>& Q);
1.277 +
1.278 + void augment(Node x, typename Graph::template NodeMap<Node>& ear,
1.279 + UFE& blossom, UFE& tree);
1.280 +
1.281 + };
1.282 +
1.283 +
1.284 + // **********************************************************************
1.285 + // IMPLEMENTATIONS
1.286 + // **********************************************************************
1.287 +
1.288 +
1.289 + template <typename Graph>
1.290 + void MaxMatching<Graph>::run() {
1.291 + if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
1.292 + greedyMatching();
1.293 + runEdmonds(0);
1.294 + } else runEdmonds(1);
1.295 + }
1.296 +
1.297 +
1.298 + template <typename Graph>
1.299 + void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
1.300 +
1.301 + for(NodeIt v(g); v!=INVALID; ++v)
1.302 + position.set(v,C);
1.303 +
1.304 + typename Graph::template NodeMap<Node> ear(g,INVALID);
1.305 + //undefined for the base nodes of the blossoms (i.e. for the
1.306 + //representative elements of UFE blossom) and for the nodes in C
1.307 +
1.308 + typename UFE::MapType blossom_base(g);
1.309 + UFE blossom(blossom_base);
1.310 + typename UFE::MapType tree_base(g);
1.311 + UFE tree(tree_base);
1.312 + //If these UFE's would be members of the class then also
1.313 + //blossom_base and tree_base should be a member.
1.314 +
1.315 + for(NodeIt v(g); v!=INVALID; ++v) {
1.316 + if ( position[v]==C && _mate[v]==INVALID ) {
1.317 + blossom.insert(v);
1.318 + tree.insert(v);
1.319 + position.set(v,D);
1.320 + if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
1.321 + else normShrink( v, ear, blossom, tree );
1.322 + }
1.323 + }
1.324 + }
1.325 +
1.326 +
1.327 + template <typename Graph>
1.328 + void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
1.329 + UFE& blossom, UFE& tree) {
1.330 +
1.331 + std::queue<Node> Q; //queue of the totally unscanned nodes
1.332 + Q.push(v);
1.333 + std::queue<Node> R;
1.334 + //queue of the nodes which must be scanned for a possible shrink
1.335 +
1.336 + while ( !Q.empty() ) {
1.337 + Node x=Q.front();
1.338 + Q.pop();
1.339 + if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
1.340 + else R.push(x);
1.341 + }
1.342 +
1.343 + while ( !R.empty() ) {
1.344 + Node x=R.front();
1.345 + R.pop();
1.346 +
1.347 + for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
1.348 + Node y=g.runningNode(e);
1.349 +
1.350 + if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
1.351 + //x and y must be in the same tree
1.352 +
1.353 + typename Graph::template NodeMap<bool> path(g,false);
1.354 +
1.355 + Node b=blossom.find(x);
1.356 + path.set(b,true);
1.357 + b=_mate[b];
1.358 + while ( b!=INVALID ) {
1.359 + b=blossom.find(ear[b]);
1.360 + path.set(b,true);
1.361 + b=_mate[b];
1.362 + } //going till the root
1.363 +
1.364 + Node top=y;
1.365 + Node middle=blossom.find(top);
1.366 + Node bottom=x;
1.367 + while ( !path[middle] )
1.368 + shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
1.369 +
1.370 + Node base=middle;
1.371 + top=x;
1.372 + middle=blossom.find(top);
1.373 + bottom=y;
1.374 + Node blossom_base=blossom.find(base);
1.375 + while ( middle!=blossom_base )
1.376 + shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
1.377 +
1.378 + blossom.makeRep(base);
1.379 + } // if shrink is needed
1.380 +
1.381 + while ( !Q.empty() ) {
1.382 + Node x=Q.front();
1.383 + Q.pop();
1.384 + if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
1.385 + else R.push(x);
1.386 + }
1.387 + } //for e
1.388 + } // while ( !R.empty() )
1.389 + }
1.390 +
1.391 +
1.392 + template <typename Graph>
1.393 + void MaxMatching<Graph>::normShrink(Node v,
1.394 + typename Graph::template
1.395 + NodeMap<Node>& ear,
1.396 + UFE& blossom, UFE& tree) {
1.397 + std::queue<Node> Q; //queue of the unscanned nodes
1.398 + Q.push(v);
1.399 + while ( !Q.empty() ) {
1.400 +
1.401 + Node x=Q.front();
1.402 + Q.pop();
1.403 +
1.404 + for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
1.405 + Node y=g.runningNode(e);
1.406 +
1.407 + switch ( position[y] ) {
1.408 + case D: //x and y must be in the same tree
1.409 +
1.410 + if ( blossom.find(x) != blossom.find(y) ) { //shrink
1.411 + typename Graph::template NodeMap<bool> path(g,false);
1.412 +
1.413 + Node b=blossom.find(x);
1.414 + path.set(b,true);
1.415 + b=_mate[b];
1.416 + while ( b!=INVALID ) {
1.417 + b=blossom.find(ear[b]);
1.418 + path.set(b,true);
1.419 + b=_mate[b];
1.420 + } //going till the root
1.421 +
1.422 + Node top=y;
1.423 + Node middle=blossom.find(top);
1.424 + Node bottom=x;
1.425 + while ( !path[middle] )
1.426 + shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
1.427 +
1.428 + Node base=middle;
1.429 + top=x;
1.430 + middle=blossom.find(top);
1.431 + bottom=y;
1.432 + Node blossom_base=blossom.find(base);
1.433 + while ( middle!=blossom_base )
1.434 + shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
1.435 +
1.436 + blossom.makeRep(base);
1.437 + }
1.438 + break;
1.439 + case C:
1.440 + if ( _mate[y]!=INVALID ) { //grow
1.441 +
1.442 + ear.set(y,x);
1.443 + Node w=_mate[y];
1.444 + blossom.insert(w);
1.445 + position.set(y,A);
1.446 + position.set(w,D);
1.447 + tree.insert(y);
1.448 + tree.insert(w);
1.449 + tree.join(y,blossom.find(x));
1.450 + tree.join(w,y);
1.451 + Q.push(w);
1.452 + } else { //augment
1.453 + augment(x, ear, blossom, tree);
1.454 + _mate.set(x,y);
1.455 + _mate.set(y,x);
1.456 + return;
1.457 + } //if
1.458 + break;
1.459 + default: break;
1.460 + }
1.461 + }
1.462 + }
1.463 + }
1.464 +
1.465 + template <typename Graph>
1.466 + void MaxMatching<Graph>::greedyMatching() {
1.467 + for(NodeIt v(g); v!=INVALID; ++v)
1.468 + if ( _mate[v]==INVALID ) {
1.469 + for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
1.470 + Node y=g.runningNode(e);
1.471 + if ( _mate[y]==INVALID && y!=v ) {
1.472 + _mate.set(v,y);
1.473 + _mate.set(y,v);
1.474 + break;
1.475 + }
1.476 + }
1.477 + }
1.478 + }
1.479 +
1.480 + template <typename Graph>
1.481 + int MaxMatching<Graph>::size() const {
1.482 + int s=0;
1.483 + for(NodeIt v(g); v!=INVALID; ++v) {
1.484 + if ( _mate[v]!=INVALID ) {
1.485 + ++s;
1.486 + }
1.487 + }
1.488 + return s/2;
1.489 + }
1.490 +
1.491 + template <typename Graph>
1.492 + void MaxMatching<Graph>::resetMatching() {
1.493 + for(NodeIt v(g); v!=INVALID; ++v)
1.494 + _mate.set(v,INVALID);
1.495 + }
1.496 +
1.497 + template <typename Graph>
1.498 + bool MaxMatching<Graph>::noShrinkStep(Node x,
1.499 + typename Graph::template
1.500 + NodeMap<Node>& ear,
1.501 + UFE& blossom, UFE& tree,
1.502 + std::queue<Node>& Q) {
1.503 + for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
1.504 + Node y=g.runningNode(e);
1.505 +
1.506 + if ( position[y]==C ) {
1.507 + if ( _mate[y]!=INVALID ) { //grow
1.508 + ear.set(y,x);
1.509 + Node w=_mate[y];
1.510 + blossom.insert(w);
1.511 + position.set(y,A);
1.512 + position.set(w,D);
1.513 + tree.insert(y);
1.514 + tree.insert(w);
1.515 + tree.join(y,blossom.find(x));
1.516 + tree.join(w,y);
1.517 + Q.push(w);
1.518 + } else { //augment
1.519 + augment(x, ear, blossom, tree);
1.520 + _mate.set(x,y);
1.521 + _mate.set(y,x);
1.522 + return true;
1.523 + }
1.524 + }
1.525 + }
1.526 + return false;
1.527 + }
1.528 +
1.529 + template <typename Graph>
1.530 + void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom,
1.531 + typename Graph::template
1.532 + NodeMap<Node>& ear,
1.533 + UFE& blossom, UFE& tree,
1.534 + std::queue<Node>& Q) {
1.535 + ear.set(top,bottom);
1.536 + Node t=top;
1.537 + while ( t!=middle ) {
1.538 + Node u=_mate[t];
1.539 + t=ear[u];
1.540 + ear.set(t,u);
1.541 + }
1.542 + bottom=_mate[middle];
1.543 + position.set(bottom,D);
1.544 + Q.push(bottom);
1.545 + top=ear[bottom];
1.546 + Node oldmiddle=middle;
1.547 + middle=blossom.find(top);
1.548 + tree.erase(bottom);
1.549 + tree.erase(oldmiddle);
1.550 + blossom.insert(bottom);
1.551 + blossom.join(bottom, oldmiddle);
1.552 + blossom.join(top, oldmiddle);
1.553 + }
1.554 +
1.555 + template <typename Graph>
1.556 + void MaxMatching<Graph>::augment(Node x,
1.557 + typename Graph::template NodeMap<Node>& ear,
1.558 + UFE& blossom, UFE& tree) {
1.559 + Node v=_mate[x];
1.560 + while ( v!=INVALID ) {
1.561 +
1.562 + Node u=ear[v];
1.563 + _mate.set(v,u);
1.564 + Node tmp=v;
1.565 + v=_mate[u];
1.566 + _mate.set(u,tmp);
1.567 + }
1.568 + typename UFE::ItemIt it;
1.569 + for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
1.570 + if ( position[it] == D ) {
1.571 + typename UFE::ItemIt b_it;
1.572 + for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
1.573 + position.set( b_it ,C);
1.574 + }
1.575 + blossom.eraseClass(it);
1.576 + } else position.set( it ,C);
1.577 + }
1.578 + tree.eraseClass(x);
1.579 +
1.580 + }
1.581 +
1.582 + /// @}
1.583 +
1.584 +} //END OF NAMESPACE LEMON
1.585 +
1.586 +#endif //LEMON_MAX_MATCHING_H