o
    3ήcE@                     @   s  d dl Zd dlZd dlZd dlmZmZ d dlm	Z	 d dl
m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d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d'd( Zd)d* Zd+d, Zd-d. Z d/d0 Z!d1d2 Z"d3d4 Z#d5d6 Z$d7d8 Z%d9d: Z&dS );    N)EdgeComponentAuxGraphbridge_components)general_k_edge_subgraphspairwisec                 C   s   t tt| S )z%allows == to be used for list of sets)setmap	frozenset)list_of_sets r   c/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/connectivity/tests/test_edge_kcomponents.pyfset   s   r   c                 C   s:   |D ]}|  |}t|dkrt|}||ksJ qdS )z
    tests properties of k-edge-connected subgraphs

    the actual edge connectivity should be no less than k unless the cc is a
    single node.
       N)subgraphlennxedge_connectivity)Gccs_subgraphkccCconnectivityr   r   r   "_assert_subgraph_edge_connectivity   s   

r   c                 C   sV   ||f}||v r|| S |   s||f}||v r|| S tj| g|R  ||< || S N)is_directedr   r   )r   uvmemoedgeredger   r   r   _memo_connectivity"   s   r!   c                 C   sP   t |dD ]\}}t| |||}|  rt|t| |||}||ks%J qd S )N   )itcombinationsr!   r   min)r   r   r   r   r   r   r   r   r   r   _all_pairs_connectivity.   s   r&   c                 C   sD   |D ]}t |dkr| |}t|}||k rt| ||| qdS )z
    tests properties of k-edge-connected components

    the local edge connectivity between each pair of nodes in the original
    graph should be no less than k unless the cc is a single node.
    r   N)r   r   r   r   r&   )r   	ccs_localr   r   r   r   r   r   r   r   "_assert_local_cc_edge_connectivity8   s   

r(   c           
      C   sv  t | }i }tdD ]}t||}t||}t| ||| t| || |dks5|dkr=| 	 s=||ks=J d| 	 r]|dkr\tt
| }||ksTJ d||ks\J dn6|dkrytt
| }||kspJ d||ksxJ dn|dkrtt| }||ksJ d||ksJ dtdd t| |dD }	|	|ksJ d	|dkrtd
d |D r dS qdS )z
    Helper - generates all k-edge-components using the aux graph.  Checks the
    both local and subgraph edge connectivity of each cc. Also checks that
    alternate methods of computing the k-edge-ccs generate the same result.
    r   r"   zXSubgraphs and components should be the same when k == 1 or (k == 2 and not G.directed())zk=1 failed altzk=2 failed altc                 S   s   g | ]}t | qS r   )r   nodes).0r   r   r   r   
<listcomp>z   s    z,_check_edge_connectivity.<locals>.<listcomp>r   zalt subgraph method failedc                 s   s    | ]	}t |d kV  qdS )r   N)r   )r*   r   r   r   r   	<genexpr>   s    z+_check_edge_connectivity.<locals>.<genexpr>N)r   	constructr#   countr   k_edge_componentsk_edge_subgraphsr(   r   r   r   strongly_connected_componentsconnected_componentsr   r   all)
r   	aux_graphr   r   r'   r   alt_sccsalt_ccsalt_bridge_ccsalt_subgraph_ccsr   r   r   _check_edge_connectivityK   sD   
r:   c                  C   s   t  } tjtt j| dd tjtt j| dd t| }ttt	|jdd ttt	|jdd ttt	t
| dd d S )Nr   r,   )r   Graphpytestraises
ValueErrorr0   r1   r   r.   listr   )r   r5   r   r   r   test_zero_k_exception   s   
r@   c                  C   s|   t  } g tt j| ddksJ g tt j| ddksJ t  } g tt j| ddks/J g tt j| ddks<J d S )N   r,   )r   r;   r?   r0   r1   DiGraphr   r   r   r   test_empty_input   s   rD   c                  C   s   t  } tt jtj|  tjt jt j| dd tjt jt j| dd tt j t	t
|  W d    n1 s:w   Y  tt j t	t
t   W d    d S 1 sYw   Y  d S )Nr"   r,   )r   
MultiGraphr<   r=   NetworkXNotImplementedr   r.   r0   r1   nextr   rB   rC   r   r   r   test_not_implemented   s   "rH   c                  C   s   t  } | d tt| dd}t|dksJ |D ]
}| dks%J q| d tt| dd}t|dks;J |D ]
}| dksGJ q=d S )Nr   r   r,   r"   )r   r;   add_noder?   r   r   number_of_nodes)r   	subgraphsr   r   r   r   )test_general_k_edge_subgraph_quick_return   s   

rL   c                  C   s.   ddg} | D ]}t jdd|d}t| qd S )N         皙?seedr   gnp_random_graphr:   seedsrR   r   r   r   r   test_random_gnp   s
   
rW   c                  C   sR   ddg} | D ] }t jd|dd}t t j||d}|t | t| qd S )N      rO     rR   triesrQ   )r   random_powerlaw_tree_sequencer;   configuration_modelremove_edges_fromselfloop_edgesr:   rV   rR   deg_seqr   r   r   r   test_configuration   s   
rc   c                  C   s2   dg} | D ]}ddg}t j||d}t| qd S )NrO   rM   F   g?rY   (   g333333?rQ   )r   random_shell_graphr:   rV   rR   constructorr   r   r   r   
test_shell   s   
rk   c                  C   s   t  } t|  d S r   )r   karate_club_graphr:   rC   r   r   r   test_karate   s   rm   c                  C   s:   g d} g d}t tjdd | | D  }t| d S )N)r   r"         r   ro   )rA         rA      	   
   rt   )         rY   rw   )   rM   rX   rN   rz   rX   )ro   rt   rp   rA   )rp   rw   c                 s       | ]}t |V  qd S r   r   r*   pathr   r   r   r-          z%test_tarjan_bridge.<locals>.<genexpr>)r   r;   r#   chainr:   )ccsbridgesr   r   r   r   test_tarjan_bridge   s   r   c               
   C   s   g d} g d}t tjdd | | D  }tt|}th ddhh dh dd	hd
hdhdhdhg	}||ks<J t| d S )N)rn   rs   rz   rM   rN   rz   )r{   r|   )rO      )         c                 s   r}   r   r   r~   r   r   r   r-      r   z!test_bridge_cc.<locals>.<genexpr>>   r   r"   rp   ro   rA   >   rt   ru   rv   >   rz   rM   rN   rO   r   r   r   r   )r   r;   r#   r   r   r   r:   )cc2r   r   
bridge_ccs
target_ccsr   r   r   test_bridge_cc   s   ,r   c               
   C   s  d\	} }}}}}}}}| ||||f| ||f| |||||| f||f|||f||fg}	t tjdd |	D  }
t|
}t|jdd}t| ||||||h||hg}||ksXJ tt j|
dd}||ksgJ t|jdd}t| ||||||h|h|hg}||ksJ tt j|
dd}||ksJ t|jdd}t| h||||h|h|h|h|hg}||ksJ t|jdd}t| h|h|h|h|h|h|h|h|hg	}||ksJ t|
 d S )	N	abcdefghic                 S      g | ]}t |qS r   r   r~   r   r   r   r+         z-test_undirected_aux_graph.<locals>.<listcomp>r   r,   r"   rp   ro   )	r   r;   r#   r   r   r.   r   r1   r:   )abcdefghipathsr   r5   components_1target_1alt_1components_2target_2alt_2components_3target_3components_4target_4r   r   r   test_undirected_aux_graph  s6   
 &,r   c                  C   s   g d} t tjdd | D  }t|}t|d}tdhdhdhdhh d	h d
g}||ks5J t|d}tdhdhdhdhh dg}||ksQJ d S )N)rz   rM   rN   rX   rz   rN   rX   rM   r   r   r   r   r   r   r   r   )rz   e   r   )rM   f   r   )rN   g   r   )rX   h   r   c                 S   r   r   r   r~   r   r   r   r+   5  r   z2test_local_subgraph_difference.<locals>.<listcomp>rp   r   r   r   r   >   r   r   r   r   >   rz   rM   rN   rX   >   rz   rM   rN   rX   r   r   r   r   )	r   r;   r#   r   r   r.   r   r1   r0   )r   r   r5   subgraph_ccssubgraph_target	local_ccslocal_targetr   r   r   test_local_subgraph_difference+  s   	
r   c                  C   s   ddg} t tjdd | D  }tt j|ddtt j|ddks%J tt j|ddtt j|ddks9J tt j|ddtt j|ddksMJ t| d S )	N)r   r"   rp   ro   r   )r   rp   r   c                 S   r   r   r   r~   r   r   r   r+   H  r   z;test_local_subgraph_difference_directed.<locals>.<listcomp>r   r,   r"   rp   )r   rB   r#   r   r   r0   r1   r:   )dipathsr   r   r   r   'test_local_subgraph_difference_directedF  s   (((r   c                  C   s   g d} t tjdd | D  }tt j|ddtt j|ddks%J tt j|ddtt j|ddks9J tt j|ddtt j|ddksMJ t| d S )N)r   )r   r   r   r   )rz   r   c                 S   r   r   r   r~   r   r   r   r+   \  r   z"test_triangles.<locals>.<listcomp>r   r,   r"   rp   )r   r;   r#   r   r   r0   r1   r:   )r   r   r   r   r   test_trianglesV  s   (((r   c                  C   s   g d} t tjdd | D  }tt j|dd}tt j|dd}||ks)J t| d }t| d }||dh|v sBJ ||v sHJ ||v sNJ |	ddksWJ t
| d S )	N)r   r   )d   rN   )rM   r   r   )rN      r   )rX   i,  r   c                 S   r   r   r   r~   r   r   r   r+   t  r   z$test_four_clique.<locals>.<listcomp>rp   r,   r   r   r   )r   r;   r#   r   r   r0   r1   r	   uniondegreer:   )r   r   r   rK   clique1clique2r   r   r   test_four_cliqueh  s   r   c                  C   s  t t dt d} g d}| tjdd |D   ttt | 	 dks,J t
t j| ddt
t j| ddks@J t
t j| ddt
t j| ddksTJ t
t j| ddt
t j| ddkshJ t
t j| ddt
t j| ddks|J t|  d S )	NrA   ))r   r   rq   )r"   r   rr   )rp   r   rt   )ro   r   r   c                 S   r   r   r   r~   r   r   r   r+     r   z$test_five_clique.<locals>.<listcomp>ro   rp   r,   rq   )r   disjoint_unioncomplete_graphadd_edges_fromr#   r   r%   dictr   valuesr   r0   r1   r:   )r   r   r   r   r   test_five_clique  s   ((((r   c               
   C   sZ  d\	} }}}}}}}}| ||||f| ||f| |||||| f||f|||f||fg}	t tjdd |	D  }
t|
}t|jdd}t| ||||||h|h|hg}||ksYJ tt |
}||ksfJ t|jdd}t|h|h|h||||h|h| hg}||ksJ t|jdd}t| h|h|h|h|h|h|h|h|hg	}||ksJ d S )Nr   c                 S   r   r   r   r~   r   r   r   r+     r   z+test_directed_aux_graph.<locals>.<listcomp>r   r,   r"   rp   )	r   rB   r#   r   r   r.   r   r1   r2   )r   r   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   r   r   test_directed_aux_graph  s*   
 &,r   c                  C   s.   dg} | D ]}t jddd|d}t| qd S )Nr   rO   rP   T)directedrR   rS   rU   r   r   r   test_random_gnp_directed  s
   
r   c                  C   sP   dg} | D ] }t jd|dd}t t j||d}|t | t| qd S )NC   rO   rZ   r[   rQ   )r   r]   rB   r^   r_   r`   r:   ra   r   r   r   test_configuration_directed  s   
r   c                  C   s6   dg} | D ]}ddg}t j||d }t| qd S )N   rd   rf   rQ   )r   rh   to_directedr:   ri   r   r   r   test_shell_directed  s   
r   c                  C   s   t   } t|  d S r   )r   rl   r   r:   rC   r   r   r   test_karate_directed  s   r   )'	itertoolsr#   r<   networkxr    networkx.algorithms.connectivityr   r   1networkx.algorithms.connectivity.edge_kcomponentsr   networkx.utilsr   r   r   r!   r&   r(   r:   r@   rD   rH   rL   rW   rc   rk   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sB    
>
	
	*!" 
	