U
    ?hN                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
 G dd dZG dd	 d	ZG d
d dZG dd dZG dd dZdS )    N)permutations)raises)matching_dict_to_set)edges_equalc                   @   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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 )/TestMaxWeightMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.max_weight_matching` function.

    c                 C   s4   t  }t |t kstt |t ks0tdS )zEmpty graphN)nxGraphmax_weight_matchingsetAssertionErrormin_weight_matchingselfG r   Y/var/www/html/venv/lib/python3.8/site-packages/networkx/algorithms/tests/test_matching.pytest_trivial1   s    z#TestMaxWeightMatching.test_trivial1c                 C   sD   t  }|jdddd t |t ks,tt |t ks@td S )Nr   d   weight)r   r   add_edger	   r
   r   r   r   r   r   r   test_selfloop   s    z#TestMaxWeightMatching.test_selfloopc                 C   sT   t  }|dd tt |tddds2ttt |tdddsPtd S )Nr      r   r   r   r   r   r   r	   r   r   r   r   r   r   r   test_single_edge   s      z&TestMaxWeightMatching.test_single_edgec                 C   sh   t  }|jdddd |jdddd tt |tdddsFttt |tdddsdtd S )	Nonetwo
   r   three   )r   r   r   r   r   r   r   r   r   test_two_path'   s    z#TestMaxWeightMatching.test_two_pathc                 C   s   t  }|jdddd |jdddd |jdddd tt |tdddsVttt |dtddddd	szttt |tddd
sttt |dtddd
std S )Nr         r      r       r#   r%   r   r#   r%   r&   r   r%   r   r   r   r   r   	test_path4   s(     
  
 zTestMaxWeightMatching.test_pathc                 C   s   t  }|jdddd |jdddd |jdddd |jdddd tt |tdddsfttt |tdddstd S )Nr   r&   r#   r   r%   r)   r   r#   r   r   r   r   r   test_squareF   s      z!TestMaxWeightMatching.test_squarec                 C   sr   t  }|jddddd |jddddd tt j|ddtddd	sNttt j|ddtddisntd S )
Nr   r   r   r    )r   abcdr   r-   r   r!   r   r   r   r   r   test_edge_attribute_nameS   s    
z.TestMaxWeightMatching.test_edge_attribute_namec                 C   s   t  }|jddtjd |jddtdd |jdddd |jddtdd tt |t	dddddsxt
tt |t	dddddst
d S )	Nr   r#   r   r%   g      @r&   g       @r(   )r   r   r   mathpiexpsqrtr   r	   r   r   r   r   r   r   r   test_floating_point_weights`   s      z1TestMaxWeightMatching.test_floating_point_weightsc                 C   s   t  }|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ddd	svttt j|d
dtdddddsttt |tdddstd S )Nr   r#   r   r%   r&   ir+   T)Zmaxcardinalityr(   r)   r   r   r   r   r   test_negative_weightsm   s$      z+TestMaxWeightMatching.test_negative_weightsc                 C   s   t  }|ddddg tddddd	}tt ||s@ttt ||sTt|d
dg tddddddd}tt ||sttt ||stdS )z-Create S-blossom and use it for augmentation:r   r#      r   r%   	   r#   r%   r   )r%   r&      r#   r   r&   r%   r(   )r      r$   )r&   r$   r=   r=   r$   r   r#   r%   r&   r$   r=   Nr   r   add_weighted_edges_fromr   r   r	   r   r   r   r   answerr   r   r   test_s_blossom   s    z$TestMaxWeightMatching.test_s_blossomc                 C   s  t  }|ddddddg tddd	d
ddd}tt ||sHttt ||s\t|jdd
dd |jdddd tt ||sttt ||st|	dd |jdddd td	ddd
ddd}tt ||sttt ||stdS )z=Create S-blossom, relabel as T-blossom, use for augmentation:r   r#   r:   r   r%   r8   r;   )r   r&   r$   )r&   r$   r&   )r   r=   r%   r=   r%   r#   r$   r&   r   r>   r   N)
r   r   r@   r   r   r	   r   r   r   Zremove_edgerA   r   r   r   test_s_t_blossom   s     z&TestMaxWeightMatching.test_s_t_blossomc              	   C   s   t  }|dddddddg dd	d
dddd}dd t|D }dd t |D }||ksdtdd t |D }||kstdS )z.Create nested S-blossom, use for augmentation:rD   r9   r;   )r#   r&   r8   )r%   r$   r8   )r&   r$   r   )r$   r=   r=   r%   r&   r   r#   r=   r$   r>   c                 S   s   h | ]}t |qS r   	frozenset.0er   r   r   	<setcomp>   s     z>TestMaxWeightMatching.test_nested_s_blossom.<locals>.<setcomp>c                 S   s   h | ]}t |qS r   rG   rI   r   r   r   rL      s     c                 S   s   h | ]}t |qS r   rG   rI   r   r   r   rL      s     N)r   r   r@   r   r	   r   r   )r   r   Zdict_formatexpectedrB   r   r   r   test_nested_s_blossom   s"    z+TestMaxWeightMatching.test_nested_s_blossomc                 C   sj   t  }|ddddddddd	g	 td
dddddddd}tt ||sRttt ||sftdS )z<Create S-blossom, relabel as S, include in nested S-blossom:)r   r#   r   )r   r<   r   )r#   r%      )r%   r&      )r%   r$   rP   r&   r$      )r$   r=   r   )r=   r<   r   )r<   r8   r8   r#   r   r&   r%   r=   r$   r8   r<   r   r#   r%   r&   r$   r=   r<   r8   Nr?   rA   r   r   r   test_nested_s_blossom_relabel   s     z3TestMaxWeightMatching.test_nested_s_blossom_relabelc                 C   sl   t  }|ddddddddd	d
g
 tddddddddd}tt ||sTttt ||shtdS )z5Create nested S-blossom, augment, expand recursively:r7   rE   r;   )r#   r&   rO   )r%   r$   rO   )r&   r$      )r&   r=   rO   )r$   r<   rO   )r=   r<   rU   )r<   r8   rO   r#   r   r$   r=   r%   r&   r8   r<   rS   Nr?   rA   r   r   r   test_nested_s_blossom_expand   s"    z2TestMaxWeightMatching.test_nested_s_blossom_expandc              
   C   sh   t  }|ddddddddg td	d
ddddddd}tt ||sPttt ||sdtdS )z'Create S-blossom, relabel as T, expand:)r   r#      )r   r$      )r   r=      r#   r%   rR   )r%   r&   rX   rQ   )r&   r8   rU   )r$   r<      r=   r%   r#   r8   r<   r   r$   r&   rS   Nr?   rA   r   r   r   test_s_blossom_relabel_expand   s    z3TestMaxWeightMatching.test_s_blossom_relabel_expandc                 C   sj   t  }|ddddddddd	g	 td
dddddddd}tt ||sRttt ||sftdS )z.Create nested S-blossom, relabel as T, expand:)r   r#      )r   r%   rP   )r   r8   r8   rZ   )r#   r&      )r%   r$   r^   )r&   r$   r[   )r&   r<   r<   )r$   r=   r<   r8   r%   r#   r<   r=   r$   r&   r   rS   Nr?   rA   r   r   r   $test_nested_s_blossom_relabel_expand   s     z:TestMaxWeightMatching.test_nested_s_blossom_relabel_expandc                 C   st   t  }|ddddddddd	d
g
 ddddddddddd
}t|}tt ||s\ttt ||sptdS )zTCreate blossom, relabel as T in more than one way, expand,
        augment:
        r   r#   -   r   r$   ra   r#   r%   2   r%   r&   ra   r&   r$   rd   r   r=      r%   r:   #   )r&   r8   rj   r$   r<      r:   r   r$   r=   r%   r#   r8   r<   r   r$   r&   r   r:   
r   r#   r%   r&   r$   r=   r<   r8   r:   r   Nr?   )r   r   ZansdictrB   r   r   r   test_nasty_blossom1  s$    z)TestMaxWeightMatching.test_nasty_blossom1c                 C   st   t  }|ddddddddd	d
g
 ddddddddddd
}t|}tt ||s\ttt ||sptdS )zAgain but slightly different:r`   rb   rc   re   rf   rg   ri   )r&   r8   rl   )r$   r<   (   rm   r=   r%   r#   r8   r<   r   r$   r&   r   r:   rn   Nr?   r   r   ZansrB   r   r   r   test_nasty_blossom2  s$    z)TestMaxWeightMatching.test_nasty_blossom2c                 C   st   t  }|ddddddddd	d
g
 ddddddddddd
}t|}tt ||s\ttt ||sptdS )zuCreate blossom, relabel as T, expand such that a new
        least-slack S-to-free dge is produced, augment:
        r`   rb   rc   re   rf   rg   ri   )r&   r8      rk   rm   r=   r%   r#   r8   r<   r   r$   r&   r   r:   rn   Nr?   rq   r   r   r   test_nasty_blossom_least_slack5  s$    z4TestMaxWeightMatching.test_nasty_blossom_least_slackc                 C   s~   t  }|ddddddddd	d
dddg ddddddddddddd}t|}tt ||sfttt ||sztdS )z8Create nested blossom, relabel as T in more than one wayr`   )r   r<   ra   rc   re   )r&   r$   _   )r&   r=   ^   )r$   r=   rv   )r=   r<   rd   )r   r8   rh   )r%   r    rj   )r$   r:   $   )r<   r   rl   )r    rO   r$   r8   r%   r#   r=   r:   r&   r   r   r$   r<   rO   r    )r   r#   r%   r&   r$   r=   r<   r8   r:   r   r    rO   Nr?   rq   r   r   r   test_nasty_blossom_augmentingM  sB    z3TestMaxWeightMatching.test_nasty_blossom_augmentingc                 C   sv   t  }|ddddddddd	d
dg ddddddddddd
}t|}tt ||s^ttt ||srtdS )z:Create nested S-blossom, relabel as S, expand recursively:)r   r#   rp   )r   r%   rp   )r#   r%   <   )r#   r&   7   )r%   r$   rz   rf   )r   r8   rY   )r$   r<   rh   )r<   r=   r   )r8   r   r   )r&   r:   rh   r#   r   r$   r:   r%   r<   r=   r   r&   r8   rn   Nr?   rq   r   r   r   %test_nasty_blossom_expand_recursivelyu  s&    z;TestMaxWeightMatching.test_nasty_blossom_expand_recursivelyc                 C   sR   t j}t|t jt   t|t jt   t|t jt   t|t jt   d S N)r   NetworkXNotImplementedr   r	   
MultiGraphMultiDiGraphDiGraphr   r   errorr   r   r   test_wrong_graph_type  s
    z+TestMaxWeightMatching.test_wrong_graph_typeN)__name__
__module____qualname____doc__r   r   r   r"   r*   r,   r.   r3   r6   rC   rF   rN   rT   rV   r\   r_   ro   rr   rt   rx   r{   r   r   r   r   r   r      s.   
(r   c                   @   sX   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S )TestIsMatchingzXUnit tests for the
    :func:`~networkx.algorithms.matching.is_matching` function.

    c                 C   s(   t d}t |ddddds$td S Nr&   r   r   r%   r#   )r   r   r#   r%   r   
path_graphis_matchingr   r   r   r   r   	test_dict  s    
zTestIsMatching.test_dictc                 C   s    t d}t |t std S )Nr&   )r   r   r   r
   r   r   r   r   r   test_empty_matching  s    
z"TestIsMatching.test_empty_matchingc                 C   s    t d}t |dhstd S )Nr&   r+   r   r   r   r   r   r     s    
zTestIsMatching.test_single_edgec                 C   s^   t d}t |ddhstt |ddhs2tt |ddhsFtt |ddhsZtd S )Nr&   r   r'   r   r   )r%   r#   r   r   r   r   r   test_edge_order  s
    
zTestIsMatching.test_edge_orderc                 C   s"   t d}t |ddhstd S Nr&   r   r'   r   r   r   r   r   test_valid_matching  s    
z"TestIsMatching.test_valid_matchingc                 C   sP   t j}t d}t|t j|ddh t|t j|ddh t|t j|ddh d S )Nr&   r   r$   r'   r   r   r#   r   )r   NetworkXErrorr   r   r   r   r   r   r   r   r   test_invalid_input  s
    
z!TestIsMatching.test_invalid_inputc                 C   s^   t j}t d}t|t j|ddh t |dddhr:t|dd t |ddhrZtd S Nr&   )r$   r$   r'   r   r   r+   r   )r   r   r   r   r   r   r   r   r   r   r   test_selfloops  s    
zTestIsMatching.test_selfloopsc                 C   s$   t d}t |dddhr td S )Nr&   r   r+   r'   r   r   r   r   r   test_invalid_matching  s    
z$TestIsMatching.test_invalid_matchingc                 C   sf   t d}t |ddhrttt jt j|dh t |j}t |dhsPtt |dhrbtd S )Nr&   r   r%   r+   )r   rz   r   r   )r   r   r   r   r   r   r   edgesr   r   r   r   test_invalid_edge  s    
z TestIsMatching.test_invalid_edgeN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TestIsMaximalMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.is_maximal_matching` function.

    c                 C   s(   t d}t |ddddds$td S r   r   r   is_maximal_matchingr   r   r   r   r   r     s    
zTestIsMaximalMatching.test_dictc                 C   s`   t j}t d}t|t j|dh t|t j|dh t|t j|ddh t|t j|ddh d S Nr&   r   )r$   r   r   r'   r   )r   r   r   r   r   r   r   r   r   r     s    
z(TestIsMaximalMatching.test_invalid_inputc                 C   s"   t d}t |ddhstd S r   r   r   r   r   r   
test_valid  s    
z TestIsMaximalMatching.test_validc                 C   sT   t d}t |dddhr tt |dhr2t|dd t |dhrPtd S )Nr&   r   r+   r'   r   r   r   )r   r   r   r   r   r   r   r   r   test_not_matching  s
    
z'TestIsMaximalMatching.test_not_matchingc                 C   s    t d}t |dhrtd S )Nr&   r   r   r   r   r   r   test_not_maximal  s    
z&TestIsMaximalMatching.test_not_maximalN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c                   @   sH   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S )TestIsPerfectMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.is_perfect_matching` function.

    c                 C   s(   t d}t |ddddds$td S r   r   r   is_perfect_matchingr   r   r   r   r   r     s    
zTestIsPerfectMatching.test_dictc                 C   s"   t d}t |ddhstd S r   r   r   r   r   r   r     s    
z TestIsPerfectMatching.test_validc                 C   sH   t d}|dd |dd |dd t |dddhsDtd S )	Nr&   r   r   r$   r#   r   r&   r   )r$   r#   r   Zcycle_graphr   r   r   r   r   r   r   test_valid_not_path  s
    
z)TestIsPerfectMatching.test_valid_not_pathc                 C   s`   t j}t d}t|t j|dh t|t j|dh t|t j|ddh t|t j|ddh d S r   )r   r   r   r   r   r   r   r   r   r     s    
z(TestIsPerfectMatching.test_invalid_inputc                 C   s^   t j}t d}t|t j|ddh t |dddhr:t|dd t |ddhrZtd S r   )r   r   r   r   r   r   r   r   r   r   r   r     s    
z$TestIsPerfectMatching.test_selfloopsc                 C   s6   t d}t |dhrtt |dddhr2td S )Nr&   r   r   r+   r'   r   r   r   r   r   r   "  s    
z'TestIsPerfectMatching.test_not_matchingc                 C   s:   t d}|dd |dd t |ddhr6td S )Nr&   r   r   r   r   r   r   r   r   r   test_maximal_but_not_perfect'  s    
z2TestIsPerfectMatching.test_maximal_but_not_perfectN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TestMaximalMatchingzTUnit tests for the
    :func:`~networkx.algorithms.matching.maximal_matching`.

    c                 C   s:   dddddddg}t |}t |}t ||s6td S )Nr+   )r   r$   r'   )r#   r$   )r%   r&   )r%   r=   )r$   r=   )r   r   maximal_matchingr   r   )r   r   r   matchingr   r   r   r   5  s    

z'TestMaximalMatching.test_valid_matchingc                 C   s8   t d}t |}dt|ks$tt ||s4td S )Nr$   r   )r   Z
star_graphr   lenr   r   r   r   r   r   r   r   test_single_edge_matching;  s    

z-TestMaximalMatching.test_single_edge_matchingc                 C   s\   t d}|ddg t |}t|dks2ttdd |D rHtt ||sXtd S )Nr%   r   )r   r   r   c                 s   s   | ]\}}||kV  qd S r|   r   )rJ   uvr   r   r   	<genexpr>I  s     z6TestMaximalMatching.test_self_loops.<locals>.<genexpr>)r   r   add_edges_fromr   r   r   anyr   r   r   r   r   test_self_loopsB  s    

z#TestMaximalMatching.test_self_loopsc                 C   s`   t tdD ]N}t }|| |ddg t|}t|dksJtt	||stqdS )zTests that a maximal matching is computed correctly
        regardless of the order in which nodes are added to the graph.

        r%   r   )r   r#   r   N)
r   ranger   r   Zadd_nodes_fromr   r   r   r   r   )r   Znodesr   r   r   r   r   test_orderingL  s    

z!TestMaximalMatching.test_orderingc                 C   s@   t j}t|t jt   t|t jt   t|t jt   d S r|   )r   r}   r   r   r~   r   r   r   r   r   r   r   Y  s    z)TestMaximalMatching.test_wrong_graph_typeN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   /  s   
r   )r/   	itertoolsr   Zpytestr   Znetworkxr   Znetworkx.algorithms.matchingr   Znetworkx.utilsr   r   r   r   r   r   r   r   r   r   <module>   s      ?$8