o
    3ήc#                     @   sX   d dl Z d dlZd dlmZmZ d dlmZmZ dd Z	G dd dZ
G dd	 d	ZdS )
    N)treewidth_min_degreetreewidth_min_fill_in)MinDegreeHeuristicmin_fill_in_heuristicc           
      C   s   |   D ]}d}|  D ]
}||v rd} nq|sJ q|  D ]\}}d}|  D ]}||v r8||v r8d} nq*|s=J q |   D ] }g }|  D ]}||v rU|| qJ||}	t|	sbJ qBdS )z/Check if the given tree decomposition is valid.FTN)nodesedgesappendsubgraphnxis_connected)
graphdecompxappear_oncebagyappear_togethervsubset	sub_graph r   ]/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/approximation/tests/test_treewidth.pyis_tree_decomp   s2   



r   c                   @   T   e Zd 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S )TestTreewidthMinDegreez&Unit tests for the min_degree functionc                 C   s  t  | _| jdd | jdd | jdd t  | _| jdd | jdd | jdd | jdd | jdd | jdd | jdd t  | _| jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd	 | jdd
 | jdd | jdd	 | jdd
 | jdd	 | jdd
 | jd	d
 dS )"Setup for different kinds of trees                     r      	   Nr
   Graphcompleteadd_edge
small_treedeterministic_graphclsr   r   r   setup_class.   sB   


z"TestTreewidthMinDegree.setup_classc                 C   "   t  }t|\}}t|| dS z-Test Petersen graph tree decomposition resultN)r
   petersen_graphr   r   selfG_r   r   r   r   test_petersen_graph]      z*TestTreewidthMinDegree.test_petersen_graphc                 C   "   | j }t|\}}|dksJ dS )zTest small tree

        Test if the computed treewidth of the known self.small_tree is 2.
        As we know which value we can expect from our heuristic, values other
        than two are regressions
        r   Nr)   r   r2   r3   	treewidthr4   r   r   r   test_small_tree_treewidthc   s   z0TestTreewidthMinDegree.test_small_tree_treewidthc                 C   sb   i }| j D ]}t ||< | j | D ]}||kr|| | qqt|}||}|du r/dS J )z8Test heuristic abort condition for fully connected graphN)r'   setaddr   	best_node)r2   r   ur   deg_heuristicnoder   r   r   test_heuristic_abortq   s   


z+TestTreewidthMinDegree.test_heuristic_abortc                 C      t  }t|\}}dS zTest empty graphNr
   r&   r   r2   r3   r4   r   r   r   test_empty_graph      z'TestTreewidthMinDegree.test_empty_graphc                 C   8   t  }|d |d t|\}}|dksJ d S Nr   r   r   )r
   r&   add_noder   r9   r   r   r   test_two_component_graph   
   

z/TestTreewidthMinDegree.test_two_component_graphc                 C      t dg}t| d S N)r   arE   r2   r3   r   r   r   test_not_sortable_nodes      z.TestTreewidthMinDegree.test_not_sortable_nodesc                    s    fdd j D }t|}||}td| d g }|durptd| d || || }t|dD ]\}}||| vrI|| | q8|D ]}||| v r[|| | qL||= td| d ||}|dus!|dd g d	ks|J dS )
z(Test first steps of min_degree heuristicc                    "   i | ]}|t  j| |h qS r   r<   r*   .0nr2   r   r   
<dictcomp>       zETestTreewidthMinDegree.test_heuristic_first_steps.<locals>.<dictcomp>Graph :N	Removing r   r    )r   r   r   r   r   )	r*   r   r>   printr   	itertoolspermutationsr=   remove)r2   r   r@   	elim_nodestepsnbrsr?   r   r   rY   r   test_heuristic_first_steps   s0   



z1TestTreewidthMinDegree.test_heuristic_first_stepsN__name__
__module____qualname____doc__classmethodr-   r5   r;   rB   rG   rL   rR   rf   r   r   r   r   r   +   s    
.r   c                   @   r   )TestTreewidthMinFillInz2Unit tests for the treewidth_min_fill_in function.c                 C   s:  t  | _| jdd | jdd | jdd t  | _| jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd t  | _| jdd | jdd | jdd | jdd | jdd | jdd | jdd | jdd dS )	r   r   r   r   r   r    r!   r"   Nr%   r+   r   r   r   r-      s.   


z"TestTreewidthMinFillIn.setup_classc                 C   r.   r/   )r
   r0   r   r   r1   r   r   r   r5      r6   z*TestTreewidthMinFillIn.test_petersen_graphc                 C   r7   )z@Test if the computed treewidth of the known self.small_tree is 2r   Nr8   r9   r   r   r   r;      s   z0TestTreewidthMinFillIn.test_small_tree_treewidthc                 C   sX   i }| j D ]}t ||< | j | D ]}||kr|| | qqt|}|du r*dS J )z:Test if min_fill_in returns None for fully connected graphN)r'   r<   r=   r   )r2   r   r?   r   	next_noder   r   r   rB      s   

z+TestTreewidthMinFillIn.test_heuristic_abortc                 C   rC   rD   r
   r&   r   rF   r   r   r   rG      rH   z'TestTreewidthMinFillIn.test_empty_graphc                 C   rI   rJ   )r
   r&   rK   r   r9   r   r   r   rL      rM   z/TestTreewidthMinFillIn.test_two_component_graphc                 C   rN   rO   ro   rQ   r   r   r   rR      rS   z.TestTreewidthMinFillIn.test_not_sortable_nodesc                    s    fdd j D }td| d t|}g }|durjtd| d || || }t|dD ]\}}||| vrD|| | q3|D ]}||| v rV|| | qG||= td| d t|}|dus|dd dd	gksvJ dS )
z)Test first steps of min_fill_in heuristicc                    rT   r   rU   rV   rY   r   r   rZ      r[   zETestTreewidthMinFillIn.test_heuristic_first_steps.<locals>.<dictcomp>r\   r]   Nr^   r   r!   r    )r*   r_   r   r   r`   ra   r=   rb   )r2   r   rc   rd   re   r?   r   r   rY   r   rf      s.   

z1TestTreewidthMinFillIn.test_heuristic_first_stepsNrg   r   r   r   r   rm      s    
	rm   )r`   networkxr
   !networkx.algorithms.approximationr   r   +networkx.algorithms.approximation.treewidthr   r   r   r   rm   r   r   r   r   <module>   s     