2 * src/lemon/max_matching.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_MAX_MATCHING_H
18 #define LEMON_MAX_MATCHING_H
21 #include <lemon/invalid.h>
22 #include <lemon/unionfind.h>
23 #include <lemon/graph_utils.h>
27 ///\brief Maximum matching algorithm.
34 ///Edmonds' alternating forest maximum matching algorithm.
36 ///This class provides Edmonds' alternating forest matching
37 ///algorithm. The starting matching (if any) can be passed to the
38 ///algorithm using read-in functions \ref readNMapNode, \ref
39 ///readNMapEdge or \ref readEMapBool depending on the container. The
40 ///resulting maximum matching can be attained by write-out functions
41 ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
42 ///depending on the preferred container.
44 ///The dual side of a matching is a map of the nodes to
45 ///MaxMatching::pos_enum, having values D, A and C showing the
46 ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
47 ///a graph with factor-critical components, the nodes in A form the
48 ///barrier, and the nodes in C induce a graph having a perfect
49 ///matching. This decomposition can be attained by calling \ref
50 ///writePos after running the algorithm.
52 ///\param Graph The undirected graph type the algorithm runs on.
54 ///\author Jacint Szabo
55 template <typename Graph>
60 typedef typename Graph::Node Node;
61 typedef typename Graph::Edge Edge;
62 typedef typename Graph::UndirEdgeIt UndirEdgeIt;
63 typedef typename Graph::NodeIt NodeIt;
64 typedef typename Graph::IncEdgeIt IncEdgeIt;
66 typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
70 ///Indicates the Gallai-Edmonds decomposition of the graph.
72 ///Indicates the Gallai-Edmonds decomposition of the graph, which
73 ///shows an upper bound on the size of a maximum matching. The
74 ///nodes with pos_enum \c D induce a graph with factor-critical
75 ///components, the nodes in \c A form the canonical barrier, and the
76 ///nodes in \c C induce a graph having a perfect matching.
85 static const int HEUR_density=2;
87 typename Graph::template NodeMap<Node> _mate;
88 typename Graph::template NodeMap<pos_enum> position;
92 MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
94 ///Runs Edmonds' algorithm.
96 ///Runs Edmonds' algorithm for sparse graphs (number of edges <
97 ///2*number of nodes), and a heuristical Edmonds' algorithm with a
98 ///heuristic of postponing shrinks for dense graphs.
101 ///Runs Edmonds' algorithm.
103 ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
104 ///Edmonds' algorithm with a heuristic of postponing shrinks,
105 ///giving a faster algorithm for dense graphs.
106 void runEdmonds( int heur );
108 ///Finds a greedy matching starting from the actual matching.
110 ///Starting form the actual matching stored, it finds a maximal
112 void greedyMatching();
114 ///Returns the size of the actual matching stored.
116 ///Returns the size of the actual matching stored. After \ref
117 ///run() it returns the size of a maximum matching in the graph.
120 ///Resets the actual matching to the empty matching.
122 ///Resets the actual matching to the empty matching.
124 void resetMatching();
126 ///Returns the mate of a node in the actual matching.
128 ///Returns the mate of a \c node in the actual matching.
129 ///Returns INVALID if the \c node is not covered by the actual matching.
130 Node mate(Node& node) const {
134 ///Reads a matching from a \c Node valued \c Node map.
136 ///Reads a matching from a \c Node valued \c Node map. This map
137 ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
138 ///must hold, and \c uv will be an edge of the matching.
139 template<typename NMapN>
140 void readNMapNode(NMapN& map) {
141 for(NodeIt v(g); v!=INVALID; ++v) {
146 ///Writes the stored matching to a \c Node valued \c Node map.
148 ///Writes the stored matching to a \c Node valued \c Node map. The
149 ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
150 ///map[v]==u will hold, and now \c uv is an edge of the matching.
151 template<typename NMapN>
152 void writeNMapNode (NMapN& map) const {
153 for(NodeIt v(g); v!=INVALID; ++v) {
158 ///Reads a matching from an \c UndirEdge valued \c Node map.
160 ///Reads a matching from an \c UndirEdge valued \c Node map. \c
161 ///map[v] must be an \c UndirEdge incident to \c v. This map must
162 ///have the property that if \c g.oppositeNode(u,map[u])==v then
163 ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
164 ///joining \c u to \c v will be an edge of the matching.
165 template<typename NMapE>
166 void readNMapEdge(NMapE& map) {
167 for(NodeIt v(g); v!=INVALID; ++v) {
170 _mate.set(v,g.oppositeNode(v,e));
174 ///Writes the matching stored to an \c UndirEdge valued \c Node map.
176 ///Writes the stored matching to an \c UndirEdge valued \c Node
177 ///map. \c map[v] will be an \c UndirEdge incident to \c v. This
178 ///map will have the property that if \c g.oppositeNode(u,map[u])
179 ///== v then \c map[u]==map[v] holds, and now this edge is an edge
181 template<typename NMapE>
182 void writeNMapEdge (NMapE& map) const {
183 typename Graph::template NodeMap<bool> todo(g,true);
184 for(NodeIt v(g); v!=INVALID; ++v) {
185 if ( todo[v] && _mate[v]!=INVALID ) {
187 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
188 if ( g.runningNode(e) == u ) {
201 ///Reads a matching from a \c bool valued \c Edge map.
203 ///Reads a matching from a \c bool valued \c Edge map. This map
204 ///must have the property that there are no two incident edges \c
205 ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
206 ///map[e]==true form the matching.
207 template<typename EMapB>
208 void readEMapBool(EMapB& map) {
209 for(UndirEdgeIt e(g); e!=INVALID; ++e) {
220 ///Writes the matching stored to a \c bool valued \c Edge map.
222 ///Writes the matching stored to a \c bool valued \c Edge
223 ///map. This map will have the property that there are no two
224 ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
225 ///edges \c e with \c map[e]==true form the matching.
226 template<typename EMapB>
227 void writeEMapBool (EMapB& map) const {
228 for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
230 typename Graph::template NodeMap<bool> todo(g,true);
231 for(NodeIt v(g); v!=INVALID; ++v) {
232 if ( todo[v] && _mate[v]!=INVALID ) {
234 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
235 if ( g.runningNode(e) == u ) {
247 ///Writes the canonical decomposition of the graph after running
250 ///After calling any run methods of the class, it writes the
251 ///Gallai-Edmonds canonical decomposition of the graph. \c map
252 ///must be a node map of \ref pos_enum 's.
253 template<typename NMapEnum>
254 void writePos (NMapEnum& map) const {
255 for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
261 void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
262 UFE& blossom, UFE& tree);
264 void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
265 UFE& blossom, UFE& tree);
267 bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
268 UFE& blossom, UFE& tree, std::queue<Node>& Q);
270 void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
271 UFE& blossom, UFE& tree, std::queue<Node>& Q);
273 void augment(Node x, typename Graph::NodeMap<Node>& ear,
274 UFE& blossom, UFE& tree);
279 // **********************************************************************
281 // **********************************************************************
284 template <typename Graph>
285 void MaxMatching<Graph>::run() {
286 if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
289 } else runEdmonds(1);
293 template <typename Graph>
294 void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
296 for(NodeIt v(g); v!=INVALID; ++v)
299 typename Graph::template NodeMap<Node> ear(g,INVALID);
300 //undefined for the base nodes of the blossoms (i.e. for the
301 //representative elements of UFE blossom) and for the nodes in C
303 typename UFE::MapType blossom_base(g);
304 UFE blossom(blossom_base);
305 typename UFE::MapType tree_base(g);
307 //If these UFE's would be members of the class then also
308 //blossom_base and tree_base should be a member.
310 for(NodeIt v(g); v!=INVALID; ++v) {
311 if ( position[v]==C && _mate[v]==INVALID ) {
315 if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
316 else normShrink( v, ear, blossom, tree );
322 template <typename Graph>
323 void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
324 UFE& blossom, UFE& tree) {
326 std::queue<Node> Q; //queue of the totally unscanned nodes
329 //queue of the nodes which must be scanned for a possible shrink
331 while ( !Q.empty() ) {
334 if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
338 while ( !R.empty() ) {
342 for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
343 Node y=g.runningNode(e);
345 if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
346 //x and y must be in the same tree
348 typename Graph::template NodeMap<bool> path(g,false);
350 Node b=blossom.find(x);
353 while ( b!=INVALID ) {
354 b=blossom.find(ear[b]);
357 } //going till the root
360 Node middle=blossom.find(top);
362 while ( !path[middle] )
363 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
367 middle=blossom.find(top);
369 Node blossom_base=blossom.find(base);
370 while ( middle!=blossom_base )
371 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
373 blossom.makeRep(base);
374 } // if shrink is needed
376 while ( !Q.empty() ) {
379 if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
383 } // while ( !R.empty() )
387 template <typename Graph>
388 void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,
389 UFE& blossom, UFE& tree) {
391 std::queue<Node> Q; //queue of the unscanned nodes
393 while ( !Q.empty() ) {
398 for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
399 Node y=g.runningNode(e);
401 switch ( position[y] ) {
402 case D: //x and y must be in the same tree
404 if ( blossom.find(x) != blossom.find(y) ) { //shrink
405 typename Graph::template NodeMap<bool> path(g,false);
407 Node b=blossom.find(x);
410 while ( b!=INVALID ) {
411 b=blossom.find(ear[b]);
414 } //going till the root
417 Node middle=blossom.find(top);
419 while ( !path[middle] )
420 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
424 middle=blossom.find(top);
426 Node blossom_base=blossom.find(base);
427 while ( middle!=blossom_base )
428 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
430 blossom.makeRep(base);
434 if ( _mate[y]!=INVALID ) { //grow
443 tree.join(y,blossom.find(x));
447 augment(x, ear, blossom, tree);
459 template <typename Graph>
460 void MaxMatching<Graph>::greedyMatching() {
461 for(NodeIt v(g); v!=INVALID; ++v)
462 if ( _mate[v]==INVALID ) {
463 for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
464 Node y=g.runningNode(e);
465 if ( _mate[y]==INVALID && y!=v ) {
474 template <typename Graph>
475 int MaxMatching<Graph>::size() const {
477 for(NodeIt v(g); v!=INVALID; ++v) {
478 if ( _mate[v]!=INVALID ) {
485 template <typename Graph>
486 void MaxMatching<Graph>::resetMatching() {
487 for(NodeIt v(g); v!=INVALID; ++v)
488 _mate.set(v,INVALID);
491 template <typename Graph>
492 bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
493 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
494 for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
495 Node y=g.runningNode(e);
497 if ( position[y]==C ) {
498 if ( _mate[y]!=INVALID ) { //grow
506 tree.join(y,blossom.find(x));
510 augment(x, ear, blossom, tree);
520 template <typename Graph>
521 void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
522 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
525 while ( t!=middle ) {
530 bottom=_mate[middle];
531 position.set(bottom,D);
534 Node oldmiddle=middle;
535 middle=blossom.find(top);
537 tree.erase(oldmiddle);
538 blossom.insert(bottom);
539 blossom.join(bottom, oldmiddle);
540 blossom.join(top, oldmiddle);
543 template <typename Graph>
544 void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
545 UFE& blossom, UFE& tree) {
547 while ( v!=INVALID ) {
555 typename UFE::ItemIt it;
556 for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
557 if ( position[it] == D ) {
558 typename UFE::ItemIt b_it;
559 for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
560 position.set( b_it ,C);
562 blossom.eraseClass(it);
563 } else position.set( it ,C);
571 } //END OF NAMESPACE LEMON
573 #endif //LEMON_MAX_MATCHING_H