o
    3ήc;                     @   s  d dl Z d dlZedZd dlZd dlmZmZ ej	g dg dg dg dg dg d	g d
g dg dg	e
dZdd Zdd Zg dZg dZg dZg dZg dZg dZdd ZdVddZdVddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@dA Z'dBdC Z(dDdE Z)dFdG Z*dHdI Z+dJdK Z,dLdM Z-dNdO Z.dPdQ Z/dRdS Z0dTdU Z1dS )W    Nnumpy)
branchingsrecognition)	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   )dtypec                  C   s   t jtt jd} | S )Ncreate_using)nxfrom_numpy_arrayG_arrayMultiDiGraphG r   U/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/tree/tests/test_branchings.pyG1"   s   r   c                  C   s2   t  } | t|   d8  < tj| tjd}|S )N
   r   )r   copynpnonzeror   r   r   )Garrr   r   r   r   G2'   s   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-   )r1   )r-   r&   	   r4   r&   r)   r+   r.   r2   r$   r
   r&   ))r   r"   r"   r2   r3   r7   r4   r6   r0   r1   )r
   r)   r   r)   r"   r	   r*   r,   r#   r%   r&   r   r
   r(   )r,   r-   r)   r   r9   r#   r%   r'   r:   r(   c                 C   s,   t  }| D ]\}}}|j|||d q|S )Nweight)r   DiGraphadd_edge)edgesr   uvr=   r   r   r   build_branching}   s   rC   r=   r$   c                    s0    fdd| j ddD }t|dd d}|S )Nc                    s$   g | ]\}}}|||  fqS r   )get).0rA   rB   dataattrdefaultr   r   
<listcomp>   s   $ z sorted_edges.<locals>.<listcomp>TrF   c                 S   s   | d | d | d fS )Nr"   r$   r   r   )xr   r   r   <lambda>   s    zsorted_edges.<locals>.<lambda>)key)r@   sorted)r   rH   rI   r@   r   rG   r   sorted_edges   s   rP   c           
      C   s   t | jdd}t |jdd}t|t|ksJ t| ||}t|||}t||D ]\}}	|d d |	d d ks=J tj|d |	d  q+d S )NTrK   r"   )listr@   lenrP   zipr   testingassert_almost_equal)
r   r    rH   rI   edges1edges2e1e2abr   r   r   assert_equal_branchings   s   r\   c                  C   0   t t} t| sJ dt| dksJ d S )NT   )rC   optimal_arborescence_1r   is_arborescencer   branching_weightr   r   r   r   test_optimal_branching1      rb   c                  C   r]   NT5   )rC   optimal_branching_2ar   r`   r   ra   r   r   r   r   test_optimal_branching2a   rc   rg   c                  C   r]   rd   )rC   optimal_branching_2br   r`   r   ra   r   r   r   r   test_optimal_branching2b   rc   ri   c                  C   r]   )NT3   )rC   optimal_arborescence_2r   r`   r   ra   r   r   r   r   test_optimal_arborescence2   rc   rl   c                  C   r]   )NT   )rC   greedy_subopt_branching_1ar   r`   r   ra   r   r   r   r   "test_greedy_suboptimal_branching1a   rc   ro   c                  C   r]   )NT   )rC   greedy_subopt_branching_1br   r`   r   ra   r   r   r   r   "test_greedy_suboptimal_branching1b   rc   rr   c                  C   &   t  } t| }tt}t|| d S N)r   r   greedy_branchingrC   rq   r\   )r   BB_r   r   r   test_greedy_max1   s   
rx   c                  C   sD   t  } | d d d d= tj| dd}g d}t|}t|| d S )Nr$   r   r=   r)   rI   ))r$   r   r)   r'   r;   r#   r(   r,   r%   r9   r   r   ru   rC   r\   r   rv   r@   rw   r   r   r   test_greedy_max2   s   
r|   c                  C   s6   t  } tj| d d}g d}t|}t||dd d S )N)rH   ))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$   ry   rz   r{   r   r   r   test_greedy_max3   s
   
r}   c                  C   s2   t  } tj| dd}g d}t|}t|| d S )Nmin)kind)r$   r   r   r!   r   r   r   r"   r
   r   r   r-   r   r
   r+   r   r8   )r-   r&   r   rz   r{   r   r   r   test_greedy_min   s
   
r   c                  C   rs   rt   )r   r   maximum_branchingrC   r_   r\   r   rL   x_r   r   r   test_edmonds1_maxbranch     
r   c                  C   rs   rt   )r   r   maximum_spanning_arborescencerC   r_   r\   r   r   r   r   test_edmonds1_maxarbor  r   r   c                  C   rs   rt   )r    r   r   rC   rf   r\   r   r   r   r   test_edmonds2_maxbranch  r   r   c                  C   rs   rt   )r    r   r   rC   rk   r\   r   r   r   r   test_edmonds2_maxarbor  r   r   c                  C   s.   t  } t| }g d}t|}t|| d S )N)r:   r!   r   r   r   r   r8   r#   )r   r   minimum_spanning_arborescencerC   r\   r   rL   r@   r   r   r   r   test_edmonds2_minarbor#  s
   

r   c                  C   s*   t  } t| }g }t|}t|| d S rt   )r   r   minimum_branchingrC   r\   r   r   r   r   test_edmonds3_minbranch16  s
   
r   c                  C   s<   t  } | jdddd t| }dg}t|}t|| d S )Nr+   r5   r<   )r+   r5   r   )r   r?   r   r   rC   r\   r   r   r   r   test_edmonds3_minbranch2>  s   
r   c                  C   s  t  } 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}| | |  } t| }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}| t|ksuJ |jddD ]\}}}||h|d f|v sJ q{d S )Nr   r&   r=   r
   r$   )r=   r-   )r=   r5   r"   )r=   r+   r   )r=   r   r
   r=   r)   r)   )r=   r/   r-   r5   TrK   r=   )	r   Graphadd_edges_fromto_directedr   r   number_of_edgesrR   r@   )r   edgelistrL   r@   rA   rB   dr   r   r   test_mstJ  s6   


















	r   c                  C   s@   t  } dddgfdddgfg}| | |  } t| }d S )Nr   r&   r   1)r   r   r   r   r   r   )r   r   rL   r   r   r   test_mixed_nodetypesm  s
   
r   c                  C   s\   dd t D } tjt tjd}t|}tg }t|| t	|}t| }t|| d S )Nc                 S   s   g | ]\}}}||| fqS r   r   )rE   rA   rB   wr   r   r   rJ   y  s    z+test_edmonds1_minbranch.<locals>.<listcomp>r   )
r_   r   r   r   r>   r   r   rC   r\   r   )r@   r   rL   r   r   r   r   test_edmonds1_minbranchv  s   


r   c                  C   s   t  } ddg dfddg dfddg dfg}| | t| }|jdddd	}|d d d
 dks6J |d d d dksBJ d S )Nr   r$   r   )	otherattrr$   )
otherattr2r&   r"   r   )r   r"   )r   r"   r   )r   r&   )r   r$   r=   T)preserve_attrsseedr   r   r&   )r   r   r   r   Edmondsfind_optimumr   r   edrv   r   r   r   -test_edge_attribute_preservation_normal_graph  s   

r   c                  C   s   t  } ddg dfddg dfddg dfg}| |d  t| }|jddd	}|d d d d
 dks9J |d d d d dksGJ d S )Nr   r$   r   r"   r   r   r=   Tr   r   r   r&   )r   
MultiGraphr   r   r   r   r   r   r   r   +test_edge_attribute_preservation_multigraph  s   
 r   c                  C   s   t  } ddg dfddg dfddg dfg}| | t| }|jddd	}|d d }tt |d
 }W d    d S 1 sDw   Y  d S )Nr   r$   r   r"   r   r   r=   Fr   r   )	r   r   r   r   r   r   pytestraisesKeyError)r   r   r   rv   	edge_dict_r   r   r   test_edge_attribute_discard  s   


"r   c                  C   s   t jtt j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< t jj| d d d< g d}tj| dd	}t	t
|| d
S )zi
    Test that we can generate minimum spanning arborescences which respect the
    given partition.
    r   r&   r   	partitionr"   r-   r)   )r   r   r'   r%   r   r8   r   r9   )r   N)r   r   r   r>   EdgePartitionEXCLUDEDINCLUDEDr   r   r\   rC   )r   actual_edgesrv   r   r   r   $test_partition_spanning_arborescence  s   r   c                  C   s`   t jtt jd} d}tj }t| D ]}|d7 }|jdd}||ks%J |}q|dks.J dS )a  
    Tests the arborescence iterator.

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

    * The iterator returns 680 arboresecences
    * 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$   r=   r<     N	r   r   r   r>   mathinfr   ArborescenceIteratorsizer   arborescence_countarborescence_weightrv   new_arborescence_weightr   r   r   test_arborescence_iterator_min  s   r   c                  C   sb   t jtt jd} d}tj}tj| ddD ]}|d7 }|jdd}||ks&J |}q|dks/J d	S )
a  
    Tests the arborescence iterator.

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

    * The iterator returns 680 arboresecences
    * 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$   r=   r<   r   Nr   r   r   r   r   test_arborescence_iterator_max  s   r   c                  C   s   t jtt jd} g d}g d}d}tj }tj| ||fdD ],}|d7 }|jdd}||ks1J |}|D ]	}||j	v s>J q5|D ]	}||j	vsJJ qAq|d	ksRJ d
S )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)   )r+   r-   ))r   r"   )r&   r)   )r$   r
   r   )init_partitionr$   r=   r<      N)
r   r   r   r>   r   r   r   r   r   r@   )r   included_edgesexcluded_edgesr   r   rv   r   er   r   r   ,test_arborescence_iterator_initial_partition  s$   	
r   )r=   r$   )2r   r   importorskipr   networkxr   networkx.algorithms.treer   r   arrayintr   r   r    r_   rf   rh   rk   rn   rq   rC   rP   r\   rb   rg   ri   rl   ro   rr   rx   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    
			


#	