o
    ]ZhN                     @   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J t |t ksJ dS )zEmpty graphN)nxGraphmax_weight_matchingsetmin_weight_matchingselfG r   V/var/www/auris/lib/python3.10/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J t |t ks J d S )Nr   d   weight)r   r   add_edger	   r
   r   r   r   r   r   test_selfloop   s   z#TestMaxWeightMatching.test_selfloopc                 C   sT   t  }|dd tt |tdddsJ tt |tddds(J d S )Nr      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s#J tt |tddds2J 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s+J tt |dtddddd	s=J tt |tddd
sLJ tt |dtddd
s\J 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s3J tt |tdddsBJ 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	s'J tt j|ddtddis7J 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s<J tt 
|t	dddddsMJ d S )	Nr   r"   r   r$   g      @r%   g       @r'   )r   r   r   mathpiexpsqrtr   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	s;J tt j|d
dtdddddsNJ tt |tddds]J 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  }|g d tddddd}tt ||sJ tt ||s(J |ddg td	ddd
ddd}tt ||sDJ tt ||sNJ 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   answerr   r   r   test_s_blossom   s   z$TestMaxWeightMatching.test_s_blossomc                 C   s   t  }|g d tddddddd}tt ||s J tt ||s*J |jdddd	 |jdddd	 tt ||sDJ tt ||sNJ |dd |jdddd	 tddddddd}tt ||sqJ tt ||s{J d
S )z=Create S-blossom, relabel as T-blossom, use for augmentation:)r   r"   r9   r   r$   r7   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   Zremove_edger@   r   r   r   test_s_t_blossom   s    z&TestMaxWeightMatching.test_s_t_blossomc                 C   s~   t  }|g d ddddddd}d	d
 t|D }dd
 t |D }||ks-J dd
 t |D }||ks=J dS )z.Create nested S-blossom, use for augmentation:)rC   r8   r:   )r"   r%   r7   )r$   r#   r7   )r%   r#   r   )r#   r<   r<   r$   r%   r   r"   r<   r#   r=   c                 S      h | ]}t |qS r   	frozenset.0er   r   r   	<setcomp>       z>TestMaxWeightMatching.test_nested_s_blossom.<locals>.<setcomp>c                 S   rF   r   rG   rI   r   r   r   rL      rM   c                 S   rF   r   rG   rI   r   r   r   rL      rM   N)r   r   r?   r   r	   r   )r   r   Zdict_formatexpectedrA   r   r   r   test_nested_s_blossom   s   z+TestMaxWeightMatching.test_nested_s_blossomc              
   C   \   t  }|g d tdddddddd	d
}tt ||s"J tt ||s,J 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#   rR   r%   r#      )r#   r<   r   )r<   r;   r   )r;   r7   r7   r"   r   r%   r$   r<   r#   r7   r;   r   r"   r$   r%   r#   r<   r;   r7   Nr>   r@   r   r   r   test_nested_s_blossom_relabel      z3TestMaxWeightMatching.test_nested_s_blossom_relabelc              
   C   rP   )z5Create nested S-blossom, augment, expand recursively:)
r6   rD   r:   )r"   r%   rQ   )r$   r#   rQ   )r%   r#      )r%   r<   rQ   )r#   r;   rQ   )r<   r;   rX   )r;   r7   rQ   r"   r   r#   r<   r$   r%   r7   r;   rU   Nr>   r@   r   r   r   test_nested_s_blossom_expand   s   z2TestMaxWeightMatching.test_nested_s_blossom_expandc              
   C   rP   )z'Create S-blossom, relabel as T, expand:))r   r"      )r   r#      )r   r<      r"   r$   rT   )r$   r%   r[   rS   )r%   r7   rX   )r#   r;      r<   r$   r"   r7   r;   r   r#   r%   rU   Nr>   r@   r   r   r   test_s_blossom_relabel_expand   s   z3TestMaxWeightMatching.test_s_blossom_relabel_expandc              
   C   rP   )z.Create nested S-blossom, relabel as T, expand:)	)r   r"      )r   r$   rR   )r   r7   r7   r]   )r"   r%      )r$   r#   ra   )r%   r#   r^   )r%   r;   r;   )r#   r<   r;   r7   r$   r"   r;   r<   r#   r%   r   rU   Nr>   r@   r   r   r   $test_nested_s_blossom_relabel_expand   rW   z:TestMaxWeightMatching.test_nested_s_blossom_relabel_expandc                 C   d   t  }|g d dddddddd	d
dd
}t|}tt ||s&J tt ||s0J dS )zTCreate blossom, relabel as T in more than one way, expand,
        augment:
        )
r   r"   -   r   r#   re   r"   r$   2   r$   r%   re   r%   r#   rh   r   r<      r$   r9   #   )r%   r7   rn   r#   r;      r9   r   r#   r<   r$   r"   r7   r;   r   r#   r%   r   r9   
r   r"   r$   r%   r#   r<   r;   r7   r9   r   Nr>   )r   r   ZansdictrA   r   r   r   test_nasty_blossom1     z)TestMaxWeightMatching.test_nasty_blossom1c                 C   rc   )zAgain but slightly different:)
rd   rf   rg   ri   rj   rk   rm   )r%   r7   rp   )r#   r;   (   rq   r<   r$   r"   r7   r;   r   r#   r%   r   r9   rr   Nr>   r   r   ZansrA   r   r   r   test_nasty_blossom2  s   z)TestMaxWeightMatching.test_nasty_blossom2c                 C   rc   )zuCreate blossom, relabel as T, expand such that a new
        least-slack S-to-free dge is produced, augment:
        )
rd   rf   rg   ri   rj   rk   rm   )r%   r7      ro   rq   r<   r$   r"   r7   r;   r   r#   r%   r   r9   rr   Nr>   rv   r   r   r   test_nasty_blossom_least_slack5  rt   z4TestMaxWeightMatching.test_nasty_blossom_least_slackc                 C   sh   t  }|g d dddddddd	d
dddd}t|}tt ||s(J tt ||s2J dS )z8Create nested blossom, relabel as T in more than one way)rd   )r   r;   re   rg   ri   )r%   r#   _   )r%   r<   ^   )r#   r<   r{   )r<   r;   rh   )r   r7   rl   )r$   r   rn   )r#   r9   $   )r;   r   rp   )r   rQ   r#   r7   r$   r"   r<   r9   r%   r   r   r#   r;   rQ   r   )r   r"   r$   r%   r#   r<   r;   r7   r9   r   r   rQ   Nr>   rv   r   r   r   test_nasty_blossom_augmentingM  s(   z3TestMaxWeightMatching.test_nasty_blossom_augmentingc                 C   rc   )z:Create nested S-blossom, relabel as S, expand recursively:))r   r"   ru   )r   r$   ru   )r"   r$   <   )r"   r%   7   )r$   r#   r   rj   )r   r7   r\   )r#   r;   rl   )r;   r<   r   )r7   r   r   )r%   r9   rl   r"   r   r#   r9   r$   r;   r<   r   r%   r7   rr   Nr>   rv   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-   r2   r5   rB   rE   rO   rV   rY   r_   rb   rs   rw   ry   r}   r   r   r   r   r   r   r      s0    
(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   (   t d}t |dddddsJ d S Nr%   r   r   r$   r"   )r   r   r"   r$   r   
path_graphis_matchingr   r   r   r   	test_dict     
zTestIsMatching.test_dictc                 C   s    t d}t |t sJ d S )Nr%   )r   r   r   r
   r   r   r   r   test_empty_matching     
z"TestIsMatching.test_empty_matchingc                 C   s    t d}t |dhsJ d S )Nr%   r*   r   r   r   r   r   r     r   zTestIsMatching.test_single_edgec                 C   s^   t d}t |ddhsJ t |ddhsJ t |ddhs#J t |ddhs-J d S )Nr%   r   r&   r   r   )r$   r"   r   r   r   r   r   test_edge_order  s
   
zTestIsMatching.test_edge_orderc                 C   "   t d}t |ddhsJ d S Nr%   r   r&   r   r   r   r   r   test_valid_matching     
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   \   t j}t d}t|t j|ddh t |h drJ |dd t |ddhr,J d S Nr%   )r#   r#   r&   >   r&   r*   r   r   r   r   r*   )r   r   r   r   r   r   r   r   r   r   test_selfloops     
zTestIsMatching.test_selfloopsc                 C   s"   t d}t |h drJ d S )Nr%      r   r*   r&   r   r   r   r   r   test_invalid_matching  r   z$TestIsMatching.test_invalid_matchingc                 C   sf   t d}t |ddhrJ tt jt j|dh t |j}t |dhs(J t |dhr1J d S )Nr%   r   r$   r*   )r   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                   @   8   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   r   r   r   r   is_maximal_matchingr   r   r   r   r     r   zTestIsMaximalMatching.test_dictc                 C   `   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        
z(TestIsMaximalMatching.test_invalid_inputc                 C   r   r   r   r   r   r   r   
test_valid  r   z TestIsMaximalMatching.test_validc                 C   sR   t d}t |h drJ t |dhrJ |dd t |dhr'J d S )Nr%   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J d S )Nr%   r   r   r   r   r   r   test_not_maximal  r   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   r   r   r   r   is_perfect_matchingr   r   r   r   r     r   zTestIsPerfectMatching.test_dictc                 C   r   r   r   r   r   r   r   r     r   z TestIsPerfectMatching.test_validc                 C   sF   t d}|dd |dd |dd t |h ds!J d S )Nr%   r   r   r#   r"   >   r   r   r%   )r#   r"   r   Zcycle_graphr   r   r   r   r   r   test_valid_not_path  s
   
z)TestIsPerfectMatching.test_valid_not_pathc                 C   r   r   )r   r   r   r   r   r   r   r   r   r     r   z(TestIsPerfectMatching.test_invalid_inputc                 C   r   r   )r   r   r   r   r   r   r   r   r   r   r     r   z$TestIsPerfectMatching.test_selfloopsc                 C   s4   t d}t |dhrJ t |h drJ d S )Nr%   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rJ 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                   @   r   )TestMaximalMatchingzTUnit tests for the
    :func:`~networkx.algorithms.matching.maximal_matching`.

    c                 C   s0   g d}t |}t |}t ||sJ d S )N)r*   )r   r#   r&   )r"   r#   )r$   r%   )r$   r<   )r#   r<   )r   r   maximal_matchingr   )r   r   r   matchingr   r   r   r   5  s   

z'TestMaximalMatching.test_valid_matchingc                 C   s8   t d}t |}dt|ksJ t ||sJ d S )Nr#   r   )r   Z
star_graphr   lenr   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sJ tdd |D r$J t ||s,J 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   anyr   r   r   r   r   test_self_loopsB  s   

z#TestMaximalMatching.test_self_loopsc                 C   s`   t tdD ]'}t }|| |ddg t|}t|dks%J t||s-J 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   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