... and fix a stupid bug.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_MAX_MATCHING_H
20 #define LEMON_MAX_MATCHING_H
23 #include <lemon/bits/invalid.h>
24 #include <lemon/unionfind.h>
25 #include <lemon/graph_utils.h>
29 ///\brief Maximum matching algorithm in undirected graph.
35 ///Edmonds' alternating forest maximum matching algorithm.
37 ///This class provides Edmonds' alternating forest matching
38 ///algorithm. The starting matching (if any) can be passed to the
39 ///algorithm using read-in functions \ref readNMapNode, \ref
40 ///readNMapEdge or \ref readEMapBool depending on the container. The
41 ///resulting maximum matching can be attained by write-out functions
42 ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
43 ///depending on the preferred container.
45 ///The dual side of a matching is a map of the nodes to
46 ///MaxMatching::pos_enum, having values D, A and C showing the
47 ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
48 ///a graph with factor-critical components, the nodes in A form the
49 ///barrier, and the nodes in C induce a graph having a perfect
50 ///matching. This decomposition can be attained by calling \ref
51 ///writePos after running the algorithm.
53 ///\param Graph The undirected graph type the algorithm runs on.
55 ///\author Jacint Szabo
56 template <typename Graph>
61 typedef typename Graph::Node Node;
62 typedef typename Graph::Edge Edge;
63 typedef typename Graph::UEdge UEdge;
64 typedef typename Graph::UEdgeIt UEdgeIt;
65 typedef typename Graph::NodeIt NodeIt;
66 typedef typename Graph::IncEdgeIt IncEdgeIt;
68 typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
72 ///Indicates the Gallai-Edmonds decomposition of the graph.
74 ///Indicates the Gallai-Edmonds decomposition of the graph, which
75 ///shows an upper bound on the size of a maximum matching. The
76 ///nodes with pos_enum \c D induce a graph with factor-critical
77 ///components, the nodes in \c A form the canonical barrier, and the
78 ///nodes in \c C induce a graph having a perfect matching.
87 static const int HEUR_density=2;
89 typename Graph::template NodeMap<Node> _mate;
90 typename Graph::template NodeMap<pos_enum> position;
94 MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
96 ///Runs Edmonds' algorithm.
98 ///Runs Edmonds' algorithm for sparse graphs (number of edges <
99 ///2*number of nodes), and a heuristical Edmonds' algorithm with a
100 ///heuristic of postponing shrinks for dense graphs.
102 if ( countUEdges(g) < HEUR_density*countNodes(g) ) {
105 } else runEdmonds(1);
109 ///Runs Edmonds' algorithm.
111 ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
112 ///Edmonds' algorithm with a heuristic of postponing shrinks,
113 ///giving a faster algorithm for dense graphs.
114 void runEdmonds( int heur = 1 ) {
116 //each vertex is put to C
117 for(NodeIt v(g); v!=INVALID; ++v)
120 typename Graph::template NodeMap<Node> ear(g,INVALID);
121 //undefined for the base nodes of the blossoms (i.e. for the
122 //representative elements of UFE blossom) and for the nodes in C
124 typename UFE::MapType blossom_base(g);
125 UFE blossom(blossom_base);
126 typename UFE::MapType tree_base(g);
128 //If these UFE's would be members of the class then also
129 //blossom_base and tree_base should be a member.
131 //We build only one tree and the other vertices uncovered by the
132 //matching belong to C. (They can be considered as singleton
133 //trees.) If this tree can be augmented or no more
134 //grow/augmentation/shrink is possible then we return to this
136 for(NodeIt v(g); v!=INVALID; ++v) {
137 if ( position[v]==C && _mate[v]==INVALID ) {
141 if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
142 else normShrink( v, ear, blossom, tree );
148 ///Finds a greedy matching starting from the actual matching.
150 ///Starting form the actual matching stored, it finds a maximal
152 void greedyMatching() {
153 for(NodeIt v(g); v!=INVALID; ++v)
154 if ( _mate[v]==INVALID ) {
155 for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
156 Node y=g.runningNode(e);
157 if ( _mate[y]==INVALID && y!=v ) {
166 ///Returns the size of the actual matching stored.
168 ///Returns the size of the actual matching stored. After \ref
169 ///run() it returns the size of a maximum matching in the graph.
172 for(NodeIt v(g); v!=INVALID; ++v) {
173 if ( _mate[v]!=INVALID ) {
181 ///Resets the actual matching to the empty matching.
183 ///Resets the actual matching to the empty matching.
185 void resetMatching() {
186 for(NodeIt v(g); v!=INVALID; ++v)
187 _mate.set(v,INVALID);
190 ///Returns the mate of a node in the actual matching.
192 ///Returns the mate of a \c node in the actual matching.
193 ///Returns INVALID if the \c node is not covered by the actual matching.
194 Node mate(Node& node) const {
198 ///Reads a matching from a \c Node valued \c Node map.
200 ///Reads a matching from a \c Node valued \c Node map. This map
201 ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
202 ///must hold, and \c uv will be an edge of the matching.
203 template<typename NMapN>
204 void readNMapNode(NMapN& map) {
205 for(NodeIt v(g); v!=INVALID; ++v) {
210 ///Writes the stored matching to a \c Node valued \c Node map.
212 ///Writes the stored matching to a \c Node valued \c Node map. The
213 ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
214 ///map[v]==u will hold, and now \c uv is an edge of the matching.
215 template<typename NMapN>
216 void writeNMapNode (NMapN& map) const {
217 for(NodeIt v(g); v!=INVALID; ++v) {
222 ///Reads a matching from an \c UEdge valued \c Node map.
224 ///Reads a matching from an \c UEdge valued \c Node map. \c
225 ///map[v] must be an \c UEdge incident to \c v. This map must
226 ///have the property that if \c g.oppositeNode(u,map[u])==v then
227 ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
228 ///joining \c u to \c v will be an edge of the matching.
229 template<typename NMapE>
230 void readNMapEdge(NMapE& map) {
231 for(NodeIt v(g); v!=INVALID; ++v) {
234 _mate.set(v,g.oppositeNode(v,e));
238 ///Writes the matching stored to an \c UEdge valued \c Node map.
240 ///Writes the stored matching to an \c UEdge valued \c Node
241 ///map. \c map[v] will be an \c UEdge incident to \c v. This
242 ///map will have the property that if \c g.oppositeNode(u,map[u])
243 ///== v then \c map[u]==map[v] holds, and now this edge is an edge
245 template<typename NMapE>
246 void writeNMapEdge (NMapE& map) const {
247 typename Graph::template NodeMap<bool> todo(g,true);
248 for(NodeIt v(g); v!=INVALID; ++v) {
249 if ( todo[v] && _mate[v]!=INVALID ) {
251 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
252 if ( g.runningNode(e) == u ) {
265 ///Reads a matching from a \c bool valued \c Edge map.
267 ///Reads a matching from a \c bool valued \c Edge map. This map
268 ///must have the property that there are no two incident edges \c
269 ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
270 ///map[e]==true form the matching.
271 template<typename EMapB>
272 void readEMapBool(EMapB& map) {
273 for(UEdgeIt e(g); e!=INVALID; ++e) {
284 ///Writes the matching stored to a \c bool valued \c Edge map.
286 ///Writes the matching stored to a \c bool valued \c Edge
287 ///map. This map will have the property that there are no two
288 ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
289 ///edges \c e with \c map[e]==true form the matching.
290 template<typename EMapB>
291 void writeEMapBool (EMapB& map) const {
292 for(UEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
294 typename Graph::template NodeMap<bool> todo(g,true);
295 for(NodeIt v(g); v!=INVALID; ++v) {
296 if ( todo[v] && _mate[v]!=INVALID ) {
298 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
299 if ( g.runningNode(e) == u ) {
311 ///Writes the canonical decomposition of the graph after running
314 ///After calling any run methods of the class, it writes the
315 ///Gallai-Edmonds canonical decomposition of the graph. \c map
316 ///must be a node map of \ref pos_enum 's.
317 template<typename NMapEnum>
318 void writePos (NMapEnum& map) const {
319 for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
325 void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
326 UFE& blossom, UFE& tree);
328 void normShrink(Node v, typename Graph::template NodeMap<Node>& ear,
329 UFE& blossom, UFE& tree);
331 void shrink(Node x,Node y, typename Graph::template NodeMap<Node>& ear,
332 UFE& blossom, UFE& tree,std::queue<Node>& Q);
334 void shrinkStep(Node& top, Node& middle, Node& bottom,
335 typename Graph::template NodeMap<Node>& ear,
336 UFE& blossom, UFE& tree, std::queue<Node>& Q);
338 bool growOrAugment(Node& y, Node& x, typename Graph::template
339 NodeMap<Node>& ear, UFE& blossom, UFE& tree,
340 std::queue<Node>& Q);
342 void augment(Node x, typename Graph::template NodeMap<Node>& ear,
343 UFE& blossom, UFE& tree);
348 // **********************************************************************
350 // **********************************************************************
353 template <typename Graph>
354 void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template
355 NodeMap<Node>& ear, UFE& blossom, UFE& tree) {
356 //We have one tree which we grow, and also shrink but only if it cannot be
357 //postponed. If we augment then we return to the "for" cycle of
360 std::queue<Node> Q; //queue of the totally unscanned nodes
363 //queue of the nodes which must be scanned for a possible shrink
365 while ( !Q.empty() ) {
368 for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
369 Node y=g.runningNode(e);
370 //growOrAugment grows if y is covered by the matching and
371 //augments if not. In this latter case it returns 1.
372 if ( position[y]==C && growOrAugment(y, x, ear, blossom, tree, Q) ) return;
377 while ( !R.empty() ) {
381 for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
382 Node y=g.runningNode(e);
384 if ( position[y] == D && blossom.find(x) != blossom.find(y) )
385 //Recall that we have only one tree.
386 shrink( x, y, ear, blossom, tree, Q);
388 while ( !Q.empty() ) {
391 for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
392 Node y=g.runningNode(e);
393 //growOrAugment grows if y is covered by the matching and
394 //augments if not. In this latter case it returns 1.
395 if ( position[y]==C && growOrAugment(y, x, ear, blossom, tree, Q) ) return;
400 } // while ( !R.empty() )
404 template <typename Graph>
405 void MaxMatching<Graph>::normShrink(Node v,
406 typename Graph::template
408 UFE& blossom, UFE& tree) {
409 //We have one tree, which we grow and shrink. If we augment then we
410 //return to the "for" cycle of runEdmonds().
412 std::queue<Node> Q; //queue of the unscanned nodes
414 while ( !Q.empty() ) {
419 for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
420 Node y=g.runningNode(e);
422 switch ( position[y] ) {
423 case D: //x and y must be in the same tree
424 if ( blossom.find(x) != blossom.find(y) )
425 //x and y are in the same tree
426 shrink( x, y, ear, blossom, tree, Q);
429 //growOrAugment grows if y is covered by the matching and
430 //augments if not. In this latter case it returns 1.
431 if ( growOrAugment(y, x, ear, blossom, tree, Q) ) return;
440 template <typename Graph>
441 void MaxMatching<Graph>::shrink(Node x,Node y, typename
442 Graph::template NodeMap<Node>& ear,
443 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
444 //x and y are the two adjacent vertices in two blossoms.
446 typename Graph::template NodeMap<bool> path(g,false);
448 Node b=blossom.find(x);
451 while ( b!=INVALID ) {
452 b=blossom.find(ear[b]);
455 } //we go until the root through bases of blossoms and odd vertices
458 Node middle=blossom.find(top);
460 while ( !path[middle] )
461 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
462 //Until we arrive to a node on the path, we update blossom, tree
463 //and the positions of the odd nodes.
467 middle=blossom.find(top);
469 Node blossom_base=blossom.find(base);
470 while ( middle!=blossom_base )
471 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
472 //Until we arrive to a node on the path, we update blossom, tree
473 //and the positions of the odd nodes.
475 blossom.makeRep(base);
480 template <typename Graph>
481 void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom,
482 typename Graph::template
484 UFE& blossom, UFE& tree,
485 std::queue<Node>& Q) {
486 //We traverse a blossom and update everything.
490 while ( t!=middle ) {
495 bottom=_mate[middle];
496 position.set(bottom,D);
499 Node oldmiddle=middle;
500 middle=blossom.find(top);
502 tree.erase(oldmiddle);
503 blossom.insert(bottom);
504 blossom.join(bottom, oldmiddle);
505 blossom.join(top, oldmiddle);
509 template <typename Graph>
510 bool MaxMatching<Graph>::growOrAugment(Node& y, Node& x, typename Graph::template
511 NodeMap<Node>& ear, UFE& blossom, UFE& tree,
512 std::queue<Node>& Q) {
513 //x is in a blossom in the tree, y is outside. If y is covered by
514 //the matching we grow, otherwise we augment. In this case we
517 if ( _mate[y]!=INVALID ) { //grow
525 tree.join(y,blossom.find(x));
529 augment(x, ear, blossom, tree);
538 template <typename Graph>
539 void MaxMatching<Graph>::augment(Node x,
540 typename Graph::template NodeMap<Node>& ear,
541 UFE& blossom, UFE& tree) {
543 while ( v!=INVALID ) {
551 Node y=blossom.find(x);
552 typename UFE::ItemIt it;
553 for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
554 if ( position[it] == D ) {
555 typename UFE::ItemIt b_it;
556 for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
557 position.set( b_it ,C);
559 blossom.eraseClass(it);
560 } else position.set( it ,C);
567 } //END OF NAMESPACE LEMON
569 #endif //LEMON_MAX_MATCHING_H