2 * src/lemon/max_matching.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 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
22 #include <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>
57 typedef typename Graph::Node Node;
58 typedef typename Graph::Edge Edge;
59 typedef typename Graph::UndirEdgeIt UndirEdgeIt;
60 typedef typename Graph::NodeIt NodeIt;
61 typedef typename Graph::IncEdgeIt IncEdgeIt;
63 typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
67 ///Indicates the Gallai-Edmonds decomposition of the graph.
69 ///Indicates the Gallai-Edmonds decomposition of the graph, which
70 ///shows an upper bound on the size of a maximum matching. The
71 ///nodes with pos_enum \c D induce a graph with factor-critical
72 ///components, the nodes in \c A form the canonical barrier, and the
73 ///nodes in \c C induce a graph having a perfect matching.
82 static const int HEUR_density=2;
84 typename Graph::template NodeMap<Node> mate;
85 typename Graph::template NodeMap<pos_enum> position;
89 MaxMatching(const Graph& _g) : g(_g), mate(_g,INVALID), position(_g) {}
91 ///Runs Edmonds' algorithm.
93 ///Runs Edmonds' algorithm for sparse graphs (number of edges <
94 ///2*number of nodes), and a heuristical Edmonds' algorithm with a
95 ///heuristic of postponing shrinks for dense graphs.
98 ///Runs Edmonds' algorithm.
100 ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
101 ///Edmonds' algorithm with a heuristic of postponing shrinks,
102 ///giving a faster algorithm for dense graphs.
103 void runEdmonds( int heur );
105 ///Finds a greedy matching starting from the actual matching.
107 ///Starting form the actual matching stored, it finds a maximal
109 void greedyMatching();
111 ///Returns the size of the actual matching stored.
113 ///Returns the size of the actual matching stored. After \ref
114 ///run() it returns the size of a maximum matching in the graph.
117 ///Resets the actual matching to the empty matching.
119 ///Resets the actual matching to the empty matching.
121 void resetMatching();
123 ///Reads a matching from a \c Node map of \c Nodes.
125 ///Reads a matching from a \c Node map of \c Nodes. This map must be \e
126 ///symmetric, i.e. if \c map[u]==v then \c map[v]==u must hold, and
127 ///\c uv will be an edge of the matching.
128 template<typename NMapN>
129 void readNMapNode(NMapN& map) {
130 for(NodeIt v(g); v!=INVALID; ++v) {
135 ///Writes the stored matching to a \c Node map of \c Nodes.
137 ///Writes the stored matching to a \c Node map of \c Nodes. The
138 ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
139 ///map[v]==u will hold, and now \c uv is an edge of the matching.
140 template<typename NMapN>
141 void writeNMapNode (NMapN& map) const {
142 for(NodeIt v(g); v!=INVALID; ++v) {
147 ///Reads a matching from a \c Node map of \c Edges.
149 ///Reads a matching from a \c Node map of incident \c Edges. This
150 ///map must have the property that if \c G.target(map[u])==v then \c
151 ///G.target(map[v])==u must hold, and now this edge is an edge of
153 template<typename NMapE>
154 void readNMapEdge(NMapE& map) {
155 for(NodeIt v(g); v!=INVALID; ++v) {
158 g.source(e) == v ? mate.set(v,g.target(e)) : mate.set(v,g.source(e));
162 ///Writes the matching stored to a \c Node map of \c Edges.
164 ///Writes the stored matching to a \c Node map of incident \c
165 ///Edges. This map will have the property that if \c
166 ///g.target(map[u])==v then \c g.target(map[v])==u holds, and now this
167 ///edge is an edge of the matching.
168 template<typename NMapE>
169 void writeNMapEdge (NMapE& map) const {
170 typename Graph::template NodeMap<bool> todo(g,true);
171 for(NodeIt v(g); v!=INVALID; ++v) {
172 if ( todo[v] && mate[v]!=INVALID ) {
174 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
175 if ( g.target(e) == u ) {
188 ///Reads a matching from an \c Edge map of \c bools.
190 ///Reads a matching from an \c Edge map of \c bools. This map must
191 ///have the property that there are no two adjacent edges \c e, \c
192 ///f with \c map[e]==map[f]==true. The edges \c e with \c
193 ///map[e]==true form the matching.
194 template<typename EMapB>
195 void readEMapBool(EMapB& map) {
196 for(UndirEdgeIt e(g); e!=INVALID; ++e) {
207 ///Writes the matching stored to an \c Edge map of \c bools.
209 ///Writes the matching stored to an \c Edge map of \c bools. This
210 ///map will have the property that there are no two adjacent edges
211 ///\c e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
212 ///map[e]==true form the matching.
213 template<typename EMapB>
214 void writeEMapBool (EMapB& map) const {
215 for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
217 typename Graph::template NodeMap<bool> todo(g,true);
218 for(NodeIt v(g); v!=INVALID; ++v) {
219 if ( todo[v] && mate[v]!=INVALID ) {
221 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
222 if ( g.target(e) == u ) {
234 ///Writes the canonical decomposition of the graph after running
237 ///After calling any run methods of the class, it writes the
238 ///Gallai-Edmonds canonical decomposition of the graph. \c map
239 ///must be a node map of \ref pos_enum 's.
240 template<typename NMapEnum>
241 void writePos (NMapEnum& map) const {
242 for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
247 void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
248 UFE& blossom, UFE& tree);
250 void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
251 UFE& blossom, UFE& tree);
253 bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
254 UFE& blossom, UFE& tree, std::queue<Node>& Q);
256 void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
257 UFE& blossom, UFE& tree, std::queue<Node>& Q);
259 void augment(Node x, typename Graph::NodeMap<Node>& ear,
260 UFE& blossom, UFE& tree);
265 // **********************************************************************
267 // **********************************************************************
270 template <typename Graph>
271 void MaxMatching<Graph>::run() {
272 if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
275 } else runEdmonds(1);
279 template <typename Graph>
280 void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
282 for(NodeIt v(g); v!=INVALID; ++v)
285 typename Graph::template NodeMap<Node> ear(g,INVALID);
286 //undefined for the base nodes of the blossoms (i.e. for the
287 //representative elements of UFE blossom) and for the nodes in C
289 typename UFE::MapType blossom_base(g);
290 UFE blossom(blossom_base);
291 typename UFE::MapType tree_base(g);
294 for(NodeIt v(g); v!=INVALID; ++v) {
295 if ( position[v]==C && mate[v]==INVALID ) {
299 if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
300 else normShrink( v, ear, blossom, tree );
306 template <typename Graph>
307 void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
308 UFE& blossom, UFE& tree) {
310 std::queue<Node> Q; //queue of the totally unscanned nodes
313 //queue of the nodes which must be scanned for a possible shrink
315 while ( !Q.empty() ) {
318 if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
322 while ( !R.empty() ) {
326 for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
329 if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
330 //x and y must be in the same tree
332 typename Graph::template NodeMap<bool> path(g,false);
334 Node b=blossom.find(x);
337 while ( b!=INVALID ) {
338 b=blossom.find(ear[b]);
341 } //going till the root
344 Node middle=blossom.find(top);
346 while ( !path[middle] )
347 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
351 middle=blossom.find(top);
353 Node blossom_base=blossom.find(base);
354 while ( middle!=blossom_base )
355 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
357 blossom.makeRep(base);
358 } // if shrink is needed
360 while ( !Q.empty() ) {
363 if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
367 } // while ( !R.empty() )
371 template <typename Graph>
372 void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,
373 UFE& blossom, UFE& tree) {
375 std::queue<Node> Q; //queue of the unscanned nodes
377 while ( !Q.empty() ) {
382 for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
385 switch ( position[y] ) {
386 case D: //x and y must be in the same tree
388 if ( blossom.find(x) != blossom.find(y) ) { //shrink
389 typename Graph::template NodeMap<bool> path(g,false);
391 Node b=blossom.find(x);
394 while ( b!=INVALID ) {
395 b=blossom.find(ear[b]);
398 } //going till the root
401 Node middle=blossom.find(top);
403 while ( !path[middle] )
404 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
408 middle=blossom.find(top);
410 Node blossom_base=blossom.find(base);
411 while ( middle!=blossom_base )
412 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
414 blossom.makeRep(base);
418 if ( mate[y]!=INVALID ) { //grow
427 tree.join(y,blossom.find(x));
431 augment(x, ear, blossom, tree);
443 template <typename Graph>
444 void MaxMatching<Graph>::greedyMatching() {
445 for(NodeIt v(g); v!=INVALID; ++v)
446 if ( mate[v]==INVALID ) {
447 for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
449 if ( mate[y]==INVALID && y!=v ) {
458 template <typename Graph>
459 int MaxMatching<Graph>::size() const {
461 for(NodeIt v(g); v!=INVALID; ++v) {
462 if ( mate[v]!=INVALID ) {
469 template <typename Graph>
470 void MaxMatching<Graph>::resetMatching() {
471 for(NodeIt v(g); v!=INVALID; ++v)
475 template <typename Graph>
476 bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
477 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
478 for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
481 if ( position[y]==C ) {
482 if ( mate[y]!=INVALID ) { //grow
490 tree.join(y,blossom.find(x));
494 augment(x, ear, blossom, tree);
504 template <typename Graph>
505 void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
506 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
509 while ( t!=middle ) {
515 position.set(bottom,D);
518 Node oldmiddle=middle;
519 middle=blossom.find(top);
521 tree.erase(oldmiddle);
522 blossom.insert(bottom);
523 blossom.join(bottom, oldmiddle);
524 blossom.join(top, oldmiddle);
527 template <typename Graph>
528 void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
529 UFE& blossom, UFE& tree) {
531 while ( v!=INVALID ) {
539 typename UFE::ItemIt it;
540 for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
541 if ( position[it] == D ) {
542 typename UFE::ItemIt b_it;
543 for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
544 position.set( b_it ,C);
546 blossom.eraseClass(it);
547 } else position.set( it ,C);
555 } //END OF NAMESPACE LEMON