src/lemon/max_matching.h
changeset 1165 c5e56125959a
parent 1164 80bb73097736
child 1166 db3d437560f3
equal deleted inserted replaced
4:0aded56825bd 5:4c84c7433571
    52   ///\param Graph The undirected graph type the algorithm runs on.
    52   ///\param Graph The undirected graph type the algorithm runs on.
    53   ///
    53   ///
    54   ///\author Jacint Szabo  
    54   ///\author Jacint Szabo  
    55   template <typename Graph>
    55   template <typename Graph>
    56   class MaxMatching {
    56   class MaxMatching {
       
    57 
       
    58   protected:
       
    59 
    57     typedef typename Graph::Node Node;
    60     typedef typename Graph::Node Node;
    58     typedef typename Graph::Edge Edge;
    61     typedef typename Graph::Edge Edge;
    59     typedef typename Graph::UndirEdgeIt UndirEdgeIt;
    62     typedef typename Graph::UndirEdgeIt UndirEdgeIt;
    60     typedef typename Graph::NodeIt NodeIt;
    63     typedef typename Graph::NodeIt NodeIt;
    61     typedef typename Graph::IncEdgeIt IncEdgeIt;
    64     typedef typename Graph::IncEdgeIt IncEdgeIt;
    75       D=0,
    78       D=0,
    76       A=1,
    79       A=1,
    77       C=2
    80       C=2
    78     }; 
    81     }; 
    79 
    82 
    80   private:
    83   protected:
    81 
    84 
    82     static const int HEUR_density=2;
    85     static const int HEUR_density=2;
    83     const Graph& g;
    86     const Graph& g;
    84     typename Graph::template NodeMap<Node> _mate;
    87     typename Graph::template NodeMap<Node> _mate;
    85     typename Graph::template NodeMap<pos_enum> position;
    88     typename Graph::template NodeMap<pos_enum> position;
   126     ///Returns INVALID if the \c node is not covered by the actual matching. 
   129     ///Returns INVALID if the \c node is not covered by the actual matching. 
   127     Node mate(Node& node) const {
   130     Node mate(Node& node) const {
   128       return _mate[node];
   131       return _mate[node];
   129     } 
   132     } 
   130 
   133 
   131     ///Reads a matching from a \c Node map of \c Nodes.
   134     ///Reads a matching from a \c Node valued \c Node map.
   132 
   135 
   133     ///Reads a matching from a \c Node map of \c Nodes. This map must be \e
   136     ///Reads a matching from a \c Node valued \c Node map. This map
   134     ///symmetric, i.e. if \c map[u]==v then \c map[v]==u must hold, and
   137     ///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
   135     ///\c uv will be an edge of the matching.
   138     ///must hold, and \c uv will be an edge of the matching.
   136     template<typename NMapN>
   139     template<typename NMapN>
   137     void readNMapNode(NMapN& map) {
   140     void readNMapNode(NMapN& map) {
   138       for(NodeIt v(g); v!=INVALID; ++v) {
   141       for(NodeIt v(g); v!=INVALID; ++v) {
   139 	_mate.set(v,map[v]);   
   142 	_mate.set(v,map[v]);   
   140       } 
   143       } 
   141     } 
   144     } 
   142     
   145     
   143     ///Writes the stored matching to a \c Node map of \c Nodes.
   146     ///Writes the stored matching to a \c Node valued \c Node map.
   144 
   147 
   145     ///Writes the stored matching to a \c Node map of \c Nodes. The
   148     ///Writes the stored matching to a \c Node valued \c Node map. The
   146     ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
   149     ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
   147     ///map[v]==u will hold, and now \c uv is an edge of the matching.
   150     ///map[v]==u will hold, and now \c uv is an edge of the matching.
   148     template<typename NMapN>
   151     template<typename NMapN>
   149     void writeNMapNode (NMapN& map) const {
   152     void writeNMapNode (NMapN& map) const {
   150       for(NodeIt v(g); v!=INVALID; ++v) {
   153       for(NodeIt v(g); v!=INVALID; ++v) {
   151 	map.set(v,_mate[v]);   
   154 	map.set(v,_mate[v]);   
   152       } 
   155       } 
   153     } 
   156     } 
   154 
   157 
   155     ///Reads a matching from a \c Node map of \c Edges.
   158     ///Reads a matching from an \c UndirEdge valued \c Node map.
   156 
   159 
   157     ///Reads a matching from a \c Node map of incident \c Edges. This
   160     ///Reads a matching from an \c UndirEdge valued \c Node map. \c
   158     ///map must have the property that if \c G.target(map[u])==v then \c
   161     ///map[v] must be an \c UndirEdge incident to \c v. This map must
   159     ///G.target(map[v])==u must hold, and now this edge is an edge of
   162     ///have the property that if \c g.oppositeNode(u,map[u])==v then
   160     ///the matching.
   163     ///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
       
   164     ///joining \c u to \v will be an edge of the matching.
   161     template<typename NMapE>
   165     template<typename NMapE>
   162     void readNMapEdge(NMapE& map) {
   166     void readNMapEdge(NMapE& map) {
   163      for(NodeIt v(g); v!=INVALID; ++v) {
   167      for(NodeIt v(g); v!=INVALID; ++v) {
   164 	Edge e=map[v];
   168 	UndirEdge e=map[v];
   165 	if ( g.valid(e) )
   169 	if ( e!=INVALID )
   166 	  g.source(e) == v ? _mate.set(v,g.target(e)) : _mate.set(v,g.source(e)); 
   170 	  g.source(e) == v ? _mate.set(v,g.target(e)) : _mate.set(v,g.source(e)); 
   167       } 
   171       } 
   168     } 
   172     } 
   169     
   173     
   170     ///Writes the matching stored to a \c Node map of \c Edges.
   174     ///Writes the matching stored to an \c UndirEdge valued \c Node map.
   171 
   175 
   172     ///Writes the stored matching to a \c Node map of incident \c
   176     ///Writes the stored matching to an \c UndirEdge valued \c Node
   173     ///Edges. This map will have the property that if \c
   177     ///map. \c map[v] will be an \c UndirEdge incident to \c v. This
   174     ///g.target(map[u])==v then \c g.target(map[v])==u holds, and now this
   178     ///map will have the property that if \c g.oppositeNode(u,map[u])
   175     ///edge is an edge of the matching.
   179     ///== v then \c map[u]==map[v] holds, and now this edge is an edge
       
   180     ///of the matching.
   176     template<typename NMapE>
   181     template<typename NMapE>
   177     void writeNMapEdge (NMapE& map)  const {
   182     void writeNMapEdge (NMapE& map)  const {
   178       typename Graph::template NodeMap<bool> todo(g,true); 
   183       typename Graph::template NodeMap<bool> todo(g,true); 
   179       for(NodeIt v(g); v!=INVALID; ++v) {
   184       for(NodeIt v(g); v!=INVALID; ++v) {
   180 	if ( todo[v] && _mate[v]!=INVALID ) {
   185 	if ( todo[v] && _mate[v]!=INVALID ) {
   191 	}
   196 	}
   192       } 
   197       } 
   193     }
   198     }
   194 
   199 
   195 
   200 
   196     ///Reads a matching from an \c Edge map of \c bools.
   201     ///Reads a matching from a \c bool valued \c Edge map.
   197     
   202     
   198     ///Reads a matching from an \c Edge map of \c bools. This map must
   203     ///Reads a matching from a \c bool valued \c Edge map. This map
   199     ///have the property that there are no two adjacent edges \c e, \c
   204     ///must have the property that there are no two incident edges \c
   200     ///f with \c map[e]==map[f]==true. The edges \c e with \c
   205     ///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
   201     ///map[e]==true form the matching.
   206     ///map[e]==true form the matching.
   202     template<typename EMapB>
   207     template<typename EMapB>
   203     void readEMapBool(EMapB& map) {
   208     void readEMapBool(EMapB& map) {
   204       for(UndirEdgeIt e(g); e!=INVALID; ++e) {
   209       for(UndirEdgeIt e(g); e!=INVALID; ++e) {
   205 	if ( map[e] ) {
   210 	if ( map[e] ) {
   210 	} 
   215 	} 
   211       } 
   216       } 
   212     }
   217     }
   213 
   218 
   214 
   219 
   215     ///Writes the matching stored to an \c Edge map of \c bools.
   220     ///Writes the matching stored to a \c bool valued \c Edge map.
   216 
   221 
   217     ///Writes the matching stored to an \c Edge map of \c bools. This
   222     ///Writes the matching stored to a \c bool valued \c Edge
   218     ///map will have the property that there are no two adjacent edges
   223     ///map. This map will have the property that there are no two
   219     ///\c e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
   224     ///incident edges \c e, \c f with \c map[e]==map[f]==true. The
   220     ///map[e]==true form the matching.
   225     ///edges \c e with \c map[e]==true form the matching.
   221     template<typename EMapB>
   226     template<typename EMapB>
   222     void writeEMapBool (EMapB& map) const {
   227     void writeEMapBool (EMapB& map) const {
   223       for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
   228       for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
   224 
   229 
   225       typename Graph::template NodeMap<bool> todo(g,true); 
   230       typename Graph::template NodeMap<bool> todo(g,true); 
   250       for(NodeIt v(g); v!=INVALID; ++v)  map.set(v,position[v]);
   255       for(NodeIt v(g); v!=INVALID; ++v)  map.set(v,position[v]);
   251     }
   256     }
   252 
   257 
   253   private: 
   258   private: 
   254 
   259 
       
   260  
   255     void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
   261     void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,  
   256 		    UFE& blossom, UFE& tree);
   262 		    UFE& blossom, UFE& tree);
   257 
   263 
   258     void normShrink(Node v, typename Graph::NodeMap<Node>& ear,  
   264     void normShrink(Node v, typename Graph::NodeMap<Node>& ear,  
   259 		    UFE& blossom, UFE& tree);
   265 		    UFE& blossom, UFE& tree);
   290     for(NodeIt v(g); v!=INVALID; ++v)
   296     for(NodeIt v(g); v!=INVALID; ++v)
   291       position.set(v,C);      
   297       position.set(v,C);      
   292 
   298 
   293     typename Graph::template NodeMap<Node> ear(g,INVALID); 
   299     typename Graph::template NodeMap<Node> ear(g,INVALID); 
   294     //undefined for the base nodes of the blossoms (i.e. for the
   300     //undefined for the base nodes of the blossoms (i.e. for the
   295     //representative elements of UFE blossom) and for the nodes in C
   301     //representative elements of UFE blossom) and for the nodes in C 
   296  
   302 
   297     typename UFE::MapType blossom_base(g);
   303     typename UFE::MapType blossom_base(g);
   298     UFE blossom(blossom_base);
   304     UFE blossom(blossom_base);
   299     typename UFE::MapType tree_base(g);
   305     typename UFE::MapType tree_base(g);
   300     UFE tree(tree_base);
   306     UFE tree(tree_base);
       
   307     //If these UFE's would be members of the class then also
       
   308     //blossom_base and tree_base should be a member.
   301 
   309 
   302     for(NodeIt v(g); v!=INVALID; ++v) {
   310     for(NodeIt v(g); v!=INVALID; ++v) {
   303       if ( position[v]==C && _mate[v]==INVALID ) {
   311       if ( position[v]==C && _mate[v]==INVALID ) {
   304 	blossom.insert(v);
   312 	blossom.insert(v);
   305 	tree.insert(v); 
   313 	tree.insert(v); 
   469     for(NodeIt v(g); v!=INVALID; ++v) {
   477     for(NodeIt v(g); v!=INVALID; ++v) {
   470       if ( _mate[v]!=INVALID ) {
   478       if ( _mate[v]!=INVALID ) {
   471 	++s;
   479 	++s;
   472       }
   480       }
   473     }
   481     }
   474     return (int)s/2;
   482     return s/2;
   475   }
   483   }
   476 
   484 
   477   template <typename Graph>
   485   template <typename Graph>
   478   void MaxMatching<Graph>::resetMatching() {
   486   void MaxMatching<Graph>::resetMatching() {
   479     for(NodeIt v(g); v!=INVALID; ++v)
   487     for(NodeIt v(g); v!=INVALID; ++v)
   560 
   568 
   561   /// @}
   569   /// @}
   562   
   570   
   563 } //END OF NAMESPACE LEMON
   571 } //END OF NAMESPACE LEMON
   564 
   572 
   565 #endif //EDMONDS_H
   573 #endif //LEMON_MAX_MATCHING_H