Ë
    D^(hý  ã                   ó¨   — d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 dgZd„ Z e	d«       e
d	«       ej                  d
¬«      dd„«       «       «       Zy)uB   Functions for computing the Kernighanâ€“Lin bipartition algorithm.é    )ÚcountN)Úis_partition)Ú
BinaryHeapÚnot_implemented_forÚpy_random_stateÚkernighan_lin_bisectionc              #   ó¦  ‡ ‡‡K  — t        «       t        «       fx\  }}Št        t        «       ‰‰ «      D ]4  \  }}}t        ˆfd„|D «       «      }‰|   j	                  ||r|n| «       Œ6 ˆˆ ˆfd„}d}	d}
|rX|rU|j                  «       \  }} |||«       |j                  «       \  }} |||«       |
||z   z  }
|	dz  }	|
|	||ff–— |r|rŒSyyyy­w)zè
    This is a modified form of Kernighan-Lin, which moves single nodes at a
    time, alternating between sides to keep the bisection balanced.  We keep
    two min-heaps of swap costs to make optimal-next-move selection fast.
    c              3   ó8   •K  — | ]  \  }}‰|   r|n| –— Œ y ­w)N© )Ú.0ÚvÚwÚsides      €úi/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/community/kernighan_lin.pyú	<genexpr>z'_kernighan_lin_sweep.<locals>.<genexpr>   s"   øè ø€ Ò>©d¨a°˜$˜qš'‘Q¨ rÓ)Ñ>ùs   ƒc                 óœ   •— ‰|   D ]C  \  }}‰‰|      }|j                  |«      }|€Œ"|d| |u r| n|z  z  }|j                  ||d«       ŒE y )Né   T)ÚgetÚinsert)	Úcosts_xÚxÚyr   Úcosts_yÚcost_yÚcostsÚedgesr   s	         €€€r   Ú_update_costsz+_kernighan_lin_sweep.<locals>._update_costs   sd   ø€ Ø˜!‘Hò 	0‰DˆAˆqØ˜D ™G‘nˆGØ—[‘[ “^ˆFØÑ!Ø˜! W°Ñ%7 ™r¸QÑ?Ñ?�Ø—‘˜q &¨$Õ/ñ	0ó    r   é   N)r   Úzipr   Úsumr   Úpop)r   r   Úcosts0Úcosts1ÚuÚside_uÚedges_uÚcost_ur   ÚiÚtotcostr   Úcost_vr   s   ``           @r   Ú_kernighan_lin_sweepr,      sè   úè ø€ ô (›\¬:«<Ð7Ð7�N€FˆF�UÜ!¤%£'¨4°Ó7ò ?Ñˆˆ6�7ÜÓ>°gÔ>Ó>ˆØˆf‰×Ñ˜Q©&¡°v°gÕ>ð?ö0ð 	
€AØ€GÙ
‘VØ—J‘J“L‰	ˆˆ6Ù�f˜aÔ Ø—J‘J“L‰	ˆˆ6Ù�f˜aÔ Ø�6˜F‘?Ñ"ˆØ	ˆQ‰ˆØ�q˜1˜a˜&Ð Ò ñ “Vˆ&�Vˆ&ùs   …CCÃCÚdirectedé   Úweight)Ú
edge_attrsc                 ó¤  ‡— t        | «      }t        | «      }|j                  |«       t        |«      D ��ci c]  \  }}||“Œ
 }	}}|€dg|dz  z  dg|dz   dz  z  z   }
n>	 |\  }}t        | ||f«      st        j                  d«      ‚dg|z  }
|D ]
  }d|
|	|   <   Œ | j                  «       r]|D ���cg c]M  }| |   j                  «       D ��cg c],  \  }}|	|   t        ˆfd„|j                  «       D «       «      f‘Œ. c}}‘ŒO }}}}nL|D ���cg c]=  }| |   j                  «       D ��cg c]  \  }}|	|   |j                  ‰d«      f‘Œ c}}‘Œ? }}}}t        |«      D ]H  }t        t        ||
«      «      }t!        |«      \  }}}|dk\  r n|d| D ]  \  }}\  }}d|
|<   d|
|<   Œ ŒJ t#        ||
«      D ��ch c]  \  }}|dk(  sŒ|’Œ }}}t#        ||
«      D ��ch c]  \  }}|dk(  sŒ|’Œ }}}||fS c c}}w # t        t
        f$ r}t        j                  d«      |‚d}~ww xY wc c}}w c c}}}w c c}}w c c}}}w c c}}w c c}}w )uß  Partition a graph into two blocks using the Kernighanâ€“Lin
    algorithm.

    This algorithm partitions a network into two sets by iteratively
    swapping pairs of nodes to reduce the edge cut between the two sets.  The
    pairs are chosen according to a modified form of Kernighan-Lin [1]_, which
    moves node individually, alternating between sides to keep the bisection
    balanced.

    Parameters
    ----------
    G : NetworkX graph
        Graph must be undirected.

    partition : tuple
        Pair of iterables containing an initial partition. If not
        specified, a random balanced partition is used.

    max_iter : int
        Maximum number of times to attempt swaps to find an
        improvement before giving up.

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

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
        Only used if partition is None

    Returns
    -------
    partition : tuple
        A pair of sets of nodes representing the bipartition.

    Raises
    ------
    NetworkXError
        If partition is not a valid partition of the nodes of the graph.

    References
    ----------
    .. [1] Kernighan, B. W.; Lin, Shen (1970).
       "An efficient heuristic procedure for partitioning graphs."
       *Bell Systems Technical Journal* 49: 291--307.
       Oxford University Press 2011.

    Nr   r   r   zpartition must be two setszpartition invalidc              3   óB   •K  — | ]  }|j                  ‰d «      –— Œ y­w)r   N)r   )r   Úer/   s     €r   r   z*kernighan_lin_bisection.<locals>.<genexpr>u   s   øè ø€ ÒD°A˜qŸu™u V¨Q×/ÑDùs   ƒ)ÚlenÚlistÚshuffleÚ	enumerateÚ	TypeErrorÚ
ValueErrorÚnxÚNetworkXErrorr   Úis_multigraphÚitemsr!   Úvaluesr   Úranger,   Úminr    )ÚGÚ	partitionÚmax_iterr/   ÚseedÚnÚlabelsr)   r   Úindexr   ÚAÚBÚerrÚar%   Údr   r3   r   Úmin_costÚmin_iÚ_Úss      `                    r   r   r   +   s–  ø€ ôj 	ˆA‹€AÜ�!‹W€FØ‡L�L�ÔÜ'¨Ó/×0‘d�a˜ˆQ�‰TÐ0€EÑ0àÐØˆs�a˜1‘f‰~  ¨¨A©°!¡|Ñ 4Ñ4‰ð	JØ‰DˆAˆqô ˜A  1˜vÔ&Ü×"Ñ"Ð#6Ó7Ð7Øˆs�Q‰wˆØò 	ˆAØˆD��q‘ŠNð	ð 	‡�Ôð ÷
ð 
ð
 ð ˜a™DŸJ™J›L÷á�A�qð �q‘œ3ÓD¸¿¹»ÔDÓDÒEõð
ˆó 
ð NT÷
ð 
ØHI°a¸±d·j±j³l×C©d¨a°ˆe�A‰h˜Ÿ™˜f aÓ(Ò)ÕCð
ˆò 
ô �8‹_ò ˆÜÔ)¨%°Ó6Ó7ˆÜ  ›ZÑˆ�%˜Ø�qŠ=Ùà! & 5˜Mò 	‰LˆAˆq‘&�1�aØˆD�‰GØˆD�ŠGñ	ðô ˜6 4Ó(×3‰tˆq�!¨A°«FŠÐ3€AÑ3Ü˜6 4Ó(×3‰tˆq�!¨A°«FŠÐ3€AÑ3Øˆaˆ4€KùóQ 1øô œ:Ð&ò 	JÜ×"Ñ"Ð#?Ó@ÀcÐIûð	Jüóùô
ùó Dùô
ùó 	4ùÛ3se   ·G9Á!G? Â4H2Ã1H,Ä H2ÄH?Ä,!H9ÅH?Æ<IÇ
IÇ IÇ.IÇ?H)ÈH$È$H)È,H2È9H?)Né
   r/   N)Ú__doc__Ú	itertoolsr   Únetworkxr:   Ú-networkx.algorithms.community.community_utilsr   Únetworkx.utilsr   r   r   Ú__all__r,   Ú_dispatchabler   r   r   r   ú<module>rY      sa   ðÙ Hå ã Ý Fß KÑ Kà$Ð
%€ò!ñ> �ZÓ Ù�ÓØ€×Ñ˜XÔ&ò]ó 'ó ó !ñ]r   