
    ިsgN                         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      Z G d d	      Z G d
 d      Z G d d      Z G d d      Zy)    N)permutations)raises)matching_dict_to_set)edges_equalc                       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 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)nxGraphmax_weight_matchingsetmin_weight_matchingselfGs     Z/var/www/html/venv/lib/python3.12/site-packages/networkx/algorithms/tests/test_matching.py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_selfloop   sU    HHJ	

1a
$%%a(CE111%%a(CE111r   c                    t        j                         }|j                  dd       t        t        j                  |      t        ddd            sJ t        t        j                  |      t        ddd            sJ y )Nr      r   r   r
   r   r   r   r   r   r   r   s     r   test_single_edgez&TestMaxWeightMatching.test_single_edge   su    HHJ	

1a""1%';aL'I
 	
 
 ""1%';aL'I
 	
 
r   c                 0   t        j                         }|j                  ddd       |j                  ddd       t        t        j                  |      t        ddd            sJ t        t        j                  |      t        ddd            sJ y )	Nonetwo
   r   three   )r$   r"   r!   r"   r   r   s     r   test_two_pathz#TestMaxWeightMatching.test_two_path'   s    HHJ	

5%
+	

5'"
-""1% 5!AB
 	
 
 ""1% u!=>
 	
 
r   c           
         t        j                         }|j                  ddd       |j                  ddd       |j                  ddd       t        t        j                  |      t        ddd            sJ t        t        j                  |d      t        ddddd	            sJ t        t        j                  |      t        ddd
            sJ t        t        j                  |d      t        ddd
            sJ y )Nr         r      r%      r)   r+   r   r)   r+   r,   r   r+   r   r   s     r   	test_pathzTestMaxWeightMatching.test_path4   s   HHJ	

1a
"	

1a
#	

1a
"""1%';aL'I
 	
 
 ""1a(*>1qUV?W*X
 	
 
 ""1%';aL'I
 	
 
 ""1a(*>1|*L
 	
 
r   c                    t        j                         }|j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       t        t        j                  |      t        ddd            sJ t        t        j                  |      t        ddd            sJ y )Nr   r,   r)   r   r+   r/   r   r)   r   r   s     r   test_squarez!TestMaxWeightMatching.test_squareF   s    HHJ	

1a
"	

1a
"	

1a
"	

1a
"""1%';aL'I
 	
 
 ""1%';aL'I
 	
 
r   c                 :   t        j                         }|j                  dddd       |j                  dddd       t        t        j                  |d      t        ddd	            sJ t        t        j                  |d      t        ddi            sJ y )
Nr!   r"   r#   r%   )r   abcdr$   r5   r   r&   r   r   s     r   test_edge_attribute_namez.TestMaxWeightMatching.test_edge_attribute_nameS   s    HHJ	

5%
4	

5'"2
6""1V4 u!=>
 	
 
 ""1V4 '5!12
 	
 
r   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                  |      t        ddddd            sJ t        t        j                  |      t        ddddd            sJ y )	Nr   r)   r   r+   g      @r,   g       @r.   )r
   r   r   mathpiexpsqrtr   r   r   r   r   s     r   test_floating_point_weightsz1TestMaxWeightMatching.test_floating_point_weights`   s    HHJ	

1a
(	

1a
,	

1a
$	

1a		#
/""1%';aARS<T'U
 	
 
 ""1%';aARS<T'U
 	
 
r   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                  |      t        ddd	            sJ t        t        j                  |d
      t        ddddd            sJ t        t        j                  |      t        ddd            sJ y )Nr   r)   r   r+   r,   ir2   T)maxcardinalityr.   r/   r   r   s     r   test_negative_weightsz+TestMaxWeightMatching.test_negative_weightsm   s    HHJ	

1a
"	

1a
#	

1a
"	

1a
#	

1a
#""1%';aL'I
 	
 
 ""1T: Q1q!9:
 	
 
 ""1%';aL'I
 	
 
r   c           	         t        j                         }|j                  g d       t        ddddd      }t	        t        j
                  |      |      sJ t	        t        j                  |      |      sJ |j                  ddg       t        d	ddd
dd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,      r)   r   r,   r+   r.   )r      r*   )r,   r*   rI   rI   r*   r   r)   r+   r,   r*   rI   Nr
   r   add_weighted_edges_fromr   r   r   r   r   r   answers      r   test_s_blossomz$TestMaxWeightMatching.test_s_blossom   s    HHJ	!!"OP%!aA&>?211!4f===211!4f===	!!9i"89%!aA!&JK211!4f===211!4f===r   c           	         t        j                         }|j                  g d       t        dddddd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	       t        dddddd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)   rF   r   r+   rD   rG   )r   r,   r*   )r,   r*   r,   )r   rI   r+   rI   r+   r)   r*   r,   r   rJ   r   N)	r
   r   rL   r   r   r   r   r   remove_edgerM   s      r   test_s_t_blossomz&TestMaxWeightMatching.test_s_t_blossom   s4   HHJ	!!O	
 &!aA!&JK211!4f===211!4f===	

1a
"	

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

1a
"%!aA!&JK211!4f===211!4f===r   c                    t        j                         }|j                  g d       ddddddd}t        |      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:)rQ   rE   rG   )r)   r,   rD   )r+   r*   rD   )r,   r*   r#   )r*   rI   rI   r+   r,   r   r)   rI   r*   rJ   N)r
   r   rL   r   	frozensetr   r   )r   r   dict_formateexpectedrN   s         r   test_nested_s_blossomz+TestMaxWeightMatching.test_nested_s_blossom   s     HHJ	!!
	
 aA!:*>{*KLQIaLLL(*(>(>q(AB1)A,BB!!!(*(>(>q(AB1)A,BB!!!	 MBBs   B7)B<Cc                     t        j                         }|j                  g d       t        dddddddd	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   rH   r#   )r)   r+      )r+   r,      )r+   r*   r]   r,   r*      )r*   rI   r#   )rI   rH   r#   )rH   rD   rD   r)   r   r,   r+   rI   r*   rD   rH   r   r)   r+   r,   r*   rI   rH   rD   NrK   rM   s      r   test_nested_s_blossom_relabelz3TestMaxWeightMatching.test_nested_s_blossom_relabel   t    HHJ	!!
	
 &!aA!aTU&VW211!4f===211!4f===r   c                     t        j                         }|j                  g d       t        dddddddd	d
      }t	        t        j
                  |      |      sJ t	        t        j                  |      |      sJ y)z5Create nested S-blossom, augment, expand recursively:)
rC   rR   rG   )r)   r,   r\   )r+   r*   r\   )r,   r*      )r,   rI   r\   )r*   rH   r\   )rI   rH   rd   )rH   rD   r\   r)   r   r*   rI   r+   r,   rD   rH   r`   NrK   rM   s      r   test_nested_s_blossom_expandz2TestMaxWeightMatching.test_nested_s_blossom_expand   st    HHJ	!!	
 &!aA!aTU&VW211!4f===211!4f===r   c                     t        j                         }|j                  g d       t        dddddddd	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   rI      r)   r+   r_   )r+   r,   rh   r^   )r,   rD   rd   )r*   rH      rI   r+   r)   rD   rH   r   r*   r,   r`   NrK   rM   s      r   test_s_blossom_relabel_expandz3TestMaxWeightMatching.test_s_blossom_relabel_expand   st    HHJ	!!		
 &!aA!aTU&VW211!4f===211!4f===r   c                     t        j                         }|j                  g d       t        dddddddd	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+   r]   )r   rD   rD   rj   )r)   r,      )r+   r*   ro   )r,   r*   rk   )r,   rH   rH   )r*   rI   rH   rD   r+   r)   rH   rI   r*   r,   r   r`   NrK   rM   s      r   $test_nested_s_blossom_relabel_expandz:TestMaxWeightMatching.test_nested_s_blossom_relabel_expand   rb   r   c                    t        j                         }|j                  g d       dddddddd	d
dd
}t        |      }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*   rs   r)   r+   2   r+   r,   rs   r,   r*   rv   r   rI      r+   rF   #   )r,   rD   r|   r*   rH      rF   r#   r*   rI   r+   r)   rD   rH   r   r*   r,   r#   rF   
r   r)   r+   r,   r*   rI   rH   rD   rF   r#   NrK   )r   r   ansdictrN   s       r   test_nasty_blossom1z)TestMaxWeightMatching.test_nasty_blossom1  s~     HHJ	!!	
 A!aA!baP%g.211!4f===211!4f===r   c                    t        j                         }|j                  g d       dddddddd	d
dd
}t        |      }t	        t        j
                  |      |      sJ t	        t        j                  |      |      sJ y)zAgain but slightly different:)
rr   rt   ru   rw   rx   ry   r{   )r,   rD   r~   )r*   rH   (   r   rI   r+   r)   rD   rH   r   r*   r,   r#   rF   r   NrK   r   r   ansrN   s       r   test_nasty_blossom2z)TestMaxWeightMatching.test_nasty_blossom2  s|    HHJ	!!	
 aA!aA"!L%c*211!4f===211!4f===r   c                    t        j                         }|j                  g d       dddddddd	d
dd
}t        |      }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:
        )
rr   rt   ru   rw   rx   ry   r{   )r,   rD      r}   r   rI   r+   r)   rD   rH   r   r*   r,   r#   rF   r   NrK   r   s       r   test_nasty_blossom_least_slackz4TestMaxWeightMatching.test_nasty_blossom_least_slack5  s~     HHJ	!!	
 aA!aA"!L%c*211!4f===211!4f===r   c                 
   t        j                         }|j                  g d       dddddddd	d
dddd}t        |      }t	        t        j
                  |      |      sJ t	        t        j                  |      |      sJ y)z8Create nested blossom, relabel as T in more than one way)rr   )r   rH   rs   ru   rw   )r,   r*   _   )r,   rI   ^   )r*   rI   r   )rI   rH   rv   )r   rD   rz   )r+   r%   r|   )r*   rF   $   )rH   r#   r~   )r%   r\   r*   rD   r+   r)   rI   rF   r,   r#   r   r*   rH   r\   r%   )r   r)   r+   r,   r*   rI   rH   rD   rF   r#   r%   r\   NrK   r   s       r   test_nasty_blossom_augmentingz3TestMaxWeightMatching.test_nasty_blossom_augmentingM  s     HHJ	!!	
$ 
 &c*211!4f===211!4f===r   c                    t        j                         }|j                  g d       dddddddd	d
dd
}t        |      }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   rx   )r   rD   ri   )r*   rH   rz   )rH   rI   r#   )rD   r#   r#   )r,   rF   rz   r)   r   r*   rF   r+   rH   rI   r#   r,   rD   r   NrK   r   s       r   %test_nasty_blossom_expand_recursivelyz;TestMaxWeightMatching.test_nasty_blossom_expand_recursivelyu  s|    HHJ	!!	
 aA!aB1!L%c*211!4f===211!4f===r   c                    t         j                  }t        |t         j                  t        j                                t        |t         j                  t        j
                                t        |t         j                  t        j                                t        |t         j                  t        j                                y N)r
   NetworkXNotImplementedr   r   
MultiGraphMultiDiGraphDiGraphr   r   errors     r   test_wrong_graph_typez+TestMaxWeightMatching.test_wrong_graph_type  sn    ))ub,,bmmo>ub,,boo.?@ub,,bjjl;ub,,bjjl;r   N)__name__
__module____qualname____doc__r   r   r   r'   r0   r3   r6   r<   rA   rO   rT   rZ   ra   re   rl   rp   r   r   r   r   r   r    r   r   r   r      sv    
22


$



$>>*",>(>*>&>(>0>,>0&>P>.<r   r   c                   F    e Zd 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
   
path_graph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,   r2   r   r   s     r   r   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,   r   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,   r   r-   r   r   s     r   test_valid_matchingz"TestIsMatching.test_valid_matching  s)    MM!~~a&&!1222r   c                    t         j                  }t        j                  d      }t        |t         j                  |ddh       t        |t         j                  |ddh       t        |t         j                  |ddh       y )Nr,   r   r*   r-   r   r   r)   r   )r
   NetworkXErrorr   r   r   r   r   r   s      r   test_invalid_inputz!TestIsMatching.test_invalid_input  s_      MM!ubnna&&)9:ubnna)V)<=ubnna$8r   c                    t         j                  }t        j                  d      }t        |t         j                  |ddh       t        j                  |h d      rJ |j                  dd       t        j                  |ddh      rJ y Nr,   )r*   r*   r-   >   r   r   r2   r-   r   r   r2   )r
   r   r   r   r   r   r   s      r   test_selfloopszTestIsMatching.test_selfloops  sq      MM!ubnna&&)9:>>!%=>>>	

1a>>!ff%56666r   c                 b    t        j                  d      }t        j                  |h d      rJ y )Nr,      r   r2   r-   r   r   s     r   test_invalid_matchingz$TestIsMatching.test_invalid_matching  s(    MM!>>!%=>>>>r   c                 Z   t        j                  d      }t        j                  |ddh      rJ t        t         j                  t         j                  |dh       t        j
                  |j                        }t        j                  |dh      sJ t        j                  |dh      rJ y )Nr,   r   r+   r2   )r   r   r   r   )r
   r   r   r   r   r   edgesr   s     r   test_invalid_edgez TestIsMatching.test_invalid_edge  s    MM!>>!ff%5666rWI>JJqww~~a&***>>!fX....r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s4    
;(+339	7?/r   r   c                   .    e Zd 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                 :   t         j                  }t        j                  d      }t        |t         j                  |dh       t        |t         j                  |dh       t        |t         j                  |ddh       t        |t         j                  |ddh       y Nr,   r   )r*   r   r   r-   r   )r
   r   r   r   r   r   s      r   r   z(TestIsMaximalMatching.test_invalid_input  x      MM!ub,,a&:ub,,a&:ub,,a)V1DEub,,a$@r   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   r   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,   r   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   r     s"    
CA;77r   r   c                   :    e Zd 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   r   r,   r
   cycle_graphr   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                  }t        j                  d      }t        |t         j                  |dh       t        |t         j                  |dh       t        |t         j                  |ddh       t        |t         j                  |ddh       y r   )r
   r   r   r   r   r   s      r   r   z(TestIsPerfectMatching.test_invalid_input  r   r   c                    t         j                  }t        j                  d      }t        |t         j                  |ddh       t        j                  |h d      rJ |j                  dd       t        j                  |ddh      rJ y r   )r
   r   r   r   r   r   r   s      r   r   z$TestIsPerfectMatching.test_selfloops  sw      MM!ub,,a&&1AB))!-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   r   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   r     s.    
C;CA	?G
?r   r   c                   .    e Zd 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)r2   )r   r*   r-   )r)   r*   )r+   r,   )r+   rI   )r*   rI   )r
   r   maximal_matchingr   )r   r   r   matchings       r   r   z'TestMaximalMatching.test_valid_matching5  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>I  s     3$!QqAv3s   )r
   r   add_edges_fromr   r   anyr   r   s      r   test_self_loopsz#TestMaximalMatching.test_self_loopsB  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+   r   )r   r)   r   N)	r   ranger
   r   add_nodes_fromr   r   r   r   )r   nodesr   r   s       r   test_orderingz!TestMaximalMatching.test_orderingL  s    
 "%(+ 	7E
AU#ff-.**1-Hx=A%%%))!X666	7r   c                 2   t         j                  }t        |t         j                  t        j                                t        |t         j                  t        j
                                t        |t         j                  t        j                                y r   )r
   r   r   r   r   r   r   r   s     r   r   z)TestMaximalMatching.test_wrong_graph_typeY  sV    ))ub))2==?;ub))2??+<=ub))2::<8r   N)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   /  s     
33379r   r   )r8   	itertoolsr   pytestr   networkxr
   networkx.algorithms.matchingr   networkx.utilsr   r   r   r   r   r   r   r   r   <module>r     sO     "   = &F< F<R</ </~!7 !7H5? 5?p.9 .9r   