mò
®ƒ…Dc           @   s  d  k  Td k l Z d k l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z	 d	 „  Z
 d
 „  Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z e d j o e d d ƒ Z e e ƒ Z n d S(   (   t   *(   s   ceil(   s
   Game_grapht   Labelled_treec           B   sA   t  Z d d g Z d  g  d „ Z d d „ Z d „  Z d „  Z RS(   Nt   labelt   childrenc         C   s   | |  _  | |  _ d  S(   N(   R   t   selfR   (   R   R   R   (    (    t   tree_decomposition.pyt   __init__   s    	i    c         C   sa   d | d t t |  i ƒ ƒ d d !d } | d 7} x$ |  i D] } | | i | ƒ 7} q@ W| S(   Nt    t   {i   iÿÿÿÿs   }
i   (	   t   indentt   strt   listR   R   t   sR   t   ut   _rec_str(   R   R	   R   R   (    (    R   R      s    ,

 c         C   s
   |  i ƒ  S(   N(   R   R   (   R   (    (    R   t   __str__    s    c         c   s1   x& |  i D] } x | D]
 } | Vq Wq
 W|  Vd  S(   N(   R   R   t   tR   (   R   R   R   (    (    R   t   __iter__#   s    
  (   t   __name__t
   __module__t	   __slots__t   NoneR   R   R   R   (    (    (    R   R      s
   	t   Tree_decompositionc           B   sJ   t  Z h  g  d „ Z d „  Z d „  Z d „  Z d Z d „  Z d „  Z RS(   Nc         C   s   t  i |  | | ƒ d  S(   N(   R   R   R   t   bagR   (   R   R   R   (    (    R   R   +   s    c         C   s   |  i S(   N(   R   R   (   R   (    (    R   t   get_bag.   s    c         C   s   |  i i | ƒ d  S(   N(   R   R   t   addt   v(   R   R   (    (    R   t
   add_to_bag1   s    c         C   s   |  i i | ƒ d  S(   N(   R   R   t   discardR   (   R   R   (    (    R   t   remove_from_bag4   s    s   Bags of vertex not Connectedc         C   sw   | |  i ƒ  j o3 | o | o |  i ‚ n t } | i |  ƒ n t } x$ |  i	 D] } | i | | | ƒ qV W| S(   N(   R   R   R   t   nodest   parentt   not_conn_ext   Truet   appendt   FalseR   R   t   _find(   R   R   R   R   R   (    (    R   R$   :   s    
 c         C   sÙ   xÒ | D]Ê } y; g  } |  i | | t ƒ | p d t | ƒ GHt Sn Wn) |  i j
 o d t | ƒ GHt Sn XxZ | | D]N } xE | D] } | | i
 ƒ  j o PqŒ qŒ Wd t | ƒ t | ƒ f GHt Sq Wq Wt S(   sX   
        Check if decomposition is a correct tree decomposition of the graph G.
        s   B^{-1}(%s) emptys   B^{-1}(%s) not connecteds   {%s,%s} not coveredN(   t   GR   t   B_invR   R$   R#   R
   R    t   wR   R   R!   (   R   R%   R'   R&   R   R   (    (    R   t   checkG   s*      	  	(	   R   R   R   R   R   R   R    R$   R(   (    (    (    R   R   *   s   				c   
      C   sf  h  } h  } g  }	 x6 | D]. } | | j o d  | | <|	 i | ƒ q q Wx|	 o|	 i ƒ  } | | j o | | | f Sn xÒ |  i | ƒ D]Á } | | j o> | | j p | | i | ƒ j o |	 i | ƒ | | | <q | | j oc | | i | ƒ j oM | | } | d  j o2 | | j o% |	 i | ƒ | | | <| | | <qPq q WqN Wd  | | f S(   N(   t   predt	   pred_predt   stackt   XR   t   PR   R"   t   popt   YR%   t   adj_vertR'   t   gett   x(
   R%   R,   R/   R-   R2   R)   R*   R'   R   R+   (    (    R   t   _augmenting_path_treeb   s2     
   0#

c         C   sb   g  } xU |  d  j oG | i |  ƒ |  | j o | i | |  ƒ | |  }  q	 | |  }  q	 W| S(   N(   t   qR   R   R"   R*   R)   (   R   R)   R*   R4   (    (    R   t   _augmenting_path€   s     c         C   sÚ   | i ƒ  } d  | | <d  } xq | d  d  d … D]\ } | | j o3 | | j o | | } q† | | } | | | <n d  } | | | <| } q0 Wh  } x= |  D]5 } x, | | j o | | } | | | <| } q¦ Wq W| S(   Niÿÿÿÿ(	   R4   R.   R   R   R-   t   pvR'   t   QR/   (   R/   R-   R4   R7   R6   R'   R   (    (    R   t   _augmentŒ   s,    
 


  

c         C   sr   t  ƒ  } xb |  D]Z } | | j oG x3 | d  j o% | | j o
 | } n | | } q& W| i | ƒ q q W| S(   N(	   t   setR   R/   R   R-   R   R)   t   cR   (   R/   R-   R)   R:   R   R   (    (    R   t   _extract_separator¤   s    	   
iÿÿÿÿc   
      C   s¡   | d j o t |  ƒ } n h  } xw t | ƒ D]i } t |  | | | ƒ \ }	 } } |	 d j o t | | | ƒ Sn t |	 | | ƒ } t | | | ƒ } q0 Wd S(   sÂ   
    Return set S that separates X from Y of minimum size. Optional argument k
    is an upper bound for the size of S; if no separator of size at most k
    exists then 'None' is returned.
    iÿÿÿÿN(   t   kt   lenR%   R-   t   ranget   iR3   R,   R/   R   R)   R*   R   R;   R5   R4   R8   (
   R%   R,   R/   R<   R?   R)   R*   R4   R-   R   (    (    R   t	   separator®   s       c   
      c   s>  t  |  ƒ } t | d ƒ } t ƒ  }	 t |  ƒ } | d j o |	 | f Vn d } xå t
 oÝ | d } d | d <| | j o Pn | | d | | <| d | | d <|  | } | |	 j o( |	 i | ƒ | d 8} | i | ƒ n% |	 i | ƒ | d 7} | i | ƒ | | j o
 | j n o |	 | f VqU qU Wd S(   sK   
    Generates all partitions (X,Y) of W such that low <= |X| <= high.
    i   i    N(   R=   t   Wt   nR>   t   fR9   R,   R/   t   lowt   size_XR!   t   jR'   R   R   t   high(
   RA   RD   RG   R'   RC   RF   RB   RE   R/   R,   (    (    R   t	   partitionÀ   s4     	 

 


c   	      C   s#  t  | ƒ } xt | | d d | ƒ D]ô \ } } t | ƒ | d j oI xÎ | D]: } t	 |  | i | g ƒ | | d ƒ } | o | SqQ qQ Wq' t | ƒ | d j oI xn | D]: } t	 |  | | i | g ƒ | d ƒ } | o | Sq± q± Wq' t	 |  | | | d ƒ } | o | Sq' q' Wd S(   s¯   
    G=(V,E) graph, W subset of W of size 3k+1. Method computes separator S of
    size at most k+1 such that each component of G-S contains at most 2k
    elements of W.
    i   i   N(   R   RA   t   wlRH   R<   R,   R/   R=   t   yR@   R%   t   uniont   SR2   R   (	   R%   RA   R<   RI   RJ   RL   R2   R/   R,   (    (    R   t
   separationÝ   s(       %  %  c         C   sK  |  i | |  i j o d Sn |  i d | d j  o t t |  i ƒ  ƒ ƒ Sn t |  ƒ } x3 t
 | ƒ d | d j  o | i | i ƒ  ƒ q` Wt |  | | ƒ } | d j o d Sn t t |  | ƒ ƒ } g  } xW | D]O } t |  | i | ƒ ƒ } | i | i | ƒ ƒ }	 | i t | |	 | ƒ ƒ qÜ W| i | ƒ }
 t |
 | ƒ S(   sH   
    Main recursive subroutine of the tree decomposition algorithm.
    i   i   i   i   N(   R%   t   sizeR<   t   orderR   R   R9   t   verticest   iterR?   R=   RA   R   t   nextRM   RL   t   connected_componentst   minus_verticest   CCR   t   Ct   induced_subgraphRK   t   Ht   intersectionR,   R"   t   RS_rec_tree_dect   B_root(   R%   RA   R<   R?   RX   RV   R   RU   RL   R,   R[   (    (    R   RZ   ò   s*       c         C   s]   | d j o |  i d } n x8 t | ƒ D]* } t |  t ƒ  | ƒ } | o | Sq+ q+ Wd S(   sy   
    Computes approximate tree-decomposition of G of width at most 4tw(G)+1 by
    recursively separating the graph.
    i   N(	   R<   R   R%   RO   R>   t   lRZ   R9   t   D(   R%   R<   R]   R\   (    (    R   t   RS_tree_dec	  s        c         c   ss   t  |  ƒ } g  V| d j oR xO t | ƒ D]= } x4 t |  |  | d ƒ D] } | i |  | ƒ | VqH Wq* Wn d S(   s=   
    Generates all sublists of size at most k of list X.
    i    i   N(	   R=   R,   RB   R<   R>   t   lastt   sublistsR   R"   (   R,   R<   R_   R   RB   (    (    R   R`     s       c         C   sO   g  } xB | D]: } x1 |  | D]% } | | j o | i | ƒ Pq q Wq W| S(   sK   
    G graph, S,C sets of vertices.
    Computes neighbours of C in S.
    N(   t   byRL   R   R%   R'   RV   R"   (   R%   RL   RV   R'   R   Ra   (    (    R   t   boundary!  s       c         C   s.  t  ƒ  }
 |  i ƒ  } | i ƒ  x¹t | | ƒ D]¨}	 t	 t
 |  |	 ƒ ƒ } xŠ| D]‚} t |  |	 | ƒ } t | ƒ t |	 ƒ j  o qQ n t | ƒ } t |	 ƒ | f } |
 i | d ƒ | i ƒ  } x| D]} |	 | g } | i | ƒ t |  | ƒ } t	 | ƒ } g  } x´ | D]Y } t |  | | ƒ } t | ƒ | j o Pn | i ƒ  | i t | ƒ t | ƒ f ƒ qW| t | ƒ f } |
 i | d ƒ |
 i! | | ƒ x | D] } |
 i! | | ƒ q¨W| i# | ƒ qÊ WqQ Wq/ W|
 i d f  f d ƒ x6 t	 |  ƒ D]( } |
 i! d f  f f  t | ƒ f ƒ qþW|
 S(   sM   
    Creates game graph for the robber and cops game with k+1 cops on G.
    i    i   N(%   t
   Game_grapht   gameR%   RP   t   Vt   sortR`   R<   t   copsRS   RT   t   Rt   robberRb   t   bR=   t   mint   rt   tuplet   post
   add_vertext   copyt   robber1R:   t   cops1t   removeRW   RX   t   R_newt   potential_movest
   robber_newR"   t   p1t   add_edget   p2R   R   (   R%   R<   Rr   Rn   Rv   Ru   Rq   RX   Rw   Rg   Rd   Rh   Re   Ri   Ry   R:   Rj   Rl   Rt   (    (    R   t   create_game_graph.  sT     	
    
#  &c         C   sO   | d } g  } x/ |  | D]# } | i t |  | | | ƒ ƒ q Wt | | ƒ S(   Ni   (	   Rn   R   R   Rd   t   pos1R"   t   _td_from_strategyR   R   (   Rd   R   Rn   R   R   R{   (    (    R   R|   b  s    
 !c         C   s„   | d j o |  i d } n x_ t | ƒ D]Q } t |  | ƒ } | i d ƒ } d f  f } | | j o t | | | ƒ Sq+ q+ Wd S(   s^   
    Computes tree decomposition of width at most k through the robber and cops
    game.
    i   i    N(   R<   R   R%   RO   R>   R\   Rz   Rd   t   winning_strategyR   t	   start_posR|   (   R%   R<   R   Rd   R\   R~   (    (    R   t   game_tree_decj  s       t   __main__i   i   N(   t   grapht   matht   ceilt   gamesRc   t   objectR   R   R3   R5   R8   R;   R@   RH   RM   RZ   R   R^   R`   Rb   Rz   R|   R   R   t   gridR%   t   D1(   RZ   R3   R   R   Rc   Rb   R%   R   Rƒ   RM   R;   R`   R8   RH   Rz   R5   R@   R^   R‡   R|   (    (    R   t   ?   s*   8				
						4		