Magic triangle is READY.
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::UndirEdge UndirEdge;
63 typedef typename Graph::UndirEdgeIt UndirEdgeIt;
64 typedef typename Graph::NodeIt NodeIt;
65 typedef typename Graph::IncEdgeIt IncEdgeIt;
67 typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
71 ///Indicates the Gallai-Edmonds decomposition of the graph.
73 ///Indicates the Gallai-Edmonds decomposition of the graph, which
74 ///shows an upper bound on the size of a maximum matching. The
75 ///nodes with pos_enum \c D induce a graph with factor-critical
76 ///components, the nodes in \c A form the canonical barrier, and the
77 ///nodes in \c C induce a graph having a perfect matching.
86 static const int HEUR_density=2;
88 typename Graph::template NodeMap<Node> _mate;
89 typename Graph::template NodeMap<pos_enum> position;
93 MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
95 ///Runs Edmonds' algorithm.
97 ///Runs Edmonds' algorithm for sparse graphs (number of edges <
98 ///2*number of nodes), and a heuristical Edmonds' algorithm with a
99 ///heuristic of postponing shrinks for dense graphs.
102 ///Runs Edmonds' algorithm.
104 ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
105 ///Edmonds' algorithm with a heuristic of postponing shrinks,
106 ///giving a faster algorithm for dense graphs.
107 void runEdmonds( int heur );
109 ///Finds a greedy matching starting from the actual matching.
111 ///Starting form the actual matching stored, it finds a maximal
113 void greedyMatching();
115 ///Returns the size of the actual matching stored.
117 ///Returns the size of the actual matching stored. After \ref
118 ///run() it returns the size of a maximum matching in the graph.
121 ///Resets the actual matching to the empty matching.
123 ///Resets the actual matching to the empty matching.
125 void resetMatching();
127 ///Returns the mate of a node in the actual matching.
129 ///Returns the mate of a \c node in the actual matching.
130 ///Returns INVALID if the \c node is not covered by the actual matching.
131 Node mate(Node& node) const {
135 ///Reads a matching from a \c Node valued \c Node map.
137 ///Reads a matching from a \c Node valued \c Node map. This map
138 ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
139 ///must hold, and \c uv will be an edge of the matching.
140 template<typename NMapN>
141 void readNMapNode(NMapN& map) {
142 for(NodeIt v(g); v!=INVALID; ++v) {
147 ///Writes the stored matching to a \c Node valued \c Node map.
149 ///Writes the stored matching to a \c Node valued \c Node map. The
150 ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
151 ///map[v]==u will hold, and now \c uv is an edge of the matching.
152 template<typename NMapN>
153 void writeNMapNode (NMapN& map) const {
154 for(NodeIt v(g); v!=INVALID; ++v) {
159 ///Reads a matching from an \c UndirEdge valued \c Node map.
161 ///Reads a matching from an \c UndirEdge valued \c Node map. \c
162 ///map[v] must be an \c UndirEdge incident to \c v. This map must
163 ///have the property that if \c g.oppositeNode(u,map[u])==v then
164 ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
165 ///joining \c u to \c v will be an edge of the matching.
166 template<typename NMapE>
167 void readNMapEdge(NMapE& map) {
168 for(NodeIt v(g); v!=INVALID; ++v) {
171 _mate.set(v,g.oppositeNode(v,e));
175 ///Writes the matching stored to an \c UndirEdge valued \c Node map.
177 ///Writes the stored matching to an \c UndirEdge valued \c Node
178 ///map. \c map[v] will be an \c UndirEdge incident to \c v. This
179 ///map will have the property that if \c g.oppositeNode(u,map[u])
180 ///== v then \c map[u]==map[v] holds, and now this edge is an edge
182 template<typename NMapE>
183 void writeNMapEdge (NMapE& map) const {
184 typename Graph::template NodeMap<bool> todo(g,true);
185 for(NodeIt v(g); v!=INVALID; ++v) {
186 if ( todo[v] && _mate[v]!=INVALID ) {
188 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
189 if ( g.runningNode(e) == u ) {
202 ///Reads a matching from a \c bool valued \c Edge map.
204 ///Reads a matching from a \c bool valued \c Edge map. This map
205 ///must have the property that there are no two incident edges \c
206 ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
207 ///map[e]==true form the matching.
208 template<typename EMapB>
209 void readEMapBool(EMapB& map) {
210 for(UndirEdgeIt e(g); e!=INVALID; ++e) {
221 ///Writes the matching stored to a \c bool valued \c Edge map.
223 ///Writes the matching stored to a \c bool valued \c Edge
224 ///map. This map will have the property that there are no two
225 ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
226 ///edges \c e with \c map[e]==true form the matching.
227 template<typename EMapB>
228 void writeEMapBool (EMapB& map) const {
229 for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
231 typename Graph::template NodeMap<bool> todo(g,true);
232 for(NodeIt v(g); v!=INVALID; ++v) {
233 if ( todo[v] && _mate[v]!=INVALID ) {
235 for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
236 if ( g.runningNode(e) == u ) {
248 ///Writes the canonical decomposition of the graph after running
251 ///After calling any run methods of the class, it writes the
252 ///Gallai-Edmonds canonical decomposition of the graph. \c map
253 ///must be a node map of \ref pos_enum 's.
254 template<typename NMapEnum>
255 void writePos (NMapEnum& map) const {
256 for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
262 void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
263 UFE& blossom, UFE& tree);
265 void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
266 UFE& blossom, UFE& tree);
268 bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
269 UFE& blossom, UFE& tree, std::queue<Node>& Q);
271 void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
272 UFE& blossom, UFE& tree, std::queue<Node>& Q);
274 void augment(Node x, typename Graph::NodeMap<Node>& ear,
275 UFE& blossom, UFE& tree);
280 // **********************************************************************
282 // **********************************************************************
285 template <typename Graph>
286 void MaxMatching<Graph>::run() {
287 if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
290 } else runEdmonds(1);
294 template <typename Graph>
295 void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
297 for(NodeIt v(g); v!=INVALID; ++v)
300 typename Graph::template NodeMap<Node> ear(g,INVALID);
301 //undefined for the base nodes of the blossoms (i.e. for the
302 //representative elements of UFE blossom) and for the nodes in C
304 typename UFE::MapType blossom_base(g);
305 UFE blossom(blossom_base);
306 typename UFE::MapType tree_base(g);
308 //If these UFE's would be members of the class then also
309 //blossom_base and tree_base should be a member.
311 for(NodeIt v(g); v!=INVALID; ++v) {
312 if ( position[v]==C && _mate[v]==INVALID ) {
316 if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
317 else normShrink( v, ear, blossom, tree );
323 template <typename Graph>
324 void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
325 UFE& blossom, UFE& tree) {
327 std::queue<Node> Q; //queue of the totally unscanned nodes
330 //queue of the nodes which must be scanned for a possible shrink
332 while ( !Q.empty() ) {
335 if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
339 while ( !R.empty() ) {
343 for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
344 Node y=g.runningNode(e);
346 if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
347 //x and y must be in the same tree
349 typename Graph::template NodeMap<bool> path(g,false);
351 Node b=blossom.find(x);
354 while ( b!=INVALID ) {
355 b=blossom.find(ear[b]);
358 } //going till the root
361 Node middle=blossom.find(top);
363 while ( !path[middle] )
364 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
368 middle=blossom.find(top);
370 Node blossom_base=blossom.find(base);
371 while ( middle!=blossom_base )
372 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
374 blossom.makeRep(base);
375 } // if shrink is needed
377 while ( !Q.empty() ) {
380 if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
384 } // while ( !R.empty() )
388 template <typename Graph>
389 void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,
390 UFE& blossom, UFE& tree) {
392 std::queue<Node> Q; //queue of the unscanned nodes
394 while ( !Q.empty() ) {
399 for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
400 Node y=g.runningNode(e);
402 switch ( position[y] ) {
403 case D: //x and y must be in the same tree
405 if ( blossom.find(x) != blossom.find(y) ) { //shrink
406 typename Graph::template NodeMap<bool> path(g,false);
408 Node b=blossom.find(x);
411 while ( b!=INVALID ) {
412 b=blossom.find(ear[b]);
415 } //going till the root
418 Node middle=blossom.find(top);
420 while ( !path[middle] )
421 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
425 middle=blossom.find(top);
427 Node blossom_base=blossom.find(base);
428 while ( middle!=blossom_base )
429 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
431 blossom.makeRep(base);
435 if ( _mate[y]!=INVALID ) { //grow
444 tree.join(y,blossom.find(x));
448 augment(x, ear, blossom, tree);
460 template <typename Graph>
461 void MaxMatching<Graph>::greedyMatching() {
462 for(NodeIt v(g); v!=INVALID; ++v)
463 if ( _mate[v]==INVALID ) {
464 for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
465 Node y=g.runningNode(e);
466 if ( _mate[y]==INVALID && y!=v ) {
475 template <typename Graph>
476 int MaxMatching<Graph>::size() const {
478 for(NodeIt v(g); v!=INVALID; ++v) {
479 if ( _mate[v]!=INVALID ) {
486 template <typename Graph>
487 void MaxMatching<Graph>::resetMatching() {
488 for(NodeIt v(g); v!=INVALID; ++v)
489 _mate.set(v,INVALID);
492 template <typename Graph>
493 bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
494 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
495 for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
496 Node y=g.runningNode(e);
498 if ( position[y]==C ) {
499 if ( _mate[y]!=INVALID ) { //grow
507 tree.join(y,blossom.find(x));
511 augment(x, ear, blossom, tree);
521 template <typename Graph>
522 void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
523 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
526 while ( t!=middle ) {
531 bottom=_mate[middle];
532 position.set(bottom,D);
535 Node oldmiddle=middle;
536 middle=blossom.find(top);
538 tree.erase(oldmiddle);
539 blossom.insert(bottom);
540 blossom.join(bottom, oldmiddle);
541 blossom.join(top, oldmiddle);
544 template <typename Graph>
545 void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
546 UFE& blossom, UFE& tree) {
548 while ( v!=INVALID ) {
556 typename UFE::ItemIt it;
557 for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
558 if ( position[it] == D ) {
559 typename UFE::ItemIt b_it;
560 for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
561 position.set( b_it ,C);
563 blossom.eraseClass(it);
564 } else position.set( it ,C);
572 } //END OF NAMESPACE LEMON
574 #endif //LEMON_MAX_MATCHING_H