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