
    VhCE                        d dl Z d dlmZ d dlZd dlZd dlmZmZ  ej                  d      Z
e
j                  g dg dg dg dg d	g d
g dg dg dg	e      Ze
j                  e
j                  ej                        dz  e      Zeedej                  d    dej                  d   f<   eeej                  d    dej                  d   df<   d Zd Zg dZg dZg dZg dZg dZg dZd;dZd<dZd<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/d0 Z0d1 Z1d2 Z2d3 Z3d4 Z4d5 Z5d6 Z6d7 Z7d8 Z8d9 Z9d: Z:y)=    N)
itemgetter)
branchingsrecognitionnumpy)	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      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      )	r   r   r      r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )dtype      c                  X    t        j                  t        t         j                        } | S Ncreate_using)nxfrom_numpy_arrayG_arrayMultiDiGraphGs    ^/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/tree/tests/test_branchings.pyG1r   '   s    
G"//BAH    c                      t         j                         } | t        j                  |       xx   dz  cc<   t	        j
                  | t        j                        }|S )N
   r   )r   copynpnonzeror   r   r   )Garrr   s     r   G2r'   ,   sC     <<>DD	b 
Dr?AHr    )r   r   r   r   r   r
   r      r   r   r   r	   r+   r   r
   )r+      r   r.      r   r0      r   ))r   r.   r   r.   r      r.   r0   r   r0   r2   r0   r   r   r2   r   r+   r4   r+   r   r2   )r6   )r2   r+   	   r9   r+   r.   r0   r3   r7   r   r   r+   ))r   r   r   r7   r8   r<   r9   r;   r5   r6   )r   r.   r   r.   r   r   r/   r1   r)   r*   r+   r   r   r-   )r1   r2   r.   r   r>   r)   r*   r,   r?   r-   c                     t        j                         }| D ]7  \  }}}|j                  |||       |s|j                  |dz   |dz   |       9 |S )Nweightr:   )r   DiGraphadd_edge)edgesdoubler   uvrC   s         r   build_branchingrJ      sX    


A 41f	

1a
'JJq1ua!eFJ34 Hr    c                     | j                  d      D cg c]  \  }}}|||j                  ||      f }}}}t        |d       }|S c c}}}w )NTdatac                     | d   | d   | d   fS )Nr   r   r    )xs    r   <lambda>zsorted_edges.<locals>.<lambda>   s    1qtQqT(: r    )key)rF   getsorted)r   attrdefaultrH   rI   rM   rF   s          r   sorted_edgesrW      sS    ABdASTT!QaDHHT7+,TET5:;EL Us    Ac                 d   t        | j                  d            }t        |j                  d            }t        |      t        |      k(  sJ t        | ||      }t        |||      }t	        ||      D ]8  \  }}	|d d |	d d k(  sJ t
        j                  j                  |d   |	d          : y )NTrL   r   )listrF   lenrW   zipr$   testingassert_almost_equal)
r   r'   rU   rV   edges1edges2e1e2abs
             r   assert_equal_branchingsrd      s    "(((%&F"(((%&Fv;#f+%%% 
b$	(B	b$	(BB 31!u"1~~


&&qtQqT23r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y )NT   )rJ   optimal_arborescence_1r   is_arborescencer   branching_weightr   s    r   test_optimal_branching1rj      s?    ./A&&q)/4/)&&q)S000r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y NT5   )rJ   optimal_branching_2ar   rh   r   ri   r   s    r   test_optimal_branching2aro      ?    ,-A&&q)/4/)&&q)R///r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y rl   )rJ   optimal_branching_2br   rh   r   ri   r   s    r   test_optimal_branching2brs      rp   r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y )NT3   )rJ   optimal_arborescence_2r   rh   r   ri   r   s    r   test_optimal_arborescence2rw      s?    ./A&&q)/4/)&&q)R///r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y )NT   )rJ   greedy_subopt_branching_1ar   rh   r   ri   r   s    r   "test_greedy_suboptimal_branching1ar{      ?    23A&&q)/4/)&&q)S000r    c                      t        t              } t        j                  |       sJ d       t	        j
                  |       dk(  sJ y )NT   )rJ   greedy_subopt_branching_1br   rh   r   ri   r   s    r   "test_greedy_suboptimal_branching1br      r|   r    c                  x    t               } t        j                  |       }t        t              }t        ||       y N)r   r   greedy_branchingrJ   r   rd   )r   BB_s      r   test_greedy_max1r      s2     	A##A&A 
3	4BAr"r    c                      t               } t        j                  t        j                  d      5  t        j                  | d      }d d d        y # 1 sw Y   y xY w)NzUnknown value for `kind`.)matchlolkind)r   pytestraisesr   NetworkXExceptionr   r   )r   r   s     r    test_greedy_branching_kwarg_kindr      sE    
A	r++3N	O 7''67 7 7s   AAc                     t        j                         } | j                  g d       | j                  d      D cg c]  \  }}}|||j	                  dd      f }}}}t        j                  t              5  |j                  t        ddd      d       d d d        t        j                  | d	
      j                  d      }t        |      ddddifddddifddddifgk(  sJ y c c}}}w # 1 sw Y   ZxY w)N))r   r+   r   r   )r+   rb   r   )r   r   r   TrL   rC   r   r   r   )rR   reversemaxr   r   r   r+   rb   r   )r   rD   add_weighted_edges_fromrF   rS   r   r   	TypeErrorsortr   r   r   rY   )r   rH   rI   rM   rF   r   s         r   *test_greedy_branching_for_unsortable_nodesr      s    


AFG?@wwDw?QRR|1daDHHXq)*RER	y	! :

z!Q*D
9:##AE288d8CA7	XqM"	
C(A	
A!}   	 S: :s    C,;C33C<c                      t               } | d   d   d   d= t        j                  | d      }g d}t        |      }t	        ||       y )Nr   r   rC   r.   rV   ))r   r   r.   r,   r@   r)   r-   r1   r*   r>   r   r   r   rJ   rd   r   r   rF   r   s       r   test_greedy_max2r      sN     	A	!Q
8##Aq1A	E 
	BAr"r    c                      t               } t        j                  | d       }g d}t        |      }t	        ||d       y )N)rU   ))r   r   r   )r+   r   r   )r+   r   r   )r   r0   r   )r.   r   r   )r2   r+   r   )r2   r.   r   )r0   r2   r   r   r   r   r   s       r   test_greedy_max3r      s<     	A##AD1A	E 
	BAr1-r    c                  |    t               } t        j                  | d      }g d}t        |      }t	        ||       y )Nminr   )r   r   r   r(   r   r   r   r   r   r   r   r2   r   r   r0   r   r=   )r2   r+   r   r   r   s       r   test_greedy_minr     s8    
A##AE2A	E 
	BAr"r    c                  x    t               } t        j                  |       }t        t              }t        ||       y r   )r   r   maximum_branchingrJ   rg   rd   r   rP   x_s      r   test_edmonds1_maxbranchr   "  s.    
A$$Q'A	/	0BAr"r    c                  x    t               } t        j                  |       }t        t              }t        ||       y r   )r   r   maximum_spanning_arborescencerJ   rg   rd   r   s      r   test_edmonds1_maxarborr   )  .    
A003A	/	0BAr"r    c                      t        j                  t        t         j                        } t	        j
                  |       }g d}t        |d      }t        ||       y )Nr   r?   r(   r   r   r   r   r=   r)   T)rG   )r   r   G_big_arrayrD   r   minimal_branchingrJ   rd   r   s       r   test_edmonds1_minimal_branchingr   0  sG    
KbjjAA$$Q'A	E 
t	,BAr"r    c                  x    t               } t        j                  |       }t        t              }t        ||       y r   )r'   r   r   rJ   rn   rd   r   s      r   test_edmonds2_maxbranchr   B  s.    
A$$Q'A	-	.BAr"r    c                  x    t               } t        j                  |       }t        t              }t        ||       y r   )r'   r   r   rJ   rv   rd   r   s      r   test_edmonds2_maxarborr   I  r   r    c                  x    t               } t        j                  |       }g d}t        |      }t	        ||       y )Nr   )r   r   minimum_spanning_arborescencerJ   rd   r   rP   rF   r   s       r   test_edmonds2_minarborr   P  s6    
A003A	E 
	BAr"r    c                  t    t               } t        j                  |       }g }t        |      }t	        ||       y r   )r   r   minimum_branchingrJ   rd   r   s       r   test_edmonds3_minbranch1r   c  s2    
A$$Q'AE		BAr"r    c                      t               } | j                  ddd       t        j                  |       }dg}t	        |      }t        ||       y )Nr0   r:   rB   )r0   r:   r   )r   rE   r   r   rJ   rd   r   s       r   test_edmonds3_minbranch2r   k  sF    
AJJq!CJ $$Q'AME		BAr"r    c                     t        j                         } dddgfdddgfdddgfdddgfdd	dgfdd	d
gfdddgfdd	dgfd	ddgfd	ddgfdddgfg}| j                  |       | j                         } t	        j
                  |       }ddhdfddhdfddhdfdd	hdfd	dhdfd	dhdfg}|j                         t        |      k(  sJ |j                  d      D ]  \  }}}||h|d   f|v rJ  y )Nr   r+   rC   r   r   )rC   r2   )rC   r:   r   )rC   r0   r   )rC   r   r   rC   r.   r.   )rC   r4   r2   r:   TrL   rC   )	r   Graphadd_edges_fromto_directedr   r   number_of_edgesrZ   rF   )r   edgelistrP   rF   rH   rI   ds          r   test_mstr   w  sj    	
A	
A	
A	
A	
A	
A	
A 	
A	
A	
A	
A	
A H X	A003A Q
Q
Q
Q
Q
QE #e*,,,777% .1aA($---.r    c                      t        j                         } dddgfdddgfg}| j                  |       | j                         } t	        j
                  |       }y )Nr   r+   r   1)r   r   r   r   r   r   )r   r   rP   s      r   test_mixed_nodetypesr     sR    

AA'!S=/)BCHX	A003Ar    c                  T   t         D  cg c]  \  } }}| || f }}} }t        j                  t         t        j                        }t        j                  |      }t        g       }t        ||       t        j                  |      }t        |      }t        ||       y c c}}} w r   )
rg   r   r   r   rD   r   r   rJ   rd   r   )rH   rI   wrF   r   rP   r   s          r   test_edmonds1_minbranchr     s     *@@@IQ1aQBZ@E@
WH2::>A 	$$Q'A		BAr" 	$$Q'A		BAr" As   B#c                      t        j                         } ddg dfddg dfddg dfg}| j                  |       t        j                  | d      }|d   d   d	   dk(  sJ |d   d   d
   dk(  sJ y Nr   r   r   )	otherattrr   )
otherattr2r+   r   r   )r   r   )r   r   r   )r   r+   )r   r   Tpreserve_attrsr   r   r+   )r   r   r   r   r   r   r   r   s      r   -test_edge_attribute_preservation_normal_graphr     s     	
A 
ACD	
ACD	
ACDH
 X$$Qt<AQ47;1$$$Q47< A%%%r    c                      t        j                         } ddg dfddg dfddg dfg}| j                  |dz         t        j                  | d      }|d   d   d   d	   dk(  sJ |d   d   d   d
   dk(  sJ y r   )r   
MultiGraphr   r   r   r   s      r   +test_edge_attribute_preservation_multigraphr     s     	A 
ACD	
ACD	
ACDH
 X\"$$Qt<AQ471:k"a'''Q471:l#q(((r    c                     t        j                         } ddg dfddg dfddg dfg}| j                  |       t        j                  | d      }|d   d   }t        j                  t              5  |d	   }d d d        y # 1 sw Y   y xY w)
Nr   r   r   r   r   r   Fr   r   )r   r   r   r   r   r   r   KeyError)r   r   r   	edge_dict_s        r   test_edge_attribute_discardr     s    

A 
ACD	
ACD	
ACDH
 X$$Qu=A!QI	x	  #k"# # #s   2BB
c                     t        j                  t        t         j                        } t         j                  j
                  | d   d   d<   t         j                  j                  | d   d   d<   t         j                  j
                  | d   d   d<   t         j                  j
                  | d   d   d<   t         j                  j                  | d   d   d<   g d}t        j                  | d	      }t        t        |      |       y
)zi
    Test that we can generate minimum spanning arborescences which respect the
    given partition.
    r   r+   r   	partitionr   r2   r.   )r   r   r,   r*   r   r=   r   r>   )r   N)r   r   r   rD   EdgePartitionEXCLUDEDINCLUDEDr   r   rd   rJ   )r   actual_edgesr   s      r   $test_partition_spanning_arborescencer     s    
 	G"**=A++44AaDGK++44AaDGK++44AaDGK++44AaDGK++44AaDGK	L 	00kJAOL91=r    c                      t        j                  t        t         j                        } d}t        j
                   }t        j                  |       D ]"  }|dz  }|j                  d      }||k\  sJ |}$ |dk(  sJ y)a  
    Tests the arborescence iterator.

    A brute force method found 680 arborescences in this graph.
    This test will not verify all of them individually, but will check two
    things

    * The iterator returns 680 arborescences
    * The weight of the arborescences is non-strictly increasing

    for more information please visit
    https://mjschwenne.github.io/2021/06/10/implementing-the-iterators.html
    r   r   r   rC   rB     N	r   r   r   rD   mathinfr   ArborescenceIteratorsizer   arborescence_countarborescence_weightr   new_arborescence_weights        r   test_arborescence_iterator_minr     s     	G"**=A88),,Q/ 6a"#&&&"9&*====5	6 $$$r    c                      t        j                  t        t         j                        } d}t        j
                  }t        j                  | d      D ]"  }|dz  }|j                  d      }||k  sJ |}$ |dk(  sJ y	)
a  
    Tests the arborescence iterator.

    A brute force method found 680 arborescences in this graph.
    This test will not verify all of them individually, but will check two
    things

    * The iterator returns 680 arborescences
    * The weight of the arborescences is non-strictly decreasing

    for more information please visit
    https://mjschwenne.github.io/2021/06/10/implementing-the-iterators.html
    r   r   F)minimumr   rC   rB   r   Nr   r   s        r   test_arborescence_iterator_maxr   !  s     	G"**=A((,,Q> 6a"#&&&"9&*====5	6 $$$r    c                  r   t        j                  t        t         j                        } g d}g d}d}t        j
                   }t        j                  | ||f      D ]P  }|dz  }|j                  d      }||k\  sJ |}|D ]  }||j                  v rJ  |D ]  }||j                  vrJ  R |d	k(  sJ y
)a$  
    Tests the arborescence iterator with three included edges and three excluded
    in the initial partition.

    A brute force method similar to the one used in the above tests found that
    there are 16 arborescences which contain the included edges and not the
    excluded edges.
    r   ))r   r   )r   r.   )r0   r2   ))r   r   )r+   r.   )r   r   r   )init_partitionr   rC   rB      N)
r   r   r   rD   r   r   r   r   r   rF   )r   included_edgesexcluded_edgesr   r   r   r   es           r   ,test_arborescence_iterator_initial_partitionr  <  s     	G"**=A-N-N88),,	>>: 
$ 	a"#&&&"9&*====5 	 A<<	  	$AAGG###	$
$ ###r    c                  <   t        j                  dddd      } t        d | j                  d      D              sJ d       t        j                  | j                                t        j                  | j                                t        j                  | j                                t        j                  | j                                t         j                  j                  j                  | j                                t         j                  j                  j                  | j                                t         j                  j                  j                  | j                                t        d	 | j                  d      D              sJ d
       y)z|
    Tests that various branching algorithms work on graphs without weights.
    For more information, see issue #7279.
    r"   g?T{   )pdirectedseedc              3   ,   K   | ]  \  }}}d |v  ywrC   NrO   .0rH   rI   r   s       r   	<genexpr>z7test_branchings_with_default_weights.<locals>.<genexpr>`       IYaAxq I   rL   z-test is for graphs without a weight attributec              3   ,   K   | ]  \  }}}d |v  ywr	  rO   r
  s       r   r  z7test_branchings_with_default_weights.<locals>.<genexpr>n  r  r  z<The above calls should not modify the initial graph in-placeN)r   erdos_renyi_graphallrF   r   r#   r   r   r   
algorithmstreer   ri   r   )graphs    r   $test_branchings_with_default_weightsr  Y  s   
   sTDEI%++4+2HII 7I $$UZZ\2$$UZZ\2&&MM((6MM''

5MM''

5I%++4+2HII FIr    )F)rC   r   );r   operatorr   r   networkxr   networkx.algorithms.treer   r   importorskipr$   arrayintr   zerosshaper   r   r'   rg   rn   rr   rv   rz   r   rJ   rW   rd   rj   ro   rs   rw   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  rO   r    r   <module>r     s       <V! ((! "! !!!  
   hhrxx.2#h>4;W]]10a 000 14;GMM!w}}Q/00 1

	 *  	 	 	 3"100011#7#,.*#$###$###&## .F4#$&$)$#$>6%6%6$:r    