
    h/                         S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
JrJr  S/r\" S5      \" S	5      \" S
5      \R                  SS j5       5       5       5       rg)zAAsynchronous Fluid Communities algorithm for community detection.    )CounterN)is_connected)NetworkXError)groupsnot_implemented_forpy_random_stateasyn_fluidcdirected
multigraph   c                 0   [        U[        5      (       d  [        S5      eUS:  d  [        S5      e[        U 5      (       d  [        S5      e[	        U 5      U:  a  [        S5      eSn[        U 5      nUR                  U5        [        USU 5       VVs0 s H  u  pgXv_M	     nnn0 n	0 n
U H  nSXU   '   XIX   '   M     SnS	nU(       GaZ  S
nUS-  n[        U 5      nUR                  U5        U GH   n[        5       n UR                  X   XU      05        X    H  n UR                  X   XU      05        M      Sn[	        UR                  5       5      S:  d  Mp  [        UR                  5       5      nUR                  5        VVs/ s H  u  nnUU-
  S:  d  M  UPM     nnn X   U;   a  X   nUS:X  d  M  S	nUR                  U5      n XU   ==   S-  ss'   XJX      -  XU   '   UX'   XU   ==   S-  ss'   XJX      -  XU   '   GM#     X:  a  O
U(       a  GMZ  [!        [#        U5      R                  5       5      $ s  snnf ! [         a     GNAf = f! [         a     GMO  f = fs  snnf ! [         a     Nf = f! [         a     Nf = f)u[  Returns communities in `G` as detected by Fluid Communities algorithm.

The asynchronous fluid communities algorithm is described in
[1]_. The algorithm is based on the simple idea of fluids interacting
in an environment, expanding and pushing each other. Its initialization is
random, so found communities may vary on different executions.

The algorithm proceeds as follows. First each of the initial k communities
is initialized in a random vertex in the graph. Then the algorithm iterates
over all vertices in a random order, updating the community of each vertex
based on its own community and the communities of its neighbors. This
process is performed several times until convergence.
At all times, each community has a total density of 1, which is equally
distributed among the vertices it contains. If a vertex changes of
community, vertex densities of affected communities are adjusted
immediately. When a complete iteration over all vertices is done, such that
no vertex changes the community it belongs to, the algorithm has converged
and returns.

This is the original version of the algorithm described in [1]_.
Unfortunately, it does not support weighted graphs yet.

Parameters
----------
G : NetworkX graph
    Graph must be simple and undirected.

k : integer
    The number of communities to be found.

max_iter : integer
    The number of maximum iterations allowed. By default 100.

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
-----
k variable is not an optional argument.

References
----------
.. [1] Parés F., Garcia-Gasulla D. et al. "Fluid Communities: A
   Competitive and Highly Scalable Community Detection Algorithm".
   [https://arxiv.org/pdf/1703.09307.pdf].
zk must be an integer.r   zk must be greater than 0.z+Fluid Communities require connected Graphs.z,k cannot be bigger than the number of nodes.g      ?N   TFg-C6?)
isinstanceintr   r   lenlistshuffle	enumerater   updateKeyErrorkeysmaxvaluesitemschoiceiterr   )Gkmax_iterseedmax_densityverticesincommunitiesdensitycom_to_numverticesvertex
iter_countcontcom_countervnew_commax_freqcomfreqbest_communitiess                        Z/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/community/asyn_fluid.pyr	   r	      s   t a344q5788??IJJ
1vzJKKKAwHLL$-hrl$;<$;DA14$;K<G23v./'2#$  JD
a
7XF!)K""K$7VAT9U#VW Y&&A8O'PQ  G;##%&*{1134 &1%6%6%8$%8	T 461 %8 ! $"*.>>"-"5 b=D"kk*:;G*v+>?1D?'[=P*QQ  F 34 +2K'&6':;q@;#9L&MM /0] d  s $v {#**,--I =*     $    $ sZ   I	I#I I2"I2,I8J
II 
I/.I/8
JJ
JJ)d   N)__doc__collectionsr   networkxnxnetworkx.algorithms.componentsr   networkx.exceptionr   networkx.utilsr   r   r   __all___dispatchabler	        r3   <module>r@      sd    G   7 , G G/ Z \"F.   # !F.r?   