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