
    sh                     :    S r S/rSS.S jrS rS\0rSSS.S jrg)	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          	        ^ Uc  [        S5      nU1mU1nX   R                  5       T-
  nU VVs1 s H'  oPR                  U5        H  n[        XV/5      iM     M)     nnnU Vs1 s H!  n[	        U4S jU 5       5      (       d  M  UiM#     n	nSn
[        T5      U:  a  U(       d   T$ SnS n[        5       =n=pU H)  n[        U UTX7U	5      u  nnnnUU:  d  M  UnUnUnUnUnM+     TU1-  mUR                  X   R                  5       T-
  5        UR                  U5        UnUnUn	X:  a   T$ Un
[        T5      U:  a  M  T$ s  snnf s  snf )Ninfc              3   ,   >#    U  H	  oT;   v   M     g 7fN ).0nodeCs     w/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/networkx/algorithms/community/local.py	<genexpr>3_clauset_greedy_source_expansion.<locals>.<genexpr>!   s     ">19       )
floatkeys	neighbors	frozensetalllenset)_calculate_local_modularity_for_candidateupdateremove)Gsourcer   BUr
   nbrTedge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_expansionr.      sy   ~u	A	A		1A,-KADT9J#D;	9J	AAK?!$s">">>!A?G
a&6/6 H3 	25%77kA)R1aq*&E5% u}	###  O	""$q()	? H 7 a&6/: HC 	L?s   .E%EEc                   ^ X!1-  mUR                  5       nUR                  5       nUR                  5       n[        5       n	X    H  n
U
T;  a,  UR                  U5        UR                  [        X/5      5        X;   a@  [	        U4S jX
    5       5      nU(       d"  UR                  U
5        U	R                  U
5        U
T;   d  M  UR                  [        X/5      5        M     U	 HK  nX    H@  nX;  d  M
  UR                  [        X/5      5        UR                  [        X/5      5        MB     MM     [        U5      S:  a  [        U5      [        U5      -  OSnXXx4$ )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   ,   >#    U  H	  oT;  v   M     g 7fr   r   )r	   nbr_nbrC_tmps     r   r   <_calculate_local_modularity_for_candidate.<locals>.<genexpr>s   s      LV'!5Vr   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*   r2   s                  @r   r   r   D   s.   < GEFFHEFFHEFFHEeO teIIaLIIi)*8 ! LQV LLN!S!##C(%<IIi)*# ( ( !,i)9(HIJi)9(HIJ !0 ( (+5zA~CJU#1E%%    clauset)r   methodc                f     [         U   nU" XUS9$ ! [         a  n[        U S35      UeSnAff = f)u
  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      sL    VU&! 00  UF8#KLMSTTUs   	 
0+0)__doc____all__r.   r   rA   r   r   r=   r   <module>rH      sB   , %
% ;? 'T@&H /

 26i P1r=   