
    h                         S r SSKJr  SSKrSSKJr  SSKJrJ	r	J
r
  S/rS r\	" S5      \
" S	5      \R                  " S
S9SS j5       5       5       rg)uB   Functions for computing the Kernighan–Lin bipartition algorithm.    )countN)is_partition)
BinaryHeapnot_implemented_forpy_random_statekernighan_lin_bisectionc              #     ^ ^^#    [        5       [        5       4=u  p#m[        [        5       TT 5       H9  u  pEn[        U4S jU 5       5      nTU   R	                  XE(       a  UOU* 5        M;     UU U4S jnSn	Sn
U(       aa  U(       aY  UR                  5       u  pGU" X$5        UR                  5       u  pU" X;5        XU-   -  n
U	S-  n	XXK44v   U(       a
  U(       a  MW  gggg7f)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   F   >#    U  H  u  pTU   (       a  UOU* v   M     g 7f)N ).0vwsides      ]/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/community/kernighan_lin.py	<genexpr>'_kernighan_lin_sweep.<locals>.<genexpr>   s      >gda$q'Qr)g   !c                    > TU    HD  u  p#TTU      nUR                  U5      nUc  M#  USXL a  U* OU-  -  nUR                  X%S5        MF     g )N   T)getinsert)	costs_xxyr   costs_ycost_ycostsedgesr   s	         r   _update_costs+_kernighan_lin_sweep.<locals>._update_costs   sY    !HDADGnG[[^F!!W%7rQ??q$/     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7NFU!%'477>g>>fQ&vg> 80 	
AG
VJJL	f JJL	f F?"	Q1&   VV&V&s   CC%!C%directed   weight)
edge_attrsc                   ^ [        U 5      n[        U 5      nUR                  U5        [        U5       VVs0 s H  u  pxX_M	     n	nnUc  S/US-  -  S/US-   S-  -  -   n
OC Uu  p[        XU45      (       d  [        R                  " S5      eS/U-  n
U H
  nSXU   '   M     U R                  5       (       ab  U VVVs/ s HP  nX   R                  5        VVs/ s H-  u  nnX   [        U4S jUR                  5        5       5      4PM/     snnPMR     nnnnOPU VVVs/ s H?  oU   R                  5        VVs/ s H  u  nnX   UR                  TS5      4PM     snnPMA     nnnn[        U5       HI  n[        [        UU
5      5      n[!        U5      u  nnnUS:  a    OUSU  H  u    nu  pSX'   SX'   M     MK     [#        Xj5       VVs1 s H  u  nnUS:X  d  M  UiM     nnn[#        Xj5       VVs1 s H  u  nnUS:X  d  M  UiM     nnnX4$ s  snnf ! [        [
        4 a  n[        R                  " S5      UeSnAff = fs  snnf s  snnnf s  snnf s  snnnf s  snnf s  snnf )uG  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   F   >#    U  H  oR                  TS 5      v   M     g7f)r"   N)r   )r   er2   s     r   r   *kernighan_lin_bisection.<locals>.<genexpr>u   s     DAuuVQ//r   )lenlistshuffle	enumerate	TypeError
ValueErrornxNetworkXErrorr   is_multigraphitemsr$   valuesr   ranger/   minr#   )G	partitionmax_iterr2   seednlabelsr,   r   indexr   ABerrar(   dr   r6   r   min_costmin_i_ss      `                    r   r   r   +   s   j 	AA!WFLL'/0/daQT/E0sa1f~A!| 44	JDA A1v&&""#677sQwADqN  	 

  DJJL(DAq 3DDDE(  	 
 NT
MSdjjlCldaehfa()lCV 	 
 8_)%67 Z%q=!&5MLAq&1DGDG *  6(3(tq!AF(A36(3(tq!AF(A34KQ 1 :& 	J""#?@cI	J
 D
 	43se   H#H  I4II"I=#I II&#I&:I,I,I	-II	II)N
   r2   N)__doc__	itertoolsr   networkxr>   -networkx.algorithms.community.community_utilsr   networkx.utilsr   r   r   __all__r/   _dispatchabler   r   r!   r   <module>r]      s_    H   F K K$
%!> Z X&] '  !]r!   