Ë
    D^(h³  ã                   óš   — 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y)z=Lukes Algorithm for exact optimal weighted tree partitioning.é    )Údeepcopy)Ú	lru_cache)ÚchoiceN)Únot_implemented_forÚlukes_partitioningÚweightg      ð?é   Ú
partitionsi   c              #   óT   K  — | |k\  sJ ‚t        || dz   «      D ]  }|| |z
  f–— Œ y ­w)Nr	   )Úrange)ÚnÚmin_size_of_first_partÚp1s      úa/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/community/lukes.pyÚ_split_n_fromr      s@   è ø€ ð Ð&Ò&Ð&Ð&ÜÐ*¨A°©EÓ2ò ˆØ�!�b‘&ˆjÓñùs   ‚&(Únode_weightÚedge_weight)Ú
node_attrsÚ
edge_attrsc           
      ó®  ‡‡‡!‡"‡#‡$‡%‡&— t        j                  | «      st        j                  d«      ‚t        j                  | «      rI| j	                  «       D ��cg c]  \  }}|dk(  sŒ|‘Œ }}}t        |«      dk(  sJ ‚|d   }t        | «      }n4t        t        | j                  «      «      }t        j                  | |«      }‰�‰€Zt        | «      Š&‰€%t        j                  ‰&t        t        «       t        Š‰€(t        j                  ‰&t        t         «       t         Šn| Š&t        j"                  ‰&‰«      j%                  «       }|D ]!  }	t'        |	t(        «      rŒ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 ]`  }i |j                  |   t4        <   ‰&j                  |   ‰   }|hg|j                  |   t4           |<   |hg|j                  |   t4           d<   Œb |j                  D �	cg c]	  }	|	|vsŒ|	‘Œ c}	D ]E  }i |j                  |   t4        <   ‰&j                  |   ‰   }|hg|j                  |   t4           |<   ŒG t        j6                  |«       	  |
|«      }‰&j                  |   ‰   }d}d}i }t        j8                  ||«      }|D �]  }t;        ||dz   «      D ]¨  }t=        ||«      D ]—  \  }}||j                  |   t4           vs||j                  |   t4           vrŒ7|j                  |   t4           |   }|j                  |   t4           |   } ||||||«      \  }}||vs||   d   |k  r||f||<   ||k  sŒ”|}|}Œ™ Œª |j?                  «       D ]!  \  }\  }} ||j                  |   t4           |<   Œ# |jA                  «        �Œ ||j                  |   t4           d<   |jC                  |«       ||k(  r|j                  |   t4           d   S �Œ‡c c}}w c c}	w )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 treesr   r	   Nz9lukes_partitioning needs integer values for node_weight (ú)Ú
undirectedc              3   ód   K  — | j                   D ]  }t        j                  | |«      rŒ|–— Œ y ­w©N)ÚnodesÚnxÚdescendants)ÚgrÚxs     r   Ú_leavesz#lukes_partitioning.<locals>._leavesv   s.   è ø€ ð —‘ò 	ˆAÜ—>‘> " aÕ(Ø“ñ	ùs   ‚&0©0c                 ó¼   •‡— t         ‰| «      «      Št        | j                  «      ‰z
  D ].  }t        ˆfd„t        j                  | |«      D «       «      sŒ,|c S  y )Nc              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr   © )Ú.0r   Útleavess     €r   ú	<genexpr>zGlukes_partitioning.<locals>._a_parent_of_leaves_only.<locals>.<genexpr>�   s   øè ø€ Ò? A�1˜”<Ñ?ùs   ƒ)Úsetr   Úallr   r   )r   r   r%   r    s     @€r   Ú_a_parent_of_leaves_onlyz4lukes_partitioning.<locals>._a_parent_of_leaves_only}   sK   ù€ ä‘g˜b“kÓ"ˆÜ�R—X‘X“ Ñ(ò 	ˆAÜÓ?¬¯©¸¸AÓ)>Ô?Õ?Ø’ñ	ó    c                 óŠ   •— ‰j                   D �cg c]  }|d   | v sŒ|d   | v sŒ|‘Œ }}t        ˆˆfd„|D «       «      S c c}w )Nr   r	   c              3   óB   •K  — | ]  }‰j                   |   ‰   –— Œ y ­wr   )Úedges)r$   Úer   Úsafe_Gs     €€r   r&   z@lukes_partitioning.<locals>._value_of_cluster.<locals>.<genexpr>‡   s   øè ø€ ÒE°A�6—<‘< ‘? ;Õ/ÑEùó   ƒ)r-   Úsum)Úclusterr.   Úvalid_edgesr   r/   s      €€r   Ú_value_of_clusterz-lukes_partitioning.<locals>._value_of_cluster„   sE   ø€ à"(§,¡,ÖV˜Q°!°A±$¸'²/ÀaÈÁdÈgÂo’qÐVˆÐVÜÔE¸ÔEÓEÐEùò Ws   �A �A ¥A c                 ó,   •— t        ˆfd„| D «       «      S )Nc              3   ó@   •K  — | ]  } ‰t        |«      «      –— Œ y ­wr   )Ú	frozenset)r$   Úcr4   s     €r   r&   zBlukes_partitioning.<locals>._value_of_partition.<locals>.<genexpr>Š   s   øè ø€ ÒF°qÑ$¤Y¨q£\×2ÑFùs   ƒ©r1   )Ú	partitionr4   s    €r   Ú_value_of_partitionz/lukes_partitioning.<locals>._value_of_partition‰   s   ø€ ÜÓF¸IÔFÓFÐFr*   c                 ó.   •— t        ˆˆfd„| D «       «      S )Nc              3   óB   •K  — | ]  }‰j                   |   ‰   –— Œ y ­wr   )r   )r$   r   r   r/   s     €€r   r&   zAlukes_partitioning.<locals>._weight_of_cluster.<locals>.<genexpr>Ž   s   øè ø€ ÒA°A�6—<‘< ‘? ;Õ/ÑAùr0   r9   )r2   r   r/   s    €€r   Ú_weight_of_clusterz.lukes_partitioning.<locals>._weight_of_clusterŒ   s   ø€ äÔA¸ÔAÓAÐAr*   c                 ó^   — | D �cg c]	  }||v sŒ|‘Œ }}t        |«      dk(  sJ ‚|d   S c c}w )Nr	   r   )Úlen)r:   Únoder8   Úccxs       r   Ú_pivotz"lukes_partitioning.<locals>._pivot�   s9   € Ø#Ö1�Q t¨q¢yŠqÐ1ˆÐ1Ü�3‹x˜1Š}Ðˆ}Ø�1‰vˆùò 2s   …	*�*c                 ó  •‡
‡—  ‰| |«      Š ‰||«      Š
‰j                  ‰
«      } ‰t        |«      «      |k  rCt        t        ˆfd„| «      «      }t        t        ˆ
fd„|«      «      }|g|z   |z   }| ‰|«      fS | |z   }	|	 ‰|	«      fS )Nc                 ó   •— | ‰k7  S r   r#   )r   rB   s    €r   ú<lambda>zClukes_partitioning.<locals>._concatenate_or_merge.<locals>.<lambda>�   ó   ø€ ¨¨S©€ r*   c                 ó   •— | ‰k7  S r   r#   )r   Úccis    €r   rF   zClukes_partitioning.<locals>._concatenate_or_merge.<locals>.<lambda>ž   rG   r*   )Úunionr7   ÚlistÚfilter)Úpartition_1Úpartition_2r   ÚiÚ
ref_weightÚ	merged_xiÚcp1Úcp2Úoption_2Úoption_1rI   rB   rC   r;   r>   s             @@€€€r   Ú_concatenate_or_mergez1lukes_partitioning.<locals>._concatenate_or_merge•   s�   ú€ Ù�[ !Ó$ˆÙ�[ !Ó$ˆØ—I‘I˜c“Nˆ	ñ œi¨	Ó2Ó3°zÒAÜ”vÓ0°+Ó>Ó?ˆCÜ”vÓ0°+Ó>Ó?ˆCà!�{ SÑ(¨3Ñ.ˆHØÑ0°Ó:Ð:Ð:à" [Ñ0ˆHØÑ0°Ó:Ð:Ð:r*   )"r   Úis_treeÚNotATreeÚis_directedÚ	in_degreer@   r   r   rK   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)   rV   Ú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    rC   r4   r;   r>   r/   s'     ``                             @@@@@@r   r   r      s|  ÿ€ ô^ �:‰:�aŒ=Ü�k‰kÐBÓCÐCä�>‰>˜!ÔØ"#§+¡+£-×:™$˜!˜Q°1¸³6’AÐ:ˆDÑ:Ü�t“9 ’>Ð!�>Ø˜‘7ˆDÜ˜1“+‰Cäœ$˜qŸw™w›-Ó(ˆDä—+‘+˜a Ó&ˆCð Ð˜kÐ1Ü˜!“ˆØÐÜ×"Ñ" 6¬<¼ÔBÜ"ˆKØÐÜ×"Ñ" 6¬<¼ÔBÜ"‰Kàˆô ×'Ñ'¨°Ó<×CÑCÓE€JØò ˆÜ˜!œSÕ!Üð+Ø+6¨-°qð:óð ðô ˜Ó&ñó 'ðô
 ˜Ó&óó 'ðô Ô&Ó'ôFó (ðFôGô Ô&Ó'ôBó (ðBòö
;ô$ ‘˜“Ó€FØò (ˆØ ˆ�	‰	�"‰”dÑØ�|‰|˜BÑ Ñ,ˆØ&( T Fˆ�	‰	�"‰”dÑ˜DÑ!Ø#% $ ˆ�	‰	�"‰”dÑ˜AÒð	(ð !ŸY™YÖ:˜¨!°6ª/’!Ò:ò 1ˆØ!#ˆ�	‰	�%ÑœÑØ�|‰|˜EÑ" ;Ñ/ˆØ).¨ yˆ�	‰	�%ÑœÑ˜tÒ$ð1ô ‡O�O�CÔð Ù)¨#Ó.ˆØ—l‘l 6Ñ*¨;Ñ7ˆØˆ
ØˆØˆ	ÜŸ™ s¨FÓ3ˆØ#ó 	ˆFÜ˜;¨°1©Ó5ò .�Ü)¨!¨[Ó9ò .‘D�A�qà §¡¨6Ñ!2´4Ñ!8Ñ8Ø C§I¡I¨fÑ$5´dÑ$;Ñ;ð !àŸI™I fÑ-¬dÑ3°AÑ6�EØŸI™I fÑ-¬dÑ3°AÑ6�EÙ"7¸¸uÀfÈfÐVWÓ"X‘K�D˜%à 	Ñ)¨Y°q©\¸!©_¸uÒ-Dà'+¨U {˜	 !™ð " UÓ*Ø%*˜
Ø)-™ñ'.ð.ð4 .7¯_©_Ó->ò >Ñ)�Ñ)Ð$ bØ-=�—	‘	˜&Ñ!¤$Ñ'¨Ò*ð>à�O‰OÖð;	ðB &4ˆ�	‰	�&Ñœ$Ñ Ñ"Ø×Ñ˜mÔ,à�TŠ>ð —9‘9˜T‘?¤4Ñ(¨Ñ+Ð+ñ] ùóM ;ùò~ ;s   ÁQÁ)QÉ	QÉ Q)NN)Ú__doc__Úcopyr   Ú	functoolsr   Úrandomr   Únetworkxr   Únetworkx.utilsr   Ú__all__r^   r]   ra   r`   rh   rg   r   Ú_dispatchabler   r#   r*   r   ú<module>r�      si   ðÙ Cå Ý Ý ã Ý .àÐ
 €à€Ø€Ø€Ø€Ø€ØÐ òð €×Ñ˜]°}ÔEòF,ó FñF,r*   