+
    �°³jµ`  ã                   óê  € R t ^ RIHt ^ RIHt ^ RIt^ RIHtH	t	 ^ RI
HtHt . ROt ! R R]P                  4      tR t]! ]R4      t]P$                  R	 4       t]P$                  R
 4       t]P$                  R 4       t]P$                  ! RR7      RR l4       t]! R4      ]P$                  ! RR7      RRR^/R l4       4       tRR ltRR ltRR ltRR ltRR lt]]P$                  R 4       4       tR# )zHFunctions for measuring the quality of a partition (into
communities).

)Údefaultdict)ÚcombinationsN)Úis_coverÚis_partition)ÚargmapÚnot_implemented_forc                   ó6   a a€ ] tR t^t oRtV 3R ltRtVtV ;t# )ÚNotAPartitionz0Raised if a given collection is not a partition.c                ó4   <€ V R V 2p\         SV `  V4       R# )z' is not a valid partition of the graph N)ÚsuperÚ__init__)ÚselfÚGÚ
collectionÚmsgÚ	__class__s   &&& €Úf/home/luca/screen-checking/.venv/lib/python3.14/site-packages/networkx/algorithms/community/quality.pyr   ÚNotAPartition.__init__   s!   ø€ Ø�ÐCÀAÀ3ÐGˆÜ‰Ñ˜Öó    © )	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r   Ú__classdict__s   @@r   r	   r	      s   ù‡ € Ù:÷õ r   r	   c                óV   € \        W4      '       d   W3# \        P                  ! R4      h)a£  Decorator to check that a valid partition is input to a function

Raises :exc:`networkx.NetworkXError` if the partition is not valid.

This decorator should be used on functions whose first two arguments
are a graph and a partition of the nodes of that graph (in that
order)::

    >>> @require_partition
    ... def foo(G, partition):
    ...     print("partition is valid!")
    ...
    >>> G = nx.complete_graph(5)
    >>> partition = [{0, 1}, {2, 3}, {4}]
    >>> foo(G, partition)
    partition is valid!
    >>> partition = [{0}, {2, 3}, {4}]
    >>> foo(G, partition)
    Traceback (most recent call last):
      ...
    networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G
    >>> partition = [{0, 1}, {1, 2, 3}, {4}]
    >>> foo(G, partition)
    Traceback (most recent call last):
      ...
    networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G

z6`partition` is not a valid partition of the nodes of G)r   ÚnxÚNetworkXError©r   Ú	partitions   &&r   Ú_require_partitionr$      s)   € ô: �A×!Ò!Øˆ|ÐÜ
×
Ò
ÐSÓ
TÐTr   c                ó.   a € \        V 3R lV 4       4      # )a2  Returns the number of intra-community edges for a partition of `G`.

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

partition : iterable of sets of nodes
    This must be a partition of the nodes of `G`.

The "intra-community edges" are those edges joining a pair of nodes
in the same block of the partition.

c              3   ób   <"  € T F$  pSP                  V4      P                  4       x € K&  	  R # 5i©N)ÚsubgraphÚsize)Ú.0Úblockr   s   & €r   Ú	<genexpr>Ú(intra_community_edges.<locals>.<genexpr>Q   s&   øé € Ð?±Y¨Eˆq�z‰z˜%Ó ×%Ñ%×'Ð'³Yùs   ƒ,/)Úsumr"   s   f&r   Úintra_community_edgesr/   B   s   ø€ ô Ô?±YÓ?Ó?Ð?r   c                óº   € V P                  4       '       d   \        P                  M\        P                  p\        P                  ! WVR7      P                  4       # )aç  Returns the number of inter-community edges for a partition of `G`.
according to the given
partition of the nodes of `G`.

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

partition : iterable of sets of nodes
    This must be a partition of the nodes of `G`.

The *inter-community edges* are those edges joining a pair of nodes
in different blocks of the partition.

Implementation note: this function creates an intermediate graph
that may require the same amount of memory as that of `G`.

)Úcreate_using)Úis_directedr    ÚMultiDiGraphÚ
MultiGraphÚquotient_graphr)   )r   r#   ÚMGs   && r   Úinter_community_edgesr7   T   s9   € ð8 ŸM™MŸOšOŒ�Š´·±€BÜ×Ò˜Q¸Ô;×@Ñ@ÓBÐBr   c                óB   € \        \        P                  ! V 4      V4      # )aK  Returns the number of inter-community non-edges according to the
given partition of the nodes of `G`.

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

partition : iterable of sets of nodes
    This must be a partition of the nodes of `G`.

A *non-edge* is a pair of nodes (undirected if `G` is undirected)
that are not adjacent in `G`. The *inter-community non-edges* are
those non-edges on a pair of nodes in different blocks of the
partition.

Implementation note: this function creates two intermediate graphs,
which may require up to twice the amount of memory as required to
store `G`.

)r7   r    Ú
complementr"   s   &&r   Úinter_community_non_edgesr:   t   s   € ô< !¤§¢¨qÓ!1°9Ó=Ð=r   Úweight)Ú
edge_attrsc                ó„  a aaaaaa	a
€ \        V\        4      '       g   \        V4      p\        S V4      '       g   \        S V4      hS P	                  4       oS'       dj   \        S P                  SR7      4      o
\        S P                  SR7      4      o\        S
P                  4       4      oS^ 8X  d   ^ # ^S^,          ,          o	MX\        S P                  SR7      4      ;o
o\        S
P                  4       4      pV^,          oS^ 8X  d   ^ # ^V^,          ,          o	V VVVV	V
VV3R lp\        \        WQ4      4      # )a1  Returns the modularity of the given partition of the graph.

Modularity is defined in [1]_ as

.. math::
    Q = \frac{1}{2m} \sum_{ij} \left( A_{ij} - \gamma\frac{k_ik_j}{2m}\right)
        \delta(c_i,c_j)

where $m$ is the number of edges (or sum of all edge weights as in [5]_),
$A$ is the adjacency matrix of `G`, $k_i$ is the (weighted) degree of $i$,
$\gamma$ is the resolution parameter, and $\delta(c_i, c_j)$ is 1 if $i$ and
$j$ are in the same community else 0.

According to [2]_ (and verified by some algebra) this can be reduced to

.. math::
   Q = \sum_{c=1}^{n}
   \left[ \frac{L_c}{m} - \gamma\left( \frac{k_c}{2m} \right) ^2 \right]

where the sum iterates over all communities $c$, $m$ is the number of edges,
$L_c$ is the number of intra-community links for community $c$,
$k_c$ is the sum of degrees of the nodes in community $c$,
and $\gamma$ is the resolution parameter.

The resolution parameter sets an arbitrary tradeoff between intra-group
edges and inter-group edges. More complex grouping patterns can be
discovered by analyzing the same network with multiple values of gamma
and then combining the results [3]_. That said, it is very common to
simply use gamma=1. More on the choice of gamma is in [4]_.

The second formula is the one actually used in calculation of the modularity.
For directed graphs the second formula replaces $k_c^2$ with $k^{in}_c k^{out}_c$.

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

communities : list or iterable of set of nodes
    These node sets must represent a partition of G's nodes.

weight : string or None, optional (default="weight")
    The edge attribute that holds the numerical value used
    as a weight. If None or an edge does not have that attribute,
    then that edge has weight 1.

resolution : float (default=1)
    If resolution is less than 1, modularity favors larger communities.
    Greater than 1 favors smaller communities.

Returns
-------
Q : float
    The modularity of the partition.

Raises
------
NotAPartition
    If `communities` is not a partition of the nodes of `G`.

Examples
--------
>>> G = nx.barbell_graph(3, 0)
>>> nx.community.modularity(G, [{0, 1, 2}, {3, 4, 5}])
0.35714285714285715
>>> nx.community.modularity(G, nx.community.label_propagation_communities(G))
0.35714285714285715

References
----------
.. [1] M. E. J. Newman "Networks: An Introduction", page 224.
   Oxford University Press, 2011.
.. [2] Clauset, Aaron, Mark EJ Newman, and Cristopher Moore.
   "Finding community structure in very large networks."
   Phys. Rev. E 70.6 (2004). <https://arxiv.org/abs/cond-mat/0408187>
.. [3] Reichardt and Bornholdt "Statistical Mechanics of Community Detection"
   Phys. Rev. E 74, 016110, 2006. https://doi.org/10.1103/PhysRevE.74.016110
.. [4] M. E. J. Newman, "Equivalence between modularity optimization and
   maximum likelihood methods for community detection"
   Phys. Rev. E 94, 052315, 2016. https://doi.org/10.1103/PhysRevE.94.052315
.. [5] Blondel, V.D. et al. "Fast unfolding of communities in large
   networks" J. Stat. Mech 10008, 1-12 (2008).
   https://doi.org/10.1088/1742-5468/2008/10/P10008
©r;   c           	      ó  <a€ \        V 4      o\        V3R  lSP                  SS^R7       4       4      p\        V
3R lS 4       4      pS'       d   \        V3R lS 4       4      MTpVS,          SV,          V,          S	,          ,
          # )c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   ©r*   ÚuÚvÚwtÚcomms   &   €r   r,   Ú=modularity.<locals>.community_contribution.<locals>.<genexpr>  s   øé € ÐXÑ%J™˜˜rÐSWÉi—"’"Ó%Jùó   ƒ“
©ÚdataÚdefaultc              3   ó6   <"  € T F  pSV,          x € K  	  R # 5ir'   r   )r*   rB   Ú
out_degrees   & €r   r,   rF     s   øé € Ð9±D¨q˜Z¨Ÿ]š]³Dùó   ƒc              3   ó6   <"  € T F  pSV,          x € K  	  R # 5ir'   r   )r*   rB   Ú	in_degrees   & €r   r,   rF     s   øé € Ð7±$¨Q˜I aŸLšL³$ùrM   ©Úsetr.   Úedges)Ú	communityÚL_cÚout_degree_sumÚin_degree_sumrE   r   ÚdirectedrO   ÚmÚnormrL   Ú
resolutionr;   s   &   @€€€€€€€€r   Úcommunity_contributionÚ*modularity.<locals>.community_contributionÿ   sm   ù€ Ü�9‹~ˆÜÔX Q§W¡W¨T¸È WÔ%JÓXÓXˆäÔ9±DÓ9Ó9ˆß;CœÔ7±$Ó7Ô7Èˆà�Q�w˜ nÕ4°}ÕDÀtÕKÕKÐKr   )Ú
isinstanceÚlistr   r	   r2   ÚdictrL   rO   r.   ÚvaluesÚdegreeÚmap)r   Úcommunitiesr;   rZ   Údeg_sumr[   rW   rO   rX   rY   rL   s   f&ff  @@@@@r   Ú
modularityre   •   s  ÿ€ ôj �k¤4×(Ò(Ü˜;Ó'ˆÜ˜˜;×'Ò'Ü˜A˜{Ó+Ð+à�}‰}‹€HßÜ˜!Ÿ,™,¨f˜,Ó5Ó6ˆ
Ü˜Ÿ™¨F˜Ó3Ó4ˆ	Ü�
×!Ñ!Ó#Ó$ˆØ�Œ6ÙØ�1�a•4�x‰ä!% a§h¡h°f hÓ&=Ó!>Ð>ˆ
�YÜ�j×'Ñ'Ó)Ó*ˆØ�a�KˆØ�Œ6ÙØ�7˜A•:�~ˆ÷Lô Lô ŒsÐ)Ó7Ó8Ð8r   rW   rZ   c               óÚ  a aaaaaa	€ \        V\        4      '       g   \        V4      p\        S V4      '       g   \        P                  ! R4      h\        \        4      o	V F*  pV F!  pVS 9   g   K  S	V;;,          ^,          uu&   K#  	  K,  	  \        S P                  SR7      4      o\        SP                  4       4      oS^ 8X  d   R# V VVV	VV3R lo\        V3R lV 4       4      # )ad  Returns Shen et al.'s extended modularity for an overlapping cover.

Standard modularity assumes each node belongs to exactly one
community. Shen et al. [1]_ extend modularity to overlapping
communities by discounting each node's contribution by the number of
communities it belongs to:

.. math::
    EQ = \frac{1}{2m} \sum_{i} \sum_{v \in C_i, w \in C_i}
         \frac{1}{O_v O_w}\!\left[ A_{vw} - \gamma\,\frac{k_v k_w}{2m}\right]

where the outer sum runs over communities $C_i$ in the cover, $A_{vw}$
is the (weighted) adjacency, $k_v$ is the (weighted) degree of $v$,
$O_v$ is the number of communities to which $v$ belongs, $m$ is the
total (weighted) edge count, and $\gamma$ is the resolution parameter.

The factor $1 / (O_v O_w)$ discounts contributions from nodes that
belong to many communities: a node in 3 communities contributes 1/3
of its modularity share to each.

The resolution parameter $\gamma$ is not part of the original Shen
definition; it is added for consistency with
:func:`~networkx.algorithms.community.quality.modularity`. Setting
$\gamma = 1$ recovers Shen's EQ exactly.

Implementation uses the equivalent community-sum form

.. math::
    EQ = \sum_{i}\!\left[\frac{\tilde{L}_{C_i}}{m}
         - \gamma\!\left(\frac{\tilde{k}_{C_i}}{2m}\right)^{\!2}\right]

where $\tilde{L}_{C_i} = \sum_{(v,w) \in L_{C_i}} 1/(O_v O_w)$ is the
overlap-discounted intra-community edge count and
$\tilde{k}_{C_i} = \sum_{v \in C_i} k_v / O_v$ is the
overlap-discounted degree sum. When the cover is actually a partition
($O_v = 1$ for all $v$), $EQ$ reduces to the standard modularity $Q$.

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

communities : iterable of sets of nodes
    A cover of `G`'s nodes: every node must appear in at least one
    set, but sets may overlap. Accepts the output of any NetworkX
    overlapping community-detection algorithm such as
    :func:`~networkx.algorithms.community.kclique.k_clique_communities`.

weight : string or None, optional (default="weight")
    Edge attribute holding the numerical edge weight. If None, or if
    an edge does not have the attribute, that edge has weight 1.

resolution : float, optional (default=1)
    Resolution parameter $\gamma$. As in standard modularity, values
    smaller than 1 favor larger communities and values greater than
    1 favor smaller communities. The degree of overlap in the cover
    is independent of $\gamma$, since $\gamma$ rescales only the
    null-model term.

Returns
-------
EQ : float
    The extended modularity of the cover.

Raises
------
NetworkXError
    If `communities` is not a valid cover of the nodes of `G` (some
    node in `G` does not belong to any community).

NetworkXNotImplemented
    If `G` is directed. Shen's formulation is for undirected graphs.

Examples
--------
On a partition, EQ equals standard modularity (up to floating-point
summation order):

>>> G = nx.barbell_graph(3, 0)
>>> partition = [{0, 1, 2}, {3, 4, 5}]
>>> Q = nx.community.modularity(G, partition)
>>> EQ = nx.community.overlapping_modularity(G, partition)
>>> abs(Q - EQ) < 1e-12
True

Evaluate an overlapping cover produced by ``k_clique_communities``.
Two K_5 cliques sharing nodes 3 and 4 yield a 2-community cover:

>>> G = nx.complete_graph(5)
>>> G.add_edges_from(nx.complete_graph(range(3, 8)).edges())
>>> cover = list(nx.community.k_clique_communities(G, 4))
>>> round(nx.community.overlapping_modularity(G, cover), 3)
0.158

See Also
--------
modularity
is_cover

References
----------
.. [1] Shen, H., Cheng, X., Cai, K., & Hu, M. B. (2009). "Detect
   overlapping and hierarchical community structure in networks."
   Physica A, 388(8), 1706-1712.
   https://doi.org/10.1016/j.physa.2008.12.021
.. [2] Lancichinetti, A., Fortunato, S., & Kertesz, J. (2009).
   "Detecting the overlapping and hierarchical community structure
   in complex networks." New J. Phys. 11, 033015. (Cover definition.)
z4`communities` is not a valid cover of the nodes of Gr>   g        c           	      óð   <a€ \        V 4      o\        VV3R  lSP                  SS	^R7       4       4      p\        VV3R lS 4       4      p^V,          S,          SVS,          ^,          ,          ,
          # )c              3   óx   <"  € T F/  w  rpVS9   g   K  VSV,          SV,          ,          ,          x € K1  	  R # 5ir'   r   )r*   rB   rC   rD   rE   Ú
memberships   &   €€r   r,   ÚIoverlapping_modularity.<locals>.community_contribution.<locals>.<genexpr>“  s:   øé € ð 
áA‘��bØ�D‰yô 1ˆB�*˜Q•- *¨Q¥-Õ/×0Ò0ÛAùs   ƒ:”&:rH   c              3   óR   <"  € T F  pSV,          SV,          ,          x € K  	  R # 5ir'   r   )r*   rB   ra   ri   s   & €€r   r,   rj   š  s!   øé € Ð@¹4°a˜˜q�	 J¨q¥M×1Ò1»4ùs   ƒ$'rP   )
rS   Ú	L_c_tildeÚ	k_c_tilderE   r   rd   ra   ri   rZ   r;   s
   &  @€€€€€€r   r[   Ú6overlapping_modularity.<locals>.community_contribution�  sk   ù€ Ü�9‹~ˆô
 õ 
àŸG™G D¨v¸q˜GÔAó
ó 
ˆ	ô Õ@¹4Ó@Ó@ˆ	ð �9�}˜wÕ&¨°yÀ7Õ7JÈqÕ6PÕ)PÕPÐPr   c              3   ó4   <"  € T F  pS! V4      x € K  	  R # 5ir'   r   ©r*   Úcr[   s   & €r   r,   Ú)overlapping_modularity.<locals>.<genexpr>Ÿ  ó   øé € Ð>±+¨QÑ% a×(Ð(³+ùó   ƒ)r]   r^   r   r    r!   r   Úintr_   ra   r.   r`   )
r   rc   r;   rZ   rS   Únoder[   rd   ra   ri   s
   f&dd  @@@@r   Úoverlapping_modularityrw     sÃ   þ€ ô` �k¤4×(Ò(Ü˜;Ó'ˆÜ�A�{×#Ò#Ü×ÒÐUÓVÐVô œSÓ!€JÛ ˆ	ÛˆDØ�qŽyØ˜4×  AÕ%Õ ó ñ !ô
 �!—(‘( &�(Ó)Ó*€FÜ�&—-‘-“/Ó"€Gà�!„|Ù÷Qò Qô$ Ô>±+Ó>Ó>Ð>r   c           	     óÚ  aa
€ V P                  4       '       d   ^M^pVS0,
          o
\        V
3R lV P                  VR7       4       4      pV P                  S,          V,          p\        V
3R lV P                  S0VR7       4       4      p	V P                  4       '       d.   V	\        V3R lV P                  S
VR7       4       4      ,          p	W6,          V,          V,          V	,
          # )aµ  
Let P = [A, B, C, D,...] be a partition, and let P' = [A', B', C, D,...]
be the partition obtained by moving the node u from community A to
community B, that is where A' = A\{u} and B = B \union {u}

The overall change in quality associated with this move is

    q_delta = constant_potts_mode(G, P') - constant_potts_model(G, P)

Throughout the algorithm the quality q_delta will be computed by
calculating two intermediate values q_rem and q_add satisfying the
property that

    q_delta = q_rem + q_add

The current function is one of a pair of similar functions
that compute these values with

q_rem = _cpm_delta_partial_eval_remove(G, u, A)
q_add = _cpm_delta_partial_eval_add(G, u, B)

c              3   ó>   <"  € T F  w  rVS9   g   K  Vx € K  	  R # 5ir'   r   )r*   rB   rD   ÚA_primes   &  €r   r,   Ú1_cpm_delta_partial_eval_remove.<locals>.<genexpr>À  s   øé € ÐNÑ$=™5˜1ÀÀgÁ—B’BÓ$=ùrG   ©rI   c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   )r*   Ú_rC   rD   rz   s   &   €r   r,   r{   Ä  s   øé € ÐQÑ$@™˜˜bÈÁL—’Ó$@ùrG   c              3   ó@   <"  € T F  w  rq2S09   g   K  Vx € K  	  R # 5ir'   r   ©r*   r~   rC   rD   rv   s   &   €r   r,   r{   Ç  s    øé € ÐVÑ)F™X˜Q 2ÐPTÈvÉ+—b’bÓ)Fùó   ƒ”
©r2   r.   ÚnodesrR   )r   rv   rS   rZ   r;   Únode_weightÚ
dir_factorÚ	n_A_primeÚu_wtÚE_diffrz   s   &f&&&&    @r   Ú_cpm_delta_partial_eval_remover‰   ¢  s³   ù€ ð4 —m‘m—o’o‘¨1€Jà˜4˜&Õ €GäÔN A§G¡G° GÔ$=ÓNÓN€Ià�7‰7�4�=˜Õ%€DäÔQ A§G¡G¨T¨F¸ GÔ$@ÓQÓQ€Fà‡}�}‡‚Ø”#ÔV¨¯©°¸v¨Ô)FÓVÓVÕVˆàÕ" YÕ.°Õ5¸Õ>Ð>r   c           	     óÆ  aa€ V P                  4       '       d   ^M^p\        V3R lV P                  VR7       4       4      pV P                  S,          V,          p\        V3R lV P                  S0VR7       4       4      p	V P                  4       '       d.   V	\        V3R lV P                  SVR7       4       4      ,          p	W“V,          V,          V,          ,
          # )zk
One of a pair of partial evaluation functions. See

    _cpm_delta_partial_eval_remove

for more details.
c              3   ó>   <"  € T F  w  rVS9   g   K  Vx € K  	  R # 5ir'   r   )r*   rB   rD   rS   s   &  €r   r,   Ú._cpm_delta_partial_eval_add.<locals>.<genexpr>Ø  s   øé € ÐJÑ7‘U�Q¸1À	¹>�bŠbÓ7ùrG   r|   c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   ©r*   r~   rC   rD   rS   s   &   €r   r,   rŒ   Þ  s   øé € ÐPÑ!=‘X�Q˜2ÀiÁ�bŠbÓ!=ùrG   c              3   ó@   <"  € T F  w  rq2S09   g   K  Vx € K  	  R # 5ir'   r   r€   s   &   €r   r,   rŒ   à  s   øé € ÐUÑ&E™(˜! ÈtÈfÉ—2’2Ó&Eùr�   r‚   )
r   rv   rS   rZ   r;   r„   r…   Ún_Br‡   ÚE_Ds
   &ff&&&    r   Ú_cpm_delta_partial_eval_addr’   Ì  s©   ù€ ð —m‘m—o’o‘¨1€Jä
ÔJ˜aŸg™g¨;˜gÔ7ÓJÓ
J€Cð �7‰7�4�=˜Õ%€Dä
ÔP §¡¨$¨°f Ô!=ÓPÓ
P€CØ‡}�}‡‚ØŒsÔU a§g¡g¨i¸f gÔ&EÓUÓUÕUˆà˜jÕ(¨3Õ.°Õ5Õ5Ð5r   c                 óB  a aaa€ \        V V3R  lS 4       4      p\        V V3R lS 4       4      p\        V3R lS P                  SVR7       4       4      p\        V3R lS P                  SVR7       4       4      p	W‰,           V^,          V,          V,          ,
          # )c              3   óX   <"  € T F  pSP                   V,          S,          x € K!  	  R # 5ir'   ©rƒ   ©r*   rB   r   r„   s   & €€r   r,   Ú._quality_delta_cpm_directed.<locals>.<genexpr>è  ó!   øé € Ð?±,¨Q�—‘˜•˜K×(Ò(³,ùó   ƒ'*c              3   óX   <"  € T F  pSP                   V,          S,          x € K!  	  R # 5ir'   r•   r–   s   & €€r   r,   r—   é  ó!   øé € Ð?±Y°�A—G‘G˜A•J˜{×+Ò+³Yùr™   c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   rŽ   s   &   €r   r,   r—   ê  s   øé € ð Ù=‰xˆq�RÀiÁ�ŠÓ=ùrG   r|   c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   )r*   r~   rC   rD   Únodes_to_adds   &   €r   r,   r—   í  s    øé € ð Ù:‰xˆq�RÀ<Ñ>O�ŠÓ:ùrG   ©r.   rR   )
r   rž   rS   rZ   r;   r„   Ún_sizeÚ	comm_sizeÚE_D_outÚE_D_ins
   fff&&f    r   Ú_quality_delta_cpm_directedr¤   å  s�   û€ ô Õ?±,Ó?Ó?€FÜÕ?±YÓ?Ó?€IÜô ØŸ7™7 <°f˜7Ô=óó €Gô ô ØŸ7™7 9°6˜7Ô:óó €Fð Õ˜j¨1�n¨vÕ5¸	ÕAÕAÐAr   c                 óÖ   a aa€ \        V V3R  lV 4       4      p\        V V3R lS 4       4      p\        V3R lS P                  WR7       4       4      pWƒV,          V,          ,
          # )c              3   óX   <"  € T F  pSP                   V,          S,          x € K!  	  R # 5ir'   r•   r–   s   & €€r   r,   Ú0_quality_delta_cpm_undirected.<locals>.<genexpr>ö  r˜   r™   c              3   óX   <"  € T F  pSP                   V,          S,          x € K!  	  R # 5ir'   r•   r–   s   & €€r   r,   r§   ÷  r›   r™   c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   rŽ   s   &   €r   r,   r§   ø  s   øé € ÐVÑ!C‘X�Q˜2ÈIÁ~�bŠbÓ!CùrG   r|   rŸ   )	r   rž   rS   rZ   r;   r„   r    r¡   r‘   s	   f&f&&f   r   Ú_quality_delta_cpm_undirectedrª   ó  sS   ú€ ô Õ?±,Ó?Ó?€FÜÕ?±YÓ?Ó?€IÜ
ÔV §¡¨ Ô!CÓVÓ
V€CØ˜fÕ$ yÕ0Õ0Ð0r   c                óŒ   a aaaa€ S P                  4       pV'       d   V VVV3R loM	V VVV3R lo\        V3R lV 4       4      # )aV
  
Computes the Constant Potts Model, which is a measure of quality of a
partition. This is defined in [1]_ as

.. math::
    Q = \sum_{C \in P} E(C,C) - \gamma n_C^2

Where

E(C,C) is the sum of all edge weights within the community C,
n_C is the sum of the weights of the nodes in C.
\gamma is the resolution parameter. See Notes below for more
more detail on resolution parameter.

The Constant Potts Model is similar to modularity, but overcomes the
so-called resolution limit problem when used in community detection
algorithms like leiden and louvain.

The Constant Potts Model is used by default in the leiden community
detection algorithm

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

communities : list or iterable of sets of nodes
    These node sets must represent a partition of G's nodes.

weight : string or None, optional
    The edge attribute that holds the numerical value used
    as a weight. If None or an edge does not have that attribute,
    then that edge has weight 1.

node_weight : string or None, optional
    The node attribute that holds the numerical value used as
    a weight. If None or an edge does not have tha attribute,
    then the node is treated as having weight 1

resolution : float
    For smaller resolution values, the constant_potts_model will be
    maximised by a partition consisting of larger communities; for
    larger resolution values constant_potts_model will be maximised
    for smaller communities.

Returns
-------
Q : float
    The Constant Potts Model value of the partition.

Notes
-----

The interpretation of the resolution parameter \gamma is explained as
follows in page 3 of [1]_:

    [The Constant Potts Model] tries to maximize the number of
    internal edges while at the same time keeping relatively small
    communities.

    [The resolution, \gamma] balances these two imperatives. In fact,
    the parameter \gamma acts as the inner and outer edge density
    threshold. That is, suppose there is a community [C] with [E(C, C)]
    edges and [n_C] nodes. Then it is better to split it into two
    communities r and s whenever

    .. math::
        \frac{[E(r,s)]}{2 n_r n_s} < \gamma

    where [E(r,s)] is the number [or weighted sum] of links between
    community r and s. This ratio is exactly the density of links between
    community r and s. So, the link density between communities should be
    lower than \gamma, while the link density within communities should
    be higher than \gamma.

References
----------
.. [1] V.A. Traag, P. Van Dooren, Y. Nesterov "Narrow scope for
   resolution-limit-free community detection"
   <https://arxiv.org/abs/1104.3083>
c           	      óÄ   <a€ \        V 4      o\        V3R  lSP                  SS^R7       4       4      p\        VV3R lV  4       4      pVSV^,          ,          ,
          # )c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   rA   s   &   €r   r,   ÚGconstant_potts_model.<locals>.community_contribution.<locals>.<genexpr>X  ó    øé € ð Ù#H‘x�q˜RÐQUÉI—’Ó#HùrG   rH   c              3   ój   <"  € T F(  pSP                   V,          P                  S^4      x € K*  	  R# 5i©é   N©rƒ   Úget©r*   rv   r   r„   s   & €€r   r,   r®   \  ó)   øé € ÐNÁI¸D�a—g‘g˜d•m×'Ñ'¨°Q×7Ð7ÃIùó   ƒ03rP   ©rS   ÚE_cÚn_crE   r   r„   rZ   r;   s   &  @€€€€r   r[   Ú4constant_potts_model.<locals>.community_contributionV  sX   ù€ Ü�y“>ˆDÜô Ø#$§7¡7¨4°fÀa 7Ô#Hóó ˆCô ÕNÁIÓNÓNˆCà˜ s¨A¥vÕ.Õ.Ð.r   c           	      óÒ   <a€ \        V 4      o\        V3R  lSP                  SS^R7       4       4      p\        VV3R lV  4       4      pVSV^,          ,          ^,          ,
          # )c              3   ó>   <"  € T F  w  rq2S9   g   K  Vx € K  	  R # 5ir'   r   rA   s   &   €r   r,   r®   c  r¯   rG   rH   c              3   ój   <"  € T F(  pSP                   V,          P                  S^4      x € K*  	  R# 5ir±   r³   rµ   s   & €€r   r,   r®   g  r¶   r·   rP   r¸   s   &  @€€€€r   r[   r»   a  s]   ù€ Ü�y“>ˆDÜô Ø#$§7¡7¨4°fÀa 7Ô#Hóó ˆCô ÕNÁIÓNÓNˆCà˜ s¨A¥vÕ.°Õ2Õ2Ð2r   c              3   ó4   <"  € T F  pS! V4      x € K  	  R # 5ir'   r   rp   s   & €r   r,   Ú'constant_potts_model.<locals>.<genexpr>k  rs   rt   )r2   r.   )r   rc   r;   r„   rZ   r2   r[   s   f&fff @r   Úconstant_potts_modelrÁ   ü  s;   ü€ ðn —-‘-“/€Kß÷	/ñ 	/÷	3ð 	3ô Ô>±+Ó>Ó>Ð>r   c                ó”  € / p\        V4       F  w  r4V F  pW2V&   K	  	  K  	  V P                  4       '       g=   \        R \        V^4       4       4      pV P	                  4       '       d
   V^,          pM^ p\        V 4      pWw^,
          ,          pV P	                  4       '       g
   V^,          p^ p	Tp
V P                  4        F8  pW+^ ,          ,          W+^,          ,          8X  d   V	^,          p	K/  V
^,          p
K:  	  V	\        V P                  4      ,          pV P                  4       '       d   RpWÍ3# Wš,           V,          pWÍ3# )aØ  Returns the coverage and performance of a partition of G.

The *coverage* of a partition is the ratio of the number of
intra-community edges to the total number of edges in the graph.

The *performance* of a partition is the number of
intra-community edges plus inter-community non-edges divided by the total
number of potential edges.

This algorithm has complexity $O(C^2 + L)$ where C is the number of
communities and L is the number of links.

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

partition : sequence
    Partition of the nodes of `G`, represented as a sequence of
    sets of nodes (blocks). Each block of the partition represents a
    community.

Returns
-------
(float, float)
    The (coverage, performance) tuple of the partition, as defined above.

Raises
------
NetworkXError
    If `partition` is not a valid partition of the nodes of `G`.

Notes
-----
If `G` is a multigraph;
    - for coverage, the multiplicity of edges is counted
    - for performance, the result is -1 (total number of possible edges is not defined)

References
----------
.. [1] Santo Fortunato.
       "Community Detection in Graphs".
       *Physics Reports*, Volume 486, Feb 2010, Issue 3--5 pp. 75--174
       <https://arxiv.org/abs/0906.0612>
c              3   ó\   "  € T F"  w  r\        V4      \        V4      ,          x € K$  	  R # 5ir'   )Úlen)r*   Úp1Úp2s   &  r   r,   Ú$partition_quality.<locals>.<genexpr>¦  s&   é € ð -
Ù,F¡& "ŒC�‹G”c˜"“g×ÒÓ,Fùs   ‚*,g      ð¿)Ú	enumerateÚis_multigraphr.   r   r2   rÄ   rR   )r   r#   Únode_communityÚirS   rv   Úpossible_inter_community_edgesÚnÚtotal_pairsr/   r:   ÚeÚcoverageÚperformances   &&            r   Úpartition_qualityrÒ   n  s8  € ð` €NÜ! )Ö,‰ˆÛˆDØ#$˜4Ó ó ñ -ð
 �?‰?×Òä),ñ -
Ü,8¸ÀAÔ,Fó-
ó *
Ð&ð �=‰=�?Š?Ø*¨aÕ/Ð*øà)*Ð&ô 	ˆA‹€AØ˜1•u•+€KØ�=‰=�?Š?Ø˜ÕˆàÐØ >Ðð �W‰WŽYˆØ˜A�$Õ >°Aµ$Õ#7Ô7Ø! QÕ&Ò!à%¨Õ*Ò%ñ	 ð %¤s¨1¯7©7£|Õ3€Hà‡�×ÒØˆð Ð Ð ð -ÕHÈKÕWˆàÐ Ð r   )rÁ   re   rw   rÒ   )é    r²   )r;   r²   )r;   r„   )r;   r„   r²   )r   Úcollectionsr   Ú	itertoolsr   Únetworkxr    Ú-networkx.algorithms.community.community_utilsr   r   Únetworkx.utils.decoratorsr   r   Ú__all__r!   r	   r$   Úrequire_partitionÚ_dispatchabler/   r7   r:   re   rw   r‰   r’   r¤   rª   rÁ   rÒ   r   r   r   Ú<module>rÜ      s@  ðñõ
 $Ý "ã ß Pß Aò€ô�B×$Ñ$ô òUñD Ð-¨vÓ6Ð ð ×Ññ@ó ð@ð" ×ÑñCó ðCð> ×Ññ>ó ð>ð@ ×Ò˜XÔ&ór9ó 'ðr9ñj �ZÓ Ø×Ò˜XÔ&ðR?°Xð R?È!ó R?ó 'ó !ðR?ôj'?ôT6ô2Bô1ôo?ðd Ø×ÑñW!ó ó òW!r   