+
    )i                         R t ^ RIHt ^ RIHt ^ RIHt ^ RIt^ RI	H
t
 R.tRtRtRt^tR	tR
tR t]P&                  ! RRR7      RR l4       tR# )z=Lukes Algorithm for exact optimal weighted tree partitioning.)deepcopy)	lru_cache)choiceN)not_implemented_forlukes_partitioningweightg      ?
partitionsi   c              #   h   "   W8  g   Q h\        W^,           4       F  pW V,
          3x  K  	  R# 5i)   N)range)nmin_size_of_first_partp1s   && a/var/www/html/photoedit/myenv/lib/python3.14/site-packages/networkx/algorithms/community/lukes.py_split_n_fromr      s3      &&&*E2b&j 3s   02node_weightedge_weight)
node_attrs
edge_attrsc           
     j
  aaa!a"a#a$a%a& \         P                  ! V 4      '       g   \         P                  ! R4      h\         P                  ! V 4      '       dT   V P	                  4        UUu. uF  w  rEV^ 8X  g   K  VNK  	  ppp\        V4      ^8X  g   Q hV^ ,          p\        V 4      pM4\        \        V P                  4      4      p\         P                  ! W4      pSe   Sfa   \        V 4      o&Sf'   \         P                  ! S&\        \        4       \        oSf'   \         P                  ! S&\        \         4       \         oMV o&\         P"                  ! S&S4      P%                  4       pV F(  p	\'        V	\(        4      '       d   K  \+        RS R24      h	  \-        R4      R 4       o!\-        R4      V!3R l4       p
\/        \0        4      VV&3R l4       o#V#3R	 lo$\/        \0        4      VV&3R
 l4       o%R o"V"V$V%3R lp\3        S!! V4      4      pV F}  p/ VP                  V,          \4        &   S&P                  V,          S,          pV0.VP                  V,          \4        ,          V&   V0.VP                  V,          \4        ,          ^ &   K  	  VP                   U	u. uF  qV9  g   K  V	NK  	  up	 FZ  p/ VP                  V,          \4        &   S&P                  V,          S,          pV0.VP                  V,          \4        ,          V&   K\  	  \         P6                  ! V4        V
! V4      pS&P                  V,          S,          p^ pRp/ p\         P8                  ! VV4      pV EFS  p\;        VV^,           4       F  p\=        VV4       F  w  ppVVP                  V,          \4        ,          9  g$   VVP                  V,          \4        ,          9  d   KN  VP                  V,          \4        ,          V,          pVP                  V,          \4        ,          V,          pV! VVVVV4      w  ppVV9  g   VV,          ^,          V8  d   VV3VV&   VV8:  g   K  TpTpK  	  K  	  VP?                  4        F*  w  pw  pp VVP                  V,          \4        ,          V&   K,  	  VPA                  4        EKV  	  VVP                  V,          \4        ,          ^ &   VPC                  V4       VV8X  g   EK  VP                  V,          \4        ,          ^ ,          # u uppi u up	i )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 treesNz9lukes_partitioning needs integer values for node_weight ()
undirectedc              3   x   "   V P                    F%  p\        P                  ! W4      '       d   K!  Vx  K'  	  R # 5iN)nodesnxdescendants)grxs   & r   _leaves#lukes_partitioning.<locals>._leavesv   s)      A>>"(( s   ):
:c                 J  <a \        S! V 4      4      o\        V P                  4      S,
           Fo  p\        ;QJ d3    V3R  l\        P                  ! W4       4       F  '       d   K   RM(	  RM$! V3R  l\        P                  ! W4       4       4      '       g   Km  Vu # 	  R# )c              3   ,   <"   T F	  qS9   x  K  	  R # 5ir    ).0r   tleavess   & r   	<genexpr>Glukes_partitioning.<locals>._a_parent_of_leaves_only.<locals>.<genexpr>   s     ?)>A<)>s   FTN)setr   allr   r   )r   r   r%   r   s   & @r   _a_parent_of_leaves_only4lukes_partitioning.<locals>._a_parent_of_leaves_only}   sc    gbk"RXX((As?)>?sss?)>??? )    c                    < SP                    Uu. uF$  q^ ,          V 9   g   K  V^,          V 9   g   K"  VNK&  	  pp\        VV3R lV 4       4      # u upi )    c              3   X   <"   T F  pSP                   V,          S,          x  K!  	  R # 5ir   )edges)r$   er   safe_Gs   & r   r&   @lukes_partitioning.<locals>._value_of_cluster.<locals>.<genexpr>   s!     EA6<<?;//   '*)r0   sum)clusterr1   valid_edgesr   r2   s   &  r   _value_of_cluster-lukes_partitioning.<locals>._value_of_cluster   sH    "(,,V,QA$'/qadgoqq,VEEEE Ws   AAAc                 .   < \        V3R  lV  4       4      # )c              3   F   <"   T F  pS! \        V4      4      x  K  	  R # 5ir   )	frozenset)r$   cr8   s   & r   r&   Blukes_partitioning.<locals>._value_of_partition.<locals>.<genexpr>   s     FIq$Yq\22Is   !r5   )	partitionr8   s   &r   _value_of_partition/lukes_partitioning.<locals>._value_of_partition   s    FIFFFr,   c                 0   < \        VV3R  lV  4       4      # )c              3   X   <"   T F  pSP                   V,          S,          x  K!  	  R # 5ir   )r   )r$   r   r   r2   s   & r   r&   Alukes_partitioning.<locals>._weight_of_cluster.<locals>.<genexpr>   s!     AA6<<?;//r4   r?   )r6   r   r2   s   &r   _weight_of_cluster.lukes_partitioning.<locals>._weight_of_cluster   s    AAAAr,   c                 t    V  Uu. uF  q!V9   g   K  VNK  	  pp\        V4      ^8X  g   Q hV^ ,          # u upi )r
   )len)r@   noder=   ccxs   &&  r   _pivot"lukes_partitioning.<locals>._pivot   s:    #1)Qqyqq)13x1}}1v 2s   55c                 4  <a
a S! W4      oS! W4      o
SP                  S
4      pS! \        V4      4      V8:  dN   \        \        V3R  lV 4      4      p\        \        V
3R lV4      4      pV.V,           V,           pVS! V4      3# W,           p	V	S! V	4      3# )c                    < V S8g  # r   r#   )r   rK   s   &r   <lambda>Clukes_partitioning.<locals>._concatenate_or_merge.<locals>.<lambda>   	    Sr,   c                    < V S8g  # r   r#   )r   ccis   &r   rP   rQ      rR   r,   )unionr<   listfilter)partition_1partition_2r   i
ref_weight	merged_xicp1cp2option_2option_1rT   rK   rL   rA   rF   s   &&&&&     @@r   _concatenate_or_merge1lukes_partitioning.<locals>._concatenate_or_merge   s    [$[$IIcN	 i	23zAv0+>?Cv0+>?C!{S(3.H0:::"0H0:::r,   )"r   is_treeNotATreeis_directed	in_degreerI   r   r   rV   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   r   droott_G
all_n_attrr   r*   ra   leaves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   rL   r8   rA   rF   r2   s'   &&ff                             @@@@@@r   r   r      s_   ^ ::a==kkBCC>>!"#++-:-$!16AA-D:t9>!>7D1+C$qww-(D++a&C k1!""6<B"K""6<B"K ''<CCEJ!S!!++6-q:   & '
 & ' &'F (FG &'B (B
;$ F 		"d||B,&(TF		"dD!#%$		"dA	  !YY:Y6/!!Y:!#		%||E";/).y		%t$ ; OOC )#.ll6*;7
	sF3#F;15)![9DAq6!24!88CIIf$5d$;; !IIf-d3A6EIIf-d3A6E"7uffVW"XKD%	)Yq\!_u-D'+U{	! "U*%*
)-' : 64 .7__->))$b-=		&!$'* .?OO; $B &4		&$"m,T> 99T?4(++i ;~ ;s   *T*:T*%T02T0)NN)__doc__copyr   	functoolsr   randomr   networkxr   networkx.utilsr   __all__rj   ri   rm   rl   rt   rs   r   _dispatchabler   r#   r,   r   <module>r      sg    C     .
   ]}EF, FF,r,   