o
    3ήc.                     @   s`   d Z ddlZddlZddlZddlmZmZmZm	Z	m
Z
 G dd dZdd ZG dd	 d	ZdS )
zHUnit tests for the :mod:`networkx.algorithms.bipartite.matching` module.    N)eppstein_matchinghopcroft_karp_matchingmaximum_matchingminimum_weight_full_matchingto_vertex_coverc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )TestMatchingz(Tests for bipartite matching algorithms.c                 C   sj  t dd| _ddddd| _g d}ttd| _t  | _| j	td | j
| t  }|	g d	 |d
d |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd || _dS )a&  Creates a bipartite graph for use in testing matching algorithms.

        The bipartite graph has a maximum cardinality matching that leaves
        vertex 1 and vertex 10 unmatched. The first six numbers are the left
        vertices and the next six numbers are the right vertices.

              r      r   r
   r   r	   ))r      )r      )r      )r   	   )r	   r   )   r   )r   r   )      r      )r
   Cr
   Br   Gr
   Fr
   Er   r   r
   Dr
   Ir   A)r   r    )r   r   r   r   )r   Hr
   r   )r
   r$   )r   r"   )r   r   r
   r&   r   r#   r   r   r!   r(   r   r   r%   r   r   r'   N)nxcomplete_bipartite_graphsimple_graphsimple_solutionsetrange	top_nodesGraphgraphadd_nodes_fromadd_edges_fromadd_edgedisconnected_graph)selfedgesr    r8   X/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/bipartite/tests/test_matching.pysetup_method   s@   

zTestMatching.setup_methodc                    sT   | t tj   }|t tdddh ksJ t fddtdD s(J dS )zAsserts that the matching is what we expect from the bipartite graph
        constructed in the :meth:`setup` fixture.

        r   r
   
   c                 3   s(    | ]}| v r|  |  kV  qd S Nr8   ).0uMr8   r9   	<genexpr>_   s   & z+TestMatching.check_match.<locals>.<genexpr>N)	frozenset	itertoolschainitemsr.   all)r6   matchingmatched_verticesr8   r?   r9   check_matchR   s   "zTestMatching.check_matchc                 C   s<   t |dksJ | j D ]\}}||v s||v sJ qdS )zAsserts that the given set of vertices is the vertex cover we
        expected from the bipartite graph constructed in the :meth:`setup`
        fixture.

        r   N)lenr1   r7   )r6   verticesr>   vr8   r8   r9   check_vertex_covera   s
   zTestMatching.check_vertex_coverc                 C      |  t| j| j dS )zTests that David Eppstein's implementation of the Hopcroft--Karp
        algorithm produces a maximum cardinality matching.

        N)rI   r   r1   r/   r6   r8   r8   r9   test_eppstein_matchingo      z#TestMatching.test_eppstein_matchingc                 C   rN   )zwTests that the Hopcroft--Karp algorithm produces a maximum
        cardinality matching in a bipartite graph.

        N)rI   r   r1   r/   rO   r8   r8   r9   test_hopcroft_karp_matchingv   rQ   z(TestMatching.test_hopcroft_karp_matchingc                 C   s,   t | j| j}t| j|| j}| | dS )zATest for converting a maximum matching to a minimum vertex cover.N)r   r1   r/   r   rM   )r6   rG   vertex_coverr8   r8   r9   test_to_vertex_cover}   s   z!TestMatching.test_to_vertex_coverc                 C      t | j}|| jksJ d S r<   )r   r+   r,   r6   matchr8   r8   r9   test_eppstein_matching_simple      
z*TestMatching.test_eppstein_matching_simplec                 C   rU   r<   )r   r+   r,   rV   r8   r8   r9   "test_hopcroft_karp_matching_simple   rY   z/TestMatching.test_hopcroft_karp_matching_simplec                 C   <   t tj t| j}W d    d S 1 sw   Y  d S r<   )pytestraisesr)   AmbiguousSolutionr   r5   rV   r8   r8   r9   #test_eppstein_matching_disconnected      "z0TestMatching.test_eppstein_matching_disconnectedc                 C   r[   r<   )r\   r]   r)   r^   r   r5   rV   r8   r8   r9   (test_hopcroft_karp_matching_disconnected   r`   z5TestMatching.test_hopcroft_karp_matching_disconnectedc           
      C   s  t  }|dd |dd |dd |dd |dd |dd |dd |dd	 t |}t  }| D ]}|d
|f |d|f qA| D ]\}}|d
|fd|f qVdd |D }t||}t	|||}t
|dd |D  }	h d|	ksJ dS )zTest from issue 2127r$   r   r   r   r    r   r   r"   r&   r   r
   c                 S   s   h | ]
}|d  d kr|qS )r   r8   )r=   nr8   r8   r9   	<setcomp>   s    z/TestMatching.test_issue_2127.<locals>.<setcomp>c                 S   s   h | ]\}}|qS r8   r8   )r=   _rL   r8   r8   r9   rc      s    >   r   r    r   r&   r"   N)r)   DiGraphr4   transitive_closurer0   nodesadd_noder7   r   r   r-   )
r6   r   tcbtcrL   r>   r/   rG   rS   independent_setr8   r8   r9   test_issue_2127   s*   

zTestMatching.test_issue_2127c                 C   sJ   t g d}t|}t||}| D ]\}}||v s"||v s"J qd S )N))r   r	   )r
   r	   )r
   r   )r   r	   )r)   r0   r   r   r7   )r6   r   rG   rS   r>   rL   r8   r8   r9   test_vertex_cover_issue_2384   s   
z)TestMatching.test_vertex_cover_issue_2384c                 C   s`   t  }g d}|dd |D  t|}t||}| D ]\}}||v s-||v s-J qd S )N))r   r   )r
   r   )r
   r
   )r
   r   )r   r   c                 S   s    g | ]\}}|d f|dffqS )LRr8   )r=   ijr8   r8   r9   
<listcomp>   s     z=TestMatching.test_vertex_cover_issue_3306.<locals>.<listcomp>)r)   r0   r3   r   r   r7   )r6   r   r7   rG   rS   r>   rL   r8   r8   r9   test_vertex_cover_issue_3306   s   
z)TestMatching.test_vertex_cover_issue_3306c                 C   s|   t  }t  }t  }t  }t  }t||f||f||f||fg}t|}t||}| D ]\}	}
|	|v s;|
|v s;J q-d S r<   )objectr)   r0   r   r   r7   )r6   abcder   rG   rS   r>   rL   r8   r8   r9   test_unorderable_nodes   s   "
z#TestMatching.test_unorderable_nodesN)__name__
__module____qualname____doc__r:   rI   rM   rP   rR   rT   rX   rZ   r_   ra   rl   rm   rs   rz   r8   r8   r8   r9   r      s     ?
r   c                     s   t  } | jg ddd | jg ddd | g d t|  t tt| ks-J t fddt 	 D s>J d	S )
z!Test in accordance to issue #1927)ru   r   r	   r   r   	bipartite)r
   rv   rw   r
   ))ru   r
   )ru   rv   )r   rv   )r   rw   )r	   rw   )r   r
   c                 3   s     | ]}|t   v V  qd S r<   )r-   keys)r=   xrG   r8   r9   rA      s    z)test_eppstein_matching.<locals>.<genexpr>N)
r)   r0   r2   r3   r   rJ   r   rF   r-   values)r   r8   r   r9   rP      s   &rP   c                   @   sX   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdS )TestMinimumWeightFullMatchingc                 C   s   t d d S )Nscipy)r\   importorskip)clsr8   r8   r9   setup_class   s   z)TestMinimumWeightFullMatching.setup_classc                 C   s~   t  }|jddgdd |jddgdd |jdddd |jdddd |jddd	d t|}|ddddd
ks=J d S )Nr
   r   r   r   r	   r   d   weight2   )r
   r   r   r	   )r)   r0   r2   r4   r   )r6   r   rG   r8   r8   r9   2test_minimum_weight_full_matching_incomplete_graph   s   zPTestMinimumWeightFullMatching.test_minimum_weight_full_matching_incomplete_graphc                 C   s   t  }|jg ddd |jg ddd |jdddd |jd	ddd |jd
ddd |jd
ddd |jd
ddd tt t| W d    d S 1 sSw   Y  d S )N)r
   r   r	   r   r   )r   r   r   r
   r   r   r   r   r	   r   r   r   )r)   r0   r2   r4   r\   r]   
ValueErrorr   )r6   r   r8   r8   r9   7test_minimum_weight_full_matching_with_no_full_matching   s   
"zUTestMinimumWeightFullMatching.test_minimum_weight_full_matching_with_no_full_matchingc                 C   s   t dd}|jdddd |jdddd |jdddd |jdddd |jddd	d |jddd
d |jdddd |jdddd |jdddd t|}|dddddddks_J d S )Nr	   r     r   r      r   r
     X  r   ,     )r   r
   r   r   r	   r   r)   r*   r4   r   r6   r   rG   r8   r8   r9   (test_minimum_weight_full_matching_square   s   zFTestMinimumWeightFullMatching.test_minimum_weight_full_matching_squarec                 C   s   t dd}|jdddd |jdddd |jdddd |jddd	d |jd	ddd |jd	dd
d |jd	ddd |jd	ddd |jdddd |jdddd |jdddd |jdddd t|}|dddddd	dkswJ d S )Nr	   r   r   r   r   r   r   r   r
   r   r   r   r   r   "  r   r
   r   r   r   r   r   r   r8   r8   r9   .test_minimum_weight_full_matching_smaller_left     zLTestMinimumWeightFullMatching.test_minimum_weight_full_matching_smaller_leftc                 C   s   t dd}|jdddd |jdddd |jdddd |jddd	d |jd	ddd |jd	dd
d |jd	ddd |jd	ddd |jdddd |jdddd |jdddd |jdddd t|g dd}|dddddd	dks{J d S )Nr	   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   )r	   r   r   r   )r/   r   r   r   r8   r8   r9   9test_minimum_weight_full_matching_smaller_top_nodes_right  s   zWTestMinimumWeightFullMatching.test_minimum_weight_full_matching_smaller_top_nodes_rightc                 C   s   t dd}|jdddd |jdddd |jdddd |jd	dd
d |jd	ddd |jd	ddd |jdddd |jdddd |jdddd |jddd	d |jdddd |jdddd t|}|dddd	dddkswJ d S )Nr   r	   r   r   r   r   r   r   r
   r   r   r   r   r   r   )r
   r   r	   r   r   r   r   r   r8   r8   r9   /test_minimum_weight_full_matching_smaller_right%  r   zMTestMinimumWeightFullMatching.test_minimum_weight_full_matching_smaller_rightc                 C   sn   t dd}|jdddd |jdddd |jdddd |jdddd t|}|ddddd	ks5J d S )
Nr   r   r   r	   皙?r
   g333333?r   r   r   r8   r8   r9   2test_minimum_weight_full_matching_negative_weights6  s   zPTestMinimumWeightFullMatching.test_minimum_weight_full_matching_negative_weightsc                 C   sr   t dd}|jdddd |jdddd |jdddd |jdddd t|dd}|ddddd	ks7J d S )
Nr   r   )massr	   r   r
   r   r   r   r   r   r8   r8   r9   6test_minimum_weight_full_matching_different_weight_key?  s   zTTestMinimumWeightFullMatching.test_minimum_weight_full_matching_different_weight_keyN)r{   r|   r}   classmethodr   r   r   r   r   r   r   r   r   r8   r8   r8   r9   r      s    

	r   )r~   rC   r\   networkxr)   &networkx.algorithms.bipartite.matchingr   r   r   r   r   r   rP   r   r8   r8   r8   r9   <module>   s    	 @