Added a short tutorial on using graphs.
2 #ifndef HUGO_MAX_MATCHING_H
3 #define HUGO_MAX_MATCHING_H
7 ///\brief Maximum matching algorithm.
12 #include <unionfind.h>
19 ///Maximum matching algorithms class.
21 ///This class provides Edmonds' alternating forest matching
22 ///algorithm. The starting matching (if any) can be passed to the
23 ///algorithm using read-in functions \ref readNMapNode, \ref
24 ///readNMapEdge or \ref readEMapBool depending on the container. The
25 ///resulting maximum matching can be attained by write-out functions
26 ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
27 ///depending on the preferred container.
29 ///The dual side of a mathcing is a map of the nodes to
30 ///MaxMatching::pos_enum, having values D, A and C showing the
31 ///Gallai-Edmonds decomposition of the graph. The nodes in D induce
32 ///a graph with factor-critical components, the nodes in A form the
33 ///barrier, and the nodes in C induce a graph having a perfect
34 ///matching. This decomposition can be attained by calling \ref
35 ///writePos after running the algorithm. Before subsequent runs,
36 ///the function \ref resetPos() must be called.
38 ///\param Graph The undirected graph type the algorithm runs on.
40 ///\author Jacint Szabo
41 template <typename Graph>
43 typedef typename Graph::Node Node;
44 typedef typename Graph::Edge Edge;
45 typedef typename Graph::EdgeIt EdgeIt;
46 typedef typename Graph::NodeIt NodeIt;
47 typedef typename Graph::OutEdgeIt OutEdgeIt;
49 typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
53 ///Indicates the Gallai-Edmonds decomposition of the graph.
55 ///Indicates the Gallai-Edmonds decomposition of the graph, which
56 ///shows an upper bound on the size of a maximum matching. The
57 ///nodes with pos_enum \c D induce a graph with factor-critical
58 ///components, the nodes in \c A form the canonical barrier, and the
59 ///nodes in \c C induce a graph having a perfect matching.
69 typename Graph::template NodeMap<Node> mate;
70 typename Graph::template NodeMap<pos_enum> position;
74 MaxMatching(const Graph& _G) : G(_G), mate(_G,INVALID), position(_G,C) {}
76 ///Runs Edmonds' algorithm.
78 ///Runs Edmonds' algorithm for sparse graphs (edgeNum >=
79 ///2*nodeNum), and a heuristical Edmonds' algorithm with a
80 ///heuristic of postponing shrinks for dense graphs. \pre Before
81 ///the subsequent calls \ref resetPos must be called.
84 ///Runs Edmonds' algorithm.
86 ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
87 ///Edmonds' algorithm with a heuristic of postponing shrinks,
88 ///giving a faster algorithm for dense graphs. \pre Before the
89 ///subsequent calls \ref resetPos must be called.
90 void runEdmonds( int heur );
92 ///Finds a greedy matching starting from the actual matching.
94 ///Starting form the actual matching stored, it finds a maximal
96 void greedyMatching();
98 ///Returns the size of the actual matching stored.
100 ///Returns the size of the actual matching stored. After \ref
101 ///run() it returns the size of a maximum matching in the graph.
104 ///Resets the map storing the Gallai-Edmonds decomposition.
106 ///Resets the map storing the Gallai-Edmonds decomposition of the
107 ///graph, making it possible to run the algorithm. Must be called
108 ///before all runs of the Edmonds algorithm, except for the first
112 ///Resets the actual matching to the empty matching.
114 ///Resets the actual matching to the empty matching.
116 void resetMatching();
118 ///Reads a matching from a \c Node map of \c Nodes.
120 ///Reads a matching from a \c Node map of \c Nodes. This map must be \e
121 ///symmetric, i.e. if \c map[u]=v then \c map[v]=u must hold, and
122 ///now \c uv is an edge of the matching.
123 template<typename NMapN>
124 void readNMapNode(NMapN& map) {
126 for( G.first(v); G.valid(v); G.next(v)) {
131 ///Writes the stored matching to a \c Node map of \c Nodes.
133 ///Writes the stored matching to a \c Node map of \c Nodes. The
134 ///resulting map will be \e symmetric, i.e. if \c map[u]=v then \c
135 ///map[v]=u will hold, and now \c uv is an edge of the matching.
136 template<typename NMapN>
137 void writeNMapNode (NMapN& map) const {
139 for( G.first(v); G.valid(v); G.next(v)) {
144 ///Reads a matching from a \c Node map of \c Edges.
146 ///Reads a matching from a \c Node map of incident \c Edges. This
147 ///map must have the property that if \c G.bNode(map[u])=v then \c
148 ///G.bNode(map[v])=u must hold, and now this edge is an edge of
150 template<typename NMapE>
151 void readNMapEdge(NMapE& map) {
153 for( G.first(v); G.valid(v); G.next(v)) {
156 G.tail(e) == v ? mate.set(v,G.head(e)) : mate.set(v,G.tail(e));
160 ///Writes the matching stored to a \c Node map of \c Edges.
162 ///Writes the stored matching to a \c Node map of incident \c
163 ///Edges. This map will have the property that if \c
164 ///G.bNode(map[u])=v then \c G.bNode(map[v])=u holds, and now this
165 ///edge is an edge of the matching.
166 template<typename NMapE>
167 void writeNMapEdge (NMapE& map) const {
168 typename Graph::template NodeMap<bool> todo(G,false);
170 for( G.first(v); G.valid(v); G.next(v)) {
171 if ( mate[v]!=INVALID ) todo.set(v,true);
174 for( G.first(e); G.valid(e); G.next(e)) {
175 if ( todo[G.head(e)] && todo[G.tail(e)] ) {
178 if ( mate[u]=v && mate[v]=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) {
197 for( G.first(e); G.valid(e); G.next(e)) {
208 ///Writes the matching stored to an \c Edge map of \c bools.
210 ///Writes the matching stored to an \c Edge map of \c bools. This
211 ///map will have the property that there are no two adjacent edges
212 ///\c e, \c f with \c map[e]=map[f]=true. The edges \c e with \c
213 ///map[e]=true form the matching.
214 template<typename EMapB>
215 void writeEMapBool (EMapB& map) const {
216 typename Graph::template NodeMap<bool> todo(G,false);
218 for( G.first(v); G.valid(v); G.next(v)) {
219 if ( mate[v]!=INVALID ) todo.set(v,true);
223 for( G.first(e); G.valid(e); G.next(e)) {
225 if ( todo[G.head(e)] && todo[G.tail(e)] ) {
228 if ( mate[u]=v && mate[v]=u ) {
237 ///Writes the canonical decomposition of the graph after running
240 ///After calling any run methods of the class, and before calling
241 ///\ref resetPos(), it writes the Gallai-Edmonds canonical
242 ///decomposition of the graph. \c map must be a node map of \ref pos_enum 's.
243 template<typename NMapEnum>
244 void writePos (NMapEnum& map) const {
246 for( G.first(v); G.valid(v); G.next(v)) map.set(v,position[v]);
251 void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
252 UFE& blossom, UFE& tree);
254 void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
255 UFE& blossom, UFE& tree);
257 bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
258 UFE& blossom, UFE& tree, std::queue<Node>& Q);
260 void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
261 UFE& blossom, UFE& tree, std::queue<Node>& Q);
263 void augment(Node x, typename Graph::NodeMap<Node>& ear,
264 UFE& blossom, UFE& tree);
269 // **********************************************************************
271 // **********************************************************************
274 template <typename Graph>
275 void MaxMatching<Graph>::run() {
276 if ( G.edgeNum() > 2*G.nodeNum() ) {
279 } else runEdmonds(0);
282 template <typename Graph>
283 void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
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);
295 for( G.first(v); G.valid(v); G.next(v) ) {
296 if ( position[v]==C && mate[v]==INVALID ) {
300 if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
301 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() ) {
327 for( G.first(e,x); G.valid(e); G.next(e) ) {
330 if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
331 //x and y must be in the same tree
333 typename Graph::template NodeMap<bool> path(G,false);
335 Node b=blossom.find(x);
338 while ( b!=INVALID ) {
339 b=blossom.find(ear[b]);
342 } //going till the root
345 Node middle=blossom.find(top);
347 while ( !path[middle] )
348 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
352 middle=blossom.find(top);
354 Node blossom_base=blossom.find(base);
355 while ( middle!=blossom_base )
356 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
358 blossom.makeRep(base);
359 } // if shrink is needed
361 while ( !Q.empty() ) {
364 if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
368 } // 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( G.first(e,x); G.valid(e); G.next(e) ) {
385 switch ( position[y] ) {
386 case D: //x and y must be in the same tree
387 if ( blossom.find(x) != blossom.find(y) ) { //shrink
388 typename Graph::template NodeMap<bool> path(G,false);
390 Node b=blossom.find(x);
393 while ( b!=INVALID ) {
394 b=blossom.find(ear[b]);
397 } //going till the root
400 Node middle=blossom.find(top);
402 while ( !path[middle] )
403 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
407 middle=blossom.find(top);
409 Node blossom_base=blossom.find(base);
410 while ( middle!=blossom_base )
411 shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
413 blossom.makeRep(base);
417 if ( mate[y]!=INVALID ) { //grow
425 tree.join(y,blossom.find(x));
429 augment(x, ear, blossom, tree);
441 template <typename Graph>
442 void MaxMatching<Graph>::greedyMatching() {
444 for( G.first(v); G.valid(v); G.next(v) )
445 if ( mate[v]==INVALID ) {
447 for( G.first(e,v); G.valid(e); G.next(e) ) {
449 if ( mate[y]==INVALID && y!=v ) {
458 template <typename Graph>
459 int MaxMatching<Graph>::size() const {
462 for(G.first(v); G.valid(v); G.next(v) ) {
463 if ( G.valid(mate[v]) ) {
470 template <typename Graph>
471 void MaxMatching<Graph>::resetPos() {
473 for( G.first(v); G.valid(v); G.next(v))
477 template <typename Graph>
478 void MaxMatching<Graph>::resetMatching() {
480 for( G.first(v); G.valid(v); G.next(v))
484 template <typename Graph>
485 bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
486 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
488 for( G.first(e,x); G.valid(e); G.next(e) ) {
491 if ( position[y]==C ) {
492 if ( mate[y]!=INVALID ) { //grow
500 tree.join(y,blossom.find(x));
504 augment(x, ear, blossom, tree);
514 template <typename Graph>
515 void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
516 UFE& blossom, UFE& tree, std::queue<Node>& Q) {
519 while ( t!=middle ) {
525 position.set(bottom,D);
528 Node oldmiddle=middle;
529 middle=blossom.find(top);
531 tree.erase(oldmiddle);
532 blossom.insert(bottom);
533 blossom.join(bottom, oldmiddle);
534 blossom.join(top, oldmiddle);
537 template <typename Graph>
538 void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
539 UFE& blossom, UFE& tree) {
541 while ( G.valid(v) ) {
549 typename UFE::ItemIt it;
550 for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
551 if ( position[it] == D ) {
552 typename UFE::ItemIt b_it;
553 for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
554 position.set( b_it ,C);
556 blossom.eraseClass(it);
557 } else position.set( it ,C);
566 } //END OF NAMESPACE HUGO