
    wg<                         d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
 ddgZ e
d       ej                  d	
      	 dd              Z e
d       ej                  d	
      	 dd              ZddZd Zd Zd Zy)zQFunction for detecting communities based on Louvain Community Detection
Algorithm    N)defaultdictdeque)
modularity)py_random_statelouvain_communitieslouvain_partitionsseedweight)
edge_attrsc                     t        | ||||      }|&|dk  rt        d      t        j                  ||      }t	        |d      }|j                         S )u  Find the best partition of a graph using the Louvain Community Detection
    Algorithm.

    Louvain Community Detection Algorithm is a simple method to extract the community
    structure of a network. This is a heuristic method based on modularity optimization. [1]_

    The algorithm works in 2 steps. On the first step it assigns every node to be
    in its own community and then for each node it tries to find the maximum positive
    modularity gain by moving each node to all of its neighbor communities. If no positive
    gain is achieved the node remains in its original community.

    The modularity gain obtained by moving an isolated node $i$ into a community $C$ can
    easily be calculated by the following formula (combining [1]_ [2]_ and some algebra):

    .. math::
        \Delta Q = \frac{k_{i,in}}{2m} - \gamma\frac{ \Sigma_{tot} \cdot k_i}{2m^2}

    where $m$ is the size of the graph, $k_{i,in}$ is the sum of the weights of the links
    from $i$ to nodes in $C$, $k_i$ is the sum of the weights of the links incident to node $i$,
    $\Sigma_{tot}$ is the sum of the weights of the links incident to nodes in $C$ and $\gamma$
    is the resolution parameter.

    For the directed case the modularity gain can be computed using this formula according to [3]_

    .. math::
        \Delta Q = \frac{k_{i,in}}{m}
        - \gamma\frac{k_i^{out} \cdot\Sigma_{tot}^{in} + k_i^{in} \cdot \Sigma_{tot}^{out}}{m^2}

    where $k_i^{out}$, $k_i^{in}$ are the outer and inner weighted degrees of node $i$ and
    $\Sigma_{tot}^{in}$, $\Sigma_{tot}^{out}$ are the sum of in-going and out-going links incident
    to nodes in $C$.

    The first phase continues until no individual move can improve the modularity.

    The second phase consists in building a new network whose nodes are now the communities
    found in the first phase. To do so, the weights of the links between the new nodes are given by
    the sum of the weight of the links between nodes in the corresponding two communities. Once this
    phase is complete it is possible to reapply the first phase creating bigger communities with
    increased modularity.

    The above two phases are executed until no modularity gain is achieved (or is less than
    the `threshold`, or until `max_levels` is reached).

    Be careful with self-loops in the input graph. These are treated as
    previously reduced communities -- as if the process had been started
    in the middle of the algorithm. Large self-loop edge weights thus
    represent strong communities and in practice may be hard to add
    other nodes to.  If your input graph edge weights for self-loops
    do not represent already reduced communities you may want to remove
    the self-loops before inputting that graph.

    Parameters
    ----------
    G : NetworkX graph
    weight : string or None, optional (default="weight")
        The name of an edge attribute that holds the numerical value
        used as a weight. If None then each edge has weight 1.
    resolution : float, optional (default=1)
        If resolution is less than 1, the algorithm favors larger communities.
        Greater than 1 favors smaller communities
    threshold : float, optional (default=0.0000001)
        Modularity gain threshold for each level. If the gain of modularity
        between 2 levels of the algorithm is less than the given threshold
        then the algorithm stops and returns the resulting communities.
    max_level : int or None, optional (default=None)
        The maximum number of levels (steps of the algorithm) to compute.
        Must be a positive integer or None. If None, then there is no max
        level and the threshold parameter determines the stopping condition.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    list
        A list of sets (partition of `G`). Each set represents one community and contains
        all the nodes that constitute it.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.petersen_graph()
    >>> nx.community.louvain_communities(G, seed=123)
    [{0, 4, 5, 7, 9}, {1, 2, 3, 6, 8}]

    Notes
    -----
    The order in which the nodes are considered can affect the final output. In the algorithm
    the ordering happens using a random shuffle.

    References
    ----------
    .. [1] 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
    .. [2] Traag, V.A., Waltman, L. & van Eck, N.J. From Louvain to Leiden: guaranteeing
       well-connected communities. Sci Rep 9, 5233 (2019). https://doi.org/10.1038/s41598-019-41695-z
    .. [3] Nicolas Dugué, Anthony Perez. Directed Louvain : maximizing modularity in directed networks.
        [Research Report] Université d’Orléans. 2015. hal-01231784. https://hal.archives-ouvertes.fr/hal-01231784

    See Also
    --------
    louvain_partitions
    r   z5max_level argument must be a positive integer or None   )maxlen)r   
ValueError	itertoolsislicer   pop)Gr
   
resolution	threshold	max_levelr	   
partitionsfinal_partitions           j/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/community/louvain.pyr   r      s]    Z $Avz9dKJ>TUU%%j)<
Jq1O      c              #     K   | j                         D cg c]  }|h }}t        j                  |       r| yt        | |||      }| j	                         }| j                         rt        | ||      }	nC| j                         }	|	j                  |        |	j                  | j                  |d             |	j                  d      }
t        |	|
||||      \  }}}d}|r^|D cg c]  }|j                          c} t        |	||d      }||z
  |k  ry|}t        |	|      }	t        |	|
||||      \  }}}|r]yyc c}w c c}w w)aw	  Yields partitions for each level of the Louvain Community Detection Algorithm

    Louvain Community Detection Algorithm is a simple method to extract the community
    structure of a network. This is a heuristic method based on modularity optimization. [1]_

    The partitions at each level (step of the algorithm) form a dendrogram of communities.
    A dendrogram is a diagram representing a tree and each level represents
    a partition of the G graph. The top level contains the smallest communities
    and as you traverse to the bottom of the tree the communities get bigger
    and the overall modularity increases making the partition better.

    Each level is generated by executing the two phases of the Louvain Community
    Detection Algorithm.

    Be careful with self-loops in the input graph. These are treated as
    previously reduced communities -- as if the process had been started
    in the middle of the algorithm. Large self-loop edge weights thus
    represent strong communities and in practice may be hard to add
    other nodes to.  If your input graph edge weights for self-loops
    do not represent already reduced communities you may want to remove
    the self-loops before inputting that graph.

    Parameters
    ----------
    G : NetworkX graph
    weight : string or None, optional (default="weight")
     The name of an edge attribute that holds the numerical value
     used as a weight. If None then each edge has weight 1.
    resolution : float, optional (default=1)
        If resolution is less than 1, the algorithm favors larger communities.
        Greater than 1 favors smaller communities
    threshold : float, optional (default=0.0000001)
     Modularity gain threshold for each level. If the gain of modularity
     between 2 levels of the algorithm is less than the given threshold
     then the algorithm stops and returns the resulting communities.
    seed : integer, random_state, or None (default)
     Indicator of random number generation state.
     See :ref:`Randomness<randomness>`.

    Yields
    ------
    list
        A list of sets (partition of `G`). Each set represents one community and contains
        all the nodes that constitute it.

    References
    ----------
    .. [1] Blondel, V.D. et al. Fast unfolding of communities in
       large networks. J. Stat. Mech 10008, 1-12(2008)

    See Also
    --------
    louvain_communities
    N)r   r
   r   datadefaultr
   r
   T)nodesnxis_emptyr   is_directedis_multigraph_convert_multigraph	__class__add_nodes_fromadd_weighted_edges_fromedgessize
_one_levelcopy
_gen_graph)r   r
   r   r   r	   u	partitionmodr#   graphminner_partitionimprovementsnew_mods                  r   r   r      sO    x  ggi(!(I(	{{1~
Q	j
HC--/K#Av{;Q%%agg61g&EF

(
#A.8q)Zd/+I K
!*+Aqvvx++?z(
 S=I%5/22<1i[$3
/	?K % )( ,s#   E
EC
E)E	 AEEc                    t        | j                               D ci c]  \  }}||
 }}}| j                         D cg c]  }|h }	}|rt        | j                  d            }
t        | j	                  d            }t        |
j                               }t        |j                               }i }| D ]x  }t        t              ||<   | j                  |d      D ]  \  }}}||k7  s||   |xx   |z  cc<    | j                  |d      D ]  \  }}}||k7  s||   |xx   |z  cc<    z nvt        | j                  d            }t        |j                               }| D ci c]3  }|| |   j                         D ci c]  \  }}||k7  s||d    c}}5 }}}}t        | j                        }|j                  |       d}d}|dkD  rd}|D ]  }d}||   }t        ||   |      }|rI
|   }|   }|xx   |z  cc<   |xx   |z  cc<   ||    |z  ||||   z  |||   z  z   z  |dz  z  z   }n0|   }|xx   |z  cc<   ||    |z  |||   |z  z  d|dz  z  z  z   }|j                         D ]R  \  } }|r$|||z  z   ||    z  |    z  z   z  |dz  z  z
  }!n|||z  z   ||    z  z  d|dz  z  z  z
  }!|!|kD  sO|!}| }T |r|xx   z  cc<   |xx   z  cc<   n|xx   z  cc<   |||   k7  s.| j                  |   j                  d|h      }"|||      j!                  |"       |	||      j#                  |       ||   j%                  |"       |	|   j'                  |       d	}|dz  }|||<    |dkD  rt        t)        t*        |            }t        t)        t*        |	            }	||	|fS c c}}w c c}w c c}}w c c}}}w )
an  Calculate one level of the Louvain partitions tree

    Parameters
    ----------
    G : NetworkX Graph/DiGraph
        The graph from which to detect communities
    m : number
        The size of the graph `G`.
    partition : list of sets of nodes
        A valid partition of the graph `G`
    resolution : positive number
        The resolution parameter for computing the modularity of a partition
    is_directed : bool
        True if `G` is a directed graph.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    r
   r   r   r   Fr      r    T)	enumerater    dict	in_degree
out_degreelistvaluesr   float	out_edgesin_edgesdegreeitemsshuffle_neighbor_weightsgetdifference_updateremoveupdateaddfilterlen)#r   r2   r/   r   r#   r	   ir.   node2comr3   
in_degreesout_degreesStot_inStot_outnbrs_nwtdegreesStotvr   
rand_nodesnb_movesr4   best_modbest_comweights2comr<   r=   remove_costrC   nbr_comgaincoms#                                      r   r+   r+      s   ( "+1779!56A16H6$%GGI.qs.O.!++X+67
1<<x<89z((*+**,- 	%A!%(DGKKK9 %1b6GAJ"$J% JJqxJ8 %1b6GAJ"$J%	% qxxxx01GNN$%VWXXQRQqTZZ\L'!TQ!VAtH~%LLXXaggJLLHK
Q, 7	'AH{H+DGX>K&qM	(^
!Y.!"j0" **Q. !GH$55	HXDV8VVXd  !X&(*844q8:NV+<AX<   +002 '#q&!$&)99'(7*;;<
 Q$  $q&!$W(>?1q!t8LM 
 (?#H&H)'* !Y.!"j0"X&(8A;&ggajnnWqc2(1+&88=,33A6(#**3/)--a0"A&o7	' Q,t VC+,I6#78Oo{22k 7.& MXs(   O
O/O!OO!O!O!c                 t    t        t              }| j                         D ]  \  }}|||   xx   |z  cc<    |S )a>  Calculate weights between node and its neighbor communities.

    Parameters
    ----------
    nbrs : dictionary
           Dictionary with nodes' neighbors as keys and their edge weight as value.
    node2com : dictionary
           Dictionary with all graph's nodes as keys and their community index as value.

    )r   r@   rD   )rT   rO   weightsnbrrW   s        r   rF   rF   M  s@     % G::< %R"$%Nr   c                    | j                         }i }t        |      D ]]  \  }}t               }|D ]6  }|||<   |j                  | j                  |   j                  d|h             8 |j                  ||       _ | j                  d      D ]D  \  }}	}
|
d   }
||   }||	   }|j                  ||ddi      d   }|j                  |||
|z          F |S )z=Generate a new graph based on the partitions of a given graphr    )r    Tr8   r
   r   r   )
r&   r:   setrJ   r    rG   add_noder)   get_edge_dataadd_edge)r   r/   HrO   rN   partr    nodenode1node2rW   com1com2temps                 r   r-   r-   ^  s    	AHY' #4 	=DHTNLL**7TF;<	= 	


1E
"# GGG. 1ub\tTHa=9(C	

4b4i
01 Hr   c                 *   |rt        j                         }nt        j                         }|j                  |        | j	                  |d      D ]@  \  }}}|j                  ||      r||   |   dxx   |z  cc<   -|j                  |||       B |S )z$Convert a Multigraph to normal Graphr   r   r
   r   )r!   DiGraphGraphr'   r)   has_edgerk   )r   r
   r#   rl   r.   rZ   rW   s          r   r%   r%   r  s    JJLHHJQGGG3 (1b::aaDGH#JJq!BJ'	(
 Hr   )r
   r   Hz>NN)r
   r   rx   N)r   FN)__doc__r   collectionsr   r   networkxr!   networkx.algorithms.communityr   networkx.utilsr   __all___dispatchabler   r   r+   rF   r-   r%    r   r   <module>r      s     *  4 * "6
7 X&PTq! ' q!h X&@DX
 ' X
vi3X"(r   