diff -r 1a8630f2e944 -r 8f1c317ebeb4 lemon/max_matching.h --- a/lemon/max_matching.h Mon Jul 25 11:17:23 2005 +0000 +++ b/lemon/max_matching.h Tue Jul 26 13:15:13 2005 +0000 @@ -97,32 +97,88 @@ ///Runs Edmonds' algorithm for sparse graphs (number of edges < ///2*number of nodes), and a heuristical Edmonds' algorithm with a ///heuristic of postponing shrinks for dense graphs. - inline void run(); + void run() { + if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) { + greedyMatching(); + runEdmonds(0); + } else runEdmonds(1); + } + ///Runs Edmonds' algorithm. ///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs ///Edmonds' algorithm with a heuristic of postponing shrinks, ///giving a faster algorithm for dense graphs. - void runEdmonds( int heur ); + void runEdmonds( int heur = 1 ) { + + for(NodeIt v(g); v!=INVALID; ++v) + position.set(v,C); + + typename Graph::template NodeMap ear(g,INVALID); + //undefined for the base nodes of the blossoms (i.e. for the + //representative elements of UFE blossom) and for the nodes in C + + typename UFE::MapType blossom_base(g); + UFE blossom(blossom_base); + typename UFE::MapType tree_base(g); + UFE tree(tree_base); + //If these UFE's would be members of the class then also + //blossom_base and tree_base should be a member. + + for(NodeIt v(g); v!=INVALID; ++v) { + if ( position[v]==C && _mate[v]==INVALID ) { + blossom.insert(v); + tree.insert(v); + position.set(v,D); + if ( heur == 1 ) lateShrink( v, ear, blossom, tree ); + else normShrink( v, ear, blossom, tree ); + } + } + } + ///Finds a greedy matching starting from the actual matching. ///Starting form the actual matching stored, it finds a maximal ///greedy matching. - void greedyMatching(); + void greedyMatching() { + for(NodeIt v(g); v!=INVALID; ++v) + if ( _mate[v]==INVALID ) { + for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) { + Node y=g.runningNode(e); + if ( _mate[y]==INVALID && y!=v ) { + _mate.set(v,y); + _mate.set(y,v); + break; + } + } + } + } ///Returns the size of the actual matching stored. ///Returns the size of the actual matching stored. After \ref ///run() it returns the size of a maximum matching in the graph. - int size() const; + int size() const { + int s=0; + for(NodeIt v(g); v!=INVALID; ++v) { + if ( _mate[v]!=INVALID ) { + ++s; + } + } + return s/2; + } + ///Resets the actual matching to the empty matching. ///Resets the actual matching to the empty matching. /// - void resetMatching(); + void resetMatching() { + for(NodeIt v(g); v!=INVALID; ++v) + _mate.set(v,INVALID); + } ///Returns the mate of a node in the actual matching. @@ -284,44 +340,6 @@ template - void MaxMatching::run() { - if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) { - greedyMatching(); - runEdmonds(0); - } else runEdmonds(1); - } - - - template - void MaxMatching::runEdmonds( int heur=1 ) { - - for(NodeIt v(g); v!=INVALID; ++v) - position.set(v,C); - - typename Graph::template NodeMap ear(g,INVALID); - //undefined for the base nodes of the blossoms (i.e. for the - //representative elements of UFE blossom) and for the nodes in C - - typename UFE::MapType blossom_base(g); - UFE blossom(blossom_base); - typename UFE::MapType tree_base(g); - UFE tree(tree_base); - //If these UFE's would be members of the class then also - //blossom_base and tree_base should be a member. - - for(NodeIt v(g); v!=INVALID; ++v) { - if ( position[v]==C && _mate[v]==INVALID ) { - blossom.insert(v); - tree.insert(v); - position.set(v,D); - if ( heur == 1 ) lateShrink( v, ear, blossom, tree ); - else normShrink( v, ear, blossom, tree ); - } - } - } - - - template void MaxMatching::lateShrink(Node v, typename Graph::template NodeMap& ear, UFE& blossom, UFE& tree) { @@ -460,38 +478,6 @@ } template - void MaxMatching::greedyMatching() { - for(NodeIt v(g); v!=INVALID; ++v) - if ( _mate[v]==INVALID ) { - for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) { - Node y=g.runningNode(e); - if ( _mate[y]==INVALID && y!=v ) { - _mate.set(v,y); - _mate.set(y,v); - break; - } - } - } - } - - template - int MaxMatching::size() const { - int s=0; - for(NodeIt v(g); v!=INVALID; ++v) { - if ( _mate[v]!=INVALID ) { - ++s; - } - } - return s/2; - } - - template - void MaxMatching::resetMatching() { - for(NodeIt v(g); v!=INVALID; ++v) - _mate.set(v,INVALID); - } - - template bool MaxMatching::noShrinkStep(Node x, typename Graph::template NodeMap& ear,