
    \Mh                     8    d Z dgZdddZd ZdeiZddddZdS )	a  
Local Community Detection Algorithms

Local Community Detection (LCD) aims to detected one or a few communities
starting from certain source nodes in the network. This differs from Global
Community Detection (GCD), which aims to partition an entire network into
communities.

LCD is often useful when only a portion of the graph is known or the
graph is large enough that GCD is infeasable

[1]_ Gives a good introduction and overview of LCD

References
----------
.. [1] Baltsou, Georgia, Konstantinos Christopoulos, and Konstantinos Tsichlas.
   Local community detection: A survey. IEEE Access 10 (2022): 110701-110726.
   https://doi.org/10.1109/ACCESS.2022.3213980


greedy_source_expansionN)cutoffc          	      <    |t          d          }|h|h} |                                         z
  } fd|D             }fd|D             }d}t                    |k     r|snd}d }	t                      x}
x}}|D ]+}t	           ||||          \  }}}}||k    r
|}|}	|}
|}|},|	hz  |                     |	                                         z
             |                    |	           |
}|}|}||k     rn|}t                    |k     S )Ninfc                 b    h | ]+}                     |          D ]}t          ||g          ,S  )	neighbors	frozenset).0nodenbrGs      c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/community/local.py	<setcomp>z3_clauset_greedy_source_expansion.<locals>.<setcomp>    s>    KKKDT9J9JKK#D#;		KKKK    c                 J    h | ]}t          fd |D                       | S )c              3       K   | ]}|v V  	d S Nr   )r
   r   Cs     r   	<genexpr>z=_clauset_greedy_source_expansion.<locals>.<setcomp>.<genexpr>!   s'      ">">419">">">">">">r   )all)r
   edger   s     r   r   z3_clauset_greedy_source_expansion.<locals>.<setcomp>!   s;    ???$s">">">">">">">>>????r       )floatkeyslenset)_calculate_local_modularity_for_candidateupdateremove)r   sourcer   BUTIR_valuemax_R	best_nodebest_node_Bbest_node_Tbest_node_IvR_tmpB_tmpT_tmpI_tmpr   s   `                 @r    _clauset_greedy_source_expansionr0      s}   ~u	A	A	&	1AKKKKAKKKA????!???AG
a&&6// 		25%%77kK 		$ 		$A)R1aAq* *&E5% u}}	###O	9""$$q()))	7??7 a&&6//: Hr   c                 R   ||hz  |                                 }|                                 }|                                 }t                      }	| |         D ]}
|
vr9|                    |           |                    t          ||
g                     |
|v rMt	          fd| |
         D                       }|s*|                    |
           |	                    |
           |
v r$|                    t          ||
g                     |	D ]Y}| |         D ]N}||vrH|                    t          ||g                     |                    t          ||g                     OZt          |          dk    rt          |          t          |          z  nd}||||fS )a$  
    Compute the local modularity R and updated variables when adding node v to the community.

    Parameters
    ----------
    G : NetworkX graph
        The input graph.
    v : node
        The candidate node to add to the community.
    C : set
        The current set of community nodes.
    B : set
        The current set of boundary nodes.
    T : set of frozenset
        The current set of boundary edges.
    I : set of frozenset
        The current set of internal boundary edges.

    Returns
    -------
    R_tmp : float
        The local modularity after adding node v.
    B_tmp : set
        The updated set of boundary nodes.
    T_tmp : set of frozenset
        The updated set of boundary edges.
    I_tmp : set of frozenset
        The updated set of internal boundary edges.
    c              3       K   | ]}|vV  	d S r   r   )r
   nbr_nbrC_tmps     r   r   z<_calculate_local_modularity_for_candidate.<locals>.<genexpr>s   s(       L L'!5 L L L L L Lr   r      )copyr   addr	   anyr   discardr   )r   r+   r   r!   r#   r$   r-   r.   r/   removed_B_nodesr   nbr_still_in_Bremoved_noderemoved_node_nbrr,   r4   s                  @r   r   r   D   s   < GEFFHHEFFHHEFFHHEeeO t + +eIIaLLLIIiC))***!88 ! L L L LQsV L L LLLN! )S!!!##C(((%<<IIiC))*** ( K K !, 	K 	Ku,,i)9<(HIIJJJi)9<(HIIJJJ	K
 (+5zzA~~CJJU##1E%%%r   clauset)r   methodc                    	 t           |         }n%# t          $ r}t          | d          |d}~ww xY w || ||          S )ux  Find the local community around a source node.

    Find the local community around a source node using Greedy Source
    Expansion. Greedy Source Expansion generally identifies a local community
    starting from the source node and expands it based on the criteria of the
    chosen algorithm.

    The algorithm is specified with the `method` keyword argument.

    * `"clauset"` [1]_ uses local modularity gain to determine local communities.
        The algorithm adds nbring nodes that maximize local modularity to the
        community iteratively, stopping when no additional nodes improve the modularity
        or when a predefined cutoff is reached.

        Local modularity measures the density of edges within a community relative
        to the total graph. By focusing on local modularity, the algorithm efficiently
        uncovers communities around a specific node without requiring global
        optimization over the entire graph.

        The algorithm assumes that the graph $G$ consists of a known community $C$ and
        an unknown set of nodes $U$, which are adjacent to $C$ . The boundary of the
        community $B$, consists of nodes in $C$ that have at least one nbr in $U$.

        Mathematically, the local modularity is expressed as:

        .. math::
            R = \frac{I}{T}

        where $T$ is the number of edges with one or more endpoints in $B$, and $I$ is the
        number of those edges with neither endpoint in $U$.

    Parameters
    ----------
    G : NetworkX graph
        The input graph.

    source : node
        The source node from which the community expansion begins.

    cutoff : int, optional (default=None)
        The maximum number of nodes to include in the community. If None, the algorithm
        expands until no further modularity gain can be made.

    method : string, optional (default='clauset')
        The algorithm to use to carry out greedy source expansion.
        Supported options: 'clauset'. Other inputs produce a ValueError

    Returns
    -------
    set
        A set of nodes representing the local community around the source node.

    Examples
    --------
    >>> G = nx.karate_club_graph()
    >>> nx.community.greedy_source_expansion(G, source=16)
    {16, 0, 4, 5, 6, 10}

    Notes
    -----
    This algorithm is designed for detecting local communities around a specific node,
    which is useful for large networks where global community detection is computationally
    expensive.

    The result of the algorithm may vary based on the structure of the graph, the choice of
    the source node, and the presence of ties between nodes during the greedy expansion process.

    References
    ----------
    .. [1] Clauset, Aaron. Finding local community structure in networks.
      Physical Review E—Statistical, Nonlinear, and Soft Matter Physics 72, no. 2 (2005): 026132.
      https://arxiv.org/pdf/physics/0503036

    z( is not a valid choice for an algorithm.N)r    r   )
ALGORITHMSKeyError
ValueError)r   r    r   r?   algoes         r   r   r      sk    VU&! U U UFLLLMMSTTU 4&0000s    
2-2)__doc____all__r0   r   rA   r   r   r   r   <module>rH      s    , %
% ;? ' ' ' ' 'T@& @& @&H /

 26i P1 P1 P1 P1 P1 P1 P1r   