o
    hx                     @   s   d dl mZmZmZ d dlmZ d dlmZ d dlZd dl	Z
d dlmZmZ dd ZG dd	 d	Zd
d Zdd Zdd Zdd Zdd ZG dd dZG dd dZdd ZG dd dZG dd dZdS )    )chainislicetee)inf)shuffleN)FORWARDREVERSEc                 C   s   t | dkrd S td}td}t }| D ]}t|| qtj|dd}|jj|	 d dd}|j
d | t | ks@J d S )	Nr   numpyscipyT)orientedF)tol	hermitian   )lenpytestimportorskipnxGraph	add_cycleincidence_matrixlinalgmatrix_ranktoarrayshape)basisnpspHbincrank r!   Y/var/www/vscode/kcb/lib/python3.10/site-packages/networkx/algorithms/tests/test_cycles.pycheck_independent   s   

r#   c                   @   s   e Zd Ze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!S )"
TestCyclesc                 C   sN   t  }t |g d t |g d t |g d |dd || _d S )Nr   r         r   r'         r   r            r.   	   )r   r   r   add_edgeG)clsr1   r!   r!   r"   setup_class   s   
zTestCycles.setup_classc                    s>   t |t  krdS || t fddtD S )NFc                 3   s$    | ]}||   kV  qd S Nr!   .0ir   lnr!   r"   	<genexpr>)   s   " z3TestCycles.is_cyclic_permutation.<locals>.<genexpr>)r   anyrange)selfar   r!   r8   r"   is_cyclic_permutation$   s
   z TestCycles.is_cyclic_permutationc                 C   s"  | j }t|d}tdd |D }|g dg dg dgks!J t|d}tdd |D }|g dg dg dgks?J t|d	}td
d |D }|g dg dg dgks]J t|d t|d	}tdd |d d D t|d g }|g dg dg dg dgksJ d S )Nr   c                 s       | ]}t |V  qd S r4   sortedr6   cr!   r!   r"   r;   .       z.TestCycles.test_cycle_basis.<locals>.<genexpr>r%   r+   r(   r   c                 s   rA   r4   rB   rD   r!   r!   r"   r;   1   rF   r/   c                 s   rA   r4   rB   rD   r!   r!   r"   r;   4   rF   ABCc                 s   rA   r4   rB   rD   r!   r!   r"   r;   9   rF   )ABC)r1   r   cycle_basisrC   r   r>   r1   cysort_cyr!   r!   r"   test_cycle_basis+   s   ((zTestCycles.test_cycle_basisc                 C   F   t tj t }t|d}W d    d S 1 sw   Y  d S Nr   )r   raisesr   NetworkXNotImplementedDiGraphrL   r>   r1   rN   r!   r!   r"   test_cycle_basis2<      "zTestCycles.test_cycle_basis2c                 C   rQ   rR   )r   rS   r   rT   
MultiGraphrL   rV   r!   r!   r"   test_cycle_basis3A   rX   zTestCycles.test_cycle_basis3c                    sh   t d}|t tdd t |}ddd t | } fddt |D }||ks2J d S )	Nr*   r'   r.   r   r   r   r   c                    s   g | ]} fd d|D qS )c                    s   g | ]}  ||qS r!   )getr6   r:   permr!   r"   
<listcomp>N   s    zBTestCycles.test_cycle_basis_ordered.<locals>.<listcomp>.<listcomp>r!   )r6   cycr^   r!   r"   r`   N   s    z7TestCycles.test_cycle_basis_ordered.<locals>.<listcomp>)r   cycle_graphupdater=   rL   relabel_nodes)r>   r1   cbGr   cbHr!   r^   r"   test_cycle_basis_orderedF   s   


z#TestCycles.test_cycle_basis_orderedc                 C   sj   t  }t |g d t |g d t |}tdd |D }|dgg dg dg dgks3J d	S )
z-Tests the function for graphs with self loopsr%   )r   r   r,   r&   c                 s   rA   r4   rB   rD   r!   r!   r"   r;   W   rF   z8TestCycles.test_cycle_basis_self_loop.<locals>.<genexpr>r   r   r   r&   )r   r&   r'   )r   r&   r,   N)r   r   r   rL   rC   rM   r!   r!   r"   test_cycle_basis_self_loopQ   s   
&z%TestCycles.test_cycle_basis_self_loopc                    s|   g d}t |}tt |}dgg dddgddgdgg}t|t|ks)J |D ] t fdd|D s;J q+d S )N)r   r   r   r   r   r&   r   r&   r&   r   r&   r   )r&   r&   r   rh   r&   r   c                 3       | ]	}  |V  qd S r4   r@   r6   rcrE   r>   r!   r"   r;   a       z0TestCycles.test_simple_cycles.<locals>.<genexpr>)r   rU   rC   simple_cyclesr   r<   )r>   edgesr1   cccar!   rt   r"   test_simple_cyclesZ   s   
zTestCycles.test_simple_cyclesc                 C   s*   t dg}tt |dggksJ d S )Nrj   r   )r   r   listrv   r>   r1   r!   r!   r"   test_simple_cycles_singletonc   s   z'TestCycles.test_simple_cycles_singletonc                 C   s:   t  }t |ddg tt |}t|dksJ d S )Nr?   r   )r   rU   r   r{   rv   r   r>   r1   rE   r!   r!   r"   test_unsortableg   s   zTestCycles.test_unsortablec                    s   t  }t |g d tt | t dksJ  d g ds'J t |g d tt |}t|dks>J g dg dg}|D ] t fdd|D sXJ qHd S )N)r   r&   r'   r   r   )
         r&   c                 3   rp   r4   rq   rr   rt   r!   r"   r;   z   ru   z6TestCycles.test_simple_cycles_small.<locals>.<genexpr>)r   rU   r   rC   rv   r   r@   r<   )r>   r1   rx   ry   r!   rt   r"   test_simple_cycles_smallo   s   z#TestCycles.test_simple_cycles_smallc                 C   s"   t  }tt |g ksJ d S r4   )r   rU   r{   rv   r|   r!   r!   r"   test_simple_cycles_empty|   s   z#TestCycles.test_simple_cycles_emptyc                 C   s  t  }td|d D ]}|d| |||d  q|d| d d t|d d| d D ]}||d| d  |||d  q1|d| d |d  td| d d| d D ]}|d| d | ||d| d  q_|d| d d| d  |S )Nr&   r   r'   )r   rU   r=   r0   )r>   kr1   r:   r!   r!   r"   worst_case_graph   s   zTestCycles.worst_case_graphc                 C   s@   t ddD ]}| |}ttt|}|d| ksJ qd S )Nr'   r   )r=   r   r   r{   r   rv   )r>   r   r1   r9   r!   r!   r"   test_worst_case_graph   s
   
z TestCycles.test_worst_case_graphc                    s   t ddD ]E}|}tt|}tt|}t|t|ks$J |D ] t fdd|D s6J q&|D ]tfdd|D sIJ q9qd S )Nr&   r   c                 3   rp   r4   rq   )r6   rrt   r!   r"   r;      ru   z;TestCycles.test_recursive_simple_and_not.<locals>.<genexpr>c                 3   rp   r4   rq   rD   rs   r>   r!   r"   r;      ru   )r=   r   rC   r   rv   recursive_simple_cyclesr   r<   )r>   r   r1   rx   rccr!   rE   rs   r>   r"   test_recursive_simple_and_not   s   
z(TestCycles.test_recursive_simple_and_notc                    s   t  }g d}|| tt |}t|dksJ tt |}t|t|ks-J |D ] t fdd|D s?J q/|D ]tfdd|D sRJ qBd S )N)rl   )r   r'   r[   )r   r'   ro   )r&   r)   r'   r&   r'   r)   )r)   r   )r)   r   r)   r*   r*   r   r*   r   )r*   r&   )r*   r'      c                 3   rp   r4   rq   rr   rt   r!   r"   r;      ru   zATestCycles.test_simple_graph_with_reported_bug.<locals>.<genexpr>c                 3   rp   r4   rq   rD   r   r!   r"   r;      ru   )r   rU   add_edges_fromrC   rv   r   r   r<   )r>   r1   rw   rx   r   r!   r   r"   #test_simple_graph_with_reported_bug   s   
z.TestCycles.test_simple_graph_with_reported_bugN)__name__
__module____qualname__classmethodr3   r@   rP   rW   rZ   rg   ri   rz   r}   r   r   r   r   r   r   r   r!   r!   r!   r"   r$      s$    
		r$   c                 C   s    t | \}}t|d  t||S r4   )r   nextzip)iterabler?   r   r!   r!   r"   pairwise   s   

r   c                 C   s   t t| t| dS Nr   )r   r   r   rE   r!   r!   r"   cycle_edges   s   r   c                 C   s   t t| S r4   )	frozensetr   r   r!   r!   r"   directed_cycle_edgeset   s   r   c                 C   s*   t | dkrtt| S tttt| S r   r   r   r   mapr   r!   r!   r"   undirected_cycle_edgeset   s   r   c                 C   s*   t | dkrtt| S tttt| S )Nr&   r   r   r!   r!   r"   multigraph_cycle_edgeset   s   r   c                   @   s   e Zd Zedd Zedd Zed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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	S ).TestCycleEnumerationc                 C   s
   t | S r4   )r   complete_graphr:   r!   r!   r"   K   s   
zTestCycleEnumeration.Kc                 C   s   t |  S r4   )r   r   to_directedr   r!   r!   r"   D   s   zTestCycleEnumeration.Dc                 C   s   |   rtS |  rtS tS r4   )is_directedr   is_multigraphr   r   gr!   r!   r"   edgeset_function   s
   z%TestCycleEnumeration.edgeset_functionc	           	         s   |d urt ||krtd| d| |dkr+||v r$td| dt|||< n||v r5|| ntd| dt fdd|D sRt| d	| d
|rjt  |jt |krlt| d| dd S d S )Ncomputed cycle z exceeds length bound computedz has already been found!zexpected cycle z was not computedc                 3   s    | ]} j | V  qd S r4   has_edger6   er   r!   r"   r;     s    z3TestCycleEnumeration.check_cycle.<locals>.<genexpr>z claimed cycle z is not a cycle of gz cycle z is not chordless)r   RuntimeErrortuplepopallsubgraphrw   )	r>   r   rE   escachesource
original_clength_bound	chordlessr!   r   r"   check_cycle   s(   
z TestCycleEnumeration.check_cycleNFc                    s`  |d u r|r	t jnt j}ttt|}t| tt|| tt||t j	| dd}| 
|}i }	|d ur=||	d< i }
||fi |	D ]}fdd|D }||}| ||||
d||| qGt|tr|t|
|krztd| dt|
 d S |D ]} fd	d|D }||}| ||||
d
||| q~t|
r|
 D ]}td| dd S d S )NT)copyr   c                       g | ]} | qS r!   r!   r6   x)unlabelr!   r"   r`   %      z>TestCycleEnumeration.check_cycle_algorithm.<locals>.<listcomp>r   z	expected z cycles, got c                    r   r!   r!   r   )labelr!   r"   r`   2  r   expectedr   z, is valid but not in the expected cycle set!)r   chordless_cyclesrv   r{   r=   r   r   dictr   rd   r   r   
isinstanceintr   values)r>   r   expected_cyclesr   r   	algorithmrelabelhedgesetparamscycle_cacherE   r   r   r!   )r   r   r"   check_cycle_algorithm  sJ   


z*TestCycleEnumeration.check_cycle_algorithmc                 C   s,   t ||D ]\}}| j|||||d qd S )N)r   r   r   )r   r   )r>   g_familycycle_countsr   r   r   r   
num_cyclesr!   r!   r"   (check_cycle_enumeration_integer_sequence>  s   z=TestCycleEnumeration.check_cycle_enumeration_integer_sequencec                 C   s   t  }t |td t |tdd d d  |dd g d}| j||dd | j||ddd d	d
 |D }| j||ddd d S )Nr*   rH   r   ))r   rm   r&   r'   r   Tr   r&   r   r   c                 S   s   g | ]
}t |d k r|qS r&   r   rD   r!   r!   r"   r`   Y      zMTestCycleEnumeration.test_directed_chordless_cycle_digons.<locals>.<listcomp>r   )r   rU   r   r=   r0   r   r>   r   r   r!   r!   r"   $test_directed_chordless_cycle_digonsO  s   z9TestCycleEnumeration.test_directed_chordless_cycle_digonsc                 C   s  t g d}ddg}| j||dd t  }t |td t |tdd |d	d
 g d}| j||dd |d
d	 g d}| j||dd dg}| j||ddd |d	d
 ddg}| j||dd t dd tdD }g }| j||dd d S )N)rm   r   r   r   r   r   rl   )r   r&   r'   r)   r*   )r   r&   r'   r)   r*   Tr   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   c                 s   &    | ]}t |D ]}||fV  qqd S r4   r=   )r6   r7   jr!   r!   r"   r;   s     $ zPTestCycleEnumeration.test_directed_chordless_cycle_undirected.<locals>.<genexpr>r   )r   rU   r   r   r=   r0   remove_edger   r!   r!   r"   (test_directed_chordless_cycle_undirected\  s(   z=TestCycleEnumeration.test_directed_chordless_cycle_undirectedc                 C   sD  t  }t |td t |tdd g tdg tddg}| j||dd | j|dd |D ddd |d	d
 |g td
d | j||dd | j|dd |D ddd |d
d	 d	d
g|d< | j||dd | j|dd |D ddd |  |d	d
 | j||dd | j|dd |D ddd d S )Nr*   r)      Tr   c                 S      g | ]
}t |d kr|qS r*   r   rD   r!   r!   r"   r`   ~  r   zGTestCycleEnumeration.test_chordless_cycles_directed.<locals>.<listcomp>r   r   r-   r'   r.   c                 S   r   r   r   rD   r!   r!   r"   r`     r   rH   c                 S   r   r   r   rD   r!   r!   r"   r`     r   c                 S   r   r   r   rD   r!   r!   r"   r`     r   )	r   rU   r   r=   r   r0   appendr   r   r>   r1   r   r!   r!   r"   test_chordless_cycles_directedw  s4   
z3TestCycleEnumeration.test_chordless_cycles_directedc                    s^    fddt dD }dd t dD } j||dd dd t dD } j||dd	 d S )
Nc                       g | ]}  |qS r!   r   r]   r>   r!   r"   r`         zOTestCycleEnumeration.test_directed_chordless_cycle_diclique.<locals>.<listcomp>r   c                 S      g | ]
}|| | d  qS r   r!   r]   r!   r!   r"   r`     r   Tr   c                 S   r   r   r!   r]   r!   r!   r"   r`     r   r&   r   r=   r   )r>   r   r   r!   r   r"   &test_directed_chordless_cycle_diclique  s   
z;TestCycleEnumeration.test_directed_chordless_cycle_dicliquec                 C   s   t dd tdD }t |td dd tdD }| j||dd | j||dd	 t |}|d
d tdddD  dd tdddD }| j||dd d S )Nc                 s       | ]}||fV  qd S r4   r!   r5   r!   r!   r"   r;     rF   zMTestCycleEnumeration.test_directed_chordless_loop_blockade.<locals>.<genexpr>r   c                 S      g | ]}|fqS r!   r!   r5   r!   r!   r"   r`         zNTestCycleEnumeration.test_directed_chordless_loop_blockade.<locals>.<listcomp>Tr   r   r   c                 s   r   r4   r!   r5   r!   r!   r"   r;     rF   r   r&   c                 S   r   r!   r!   r5   r!   r!   r"   r`     r   )r   rU   r=   r   r   MultiDiGraphr   r   r!   r!   r"   %test_directed_chordless_loop_blockade  s   
z:TestCycleEnumeration.test_directed_chordless_loop_blockadec                    s<   fddt ddD }g d} j||dd dd	 } fd
dt ddD }dd |D } j||d|d dd }g d} fddt ddD } j||d|d dd |D } fddt ddD } j||d|d g d} fddt ddD } || g d} fddt dD } || d S )Nc                    r   r!   r   r]   r   r!   r"   r`     r   zTTestCycleEnumeration.test_simple_cycles_notable_clique_sequences.<locals>.<listcomp>r&   r   )r   r   r)   r   r   #   8   T   x         r'   r   c                 {   (    dd t j| fi |D E d H  d S )Nc                 s        | ]}t |d kr|V  qdS r'   Nr   rD   r!   r!   r"   r;         zfTestCycleEnumeration.test_simple_cycles_notable_clique_sequences.<locals>.triangles.<locals>.<genexpr>r   rv   r   kwargsr!   r!   r"   	triangles     &zSTestCycleEnumeration.test_simple_cycles_notable_clique_sequences.<locals>.trianglesc                    r   r!   r   r]   r   r!   r"   r`     r   c                 S      g | ]}d | qS r   r!   r   r!   r!   r"   r`     r   )r   r   c                 {   r	  )Nc                 s   r
  )r)   Nr   rD   r!   r!   r"   r;     r  zhTestCycleEnumeration.test_simple_cycles_notable_clique_sequences.<locals>.four_cycles.<locals>.<genexpr>r  r  r!   r!   r"   four_cycles  r  zUTestCycleEnumeration.test_simple_cycles_notable_clique_sequences.<locals>.four_cycles)r   r   r   r'      -   i      iz  iv  i  c                    r   r!   r  r]   r   r!   r"   r`     r   r   r)   c                 S   r  r   r!   r   r!   r!   r"   r`     r   c                    r   r!   r   r]   r   r!   r"   r`     r   r  )r   r   r*   r   r  i  i=	  c                    r   r!   r   r]   r   r!   r"   r`     r   r.   )r   r   r   r   r-   %      i  c                    r   r!   r  r]   r   r!   r"   r`     r   r   )r>   r   r   r  r  r!   r   r"   +test_simple_cycles_notable_clique_sequences  s8   z@TestCycleEnumeration.test_simple_cycles_notable_clique_sequencesc                 C   sv  t  }t |td g tdg}| j||dd t |td g ttd}| j||dd t |td g }| j||dd t  }t |td g tdg}| j||dd t |td | j|g dd t |td | j|g dd t  }t |td t |tdd d d  g ttd}| j||dd t |td | j|g dd d S )Nr*   Tr   rH   )r   rY   r   r=   r   r   r   )r>   r   r   r!   r!   r"   1test_directed_chordless_cycle_parallel_multiedges  s2   zFTestCycleEnumeration.test_directed_chordless_cycle_parallel_multiedgesc                 C   s   t  }t |td t |tdd g tdg tddg}| j||dd | j|dd |D ddd |d	d
 |g td
d |g d | j||dd | j|dd |D ddd d S )Nr*   r)   r   Tr   c                 S   r   r   r   rD   r!   r!   r"   r`     r   zDTestCycleEnumeration.test_chordless_cycles_graph.<locals>.<listcomp>r   r-   r'   r.   )r)   r'   r-   r.   r/   r      c                 S   r   r   r   rD   r!   r!   r"   r`     r   )r   r   r   r=   r   r0   r   r   r!   r!   r"   test_chordless_cycles_graph  s   
z0TestCycleEnumeration.test_chordless_cycles_graphc                    sl  d  d dks
J t  }t D ]}|d s"|||d    |||d    qg td dg fddtd dD  }| j||dd | j|d	d |D d
dd d  d dkscJ t  }t D ]}|||d    |d s||d   | qkg t d ddg fddtd dD  }| j||dd | j|dd |D d
dd d S )Ni  r&   r   r   c                    (   g | ]} fd dt ||d D qS )c                       g | ]}|  qS r!   r!   r   r   r!   r"   r`   #  r   [TestCycleEnumeration.test_chordless_cycles_giant_hamiltonian.<locals>.<listcomp>.<listcomp>r'   r   r5   r   r!   r"   r`   "      zPTestCycleEnumeration.test_chordless_cycles_giant_hamiltonian.<locals>.<listcomp>Tr   c                 S   r   r'   r   rD   r!   r!   r"   r`   '  r   r'   r   d   c                    r  )c                    r  r!   r!   r   r   r!   r"   r`   <  r   r   r'   r   r5   r   r!   r"   r`   ;  r!  c                 S   r   r"  r   rD   r!   r!   r"   r`   @  r   )r   r   r=   r0   r   rU   )r>   r1   vr   r!   r   r"   'test_chordless_cycles_giant_hamiltonian  s:   


z<TestCycleEnumeration.test_chordless_cycles_giant_hamiltonianc                 C   sp   d}t dd t|D }| |g  | j|g dd t|d D ]}| j|g |d | j|g |dd q"d S )	Nr   c                 s   r   r4   r   )r6   r   yr!   r!   r"   r;   E  r   zMTestCycleEnumeration.test_simple_cycles_acyclic_tournament.<locals>.<genexpr>Tr   r   r   r   )r   rU   r=   r   )r>   r:   r1   r   r!   r!   r"   %test_simple_cycles_acyclic_tournamentC  s   z:TestCycleEnumeration.test_simple_cycles_acyclic_tournamentc                 C   s   t d}ttd}| ||g |dd t |g d | ||g |t tdd ttdd}| |||g |t tdd ttdd}h d}| || t|dkseJ t d}|t g d	j	 d
}| || d S )Nr.   r)   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  
r   r   r&   r'   r)   r  r   r/   r.   r-   
r)   r*   r,   r-   r.   r  r0  r/  r   r  r   r   r&   r'   r)   r  r   r/  r0  r  r.   r-   r,   )r   r   r/  r&   r0  r)   r  r.   r   )
r   rb   r   r=   r   r0   add_pathrc   r   rw   )r>   testGcyc1cyc2cyc3r   r!   r!   r"   test_simple_cycles_graphM  s$   

z-TestCycleEnumeration.test_simple_cycles_graphc                 C   s   t  }g }tdD ] }t |t| || t|D ]\}}| j|||d qq
t  }d}g }tdD ]4}||dk rA|n|d  |dkrKq7t |t|||  ||7 }t|D ]\}}| j|||d q^q7d S )Nr   r   r   r&   r   )r   rU   r=   r   r   	enumerater   r   )r>   dr   r:   r   r   r   topr!   r!   r"   test_simple_cycles_bounded  s*   
z/TestCycleEnumeration.test_simple_cycles_boundedc                 C   s   t d}t jdt jd}tt j|ddg ksJ tt j|ddg ks'J tt j|ddg ks4J tt j|ddg ksAJ d S )Nr)   )create_usingr   r   )r   rb   rU   r{   rv   r   )r>   r1   DGr!   r!   r"   %test_simple_cycles_bound_corner_cases  s   
z:TestCycleEnumeration.test_simple_cycles_bound_corner_casesc                 C   s  t t t }t|dD ]}J W d    n1 sw   Y  t t t }t|dD ]}J W d    n1 sAw   Y  t t t }t|dD ]}J W d    n1 sdw   Y  t t t }t|dD ]}J W d    d S 1 sw   Y  d S )NrH   )r   rS   
ValueErrorr   rU   rv   r   r   r~   r!   r!   r"   test_simple_cycles_bound_error  s0   "z3TestCycleEnumeration.test_simple_cycles_bound_errorc                    sl    fddt ddD }g d} j||dd dd t dD } fd	dt dD } j||dd d S )
Nc                    r   r!   r  r]   r   r!   r"   r`     r   zETestCycleEnumeration.test_chordless_cycles_clique.<locals>.<listcomp>r&   r  )r   r   r)   r   r   r  r  r  r  r  r  i  il  Tr   c                 S   r   r   r!   r]   r!   r!   r"   r`     r   c                    r   r!   r   r]   r   r!   r"   r`     r   r   )r>   r   r   r!   r   r"   test_chordless_cycles_clique  s   
z1TestCycleEnumeration.test_chordless_cycles_clique)NFN)r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r  r  r  r  r&  r(  r9  r=  r@  rB  rC  r!   r!   r!   r"   r      s>    



:
-%0
3r   c                   @   s   e Zd Ze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%S )&TestFindCyclec                 C   s   g d| _ g d| _d S )Nr%   ))rH   r   rk   r[   r[   ro   r'   r   )nodesrw   )r2   r!   r!   r"   r3     s   
zTestFindCycle.setup_classc                 C   s(   t | j}tt jjt j|| j d S r4   )	r   r   rw   r   rS   	exceptionNetworkXNoCycle
find_cyclerG  r|   r!   r!   r"   test_graph_nocycle  s   z TestFindCycle.test_graph_nocyclec                 C   sB   t | j}|dd tt || j}g d}||ksJ d S )Nr&   r   rk   rm   rn   r   r   rw   r0   r{   rJ  rG  r>   r1   r   x_r!   r!   r"   test_graph_cycle  s
   zTestFindCycle.test_graph_cyclec                 C   sF   t | j}|dd tt j|| jd d}g d}||ks!J d S )Nr&   r   orientationrL  rM  rN  r!   r!   r"   test_graph_orientation_none  s
   z)TestFindCycle.test_graph_orientation_nonec                 C   sZ   t | j}|dd tt j|| jdd}ddtfddtfddtfg}||ks+J d S )Nr&   r   originalrQ  r   )r   r   rw   r0   r{   rJ  rG  r   rN  r!   r!   r"   test_graph_orientation_original  s
   z-TestFindCycle.test_graph_orientation_originalc                 C   s6   t | j}tt || j}ddg}||ksJ d S )Nrk   r[   r   rU   rw   r{   rJ  rG  rN  r!   r!   r"   test_digraph  s   zTestFindCycle.test_digraphc                 C   s:   t | j}tt j|| jd d}ddg}||ksJ d S )NrQ  rk   r[   rV  rN  r!   r!   r"   test_digraph_orientation_none  s   z+TestFindCycle.test_digraph_orientation_nonec                 C   F   t | j}tt j|| jdd}ddtfddtfg}||ks!J d S )NrT  rQ  r   r   r   rU   rw   r{   rJ  rG  r   rN  r!   r!   r"   !test_digraph_orientation_original     z/TestFindCycle.test_digraph_orientation_originalc                 C   b   t | j}tt || j}ddg}|d |d ksJ |d d d |d d d ks/J d S )Nr   r   r   )r   r   r   r   r   r&   )r   rY   rw   r{   rJ  rG  rN  r!   r!   r"   test_multigraph  s
   (zTestFindCycle.test_multigraphc                 C   r]  )Nr^  )r   r   r   r   r   r&   )r   r   rw   r{   rJ  rG  rN  r!   r!   r"   test_multidigraph  s
   (zTestFindCycle.test_multidigraphc                 C   rY  )NignorerQ  r   r   rZ  rN  r!   r!   r"   test_digraph_ignore  r\  z!TestFindCycle.test_digraph_ignorec                 C   rY  )NreverserQ  r   r   )r   rU   rw   r{   rJ  rG  r   rN  r!   r!   r"   test_digraph_reverse  r\  z"TestFindCycle.test_digraph_reversec                 C   s   t | j}tt j|| jdd}dddtfdddtfg}|d |d ks'J |d d d |d d d ks9J |d d |d d ksGJ d S )Nra  rQ  r   r   r&   r'   )r   r   rw   r{   rJ  rG  r   rN  r!   r!   r"   test_multidigraph_ignore  s   $ z&TestFindCycle.test_multidigraph_ignorec                 C   sN   t g d}tt j|g ddd}dddtfdddtfg}||ks%J d S )N)rk   rm   rm   rh   ra  rQ  r   r&   r   )r   r   r{   rJ  r   r   rN  r!   r!   r"   test_multidigraph_ignore2  s   z'TestFindCycle.test_multidigraph_ignore2c                 C   s0   t g d}tjt jjt j|g ddd d S )N)rk   rm   r   )r)   r&   r   rT  rQ  )r   r   r   rS   rH  rI  rJ  r|   r!   r!   r"   test_multidigraph_original&  s   
z(TestFindCycle.test_multidigraph_originalc                 C   s`   t g d}tjt jjt j|dd tt j|dd}|ddtfddtfddt	fgks.J d S )N)rk   rl   rm   rT  rQ  ra  r   r   r&   )
r   rU   r   rS   rH  rI  rJ  r{   r   r   )r>   r1   r   r!   r!   r"   test_dag4  s   (zTestFindCycle.test_dagc                 C   s   t  }|g d tjt jt j|dd tt |d}ddg}||ks(J tt |d}ddg}||ks:J tt |}ddg}||ksKJ d S )N)r[   rn   rm   ro   r   r   r   rm   ro   r&   )r   rU   r   r   rS   rI  rJ  r{   rN  r!   r!   r"   test_prev_explored<  s   z TestFindCycle.test_prev_exploredc                 C   sB   t  }|g d tjt jt j|dd tt jt j| d S )N)rm   rn   rF  r   r   ri  )r   rU   r   r   rS   rI  rJ  r|   r!   r!   r"   test_no_cycleN  s   zTestFindCycle.test_no_cycleN)r   r   r   r   r3   rK  rP  rS  rU  rW  rX  r[  r_  r`  rb  rd  re  rf  rg  rh  rj  rk  r!   r!   r!   r"   rE    s(    
rE  c                 C   s   t | t |ks
J d S r4   rB   )r?   r   r!   r!   r"   assert_basis_equalW  s   rl  c                   @   sX   e Zd Ze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S )TestMinimumCycleBasisc                 C   s6   t  }t j|g ddd |jdddd || _d S )N)r   r&   r'   r)   r   weightr&   r)   r*   )r   r   r   r0   diamond_graph)r2   Tr!   r!   r"   r3   \  s   
z!TestMinimumCycleBasis.setup_classc                 C   s&   t | j}t|g dg dg d S )Nr&   r)   r   )r'   r)   r&   r   minimum_cycle_basisrp  rl  r>   mcbr!   r!   r"   test_unweighted_diamondc  s   z-TestMinimumCycleBasis.test_unweighted_diamondc                 C   s*   t j| jdd}t|g dg dg d S )Nro  rn  rr  )r)   r'   r&   r   rs  ru  r!   r!   r"   test_weighted_diamondg  s   z+TestMinimumCycleBasis.test_weighted_diamondc                 C   sp   d}t dd| D ],}tjdd|d}| }| }t|}t|}t||| | ks1J t| q	d S )Nr   i  g333333?seed)	r=   r   erdos_renyi_graphnumber_of_nodesnumber_of_edgesnumber_connected_componentsrt  r   r#   )r>   ntrialrz  rgnnodesnedgesncomprv  r!   r!   r"   test_dimensionalityk  s   


z)TestMinimumCycleBasis.test_dimensionalityc                 C   s6   t d}t |}tdd |D sJ t| d S )Nr*   c                 s   s    | ]	}t |d kV  qdS r  r   )r6   cycler!   r!   r"   r;   {  ru   z<TestMinimumCycleBasis.test_complete_graph.<locals>.<genexpr>)r   r   rt  r   r#   )r>   cgrv  r!   r!   r"   test_complete_graphx  s   

z)TestMinimumCycleBasis.test_complete_graphc                 C   s   t dd}t |rJ d S )Nr'   )r   balanced_treert  )r>   tgr!   r!   r"   test_tree_graph~  s   z%TestMinimumCycleBasis.test_tree_graphc                    s   t   tt  }g dg dg dg dg dg dgt|tks)J tfdd|D s6J |D ]}t fd	dt jj|d
dD sMJ q8t| d S )N)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   c                 3       | ]}| v V  qd S r4   r!   rD   r   r!   r"   r;     rF   z<TestMinimumCycleBasis.test_petersen_graph.<locals>.<genexpr>c                 3   s     | ]\}}  ||V  qd S r4   r   r6   ur%  )r1   r!   r"   r;     r  T)cyclic)	r   petersen_graphr{   rt  r   r   utilsr   r#   )r>   rv  rE   r!   r1   r   r"   test_petersen_graph  s   (z)TestMinimumCycleBasis.test_petersen_graphc                 C   sZ   d}t |}|dd |jD  |dd t |jD  t j|dd}t| d S )Nr.   c                 S      g | ]	\}}||d fqS )r/   r!   r  r!   r!   r"   r`         zVTestMinimumCycleBasis.test_gh6787_variable_weighted_complete_graph.<locals>.<listcomp>c                 S   r  )r   r!   r  r!   r!   r"   r`     r  ro  rn  )r   r   add_weighted_edges_fromrw   rb   rt  r#   )r>   Nr  rv  r!   r!   r"   ,test_gh6787_variable_weighted_complete_graph  s   
zBTestMinimumCycleBasis.test_gh6787_variable_weighted_complete_graphc                    s   t d}|jddgdd g dg dg dg tt j|dd}t|t ks,J t fd	d
|D s9J g dg dg dg tt |}t|t ksUJ t fdd
|D sbJ d S )Nr)   )r   r&   r   )r   r'   r   distrn  )r   r'   r   )r'   r&   r   r   )r   r&   r   c                 3   r  r4   r!   rD   r  r!   r"   r;     rF   zMTestMinimumCycleBasis.test_gh6787_and_edge_attribute_names.<locals>.<genexpr>)r'   r&   r   c                 3   r  r4   r!   rD   r  r!   r"   r;     rF   )r   rb   r  r{   rt  r   r   )r>   r1   rv  r!   r  r"   $test_gh6787_and_edge_attribute_names  s   
z:TestMinimumCycleBasis.test_gh6787_and_edge_attribute_namesN)r   r   r   r   r3   rw  rx  r  r  r  r  r  r  r!   r!   r!   r"   rm  [  s    
rm  c                   @   s   e Zd Zejde dfe dfe	 dfe
 dfe dfejdddefedefeeeedeedddfeg dd	ff	d
d ZdS )	TestGirthr  r)   r*   r,   r   *   ry  ))r   r,   )r   r.   )r   r/   )r   r.   )r&   r.   )r&   r/   )r)   r/   )r*   r/   )r,   r.   )r,   r/   )r-   r.   r'   c                 C   s   t ||ks	J d S r4   )r   girthr   r!   r!   r"   
test_girth  s    zTestGirth.test_girthN)r   r   r   r   markparametrizer   chvatal_graphtutte_graphr  heawood_graphpappus_graphrandom_labeled_treer   empty_graphr   r   r   r=   r  r!   r!   r!   r"   r    s&    




$r  )	itertoolsr   r   r   mathr   randomr   r   networkxr   %networkx.algorithms.traversal.edgedfsr   r   r#   r$   r   r   r   r   r   r   rE  rl  rm  r  r!   r!   r!   r"   <module>   s.     )   q R