jacint@537: // -*- C++ -*- jacint@537: #ifndef HUGO_MAX_MATCHING_H jacint@537: #define HUGO_MAX_MATCHING_H jacint@537: jacint@537: ///\ingroup galgs jacint@537: ///\file jacint@537: ///\brief Maximum matching algorithm. jacint@537: jacint@537: #include jacint@537: jacint@537: #include jacint@537: #include jacint@537: jacint@537: namespace hugo { jacint@537: jacint@537: /// \addtogroup galgs jacint@537: /// @{ jacint@537: jacint@537: ///Maximum matching algorithms class. jacint@537: jacint@537: ///This class provides Edmonds' alternating forest matching jacint@537: ///algorithm. The starting matching (if any) can be passed to the jacint@537: ///algorithm using read-in functions \ref readNMapNode, \ref jacint@537: ///readNMapEdge or \ref readEMapBool depending on the container. The jacint@537: ///resulting maximum matching can be attained by write-out functions jacint@537: ///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool jacint@537: ///depending on the preferred container. jacint@537: jacint@537: ///The dual side of a mathcing is a map of the nodes to jacint@537: ///MaxMatching::pos_enum, having values D, A and C showing the jacint@537: ///Gallai-Edmonds decomposition of the graph. The nodes in D induce jacint@537: ///a graph with factor-critical components, the nodes in A form the jacint@537: ///barrier, and the nodes in C induce a graph having a perfect jacint@537: ///matching. This decomposition can be attained by calling \ref jacint@537: ///writePos after running the algorithm. Before subsequent runs, jacint@537: ///the function \ref resetPos() must be called. jacint@537: jacint@537: ///\param Graph The undirected graph type the algorithm runs on. jacint@537: jacint@537: ///\author Jacint Szabo jacint@537: template jacint@537: class MaxMatching { jacint@537: typedef typename Graph::Node Node; jacint@537: typedef typename Graph::Edge Edge; jacint@537: typedef typename Graph::EdgeIt EdgeIt; jacint@537: typedef typename Graph::NodeIt NodeIt; jacint@537: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@537: jacint@537: typedef UnionFindEnum UFE; jacint@537: jacint@537: public: jacint@537: jacint@537: ///Indicates the Gallai-Edmonds decomposition of the graph. jacint@537: jacint@537: ///Indicates the Gallai-Edmonds decomposition of the graph, which jacint@537: ///shows an upper bound on the size of a maximum matching. The alpar@586: ///nodes with pos_enum \c D induce a graph with factor-critical alpar@586: ///components, the nodes in \c A form the canonical barrier, and the alpar@586: ///nodes in \c C induce a graph having a perfect matching. jacint@537: enum pos_enum { jacint@537: D=0, jacint@537: A=1, jacint@537: C=2 jacint@537: }; jacint@537: jacint@537: private: jacint@537: jacint@537: const Graph& G; jacint@537: typename Graph::template NodeMap mate; jacint@537: typename Graph::template NodeMap position; jacint@537: jacint@537: public: jacint@537: jacint@582: MaxMatching(const Graph& _G) : G(_G), mate(_G,INVALID), position(_G,C) {} jacint@537: jacint@537: ///Runs Edmonds' algorithm. jacint@537: jacint@537: ///Runs Edmonds' algorithm for sparse graphs (edgeNum >= jacint@537: ///2*nodeNum), and a heuristical Edmonds' algorithm with a jacint@537: ///heuristic of postponing shrinks for dense graphs. \pre Before jacint@537: ///the subsequent calls \ref resetPos must be called. jacint@582: inline void run(); jacint@537: jacint@537: ///Runs Edmonds' algorithm. jacint@537: jacint@537: ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs jacint@537: ///Edmonds' algorithm with a heuristic of postponing shrinks, jacint@537: ///giving a faster algorithm for dense graphs. \pre Before the jacint@537: ///subsequent calls \ref resetPos must be called. jacint@537: void runEdmonds( int heur ); jacint@537: jacint@537: ///Finds a greedy matching starting from the actual matching. jacint@537: jacint@537: ///Starting form the actual matching stored, it finds a maximal jacint@537: ///greedy matching. jacint@537: void greedyMatching(); jacint@537: jacint@537: ///Returns the size of the actual matching stored. jacint@537: jacint@537: ///Returns the size of the actual matching stored. After \ref jacint@537: ///run() it returns the size of a maximum matching in the graph. jacint@582: int size () const; jacint@537: jacint@537: ///Resets the map storing the Gallai-Edmonds decomposition. jacint@537: jacint@537: ///Resets the map storing the Gallai-Edmonds decomposition of the jacint@537: ///graph, making it possible to run the algorithm. Must be called jacint@537: ///before all runs of the Edmonds algorithm, except for the first jacint@537: ///run. jacint@537: void resetPos(); jacint@537: jacint@537: ///Resets the actual matching to the empty matching. jacint@537: jacint@537: ///Resets the actual matching to the empty matching. jacint@537: /// jacint@537: void resetMatching(); jacint@537: jacint@537: ///Reads a matching from a \c Node map of \c Nodes. jacint@537: jacint@537: ///Reads a matching from a \c Node map of \c Nodes. This map must be \e jacint@537: ///symmetric, i.e. if \c map[u]=v then \c map[v]=u must hold, and jacint@537: ///now \c uv is an edge of the matching. jacint@537: template jacint@537: void readNMapNode(NMapN& map) { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) { jacint@537: mate.set(v,map[v]); jacint@537: } jacint@537: } jacint@537: jacint@537: ///Writes the stored matching to a \c Node map of \c Nodes. jacint@537: jacint@537: ///Writes the stored matching to a \c Node map of \c Nodes. The jacint@537: ///resulting map will be \e symmetric, i.e. if \c map[u]=v then \c jacint@537: ///map[v]=u will hold, and now \c uv is an edge of the matching. jacint@537: template jacint@582: void writeNMapNode (NMapN& map) const { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) { jacint@537: map.set(v,mate[v]); jacint@537: } jacint@537: } jacint@537: jacint@537: ///Reads a matching from a \c Node map of \c Edges. jacint@537: jacint@537: ///Reads a matching from a \c Node map of incident \c Edges. This jacint@537: ///map must have the property that if \c G.bNode(map[u])=v then \c jacint@537: ///G.bNode(map[v])=u must hold, and now this edge is an edge of jacint@537: ///the matching. jacint@537: template jacint@537: void readNMapEdge(NMapE& map) { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) { jacint@537: Edge e=map[v]; jacint@537: if ( G.valid(e) ) jacint@537: G.tail(e) == v ? mate.set(v,G.head(e)) : mate.set(v,G.tail(e)); jacint@537: } jacint@537: } jacint@537: jacint@537: ///Writes the matching stored to a \c Node map of \c Edges. jacint@537: jacint@537: ///Writes the stored matching to a \c Node map of incident \c jacint@537: ///Edges. This map will have the property that if \c jacint@537: ///G.bNode(map[u])=v then \c G.bNode(map[v])=u holds, and now this jacint@537: ///edge is an edge of the matching. jacint@537: template jacint@582: void writeNMapEdge (NMapE& map) const { jacint@537: typename Graph::template NodeMap todo(G,false); jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) { jacint@537: if ( mate[v]!=INVALID ) todo.set(v,true); jacint@537: } jacint@537: NodeIt e; jacint@537: for( G.first(e); G.valid(e); G.next(e)) { jacint@537: if ( todo[G.head(e)] && todo[G.tail(e)] ) { jacint@537: Node u=G.tail(e); jacint@537: Node v=G.head(e); jacint@537: if ( mate[u]=v && mate[v]=u ) { jacint@537: map.set(u,e); jacint@537: map.set(v,e); jacint@537: todo.set(u,false); jacint@537: todo.set(v,false); jacint@537: } jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: ///Reads a matching from an \c Edge map of \c bools. jacint@537: jacint@537: ///Reads a matching from an \c Edge map of \c bools. This map must jacint@537: ///have the property that there are no two adjacent edges \c e, \c jacint@537: ///f with \c map[e]=map[f]=true. The edges \c e with \c jacint@537: ///map[e]=true form the matching. jacint@537: template jacint@537: void readEMapBool(EMapB& map) { jacint@537: EdgeIt e; jacint@537: for( G.first(e); G.valid(e); G.next(e)) { jacint@537: if ( G.valid(e) ) { jacint@537: Node u=G.tail(e); jacint@537: Node v=G.head(e); jacint@537: mate.set(u,v); jacint@537: mate.set(v,u); jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: jacint@537: ///Writes the matching stored to an \c Edge map of \c bools. jacint@537: jacint@537: ///Writes the matching stored to an \c Edge map of \c bools. This jacint@537: ///map will have the property that there are no two adjacent edges jacint@537: ///\c e, \c f with \c map[e]=map[f]=true. The edges \c e with \c jacint@537: ///map[e]=true form the matching. jacint@537: template jacint@582: void writeEMapBool (EMapB& map) const { jacint@537: typename Graph::template NodeMap todo(G,false); jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) { jacint@537: if ( mate[v]!=INVALID ) todo.set(v,true); jacint@537: } jacint@537: jacint@537: NodeIt e; jacint@537: for( G.first(e); G.valid(e); G.next(e)) { jacint@537: map.set(e,false); jacint@537: if ( todo[G.head(e)] && todo[G.tail(e)] ) { jacint@537: Node u=G.tail(e); jacint@537: Node v=G.head(e); jacint@537: if ( mate[u]=v && mate[v]=u ) { jacint@537: map.set(e,true); jacint@537: todo.set(u,false); jacint@537: todo.set(v,false); jacint@537: } jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: ///Writes the canonical decomposition of the graph after running jacint@537: ///the algorithm. jacint@537: jacint@537: ///After calling any run methods of the class, and before calling jacint@537: ///\ref resetPos(), it writes the Gallai-Edmonds canonical jacint@537: ///decomposition of the graph. \c map must be a node map of \ref pos_enum 's. jacint@537: template jacint@582: void writePos (NMapEnum& map) const { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) map.set(v,position[v]); jacint@537: } jacint@537: jacint@537: private: jacint@537: jacint@537: void lateShrink(Node v, typename Graph::template NodeMap& ear, jacint@537: UFE& blossom, UFE& tree); jacint@537: jacint@537: void normShrink(Node v, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree); jacint@537: jacint@537: bool noShrinkStep(Node x, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree, std::queue& Q); jacint@537: jacint@537: void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree, std::queue& Q); jacint@537: jacint@537: void augment(Node x, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree); jacint@537: jacint@537: }; jacint@537: jacint@537: jacint@537: // ********************************************************************** jacint@537: // IMPLEMENTATIONS jacint@537: // ********************************************************************** jacint@537: jacint@537: jacint@537: template jacint@537: void MaxMatching::run() { jacint@537: if ( G.edgeNum() > 2*G.nodeNum() ) { jacint@537: greedyMatching(); jacint@537: runEdmonds(1); jacint@537: } else runEdmonds(0); jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::runEdmonds( int heur=1 ) { jacint@537: jacint@537: typename Graph::template NodeMap ear(G,INVALID); jacint@537: //undefined for the base nodes of the blossoms (i.e. for the jacint@537: //representative elements of UFE blossom) and for the nodes in C jacint@537: jacint@537: typename UFE::MapType blossom_base(G); jacint@537: UFE blossom(blossom_base); jacint@537: typename UFE::MapType tree_base(G); jacint@537: UFE tree(tree_base); jacint@537: jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v) ) { jacint@537: if ( position[v]==C && mate[v]==INVALID ) { jacint@537: blossom.insert(v); jacint@537: tree.insert(v); jacint@537: position.set(v,D); jacint@537: if ( heur == 1 ) lateShrink( v, ear, blossom, tree ); jacint@537: else normShrink( v, ear, blossom, tree ); jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::lateShrink(Node v, typename Graph::template NodeMap& ear, jacint@537: UFE& blossom, UFE& tree) { jacint@537: jacint@537: std::queue Q; //queue of the totally unscanned nodes jacint@537: Q.push(v); jacint@537: std::queue R; jacint@537: //queue of the nodes which must be scanned for a possible shrink jacint@537: jacint@537: while ( !Q.empty() ) { jacint@537: Node x=Q.front(); jacint@537: Q.pop(); jacint@537: if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return; jacint@537: else R.push(x); jacint@537: } jacint@537: jacint@537: while ( !R.empty() ) { jacint@537: Node x=R.front(); jacint@537: R.pop(); jacint@537: jacint@537: OutEdgeIt e; jacint@537: for( G.first(e,x); G.valid(e); G.next(e) ) { jacint@537: Node y=G.bNode(e); jacint@537: jacint@537: if ( position[y] == D && blossom.find(x) != blossom.find(y) ) { jacint@537: //x and y must be in the same tree jacint@537: jacint@537: typename Graph::template NodeMap path(G,false); jacint@537: jacint@537: Node b=blossom.find(x); jacint@537: path.set(b,true); jacint@537: b=mate[b]; jacint@537: while ( b!=INVALID ) { jacint@537: b=blossom.find(ear[b]); jacint@537: path.set(b,true); jacint@537: b=mate[b]; jacint@537: } //going till the root jacint@537: jacint@537: Node top=y; jacint@537: Node middle=blossom.find(top); jacint@537: Node bottom=x; jacint@537: while ( !path[middle] ) jacint@537: shrinkStep(top, middle, bottom, ear, blossom, tree, Q); jacint@537: jacint@537: Node base=middle; jacint@537: top=x; jacint@537: middle=blossom.find(top); jacint@537: bottom=y; jacint@537: Node blossom_base=blossom.find(base); jacint@537: while ( middle!=blossom_base ) jacint@537: shrinkStep(top, middle, bottom, ear, blossom, tree, Q); jacint@537: jacint@537: blossom.makeRep(base); jacint@537: } // if shrink is needed jacint@537: jacint@537: while ( !Q.empty() ) { jacint@537: Node x=Q.front(); jacint@537: Q.pop(); jacint@537: if ( noShrinkStep(x, ear, blossom, tree, Q) ) return; jacint@537: else R.push(x); jacint@537: } jacint@537: } //for e jacint@537: } // while ( !R.empty() ) jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::normShrink(Node v, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree) { jacint@537: jacint@537: std::queue Q; //queue of the unscanned nodes jacint@537: Q.push(v); jacint@537: while ( !Q.empty() ) { jacint@537: Node x=Q.front(); jacint@537: Q.pop(); jacint@537: jacint@537: OutEdgeIt e; jacint@537: for( G.first(e,x); G.valid(e); G.next(e) ) { jacint@537: Node y=G.bNode(e); jacint@537: jacint@537: switch ( position[y] ) { jacint@537: case D: //x and y must be in the same tree jacint@537: if ( blossom.find(x) != blossom.find(y) ) { //shrink jacint@537: typename Graph::template NodeMap path(G,false); jacint@537: jacint@537: Node b=blossom.find(x); jacint@537: path.set(b,true); jacint@537: b=mate[b]; jacint@537: while ( b!=INVALID ) { jacint@537: b=blossom.find(ear[b]); jacint@537: path.set(b,true); jacint@537: b=mate[b]; jacint@537: } //going till the root jacint@537: jacint@537: Node top=y; jacint@537: Node middle=blossom.find(top); jacint@537: Node bottom=x; jacint@537: while ( !path[middle] ) jacint@537: shrinkStep(top, middle, bottom, ear, blossom, tree, Q); jacint@537: jacint@537: Node base=middle; jacint@537: top=x; jacint@537: middle=blossom.find(top); jacint@537: bottom=y; jacint@537: Node blossom_base=blossom.find(base); jacint@537: while ( middle!=blossom_base ) jacint@537: shrinkStep(top, middle, bottom, ear, blossom, tree, Q); jacint@537: jacint@537: blossom.makeRep(base); jacint@537: } jacint@537: break; jacint@537: case C: jacint@537: if ( mate[y]!=INVALID ) { //grow jacint@537: ear.set(y,x); jacint@537: Node w=mate[y]; jacint@537: blossom.insert(w); jacint@537: position.set(y,A); jacint@537: position.set(w,D); jacint@537: tree.insert(y); jacint@537: tree.insert(w); jacint@537: tree.join(y,blossom.find(x)); jacint@537: tree.join(w,y); jacint@537: Q.push(w); jacint@537: } else { //augment jacint@537: augment(x, ear, blossom, tree); jacint@537: mate.set(x,y); jacint@537: mate.set(y,x); jacint@537: return; jacint@537: } //if jacint@537: break; jacint@537: default: break; jacint@537: } jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::greedyMatching() { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v) ) jacint@537: if ( mate[v]==INVALID ) { jacint@537: OutEdgeIt e; jacint@537: for( G.first(e,v); G.valid(e); G.next(e) ) { jacint@537: Node y=G.bNode(e); jacint@537: if ( mate[y]==INVALID && y!=v ) { jacint@537: mate.set(v,y); jacint@537: mate.set(y,v); jacint@537: break; jacint@537: } jacint@537: } jacint@537: } jacint@537: } jacint@537: jacint@537: template jacint@582: int MaxMatching::size() const { jacint@537: int s=0; jacint@537: NodeIt v; jacint@537: for(G.first(v); G.valid(v); G.next(v) ) { jacint@537: if ( G.valid(mate[v]) ) { jacint@537: ++s; jacint@537: } jacint@537: } jacint@537: return (int)s/2; jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::resetPos() { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) jacint@537: position.set(v,C); jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::resetMatching() { jacint@537: NodeIt v; jacint@537: for( G.first(v); G.valid(v); G.next(v)) jacint@537: mate.set(v,INVALID); jacint@537: } jacint@537: jacint@537: template jacint@537: bool MaxMatching::noShrinkStep(Node x, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree, std::queue& Q) { jacint@537: OutEdgeIt e; jacint@537: for( G.first(e,x); G.valid(e); G.next(e) ) { jacint@537: Node y=G.bNode(e); jacint@537: jacint@537: if ( position[y]==C ) { jacint@537: if ( mate[y]!=INVALID ) { //grow jacint@537: ear.set(y,x); jacint@537: Node w=mate[y]; jacint@537: blossom.insert(w); jacint@537: position.set(y,A); jacint@537: position.set(w,D); jacint@537: tree.insert(y); jacint@537: tree.insert(w); jacint@537: tree.join(y,blossom.find(x)); jacint@537: tree.join(w,y); jacint@537: Q.push(w); jacint@537: } else { //augment jacint@537: augment(x, ear, blossom, tree); jacint@537: mate.set(x,y); jacint@537: mate.set(y,x); jacint@537: return true; jacint@537: } jacint@537: } jacint@537: } jacint@537: return false; jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree, std::queue& Q) { jacint@537: ear.set(top,bottom); jacint@537: Node t=top; jacint@537: while ( t!=middle ) { jacint@537: Node u=mate[t]; jacint@537: t=ear[u]; jacint@537: ear.set(t,u); jacint@537: } jacint@537: bottom=mate[middle]; jacint@537: position.set(bottom,D); jacint@537: Q.push(bottom); jacint@537: top=ear[bottom]; jacint@537: Node oldmiddle=middle; jacint@537: middle=blossom.find(top); jacint@537: tree.erase(bottom); jacint@537: tree.erase(oldmiddle); jacint@537: blossom.insert(bottom); jacint@537: blossom.join(bottom, oldmiddle); jacint@537: blossom.join(top, oldmiddle); jacint@537: } jacint@537: jacint@537: template jacint@537: void MaxMatching::augment(Node x, typename Graph::NodeMap& ear, jacint@537: UFE& blossom, UFE& tree) { jacint@537: Node v=mate[x]; jacint@537: while ( G.valid(v) ) { jacint@537: jacint@537: Node u=ear[v]; jacint@537: mate.set(v,u); jacint@537: Node tmp=v; jacint@537: v=mate[u]; jacint@537: mate.set(u,tmp); jacint@537: } jacint@537: typename UFE::ItemIt it; jacint@537: for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) { jacint@537: if ( position[it] == D ) { jacint@537: typename UFE::ItemIt b_it; jacint@537: for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) { jacint@537: position.set( b_it ,C); jacint@537: } jacint@537: blossom.eraseClass(it); jacint@537: } else position.set( it ,C); jacint@537: } jacint@537: tree.eraseClass(x); jacint@537: } jacint@537: jacint@537: jacint@537: jacint@537: /// @} jacint@537: jacint@537: } //END OF NAMESPACE HUGO jacint@537: jacint@537: #endif //EDMONDS_H