o
    ˷e)                     @   s~   d Z ddlmZmZ ddlZddlmZmZm	Z	 ddgZ
e	ddddZed	d
d Zdd Zdd Zdd Zdd ZdS )z3
Label propagation community detection algorithms.
    )CounterdefaultdictN)groupsnot_implemented_forpy_random_statelabel_propagation_communitiesasyn_lpa_communities   c                 #   s    dd t | D }d}|rpd}t| }|| |D ]R}| | s"q|du r1tt|j| | }ntt}| j||ddD ]\}}	}
|||	   |
7  < q=t	|
   fdd	| D }|| |vrm||||< d}q|st|
 E dH  dS )
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.
    c                 S      i | ]\}}||qS  r   ).0inr   r   f/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/community/label_propagation.py
<dictcomp>?       z(asyn_lpa_communities.<locals>.<dictcomp>TFN   )datadefaultc                    s   g | ]
\}}| kr|qS r   r   r   labelfreqmax_freqr   r   
<listcomp>]   s    z(asyn_lpa_communities.<locals>.<listcomp>)	enumeratelistshuffler   mapgetr   floatedgesmaxvaluesitemschoicer   )Gweightseedlabelscontnodesnode
label_freq_vwtbest_labelsr   r   r   r      s2   3

'directedc           	      C   s   t | }dd t| D }t|| s+| D ]\}}|D ]}t|||  qqt|| rtt}| D ]\}}|| | q3| S )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.
    c                 S   r
   r   r   )r   kr/   r   r   r   r      r   z1label_propagation_communities.<locals>.<dictcomp>)	_color_networkr   _labeling_completer$   _update_labelr   setaddr#   )	r&   coloringlabelingcolorr+   r   clustersr,   r   r   r   r   r   l   s   

c                 C   sJ   t  }tj| }| D ]\}}||v r|| | q|h||< q|S )zColors the network so that neighboring nodes all have distinct colors.

    Returns a dict keyed by color to a set of nodes with that color.
    )dictnxr9   greedy_colorr$   r8   )r&   r9   colorsr,   r;   r   r   r   r4      s   r4   c                    s   t  fdd D S )zDetermines 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   s4    | ]}t  | d kr| t| v V  qdS )r   N)len_most_frequent_labels)r   r/   r&   r:   r   r   	<genexpr>   s    *z%_labeling_complete.<locals>.<genexpr>)all)r:   r&   r   rC   r   r5      s   r5   c                    sN   ||  s	 |  hS t  fdd||  D }t| fdd| D S )zReturns a set of all labels with maximum frequency in `labeling`.

    Input `labeling` should be a dict keyed by node to labels.
    c                 3   s    | ]} | V  qd S )Nr   )r   q)r:   r   r   rD      s    z(_most_frequent_labels.<locals>.<genexpr>c                    s   h | ]
\}}| kr|qS r   r   r   r   r   r   	<setcomp>   s    z(_most_frequent_labels.<locals>.<setcomp>)r   r"   r#   r$   )r,   r:   r&   freqsr   )r:   r   r   rB      s
   
rB   c                 C   sX   t | ||}t|dkr| || < dS t|dkr(||  |vr*t||| < dS dS dS )zUpdates 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)rB   rA   popr"   )r,   r:   r&   high_labelsr   r   r   r6      s   r6   )NN)__doc__collectionsr   r   networkxr>   networkx.utilsr   r   r   __all__r   r   r4   r5   rB   r6   r   r   r   r   <module>   s    _
,