
    9i/                        S r SSKrSSKJr  SSKJr  SSKr/ SQrS r	\R                  R                  S5      \R                  R                  S5      \R                  " S	S
9SS j5       5       5       r\R                  R                  S5      \R                  R                  S5      S 5       5       r\R                  R                  S5      SSS.S j5       rg)zOFunctions for splitting a network into two communities (finding a bipartition).    N)deepcopy)count)kernighan_lin_bisectionspectral_modularity_bipartitiongreedy_node_swap_bipartitionc              #   P  ^ ^^#    [         R                  R                  5       [         R                  R                  5       4=u  p#mT R                  5        HW  u  pE[	        U4S jUR                  5        5       5      nTU   (       a  UR                  XF5        ME  UR                  XF* 5        MY     UU U4S jnSnSn	U(       aa  U(       aY  UR                  5       u  pFU" U5        UR                  5       u  pU" U
5        XU-   -  n	US-  nXXJ4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wtsides      j/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/networkx/algorithms/community/bipartitions.py	<genexpr>'_kernighan_lin_sweep.<locals>.<genexpr>   s      F47R+   !c                    > T	U    nTU    R                  5        HE  u  p#T	U   nXA:X  a  U* nTU   nX%;   d  M  UR                  U5      SU-  -   nUR                  X&SS9  MG     g )N   T)allow_increase)itemsgetinsert)
node	side_nodenbrr   side_nbrheap_nbrcost_nbr
cost_heaps	edge_infor   s
          r   _update_heap_values1_kernighan_lin_sweep.<locals>._update_heap_values   sq    J	 ,,.GCCyH$S!(+H#<<,q2v5dC /    r      N)nxutils
BinaryHeapr   sumr   pop)r!   r   heap0heap1unbrscost_ur"   itotcostr   cost_vr    s   ``          @r   _kernighan_lin_sweepr3      s      !# 3 3 5rxx7J7J7L LLLE:??$FFF7LL#LLG$ %
D 	
AG
EIIK	AIIK	AF?"	Q1&   EE%E%s   DD&"D&directed   weight)
edge_attrs
   c                 V  ^ [        U 5      nUc(  UR                  U5        [        U5      S-  nUSU XVS pOA Uu  px[
        R                  R                  XU/5      (       d  [
        R                  " S5      eU V
s0 s H  oX;   _M	     nn
[        T5      (       a  TnO"U R                  5       (       a  U4S jnOU4S jnU R                  R                  5        VVVVs0 s H:  u  pXR                  5        VVs0 s H  u  nnU" XU5      =nc  M  UU_M     snn_M<     n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     UR                  5        VVs1 s H  u  nnUS:X  d  M  UiM     nnnUR                  5        VVs1 s H  u  nnUS:X  d  M  UiM     nnnUU4$ ! [        [        4 a  n	[
        R                  " S5      U	eSn	A	ff = fs  sn
f s  snnf s  snnnnf s  snnf s  snnf )	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 nodes individually, alternating between sides to keep the bisection
balanced.

Kernighan-Lin is an approximate algorithm for maximal modularity bisection.
In [2]_ they suggest that fine-tuned improvements can be made using
greedy node swapping, (see `greedy_node_swap_bipartition`).
The improvements are typically only a few percent of the modularity value.
But they claim that can make a difference between a good and excellent method.
This function does not perform any improvements. But you can do that yourself.

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 : string or function (default: "weight")
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number or None to indicate a hidden edge.

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.
.. [2] M. E. J. Newman,
   "Modularity and community structure in networks",
   PNAS, 103 (23), p. 8577-8582,
   https://doi.org/10.1073/pnas.0601602103

Nr   zpartition must be two setszpartition invalidc                 J   > [        U4S jUR                  5        5       5      $ )Nc              3   F   >#    U  H  oR                  TS 5      v   M     g7f)r%   Nr   )r   ddr6   s     r   r   <kernighan_lin_bisection.<locals>.<lambda>.<locals>.<genexpr>   s     (PZr):):Zr   )r)   valuesr-   r   dr6   s      r   <lambda>)kernighan_lin_bisection.<locals>.<lambda>   s    S(PQXXZ(P%Pr$   c                 (   > UR                  TS5      $ )Nr%   r<   r@   s      r   rB   rC      s    QUU61%5r$   r   r%   )listshufflelen	TypeError
ValueErrorr&   NetworkXError	communityis_partitioncallableis_multigraph_adjr   ranger3   min)G	partitionmax_iterr6   seednodesmidABerrr   r   
sum_weightr-   r.   r   rA   r   r!   r0   costsmin_costmin_i_spart1part2s      `                       r   r   r   7   s   P GEU%jAoTc{E$K1	JDA ||((F33""#677*/0%$49%D0 
	
		P
5
 vv||~%GA 	
jjlVldaZa5H/HrEArElVV%  
 8_))T:; Z%q=!&5MLAq&1DGDG *   ::<2<41a16Q<E2::<2<41a16Q<E2%<E :& 	J""#?@cI	J
 1 W  32sS   G =H!H
<HHH
H#H?H%H%H	-HH	H

multigraphc                    SSK n[        R                  R                  U 5      nUR                  R	                  U5      u  p4UR                  U5      S   n[        UR                  USS2U4   5      U 5      n[        5       [        5       pU H/  u  pU	S:  a  UR                  U
5        M  UR                  U
5        M1     Xx4$ )aj  Return a bipartition of the nodes based on the spectrum of the
modularity matrix of the graph.

This method calculates the eigenvector associated with the second
largest eigenvalue of the modularity matrix, where the modularity
matrix *B* is defined by

..math::

    B_{i j} = A_{i j} - \frac{k_i k_j}{2 m},

where *A* is the adjacency matrix, `k_i` is the degree of node *i*,
and *m* is the number of edges in the graph. Nodes whose
corresponding values in the eigenvector are negative are placed in
one block, nodes whose values are nonnegative are placed in another
block.

Parameters
----------
G : NetworkX graph

Returns
-------
C : tuple
    Pair of communities as two sets of nodes of ``G``, partitioned
    according to second largest eigenvalue of the modularity matrix.

Examples
--------
>>> G = nx.karate_club_graph()
>>> MrHi, Officer = nx.community.spectral_modularity_bipartition(G)
>>> MrHi, Officer = sorted([sorted(MrHi), sorted(Officer)])
>>> MrHi
[0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21]
>>> Officer
[8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]

References
----------
.. [1] M. E. J. Newman *Networks: An Introduction*, pages 373--378
   Oxford University Press 2011.
.. [2] M. E. J. Newman,
   "Modularity and community structure in networks",
   PNAS, 103 (23), p. 8577-8582,
   https://doi.org/10.1073/pnas.0601602103

r   N)
numpyr&   linalgmodularity_matrixeigargsortziprealsetadd)rR   nprY   eigenvalueseigenvectorsindexv2leftrightr-   ns              r   r   r      s    d 
		##A&A "		a 0KJJ{#B'E	RWW\!U(+,a	0B%%q5HHQKIIaL	 
 ;r$   )
init_splitrT   c                  ^" Ucb  [        U 5      S-  n[        U 5      U-
  n[        [        R                  " [	        U 5      U5      5      nU  Vs1 s H  ofU;  d  M
  UiM     nnXW4nOj[
        R                  R                  X5      (       d  [
        R                  " S5      e[        U5      S:X  d  [
        R                  " S5      e[        U5      n[
        R                  R                  X5      n	XpSnU R                  5       n[        U R                  5      m"X:  GaX  X:  GaR  U
nUn	[        U
5      nUn[        U 5      nU(       Ga  SnSnSnUu  nn[        U"4S jU 5       5      n[        U"4S jU 5       5      nU H  nUU;   a	  UUnnUUnnOUUnnUUnn[        X   R                  5       U-  5      * U-  n[        X   R                  5       U-  5      U-  nT"U   nUSUS-  -  -  UU-
  U-
  -  nUU-   U-   n U U:  d  M  U nUnUnUn!M     UR!                  U5        W!R#                  U5        UU-  nX:  a  [        U5      UpUR!                  U5        U(       a  GM  US	-  nX:  a  X:  a  GMR  U$ s  snf )
a  Split the nodes into two communities based on greedy
modularity maximization.

The algorithm works by selecting a node to change communities which
will maximize the modularity. The swap is made and the community
structure with the highest modularity is kept.

Parameters
----------
G : NetworkX graph

init_split : 2-tuple of sets of nodes
    Pair of sets of nodes in ``G`` providing an initial bipartition
    for the algorithm. If not specified, a random balanced partition
    is used. If this pair of sets is not a partition of the nodes of `G`,
    :exc:`NetworkXException` is raised.

max_iter : int
  Maximum number of iterations of attempting swaps to find an improvement.

Returns
-------
max_split : 2-tuple of sets of nodes
    Pair of sets of nodes of ``G``, partitioned according to a
    node swap greedy modularity maximization algorithm.

Raises
------
NetworkXError
  if init_split is not a valid partition of the
  graph into two communities or if G is a MultiGraph

Examples
--------
>>> G = nx.barbell_graph(3, 0)
>>> left, right = nx.community.greedy_node_swap_bipartition(G)
>>> # Sort the communities so the nodes appear in increasing order.
>>> left, right = sorted([sorted(left), sorted(right)])
>>> sorted(left)
[0, 1, 2]
>>> sorted(right)
[3, 4, 5]

Notes
-----
This function is not implemented for multigraphs.

References
----------
.. [1] M. E. J. Newman "Networks: An Introduction", pages 373--375.
   Oxford University Press 2011.

Nr   z"init_split is not a partition of Gz init_split must be a bipartitionr   re   c              3   .   >#    U  H
  nTU   v   M     g 7fr
   r   r   rv   G_degrees     r   r   /greedy_node_swap_bipartition.<locals>.<genexpr>D  s     2TT   c              3   .   >#    U  H
  nTU   v   M     g 7fr
   r   rz   s     r   r   r|   E  s     4e!er}   r%   )rG   rm   randomsamplerE   r&   rK   rL   rJ   r   
modularitynumber_of_edgesdictdegreer)   keysremovern   )#rR   rw   rT   m1m2
some_nodesrv   other_nodesbest_split_so_farbest_mod	max_splitmax_moditsm
next_splitnext_modrV   max_swapmax_nodemax_node_commrt   ru   leftdrightdin_commout_commin_degout_degd_eiid_ejjdegd_sum_aiswap_changenon_max_node_commr{   s#                                     @r   r   r      s   n Vq[Vb[tAw34
"#;!Q
':q!;'5||((77""#GHH:!#""#EFF$Z0||&&q<H*w
C	AAHH~H

#.%i(
AHH M$KD%2T22E4e44F9(,eXG&+VGFG(-tXG&,eGFQTYY[7233a7ADIIK(23a7qk1q!t8,'1AC1GH#emh6)*H H$+M(0%% (   *!!(+ H!%-j%987LL"C eD 	qQ 
#.R s <s   	I9I9)Nr8   r6   N)__doc__r   copyr   	itertoolsr   networkxr&   __all__r3   r'   not_implemented_forpy_random_state_dispatchabler   r   r   r   r$   r   <module>r      s    U    $!N j)!X&p '  *pf j)l+< , *<~ l+26 s ,sr$   