o
    3ήcw                     @   s@  d Z ddlZddlZddlZddlm  mZ ej	j
Z
dd Zdd Zdd ZG d	d
 d
Zdd Zdd ZG dd deZG dd deZG dd de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$ej%j&d7d8 Z'd9d: Z(dS );z-Unit tests for the traveling_salesman module.    Nc                  C   s   t d td} |  D ]\}}t dd| | | d< qt }|tt	
|  |t| t|jdks>J tj| dd}t }|tt	
| | |t| t|jdkseJ d S )N*      r   
   weightr   )randomseednxcomplete_graphedgesrandintGraphadd_edges_frompairwisenx_appchristofidesremove_edges_from
find_cyclelenminimum_spanning_tree)GuvHtree r   f/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/approximation/tests/test_traveling_salesman.pytest_christofides_hamiltonian   s   

r   c                  C   s,   t d} | dd tt jtj|  d S )Nr   r      )r	   r
   remove_edgepytestraisesNetworkXErrorr   r   r   r   r   r   "test_christofides_incomplete_graph   s   
r$   c                  C   sT   t d} | dd t| }t|d t|   kr%tt|ks(J  J d S N      r   )r	   r
   add_edger   r   r   set)r   cycler   r   r   "test_christofides_ignore_selfloops$      

4r+   c                   @   s   e Zd Zedd ZdS )TestBasec                 C   s  t  | _| jh d g d| _d| _t  | _| jh d g d| _d| _t 	dt 
 | _t 	dt  | _t 
 | _| jdd	h t  | _| jdd	h t 
 | _| jh d
 g d| _d| _t 
 | _| jh d g d| _d| _d S )N>   BAr'   Cr0      r2   r/      r0   r/   r'   r0   r2      r0   D   r/   r2   r5   r/   r:      r2   r:      r:   r0   r;   r:   r/      r:   r2      r:   r2   r/   r0   r:         ?@>   r/   r0      r/   r2   rE   r2   r0   !   r2   r/       r2   r:   "   r6   r7   r9   r=   rA   rB   rD   r:   r0   r/   r2   r:   g     J@r&   )r   r   r   )r   rE   r'   >   r6   r7   r9   r<   r=   r?        @@>   r0   r/   r   r0   r2   rC   r0   r:   r&   r/   r2   r>   r/   r:      r2   r:   r'   g      9@)r	   DiGraphDGadd_weighted_edges_fromDG_cycleDG_costDG2	DG2_cycleDG2_costr
   r   unweightedUGunweightedDGincompleteUGincompleteDGUGUG_cycleUG_costUG2	UG2_cycleUG2_cost)clsr   r   r   setup_class-   s<   












zTestBase.setup_classN)__name__
__module____qualname__classmethodrm   r   r   r   r   r-   ,   s    r-   c                 C   s   | |ksJ ||ksJ d S Nr   solncostexp_solnexp_costr   r   r   validate_solution|      rx   c                 C   s.   | |ks| |d d d ksJ ||ksJ d S )Nr   rs   r   r   r   validate_symmetric_solution   s   r{   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestGreedyTSPc                    s   t j jdd}t fddt|D }t||g dd t j jdd}t fddt|D }t||g dd t j jdd}t fd	dt|D }t||g dd
 t j jdd}t fddt|D }t||g dd d S )Nr:   sourcec                 3   &    | ]\}} j | | d  V  qdS r   Nr[   .0nnbrselfr   r   	<genexpr>      $ z,TestGreedyTSP.test_greedy.<locals>.<genexpr>rF   rG   c                 3   r   r   r_   r   r   r   r   r      r   g     S@c                 3   r   r   rf   r   r   r   r   r      r   rR   c                 3   r   r   ri   r   r   r   r   r      r   )r:   r2   r0   r/   r:   g      ;@)	r   
greedy_tspr[   sumr   rx   r_   rf   ri   r   r*   ru   r   r   r   test_greedy   s   zTestGreedyTSP.test_greedyc                 C   s,   t tjtj| j t tjtj| j d S rr   )r    r!   r	   r"   r   r   rd   re   r   r   r   r   test_not_complete_graph   s   z%TestGreedyTSP.test_not_complete_graphc                 C   s   t | j t | j d S rr   )r   r   rb   rc   r   r   r   r   test_not_weighted_graph   ry   z%TestGreedyTSP.test_not_weighted_graphc                    sN   t    dh t }t fddt|D }t||g dd d S )Nr   rE   r   c                 3   $    | ]\}} | | d  V  qdS r   r   r   r#   r   r   r         " z/TestGreedyTSP.test_two_nodes.<locals>.<genexpr>rE   )r	   r   r\   r   r   r   r   rx   r   r   r#   r   test_two_nodes   s
   
zTestGreedyTSP.test_two_nodesc                 C   sT   t d}|dd t|}t|d t|  kr%tt|ks(J  J d S r%   )r	   r
   r(   r   r   r   r)   r   r   r*   r   r   r   test_ignore_selfloops   r,   z#TestGreedyTSP.test_ignore_selfloopsN)rn   ro   rp   r   r   r   r   r   r   r   r   r   r|      s    r|   c                   @   sV   e Zd Zeej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 )TestSimulatedAnnealingTSPc                    sX   j  jdddd}t fddt|D }t|| j j g d} j  j|ddd}t fddt|D }t|| j j g d	} j  j|d
ddd}t fddt|D }t|| j j  j  jdddd}t fddt|D }t|| j j	  j  jdd
ddd}t fddt|D }t|| j j	 d S )Ngreedyr:   r   r~   r   c                 3   r   r   r   r   r   r   r   r      r   zNTestSimulatedAnnealingTSP.test_simulated_annealing_directed.<locals>.<genexpr>)r:   r/   r0   r2   r:   c                 3   r   r   r   r   r   r   r   r      r   )r:   r0   r2   r/   r:   1-0mover~   r   c                 3   r   r   r   r   r   r   r   r      r   c                 3   r   r   r   r   r   r   r   r      r   c                 3   r   r   r   r   r   r   r   r      r   )
tspr[   r   r   rx   r]   r^   r_   r`   ra   r   r*   ru   initial_solr   r   r   !test_simulated_annealing_directed   s"   z;TestSimulatedAnnealingTSP.test_simulated_annealing_directedc                    s    j  jdddd}t fddt|D }t|| j j  j  jdddd}t fddt|D }t|| j	 j
  j  jddddd	}t fd
dt|D }t|| j	 j
 d S )Nr   r:   r   r   c                 3   r   r   r   r   r   r   r   r      r   zPTestSimulatedAnnealingTSP.test_simulated_annealing_undirected.<locals>.<genexpr>c                 3   r   r   r   r   r   r   r   r      r   r   r   c                 3   r   r   r   r   r   r   r   r      r   )r   rf   r   r   rx   rg   rh   ri   r{   rj   rk   r   r   r   r   #test_simulated_annealing_undirected   s   z=TestSimulatedAnnealingTSP.test_simulated_annealing_undirectedc                 C   s0   t jt| j| jdd t tj| j| jd d S )Nr   r   )r    r!   	TypeErrorr   rf   r	   r"   r   r   r   r   !test_error_on_input_order_mistake   s   z;TestSimulatedAnnealingTSP.test_error_on_input_order_mistakec                 C   s8   t jtj| j| jddd t jtj| j| jddd d S )Nr   r   r}   )r    r!   r	   r"   r   rd   re   r   r   r   r   r      s   z1TestSimulatedAnnealingTSP.test_not_complete_graphc                 C   sV   t d}|dd | |d}t|d t|  kr&tt|ks)J  J d S )Nr&   r'   r   r   )r	   r
   r(   r   r   r)   r   r   r   r   r      s   
4z/TestSimulatedAnnealingTSP.test_ignore_selfloopsc                 C   s    |  | jd |  | jd d S )Nr   )r   rb   rc   r   r   r   r   r      s   z1TestSimulatedAnnealingTSP.test_not_weighted_graphc                    s   t    dh | j dddd}t fddt|D }t||g dd | j g dddd}t fd	dt|D }t||g dd d S )
Nr   r   r   r   r   c                 3   r   r   r   r   r#   r   r   r      r   z;TestSimulatedAnnealingTSP.test_two_nodes.<locals>.<genexpr>rE   c                 3   r   r   r   r   r#   r   r   r      r   )r	   r   r\   r   r   r   rx   r   r   r#   r   r      s   z(TestSimulatedAnnealingTSP.test_two_nodesc              
      s    j  jddddddd}t fddt|D }t|| | jks&J g d	} j  j|ddd
dddd}t fddt|D }t|| | jksQJ d S )Nr   r:   r   r   r   )r~   r   alphaN_innerr   c                 3   r   r   r   r   r   r   r   r      r   z_TestSimulatedAnnealingTSP.test_failure_of_costs_too_high_when_iterations_low.<locals>.<genexpr>rQ   g?)r~   r   r   r   max_iterationsr   c                 3   r   r   r   r   r   r   r   r   
  r   )r   r_   r   r   printra   r[   r^   r   r   r   r   2test_failure_of_costs_too_high_when_iterations_low   s(   


zLTestSimulatedAnnealingTSP.test_failure_of_costs_too_high_when_iterations_lowN)rn   ro   rp   staticmethodr   simulated_annealing_tspr   r   r   r   r   r   r   r   r   r   r   r   r   r      s    
r   c                   @   s   e Zd ZeejZdd ZdS )TestThresholdAcceptingTSPc              	      s    j  jddddddd}t fddt|D }| jks!J g d	} j  j|ddd
dd}t fddt|D }| jksEJ d S )Nr   r:   r   r   r@   )r~   r   r   r   r   c                 3   r   r   r   r   r   r   r   r     r   z_TestThresholdAcceptingTSP.test_failure_of_costs_too_high_when_iterations_low.<locals>.<genexpr>rQ   r   )r~   r   	thresholdr   c                 3   r   r   r   r   r   r   r   r   &  r   )r   r_   r   r   ra   r[   r^   r   r   r   r   r     s"   	zLTestThresholdAcceptingTSP.test_failure_of_costs_too_high_when_iterations_lowN)rn   ro   rp   r   r   threshold_accepting_tspr   r   r   r   r   r   r     s    
r   c                  C   sN   t d} d| d d d< dd }tj| |dd	}t| |g d
ks%J d S )N	   r   r@   r&   r   c                 S   s   t j| d|dddS )Nr   r@   r   r   r   r   r   r   r   r   r   my_tsp_method/  s   z&test_TSP_method.<locals>.my_tsp_methodF)methodr*   	r@   r'   rE   r   r   rX         r&   )r	   cycle_graphr   traveling_salesman_problemr   )r   r   pathr   r   r   test_TSP_method+  s   
r   c                  C   sd   t d} tj| ddgdd}|g dg dfv sJ tj| ddgd}|g d	g d
fv s0J d S )Nr   r'   r   F)nodesr*   )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   test_TSP_unweighted7  s
   
r   c            
      C   s  t d} d| d d d< d| d d d< d| d d d< d| d d d< d| d d d< d| d d	 d< d| d	 d
 d< d| d
 d d< d| d d d< tj}g dg df}g dg df}g dg df}|| dd	gdd}||v swJ || dd	gddd}||v sJ || ddd}||v sJ tjtjdd dd g}|D ]1}	|| dd	gd|	d}||v sJ || dd	gd|	dd}||v sJ || d|	dd}||v sJ qd S )Nr   rE   r   r   r   r'   r@   r&   r   r   rX   )r'   rE   r   r   rX   r   r   )r   r   rX   r   r   rE   r'   )r'   rE   r   r   rX   r   r   r   rX   r   r   rE   r'   )r   r   rX   r   r   rE   r'   rE   r   r   rX   r   r   )	r&   r   r   rX   r   r   rE   r'   r@   r   )r   r   F)r   r   r*   )r   r*   c                 S      t j| d|dS Nr   r   r   r   wtr   r   r   <lambda>e      z#test_TSP_weighted.<locals>.<lambda>c                 S   r   r   )r   r   r   r   r   r   r   f  r   )r   r   r   )r   r   r   r*   )r   r   r*   )r	   r   r   r   r   r   )
r   r   expected_pathsexpected_cyclesexpected_tourpathsr*   r   tourpathmethodsr   r   r   r   test_TSP_weighted@  sF   
r   c                  C   s   t d} | g d d| d d d< t| }t| t|dkr+tt|dks-J tj| dd	}t| t|d
krFtt|dksHJ d S )Nr   ))r@   r   )r   r   )r      )r   r   r&   r@   r   r8   r5   F)r*   r3   )r	   r   r   r   r   r   r   r)   )r   r*   r   r   r   r   $test_TSP_incomplete_graph_short_paths  s   

 $r   c               	   C      ddl m  m  m}  td}td |g dg dg dg dg d	g d
g}g d}tj|tj	d}| 
|\}}t|ddksIJ t	 }|| tj|j|js]J dS )z>
    Test the Held-Karp relaxation with the ascent method
    r   Nnumpyscipy)r   a   <   I   r8   4   )r   r   )   r   Z   rI   )r   r   r      #   r   )r   r   r   r   _   .   )r8   r   r   r   r   Q   )r   rI   r   r   r   r   )r   r'   )rE   r@   r'   rE   r@   r   )r&   r   r   r&   create_usingrE   g     i@4networkx.algorithms.approximation.traveling_salesman
algorithmsapproximationtraveling_salesmanr    importorskiparrayr	   from_numpy_arrayrZ   held_karp_ascentroundr   utilsedges_equalr   r   npG_arraysolution_edgesr   opt_hkz_starsolutionr   r   r   test_held_karp_ascent  s&   


r   c               	      s(  ddl m  m  m}  td}td |g dg dg dg dg d	g d
g}i ddddddddddddddddddddddddddddddddddd di tj|tj	d!}| 
|\}t|d"d#ks~J fd$d%D  fd&d% D ksJ dS )'z
    Test the ascent method using a modified version of Figure 2 on page 1140
    in 'The Traveling Salesman Problem and Minimum Spanning Trees' by Held and
    Karp
    r   Nr   r   )r   d   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   rE   r   竪?r   r   r   rE   UUUUUU?r   r@   rE   r   rE   r   rE   r'   r   r'   r@   r'   r&         ?r@   r   r@   r'   r@   r&   r&   r   r&   r'   r&   r@   r   rE   g     r@c                       i | ]
}|t  | d qS r@   r   r   keyr   r   r   
<dictcomp>      z3test_ascent_fractional_solution.<locals>.<dictcomp>c                    r  r  r  r  solution_z_starr   r   r        r   r   r   r   r    r   r   r	   r   rZ   r   r   r   r   r   r   r   r   r  r   r   test_ascent_fractional_solution  sn   

	
r  c               
   C   s   ddl m  m  m}  td}td |g dg dg dg dg d	g d
g dg}g d}tj|tj	d}| 
|\}}t|ddksLJ t	 }|| tj|j|js`J dS )q
    Tests the ascent method using a truly asymmetric graph for which the
    solution has been brute forced
    r   Nr   r   r      ?   ;   E      r   >   r   [   5   K   W   /   r'  R   r   r   rC   r      D      r&   r   :   rP   ]   r   r.  r$  7   r   =   O   X   r%  r3   L   b   r   (   r   r2  r1  r5  r   -   r   )r   r   r   rE   r&   )r&   r   r   )r   r@   r   rE   g     g@r   r   r   r   r   test_ascent_method_asymmetric  s(   


r<  c               	   C   r   )r  r   Nr   r   )r   r:  '   \      r   )H   r   r@   r5   r   r   )r   r   r   r7  F   r$  )1   G   r  r   r7  ^   )J   r      +   r   r'  )8   rG  r'   A      r   )r   r
  r   )r'   r   r   r@   rE   r   rE   g      b@r   r   r   r   r   test_ascent_method_asymmetric_2  s&   


rL  c            
   	   C   s   ddl m  m  m}  td}td |g dg dg dg dg d	g d
g}g d}g d}tj|tj	d}| 
|\}}t|ddksMJ t	 }|| t	 }	|	| tj|j|jsstj|j|	jsuJ dS dS )a  
    Tests the ascent method using a truly asymmetric graph with a fractional
    solution for which the solution has been brute forced.

    In this graph their are two different optimal, integral solutions (which
    are also the overall atsp solutions) to the Held Karp relaxation. However,
    this particular graph has two different tours of optimal value and the
    possible solutions in the held_karp_ascent function are not stored in an
    ordered data structure.
    r   Nr   r   )r   r   r&   rE   r   r@   )r   r   r   r   r   r@   )r@   r   r   r   rE   r   )r   rE   r   r   r@   r@   )r&   r&   r@   r@   r   r'   )r'   r   r   r'   r@   r   )r   r'   r   r;  r'   r   rK  r  )rM  rN  r   r  r   )r&   rE   r   rE   g      *@r   )
r   r   r   solution1_edgessolution2_edgesr   r   r   	solution1	solution2r   r   r   "test_held_karp_ascent_asymmetric_3$  s0   



rS  c               	      s(  ddl m  m  m}  td}td |g dg dg dg dg d	g d
g}i ddddddddddddddddddddddddddddddddddddi tj|tj	d}| 
|\}t|d d!ks~J fd"d#D  fd$d# D ksJ dS )%z
    Tests the ascent method using a truly asymmetric graph with a fractional
    solution for which the solution has been brute forced
    r   Nr   r   )r   r      r   r   r   )rT  r   r   r   r   r   )r   rT  r   r   r   r   )r   r   r   r   rT  r   )r   rE   r   r   r   rT  )rE   r   r   rT  r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  r  r  r  r	  r
  r   rE   g      s@c                    r  r  r  r  r  r   r   r    r  z?test_held_karp_ascent_fractional_asymmetric.<locals>.<dictcomp>c                    r  r  r  r  r  r   r   r    r  r  r  r   r  r   +test_held_karp_ascent_fractional_asymmetricQ  sn   

	
rU  c               
      s   ddl m  m  m}  td td i dddddd	d
ddddd	dddddd	dd	dddddd	dddddd	ddddi}ddddddddd d!	}t }|D ]\}}||f|jv ss||f|jv rtqa|	|| qa| 
||  fd"d# D |ksJ dS )$aY  
    Test that we can create an exponential distribution of spanning trees such
    that the probability of each tree is proportional to the product of edge
    weights.

    Results of this test have been confirmed with hypothesis testing from the
    created distribution.

    This test uses the symmetric, fractional Held Karp solution.
    r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  r  r  r  r  r	  r
  gw-!lgUg?g?5^IҿgDJտgW2)	r   r   r   r   r   r  r	  r
  r  c                    r  r  r  r  gammar   r   r    r  z3test_spanning_tree_distribution.<locals>.<dictcomp>)r   r   r   r   r    r   r	   
MultiGraphr   r(   spanning_tree_distribution)r   r   solution_gammar   r   r   r   rV  r   test_spanning_tree_distribution  sr   

	
r[  c                  C   sf   t d t d g d} t }||  dd }tj|d|d}g dg d	g}||v s1J d
S )z
    Test the complete asadpour tsp algorithm with the fractional, symmetric
    Held Karp solution. This test also uses an incomplete graph as input.
    r   r   ))r   r   r   )r   rE   r   )r   r&   r   )r   rE   r   )r   r@   r   )rE   r'   r   )r'   r@   r   )r'   r&   r   )r@   r&   r   )r   r   r   )rE   r   r   )r&   r   r   )rE   r   r   )r@   r   r   r'   rE   r   )r@   r'   r   )r&   r'   r   )r&   r@   r   c                 S      t | |dS )Nr-  r   asadpour_atspr   r   r   r   fixed_asadpour     z)test_asadpour_tsp.<locals>.fixed_asadpourr   r   r   )r   r@   r&   r   rE   r'   rE   r   )r'   rE   r   r   r@   r&   r'   N)r    r   r	   rZ   r\   r   r   )	edge_listr   r`  tourexpected_toursr   r   r   test_asadpour_tsp  s   


rf  c               	   C   s   t d} t d | g dg dg dg dg dg dg}d	d
ddddd}g dg dg}tj|tjd}tj||dd dd }tj|d|d}||v sTJ dS )a   
    This test uses airline prices between the six largest cities in the US.

        * New York City -> JFK
        * Los Angeles -> LAX
        * Chicago -> ORD
        * Houston -> IAH
        * Phoenix -> PHX
        * Philadelphia -> PHL

    Flight prices from August 2021 using Delta or American airlines to get
    nonstop flight. The brute force solution found the optimal tour to cost $872

    This test also uses the `source` keyword argument to ensure that the tour
    always starts at city 0.
    r   r   r                  i  r      {         i)     r   rs  ro     i/  rk  rs  r   u   rv  i  rp     rv  r   ?  rl  iL  rn  rv  ry  r   JFKLAXORDIAHPHXPHLr   r   rE   r'   r@   r&   )r{  r|  r  r}  r~  r  r{  )r{  r}  r  r|  r~  r  r{  r   Fcopyc                 S   s   t j| |dddS )N%   r{  r}   r^  r   r   r   r   r`  (  s   z0test_asadpour_real_world.<locals>.fixed_asadpourr   rb  N	r    r   r   r	   r   rZ   relabel_nodesr   r   )r   r   node_mapre  r   r`  rd  r   r   r   test_asadpour_real_world  s(   

r  c               	   C   s   t d} t d | g dg dg dg dg dg dg}d	d
ddddd}g dg dg}tj|tjd}tj||dd dd }tj|dd|d}||v sUJ dS )a  
    This test uses airline prices between the six largest cities in the US. This
    time using a path, not a cycle.

        * New York City -> JFK
        * Los Angeles -> LAX
        * Chicago -> ORD
        * Houston -> IAH
        * Phoenix -> PHX
        * Philadelphia -> PHL

    Flight prices from August 2021 using Delta or American airlines to get
    nonstop flight. The brute force solution found the optimal tour to cost $872
    r   r   rg  rm  rr  ru  rw  rz  r{  r|  r}  r~  r  r  r  )r}  r  r|  r~  r  r{  )r{  r  r~  r}  r  r|  r   Fr  c                 S   r]  )NrH  r^  r   r   r   r   r`  X  ra  z5test_asadpour_real_world_path.<locals>.fixed_asadpourr   )r   r*   r   Nr  )r   r   r  r   r   r`  r   r   r   r   test_asadpour_real_world_path0  s,   

r  c                  C   s>   t jdt jd} t | dd | d tt jtj	|  dS )zi
    Test that the proper exception is raised when asadpour_atsp is given an
    disconnected graph.
    r@   r   r   r   r&   N)
r	   r
   rZ   set_edge_attributesadd_noder    r!   r"   r   r_  r#   r   r   r    test_asadpour_disconnected_graphb  s   
r  c                  C   s@   t jdt jd} t | dd | dd tt jtj	|  dS )zf
    Test that the proper exception is raised when asadpour_atsp is given an
    incomplete graph
    r@   r   r   r   r   N)
r	   r
   rZ   r  r   r    r!   r"   r   r_  r#   r   r   r   test_asadpour_incomplete_graphq  s   r  c                  C   s   t  } tt jtj|  dS )z=
    Test the asadpour_atsp function with an empty graph
    N)r	   rZ   r    r!   r"   r   r_  r#   r   r   r   test_asadpour_empty_graph  s   r  c               
   C   s   t d} | g dg dg dg dg dg dg dg}tj|tjd	}td
D ]}tj|tj	d}g d|ks=J q+dS )ar  
    This test uses an integral held karp solution and the held karp function
    will return a graph rather than a dict, bypassing most of the asadpour
    algorithm.

    At first glance, this test probably doesn't look like it ensures that we
    skip the rest of the asadpour algorithm, but it does. We are not fixing a
    see for the random number generator, so if we sample any spanning trees
    the approximation would be different basically every time this test is
    executed but it is not since held karp is deterministic and we do not
    reach the portion of the code with the dependence on random numbers.
    r   r  r!  r(  r+  r0  r4  r9  r   rE   )r   )	r   r'   rE   r&   rE   r   r@   r   r   N)
r    r   r   r	   r   rZ   ranger   r   r_  )r   r   r   _rd  r   r   r    test_asadpour_integral_held_karp  s    
r  c                  C   s:   t d g d} t }||  t tjtj| dS )zP
    Test the asadpour algorithm with a graph without a hamiltonian circuit
    r   )	)r   r   r   )r   rE   r   )r   r'   r5   )r   rE   r@   )r   r'   r   )rE   r   r'   )rE   r'   rE   )r'   r   r&   r\  N)	r    r   r	   rZ   r\   r!   r"   r   r   )r   r   r   r   r   test_directed_tsp_impossible  s
   

r  ))__doc__r   r    networkxr	   !networkx.algorithms.approximationr   r   r   r   r   r   r$   r+   r-   rx   r{   r|   r   r   r   r   r   r   r   r  r<  rL  rS  rU  r[  rf  r  r  r  r  r  markslowr  r  r   r   r   r   <module>   sF    P(a	3#9#"-5=;22	
#