
    \Mh                         d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z
 dgZdZd	ZdZd
ZdZdZd Z ej        dd          dd            ZdS )z=Lukes Algorithm for exact optimal weighted tree partitioning.    )deepcopy)	lru_cache)choiceN)not_implemented_forlukes_partitioningweightg      ?   
partitionsi   c              #   X   K   | |k    sJ t          || dz             D ]}|| |z
  fV  d S )Nr	   )range)nmin_size_of_first_partp1s      c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/community/lukes.py_split_n_fromr      sY       &&&&&*AE22  !b&j     node_weightedge_weight)
node_attrs
edge_attrsc           
      V	   !"#$ t          j        |           st          j        d          t          j        |           rKd |                                 D             }t          |          dk    sJ |d         }t          |           }n6t          t          | j	                            }t          j
        | |          }bt          |           $'t          j        $t          t                     t          't          j        $t          t                      t           n| $t          j        $                                          }|D ]*}t'          |t(                    st+          d d          +t-          d          d	             t-          d          fd
            }t/          t0                    $fd              fd!t/          t0                    $fd            "d !"fd}	t3           |                    ##D ]d}
i |j	        |
         t4          <   $j	        |
                  }|
hg|j	        |
         t4                   |<   |
hg|j	        |
         t4                   d<   e#fd|j	        D             D ]G}i |j	        |         t4          <   $j	        |                  }|hg|j	        |         t4                   |<   Ht          j        |           	  ||          }$j	        |                  }d}d}i }t          j        ||          }|D ]%}t;          ||dz             D ]}t=          ||          D ]\  }}||j	        |         t4                   vs||j	        |         t4                   vr:|j	        |         t4                   |         }|j	        |         t4                   |         } |	|||||          \  }}||vs||         d         |k     r||f||<   ||k    r|}|}|                                D ]#\  }\  }}||j	        |         t4                   |<   $|                                  '||j	        |         t4                   d<   |!                    |           ||k    r|j	        |         t4                   d         S )u  Optimal partitioning of a weighted tree using the Lukes algorithm.

    This algorithm partitions a connected, acyclic graph featuring integer
    node weights and float edge weights. The resulting clusters are such
    that the total weight of the nodes in each cluster does not exceed
    max_size and that the weight of the edges that are cut by the partition
    is minimum. The algorithm is based on [1]_.

    Parameters
    ----------
    G : NetworkX graph

    max_size : int
        Maximum weight a partition can have in terms of sum of
        node_weight for all nodes in the partition

    edge_weight : key
        Edge data key to use as weight. If None, the weights are all
        set to one.

    node_weight : key
        Node data key to use as weight. If None, the weights are all
        set to one. The data must be int.

    Returns
    -------
    partition : list
        A list of sets of nodes representing the clusters of the
        partition.

    Raises
    ------
    NotATree
        If G is not a tree.
    TypeError
        If any of the values of node_weight is not int.

    References
    ----------
    .. [1] Lukes, J. A. (1974).
       "Efficient Algorithm for the Partitioning of Trees."
       IBM Journal of Research and Development, 18(3), 217–224.

    z&lukes_partitioning works only on treesc                 $    g | ]\  }}|d k    |S )r    ).0r   ds      r   
<listcomp>z&lukes_partitioning.<locals>.<listcomp>O   s!    :::$!Q166A666r   r	   r   Nz9lukes_partitioning needs integer values for node_weight ()
undirectedc              3   P   K   | j         D ]}t          j        | |          s|V  d S N)nodesnxdescendants)grxs     r   _leavesz#lukes_partitioning.<locals>._leavesv   sA        	 	A>"a(( 	 	r   c                     t           |                     t          | j                  z
  D ]4}t          fdt          j        | |          D                       r|c S 5d S )Nc              3       K   | ]}|v V  	d S r    r   )r   r%   tleavess     r   	<genexpr>zGlukes_partitioning.<locals>._a_parent_of_leaves_only.<locals>.<genexpr>   s'      ??A1<??????r   )setr!   allr"   r#   )r$   r   r)   r&   s     @r   _a_parent_of_leaves_onlyz4lukes_partitioning.<locals>._a_parent_of_leaves_only}   s~    ggbkk""RX( 	 	A????A)>)>????? 	 	r   c                 d      fdj         D             }t          fd|D                       S )Nc                 <    g | ]}|d          v |d         v |S )r   r	   r   )r   eclusters     r   r   zAlukes_partitioning.<locals>._value_of_cluster.<locals>.<listcomp>   s.    VVVQ!A$'//adgooqooor   c              3   >   K   | ]}j         |                  V  d S r    )edges)r   r0   r   safe_Gs     r   r*   z@lukes_partitioning.<locals>._value_of_cluster.<locals>.<genexpr>   s.      EEA6<?;/EEEEEEr   )r3   sum)r1   valid_edgesr   r4   s   ` r   _value_of_clusterz-lukes_partitioning.<locals>._value_of_cluster   sE    VVVV&,VVVEEEEEEEEEEEr   c                 :    t          fd| D                       S )Nc              3   H   K   | ]} t          |                    V  d S r    )	frozenset)r   cr7   s     r   r*   zBlukes_partitioning.<locals>._value_of_partition.<locals>.<genexpr>   s5      FFq$$Yq\\22FFFFFFr   r5   )	partitionr7   s    r   _value_of_partitionz/lukes_partitioning.<locals>._value_of_partition   s&    FFFFIFFFFFFr   c                 <    t          fd| D                       S )Nc              3   >   K   | ]}j         |                  V  d S r    )r!   )r   r   r   r4   s     r   r*   zAlukes_partitioning.<locals>._weight_of_cluster.<locals>.<genexpr>   s.      AAA6<?;/AAAAAAr   r<   )r1   r   r4   s    r   _weight_of_clusterz.lukes_partitioning.<locals>._weight_of_cluster   s)    AAAAAAAAAAAr   c                 Z    fd| D             }t          |          dk    sJ |d         S )Nc                     g | ]}|v |	S r   r   )r   r;   nodes     r   r   z6lukes_partitioning.<locals>._pivot.<locals>.<listcomp>   s    111Qtqyyqyyyr   r	   r   )len)r=   rD   ccxs    ` r   _pivotz"lukes_partitioning.<locals>._pivot   s8    1111)1113xx1}}}}1vr   c                 j  
  | |           ||          
                     
          } t          |                    |k    rVt          t          fd|                     }t          t          
fd|                    }|g|z   |z   }| |          fS | |z   }	|	 |	          fS )Nc                     | k    S r    r   )r%   rF   s    r   <lambda>zClukes_partitioning.<locals>._concatenate_or_merge.<locals>.<lambda>       S r   c                     | k    S r    r   )r%   ccis    r   rJ   zClukes_partitioning.<locals>._concatenate_or_merge.<locals>.<lambda>   rK   r   )unionr:   listfilter)partition_1partition_2r%   i
ref_weight	merged_xicp1cp2option_2option_1rM   rF   rG   r>   rA   s             @@r   _concatenate_or_mergez1lukes_partitioning.<locals>._concatenate_or_merge   s    f[!$$f[!$$IIcNN	 i	2233zAAv0000+>>??Cv0000+>>??C!{S(3.H00::::"[0H00::::r   c                     g | ]}|v|	S r   r   )r   r%   leavess     r   r   z&lukes_partitioning.<locals>.<listcomp>   s    :::!6//!///r   )"r"   is_treeNotATreeis_directed	in_degreerE   r   r   rO   r!   dfs_treeset_edge_attributesD_EDGE_VALUED_EDGE_Wset_node_attributesD_NODE_VALUED_NODE_Wget_node_attributesvalues
isinstanceint	TypeErrorr   r   CLUSTER_EVAL_CACHE_SIZEr+   PKEY_clear_cacher#   r   r   itemsclearremove_nodes_from)%Gmax_sizer   r   roott_G
all_n_attrr%   r-   rZ   lvslotinnerx_nodeweight_of_x
best_valuebest_partition	bp_bufferx_descendantsi_nodejabpart1part2partvaluewbest_part_for_vlvlr&   rG   r7   r>   rA   r\   r4   s%     ``                          @@@@@@@r   r   r      su   ^ :a== 'kBCCC>! 	'::!++--:::Dt99>>>>7D1++CC$qw--((D+a&&C k1!"6<BBB"K"6<BBB"K '<<CCEEJ  !S!! 	:+6: : :  	 &&  '&
 &&    '& &''F F F F F ('FG G G G G &''B B B B B ('B  
; ; ; ; ; ; ;$ F ( ( 	"d|B,&(TF	"dD!#%$	"dA::::SY::: 1 1!#	%|E";/).y	%t$$OC.,))#..l6*;7
	sF33# 	 	F;155 . .)![99 . .DAq6!24!888CIf$5d$;;; !If-d3A6EIf-d3A6E"7"7uffVW"X"XKD%	))Yq\!_u-D-D'+U{	! "U**%*
)-'.2 .7__->-> > >))$b-=	&!$'**OO &4	&$"m,,,T>> 9T?4(++].,r   )NN)__doc__copyr   	functoolsr   randomr   networkxr"   networkx.utilsr   __all__rd   rc   rg   rf   rn   rm   r   _dispatchabler   r   r   r   <module>r      s    C C                       . . . . . .
     ]}EEEF, F, F, FEF, F, F,r   