Ë
    D^(hf.  ã                   ó4  — d Z ddlmZmZmZ ddlZddlmZm	Z	m
Z
 g d¢Z e
d«       ej                  d¬«      ddd	œd
„«       «       Zdd„Z e
d«       ej                  d¬«      dd„«       «       Z e	d«      ej                  d„ «       «       Zd„ Zd„ Zd„ Zd„ Zy)z3
Label propagation community detection algorithms.
é    )ÚCounterÚdefaultdictÚdequeN)ÚgroupsÚnot_implemented_forÚpy_random_state)Úlabel_propagation_communitiesÚasyn_lpa_communitiesÚ"fast_label_propagation_communitiesÚseedÚweight)Ú
edge_attrs)r   r   c             #   ó´  K  — t        | «      }|j                  |«       t        | «      }t        | «      D ��ci c]  \  }}||“Œ
 }}}|rß|j	                  «       }|j                  |«       | j                  |«      dkD  r§t        | |||«      }t        |j                  «       «      }	|j                  |D �
cg c]  }
||
   |	k(  sŒ|
‘Œ c}
«      }
||   |
k7  rP|
||<   t        j                  | |«      D ]2  }||   |
k7  sŒ||vsŒ|j                  |«       |j                  |«       Œ4 |rŒßt        |«      j                  «       E d{  –—†  yc c}}w c c}
w 7 Œ­w)uÛ  Returns communities in `G` as detected by fast label propagation.

    The fast label propagation algorithm is described in [1]_. The algorithm is
    probabilistic and the found communities may vary in different executions.

    The algorithm operates as follows. First, the community label of each node is
    set to a unique label. The algorithm then repeatedly updates the labels of
    the nodes to the most frequent label in their neighborhood. In case of ties,
    a random label is chosen from the most frequent labels.

    The algorithm maintains a queue of nodes that still need to be processed.
    Initially, all nodes are added to the queue in a random order. Then the nodes
    are removed from the queue one by one and processed. If a node updates its label,
    all its neighbors that have a different label are added to the queue (if not
    already in the queue). The algorithm stops when the queue is empty.

    Parameters
    ----------
    G : Graph, DiGraph, MultiGraph, or MultiDiGraph
        Any NetworkX graph.

    weight : string, or None (default)
        The edge attribute representing a non-negative weight of an edge. If None,
        each edge is assumed to have weight one. The weight of an edge is used in
        determining the frequency with which a label appears among the neighbors of
        a node (edge with weight `w` is equivalent to `w` unweighted edges).

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

    Returns
    -------
    communities : iterable
        Iterable of communities given as sets of nodes.

    Notes
    -----
    Edge directions are ignored for directed graphs.
    Edge weights must be non-negative numbers.

    References
    ----------
    .. [1] Vincent A. Traag & Lovro Å ubelj. "Large network community detection by
       fast label propagation." Scientific Reports 13 (2023): 2701.
       https://doi.org/10.1038/s41598-023-29610-z
    r   N)r   ÚshuffleÚsetÚ	enumerateÚpopleftÚremoveÚdegreeÚ_fast_label_countÚmaxÚvaluesÚchoiceÚnxÚall_neighborsÚappendÚaddr   )ÚGr   r   Únodes_queueÚ	nodes_setÚiÚnodeÚcommsÚlabel_freqsÚmax_freqÚcommÚnbrs               úm/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/community/label_propagation.pyr   r      sW  è ø€ ôf ˜“(€KØ‡L�L�Ôô �A“€Iô %.¨a£L×1™˜˜DˆT�1‰WÐ1€EÑ1á
à×"Ñ"Ó$ˆØ×Ñ˜Ôð �8‰8�D‹>˜AÒä+¨A¨u°d¸FÓCˆKÜ˜;×-Ñ-Ó/Ó0ˆHð —;‘;Ø"-ÖO˜$°¸TÑ1BÀhÓ1N’ÒOóˆDð �T‰{˜dÒ"Ø"��d‘ô ×+Ñ+¨A¨tÓ4ò +�CØ˜S‘z TÓ)¨c¸Ò.BØ#×*Ñ*¨3Ô/Ø!Ÿ™ cÕ*ð+ò) ô2 �e‹}×#Ñ#Ó%×%Ñ%ùó7 2ùò Pð &úsA   ‚6E¸EÁA0EÂ5EÃEÃ5EÃ=EÄ&EÄ)EÅEÅEc           	      ó”  — |€Ì| j                  «       s5t        t        |j                  t	        j
                  | |«      «      «      }|S t        t        «      }| |   D ]!  }|||   xx   t        | |   |   «      z  cc<   Œ# | j                  «       r=| j                  |   D ]+  }|||   xx   t        | j                  |   |   «      z  cc<   Œ- |S t        t        «      }| j                  ||d¬«      D ]  \  }}}|||   xx   |z  cc<   Œ | j                  «       r-| j                  ||d¬«      D ]  \  }}}|||   xx   |z  cc<   Œ |S )z�Computes the frequency of labels in the neighborhood of a node.

    Returns a dictionary keyed by label to the frequency of that label.
    é   ©ÚdataÚdefault)Úis_multigraphr   ÚmapÚgetr   r   r   ÚintÚlenÚis_directedÚpredÚfloatÚedgesÚin_edges)r   r#   r"   r   r$   r'   Ú_Úws           r(   r   r   i   s^  € ð €~à�‰Ô Ü!¤# e§i¡i´×1AÑ1AÀ!ÀTÓ1JÓ"KÓLˆKð. Ðô' &¤cÓ*ˆKØ˜‘wò =�Ø˜E #™JÓ'¬3¨q°©w°s©|Ó+<Ñ<Ô'ð=ð �}‰}ŒØŸ6™6 $™<ò F�CØ  c¡
Ó+¬s°1·6±6¸$±<ÀÑ3DÓ/EÑEÔ+ðFð Ðô "¤%Ó(ˆØŸ™ ¨F¸A˜Ó>ò 	)‰IˆAˆs�AØ˜˜c™
Ó# qÑ(Ô#ð	)ð �=‰=Œ?ØŸZ™Z¨°6À1˜ZÓEò -‘	��Q˜Ø˜E #™JÓ'¨1Ñ,Ô'ð-ð Ðó    é   c              #   óŠ  K  — t        | «      D ��ci c]  \  }}||“Œ
 }}}d}|rîd}t        | «      }|j                  |«       |D ]È  }| |   sŒ	|€#t        t	        |j
                  | |   «      «      }	n<t        t        «      }	| j                  ||d¬«      D ]  \  }
}}|	||   xx   |z  cc<   Œ t        |	j                  «       «      }|	j                  «       D ��cg c]  \  }}||k(  sŒ|‘Œ }}}||   |vsŒ³|j                  |«      ||<   d}ŒÊ |rŒît        |«      j                  «       E d{  –—†  yc c}}w c c}}w 7 Œ­w)uÖ  Returns communities in `G` as detected by asynchronous label
    propagation.

    The asynchronous label propagation algorithm is described in
    [1]_. The algorithm is probabilistic and the found communities may
    vary on different executions.

    The algorithm proceeds as follows. After initializing each node with
    a unique label, the algorithm repeatedly sets the label of a node to
    be the label that appears most frequently among that nodes
    neighbors. The algorithm halts when each node has the label that
    appears most frequently among its neighbors. The algorithm is
    asynchronous because each node is updated without waiting for
    updates on the remaining nodes.

    This generalized version of the algorithm in [1]_ accepts edge
    weights.

    Parameters
    ----------
    G : Graph

    weight : string
        The edge attribute representing the weight of an edge.
        If None, each edge is assumed to have weight one. In this
        algorithm, the weight of an edge is used in determining the
        frequency with which a label appears among the neighbors of a
        node: a higher weight means the label appears more often.

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

    Returns
    -------
    communities : iterable
        Iterable of communities given as sets of nodes.

    Notes
    -----
    Edge weight attributes must be numerical.

    References
    ----------
    .. [1] Raghavan, Usha Nandini, RÃ©ka Albert, and Soundar Kumara. "Near
           linear time algorithm to detect community structures in large-scale
           networks." Physical Review E 76.3 (2007): 036106.
    TFNr*   r+   )r   Úlistr   r   r/   r0   r   r5   r6   r   r   Úitemsr   r   )r   r   r   r!   ÚnÚlabelsÚcontÚnodesr"   Ú
label_freqr8   ÚvÚwtr%   ÚlabelÚfreqÚbest_labelss                    r(   r
   r
   Œ   s_  è ø€ ôh  )¨›|×,‘t�q˜!ˆa�‰dÐ,€FÑ,Ø€Dá
ØˆÜ�Q“ˆØ�‰�UÔàò 	ˆDØ�T’7Øð ˆ~ô %¤S¨¯©°Q°t±WÓ%=Ó>‘
ô )¬Ó/�
Ø !§¡¨°6À1 Ó Eò 0‘H�A�q˜"Ø˜v a™yÓ)¨RÑ/Ô)ð0ô ˜:×,Ñ,Ó.Ó/ˆHà)3×)9Ñ)9Ó);÷Ù%˜% ¸tÀxÓ?O’ðˆKñ ð �d‰| ;Ò.Ø#Ÿ{™{¨;Ó7��t‘Ø‘ð?	ò ôL �f‹~×$Ñ$Ó&×&Ñ&ùóS -ùó:ð 'ús:   ‚E‘D5žB=EÃD;Ã)D;Ã-
EÃ8EÄEÄ/EÄ0EÚdirectedc                 ó€  — t        | «      }t        | «      D ��ci c]  \  }}||“Œ
 }}}t        || «      s9|j                  «       D ]  \  }}|D ]  }t	        ||| «       Œ Œ t        || «      sŒ9t        t        «      }|j                  «       D ]  \  }	}
||
   j                  |	«       Œ |j                  «       S c c}}w )ab  Generates community sets determined by label propagation

    Finds communities in `G` using a semi-synchronous label propagation
    method [1]_. This method combines the advantages of both the synchronous
    and asynchronous models. Not implemented for directed graphs.

    Parameters
    ----------
    G : graph
        An undirected NetworkX graph.

    Returns
    -------
    communities : iterable
        A dict_values object that contains a set of nodes for each community.

    Raises
    ------
    NetworkXNotImplemented
       If the graph is directed

    References
    ----------
    .. [1] Cordasco, G., & Gargano, L. (2010, December). Community detection
       via semi-synchronous label propagation algorithms. In Business
       Applications of Social Network Analysis (BASNA), 2010 IEEE International
       Workshop on (pp. 1-8). IEEE.
    )	Ú_color_networkr   Ú_labeling_completer>   Ú_update_labelr   r   r   r   )r   ÚcoloringÚkrD   ÚlabelingÚcolorrB   r?   Úclustersr"   rF   s              r(   r	   r	   ì   sÂ   € ô> ˜aÓ €Hä!*¨1£×.™˜˜A��1‘Ð.€HÑ.Ü  ¨1Ô-à$ŸN™NÓ,ò 	.‰LˆE�5Øò .�Ü˜a ¨1Õ-ñ.ð	.ô ! ¨1Õ-ô œ3Ó€HØ—~‘~Ó'ò "‰ˆˆeØ�‰×Ñ˜DÕ!ð"à�?‰?ÓÐùó /s   šB:c                 ó¶   — i }t         j                  j                  | «      }|j                  «       D ]$  \  }}||v r||   j	                  |«       Œ|h||<   Œ& |S )z‘Colors the network so that neighboring nodes all have distinct colors.

    Returns a dict keyed by color to a set of nodes with that color.
    )r   rN   Úgreedy_colorr>   r   )r   rN   Úcolorsr"   rQ   s        r(   rK   rK     sb   € ð
 €HÜ�[‰[×%Ñ% aÓ(€FØ—|‘|“~ò %‰ˆˆeØ�HÑØ�U‰O×Ñ Õ%à#˜fˆH�UŠOð	%ð
 €Or:   c                 ó0   ‡ ‡— t        ˆˆ fd„‰D «       «      S )zêDetermines whether or not LPA is done.

    Label propagation is complete when all nodes have a label that is
    in the set of highest frequency labels amongst its neighbors.

    Nodes with no neighbors are considered complete.
    c              3   óf   •K  — | ](  }t        ‰|   «      d kD  sŒ‰|   t        |‰‰«      v –— Œ* y­w)r   N)r2   Ú_most_frequent_labels)Ú.0rD   r   rP   s     €€r(   ú	<genexpr>z%_labeling_complete.<locals>.<genexpr>1  s9   øè ø€ ò ØABÌ3ÈqÐQRÉtË9ÐWXË=ˆ�‰Ô,¨Q°¸!Ó<Ô<ñùs   ƒ1š1)Úall)rP   r   s   ``r(   rL   rL   )  s   ù€ ô ô ØFGôó ð r:   c                 óØ   ‡— ||    s‰|    hS t        ˆfd„||    D «       «      }t        |j                  «       «      }|j                  «       D ��ch c]  \  }}||k(  sŒ|’Œ c}}S c c}}w )z†Returns a set of all labels with maximum frequency in `labeling`.

    Input `labeling` should be a dict keyed by node to labels.
    c              3   ó(   •K  — | ]	  }‰|   –— Œ y ­w©N© )rY   ÚqrP   s     €r(   rZ   z(_most_frequent_labels.<locals>.<genexpr>A  s   øè ø€ Ò1 A�H˜Q•KÑ1ùs   ƒ)r   r   r   r>   )r"   rP   r   Úfreqsr%   rF   rG   s    `     r(   rX   rX   6  sf   ø€ ð
 ˆTŠ7ð ˜‘ÐÐô Ó1¨¨4©Ô1Ó1€EÜ�5—<‘<“>Ó"€HØ%*§[¡[£]×G‘k�e˜T°d¸hÓ6FŠEÓGÐGùÓGs   ÁA&ÁA&c                 ó¬   — t        | ||«      }t        |«      dk(  r|j                  «       || <   yt        |«      dkD  r||    |vrt        |«      || <   yyy)zÕUpdates the label of a node using the Prec-Max tie breaking algorithm

    The algorithm is explained in: 'Community Detection via Semi-Synchronous
    Label Propagation Algorithms' Cordasco and Gargano, 2011
    r*   N)rX   r2   Úpopr   )r"   rP   r   Úhigh_labelss       r(   rM   rM   F  s`   € ô (¨¨h¸Ó:€KÜ
ˆ;Ó˜1ÒØ$Ÿ™Ó*ˆ�ŠÜ	ˆ[Ó	˜AÒ	à�D‰> Ñ,Ü  Ó-ˆH�TŠNð -ð 
r:   r^   )NN)Ú__doc__Úcollectionsr   r   r   Únetworkxr   Únetworkx.utilsr   r   r   Ú__all__Ú_dispatchabler   r   r
   r	   rK   rL   rX   rM   r_   r:   r(   ú<module>rk      sÌ   ðñ÷ 4Ñ 3ã ß GÑ Gò€ñ �ÓØ€×Ñ˜XÔ&Ø48¸tó S&ó 'ó ðS&ól ñF �ÓØ€×Ñ˜XÔ&ò['ó 'ó ð['ñ| �ZÓ Ø×Ññ)ó ó !ð)òXò
òHó .r:   