o
    3ήc2                     @   s&  d Z ddlZddlZejejejejgZej	
deej	
dddd Zej	
deej	
dd	d
gdd Zej	
deej	
dddd Zej	
dedd Zej	
deej	
ddej	
dddd Zej	
dejejejgej	
dg ddd ZG dd dZdS )zCUnit tests for the :mod:`networkx.generators.random_graphs` module.    N	generatordirected)TFc                 C   sB   | dd|d}t |dksJ | dksJ | |ksJ dS )zMIf the edge probability `p` is <=0, the resulting graph should have no edges.
   皙r   r   Nlennumber_of_edgesis_directedr   r   G r   S/tmp/pip-target-vg8gfxp4/lib/python/networkx/generators/tests/test_random_graphs.py-test_gnp_generators_negative_edge_probability   s   r   )r   expected_num_edges)F-   )TZ   c                 C   sB   | dd|d}t |dksJ | |ksJ | |ksJ dS )zKIf the edge probability `p` is >=1, the resulting graph should be complete.r   g?r   Nr   )r   r   r   r   r   r   r   3test_gnp_generators_greater_than_1_edge_probability   s   	r   c                 C   s2   | dd|d}t |dksJ | |ksJ dS )zIIf the edge probability `p` is >0 and <1, test only the basic properties.r   皙?r   N)r   r
   r   r   r   r   test_gnp_generators_basic'   s   r   c                    sF   d}t  fddt|D }t|t| d |d d ks!J dS )zUIf the edge probability `p` is close to 1, the resulting graph should have all edges.d   c                 3   s"    | ]} d ddd  V  qdS )r   gwJ?Tr   N)r	   .0_r   r   r   	<genexpr>4   s    
z7test_gnp_generators_for_p_close_to_1.<locals>.<genexpr>r   g       @N)sumrangeabsfloat)r   runsedgesr   r   r   $test_gnp_generators_for_p_close_to_10   s
   (r"   p)g?皙?c           	         s   d}d  fddt  D }t |D ]'}|  ||d}|jD ]\}}|| |  d7  < |s9|| |  d7  < qqt  D ]*}t  D ]#}||krV|| | dksUJ qEt|| | t| | dkshJ qEq?d	S )
zOTest that gnp generators generate edges according to the their probability `p`.i     c                    s   g | ]}d g  qS )r   r   r   nr   r   
<listcomp>A   s    z8test_gnp_generators_edge_probability.<locals>.<listcomp>r      r   gQ?N)r   r!   r   r   )	r   r#   r   r    edge_countsir   vwr   r&   r   $test_gnp_generators_edge_probability:   s$   &r.   )seedr   r   ))*   Fi  )r0   Ti	  ):  Fi  )r1   Ti	  c                 C   sD   | dd||d}t |dksJ | |ksJ | |ks J dS )z:Test that aliases give the same result with the same seed.r         ?)r/   r   Nr   )r   r/   r   r   r   r   r   r   test_gnp_random_graph_aliasesR   s   	r3   c                   @   sH   e Zd Zdd ZdddZdd	d
Zdd Zdd Zdd Zdd Z	dS )TestGeneratorsRandomc                    s  d}t dd| t jdd|dd t dd| t ddd| t dks)J   dks1J t jddd	d|d
 t dksCJ   dksKJ tjt j	t jddd	dd t ddd| t dkshJ   dkspJ t 
ddd| t dksJ   dksJ t 
ddd| t dksJ   dksJ t dd| t dd|   dksJ t dd|t d   dksJ t dddd|   dksJ t dddd|   dksJ t dddd|   dksJ t dddd|   dksJ   dk sJ t dddd|   dks)J   dk s2J t ddd| t ddd|   dksKJ t dd| tt j	t jdd tt j	t jdd ddg}t || dd  fd!d"}t dd	d| t fd#d$  D dksJ | sJ tt j	t jdd	d| tt j	t jddd| tt j	t jddd| t dd	d|  sJ t jddd%dd&}t jddd%dd& d S )'Nr0   r      Tr   r      r2   r   )triesr/   r   )r7      g        r)      i#  r%   i'  c         ?i,  i  333333?   g      ?   !   )r   r5   r$   )r5   (   r$   c                    sJ    fdd D }|D ]} fdd  |D }t|dks" dS qdS )z
            A tree is a caterpillar iff all nodes of degree >=3 are surrounded
            by at most two nodes of degree two or greater.
            ref: http://mathworld.wolfram.com/CaterpillarGraph.html
            c                       g | ]}  |d kr|qS )r9   degreer   r'   gr   r   r(          zRTestGeneratorsRandom.test_random_graph.<locals>.is_caterpillar.<locals>.<listcomp>c                    rA   r6   rB   )r   nbhrE   r   r   r(      rG   r6   FT)	neighborsr   )rF   
deg_over_3r'   nbh_deg_over_2r   rE   r   is_caterpillar   s   z>TestGeneratorsRandom.test_random_graph.<locals>.is_caterpillarc                    s     fdd D }  |S )z
            A tree is a lobster if it has the property that the removal of leaf
            nodes leaves a caterpillar graph (Gallian 2007)
            ref: http://mathworld.wolfram.com/LobsterGraph.html
            c                    s   g | ]}  |d kr|qS )r)   rB   rD   rE   r   r   r(      rG   zNTestGeneratorsRandom.test_random_graph.<locals>.is_lobster.<locals>.<listcomp>)subgraph)rF   	non_leafs)rM   rE   r   
is_lobster   s   z:TestGeneratorsRandom.test_random_graph.<locals>.is_lobsterc                 3   s    | ]}  |V  qd S NrB   rD   )r   r   r   r      s    z9TestGeneratorsRandom.test_random_graph.<locals>.<genexpr>   )r/   r7   )nxgnm_random_graphdense_gnm_random_graphwatts_strogatz_graphr   r	   connected_watts_strogatz_graphpytestraisesNetworkXErrornewman_watts_strogatz_graphbarabasi_albert_graphcomplete_graphextended_barabasi_albert_graphpowerlaw_cluster_graphrandom_regular_graphrandom_shell_graphrandom_lobstermaxnodesrandom_powerlaw_tree_sequencerandom_powerlaw_tree)selfr/   constructorrP   seqr   )r   rM   r   test_random_graphb   sv   	$z&TestGeneratorsRandom.test_random_graphr)   r8   r;   c              	   C   s  g d}t d}|D ]}t d||}t d||d|}| | ks'J t d||}	t d||d|}
|	 |
 ksAJ t d||}t d||||}| | ks[J t d|||||}t d|||}t d|||}	t| |	 |   krt| |	 ksJ  J qt j}t	t j
||||d t	t j
||||d t	t j
|d||d t	t j
|d||d t t||d }tj	t j
|d||||d d	S )
z
        Tests that the dual BA random graph generated behaves consistently.

        Tests the exceptions are raised as expected.

        The graphs generation are repeated several times to prevent lucky shots

        r0   r1   i
  r   r   r)   r   g      g      ?)initial_graphN)rS   r]   r\   dual_barabasi_albert_graphr!   sizeminrc   rX   rY   rZ   )rg   m1m2r#   seedsrl   r/   BA1DBA1BA2DBA2BA3DBA3DBAdbaginitialr   r   r   test_dual_barabasi_albert   s4   	
z.TestGeneratorsRandom.test_dual_barabasi_albertr6   c           
      C   s  g d}|D ]_}t d||}| }t d|dd|}| |ks$J t d|dd|}| |d ks7J t d|dd|}| |ksHJ t d|dd|}| | ks[J | | k seJ qt j}	tt j|	||dd tt j|	dddd tt j|	dddd d	S )
z
        Tests that the extended BA random graph generated behaves consistently.

        Tests the exceptions are raised as expected.

        The graphs generation are repeated several times to prevent lucky-shots

        rk   r   r   r$   r6   r<   r)   r;   N)rS   r\   r	   r^   rn   rX   rY   rZ   )
rg   mrr   r/   BA_modelBA_model_edgesG1G2G3ebagr   r   r   test_extended_barabasi_albert   s"   	z2TestGeneratorsRandom.test_extended_barabasi_albertc                 C   s6   d}t dd|}t|dksJ | dksJ dS )zYTests that a 0-regular graph has the correct number of nodes and
        edges.

        r0   r   r   N)rS   r`   r   r	   )rg   r/   r   r   r   r   test_random_zero_regular_graph  s   z3TestGeneratorsRandom.test_random_zero_regular_graphc                 C   s   t dd}t|dksJ | dksJ t jdddd}t|dks&J | dks.J t dd}t|dks<J | dksDJ t jdddd}t|dksTJ | d	ks\J t dd
}t|dksjJ | dksrJ d S )Nr   r9   r0   r/   r   r   Tr   r   r   r   )rS   rT   r   r	   )rg   r   r   r   r   test_gnm#  s   zTestGeneratorsRandom.test_gnmc                 C   sx   t tjtjddd t tjtjddd tjddddd tjddddd tjddddd tjddddd d S )Nr      r2   	   r   r   r;   )rX   rY   rS   rZ   rV   r[   )rg   r   r   r   test_watts_strogatz_big_k8  s   z.TestGeneratorsRandom.test_watts_strogatz_big_kc                    sP    fdd} fdd}d t d||}t jd||dd}t|dks&J d S )	Nc                    s    ||  S rQ   r   )ur-   zcr   r   integralG     z?TestGeneratorsRandom.test_random_kernel_graph.<locals>.integralc                    s   |  | S rQ   r   )r   r-   rr   r   r   rootJ  r   z;TestGeneratorsRandom.test_random_kernel_graph.<locals>.rootr)   i  r0   r   )rS   random_kernel_graphr   )rg   r   r   graphr   r   r   test_random_kernel_graphF  s   z-TestGeneratorsRandom.test_random_kernel_graphN)r)   r8   r;   rH   )
__name__
__module____qualname__rj   r|   r   r   r   r   r   r   r   r   r   r4   a   s    
c
.&
r4   )__doc__rX   networkxrS   gnp_random_graphfast_gnp_random_graphbinomial_grapherdos_renyi_graph_gnp_generatorsmarkparametrizer   r   r   r"   r.   r3   r4   r   r   r   r   <module>   sF    

	