
    fiE              	          d dl Z d dlmZ d dlZd dlZd dlmZ ej        	                    dej
        ej        ej        f          ej        	                    ddhdhddhd	dhf          d
                         Zej        	                    dej
        ej        ej        f          ej        	                    dddhddhf          d                         Zej        	                    dej        ej        ej        f          ej        	                    dej        ej        ej        f          d                         Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          ZdS )    N)permutations)edges_equalfnedgeset)r      )r   r         )r   r   c                     t          j        d          }t          j        t           j        d          5   | ||           ddd           dS # 1 swxY w Y   dS )zSAll is_*matching functions have consistent exception message for node
    not in G.   zmatching.*with node not in GmatchNnx
path_graphpytestraisesNetworkXErrorr   r   Gs      /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/networkx/algorithms/tests/test_matching.pytest_is_matching_node_not_in_Gr   
   s     	aA	r'/M	N	N	N  
1g                    AAA)r      r	   )r   c                     t          j        d          }t          j        t           j        d          5   | ||           ddd           dS # 1 swxY w Y   dS )z_All is_*matching functions have consistent exception message for invalid
    edges in matching.r   z.*non-2-tuple edge.*r   Nr   r   s      r   test_is_matching_invalid_edger      s     	aA	r'/E	F	F	F  
1g                 r   
graph_typec                      |            }t          j        t          j                  5   | |           d d d            d S # 1 swxY w Y   d S N)r   r   r   NetworkXNotImplemented)r   r   r   s      r   test_wrong_graph_typer!   0   s    
 	
A	r0	1	1  
1                 s   AA	Ac                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )TestMaxWeightMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.max_weight_matching` function.

    c                     t          j                    }t          j        |          t                      k    sJ t          j        |          t                      k    sJ dS )zEmpty graphN)r   Graphmax_weight_matchingsetmin_weight_matchingselfr   s     r   test_trivial1z#TestMaxWeightMatching.test_trivial1@   sS    HJJ%a((CEE1111%a((CEE111111    c                     t          j                    }|                    ddd           t          j        |          t	                      k    sJ t          j        |          t	                      k    sJ d S )Nr   d   weight)r   r%   add_edger&   r'   r(   r)   s     r   test_selfloopz#TestMaxWeightMatching.test_selfloopF   sk    HJJ	

1a
$$$%a((CEE1111%a((CEE111111r,   c                     t          j                    }|                    dd           t          t          j        |          dh          sJ t          t          j        |          dh          sJ d S )Nr   r   r   r   r   r%   r1   r   r&   r(   r)   s     r   test_single_edgez&TestMaxWeightMatching.test_single_edgeL   sm    HJJ	

1a21!44vh?????21!44vh???????r,   c                     t          j                    }|                    ddd           |                    ddd           t          t          j        |          dh          sJ t          t          j        |          dh          sJ d S )	Nonetwo
   r/   three   r9   r;   r8   r9   r5   r)   s     r   test_two_pathz#TestMaxWeightMatching.test_two_pathR   s    HJJ	

5%
+++	

5'"
---21!447G6HIIIII21!44~6FGGGGGGGr,   c                    t          j                    }|                    ddd           |                    ddd           |                    ddd           t          t          j        |          dh          sJ t          t          j        |d          d	d
h          sJ t          t          j        |          d	d
h          sJ t          t          j        |d          d	d
h          sJ d S )Nr   r	   r   r/   r
   r<   r   r   r   r	   r
   r   r5   r)   s     r   	test_pathzTestMaxWeightMatching.test_pathY   s    HJJ	

1a
"""	

1a
###	

1a
"""21!44vh?????21!Q77&&9IJJJJJ21!44vv6FGGGGG21!Q77&&9IJJJJJJJr,   c                    t          j                    }|                    ddd           |                    ddd           |                    ddd           |                    ddd           t          t          j        |          ddh          sJ t          t          j        |          dd	h          sJ d S )
Nr   r   r	   r/   r
   rA   rB   r   r   r   r5   r)   s     r   test_squarez!TestMaxWeightMatching.test_squarec   s    HJJ	

1a
"""	

1a
"""	

1a
"""	

1a
"""21!44vv6FGGGGG21!44vv6FGGGGGGGr,   c                 ,   t          j                    }|                    dddd           |                    dddd           t          t          j        |d          d	h          sJ t          t          j        |d          d
h          sJ d S )Nr8   r9   r:   r<   )r0   abcdr;   rH   r/   r>   r=   r5   r)   s     r   test_edge_attribute_namez.TestMaxWeightMatching.test_edge_attribute_namel   s    HJJ	

5%
444	

5'"2
66621!FCCCnEUVVVVV21!FCCCFVEWXXXXXXXr,   c                    t          j                    }|                    ddt          j                   |                    ddt          j        d                     |                    ddd           |                    ddt          j        d                     t          t          j        |          dd	h          sJ t          t          j	        |          dd	h          sJ d S )
Nr   r	   r/   r
   g      @r   g       @rE   r   )
r   r%   r1   mathpiexpsqrtr   r&   r(   r)   s     r   test_floating_point_weightsz1TestMaxWeightMatching.test_floating_point_weightss   s    HJJ	

1a
(((	

1a
,,,	

1a
$$$	

1a	#
///21!44vv6FGGGGG21!44vv6FGGGGGGGr,   c                    t          j                    }|                    ddd           |                    ddd           |                    ddd           |                    ddd           |                    ddd           t          t          j        |          d	h          sJ t          t          j        |d
          ddh          sJ t          t          j        |          d	dh          sJ d S )Nr   r	   r/   r
   r   irA   T)maxcardinalityr   r
   r	   r   rB   r5   r)   s     r   test_negative_weightsz+TestMaxWeightMatching.test_negative_weights|   s   HJJ	

1a
"""	

1a
###	

1a
"""	

1a
###	

1a
###21!44vh?????"1T:::VV<L
 
 	
 	
 	
 21!44vv6FGGGGGGGr,   c                    t          j                    }|                    g d           ddh}t          t          j        |          |          sJ t          t          j        |          |          sJ |                    ddg           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z-Create S-blossom and use it for augmentation:)r   r	      r   r
   	   r	   r
   r:   )r
   r      rA   rB   )r      r   )r   r   r^      r   r^   r   r   r   Nr   r%   add_weighted_edges_fromr   r&   r(   r*   r   answers      r   test_s_blossomz$TestMaxWeightMatching.test_s_blossom   s    HJJ	!!"O"O"OPPP&!21!44f=====21!44f=====	!!9i"8999)))21!44f=====21!44f=======r,   c                    t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ |                    ddd           |                    ddd           t          t          j        |          |          sJ t          t          j        |          |          sJ |                    dd           |                    ddd           h d	}t          t          j        |          |          sJ t          t          j        |          |          sJ d
S )z=Create S-blossom, relabel as T-blossom, use for augmentation:)r   r	   r[   r   r
   rY   r\   )r   r   r   )r   r   r   )r   r^   r
   r_   r   r   r
   r/   r   r^   >   rA   r
   r^   ra   N)r   r%   rc   r   r&   r(   r1   remove_edgerd   s      r   test_s_t_blossomz&TestMaxWeightMatching.test_s_t_blossom   sn   HJJ	!!OOO	
 	
 	
 *))21!44f=====21!44f=====	

1a
"""	

1a
"""21!44f=====21!44f=====	a	

1a
""")))21!44f=====21!44f=======r,   c                    t          j                    }|                    g d           h d}d |D             }d t          j        |          D             }||k    sJ d t          j        |          D             }||k    sJ dS )z.Create nested S-blossom, use for augmentation:)rh   rZ   r\   )r	   r   rY   )r
   r   rY   )r   r   r:   )r   r^   r^   >   rT   rU   r   r^   c                 ,    h | ]}t          |          S  	frozenset.0es     r   	<setcomp>z>TestMaxWeightMatching.test_nested_s_blossom.<locals>.<setcomp>   s    ;;;QIaLL;;;r,   c                 ,    h | ]}t          |          S rp   rq   rs   s     r   rv   z>TestMaxWeightMatching.test_nested_s_blossom.<locals>.<setcomp>       BBB1)A,,BBBr,   c                 ,    h | ]}t          |          S rp   rq   rs   s     r   rv   z>TestMaxWeightMatching.test_nested_s_blossom.<locals>.<setcomp>   rx   r,   N)r   r%   rc   r&   r(   )r*   r   expected_edgesetexpectedre   s        r   test_nested_s_blossomz+TestMaxWeightMatching.test_nested_s_blossom   s     HJJ	!!  
	
 
	
 
	
 433;;*:;;;BB(>q(A(ABBB!!!!BB(>q(A(ABBB!!!!!!r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          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   r   r   r      )r   r^   r:   )r^   r]   r:   )r]   rY   rY   >   rA   rB   rn   r]   rY   Nrb   rd   s      r   test_nested_s_blossom_relabelz3TestMaxWeightMatching.test_nested_s_blossom_relabel       HJJ	!!
 
 
	
 	
 	
 21121!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z5Create nested S-blossom, augment, expand recursively:)
rX   ri   r\   )r	   r   r~   )r
   r   r~   )r   r      )r   r^   r~   )r   r]   r~   )r^   r]   r   )r]   rY   r~   >   rA   r
   r   r   r^   r   Nrb   rd   s      r   test_nested_s_blossom_expandz2TestMaxWeightMatching.test_nested_s_blossom_expand   s    HJJ	!!  	
 	
 	
 21121!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z'Create S-blossom, relabel as T, expand:))r   r	      )r   r      )r   r^      r	   r
   r   )r
   r   r   r   )r   rY   r   )r   r]      >   r`   r   r   rY   r   r]   Nrb   rd   s      r   test_s_blossom_relabel_expandz3TestMaxWeightMatching.test_s_blossom_relabel_expand   s    HJJ	!!	 	 		
 	
 	
 21121!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z.Create nested S-blossom, relabel as T, expand:)	)r   r	      )r   r
   r   )r   rY   rY   r   )r	   r      )r
   r   r   )r   r   r   )r   r]   r]   )r   r^   r]   >   r   r]   r   rY   r   rn   Nrb   rd   s      r   $test_nested_s_blossom_relabel_expandz:TestMaxWeightMatching.test_nested_s_blossom_relabel_expand   r   r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )zTCreate blossom, relabel as T in more than one way, expand,
        augment:
        )
r   r	   -   r   r   r   r	   r
   2   r
   r   r   r   r   r   r   r^      r
   r[   #   )r   rY   r   r   r]      r[   r:   r      r[   r:   r`   r   r   r   Nrb   rd   s      r   test_nasty_blossom1z)TestMaxWeightMatching.test_nasty_blossom1       HJJ	!!  	
 	
 	
 ;::21!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )zAgain but slightly different:)
r   r   r   r   r   r   r   )r   rY   r   )r   r]   (   r   r   Nrb   rd   s      r   test_nasty_blossom2z)TestMaxWeightMatching.test_nasty_blossom2(  s    HJJ	!!  	
 	
 	
 ;::21!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )zuCreate blossom, relabel as T, expand such that a new
        least-slack S-to-free dge is produced, augment:
        )
r   r   r   r   r   r   r   )r   rY      r   r   r   Nrb   rd   s      r   test_nasty_blossom_least_slackz4TestMaxWeightMatching.test_nasty_blossom_least_slack=  r   r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z8Create nested blossom, relabel as T in more than one way)r   )r   r]   r   r   r   )r   r   _   )r   r^   ^   )r   r^   r   )r^   r]   r   )r   rY   r   )r
   r<   r   )r   r[   $   )r]   r:   r   )r<   r~   r   >   r   r[   r]   r:   r<   r~   r   r   r   Nrb   rd   s      r   test_nasty_blossom_augmentingz3TestMaxWeightMatching.test_nasty_blossom_augmentingT  s     HJJ	!!  	
 	
 	
" EDD21!44f=====21!44f=======r,   c                     t          j                    }|                    g d           h d}t          t          j        |          |          sJ t          t          j        |          |          sJ dS )z:Create nested S-blossom, relabel as S, expand recursively:))r   r	   r   )r   r
   r   )r	   r
   <   )r	   r   7   )r
   r   r   r   )r   rY   r   )r   r]   r   )r]   r^   r:   )rY   r:   r:   )r   r[   r   >   r   r[   r^   r]   rY   r:   rA   r   Nrb   rd   s      r   %test_nasty_blossom_expand_recursivelyz;TestMaxWeightMatching.test_nasty_blossom_expand_recursivelyn  s    HJJ	!!  	
 	
 	
 ;::21!44f=====21!44f=======r,   N)__name__
__module____qualname____doc__r+   r2   r6   r?   rC   rF   rI   rO   rV   rf   rl   r|   r   r   r   r   r   r   r   r   r   rp   r,   r   r#   r#   :   sn        
2 2 22 2 2@ @ @H H HK K KH H HY Y YH H HH H H> > >> > >*" " ",> > >(> > >*> > >&> > >(> > >.> > >*> > >.> > >4> > > > >r,   r#   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )TestIsMatchingzXUnit tests for the
    :func:`~networkx.algorithms.matching.is_matching` function.

    c                 f    t          j        d          }t          j        |ddddd          sJ d S Nr   r   r   r
   r	   )r   r   r	   r
   r   r   is_matchingr)   s     r   	test_dictzTestIsMatching.test_dict  s;    M!~aQ1q!9!9:::::::r,   c                 t    t          j        d          }t          j        |t                                sJ d S )Nr   )r   r   r   r'   r)   s     r   test_empty_matchingz"TestIsMatching.test_empty_matching  s3    M!~a'''''''r,   c                 ^    t          j        d          }t          j        |dh          sJ d S )Nr   rA   r   r)   s     r   r6   zTestIsMatching.test_single_edge  s1    M!~a&*******r,   c                     t          j        d          }t          j        |ddh          sJ t          j        |ddh          sJ t          j        |ddh          sJ t          j        |ddh          sJ d S )Nr   r4   r   r   r   )r
   r	   r   r)   s     r   test_edge_orderzTestIsMatching.test_edge_order  s    M!~a&&!122222~a&&!122222~a&&!122222~a&&!12222222r,   c                 `    t          j        d          }t          j        |ddh          sJ d S Nr   r4   r   r   r)   s     r   test_valid_matchingz"TestIsMatching.test_valid_matching  s4    M!~a&&!12222222r,   c                     t          j        d          }t          j        |h d          rJ |                    dd           t          j        |ddh          rJ d S Nr   >   r   r   rA   r   r   r   rA   )r   r   r   r1   r)   s     r   test_selfloopszTestIsMatching.test_selfloops  sg    M!>!%=%=%=>>>>>	

1a>!ff%56666666r,   c                 `    t          j        d          }t          j        |h d          rJ d S )Nr      r4   rA   r   r   r)   s     r   test_invalid_matchingz$TestIsMatching.test_invalid_matching  s6    M!>!%=%=%=>>>>>>>r,   c                     t          j        d          }t          j        |ddh          rJ t          j        |j                  }t          j        |dh          sJ t          j        |dh          rJ d S )Nr   r   r
   rA   r4   r   )r   r   r   DiGraphedgesr)   s     r   test_invalid_edgez TestIsMatching.test_invalid_edge  sy    M!>!ff%566666Jqw~a&*****>!fX.......r,   N)r   r   r   r   r   r   r6   r   r   r   r   r   rp   r,   r   r   r     s         
; ; ;( ( (+ + +3 3 33 3 37 7 7? ? ?/ / / / /r,   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )TestIsMaximalMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.is_maximal_matching` function.

    c                 f    t          j        d          }t          j        |ddddd          sJ d S r   r   r   is_maximal_matchingr)   s     r   r   zTestIsMaximalMatching.test_dict  <    M!%aQ1q)A)ABBBBBBBr,   c                 `    t          j        d          }t          j        |ddh          sJ d S r   r   r)   s     r   
test_validz TestIsMaximalMatching.test_valid  5    M!%a&&)9:::::::r,   c                     t          j        d          }t          j        |h d          rJ t          j        |dh          rJ |                    dd           t          j        |dh          rJ d S )Nr   r   r   r   r   )r   r   r   r1   r)   s     r   test_not_matchingz'TestIsMaximalMatching.test_not_matching  s    M!)!-E-E-EFFFFF)!fX66666	

1a)!fX6666666r,   c                 ^    t          j        d          }t          j        |dh          rJ d S )Nr   r4   r   r)   s     r   test_not_maximalz&TestIsMaximalMatching.test_not_maximal  s2    M!)!fX6666666r,   N)r   r   r   r   r   r   r   r   rp   r,   r   r   r     s]         
C C C; ; ;7 7 77 7 7 7 7r,   r   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	TestIsPerfectMatchingz`Unit tests for the
    :func:`~networkx.algorithms.matching.is_perfect_matching` function.

    c                 f    t          j        d          }t          j        |ddddd          sJ d S r   r   r   is_perfect_matchingr)   s     r   r   zTestIsPerfectMatching.test_dict  r   r,   c                 `    t          j        d          }t          j        |ddh          sJ d S r   r   r)   s     r   r   z TestIsPerfectMatching.test_valid  r   r,   c                     t          j        d          }|                    dd           |                    dd           |                    dd           t          j        |h d          sJ d S )Nr   r   r   r   r	   >   r   r	   r   rE   r   cycle_graphr1   r   r)   s     r   test_valid_not_pathz)TestIsPerfectMatching.test_valid_not_path  ss    N1	

1a	

1a	

1a%a)A)A)ABBBBBBBr,   c                     t          j        d          }t          j        |h d          rJ |                    dd           t          j        |ddh          rJ d S r   )r   r   r   r1   r)   s     r   r   z$TestIsPerfectMatching.test_selfloops  si    M!)!-E-E-EFFFFF	

1a)!ff-=>>>>>>>r,   c                     t          j        d          }t          j        |dh          rJ t          j        |h d          rJ d S )Nr   r   r   r   r)   s     r   r   z'TestIsPerfectMatching.test_not_matching  sR    M!)!fX66666)!-E-E-EFFFFFFFr,   c                     t          j        d          }|                    dd           |                    dd           t          j        |ddh          rJ d S )Nr   r   r   rE   r   r   r)   s     r   test_maximal_but_not_perfectz2TestIsPerfectMatching.test_maximal_but_not_perfect  s]    N1	

1a	

1a)!ff-=>>>>>>>r,   N)
r   r   r   r   r   r   r   r   r   r   rp   r,   r   r   r     s         
C C C; ; ;C C C? ? ?G G G
? ? ? ? ?r,   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )TestMaximalMatchingzTUnit tests for the
    :func:`~networkx.algorithms.matching.maximal_matching`.

    c                     g d}t          j        |          }t          j        |          }t          j        ||          sJ d S )N)rA   )r   r   r   )r	   r   rB   rj   rn   )r   r%   maximal_matchingr   )r*   r   r   matchings       r   r   z'TestMaximalMatching.test_valid_matching  sI    HHHHUOO&q))%a2222222r,   c                     t          j        d          }t          j        |          }dt          |          k    sJ t          j        ||          sJ d S )Nr   r   )r   
star_graphr   lenr   r*   r   r  s      r   test_single_edge_matchingz-TestMaximalMatching.test_single_edge_matching  sW    M!&q))CMM!!!!%a2222222r,   c                    t          j        d          }|                    ddg           t          j        |          }t	          |          dk    sJ t          d |D                       rJ t          j        ||          sJ d S )Nr
   r   )r   r   r   c              3   (   K   | ]\  }}||k    V  d S r   rp   )rt   uvs      r   	<genexpr>z6TestMaximalMatching.test_self_loops.<locals>.<genexpr>  s*      33$!QqAv333333r,   )r   r   add_edges_fromr   r  anyr   r  s      r   test_self_loopsz#TestMaximalMatching.test_self_loops  s    M!	&&)***&q))8}}!!!!33(33333333%a2222222r,   c                 B   t          t          d                    D ]}t          j                    }|                    |           |                    ddg           t          j        |          }t          |          dk    sJ t          j        ||          sJ dS )zTests that a maximal matching is computed correctly
        regardless of the order in which nodes are added to the graph.

        r
   r4   )r   r	   r   N)	r   ranger   r%   add_nodes_fromr  r   r  r   )r*   nodesr   r  s       r   test_orderingz!TestMaximalMatching.test_ordering  s    
 "%((++ 	7 	7E

AU###ff-...*1--Hx==A%%%%)!X666666	7 	7r,   N)r   r   r   r   r   r  r  r  rp   r,   r   r   r     sZ         
3 3 33 3 33 3 37 7 7 7 7r,   r   )rK   	itertoolsr   r   networkxr   networkx.utilsr   markparametrizer   r   r   r   r   
MultiGraphr   MultiDiGraphr&   r(   r   r!   r#   r   r   r   r   rp   r,   r   <module>r     s    " " " " " "      & & & & & & 2>2123I
J  					     2>2123I
J  	F	v     rz2?'STT2!2#92;N
O    UTH> H> H> H> H> H> H> H>V
// // // // // // // //d7 7 7 7 7 7 7 74(? (? (? (? (? (? (? (?V(7 (7 (7 (7 (7 (7 (7 (7 (7 (7r,   