
    fi                         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dS )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 ]B\  }}}t          fd|D                       }|                             ||r|n|            C fd}d}	d}
|rd|rf|                                \  }} |||           |                                \  }} |||           |
||z   z  }
|	dz  }	|
|	||ffV  |r|`dS dS dS dS )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| V  d S )N ).0vwsides      /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/networkx/algorithms/community/kernighan_lin.py	<genexpr>z'_kernighan_lin_sweep.<locals>.<genexpr>   s6      >>da$q')QQr>>>>>>    c                     |         D ]P\  }}|                  }|                     |          }|&|d| |u r| n|z  z  }|                    ||d           Qd S )N   T)getinsert)	costs_xxyr   costs_ycost_ycostsedgesr   s	         r   _update_costsz+_kernighan_lin_sweep.<locals>._update_costs   s|    !H 	0 	0DAqDGnG[[^^F!!W%7%7rrQ??q&$///	0 	0r   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7NFFU!%''477 ? ?67>>>>g>>>>>fQ& =vg>>>>0 0 0 0 0 0 0 	
AG
 !V !JJLL	6fa   JJLL	6fa   6F?"	Qq1a&      !V ! ! ! ! ! ! ! ! !r   directed   weight)
edge_attrs
   c                 ^    t                     }t                     }|                    |           d t          |          D             |dg|dz  z  dg|dz   dz  z  z   }nq	 |\  }}	n.# t          t
          f$ r}
t          j        d          |
d}
~
ww xY wt           ||	f          st          j        d          dg|z  }|D ]}d||         <    	                                r fd|D             }n fd	|D             }t          |          D ]X}t          t          ||                    }t          |          \  }}}|dk    r n|d|         D ]\  }}\  }}d||<   d||<   Yd
 t          ||          D             }d t          ||          D             }	||	fS )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.

    c                     i | ]\  }}||	S r   r   )r   r)   r   s      r   
<dictcomp>z+kernighan_lin_bisection.<locals>.<dictcomp>c   s    000daQ000r   Nr   r   r   zpartition must be two setszpartition invalidc                 ^    g | ])}fd |                                          D             *S )c                     g | ]:\  }}|         t          fd |                                D                       f;S )c              3   D   K   | ]}|                     d           V  dS )r   Nr   )r   er/   s     r   r   z@kernighan_lin_bisection.<locals>.<listcomp>.<listcomp>.<genexpr>u   s1      DDAquuVQ//DDDDDDr   )r!   values)r   r%   dindexr/   s      r   
<listcomp>z6kernighan_lin_bisection.<locals>.<listcomp>.<listcomp>t   s[       Aq q3DDDDDDDDDE  r   itemsr   r   Gr<   r/   s     r   r=   z+kernighan_lin_bisection.<locals>.<listcomp>s   s]     
 
 

 	    aDJJLL  
 
 
r   c                 ^    g | ])}fd |                                          D             *S )c                 R    g | ]#\  }}|         |                     d           f$S r   r8   )r   r%   r9   r<   r/   s      r   r=   z6kernighan_lin_bisection.<locals>.<listcomp>.<listcomp>|   s4    CCCdaeAhfa(()CCCr   r>   r@   s     r   r=   z+kernighan_lin_bisection.<locals>.<listcomp>{   sJ     
 
 
HICCCCCadjjllCCC
 
 
r   c                 $    h | ]\  }}|d k    |S )r   r   r   r%   ss      r   	<setcomp>z*kernighan_lin_bisection.<locals>.<setcomp>   !    333tq!AFFFFFr   c                 $    h | ]\  }}|d k    |S rD   r   rF   s      r   rH   z*kernighan_lin_bisection.<locals>.<setcomp>   rI   r   )lenlistshuffle	enumerate	TypeError
ValueErrornxNetworkXErrorr   is_multigraphranger,   minr    )rA   	partitionmax_iterr/   seednlabelsr   ABerrar   r)   r   min_costmin_i_r%   r   r<   s   `  `                @r   r   r   +   s\   j 	AA!WWFLL00i//000Esa1f~A!| 44	JDAqq:& 	J 	J 	J"#?@@cI	JA1v&& 	8"#6777sQw 	 	ADqNN 

 
 
 
 
 

 
 
 

 
 
 
 
 
MS
 
 
 8__  )%6677 ZZ%q==E!&5&M 	 	LAq&1aDGDGG	 	43s64((333A33s64((333Aa4Ks   *A0 0BBB)Nr1   r/   N)__doc__	itertoolsr   networkxrQ   -networkx.algorithms.community.community_utilsr   networkx.utilsr   r   r   __all__r,   _dispatchabler   r   r   r   <module>ri      s    H H           F F F F F F K K K K K K K K K K$
%! ! !> Z  X&&&] ] ] '&  ! ] ] ]r   