
    Vh"                     b    d dl Z d dlZd dlmZmZ d dlmZmZ d Z	 G d d      Z
 G d d      Zy)	    N)treewidth_min_degreetreewidth_min_fill_in)MinDegreeHeuristicmin_fill_in_heuristicc                    | j                         D ]%  }d}|j                         D ]
  }||v sd} n |r%J  | j                         D ]-  \  }}d}|j                         D ]  }||v s||v sd} n |r-J  | j                         D ]W  }g }|j                         D ]  }||v s|j                  |        |j                  |      }	t	        j
                  |	      rWJ  y)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_graphs
             f/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/approximation/tests/test_treewidth.pyis_tree_decompr      s   [[] <<> 	CCx"	 {  1<<> 	CCxAH"&	  [[] *<<> 	#CCxc"	# OOF+	y)))*    c                   J    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zy
)TestTreewidthMinDegreez&Unit tests for the min_degree functionc                 *   t        j                         | _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       t        j                         | _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       t        j                         | _        | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd	       | j
                  j                  dd
       | j
                  j                  dd       | j
                  j                  dd	       | j
                  j                  dd
       | j
                  j                  dd	       | j
                  j                  dd
       | j
                  j                  d	d
       y)"Setup for different kinds of trees                     r      	   Nr   Graphcompleteadd_edge
small_treedeterministic_graphclss    r   setup_classz"TestTreewidthMinDegree.setup_class.   s    xxza#a#a#1%1%1%1%1%1%1%"$((*((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.((A.r   c                 `    t        j                         }t        |      \  }}t        ||       yz-Test Petersen graph tree decomposition resultN)r   petersen_graphr   r   selfG_r   s       r   test_petersen_graphz*TestTreewidthMinDegree.test_petersen_graph]   s(    (+	6q&!r   c                 F    | j                   }t        |      \  }}|dk(  sJ y)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   r5   r6   	treewidthr7   s       r   test_small_tree_treewidthz0TestTreewidthMinDegree.test_small_tree_treewidthc   s)     OO -Q/	1A~~r   c                     i }| j                   D ]=  }t               ||<   | j                   |   D ]  }||k7  s	||   j                  |        ? t        |      }|j	                  |      }|yJ )z8Test heuristic abort condition for fully connected graphN)r*   setaddr   	best_node)r5   r   ur   deg_heuristicnodes         r   test_heuristic_abortz+TestTreewidthMinDegree.test_heuristic_abortq   s|     	$AuE!H]]1% $6!HLLO$	$ +51&&u-<5r   c                 H    t        j                         }t        |      \  }}yzTest empty graphNr   r)   r   r5   r6   r7   s      r   test_empty_graphz'TestTreewidthMinDegree.test_empty_graph   s    HHJ#A&1r   c                     t        j                         }|j                  d       |j                  d       t        |      \  }}|dk(  sJ y Nr   r    r   )r   r)   add_noder   r;   s       r   test_two_component_graphz/TestTreewidthMinDegree.test_two_component_graph   s=    HHJ	

1	

1+A.	1A~~r   c                 F    t        j                  dg      }t        |       y N)r   arH   r5   r6   s     r   test_not_sortable_nodesz.TestTreewidthMinDegree.test_not_sortable_nodes   s    HHhZ Qr   c                    | j                   D ci c]  }|t        | j                   |         |hz
  ! }}t        |      }|j                  |      }g }||j	                  |       ||   }t        j                  |d      D ]!  \  }}|||   vs||   j                  |       # |D ]  }|||   v s||   j                  |         ||= |j                  |      }||dd g dk(  sJ yc c}w )z(Test first steps of min_degree heuristicNr    r#   )r   r   r    r!   r"   )	r-   r?   r   rA   r
   	itertoolspermutationsr@   remove)	r5   nr   rC   	elim_nodestepsnbrsrB   r   s	            r   test_heuristic_first_stepsz1TestTreewidthMinDegree.test_heuristic_first_steps   s$    @D?W?W
:;As4++A./1#55
 
 +51!++E2	#LL##D!..tQ7 $1E!H$!HLLO$  /a(!HOOI./ i %//6I #  RayO+++/
s   $C,N__name__
__module____qualname____doc__classmethodr0   r8   r=   rE   rJ   rN   rS   r\    r   r   r   r   +   s;    0,/ ,/\" '
 ,r   r   c                   J    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zy
)TestTreewidthMinFillInz2Unit tests for the treewidth_min_fill_in function.c                    t        j                         | _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       t        j                         | _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       t        j                         | _        | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       | j
                  j                  dd       y)	r   r   r    r!   r"   r#   r$   r%   Nr(   r.   s    r   r0   z"TestTreewidthMinFillIn.setup_class   s    xxza#a#a#1%1%1%1%1%1%1%1%1%"$((*((A.((A.((A.((A.((A.((A.((A.((A.r   c                 `    t        j                         }t        |      \  }}t        ||       yr2   )r   r3   r   r   r4   s       r   r8   z*TestTreewidthMinFillIn.test_petersen_graph   s(    )!,	6q&!r   c                 F    | j                   }t        |      \  }}|dk(  sJ y)z@Test if the computed treewidth of the known self.small_tree is 2r    Nr:   r;   s       r   r=   z0TestTreewidthMinFillIn.test_small_tree_treewidth   s'    OO -Q/	1A~~r   c                     i }| j                   D ]=  }t               ||<   | j                   |   D ]  }||k7  s	||   j                  |        ? t        |      }|yJ )z:Test if min_fill_in returns None for fully connected graphN)r*   r?   r@   r   )r5   r   rB   r   	next_nodes        r   rE   z+TestTreewidthMinFillIn.test_heuristic_abort   sm     	$AuE!H]]1% $6!HLLO$	$
 *%0	5r   c                 H    t        j                         }t        |      \  }}yrG   r   r)   r   rI   s      r   rJ   z'TestTreewidthMinFillIn.test_empty_graph   s    HHJ$Q'1r   c                     t        j                         }|j                  d       |j                  d       t        |      \  }}|dk(  sJ y rL   )r   r)   rM   r   r;   s       r   rN   z/TestTreewidthMinFillIn.test_two_component_graph   s=    HHJ	

1	

1,Q/	1A~~r   c                 F    t        j                  dg      }t        |       y rP   rl   rR   s     r   rS   z.TestTreewidthMinFillIn.test_not_sortable_nodes   s    HHhZ a r   c                    | j                   D ci c]  }|t        | j                   |         |hz
  ! }}t        |      }g }||j                  |       ||   }t	        j
                  |d      D ]!  \  }}|||   vs||   j                  |       # |D ]  }|||   v s||   j                  |         ||= t        |      }||dd ddgk(  sJ yc c}w )z)Test first steps of min_fill_in heuristicNr    r$   r#   )r-   r?   r   r
   rU   rV   r@   rW   )r5   rX   r   rY   rZ   r[   rB   r   s           r   r\   z1TestTreewidthMinFillIn.test_heuristic_first_steps   s    @D?W?W
:;As4++A./1#55
 
 *%0	#LL##D!..tQ7 $1E!H$!HLLO$  /a(!HOOI./ i -e4I #  RayQF"""-
s   $CNr]   rc   r   r   re   re      s:    </ /8"(
!#r   re   )rU   networkxr   !networkx.algorithms.approximationr   r   +networkx.algorithms.approximation.treewidthr   r   r   r   re   rc   r   r   <module>rs      s3     *:, ,De# e#r   