
    GdJ                     &   d Z ddlZddlmZ d(dZ ej                    Ze	                    dgdd           e	                    g d	d
d           e	                    ddgdd           e	                    dgdd           e
                    g d           d e                    d          D             Z ej        e          Z ej        d            ej        eedd            ej        eede            ej        eed            ej        d             ej        d!            ej                      eed"          \  ZZeD ]Z ej        e          Z ej        d            ej        eedd           d# e                    d          D             Z ej        eee$            ej        eed            ej        d             ej        d%            ej                      ej                    ZeD ]Z ej        ee          Ze
                    e                                            ej        ee          sJ d& e                    d          D             Z ej        e          Z ej        d            ej        eedd            ej        eede            ej        eed            ej        d             ej        d'            ej                     dS ))a3  
=========
Subgraphs
=========
Example of partitioning a directed graph with nodes labeled as
supported and unsupported nodes into a list of subgraphs
that contain only entirely supported or entirely unsupported nodes.
Adopted from 
https://github.com/lobpcg/python_examples/blob/master/networkx_example.py
    NTc                    	 d                       d          D             d                       d          D             	                                                     	fd j                                        D                                            	fd j                                        D                        t          j                    }|                    t           j	                  t          j	                  z
             |rd                      d          D             }t          j
                  }t          j        d	
           t          j        |dd           t          j        ||           t          j        |d           t          j        d           t          j        d           t          j                     t          j
        |          }t          j        d	
            fd|j         D             }t          j        ||dd           t          j        |||           t          j        ||d           t          j        d           t          j        d           t          j                     fdt          j                                                  D             }||fS )zPartition a directed graph into a list of subgraphs that contain
    only entirely supported or entirely unsupported nodes.
    c                 $    h | ]\  }}|d k    |S )	supported .0nds      <share/doc/networkx-3.1/examples/algorithms/plot_subgraphs.py	<setcomp>z%graph_partitioning.<locals>.<setcomp>   s&    SSSTQ!{BRBRqBRBRBR    	node_typedatac                 $    h | ]\  }}|d k    |S )unsupportedr   r   s      r   r   z%graph_partitioning.<locals>.<setcomp>   s&    WWWtq!ADVDVDVDVDVr   c              3   l   K   | ].\  }}|v r%|                                 D ]\  }}|v 	|||fV  /d S Nitemsr   r	   nbrsnbrr
   supported_nodesunsupported_nodess        r   	<genexpr>z%graph_partitioning.<locals>.<genexpr>   so        Atjjll  C###	 
C $#### r   c              3   l   K   | ].\  }}|v r%|                                 D ]\  }}|v 	|||fV  /d S r   r   r   s        r   r   z%graph_partitioning.<locals>.<genexpr>"   so        At!!!jjll "!C/!!	 
C "!!!! r   c                     g | ]\  }}|S r   r   )r   _cs      r   
<listcomp>z&graph_partitioning.<locals>.<listcomp>0   s    AAAdaAAAr   
node_color   r$   figsize333333?kalpha
edge_colorr"      	font_sizeoffz*The stripped graph with the edges removed.c                 6    g | ]}j         |         d          S r,   )nodes)r   r	   Gs     r   r!   z&graph_partitioning.<locals>.<listcomp><   s$    AAAAqwqz,'AAAr   zThe removed edges.c                 ^    g | ])}                     |                                          *S r   )subgraphcopy)r   r    Hs     r   r!   z&graph_partitioning.<locals>.<listcomp>H   s<       !"

1  r   )r2   r6   remove_edges_fromadjr   nxDiGraphadd_edges_fromsetedgesspring_layoutpltfiguredraw_networkx_edgesdraw_networkx_nodesdraw_networkx_labelsaxistitleshowconnected_componentsto_undirected)
r3   plotting	G_minus_H_node_colors_posncl	subgraphsr7   r   r   s
   `      @@@r   graph_partitioningrP      s   
 TSQWW+W%>%>SSSOWWqwwKw'@'@WWW 	
A     u{{}}           u{{}}      
IS\\CLL8999 AAagg<g&@&@AAA""
6""""
q$ccBBBB
q$<@@@@
426666	>???


	**
6""""AAAAAAA
y$ccJJJJ
y$3????
	42>>>>	&'''


   &(&=aoo>O>O&P&P  I ir   Ininputb)r   r"   )ACEFr   gBDr   rOutoutputm))rQ   rT   )rT   rY   )rY   rU   )rY   rZ   )rZ   rV   )rU   rW   )rV   rW   )rW   r\   c                     g | ]\  }}|S r   r   r   r   ncs      r   r!   r!   n   s    AAA%!R2AAAr   r"   r   r#   r%   r'   r(   r)   g?)r*   r"   r-   r.   r0   zThe original graph.)rJ   c                     g | ]\  }}|S r   r   r`   s      r   r!   r!      s    KKK2KKKr   r,   zOne of the subgraphs.c                     g | ]\  }}|S r   r   r`   s      r   r!   r!      s    CCC%!R2CCCr   zThe reconstructed graph.)T) __doc__networkxr:   matplotlib.pyplotpyplotr@   rP   r;   G_exadd_nodes_fromr<   r2   node_color_listspectral_layoutposrA   rB   rC   rD   rE   rF   rG   subgraphs_of_G_exremoved_edgesr5   r?   rM   node_color_list_cG_ex_rcomposer>   is_isomorphicr   r   r   <module>rs      s  	 	          <  <  <  < P rz||   TFg#  > > >   (((KC  P P P   S#J-C  H H H   UGxC  @ @ @   	 	 	  " BA4::<:#@#@AAAb 

6      tS < < < <  tS H H H H  cR 0 0 0 0  		
       


 $6#5dT#J#J#J   = " 	 	H2H%%DCJvB8TEEEEKK\)J)JKKKB8T6GHHHHBHdb9999CHUOOOCI%&&&CHJJJJ 
! * *HRZ))FF   m))++ , , , rf%% % % % DC6<<\<#B#BCCCb   

6      vs## > > > >  vs#/ J J J J  r 2 2 2 2  		
$ % % % 




r   