
    VhE              	       F   d dl Z d dlmZ d dlZd dlZd dlmZ ej                  j                  dej                  ej                  ej                  f      ej                  j                  ddhdhddhd	dhf      d
               Zej                  j                  dej                  ej                  ej                  f      ej                  j                  dddhddhf      d               Zej                  j                  dej                  ej                   ej"                  f      ej                  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y)    N)permutations)edges_equalfnedgeset)r      )r   r         )r   r   c                     t        j                  d      }t        j                  t         j                  d      5   | ||       ddd       y# 1 sw Y   yxY w)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      W/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/tests/test_matching.pytest_is_matching_node_not_in_Gr   
   sE     	aA	r''/M	N 
1g     
AA)r      r	   )r   c                     t        j                  d      }t        j                  t         j                  d      5   | ||       ddd       y# 1 sw Y   yxY w)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      sE     	aA	r''/E	F 
1g  r   
graph_typec                      |       }t        j                  t        j                        5   | |       d d d        y # 1 sw Y   y xY wN)r   r   r   NetworkXNotImplemented)r   r   r   s      r   test_wrong_graph_typer!   0   s9    
 	A	r00	1 
1  s	   	=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y)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 y)zEmpty graphN)r   Graphmax_weight_matchingsetmin_weight_matchingselfr   s     r   test_trivial1z#TestMaxWeightMatching.test_trivial1@   sC    HHJ%%a(CE111%%a(CE111    c                     t        j                         }|j                  ddd       t        j                  |      t	               k(  sJ t        j
                  |      t	               k(  sJ y )Nr   d   weight)r   r%   add_edger&   r'   r(   r)   s     r   test_selfloopz#TestMaxWeightMatching.test_selfloopF   sU    HHJ	

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

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

5%
+	

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

1a
"	

1a
#	

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

1a
"	

1a
"	

1a
"	

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

5%
4	

5'"2
6211!FCnEUVVV211!FCFVEWXXXr,   c                    t        j                         }|j                  ddt        j                         |j                  ddt        j
                  d             |j                  ddd       |j                  ddt        j                  d             t        t        j                  |      dd	h      sJ t        t        j                  |      dd	h      sJ y )
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    HHJ	

1a
(	

1a
,	

1a
$	

1a		#
/211!4vv6FGGG211!4vv6FGGGr,   c                    t        j                         }|j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       |j                  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 y )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    HHJ	

1a
"	

1a
#	

1a
"	

1a
#	

1a
#211!4vh???""1T:VV<L
 	
 
 211!4vv6FGGGr,   c                    t        j                         }|j                  g d       ddh}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ |j                  ddg       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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    HHJ	!!"OP&!211!4f===211!4f===	!!9i"89)211!4f===211!4f===r,   c                    t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ |j                  ddd       |j                  ddd       t        t        j                  |      |      sJ t        t        j
                  |      |      sJ |j                  dd       |j                  ddd       h d	}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y
)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   s   HHJ	!!O	
 *211!4f===211!4f===	

1a
"	

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

1a
")211!4f===211!4f===r,   c                 p   t        j                         }|j                  g d       h d}|D ch c]  }t        |       }}t        j                  |      D ch c]  }t        |       }}||k(  sJ t        j
                  |      D ch c]  }t        |       }}||k(  sJ yc c}w c c}w c c}w )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^   N)r   r%   rc   	frozensetr&   r(   )r*   r   expected_edgeseteexpectedre   s         r   test_nested_s_blossomz+TestMaxWeightMatching.test_nested_s_blossom   s     HHJ	!!
	
 4*:;QIaL;;(*(>(>q(AB1)A,BB!!!(*(>(>q(AB1)A,BB!!!	 <BBs   B)B.B3c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)z<Create S-blossom, relabel as S, include in nested S-blossom:)	)r   r	   r:   )r   r]   r:   )r	   r
      )r
   r      )r
   r   rv   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   ]    HHJ	!!
	
 2211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)z5Create nested S-blossom, augment, expand recursively:)
rX   ri   r\   )r	   r   ru   )r
   r   ru   )r   r      )r   r^   ru   )r   r]   ru   )r^   r]   r}   )r]   rY   ru   >   rA   r
   r   r   r^   ry   Nrb   rd   s      r   test_nested_s_blossom_expandz2TestMaxWeightMatching.test_nested_s_blossom_expand   s]    HHJ	!!	
 2211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)z'Create S-blossom, relabel as T, expand:))r   r	      )r   r      )r   r^      r	   r
   rx   )r
   r   r   rw   )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]    HHJ	!!		
 2211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)z.Create nested S-blossom, relabel as T, expand:)	)r   r	      )r   r
   rv   )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                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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  _     HHJ	!!	
 ;211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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]    HHJ	!!	
 ;211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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<   ru   r   >   r   r[   r]   r:   r<   ru   r   r   r   Nrb   rd   s      r   test_nasty_blossom_augmentingz3TestMaxWeightMatching.test_nasty_blossom_augmentingT  s`     HHJ	!!	
" E211!4f===211!4f===r,   c                     t        j                         }|j                  g d       h d}t        t        j                  |      |      sJ t        t        j
                  |      |      sJ y)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]    HHJ	!!	
 ;211!4f===211!4f===r,   N)__name__
__module____qualname____doc__r+   r2   r6   r?   rC   rF   rI   rO   rV   rf   rl   rs   rz   r   r   r   r   r   r   r   r    r,   r   r#   r#   :   sw    
22@HKHYHH>>*",>(>*>&>(>.>*>.>4>r,   r#   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TestIsMatchingzXUnit tests for the
    :func:`~networkx.algorithms.matching.is_matching` function.

    c                 h    t        j                  d      }t        j                  |ddddd      sJ y Nr   r   r   r
   r	   )r   r   r	   r
   r   r   is_matchingr)   s     r   	test_dictzTestIsMatching.test_dict  s-    MM!~~aQ1q!9:::r,   c                 n    t        j                  d      }t        j                  |t                     sJ y )Nr   )r   r   r   r'   r)   s     r   test_empty_matchingz"TestIsMatching.test_empty_matching  s&    MM!~~a'''r,   c                 `    t        j                  d      }t        j                  |dh      sJ y )Nr   rA   r   r)   s     r   r6   zTestIsMatching.test_single_edge  s&    MM!~~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 y )Nr   r4   r   r   r   )r
   r	   r   r)   s     r   test_edge_orderzTestIsMatching.test_edge_order  st    MM!~~a&&!1222~~a&&!1222~~a&&!1222~~a&&!1222r,   c                 b    t        j                  d      }t        j                  |ddh      sJ y Nr   r4   r   r   r)   s     r   test_valid_matchingz"TestIsMatching.test_valid_matching  s)    MM!~~a&&!1222r,   c                     t        j                  d      }t        j                  |h d      rJ |j                  dd       t        j                  |ddh      rJ y Nr   >   r   r   rA   r   r   r   rA   )r   r   r   r1   r)   s     r   test_selfloopszTestIsMatching.test_selfloops  sO    MM!>>!%=>>>	

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

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

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

    c                 h    t        j                  d      }t        j                  |ddddd      sJ y r   r   r   is_perfect_matchingr)   s     r   r   zTestIsPerfectMatching.test_dict  r   r,   c                 b    t        j                  d      }t        j                  |ddh      sJ y r   r   r)   s     r   r   z TestIsPerfectMatching.test_valid  r   r,   c                     t        j                  d      }|j                  dd       |j                  dd       |j                  dd       t        j                  |h d      sJ y )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  sQ    NN1	

1a	

1a	

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

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

1a	

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

    c                     g d}t        j                  |      }t        j                  |      }t        j                  ||      sJ y )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  s:    HHHUO&&q)%%a222r,   c                     t        j                  d      }t        j                  |      }dt        |      k(  sJ t        j                  ||      sJ y )Nr   r   )r   
star_graphr   lenr   r*   r   r   s      r   test_single_edge_matchingz-TestMaximalMatching.test_single_edge_matching  sH    MM!&&q)CM!!!%%a222r,   c                     t        j                  d      }|j                  ddg       t        j                  |      }t	        |      dk(  sJ t        d |D              rJ t        j                  ||      sJ y )Nr
   r   )r   r   r   c              3   ,   K   | ]  \  }}||k(    y wr   r   ).0uvs      r   	<genexpr>z6TestMaximalMatching.test_self_loops.<locals>.<genexpr>  s     3$!QqAv3s   )r   r   add_edges_fromr   r   anyr   r   s      r   test_self_loopsz#TestMaximalMatching.test_self_loops  so    MM!	&&)*&&q)8}!!!3(3333%%a222r,   c                     t        t        d            D ]w  }t        j                         }|j	                  |       |j                  ddg       t        j                  |      }t        |      dk(  sJ t        j                  ||      rwJ  y)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    
 "%(+ 	7E
AU#ff-.**1-Hx=A%%%))!X666	7r,   N)r   r   r   r   r   r   r  r  r   r,   r   r   r     s    
3337r,   r   )rK   	itertoolsr   r   networkxr   networkx.utilsr   markparametrizer   r   r   r   r   
MultiGraphr   MultiDiGraphr&   r(   r   r!   r#   r   r   r   r   r   r,   r   <module>r     s    "   & 2>>21123I3I
J 					 2>>21123I3I
J 	F	v rzz2??'ST2!!2#9#92;N;N
O UH> H>V
// //d7 74(? (?V(7 (7r,   