o
    ˷eI                     @   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   ^/var/www/ideatree/venv/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 |dtddddd
sMJ tt |tddds\J tt |dtdddslJ d S )Nr   r"   r   r$   r%   ir*   r'   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   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   rG   r   rH   rJ   r   r   r   rM      rN   c                 S   rG   r   rH   rJ   r   r   r   rM      rN   N)r   r   r?   r   r	   r   )r   r   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#   rT   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   rR   )z5Create nested S-blossom, augment, expand recursively:)
r6   rD   r:   )r"   r%   rS   )r$   r#   rS   )r%   r#      )r%   r<   rS   )r#   r;   rS   )r<   r;   rZ   )r;   r7   rS   r"   r   r#   r<   r$   r%   r7   r;   rW   Nr>   r@   r   r   r   test_nested_s_blossom_expand   s   z2TestMaxWeightMatching.test_nested_s_blossom_expandc              
   C   rR   )z'Create S-blossom, relabel as T, expand:))r   r"      )r   r#      )r   r<      r"   r$   rV   )r$   r%   r]   rU   )r%   r7   rZ   )r#   r;      r<   r$   r"   r7   r;   r   r#   r%   rW   Nr>   r@   r   r   r   test_s_blossom_relabel_expand   s   z3TestMaxWeightMatching.test_s_blossom_relabel_expandc              
   C   rR   )z.Create nested S-blossom, relabel as T, expand:)	)r   r"      )r   r$   rT   )r   r7   r7   r_   )r"   r%      )r$   r#   rc   )r%   r#   r`   )r%   r;   r;   )r#   r<   r;   r7   r$   r"   r;   r<   r#   r%   r   rW   Nr>   r@   r   r   r   $test_nested_s_blossom_relabel_expand   rY   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#   rg   r"   r$   2   r$   r%   rg   r%   r#   rj   r   r<      r$   r9   #   )r%   r7   rp   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   ansdictrA   r   r   r   test_nasty_blossom1	     z)TestMaxWeightMatching.test_nasty_blossom1c                 C   re   )zAgain but slightly different:)
rf   rh   ri   rk   rl   rm   ro   )r%   r7   rr   )r#   r;   (   rs   r<   r$   r"   r7   r;   r   r#   r%   r   r9   rt   Nr>   r   r   ansrA   r   r   r   test_nasty_blossom2!  s   z)TestMaxWeightMatching.test_nasty_blossom2c                 C   re   )zuCreate blossom, relabel as T, expand such that a new
        least-slack S-to-free dge is produced, augment:
        )
rf   rh   ri   rk   rl   rm   ro   )r%   r7      rq   rs   r<   r$   r"   r7   r;   r   r#   r%   r   r9   rt   Nr>   ry   r   r   r   test_nasty_blossom_least_slack7  rw   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)rf   )r   r;   rg   ri   rk   )r%   r#   _   )r%   r<   ^   )r#   r<   r   )r<   r;   rj   )r   r7   rn   )r$   r   rp   )r#   r9   $   )r;   r   rr   )r   rS   r#   r7   r$   r"   r<   r9   r%   r   r   r#   r;   rS   r   )r   r"   r$   r%   r#   r<   r;   r7   r9   r   r   rS   Nr>   ry   r   r   r   test_nasty_blossom_augmentingO  s(   z3TestMaxWeightMatching.test_nasty_blossom_augmentingc                 C   re   )z:Create nested S-blossom, relabel as S, expand recursively:))r   r"   rx   )r   r$   rx   )r"   r$   <   )r"   r%   7   )r$   r#   r   rl   )r   r7   r^   )r#   r;   rn   )r;   r<   r   )r7   r   r   )r%   r9   rn   r"   r   r#   r9   r$   r;   r<   r   r%   r7   rt   Nr>   ry   r   r   r   %test_nasty_blossom_expand_recursivelyw  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   rF   rQ   rX   r[   ra   rd   rv   r{   r}   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   s\   t j}t d}t|t j|ddh t |h drJ |dd t |h dr,J 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   "   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                   @   s0   e Zd Z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   r   r   r   r   r   r   r   
test_valid  r   z TestIsMaximalMatching.test_validc                 C   r   r   r   r   r   r   r   test_not_matching  r   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     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 )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   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   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     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     s   

z'TestMaximalMatching.test_valid_matchingc                 C   s8   t d}t |}dt|ksJ t ||sJ d S )Nr#   r   )r   
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   )rK   uvr   r   r   	<genexpr>(  s    z6TestMaximalMatching.test_self_loops.<locals>.<genexpr>)r   r   add_edges_fromr   r   anyr   r   r   r   r   test_self_loops!  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   add_nodes_fromr   r   r   r   )r   nodesr   r   r   r   r   test_ordering+  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   8  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   pytestr   networkxr   networkx.algorithms.matchingr   networkx.utilsr   r   r   r   r   r   r   r   r   r   <module>   s       ?"